Skip to content

Commit

Permalink
Rename string_compare_nocase to string::icmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Mott committed Mar 3, 2021
1 parent 258ad42 commit fc363e0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 42 deletions.
7 changes: 1 addition & 6 deletions libs/shaderlib.h
Expand Up @@ -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)
Expand Down Expand Up @@ -50,7 +45,7 @@ inline const std::string& texdef_name_default()
namespace scene
{

/**
/**
* greebo: This replaces the shader of the visited face/patch with <replace>
* if the face is textured with <find> and increases the given <counter>.
*/
Expand Down
52 changes: 22 additions & 30 deletions libs/string/string.h
Expand Up @@ -25,18 +25,14 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#include <cstring>

/// \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);
Expand All @@ -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).
Expand All @@ -64,35 +78,13 @@ 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.
/// Treats all ascii characters as lower-case during comparisons.
/// 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;
}
12 changes: 6 additions & 6 deletions test/Basic.cpp
Expand Up @@ -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)
Expand Down

0 comments on commit fc363e0

Please sign in to comment.