1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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

This commit is contained in:
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

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() }
}