diff options
-rw-r--r-- | ChangeLog | 13 | ||||
-rw-r--r-- | eval.c | 4 | ||||
-rw-r--r-- | regex.c | 21 | ||||
-rw-r--r-- | regex.h | 3 | ||||
-rw-r--r-- | txr.1 | 37 |
5 files changed, 74 insertions, 4 deletions
@@ -1,3 +1,16 @@ +2015-02-20 Kaz Kylheku <kaz@kylheku.com> + + String-returning wrappers for some regex matching functions. + + * eval.c (eval_init): Register search-regst, match-regst + and match-regst-right intrinsics. + + * regex.c (search_regst, match_regst, match_regst_right): New functions. + + * regex.h (search_regst, match_regst, match_regst_right): Declared. + + * txr.1: Documented new variants. + 2015-02-15 Kaz Kylheku <kaz@kylheku.com> * regex.c (print_rec): A compound must use parentheses for @@ -4022,9 +4022,13 @@ void eval_init(void) reg_fun(intern(lit("regexp"), user_package), func_n1(regexp)); reg_fun(intern(lit("search-regex"), user_package), func_n4o(search_regex, 2)); reg_fun(intern(lit("range-regex"), user_package), func_n4o(range_regex, 2)); + reg_fun(intern(lit("search-regst"), user_package), func_n4o(search_regst, 2)); reg_fun(intern(lit("match-regex"), user_package), func_n3o(match_regex, 2)); + reg_fun(intern(lit("match-regst"), user_package), func_n3o(match_regst, 2)); reg_fun(intern(lit("match-regex-right"), user_package), func_n3o(match_regex_right, 2)); + reg_fun(intern(lit("match-regst-right"), user_package), + func_n3o(match_regst_right, 2)); reg_fun(intern(lit("regsub"), user_package), func_n3(regsub)); reg_fun(intern(lit("regex-parse"), user_package), func_n2o(regex_parse, 1)); @@ -2131,6 +2131,27 @@ val regsub(val regex, val repl, val str) return cat_str(out, nil); } +val search_regst(val haystack, val needle_regex, val start_num, val from_end) +{ + val range = range_regex(haystack, needle_regex, start_num, from_end); + return if2(range, sub_str(haystack, car(range), cdr(range))); +} + +val match_regst(val str, val regex, val pos_in) +{ + val pos = default_arg(pos_in, zero); + val len = match_regex(str, regex, pos); + return if2(len, sub_str(str, pos, plus(pos, len))); +} + +val match_regst_right(val str, val regex, val end) +{ + val len = match_regex_right(str, regex, end); + return if2(len, if3(null_or_missing_p(end), + sub_str(str, neg(len), t), + sub_str(str, minus(end, len), end))); +} + val space_k, digit_k, word_char_k; val cspace_k, cdigit_k, cword_char_k; @@ -35,6 +35,9 @@ val search_regex(val haystack, val needle_regex, val start_num, val from_end); val range_regex(val haystack, val needle_regex, val start_num, val from_end); val match_regex(val str, val regex, val pos); val match_regex_right(val str, val regex, val end); +val search_regst(val haystack, val needle_regex, val start_num, val from_end); +val match_regst(val str, val regex, val pos); +val match_regst_right(val str, val regex, val end); val regsub(val regex, val repl, val str); void regex_init(void); @@ -19705,6 +19705,7 @@ function to obtain more information about the form. .synb .mets (search-regex < string < regex >> [ start <> [ from-end ]]) .mets (range-regex < string < regex >> [ start <> [ from-end ]]) +.mets (range-regst < string < regex >> [ start <> [ from-end ]]) .syne .desc The @@ -19749,9 +19750,19 @@ indicates the position one element past the last character of the match. If the match is empty, the two integers are equal. -.coNP Function @ match-regex +The +.code search-regst +differs from +.code search-regex +in the representation of the return value in the matching case. +Rather than returning the position and length of the match, +it returns the matching substring of +.metn string . + +.coNP Functions @ match-regex and @ match-regst .synb .mets (match-regex < string < regex <> [ position ]) +.mets (match-regst < string < regex <> [ position ]) .syne .desc The @@ -19770,9 +19781,19 @@ If it does not match, then .code nil is returned. -.coNP Function @ match-regex-right +The +.code match-regst +differs from +.code match-regex +in the representation of the return value in the matching case. +Rather than returning the length of the match, it returns +matching substring of +.metn string . + +.coNP Functions @ match-regex-right and @ match-regst-right .synb .mets (match-regex-right < string < regex <> [ end-position ]) +.mets (match-regst-right < string < regex <> [ end-position ]) .syne .desc The @@ -19787,8 +19808,7 @@ If is not specified, it defaults to the length of the string, and the function performs a right-anchored regex match. -If a match is found, then the length of the match is returned, and -the matching substring is then returned. +If a match is found, then the length of the match is returned. The match must terminate just before .meta end-position @@ -19806,6 +19826,15 @@ If no such a match is found, then .code nil is returned. +The +.code match-regst-right +differs from +.code match-regst-right +in the representation of the return value in the matching case. +Rather than returning the length of the match, it returns +matching substring of +.metn string . + .TP* Examples: .cblk |