* doc/invoke.texi (RS/6000 and PowerPC Options): Document -mhtm and
[official-gcc.git] / libgo / go / strings / strings_generic.go
blob811cb803168be21c78c78ed81c5e310aa9466ec4
1 // Copyright 2015 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 !amd64
7 package strings
9 // TODO: implements short string optimization on non amd64 platforms
10 // and get rid of strings_amd64.go
12 // Index returns the index of the first instance of sep in s, or -1 if sep is not present in s.
13 func Index(s, sep string) int {
14 n := len(sep)
15 switch {
16 case n == 0:
17 return 0
18 case n == 1:
19 return IndexByte(s, sep[0])
20 case n == len(s):
21 if sep == s {
22 return 0
24 return -1
25 case n > len(s):
26 return -1
28 // Rabin-Karp search
29 hashsep, pow := hashStr(sep)
30 var h uint32
31 for i := 0; i < n; i++ {
32 h = h*primeRK + uint32(s[i])
34 if h == hashsep && s[:n] == sep {
35 return 0
37 for i := n; i < len(s); {
38 h *= primeRK
39 h += uint32(s[i])
40 h -= pow * uint32(s[i-n])
41 i++
42 if h == hashsep && s[i-n:i] == sep {
43 return i - n
46 return -1