Source file
src/strings/strings.go
Documentation: strings
1
2
3
4
5
6
7
8 package strings
9
10 import (
11 "internal/bytealg"
12 "unicode"
13 "unicode/utf8"
14 )
15
16
17
18
19 func explode(s string, n int) []string {
20 l := utf8.RuneCountInString(s)
21 if n < 0 || n > l {
22 n = l
23 }
24 a := make([]string, n)
25 for i := 0; i < n-1; i++ {
26 ch, size := utf8.DecodeRuneInString(s)
27 a[i] = s[:size]
28 s = s[size:]
29 if ch == utf8.RuneError {
30 a[i] = string(utf8.RuneError)
31 }
32 }
33 if n > 0 {
34 a[n-1] = s
35 }
36 return a
37 }
38
39
40
41 func Count(s, substr string) int {
42
43 if len(substr) == 0 {
44 return utf8.RuneCountInString(s) + 1
45 }
46 if len(substr) == 1 {
47 return bytealg.CountString(s, substr[0])
48 }
49 n := 0
50 for {
51 i := Index(s, substr)
52 if i == -1 {
53 return n
54 }
55 n++
56 s = s[i+len(substr):]
57 }
58 }
59
60
61 func Contains(s, substr string) bool {
62 return Index(s, substr) >= 0
63 }
64
65
66 func ContainsAny(s, chars string) bool {
67 return IndexAny(s, chars) >= 0
68 }
69
70
71 func ContainsRune(s string, r rune) bool {
72 return IndexRune(s, r) >= 0
73 }
74
75
76 func LastIndex(s, substr string) int {
77 n := len(substr)
78 switch {
79 case n == 0:
80 return len(s)
81 case n == 1:
82 return LastIndexByte(s, substr[0])
83 case n == len(s):
84 if substr == s {
85 return 0
86 }
87 return -1
88 case n > len(s):
89 return -1
90 }
91
92 hashss, pow := bytealg.HashStrRev(substr)
93 last := len(s) - n
94 var h uint32
95 for i := len(s) - 1; i >= last; i-- {
96 h = h*bytealg.PrimeRK + uint32(s[i])
97 }
98 if h == hashss && s[last:] == substr {
99 return last
100 }
101 for i := last - 1; i >= 0; i-- {
102 h *= bytealg.PrimeRK
103 h += uint32(s[i])
104 h -= pow * uint32(s[i+n])
105 if h == hashss && s[i:i+n] == substr {
106 return i
107 }
108 }
109 return -1
110 }
111
112
113 func IndexByte(s string, c byte) int {
114 return bytealg.IndexByteString(s, c)
115 }
116
117
118
119
120
121 func IndexRune(s string, r rune) int {
122 switch {
123 case 0 <= r && r < utf8.RuneSelf:
124 return IndexByte(s, byte(r))
125 case r == utf8.RuneError:
126 for i, r := range s {
127 if r == utf8.RuneError {
128 return i
129 }
130 }
131 return -1
132 case !utf8.ValidRune(r):
133 return -1
134 default:
135 return Index(s, string(r))
136 }
137 }
138
139
140
141 func IndexAny(s, chars string) int {
142 if chars == "" {
143
144 return -1
145 }
146 if len(chars) == 1 {
147
148 r := rune(chars[0])
149 if r >= utf8.RuneSelf {
150 r = utf8.RuneError
151 }
152 return IndexRune(s, r)
153 }
154 if len(s) > 8 {
155 if as, isASCII := makeASCIISet(chars); isASCII {
156 for i := 0; i < len(s); i++ {
157 if as.contains(s[i]) {
158 return i
159 }
160 }
161 return -1
162 }
163 }
164 for i, c := range s {
165 if IndexRune(chars, c) >= 0 {
166 return i
167 }
168 }
169 return -1
170 }
171
172
173
174
175 func LastIndexAny(s, chars string) int {
176 if chars == "" {
177
178 return -1
179 }
180 if len(s) == 1 {
181 rc := rune(s[0])
182 if rc >= utf8.RuneSelf {
183 rc = utf8.RuneError
184 }
185 if IndexRune(chars, rc) >= 0 {
186 return 0
187 }
188 return -1
189 }
190 if len(s) > 8 {
191 if as, isASCII := makeASCIISet(chars); isASCII {
192 for i := len(s) - 1; i >= 0; i-- {
193 if as.contains(s[i]) {
194 return i
195 }
196 }
197 return -1
198 }
199 }
200 if len(chars) == 1 {
201 rc := rune(chars[0])
202 if rc >= utf8.RuneSelf {
203 rc = utf8.RuneError
204 }
205 for i := len(s); i > 0; {
206 r, size := utf8.DecodeLastRuneInString(s[:i])
207 i -= size
208 if rc == r {
209 return i
210 }
211 }
212 return -1
213 }
214 for i := len(s); i > 0; {
215 r, size := utf8.DecodeLastRuneInString(s[:i])
216 i -= size
217 if IndexRune(chars, r) >= 0 {
218 return i
219 }
220 }
221 return -1
222 }
223
224
225 func LastIndexByte(s string, c byte) int {
226 for i := len(s) - 1; i >= 0; i-- {
227 if s[i] == c {
228 return i
229 }
230 }
231 return -1
232 }
233
234
235
236 func genSplit(s, sep string, sepSave, n int) []string {
237 if n == 0 {
238 return nil
239 }
240 if sep == "" {
241 return explode(s, n)
242 }
243 if n < 0 {
244 n = Count(s, sep) + 1
245 }
246
247 if n > len(s)+1 {
248 n = len(s) + 1
249 }
250 a := make([]string, n)
251 n--
252 i := 0
253 for i < n {
254 m := Index(s, sep)
255 if m < 0 {
256 break
257 }
258 a[i] = s[:m+sepSave]
259 s = s[m+len(sep):]
260 i++
261 }
262 a[i] = s
263 return a[:i+1]
264 }
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279 func SplitN(s, sep string, n int) []string { return genSplit(s, sep, 0, n) }
280
281
282
283
284
285
286
287
288
289
290
291
292 func SplitAfterN(s, sep string, n int) []string {
293 return genSplit(s, sep, len(sep), n)
294 }
295
296
297
298
299
300
301
302
303
304
305
306
307
308 func Split(s, sep string) []string { return genSplit(s, sep, 0, -1) }
309
310
311
312
313
314
315
316
317
318
319
320 func SplitAfter(s, sep string) []string {
321 return genSplit(s, sep, len(sep), -1)
322 }
323
324 var asciiSpace = [256]uint8{'\t': 1, '\n': 1, '\v': 1, '\f': 1, '\r': 1, ' ': 1}
325
326
327
328
329 func Fields(s string) []string {
330
331
332 n := 0
333 wasSpace := 1
334
335 setBits := uint8(0)
336 for i := 0; i < len(s); i++ {
337 r := s[i]
338 setBits |= r
339 isSpace := int(asciiSpace[r])
340 n += wasSpace & ^isSpace
341 wasSpace = isSpace
342 }
343
344 if setBits >= utf8.RuneSelf {
345
346 return FieldsFunc(s, unicode.IsSpace)
347 }
348
349 a := make([]string, n)
350 na := 0
351 fieldStart := 0
352 i := 0
353
354 for i < len(s) && asciiSpace[s[i]] != 0 {
355 i++
356 }
357 fieldStart = i
358 for i < len(s) {
359 if asciiSpace[s[i]] == 0 {
360 i++
361 continue
362 }
363 a[na] = s[fieldStart:i]
364 na++
365 i++
366
367 for i < len(s) && asciiSpace[s[i]] != 0 {
368 i++
369 }
370 fieldStart = i
371 }
372 if fieldStart < len(s) {
373 a[na] = s[fieldStart:]
374 }
375 return a
376 }
377
378
379
380
381
382
383
384 func FieldsFunc(s string, f func(rune) bool) []string {
385
386
387 type span struct {
388 start int
389 end int
390 }
391 spans := make([]span, 0, 32)
392
393
394
395
396
397 start := -1
398 for end, rune := range s {
399 if f(rune) {
400 if start >= 0 {
401 spans = append(spans, span{start, end})
402
403
404
405 start = ^start
406 }
407 } else {
408 if start < 0 {
409 start = end
410 }
411 }
412 }
413
414
415 if start >= 0 {
416 spans = append(spans, span{start, len(s)})
417 }
418
419
420 a := make([]string, len(spans))
421 for i, span := range spans {
422 a[i] = s[span.start:span.end]
423 }
424
425 return a
426 }
427
428
429
430 func Join(elems []string, sep string) string {
431 switch len(elems) {
432 case 0:
433 return ""
434 case 1:
435 return elems[0]
436 }
437 n := len(sep) * (len(elems) - 1)
438 for i := 0; i < len(elems); i++ {
439 n += len(elems[i])
440 }
441
442 var b Builder
443 b.Grow(n)
444 b.WriteString(elems[0])
445 for _, s := range elems[1:] {
446 b.WriteString(sep)
447 b.WriteString(s)
448 }
449 return b.String()
450 }
451
452
453 func HasPrefix(s, prefix string) bool {
454 return len(s) >= len(prefix) && s[0:len(prefix)] == prefix
455 }
456
457
458 func HasSuffix(s, suffix string) bool {
459 return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix
460 }
461
462
463
464
465 func Map(mapping func(rune) rune, s string) string {
466
467
468
469
470
471
472 var b Builder
473
474 for i, c := range s {
475 r := mapping(c)
476 if r == c && c != utf8.RuneError {
477 continue
478 }
479
480 var width int
481 if c == utf8.RuneError {
482 c, width = utf8.DecodeRuneInString(s[i:])
483 if width != 1 && r == c {
484 continue
485 }
486 } else {
487 width = utf8.RuneLen(c)
488 }
489
490 b.Grow(len(s) + utf8.UTFMax)
491 b.WriteString(s[:i])
492 if r >= 0 {
493 b.WriteRune(r)
494 }
495
496 s = s[i+width:]
497 break
498 }
499
500
501 if b.Cap() == 0 {
502 return s
503 }
504
505 for _, c := range s {
506 r := mapping(c)
507
508 if r >= 0 {
509
510
511
512 if r < utf8.RuneSelf {
513 b.WriteByte(byte(r))
514 } else {
515
516 b.WriteRune(r)
517 }
518 }
519 }
520
521 return b.String()
522 }
523
524
525
526
527
528 func Repeat(s string, count int) string {
529 if count == 0 {
530 return ""
531 }
532
533
534
535
536
537 if count < 0 {
538 panic("strings: negative Repeat count")
539 } else if len(s)*count/count != len(s) {
540 panic("strings: Repeat count causes overflow")
541 }
542
543 n := len(s) * count
544 var b Builder
545 b.Grow(n)
546 b.WriteString(s)
547 for b.Len() < n {
548 if b.Len() <= n/2 {
549 b.WriteString(b.String())
550 } else {
551 b.WriteString(b.String()[:n-b.Len()])
552 break
553 }
554 }
555 return b.String()
556 }
557
558
559 func ToUpper(s string) string {
560 isASCII, hasLower := true, false
561 for i := 0; i < len(s); i++ {
562 c := s[i]
563 if c >= utf8.RuneSelf {
564 isASCII = false
565 break
566 }
567 hasLower = hasLower || ('a' <= c && c <= 'z')
568 }
569
570 if isASCII {
571 if !hasLower {
572 return s
573 }
574 var b Builder
575 b.Grow(len(s))
576 for i := 0; i < len(s); i++ {
577 c := s[i]
578 if 'a' <= c && c <= 'z' {
579 c -= 'a' - 'A'
580 }
581 b.WriteByte(c)
582 }
583 return b.String()
584 }
585 return Map(unicode.ToUpper, s)
586 }
587
588
589 func ToLower(s string) string {
590 isASCII, hasUpper := true, false
591 for i := 0; i < len(s); i++ {
592 c := s[i]
593 if c >= utf8.RuneSelf {
594 isASCII = false
595 break
596 }
597 hasUpper = hasUpper || ('A' <= c && c <= 'Z')
598 }
599
600 if isASCII {
601 if !hasUpper {
602 return s
603 }
604 var b Builder
605 b.Grow(len(s))
606 for i := 0; i < len(s); i++ {
607 c := s[i]
608 if 'A' <= c && c <= 'Z' {
609 c += 'a' - 'A'
610 }
611 b.WriteByte(c)
612 }
613 return b.String()
614 }
615 return Map(unicode.ToLower, s)
616 }
617
618
619
620 func ToTitle(s string) string { return Map(unicode.ToTitle, s) }
621
622
623
624 func ToUpperSpecial(c unicode.SpecialCase, s string) string {
625 return Map(c.ToUpper, s)
626 }
627
628
629
630 func ToLowerSpecial(c unicode.SpecialCase, s string) string {
631 return Map(c.ToLower, s)
632 }
633
634
635
636 func ToTitleSpecial(c unicode.SpecialCase, s string) string {
637 return Map(c.ToTitle, s)
638 }
639
640
641
642 func ToValidUTF8(s, replacement string) string {
643 var b Builder
644
645 for i, c := range s {
646 if c != utf8.RuneError {
647 continue
648 }
649
650 _, wid := utf8.DecodeRuneInString(s[i:])
651 if wid == 1 {
652 b.Grow(len(s) + len(replacement))
653 b.WriteString(s[:i])
654 s = s[i:]
655 break
656 }
657 }
658
659
660 if b.Cap() == 0 {
661 return s
662 }
663
664 invalid := false
665 for i := 0; i < len(s); {
666 c := s[i]
667 if c < utf8.RuneSelf {
668 i++
669 invalid = false
670 b.WriteByte(c)
671 continue
672 }
673 _, wid := utf8.DecodeRuneInString(s[i:])
674 if wid == 1 {
675 i++
676 if !invalid {
677 invalid = true
678 b.WriteString(replacement)
679 }
680 continue
681 }
682 invalid = false
683 b.WriteString(s[i : i+wid])
684 i += wid
685 }
686
687 return b.String()
688 }
689
690
691
692 func isSeparator(r rune) bool {
693
694 if r <= 0x7F {
695 switch {
696 case '0' <= r && r <= '9':
697 return false
698 case 'a' <= r && r <= 'z':
699 return false
700 case 'A' <= r && r <= 'Z':
701 return false
702 case r == '_':
703 return false
704 }
705 return true
706 }
707
708 if unicode.IsLetter(r) || unicode.IsDigit(r) {
709 return false
710 }
711
712 return unicode.IsSpace(r)
713 }
714
715
716
717
718
719
720 func Title(s string) string {
721
722
723
724 prev := ' '
725 return Map(
726 func(r rune) rune {
727 if isSeparator(prev) {
728 prev = r
729 return unicode.ToTitle(r)
730 }
731 prev = r
732 return r
733 },
734 s)
735 }
736
737
738
739 func TrimLeftFunc(s string, f func(rune) bool) string {
740 i := indexFunc(s, f, false)
741 if i == -1 {
742 return ""
743 }
744 return s[i:]
745 }
746
747
748
749 func TrimRightFunc(s string, f func(rune) bool) string {
750 i := lastIndexFunc(s, f, false)
751 if i >= 0 && s[i] >= utf8.RuneSelf {
752 _, wid := utf8.DecodeRuneInString(s[i:])
753 i += wid
754 } else {
755 i++
756 }
757 return s[0:i]
758 }
759
760
761
762 func TrimFunc(s string, f func(rune) bool) string {
763 return TrimRightFunc(TrimLeftFunc(s, f), f)
764 }
765
766
767
768 func IndexFunc(s string, f func(rune) bool) int {
769 return indexFunc(s, f, true)
770 }
771
772
773
774 func LastIndexFunc(s string, f func(rune) bool) int {
775 return lastIndexFunc(s, f, true)
776 }
777
778
779
780
781 func indexFunc(s string, f func(rune) bool, truth bool) int {
782 for i, r := range s {
783 if f(r) == truth {
784 return i
785 }
786 }
787 return -1
788 }
789
790
791
792
793 func lastIndexFunc(s string, f func(rune) bool, truth bool) int {
794 for i := len(s); i > 0; {
795 r, size := utf8.DecodeLastRuneInString(s[0:i])
796 i -= size
797 if f(r) == truth {
798 return i
799 }
800 }
801 return -1
802 }
803
804
805
806
807
808
809
810
811
812 type asciiSet [8]uint32
813
814
815
816 func makeASCIISet(chars string) (as asciiSet, ok bool) {
817 for i := 0; i < len(chars); i++ {
818 c := chars[i]
819 if c >= utf8.RuneSelf {
820 return as, false
821 }
822 as[c/32] |= 1 << (c % 32)
823 }
824 return as, true
825 }
826
827
828 func (as *asciiSet) contains(c byte) bool {
829 return (as[c/32] & (1 << (c % 32))) != 0
830 }
831
832
833
834 func Trim(s, cutset string) string {
835 if s == "" || cutset == "" {
836 return s
837 }
838 if len(cutset) == 1 && cutset[0] < utf8.RuneSelf {
839 return trimLeftByte(trimRightByte(s, cutset[0]), cutset[0])
840 }
841 if as, ok := makeASCIISet(cutset); ok {
842 return trimLeftASCII(trimRightASCII(s, &as), &as)
843 }
844 return trimLeftUnicode(trimRightUnicode(s, cutset), cutset)
845 }
846
847
848
849
850
851 func TrimLeft(s, cutset string) string {
852 if s == "" || cutset == "" {
853 return s
854 }
855 if len(cutset) == 1 && cutset[0] < utf8.RuneSelf {
856 return trimLeftByte(s, cutset[0])
857 }
858 if as, ok := makeASCIISet(cutset); ok {
859 return trimLeftASCII(s, &as)
860 }
861 return trimLeftUnicode(s, cutset)
862 }
863
864 func trimLeftByte(s string, c byte) string {
865 for len(s) > 0 && s[0] == c {
866 s = s[1:]
867 }
868 return s
869 }
870
871 func trimLeftASCII(s string, as *asciiSet) string {
872 for len(s) > 0 {
873 if !as.contains(s[0]) {
874 break
875 }
876 s = s[1:]
877 }
878 return s
879 }
880
881 func trimLeftUnicode(s, cutset string) string {
882 for len(s) > 0 {
883 r, n := rune(s[0]), 1
884 if r >= utf8.RuneSelf {
885 r, n = utf8.DecodeRuneInString(s)
886 }
887 if !ContainsRune(cutset, r) {
888 break
889 }
890 s = s[n:]
891 }
892 return s
893 }
894
895
896
897
898
899 func TrimRight(s, cutset string) string {
900 if s == "" || cutset == "" {
901 return s
902 }
903 if len(cutset) == 1 && cutset[0] < utf8.RuneSelf {
904 return trimRightByte(s, cutset[0])
905 }
906 if as, ok := makeASCIISet(cutset); ok {
907 return trimRightASCII(s, &as)
908 }
909 return trimRightUnicode(s, cutset)
910 }
911
912 func trimRightByte(s string, c byte) string {
913 for len(s) > 0 && s[len(s)-1] == c {
914 s = s[:len(s)-1]
915 }
916 return s
917 }
918
919 func trimRightASCII(s string, as *asciiSet) string {
920 for len(s) > 0 {
921 if !as.contains(s[len(s)-1]) {
922 break
923 }
924 s = s[:len(s)-1]
925 }
926 return s
927 }
928
929 func trimRightUnicode(s, cutset string) string {
930 for len(s) > 0 {
931 r, n := rune(s[len(s)-1]), 1
932 if r >= utf8.RuneSelf {
933 r, n = utf8.DecodeLastRuneInString(s)
934 }
935 if !ContainsRune(cutset, r) {
936 break
937 }
938 s = s[:len(s)-n]
939 }
940 return s
941 }
942
943
944
945 func TrimSpace(s string) string {
946
947 start := 0
948 for ; start < len(s); start++ {
949 c := s[start]
950 if c >= utf8.RuneSelf {
951
952
953 return TrimFunc(s[start:], unicode.IsSpace)
954 }
955 if asciiSpace[c] == 0 {
956 break
957 }
958 }
959
960
961 stop := len(s)
962 for ; stop > start; stop-- {
963 c := s[stop-1]
964 if c >= utf8.RuneSelf {
965
966 return TrimRightFunc(s[start:stop], unicode.IsSpace)
967 }
968 if asciiSpace[c] == 0 {
969 break
970 }
971 }
972
973
974
975
976 return s[start:stop]
977 }
978
979
980
981 func TrimPrefix(s, prefix string) string {
982 if HasPrefix(s, prefix) {
983 return s[len(prefix):]
984 }
985 return s
986 }
987
988
989
990 func TrimSuffix(s, suffix string) string {
991 if HasSuffix(s, suffix) {
992 return s[:len(s)-len(suffix)]
993 }
994 return s
995 }
996
997
998
999
1000
1001
1002
1003 func Replace(s, old, new string, n int) string {
1004 if old == new || n == 0 {
1005 return s
1006 }
1007
1008
1009 if m := Count(s, old); m == 0 {
1010 return s
1011 } else if n < 0 || m < n {
1012 n = m
1013 }
1014
1015
1016 var b Builder
1017 b.Grow(len(s) + n*(len(new)-len(old)))
1018 start := 0
1019 for i := 0; i < n; i++ {
1020 j := start
1021 if len(old) == 0 {
1022 if i > 0 {
1023 _, wid := utf8.DecodeRuneInString(s[start:])
1024 j += wid
1025 }
1026 } else {
1027 j += Index(s[start:], old)
1028 }
1029 b.WriteString(s[start:j])
1030 b.WriteString(new)
1031 start = j + len(old)
1032 }
1033 b.WriteString(s[start:])
1034 return b.String()
1035 }
1036
1037
1038
1039
1040
1041
1042 func ReplaceAll(s, old, new string) string {
1043 return Replace(s, old, new, -1)
1044 }
1045
1046
1047
1048
1049 func EqualFold(s, t string) bool {
1050 for s != "" && t != "" {
1051
1052 var sr, tr rune
1053 if s[0] < utf8.RuneSelf {
1054 sr, s = rune(s[0]), s[1:]
1055 } else {
1056 r, size := utf8.DecodeRuneInString(s)
1057 sr, s = r, s[size:]
1058 }
1059 if t[0] < utf8.RuneSelf {
1060 tr, t = rune(t[0]), t[1:]
1061 } else {
1062 r, size := utf8.DecodeRuneInString(t)
1063 tr, t = r, t[size:]
1064 }
1065
1066
1067
1068
1069 if tr == sr {
1070 continue
1071 }
1072
1073
1074 if tr < sr {
1075 tr, sr = sr, tr
1076 }
1077
1078 if tr < utf8.RuneSelf {
1079
1080 if 'A' <= sr && sr <= 'Z' && tr == sr+'a'-'A' {
1081 continue
1082 }
1083 return false
1084 }
1085
1086
1087
1088 r := unicode.SimpleFold(sr)
1089 for r != sr && r < tr {
1090 r = unicode.SimpleFold(r)
1091 }
1092 if r == tr {
1093 continue
1094 }
1095 return false
1096 }
1097
1098
1099 return s == t
1100 }
1101
1102
1103 func Index(s, substr string) int {
1104 n := len(substr)
1105 switch {
1106 case n == 0:
1107 return 0
1108 case n == 1:
1109 return IndexByte(s, substr[0])
1110 case n == len(s):
1111 if substr == s {
1112 return 0
1113 }
1114 return -1
1115 case n > len(s):
1116 return -1
1117 case n <= bytealg.MaxLen:
1118
1119 if len(s) <= bytealg.MaxBruteForce {
1120 return bytealg.IndexString(s, substr)
1121 }
1122 c0 := substr[0]
1123 c1 := substr[1]
1124 i := 0
1125 t := len(s) - n + 1
1126 fails := 0
1127 for i < t {
1128 if s[i] != c0 {
1129
1130
1131 o := IndexByte(s[i+1:t], c0)
1132 if o < 0 {
1133 return -1
1134 }
1135 i += o + 1
1136 }
1137 if s[i+1] == c1 && s[i:i+n] == substr {
1138 return i
1139 }
1140 fails++
1141 i++
1142
1143 if fails > bytealg.Cutover(i) {
1144 r := bytealg.IndexString(s[i:], substr)
1145 if r >= 0 {
1146 return r + i
1147 }
1148 return -1
1149 }
1150 }
1151 return -1
1152 }
1153 c0 := substr[0]
1154 c1 := substr[1]
1155 i := 0
1156 t := len(s) - n + 1
1157 fails := 0
1158 for i < t {
1159 if s[i] != c0 {
1160 o := IndexByte(s[i+1:t], c0)
1161 if o < 0 {
1162 return -1
1163 }
1164 i += o + 1
1165 }
1166 if s[i+1] == c1 && s[i:i+n] == substr {
1167 return i
1168 }
1169 i++
1170 fails++
1171 if fails >= 4+i>>4 && i < t {
1172
1173 j := bytealg.IndexRabinKarp(s[i:], substr)
1174 if j < 0 {
1175 return -1
1176 }
1177 return i + j
1178 }
1179 }
1180 return -1
1181 }
1182
1183
1184
1185
1186
1187 func Cut(s, sep string) (before, after string, found bool) {
1188 if i := Index(s, sep); i >= 0 {
1189 return s[:i], s[i+len(sep):], true
1190 }
1191 return s, "", false
1192 }
1193
View as plain text