1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 // indexShortStr returns the index of the first instance of c in s, or -1 if c is not present in s.
14 // indexShortStr requires 2 <= len(c) <= shortStringLen
15 func indexShortStr(s
, c
[]byte) int // ../runtime/asm_amd64.s
16 func countByte(s
[]byte, c
byte) int // ../runtime/asm_amd64.s
18 var shortStringLen
int
28 // Index returns the index of the first instance of sep in s, or -1 if sep is not present in s.
29 func Index(s
, sep
[]byte) int {
35 return IndexByte(s
, sep
[0])
43 case n
<= shortStringLen
:
44 // Use brute force when s and sep both are small
46 return indexShortStr(s
, sep
)
54 // IndexByte skips 16/32 bytes per iteration,
55 // so it's faster than indexShortStr.
56 o
:= IndexByte(t
[i
:], c
)
62 if Equal(s
[i
:i
+n
], sep
) {
67 // Switch to indexShortStr when IndexByte produces too many false positives.
68 // Too many means more that 1 error per 8 characters.
69 // Allow some errors in the beginning.
71 r
:= indexShortStr(s
[i
:], sep
)
80 return indexRabinKarp(s
, sep
)
83 // Count counts the number of non-overlapping instances of sep in s.
84 // If sep is an empty slice, Count returns 1 + the number of UTF-8-encoded code points in s.
85 func Count(s
, sep
[]byte) int {
86 if len(sep
) == 1 && cpu
.X86
.HasPOPCNT
{
87 return countByte(s
, sep
[0])
89 return countGeneric(s
, sep
)