1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-20 06:12:57 +01:00

Trim ideographic spaces as well.

This commit is contained in:
Aaron van Geffen
2017-12-23 12:18:15 +01:00
committed by Michael Steenbeek
parent 6c26009f19
commit 4bc2ad18c4
3 changed files with 12 additions and 3 deletions

View File

@@ -447,6 +447,12 @@ namespace String
return utf8_write_codepoint(dst, codepoint);
}
bool IsWhiteSpace(codepoint_t codepoint)
{
// 0x3000 is the 'ideographic space', a 'fullwidth' character used in CJK languages.
return iswspace((wchar_t)codepoint) || codepoint == 0x3000;
}
utf8 * Trim(utf8 * str)
{
utf8 * firstNonWhitespace = nullptr;
@@ -456,7 +462,7 @@ namespace String
utf8 * nextCh;
while ((codepoint = GetNextCodepoint(ch, &nextCh)) != '\0')
{
if (codepoint <= WCHAR_MAX && !iswspace((wchar_t)codepoint))
if (codepoint <= WCHAR_MAX && !IsWhiteSpace(codepoint))
{
if (firstNonWhitespace == nullptr)
{
@@ -496,7 +502,7 @@ namespace String
const utf8 * nextCh;
while ((codepoint = GetNextCodepoint(ch, &nextCh)) != '\0')
{
if (codepoint <= WCHAR_MAX && !iswspace((wchar_t)codepoint))
if (codepoint <= WCHAR_MAX && !IsWhiteSpace(codepoint))
{
return ch;
}
@@ -519,7 +525,7 @@ namespace String
const utf8 * endSubstr = nullptr;
while ((codepoint = GetNextCodepoint(ch, &nextCh)) != '\0')
{
bool isWhiteSpace = codepoint <= WCHAR_MAX && iswspace((wchar_t)codepoint);
bool isWhiteSpace = codepoint <= WCHAR_MAX && IsWhiteSpace(codepoint);
if (!isWhiteSpace)
{
if (startSubstr == nullptr)

View File

@@ -94,6 +94,7 @@ namespace String
codepoint_t GetNextCodepoint(const utf8 * ptr, const utf8 * * nextPtr = nullptr);
utf8 * WriteCodepoint(utf8 * dst, codepoint_t codepoint);
bool IsWhiteSpace(codepoint_t codepoint);
utf8 * Trim(utf8 * str);
const utf8 * TrimStart(const utf8 * str);
utf8 * TrimStart(utf8 * buffer, size_t bufferSize, const utf8 * src);

View File

@@ -18,6 +18,8 @@ INSTANTIATE_TEST_CASE_P(TrimData, StringTest, testing::Values(
TCase(" ", ""),
TCase(" ストリング", "ストリング"),
TCase("ストリング ", "ストリング"),
TCase(" ストリング ", "ストリング"),
TCase("    ", ""),
TCase("", ""),
TCase("\n", ""),
TCase("\n\n\n\r\n", ""),