Pre-allocate KMP prefix for string.index

pull/853/head
Nick Treleaven 2019-06-30 12:24:04 +01:00 committed by Alexander Medvednikov
parent 388eb36ecb
commit b79defd7a9
1 changed files with 2 additions and 2 deletions

View File

@ -352,7 +352,7 @@ pub fn (s string) index(p string) int {
if p.len > s.len { if p.len > s.len {
return -1 return -1
} }
mut prefix := [0] mut prefix := [0; p.len]
mut j := 0 mut j := 0
for i := 1; i < p.len; i++ { for i := 1; i < p.len; i++ {
for p[j] != p[i] && j > 0 { for p[j] != p[i] && j > 0 {
@ -361,7 +361,7 @@ pub fn (s string) index(p string) int {
if p[j] == p[i] { if p[j] == p[i] {
j++ j++
} }
prefix << j prefix[i] = j
} }
j = 0 j = 0
for i := 0; i < s.len; i++ { for i := 0; i < s.len; i++ {