From 5199a3cbb6cee8b8a8bc34d8456f64758efdeb36 Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Tue, 1 Apr 2014 18:03:19 +0900 Subject: [PATCH] Simplified some functions in tstring.cpp by replacing with standard functions. --- taglib/toolkit/tstring.cpp | 51 +++++++++++--------------------------- 1 file changed, 14 insertions(+), 37 deletions(-) diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp index daf9c0eb..3367f41e 100644 --- a/taglib/toolkit/tstring.cpp +++ b/taglib/toolkit/tstring.cpp @@ -37,6 +37,7 @@ #include #include +#include #ifdef HAVE_STD_CODECVT # include @@ -478,49 +479,27 @@ ByteVector String::data(Type t) const int String::toInt(bool *ok) const { - int value = 0; - - const size_t size = d->data->size(); - const bool negative = size > 0 && (*d->data)[0] == '-'; - const size_t start = negative ? 1 : 0; - - size_t i = start; - for(; i < size && (*d->data)[i] >= '0' && (*d->data)[i] <= '9'; i++) - value = value * 10 + ((*d->data)[i] - '0'); - - if(negative) - value = value * -1; + const wchar_t *begin = d->data->c_str(); + wchar_t *end; + const int value = static_cast(::wcstol(begin, &end, 10)); + // Has wcstol() consumed the entire string? if(ok) - *ok = (size > start && i == size); + *ok = (end > begin && *end == L'\0'); return value; } String String::stripWhiteSpace() const { - std::wstring::const_iterator begin = d->data->begin(); - std::wstring::const_iterator end = d->data->end(); + static const wchar_t *WhiteSpaceChars = L"\t\n\f\r "; - while(begin != end && - (*begin == '\t' || *begin == '\n' || *begin == '\f' || - *begin == '\r' || *begin == ' ')) - { - ++begin; - } + const size_t pos1 = d->data->find_first_not_of(WhiteSpaceChars); + if(pos1 == std::wstring::npos) + return String::null; - if(begin == end) - return null; - - // There must be at least one non-whitespace character here for us to have - // gotten this far, so we should be safe not doing bounds checking. - - do { - --end; - } while(*end == '\t' || *end == '\n' || - *end == '\f' || *end == '\r' || *end == ' '); - - return String(std::wstring(begin, end + 1)); + const size_t pos2 = d->data->find_last_not_of(WhiteSpaceChars); + return substr(pos1, pos2 - pos1 + 1); } bool String::isLatin1() const @@ -565,10 +544,8 @@ bool String::operator==(const String &s) const bool String::operator==(const char *s) const { for(std::wstring::const_iterator it = d->data->begin(); it != d->data->end(); it++) { - if(*it != static_cast(*s)) + if(*it != static_cast(*s++)) return false; - - s++; } return true; @@ -736,7 +713,7 @@ void String::copyFromUTF16(const wchar_t *s, size_t length, Type t) (*d->data)[i] = Utils::byteSwap(static_cast(s[i])); } else { - ::memcpy(&(*d->data)[0], s, length * sizeof(wchar_t)); + ::wmemcpy(&(*d->data)[0], s, length); } } }