diff --git a/src/lol/base/string.h b/src/lol/base/string.h index 1f26d6d0..cb795b7a 100644 --- a/src/lol/base/string.h +++ b/src/lol/base/string.h @@ -197,6 +197,27 @@ public: return !(*this == s); } + + inline bool operator ==(char const* sz) const + { + int i; + for (i = 0; i < this->m_count; ++i) + { + if (i < this->m_count - 1 && sz[i] == '\0') + return false; + + if ((*this)[i] != sz[i]) + return false; + } + + return true; + } + + inline bool operator !=(char const* sz) const + { + return !(*this == sz); + } + #ifdef __GNUC__ # define LOL_FMT_ATTR(n, p) __attribute__((format(printf, n, p))) #else @@ -207,6 +228,16 @@ public: static String Printf(char const *format, va_list ap); }; +inline bool operator ==(char const* sz, String const &s) +{ + return s == sz; +} + +inline bool operator !=(char const* sz, String const &s) +{ + return s != sz; +} + } /* namespace lol */ #endif // __LOL_BASE_STRING_H__ diff --git a/test/unit/string.cpp b/test/unit/string.cpp index 37c45e74..7ec25a5b 100644 --- a/test/unit/string.cpp +++ b/test/unit/string.cpp @@ -123,6 +123,31 @@ LOLUNIT_FIXTURE(StringTest) LOLUNIT_ASSERT(!(s1 != s3)); } + LOLUNIT_TEST(StringCharsEqual) + { + char const* sz = "abc"; + String s1("abc"); + String s2("ab"); + + LOLUNIT_ASSERT(s1 == sz); + LOLUNIT_ASSERT(sz == s1); + LOLUNIT_ASSERT(!(s2 == sz)); + LOLUNIT_ASSERT(!(sz == s2)); + } + + LOLUNIT_TEST(StringCharsDifferent) + { + char const* sz = "abc"; + String s1("ab"); + String s2("abc"); + + LOLUNIT_ASSERT(s1 != sz); + LOLUNIT_ASSERT(sz != s1); + LOLUNIT_ASSERT(!(s2 != sz)); + LOLUNIT_ASSERT(!(sz != s2)); + } + + LOLUNIT_TEST(StringPrintf) { String s1 = "3a";