-
Notifications
You must be signed in to change notification settings - Fork 51
/
substr.hpp
2293 lines (2059 loc) · 74.7 KB
/
substr.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
#ifndef _C4_SUBSTR_HPP_
#define _C4_SUBSTR_HPP_
/** @file substr.hpp read+write string views */
#include <string.h>
#include <ctype.h>
#include <type_traits>
#include "c4/config.hpp"
#include "c4/error.hpp"
#include "c4/substr_fwd.hpp"
#ifdef __clang__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wold-style-cast"
#elif defined(__GNUC__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wtype-limits" // disable warnings on size_t>=0, used heavily in assertions below. These assertions are a preparation step for providing the index type as a template parameter.
# pragma GCC diagnostic ignored "-Wuseless-cast"
# pragma GCC diagnostic ignored "-Wold-style-cast"
#endif
namespace c4 {
/** @defgroup doc_substr Substring: read/write string views
* @{ */
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
/** @cond dev */
namespace detail {
template<typename C>
static inline void _do_reverse(C *C4_RESTRICT first, C *C4_RESTRICT last)
{
while(last > first)
{
C tmp = *last;
*last-- = *first;
*first++ = tmp;
}
}
} // namespace detail
/** @endcond */
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
/** @cond dev */
// utility macros to deuglify SFINAE code; undefined after the class.
// https://stackoverflow.com/questions/43051882/how-to-disable-a-class-member-funrtion-for-certain-template-types
#define C4_REQUIRE_RW(ret_type) \
template <typename U=C> \
typename std::enable_if< ! std::is_const<U>::value, ret_type>::type
/** @endcond */
/** a non-owning string-view, consisting of a character pointer
* and a length.
*
* @note The pointer is explicitly restricted.
*
* @see a [quickstart
* sample](https://rapidyaml.readthedocs.io/latest/doxygen/group__doc__quickstart.html#ga43e253da0692c13967019446809c1113)
* in rapidyaml's documentation.
*
* @see @ref substr and @ref to_substr()
* @see @ref csubstr and @ref to_csubstr()
*/
template<class C>
struct C4CORE_EXPORT basic_substring
{
public:
/** a restricted pointer to the first character of the substring */
C * C4_RESTRICT str;
/** the length of the substring */
size_t len;
public:
/** @name Types */
/** @{ */
using CC = typename std::add_const<C>::type; //!< CC=const char
using NCC_ = typename std::remove_const<C>::type; //!< NCC_=non const char
using ro_substr = basic_substring<CC>;
using rw_substr = basic_substring<NCC_>;
using char_type = C;
using size_type = size_t;
using iterator = C*;
using const_iterator = CC*;
enum : size_t { npos = (size_t)-1, NONE = (size_t)-1 };
/// convert automatically to substring of const C
template<class U=C>
C4_ALWAYS_INLINE operator typename std::enable_if<!std::is_const<U>::value, ro_substr const&>::type () const noexcept
{
return *(ro_substr const*)this; // don't call the str+len ctor because it does a check
}
/** @} */
public:
/** @name Default construction and assignment */
/** @{ */
C4_ALWAYS_INLINE constexpr basic_substring() noexcept : str(), len() {}
C4_ALWAYS_INLINE basic_substring(basic_substring const&) noexcept = default;
C4_ALWAYS_INLINE basic_substring(basic_substring &&) noexcept = default;
C4_ALWAYS_INLINE basic_substring(std::nullptr_t) noexcept : str(nullptr), len(0) {}
C4_ALWAYS_INLINE basic_substring& operator= (basic_substring const&) noexcept = default;
C4_ALWAYS_INLINE basic_substring& operator= (basic_substring &&) noexcept = default;
C4_ALWAYS_INLINE basic_substring& operator= (std::nullptr_t) noexcept { str = nullptr; len = 0; return *this; }
C4_ALWAYS_INLINE void clear() noexcept { str = nullptr; len = 0; }
/** @} */
public:
/** @name Construction and assignment from characters with the same type */
/** @{ */
/** Construct from an array.
* @warning the input string need not be zero terminated, but the
* length is taken as if the string was zero terminated */
template<size_t N>
C4_ALWAYS_INLINE constexpr basic_substring(C (&s_)[N]) noexcept : str(s_), len(N-1) {}
/** Construct from a pointer and length.
* @warning the input string need not be zero terminated. */
C4_ALWAYS_INLINE basic_substring(C *s_, size_t len_) noexcept : str(s_), len(len_) { C4_ASSERT(str || !len_); }
/** Construct from two pointers.
* @warning the end pointer MUST BE larger than or equal to the begin pointer
* @warning the input string need not be zero terminated */
C4_ALWAYS_INLINE basic_substring(C *beg_, C *end_) noexcept : str(beg_), len(static_cast<size_t>(end_ - beg_)) { C4_ASSERT(end_ >= beg_); }
/** Construct from a C-string (zero-terminated string)
* @warning the input string MUST BE zero terminated.
* @warning will call strlen()
* @note this overload uses SFINAE to prevent it from overriding the array ctor
* @see For a more detailed explanation on why the plain overloads cannot
* coexist, see http://cplusplus.bordoon.com/specializeForCharacterArrays.html */
template<class U, typename std::enable_if<std::is_same<U, C*>::value || std::is_same<U, NCC_*>::value, int>::type=0>
C4_ALWAYS_INLINE basic_substring(U s_) noexcept : str(s_), len(s_ ? strlen(s_) : 0) {}
/** Assign from an array.
* @warning the input string need not be zero terminated, but the
* length is taken as if the string was zero terminated */
template<size_t N>
C4_ALWAYS_INLINE void assign(C (&s_)[N]) noexcept { str = (s_); len = (N-1); }
/** Assign from a pointer and length.
* @warning the input string need not be zero terminated. */
C4_ALWAYS_INLINE void assign(C *s_, size_t len_) noexcept { str = s_; len = len_; C4_ASSERT(str || !len_); }
/** Assign from two pointers.
* @warning the end pointer MUST BE larger than or equal to the begin pointer
* @warning the input string need not be zero terminated. */
C4_ALWAYS_INLINE void assign(C *beg_, C *end_) noexcept { C4_ASSERT(end_ >= beg_); str = (beg_); len = static_cast<size_t>(end_ - beg_); }
/** Assign from a C-string (zero-terminated string)
* @warning the input string must be zero terminated.
* @warning will call strlen()
* @note this overload uses SFINAE to prevent it from overriding the array ctor
* @see For a more detailed explanation on why the plain overloads cannot
* coexist, see http://cplusplus.bordoon.com/specializeForCharacterArrays.html */
template<class U, typename std::enable_if<std::is_same<U, C*>::value || std::is_same<U, NCC_*>::value, int>::type=0>
C4_ALWAYS_INLINE void assign(U s_) noexcept { str = (s_); len = (s_ ? strlen(s_) : 0); }
/** Assign from an array.
* @warning the input string need not be zero terminated. */
template<size_t N>
C4_ALWAYS_INLINE basic_substring& operator= (C (&s_)[N]) noexcept { str = (s_); len = (N-1); return *this; }
/** Assign from a C-string (zero-terminated string)
* @warning the input string MUST BE zero terminated.
* @warning will call strlen()
* @note this overload uses SFINAE to prevent it from overriding the array ctor
* @see For a more detailed explanation on why the plain overloads cannot
* coexist, see http://cplusplus.bordoon.com/specializeForCharacterArrays.html */
template<class U, typename std::enable_if<std::is_same<U, C*>::value || std::is_same<U, NCC_*>::value, int>::type=0>
C4_ALWAYS_INLINE basic_substring& operator= (U s_) noexcept { str = s_; len = s_ ? strlen(s_) : 0; return *this; }
/** @} */
public:
/** @name Standard accessor methods */
/** @{ */
C4_ALWAYS_INLINE C4_PURE bool has_str() const noexcept { return ! empty() && str[0] != C(0); }
C4_ALWAYS_INLINE C4_PURE bool empty() const noexcept { return (len == 0 || str == nullptr); }
C4_ALWAYS_INLINE C4_PURE bool not_empty() const noexcept { return (len != 0 && str != nullptr); }
C4_ALWAYS_INLINE C4_PURE size_t size() const noexcept { return len; }
C4_ALWAYS_INLINE C4_PURE iterator begin() noexcept { return str; }
C4_ALWAYS_INLINE C4_PURE iterator end () noexcept { return str + len; }
C4_ALWAYS_INLINE C4_PURE const_iterator begin() const noexcept { return str; }
C4_ALWAYS_INLINE C4_PURE const_iterator end () const noexcept { return str + len; }
C4_ALWAYS_INLINE C4_PURE C * data() noexcept { return str; }
C4_ALWAYS_INLINE C4_PURE C const* data() const noexcept { return str; }
C4_ALWAYS_INLINE C4_PURE C & operator[] (size_t i) noexcept { C4_ASSERT(i >= 0 && i < len); return str[i]; }
C4_ALWAYS_INLINE C4_PURE C const& operator[] (size_t i) const noexcept { C4_ASSERT(i >= 0 && i < len); return str[i]; }
C4_ALWAYS_INLINE C4_PURE C & front() noexcept { C4_ASSERT(len > 0 && str != nullptr); return *str; }
C4_ALWAYS_INLINE C4_PURE C const& front() const noexcept { C4_ASSERT(len > 0 && str != nullptr); return *str; }
C4_ALWAYS_INLINE C4_PURE C & back() noexcept { C4_ASSERT(len > 0 && str != nullptr); return *(str + len - 1); }
C4_ALWAYS_INLINE C4_PURE C const& back() const noexcept { C4_ASSERT(len > 0 && str != nullptr); return *(str + len - 1); }
/** @} */
public:
/** @name Comparison methods */
/** @{ */
C4_PURE int compare(C const c) const noexcept
{
C4_XASSERT((str != nullptr) || len == 0);
if(C4_LIKELY(str != nullptr && len > 0))
return (*str != c) ? *str - c : (static_cast<int>(len) - 1);
else
return -1;
}
C4_PURE int compare(const char *C4_RESTRICT that, size_t sz) const noexcept
{
C4_XASSERT(that || sz == 0);
C4_XASSERT(str || len == 0);
if(C4_LIKELY(str && that))
{
{
const size_t min = len < sz ? len : sz;
for(size_t i = 0; i < min; ++i)
if(str[i] != that[i])
return str[i] < that[i] ? -1 : 1;
}
if(len < sz)
return -1;
else if(len == sz)
return 0;
else
return 1;
}
else if(len == sz)
{
C4_XASSERT(len == 0 && sz == 0);
return 0;
}
return len < sz ? -1 : 1;
}
C4_ALWAYS_INLINE C4_PURE int compare(ro_substr const that) const noexcept { return this->compare(that.str, that.len); }
C4_ALWAYS_INLINE C4_PURE bool operator== (std::nullptr_t) const noexcept { return str == nullptr; }
C4_ALWAYS_INLINE C4_PURE bool operator!= (std::nullptr_t) const noexcept { return str != nullptr; }
C4_ALWAYS_INLINE C4_PURE bool operator== (C const c) const noexcept { return this->compare(c) == 0; }
C4_ALWAYS_INLINE C4_PURE bool operator!= (C const c) const noexcept { return this->compare(c) != 0; }
C4_ALWAYS_INLINE C4_PURE bool operator< (C const c) const noexcept { return this->compare(c) < 0; }
C4_ALWAYS_INLINE C4_PURE bool operator> (C const c) const noexcept { return this->compare(c) > 0; }
C4_ALWAYS_INLINE C4_PURE bool operator<= (C const c) const noexcept { return this->compare(c) <= 0; }
C4_ALWAYS_INLINE C4_PURE bool operator>= (C const c) const noexcept { return this->compare(c) >= 0; }
template<class U> C4_ALWAYS_INLINE C4_PURE bool operator== (basic_substring<U> const that) const noexcept { return this->compare(that) == 0; }
template<class U> C4_ALWAYS_INLINE C4_PURE bool operator!= (basic_substring<U> const that) const noexcept { return this->compare(that) != 0; }
template<class U> C4_ALWAYS_INLINE C4_PURE bool operator< (basic_substring<U> const that) const noexcept { return this->compare(that) < 0; }
template<class U> C4_ALWAYS_INLINE C4_PURE bool operator> (basic_substring<U> const that) const noexcept { return this->compare(that) > 0; }
template<class U> C4_ALWAYS_INLINE C4_PURE bool operator<= (basic_substring<U> const that) const noexcept { return this->compare(that) <= 0; }
template<class U> C4_ALWAYS_INLINE C4_PURE bool operator>= (basic_substring<U> const that) const noexcept { return this->compare(that) >= 0; }
template<size_t N> C4_ALWAYS_INLINE C4_PURE bool operator== (const char (&that)[N]) const noexcept { return this->compare(that, N-1) == 0; }
template<size_t N> C4_ALWAYS_INLINE C4_PURE bool operator!= (const char (&that)[N]) const noexcept { return this->compare(that, N-1) != 0; }
template<size_t N> C4_ALWAYS_INLINE C4_PURE bool operator< (const char (&that)[N]) const noexcept { return this->compare(that, N-1) < 0; }
template<size_t N> C4_ALWAYS_INLINE C4_PURE bool operator> (const char (&that)[N]) const noexcept { return this->compare(that, N-1) > 0; }
template<size_t N> C4_ALWAYS_INLINE C4_PURE bool operator<= (const char (&that)[N]) const noexcept { return this->compare(that, N-1) <= 0; }
template<size_t N> C4_ALWAYS_INLINE C4_PURE bool operator>= (const char (&that)[N]) const noexcept { return this->compare(that, N-1) >= 0; }
/** @} */
public:
/** @name Sub-selection methods */
/** @{ */
/** true if *this is a substring of that (ie, from the same buffer) */
C4_ALWAYS_INLINE C4_PURE bool is_sub(ro_substr const that) const noexcept
{
return that.is_super(*this);
}
/** true if that is a substring of *this (ie, from the same buffer) */
C4_ALWAYS_INLINE C4_PURE bool is_super(ro_substr const that) const noexcept
{
if(C4_LIKELY(len > 0))
return that.str >= str && that.str+that.len <= str+len;
else
return that.len == 0 && that.str == str && str != nullptr;
}
/** true if there is overlap of at least one element between that and *this */
C4_ALWAYS_INLINE C4_PURE bool overlaps(ro_substr const that) const noexcept
{
// thanks @timwynants
return that.str+that.len > str && that.str < str+len;
}
public:
/** return [first,len[ */
C4_ALWAYS_INLINE C4_PURE basic_substring sub(size_t first) const noexcept
{
C4_ASSERT(first >= 0 && first <= len);
return basic_substring(str + first, len - first);
}
/** return [first,first+num[. If num==npos, return [first,len[ */
C4_ALWAYS_INLINE C4_PURE basic_substring sub(size_t first, size_t num) const noexcept
{
C4_ASSERT(first >= 0 && first <= len);
C4_ASSERT((num >= 0 && num <= len) || (num == npos));
size_t rnum = num != npos ? num : len - first;
C4_ASSERT((first >= 0 && first + rnum <= len) || (num == 0));
return basic_substring(str + first, rnum);
}
/** return [first,last[. If last==npos, return [first,len[ */
C4_ALWAYS_INLINE C4_PURE basic_substring range(size_t first, size_t last=npos) const noexcept
{
C4_ASSERT(first >= 0 && first <= len);
last = last != npos ? last : len;
C4_ASSERT(first <= last);
C4_ASSERT(last >= 0 && last <= len);
return basic_substring(str + first, last - first);
}
/** return the first @p num elements: [0,num[*/
C4_ALWAYS_INLINE C4_PURE basic_substring first(size_t num) const noexcept
{
C4_ASSERT(num <= len || num == npos);
return basic_substring(str, num != npos ? num : len);
}
/** return the last @p num elements: [len-num,len[*/
C4_ALWAYS_INLINE C4_PURE basic_substring last(size_t num) const noexcept
{
C4_ASSERT(num <= len || num == npos);
return num != npos ?
basic_substring(str + len - num, num) :
*this;
}
/** offset from the ends: return [left,len-right[ ; ie, trim a
number of characters from the left and right. This is
equivalent to python's negative list indices. */
C4_ALWAYS_INLINE C4_PURE basic_substring offs(size_t left, size_t right) const noexcept
{
C4_ASSERT(left >= 0 && left <= len);
C4_ASSERT(right >= 0 && right <= len);
C4_ASSERT(left <= len - right + 1);
return basic_substring(str + left, len - right - left);
}
/** return [0, pos[ . Same as .first(pos), but provided for compatibility with .right_of() */
C4_ALWAYS_INLINE C4_PURE basic_substring left_of(size_t pos) const noexcept
{
C4_ASSERT(pos <= len || pos == npos);
return (pos != npos) ?
basic_substring(str, pos) :
*this;
}
/** return [0, pos+include_pos[ . Same as .first(pos+1), but provided for compatibility with .right_of() */
C4_ALWAYS_INLINE C4_PURE basic_substring left_of(size_t pos, bool include_pos) const noexcept
{
C4_ASSERT(pos <= len || pos == npos);
return (pos != npos) ?
basic_substring(str, pos+include_pos) :
*this;
}
/** return [pos+1, len[ */
C4_ALWAYS_INLINE C4_PURE basic_substring right_of(size_t pos) const noexcept
{
C4_ASSERT(pos <= len || pos == npos);
return (pos != npos) ?
basic_substring(str + (pos + 1), len - (pos + 1)) :
basic_substring(str + len, size_t(0));
}
/** return [pos+!include_pos, len[ */
C4_ALWAYS_INLINE C4_PURE basic_substring right_of(size_t pos, bool include_pos) const noexcept
{
C4_ASSERT(pos <= len || pos == npos);
return (pos != npos) ?
basic_substring(str + (pos + !include_pos), len - (pos + !include_pos)) :
basic_substring(str + len, size_t(0));
}
public:
/** given @p subs a substring of the current string, get the
* portion of the current string to the left of it */
C4_ALWAYS_INLINE C4_PURE basic_substring left_of(ro_substr const subs) const noexcept
{
C4_ASSERT(is_super(subs) || subs.empty());
auto ssb = subs.begin();
auto b = begin();
auto e = end();
if(ssb >= b && ssb <= e)
return sub(0, static_cast<size_t>(ssb - b));
else
return sub(0, 0);
}
/** given @p subs a substring of the current string, get the
* portion of the current string to the right of it */
C4_ALWAYS_INLINE C4_PURE basic_substring right_of(ro_substr const subs) const noexcept
{
C4_ASSERT(is_super(subs) || subs.empty());
auto sse = subs.end();
auto b = begin();
auto e = end();
if(sse >= b && sse <= e)
return sub(static_cast<size_t>(sse - b), static_cast<size_t>(e - sse));
else
return sub(0, 0);
}
/** @} */
public:
/** @name Removing characters (trim()) / patterns (strip()) from the tips of the string */
/** @{ */
/** trim left */
basic_substring triml(const C c) const
{
if( ! empty())
{
size_t pos = first_not_of(c);
if(pos != npos)
return sub(pos);
}
return sub(0, 0);
}
/** trim left ANY of the characters.
* @see stripl() to remove a pattern from the left */
basic_substring triml(ro_substr chars) const
{
if( ! empty())
{
size_t pos = first_not_of(chars);
if(pos != npos)
return sub(pos);
}
return sub(0, 0);
}
/** trim the character c from the right */
basic_substring trimr(const C c) const
{
if( ! empty())
{
size_t pos = last_not_of(c, npos);
if(pos != npos)
return sub(0, pos+1);
}
return sub(0, 0);
}
/** trim right ANY of the characters
* @see stripr() to remove a pattern from the right */
basic_substring trimr(ro_substr chars) const
{
if( ! empty())
{
size_t pos = last_not_of(chars, npos);
if(pos != npos)
return sub(0, pos+1);
}
return sub(0, 0);
}
/** trim the character c left and right */
basic_substring trim(const C c) const
{
return triml(c).trimr(c);
}
/** trim left and right ANY of the characters
* @see strip() to remove a pattern from the left and right */
basic_substring trim(ro_substr const chars) const
{
return triml(chars).trimr(chars);
}
/** remove a pattern from the left
* @see triml() to remove characters*/
basic_substring stripl(ro_substr pattern) const
{
if( ! begins_with(pattern))
return *this;
return sub(pattern.len < len ? pattern.len : len);
}
/** remove a pattern from the right
* @see trimr() to remove characters*/
basic_substring stripr(ro_substr pattern) const
{
if( ! ends_with(pattern))
return *this;
return left_of(len - (pattern.len < len ? pattern.len : len));
}
/** @} */
public:
/** @name Lookup methods */
/** @{ */
inline size_t find(const C c, size_t start_pos=0) const
{
return first_of(c, start_pos);
}
inline size_t find(ro_substr pattern, size_t start_pos=0) const
{
C4_ASSERT(start_pos == npos || (start_pos >= 0 && start_pos <= len));
if(len < pattern.len) return npos;
for(size_t i = start_pos, e = len - pattern.len + 1; i < e; ++i)
{
bool gotit = true;
for(size_t j = 0; j < pattern.len; ++j)
{
C4_ASSERT(i + j < len);
if(str[i + j] != pattern.str[j])
{
gotit = false;
break;
}
}
if(gotit)
{
return i;
}
}
return npos;
}
public:
/** count the number of occurrences of c */
inline size_t count(const C c, size_t pos=0) const
{
C4_ASSERT(pos >= 0 && pos <= len);
size_t num = 0;
pos = find(c, pos);
while(pos != npos)
{
++num;
pos = find(c, pos + 1);
}
return num;
}
/** count the number of occurrences of s */
inline size_t count(ro_substr c, size_t pos=0) const
{
C4_ASSERT(pos >= 0 && pos <= len);
size_t num = 0;
pos = find(c, pos);
while(pos != npos)
{
++num;
pos = find(c, pos + c.len);
}
return num;
}
/** get the substr consisting of the first occurrence of @p c after @p pos, or an empty substr if none occurs */
inline basic_substring select(const C c, size_t pos=0) const
{
pos = find(c, pos);
return pos != npos ? sub(pos, 1) : basic_substring();
}
/** get the substr consisting of the first occurrence of @p pattern after @p pos, or an empty substr if none occurs */
inline basic_substring select(ro_substr pattern, size_t pos=0) const
{
pos = find(pattern, pos);
return pos != npos ? sub(pos, pattern.len) : basic_substring();
}
public:
struct first_of_any_result
{
size_t which;
size_t pos;
inline operator bool() const { return which != NONE && pos != npos; }
};
first_of_any_result first_of_any(ro_substr s0, ro_substr s1) const
{
ro_substr s[2] = {s0, s1};
return first_of_any_iter(&s[0], &s[0] + 2);
}
first_of_any_result first_of_any(ro_substr s0, ro_substr s1, ro_substr s2) const
{
ro_substr s[3] = {s0, s1, s2};
return first_of_any_iter(&s[0], &s[0] + 3);
}
first_of_any_result first_of_any(ro_substr s0, ro_substr s1, ro_substr s2, ro_substr s3) const
{
ro_substr s[4] = {s0, s1, s2, s3};
return first_of_any_iter(&s[0], &s[0] + 4);
}
first_of_any_result first_of_any(ro_substr s0, ro_substr s1, ro_substr s2, ro_substr s3, ro_substr s4) const
{
ro_substr s[5] = {s0, s1, s2, s3, s4};
return first_of_any_iter(&s[0], &s[0] + 5);
}
template<class It>
first_of_any_result first_of_any_iter(It first_span, It last_span) const
{
for(size_t i = 0; i < len; ++i)
{
size_t curr = 0;
for(It it = first_span; it != last_span; ++curr, ++it)
{
auto const& chars = *it;
if((i + chars.len) > len) continue;
bool gotit = true;
for(size_t j = 0; j < chars.len; ++j)
{
C4_ASSERT(i + j < len);
if(str[i + j] != chars[j])
{
gotit = false;
break;
}
}
if(gotit)
{
return {curr, i};
}
}
}
return {NONE, npos};
}
public:
/** true if the first character of the string is @p c */
bool begins_with(const C c) const
{
return len > 0 ? str[0] == c : false;
}
/** true if the first @p num characters of the string are @p c */
bool begins_with(const C c, size_t num) const
{
if(len < num)
{
return false;
}
for(size_t i = 0; i < num; ++i)
{
if(str[i] != c)
{
return false;
}
}
return true;
}
/** true if the string begins with the given @p pattern */
bool begins_with(ro_substr pattern) const
{
if(len < pattern.len)
{
return false;
}
for(size_t i = 0; i < pattern.len; ++i)
{
if(str[i] != pattern[i])
{
return false;
}
}
return true;
}
/** true if the first character of the string is any of the given @p chars */
bool begins_with_any(ro_substr chars) const
{
if(len == 0)
{
return false;
}
for(size_t i = 0; i < chars.len; ++i)
{
if(str[0] == chars.str[i])
{
return true;
}
}
return false;
}
/** true if the last character of the string is @p c */
bool ends_with(const C c) const
{
return len > 0 ? str[len-1] == c : false;
}
/** true if the last @p num characters of the string are @p c */
bool ends_with(const C c, size_t num) const
{
if(len < num)
{
return false;
}
for(size_t i = len - num; i < len; ++i)
{
if(str[i] != c)
{
return false;
}
}
return true;
}
/** true if the string ends with the given @p pattern */
bool ends_with(ro_substr pattern) const
{
if(len < pattern.len)
{
return false;
}
for(size_t i = 0, s = len-pattern.len; i < pattern.len; ++i)
{
if(str[s+i] != pattern[i])
{
return false;
}
}
return true;
}
/** true if the last character of the string is any of the given @p chars */
bool ends_with_any(ro_substr chars) const
{
if(len == 0)
{
return false;
}
for(size_t i = 0; i < chars.len; ++i)
{
if(str[len - 1] == chars[i])
{
return true;
}
}
return false;
}
public:
/** @return the first position where c is found in the string, or npos if none is found */
size_t first_of(const C c, size_t start=0) const
{
C4_ASSERT(start == npos || (start >= 0 && start <= len));
for(size_t i = start; i < len; ++i)
{
if(str[i] == c)
return i;
}
return npos;
}
/** @return the last position where c is found in the string, or npos if none is found */
size_t last_of(const C c, size_t start=npos) const
{
C4_ASSERT(start == npos || (start >= 0 && start <= len));
if(start == npos)
start = len;
for(size_t i = start-1; i != size_t(-1); --i)
{
if(str[i] == c)
return i;
}
return npos;
}
/** @return the first position where ANY of the chars is found in the string, or npos if none is found */
size_t first_of(ro_substr chars, size_t start=0) const
{
C4_ASSERT(start == npos || (start >= 0 && start <= len));
for(size_t i = start; i < len; ++i)
{
for(size_t j = 0; j < chars.len; ++j)
{
if(str[i] == chars[j])
return i;
}
}
return npos;
}
/** @return the last position where ANY of the chars is found in the string, or npos if none is found */
size_t last_of(ro_substr chars, size_t start=npos) const
{
C4_ASSERT(start == npos || (start >= 0 && start <= len));
if(start == npos)
start = len;
for(size_t i = start-1; i != size_t(-1); --i)
{
for(size_t j = 0; j < chars.len; ++j)
{
if(str[i] == chars[j])
return i;
}
}
return npos;
}
public:
size_t first_not_of(const C c) const
{
for(size_t i = 0; i < len; ++i)
{
if(str[i] != c)
return i;
}
return npos;
}
size_t first_not_of(const C c, size_t start) const
{
C4_ASSERT((start >= 0 && start <= len) || (start == len && len == 0));
for(size_t i = start; i < len; ++i)
{
if(str[i] != c)
return i;
}
return npos;
}
size_t last_not_of(const C c) const
{
for(size_t i = len-1; i != size_t(-1); --i)
{
if(str[i] != c)
return i;
}
return npos;
}
size_t last_not_of(const C c, size_t start) const
{
C4_ASSERT(start == npos || (start >= 0 && start <= len));
if(start == npos)
start = len;
for(size_t i = start-1; i != size_t(-1); --i)
{
if(str[i] != c)
return i;
}
return npos;
}
size_t first_not_of(ro_substr chars) const
{
for(size_t i = 0; i < len; ++i)
{
bool gotit = true;
for(size_t j = 0; j < chars.len; ++j)
{
if(str[i] == chars.str[j])
{
gotit = false;
break;
}
}
if(gotit)
{
return i;
}
}
return npos;
}
size_t first_not_of(ro_substr chars, size_t start) const
{
C4_ASSERT((start >= 0 && start <= len) || (start == len && len == 0));
for(size_t i = start; i < len; ++i)
{
bool gotit = true;
for(size_t j = 0; j < chars.len; ++j)
{
if(str[i] == chars.str[j])
{
gotit = false;
break;
}
}
if(gotit)
{
return i;
}
}
return npos;
}
size_t last_not_of(ro_substr chars) const
{
for(size_t i = len-1; i != size_t(-1); --i)
{
bool gotit = true;
for(size_t j = 0; j < chars.len; ++j)
{
if(str[i] == chars.str[j])
{
gotit = false;
break;
}
}
if(gotit)
{
return i;
}
}
return npos;
}
size_t last_not_of(ro_substr chars, size_t start) const
{
C4_ASSERT(start == npos || (start >= 0 && start <= len));
if(start == npos)
start = len;
for(size_t i = start-1; i != size_t(-1); --i)
{
bool gotit = true;
for(size_t j = 0; j < chars.len; ++j)
{
if(str[i] == chars.str[j])
{
gotit = false;
break;
}
}
if(gotit)
{
return i;
}
}
return npos;
}
/** @} */
public:
/** @name Range lookup methods */
/** @{ */
/** get the range delimited by an open-close pair of characters.
* @note There must be no nested pairs.
* @note No checks for escapes are performed. */
basic_substring pair_range(CC open, CC close) const
{
size_t b = find(open);
if(b == npos)
return basic_substring();
size_t e = find(close, b+1);
if(e == npos)
return basic_substring();
basic_substring ret = range(b, e+1);
C4_ASSERT(ret.sub(1).find(open) == npos);
return ret;
}
/** get the range delimited by a single open-close character (eg, quotes).