Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions vlib/builtin/string.v
Original file line number Diff line number Diff line change
Expand Up @@ -492,21 +492,21 @@ pub fn (s string) replace_each(vals []string) string {
return s.clone()
}
idxs.sort(a.idx < b.idx)
mut b := unsafe { malloc_noscan(new_len + 1) } // add space for 0 terminator
mut buf := unsafe { malloc_noscan(new_len + 1) } // add space for 0 terminator
// Fill the new string
mut idx_pos := 0
mut cur_idx := idxs[idx_pos]
mut b_i := 0
mut buf_i := 0
for i := 0; i < s.len; i++ {
if i == cur_idx.idx {
// Reached the location of rep, replace it with "with"
rep := vals[cur_idx.val_idx]
with := vals[cur_idx.val_idx + 1]
for j in 0 .. with.len {
unsafe {
b[b_i] = with[j]
buf[buf_i] = with[j]
}
b_i++
buf_i++
}
// Skip the length of rep, since we just replaced it with "with"
i += rep.len - 1
Expand All @@ -518,14 +518,14 @@ pub fn (s string) replace_each(vals []string) string {
} else {
// Rep doesnt start here, just copy
unsafe {
b[b_i] = s.str[i]
buf[buf_i] = s.str[i]
}
b_i++
buf_i++
}
}
unsafe {
b[new_len] = 0
return tos(b, new_len)
buf[new_len] = 0
return tos(buf, new_len)
}
}

Expand Down
Loading