From f7d8fb0cf47a8900d6443457c33281575c83a0a3 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 23 Sep 2019 23:34:42 +0300 Subject: [PATCH] string: bring back old index() without an allocation --- vlib/builtin/string.v | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 9f25da34a0..ce137f1665 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -355,20 +355,18 @@ pub fn (s string) substr(start, end int) string { return res } -pub fn (s string) index_old(p string) int { +pub fn (s string) index(p string) int { if p.len > s.len { return -1 } mut i := 0 for i < s.len { mut j := 0 - mut ii := i - for j < p.len && s[ii] == p[j] { + for j < p.len && s[i + j] == p[j] { j++ - ii++ } if j == p.len { - return i - p.len + 1 + return i } i++ } @@ -376,7 +374,7 @@ pub fn (s string) index_old(p string) int { } // KMP search -pub fn (s string) index(p string) int { +pub fn (s string) index_kmp(p string) int { if p.len > s.len { return -1 }