hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 70127bdedc754bd3bf96f7bd48befb8a27999df9
parent 8680b52afbc819dc4076e6e49f85256ca93b5b10
Author: Sebastian <sebastian@sebsite.pw>
Date:   Mon, 27 Nov 2023 01:18:10 -0500

strings: remove duplicate allocation in multireplace

Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mstrings/replace.ha | 14+++++---------
1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/strings/replace.ha b/strings/replace.ha @@ -19,21 +19,17 @@ export fn replace(s: str, needle: str, target: str) str = { // // The caller must free the return value. export fn multireplace(s: str, repls: (str, str)...) str = { - let replb: []([]u8, []u8) = alloc([], len(repls)); - defer free(replb); - for (let i = 0z; i < len(repls); i += 1) { - static append(replb, (toutf8(repls[i].0), toutf8(repls[i].1))); - }; let b = toutf8(s); let res: []u8 = []; let i = 0z; let prev = 0z; // end of previous match, so we can append in chunks for (i < len(b)) :step { - for (let j = 0z; j < len(replb); j += 1) { - if (bytes::hasprefix(b[i..], replb[j].0)) { + for (let j = 0z; j < len(repls); j += 1) { + const replb = (toutf8(repls[j].0), toutf8(repls[j].1)); + if (bytes::hasprefix(b[i..], replb.0)) { append(res, b[prev..i]...); - append(res, replb[j].1...); - i += len(replb[j].0); + append(res, replb.1...); + i += len(replb.0); prev = i; continue :step; };