summaryrefslogtreecommitdiffstats
path: root/regex.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2012-03-04 18:34:13 -0800
committerKaz Kylheku <kaz@kylheku.com>2012-03-04 18:34:13 -0800
commitfb96a3ee7732129d0ee475d7d8438aeffe39d80e (patch)
tree646874643d4e325dc15928e51c8f732b3eea07ad /regex.c
parentdde4f53cbd55b758abf04ff02dfb71b074ccb836 (diff)
downloadtxr-fb96a3ee7732129d0ee475d7d8438aeffe39d80e.tar.gz
txr-fb96a3ee7732129d0ee475d7d8438aeffe39d80e.tar.bz2
txr-fb96a3ee7732129d0ee475d7d8438aeffe39d80e.zip
* eval.c (eval_init): New intrinsic function, regsub.
* regex.c (regsub): New function. * regex.h (regsub): Declared. * txr.1: Doc stub added.
Diffstat (limited to 'regex.c')
-rw-r--r--regex.c28
1 files changed, 28 insertions, 0 deletions
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);
+}