1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-16 11:33:03 +01:00

Fix String::Trim and add test

This commit is contained in:
Ted John
2017-02-24 21:48:06 +00:00
parent 84d69b797b
commit 3b341de835
3 changed files with 38 additions and 19 deletions

View File

@@ -399,36 +399,29 @@ namespace String
codepoint_t codepoint;
const utf8 * ch = s.c_str();
const utf8 * nextCh;
const utf8 * firstNonWhitespace = nullptr;
const utf8 * lastNonWhitespace = nullptr;
const utf8 * startSubstr = nullptr;
const utf8 * endSubstr = nullptr;
while ((codepoint = GetNextCodepoint(ch, &nextCh)) != '\0')
{
bool isWhiteSpace = codepoint <= WCHAR_MAX && iswspace((wchar_t)codepoint);
if (!isWhiteSpace)
{
if (firstNonWhitespace == nullptr)
if (startSubstr == nullptr)
{
firstNonWhitespace = ch;
startSubstr = ch;
}
lastNonWhitespace = ch;
endSubstr = ch;
}
ch = nextCh;
}
if (firstNonWhitespace != nullptr &&
firstNonWhitespace != s.c_str())
{
size_t newStringSize = ch - firstNonWhitespace;
return std::string(firstNonWhitespace, newStringSize);
}
else if (lastNonWhitespace != nullptr)
{
size_t newStringSize = lastNonWhitespace - s.c_str() + 1;
return std::string(s.c_str(), newStringSize);
}
else
if (startSubstr == nullptr)
{
// String is all whitespace
return std::string();
}
size_t stringLength = endSubstr - startSubstr + 1;
return std::string(startSubstr, stringLength);
}
}