From: Collin Funk Date: Sat, 2 Nov 2024 03:49:16 +0000 (-0700) Subject: test: add string operators added by POSIX 2024 X-Git-Tag: v9.6~117 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ee231019e185732780c72b46616db64a836f50f0;p=thirdparty%2Fcoreutils.git test: add string operators added by POSIX 2024 * src/test.c (binop): Recognize the ">" and "<" operators. (three_arguments): Likewise. (binary_operator): Implement the "<" and ">" operators. (usage): Add operators to --help output. * tests/test/test.pl (@Tests): Add functionality tests. * doc/coreutils.texi (test invocation, String tests): Document new operators. * NEWS: Mention the new feature. --- diff --git a/NEWS b/NEWS index 75263d0272..64d137ab0f 100644 --- a/NEWS +++ b/NEWS @@ -46,6 +46,9 @@ GNU coreutils NEWS -*- outline -*- %$ format, where '' is an integer referencing a particular argument, thus allowing repetition or reordering of printf arguments. + test supports the POSIX:2024 specified '<' and '>' operators with strings, + to compare the string locale collating order. + timeout now supports the POSIX:2024 specified -f, and -p short options, corresponding to --foreground, and --preserve-status respectively. diff --git a/doc/coreutils.texi b/doc/coreutils.texi index 60a07b2ef7..e016fdf26a 100644 --- a/doc/coreutils.texi +++ b/doc/coreutils.texi @@ -13733,7 +13733,7 @@ Exit status: * File type tests:: @code{-[bcdfhLpSt]} * Access permission tests:: @code{-[gkruwxOG]} * File characteristic tests:: @code{-e -s -nt -ot -ef} -* String tests:: @code{-z -n = == !=} +* String tests:: @code{-z -n = == != > <} * Numeric tests:: @code{-eq -ne -lt -le -gt -ge} * Connectives for test:: @code{! -a -o} @end menu @@ -13942,6 +13942,16 @@ shells and systems. @cindex not-equal string check True if the strings are not equal. +@item @var{string1} > @var{string2} +@opindex > +@cindex greater-than string check +True if @var{string1} is greater than @var{string2} in the current locale. + +@item @var{string1} < @var{string2} +@opindex < +@cindex less-than string check +True if @var{string1} is less than @var{string2} in the current locale. + @end table diff --git a/src/test.c b/src/test.c index 5af9243474..7d3a788792 100644 --- a/src/test.c +++ b/src/test.c @@ -173,7 +173,7 @@ static bool binop (char const *s) { return ((STREQ (s, "=")) || (STREQ (s, "!=")) || (STREQ (s, "==")) || - (STREQ (s, "-nt")) || + (STREQ (s, "-nt")) || (STREQ (s, ">")) || (STREQ (s, "<")) || (STREQ (s, "-ot")) || (STREQ (s, "-ef")) || (STREQ (s, "-eq")) || (STREQ (s, "-ne")) || (STREQ (s, "-lt")) || (STREQ (s, "-le")) || (STREQ (s, "-gt")) || (STREQ (s, "-ge"))); @@ -189,7 +189,7 @@ binop (char const *s) * '-t' int * '-'('z'|'n') string * string - * string ('!='|'=') string + * string ('!='|'='|>|<) string * '-'(eq|ne|le|lt|ge|gt) * file '-'(nt|ot|ef) file * '(' ')' @@ -371,6 +371,20 @@ binary_operator (bool l_is_l) return value; } + if (STREQ (argv[op], ">")) + { + bool value = strcoll (argv[pos], argv[pos + 2]) > 0; + pos += 3; + return value; + } + + if (STREQ (argv[op], "<")) + { + bool value = strcoll (argv[pos], argv[pos + 2]) < 0; + pos += 3; + return value; + } + /* Not reached. */ affirm (false); } @@ -615,7 +629,8 @@ three_arguments (void) value = one_argument (); advance (false); } - else if (STREQ (argv[pos + 1], "-a") || STREQ (argv[pos + 1], "-o")) + else if (STREQ (argv[pos + 1], "-a") || STREQ (argv[pos + 1], "-o") + || STREQ (argv[pos + 1], ">") || STREQ (argv[pos + 1], "<")) value = expr (); else test_syntax_error (_("%s: binary operator expected"), @@ -708,6 +723,8 @@ EXPRESSION is true or false and sets exit status. It is one of:\n\ -z STRING the length of STRING is zero\n\ STRING1 = STRING2 the strings are equal\n\ STRING1 != STRING2 the strings are not equal\n\ + STRING1 > STRING2 STRING1 is greater than STRING2 in the current locale\n\ + STRING1 < STRING2 STRING1 is less than STRING2 in the current locale\n\ "), stdout); fputs (_("\ \n\ diff --git a/tests/test/test.pl b/tests/test/test.pl index 0433461d86..e8509448b4 100755 --- a/tests/test/test.pl +++ b/tests/test/test.pl @@ -185,6 +185,14 @@ my @Tests = ['paren-3', "'(' ')' ')'"], ['paren-4', "'(' ! ')'"], ['paren-5', "'(' -a ')'"], + + ['less-collate-1', "'a' '<' 'b'"], + ['less-collate-2', "'a' '<' 'a'", {EXIT=>1}], + ['less-collate-3', "'b' '<' 'a'", {EXIT=>1}], + + ['greater-collate-1', "'b' '>' 'a'"], + ['greater-collate-2', "'a' '>' 'a'", {EXIT=>1}], + ['greater-collate-3', "'a' '>' 'b'", {EXIT=>1}], ); @Tests = add_inverse_op_tests \@Tests;