/
string_utils.cpp
860 lines (754 loc) · 21.9 KB
/
string_utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
/*
Copyright (C) 2003 by David White <dave@whitevine.net>
Copyright (C) 2005 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
Copyright (C) 2005 - 2018 by Philippe Plantier <ayin@anathas.org>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* Various string-routines.
*/
#include "gettext.hpp"
#include "log.hpp"
#include "serialization/string_utils.hpp"
#include "serialization/unicode.hpp"
#include "utils/general.hpp"
#include <cassert>
#include <array>
#include <stdexcept>
#include <boost/algorithm/string.hpp>
static lg::log_domain log_engine("engine");
#define ERR_GENERAL LOG_STREAM(err, lg::general())
#define ERR_NG LOG_STREAM(err, log_engine)
namespace utils {
bool isnewline(const char c)
{
return c == '\r' || c == '\n';
}
// Make sure that we can use Mac, DOS, or Unix style text files on any system
// and they will work, by making sure the definition of whitespace is consistent
bool portable_isspace(const char c)
{
// returns true only on ASCII spaces
if (static_cast<unsigned char>(c) >= 128)
return false;
return isnewline(c) || isspace(static_cast<unsigned char>(c));
}
// Make sure we regard '\r' and '\n' as a space, since Mac, Unix, and DOS
// all consider these differently.
bool notspace(const char c)
{
return !portable_isspace(c);
}
/**
* Splits a (comma-)separated string into a vector of pieces.
* @param[in] val A (comma-)separated string.
* @param[in] c The separator character (usually a comma).
* @param[in] flags Flags controlling how the split is done.
* This is a bit field with two settings (both on by default):
* REMOVE_EMPTY causes empty pieces to be skipped/removed.
* STRIP_SPACES causes the leading and trailing spaces of each piece to be ignored/stripped.
*
* Basic method taken from http://stackoverflow.com/a/236803
*/
std::vector<std::string> split(const std::string& val, const char c, const int flags)
{
std::vector<std::string> res;
std::stringstream ss;
ss.str(val);
std::string item;
while(std::getline(ss, item, c)) {
if(flags & STRIP_SPACES) {
boost::trim(item);
}
if(!(flags & REMOVE_EMPTY) || !item.empty()) {
res.push_back(std::move(item));
}
}
return res;
}
std::vector<std::string> square_parenthetical_split(const std::string& val,
const char separator, const std::string& left,
const std::string& right,const int flags)
{
std::vector< std::string > res;
std::vector<char> part;
bool in_parenthesis = false;
std::vector<std::string::const_iterator> square_left;
std::vector<std::string::const_iterator> square_right;
std::vector< std::string > square_expansion;
std::string lp=left;
std::string rp=right;
std::string::const_iterator i1 = val.begin();
std::string::const_iterator i2;
std::string::const_iterator j1;
if (flags & STRIP_SPACES) {
while (i1 != val.end() && portable_isspace(*i1))
++i1;
}
i2=i1;
j1=i1;
if (i1 == val.end()) return res;
if (!separator) {
ERR_GENERAL << "Separator must be specified for square bracket split function." << std::endl;
return res;
}
if(left.size()!=right.size()){
ERR_GENERAL << "Left and Right Parenthesis lists not same length" << std::endl;
return res;
}
while (true) {
if(i2 == val.end() || (!in_parenthesis && *i2 == separator)) {
//push back square contents
std::size_t size_square_exp = 0;
for (std::size_t i=0; i < square_left.size(); i++) {
std::string tmp_val(square_left[i]+1,square_right[i]);
std::vector< std::string > tmp = split(tmp_val);
for(const std::string& piece : tmp) {
std::size_t found_tilde = piece.find_first_of('~');
if (found_tilde == std::string::npos) {
std::size_t found_asterisk = piece.find_first_of('*');
if (found_asterisk == std::string::npos) {
std::string tmp2(piece);
boost::trim(tmp2);
square_expansion.push_back(tmp2);
}
else { //'*' multiple expansion
std::string s_begin = piece.substr(0,found_asterisk);
boost::trim(s_begin);
std::string s_end = piece.substr(found_asterisk+1);
boost::trim(s_end);
for (int ast=std::stoi(s_end); ast>0; --ast)
square_expansion.push_back(s_begin);
}
}
else { //expand number range
std::string s_begin = piece.substr(0,found_tilde);
boost::trim(s_begin);
int begin = std::stoi(s_begin);
std::size_t padding = 0, padding_end = 0;
while (padding<s_begin.size() && s_begin[padding]=='0') {
padding++;
}
std::string s_end = piece.substr(found_tilde+1);
boost::trim(s_end);
int end = std::stoi(s_end);
while (padding_end<s_end.size() && s_end[padding_end]=='0') {
padding_end++;
}
if (padding*padding_end > 0 && s_begin.size() != s_end.size()) {
ERR_GENERAL << "Square bracket padding sizes not matching: "
<< s_begin << " and " << s_end <<".\n";
}
if (padding_end > padding) padding = padding_end;
int increment = (end >= begin ? 1 : -1);
end+=increment; //include end in expansion
for (int k=begin; k!=end; k+=increment) {
std::string pb = std::to_string(k);
for (std::size_t p=pb.size(); p<=padding; p++)
pb = std::string("0") + pb;
square_expansion.push_back(pb);
}
}
}
if (i*square_expansion.size() != (i+1)*size_square_exp ) {
std::string tmp2(i1, i2);
ERR_GENERAL << "Square bracket lengths do not match up: " << tmp2 << std::endl;
return res;
}
size_square_exp = square_expansion.size();
}
//combine square contents and rest of string for comma zone block
std::size_t j = 0;
std::size_t j_max = 0;
if (!square_left.empty())
j_max = square_expansion.size() / square_left.size();
do {
j1 = i1;
std::string new_val;
for (std::size_t i=0; i < square_left.size(); i++) {
std::string tmp_val(j1, square_left[i]);
new_val.append(tmp_val);
std::size_t k = j+i*j_max;
if (k < square_expansion.size())
new_val.append(square_expansion[k]);
j1 = square_right[i]+1;
}
std::string tmp_val(j1, i2);
new_val.append(tmp_val);
if (flags & STRIP_SPACES)
boost::trim_right(new_val);
if (!(flags & REMOVE_EMPTY) || !new_val.empty())
res.push_back(new_val);
j++;
} while (j<j_max);
if (i2 == val.end()) //escape loop
break;
++i2;
if (flags & STRIP_SPACES) { //strip leading spaces
while (i2 != val.end() && portable_isspace(*i2))
++i2;
}
i1=i2;
square_left.clear();
square_right.clear();
square_expansion.clear();
continue;
}
if(!part.empty() && *i2 == part.back()) {
part.pop_back();
if (*i2 == ']') square_right.push_back(i2);
if (part.empty())
in_parenthesis = false;
++i2;
continue;
}
bool found=false;
for(std::size_t i=0; i < lp.size(); i++) {
if (*i2 == lp[i]){
if (*i2 == '[')
square_left.push_back(i2);
++i2;
part.push_back(rp[i]);
found=true;
break;
}
}
if(!found){
++i2;
} else
in_parenthesis = true;
}
if(!part.empty()){
ERR_GENERAL << "Mismatched parenthesis:\n"<<val<< std::endl;
}
return res;
}
std::map<std::string, std::string> map_split(
const std::string& val
, char major
, char minor
, int flags
, const std::string& default_value)
{
//first split by major so that we get a vector with the key-value pairs
std::vector< std::string > v = split(val, major, flags);
//now split by minor to extract keys and values
std::map< std::string, std::string > res;
for( std::vector< std::string >::iterator i = v.begin(); i != v.end(); ++i) {
std::size_t pos = i->find_first_of(minor);
std::string key, value;
if(pos == std::string::npos) {
key = (*i);
value = default_value;
} else {
key = i->substr(0, pos);
value = i->substr(pos + 1);
}
res[key] = value;
}
return res;
}
std::vector<std::string> parenthetical_split(const std::string& val,
const char separator, const std::string& left,
const std::string& right,const int flags)
{
std::vector< std::string > res;
std::vector<char> part;
bool in_parenthesis = false;
std::string lp=left;
std::string rp=right;
std::string::const_iterator i1 = val.begin();
std::string::const_iterator i2;
if (flags & STRIP_SPACES) {
while (i1 != val.end() && portable_isspace(*i1))
++i1;
}
i2=i1;
if(left.size()!=right.size()){
ERR_GENERAL << "Left and Right Parenthesis lists not same length" << std::endl;
return res;
}
while (i2 != val.end()) {
if(!in_parenthesis && separator && *i2 == separator){
std::string new_val(i1, i2);
if (flags & STRIP_SPACES)
boost::trim_right(new_val);
if (!(flags & REMOVE_EMPTY) || !new_val.empty())
res.push_back(new_val);
++i2;
if (flags & STRIP_SPACES) {
while (i2 != val.end() && portable_isspace(*i2))
++i2;
}
i1=i2;
continue;
}
if(!part.empty() && *i2 == part.back()){
part.pop_back();
if(!separator && part.empty()){
std::string new_val(i1, i2);
if (flags & STRIP_SPACES)
boost::trim(new_val);
res.push_back(new_val);
++i2;
i1=i2;
}else{
if (part.empty())
in_parenthesis = false;
++i2;
}
continue;
}
bool found=false;
for(std::size_t i=0; i < lp.size(); i++){
if (*i2 == lp[i]){
if (!separator && part.empty()){
std::string new_val(i1, i2);
if (flags & STRIP_SPACES)
boost::trim(new_val);
res.push_back(new_val);
++i2;
i1=i2;
}else{
++i2;
}
part.push_back(rp[i]);
found=true;
break;
}
}
if(!found){
++i2;
} else
in_parenthesis = true;
}
std::string new_val(i1, i2);
if (flags & STRIP_SPACES)
boost::trim(new_val);
if (!(flags & REMOVE_EMPTY) || !new_val.empty())
res.push_back(std::move(new_val));
if(!part.empty()){
ERR_GENERAL << "Mismatched parenthesis:\n"<<val<< std::endl;
}
return res;
}
std::pair<string_view, string_view> vertical_split(const std::string& val)
{
// Count the number of lines.
int num_lines = std::count(val.begin(), val.end(), '\n') + 1;
if(num_lines < 2) {
throw std::logic_error("utils::vertical_split: the string contains only one line");
}
// Split the string at the point where we have encountered
// (number of lines / 2 - 1) line separators.
int split_point = 0;
int num_found_line_separators = 0;
for(std::size_t i = 0; i < val.size(); ++i) {
if(val[i] == '\n') {
++num_found_line_separators;
if(num_found_line_separators >= num_lines / 2 - 1) {
split_point = i;
break;
}
}
}
assert(split_point != 0);
return { string_view(val.data(), split_point),
string_view(&val[split_point + 1], val.size() - (split_point + 1)) };
}
// Modify a number by string representing integer difference, or optionally %
int apply_modifier(const int number, const std::string& amount, const int minimum)
{
int value = 0;
try {
value = std::stoi(amount);
} catch(const std::invalid_argument&) {
}
if(amount.back() == '%') {
value = div100rounded(number * value);
}
value += number;
if(minimum > 0 && value < minimum) {
value = minimum;
}
return value;
}
std::string escape(const std::string &str, const char *special_chars)
{
std::string::size_type pos = str.find_first_of(special_chars);
if (pos == std::string::npos) {
// Fast path, possibly involving only reference counting.
return str;
}
std::string res = str;
do {
res.insert(pos, 1, '\\');
pos = res.find_first_of(special_chars, pos + 2);
} while (pos != std::string::npos);
return res;
}
std::string unescape(const std::string &str)
{
std::string::size_type pos = str.find('\\');
if (pos == std::string::npos) {
// Fast path, possibly involving only reference counting.
return str;
}
std::string res = str;
do {
res.erase(pos, 1);
pos = res.find('\\', pos + 1);
} while (pos != std::string::npos);
return str;
}
std::string urlencode(const std::string &str)
{
static const std::string nonresv_str =
"-."
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"_"
"abcdefghijklmnopqrstuvwxyz"
"~";
static const std::set<char> nonresv(nonresv_str.begin(), nonresv_str.end());
std::ostringstream res;
res << std::hex;
res.fill('0');
for(char c : str) {
if(nonresv.count(c) != 0) {
res << c;
continue;
}
res << '%';
res.width(2);
res << static_cast<int>(c);
}
return res.str();
}
bool string_bool(const std::string& str, bool def) {
if (str.empty()) return def;
// yes/no is the standard, test it first
if (str == "yes") return true;
if (str == "no"|| str == "false" || str == "off" || str == "0" || str == "0.0")
return false;
// all other non-empty string are considered as true
return true;
}
std::string bool_string(const bool value)
{
std::ostringstream ss;
ss << std::boolalpha << value;
return ss.str();
}
std::string signed_value(int val)
{
std::ostringstream oss;
oss << (val >= 0 ? "+" : font::unicode_minus) << std::abs(val);
return oss.str();
}
std::string half_signed_value(int val)
{
std::ostringstream oss;
if (val < 0)
oss << font::unicode_minus;
oss << std::abs(val);
return oss.str();
}
static void si_string_impl_stream_write(std::stringstream &ss, double input) {
std::streamsize oldprec = ss.precision();
#ifdef _MSC_VER
// Visual C++ makes 'precision' set the number of decimal places.
// Other platforms make it set the number of significant figures
ss.precision(1);
ss << std::fixed
<< input;
#else
// Workaround to display 1023 KiB instead of 1.02e3 KiB
if (input >= 1000)
ss.precision(4);
else
ss.precision(3);
ss << input;
#endif
ss.precision(oldprec);
}
std::string si_string(double input, bool base2, const std::string& unit) {
const double multiplier = base2 ? 1024 : 1000;
typedef std::array<std::string, 9> strings9;
strings9 prefixes;
strings9::const_iterator prefix;
if (input == 0.0) {
strings9 tmp { { "","","","","","","","","" } };
prefixes = tmp;
prefix = prefixes.begin();
} else if (input < 1.0) {
strings9 tmp { {
"",
_("prefix_milli^m"),
_("prefix_micro^µ"),
_("prefix_nano^n"),
_("prefix_pico^p"),
_("prefix_femto^f"),
_("prefix_atto^a"),
_("prefix_zepto^z"),
_("prefix_yocto^y")
} };
prefixes = tmp;
prefix = prefixes.begin();
while (input < 1.0 && *prefix != prefixes.back()) {
input *= multiplier;
++prefix;
}
} else {
strings9 tmp { {
"",
(base2 ?
_("prefix_kibi^K") :
_("prefix_kilo^k")
),
_("prefix_mega^M"),
_("prefix_giga^G"),
_("prefix_tera^T"),
_("prefix_peta^P"),
_("prefix_exa^E"),
_("prefix_zetta^Z"),
_("prefix_yotta^Y")
} };
prefixes = tmp;
prefix = prefixes.begin();
while (input > multiplier && *prefix != prefixes.back()) {
input /= multiplier;
++prefix;
}
}
std::stringstream ss;
si_string_impl_stream_write(ss, input);
ss << ' '
<< *prefix
<< (base2 && (!(*prefix).empty()) ? _("infix_binary^i") : "")
<< unit;
return ss.str();
}
static bool is_username_char(char c) {
return ((c == '_') || (c == '-'));
}
static bool is_wildcard_char(char c) {
return ((c == '?') || (c == '*'));
}
bool isvalid_username(const std::string& username) {
const std::size_t alnum = std::count_if(username.begin(), username.end(), isalnum);
const std::size_t valid_char =
std::count_if(username.begin(), username.end(), is_username_char);
if ((alnum + valid_char != username.size())
|| valid_char == username.size() || username.empty() )
{
return false;
}
return true;
}
bool isvalid_wildcard(const std::string& username) {
const std::size_t alnum = std::count_if(username.begin(), username.end(), isalnum);
const std::size_t valid_char =
std::count_if(username.begin(), username.end(), is_username_char);
const std::size_t wild_char =
std::count_if(username.begin(), username.end(), is_wildcard_char);
if ((alnum + valid_char + wild_char != username.size())
|| valid_char == username.size() || username.empty() )
{
return false;
}
return true;
}
bool word_completion(std::string& text, std::vector<std::string>& wordlist) {
std::vector<std::string> matches;
const std::size_t last_space = text.rfind(" ");
// If last character is a space return.
if (last_space == text.size() -1) {
wordlist = matches;
return false;
}
bool text_start;
std::string semiword;
if (last_space == std::string::npos) {
text_start = true;
semiword = text;
} else {
text_start = false;
semiword.assign(text, last_space + 1, text.size());
}
std::string best_match = semiword;
for (std::vector<std::string>::const_iterator word = wordlist.begin();
word != wordlist.end(); ++word)
{
if (word->size() < semiword.size()
|| !std::equal(semiword.begin(), semiword.end(), word->begin(),
chars_equal_insensitive))
{
continue;
}
if (matches.empty()) {
best_match = *word;
} else {
int j = 0;
while (toupper(best_match[j]) == toupper((*word)[j])) j++;
if (best_match.begin() + j < best_match.end()) {
best_match.erase(best_match.begin() + j, best_match.end());
}
}
matches.push_back(*word);
}
if(!matches.empty()) {
text.replace(last_space + 1, best_match.size(), best_match);
}
wordlist = matches;
return text_start;
}
static bool is_word_boundary(char c) {
return (c == ' ' || c == ',' || c == ':' || c == '\'' || c == '"' || c == '-');
}
bool word_match(const std::string& message, const std::string& word) {
std::size_t first = message.find(word);
if (first == std::string::npos) return false;
if (first == 0 || is_word_boundary(message[first - 1])) {
std::size_t next = first + word.size();
if (next == message.size() || is_word_boundary(message[next])) {
return true;
}
}
return false;
}
bool wildcard_string_match(const std::string& str, const std::string& match) {
const bool wild_matching = (!match.empty() && (match[0] == '*' || match[0] == '+'));
const std::string::size_type solid_begin = match.find_first_not_of("*+");
const bool have_solids = (solid_begin != std::string::npos);
// Check the simple cases first
if(!have_solids) {
const std::string::size_type plus_count = std::count(match.begin(), match.end(), '+');
return match.empty() ? str.empty() : str.length() >= plus_count;
} else if(str.empty()) {
return false;
}
const std::string::size_type solid_end = match.find_first_of("*+", solid_begin);
const std::string::size_type solid_len = (solid_end == std::string::npos)
? match.length() - solid_begin : solid_end - solid_begin;
// Since + always consumes at least one character, increment current if the match
// begins with one
std::string::size_type current = match[0] == '+' ? 1 : 0;
bool matches;
do {
matches = true;
// Now try to place the str into the solid space
const std::string::size_type test_len = str.length() - current;
for(std::string::size_type i=0; i < solid_len && matches; ++i) {
char solid_c = match[solid_begin + i];
if(i > test_len || !(solid_c == '?' || solid_c == str[current+i])) {
matches = false;
}
}
if(matches) {
// The solid space matched, now consume it and attempt to find more
const std::string consumed_match = (solid_begin+solid_len < match.length())
? match.substr(solid_end) : "";
const std::string consumed_str = (solid_len < test_len)
? str.substr(current+solid_len) : "";
matches = wildcard_string_match(consumed_str, consumed_match);
}
} while(wild_matching && !matches && ++current < str.length());
return matches;
}
std::string indent(const std::string& string, std::size_t indent_size)
{
if(indent_size == 0) {
return string;
}
const std::string indent(indent_size, ' ');
if(string.empty()) {
return indent;
}
const std::vector<std::string>& lines = split(string, '\x0A', 0);
std::string res;
for(std::size_t lno = 0; lno < lines.size();) {
const std::string& line = lines[lno];
// Lines containing only a carriage return count as empty.
if(!line.empty() && line != "\x0D") {
res += indent;
}
res += line;
if(++lno < lines.size()) {
res += '\x0A';
}
}
return res;
}
std::vector<std::string> quoted_split(const std::string& val, char c, int flags, char quote)
{
std::vector<std::string> res;
std::string::const_iterator i1 = val.begin();
std::string::const_iterator i2 = val.begin();
while (i2 != val.end()) {
if (*i2 == quote) {
// Ignore quoted character
++i2;
if (i2 != val.end()) ++i2;
} else if (*i2 == c) {
std::string new_val(i1, i2);
if (flags & STRIP_SPACES)
boost::trim(new_val);
if (!(flags & REMOVE_EMPTY) || !new_val.empty())
res.push_back(std::move(new_val));
++i2;
if (flags & STRIP_SPACES) {
while(i2 != val.end() && *i2 == ' ')
++i2;
}
i1 = i2;
} else {
++i2;
}
}
std::string new_val(i1, i2);
if (flags & STRIP_SPACES)
boost::trim(new_val);
if (!(flags & REMOVE_EMPTY) || !new_val.empty())
res.push_back(new_val);
return res;
}
std::pair<int, int> parse_range(const std::string& str)
{
const std::string::const_iterator dash = std::find(str.begin(), str.end(), '-');
const std::string a(str.begin(), dash);
const std::string b = dash != str.end() ? std::string(dash + 1, str.end()) : a;
std::pair<int,int> res {0,0};
try {
res = std::make_pair(std::stoi(a), std::stoi(b));
if (res.second < res.first) {
res.second = res.first;
}
} catch(const std::invalid_argument&) {}
return res;
}
std::vector<std::pair<int, int>> parse_ranges(const std::string& str)
{
std::vector<std::pair<int, int>> to_return;
for(const std::string& r : utils::split(str)) {
to_return.push_back(parse_range(r));
}
return to_return;
}
void ellipsis_truncate(std::string& str, const std::size_t size)
{
const std::size_t prev_size = str.length();
utf8::truncate(str, size);
if(str.length() != prev_size) {
str += font::ellipsis;
}
}
} // end namespace utils