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