MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / horspool

Function horspool

strings/horspool/horspool.go:16–37  ·  view source on GitHub ↗
(t, p []rune)

Source from the content-addressed store, hash-verified

14}
15
16func horspool(t, p []rune) (int, error) {
17 shiftMap := computeShiftMap(t, p)
18 pos := 0
19 for pos <= len(t)-len(p) {
20 if isMatch(pos, t, p) {
21 return pos, nil
22 }
23 if pos+len(p) >= len(t) {
24 // because the remaining length of the input string
25 // is the same as the length of the pattern
26 // and it does not match the pattern
27 // it is impossible to find the pattern
28 break
29 }
30
31 // because of the check above
32 // t[pos+len(p)] is defined
33 pos += shiftMap[t[pos+len(p)]]
34 }
35
36 return -1, ErrNotFound
37}
38
39// Checks if the array p matches the subarray of t starting at pos.
40// Note that backward iteration.

Callers 1

HorspoolFunction · 0.85

Calls 2

computeShiftMapFunction · 0.85
isMatchFunction · 0.85

Tested by

no test coverage detected