-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
py_string.hpp
1535 lines (1464 loc) · 42.6 KB
/
py_string.hpp
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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#ifndef PY_STRING_HPP
#define PY_STRING_HPP
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cwctype>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <limits>
#include <map>
#include <regex>
#include <sstream>
#include <string>
#include <unordered_map>
namespace py {
#if __cplusplus >= 201703L
#include <optional>
using std::nullopt;
using std::optional;
#else
namespace null_allow {
template <class T> class null_allow {
private:
T m_value;
bool m_has_value;
public:
null_allow<T>(std::nullptr_t)
: m_has_value(false)
{
}
null_allow<T>(T _Value)
: m_value(_Value)
, m_has_value(true)
{
}
null_allow<T> &operator=(std::nullptr_t)
{
m_has_value = false;
return *this;
}
null_allow<T> &operator=(T _Value)
{
m_value = _Value;
m_has_value = true;
return *this;
}
bool operator==(null_allow<T> rhs) const
{
return (m_has_value && rhs.m_has_value)
? (m_value == rhs.m_value)
: (m_has_value == rhs.m_has_value);
};
bool operator!=(null_allow<T> rhs) const
{
return !(this->operator==(rhs));
};
bool operator==(std::nullptr_t) const { return !m_has_value; }
bool operator!=(std::nullptr_t) const { return m_has_value; }
bool operator==(T &&_Value) const { return m_value == _Value; }
bool operator!=(T &&_Value) const { return m_value != _Value; }
bool has_value() const { return m_has_value; }
T value() const { return m_value; }
T value_or(T &&v) const { return m_has_value ? m_value : v; }
};
} // namespace null_allow
template <class T> using optional = null_allow::null_allow<T>;
const std::nullptr_t nullopt = nullptr;
#endif
using optional_int = optional<int>;
template <class T> class slice {
using value_type = T;
private:
optional<T> m_start;
optional<T> m_stop;
optional<T> m_step;
public:
slice()
: m_start()
, m_stop()
, m_step() {};
template <class U>
slice(U stop)
: m_start()
, m_stop(stop)
, m_step() {};
template <class U, class V>
slice(U start, V stop)
: m_start(start)
, m_stop(stop)
, m_step() {};
template <class U, class V, class W>
slice(U start, V stop, W step)
: m_start(start)
, m_stop(stop)
, m_step(step) {};
std::tuple<T, T, T, T> adjust_index(T length) const;
};
#ifndef PY_STR_UTIL
#define PY_STR_UTIL
namespace util {
template <class _Elme> inline bool isRowBoundary(_Elme c)
{
switch (c) {
case '\n':
case '\r':
case '\v':
case '\f':
case '\x1c':
case '\x1d':
case '\x1e':
case '\x85':
// case '\u2028':
// case '\u2029':
return true;
default:
return false;
}
}
template <class _Elme> inline bool isprintable(_Elme c)
{
return (32 <= c) && (c <= 126);
}
template <class _Elme> inline bool isspace(_Elme c)
{
return ((9 <= c) && (c <= 13)) || ((28 <= c) && (c <= 32));
}
template <class _Elme> inline bool isascii(_Elme c)
{
return (0 <= c && c <= 127);
}
/*
template <class _Elme>
inline bool ishexdigit(_Elme c)
{
return ((48 <= c) && (c <= 57)) || ((65 <= c) && (c <= 70)) || ((97 <= c) && (c
<= 102));
}
template <class _Elme>
inline bool isoctdigit(_Elme c)
{
return (48 <= c && c <= 55)
}
template <class _Elme>
inline bool isbindigit(_Elme c)
{
return c == 48 || c == 49;
}
*/
template <class T> inline std::string __to_string(T _Val, const char *_Format)
{
char buf[std::numeric_limits<T>::digits + 3];
std::sprintf(buf, _Format, _Val);
return buf;
}
inline std::string to_string_oct(short _Val)
{
return __to_string(_Val, "%o");
}
inline std::string to_string_oct(unsigned short _Val)
{
return __to_string(_Val, "%o");
}
inline std::string to_string_oct(int _Val) { return __to_string(_Val, "%o"); }
inline std::string to_string_oct(unsigned int _Val)
{
return __to_string(_Val, "%o");
}
inline std::string to_string_oct(long _Val)
{
return __to_string(_Val, "%lo");
}
inline std::string to_string_oct(unsigned long _Val)
{
return __to_string(_Val, "%lo");
}
inline std::string to_string_oct(long long _Val)
{
return __to_string(_Val, "%llo");
}
inline std::string to_string_oct(unsigned long long _Val)
{
return __to_string(_Val, "%llo");
}
//
inline std::string to_string_hex(short _Val)
{
return __to_string(_Val, "%x");
}
inline std::string to_string_hex(unsigned short _Val)
{
return __to_string(_Val, "%x");
}
inline std::string to_string_hex(int _Val) { return __to_string(_Val, "%x"); }
inline std::string to_string_hex(unsigned int _Val)
{
return __to_string(_Val, "%x");
}
inline std::string to_string_hex(long _Val)
{
return __to_string(_Val, "%lx");
}
inline std::string to_string_hex(unsigned long _Val)
{
return __to_string(_Val, "%lx");
}
inline std::string to_string_hex(long long _Val)
{
return __to_string(_Val, "%llx");
}
inline std::string to_string_hex(unsigned long long _Val)
{
return __to_string(_Val, "%llx");
}
inline std::string to_string_HEX(short _Val)
{
return __to_string(_Val, "%X");
}
inline std::string to_string_HEX(unsigned short _Val)
{
return __to_string(_Val, "%X");
}
inline std::string to_string_HEX(int _Val) { return __to_string(_Val, "%X"); }
inline std::string to_string_HEX(unsigned int _Val)
{
return __to_string(_Val, "%X");
}
inline std::string to_string_HEX(long _Val)
{
return __to_string(_Val, "%lX");
}
inline std::string to_string_HEX(unsigned long _Val)
{
return __to_string(_Val, "%lX");
}
inline std::string to_string_HEX(long long _Val)
{
return __to_string(_Val, "%llX");
}
inline std::string to_string_HEX(unsigned long long _Val)
{
return __to_string(_Val, "%llX");
}
template <class T> inline size_t decimal_place(T x)
{
size_t i = 0, j = 10;
T t = x;
while (t != std::floor(t)) {
t = x * j;
j *= 10;
++i;
}
return i;
}
template <class T> T sign(T n) { return (n > 0) - (n < 0); }
template <class T>
inline std::tuple<T, T, T, T> adjust_index(optional<T> _start,
optional<T> _stop,
optional<T> _step, T _length)
{
T start, stop, step = _step.value_or(1), upper, lower;
T step_sign = sign(step);
bool step_is_negative = step_sign < 0;
/* Find lower and upper bounds for start and stop. */
if (step_is_negative) {
lower = -1;
upper = _length + lower;
} else {
lower = 0;
upper = _length;
}
// Compute start.
if (_start.has_value()) {
start = _start.value();
if (sign(start) < 0) {
start += _length;
if (start < lower /* Py_LT */) {
start = lower;
}
} else {
if (start > upper /* Py_GT */) {
start = upper;
}
}
} else {
start = step_is_negative ? upper : lower;
}
// Compute stop.
if (_stop.has_value()) {
stop = _stop.value();
if (sign(stop) < 0) {
stop += _length;
if (stop < lower /* Py_LT */) {
stop = lower;
}
} else {
if (stop > upper /* Py_GT */) {
stop = upper;
}
}
} else {
stop = step_is_negative ? lower : upper;
}
T len = 0;
if (step < 0) {
if (stop < start) {
len = (start - stop - 1) / (-step) + 1;
}
} else {
if (start < stop) {
len = (stop - start - 1) / step + 1;
}
}
return std::make_tuple(start, stop, step, len);
}
template <class T>
inline std::tuple<T, T, T, T> adjust_index(optional<T> _start,
optional<T> _stop, T _length)
{
return adjust_index<T>(_start, _stop, 1, _length);
}
inline void adjust_index(int &start, int &end, int len)
{
std::tie(start, end, std::ignore, std::ignore)
= adjust_index<int>(start, end, 1, len);
}
template <class T> inline std::string itobin(T n)
{
std::string str;
T p = 1;
for (T i = sizeof(T) * 8; i--;)
str.push_back(n & (p << i) ? '1' : '0');
auto pos = str.find('1');
if (pos != std::string::npos)
return str.substr(pos);
return "0";
}
inline void sepinsert(std::string &str, const std::string sep, size_t len)
{
std::string dst;
size_t count = 0;
auto start = str.rbegin();
auto end = str.rend();
for (; start != end; ++start) {
++count;
dst.push_back(*start);
if (!(count % len)) {
dst += sep;
}
}
if (dst.back() == sep.back())
dst.pop_back();
str.clear();
std::reverse_copy(dst.begin(), dst.end(), std::back_inserter(str));
}
inline void __format_eq(std::string &str, char c, size_t len)
{
static std::regex mc("((-|\\+| )?(0[boxX])?)?([0-9a-fA-F,_.]+?)");
std::smatch sub;
std::regex_match(str, sub, mc);
str = sub.str(1)
+ std::string((len - sub.str(1).size()) - sub.str(4).size(), c)
+ sub.str(4);
}
inline char _get_fill_char(std::string str)
{
return str.empty() ? ' ' : str[0];
}
inline size_t _get_size(std::string str)
{
return str.empty() ? 0 : std::stoul(str);
}
} // namespace util
#endif
namespace format_parser {
template <typename T>
void _sign_format(T &str, std::string sign, bool negative);
template <typename T,
typename std::enable_if_t<!std::is_integral<T>::value
&& !std::is_floating_point<T>::value,
std::nullptr_t> = nullptr>
bool format(std::string r, T target, std::string &dst);
template <typename T, typename std::enable_if_t<std::is_integral<T>::value,
std::nullptr_t> = nullptr>
bool format(std::string r, T target, std::string &dst);
template <typename T,
typename std::enable_if_t<std::is_floating_point<T>::value,
std::nullptr_t> = nullptr>
bool format(std::string r, T target, std::string &dst);
} // namespace format_parser
template <class _Elme> class basic_string : public std::basic_string<_Elme> {
public:
using transtable_t = std::unordered_map<_Elme, basic_string<_Elme>>;
private:
size_t _back_index(int index) const { return this->size() + index; }
basic_string<_Elme> _format_automatic(basic_string<_Elme> &_Str) const
{
return _Str;
}
template <class Head, class... Tail>
basic_string<_Elme> _format_automatic(basic_string<_Elme> &_Str, Head head,
Tail... tail) const
{
basic_string<_Elme> str;
std::smatch sub;
static std::regex match("\\{(![^:])?(:(.+?)?)?\\}");
if (std::regex_search(_Str, sub, match)) {
format_parser::format(sub.str(3), head, str);
_Str = _Str.pyreplace(sub.str(0), str, 1);
return this->_format_automatic(_Str, std::move(tail)...);
}
return _Str;
}
template <size_t N>
basic_string<_Elme> _format(basic_string<_Elme> &_Str) const
{
return _Str;
}
template <size_t N, class Head, class... Tail>
basic_string<_Elme> _format(basic_string<_Elme> &_Str, Head head,
Tail... tail) const // format_numbaring
{
basic_string<_Elme> str;
std::smatch sub;
std::regex num_match(std::string("\\{") + std::to_string(N)
+ std::string("(![^:])?(:(.+?)?)?\\}"));
while (std::regex_search(_Str, sub, num_match)) {
format_parser::format(sub.str(4), head, str);
_Str = _Str.pyreplace(sub.str(0), str);
}
return this->_format<N + 1>(_Str, std::move(tail)...);
}
template <class Map> basic_string<_Elme> _format_map(Map &map);
template <class Map> static transtable_t _maketrans(Map &table_map)
{
transtable_t table;
for (auto &&m : table_map) {
table[m.first] = m.second;
}
return table;
}
public:
basic_string<_Elme>()
: std::basic_string<_Elme>() {};
basic_string<_Elme>(std::basic_string<_Elme> &&_Str)
: std::basic_string<_Elme>(_Str) {};
basic_string<_Elme>(const std::basic_string<_Elme> &_Str)
: std::basic_string<_Elme>(_Str) {};
using std::basic_string<_Elme>::basic_string;
using std::basic_string<_Elme>::operator=;
using std::basic_string<_Elme>::operator+=;
// override//
const _Elme &at(int _Index) const
{
return (_Index < 0) ? std::basic_string<_Elme>::at(_back_index(_Index))
: std::basic_string<_Elme>::at(_Index);
}
_Elme &at(int _Index)
{
return (_Index < 0) ? std::basic_string<_Elme>::at(_back_index(_Index))
: std::basic_string<_Elme>::at(_Index);
}
const _Elme &operator[](int _Index) const
{
return (_Index < 0) ? std::string::operator[](_back_index(_Index))
: std::string::operator[](_Index);
}
_Elme &operator[](int _Index)
{
return (_Index < 0) ? std::string::operator[](_back_index(_Index))
: std::string::operator[](_Index);
}
basic_string<_Elme>
substr(const typename std::basic_string<_Elme>::size_type pos = 0,
const typename std::basic_string<_Elme>::size_type n
= std::basic_string<_Elme>::npos) const noexcept
{
return basic_string<_Elme>(*this, pos, n,
std::basic_string<_Elme>::get_allocator());
}
/////
basic_string<_Elme> operator*(size_t i) const;
basic_string<_Elme> &operator*=(size_t i);
basic_string<_Elme> operator[](std::initializer_list<optional_int> slice);
basic_string<_Elme> capitalize(void) const noexcept;
basic_string<_Elme> center(size_t width, _Elme fillchar = ' ') const;
size_t count(basic_string<_Elme> sub, int start = 0,
int end = std::numeric_limits<int>::max()) const;
bool endswith(basic_string<_Elme> suffix, int start = 0,
int end = std::numeric_limits<int>::max()) const;
basic_string<_Elme> expandtabs(size_t tabsize = 8) const;
int pyfind(basic_string<_Elme> sub, int start = 0,
int end = std::numeric_limits<int>::max()) const;
template <class... Args> basic_string<_Elme> format(Args... args);
basic_string<_Elme>
format_map(std::map<basic_string<_Elme>, basic_string<_Elme>> map);
basic_string<_Elme>
format_map(std::unordered_map<basic_string<_Elme>, basic_string<_Elme>> map);
int index(basic_string<_Elme> sub, int start = 0,
int end = std::numeric_limits<int>::max()) const;
bool isalnum(void) const;
bool isalpha(void) const;
bool isascii(void) const;
bool isdecimal(void) const;
bool isdigit(void) const;
bool islower(void) const;
bool isnumeric(void) const;
bool isprintable(void) const;
bool isspace(void) const;
bool istitle(void) const;
bool isupper(void) const;
template <typename _Iterable>
basic_string<_Elme> join(_Iterable &&iterable) const;
basic_string<_Elme> ljust(size_t width, _Elme fillchar = ' ') const;
basic_string<_Elme> lower(void) const noexcept;
basic_string<_Elme> lstrip(void) const;
basic_string<_Elme> lstrip(basic_string<_Elme> chars) const;
std::tuple<basic_string<_Elme>, basic_string<_Elme>, basic_string<_Elme>>
partition(basic_string<_Elme> sep) const;
basic_string<_Elme>
pyreplace(basic_string<_Elme> old, basic_string<_Elme> _new,
size_t count = std::numeric_limits<size_t>::max()) const;
int pyrfind(basic_string<_Elme> sub, int start = 0,
int end = std::numeric_limits<int>::max()) const;
int rindex(basic_string<_Elme> sub, int start = 0,
int end = std::numeric_limits<int>::max()) const;
basic_string<_Elme> rjust(size_t width, _Elme fillchar = ' ') const;
std::tuple<basic_string<_Elme>, basic_string<_Elme>, basic_string<_Elme>>
rpartition(basic_string<_Elme> sep) const;
template <typename _Iterable>
void rsplit(_Iterable &dst,
size_t maxsplit = std::numeric_limits<size_t>::max()) const;
template <typename _Iterable>
void rsplit(_Iterable &dst, basic_string<_Elme> sep,
size_t maxsplit = std::numeric_limits<size_t>::max()) const;
basic_string<_Elme> rstrip(void) const;
basic_string<_Elme> rstrip(basic_string<_Elme> chars) const;
template <typename _Iterable>
void split(_Iterable &dst,
size_t maxsplit = std::numeric_limits<size_t>::max()) const;
template <typename _Iterable>
void split(_Iterable &dst, basic_string<_Elme> sep,
size_t maxsplit = std::numeric_limits<size_t>::max()) const;
template <typename _Iterable>
void splitlines(_Iterable &dst, bool keepends = false) const;
bool startswith(basic_string<_Elme> suffix, int start = 0,
int end = std::numeric_limits<int>::max()) const;
basic_string<_Elme> strip(void) const;
basic_string<_Elme> strip(basic_string<_Elme> chars) const;
basic_string<_Elme> swapcase(void) const;
basic_string<_Elme> title(void) const;
basic_string<_Elme> translate(transtable_t &table) const;
basic_string<_Elme> upper(void) const;
basic_string<_Elme> zfill(size_t width) const;
basic_string<_Elme> slice(optional_int index) const;
basic_string<_Elme> slice(optional_int start, optional_int end) const;
basic_string<_Elme> slice(optional_int start, optional_int end,
optional_int step) const;
static transtable_t
maketrans(std::unordered_map<_Elme, basic_string<_Elme>> table_map)
{
return basic_string<_Elme>::_maketrans(table_map);
}
static transtable_t maketrans(std::map<_Elme, basic_string<_Elme>> table_map)
{
return basic_string<_Elme>::_maketrans(table_map);
}
static transtable_t maketrans(basic_string<_Elme> from,
basic_string<_Elme> to)
{
return basic_string<_Elme>::maketrans(from, to, "");
}
static transtable_t maketrans(basic_string<_Elme> from,
basic_string<_Elme> to,
basic_string<_Elme> delchars)
{
transtable_t table;
for (int i = std::min(from.size(), to.size()); i--;) {
table[from[i]] = to[i];
}
for (auto &&s : delchars) {
table[s] = "";
}
return table;
}
};
using string = basic_string<char>;
using wstring = basic_string<wchar_t>;
template <class _Elme>
basic_string<_Elme> operator+(basic_string<_Elme> r, basic_string<_Elme> l)
{
r += l;
return r;
}
template <class _Elme>
basic_string<_Elme> basic_string<_Elme>::operator*(size_t i) const
{
if (i <= 0)
return "";
basic_string<_Elme> str(*this);
str.reserve(str.size() * i);
for (; --i;) {
str += *this;
}
return str;
}
template <class _Elme>
basic_string<_Elme> &basic_string<_Elme>::operator*=(size_t i)
{
*this = *this * i;
return *this;
}
template <class _Elme>
basic_string<_Elme>
basic_string<_Elme>::operator[](std::initializer_list<optional_int> slice)
{
basic_string<_Elme> str;
auto in = slice.begin();
optional_int index1 = *in++;
optional_int index2 = *in++;
optional_int index3 = *in;
switch (slice.size()) {
case 0:
return *this;
case 1:
return this->slice(index1);
case 2:
return this->slice(index1, index2);
default: // case 3:
return this->slice(index1, index2, index3);
}
}
template <class _Elme>
basic_string<_Elme> basic_string<_Elme>::capitalize(void) const noexcept
{
basic_string<_Elme> str = this->lower();
str[0] = std::toupper(str[0]);
return str;
}
template <class _Elme>
basic_string<_Elme> basic_string<_Elme>::center(size_t width,
_Elme fillchar) const
{
size_t r, l, len = this->size();
if (width <= len)
return *this;
l = width - len;
r = l / 2 + l % 2;
return basic_string<_Elme>(l - r, fillchar) + *this
+ basic_string<_Elme>(r, fillchar);
}
template <class _Elme>
size_t basic_string<_Elme>::count(basic_string<_Elme> sub, int start,
int end) const
{
size_t sublen = sub.size();
if (sublen == 0) {
return this->size() + 1;
}
util::adjust_index(start, end, this->size());
size_t nummatches = 0;
size_t cursor = this->find(sub, start);
while ((cursor != std::basic_string<_Elme>::npos)
&& (cursor + sublen <= end)) {
cursor = this->find(sub, cursor + sublen);
++nummatches;
}
return nummatches;
}
template <class _Elme>
bool basic_string<_Elme>::endswith(basic_string<_Elme> suffix, int start,
int end) const
{
int len = this->size();
int sublen = suffix.size();
util::adjust_index(start, end, len);
if (end - start < sublen || start > len) {
return false;
}
if (end - sublen > start) {
start = end - sublen;
}
if (end - start >= sublen) {
return (!std::memcmp(this->c_str() + start * sizeof(_Elme), suffix.c_str(),
sublen * sizeof(_Elme)));
}
return false;
}
template <class _Elme>
inline basic_string<_Elme> basic_string<_Elme>::expandtabs(size_t tabsize) const
{
return this->pyreplace("\t", basic_string<_Elme>(tabsize, ' '));
}
template <class _Elme>
int basic_string<_Elme>::pyfind(basic_string<_Elme> sub, int start,
int end) const
{
util::adjust_index(start, end, this->size());
size_t result = this->find(sub, start);
if (result == std::basic_string<_Elme>::npos || (result + sub.size() > end)) {
return -1;
}
return result;
}
template <class _Elme>
template <class... Args>
basic_string<_Elme> basic_string<_Elme>::format(Args... args)
{
basic_string<_Elme> str(*this);
static std::regex match("\\{(![^:])?(:(.+?)?)?\\}");
if (std::regex_search(str, match)) {
return this->_format_automatic(str, std::move(args)...);
}
return this->_format<0>(str, std::move(args)...);
}
template <class _Elme>
basic_string<_Elme> basic_string<_Elme>::format_map(
std::map<basic_string<_Elme>, basic_string<_Elme>> map)
{
return this->_format_map(map);
}
template <class _Elme>
basic_string<_Elme> basic_string<_Elme>::format_map(
std::unordered_map<basic_string<_Elme>, basic_string<_Elme>> map)
{
return this->_format_map(map);
}
template <class _Elme>
template <class Map>
inline basic_string<_Elme> basic_string<_Elme>::_format_map(Map &map)
{
basic_string<_Elme> str(*this);
basic_string<_Elme> key, val;
size_t s_cursor = str.find("{");
size_t e_cursor = str.find("}", s_cursor);
size_t key_len;
while (s_cursor != std::basic_string<_Elme>::npos
&& e_cursor != std::basic_string<_Elme>::npos) {
key_len = e_cursor - s_cursor - 1;
key = str.substr(s_cursor + 1, key_len);
if (map.count(key)) {
val = map[key];
str.replace(s_cursor, key_len + 2, val);
e_cursor -= key_len + 2;
e_cursor += val.size();
}
s_cursor = str.find("{", e_cursor);
e_cursor = str.find("}", s_cursor);
}
return str;
}
template <class _Elme>
inline int basic_string<_Elme>::index(basic_string<_Elme> sub, int start,
int end) const
{
return this->pyfind(sub, start, end);
}
template <class _Elme> bool basic_string<_Elme>::isalnum(void) const
{
for (auto &&s : *this) {
if (!std::isalnum(s))
return false;
}
return !this->empty();
}
template <class _Elme> bool basic_string<_Elme>::isalpha(void) const
{
for (auto &&s : *this) {
if (!std::isalpha(s))
return false;
}
return !this->empty();
}
template <class _Elme> bool basic_string<_Elme>::isascii(void) const
{
if (this->empty())
return true;
for (auto &&s : *this) {
if (!util::isascii(s))
return false;
}
return true;
}
template <class _Elme> bool basic_string<_Elme>::isdecimal(void) const
{
for (auto &&s : *this) {
if (!std::isdigit(s))
return false;
}
return !this->empty();
}
template <class _Elme> bool basic_string<_Elme>::isdigit(void) const
{
return this->isdecimal();
}
template <class _Elme> bool basic_string<_Elme>::islower(void) const
{
for (auto &&s : *this) {
if (!std::islower(s))
return false;
}
return !this->empty();
}
template <class _Elme> bool basic_string<_Elme>::isnumeric(void) const
{
return this->isdecimal();
}
template <class _Elme> bool basic_string<_Elme>::isprintable(void) const
{
for (auto &&s : *this) {
if (!util::isprintable<_Elme>(s))
return false;
}
return !this->empty();
}
template <class _Elme> bool basic_string<_Elme>::isspace(void) const
{
for (auto &&s : *this) {
if (!util::isspace<_Elme>(s))
return false;
}
return !this->empty();
}
template <class _Elme> bool basic_string<_Elme>::istitle(void) const
{
if (this->size() == 0)
return false;
if (this->size() == 1)
return std::isupper(this->at(0));
bool cased = false, previous_is_cased = false;
for (auto &&s : *this) {
if (std::isupper(s)) {
if (previous_is_cased) {
return false;
}
previous_is_cased = true;
cased = true;
} else if (std::islower(s)) {
if (!previous_is_cased) {
return false;
}
previous_is_cased = true;
cased = true;
} else {
previous_is_cased = false;
}
}
return cased;
}
template <class _Elme> bool basic_string<_Elme>::isupper(void) const
{
{
for (auto &&s : *this) {
if (!std::isupper(s))
return false;
}
return !this->empty();
}
}
template <class _Elme>
template <typename _Iterable>
basic_string<_Elme> basic_string<_Elme>::join(_Iterable &&iterable) const
{
basic_string<_Elme> str = "";
const basic_string<_Elme> *sep = &str;
for (auto &&i : iterable) {
str += *sep;
str += i;
sep = this;
}
return str;
}
template <class _Elme>
basic_string<_Elme> basic_string<_Elme>::ljust(size_t width,
_Elme fillchar) const
{
if (width <= this->size()) {
return *this;
}
return *this + basic_string<_Elme>(width - this->size(), fillchar);
}
template <class _Elme>
basic_string<_Elme> basic_string<_Elme>::lower(void) const noexcept
{
{
basic_string<_Elme> str(*this);
for (auto &&s : str) {
s = std::tolower(s);
}
return str;
}
}
template <class _Elme>
basic_string<_Elme> basic_string<_Elme>::lstrip(void) const
{
if (this->empty())
return *this;
auto itr = this->begin();
while (util::isspace(*itr))
++itr;
return this->substr(std::distance(this->begin(), itr));
}
template <class _Elme>
basic_string<_Elme> basic_string<_Elme>::lstrip(basic_string<_Elme> chars) const
{
if (this->empty())
return *this;
if (chars.empty())
return this->lstrip();
auto itr = this->begin();
while (chars.find(*itr) != std::basic_string<_Elme>::npos)
++itr;
return this->substr(std::distance(this->begin(), itr));
}
template <class _Elme>
std::tuple<basic_string<_Elme>, basic_string<_Elme>, basic_string<_Elme>>
basic_string<_Elme>::partition(basic_string<_Elme> sep) const
{
auto index = this->find(sep);
if (index == std::basic_string<_Elme>::npos) {
return std::make_tuple(*this, "", "");
}
return std::make_tuple(this->substr(0, index), sep,
this->substr(index + sep.size()));
}
template <class _Elme>
basic_string<_Elme> basic_string<_Elme>::pyreplace(basic_string<_Elme> old,
basic_string<_Elme> _new,
size_t count) const
{
size_t cursor = 0;
basic_string<_Elme> s(*this);
size_t oldlen = old.size(), newlen = _new.size();
cursor = s.find(old, cursor);
while (cursor != std::basic_string<_Elme>::npos && cursor <= s.size()
&& count > 0) {
s.replace(cursor, oldlen, _new);
cursor += newlen;
if (oldlen != 0) {
cursor = s.find(old, cursor);
} else {
++cursor;
}
--count;
}
return s;
}
template <class _Elme>
int basic_string<_Elme>::pyrfind(basic_string<_Elme> sub, int start,
int end) const
{