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 func Fields(s []byte) [][]byte {
456
457
458 n := 0
459 wasSpace := 1
460
461 setBits := uint8(0)
462 for i := 0; i < len(s); i++ {
463 r := s[i]
464 setBits |= r
465 isSpace := int(asciiSpace[r])
466 n += wasSpace & ^isSpace
467 wasSpace = isSpace
468 }
469
470 if setBits >= utf8.RuneSelf {
471
472 return FieldsFunc(s, unicode.IsSpace)
473 }
474
475
476 a := make([][]byte, n)
477 na := 0
478 fieldStart := 0
479 i := 0
480
481 for i < len(s) && asciiSpace[s[i]] != 0 {
482 i++
483 }
484 fieldStart = i
485 for i < len(s) {
486 if asciiSpace[s[i]] == 0 {
487 i++
488 continue
489 }
490 a[na] = s[fieldStart:i:i]
491 na++
492 i++
493
494 for i < len(s) && asciiSpace[s[i]] != 0 {
495 i++
496 }
497 fieldStart = i
498 }
499 if fieldStart < len(s) {
500 a[na] = s[fieldStart:len(s):len(s)]
501 }
502 return a
503 }
504
505
506
507
508
509
510
511
512 func FieldsFunc(s []byte, f func(rune) bool) [][]byte {
513
514
515 type span struct {
516 start int
517 end int
518 }
519 spans := make([]span, 0, 32)
520
521
522
523
524
525 start := -1
526 for i := 0; i < len(s); {
527 size := 1
528 r := rune(s[i])
529 if r >= utf8.RuneSelf {
530 r, size = utf8.DecodeRune(s[i:])
531 }
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 wid := 1
614 r := rune(s[i])
615 if r >= utf8.RuneSelf {
616 r, wid = utf8.DecodeRune(s[i:])
617 }
618 r = mapping(r)
619 if r >= 0 {
620 b = utf8.AppendRune(b, r)
621 }
622 i += wid
623 }
624 return b
625 }
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643 func Repeat(b []byte, count int) []byte {
644 if count == 0 {
645 return []byte{}
646 }
647
648
649
650
651 if count < 0 {
652 panic("bytes: negative Repeat count")
653 }
654 hi, lo := bits.Mul(uint(len(b)), uint(count))
655 if hi > 0 || lo > uint(maxInt) {
656 panic("bytes: Repeat output length overflow")
657 }
658 n := int(lo)
659
660 if len(b) == 0 {
661 return []byte{}
662 }
663
664
665
666
667
668
669
670
671
672
673
674 const chunkLimit = 8 * 1024
675 chunkMax := n
676 if chunkMax > chunkLimit {
677 chunkMax = chunkLimit / len(b) * len(b)
678 if chunkMax == 0 {
679 chunkMax = len(b)
680 }
681 }
682 nb := bytealg.MakeNoZero(n)[:n:n]
683 bp := copy(nb, b)
684 for bp < n {
685 chunk := min(bp, chunkMax)
686 bp += copy(nb[bp:], nb[:chunk])
687 }
688 return nb
689 }
690
691
692
693 func ToUpper(s []byte) []byte {
694 isASCII, hasLower := true, false
695 for i := 0; i < len(s); i++ {
696 c := s[i]
697 if c >= utf8.RuneSelf {
698 isASCII = false
699 break
700 }
701 hasLower = hasLower || ('a' <= c && c <= 'z')
702 }
703
704 if isASCII {
705 if !hasLower {
706
707 return append([]byte(""), s...)
708 }
709 b := bytealg.MakeNoZero(len(s))[:len(s):len(s)]
710 for i := 0; i < len(s); i++ {
711 c := s[i]
712 if 'a' <= c && c <= 'z' {
713 c -= 'a' - 'A'
714 }
715 b[i] = c
716 }
717 return b
718 }
719 return Map(unicode.ToUpper, s)
720 }
721
722
723
724 func ToLower(s []byte) []byte {
725 isASCII, hasUpper := true, false
726 for i := 0; i < len(s); i++ {
727 c := s[i]
728 if c >= utf8.RuneSelf {
729 isASCII = false
730 break
731 }
732 hasUpper = hasUpper || ('A' <= c && c <= 'Z')
733 }
734
735 if isASCII {
736 if !hasUpper {
737 return append([]byte(""), s...)
738 }
739 b := bytealg.MakeNoZero(len(s))[:len(s):len(s)]
740 for i := 0; i < len(s); i++ {
741 c := s[i]
742 if 'A' <= c && c <= 'Z' {
743 c += 'a' - 'A'
744 }
745 b[i] = c
746 }
747 return b
748 }
749 return Map(unicode.ToLower, s)
750 }
751
752
753 func ToTitle(s []byte) []byte { return Map(unicode.ToTitle, s) }
754
755
756
757 func ToUpperSpecial(c unicode.SpecialCase, s []byte) []byte {
758 return Map(c.ToUpper, s)
759 }
760
761
762
763 func ToLowerSpecial(c unicode.SpecialCase, s []byte) []byte {
764 return Map(c.ToLower, s)
765 }
766
767
768
769 func ToTitleSpecial(c unicode.SpecialCase, s []byte) []byte {
770 return Map(c.ToTitle, s)
771 }
772
773
774
775 func ToValidUTF8(s, replacement []byte) []byte {
776 b := make([]byte, 0, len(s)+len(replacement))
777 invalid := false
778 for i := 0; i < len(s); {
779 c := s[i]
780 if c < utf8.RuneSelf {
781 i++
782 invalid = false
783 b = append(b, c)
784 continue
785 }
786 _, wid := utf8.DecodeRune(s[i:])
787 if wid == 1 {
788 i++
789 if !invalid {
790 invalid = true
791 b = append(b, replacement...)
792 }
793 continue
794 }
795 invalid = false
796 b = append(b, s[i:i+wid]...)
797 i += wid
798 }
799 return b
800 }
801
802
803
804 func isSeparator(r rune) bool {
805
806 if r <= 0x7F {
807 switch {
808 case '0' <= r && r <= '9':
809 return false
810 case 'a' <= r && r <= 'z':
811 return false
812 case 'A' <= r && r <= 'Z':
813 return false
814 case r == '_':
815 return false
816 }
817 return true
818 }
819
820 if unicode.IsLetter(r) || unicode.IsDigit(r) {
821 return false
822 }
823
824 return unicode.IsSpace(r)
825 }
826
827
828
829
830
831
832 func Title(s []byte) []byte {
833
834
835
836 prev := ' '
837 return Map(
838 func(r rune) rune {
839 if isSeparator(prev) {
840 prev = r
841 return unicode.ToTitle(r)
842 }
843 prev = r
844 return r
845 },
846 s)
847 }
848
849
850
851 func TrimLeftFunc(s []byte, f func(r rune) bool) []byte {
852 i := indexFunc(s, f, false)
853 if i == -1 {
854 return nil
855 }
856 return s[i:]
857 }
858
859
860
861 func TrimRightFunc(s []byte, f func(r rune) bool) []byte {
862 i := lastIndexFunc(s, f, false)
863 if i >= 0 && s[i] >= utf8.RuneSelf {
864 _, wid := utf8.DecodeRune(s[i:])
865 i += wid
866 } else {
867 i++
868 }
869 return s[0:i]
870 }
871
872
873
874 func TrimFunc(s []byte, f func(r rune) bool) []byte {
875 return TrimRightFunc(TrimLeftFunc(s, f), f)
876 }
877
878
879
880 func TrimPrefix(s, prefix []byte) []byte {
881 if HasPrefix(s, prefix) {
882 return s[len(prefix):]
883 }
884 return s
885 }
886
887
888
889 func TrimSuffix(s, suffix []byte) []byte {
890 if HasSuffix(s, suffix) {
891 return s[:len(s)-len(suffix)]
892 }
893 return s
894 }
895
896
897
898
899 func IndexFunc(s []byte, f func(r rune) bool) int {
900 return indexFunc(s, f, true)
901 }
902
903
904
905
906 func LastIndexFunc(s []byte, f func(r rune) bool) int {
907 return lastIndexFunc(s, f, true)
908 }
909
910
911
912
913 func indexFunc(s []byte, f func(r rune) bool, truth bool) int {
914 start := 0
915 for start < len(s) {
916 wid := 1
917 r := rune(s[start])
918 if r >= utf8.RuneSelf {
919 r, wid = utf8.DecodeRune(s[start:])
920 }
921 if f(r) == truth {
922 return start
923 }
924 start += wid
925 }
926 return -1
927 }
928
929
930
931
932 func lastIndexFunc(s []byte, f func(r rune) bool, truth bool) int {
933 for i := len(s); i > 0; {
934 r, size := rune(s[i-1]), 1
935 if r >= utf8.RuneSelf {
936 r, size = utf8.DecodeLastRune(s[0:i])
937 }
938 i -= size
939 if f(r) == truth {
940 return i
941 }
942 }
943 return -1
944 }
945
946
947
948
949
950
951
952
953
954 type asciiSet [8]uint32
955
956
957
958 func makeASCIISet(chars string) (as asciiSet, ok bool) {
959 for i := 0; i < len(chars); i++ {
960 c := chars[i]
961 if c >= utf8.RuneSelf {
962 return as, false
963 }
964 as[c/32] |= 1 << (c % 32)
965 }
966 return as, true
967 }
968
969
970 func (as *asciiSet) contains(c byte) bool {
971 return (as[c/32] & (1 << (c % 32))) != 0
972 }
973
974
975
976
977 func containsRune(s string, r rune) bool {
978 for _, c := range s {
979 if c == r {
980 return true
981 }
982 }
983 return false
984 }
985
986
987
988 func Trim(s []byte, cutset string) []byte {
989 if len(s) == 0 {
990
991 return nil
992 }
993 if cutset == "" {
994 return s
995 }
996 if len(cutset) == 1 && cutset[0] < utf8.RuneSelf {
997 return trimLeftByte(trimRightByte(s, cutset[0]), cutset[0])
998 }
999 if as, ok := makeASCIISet(cutset); ok {
1000 return trimLeftASCII(trimRightASCII(s, &as), &as)
1001 }
1002 return trimLeftUnicode(trimRightUnicode(s, cutset), cutset)
1003 }
1004
1005
1006
1007 func TrimLeft(s []byte, cutset string) []byte {
1008 if len(s) == 0 {
1009
1010 return nil
1011 }
1012 if cutset == "" {
1013 return s
1014 }
1015 if len(cutset) == 1 && cutset[0] < utf8.RuneSelf {
1016 return trimLeftByte(s, cutset[0])
1017 }
1018 if as, ok := makeASCIISet(cutset); ok {
1019 return trimLeftASCII(s, &as)
1020 }
1021 return trimLeftUnicode(s, cutset)
1022 }
1023
1024 func trimLeftByte(s []byte, c byte) []byte {
1025 for len(s) > 0 && s[0] == c {
1026 s = s[1:]
1027 }
1028 if len(s) == 0 {
1029
1030 return nil
1031 }
1032 return s
1033 }
1034
1035 func trimLeftASCII(s []byte, as *asciiSet) []byte {
1036 for len(s) > 0 {
1037 if !as.contains(s[0]) {
1038 break
1039 }
1040 s = s[1:]
1041 }
1042 if len(s) == 0 {
1043
1044 return nil
1045 }
1046 return s
1047 }
1048
1049 func trimLeftUnicode(s []byte, cutset string) []byte {
1050 for len(s) > 0 {
1051 r, n := rune(s[0]), 1
1052 if r >= utf8.RuneSelf {
1053 r, n = utf8.DecodeRune(s)
1054 }
1055 if !containsRune(cutset, r) {
1056 break
1057 }
1058 s = s[n:]
1059 }
1060 if len(s) == 0 {
1061
1062 return nil
1063 }
1064 return s
1065 }
1066
1067
1068
1069 func TrimRight(s []byte, cutset string) []byte {
1070 if len(s) == 0 || cutset == "" {
1071 return s
1072 }
1073 if len(cutset) == 1 && cutset[0] < utf8.RuneSelf {
1074 return trimRightByte(s, cutset[0])
1075 }
1076 if as, ok := makeASCIISet(cutset); ok {
1077 return trimRightASCII(s, &as)
1078 }
1079 return trimRightUnicode(s, cutset)
1080 }
1081
1082 func trimRightByte(s []byte, c byte) []byte {
1083 for len(s) > 0 && s[len(s)-1] == c {
1084 s = s[:len(s)-1]
1085 }
1086 return s
1087 }
1088
1089 func trimRightASCII(s []byte, as *asciiSet) []byte {
1090 for len(s) > 0 {
1091 if !as.contains(s[len(s)-1]) {
1092 break
1093 }
1094 s = s[:len(s)-1]
1095 }
1096 return s
1097 }
1098
1099 func trimRightUnicode(s []byte, cutset string) []byte {
1100 for len(s) > 0 {
1101 r, n := rune(s[len(s)-1]), 1
1102 if r >= utf8.RuneSelf {
1103 r, n = utf8.DecodeLastRune(s)
1104 }
1105 if !containsRune(cutset, r) {
1106 break
1107 }
1108 s = s[:len(s)-n]
1109 }
1110 return s
1111 }
1112
1113
1114
1115 func TrimSpace(s []byte) []byte {
1116
1117 start := 0
1118 for ; start < len(s); start++ {
1119 c := s[start]
1120 if c >= utf8.RuneSelf {
1121
1122
1123 return TrimFunc(s[start:], unicode.IsSpace)
1124 }
1125 if asciiSpace[c] == 0 {
1126 break
1127 }
1128 }
1129
1130
1131 stop := len(s)
1132 for ; stop > start; stop-- {
1133 c := s[stop-1]
1134 if c >= utf8.RuneSelf {
1135 return TrimFunc(s[start:stop], unicode.IsSpace)
1136 }
1137 if asciiSpace[c] == 0 {
1138 break
1139 }
1140 }
1141
1142
1143
1144
1145 if start == stop {
1146
1147
1148 return nil
1149 }
1150 return s[start:stop]
1151 }
1152
1153
1154
1155 func Runes(s []byte) []rune {
1156 t := make([]rune, utf8.RuneCount(s))
1157 i := 0
1158 for len(s) > 0 {
1159 r, l := utf8.DecodeRune(s)
1160 t[i] = r
1161 i++
1162 s = s[l:]
1163 }
1164 return t
1165 }
1166
1167
1168
1169
1170
1171
1172
1173 func Replace(s, old, new []byte, n int) []byte {
1174 m := 0
1175 if n != 0 {
1176
1177 m = Count(s, old)
1178 }
1179 if m == 0 {
1180
1181 return append([]byte(nil), s...)
1182 }
1183 if n < 0 || m < n {
1184 n = m
1185 }
1186
1187
1188 t := make([]byte, len(s)+n*(len(new)-len(old)))
1189 w := 0
1190 start := 0
1191 for i := 0; i < n; i++ {
1192 j := start
1193 if len(old) == 0 {
1194 if i > 0 {
1195 _, wid := utf8.DecodeRune(s[start:])
1196 j += wid
1197 }
1198 } else {
1199 j += Index(s[start:], old)
1200 }
1201 w += copy(t[w:], s[start:j])
1202 w += copy(t[w:], new)
1203 start = j + len(old)
1204 }
1205 w += copy(t[w:], s[start:])
1206 return t[0:w]
1207 }
1208
1209
1210
1211
1212
1213
1214 func ReplaceAll(s, old, new []byte) []byte {
1215 return Replace(s, old, new, -1)
1216 }
1217
1218
1219
1220
1221 func EqualFold(s, t []byte) bool {
1222
1223 i := 0
1224 for ; i < len(s) && i < len(t); i++ {
1225 sr := s[i]
1226 tr := t[i]
1227 if sr|tr >= utf8.RuneSelf {
1228 goto hasUnicode
1229 }
1230
1231
1232 if tr == sr {
1233 continue
1234 }
1235
1236
1237 if tr < sr {
1238 tr, sr = sr, tr
1239 }
1240
1241 if 'A' <= sr && sr <= 'Z' && tr == sr+'a'-'A' {
1242 continue
1243 }
1244 return false
1245 }
1246
1247 return len(s) == len(t)
1248
1249 hasUnicode:
1250 s = s[i:]
1251 t = t[i:]
1252 for len(s) != 0 && len(t) != 0 {
1253
1254 var sr, tr rune
1255 if s[0] < utf8.RuneSelf {
1256 sr, s = rune(s[0]), s[1:]
1257 } else {
1258 r, size := utf8.DecodeRune(s)
1259 sr, s = r, s[size:]
1260 }
1261 if t[0] < utf8.RuneSelf {
1262 tr, t = rune(t[0]), t[1:]
1263 } else {
1264 r, size := utf8.DecodeRune(t)
1265 tr, t = r, t[size:]
1266 }
1267
1268
1269
1270
1271 if tr == sr {
1272 continue
1273 }
1274
1275
1276 if tr < sr {
1277 tr, sr = sr, tr
1278 }
1279
1280 if tr < utf8.RuneSelf {
1281
1282 if 'A' <= sr && sr <= 'Z' && tr == sr+'a'-'A' {
1283 continue
1284 }
1285 return false
1286 }
1287
1288
1289
1290 r := unicode.SimpleFold(sr)
1291 for r != sr && r < tr {
1292 r = unicode.SimpleFold(r)
1293 }
1294 if r == tr {
1295 continue
1296 }
1297 return false
1298 }
1299
1300
1301 return len(s) == len(t)
1302 }
1303
1304
1305 func Index(s, sep []byte) int {
1306 n := len(sep)
1307 switch {
1308 case n == 0:
1309 return 0
1310 case n == 1:
1311 return IndexByte(s, sep[0])
1312 case n == len(s):
1313 if Equal(sep, s) {
1314 return 0
1315 }
1316 return -1
1317 case n > len(s):
1318 return -1
1319 case n <= bytealg.MaxLen:
1320
1321 if len(s) <= bytealg.MaxBruteForce {
1322 return bytealg.Index(s, sep)
1323 }
1324 c0 := sep[0]
1325 c1 := sep[1]
1326 i := 0
1327 t := len(s) - n + 1
1328 fails := 0
1329 for i < t {
1330 if s[i] != c0 {
1331
1332
1333 o := IndexByte(s[i+1:t], c0)
1334 if o < 0 {
1335 return -1
1336 }
1337 i += o + 1
1338 }
1339 if s[i+1] == c1 && Equal(s[i:i+n], sep) {
1340 return i
1341 }
1342 fails++
1343 i++
1344
1345 if fails > bytealg.Cutover(i) {
1346 r := bytealg.Index(s[i:], sep)
1347 if r >= 0 {
1348 return r + i
1349 }
1350 return -1
1351 }
1352 }
1353 return -1
1354 }
1355 c0 := sep[0]
1356 c1 := sep[1]
1357 i := 0
1358 fails := 0
1359 t := len(s) - n + 1
1360 for i < t {
1361 if s[i] != c0 {
1362 o := IndexByte(s[i+1:t], c0)
1363 if o < 0 {
1364 break
1365 }
1366 i += o + 1
1367 }
1368 if s[i+1] == c1 && Equal(s[i:i+n], sep) {
1369 return i
1370 }
1371 i++
1372 fails++
1373 if fails >= 4+i>>4 && i < t {
1374
1375
1376
1377
1378
1379
1380
1381
1382 j := bytealg.IndexRabinKarp(s[i:], sep)
1383 if j < 0 {
1384 return -1
1385 }
1386 return i + j
1387 }
1388 }
1389 return -1
1390 }
1391
1392
1393
1394
1395
1396
1397
1398 func Cut(s, sep []byte) (before, after []byte, found bool) {
1399 if i := Index(s, sep); i >= 0 {
1400 return s[:i], s[i+len(sep):], true
1401 }
1402 return s, nil, false
1403 }
1404
1405
1406
1407
1408 func Clone(b []byte) []byte {
1409 if b == nil {
1410 return nil
1411 }
1412 return append([]byte{}, b...)
1413 }
1414
1415
1416
1417
1418
1419
1420
1421 func CutPrefix(s, prefix []byte) (after []byte, found bool) {
1422 if !HasPrefix(s, prefix) {
1423 return s, false
1424 }
1425 return s[len(prefix):], true
1426 }
1427
1428
1429
1430
1431
1432
1433
1434 func CutSuffix(s, suffix []byte) (before []byte, found bool) {
1435 if !HasSuffix(s, suffix) {
1436 return s, false
1437 }
1438 return s[:len(s)-len(suffix)], true
1439 }
1440
View as plain text