-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring.hpp
More file actions
2140 lines (1784 loc) · 74.7 KB
/
Copy pathstring.hpp
File metadata and controls
2140 lines (1784 loc) · 74.7 KB
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
//
// Copyright (c) 2025-2025 Martin Moene
//
// https://github.com/martinmoene/string-lite
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef NONSTD_STRING_BARE_HPP
#define NONSTD_STRING_BARE_HPP
#define string_bare_MAJOR 0
#define string_bare_MINOR 0
#define string_bare_PATCH 0
#define string_bare_VERSION string_STRINGIFY(string_bare_MAJOR) "." string_STRINGIFY(string_bare_MINOR) "." string_STRINGIFY(string_bare_PATCH)
#define string_STRINGIFY( x ) string_STRINGIFY_( x )
#define string_STRINGIFY_( x ) #x
// tweak header support:
#ifdef __has_include
# if __has_include(<nonstd/string.tweak.hpp>)
# include <nonstd/string.tweak.hpp>
# endif
#define string_HAVE_TWEAK_HEADER 1
#else
#define string_HAVE_TWEAK_HEADER 0
//# pragma message("string.hpp: Note: Tweak header not supported.")
#endif
// string configuration:
// Select character types to provide:
#if !defined( string_CONFIG_PROVIDE_CHAR_T )
# define string_CONFIG_PROVIDE_CHAR_T 1
#endif
#if !defined( string_CONFIG_PROVIDE_WCHAR_T )
# define string_CONFIG_PROVIDE_WCHAR_T 0
#endif
#if !defined( string_CONFIG_PROVIDE_CHAR8_T )
# define string_CONFIG_PROVIDE_CHAR8_T 0
#endif
#if !defined( string_CONFIG_PROVIDE_CHAR16_T )
# define string_CONFIG_PROVIDE_CHAR16_T 0
#endif
#if !defined( string_CONFIG_PROVIDE_CHAR32_T )
# define string_CONFIG_PROVIDE_CHAR32_T 0
#endif
// Provide regex variants: default off, as it increases compile time considerably.
#if !defined( string_CONFIG_PROVIDE_REGEX )
# define string_CONFIG_PROVIDE_REGEX 0
#endif
// Control presence of exception handling (try and auto discover):
#ifndef string_CONFIG_NO_EXCEPTIONS
# if defined(_MSC_VER)
# include <cstddef> // for _HAS_EXCEPTIONS
# endif
# if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || (_HAS_EXCEPTIONS)
# define string_CONFIG_NO_EXCEPTIONS 0
# else
# define string_CONFIG_NO_EXCEPTIONS 1
# endif
#endif
// C++ language version detection:
// Note: VC14.0/1900 (VS2015) lacks too much from C++14.
#ifndef string_CPLUSPLUS
# if defined(_MSVC_LANG ) && !defined(__clang__)
# define string_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG )
# else
# define string_CPLUSPLUS __cplusplus
# endif
#endif
#define string_CPP98_OR_GREATER ( string_CPLUSPLUS >= 199711L )
#define string_CPP11_OR_GREATER ( string_CPLUSPLUS >= 201103L )
#define string_CPP14_OR_GREATER ( string_CPLUSPLUS >= 201402L )
#define string_CPP17_OR_GREATER ( string_CPLUSPLUS >= 201703L )
#define string_CPP20_OR_GREATER ( string_CPLUSPLUS >= 202002L )
#define string_CPP23_OR_GREATER ( string_CPLUSPLUS >= 202302L )
#define string_CPP26_OR_GREATER ( string_CPLUSPLUS > 202302L ) // not finalized yet
// MSVC version:
#if defined(_MSC_VER ) && !defined(__clang__)
# define string_COMPILER_MSVC_VER (_MSC_VER )
# define string_COMPILER_MSVC_VERSION (_MSC_VER / 10 - 10 * ( 5 + (_MSC_VER < 1900 ) ) )
# define string_COMPILER_MSVC_VERSION_FULL (_MSC_VER - 100 * ( 5 + (_MSC_VER < 1900 ) ) )
#else
# define string_COMPILER_MSVC_VER 0
# define string_COMPILER_MSVC_VERSION 0
# define string_COMPILER_MSVC_VERSION_FULL 0
#endif
// clang version:
#define string_COMPILER_VERSION( major, minor, patch ) ( 10 * ( 10 * (major) + (minor) ) + (patch) )
#if defined( __apple_build_version__ )
# define string_COMPILER_APPLECLANG_VERSION string_COMPILER_VERSION( __clang_major__, __clang_minor__, __clang_patchlevel__ )
# define string_COMPILER_CLANG_VERSION 0
#elif defined( __clang__ )
# define string_COMPILER_APPLECLANG_VERSION 0
# define string_COMPILER_CLANG_VERSION string_COMPILER_VERSION( __clang_major__, __clang_minor__, __clang_patchlevel__ )
#else
# define string_COMPILER_APPLECLANG_VERSION 0
# define string_COMPILER_CLANG_VERSION 0
#endif
// GNUC version:
#if defined(__GNUC__) && !defined(__clang__)
# define string_COMPILER_GNUC_VERSION string_COMPILER_VERSION( __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__ )
#else
# define string_COMPILER_GNUC_VERSION 0
#endif
// half-open range [lo..hi):
#define string_BETWEEN( v, lo, hi ) ( (lo) <= (v) && (v) < (hi) )
// Presence of language and library features:
#define string_CPP11_100 (string_CPP11_OR_GREATER || string_COMPILER_MSVC_VER >= 1600)
#define string_CPP11_110 (string_CPP11_OR_GREATER || string_COMPILER_MSVC_VER >= 1700)
#define string_CPP11_120 (string_CPP11_OR_GREATER || string_COMPILER_MSVC_VER >= 1800)
#define string_CPP11_140 (string_CPP11_OR_GREATER || string_COMPILER_MSVC_VER >= 1900)
#define string_CPP11_141 (string_CPP11_OR_GREATER || string_COMPILER_MSVC_VER >= 1910)
#define string_CPP11_000 (string_CPP11_OR_GREATER)
#define string_CPP14_000 (string_CPP14_OR_GREATER)
#define string_CPP14_120 (string_CPP14_OR_GREATER || string_COMPILER_MSVC_VER >= 1800)
#define string_CPP17_000 (string_CPP17_OR_GREATER)
#define string_CPP17_120 (string_CPP17_OR_GREATER || string_COMPILER_MSVC_VER >= 1800)
#define string_CPP17_140 (string_CPP17_OR_GREATER || string_COMPILER_MSVC_VER >= 1900)
#define string_CPP20_000 (string_CPP20_OR_GREATER)
// Presence of C++11 language features:
#define string_HAVE_FREE_BEGIN string_CPP11_120
#define string_HAVE_CONSTEXPR_11 (string_CPP11_000 && !string_BETWEEN(string_COMPILER_MSVC_VER, 1, 1910))
#define string_HAVE_NOEXCEPT string_CPP11_140
#define string_HAVE_NULLPTR string_CPP11_100
#define string_HAVE_DEFAULT_FN_TPL_ARGS string_CPP11_120
#define string_HAVE_EXPLICIT_CONVERSION string_CPP11_120
#define string_HAVE_CHAR16_T string_CPP11_000
#define string_HAVE_CHAR32_T string_HAVE_CHAR16_T
// Presence of C++14 language features:
#define string_HAVE_CONSTEXPR_14 string_CPP14_000
#define string_HAVE_FREE_CBEGIN string_CPP14_120
// Presence of C++17 language features:
#define string_HAVE_FREE_SIZE string_CPP17_140
#define string_HAVE_NODISCARD string_CPP17_000
#define string_HAVE_STRING_VIEW string_CPP17_000
// Presence of C++20 language features:
#define string_HAVE_CHAR8_T string_CPP20_000
// Presence of C++ library features:
#define string_HAVE_REGEX (string_CPP11_000 && !string_BETWEEN(string_COMPILER_GNUC_VERSION, 1, 490))
#define string_HAVE_TYPE_TRAITS string_CPP11_110
// Usage of C++ language features:
#if string_HAVE_CONSTEXPR_11
# define string_constexpr constexpr
#else
# define string_constexpr /*constexpr*/
#endif
#if string_HAVE_CONSTEXPR_14
# define string_constexpr14 constexpr
#else
# define string_constexpr14 /*constexpr*/
#endif
#if string_HAVE_EXPLICIT_CONVERSION
# define string_explicit explicit
#else
# define string_explicit /*explicit*/
#endif
#if string_HAVE_NOEXCEPT && !string_CONFIG_NO_EXCEPTIONS
# define string_noexcept noexcept
#else
# define string_noexcept /*noexcept*/
#endif
#if string_HAVE_NODISCARD
# define string_nodiscard [[nodiscard]]
#else
# define string_nodiscard /*[[nodiscard]]*/
#endif
#if string_HAVE_NULLPTR
# define string_nullptr nullptr
#else
# define string_nullptr NULL
#endif
#if string_HAVE_EXPLICIT_CONVERSION
# define string_explicit_cv explicit
#else
# define string_explicit_cv /*explicit*/
#endif
// Additional includes:
#include <cassert>
#include <algorithm> // std::transform()
#include <iterator>
#include <locale>
#include <limits>
#include <string>
#include <tuple>
#include <vector>
#if string_HAVE_STRING_VIEW
# include <string_view>
#else
// # pragma message("string.hpp: Using internal nonstd::std17::basic_string_view<CharT>.")
#endif
#if string_CONFIG_PROVIDE_REGEX && string_HAVE_REGEX
# include <regex>
#endif
namespace nonstd {
//
// string details:
//
namespace string {
// npos
#if string_HAVE_STRING_VIEW
static string_constexpr std::size_t npos = std::string_view::npos;
#elif string_CPP17_OR_GREATER
static string_constexpr std::size_t npos = std::string::npos;
#elif string_CPP11_OR_GREATER
enum : std::size_t { npos = std::string::npos };
#else
enum { npos = std::string::npos };
#endif // string_HAVE_STRING_VIEW
namespace detail {
template< typename T >
string_constexpr std::size_t to_size_t(T value) string_noexcept
{
return static_cast<std::size_t>( value );
}
} // namespace detail
namespace std14 {
} // namespace std14
namespace std17 {
#if string_HAVE_STRING_VIEW
using std::basic_string_view;
# if string_CONFIG_PROVIDE_CHAR_T
using std::string_view;
# endif
# if string_CONFIG_PROVIDE_WCHAR_T
using std::wstring_view;
# endif
# if string_CONFIG_PROVIDE_CHAR8_T
using std::u8string_view;
# endif
# if string_CONFIG_PROVIDE_CHAR16_T
using std::u16string_view;
# endif
# if string_CONFIG_PROVIDE_CHAR32_T
using std::u32string_view;
# endif
#else // string_HAVE_STRING_VIEW
// Local basic_string_view.
template
<
class CharT,
class Traits = std::char_traits<CharT>
>
class basic_string_view
{
public:
// Member types:
typedef Traits traits_type;
typedef CharT value_type;
typedef CharT * pointer;
typedef CharT const * const_pointer;
typedef CharT & reference;
typedef CharT const & const_reference;
typedef const_pointer iterator;
typedef const_pointer const_iterator;
typedef std::reverse_iterator< const_iterator > reverse_iterator;
typedef std::reverse_iterator< const_iterator > const_reverse_iterator;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
// Constants:
#if string_CPP17_OR_GREATER
static string_constexpr std::size_t npos = string::npos;
#elif string_CPP11_OR_GREATER
enum : std::size_t { npos = string::npos };
#else
enum { npos = string::npos };
#endif
// 24.4.2.1 Construction and assignment:
string_constexpr basic_string_view() string_noexcept
: data_( string_nullptr )
, size_( 0 )
{}
string_constexpr basic_string_view( CharT const * s ) string_noexcept // non-standard noexcept
: data_( s )
, size_( Traits::length(s) )
{}
string_constexpr basic_string_view( CharT const * s, size_type count ) string_noexcept // non-standard noexcept
: data_( s )
, size_( count )
{}
string_constexpr basic_string_view( CharT const * b, CharT const * e ) string_noexcept // C++20, non-standard noexcept
: data_( b )
, size_( detail::to_size_t(e - b) )
{}
string_constexpr basic_string_view( std::basic_string<CharT> & s ) string_noexcept // non-standard noexcept
: data_( s.data() )
, size_( s.size() )
{}
string_constexpr basic_string_view( std::basic_string<CharT> const & s ) string_noexcept // non-standard noexcept
: data_( s.data() )
, size_( s.size() )
{}
// #if string_HAVE_EXPLICIT_CONVERSION
template< class Allocator >
string_nodiscard string_explicit operator std::basic_string<CharT, Traits, Allocator>() const
{
return to_string( Allocator() );
}
// #endif // string_HAVE_EXPLICIT_CONVERSION
#if string_CPP11_OR_GREATER
template< class Allocator = std::allocator<CharT> >
string_nodiscard std::basic_string<CharT, Traits, Allocator>
to_string( Allocator const & a = Allocator() ) const
{
return std::basic_string<CharT, Traits, Allocator>( begin(), end(), a );
}
#else
string_nodiscard std::basic_string<CharT, Traits>
to_string() const
{
return std::basic_string<CharT, Traits>( begin(), end() );
}
template< class Allocator >
string_nodiscard std::basic_string<CharT, Traits, Allocator>
to_string( Allocator const & a ) const
{
return std::basic_string<CharT, Traits, Allocator>( begin(), end(), a );
}
#endif // string_CPP11_OR_GREATER
string_nodiscard string_constexpr14 size_type find( basic_string_view v, size_type pos = 0 ) const string_noexcept // (1)
{
return assert( v.size() == 0 || v.data() != string_nullptr )
, pos >= size()
? npos
: to_pos( std::search( cbegin() + pos, cend(), v.cbegin(), v.cend(), Traits::eq ) );
}
string_nodiscard string_constexpr14 size_type rfind( basic_string_view v, size_type pos = npos ) const string_noexcept // (1)
{
if ( size() < v.size() )
{
return npos;
}
if ( v.empty() )
{
return (std::min)( size(), pos );
}
const_iterator last = cbegin() + (std::min)( size() - v.size(), pos ) + v.size();
const_iterator result = std::find_end( cbegin(), last, v.cbegin(), v.cend(), Traits::eq );
return result != last ? size_type( result - cbegin() ) : npos;
}
string_nodiscard string_constexpr14 size_type find( CharT c, size_type pos = 0 ) const string_noexcept // (2)
{
return find( basic_string_view( &c, 1 ), pos );
}
string_nodiscard string_constexpr size_type find_first_of( basic_string_view v, size_type pos = 0 ) const string_noexcept // (1)
{
return pos >= size()
? npos
: to_pos( std::find_first_of( cbegin() + pos, cend(), v.cbegin(), v.cend(), Traits::eq ) );
}
string_nodiscard string_constexpr size_type find_first_of( CharT c, size_type pos = 0 ) const string_noexcept // (2)
{
return find_first_of( basic_string_view( &c, 1 ), pos );
}
string_nodiscard string_constexpr size_type find_last_of( basic_string_view v, size_type pos = npos ) const string_noexcept // (1)
{
return empty()
? npos
: pos >= size()
? find_last_of( v, size() - 1 )
: to_pos( std::find_first_of( const_reverse_iterator( cbegin() + pos + 1 ), crend(), v.cbegin(), v.cend(), Traits::eq ) );
}
string_nodiscard string_constexpr size_type find_first_not_of( basic_string_view v, size_type pos = 0 ) const string_noexcept // (1)
{
return pos >= size()
? npos
: to_pos( std::find_if( cbegin() + pos, cend(), not_in_view( v ) ) );
}
string_nodiscard string_constexpr size_type find_last_not_of( basic_string_view v, size_type pos = npos ) const string_noexcept // (1)
{
return empty()
? npos
: pos >= size()
? find_last_not_of( v, size() - 1 )
: to_pos( std::find_if( const_reverse_iterator( cbegin() + pos + 1 ), crend(), not_in_view( v ) ) );
}
string_nodiscard string_constexpr14 int compare( basic_string_view v ) const string_noexcept
{
auto const result = Traits::compare( data(), v.data(), (std::min)(size(), v.size()) );
return result != 0
? result
: size() == v.size() ? 0 : size() < v.size() ? -1 : +1;
}
string_nodiscard string_constexpr bool empty() const string_noexcept { return size_ == 0; }
string_nodiscard string_constexpr size_type size() const string_noexcept { return size_; }
string_nodiscard string_constexpr size_type length() const string_noexcept { return size_; }
string_nodiscard string_constexpr const_pointer data() const string_noexcept { return data_; }
string_nodiscard string_constexpr14 basic_string_view substr( size_type pos = 0, size_type n = npos ) const
{
#if string_CONFIG_NO_EXCEPTIONS
assert( pos <= size() );
#else
if ( pos > size() )
{
throw std::out_of_range("string_view::substr()");
}
#endif
return basic_string_view( data() + pos, n == npos? size() - pos : (std::min)( n, size() - pos ) );
}
string_nodiscard string_constexpr const_iterator begin() const string_noexcept { return data_; }
string_nodiscard string_constexpr const_iterator end() const string_noexcept { return data_ + size_; }
string_nodiscard string_constexpr const_iterator cbegin() const string_noexcept { return begin(); }
string_nodiscard string_constexpr const_iterator cend() const string_noexcept { return end(); }
string_nodiscard string_constexpr const_reverse_iterator rbegin() const string_noexcept { return const_reverse_iterator( end() ); }
string_nodiscard string_constexpr const_reverse_iterator rend() const string_noexcept { return const_reverse_iterator( begin() ); }
string_nodiscard string_constexpr const_reverse_iterator crbegin() const string_noexcept { return rbegin(); }
string_nodiscard string_constexpr const_reverse_iterator crend() const string_noexcept { return rend(); }
private:
struct not_in_view
{
const basic_string_view v;
string_constexpr explicit not_in_view( basic_string_view v_ ) : v( v_ ) {}
string_nodiscard string_constexpr bool operator()( CharT c ) const
{
return npos == v.find_first_of( c );
}
};
string_nodiscard string_constexpr size_type to_pos( const_iterator it ) const
{
return it == cend() ? npos : size_type( it - cbegin() );
}
string_nodiscard string_constexpr size_type to_pos( const_reverse_iterator it ) const
{
return it == crend() ? npos : size_type( crend() - it - 1 );
}
private:
const_pointer data_;
size_type size_;
};
template< class CharT, class Traits >
string_nodiscard string_constexpr bool
operator==( basic_string_view<CharT,Traits> lhs, basic_string_view<CharT,Traits> rhs ) string_noexcept
{
return lhs.compare( rhs ) == 0;
}
template< class CharT, class Traits >
string_nodiscard string_constexpr bool
operator!=( basic_string_view<CharT,Traits> lhs, basic_string_view<CharT,Traits> rhs ) string_noexcept
{
return lhs.compare( rhs ) != 0;
}
template< class CharT, class Traits >
string_nodiscard string_constexpr bool
operator<( basic_string_view<CharT,Traits> lhs, basic_string_view<CharT,Traits> rhs ) string_noexcept
{
return lhs.compare( rhs ) < 0;
}
template< class CharT, class Traits >
string_nodiscard string_constexpr bool
operator<=( basic_string_view<CharT,Traits> lhs, basic_string_view<CharT,Traits> rhs ) string_noexcept
{
return lhs.compare( rhs ) <= 0;
}
template< class CharT, class Traits >
string_nodiscard string_constexpr bool
operator>( basic_string_view<CharT,Traits> lhs, basic_string_view<CharT,Traits> rhs ) string_noexcept
{
return lhs.compare( rhs ) > 0;
}
template< class CharT, class Traits >
string_nodiscard string_constexpr bool
operator>=( basic_string_view<CharT,Traits> lhs, basic_string_view<CharT,Traits> rhs ) string_noexcept
{
return compare( lhs, rhs ) >= 0;
}
#if string_CONFIG_PROVIDE_CHAR_T
typedef basic_string_view<char> string_view;
#endif
#if string_CONFIG_PROVIDE_WCHAR_T
typedef basic_string_view<wchar_t> wstring_view;
#endif
#if string_CONFIG_PROVIDE_CHAR8_T && string_HAVE_CHAR8_T
typedef basic_string_view<char8_t> u8string_view;
#endif
#if string_CONFIG_PROVIDE_CHAR16_T
typedef basic_string_view<char16_t> u16string_view;
#endif
#if string_CONFIG_PROVIDE_CHAR32_T
typedef basic_string_view<char32_t> u32string_view;
#endif
template< typename CharT >
string_nodiscard inline std::size_t size( basic_string_view<CharT> const & sv ) string_noexcept
{
return sv.size();
}
#endif // string_HAVE_STRING_VIEW
} // namespace std17
namespace std20 {
// type identity, to establish non-deduced contexts in template argument deduction:
template< typename T >
struct type_identity
{
typedef T type;
};
} // namespace std20
namespace std23 {
} // namespace std23
namespace detail {
//
// Utilities:
//
// null character:
template< typename CharT >
string_nodiscard string_constexpr CharT nullchr() string_noexcept
{
return 0;
}
// null C-string:
#if string_CONFIG_PROVIDE_CHAR_T
string_nodiscard inline string_constexpr char const * nullstr( char ) string_noexcept
{
return "";
}
#endif
#if string_CONFIG_PROVIDE_WCHAR_T
string_nodiscard inline string_constexpr wchar_t const * nullstr( wchar_t ) string_noexcept
{
return L"";
}
#endif
#if string_CONFIG_PROVIDE_CHAR8_T
string_nodiscard inline string_constexpr char8_t const * nullstr( char8_t ) string_noexcept
{
return u8"";
}
#endif
#if string_CONFIG_PROVIDE_CHAR16_T
string_nodiscard inline string_constexpr char16_t const * nullstr( char16_t ) string_noexcept
{
return u"";
}
#endif
#if string_CONFIG_PROVIDE_CHAR32_T
string_nodiscard inline string_constexpr char32_t const * nullstr( char32_t ) string_noexcept
{
return U"";
}
#endif
// default strip set:
#if string_CONFIG_PROVIDE_CHAR_T
string_nodiscard inline string_constexpr char const * default_strip_set( char ) string_noexcept
{
return " \t\n";
}
#endif
#if string_CONFIG_PROVIDE_WCHAR_T
string_nodiscard inline string_constexpr wchar_t const * default_strip_set( wchar_t ) string_noexcept
{
return L" \t\n";
}
#endif
#if string_CONFIG_PROVIDE_CHAR8_T
string_nodiscard inline string_constexpr char8_t const * default_strip_set( char8_t ) string_noexcept
{
return u8" \t\n";
}
#endif
#if string_CONFIG_PROVIDE_CHAR16_T
string_nodiscard inline string_constexpr char16_t const * default_strip_set( char16_t ) string_noexcept
{
return u" \t\n";
}
#endif
#if string_CONFIG_PROVIDE_CHAR32_T
string_nodiscard inline string_constexpr char32_t const * default_strip_set( char32_t ) string_noexcept
{
return U" \t\n";
}
#endif
// to_string(sv):
#if string_HAVE_STRING_VIEW
#define MK_DETAIL_TO_STRING_SV(T) \
string_nodiscard inline std::basic_string<T> \
to_string( std17::basic_string_view<T> sv ) \
{ \
return std::basic_string<T>( sv ); \
}
#else
#define MK_DETAIL_TO_STRING_SV(T) \
string_nodiscard inline std::basic_string<T> \
to_string( std17::basic_string_view<T> sv ) \
{ \
return std::basic_string<T>( sv.begin(), sv.end() ); \
}
#endif
#if string_CONFIG_PROVIDE_CHAR_T
MK_DETAIL_TO_STRING_SV( char )
#endif
#if string_CONFIG_PROVIDE_WCHAR_T
MK_DETAIL_TO_STRING_SV( wchar_t )
#endif
#if string_CONFIG_PROVIDE_CHAR8_T
MK_DETAIL_TO_STRING_SV( char8_t )
#endif
#if string_CONFIG_PROVIDE_CHAR16_T
MK_DETAIL_TO_STRING_SV( char16_t )
#endif
#if string_CONFIG_PROVIDE_CHAR32_T
MK_DETAIL_TO_STRING_SV( char32_t )
#endif
#undef MK_DETAIL_TO_STRING_SV
} // namespace detail
} // namespace string
// enable use of string-specific namespaces detail and stdxx:
using namespace string;
//
// Observers
//
// length()
#define string_MK_LENGTH(T) \
string_nodiscard inline string_constexpr std::size_t \
length( std17::basic_string_view<T> text ) string_noexcept \
{ \
return text.length(); \
}
// size()
#define string_MK_SIZE(T) \
string_nodiscard inline string_constexpr std::size_t \
size( std17::basic_string_view<T> text ) string_noexcept \
{ \
return text.size(); \
}
// is_empty()
#define string_MK_IS_EMPTY(T) \
string_nodiscard inline string_constexpr bool \
is_empty( std17::basic_string_view<T> text ) string_noexcept \
{ \
return text.empty(); \
}
//
// Searching:
//
namespace string {
namespace detail {
template< typename CharT, typename SeekT >
string_nodiscard std::size_t
find_first(
std17::basic_string_view<CharT> text
, SeekT const & seek, std::size_t pos ) string_noexcept
{
return text.find( seek, pos );
}
} // namespace detail
} // namespace string
// find_first()
#define string_MK_FIND_FIRST(CharT) \
template< typename SeekT > \
string_nodiscard std::size_t \
find_first( \
std17::basic_string_view<CharT> text \
, SeekT const & seek ) string_noexcept \
{ \
return text.find( seek ); \
}
#if string_CPP17_000
# define string_MK_FIND_FIRST_CHAR(CharT) \
string_nodiscard inline std::size_t \
find_first( \
std17::basic_string_view<CharT> text \
, CharT seek ) string_noexcept \
{ \
return find_first( text, std::basic_string<CharT>( &seek, &seek + 1 ) ); \
}
#else
# define string_MK_FIND_FIRST_CHAR(CharT) \
string_nodiscard inline std::size_t \
find_first( \
std17::basic_string_view<CharT> text \
, CharT seek ) string_noexcept \
{ \
return find_first( text, std17::basic_string_view<CharT>( &seek, &seek + 1 ) ); \
}
#endif
// find_last()
#define string_MK_FIND_LAST(CharT) \
template< typename SeekT > \
string_nodiscard std::size_t \
find_last( \
std17::basic_string_view<CharT> text \
, SeekT const & seek ) string_noexcept \
{ \
return text.rfind( seek ); \
}
#if string_CPP17_000
# define string_MK_FIND_LAST_CHAR(CharT) \
string_nodiscard inline std::size_t \
find_last( \
std17::basic_string_view<CharT> text \
, CharT seek ) \
{ \
return find_last( text, std::basic_string<CharT>( &seek, &seek + 1 ) ); \
}
#else
# define string_MK_FIND_LAST_CHAR(CharT) \
string_nodiscard inline std::size_t \
find_last( \
std17::basic_string_view<CharT> text \
, CharT seek ) \
{ \
return find_last( text, std17::basic_string_view<CharT>( &seek, &seek + 1 ) ); \
}
#endif
// find_first_of()
#define string_MK_FIND_FIRST_OF(CharT) \
template< typename SeekT > \
string_nodiscard std::size_t \
find_first_of( \
std17::basic_string_view<CharT> text \
, SeekT const & seek ) \
{ \
return text.find_first_of( seek ); \
}
// find_last_of()
#define string_MK_FIND_LAST_OF(CharT) \
template< typename SeekT > \
string_nodiscard std::size_t \
find_last_of( \
std17::basic_string_view<CharT> text \
, SeekT const & seek ) \
{ \
return text.find_last_of( seek ); \
}
// find_first_not_of()
#define string_MK_FIND_FIRST_NOT_OF(CharT) \
template< typename SeekT > \
string_nodiscard std::size_t \
find_first_not_of( \
std17::basic_string_view<CharT> text \
, SeekT const & seek ) \
{ \
return text.find_first_not_of( seek ); \
}
// find_last_not_of()
#define string_MK_FIND_LAST_NOT_OF(CharT) \
template< typename SeekT > \
string_nodiscard std::size_t \
find_last_not_of( \
std17::basic_string_view<CharT> text \
, SeekT const & seek ) \
{ \
return text.find_last_not_of( seek ); \
}
// TODO: ??? find_if()
// TODO: ??? find_if_not()
// contains() - C++23
#if string_CPP23_OR_GREATER
# define string_MK_CONTAINS(CharT) \
template< typename SeekT > \
string_nodiscard bool \
contains( \
std17::basic_string_view<CharT> text \
, SeekT const & seek ) string_noexcept \
{ \
return text.contains( seek ); \
}
#else
# define string_MK_CONTAINS(CharT) \
template< typename SeekT > \
string_nodiscard bool \
contains( \
std17::basic_string_view<CharT> text \
, SeekT const & seek ) string_noexcept \
{ \
return string::npos != find_first(text, seek); \
}
#endif
// contains_all_of()
# define string_MK_CONTAINS_ALL_OF(CharT) \
string_nodiscard inline bool \
contains_all_of( \
std17::basic_string_view<CharT> text \
, std17::basic_string_view<CharT> set ) \
{ \
for ( auto const chr : set ) \
{ \
if ( ! contains( text, chr ) ) \
return false; \
} \
return true; \
}
// contains_any_of()
# define string_MK_CONTAINS_ANY_OF(CharT) \
string_nodiscard inline bool \
contains_any_of( \
std17::basic_string_view<CharT> text \
, std17::basic_string_view<CharT> set ) \
{ \
for ( auto const chr : set ) \
{ \
if ( contains( text, chr ) ) \
return true; \
} \
return false; \
}
// contains_none_of()
# define string_MK_CONTAINS_NONE_OF(CharT) \
string_nodiscard inline bool \
contains_none_of( \
std17::basic_string_view<CharT> text \
, std17::basic_string_view<CharT> set ) \
{ \
return ! contains_any_of( text, set ); \
}
// starts_with() - C++20
#if string_CPP20_OR_GREATER