From 79f55cbf6c6e7412473e119a02aa637e985721f5 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 10 Jun 2014 07:29:56 -0700 Subject: * 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. --- ChangeLog | 20 +++++++++++++++++++ eval.c | 9 +++++++-- lib.c | 40 +++++++++++++++++++++++++++++--------- lib.h | 8 ++++++-- txr.1 | 66 +++++++++++++++++++++++++++++++++++++++++++++------------------ 5 files changed, 111 insertions(+), 32 deletions(-) diff --git a/ChangeLog b/ChangeLog index 025aed8f..4388c617 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,23 @@ +2014-06-10 Kaz Kylheku + + * 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 * Makefile (PROG): Removing ./ prefix from variable name; adding it 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 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 ) - -.TP -Description: - -The string-lt function returns t if is lexicographically prior -to . 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 ) + (cmp-str ) .TP Semantics: -The string-cmp function returns a negative integer if +The cmp-str function returns a negative integer if is lexicographically prior to , 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= ) + (str< ) + (str> ) + (str<= ) + (str>= ) + +.TP +Semantics: + +These functions compare and 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 is lexicographically before +, otherwise nil. + +The str> function returns t if is lexicographically after +, otherwise nil. + +The str< function returns t if is lexicographically before +, or if they are exactly the same, otherwise nil. + +The str< function returns t if is lexicographically after +, or if they are exactly the same, otherwise nil. + +.SS Function string-lt + +.TP +Syntax: + + (string-lt ) + +.TP +Description: + +The string-lt is a deprecated alias for str<. + .SH VECTORS .SS Function vector -- cgit v1.2.3