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. --- lib.c | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) (limited to 'lib.c') 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) -- cgit v1.2.3