-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
ustring.cpp
5432 lines (4604 loc) · 121 KB
/
ustring.cpp
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
/**************************************************************************/
/* ustring.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/
#include "ustring.h"
#include "core/crypto/crypto_core.h"
#include "core/math/color.h"
#include "core/math/math_funcs.h"
#include "core/os/memory.h"
#include "core/string/print_string.h"
#include "core/string/string_name.h"
#include "core/string/translation.h"
#include "core/string/ucaps.h"
#include "core/variant/variant.h"
#include "core/version_generated.gen.h"
#include <stdio.h>
#include <stdlib.h>
#include <cstdint>
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS // to disable build-time warning which suggested to use strcpy_s instead strcpy
#endif
#if defined(MINGW_ENABLED) || defined(_MSC_VER)
#define snprintf _snprintf_s
#endif
static const int MAX_DECIMALS = 32;
static _FORCE_INLINE_ char32_t lower_case(char32_t c) {
return (is_ascii_upper_case(c) ? (c + ('a' - 'A')) : c);
}
const char CharString::_null = 0;
const char16_t Char16String::_null = 0;
const char32_t String::_null = 0;
const char32_t String::_replacement_char = 0xfffd;
bool select_word(const String &p_s, int p_col, int &r_beg, int &r_end) {
const String &s = p_s;
int beg = CLAMP(p_col, 0, s.length());
int end = beg;
if (s[beg] > 32 || beg == s.length()) {
bool symbol = beg < s.length() && is_symbol(s[beg]);
while (beg > 0 && s[beg - 1] > 32 && (symbol == is_symbol(s[beg - 1]))) {
beg--;
}
while (end < s.length() && s[end + 1] > 32 && (symbol == is_symbol(s[end + 1]))) {
end++;
}
if (end < s.length()) {
end += 1;
}
r_beg = beg;
r_end = end;
return true;
} else {
return false;
}
}
/*************************************************************************/
/* Char16String */
/*************************************************************************/
bool Char16String::operator<(const Char16String &p_right) const {
if (length() == 0) {
return p_right.length() != 0;
}
return is_str_less(get_data(), p_right.get_data());
}
Char16String &Char16String::operator+=(char16_t p_char) {
const int lhs_len = length();
resize(lhs_len + 2);
char16_t *dst = ptrw();
dst[lhs_len] = p_char;
dst[lhs_len + 1] = 0;
return *this;
}
void Char16String::operator=(const char16_t *p_cstr) {
copy_from(p_cstr);
}
const char16_t *Char16String::get_data() const {
if (size()) {
return &operator[](0);
} else {
return u"";
}
}
void Char16String::copy_from(const char16_t *p_cstr) {
if (!p_cstr) {
resize(0);
return;
}
const char16_t *s = p_cstr;
for (; *s; s++) {
}
size_t len = s - p_cstr;
if (len == 0) {
resize(0);
return;
}
Error err = resize(++len); // include terminating null char
ERR_FAIL_COND_MSG(err != OK, "Failed to copy char16_t string.");
memcpy(ptrw(), p_cstr, len * sizeof(char16_t));
}
/*************************************************************************/
/* CharString */
/*************************************************************************/
bool CharString::operator<(const CharString &p_right) const {
if (length() == 0) {
return p_right.length() != 0;
}
return is_str_less(get_data(), p_right.get_data());
}
bool CharString::operator==(const CharString &p_right) const {
if (length() == 0) {
// True if both have length 0, false if only p_right has a length
return p_right.length() == 0;
} else if (p_right.length() == 0) {
// False due to unequal length
return false;
}
return strcmp(ptr(), p_right.ptr()) == 0;
}
CharString &CharString::operator+=(char p_char) {
const int lhs_len = length();
resize(lhs_len + 2);
char *dst = ptrw();
dst[lhs_len] = p_char;
dst[lhs_len + 1] = 0;
return *this;
}
void CharString::operator=(const char *p_cstr) {
copy_from(p_cstr);
}
const char *CharString::get_data() const {
if (size()) {
return &operator[](0);
} else {
return "";
}
}
void CharString::copy_from(const char *p_cstr) {
if (!p_cstr) {
resize(0);
return;
}
size_t len = strlen(p_cstr);
if (len == 0) {
resize(0);
return;
}
Error err = resize(++len); // include terminating null char
ERR_FAIL_COND_MSG(err != OK, "Failed to copy C-string.");
memcpy(ptrw(), p_cstr, len);
}
/*************************************************************************/
/* String */
/*************************************************************************/
Error String::parse_url(String &r_scheme, String &r_host, int &r_port, String &r_path) const {
// Splits the URL into scheme, host, port, path. Strip credentials when present.
String base = *this;
r_scheme = "";
r_host = "";
r_port = 0;
r_path = "";
int pos = base.find("://");
// Scheme
if (pos != -1) {
r_scheme = base.substr(0, pos + 3).to_lower();
base = base.substr(pos + 3, base.length() - pos - 3);
}
pos = base.find("/");
// Path
if (pos != -1) {
r_path = base.substr(pos, base.length() - pos);
base = base.substr(0, pos);
}
// Host
pos = base.find("@");
if (pos != -1) {
// Strip credentials
base = base.substr(pos + 1, base.length() - pos - 1);
}
if (base.begins_with("[")) {
// Literal IPv6
pos = base.rfind("]");
if (pos == -1) {
return ERR_INVALID_PARAMETER;
}
r_host = base.substr(1, pos - 1);
base = base.substr(pos + 1, base.length() - pos - 1);
} else {
// Anything else
if (base.get_slice_count(":") > 2) {
return ERR_INVALID_PARAMETER;
}
pos = base.rfind(":");
if (pos == -1) {
r_host = base;
base = "";
} else {
r_host = base.substr(0, pos);
base = base.substr(pos, base.length() - pos);
}
}
if (r_host.is_empty()) {
return ERR_INVALID_PARAMETER;
}
r_host = r_host.to_lower();
// Port
if (base.begins_with(":")) {
base = base.substr(1, base.length() - 1);
if (!base.is_valid_int()) {
return ERR_INVALID_PARAMETER;
}
r_port = base.to_int();
if (r_port < 1 || r_port > 65535) {
return ERR_INVALID_PARAMETER;
}
}
return OK;
}
void String::copy_from(const char *p_cstr) {
// copy Latin-1 encoded c-string directly
if (!p_cstr) {
resize(0);
return;
}
const size_t len = strlen(p_cstr);
if (len == 0) {
resize(0);
return;
}
resize(len + 1); // include 0
char32_t *dst = this->ptrw();
for (size_t i = 0; i <= len; i++) {
#if CHAR_MIN == 0
uint8_t c = p_cstr[i];
#else
uint8_t c = p_cstr[i] >= 0 ? p_cstr[i] : uint8_t(256 + p_cstr[i]);
#endif
if (c == 0 && i < len) {
print_unicode_error("NUL character", true);
dst[i] = _replacement_char;
} else {
dst[i] = c;
}
}
}
void String::copy_from(const char *p_cstr, const int p_clip_to) {
// copy Latin-1 encoded c-string directly
if (!p_cstr) {
resize(0);
return;
}
int len = 0;
const char *ptr = p_cstr;
while ((p_clip_to < 0 || len < p_clip_to) && *(ptr++) != 0) {
len++;
}
if (len == 0) {
resize(0);
return;
}
resize(len + 1); // include 0
char32_t *dst = this->ptrw();
for (int i = 0; i < len; i++) {
#if CHAR_MIN == 0
uint8_t c = p_cstr[i];
#else
uint8_t c = p_cstr[i] >= 0 ? p_cstr[i] : uint8_t(256 + p_cstr[i]);
#endif
if (c == 0) {
print_unicode_error("NUL character", true);
dst[i] = _replacement_char;
} else {
dst[i] = c;
}
}
dst[len] = 0;
}
void String::copy_from(const wchar_t *p_cstr) {
#ifdef WINDOWS_ENABLED
// wchar_t is 16-bit, parse as UTF-16
parse_utf16((const char16_t *)p_cstr);
#else
// wchar_t is 32-bit, copy directly
copy_from((const char32_t *)p_cstr);
#endif
}
void String::copy_from(const wchar_t *p_cstr, const int p_clip_to) {
#ifdef WINDOWS_ENABLED
// wchar_t is 16-bit, parse as UTF-16
parse_utf16((const char16_t *)p_cstr, p_clip_to);
#else
// wchar_t is 32-bit, copy directly
copy_from((const char32_t *)p_cstr, p_clip_to);
#endif
}
void String::copy_from(const char32_t &p_char) {
if (p_char == 0) {
print_unicode_error("NUL character", true);
return;
}
resize(2);
char32_t *dst = ptrw();
if ((p_char & 0xfffff800) == 0xd800) {
print_unicode_error(vformat("Unpaired surrogate (%x)", (uint32_t)p_char));
dst[0] = _replacement_char;
} else if (p_char > 0x10ffff) {
print_unicode_error(vformat("Invalid unicode codepoint (%x)", (uint32_t)p_char));
dst[0] = _replacement_char;
} else {
dst[0] = p_char;
}
dst[1] = 0;
}
void String::copy_from(const char32_t *p_cstr) {
if (!p_cstr) {
resize(0);
return;
}
int len = 0;
const char32_t *ptr = p_cstr;
while (*(ptr++) != 0) {
len++;
}
if (len == 0) {
resize(0);
return;
}
copy_from_unchecked(p_cstr, len);
}
void String::copy_from(const char32_t *p_cstr, const int p_clip_to) {
if (!p_cstr) {
resize(0);
return;
}
int len = 0;
const char32_t *ptr = p_cstr;
while ((p_clip_to < 0 || len < p_clip_to) && *(ptr++) != 0) {
len++;
}
if (len == 0) {
resize(0);
return;
}
copy_from_unchecked(p_cstr, len);
}
// assumes the following have already been validated:
// p_char != nullptr
// p_length > 0
// p_length <= p_char strlen
void String::copy_from_unchecked(const char32_t *p_char, const int p_length) {
resize(p_length + 1);
char32_t *dst = ptrw();
dst[p_length] = 0;
for (int i = 0; i < p_length; i++) {
if (p_char[i] == 0) {
print_unicode_error("NUL character", true);
dst[i] = _replacement_char;
continue;
}
if ((p_char[i] & 0xfffff800) == 0xd800) {
print_unicode_error(vformat("Unpaired surrogate (%x)", (uint32_t)p_char[i]));
dst[i] = _replacement_char;
continue;
}
if (p_char[i] > 0x10ffff) {
print_unicode_error(vformat("Invalid unicode codepoint (%x)", (uint32_t)p_char[i]));
dst[i] = _replacement_char;
continue;
}
dst[i] = p_char[i];
}
}
void String::operator=(const char *p_str) {
copy_from(p_str);
}
void String::operator=(const char32_t *p_str) {
copy_from(p_str);
}
void String::operator=(const wchar_t *p_str) {
copy_from(p_str);
}
String String::operator+(const String &p_str) const {
String res = *this;
res += p_str;
return res;
}
String String::operator+(char32_t p_char) const {
String res = *this;
res += p_char;
return res;
}
String operator+(const char *p_chr, const String &p_str) {
String tmp = p_chr;
tmp += p_str;
return tmp;
}
String operator+(const wchar_t *p_chr, const String &p_str) {
#ifdef WINDOWS_ENABLED
// wchar_t is 16-bit
String tmp = String::utf16((const char16_t *)p_chr);
#else
// wchar_t is 32-bit
String tmp = (const char32_t *)p_chr;
#endif
tmp += p_str;
return tmp;
}
String operator+(char32_t p_chr, const String &p_str) {
return (String::chr(p_chr) + p_str);
}
String &String::operator+=(const String &p_str) {
const int lhs_len = length();
if (lhs_len == 0) {
*this = p_str;
return *this;
}
const int rhs_len = p_str.length();
if (rhs_len == 0) {
return *this;
}
resize(lhs_len + rhs_len + 1);
const char32_t *src = p_str.ptr();
char32_t *dst = ptrw() + lhs_len;
// Don't copy the terminating null with `memcpy` to avoid undefined behavior when string is being added to itself (it would overlap the destination).
memcpy(dst, src, rhs_len * sizeof(char32_t));
*(dst + rhs_len) = _null;
return *this;
}
String &String::operator+=(const char *p_str) {
if (!p_str || p_str[0] == 0) {
return *this;
}
const int lhs_len = length();
const size_t rhs_len = strlen(p_str);
resize(lhs_len + rhs_len + 1);
char32_t *dst = ptrw() + lhs_len;
for (size_t i = 0; i <= rhs_len; i++) {
#if CHAR_MIN == 0
uint8_t c = p_str[i];
#else
uint8_t c = p_str[i] >= 0 ? p_str[i] : uint8_t(256 + p_str[i]);
#endif
if (c == 0 && i < rhs_len) {
print_unicode_error("NUL character", true);
dst[i] = _replacement_char;
} else {
dst[i] = c;
}
}
return *this;
}
String &String::operator+=(const wchar_t *p_str) {
#ifdef WINDOWS_ENABLED
// wchar_t is 16-bit
*this += String::utf16((const char16_t *)p_str);
#else
// wchar_t is 32-bit
*this += String((const char32_t *)p_str);
#endif
return *this;
}
String &String::operator+=(const char32_t *p_str) {
*this += String(p_str);
return *this;
}
String &String::operator+=(char32_t p_char) {
if (p_char == 0) {
print_unicode_error("NUL character", true);
return *this;
}
const int lhs_len = length();
resize(lhs_len + 2);
char32_t *dst = ptrw();
if ((p_char & 0xfffff800) == 0xd800) {
print_unicode_error(vformat("Unpaired surrogate (%x)", (uint32_t)p_char));
dst[lhs_len] = _replacement_char;
} else if (p_char > 0x10ffff) {
print_unicode_error(vformat("Invalid unicode codepoint (%x)", (uint32_t)p_char));
dst[lhs_len] = _replacement_char;
} else {
dst[lhs_len] = p_char;
}
dst[lhs_len + 1] = 0;
return *this;
}
bool String::operator==(const char *p_str) const {
// compare Latin-1 encoded c-string
int len = 0;
const char *aux = p_str;
while (*(aux++) != 0) {
len++;
}
if (length() != len) {
return false;
}
if (is_empty()) {
return true;
}
int l = length();
const char32_t *dst = get_data();
// Compare char by char
for (int i = 0; i < l; i++) {
if ((char32_t)p_str[i] != dst[i]) {
return false;
}
}
return true;
}
bool String::operator==(const wchar_t *p_str) const {
#ifdef WINDOWS_ENABLED
// wchar_t is 16-bit, parse as UTF-16
return *this == String::utf16((const char16_t *)p_str);
#else
// wchar_t is 32-bit, compare char by char
return *this == (const char32_t *)p_str;
#endif
}
bool String::operator==(const char32_t *p_str) const {
int len = 0;
const char32_t *aux = p_str;
while (*(aux++) != 0) {
len++;
}
if (length() != len) {
return false;
}
if (is_empty()) {
return true;
}
int l = length();
const char32_t *dst = get_data();
/* Compare char by char */
for (int i = 0; i < l; i++) {
if (p_str[i] != dst[i]) {
return false;
}
}
return true;
}
bool String::operator==(const String &p_str) const {
if (length() != p_str.length()) {
return false;
}
if (is_empty()) {
return true;
}
int l = length();
const char32_t *src = get_data();
const char32_t *dst = p_str.get_data();
/* Compare char by char */
for (int i = 0; i < l; i++) {
if (src[i] != dst[i]) {
return false;
}
}
return true;
}
bool String::operator==(const StrRange &p_str_range) const {
int len = p_str_range.len;
if (length() != len) {
return false;
}
if (is_empty()) {
return true;
}
const char32_t *c_str = p_str_range.c_str;
const char32_t *dst = &operator[](0);
/* Compare char by char */
for (int i = 0; i < len; i++) {
if (c_str[i] != dst[i]) {
return false;
}
}
return true;
}
bool operator==(const char *p_chr, const String &p_str) {
return p_str == p_chr;
}
bool operator==(const wchar_t *p_chr, const String &p_str) {
#ifdef WINDOWS_ENABLED
// wchar_t is 16-bit
return p_str == String::utf16((const char16_t *)p_chr);
#else
// wchar_t is 32-bi
return p_str == String((const char32_t *)p_chr);
#endif
}
bool operator!=(const char *p_chr, const String &p_str) {
return !(p_str == p_chr);
}
bool operator!=(const wchar_t *p_chr, const String &p_str) {
#ifdef WINDOWS_ENABLED
// wchar_t is 16-bit
return !(p_str == String::utf16((const char16_t *)p_chr));
#else
// wchar_t is 32-bi
return !(p_str == String((const char32_t *)p_chr));
#endif
}
bool String::operator!=(const char *p_str) const {
return (!(*this == p_str));
}
bool String::operator!=(const wchar_t *p_str) const {
return (!(*this == p_str));
}
bool String::operator!=(const char32_t *p_str) const {
return (!(*this == p_str));
}
bool String::operator!=(const String &p_str) const {
return !((*this == p_str));
}
bool String::operator<=(const String &p_str) const {
return !(p_str < *this);
}
bool String::operator>(const String &p_str) const {
return p_str < *this;
}
bool String::operator>=(const String &p_str) const {
return !(*this < p_str);
}
bool String::operator<(const char *p_str) const {
if (is_empty() && p_str[0] == 0) {
return false;
}
if (is_empty()) {
return true;
}
return is_str_less(get_data(), p_str);
}
bool String::operator<(const wchar_t *p_str) const {
if (is_empty() && p_str[0] == 0) {
return false;
}
if (is_empty()) {
return true;
}
#ifdef WINDOWS_ENABLED
// wchar_t is 16-bit
return is_str_less(get_data(), String::utf16((const char16_t *)p_str).get_data());
#else
// wchar_t is 32-bit
return is_str_less(get_data(), (const char32_t *)p_str);
#endif
}
bool String::operator<(const char32_t *p_str) const {
if (is_empty() && p_str[0] == 0) {
return false;
}
if (is_empty()) {
return true;
}
return is_str_less(get_data(), p_str);
}
bool String::operator<(const String &p_str) const {
return operator<(p_str.get_data());
}
signed char String::nocasecmp_to(const String &p_str) const {
if (is_empty() && p_str.is_empty()) {
return 0;
}
if (is_empty()) {
return -1;
}
if (p_str.is_empty()) {
return 1;
}
const char32_t *that_str = p_str.get_data();
const char32_t *this_str = get_data();
while (true) {
if (*that_str == 0 && *this_str == 0) { // If both strings are at the end, they are equal.
return 0;
} else if (*this_str == 0) { // If at the end of this, and not of other, we are less.
return -1;
} else if (*that_str == 0) { // If at end of other, and not of this, we are greater.
return 1;
} else if (_find_upper(*this_str) < _find_upper(*that_str)) { // If current character in this is less, we are less.
return -1;
} else if (_find_upper(*this_str) > _find_upper(*that_str)) { // If current character in this is greater, we are greater.
return 1;
}
this_str++;
that_str++;
}
}
signed char String::casecmp_to(const String &p_str) const {
if (is_empty() && p_str.is_empty()) {
return 0;
}
if (is_empty()) {
return -1;
}
if (p_str.is_empty()) {
return 1;
}
const char32_t *that_str = p_str.get_data();
const char32_t *this_str = get_data();
while (true) {
if (*that_str == 0 && *this_str == 0) { // If both strings are at the end, they are equal.
return 0;
} else if (*this_str == 0) { // If at the end of this, and not of other, we are less.
return -1;
} else if (*that_str == 0) { // If at end of other, and not of this, we are greater.
return 1;
} else if (*this_str < *that_str) { // If current character in this is less, we are less.
return -1;
} else if (*this_str > *that_str) { // If current character in this is greater, we are greater.
return 1;
}
this_str++;
that_str++;
}
}
static _FORCE_INLINE_ signed char natural_cmp_common(const char32_t *&r_this_str, const char32_t *&r_that_str) {
// Keep ptrs to start of numerical sequences.
const char32_t *this_substr = r_this_str;
const char32_t *that_substr = r_that_str;
// Compare lengths of both numerical sequences, ignoring leading zeros.
while (is_digit(*r_this_str)) {
r_this_str++;
}
while (is_digit(*r_that_str)) {
r_that_str++;
}
while (*this_substr == '0') {
this_substr++;
}
while (*that_substr == '0') {
that_substr++;
}
int this_len = r_this_str - this_substr;
int that_len = r_that_str - that_substr;
if (this_len < that_len) {
return -1;
} else if (this_len > that_len) {
return 1;
}
// If lengths equal, compare lexicographically.
while (this_substr != r_this_str && that_substr != r_that_str) {
if (*this_substr < *that_substr) {
return -1;
} else if (*this_substr > *that_substr) {
return 1;
}
this_substr++;
that_substr++;
}
return 0;
}
signed char String::naturalcasecmp_to(const String &p_str) const {
const char32_t *this_str = get_data();
const char32_t *that_str = p_str.get_data();
if (this_str && that_str) {
while (*this_str == '.' || *that_str == '.') {
if (*this_str++ != '.') {
return 1;
}
if (*that_str++ != '.') {
return -1;
}
if (!*that_str) {
return 1;
}
if (!*this_str) {
return -1;
}
}
while (*this_str) {
if (!*that_str) {
return 1;
} else if (is_digit(*this_str)) {
if (!is_digit(*that_str)) {
return -1;
}
signed char ret = natural_cmp_common(this_str, that_str);
if (ret) {
return ret;
}
} else if (is_digit(*that_str)) {
return 1;
} else {
if (*this_str < *that_str) { // If current character in this is less, we are less.
return -1;
} else if (*this_str > *that_str) { // If current character in this is greater, we are greater.
return 1;
}
this_str++;
that_str++;
}
}
if (*that_str) {
return -1;
}
}
return 0;
}
signed char String::naturalnocasecmp_to(const String &p_str) const {
const char32_t *this_str = get_data();
const char32_t *that_str = p_str.get_data();
if (this_str && that_str) {
while (*this_str == '.' || *that_str == '.') {
if (*this_str++ != '.') {
return 1;
}
if (*that_str++ != '.') {
return -1;
}
if (!*that_str) {
return 1;
}
if (!*this_str) {
return -1;
}