-
Notifications
You must be signed in to change notification settings - Fork 12
/
CorradeString.hpp
2517 lines (1992 loc) · 99 KB
/
CorradeString.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
/*
Corrade::Containers::String
Corrade::Containers::StringView
— lightweight and optimized string (view) classes
https://doc.magnum.graphics/corrade/classCorrade_1_1Containers_1_1BasicString.html
https://doc.magnum.graphics/corrade/classCorrade_1_1Containers_1_1BasicStringView.html
Depends on CorradeEnumSet.h, the implementation depends on CorradePair.h
and CorradeCpu.hpp.
This is a single-header library generated from the Corrade project. With
the goal being easy integration, it's deliberately free of all comments
to keep the file size small. More info, detailed changelogs and docs here:
- Project homepage — https://magnum.graphics/corrade/
- Documentation — https://doc.magnum.graphics/corrade/
- GitHub project page — https://github.com/mosra/corrade
- GitHub Singles repository — https://github.com/mosra/magnum-singles
The library has a separate non-inline implementation part, enable it *just
once* like this:
#define CORRADE_STRING_IMPLEMENTATION
#include <CorradeString.hpp>
If you need the deinlined symbols to be exported from a shared library,
`#define CORRADE_UTILITY_EXPORT` and `CORRADE_UTILITY_LOCAL` as
appropriate. Runtime CPU dispatch for the implementation is enabled by
default, you can disable it with `#define CORRADE_NO_CPU_RUNTIME_DISPATCH`
before including the file in both the headers and the implementation. To
enable the IFUNC functionality for CPU runtime dispatch,
`#define CORRADE_CPU_USE_IFUNC`.
The STL compatibility bits are included as well --- opt-in by specifying
either `#define CORRADE_STRING_STL_COMPATIBILITY` or
`#define CORRADE_STRING_STL_VIEW_COMPATIBILITY` before including the file.
Including it multiple times with different macros defined works too.
v2020.06-1846-gc4cdf (2025-01-07)
- Fixed embarrassing bugs in the NEON and WASM SIMD code paths for find()
- SFINAE is now done in template args as that's simpler for the compiler
- std::string STL compatibility is now inline, meaning the
CORRADE_STRING_STL_COMPATIBILITY macro doesn't need to be defined also
for CORRADE_STRING_IMPLEMENTATION anymore
v2020.06-1687-g6b5f (2024-06-29)
- New, SIMD-optimized count() API
- Literals are now available in an inline Literals::StringLiterals
namespace for finer control over which literals get actually used
- String copy construction and copy assignment now makes the copy a SSO
only if the original instance was a SSO as well
v2020.06-1502-g147e (2023-09-11)
- Initial release
Generated from Corrade v2020.06-1846-gc4cdf (2025-01-07), 2517 / 2176 LoC
*/
/*
This file is part of Corrade.
Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025
Vladimír Vondruš <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#if !defined(CORRADE_CONSTEXPR_ASSERT) && !defined(NDEBUG)
#include <cassert>
#endif
#include <cstddef>
#include <type_traits>
#include "CorradeEnumSet.h"
#ifdef _MSC_VER
#define CORRADE_TARGET_MSVC
#endif
#if !defined(__x86_64) && !defined(_M_X64) && !defined(__aarch64__) && !defined(_M_ARM64) && !defined(__powerpc64__) && !defined(__wasm64__)
#define CORRADE_TARGET_32BIT
#endif
#ifdef __BYTE_ORDER__
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define CORRADE_TARGET_BIG_ENDIAN
#elif __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
#error what kind of endianness is this?
#endif
#elif defined(__hppa__) || \
defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
(defined(__MIPS__) && defined(__MIPSEB__)) || \
defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
defined(__sparc__)
#define CORRADE_TARGET_BIG_ENDIAN
#endif
#ifndef CORRADE_NO_CPU_RUNTIME_DISPATCH
#define CORRADE_BUILD_CPU_RUNTIME_DISPATCH
#endif
#ifndef CorradeString_hpp
#define CorradeString_hpp
#ifndef CORRADE_UTILITY_EXPORT
#define CORRADE_UTILITY_EXPORT
#endif
#ifndef CORRADE_UTILITY_LOCAL
#define CORRADE_UTILITY_LOCAL
#endif
#if defined(CORRADE_BUILD_CPU_RUNTIME_DISPATCH) && !defined(CORRADE_CPU_USE_IFUNC)
#define CORRADE_UTILITY_CPU_DISPATCHER_DECLARATION(name) \
CORRADE_UTILITY_EXPORT decltype(name) name ## Implementation(Cpu::Features);
#define CORRADE_UTILITY_CPU_DISPATCHED_DECLARATION(name) (*name)
#else
#define CORRADE_UTILITY_CPU_DISPATCHER_DECLARATION(name)
#define CORRADE_UTILITY_CPU_DISPATCHED_DECLARATION(name) (name)
#endif
namespace Corrade { namespace Containers {
class String;
template<class> class BasicStringView;
typedef BasicStringView<const char> StringView;
template<class, class> class Pair;
}}
#endif
#ifndef Corrade_Corrade_h
#define Corrade_Corrade_h
namespace Corrade {
namespace Cpu {
class Features;
}
}
#endif
#ifndef Corrade_Utility_Move_h
#define Corrade_Utility_Move_h
#ifdef CORRADE_MSVC2015_COMPATIBILITY
#include <utility>
#endif
namespace Corrade { namespace Utility {
template<class T> constexpr T&& forward(typename std::remove_reference<T>::type& t) noexcept {
return static_cast<T&&>(t);
}
template<class T> constexpr T&& forward(typename std::remove_reference<T>::type&& t) noexcept {
static_assert(!std::is_lvalue_reference<T>::value, "T can't be a lvalue reference");
return static_cast<T&&>(t);
}
template<class T> constexpr typename std::remove_reference<T>::type&& move(T&& t) noexcept {
return static_cast<typename std::remove_reference<T>::type&&>(t);
}
#ifndef CORRADE_MSVC2015_COMPATIBILITY
template<class T> void swap(T& a, typename std::common_type<T>::type& b) noexcept(std::is_nothrow_move_constructible<T>::value && std::is_nothrow_move_assignable<T>::value) {
T tmp = static_cast<T&&>(a);
a = static_cast<T&&>(b);
b = static_cast<T&&>(tmp);
}
#else
using std::swap;
#endif
#ifndef CORRADE_MSVC2015_COMPATIBILITY
template<std::size_t size, class T> void swap(T(&a)[size], typename std::common_type<T(&)[size]>::type b) noexcept(std::is_nothrow_move_constructible<T>::value && std::is_nothrow_move_assignable<T>::value) {
for(std::size_t i = 0; i != size; ++i) {
T tmp = static_cast<T&&>(a[i]);
a[i] = static_cast<T&&>(b[i]);
b[i] = static_cast<T&&>(tmp);
}
}
#endif
}}
#endif
#ifndef CORRADE_CONSTEXPR_ASSERT
#ifdef NDEBUG
#define CORRADE_CONSTEXPR_ASSERT(condition, message) static_cast<void>(0)
#else
#define CORRADE_CONSTEXPR_ASSERT(condition, message) \
static_cast<void>((condition) ? 0 : ([&]() { \
assert(!#condition); \
}(), 0))
#endif
#endif
#ifndef CORRADE_CONSTEXPR_DEBUG_ASSERT
#define CORRADE_CONSTEXPR_DEBUG_ASSERT(condition, message) \
CORRADE_CONSTEXPR_ASSERT(condition, message)
#endif
#ifndef Corrade_Containers_StringView_h
#define Corrade_Containers_StringView_h
namespace Corrade { namespace Containers {
namespace Implementation {
template<class, class> struct StringViewConverter;
}
enum class StringViewFlag: std::size_t {
Global = std::size_t{1} << (sizeof(std::size_t)*8 - 1),
NullTerminated = std::size_t{1} << (sizeof(std::size_t)*8 - 2)
};
namespace Implementation {
enum: std::size_t {
StringViewSizeMask = std::size_t(StringViewFlag::NullTerminated)|std::size_t(StringViewFlag::Global)
};
}
typedef EnumSet<StringViewFlag
, Implementation::StringViewSizeMask
> StringViewFlags;
CORRADE_ENUMSET_OPERATORS(StringViewFlags)
template<class T> class
#ifndef CORRADE_TARGET_MSVC
CORRADE_UTILITY_EXPORT
#endif
BasicStringView {
public:
template<class U, class = typename std::enable_if<std::is_same<std::nullptr_t, U>::value>::type> constexpr /*implicit*/ BasicStringView(U) noexcept: _data{}, _sizePlusFlags{std::size_t(StringViewFlag::Global)} {}
constexpr /*implicit*/ BasicStringView() noexcept: _data{}, _sizePlusFlags{std::size_t(StringViewFlag::Global)} {}
constexpr /*implicit*/ BasicStringView(T* data, std::size_t size, StringViewFlags flags = {}) noexcept: _data{data}, _sizePlusFlags{(
#ifdef CORRADE_TARGET_32BIT
CORRADE_CONSTEXPR_DEBUG_ASSERT(size < std::size_t{1} << (sizeof(std::size_t)*8 - 2),
"Containers::StringView: string expected to be smaller than 2^" << Utility::Debug::nospace << sizeof(std::size_t)*8 - 2 << "bytes, got" << size),
#endif
CORRADE_CONSTEXPR_DEBUG_ASSERT(data || !(flags & StringViewFlag::NullTerminated),
"Containers::StringView: can't use StringViewFlag::NullTerminated with null data"),
size|(std::size_t(flags) & Implementation::StringViewSizeMask))} {}
/*implicit*/ BasicStringView(String& data) noexcept;
template<class U = T, class = typename std::enable_if<std::is_const<U>::value>::type> /*implicit*/ BasicStringView(const String& data) noexcept;
template<class U, class = typename std::enable_if<std::is_same<const U, T>::value>::type> constexpr /*implicit*/ BasicStringView(BasicStringView<U> mutable_) noexcept: _data{mutable_._data}, _sizePlusFlags{mutable_._sizePlusFlags} {}
template<class U, class = typename std::enable_if<std::is_pointer<U>::value && std::is_convertible<const U&, T*>::value>::type> /*implicit*/ BasicStringView(U data, StringViewFlags extraFlags = {}) noexcept: BasicStringView{data, extraFlags, nullptr} {}
template<class U, class = decltype(Implementation::StringViewConverter<T, typename std::decay<U&&>::type>::from(std::declval<U&&>()))> constexpr /*implicit*/ BasicStringView(U&& other) noexcept: BasicStringView{Implementation::StringViewConverter<T, typename std::decay<U&&>::type>::from(Utility::forward<U>(other))} {}
template<class U, class = decltype(Implementation::StringViewConverter<T, U>::to(std::declval<BasicStringView<T>>()))> constexpr /*implicit*/ operator U() const {
return Implementation::StringViewConverter<T, U>::to(*this);
}
constexpr explicit operator bool() const {
return _data && (_sizePlusFlags & ~Implementation::StringViewSizeMask);
}
constexpr StringViewFlags flags() const {
return StringViewFlag(_sizePlusFlags & Implementation::StringViewSizeMask);
}
constexpr T* data() const { return _data; }
constexpr std::size_t size() const {
return _sizePlusFlags & ~Implementation::StringViewSizeMask;
}
constexpr bool isEmpty() const {
return !(_sizePlusFlags & ~Implementation::StringViewSizeMask);
}
constexpr T* begin() const { return _data; }
constexpr T* cbegin() const { return _data; }
constexpr T* end() const {
return _data + (_sizePlusFlags & ~Implementation::StringViewSizeMask);
}
constexpr T* cend() const {
return _data + (_sizePlusFlags & ~Implementation::StringViewSizeMask);
}
constexpr T& front() const;
constexpr T& back() const;
constexpr T& operator[](std::size_t i) const;
constexpr BasicStringView<T> slice(T* begin, T* end) const;
constexpr BasicStringView<T> slice(std::size_t begin, std::size_t end) const;
template<class U, class = typename std::enable_if<std::is_convertible<U, T*>::value && !std::is_convertible<U, std::size_t>::value>::type> constexpr BasicStringView<T> sliceSize(U begin, std::size_t size) const {
return slice(begin, begin + size);
}
constexpr BasicStringView<T> sliceSize(std::size_t begin, std::size_t size) const {
return slice(begin, begin + size);
}
template<class U, class = typename std::enable_if<std::is_convertible<U, T*>::value && !std::is_convertible<U, std::size_t>::value>::type> constexpr BasicStringView<T> prefix(U end) const {
return static_cast<T*>(end) ? slice(_data, end) : BasicStringView<T>{};
}
constexpr BasicStringView<T> suffix(T* begin) const {
return _data && !begin ? BasicStringView<T>{} : slice(begin, _data + (_sizePlusFlags & ~Implementation::StringViewSizeMask));
}
constexpr BasicStringView<T> prefix(std::size_t size) const {
return slice(0, size);
}
constexpr BasicStringView<T> exceptPrefix(std::size_t size) const {
return slice(size, _sizePlusFlags & ~Implementation::StringViewSizeMask);
}
constexpr BasicStringView<T> exceptSuffix(std::size_t size) const {
return slice(0, (_sizePlusFlags & ~Implementation::StringViewSizeMask) - size);
}
bool hasPrefix(StringView prefix) const;
bool hasPrefix(char prefix) const;
bool hasSuffix(StringView suffix) const;
bool hasSuffix(char suffix) const;
BasicStringView<T> exceptPrefix(StringView prefix) const;
template<class = typename std::enable_if<std::is_same<typename std::decay<T>::type, char>::value>::type> BasicStringView<T> exceptPrefix(T&& prefix) const = delete;
BasicStringView<T> exceptSuffix(StringView suffix) const;
template<class = typename std::enable_if<std::is_same<typename std::decay<T>::type, char>::value>::type> BasicStringView<T> exceptSuffix(T&& suffix) const = delete;
BasicStringView<T> trimmed(StringView characters) const {
return trimmedPrefix(characters).trimmedSuffix(characters);
}
BasicStringView<T> trimmed() const;
BasicStringView<T> trimmedPrefix(StringView characters) const;
BasicStringView<T> trimmedPrefix() const;
BasicStringView<T> trimmedSuffix(StringView characters) const;
BasicStringView<T> trimmedSuffix() const;
BasicStringView<T> find(StringView substring) const {
return findOr(substring, nullptr);
}
BasicStringView<T> find(char character) const {
return findOr(character, nullptr);
}
BasicStringView<T> findOr(StringView substring, T* fail) const;
BasicStringView<T> findOr(char character, T* fail) const;
BasicStringView<T> findLast(StringView substring) const {
return findLastOr(substring, nullptr);
}
BasicStringView<T> findLast(char character) const {
return findLastOr(character, nullptr);
}
BasicStringView<T> findLastOr(StringView substring, T* fail) const;
BasicStringView<T> findLastOr(char character, T* fail) const;
bool contains(StringView substring) const;
bool contains(char character) const;
BasicStringView<T> findAny(StringView characters) const {
return findAnyOr(characters, nullptr);
}
BasicStringView<T> findAnyOr(StringView characters, T* fail) const;
BasicStringView<T> findLastAny(StringView characters) const {
return findLastAnyOr(characters, nullptr);
}
BasicStringView<T> findLastAnyOr(StringView characters, T* fail) const;
bool containsAny(StringView substring) const;
std::size_t count(char character) const;
private:
template<class> friend class BasicStringView;
friend String;
friend CORRADE_UTILITY_EXPORT bool operator==(StringView, StringView);
friend CORRADE_UTILITY_EXPORT bool operator!=(StringView, StringView);
friend CORRADE_UTILITY_EXPORT bool operator<(StringView, StringView);
friend CORRADE_UTILITY_EXPORT bool operator<=(StringView, StringView);
friend CORRADE_UTILITY_EXPORT bool operator>=(StringView, StringView);
friend CORRADE_UTILITY_EXPORT bool operator>(StringView, StringView);
friend CORRADE_UTILITY_EXPORT String operator+(StringView, StringView);
friend CORRADE_UTILITY_EXPORT String operator*(StringView, std::size_t);
explicit BasicStringView(T* data, StringViewFlags flags, std::nullptr_t) noexcept;
constexpr explicit BasicStringView(T* data, std::size_t sizePlusFlags, std::nullptr_t) noexcept: _data{data}, _sizePlusFlags{sizePlusFlags} {}
T* _data;
std::size_t _sizePlusFlags;
};
typedef BasicStringView<const char> StringView;
typedef BasicStringView<char> MutableStringView;
CORRADE_UTILITY_EXPORT bool operator==(StringView a, StringView b);
CORRADE_UTILITY_EXPORT bool operator!=(StringView a, StringView b);
CORRADE_UTILITY_EXPORT bool operator<(StringView a, StringView b);
CORRADE_UTILITY_EXPORT bool operator<=(StringView a, StringView b);
CORRADE_UTILITY_EXPORT bool operator>=(StringView a, StringView b);
CORRADE_UTILITY_EXPORT bool operator>(StringView a, StringView b);
CORRADE_UTILITY_EXPORT String operator+(StringView a, StringView b);
CORRADE_UTILITY_EXPORT String operator*(StringView string, std::size_t count);
CORRADE_UTILITY_EXPORT String operator*(std::size_t count, StringView string);
namespace Literals {
inline
namespace StringLiterals {
#if defined(CORRADE_TARGET_CLANG) && __clang_major__ >= 17
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-literal-operator"
#endif
constexpr StringView operator"" _s(const char* data, std::size_t size) {
return StringView{data, size, StringViewFlag(std::size_t(StringViewFlag::Global)|std::size_t(StringViewFlag::NullTerminated))};
}
#if defined(CORRADE_TARGET_CLANG) && __clang_major__ >= 17
#pragma clang diagnostic pop
#endif
}}
template<class T> constexpr T& BasicStringView<T>::operator[](const std::size_t i) const {
return CORRADE_CONSTEXPR_DEBUG_ASSERT(i < size() + (flags() & StringViewFlag::NullTerminated ? 1 : 0),
"Containers::StringView::operator[](): index" << i << "out of range for" << size() << (flags() & StringViewFlag::NullTerminated ? "null-terminated bytes" : "bytes")), _data[i];
}
template<class T> constexpr T& BasicStringView<T>::front() const {
return CORRADE_CONSTEXPR_DEBUG_ASSERT(size(), "Containers::StringView::front(): view is empty"), _data[0];
}
template<class T> constexpr T& BasicStringView<T>::back() const {
return CORRADE_CONSTEXPR_DEBUG_ASSERT(size(), "Containers::StringView::back(): view is empty"), _data[size() - 1];
}
template<class T> constexpr BasicStringView<T> BasicStringView<T>::slice(T* const begin, T* const end) const {
return CORRADE_CONSTEXPR_DEBUG_ASSERT(_data <= begin && begin <= end && end <= _data + (_sizePlusFlags & ~Implementation::StringViewSizeMask),
"Containers::StringView::slice(): slice ["
<< Utility::Debug::nospace << begin - _data
<< Utility::Debug::nospace << ":"
<< Utility::Debug::nospace << end - _data
<< Utility::Debug::nospace << "] out of range for"
<< (_sizePlusFlags & ~Implementation::StringViewSizeMask) << "elements"),
BasicStringView<T>{begin, std::size_t(end - begin)|
(_sizePlusFlags & std::size_t(StringViewFlag::Global))|
((_sizePlusFlags & std::size_t(StringViewFlag::NullTerminated))*(end == _data + (_sizePlusFlags & ~Implementation::StringViewSizeMask))),
nullptr};
}
template<class T> constexpr BasicStringView<T> BasicStringView<T>::slice(const std::size_t begin, const std::size_t end) const {
return CORRADE_CONSTEXPR_DEBUG_ASSERT(begin <= end && end <= (_sizePlusFlags & ~Implementation::StringViewSizeMask),
"Containers::StringView::slice(): slice ["
<< Utility::Debug::nospace << begin
<< Utility::Debug::nospace << ":"
<< Utility::Debug::nospace << end
<< Utility::Debug::nospace << "] out of range for"
<< (_sizePlusFlags & ~Implementation::StringViewSizeMask) << "elements"),
BasicStringView<T>{_data + begin, (end - begin)|
(_sizePlusFlags & std::size_t(StringViewFlag::Global))|
((_sizePlusFlags & std::size_t(StringViewFlag::NullTerminated))*(end == (_sizePlusFlags & ~Implementation::StringViewSizeMask))),
nullptr};
}
namespace Implementation {
CORRADE_UTILITY_EXPORT const char* stringFindString(const char* data, std::size_t size, const char* substring, std::size_t substringSize);
CORRADE_UTILITY_EXPORT const char* stringFindLastString(const char* data, std::size_t size, const char* substring, std::size_t substringSize);
CORRADE_UTILITY_EXPORT extern const char* CORRADE_UTILITY_CPU_DISPATCHED_DECLARATION(stringFindCharacter)(const char* data, std::size_t size, char character);
CORRADE_UTILITY_CPU_DISPATCHER_DECLARATION(stringFindCharacter)
CORRADE_UTILITY_EXPORT const char* stringFindLastCharacter(const char* data, std::size_t size, char character);
CORRADE_UTILITY_EXPORT const char* stringFindAny(const char* data, std::size_t size, const char* characters, std::size_t characterCount);
CORRADE_UTILITY_EXPORT const char* stringFindLastAny(const char* data, std::size_t size, const char* characters, std::size_t characterCount);
CORRADE_UTILITY_EXPORT const char* stringFindNotAny(const char* data, std::size_t size, const char* characters, std::size_t characterCount);
CORRADE_UTILITY_EXPORT const char* stringFindLastNotAny(const char* data, std::size_t size, const char* characters, std::size_t characterCount);
CORRADE_UTILITY_EXPORT extern std::size_t CORRADE_UTILITY_CPU_DISPATCHED_DECLARATION(stringCountCharacter)(const char* data, std::size_t size, char character);
CORRADE_UTILITY_CPU_DISPATCHER_DECLARATION(stringCountCharacter)
}
template<class T> inline BasicStringView<T> BasicStringView<T>::trimmedPrefix(const StringView characters) const {
const std::size_t size = this->size();
T* const found = const_cast<T*>(Implementation::stringFindNotAny(_data, size, characters._data, characters.size()));
return suffix(found ? found : _data + size);
}
template<class T> inline BasicStringView<T> BasicStringView<T>::trimmedSuffix(const StringView characters) const {
T* const found = const_cast<T*>(Implementation::stringFindLastNotAny(_data, size(), characters._data, characters.size()));
return prefix(found ? found + 1 : _data);
}
template<class T> inline BasicStringView<T> BasicStringView<T>::findOr(const StringView substring, T* const fail) const {
const std::size_t substringSize = substring.size();
if(const char* const found = Implementation::stringFindString(_data, size(), substring._data, substringSize))
return slice(const_cast<T*>(found), const_cast<T*>(found + substringSize));
return BasicStringView<T>{fail, 0 /* empty, no flags */, nullptr};
}
template<class T> inline BasicStringView<T> BasicStringView<T>::findOr(const char character, T* const fail) const {
if(const char* const found = Implementation::stringFindCharacter(_data, size(), character))
return slice(const_cast<T*>(found), const_cast<T*>(found + 1));
return BasicStringView<T>{fail, 0 /* empty, no flags */, nullptr};
}
template<class T> inline BasicStringView<T> BasicStringView<T>::findLastOr(const StringView substring, T* const fail) const {
const std::size_t substringSize = substring.size();
if(const char* const found = Implementation::stringFindLastString(_data, size(), substring._data, substringSize))
return slice(const_cast<T*>(found), const_cast<T*>(found + substringSize));
return BasicStringView<T>{fail, 0 /* empty, no flags */, nullptr};
}
template<class T> inline BasicStringView<T> BasicStringView<T>::findLastOr(const char character, T* const fail) const {
if(const char* const found = Implementation::stringFindLastCharacter(_data, size(), character))
return slice(const_cast<T*>(found), const_cast<T*>(found + 1));
return BasicStringView<T>{fail, 0 /* empty, no flags */, nullptr};
}
template<class T> inline bool BasicStringView<T>::contains(const StringView substring) const {
return Implementation::stringFindString(_data, size(), substring._data, substring.size());
}
template<class T> inline bool BasicStringView<T>::contains(const char character) const {
return Implementation::stringFindCharacter(_data, size(), character);
}
template<class T> inline BasicStringView<T> BasicStringView<T>::findAnyOr(const StringView characters, T* const fail) const {
if(const char* const found = Implementation::stringFindAny(_data, size(), characters._data, characters.size()))
return slice(const_cast<T*>(found), const_cast<T*>(found + 1));
return BasicStringView<T>{fail, 0 /* empty, no flags */, nullptr};
}
template<class T> inline BasicStringView<T> BasicStringView<T>::findLastAnyOr(const StringView characters, T* const fail) const {
if(const char* const found = Implementation::stringFindLastAny(_data, size(), characters._data, characters.size()))
return slice(const_cast<T*>(found), const_cast<T*>(found + 1));
return BasicStringView<T>{fail, 0 /* empty, no flags */, nullptr};
}
template<class T> inline bool BasicStringView<T>::containsAny(const StringView characters) const {
return Implementation::stringFindAny(_data, size(), characters._data, characters.size());
}
template<class T> inline std::size_t BasicStringView<T>::count(const char character) const {
return Implementation::stringCountCharacter(_data, size(), character);
}
}}
#endif
#ifndef Corrade_Containers_String_h
#define Corrade_Containers_String_h
namespace Corrade { namespace Containers {
namespace Implementation {
template<class> struct StringConverter;
enum: std::size_t {
SmallStringBit = 0x40,
SmallStringSize = sizeof(std::size_t)*3 - 1
};
}
struct AllocatedInitT {
struct Init {};
constexpr explicit AllocatedInitT(Init) {}
};
constexpr AllocatedInitT AllocatedInit{AllocatedInitT::Init{}};
class CORRADE_UTILITY_EXPORT String {
public:
typedef void(*Deleter)(char*, std::size_t);
static String nullTerminatedView(StringView view);
static String nullTerminatedView(AllocatedInitT, StringView view);
static String nullTerminatedGlobalView(StringView view);
static String nullTerminatedGlobalView(AllocatedInitT, StringView view);
/*implicit*/ String() noexcept;
/*implicit*/ String(StringView view);
/*implicit*/ String(MutableStringView view);
template<class T, class = typename std::enable_if<std::is_convertible<T, const char*>::value && !std::is_convertible<T, std::size_t>::value>::type> /*implicit*/ String(T data): String{nullptr, nullptr, nullptr, data} {}
/*implicit*/ String(const char* data, std::size_t size);
explicit String(AllocatedInitT, StringView view);
explicit String(AllocatedInitT, MutableStringView view);
explicit String(AllocatedInitT, String&& other);
explicit String(AllocatedInitT, const String& other);
explicit String(AllocatedInitT, const char* data);
explicit String(AllocatedInitT, const char* data, std::size_t size);
explicit String(char* data, std::size_t size, Deleter deleter) noexcept;
template<class T, typename std::enable_if<std::is_convertible<T, Deleter>::value && !std::is_convertible<T, std::size_t>::value, int>::type = 0> String(char* data, T deleter) noexcept: String{deleter, nullptr, data} {}
explicit String(const char* data, std::size_t size, Deleter deleter) noexcept: String{const_cast<char*>(data), size, deleter} {}
template<class T, class = typename std::enable_if<std::is_convertible<T, Deleter>::value && !std::is_convertible<T, std::size_t>::value, const char*>::type> String(const char* data, T deleter) noexcept: String{deleter, nullptr, const_cast<char*>(data)} {}
explicit String(std::nullptr_t, std::size_t size, Deleter deleter) = delete;
template<class T, typename std::enable_if<std::is_convertible<T, Deleter>::value && !std::is_convertible<T, std::size_t>::value, int>::type = 0> String(std::nullptr_t, T) noexcept = delete;
explicit String(Corrade::ValueInitT, std::size_t size);
explicit String(Corrade::NoInitT, std::size_t size);
explicit String(Corrade::DirectInitT, std::size_t size, char c);
template<class T, class = decltype(Implementation::StringConverter<typename std::decay<T&&>::type>::from(std::declval<T&&>()))> /*implicit*/ String(T&& other) noexcept: String{Implementation::StringConverter<typename std::decay<T&&>::type>::from(Utility::forward<T>(other))} {}
~String();
String(const String& other);
String(String&& other) noexcept;
String& operator=(const String& other);
String& operator=(String&& other) noexcept;
template<class T, class = decltype(Implementation::StringConverter<T>::to(std::declval<String>()))> /*implicit*/ operator T() const {
return Implementation::StringConverter<T>::to(*this);
}
explicit operator bool() const;
bool isSmall() const {
return _small.size & Implementation::SmallStringBit;
}
StringViewFlags viewFlags() const;
char* data();
const char* data() const;
Deleter deleter() const;
bool isEmpty() const;
std::size_t size() const;
char* begin();
const char* begin() const;
const char* cbegin() const;
char* end();
const char* end() const;
const char* cend() const;
char& front();
char front() const;
char& back();
char back() const;
char& operator[](std::size_t i);
char operator[](std::size_t i) const;
MutableStringView slice(char* begin, char* end);
StringView slice(const char* begin, const char* end) const;
MutableStringView slice(std::size_t begin, std::size_t end);
StringView slice(std::size_t begin, std::size_t end) const;
template<class T, class = typename std::enable_if<std::is_convertible<T, char*>::value && !std::is_convertible<T, std::size_t>::value>::type> MutableStringView sliceSize(T begin, std::size_t size) {
return sliceSizePointerInternal(begin, size);
}
template<class T, class = typename std::enable_if<std::is_convertible<T, const char*>::value && !std::is_convertible<T, std::size_t>::value>::type> StringView sliceSize(T begin, std::size_t size) const {
return sliceSizePointerInternal(begin, size);
}
MutableStringView sliceSize(std::size_t begin, std::size_t size);
StringView sliceSize(std::size_t begin, std::size_t size) const;
template<class T, class = typename std::enable_if<std::is_convertible<T, char*>::value && !std::is_convertible<T, std::size_t>::value>::type> MutableStringView prefix(T end) {
return prefixPointerInternal(end);
}
template<class T, class = typename std::enable_if<std::is_convertible<T, const char*>::value && !std::is_convertible<T, std::size_t>::value>::type> StringView prefix(T end) const {
return prefixPointerInternal(end);
}
MutableStringView suffix(char* begin);
StringView suffix(const char* begin) const;
MutableStringView prefix(std::size_t size);
StringView prefix(std::size_t size) const;
MutableStringView exceptPrefix(std::size_t size);
StringView exceptPrefix(std::size_t size) const;
MutableStringView exceptSuffix(std::size_t size);
StringView exceptSuffix(std::size_t size) const;
bool hasPrefix(StringView prefix) const;
bool hasPrefix(char prefix) const;
bool hasSuffix(StringView suffix) const;
bool hasSuffix(char suffix) const;
MutableStringView exceptPrefix(StringView prefix);
StringView exceptPrefix(StringView prefix) const;
template<class T, class = typename std::enable_if<std::is_same<typename std::decay<T>::type, char>::value>::type> MutableStringView exceptPrefix(T&& prefix) = delete;
template<class T, class = typename std::enable_if<std::is_same<typename std::decay<T>::type, char>::value>::type> StringView exceptPrefix(T&& prefix) const = delete;
MutableStringView exceptSuffix(StringView suffix);
StringView exceptSuffix(StringView suffix) const;
template<class T, class = typename std::enable_if<std::is_same<typename std::decay<T>::type, char>::value>::type> MutableStringView exceptSuffix(T&& suffix) = delete;
template<class T, class = typename std::enable_if<std::is_same<typename std::decay<T>::type, char>::value>::type> StringView exceptSuffix(T&& suffix) const = delete;
MutableStringView trimmed(StringView characters);
StringView trimmed(StringView characters) const;
MutableStringView trimmed();
StringView trimmed() const;
MutableStringView trimmedPrefix(StringView characters);
StringView trimmedPrefix(StringView characters) const;
MutableStringView trimmedPrefix();
StringView trimmedPrefix() const;
MutableStringView trimmedSuffix(StringView characters);
StringView trimmedSuffix(StringView characters) const;
MutableStringView trimmedSuffix();
StringView trimmedSuffix() const;
MutableStringView find(StringView substring);
StringView find(StringView substring) const;
MutableStringView find(char character);
StringView find(char character) const;
MutableStringView findOr(StringView substring, char* fail);
StringView findOr(StringView substring, const char* fail) const;
MutableStringView findOr(char character, char* fail);
StringView findOr(char character, const char* fail) const;
MutableStringView findLast(StringView substring);
StringView findLast(StringView substring) const;
MutableStringView findLast(char character);
StringView findLast(char character) const;
MutableStringView findLastOr(StringView substring, char* fail);
StringView findLastOr(StringView substring, const char* fail) const;
MutableStringView findLastOr(char character, char* fail);
StringView findLastOr(char character, const char* fail) const;
bool contains(StringView substring) const;
bool contains(char character) const;
MutableStringView findAny(StringView characters);
StringView findAny(StringView characters) const;
MutableStringView findAnyOr(StringView characters, char* fail);
StringView findAnyOr(StringView characters, const char* fail) const;
MutableStringView findLastAny(StringView characters);
StringView findLastAny(StringView characters) const;
MutableStringView findLastAnyOr(StringView characters, char* fail);
StringView findLastAnyOr(StringView characters, const char* fail) const;
bool containsAny(StringView substring) const;
std::size_t count(char character) const;
char* release();
private:
explicit String(std::nullptr_t, std::nullptr_t, std::nullptr_t, const char* data);
explicit String(Deleter deleter, std::nullptr_t, char* data) noexcept;
CORRADE_UTILITY_LOCAL void construct(Corrade::NoInitT, std::size_t size);
CORRADE_UTILITY_LOCAL void construct(const char* data, std::size_t size);
CORRADE_UTILITY_LOCAL void copyConstruct(const String& other);
CORRADE_UTILITY_LOCAL void destruct();
CORRADE_UTILITY_LOCAL Pair<const char*, std::size_t> dataInternal() const;
MutableStringView sliceSizePointerInternal(char* begin, std::size_t size);
StringView sliceSizePointerInternal(const char* begin, std::size_t size) const;
MutableStringView prefixPointerInternal(char* end);
StringView prefixPointerInternal(const char* end) const;
struct Small {
#ifdef CORRADE_TARGET_BIG_ENDIAN
unsigned char size;
char data[Implementation::SmallStringSize];
#else
char data[Implementation::SmallStringSize];
unsigned char size;
#endif
};
struct Large {
#ifdef CORRADE_TARGET_BIG_ENDIAN
std::size_t size;
char* data;
void(*deleter)(char*, std::size_t);
#else
char* data;
void(*deleter)(char*, std::size_t);
std::size_t size;
#endif
};
union {
Small _small;
Large _large;
};
};
}}
#endif
#ifdef CORRADE_STRING_STL_COMPATIBILITY
#include <string>
#ifndef Corrade_Containers_StringStl_h
#define Corrade_Containers_StringStl_h
namespace Corrade { namespace Containers { namespace Implementation {
template<> struct CORRADE_UTILITY_EXPORT StringConverter<std::string> {
static String from(const std::string& other);
static std::string to(const String& other);
};
template<> struct CORRADE_UTILITY_EXPORT StringViewConverter<const char, std::string> {
static StringView from(const std::string& other);
static std::string to(StringView other);
};
template<> struct CORRADE_UTILITY_EXPORT StringViewConverter<char, std::string> {
static MutableStringView from(std::string& other);
static std::string to(MutableStringView other);
};
#define CORRADE_STRING_STL_INLINE inline
CORRADE_STRING_STL_INLINE StringView StringViewConverter<const char, std::string>::from(const std::string& other) {
return StringView{other.data(), other.size(), StringViewFlag::NullTerminated};
}
CORRADE_STRING_STL_INLINE std::string StringViewConverter<const char, std::string>::to(StringView other) {
return std::string{other.data(), other.size()};
}
CORRADE_STRING_STL_INLINE MutableStringView StringViewConverter<char, std::string>::from(std::string& other) {
return MutableStringView{&other[0], other.size(), StringViewFlag::NullTerminated};
}
CORRADE_STRING_STL_INLINE std::string StringViewConverter<char, std::string>::to(MutableStringView other) {
return std::string{other.data(), other.size()};
}
CORRADE_STRING_STL_INLINE String StringConverter<std::string>::from(const std::string& other) {
return String{other.data(), other.size()};
}
CORRADE_STRING_STL_INLINE std::string StringConverter<std::string>::to(const String& other) {
return std::string{other.data(), other.size()};
}
}}}
#endif
#endif
#ifdef CORRADE_STRING_STL_VIEW_COMPATIBILITY
#include <string_view>
#ifndef Corrade_Containers_StringStlView_h
#define Corrade_Containers_StringStlView_h
namespace Corrade { namespace Containers { namespace Implementation {
template<> struct StringConverter<std::string_view> {
static String from(std::string_view other) {
return String{other.data(), other.size()};
}
static std::string_view to(const String& other) {
return std::string_view{other.data(), other.size()};
}
};
template<> struct StringViewConverter<const char, std::string_view> {
static StringView from(std::string_view other) {
return StringView{other.data(), other.size()};
}
static std::string_view to(StringView other) {
return std::string_view{other.data(), other.size()};
}
};
template<> struct StringViewConverter<char, std::string_view> {
static std::string_view to(MutableStringView other) {
return std::string_view{other.data(), other.size()};
}
};
}}}
#endif
#endif
#ifdef CORRADE_STRING_IMPLEMENTATION
#include <cstdint>
#include <cstdlib>
#include <cstring>
#if (!defined(CORRADE_ASSERT) || !defined(CORRADE_INTERNAL_ASSERT) || !defined(CORRADE_INTERNAL_ASSERT_UNREACHABLE)) && !defined(NDEBUG)
#include <cassert>
#endif
#ifndef CORRADE_ASSERT
#ifdef NDEBUG
#define CORRADE_ASSERT(condition, message, returnValue) do {} while(false)
#else
#define CORRADE_ASSERT(condition, message, returnValue) assert(condition)
#endif
#endif
#ifndef CORRADE_INTERNAL_ASSERT
#ifdef NDEBUG
#define CORRADE_INTERNAL_ASSERT(condition) do {} while(false)
#else
#define CORRADE_INTERNAL_ASSERT(condition) assert(condition)
#endif
#endif
#ifndef CORRADE_INTERNAL_ASSERT_UNREACHABLE
#ifdef NDEBUG
#ifdef CORRADE_TARGET_GCC
#define CORRADE_INTERNAL_ASSERT_UNREACHABLE() __builtin_unreachable()
#elif defined(CORRADE_TARGET_MSVC)
#define CORRADE_INTERNAL_ASSERT_UNREACHABLE() __assume(0)
#else
#define CORRADE_INTERNAL_ASSERT_UNREACHABLE() std::abort()
#endif
#else