-
Notifications
You must be signed in to change notification settings - Fork 0
/
strcase.go
1644 lines (1561 loc) · 39.9 KB
/
strcase.go
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 2023 Charlie Vieth. All rights reserved.
// Use of this source code is governed by the MIT license.
package strcase
import (
"strings"
"unicode/utf8"
"github.com/charlievieth/strcase/internal/bytealg"
"github.com/charlievieth/strcase/internal/tables"
)
const UnicodeVersion = tables.UnicodeVersion
const maxBruteForce = 16 // substring length
const maxLen = 32 // subject length
func clamp(n int) int {
if n < 0 {
return -1
}
if n > 0 {
return 1
}
return 0
}
// Compare returns an integer comparing two strings lexicographically
// ignoring case.
// The result will be 0 if a == b, -1 if a < b, and +1 if a > b.
func Compare(s, t string) int {
// TODO: move next to hasPrefixUnicode
i := 0
for ; i < len(s) && i < len(t); i++ {
sr := s[i]
tr := t[i]
if (sr|tr)&utf8.RuneSelf != 0 {
goto hasUnicode
}
if sr == tr || _lower[sr] == _lower[tr] {
continue
}
if _lower[sr] < _lower[tr] {
return -1
}
return 1
}
return clamp(len(s) - len(t))
hasUnicode:
s = s[i:]
t = t[i:]
for _, sr := range s {
// If t is exhausted the strings are not equal.
if len(t) == 0 {
return 1
}
// Extract first rune from second string.
var tr rune
if t[0] < utf8.RuneSelf {
tr, t = rune(_lower[t[0]]), t[1:]
} else {
r, size := utf8.DecodeRuneInString(t)
tr, t = tables.CaseFold(r), t[size:]
}
// Easy case.
if sr == tr || tables.CaseFold(sr) == tr {
continue
}
return clamp(int(tables.CaseFold(sr)) - int(tr))
}
if len(t) == 0 {
return 0
}
return -1
}
// EqualFold reports whether s and t, interpreted as UTF-8 strings,
// are equal under simple Unicode case-folding, which is a more general
// form of case-insensitivity.
//
// EqualFold is included for symmetry with the strings package and because
// our implementation is usually 2x faster than [strings.EqualFold].
func EqualFold(s, t string) bool {
return Compare(s, t) == 0
}
func isAlpha(c byte) bool {
return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z'
}
// NB: we previously used a table that only contained the 128 ASCII characters
// and a mask, but this is a about ~6% faster.
var _lower = [256]byte{
0, 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, ' ', '!', '"', '#', '$', '%',
'&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4',
'5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'a', 'b', 'c',
'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '[', '\\', ']', '^', '_', '`', 'a',
'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', 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,
}
// containsKelvin returns true if string s contains rune 'K' (Kelvin).
func containsKelvin(s string) bool {
// TODO: it might be faster to check with IndexNonASCII first
// then with Count.
return len(s) > 0 && indexRuneCase(s, '\u212A') != -1
}
// HasPrefix tests whether the string s begins with prefix ignoring case.
func HasPrefix(s, prefix string) bool {
ok, _ := hasPrefixUnicode(s, prefix)
return ok
}
// hasPrefixUnicode returns if string s begins with prefix (ignoring case) and
// if all of s was consumed matching prefix (either before a match could be found
// or is prefix consumes all of s).
func hasPrefixUnicode(s, prefix string) (bool, bool) {
// TODO: return an enum instead of two bools
// The max difference in encoded lengths between cases is 2 bytes for
// [kK] (1 byte) and Kelvin 'K' (3 bytes).
n := len(s)
if len(prefix) > n*3 || (len(prefix) > n*2 && !containsKelvin(prefix)) {
return false, true
}
// ASCII fast path
i := 0
for ; i < len(s) && i < len(prefix); i++ {
sr := s[i]
tr := prefix[i]
if (sr|tr)&utf8.RuneSelf != 0 {
goto hasUnicode
}
if tr == sr || _lower[sr] == _lower[tr] {
continue
}
return false, i == len(s)-1
}
// Check if we've exhausted s
return i == len(prefix), i == len(s)
hasUnicode:
s = s[i:]
prefix = prefix[i:]
for _, tr := range prefix {
// If s is exhausted the strings are not equal.
if len(s) == 0 {
return false, true
}
var sr rune
if s[0] < utf8.RuneSelf {
sr, s = rune(_lower[s[0]]), s[1:]
} else {
r, size := utf8.DecodeRuneInString(s)
sr, s = r, s[size:]
}
if tr == sr || tables.CaseFold(tr) == tables.CaseFold(sr) {
continue
}
return false, len(s) == 0
}
return true, len(s) == 0 // s exhausted
}
// TrimPrefix returns s without the provided leading prefix string.
// If s doesn't start with prefix, s is returned unchanged.
func TrimPrefix(s, prefix string) string {
// The max difference in encoded lengths between cases is 2 bytes for
// [kK] (1 byte) and Kelvin 'K' (3 bytes).
n := len(s)
if n*3 < len(prefix) || (n*2 < len(prefix) && !containsKelvin(prefix)) {
return s
}
// ASCII fast path
i := 0
for ; i < len(s) && i < len(prefix); i++ {
sr := s[i]
tr := prefix[i]
if (sr|tr)&utf8.RuneSelf != 0 {
goto hasUnicode
}
if tr == sr || _lower[sr] == _lower[tr] {
continue
}
return s
}
return s[i:]
hasUnicode:
ss := s
s = s[i:]
prefix = prefix[i:]
for _, tr := range prefix {
// If s is exhausted the strings are not equal.
if len(s) == 0 {
return ss
}
var sr rune
if s[0] < utf8.RuneSelf {
sr, s = rune(_lower[s[0]]), s[1:]
} else {
r, size := utf8.DecodeRuneInString(s)
sr, s = tables.CaseFold(r), s[size:]
}
if tr == sr || tables.CaseFold(tr) == sr {
continue
}
return ss
}
return s
}
// HasSuffix tests whether the string s ends with suffix.
func HasSuffix(s, suffix string) bool {
ok, _, _ := hasSuffixUnicode(s, suffix)
return ok
}
// hasSuffixUnicode returns if string s ends with suffix and if all of s was
// consumed matching suffix (either before a match could be found or is suffix
// consumes all of s).
func hasSuffixUnicode(s, suffix string) (bool, bool, int) {
// The max difference in encoded lengths between cases is 2 bytes for
// [kK] (1 byte) and Kelvin 'K' (3 bytes).
nt := len(suffix)
ns := len(s)
if nt == 0 {
return true, false, ns
}
if ns*3 < nt || (ns*2 < nt && !containsKelvin(suffix)) {
return false, true, 0
}
t := suffix
i := ns - 1
j := nt - 1
for ; i >= 0 && j >= 0; i, j = i-1, j-1 {
sr := s[i]
tr := t[j]
if (sr|tr)&utf8.RuneSelf != 0 {
goto hasUnicode
}
if tr == sr || _lower[sr] == _lower[tr] {
continue
}
return false, i == 0, 0
}
return j == -1, i <= 0, i + 1
hasUnicode:
s = s[:i+1]
t = t[:j+1]
for len(s) != 0 && len(t) != 0 {
var sr, tr rune
if n := len(s) - 1; s[n] < utf8.RuneSelf {
sr, s = rune(_lower[s[n]]), s[:n]
} else {
r, size := utf8.DecodeLastRuneInString(s)
sr, s = r, s[:len(s)-size]
}
if n := len(t) - 1; t[n] < utf8.RuneSelf {
tr, t = rune(_lower[t[n]]), t[:n]
} else {
r, size := utf8.DecodeLastRuneInString(t)
tr, t = r, t[:len(t)-size]
}
if sr == tr || tables.CaseFold(sr) == tables.CaseFold(tr) {
continue
}
return false, len(s) == 0, 0
}
return len(t) == 0, len(s) == 0, len(s)
}
// TrimSuffix returns s without the provided trailing suffix string.
// If s doesn't end with suffix, s is returned unchanged.
func TrimSuffix(s, suffix string) string {
if match, _, i := hasSuffixUnicode(s, suffix); match {
return s[:i]
}
return s
}
// bruteForceIndexUnicode performs a brute-force search for substr in s.
func bruteForceIndexUnicode(s, substr string) int {
var u0, u1 rune
var sz0, sz1 int
if substr[0] < utf8.RuneSelf {
u0, sz0 = rune(substr[0]), 1
} else {
u0, sz0 = utf8.DecodeRuneInString(substr)
}
if substr[sz0] < utf8.RuneSelf {
u1, sz1 = rune(substr[sz0]), 1
} else {
u1, sz1 = utf8.DecodeRuneInString(substr[sz0:])
}
folds0 := tables.FoldMapExcludingUpperLower(u0)
folds1 := tables.FoldMapExcludingUpperLower(u1)
hasFolds0 := folds0[0] != 0
hasFolds1 := folds1[0] != 0
needle := substr[sz0+sz1:]
// Ugly hack
var l0, l1 rune
if u0 != 'İ' {
u0, l0, _ = tables.ToUpperLower(u0)
} else {
l0 = 'İ'
}
if u1 != 'İ' {
u1, l1, _ = tables.ToUpperLower(u1)
} else {
l1 = 'İ'
}
// Limit search space.
t := len(s) - len(substr)/3 + 2
if t > len(s) {
t = len(s)
}
switch {
case !hasFolds0 && u0 == l0 && !hasFolds1 && u1 == l1:
// Fast check for the first rune.
i := strings.Index(s, substr[:sz0+sz1])
if i < 0 {
return -1
}
for i < t {
var n0 int
var r0 rune
if s[i] < utf8.RuneSelf {
r0, n0 = rune(s[i]), 1
} else {
r0, n0 = utf8.DecodeRuneInString(s[i:])
}
if r0 != u0 {
i += n0
continue
}
if i+n0 >= t {
break
}
var n1 int
var r1 rune
if s[i+n0] < utf8.RuneSelf {
r1, n1 = rune(s[i+n0]), 1
} else {
r1, n1 = utf8.DecodeRuneInString(s[i+n0:])
}
if r1 != u1 {
i += n0
if r1 != u0 {
i += n1 // Skip 2 runes when possible
}
continue
}
match, noMore := hasPrefixUnicode(s[i+n0+n1:], needle)
if match {
return i
}
if noMore {
break
}
i += n0
if r1 != u0 {
i += n1 // Skip 2 runes when possible
}
}
return -1
case !hasFolds0 && !hasFolds1:
// TODO: check is adding a fast check for l0 and u0 is faster
i := 0
for i < t {
var n0 int
var r0 rune
if s[i] < utf8.RuneSelf {
r0, n0 = rune(s[i]), 1
} else {
r0, n0 = utf8.DecodeRuneInString(s[i:])
}
if r0 != u0 && r0 != l0 {
i += n0
continue
}
if i+n0 >= t {
break
}
var n1 int
var r1 rune
if s[i+n0] < utf8.RuneSelf {
r1, n1 = rune(s[i+n0]), 1
} else {
r1, n1 = utf8.DecodeRuneInString(s[i+n0:])
}
if r1 != u1 && r1 != l1 {
i += n0
if r1 != u0 && r1 != l0 {
i += n1 // Skip 2 runes when possible
}
continue
}
match, noMore := hasPrefixUnicode(s[i+n0+n1:], needle)
if match {
return i
}
if noMore {
break
}
i += n0
if r1 != u0 && r1 != l0 {
i += n1 // Skip 2 runes when possible
}
}
return -1
default:
// TODO: see if there is a better cutoff to use
i := 0
for i < t {
var n0 int
var r0 rune
if s[i] < utf8.RuneSelf {
r0, n0 = rune(s[i]), 1
} else {
r0, n0 = utf8.DecodeRuneInString(s[i:])
}
if r0 != u0 && r0 != l0 {
if !hasFolds0 || (r0 != folds0[0] && r0 != folds0[1]) {
i += n0
continue
}
}
if i+n0 >= t {
break
}
var n1 int
var r1 rune
if s[i+n0] < utf8.RuneSelf {
r1, n1 = rune(s[i+n0]), 1
} else {
r1, n1 = utf8.DecodeRuneInString(s[i+n0:])
}
if r1 != u1 && r1 != l1 {
if !hasFolds1 || (r1 != folds1[0] && r1 != folds1[1]) {
i += n0
if !hasFolds0 && r1 != u0 && r1 != l0 {
i += n1 // Skip 2 runes when possible
}
continue
}
}
match, noMore := hasPrefixUnicode(s[i+n0+n1:], needle)
if match {
return i
}
if noMore {
break
}
i += n0
if !hasFolds0 && r1 != u0 && r1 != l0 {
i += n1 // Skip 2 runes when possible
}
}
return -1
}
}
// nonLetterASCII checks if the first 32 bytes of s consist only of
// non-letter ASCII characters. This is used to quickly check if we
// can use strings.Index.
func nonLetterASCII(s string) bool {
for i := 0; i < len(s); i++ {
// NB: this is faster than using a lookup table
c := s[i] | ' ' // simplify check for alpha
if c&utf8.RuneSelf != 0 || 'a' <= c && c <= 'z' {
return false
}
}
return true
}
// TODO: check substr for any folds - this should almost always be faster
// than our current search - especially since we end up having to scan it
// multiple times. Maybe: check first rune (and maybe second), then check
// if substr has any folds.
//
// That said this library is meant for dealing with text and this might
// only with languages that don't have any/few folds.
//
// One thing we could do is use the longest prefix that does not have any
// folds and use the fast string search for that.
// Index returns the index of the first instance of substr in s, or -1 if
// substr is not present in s.
func Index(s, substr string) int {
n := len(substr)
var r rune
var size int
if n > 0 {
if substr[0] < utf8.RuneSelf {
r, size = rune(substr[0]), 1
} else {
r, size = utf8.DecodeRuneInString(substr)
}
}
switch {
case n == 0:
return 0
case n == 1:
return IndexByte(s, byte(r))
case n == size:
return IndexRune(s, r)
case n >= len(s):
if n > len(s)*3 {
return -1
}
// Match here is possible due to upper/lower case runes
// having different encoded sizes.
//
// Fast check to see if s contains the first character of substr.
i := IndexRune(s, r)
if i < 0 {
return -1
}
// Reduce the search space
s = s[i:]
// Kelvin K is three times the size of ASCII [Kk] so we need
// to check for it to see if the longer needle (substr) could
// possibly match the shorter haystack (s).
if n > len(s)*2 && !containsKelvin(substr) {
return -1
}
// NB: until disproven this is sufficiently fast (and maybe fastest)
if o := bruteForceIndexUnicode(s, substr); o != -1 {
return o + i
}
return -1
case n <= maxLen: // WARN: 32 is for arm64 (see: bytealg.MaxLen)
// WARN:
// * this does not take non-folding runes into account
// * figure out when this negatively impacts performance
// * consider skipping if s is small relative to substr
// * check if s contains the first char of substr first ???
// * check after the maxBruteForce check ???
//
// Fast path if the sub-string is all non-alpha ASCII chars
//
// NB: This only works because len(substr) <= 32
//
// TODO:
// * Are we just gaming benchmarks here and need a quicker
// cutover to Rabin-Karp?
// * We should skip this check if s is small
//
if bytealg.NativeIndex && n <= 32 && nonLetterASCII(substr) {
return bytealg.IndexString(s, substr)
}
// TODO: tune this
if len(s) <= maxBruteForce {
return bruteForceIndexUnicode(s, substr)
}
// fallthrough
}
var u0, u1 rune
var sz0, sz1 int
if substr[0] < utf8.RuneSelf {
u0, sz0 = rune(substr[0]), 1
} else {
u0, sz0 = utf8.DecodeRuneInString(substr)
}
if substr[sz0] < utf8.RuneSelf {
u1, sz1 = rune(substr[sz0]), 1
} else {
u1, sz1 = utf8.DecodeRuneInString(substr[sz0:])
}
// hasFolds{0,1} should be rare so consider optimizing
// the no folds case
folds0 := tables.FoldMapExcludingUpperLower(u0)
folds1 := tables.FoldMapExcludingUpperLower(u1)
hasFolds0 := folds0[0] != 0
hasFolds1 := folds1[0] != 0
needle := substr[sz0+sz1:]
// Ugly hack
var l0, l1 rune
if u0 != 'İ' {
u0, l0, _ = tables.ToUpperLower(u0)
} else {
l0 = 'İ'
}
if u1 != 'İ' {
u1, l1, _ = tables.ToUpperLower(u1)
} else {
l1 = 'İ'
}
fails := 0
// TODO: see if we can stop sooner.
t := len(s) - len(substr)/3 + 1
if t > len(s) {
t = len(s)
}
for i := 0; i < t; {
var r0 rune
var n0 int
if s[i] < utf8.RuneSelf {
r0, n0 = rune(s[i]), 1
} else {
r0, n0 = utf8.DecodeRuneInString(s[i:])
}
if r0 != u0 && r0 != l0 && (!hasFolds0 || (r0 != folds0[0] && r0 != folds0[1])) {
var o, sz int
if !hasFolds0 {
o, sz = indexRune2(s[i+n0:], l0, u0)
} else {
// TODO: pass folds to indexRune so that we don't have to
// look them up again.
o, sz = indexRune(s[i+n0:], l0)
}
if o < 0 {
return -1
}
i += o + n0
n0 = sz // The rune we matched on might not be the same size as c0
}
if i+n0 >= t {
return -1
}
var r1 rune
var n1 int
if s[i+n0] < utf8.RuneSelf {
r1, n1 = rune(s[i+n0]), 1
} else {
r1, n1 = utf8.DecodeRuneInString(s[i+n0:])
}
if r1 == u1 || r1 == l1 || (hasFolds1 && (r1 == folds1[0] || r1 == folds1[1])) {
match, exhausted := hasPrefixUnicode(s[i+n0+n1:], needle)
if match {
return i
}
if exhausted {
return -1
}
}
fails++
i += n0
// NB: After tuning this appears to be ideal across platforms.
if fails >= 4+i>>4 && i < t {
// From bytes/bytes.go:
//
// Give up on IndexByte, it isn't skipping ahead
// far enough to be better than Rabin-Karp.
// Experiments (using IndexPeriodic) suggest
// the cutover is about 16 byte skips.
// TODO: if large prefixes of sep are matching
// we should cutover at even larger average skips,
// because Equal becomes that much more expensive.
// This code does not take that effect into account.
//
// NB(charlie): The above part about "Equal" being
// more expensive is particularly relevant us since
// our "Equal" is drastically more expensive than
// the stdlibs.
j := indexRabinKarpUnicode(s[i:], substr)
if j < 0 {
return -1
}
return i + j
}
}
return -1
}
// LastIndex returns the index of the last instance of substr in s, or -1 if
// substr is not present in s.
func LastIndex(s, substr string) int {
n := len(substr)
var u rune
var size int
if n > 0 {
if substr[n-1] < utf8.RuneSelf {
u, size = rune(substr[n-1]), 1
} else {
u, size = utf8.DecodeRuneInString(substr)
}
}
switch {
case n == 0:
return len(s)
case n == 1:
return LastIndexByte(s, substr[0])
case n == size:
return lastIndexRune(s, u)
case n >= len(s):
if n > len(s)*3 {
return -1
}
if n > len(s)*2 && !containsKelvin(substr) {
return -1
}
// fallthrough
}
return indexRabinKarpRevUnicode(s, substr)
}
// IndexByte returns the index of the first instance of c (ignoring case) in s,
// or -1 if c is not present in s. Matching is case-insensitive and Unicode
// simple folding is used which means that bytes 'K' and 'k' match Kelvin 'K'
// bytes 'S' and 's' match 'ſ'. Therefore, string s may be scanned twice when
// c is [KkSs] (because the optimized assembly ASCII only).
//
// On amd64 and arm64 this is only ~20-25% slower than strings.IndexByte for
// small strings (<4M) and ~6% slower for larger strings.
// The slowdown for small strings is due to some additional initial overhead.
func IndexByte(s string, c byte) int {
// TODO: the below quick check is only to improve benchmark performance for
// small strings (where the overhead of this function and IndexByteString
// not being inlined becomes noticeable ~1ns).
// See if we can get this function inlined or reduce the overhead of
// indexByte.
//
// Fast check for bytes that can't be folded to from Unicode chars.
// This shaves one or two nanoseconds from IndexByte, which is
// meaningful when s is small.
switch c {
case 'K', 'S', 'k', 's':
i, _ := indexByte(s, c)
return i
default:
return bytealg.IndexByteString(s, c)
}
}
// IndexByte returns the index of the first instance of c (ignoring case) in s,
// or -1 if c is not present in s. Case matching is ASCII only, unlike
// [IndexByte] which is Unicode aware.
func IndexByteASCII(s string, c byte) int {
return bytealg.IndexByteString(s, c)
}
// indexByte returns the index of the first instance of c in s, or -1 if c is
// not present in s and the size (in bytes) of the character matched (this is
// needed to handle matching [Kk] and [Ss] to multibyte characters 'K' and 'ſ').
func indexByte(s string, c byte) (int, int) {
if len(s) == 0 {
return -1, 1
}
n := bytealg.IndexByteString(s, c)
// Special case for Unicode characters that map to ASCII.
var r rune
var sz int
switch c {
case 'K', 'k':
r = 'K' // Kelvin K
sz = 3
case 'S', 's':
r = 'ſ' // Latin small letter long S
sz = 2
default:
return n, 1
}
// Search for Unicode characters that map to ASCII byte 'c'
if n > 0 {
if n < sz {
return n, 1 // Matched c before a possible rune 'r'
}
s = s[:n] // Limit search space
}
if o := indexRuneCase(s, r); n == -1 || (o != -1 && o < n) {
return o, sz
}
return n, 1
}
// LastIndexByte returns the index of the last instance of c in s, or -1
// if c is not present in s.
func LastIndexByte(s string, c byte) int {
if len(s) == 0 {
return -1
}
if !isAlpha(c) {
return strings.LastIndexByte(s, c)
}
// Special case for Unicode characters that map to ASCII.
var r rune
switch c {
case 'K', 'k':
r = 'K'
case 'S', 's':
r = 'ſ'
default:
c |= ' ' // convert to lower case
for i := len(s) - 1; i >= 0; i-- {
if s[i]|' ' == c {
return i
}
}
return -1
}
// Handle ASCII characters with Unicode mappings
c |= ' ' // convert to lower case
for i := len(s); i > 0; {
if s[i-1] < utf8.RuneSelf {
i--
if s[i]|' ' == c {
return i
}
} else {
sr, size := utf8.DecodeLastRuneInString(s[:i])
i -= size
if sr == r {
return i
}
}
}
return -1
}
// IndexRune returns the index of the first instance of the Unicode code point
// r, or -1 if rune is not present in s.
// If r is utf8.RuneError, it returns the first instance of any
// invalid UTF-8 byte sequence.
func IndexRune(s string, r rune) int {
// TODO: This is faster than strings.IndexRune when r is not ASCII
// so make a PR to go/strings.
// TODO: consider checking if r is ASCII here so that it can be inlined
i, _ := indexRune(s, r)
return i
}
// indexRune returns the index of the first instance of the Unicode code point
// r and the size of the rune that matched.
func indexRune(s string, r rune) (int, int) {
// TODO: handle invalid runes
switch {
case 0 <= r && r < utf8.RuneSelf:
// TODO: Check if we can use bytealg.IndexByteString directly.
return indexByte(s, byte(r))
case r == utf8.RuneError:
for i, r := range s {
if r == utf8.RuneError {
return i, utf8.RuneLen(r)
}
}
return -1, 1
case !utf8.ValidRune(r):
return -1, 1
default:
// TODO: use a function for len(folds)
if folds := tables.FoldMap(r); folds != nil {
size := utf8.RuneLen(r)
n := indexRuneCase(s, r)
if n == 0 {
return 0, size
}
if n > 0 {
s = s[:n]
}
for i := 0; i < len(folds); i++ {
rr := rune(folds[i])
if rr == r {
continue
}
if rr == 0 {
break
}
o := indexRuneCase(s, rr)
if o != -1 && (n == -1 || o < n) {
n = o
s = s[:n]
size = utf8.RuneLen(rr)
}
}
return n, size
} else if u, l, ok := tables.ToUpperLower(r); ok {
return indexRune2(s, l, u)
}
return indexRuneCase(s, r), utf8.RuneLen(r)
}
}
// TODO: consider creating a version of this function for when we've already
// decoded the rune and know that it is valid and not-ASCII.
//
// indexRuneCase is a *case-sensitive* version of strings.IndexRune that is
// generally faster for Unicode characters since it searches for the rune's
// second byte, which is generally more unique, instead of the first byte,
// like strings.IndexRune.
func indexRuneCase(s string, r rune) int {
// TODO:
// * remove if my PR is ever merged
// * consider searching for the first byte if it is not one of:
// 240, 243, or 244 (which are the first byte of ~78% of multi-byte
// Unicode characters).
switch {
case 0 <= r && r < utf8.RuneSelf:
return strings.IndexByte(s, byte(r))
case r == utf8.RuneError:
for i, r := range s {
if r == utf8.RuneError {
return i
}
}
return -1
case !utf8.ValidRune(r):
return -1
default:
var n int
var c0, c1, c2, c3 byte
// Inlined version of utf8.EncodeRune. This is one of our hottest
// functions and EncodeRune or string(r) add significant overhead
// when the string being searched is small.
const (
tx = 0b10000000
t2 = 0b11000000
t3 = 0b11100000
t4 = 0b11110000
maskx = 0b00111111
rune2Max = 1<<11 - 1
rune3Max = 1<<16 - 1
)
switch i := uint32(r); {
case i <= rune2Max:
c0 = t2 | byte(r>>6)
c1 = tx | byte(r)&maskx
n = 2
// NB: removed the invalid rune check since that is
// performed above.
case i <= rune3Max:
c0 = t3 | byte(r>>12)
c1 = tx | byte(r>>6)&maskx
c2 = tx | byte(r)&maskx
n = 3
default:
c0 = t4 | byte(r>>18)
c1 = tx | byte(r>>12)&maskx
c2 = tx | byte(r>>6)&maskx
c3 = tx | byte(r)&maskx
n = 4
}
// Search for r using the last byte of its UTF-8 encoded form
// since it is more unique than the first byte. This 4-5x faster
// when all the text is Unicode.
var fails, i int
switch n {
case 2:
i = 1
for i < len(s) {
if s[i] != c1 {
o := strings.IndexByte(s[i+1:], c1)
if o < 0 {
return -1
}
i += o + 1
}
if s[i-1] == c0 {
return i - 1
}
fails++
i++
// Switch to bytealg.IndexString, if implemented, when the needle
// is small and IndexByte produces too many false positives.
if (bytealg.NativeIndex && fails > bytealg.Cutover(i) && i < len(s)) ||
(!bytealg.NativeIndex && fails >= 4+i>>4 && i < len(s)) {