Merge pull request #426 from TsudaKageyu/string-cmp

Added some operators to compare String to string literals.
This commit is contained in:
Lukáš Lalinský 2014-08-12 18:15:37 +02:00
commit 8765d40c2c
3 changed files with 63 additions and 4 deletions

View File

@ -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<uchar>(*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)

View File

@ -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.
*/

View File

@ -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');