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