golang关于查找字符串的源码
// Index returns the index of the first instance of substr in s, or -1 if substr is not present in s.func Index(s, substr string) int {n := len(substr)switch {case n == 0:return 0case n == 1:return IndexByte(s, substr[0])case n == len(s):if substr == s {return 0}return -1case n > len(s):return -1case n <= bytealg.MaxLen:// Use brute force when s and substr both are smallif len(s) <= bytealg.MaxBruteForce {return bytealg.IndexString(s, substr)}c0 := substr[0]c1 := substr[1]i := 0t := len(s) - n + 1fails := 0for i < t {if s[i] != c0 {// IndexByte is faster than bytealg.IndexString, so use it as long as// we're not getting lots of false positives.o := IndexByte(s[i:t], c0)if o < 0 {return -1}i += o}if s[i+1] == c1 && s[i:i+n] == substr {return i}fails++i++// Switch to bytealg.IndexString when IndexByte produces too many false positives.if fails > bytealg.Cutover(i) {r := bytealg.IndexString(s[i:], substr)if r >= 0 {return r + i}return -1}}return -1}c0 := substr[0]c1 := substr[1]i := 0t := len(s) - n + 1fails := 0for i < t {if s[i] != c0 {o := IndexByte(s[i:t], c0)if o < 0 {return -1}i += o}if s[i+1] == c1 && s[i:i+n] == substr {return i}i++fails++if fails >= 4+i>>4 && i < t {// See comment in ../bytes/bytes_generic.go.j := indexRabinKarp(s[i:], substr)if j < 0 {return -1}return i + j}}return -1}func indexRabinKarp(s, substr string) int {// Rabin-Karp searchhashss, pow := hashStr(substr)n := len(substr)var h uint32for i := 0; i < n; i++ {h = h*primeRK + uint32(s[i])}if h == hashss && s[:n] == substr {return 0}for i := n; i < len(s); {h *= primeRKh += uint32(s[i])h -= pow * uint32(s[i-n])i++if h == hashss && s[i-n:i] == substr {return i - n}}return -1}
