builtin.string: optimize replace() (#9955)
parent
000d4d3064
commit
3363c3ef65
|
@ -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`.
|
// replace replaces all occurences of `rep` with the string passed in `with`.
|
||||||
pub fn (s string) replace(rep string, with string) string {
|
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()
|
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{}
|
mut idxs := []int{cap: s.len / rep.len}
|
||||||
defer {
|
defer {
|
||||||
unsafe { idxs.free() }
|
unsafe { idxs.free() }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue