Add STRICMP case-insensitive string compare function (#2164)

This commit is contained in:
Rangi authored and GitHub committed 2026-09-27 00:19:10 +02:00
1 parent d95e9260e4
commit cf897aa065
4 files changed
+19 -1

No files matched your search

+1
View File
@@ -659,6 +659,7 @@ The following functions operate on string expressions, but return integers:
.It Sy Name Ta Sy Operation .It Sy Name Ta Sy Operation
.It Fn STRLEN str Ta Returns the number of characters in Ar str . .It Fn STRLEN str Ta Returns the number of characters in Ar str .
.It Fn STRCMP str1 str2 Ta Compares Ar str1 No and Ar str2 No according to ASCII ordering of their characters. Returns -1 if Ar str1 No is lower than Ar str2 Ns , 1 if Ar str1 No is greater than Ar str2 Ns , or 0 if they match. .It Fn STRCMP str1 str2 Ta Compares Ar str1 No and Ar str2 No according to ASCII ordering of their characters. Returns -1 if Ar str1 No is lower than Ar str2 Ns , 1 if Ar str1 No is greater than Ar str2 Ns , or 0 if they match.
.It Fn STRICMP str1 str2 Ta Compares Ar str1 No and Ar str2 No according to case-insensitive ASCII ordering of their characters. Returns -1 if Ar str1 No is lower than Ar str2 Ns , 1 if Ar str1 No is greater than Ar str2 Ns , or 0 if they match.
.It Fn STRFIND str sub Ta Returns the first index of Ar sub No in Ar str Ns , or -1 if it's not present. .It Fn STRFIND str sub Ta Returns the first index of Ar sub No in Ar str Ns , or -1 if it's not present.
.It Fn STRRFIND str sub Ta Returns the last index of Ar sub No in Ar str Ns , or -1 if it's not present. .It Fn STRRFIND str sub Ta Returns the last index of Ar sub No in Ar str Ns , or -1 if it's not present.
.It Fn STRSTARTS str prefix Ta Returns 1 if Ar str No starts with Ar prefix Ns , or 0 otherwise. .It Fn STRSTARTS str prefix Ta Returns 1 if Ar str No starts with Ar prefix Ns , or 0 otherwise.
+1
View File
@@ -185,6 +185,7 @@ static UpperMap<int> const keywords{
{"STRENDS", T_(OP_STRENDS) }, {"STRENDS", T_(OP_STRENDS) },
{"STRFIND", T_(OP_STRFIND) }, {"STRFIND", T_(OP_STRFIND) },
{"STRFMT", T_(OP_STRFMT) }, {"STRFMT", T_(OP_STRFMT) },
{"STRICMP", T_(OP_STRICMP) },
{"STRIN", T_(OP_STRIN) }, {"STRIN", T_(OP_STRIN) },
{"STRLEN", T_(OP_STRLEN) }, {"STRLEN", T_(OP_STRLEN) },
{"STRLWR", T_(OP_STRLWR) }, {"STRLWR", T_(OP_STRLWR) },
+9 -1
View File
@@ -5,6 +5,8 @@
%define api.token.constructor %define api.token.constructor
%code requires { %code requires {
#include "platform.hpp"
#include <stdint.h> #include <stdint.h>
#include <string> #include <string>
#include <variant> #include <variant>
@@ -301,6 +303,7 @@
%token OP_STRENDS "STRENDS" %token OP_STRENDS "STRENDS"
%token OP_STRFIND "STRFIND" %token OP_STRFIND "STRFIND"
%token OP_STRFMT "STRFMT" %token OP_STRFMT "STRFMT"
%token OP_STRICMP "STRICMP"
%token OP_STRIN "STRIN" %token OP_STRIN "STRIN"
%token OP_STRLEN "STRLEN" %token OP_STRLEN "STRLEN"
%token OP_STRLWR "STRLWR" %token OP_STRLWR "STRLWR"
@@ -1483,7 +1486,12 @@ relocexpr_no_str:
$$.makeNumber(fix_ATan2($3, $5, $6)); $$.makeNumber(fix_ATan2($3, $5, $6));
} }
| OP_STRCMP LPAREN string COMMA string RPAREN { | OP_STRCMP LPAREN string COMMA string RPAREN {
$$.makeNumber($3.compare($5)); int result = $3.compare($5);
$$.makeNumber(result < 0 ? -1 : result > 0 ? 1 : 0);
}
| OP_STRICMP LPAREN string COMMA string RPAREN {
int result = strcasecmp($3.c_str(), $5.c_str());
$$.makeNumber(result < 0 ? -1 : result > 0 ? 1 : 0);
} }
| OP_STRFIND LPAREN string COMMA string RPAREN { | OP_STRFIND LPAREN string COMMA string RPAREN {
size_t pos = $3.find($5); size_t pos = $3.find($5);
+8
View File
@@ -5,3 +5,11 @@ assert "fire flower" === "fire" ++ " " ++ "flower"
assert "a" === "b" == 0 assert "a" === "b" == 0
assert 1 == "a" !== "b" assert 1 == "a" !== "b"
assert 1 + 2 * 3 ** "x" !== "y" == 7 assert 1 + 2 * 3 ** "x" !== "y" == 7
assert !STRICMP("hello", "hello")
assert !STRICMP("hello", "HELLO")
assert !STRICMP("HeLlO", "hElLo")
assert STRICMP("HELLO", "goodBYE") > 0
assert STRICMP("hello", "GOODbye") > 0
assert STRICMP("", "hello") < 0
assert STRICMP("hello", "") > 0