From b79defd7a90ef94ffa8840d45b38c87f64577b1c Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Sun, 30 Jun 2019 12:24:04 +0100 Subject: [PATCH] Pre-allocate KMP prefix for string.index --- vlib/builtin/string.v | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 1e8dfe1b0f..9c62625bf6 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -352,7 +352,7 @@ pub fn (s string) index(p string) int { if p.len > s.len { return -1 } - mut prefix := [0] + mut prefix := [0; p.len] mut j := 0 for i := 1; i < p.len; i++ { for p[j] != p[i] && j > 0 { @@ -361,7 +361,7 @@ pub fn (s string) index(p string) int { if p[j] == p[i] { j++ } - prefix << j + prefix[i] = j } j = 0 for i := 0; i < s.len; i++ {