1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

Pre-allocate KMP prefix for string.index

This commit is contained in:
Nick Treleaven 2019-06-30 12:24:04 +01:00 committed by Alexander Medvednikov
parent 388eb36ecb
commit b79defd7a9

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++ {