From fc363e020e41e4c45facb0c7f4a2f468143cca75 Mon Sep 17 00:00:00 2001 From: Matthew Mott Date: Wed, 3 Mar 2021 21:07:36 +0000 Subject: [PATCH] Rename string_compare_nocase to string::icmp --- libs/shaderlib.h | 7 +----- libs/string/string.h | 52 +++++++++++++++++++------------------------- test/Basic.cpp | 12 +++++----- 3 files changed, 29 insertions(+), 42 deletions(-) diff --git a/libs/shaderlib.h b/libs/shaderlib.h index 0916494044..9f24b2966e 100644 --- a/libs/shaderlib.h +++ b/libs/shaderlib.h @@ -15,11 +15,6 @@ inline bool shader_equal(const std::string& shader, const std::string& other) return string_equal_nocase(shader.c_str(), other.c_str()); } -inline bool shader_equal_n(const char* shader, const char* other, std::size_t n) -{ - return string_equal_nocase_n(shader, other, n); -} - inline bool shader_valid(const char* shader) { return string_is_ascii(shader) @@ -50,7 +45,7 @@ inline const std::string& texdef_name_default() namespace scene { -/** +/** * greebo: This replaces the shader of the visited face/patch with * if the face is textured with and increases the given . */ diff --git a/libs/string/string.h b/libs/string/string.h index c35790e6b0..8b10843c49 100644 --- a/libs/string/string.h +++ b/libs/string/string.h @@ -25,18 +25,14 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include -/// \brief Returns true if [\p string, \p string + \p n) is lexicographically equal to [\p other, \p other + \p n). -/// O(n) -inline bool string_equal_n(const char* string, const char* other, std::size_t n) +namespace string { - return std::strncmp(string, other, n) == 0; -} /// \brief Returns <0 if \p string is lexicographically less than \p other after converting both to lower-case. /// Returns >0 if \p string is lexicographically greater than \p other after converting both to lower-case. /// Returns 0 if \p string is lexicographically equal to \p other after converting both to lower-case. /// O(n) -inline int string_compare_nocase(const char* string, const char* other) +inline int icmp(const char* string, const char* other) { #ifdef WIN32 return _stricmp(string, other); @@ -45,6 +41,24 @@ inline int string_compare_nocase(const char* string, const char* other) #endif } +/// Case-insensitive comparison functor for use with data structures +struct ILess +{ + bool operator() (const std::string& lhs, const std::string& rhs) const + { + return icmp(lhs.c_str(), rhs.c_str()) < 0; + } +}; + +} + +/// \brief Returns true if [\p string, \p string + \p n) is lexicographically equal to [\p other, \p other + \p n). +/// O(n) +inline bool string_equal_n(const char* string, const char* other, std::size_t n) +{ + return std::strncmp(string, other, n) == 0; +} + /// \brief Returns <0 if [\p string, \p string + \p n) is lexicographically less than [\p other, \p other + \p n). /// Returns >0 if [\p string, \p string + \p n) is lexicographically greater than [\p other, \p other + \p n). /// Returns 0 if [\p string, \p string + \p n) is lexicographically equal to [\p other, \p other + \p n). @@ -64,15 +78,7 @@ inline int string_compare_nocase_n(const char* string, const char* other, std::s /// O(n) inline bool string_equal_nocase(const char* string, const char* other) { - return string_compare_nocase(string, other) == 0; -} - -/// \brief Returns true if [\p string, \p string + \p n) is lexicographically equal to [\p other, \p other + \p n). -/// Treats all ascii characters as lower-case during comparisons. -/// O(n) -inline bool string_equal_nocase_n(const char* string, const char* other, std::size_t n) -{ - return string_compare_nocase_n(string, other, n) == 0; + return string::icmp(string, other) == 0; } /// \brief Returns true if \p string is lexicographically less than \p other. @@ -80,19 +86,5 @@ inline bool string_equal_nocase_n(const char* string, const char* other, std::si /// O(n) inline bool string_less_nocase(const char* string, const char* other) { - return string_compare_nocase(string, other) < 0; -} - -namespace string -{ - -/// Case-insensitive comparison functor for use with data structures -struct ILess -{ - bool operator() (const std::string& lhs, const std::string& rhs) const - { - return string_compare_nocase(lhs.c_str(), rhs.c_str()) < 0; - } -}; - + return string::icmp(string, other) < 0; } \ No newline at end of file diff --git a/test/Basic.cpp b/test/Basic.cpp index 7f3df6c1a2..4f3e3facec 100644 --- a/test/Basic.cpp +++ b/test/Basic.cpp @@ -12,13 +12,13 @@ namespace test TEST(BasicTest, StringCompareNoCase) { - EXPECT_EQ(string_compare_nocase("blah", "blah"), 0); - EXPECT_EQ(string_compare_nocase("blah", "BLAH"), 0); - EXPECT_EQ(string_compare_nocase("MiXeD", "mIxED"), 0); + EXPECT_EQ(string::icmp("blah", "blah"), 0); + EXPECT_EQ(string::icmp("blah", "BLAH"), 0); + EXPECT_EQ(string::icmp("MiXeD", "mIxED"), 0); - EXPECT_EQ(string_compare_nocase("a", "b"), -1); - EXPECT_EQ(string_compare_nocase("b", "a"), 1); - EXPECT_EQ(string_compare_nocase("baaaaa", "aaaaa"), 1); + EXPECT_EQ(string::icmp("a", "b"), -1); + EXPECT_EQ(string::icmp("b", "a"), 1); + EXPECT_EQ(string::icmp("baaaaa", "aaaaa"), 1); } TEST(BasicTest, StringILessFunctor)