...
Source file
src/bytes/bytes_amd64.go
Documentation: bytes
1
2
3
4
5 package bytes
6
7 import "internal/cpu"
8
9
10
11
12
13 func indexShortStr(s, c []byte) int
14 func countByte(s []byte, c byte) int
15
16 var shortStringLen int
17
18 func init() {
19 if cpu.X86.HasAVX2 {
20 shortStringLen = 63
21 } else {
22 shortStringLen = 31
23 }
24 }
25
26
27 func Index(s, sep []byte) int {
28 n := len(sep)
29 switch {
30 case n == 0:
31 return 0
32 case n == 1:
33 return IndexByte(s, sep[0])
34 case n == len(s):
35 if Equal(sep, s) {
36 return 0
37 }
38 return -1
39 case n > len(s):
40 return -1
41 case n <= shortStringLen:
42
43 if len(s) <= 64 {
44 return indexShortStr(s, sep)
45 }
46 c := sep[0]
47 i := 0
48 t := s[:len(s)-n+1]
49 fails := 0
50 for i < len(t) {
51 if t[i] != c {
52
53
54 o := IndexByte(t[i:], c)
55 if o < 0 {
56 return -1
57 }
58 i += o
59 }
60 if Equal(s[i:i+n], sep) {
61 return i
62 }
63 fails++
64 i++
65
66
67
68 if fails > (i+16)/8 {
69 r := indexShortStr(s[i:], sep)
70 if r >= 0 {
71 return r + i
72 }
73 return -1
74 }
75 }
76 return -1
77 }
78 return indexRabinKarp(s, sep)
79 }
80
81
82
83 func Count(s, sep []byte) int {
84 if len(sep) == 1 && cpu.X86.HasPOPCNT {
85 return countByte(s, sep[0])
86 }
87 return countGeneric(s, sep)
88 }
89
View as plain text