diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2012-03-04 18:34:13 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2012-03-04 18:34:13 -0800 |
commit | fb96a3ee7732129d0ee475d7d8438aeffe39d80e (patch) | |
tree | 646874643d4e325dc15928e51c8f732b3eea07ad /regex.c | |
parent | dde4f53cbd55b758abf04ff02dfb71b074ccb836 (diff) | |
download | txr-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.c | 28 |
1 files changed, 28 insertions, 0 deletions
@@ -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); +} |