Add starts_with to filter expressions

This commit is contained in:
jcorporation
2022-09-27 19:45:15 +02:00
parent 512cd7b0de
commit 868a06eaf9
7 changed files with 92 additions and 19 deletions

View File

@@ -112,3 +112,32 @@ IcuCompare::IsIn(const char *haystack) const noexcept
return false;
#endif
}
bool
IcuCompare::StartsWith(const char *haystack) const noexcept
{
#ifdef HAVE_ICU_CASE_FOLD
return StringIsEqual(IcuCaseFold(haystack).c_str(),
needle.c_str(), strlen(needle.c_str()));
#elif defined(_WIN32)
if (needle == nullptr)
/* the MultiByteToWideChar() call in the constructor
has failed, so let's always fail the comparison */
return false;
try {
auto w_haystack = MultiByteToWideChar(CP_UTF8, haystack);
return FindNLSStringEx(LOCALE_NAME_INVARIANT,
FIND_STARTSWITH|NORM_IGNORECASE,
w_haystack.c_str(), -1,
needle.c_str(), -1,
nullptr,
nullptr, nullptr, 0) == 0;
} catch (...) {
/* MultiByteToWideChar() has failed */
return false;
}
#else
return strncmp(haystack, needle.c_str(), strlen(needle.c_str()));
#endif
}

View File

@@ -72,6 +72,9 @@ public:
[[gnu::pure]]
bool IsIn(const char *haystack) const noexcept;
[[gnu::pure]]
bool StartsWith(const char *haystack) const noexcept;
};
#endif