2018-23-01 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libgo / go / bytes / bytes_arm64.go
blob1213b067a9dce9dd77484f1e4645f1ec42d0519f
1 // Copyright 2017 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.
5 // +build ignore
7 package bytes
9 func countByte(s []byte, c byte) int // bytes_arm64.s
11 // Index returns the index of the first instance of sep in s, or -1 if sep is not present in s.
12 func Index(s, sep []byte) int {
13 n := len(sep)
14 switch {
15 case n == 0:
16 return 0
17 case n == 1:
18 return IndexByte(s, sep[0])
19 case n == len(s):
20 if Equal(sep, s) {
21 return 0
23 return -1
24 case n > len(s):
25 return -1
27 c := sep[0]
28 i := 0
29 fails := 0
30 t := s[:len(s)-n+1]
31 for i < len(t) {
32 if t[i] != c {
33 o := IndexByte(t[i:], c)
34 if o < 0 {
35 break
37 i += o
39 if Equal(s[i:i+n], sep) {
40 return i
42 i++
43 fails++
44 if fails >= 4+i>>4 && i < len(t) {
45 // Give up on IndexByte, it isn't skipping ahead
46 // far enough to be better than Rabin-Karp.
47 // Experiments (using IndexPeriodic) suggest
48 // the cutover is about 16 byte skips.
49 // TODO: if large prefixes of sep are matching
50 // we should cutover at even larger average skips,
51 // because Equal becomes that much more expensive.
52 // This code does not take that effect into account.
53 j := indexRabinKarp(s[i:], sep)
54 if j < 0 {
55 return -1
57 return i + j
60 return -1
63 // Count counts the number of non-overlapping instances of sep in s.
64 // If sep is an empty slice, Count returns 1 + the number of UTF-8-encoded code points in s.
65 func Count(s, sep []byte) int {
66 if len(sep) == 1 {
67 return countByte(s, sep[0])
69 return countGeneric(s, sep)