From fb96a3ee7732129d0ee475d7d8438aeffe39d80e Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sun, 4 Mar 2012 18:34:13 -0800 Subject: * eval.c (eval_init): New intrinsic function, regsub. * regex.c (regsub): New function. * regex.h (regsub): Declared. * txr.1: Doc stub added. --- regex.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'regex.c') diff --git a/regex.c b/regex.c index 2ad5a5cb..6c2a914d 100644 --- a/regex.c +++ b/regex.c @@ -1723,3 +1723,31 @@ val match_regex(val str, val reg, val pos) return nil; } + +val regsub(val str, val regex, val repl) +{ + list_collect_decl (out, ptail); + val pos = zero; + + do { + cons_bind (find, len, search_regex(str, regex, pos, nil)); + if (!find) { + if (pos == zero) + return str; + list_collect(ptail, sub_str(str, pos, nil)); + break; + } + list_collect(ptail, sub_str(str, pos, find)); + list_collect(ptail, repl); + if (len == zero && eql(find, pos)) { + if (lt(pos, length_str(str))) { + list_collect(ptail, chr_str(str, pos)); + pos = plus(pos, one); + } + } else { + pos = plus(find, len); + } + } while (lt(pos, length_str(str))); + + return cat_str(out, nil); +} -- cgit v1.2.3