diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp index 8287244b..5577feef 100644 --- a/taglib/toolkit/tstring.cpp +++ b/taglib/toolkit/tstring.cpp @@ -607,12 +607,39 @@ const TagLib::wchar &String::operator[](int i) const bool String::operator==(const String &s) const { - return d == s.d || d->data == s.d->data; + return (d == s.d || d->data == s.d->data); } bool String::operator!=(const String &s) const { - return !operator==(s); + return !(*this == s); +} + +bool String::operator==(const char *s) const +{ + const wchar_t *p = toCWString(); + + while(*p != L'\0' || *s != '\0') + { + if(*p++ != static_cast(*s++)) + return false; + } + return true; +} + +bool String::operator!=(const char *s) const +{ + return !(*this == s); +} + +bool String::operator==(const wchar_t *s) const +{ + return (::wcscmp(toCWString(), s) == 0); +} + +bool String::operator!=(const wchar_t *s) const +{ + return !(*this == s); } String &String::operator+=(const String &s) diff --git a/taglib/toolkit/tstring.h b/taglib/toolkit/tstring.h index 6d3257eb..ab046d8e 100644 --- a/taglib/toolkit/tstring.h +++ b/taglib/toolkit/tstring.h @@ -397,6 +397,30 @@ namespace TagLib { */ bool operator!=(const String &s) const; + /*! + * Compares each character of the String with each character of \a s and + * returns true if the strings match. + */ + bool operator==(const char *s) const; + + /*! + * Compares each character of the String with each character of \a s and + * returns false if the strings match. + */ + bool operator!=(const char *s) const; + + /*! + * Compares each character of the String with each character of \a s and + * returns true if the strings match. + */ + bool operator==(const wchar_t *s) const; + + /*! + * Compares each character of the String with each character of \a s and + * returns false if the strings match. + */ + bool operator!=(const wchar_t *s) const; + /*! * Appends \a s to the end of the String. */ diff --git a/tests/test_string.cpp b/tests/test_string.cpp index 40a21576..2d59bba2 100644 --- a/tests/test_string.cpp +++ b/tests/test_string.cpp @@ -56,6 +56,14 @@ public: char str[] = "taglib string"; CPPUNIT_ASSERT(strcmp(s.toCString(), str) == 0); + CPPUNIT_ASSERT(s == "taglib string"); + CPPUNIT_ASSERT(s != "taglib STRING"); + CPPUNIT_ASSERT(s != "taglib"); + CPPUNIT_ASSERT(s != "taglib string taglib"); + CPPUNIT_ASSERT(s == L"taglib string"); + CPPUNIT_ASSERT(s != L"taglib STRING"); + CPPUNIT_ASSERT(s != L"taglib"); + CPPUNIT_ASSERT(s != L"taglib string taglib"); String unicode("José Carlos", String::UTF8); CPPUNIT_ASSERT(strcmp(unicode.toCString(), "Jos\xe9 Carlos") == 0); @@ -73,8 +81,8 @@ public: String unicode2(unicode.to8Bit(true), String::UTF8); CPPUNIT_ASSERT(unicode == unicode2); - String unicode3(L"\u65E5\u672C\u8A9E"); - CPPUNIT_ASSERT(*(unicode3.toCWString() + 1) == L'\u672C'); + String unicode3(L"\u65E5\u672C\u8A9E"); + CPPUNIT_ASSERT(*(unicode3.toCWString() + 1) == L'\u672C'); String unicode4(L"\u65e5\u672c\u8a9e", String::UTF16BE); CPPUNIT_ASSERT(unicode4[1] == L'\u672c');