37std::string
tkn::wrap_text(
const tkstring &_text,
size_t _first_content_col,
size_t _continuation_col,
size_t _max_line_width)
39 const size_t min_content_width = 20;
40 const size_t first_avail = _first_content_col < _max_line_width ? _max_line_width - _first_content_col : min_content_width;
41 const size_t continuation_avail = _continuation_col < _max_line_width ? _max_line_width - _continuation_col : min_content_width;
42 const std::string continuation_indent(_continuation_col,
' ');
44 auto wrap_paragraph = [&](
const std::string &_paragraph,
size_t _first_avail) {
47 bool first_chunk =
true;
48 while (pos < _paragraph.size()) {
49 if (!first_chunk) result +=
"\n" + continuation_indent;
50 const size_t content_avail = first_chunk ? _first_avail : continuation_avail;
51 if (_paragraph.size() - pos <= content_avail) {
52 result += _paragraph.substr(pos);
55 size_t wrap_at = _paragraph.rfind(
' ', pos + content_avail);
56 if (wrap_at == std::string::npos || wrap_at <= pos) {
57 result += _paragraph.substr(pos, content_avail);
60 result += _paragraph.substr(pos, wrap_at - pos);
62 while (pos < _paragraph.size() && _paragraph[pos] ==
' ') pos++;
71 bool first_para =
true;
72 for (
auto ¶ : paragraphs) {
73 if (!first_para) wrapped +=
"\n" + continuation_indent;
74 wrapped += wrap_paragraph(para, first_para ? first_avail : continuation_avail);
82 std::transform(begin(), end(), begin(),[](
unsigned char _c){
return std::tolower(_c); });
88 std::transform(begin(), end(), begin(),[](
unsigned char _c){
return std::toupper(_c); });
104 const char* cp = data();
105 size_t len = length();
106 if (len == 0)
return false;
108 for (
size_t i = 0; i < len; ++i) {
109 if (cp[i] !=
' ' && !isdigit(cp[i]))
return false;
110 if (cp[i] ==
' ') b++;
111 if (isdigit(cp[i])) d++;
139 int i_dot, i_e, i_plus, i_minus, i_comma;
141 i_dot = tmp.
index(
".");
142 if (i_dot > -1) tmp.replace(i_dot, 1,
" ", 1);
143 i_comma = tmp.
index(
",");
144 if (i_comma > -1) tmp.replace(i_comma, 1,
" ", 1);
145 i_e = tmp.
index(
"e");
147 tmp.replace(i_e, 1,
" ", 1);
150 i_e = tmp.
index(
"E");
151 if (i_e > -1) tmp.replace(i_e, 1,
" ", 1);
153 i_plus = tmp.
index(
"+");
154 if (i_plus > -1) tmp.replace(i_plus, 1,
" ", 1);
155 i_minus = tmp.
index(
"-");
156 if (i_minus > -1) tmp.replace(i_minus, 1,
" ", 1);
198 int end =
index(
" ");
201 return std::atoi(data());
205 return std::atoi(tmp.data());
215 int comma =
index(
",");
216 int end =
index(
" ");
218 if (comma == -1 && end == -1)
219 return std::atof(data());
223 tmp.replace(comma, 1,
".");
227 return std::atof(tmp.data());
231 return std::atof(tmp.data());
247 return tmp_copy.find(test,_pos);
258 return strcmp(_s, data()) == 0;
259 return strcasecmp(_s, data()) == 0;
264 if (!_s)
return true;
266 size_t l = strlen(_s);
267 if (l > length())
return false;
268 const char *s2 = data() + length() - l;
271 return strcmp(_s, s2) == 0;
272 return strcasecmp(_s, s2) == 0;
277 std::vector<tkstring> tokens;
278 std::list<int> splitIndex;
280 size_t i, start, nrDiff = 0;
282 for (i = 0; i < _delim.length(); i++) {
284 while (start < length()) {
285 size_t pos = find(_delim.at(i), start);
286 if (pos == npos)
break;
287 splitIndex.push_back(pos);
290 if (start > 0) nrDiff++;
292 splitIndex.push_back(length());
298 std::list<int>::const_iterator it;
300 for (it = splitIndex.begin(); it != splitIndex.end(); ++it) {
302 for (it = splitIndex.begin(); it != (std::list<int>::const_iterator) splitIndex.end(); ++it) {
305 if (stop - 1 >= start + 1) {
307 if(tok.length()) tokens.push_back(tok);
316 std::vector<tkstring> tokens;
317 size_t start = 0, pos = 0;
320 while ((pos = find(_delim, start)) != npos) {
322 if (token.length()) {
323 tokens.push_back(token);
326 start = pos + _delim.length();
331 if (token.length()) {
332 tokens.push_back(token);
340 if (_s1 && _ls1 > 0) {
342 while ((pos = find(_s1,pos,_ls1)) != npos) {
343 replace(pos, _ls1, _s2, _ls2);
352 std::size_t found = find_last_of(_s1);
360 std::size_t found = find_last_of(_s1);
369 static size_t buffer_size;
372 va_start(argptr, _format);
373 size_t length = vsnprintf(buffer, buffer_size, _format, argptr);
376 if (length + 1 > buffer_size) {
377 buffer_size = length + 1;
378 char *tmp =
static_cast<char*
>(realloc(buffer, buffer_size));
379 if(tmp) buffer = tmp;
381 va_start(argptr, _format);
382 vsnprintf(buffer, buffer_size, _format, argptr);
399 for(
size_t i=0 ; i<length() ; i++) {
400 if ((*
this)[i] >=
'a' && (*
this)[i] <=
'z') {
401 (*this)[i] -= (
'a' -
'A');
412 const char *cp = data();
413 size_t len = length();
415 for (
size_t i = 0; i < len; ++i)
426 const char *cp = data();
427 size_t len = length();
429 for (
size_t i = 0; i < len; ++i)
443 const char *cp = data();
444 size_t len = length();
445 if (len == 0)
return false;
446 for (
size_t i = 0; i < len; ++i)
455 static size_t buffer_size;
458 va_start(argptr, _format);
459 size_t length = vsnprintf(buffer, buffer_size, _format, argptr);
462 if (length + 1 > buffer_size) {
463 buffer_size = length + 1;
464 char *tmp =
static_cast<char*
>(realloc(buffer, buffer_size));
465 if(tmp) buffer = tmp;
467 va_start(argptr, _format);
468 vsnprintf(buffer, buffer_size, _format, argptr);
483 tkstring sy_dec, sy_exp, sey_dec, sey_exp;
484 double y_dec, ey_dec;
488 std::vector<tkstring> loa_y = sy.
tokenize(
"e");
489 sy_dec = loa_y.front();
490 sy_exp = loa_y.back();
492 y_dec = sy_dec.
atof();
493 y_exp = sy_exp.
atoi();
496 std::vector<tkstring> loa_ey = sey.
tokenize(
"e");
498 sey_dec = loa_ey.front();
499 sey_exp = loa_ey.back();
501 ey_dec = sey_dec.
atof();
502 ey_exp = sey_exp.
atoi();
504 double err = ey_dec * pow(10., ey_exp - y_exp);
510 if (y_exp == ey_exp) s =
Form(
"%1.2g.0(%g.0).10$^{%d}$", y_dec, ey_dec, y_exp);
511 else s =
Form(
"%1.3g.0(%g.0).10$^{%d}$", y_dec, err, y_exp);
513 if (y_exp == ey_exp) s =
Form(
"%1.2g.0(%g0).10$^{%d}$", y_dec, ey_dec, y_exp);
514 else s =
Form(
"%1.3g.0(%g0).10$^{%d}$", y_dec, err, y_exp);
516 if (y_exp == ey_exp) s =
Form(
"%1.2g.0(%g).10$^{%d}$", y_dec, ey_dec, y_exp);
517 else s =
Form(
"%1.3g.0(%g).10$^{%d}$", y_dec, err, y_exp);
519 }
else if (
Form(
"%1.3g", y_dec) ==
Form(
"%1.2g", y_dec) &&
Form(
"%1.2g", y_dec).
contains(
".") && err < 1) {
521 if (y_exp == ey_exp) s =
Form(
"%1.2g0(%g.0).10$^{%d}$", y_dec, ey_dec, y_exp);
522 else s =
Form(
"%1.3g0(%g.0).10$^{%d}$", y_dec, err, y_exp);
524 if (y_exp == ey_exp) s =
Form(
"%1.2g0(%g0).10$^{%d}$", y_dec, ey_dec, y_exp);
525 else s =
Form(
"%1.3g0(%g0).10$^{%d}$", y_dec, err, y_exp);
527 if (y_exp == ey_exp) s =
Form(
"%1.2g0(%g).10$^{%d}$", y_dec, ey_dec, y_exp);
528 else s =
Form(
"%1.3g0(%g).10$^{%d}$", y_dec, err, y_exp);
531 if (y_exp == ey_exp) s =
Form(
"%1.2g(%g.0).10$^{%d}$", y_dec, ey_dec, y_exp);
532 else s =
Form(
"%1.3g(%g.0).10$^{%d}$", y_dec, err, y_exp);
534 if (y_exp == ey_exp) s =
Form(
"%1.2g(%g0).10$^{%d}$", y_dec, ey_dec, y_exp);
535 else s =
Form(
"%1.3g(%g0).10$^{%d}$", y_dec, err, y_exp);
537 if (y_exp == ey_exp) s =
Form(
"%1.2g(%g).10$^{%d}$", y_dec, ey_dec, y_exp);
538 else s =
Form(
"%1.3g(%g).10$^{%d}$", y_dec, err, y_exp);;
556 for(
const auto &s: tmp.
tokenize(
" \n\t")) {
557 if (tmp2.length()) tmp2 +=
" ";
573 while ((pos = find(_st,pos)) != npos) {
590 for(
const auto &s: tmp.
tokenize(
" \n\t")) {
591 if (tmp2.length()) tmp2 +=
" ";
615 if (pat ==
"*")
return true;
617 std::vector<tkstring> tok = pat.
tokenize(
"*");
618 int n_tok = tok.size();
628 int idx = 0, num = 0;
629 for (
int ii = 0; ii < n_tok; ii += 1) {
630 idx =
index(tok.at(ii), idx);
636 return (num == n_tok);
646 if(_skip_white) getline(_strm >> std::ws, *
this);
647 else getline(_strm, *
this);
654 if(
error.is_empty())
return -1.;
672 double precision = 1.0;
int expo;
size_t l1,l2;
677 if ( l1 == std::string::npos ) {
678 if ( l2 == std::string::npos )
683 precision = pow(10.,expo);
686 if ( l2 == std::string::npos ) {
687 expo = - (st.size() - l1 - 1);
688 precision = pow(10.0,expo);
693 expo = - (l2 - l1 - 1) + expo;
694 precision = pow(10.0,expo);
697 if ( precision < 0 ) precision = -1.0 * precision;
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
tkstring extract_alpha()
Returns a tkstring composed only of the alphabetic letters of the original tkstring.
tkstring strip_all_extra_white_space() const
tkstring copy() const
Returns a copy of this string.
tkstring & to_lower()
Change all letters to lower case.
static const char * form(const char *_format,...)
bool is_float() const
Checks if string contains a floating point or integer number.
tkstring get_last_occurence(const char *_s1)
std::vector< tkstring > tokenize(const tkstring &_delim=" ") const
Create a vector of string separated by at least one delimiter.
static tkstring Form(const char *_format,...)
tkstring substr(size_type __pos=0, size_type __n=npos) const
Inlines.
std::vector< tkstring > tokenize_from_string(const tkstring &_delim) const
Create a vector of string separated by a full string as delimiter.
bool match(const char *_pattern) const
bool is_alpha() const
Checks whether tkstring is only composed of alphabetic letters.
std::istream & read_line(std::istream &_strm, bool _skip_white=true)
tkstring::read_line
int atoi() const
Converts a string to integer value.
bool ends_with(const char *_s, ECaseCompare _cmp=kExact) const
static double get_absolute_error(tkstring val, tkstring error)
Get absolute uncertainty from value and error strings (1.27 4 -> 0.04), returns -1 in case of empty e...
bool equal_to(const char *_s, ECaseCompare _cmp=kExact) const
Returns true if the string and _s are identical.
size_t index(const char *_s, size_t _pos=0, ECaseCompare _cmp=kExact) const
Returns the index of the substring _s.
tkstring remove_alpha()
Returns a tkstring composed only of the non alphabetic letters of the original tkstring.
tkstring & remove_all_extra_white_space()
bool contains(const char *_pat, ECaseCompare _cmp=kExact) const
int count_string(const tkstring &_st) const
bool begins_with(const char *_s, ECaseCompare _cmp=kExact) const
tkstring & capitalize()
Change first letter of string from lower to upper case.
static double get_precision(tkstring _st)
Extract the precision for a given ENSDF data.
tkstring & replace_all(const tkstring &_s1, const tkstring &_s2)
bool is_digit() const
Checks if all characters in string are digits (0-9) or whitespaces.
tkstring remove_last_occurence(const char *_s1)
double atof() const
Converts a string to double value.
tkstring & to_upper()
Change all letters to upper case.
std::string wrap_text(const tkstring &_text, size_t _first_content_col, size_t _continuation_col, size_t _max_line_width=80)
tklog & error(tklog &log)