diff options
| author | Martin Ridgers <[email protected]> | 2021-10-25 09:39:31 +0200 |
|---|---|---|
| committer | Martin Ridgers <[email protected]> | 2021-10-25 09:48:30 +0200 |
| commit | c80bc1e4b2ea10c2e871091f996d19a18218f231 (patch) | |
| tree | f3487b5588c30167ac8278c6079bb8b334f669f7 | |
| parent | Merged main (diff) | |
| download | zen-c80bc1e4b2ea10c2e871091f996d19a18218f231.tar.xz zen-c80bc1e4b2ea10c2e871091f996d19a18218f231.zip | |
Cross-platform string comparison helper plus tests
| -rw-r--r-- | zencore/include/zencore/string.h | 13 | ||||
| -rw-r--r-- | zencore/string.cpp | 12 |
2 files changed, 25 insertions, 0 deletions
diff --git a/zencore/include/zencore/string.h b/zencore/include/zencore/string.h index 23193501c..7af27ca20 100644 --- a/zencore/include/zencore/string.h +++ b/zencore/include/zencore/string.h @@ -696,6 +696,19 @@ ForEachStrTok(const std::string_view& Str, char Delim, Fn&& Func) ////////////////////////////////////////////////////////////////////////// +inline int32_t +StrCaseCompare(const char* Lhs, const char* Rhs, int64_t Length=-1) +{ + // A helper for cross-platform case-insensitive string comparison. +#if ZEN_PLATFORM_WINDOWS + return (Length < 0) ? _stricmp(Lhs, Rhs) : _strnicmp(Lhs, Rhs, size_t(Length)); +#else + return (Length < 0) ? strcasecmp(Lhs, Rhs) : strncasecmp(Lhs, Rhs, size_t(Length)); +#endif +} + +////////////////////////////////////////////////////////////////////////// + /** * ASCII character bitset useful for fast and readable parsing * diff --git a/zencore/string.cpp b/zencore/string.cpp index 34ea0a87d..35dfcfcf3 100644 --- a/zencore/string.cpp +++ b/zencore/string.cpp @@ -951,6 +951,18 @@ TEST_CASE("string") CHECK_EQ(ToLower("TE%St"sv), "te%st"sv); } + SUBCASE("StrCaseCompare") + { + CHECK(StrCaseCompare("foo", "FoO") == 0); + CHECK(StrCaseCompare("Bar", "bAs") < 0); + CHECK(StrCaseCompare("bAr", "Bas") < 0); + CHECK(StrCaseCompare("BBr", "Bar") > 0); + CHECK(StrCaseCompare("Bbr", "BAr") > 0); + CHECK(StrCaseCompare("foo", "FoO", 3) == 0); + CHECK(StrCaseCompare("Bar", "bAs", 3) < 0); + CHECK(StrCaseCompare("BBr", "Bar", 2) > 0); + } + SUBCASE("ForEachStrTok") { const auto Tokens = "here,is,my,different,tokens"sv; |