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