Simplified some functions in tstring.cpp by replacing with standard functions.

This commit is contained in:
Tsuda Kageyu 2014-04-01 18:03:19 +09:00
parent d93b4af8ca
commit 5199a3cbb6

View File

@ -37,6 +37,7 @@
#include <iostream>
#include <cstring>
#include <cwchar>
#ifdef HAVE_STD_CODECVT
# include <codecvt>
@ -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<int>(::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<uchar>(*s))
if(*it != static_cast<uchar>(*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<ushort>(s[i]));
}
else {
::memcpy(&(*d->data)[0], s, length * sizeof(wchar_t));
::wmemcpy(&(*d->data)[0], s, length);
}
}
}