forked from CloverHackyColor/CloverBootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXStringAbstract.h
More file actions
1737 lines (1589 loc) · 58.2 KB
/
Copy pathXStringAbstract.h
File metadata and controls
1737 lines (1589 loc) · 58.2 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
/*
*
* Created by jief in 1997.
* Copyright (c) 2020 Jief
* All rights reserved.
*
*/
#if !defined(__XSTRINGABSTRACT_H__)
#define __XSTRINGABSTRACT_H__
#include "XToolsCommon.h"
#include "unicode_conversions.h"
#include <XToolsConf.h>
#ifndef DEBUG_ALL
#define DEBUG_XStringAbstract 0
#else
#define DEBUG_XStringAbstract DEBUG_ALL
#endif
#if DEBUG_XStringAbstract == 0
#define DBG_XSTRING(...)
#else
#define DBG_XSTRING(...) DebugLog(DEBUG_XStringAbstract, __VA_ARGS__)
#endif
// #define XSTRING_CACHING_OF_SIZE
#define asciiToLower(ch) \
(((ch >= L'A') && (ch <= L'Z')) ? ((ch - L'A') + L'a') : ch)
#define asciiToUpper(ch) \
(((ch >= L'a') && (ch <= L'z')) ? ((ch - L'a') + L'A') : ch)
template <typename S, typename O>
int XStringAbstract__startWith(const S *src, const O *other, bool ignoreCase) {
// size_t nb = 0;
const S *src2 = src;
const O *other2 = other;
char32_t src_char32;
char32_t other_char32;
other2 = get_char32_from_string(other2, &other_char32);
if (!other_char32)
return true; // startWith with empty string is considered true
src2 = get_char32_from_string(src2, &src_char32);
while (other_char32) {
if (ignoreCase) {
src_char32 = asciiToLower(src_char32);
other_char32 = asciiToLower(other_char32);
}
if (src_char32 != other_char32)
return false;
src2 = get_char32_from_string(src2, &src_char32);
other2 = get_char32_from_string(other2, &other_char32);
// nb += 1;
};
return src_char32 != 0;
}
template <typename S, typename O>
int XStringAbstract__startWithOrEqualTo(const S *src, const O *other,
bool ignoreCase) {
// size_t nb = 0;
const S *src2 = src;
const O *other2 = other;
char32_t src_char32;
char32_t other_char32;
other2 = get_char32_from_string(other2, &other_char32);
if (!other_char32)
return true; // startWith with empty string is considered true
src2 = get_char32_from_string(src2, &src_char32);
while (other_char32) {
if (ignoreCase) {
src_char32 = asciiToLower(src_char32);
other_char32 = asciiToLower(other_char32);
}
if (src_char32 != other_char32)
return false;
src2 = get_char32_from_string(src2, &src_char32);
other2 = get_char32_from_string(other2, &other_char32);
// nb += 1;
};
return true;
}
/*
* Returns 1 if src > other
*/
template <typename S, typename O>
int XStringAbstract__compare(const S *src, const O *other, bool ignoreCase) {
if (src == NULL || *src == 0)
return other == NULL || *other == 0 ? 0 : -1;
if (other == NULL || *other == 0)
return 1;
// size_t len_s = length_of_utf_string(src);
// size_t len_other = length_of_utf_string(other);
// size_t nb = 0;
const S *src2 = src;
const O *other2 = other;
char32_t src_char32;
char32_t other_char32;
src2 = get_char32_from_string(src2, &src_char32);
other2 = get_char32_from_string(other2, &other_char32);
while (src_char32) {
if (ignoreCase) {
src_char32 = asciiToLower(src_char32);
other_char32 = asciiToLower(other_char32);
}
if (src_char32 != other_char32)
break;
src2 = get_char32_from_string(src2, &src_char32);
other2 = get_char32_from_string(other2, &other_char32);
// nb += 1;
};
if (src_char32 == other_char32)
return 0;
return src_char32 > other_char32 ? 1 : -1;
}
template <typename S, typename O>
int XStringAbstract__ncompare(const S *src, const O *other, size_t n,
bool ignoreCase) {
if (n == 0)
return 0; // string of 0 length are equal.
const S *src2 = src;
const O *other2 = other;
char32_t src_char32;
char32_t other_char32;
src2 = get_char32_from_string(src2, &src_char32);
other2 = get_char32_from_string(other2, &other_char32);
size_t nb = 1;
while (src_char32 && nb < n) {
if (ignoreCase) {
src_char32 = asciiToLower(src_char32);
other_char32 = asciiToLower(other_char32);
}
if (src_char32 != other_char32)
break;
src2 = get_char32_from_string(src2, &src_char32);
other2 = get_char32_from_string(other2, &other_char32);
nb += 1;
};
if (src_char32 == other_char32)
return 0;
return src_char32 > other_char32 ? 1 : -1;
}
template <typename O, typename P>
size_t XStringAbstract__indexOf(const O **s, const P *other, size_t offsetRet,
bool toLower) {
size_t Idx = 0;
char32_t s_char32;
char32_t other_char32;
do {
const O *s2 = *s;
const P *other2 = other;
do {
s2 = get_char32_from_string(s2, &s_char32);
other2 = get_char32_from_string(other2, &other_char32);
if (toLower) {
s_char32 = asciiToLower(s_char32);
other_char32 = asciiToLower(other_char32);
}
} while (s_char32 && other_char32 && s_char32 == other_char32);
if (other_char32 == 0)
return Idx + offsetRet;
*s = get_char32_from_string(*s, &s_char32);
Idx++;
} while (s_char32);
return MAX_XSIZE;
}
template <typename O, typename P>
size_t XStringAbstract__indexOf(const O *s, size_t Pos, const P *other,
bool toLower) {
if (*other == 0)
return Pos;
char32_t char32 = 1;
for (size_t Idx = 0; Idx < Pos; Idx += 1) {
s = get_char32_from_string(s, &char32);
}
if (!char32)
return MAX_XSIZE;
return XStringAbstract__indexOf(&s, other, Pos, toLower);
}
/*
* Find the last occurence of other, until pos
* NOTE : do not pass SIZE_T_MAX as pos. maximum value is SIZE_T_MAX-1
*/
template <typename O, typename P>
size_t XStringAbstract__rindexOf(const O *s, size_t Pos, const P *other,
bool toLower) {
if (*other == 0)
return Pos > length_of_utf_string(s) ? length_of_utf_string(s) : Pos;
size_t index = XStringAbstract__indexOf(&s, other, 0, toLower);
size_t prev_index =
index; // initialize to index in case of index is already == Pos
char32_t char32;
s = get_char32_from_string(s, &char32);
while (char32 && index < Pos) {
prev_index = index;
index = XStringAbstract__indexOf(&s, other, index + 1, toLower);
s = get_char32_from_string(s, &char32);
};
if (index == Pos)
return index;
if (prev_index <= Pos)
return prev_index;
return MAX_XSIZE;
}
template <class T, class ThisXStringClass, class ThisLStringClass>
class __String {
public:
typedef T char_t;
typedef ThisXStringClass xs_t;
typedef ThisLStringClass ls_t;
protected:
T *__m_data = nullptr;
protected:
#ifdef XSTRING_CACHING_OF_SIZE
size_t __m_size;
#endif
// convenience method. Did it this way to avoid #define in header. They can
// have an impact on other headers
size_t Xmin(size_t x1, size_t x2) const {
if (x1 < x2)
return x1;
return x2;
}
size_t Xmax(size_t x1, size_t x2) const {
if (x1 > x2)
return x1;
return x2;
}
#ifdef XSTRING_CACHING_OF_SIZE
#if 1 // if 1, the size will be checked. For debug purpose.
void checkSizeCached() const { // Debug method
if (size_of_utf_string(__m_data) != __m_size) {
panic("bug XString");
}
}
#define XSTRING_CHECK_SIZE __String<T, ThisXStringClass>::checkSizeCached()
#else
#define XSTRING_CHECK_SIZE
#endif
#else
#define XSTRING_CHECK_SIZE
#endif
// Method _data is protected intentionally. It's a const method returning
// non-const pointer. That's intentional, but dangerous. Do not expose to
// public. If you need a non-const pointer for low-level access, use dataSized
// and specify the size pos is counted in logical char (UTF32 char)
template <typename IntegralType, enable_if(is_integral(IntegralType))>
T *_data(IntegralType pos) const {
XSTRING_CHECK_SIZE;
if (pos < 0) {
log_technical_bug("T* data(int i) -> i < 0");
return __m_data;
}
size_t offset = size_of_utf_string_len(
__m_data, (unsigned_type(IntegralType))
pos); // If pos is too big, size_of_utf_string_len returns
// the end of the string
return __m_data + offset;
}
public:
#ifdef XSTRING_CACHING_OF_SIZE
constexpr __String(const T *s, size_t size)
: __m_data((T *)s), __m_size(size) {
} // Do NOT call with size != strlen(s). This is for litteral operator, not
// for public usage.
#else
constexpr __String(const T *s) : __m_data((T *)s) {}
#endif
// constexpr __String() : m_data(&nullChar) { }
constexpr __String(const __String &) = delete;
constexpr __String() = delete;
// no assignement, no destructor
constexpr const T *s() const {
XSTRING_CHECK_SIZE;
return __m_data;
}
constexpr const T *data() const {
XSTRING_CHECK_SIZE;
return __m_data;
}
template <typename IntegralType, enable_if(is_integral(IntegralType))>
constexpr const T *data(IntegralType pos) const {
return _data(pos);
}
size_t length() const {
return length_of_utf_string(data());
} // TODO: caching length
#ifdef XSTRING_CACHING_OF_SIZE
size_t size() const {
XSTRING_CHECK_SIZE;
return __m_size;
}
size_t sizeInNativeChars() const { return size(); }
size_t sizeInBytes() const { return size() * sizeof(T); }
size_t sizeInBytesIncludingTerminator() const {
return (size() + 1) * sizeof(T);
} // usefull for unit tests
#else
size_t size() const {
XSTRING_CHECK_SIZE;
return size_of_utf_string(data());
}
size_t sizeInNativeChars() const { return size_of_utf_string(data()); }
size_t sizeInBytes() const { return size_of_utf_string(data()) * sizeof(T); }
size_t sizeInBytesIncludingTerminator() const {
return (size_of_utf_string(data()) + 1) * sizeof(T);
} // usefull for unit tests
#endif
/* Empty ? */
bool isEmpty() const { return data() == nullptr || *data() == 0; }
bool notEmpty() const { return !isEmpty(); }
//--------------------------------------------------------------------- cast
// int ToInt() const;
// size_t ToUInt() const;
//---------------------------------------------------------------------
//charAt, []
template <typename IntegralType, enable_if(is_integral(IntegralType))>
char32_t char32At(IntegralType i) const {
if (i < 0) {
#ifdef JIEF_DEBUG
panic("__String<T>::char32At(size_t i) : i < 0. System halted\n");
#else
return 0;
#endif
}
size_t nb = 0;
const T *p = data();
char32_t char32 = 0;
do {
p = get_char32_from_string(p, &char32);
if (!char32) {
#ifdef JIEF_DEBUG
if ((unsigned_type(IntegralType))i == nb)
return 0; // no panic if we want to access the null terminator
panic("__String::char32At(size_t i) : i >= length(). System halted\n");
#else
return 0;
#endif
}
nb += 1;
} while (nb <= (unsigned_type(IntegralType))i);
return char32;
}
template <typename IntegralType, enable_if(is_integral(IntegralType))>
char16_t char16At(IntegralType i) const {
char32_t char32 = char32At(i);
if (char32 >= 0x10000)
return 0xFFFD; // � REPLACEMENT CHARACTER used to replace an unknown,
// unrecognized or unrepresentable character
return (char16_t)char32;
}
/* [] */
template <typename IntegralType, enable_if(is_integral(IntegralType))>
char32_t operator[](IntegralType i) const {
return char32At(i);
}
char32_t lastChar() const {
if (length() > 0)
return char32At(length() - 1);
else
return 0;
}
// /* copy ctor */
// __String<T, ThisXStringClass>(const __String<T, ThisXStringClass> &S)
//{ Init(0); takeValueFrom(S); }
// /* ctor */
// template<typename O, class OtherXStringClass>
// explicit __String<T, ThisXStringClass>(const __String<O,
//OtherXStringClass>& S) { Init(0); takeValueFrom(S); }
//// template<typename O>
//// explicit __String<T, ThisXStringClass>(const O* S) { Init(0);
///takeValueFrom(S); }
/* Copy Assign */ // Only other XString, no litteral at the moment.
// __String<T, ThisXStringClass>& operator =(const __String<T,
//ThisXStringClass>& S) { strcpy(S.s()); return *this; }
// /* Assign */
// template<typename O, class OtherXStringClass>
// ThisXStringClass& operator =(const __String<O, OtherXStringClass>& S)
//{ strcpy(S.s()); return *((ThisXStringClass*)this); }
//// template<class O>
//// ThisXStringClass& operator =(const O* S) { strcpy(S); return
///*this; }
//---------------------------------------------------------------------
//indexOf, rindexOf, contains, subString, startWith
/* indexOf */
size_t indexOf(char32_t char32Searched, size_t Pos = 0) const {
char32_t buf[2] = {char32Searched, 0};
return XStringAbstract__indexOf(data(), Pos, buf, false);
}
template <typename O> size_t indexOf(const O *S, size_t Pos = 0) const {
return XStringAbstract__indexOf(data(), Pos, S, false);
}
template <typename O, class OtherXStringClass>
size_t indexOf(
const __String<O, OtherXStringClass, typename OtherXStringClass::ls_t> &S,
size_t Pos = 0) const {
return indexOf(S.s(), Pos);
}
/* IC */
size_t indexOfIC(char32_t char32Searched, size_t Pos = 0) const {
char32_t buf[2] = {char32Searched, 0};
return XStringAbstract__indexOf(data(), Pos, buf, true);
}
template <typename O> size_t indexOfIC(const O *S, size_t Pos = 0) const {
return XStringAbstract__indexOf(data(), Pos, S, true);
}
template <typename O, class OtherXStringClass>
size_t indexOfIC(
const __String<O, OtherXStringClass, typename OtherXStringClass::ls_t> &S,
size_t Pos = 0) const {
return indexOfIC(S.s(), Pos);
}
/* rindexOf */
size_t rindexOf(const char32_t char32Searched,
size_t Pos = MAX_XSIZE - 1) const {
char32_t buf[2] = {char32Searched, 0};
return XStringAbstract__rindexOf(data(), Pos, buf, false);
}
template <typename O>
size_t rindexOf(const O *S, size_t Pos = MAX_XSIZE - 1) const {
return XStringAbstract__rindexOf(data(), Pos, S, false);
}
template <typename O, class OtherXStringClass>
size_t rindexOf(
const __String<O, OtherXStringClass, typename OtherXStringClass::ls_t> &S,
size_t Pos = MAX_XSIZE - 1) const {
return rindexOf(S.s(), Pos);
}
/* IC */
size_t rindexOfIC(const char32_t char32Searched,
size_t Pos = MAX_XSIZE - 1) const {
char32_t buf[2] = {char32Searched, 0};
return XStringAbstract__rindexOf(data(), Pos, buf, true);
}
template <typename O>
size_t rindexOfIC(const O *S, size_t Pos = MAX_XSIZE - 1) const {
return XStringAbstract__rindexOf(data(), Pos, S, true);
}
template <typename O, class OtherXStringClass>
size_t rindexOfIC(
const __String<O, OtherXStringClass, typename OtherXStringClass::ls_t> &S,
size_t Pos = MAX_XSIZE - 1) const {
return rindexOf(S.s(), Pos);
}
template <typename O, class OtherXStringClass>
bool contains(const __String<O, OtherXStringClass,
typename OtherXStringClass::ls_t> &S) const {
return indexOf(S) != MAX_XSIZE;
}
template <typename O> bool contains(const O *S) const {
return indexOf(S) != MAX_XSIZE;
}
template <typename O, class OtherXStringClass>
size_t containsIC(const __String<O, OtherXStringClass,
typename OtherXStringClass::ls_t> &S) const {
return indexOfIC(S) != MAX_XSIZE;
}
template <typename O> size_t containsIC(const O *S) const {
return indexOfIC(S) != MAX_XSIZE;
}
ThisXStringClass subString(size_t pos, size_t count) const {
// if ( pos > length() ) return ThisXStringClass();
// if ( count > length()-pos ) count = length()-pos;
ThisXStringClass ret;
const T *src = data();
char32_t char32 = 1;
while (char32 && pos > 0) {
src = get_char32_from_string(src, &char32);
pos -= 1;
};
ret.strncat(src, count);
return ret;
}
template <typename O, enable_if(is_char(O))>
bool startWith(O otherChar) const {
O other[2] = {otherChar, 0};
return XStringAbstract__startWith(data(), other, false);
}
template <typename O, class OtherXStringClass>
bool
startWith(const __String<O, OtherXStringClass,
typename OtherXStringClass::ls_t> &otherS) const {
return XStringAbstract__startWith(data(), otherS.data(), false);
}
template <typename O> bool startWith(const O *other) const {
return XStringAbstract__startWith(data(), other, false);
}
template <typename O, class OtherXStringClass>
bool
startWithIC(const __String<O, OtherXStringClass,
typename OtherXStringClass::ls_t> &otherS) const {
return XStringAbstract__startWith(data(), otherS.data(), true);
}
template <typename O> bool startWithIC(const O *other) const {
return XStringAbstract__startWith(data(), other, true);
}
template <typename O, enable_if(is_char(O))>
bool startWithOrEqualTo(O otherChar) const {
O other[2] = {otherChar, 0};
return XStringAbstract__startWithOrEqualTo(data(), other, false);
}
template <typename O, class OtherXStringClass>
bool startWithOrEqualTo(
const __String<O, OtherXStringClass, typename OtherXStringClass::ls_t>
&otherS) const {
return XStringAbstract__startWithOrEqualTo(data(), otherS.data(), false);
}
template <typename O> bool startWithOrEqualTo(const O *other) const {
return XStringAbstract__startWithOrEqualTo(data(), other, false);
}
template <typename O, class OtherXStringClass>
bool startWithOrEqualToIC(
const __String<O, OtherXStringClass, typename OtherXStringClass::ls_t>
&otherS) const {
return XStringAbstract__startWithOrEqualTo(data(), otherS.data(), true);
}
template <typename O> bool startWithOrEqualToIC(const O *other) const {
return XStringAbstract__startWithOrEqualTo(data(), other, true);
}
template <typename O, class OtherXStringClass>
bool endWithOrEqualToIC(
const __String<O, OtherXStringClass, typename OtherXStringClass::ls_t>
&otherS) const {
if (length() < otherS.length())
return false;
return XStringAbstract__rindexOf(data(), SIZE_T_MAX - 1, otherS.data(),
true) == length() - otherS.length();
}
//---------------------------------------------------------------------
ThisXStringClass basename() const {
size_t lastSepPos = MAX_XSIZE;
size_t pos = 0;
const T *p = data();
char32_t char32 = 0;
p = get_char32_from_string(p, &char32);
while (char32) {
if (char32 == U'/' || char32 == U'\\')
lastSepPos = pos;
pos += 1;
p = get_char32_from_string(p, &char32);
};
if (lastSepPos == MAX_XSIZE) {
if (p == data())
return ThisXStringClass().takeValueFrom(".");
}
return subString(lastSepPos + 1, MAX_XSIZE);
}
ThisXStringClass dirname() const {
size_t idx;
idx = rindexOf('\\');
if (idx != MAX_XSIZE)
return subString(0, idx);
idx = rindexOf('/');
if (idx != MAX_XSIZE)
return subString(0, idx);
return ThisXStringClass();
}
// bool IsDigits(size_t pos, size_t count) const;
//{
// const T *p;
// const T *q;
//
// if ( pos >= size() ) {
// return false;
// }
// if ( pos+count > size() ) {
// return false;
// }
// p = data() + pos;
// q = p + count;
// for ( ; p < q ; p+=1 ) {
// if ( *p < '0' ) return false;
// if ( *p > '9' ) return false;
// }
// return true;
//}
//---------------------------------------------------------------------
//strcmp, equal, comparison operator
template <typename O> int strcmp(const O *S) const {
return XStringAbstract__compare(data(), S, false);
}
// int Compare(const char* S) const { return ::Compare<T, char>(data(), S);
//} int Compare(const char16_t* S) const { return ::Compare<T,
//char16_t>(data(), S); }; int Compare(const char32_t* S) const { return
//::Compare<T, char32_t>(data(), S); }; int Compare(const wchar_t* S) const {
//return ::Compare<T, wchar_t>(data(), S); };
//
template <typename O> int strncmp(const O *S, size_t n) const {
return XStringAbstract__ncompare(data(), S, n, false);
}
template <typename O, class OtherXStringClass>
bool isEqual(const __String<O, OtherXStringClass,
typename OtherXStringClass::ls_t> &S) const {
return XStringAbstract__compare(data(), S.s(), false) == 0;
}
template <typename O> bool isEqual(const O *S) const {
return XStringAbstract__compare(data(), S, false) == 0;
}
template <typename O, class OtherXStringClass>
bool isEqualIC(const __String<O, OtherXStringClass,
typename OtherXStringClass::ls_t> &S) const {
return XStringAbstract__compare(data(), S.s(), true) == 0;
}
template <typename O> bool isEqualIC(const O *S) const {
return XStringAbstract__compare(data(), S, true) == 0;
}
// bool SubStringEqual(size_t Pos, const T* S) const { return
//(memcmp(data(Pos), S, wcslen(S)) == 0); }
template <typename IntegralType, typename O, class OtherXStringClass>
bool isEqualAtIC(IntegralType pos,
const __String<O, OtherXStringClass,
typename OtherXStringClass::ls_t> &S) const {
#ifdef JIEF_DEBUG
if (pos < 0)
panic("XString::equalAtIC -> i < 0");
#else
if (pos < 0)
return false;
#endif
if ((unsigned_type(IntegralType))pos > length() - S.length())
return false;
return XStringAbstract__ncompare(data() + (unsigned_type(IntegralType))pos,
S.s(), S.length(), true) == 0;
}
public:
// == operator
template <typename O, class OtherXStringClass>
bool operator==(const __String<O, OtherXStringClass,
typename OtherXStringClass::ls_t> &s2) const {
return (*this).strcmp(s2.s()) == 0;
}
// template<typename O>
// bool operator == (const O* s2) const { return (*this).strcmp(s2) == 0; }
// template<typename O>
// friend bool operator == (const O* s1, ThisXStringClass& s2) { return
//s2.strcmp(s1) == 0; }
template <typename O, class OtherXStringClass>
bool operator!=(const __String<O, OtherXStringClass,
typename OtherXStringClass::ls_t> &s2) const {
return !(*this == s2);
}
// template<typename O>
// bool operator != (const O* s2) const { return !(*this == s2); }
// template<typename O>
// friend bool operator != (const O* s1, const ThisXStringClass& s2) {
//return s2.strcmp(s1) != 0; }
template <typename O, class OtherXStringClass>
bool operator<(const __String<O, OtherXStringClass,
typename OtherXStringClass::ls_t> &s2) const {
return (*this).strcmp(s2.s()) < 0;
}
// template<typename O>
// bool operator < (const O* s2) const { return (*this).strcmp(s2) < 0; }
// template<typename O>
// friend bool operator < (const O* s1, const ThisXStringClass& s2) {
//return s2.strcmp(s1) > 0; }
template <typename O, class OtherXStringClass>
bool operator>(const __String<O, OtherXStringClass,
typename OtherXStringClass::ls_t> &s2) const {
return (*this).strcmp(s2.s()) > 0;
}
// template<typename O>
// bool operator > (const O* s2) const { return (*this).strcmp(s2) > 0; }
// template<typename O>
// friend bool operator > (const O* s1, const ThisXStringClass& s2) {
//return s2.strcmp(s1) < 0; }
template <typename O, class OtherXStringClass>
bool operator<=(const __String<O, OtherXStringClass,
typename OtherXStringClass::ls_t> &s2) const {
return (*this).strcmp(s2.s()) <= 0;
}
// template<typename O>
// bool operator <= (const O* s2) const { return (*this).strcmp(s2) <= 0;
//} template<typename O> friend bool operator <= (const O* s1, const
//ThisXStringClass& s2) { return s2.strcmp(s1) >= 0; }
template <typename O, class OtherXStringClass>
bool operator>=(const __String<O, OtherXStringClass,
typename OtherXStringClass::ls_t> &s2) const {
return (*this).strcmp(s2.s()) >= 0;
}
// template<typename O>
// bool operator >= (const O* s2) const { return (*this).strcmp(s2) >= 0;
//} template<typename O> friend bool operator >= (const O* s1, const
//ThisXStringClass& s2) { return s2.strcmp(s1) <= 0; }
};
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx LString
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
template <class T, class ThisXStringClass, class ThisLStringClass>
class LString : public __String<T, ThisXStringClass, ThisLStringClass> {
public:
protected:
#ifdef XSTRING_CACHING_OF_SIZE
constexpr LString(const T *s) : __String<T, ThisXStringClass>(s, 0) {};
constexpr LString(const T *s, size_t size)
: __String<T, ThisXStringClass>(s, size) {};
constexpr LString(const LString &L)
: __String<T, ThisXStringClass>(L.data(), L.size()) {};
#else
constexpr LString(const T *s)
: __String<T, ThisXStringClass, ThisLStringClass>(s) {};
constexpr LString(const T *s, size_t size)
: __String<T, ThisXStringClass, ThisLStringClass>(s, size) {};
constexpr LString(const LString &L)
: __String<T, ThisXStringClass, ThisLStringClass>(L.data()) {};
#endif
constexpr LString() = delete;
// no assignement, no destructor
};
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
///* __String + char32_t */
// template<typename CharType1, class XStringClass1>
// XStringClass1 operator + (const __String<CharType1, XStringClass1>& p1,
// char32_t p2) { XStringClass1 s; s.takeValueFrom(p1); s.strcat(p2); return s;
// }
//
///* __String + __String */
// template<typename CharType1, class XStringClass1, typename CharType2, class
// XStringClass2> XStringClass1 operator + (const __String<CharType1,
// XStringClass1>& p1, const __String<CharType2, XStringClass2>& p2) {
// XStringClass1 s; s.takeValueFrom(p1); s.strcat(p2); return s; }
//
///* char* + __String */
// template<typename CharType1, typename CharType2, class XStringClass2>
// XStringClass2 operator + (const CharType1* p1, const __String<CharType2,
// XStringClass2>& p2) { XStringClass2 s; s.takeValueFrom(p1); s.strcat(p2);
// return s; }
//
///* __String + char* */
// template<typename T1, class XStringClass1, typename CharType2>
// XStringClass1 operator + (const __String<T1, XStringClass1>& p1, const
// CharType2* p2) { XStringClass1 s; s.takeValueFrom(p1); s.strcat(p2); return
// s; }
template <typename Base> _xtools__true_type is_base_of_test_func(Base *);
template <typename Base> _xtools__false_type is_base_of_test_func(void *);
template <typename B, typename D>
auto test_pre_is_base_of(int)
-> decltype(is_base_of_test_func<B>(static_cast<D *>(nullptr)));
template <class, class = _xtools__void_t<>, class = _xtools__void_t<>,
class = _xtools__void_t<>>
struct __string_type {
typedef void type;
};
template <typename T>
// struct __string_type<T, _xtools__void_t<typename T::xs_t>,
// _xtools__void_t<typename T::char_t>> { typedef __String<typename T::char_t,
// typename T::xs_t> type; }; struct __string_type<T, _xtools__void_t<typename
// T::char_t>, _xtools__void_t<typename T::xs_t>, _xtools__void_t<typename
// T::ls_t>> { typedef __String<typename T::char_t, typename T::xs_t, typename
// T::ls_t> type; };
struct __string_type<T, _xtools__void_t<typename T::char_t>> {
typedef __String<typename T::char_t, typename T::xs_t, typename T::ls_t> type;
};
#define is___String_t(x) \
decltype(test_pre_is_base_of<typename __string_type<x>::type, x>(0))
#define is___String(x) is___String_t(x)::value
// template< class, class = _xtools__void_t<>, class = _xtools__void_t<> >
// struct __lstring_type { typedef void type; };
// template< typename T >
// struct __lstring_type<T, _xtools__void_t<typename T::xs_t>,
// _xtools__void_t<typename T::char_t>> { typedef LString<typename T::char_t,
// typename T::xs_t> type; };
template <class, class = _xtools__void_t<>, class = _xtools__void_t<>,
class = _xtools__void_t<>>
struct __lstring_type {
typedef void type;
};
template <typename T>
struct __lstring_type<T, _xtools__void_t<typename T::xs_t>,
_xtools__void_t<typename T::char_t>,
_xtools__void_t<typename T::ls_t>> {
typedef LString<typename T::char_t, typename T::xs_t, typename T::ls_t> type;
};
// struct __lstring_type<T, _xtools__void_t<typename T::char_t>,
// _xtools__void_t<typename T::xs_t>, xtools__void_t<typename T::ls_t>> {
// typedef LString<typename T::char_t, typename T::xs_t, typename T::ls_t> type;
// };
#define is___LString_t(x) \
decltype(test_pre_is_base_of<typename __lstring_type<x>::type, x>(0))
#define is___LString(x) is___LString_t(x)::value
/* __string_class_or<T1, T2>::type is T1 is T1 is a subclass of __String. If T1
* is not a subclass of __String, returns T2 if it's a subclass of __String */
template <typename T1, typename T2, typename Tdummy = void>
struct __string_class_or;
template <typename T1, typename T2>
struct __string_class_or<T1, T2,
enable_if_t(!is___String(T1) &&
!is___String(T2))> { /*typedef double
type;*/
};
template <typename T1, typename T2>
struct __string_class_or<T1, T2, enable_if_t(is___String(T1))> {
typedef typename T1::xs_t type;
};
template <typename T1, typename T2>
struct __string_class_or<T1, T2,
enable_if_t(!is___String(T1) && is___String(T2))> {
typedef typename T2::xs_t type;
};
/* ------------ get_char_ptr(x) --------------*/
template <typename T, typename Tdummy = void> struct _xstringarray__char_type;
template <typename T>
struct _xstringarray__char_type<T, enable_if_t(is___String(T))> {
static const typename T::char_t *getCharPtr(const T &t) { return t.s(); }
};
template <typename T>
struct _xstringarray__char_type<T *, enable_if_t(is_char(T))> {
static const T *getCharPtr(T *t) { return t; }
};
// template<typename T>
// struct _xstringarray__char_type<const T*, enable_if_t(is_char(T))>
//{
// static const T* getCharPtr(const T* t) { return t; }
// };
template <typename T> struct _xstringarray__char_type<const T[]> {
static const T *getCharPtr(T *t) { return t; }
};
template <typename T, size_t _Np> struct _xstringarray__char_type<T[_Np]> {
static const T *getCharPtr(const T *t) { return t; }
};
#ifdef _MSC_VER
// I don't know why it's needed with VS.
template <typename T>
struct _xstringarray__char_type<T, enable_if_t(is___LString(T))> {
static const typename T::char_t *getCharPtr(const T &t) { return t.s(); }
};
#endif
#define get_char_ptr(x) _xstringarray__char_type<typeof(x)>::getCharPtr(x)
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// #define data() super::data()
template <class T, class ThisXStringClass, class ThisLStringClass>
class XStringAbstract : public __String<T, ThisXStringClass, ThisLStringClass> {
public:
using super = __String<T, ThisXStringClass, ThisLStringClass>;
using ls_t = ThisLStringClass;
protected:
static T nullChar;
size_t m_allocatedSize; // Must include null terminator. Real memory allocated
// is only m_allocatedSize (not m_allocatedSize+1)
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Init , Alloc
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
/*
* nNewSize must include null terminator.
*/
void Alloc(size_t nNewAllocatedSize) {
if (m_allocatedSize == 0) {
super::__m_data = (T *)AllocatePool(nNewAllocatedSize * sizeof(T)); //Slice: was malloc
} else {
// super::__m_data =
// (T *)Xrealloc(super::__m_data, nNewAllocatedSize * sizeof(T),
// m_allocatedSize * sizeof(T));
super::__m_data = (T*)ReallocatePool( m_allocatedSize * sizeof(T),
nNewAllocatedSize * sizeof(T), super::__m_data);
}
if (!super::__m_data) {
log_technical_bug("XStringAbstract::Alloc(%zu) : Xrealloc(%" PRIuPTR
", %lu, %zd) returned NULL. System halted\n",
nNewAllocatedSize, uintptr_t(super::__m_data),
nNewAllocatedSize * sizeof(T),
m_allocatedSize * sizeof(T));
m_allocatedSize = 0;
return;
}
m_allocatedSize = nNewAllocatedSize;
}
// public:
/*
* Make sure this string has allocated size of at least nNewSize+1.
*/
bool CheckSize(size_t nNewAllocatedSize,
size_t nGrowBy = XStringGrowByDefault) // nNewSize is in number
// of chars, NOT bytes
{
// DBG_XSTRING("CheckSize: m_size=%d, nNewSize=%d\n", m_size, nNewSize);
if (m_allocatedSize < nNewAllocatedSize + 1) {
nNewAllocatedSize += nGrowBy;
if (m_allocatedSize == 0) { // if ( *data() ) {
// Even if m_allocatedSize == 0, data() might not be NULL because it can
// points to a litteral. So we need to alloc and cpy the litteral in the
// newly allocated buffer.
size_t size = super::size();
if (nNewAllocatedSize < size)
nNewAllocatedSize = size;
const T *m_dataSav = super::data();
// super::__m_data = NULL; // No need, Alloc will reassign it.
Alloc(nNewAllocatedSize + 1);
utf_string_from_utf_string(super::__m_data, m_allocatedSize, m_dataSav);
return true;
} else {
Alloc(nNewAllocatedSize + 1);
return true;
}
}
return false;
}
public:
/* default ctor */
#ifdef XSTRING_CACHING_OF_SIZE
XStringAbstract()
: __String<T, ThisXStringClass, ThisLStringClass>(&nullChar, 0),
m_allocatedSize(0) {}
#else
XStringAbstract()
: __String<T, ThisXStringClass, ThisLStringClass>(&nullChar),
m_allocatedSize(0) {}