builtin.string: optimize replace() (#9955)

pull/9957/head
JalonSolov 2021-05-01 14:27:49 -04:00 committed by GitHub
parent 000d4d3064
commit 3363c3ef65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 2 deletions

View File

@ -275,12 +275,12 @@ pub fn (s string) replace_once(rep string, with string) string {
// replace replaces all occurences of `rep` with the string passed in `with`.
pub fn (s string) replace(rep string, with string) string {
if s.len == 0 || rep.len == 0 {
if s.len == 0 || rep.len == 0 || rep.len > s.len {
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{}
mut idxs := []int{cap: s.len / rep.len}
defer {
unsafe { idxs.free() }
}