diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2016-04-20 05:13:19 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2016-04-20 05:13:19 -0700 |
commit | 03213f2aa15930d7336f582238f8abfacf17584a (patch) | |
tree | b06c13268925e3d9f40a45f041b1f6a3d2bfeb6c /regex.c | |
parent | 04575dff348209315fb24e3c809a93343f39783b (diff) | |
download | txr-03213f2aa15930d7336f582238f8abfacf17584a.tar.gz txr-03213f2aa15930d7336f582238f8abfacf17584a.tar.bz2 txr-03213f2aa15930d7336f582238f8abfacf17584a.zip |
read-until-match can optionally keep matched text.
* regex.c (read_until_match): New argument, include_match.
Three times repeated termination code refactored into block
reached by forward goto.
(regex_init): Registration of read-until-match updated.
* regex.h (read_until_match): Declaration updated.
* stream.c (struct record_adapter_base): New member,
include_match.
(record_adapter_get_line): Pass match to read_until_match
as new argument.
(record_adapater): New argument, include_match.
(stream_init): Update registration of record-adapter.
* stream.h (record_adapter): Declaration updated.
* txr.1: Updated.
Diffstat (limited to 'regex.c')
-rw-r--r-- | regex.c | 40 |
1 files changed, 19 insertions, 21 deletions
@@ -2519,14 +2519,14 @@ val match_regst_right(val str, val regex, val end) sub_str(str, minus(end, len), end))); } -val read_until_match(val regex, val stream) +val read_until_match(val regex, val stream_in, val include_match_in) { regex_machine_t regm; val out = nil; val stack = nil; val match = nil; - - stream = default_arg(stream, std_input); + val stream = default_arg(stream_in, std_input); + val include_match = default_bool_arg(include_match_in); regex_machine_init(®m, regex); @@ -2537,18 +2537,11 @@ val read_until_match(val regex, val stream) switch (regex_machine_feed(®m, 0)) { case REGM_FAIL: case REGM_INCOMPLETE: - if (match) { - while (stack != match) - unget_char(pop(&stack), stream); - if (!out) - out = null_string; - break; - } + if (match) + goto out_match; break; case REGM_MATCH: - if (!out) - out = null_string; - break; + goto out_match; } break; } @@ -2557,13 +2550,8 @@ val read_until_match(val regex, val stream) case REGM_FAIL: unget_char(ch, stream); - if (match) { - while (stack != match) - unget_char(pop(&stack), stream); - if (!out) - out = null_string; - break; - } + if (match) + goto out_match; while (stack) unget_char(pop(&stack), stream); @@ -2589,6 +2577,16 @@ val read_until_match(val regex, val stream) break; } + if (nil) { +out_match: + while (stack != match) + unget_char(pop(&stack), stream); + if (!out) + out = null_string; + if (include_match) + out = cat_str(cons(out, nreverse(stack)), nil); + } + regex_machine_cleanup(®m); return out; } @@ -2674,6 +2672,6 @@ void regex_init(void) reg_fun(intern(lit("reg-expand-nongreedy"), system_package), func_n1(reg_expand_nongreedy)); reg_fun(intern(lit("reg-optimize"), system_package), func_n1(reg_optimize)); - reg_fun(intern(lit("read-until-match"), user_package), func_n2o(read_until_match, 1)); + reg_fun(intern(lit("read-until-match"), user_package), func_n3o(read_until_match, 1)); init_special_char_sets(); } |