This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathfoundation-string-native.cpp.h
More file actions
1141 lines (995 loc) · 41.7 KB
/
foundation-string-native.cpp.h
File metadata and controls
1141 lines (995 loc) · 41.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include <foundation.h>
#include <foundation-auto.h>
#include <foundation-unicode.h>
#include <foundation-string-hash.h>
#include "foundation-private.h"
////////////////////////////////////////////////////////////////////////////////
// This file contains low-level native string operations and is designed to be
// included in foundation-string.cpp.
// Although the current operations are based around 'char_t' the properties
// of the strings they assume are that all their characters are 1 unit long,
// are composed and folding preserves length.
////////////////////////////////////////////////////////////////////////////////
// Identity function used by templates for cases where no case folding is
// required.
inline
char_t __MCNativeChar_NoFold(char_t p_char)
{
return p_char;
}
// Return the folded version of 'char'.
inline
char_t __MCNativeChar_Fold(char_t p_char)
{
#if defined(__WINDOWS_1252__) || defined(__ISO_8859_1__)
static const char_t kMCStringFoldWindows1252_Mapping[256] =
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x9a, 0x8b, 0x9c, 0x8d, 0x9e, 0x8f,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0xff,
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf,
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
};
return kMCStringFoldWindows1252_Mapping[p_char];
#elif defined(__MACROMAN__)
static const char_t kMCStringFoldMacRoman_Mapping[256] =
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
0x8a, 0x8c, 0x8d, 0x8e, 0x96, 0x9a, 0x9f, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xbe, 0xbf,
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0x88, 0x8b, 0x9b, 0xcf, 0xcf,
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd8, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0x89, 0x90, 0x87, 0x91, 0x8f, 0x92, 0x94, 0x95, 0x93, 0x97, 0x99,
0xf0, 0x98, 0x9c, 0x9e, 0x9d, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
};
return kMCStringFoldMacRoman_Mapping[p_char];
#else
#error Unknown native encoding
#endif
}
// Return the folded version of 'char' into 'folded_char', and return true if
// the char is cased (i.e. there exists c' != c s.t. fold(c') == c).
static inline bool
__MCNativeChar_CheckedFold(char_t p_char,
char_t& r_folded_char)
{
/* These bit-tables can be automatically generated from the tables
in __MCNativeChar__Fold() and __MCNativeChar_Uppercase() using a
script like:
function MakeCheckMap pLowerMap, pUpperMap
local tResult, tRow, tBit, tChar, tBitMap
put empty into tResult
repeat with tRow = 1 to 8
put 0 into tBitMap
repeat with tBit = 1 to 32
put (tRow - 1) * 32 + tBit into tChar
if (item tChar of pLowerMap) is not \
(item tChar of pUpperMap) then
add 1 * (2 ^ (tBit - 1)) to tBitMap
end if
end repeat
put baseConvert(tBitMap, 10, 16) into tBitMap
repeat while the number of chars in tBitMap < 8
put "0" before tBitMap
end repeat
put "0x" & tBitMap & "U" after tResult
put comma & return after tResult
end repeat
return tResult
end MakeCheckMap
*/
uint8_t t_hi = p_char >> 5;
uint8_t t_lo = p_char & 0x1F;
#if defined(__WINDOWS_1252__) || defined(__ISO_8859_1__)
static const uint32_t kMCStringCheckFoldWindows1252_Mapping[8] =
{
0x00000000U,
0x00000000U,
0x07FFFFFEU,
0x07FFFFFEU,
0xD4005400U,
0x00000000U,
0x7F7FFFFFU,
0xFF7FFFFFU,
};
if (!(kMCStringCheckFoldWindows1252_Mapping[t_hi] & (1 << t_lo)))
{
r_folded_char = p_char;
return false;
}
#elif defined(__MACROMAN__)
static const uint32_t kMCStringCheckFoldMacRoman_Mapping[8] =
{
0x00000000U,
0x00000000U,
0x07FFFFFEU,
0x07FFFFFEU,
0xFFFFFFFFU,
0xC000C000U,
0x0300F800U,
0x003EFFE0U,
};
if (!(kMCStringCheckFoldMacRoman_Mapping[t_hi] & (1 << t_lo)))
{
r_folded_char = p_char;
return false;
}
#else
#error Unknown native encoding
#endif
r_folded_char = __MCNativeChar_Fold(p_char);
return true;
}
// Fold 'length' characters in 'chars', placing them into 'out_chars'.
static inline void
__MCNativeStr_Fold(const char_t *p_chars,
size_t p_length,
char_t *r_out_chars)
{
for(; p_length > 0; p_length -= 1)
*r_out_chars++ = __MCNativeChar_Fold(*p_chars++);
}
// Fold 'length' characters in 'chars', placing them into 'out_chars'.
// The function returns true if at least one of the chars is cased.
static inline bool
__MCNativeStr_CheckedFold(const char_t *p_chars,
size_t p_length,
char_t *r_out_char)
{
bool t_needs_fold;
t_needs_fold = false;
for(; p_length > 0; p_length -= 1)
{
if (__MCNativeChar_CheckedFold(*p_chars++, *r_out_char++))
{
t_needs_fold = true;
break;
}
}
for(; p_length > 0; p_length -= 1)
*r_out_char++ = __MCNativeChar_Fold(*p_chars++);
return t_needs_fold;
}
////////////////////////////////////////////////////////////////////////////////
// Return the lower-case version of 'char'.
static inline
char_t __MCNativeChar_Lowercase(char_t p_char)
{
return __MCNativeChar_Fold(p_char);
}
// Return the upper-case version of 'char'.
static inline
char_t __MCNativeChar_Uppercase(char_t p_char)
{
#if defined(__WINDOWS_1252__) || defined(__ISO_8859_1__)
static char_t kMCStringUppercaseWindows1252_Mapping[256] =
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x8a, 0x9b, 0x8c, 0x9d, 0x8e, 0x9f,
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xf7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0x9f,
};
return kMCStringUppercaseWindows1252_Mapping[p_char];
#elif defined(__MACROMAN__)
static char_t kMCStringUppercaseMacRoman_Mapping[256] =
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0xe7, 0xcb, 0xe5, 0x80, 0xcc, 0x81, 0x82, 0x83, 0xe9,
0xe6, 0xe8, 0xea, 0xed, 0xeb, 0xec, 0x84, 0xee, 0xf1, 0xef, 0x85, 0xcd, 0xf2, 0xf4, 0xf3, 0x86,
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xae, 0xaf,
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xce,
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd9, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0x49, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
};
return kMCStringUppercaseMacRoman_Mapping[p_char];
#else
#error Unknown native encoding
#endif
}
////////////////////////////////////////////////////////////////////////////////
// Compare two uncased or prefolded chars for caseless equality.
inline bool
__MCNativeChar_Equal_Unfolded(char_t p_left,
char_t p_right)
{
return p_left == p_right;
}
// Compare an unfolded char with an uncased or prefolded char for caseless
// equality.
inline bool
__MCNativeChar_Equal_Prefolded(char_t p_left,
char_t p_folded_right)
{
if (p_left != p_folded_right)
return __MCNativeChar_Fold(p_left) ==
p_folded_right;
return true;
}
// Compare two unfolded chars for caseless equality.
inline bool
__MCNativeChar_Equal_Folded(char_t p_left,
char_t p_right)
{
if (p_left != p_right)
return __MCNativeChar_Fold(p_left) ==
__MCNativeChar_Fold(p_right);
return true;
}
// Compare two uncased or prefolded chars.
inline ssize_t
__MCNativeChar_Compare_Unfolded(char_t p_left,
char_t p_right)
{
return p_left - p_right;
}
// Compare an unfolded char with an uncased or prefolded char.
inline ssize_t
__MCNativeChar_Compare_Prefolded(char_t p_left,
char_t p_folded_right)
{
if (p_left != p_folded_right)
return __MCNativeChar_Fold(p_left) -
p_folded_right;
return 0;
}
// Compare two unfolded chars.
inline ssize_t
__MCNativeChar_Compare_Folded(char_t p_left,
char_t p_right)
{
if (p_left != p_right)
return __MCNativeChar_Fold(p_left) -
__MCNativeChar_Fold(p_right);
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// Check the two strings for equality, using the given char comparison method.
template<bool (*CharEqual)(char_t left, char_t right)>
static inline bool
__MCNativeStr_Equal(const char_t *p_left_chars,
size_t p_left_length,
const char_t *p_right_chars,
size_t p_right_length)
{
if (p_left_length != p_right_length)
return false;
if (p_left_chars == p_right_chars)
return true;
while(p_left_length > 0)
{
if (!CharEqual(*p_left_chars++, *p_right_chars++))
return false;
p_left_length -= 1;
}
return true;
}
// Compare two strings, using the given char comparison method.
template<ssize_t (*CharCompare)(char_t left, char_t right)>
static inline ssize_t
__MCNativeStr_Compare(const char_t *p_left_chars,
size_t p_left_length,
const char_t *p_right_chars,
size_t p_right_length)
{
for(;;)
{
if (p_left_length == 0 || p_right_length == 0)
break;
ssize_t t_diff;
t_diff = CharCompare(*p_left_chars++, *p_right_chars++);
if (t_diff != 0)
return t_diff;
p_left_length -= 1;
p_right_length -= 1;
}
return (signed)p_left_length - (signed)p_right_length;
}
// Find the maximum equal prefix of the two strings, using the given char
// comparison method. The length of the shared prefix is returned.
template<bool (*CharEqual)(char_t left, char_t right)>
static inline size_t
__MCNativeStr_Prefix(const char_t *p_left_chars,
size_t p_left_length,
const char_t *p_right_chars,
size_t p_right_length)
{
if (p_right_length < p_left_length)
p_left_length = p_right_length;
size_t t_prefix;
t_prefix = 0;
for(;;)
{
if (p_left_length == 0)
break;
if (!CharEqual(*p_left_chars++, *p_right_chars++))
break;
t_prefix += 1;
p_left_length -= 1;
}
return t_prefix;
}
// Find the maximum equal suffix of the two strings, using the given char
// comparison method. The length of the shared suffix is returned.
template<bool (*CharEqual)(char_t left, char_t right)>
static inline size_t
__MCNativeStr_Suffix(const char_t *p_left_chars,
size_t p_left_length,
const char_t *p_right_chars,
size_t p_right_length)
{
p_left_chars += p_left_length;
p_right_chars += p_right_length;
if (p_right_length < p_left_length)
p_left_length = p_right_length;
size_t t_suffix;
t_suffix = 0;
for(;;)
{
if (p_left_length == 0)
break;
if (!CharEqual(*--p_left_chars, *--p_right_chars))
break;
t_suffix += 1;
p_left_length -= 1;
}
return t_suffix;
}
// Return the hash of the given string, using the given char folding method.
template<char_t (*CharFold)(char_t chr)>
static inline hash_t
__MCNativeStr_Hash(const char_t *p_chars,
size_t p_char_count)
{
MCHashCharsContext t_context;
while (p_char_count--)
t_context.consume(MCUnicodeCharMapFromNative(CharFold(*p_chars++)));
return t_context;
}
////////////////////////////////////////////////////////////////////////////////
// Most delimiter based string operations depend on a single 'scanning'
// operation:
//
// Scan(haystack, needle, max_count -> found_count, last_found_offset)
//
// If max_count is 0, then the number of occurrences of needle in haystack is
// returned.
//
// If max_count is not 0, then the haystack will be scanned for at most
// max_count occurences with the number found returned.
//
// In both cases, if at least 1 occurrence was found then the offset of the last
// found occurrence is also returned.
template<bool (*CharEqual)(char_t left, char_t right)>
struct __MCNativeStr_Forward
{
static inline size_t CharScan(const char_t *p_haystack_chars,
size_t p_haystack_length,
char_t p_needle_char,
size_t p_max_count,
size_t *r_offset)
{
size_t t_count;
t_count = 0;
size_t t_char_offset;
t_char_offset = 0;
size_t t_offset = 0;
while(t_char_offset < p_haystack_length)
{
if (CharEqual(p_haystack_chars[t_char_offset], p_needle_char))
{
t_offset = t_char_offset;
t_count += 1;
if (t_count == p_max_count)
break;
}
t_char_offset += 1;
}
if (t_count > 0 &&
r_offset != nil)
*r_offset = t_offset;
return t_count;
}
static inline size_t Scan(const char_t *p_haystack_chars,
size_t p_haystack_length,
const char_t *p_needle_chars,
size_t p_needle_length,
size_t p_max_count,
size_t *r_offset)
{
if (p_needle_length == 0)
return 0;
p_haystack_length -= p_needle_length;
size_t t_count;
t_count = 0;
size_t t_char_offset;
t_char_offset = 0;
size_t t_offset = 0;
while(t_char_offset <= p_haystack_length)
{
if (__MCNativeStr_Equal<CharEqual>(p_haystack_chars + t_char_offset,
p_needle_length,
p_needle_chars,
p_needle_length))
{
t_offset = t_char_offset;
t_count += 1;
if (t_count == p_max_count)
break;
t_char_offset += p_needle_length;
}
else
t_char_offset += 1;
}
if (t_count > 0 &&
r_offset != nil)
*r_offset = t_offset;
return t_count;
}
};
template<bool (*CharEqual)(char_t left, char_t right)>
struct __MCNativeStr_Reverse
{
static inline size_t CharScan(const char_t *p_haystack_chars,
size_t p_haystack_length,
char_t p_needle_char,
size_t p_max_count,
size_t *r_offset)
{
size_t t_count;
t_count = 0;
size_t t_char_offset;
t_char_offset = p_haystack_length;
size_t t_offset = 0;
while(t_char_offset > 0)
{
if (CharEqual(p_haystack_chars[t_char_offset - 1], p_needle_char))
{
t_offset = t_char_offset;
t_count += 1;
if (t_count == p_max_count)
break;
}
t_char_offset -= 1;
}
if (t_count > 0 &&
r_offset != nil)
*r_offset = t_offset - 1;
return t_count;
}
static inline size_t Scan(const char_t *p_haystack_chars,
size_t p_haystack_length,
const char_t *p_needle_chars,
size_t p_needle_length,
size_t p_max_count,
size_t *r_offset)
{
if (p_needle_length == 0)
return 0;
p_haystack_length -= p_needle_length;
size_t t_count;
t_count = 0;
size_t t_char_offset;
t_char_offset = p_haystack_length;
size_t t_offset = 0;
while(t_char_offset > 0)
{
if (__MCNativeStr_Equal<CharEqual>(p_haystack_chars + t_char_offset - 1,
p_needle_length,
p_needle_chars,
p_needle_length))
{
t_offset = t_char_offset;
t_count += 1;
if (t_count == p_max_count)
break;
t_char_offset -= p_needle_length;
}
else
t_char_offset -= 1;
}
if (t_count > 0 &&
r_offset != nil)
*r_offset = t_offset - 1;
return t_count;
}
};
////////////////////////////////////////////////////////////////////////////////
// This is the maximum length of 'needle' which will be prefolded in a caseless
// operation.
#define __NATIVEOP_PREFOLD_LIMIT 64
// Returns true if the given string options require folded comparison.
static inline bool __MCNativeOp_IsFolded(MCStringOptions p_options)
{
return p_options >= kMCStringOptionCompareFolded;
}
// Compare the two strings for equality, taking into account the given options.
static bool __MCNativeOp_IsEqualTo(const char_t *p_left_chars,
size_t p_left_length,
const char_t *p_right_chars,
size_t p_right_length,
MCStringOptions p_options)
{
if (__MCNativeOp_IsFolded(p_options))
return __MCNativeStr_Equal<__MCNativeChar_Equal_Folded>(p_left_chars,
p_left_length,
p_right_chars,
p_right_length);
return __MCNativeStr_Equal<__MCNativeChar_Equal_Unfolded>(p_left_chars,
p_left_length,
p_right_chars,
p_right_length);
}
// Compare the two strings, taking into account the given options.
static ssize_t __MCNativeOp_CompareTo(const char_t *p_left_chars,
size_t p_left_length,
const char_t *p_right_chars,
size_t p_right_length,
MCStringOptions p_options)
{
if (__MCNativeOp_IsFolded(p_options))
return __MCNativeStr_Compare<__MCNativeChar_Compare_Folded>(p_left_chars,
p_left_length,
p_right_chars,
p_right_length);
return __MCNativeStr_Compare<__MCNativeChar_Compare_Unfolded>(p_left_chars,
p_left_length,
p_right_chars,
p_right_length);
}
// Hash the given string, taking into account the given options.
static hash_t __MCNativeOp_Hash(const char_t *p_chars,
size_t p_char_count,
MCStringOptions p_options)
{
if (__MCNativeOp_IsFolded(p_options))
return __MCNativeStr_Hash<__MCNativeChar_Fold>(p_chars,
p_char_count);
return __MCNativeStr_Hash<__MCNativeChar_NoFold>(p_chars,
p_char_count);
}
// Return the length of maximum shared prefix of the two strings, taking into
// account the given options.
static size_t __MCNativeOp_SharedPrefix(const char_t *p_left_chars,
size_t p_left_length,
const char_t *p_right_chars,
size_t p_right_length,
MCStringOptions p_options)
{
if (__MCNativeOp_IsFolded(p_options))
return __MCNativeStr_Prefix<__MCNativeChar_Equal_Folded>(p_left_chars,
p_left_length,
p_right_chars,
p_right_length);
return __MCNativeStr_Prefix<__MCNativeChar_Equal_Unfolded>(p_left_chars,
p_left_length,
p_right_chars,
p_right_length);
}
// Return the length of maximum shared suffix of the two strings, taking into
// account the given options.
static size_t __MCNativeOp_SharedSuffix(const char_t *p_left_chars,
size_t p_left_length,
const char_t *p_right_chars,
size_t p_right_length,
MCStringOptions p_options)
{
if (__MCNativeOp_IsFolded(p_options))
return __MCNativeStr_Suffix<__MCNativeChar_Equal_Folded>(p_left_chars,
p_left_length,
p_right_chars,
p_right_length);
return __MCNativeStr_Suffix<__MCNativeChar_Equal_Unfolded>(p_left_chars,
p_left_length,
p_right_chars,
p_right_length);
}
// Perform the appropriate direction 'scan' operation with the given parameters.
// The direction is determined by the classed used for 'Actions'.
template<template<bool (*CharEqual)(char_t left, char_t right)> class Actions>
static size_t __MCNativeOp_Scan(const char_t *p_haystack_chars,
size_t p_haystack_length,
const char_t *p_needle_chars,
size_t p_needle_length,
size_t p_max_count,
MCStringOptions p_options,
size_t *r_offset)
{
char_t t_folded_needle_char;
char_t t_folded_needle[__NATIVEOP_PREFOLD_LIMIT];
if (p_needle_length > p_haystack_length)
return 0;
if (!__MCNativeOp_IsFolded(p_options))
{
if (p_needle_length == 1)
{
goto unfolded_char;
}
else
{
goto unfolded;
}
}
else
{
if (p_needle_length == 1)
{
if (__MCNativeChar_CheckedFold(p_needle_chars[0], t_folded_needle_char))
goto prefolded_char;
else
goto unfolded_char;
}
else if (p_needle_length < __NATIVEOP_PREFOLD_LIMIT)
{
if (__MCNativeStr_CheckedFold(p_needle_chars, p_needle_length, t_folded_needle))
goto prefolded;
else
goto unfolded;
}
else
{
goto folded;
}
}
unfolded_char:
return Actions<__MCNativeChar_Equal_Unfolded>::CharScan
(p_haystack_chars,
p_haystack_length,
p_needle_chars[0],
p_max_count,
r_offset);
unfolded:
return Actions<__MCNativeChar_Equal_Unfolded>::Scan
(p_haystack_chars,
p_haystack_length,
p_needle_chars,
p_needle_length,
p_max_count,
r_offset);
prefolded_char:
return Actions<__MCNativeChar_Equal_Prefolded>::CharScan
(p_haystack_chars,
p_haystack_length,
t_folded_needle_char,
p_max_count,
r_offset);
prefolded:
return Actions<__MCNativeChar_Equal_Prefolded>::Scan
(p_haystack_chars,
p_haystack_length,
t_folded_needle,
p_needle_length,
p_max_count,
r_offset);
folded:
return Actions<__MCNativeChar_Equal_Folded>::Scan
(p_haystack_chars,
p_haystack_length,
p_needle_chars,
p_needle_length,
p_max_count,
r_offset);
}
#define __MCNativeOp_ForwardScan __MCNativeOp_Scan<__MCNativeStr_Forward>
#define __MCNativeOp_ReverseScan __MCNativeOp_Scan<__MCNativeStr_Reverse>
// Search forward for needle in haystack, using the given options.
// If needle is found, then its offset is returned and the result is true.
static bool __MCNativeOp_FirstIndexOf(const char_t *p_haystack_chars,
size_t p_haystack_length,
const char_t *p_needle_chars,
size_t p_needle_length,
MCStringOptions p_options,
size_t& r_offset)
{
return __MCNativeOp_ForwardScan(p_haystack_chars,
p_haystack_length,
p_needle_chars,
p_needle_length,
1,
p_options,
&r_offset) == 1;
}
// Search backward for needle in haystack, using the given options.
// If needle is found, then its offset is returned and the result is true.
static bool __MCNativeOp_LastIndexOf(const char_t *p_haystack_chars,
size_t p_haystack_length,
const char_t *p_needle_chars,
size_t p_needle_length,
MCStringOptions p_options,
size_t& r_offset)
{
return __MCNativeOp_ReverseScan(p_haystack_chars,
p_haystack_length,
p_needle_chars,
p_needle_length,
1,
p_options,
&r_offset) == 1;
}
// Skips count occurrences of needle in haystack, using the given options.
// If the specified number of occurrences are found then true is returned and
// the offset of the last one is passed back.
static bool __MCNativeOp_Skip(const char_t *p_haystack_chars,
size_t p_haystack_length,
const char_t *p_needle_chars,
size_t p_needle_length,
size_t p_count,
MCStringOptions p_options,
size_t *r_last_offset)
{
return __MCNativeOp_ForwardScan(p_haystack_chars,
p_haystack_length,
p_needle_chars,
p_needle_length,
p_count,
p_options,
r_last_offset) == p_count;
}
// Return the number of occurrences of needle in haystack, using the given
// options. The offset of the last found occurrence is returned, if any.
static size_t __MCNativeOp_Count(const char_t *p_haystack_chars,
size_t p_haystack_length,
const char_t *p_needle_chars,
size_t p_needle_length,
MCStringOptions p_options,
size_t *r_last_offset)
{
return __MCNativeOp_ForwardScan(p_haystack_chars,
p_haystack_length,
p_needle_chars,
p_needle_length,
0,
p_options,
r_last_offset);
}
// Return true if haystack contains needle, using the given options.
static bool __MCNativeOp_Contains(const char_t *p_haystack_chars,
size_t p_haystack_length,
const char_t *p_needle_chars,
size_t p_needle_length,
MCStringOptions p_options)
{
return __MCNativeOp_ForwardScan(p_haystack_chars,
p_haystack_length,
p_needle_chars,
p_needle_length,
1,
p_options,
nil) == 1;
}
// Return true if haystack begins with needle, using the given options.
static bool __MCNativeOp_BeginsWith(const char_t *p_haystack_chars,
size_t p_haystack_length,
const char_t *p_needle_chars,
size_t p_needle_length,
MCStringOptions p_options)
{
if (p_needle_length > p_haystack_length)
return false;
return __MCNativeOp_IsEqualTo(p_haystack_chars,
p_needle_length,
p_needle_chars,
p_needle_length,
p_options);
}
// Return true if haystack ends with needle, using the given options.
static bool __MCNativeOp_EndsWith(const char_t *p_haystack_chars,
size_t p_haystack_length,
const char_t *p_needle_chars,
size_t p_needle_length,
MCStringOptions p_options)
{
if (p_needle_length > p_haystack_length)
return false;
return __MCNativeOp_IsEqualTo(p_haystack_chars + p_haystack_length - p_needle_length,
p_needle_length,
p_needle_chars,
p_needle_length,
p_options);
}
////////////////////////////////////////////////////////////////////////////////
// The Core template (parameterized by char equality comparator) for the
// optimized DelimiterOffset function when delimiter is a single char.
template<bool (*DelimiterCharEqual)(char_t left, char_t right)>
static bool __MCNativeOp_ForwardCharDelimitedOffset_Core(const char_t *p_haystack_chars,
size_t p_haystack_length,
const char_t *p_needle_chars,
size_t p_needle_length,
char_t p_delimiter_char,
size_t p_skip,
MCStringOptions p_options,
size_t& r_index,
size_t *r_found_offset,
size_t *r_before_offset,
size_t *r_after_offset)
{
if (p_needle_length == 0)
return false;
size_t t_index;
t_index = 0;
size_t t_offset = 0;
t_offset = 0;
size_t t_end_before;
t_end_before = 0;
for(; p_skip > 0 && t_offset < p_haystack_length; t_offset++)
{
if (DelimiterCharEqual(p_haystack_chars[t_offset], p_delimiter_char))
{
p_skip -= 1;
t_index += 1;
t_end_before = t_offset;
}
}
size_t t_start_found;
if (!__MCNativeOp_FirstIndexOf(p_haystack_chars + t_offset,