builtin: a small optimization in string.replace()

pull/10172/head
Alexander Medvednikov 2021-05-22 14:35:33 +03:00
parent 565dbc4051
commit 7c0f8f7644
1 changed files with 3 additions and 0 deletions

View File

@ -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 { if s.len == 0 || rep.len == 0 || rep.len > s.len {
return s.clone() return s.clone()
} }
if !s.contains(rep) {
return s.clone()
}
// TODO PERF Allocating ints is expensive. Should be a stack array // TODO PERF Allocating ints is expensive. Should be a stack array
// Get locations of all reps within this string // Get locations of all reps within this string
mut idxs := []int{cap: s.len / rep.len} mut idxs := []int{cap: s.len / rep.len}