summaryrefslogtreecommitdiffstats
path: root/regex.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2020-10-05 06:19:02 -0700
committerKaz Kylheku <kaz@kylheku.com>2020-10-05 06:19:02 -0700
commit2d4d37907eb060fc23cc4a9f8ebf6b577db3a18b (patch)
treef3d7a0a522c2ccacde21ab944fb53ec0c7363c85 /regex.c
parent77f92bd8b4cb96ddae97b2cdec2427fd4c16c6ee (diff)
downloadtxr-2d4d37907eb060fc23cc4a9f8ebf6b577db3a18b.tar.gz
txr-2d4d37907eb060fc23cc4a9f8ebf6b577db3a18b.tar.bz2
txr-2d4d37907eb060fc23cc4a9f8ebf6b577db3a18b.zip
New functions trim-left and trim-right.
* regex.c (trim_left, trim_right): New static functions. (regex_init): New intrinsics registered. * tests/015/trim.tl, tests/015/trim.expected: New files. * txr.1: Documented.
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 acb2c7a8..27961970 100644
--- a/regex.c
+++ b/regex.c
@@ -3224,6 +3224,32 @@ val count_until_match(val regex, val stream_in)
return scan_until_common(lit("count-until-match"), regex, stream_in, nil, nil);
}
+static val trim_left(val regex, val string)
+{
+ if (regexp(regex)) {
+ val pos = match_regex(string, regex, nil);
+ if (pos)
+ return sub_str(string, pos, t);
+ } else if (starts_with(regex, string, nil, nil)) {
+ return sub_str(string, length(regex), t);
+ }
+
+ return string;
+}
+
+static val trim_right(val regex, val string)
+{
+ if (regexp(regex)) {
+ val pos = match_regex_right(string, regex, nil);
+ if (pos)
+ return sub_str(string, zero, minus(length(string), pos));
+ } else if (ends_with(regex, string, nil, nil)) {
+ return sub_str(string, zero, minus(length(string), length(regex)));
+ }
+
+ return string;
+}
+
static char_set_t *create_wide_cs(void)
{
#ifdef FULL_UNICODE
@@ -3350,6 +3376,8 @@ void regex_init(void)
reg_fun(intern(lit("fr^"), user_package), func_n2o(regex_range_left_fun, 1));
reg_fun(intern(lit("fr$"), user_package), func_n2o(regex_range_right_fun, 1));
reg_fun(intern(lit("frr"), user_package), func_n3o(regex_range_search_fun, 1));
+ reg_fun(intern(lit("trim-left"), user_package), func_n2(trim_left));
+ reg_fun(intern(lit("trim-right"), user_package), func_n2(trim_right));
init_special_char_sets();
}