Avoid creating new String object when comparing

This commit is contained in:
Tsuda Kageyu 2013-03-18 02:51:11 +09:00
parent 0792eedd12
commit 6e3639de9e
3 changed files with 35 additions and 6 deletions

View File

@ -330,9 +330,10 @@ String &String::append(const String &s)
String String::upper() const
{
String s;
static const int shift = 'A' - 'a';
static int shift = 'A' - 'a';
String s;
s.d->data.reserve(d->data.size());
for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); ++it) {
if(*it >= 'a' && *it <= 'z')
@ -537,7 +538,24 @@ const TagLib::wchar &String::operator[](size_t 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 char *s) const
{
for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); it++) {
if(*it != static_cast<uchar>(*s))
return false;
s++;
}
return true;
}
bool String::operator==(const wchar_t *s) const
{
return (d->data == s);
}
bool String::operator!=(const String &s) const
@ -700,7 +718,6 @@ void String::detach()
// private members
////////////////////////////////////////////////////////////////////////////////
void String::copyFromLatin1(const char *s, size_t length)
{
d->data.resize(length);

View File

@ -342,11 +342,23 @@ namespace TagLib {
const wchar &operator[](size_t i) const;
/*!
* Compares each character of the String with each character of \a s and
* Compares each character of the String with each character in \a s and
* returns true if the strings match.
*/
bool operator==(const String &s) const;
/*!
* Compares each character of the String with each character in \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 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.

View File

@ -116,7 +116,7 @@ public:
CPPUNIT_ASSERT_EQUAL(a, String(d, String::UTF16));
}
// this test is expected to print "TagLib: String::prepare() -
// this test is expected to print "TagLib: String::copyFromUTF16() -
// Invalid UTF16 string." on the console 3 times
void testUTF16DecodeInvalidBOM()
{