From 13a258d9ed101109f4c516c3b4935d0ca9670a2c Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Thu, 19 Nov 2015 14:42:36 +0900 Subject: [PATCH] Backport shorter versions of some functions from taglib2. --- taglib/toolkit/tstring.cpp | 106 +++++++++++++++---------------------- 1 file changed, 42 insertions(+), 64 deletions(-) diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp index 3255f564..0f051e6e 100644 --- a/taglib/toolkit/tstring.cpp +++ b/taglib/toolkit/tstring.cpp @@ -26,7 +26,7 @@ // This class assumes that std::basic_string has a contiguous and null-terminated buffer. #ifdef HAVE_CONFIG_H -#include +# include #endif #include "tstring.h" @@ -36,6 +36,8 @@ #include "tutils.h" #include +#include +#include #include #include @@ -229,16 +231,12 @@ namespace TagLib { class String::StringPrivate : public RefCounter { public: - StringPrivate() - : RefCounter() - { - } + StringPrivate() : + RefCounter() {} - StringPrivate(uint n, wchar_t c) - : RefCounter() - , data(static_cast(n), c) - { - } + StringPrivate(uint n, wchar_t c) : + RefCounter(), + data(static_cast(n), c) {} /*! * Stores string in UTF-16. The byte order depends on the CPU endian. @@ -257,19 +255,19 @@ String String::null; // public members //////////////////////////////////////////////////////////////////////////////// -String::String() - : d(new StringPrivate()) +String::String() : + d(new StringPrivate()) { } -String::String(const String &s) - : d(s.d) +String::String(const String &s) : + d(s.d) { d->ref(); } -String::String(const std::string &s, Type t) - : d(new StringPrivate()) +String::String(const std::string &s, Type t) : + d(new StringPrivate()) { if(t == Latin1) copyFromLatin1(d->data, s.c_str(), s.length()); @@ -280,8 +278,8 @@ String::String(const std::string &s, Type t) } } -String::String(const wstring &s, Type t) - : d(new StringPrivate()) +String::String(const wstring &s, Type t) : + d(new StringPrivate()) { if(t == UTF16 || t == UTF16BE || t == UTF16LE) { // This looks ugly but needed for the compatibility with TagLib1.8. @@ -298,8 +296,8 @@ String::String(const wstring &s, Type t) } } -String::String(const wchar_t *s, Type t) - : d(new StringPrivate()) +String::String(const wchar_t *s, Type t) : + d(new StringPrivate()) { if(t == UTF16 || t == UTF16BE || t == UTF16LE) { // This looks ugly but needed for the compatibility with TagLib1.8. @@ -316,8 +314,8 @@ String::String(const wchar_t *s, Type t) } } -String::String(const char *s, Type t) - : d(new StringPrivate()) +String::String(const char *s, Type t) : + d(new StringPrivate()) { if(t == Latin1) copyFromLatin1(d->data, s, ::strlen(s)); @@ -328,8 +326,8 @@ String::String(const char *s, Type t) } } -String::String(wchar_t c, Type t) - : d(new StringPrivate()) +String::String(wchar_t c, Type t) : + d(new StringPrivate()) { if(t == UTF16 || t == UTF16BE || t == UTF16LE) copyFromUTF16(d->data, &c, 1, t); @@ -338,16 +336,16 @@ String::String(wchar_t c, Type t) } } -String::String(char c, Type t) - : d(new StringPrivate(1, static_cast(c))) +String::String(char c, Type t) : + d(new StringPrivate(1, static_cast(c))) { if(t != Latin1 && t != UTF8) { debug("String::String() -- char should not contain UTF16."); } } -String::String(const ByteVector &v, Type t) - : d(new StringPrivate()) +String::String(const ByteVector &v, Type t) : + d(new StringPrivate()) { if(v.isEmpty()) return; @@ -582,49 +580,29 @@ int String::toInt() const int String::toInt(bool *ok) const { - int value = 0; - - uint size = d->data.size(); - bool negative = size > 0 && d->data[0] == '-'; - uint start = negative ? 1 : 0; - uint 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; + errno = 0; + const long value = ::wcstol(begin, &end, 10); - if(ok) - *ok = (size > start && i == size); + // Has wcstol() consumed the entire string and not overflowed? + if(ok) { + *ok = (errno == 0 && end > begin && *end == L'\0'); + *ok = (*ok && value > INT_MIN && value < INT_MAX); + } - return value; -} + return static_cast(value);} String String::stripWhiteSpace() const { - wstring::const_iterator begin = d->data.begin(); - wstring::const_iterator end = d->data.end(); - - while(begin != end && - (*begin == '\t' || *begin == '\n' || *begin == '\f' || - *begin == '\r' || *begin == ' ')) - { - ++begin; - } - - 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. + static const wchar_t *WhiteSpaceChars = L"\t\n\f\r "; - do { - --end; - } while(*end == '\t' || *end == '\n' || - *end == '\f' || *end == '\r' || *end == ' '); + const size_t pos1 = d->data.find_first_not_of(WhiteSpaceChars); + if(pos1 == std::wstring::npos) + return String(); - return String(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 -- 2.40.0