From 3dcd046c5f56d56faef4937e22def542d84a7e9b Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 27 Jul 2022 05:18:13 -0700 Subject: regsub: avoid consing list. * regex.c (regsub): Accumulate output string directly using string_extend, rather than accumulating a list of pieces which are catenated with cat_str. --- regex.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/regex.c b/regex.c index 1e8034d3..0c2d7940 100644 --- a/regex.c +++ b/regex.c @@ -2896,25 +2896,25 @@ val regsub(val regex, val repl, val str) rf, rt); } } else { - list_collect_decl (out, ptail); val pos = zero; + val out = mkustring(zero); do { cons_bind (find, len, search_regex(str, regex, pos, nil)); if (!find) { if (pos == zero) return str; - ptail = list_collect(ptail, sub_str(str, pos, nil)); - break; + return string_extend(out, sub_str(str, pos, nil), t); } - ptail = list_collect(ptail, sub_str(str, pos, find)); - ptail = list_collect(ptail, if3(isfunc, - funcall1(repl, sub_str(str, find, - plus(find, len))), - repl)); + string_extend(out, sub_str(str, pos, find), nil); + string_extend(out, if3(isfunc, + funcall1(repl, sub_str(str, find, + plus(find, len))), + repl), + nil); if (len == zero && eql(find, pos)) { if (lt(pos, length_str(str))) { - ptail = list_collect(ptail, chr_str(str, pos)); + string_extend(out, chr_str(str, pos), nil); pos = plus(pos, one); } } else { @@ -2922,7 +2922,7 @@ val regsub(val regex, val repl, val str) } } while (lt(pos, length_str(str))); - return cat_str(out, nil); + return string_finish(out); } } -- cgit v1.2.3