From 6e3639de9e4a6b984dbcdaa15f109b8256cf7fa6 Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Mon, 18 Mar 2013 02:51:11 +0900 Subject: [PATCH] Avoid creating new String object when comparing --- taglib/toolkit/tstring.cpp | 25 +++++++++++++++++++++---- taglib/toolkit/tstring.h | 14 +++++++++++++- tests/test_string.cpp | 2 +- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp index 2b723100..e88b70f7 100644 --- a/taglib/toolkit/tstring.cpp +++ b/taglib/toolkit/tstring.cpp @@ -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(*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); diff --git a/taglib/toolkit/tstring.h b/taglib/toolkit/tstring.h index ee81f999..c502e56c 100644 --- a/taglib/toolkit/tstring.h +++ b/taglib/toolkit/tstring.h @@ -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. diff --git a/tests/test_string.cpp b/tests/test_string.cpp index 1e37d7a2..c67fa41e 100644 --- a/tests/test_string.cpp +++ b/tests/test_string.cpp @@ -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() {