Kaynağa Gözat

Added comparison operators between strings and char const*

legacy
Benlitz benlitz 11 yıl önce
ebeveyn
işleme
e7b0c85e0f
2 değiştirilmiş dosya ile 56 ekleme ve 0 silme
  1. +31
    -0
      src/lol/base/string.h
  2. +25
    -0
      test/unit/string.cpp

+ 31
- 0
src/lol/base/string.h Dosyayı Görüntüle

@@ -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__


+ 25
- 0
test/unit/string.cpp Dosyayı Görüntüle

@@ -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";


Yükleniyor…
İptal
Kaydet