summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog13
-rw-r--r--eval.c4
-rw-r--r--regex.c21
-rw-r--r--regex.h3
-rw-r--r--txr.137
5 files changed, 74 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index ad117473..fefd532f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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
diff --git a/eval.c b/eval.c
index 645af5f2..5721aa85 100644
--- a/eval.c
+++ b/eval.c
@@ -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));
diff --git a/regex.c b/regex.c
index f3b4ffc6..1c7c63a3 100644
--- a/regex.c
+++ b/regex.c
@@ -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;
diff --git a/regex.h b/regex.h
index e3d3715d..90695a1d 100644
--- a/regex.h
+++ b/regex.h
@@ -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);
diff --git a/txr.1 b/txr.1
index 843394fa..e0b86ebf 100644
--- a/txr.1
+++ b/txr.1
@@ -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