From 75da1e42405a00e6db3c620923bccc163d675902 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Sat, 29 Jun 2019 16:29:29 +0100 Subject: [PATCH] Speed up and simplify string.replace --- vlib/builtin/string.v | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 904fbc5037..0086930933 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -77,28 +77,19 @@ pub fn (s string) replace(rep, with string) string { if s.len == 0 || rep.len == 0 { return s } - if !s.contains(rep) { - return s - } // println('"$s" replace "$rep" with "$with" rep.len=$rep.len') // TODO PERF Allocating ints is expensive. Should be a stack array // Get locations of all reps within this string mut idxs := []int{} - // idxs := []int { - // 2, 8, 14 - // } - for i := 0; i < s.len; i++ { - // Do we have the string we are looking for (rep) starting at i? - // Go thru all chars in rep and compare. - mut rep_i := 0 - mut j := i - for rep_i < rep.len && j < s.len && s[j] == rep[rep_i] { - rep_i++ - j++ - } - if rep_i == rep.len { - idxs << i - } + mut rem := s + mut rstart := 0 + for { + mut i := rem.index(rep) + if i < 0 {break} + idxs << rstart + i + i += rep.len + rstart += i + rem = rem.substr(i, rem.len) } // Dont change the string if there's nothing to replace if idxs.len == 0 {