diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2022-07-27 05:18:13 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2022-07-27 05:18:13 -0700 |
commit | c04217e25e9b5aeb04ba765a66c9b1e2cc011533 (patch) | |
tree | 0ce69a2d7a65b4ed53980991702056d2442ecf90 | |
parent | afe568e3eb40e3a6cf91bccd3d4565e9e9f5c540 (diff) | |
download | txr-c04217e25e9b5aeb04ba765a66c9b1e2cc011533.tar.gz txr-c04217e25e9b5aeb04ba765a66c9b1e2cc011533.tar.bz2 txr-c04217e25e9b5aeb04ba765a66c9b1e2cc011533.zip |
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.
-rw-r--r-- | regex.c | 20 |
1 files changed, 10 insertions, 10 deletions
@@ -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); } } |