From 7c0f8f76449339f28c7cc9540f005b3ef2b38794 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 22 May 2021 14:35:33 +0300 Subject: [PATCH] builtin: a small optimization in string.replace() --- vlib/builtin/string.v | 3 +++ 1 file changed, 3 insertions(+) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index f4fe30c2f8..e2aff1d11a 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -279,6 +279,9 @@ pub fn (s string) replace(rep string, with string) string { if s.len == 0 || rep.len == 0 || rep.len > s.len { return s.clone() } + if !s.contains(rep) { + return s.clone() + } // TODO PERF Allocating ints is expensive. Should be a stack array // Get locations of all reps within this string mut idxs := []int{cap: s.len / rep.len}