summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2022-05-30 06:44:28 -0700
committerKaz Kylheku <kaz@kylheku.com>2022-05-30 06:44:28 -0700
commitcce3c4bef38888be015b0652d136e34c8ae7e59f (patch)
tree8f1af5da0718bf01b71ca860616ca7b8f3d652a6
parentbc591cb5ee04582dbdf17a0125987ec03e8c064f (diff)
downloadtxr-cce3c4bef38888be015b0652d136e34c8ae7e59f.tar.gz
txr-cce3c4bef38888be015b0652d136e34c8ae7e59f.tar.bz2
txr-cce3c4bef38888be015b0652d136e34c8ae7e59f.zip
New: spln and tokn functions.
Instead of trying to work the new count parameter into the spl and tok functions, it's better to make new ones. * eval.c (eval_init): spln and tokn intrinsics registered. * lib.[ch] (spln, tokn): New functions. * tests/015/split.tl: New test cases. * txr.1: Documented. * stdlib/doc-syms.tl: Updated.
-rw-r--r--eval.c2
-rw-r--r--lib.c24
-rw-r--r--lib.h2
-rw-r--r--stdlib/doc-syms.tl3
-rw-r--r--tests/015/split.tl27
-rw-r--r--txr.122
6 files changed, 78 insertions, 2 deletions
diff --git a/eval.c b/eval.c
index cbe37f55..2986935d 100644
--- a/eval.c
+++ b/eval.c
@@ -7215,10 +7215,12 @@ void eval_init(void)
reg_fun(intern(lit("cat-str"), user_package), func_n2o(cat_str, 1));
reg_fun(intern(lit("split-str"), user_package), func_n4o(split_str_keep, 2));
reg_fun(intern(lit("spl"), user_package), func_n3o(spl, 2));
+ reg_fun(intern(lit("spln"), user_package), func_n4o(spln, 3));
reg_fun(intern(lit("split-str-set"), user_package), func_n2(split_str_set));
reg_fun(intern(lit("sspl"), user_package), func_n2(sspl));
reg_fun(intern(lit("tok-str"), user_package), func_n4o(tok_str, 2));
reg_fun(intern(lit("tok"), user_package), func_n3o(tok, 2));
+ reg_fun(intern(lit("tokn"), user_package), func_n4o(tokn, 3));
reg_fun(intern(lit("tok-where"), user_package), func_n2(tok_where));
reg_fun(intern(lit("list-str"), user_package), func_n1(list_str));
reg_fun(intern(lit("trim-str"), user_package), func_n1(trim_str));
diff --git a/lib.c b/lib.c
index c4d1bc5c..23fd5008 100644
--- a/lib.c
+++ b/lib.c
@@ -5859,6 +5859,18 @@ val spl(val sep, val arg1, val arg2)
split_str_keep(arg2, sep, arg1, nil));
}
+val spln(val count, val sep, val arg1, val arg2)
+{
+ val self = lit("spln");
+
+ if (null_or_missing_p(count))
+ uw_throwf(error_s, lit("~a: count ~s isn't an integer"), self, count, nao);
+
+ return if3(missingp(arg2),
+ split_str_keep(arg1, sep, arg2, count),
+ split_str_keep(arg2, sep, arg1, count));
+}
+
val split_str(val str, val sep)
{
return split_str_keep(str, sep, nil, nil);
@@ -5977,6 +5989,18 @@ val tok(val tok_regex, val arg1, val arg2)
tok_str(arg2, tok_regex, arg1, nil));
}
+val tokn(val count, val tok_regex, val arg1, val arg2)
+{
+ val self = lit("tokn");
+
+ if (null_or_missing_p(count))
+ uw_throwf(error_s, lit("~a: count ~s isn't an integer"), self, count, nao);
+
+ return if3(missingp(arg2),
+ tok_str(arg1, tok_regex, arg2, count),
+ tok_str(arg2, tok_regex, arg1, count));
+}
+
val tok_where(val str, val tok_regex)
{
list_collect_decl (out, iter);
diff --git a/lib.h b/lib.h
index 585a2c05..59b20645 100644
--- a/lib.h
+++ b/lib.h
@@ -927,10 +927,12 @@ val fmt_join(struct args *args);
val split_str(val str, val sep);
val split_str_keep(val str, val sep, val keep_sep_opt, val count_opt);
val spl(val sep, val arg1, val arg2);
+val spln(val count, val sep, val arg1, val arg2);
val split_str_set(val str, val set);
val sspl(val set, val str);
val tok_str(val str, val tok_regex, val keep_sep_opt, val count_opt);
val tok(val tok_regex, val arg1, val arg2);
+val tokn(val count, val tok_regex, val arg1, val arg2);
val tok_where(val str, val tok_regex);
val list_str(val str);
val trim_str(val str);
diff --git a/stdlib/doc-syms.tl b/stdlib/doc-syms.tl
index b9956422..47f0a89f 100644
--- a/stdlib/doc-syms.tl
+++ b/stdlib/doc-syms.tl
@@ -1838,12 +1838,13 @@
("span-str" "N-0394CA3A")
("special-operator-p" "N-01E259AD")
("special-var-p" "N-00833473")
- ("spl" "N-03C7F5FA")
+ ("spl" "N-026FC0BD")
("splice" "N-03BC798C")
("split" "N-02FD4882")
("split*" "N-02FD4882")
("split-str" "N-000386B4")
("split-str-set" "N-0296195B")
+ ("spln" "N-026FC0BD")
("sqrt" "D-0027")
("square" "D-0032")
("ssize-t" "N-01153D9E")
diff --git a/tests/015/split.tl b/tests/015/split.tl
index 9e952342..15b3f2a7 100644
--- a/tests/015/split.tl
+++ b/tests/015/split.tl
@@ -211,6 +211,23 @@
(tok-str "a,b,c" #/[^,]/ t 4) ("" "a" "," "b" "," "c" ""))
(mtest
+ (tok #/[^,]/ "a,b,c") #"a b c"
+ (tokn : #/[^,]/ "a,b,c") :error
+ (tokn nil #/[^,]/ "a,b,c") :error
+ (tokn 0 #/[^,]/ "a,b,c") ("a,b,c")
+ (tokn 1 #/[^,]/ "a,b,c") ("a" ",b,c")
+ (tokn 2 #/[^,]/ "a,b,c") ("a" "b" ",c")
+ (tokn 3 #/[^,]/ "a,b,c") ("a" "b" "c")
+ (tokn 4 #/[^,]/ "a,b,c") ("a" "b" "c"))
+
+(mtest
+ (tokn 0 #/[^,]/ t "a,b,c") ("a,b,c")
+ (tokn 1 #/[^,]/ t "a,b,c") ("" "a" ",b,c")
+ (tokn 2 #/[^,]/ t "a,b,c") ("" "a" "," "b" ",c")
+ (tokn 3 #/[^,]/ t "a,b,c") ("" "a" "," "b" "," "c" "")
+ (tokn 4 #/[^,]/ t "a,b,c") ("" "a" "," "b" "," "c" ""))
+
+(mtest
(join) ""
(join "") ""
(join "" "") ""
@@ -271,3 +288,13 @@
(split-str "a12b34c567d" #/[0-9]+/ t 2) ("a" "12" "b" "34" "c567d")
(split-str "a12b34c567d" #/[0-9]+/ t 3) ("a" "12" "b" "34" "c" "567" "d")
(split-str "a12b34c567d" #/[0-9]+/ t 4) ("a" "12" "b" "34" "c" "567" "d"))
+
+(mtest
+ (spl "," "a,b,c") #"a b c"
+ (spln : "," "a,b,c") :error
+ (spln nil "," "a,b,c") :error
+ (spln 0 "," "a,b,c") ("a,b,c")
+ (spln 1 "," "a,b,c") ("a" "b,c")
+ (spln 2 "," "a,b,c") ("a" "b" "c")
+ (spln 3 "," "a,b,c") ("a" "b" "c")
+ (spln 4 "," "a,b,c") ("a" "b" "c"))
diff --git a/txr.1 b/txr.1
index 3c68e74d..c243d516 100644
--- a/txr.1
+++ b/txr.1
@@ -25486,9 +25486,10 @@ is zero, then
returns a list of one element, which is
.metn string .
-.coNP Function @ spl
+.coNP Functions @ spl and @ spln
.synb
.mets (spl < sep <> [ keep-between ] << string )
+.mets (spln < count < sep <> [ keep-between ] << string )
.syne
.desc
The
@@ -25520,6 +25521,15 @@ family, in the common situation when
.meta string
is the unbound argument.
+The
+.code spln
+function is similar to
+.codn spl ,
+taking a required argument
+.metn count ,
+which behaves exactly like the same-named argument of
+.codn spl-str .
+
.coNP Functions @ split-str-set and @ sspl
.synb
.mets (split-str-set < string << set )
@@ -25672,6 +25682,7 @@ parameter.
.coNP Function @ tok
.synb
.mets (tok < regex <> [ keep-between ] << string )
+.mets (tokn < count < regex <> [ keep-between ] << string )
.syne
.desc
The
@@ -25703,6 +25714,15 @@ family, in the common situation when
.meta string
is the unbound argument.
+The
+.code tokn
+function is similar to
+.codn tok ,
+taking a required argument
+.metn count ,
+which behaves exactly like the same-named argument of
+.codn tok-str .
+
.coNP Function @ list-str
.synb
.mets (list-str << string )