1
0
Fork 0

Merge pull request #2889 from justanotheranonymoususer/more-str-functions

Case insensitive string functions
This commit is contained in:
Duncan Ogilvie 2022-08-05 14:49:21 +02:00 committed by GitHub
commit 7dcfab2a82
Signed by: GitHub
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 2 deletions

View File

@ -156,7 +156,9 @@ void ExpressionFunctions::Init()
ExpressionFunctions::Register("utf8", ValueTypeString, { ValueTypeNumber }, Exprfunc::utf8, nullptr);
ExpressionFunctions::Register("utf16", ValueTypeString, { ValueTypeNumber }, Exprfunc::utf16, nullptr);
ExpressionFunctions::Register("strstr", ValueTypeNumber, { ValueTypeString, ValueTypeString }, Exprfunc::strstr, nullptr);
ExpressionFunctions::Register("stristr", ValueTypeNumber, { ValueTypeString, ValueTypeString }, Exprfunc::stristr, nullptr);
ExpressionFunctions::Register("streq", ValueTypeNumber, { ValueTypeString, ValueTypeString }, Exprfunc::streq, nullptr);
ExpressionFunctions::Register("strieq", ValueTypeNumber, { ValueTypeString, ValueTypeString }, Exprfunc::strieq, nullptr);
ExpressionFunctions::Register("strlen", ValueTypeNumber, { ValueTypeString }, Exprfunc::strlen, nullptr);
}

View File

@ -13,6 +13,8 @@
#include "exhandlerinfo.h"
#include <vector>
#include <regex>
#include <string>
#include <cctype>
/// <summary>
/// Creates an owning ExpressionValue string
@ -600,22 +602,52 @@ namespace Exprfunc
bool streq(ExpressionValue* result, int argc, const ExpressionValue* argv, void* userdata)
{
assert(argc == 1);
assert(argc == 2);
assert(argv[0].type == ValueTypeString);
assert(argv[1].type == ValueTypeString);
*result = ValueNumber(::strcmp(argv[0].string.ptr, argv[1].string.ptr) == 0);
return true;
}
bool strieq(ExpressionValue* result, int argc, const ExpressionValue* argv, void* userdata)
{
assert(argc == 2);
assert(argv[0].type == ValueTypeString);
assert(argv[1].type == ValueTypeString);
*result = ValueNumber(::stricmp(argv[0].string.ptr, argv[1].string.ptr) == 0);
return true;
}
bool strstr(ExpressionValue* result, int argc, const ExpressionValue* argv, void* userdata)
{
assert(argc == 1);
assert(argc == 2);
assert(argv[0].type == ValueTypeString);
assert(argv[1].type == ValueTypeString);
*result = ValueNumber(::strstr(argv[0].string.ptr, argv[1].string.ptr) != nullptr);
return true;
}
bool stristr(ExpressionValue* result, int argc, const ExpressionValue* argv, void* userdata)
{
assert(argc == 2);
assert(argv[0].type == ValueTypeString);
assert(argv[1].type == ValueTypeString);
size_t len1 = ::strlen(argv[0].string.ptr);
size_t len2 = ::strlen(argv[1].string.ptr);
auto it = std::search(
argv[0].string.ptr, argv[0].string.ptr + len1,
argv[1].string.ptr, argv[1].string.ptr + len2,
[](char ch1, char ch2) { return std::toupper(ch1) == std::toupper(ch2); }
);
*result = ValueNumber(it != argv[0].string.ptr + len1);
return true;
}
bool strlen(ExpressionValue* result, int argc, const ExpressionValue* argv, void* userdata)
{
assert(argc == 1);

View File

@ -87,7 +87,9 @@ namespace Exprfunc
duint exinfo(duint index);
bool streq(ExpressionValue* result, int argc, const ExpressionValue* argv, void* userdata);
bool strieq(ExpressionValue* result, int argc, const ExpressionValue* argv, void* userdata);
bool strstr(ExpressionValue* result, int argc, const ExpressionValue* argv, void* userdata);
bool stristr(ExpressionValue* result, int argc, const ExpressionValue* argv, void* userdata);
bool strlen(ExpressionValue* result, int argc, const ExpressionValue* argv, void* userdata);
bool utf16(ExpressionValue* result, int argc, const ExpressionValue* argv, void* userdata);
bool utf8(ExpressionValue* result, int argc, const ExpressionValue* argv, void* userdata);