summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2014-06-10 07:29:56 -0700
committerKaz Kylheku <kaz@kylheku.com>2014-06-10 07:29:56 -0700
commit79f55cbf6c6e7412473e119a02aa637e985721f5 (patch)
tree7f8d7fc468dc5499431e570b3704a22e243999df
parent06e308526e9c612eaf6e36c36f2213bb915038e4 (diff)
downloadtxr-79f55cbf6c6e7412473e119a02aa637e985721f5.tar.gz
txr-79f55cbf6c6e7412473e119a02aa637e985721f5.tar.bz2
txr-79f55cbf6c6e7412473e119a02aa637e985721f5.zip
* eval.c (eval_init): Change registration of string_cmp
to cmp_str. Add registrations for str_eq, str_lt, str_gt, str_le, and str_lt. * lib.c (string_cmp): Name changes to cmp_str, and the function fixed so that it actually works. The name change doesn't affect anyone because the function was too broken to use due to the incorrect type dispatch. (string_lt): Name changes to str_lt. (str_eq, str_gt, str_le, str_ge): New functions. * lib.h (string_cmp, string_lt): Declarations renamed. (str_eq, str_gt, str_le, str_ge): New declarations. * txr.1: Document string-cmp to cmp-str rename, that string-lt is deprecated, and the new str<, str>, str>=, str<= and str= functions.
-rw-r--r--ChangeLog20
-rw-r--r--eval.c9
-rw-r--r--lib.c40
-rw-r--r--lib.h8
-rw-r--r--txr.166
5 files changed, 111 insertions, 32 deletions
diff --git a/ChangeLog b/ChangeLog
index 025aed8f..4388c617 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,25 @@
2014-06-10 Kaz Kylheku <kaz@kylheku.com>
+ * eval.c (eval_init): Change registration of string_cmp
+ to cmp_str. Add registrations for str_eq, str_lt,
+ str_gt, str_le, and str_lt.
+
+ * lib.c (string_cmp): Name changes to cmp_str, and the
+ function fixed so that it actually works.
+ The name change doesn't affect anyone because the function
+ was too broken to use due to the incorrect type dispatch.
+ (string_lt): Name changes to str_lt.
+ (str_eq, str_gt, str_le, str_ge): New functions.
+
+ * lib.h (string_cmp, string_lt): Declarations renamed.
+ (str_eq, str_gt, str_le, str_ge): New declarations.
+
+ * txr.1: Document string-cmp to cmp-str rename, that
+ string-lt is deprecated, and the new str<, str>,
+ str>=, str<= and str= functions.
+
+2014-06-10 Kaz Kylheku <kaz@kylheku.com>
+
* Makefile (PROG): Removing ./ prefix from variable name; adding it
to invocations of $(PROG) in some rules.
(txr.o): Pass several strings as macros on the command line:
diff --git a/eval.c b/eval.c
index 068e1a9e..bbae73a6 100644
--- a/eval.c
+++ b/eval.c
@@ -3446,8 +3446,13 @@ void eval_init(void)
reg_fun(intern(lit("tok-str"), user_package), func_n3o(tok_str, 1));
reg_fun(intern(lit("list-str"), user_package), func_n1(list_str));
reg_fun(intern(lit("trim-str"), user_package), func_n1(trim_str));
- reg_fun(intern(lit("string-cmp"), user_package), func_n2(string_cmp));
- reg_fun(intern(lit("string-lt"), user_package), func_n2(string_lt));
+ reg_fun(intern(lit("cmp-str"), user_package), func_n2(cmp_str));
+ reg_fun(intern(lit("string-lt"), user_package), func_n2(str_lt));
+ reg_fun(intern(lit("str="), user_package), func_n2(str_eq));
+ reg_fun(intern(lit("str<"), user_package), func_n2(str_lt));
+ reg_fun(intern(lit("str>"), user_package), func_n2(str_gt));
+ reg_fun(intern(lit("str<="), user_package), func_n2(str_le));
+ reg_fun(intern(lit("str>="), user_package), func_n2(str_ge));
reg_fun(intern(lit("int-str"), user_package), func_n2o(int_str, 1));
reg_fun(intern(lit("flo-str"), user_package), func_n1(flo_str));
reg_fun(intern(lit("num-str"), user_package), func_n1(num_str));
diff --git a/lib.c b/lib.c
index b0118f45..e8117bf4 100644
--- a/lib.c
+++ b/lib.c
@@ -2499,9 +2499,9 @@ val trim_str(val str)
}
}
-val string_cmp(val astr, val bstr)
+val cmp_str(val astr, val bstr)
{
- switch (TYPE_PAIR(tag(astr), tag(bstr))) {
+ switch (TYPE_PAIR(type(astr), type(bstr))) {
case TYPE_PAIR(LIT, LIT):
case TYPE_PAIR(STR, STR):
case TYPE_PAIR(LIT, STR):
@@ -2522,25 +2522,47 @@ val string_cmp(val astr, val bstr)
val bch = chr_str(bstr, i);
if (ach < bch)
- return num_fast(-1);
+ return one;
else if (ach < bch)
- return num_fast(1);
+ return one;
}
if (length_str_lt(bstr, i))
- return num_fast(-1);
+ return negone;
if (length_str_lt(astr, i))
- return num_fast(1);
+ return negone;
return zero;
}
default:
- uw_throwf(error_s, lit("string-cmp: invalid operands ~s ~s"),
+ uw_throwf(error_s, lit("cmp-str: invalid operands ~s ~s"),
astr, bstr, nao);
}
}
-val string_lt(val astr, val bstr)
+val str_eq(val astr, val bstr)
{
- return wcscmp(c_str(astr), c_str(bstr)) < 0 ? t : nil;
+ return if2(cmp_str(astr, bstr) == zero, t);
+}
+
+val str_lt(val astr, val bstr)
+{
+ return if2(cmp_str(astr, bstr) == negone, t);
+}
+
+val str_gt(val astr, val bstr)
+{
+ return if2(cmp_str(astr, bstr) == one, t);
+}
+
+val str_le(val astr, val bstr)
+{
+ val cmp = cmp_str(astr, bstr);
+ return if2(cmp == zero || cmp == negone, t);
+}
+
+val str_ge(val astr, val bstr)
+{
+ val cmp = cmp_str(astr, bstr);
+ return if2(cmp == zero || cmp == one, t);
}
val int_str(val str, val base)
diff --git a/lib.h b/lib.h
index 4f652e6d..157649af 100644
--- a/lib.h
+++ b/lib.h
@@ -573,8 +573,12 @@ val split_str_set(val str, val set);
val tok_str(val str, val tok_regex, val keep_sep);
val list_str(val str);
val trim_str(val str);
-val string_cmp(val astr, val bstr);
-val string_lt(val astr, val bstr);
+val cmp_str(val astr, val bstr);
+val str_eq(val astr, val bstr);
+val str_lt(val astr, val bstr);
+val str_gt(val astr, val bstr);
+val str_le(val astr, val bstr);
+val str_ge(val astr, val bstr);
val int_str(val str, val base);
val flo_str(val str);
val num_str(val str);
diff --git a/txr.1 b/txr.1
index 1d8a617b..0bd0a9f8 100644
--- a/txr.1
+++ b/txr.1
@@ -9075,22 +9075,6 @@ The trim-str function produces a copy of <string> from which leading and
trailing whitespace is removed. Whitespace consists of spaces, tabs,
carriage returns, linefeeds, vertical tabs and form feeds.
-.SS Function string-lt
-
-.TP
-Syntax:
-
- (string-lt <left-str> <right-str>)
-
-.TP
-Description:
-
-The string-lt function returns t if <left-str> is lexicographically prior
-to <right-str>. The behavior does not depend on any kind of locale.
-
-Note that this function forces (fully instantiates) any lazy string arguments,
-even if doing is is not necessary.
-
.SS Function chrp
.TP
@@ -9611,17 +9595,17 @@ These functions can be used to test such as string whether it is longer
or shorter than a given length, without forcing the string beyond
that length.
-.SS Function string-cmp
+.SS Function cmp-str
.TP
Syntax:
- (string-cmp <left-string> <right-string>)
+ (cmp-str <left-string> <right-string>)
.TP
Semantics:
-The string-cmp function returns a negative integer if <left-string>
+The cmp-str function returns a negative integer if <left-string>
is lexicographically prior to <right-string>, and a positive integer
if the reverse situation is the case. Otherwise the strings are equal
and zero is returned.
@@ -9635,6 +9619,50 @@ The lexicographic ordering is naive, based on the character code point
values in Unicode taken as integers, without regard for locale-specific
collation orders.
+.SS Functions str=, str<, str>, str>= and str<=
+
+.TP
+Syntax:
+
+ (str= <left-string> <right-string>)
+ (str< <left-string> <right-string>)
+ (str> <left-string> <right-string>)
+ (str<= <left-string> <right-string>)
+ (str>= <left-string> <right-string>)
+
+.TP
+Semantics:
+
+These functions compare <left-string> and <right-string> lexicographically,
+as if by the cmp-str function.
+
+The str= function returns t if the two strings are exactly the same, character
+for character, otherwise it returns nil.
+
+The str< function returns t if <left-string> is lexicographically before
+<right-string>, otherwise nil.
+
+The str> function returns t if <left-string> is lexicographically after
+<right-string>, otherwise nil.
+
+The str< function returns t if <left-string> is lexicographically before
+<right-string>, or if they are exactly the same, otherwise nil.
+
+The str< function returns t if <left-string> is lexicographically after
+<right-string>, or if they are exactly the same, otherwise nil.
+
+.SS Function string-lt
+
+.TP
+Syntax:
+
+ (string-lt <left-str> <right-str>)
+
+.TP
+Description:
+
+The string-lt is a deprecated alias for str<.
+
.SH VECTORS
.SS Function vector