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 shouldUseASCIISet(len(s)) {
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 shouldUseASCIISet(len(s)) {
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
947
948
949
950
951
952
953 type asciiSet [256]bool
954
955
956
957 func makeASCIISet(chars string) (as asciiSet, ok bool) {
958 for i := 0; i < len(chars); i++ {
959 c := chars[i]
960 if c >= utf8.RuneSelf {
961 return as, false
962 }
963 as[c] = true
964 }
965 return as, true
966 }
967
968
969 func (as *asciiSet) contains(c byte) bool {
970 return as[c]
971 }
972
973
974
975
976
977
978
979 func shouldUseASCIISet(bufLen int) bool {
980 return bufLen > 8
981 }
982
983
984
985
986 func containsRune(s string, r rune) bool {
987 for _, c := range s {
988 if c == r {
989 return true
990 }
991 }
992 return false
993 }
994
995
996
997 func Trim(s []byte, cutset string) []byte {
998 if len(s) == 0 {
999
1000 return nil
1001 }
1002 if cutset == "" {
1003 return s
1004 }
1005 if len(cutset) == 1 && cutset[0] < utf8.RuneSelf {
1006 return trimLeftByte(trimRightByte(s, cutset[0]), cutset[0])
1007 }
1008 if as, ok := makeASCIISet(cutset); ok {
1009 return trimLeftASCII(trimRightASCII(s, &as), &as)
1010 }
1011 return trimLeftUnicode(trimRightUnicode(s, cutset), cutset)
1012 }
1013
1014
1015
1016 func TrimLeft(s []byte, cutset string) []byte {
1017 if len(s) == 0 {
1018
1019 return nil
1020 }
1021 if cutset == "" {
1022 return s
1023 }
1024 if len(cutset) == 1 && cutset[0] < utf8.RuneSelf {
1025 return trimLeftByte(s, cutset[0])
1026 }
1027 if as, ok := makeASCIISet(cutset); ok {
1028 return trimLeftASCII(s, &as)
1029 }
1030 return trimLeftUnicode(s, cutset)
1031 }
1032
1033 func trimLeftByte(s []byte, c byte) []byte {
1034 for len(s) > 0 && s[0] == c {
1035 s = s[1:]
1036 }
1037 if len(s) == 0 {
1038
1039 return nil
1040 }
1041 return s
1042 }
1043
1044 func trimLeftASCII(s []byte, as *asciiSet) []byte {
1045 for len(s) > 0 {
1046 if !as.contains(s[0]) {
1047 break
1048 }
1049 s = s[1:]
1050 }
1051 if len(s) == 0 {
1052
1053 return nil
1054 }
1055 return s
1056 }
1057
1058 func trimLeftUnicode(s []byte, cutset string) []byte {
1059 for len(s) > 0 {
1060 r, n := utf8.DecodeRune(s)
1061 if !containsRune(cutset, r) {
1062 break
1063 }
1064 s = s[n:]
1065 }
1066 if len(s) == 0 {
1067
1068 return nil
1069 }
1070 return s
1071 }
1072
1073
1074
1075 func TrimRight(s []byte, cutset string) []byte {
1076 if len(s) == 0 || cutset == "" {
1077 return s
1078 }
1079 if len(cutset) == 1 && cutset[0] < utf8.RuneSelf {
1080 return trimRightByte(s, cutset[0])
1081 }
1082 if as, ok := makeASCIISet(cutset); ok {
1083 return trimRightASCII(s, &as)
1084 }
1085 return trimRightUnicode(s, cutset)
1086 }
1087
1088 func trimRightByte(s []byte, c byte) []byte {
1089 for len(s) > 0 && s[len(s)-1] == c {
1090 s = s[:len(s)-1]
1091 }
1092 return s
1093 }
1094
1095 func trimRightASCII(s []byte, as *asciiSet) []byte {
1096 for len(s) > 0 {
1097 if !as.contains(s[len(s)-1]) {
1098 break
1099 }
1100 s = s[:len(s)-1]
1101 }
1102 return s
1103 }
1104
1105 func trimRightUnicode(s []byte, cutset string) []byte {
1106 for len(s) > 0 {
1107 r, n := rune(s[len(s)-1]), 1
1108 if r >= utf8.RuneSelf {
1109 r, n = utf8.DecodeLastRune(s)
1110 }
1111 if !containsRune(cutset, r) {
1112 break
1113 }
1114 s = s[:len(s)-n]
1115 }
1116 return s
1117 }
1118
1119
1120
1121 func TrimSpace(s []byte) []byte {
1122
1123 for lo, c := range s {
1124 if c >= utf8.RuneSelf {
1125
1126
1127 return TrimFunc(s[lo:], unicode.IsSpace)
1128 }
1129 if asciiSpace[c] != 0 {
1130 continue
1131 }
1132 s = s[lo:]
1133
1134 for hi := len(s) - 1; hi >= 0; hi-- {
1135 c := s[hi]
1136 if c >= utf8.RuneSelf {
1137 return TrimFunc(s[:hi+1], unicode.IsSpace)
1138 }
1139 if asciiSpace[c] == 0 {
1140
1141
1142
1143 return s[:hi+1]
1144 }
1145 }
1146 }
1147
1148
1149 return nil
1150 }
1151
1152
1153
1154 func Runes(s []byte) []rune {
1155 t := make([]rune, utf8.RuneCount(s))
1156 i := 0
1157 for len(s) > 0 {
1158 r, l := utf8.DecodeRune(s)
1159 t[i] = r
1160 i++
1161 s = s[l:]
1162 }
1163 return t
1164 }
1165
1166
1167
1168
1169
1170
1171
1172 func Replace(s, old, new []byte, n int) []byte {
1173 m := 0
1174 if n != 0 {
1175
1176 m = Count(s, old)
1177 }
1178 if m == 0 {
1179
1180 return append([]byte(nil), s...)
1181 }
1182 if n < 0 || m < n {
1183 n = m
1184 }
1185
1186
1187 t := make([]byte, len(s)+n*(len(new)-len(old)))
1188 w := 0
1189 start := 0
1190 if len(old) > 0 {
1191 for range n {
1192 j := start + Index(s[start:], old)
1193 w += copy(t[w:], s[start:j])
1194 w += copy(t[w:], new)
1195 start = j + len(old)
1196 }
1197 } else {
1198 w += copy(t[w:], new)
1199 for range n - 1 {
1200 _, wid := utf8.DecodeRune(s[start:])
1201 j := start + wid
1202 w += copy(t[w:], s[start:j])
1203 w += copy(t[w:], new)
1204 start = j
1205 }
1206 }
1207 w += copy(t[w:], s[start:])
1208 return t[0:w]
1209 }
1210
1211
1212
1213
1214
1215
1216 func ReplaceAll(s, old, new []byte) []byte {
1217 return Replace(s, old, new, -1)
1218 }
1219
1220
1221
1222
1223 func EqualFold(s, t []byte) bool {
1224
1225 i := 0
1226 for n := min(len(s), len(t)); i < n; i++ {
1227 sr := s[i]
1228 tr := t[i]
1229 if sr|tr >= utf8.RuneSelf {
1230 goto hasUnicode
1231 }
1232
1233
1234 if tr == sr {
1235 continue
1236 }
1237
1238
1239 if tr < sr {
1240 tr, sr = sr, tr
1241 }
1242
1243 if 'A' <= sr && sr <= 'Z' && tr == sr+'a'-'A' {
1244 continue
1245 }
1246 return false
1247 }
1248
1249 return len(s) == len(t)
1250
1251 hasUnicode:
1252 s = s[i:]
1253 t = t[i:]
1254 for len(s) != 0 && len(t) != 0 {
1255
1256 sr, size := utf8.DecodeRune(s)
1257 s = s[size:]
1258 tr, size := utf8.DecodeRune(t)
1259 t = t[size:]
1260
1261
1262
1263
1264 if tr == sr {
1265 continue
1266 }
1267
1268
1269 if tr < sr {
1270 tr, sr = sr, tr
1271 }
1272
1273 if tr < utf8.RuneSelf {
1274
1275 if 'A' <= sr && sr <= 'Z' && tr == sr+'a'-'A' {
1276 continue
1277 }
1278 return false
1279 }
1280
1281
1282
1283 r := unicode.SimpleFold(sr)
1284 for r != sr && r < tr {
1285 r = unicode.SimpleFold(r)
1286 }
1287 if r == tr {
1288 continue
1289 }
1290 return false
1291 }
1292
1293
1294 return len(s) == len(t)
1295 }
1296
1297
1298 func Index(s, sep []byte) int {
1299 n := len(sep)
1300 switch {
1301 case n == 0:
1302 return 0
1303 case n == 1:
1304 return IndexByte(s, sep[0])
1305 case n == len(s):
1306 if Equal(sep, s) {
1307 return 0
1308 }
1309 return -1
1310 case n > len(s):
1311 return -1
1312 case n <= bytealg.MaxLen:
1313
1314 if len(s) <= bytealg.MaxBruteForce {
1315 return bytealg.Index(s, sep)
1316 }
1317 c0 := sep[0]
1318 c1 := sep[1]
1319 i := 0
1320 t := len(s) - n + 1
1321 fails := 0
1322 for i < t {
1323 if s[i] != c0 {
1324
1325
1326 o := IndexByte(s[i+1:t], c0)
1327 if o < 0 {
1328 return -1
1329 }
1330 i += o + 1
1331 }
1332 if s[i+1] == c1 && Equal(s[i:i+n], sep) {
1333 return i
1334 }
1335 fails++
1336 i++
1337
1338 if fails > bytealg.Cutover(i) {
1339 r := bytealg.Index(s[i:], sep)
1340 if r >= 0 {
1341 return r + i
1342 }
1343 return -1
1344 }
1345 }
1346 return -1
1347 }
1348 c0 := sep[0]
1349 c1 := sep[1]
1350 i := 0
1351 fails := 0
1352 t := len(s) - n + 1
1353 for i < t {
1354 if s[i] != c0 {
1355 o := IndexByte(s[i+1:t], c0)
1356 if o < 0 {
1357 break
1358 }
1359 i += o + 1
1360 }
1361 if s[i+1] == c1 && Equal(s[i:i+n], sep) {
1362 return i
1363 }
1364 i++
1365 fails++
1366 if fails >= 4+i>>4 && i < t {
1367
1368
1369
1370
1371
1372
1373
1374
1375 j := bytealg.IndexRabinKarp(s[i:], sep)
1376 if j < 0 {
1377 return -1
1378 }
1379 return i + j
1380 }
1381 }
1382 return -1
1383 }
1384
1385
1386
1387
1388
1389
1390
1391 func Cut(s, sep []byte) (before, after []byte, found bool) {
1392 if i := Index(s, sep); i >= 0 {
1393 return s[:i], s[i+len(sep):], true
1394 }
1395 return s, nil, false
1396 }
1397
1398
1399
1400
1401 func Clone(b []byte) []byte {
1402 if b == nil {
1403 return nil
1404 }
1405 return append([]byte{}, b...)
1406 }
1407
1408
1409
1410
1411
1412
1413
1414 func CutPrefix(s, prefix []byte) (after []byte, found bool) {
1415 if !HasPrefix(s, prefix) {
1416 return s, false
1417 }
1418 return s[len(prefix):], true
1419 }
1420
1421
1422
1423
1424
1425
1426
1427 func CutSuffix(s, suffix []byte) (before []byte, found bool) {
1428 if !HasSuffix(s, suffix) {
1429 return s, false
1430 }
1431 return s[:len(s)-len(suffix)], true
1432 }
1433
View as plain text