From 6c26009f196568a19e393b8eb1e45f0ae9567f77 Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Sat, 23 Dec 2017 02:39:09 +0100 Subject: [PATCH] Fix #6097: String::Trim wasn't taking multibyte chars into account. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extends StringTest with multibyte tests. NB: ストリング is a katakana transcription of 'string'. --- src/openrct2/core/String.cpp | 10 ++++++++-- test/tests/StringTest.cpp | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/openrct2/core/String.cpp b/src/openrct2/core/String.cpp index 9dcd88ca38..8b45f6e928 100644 --- a/src/openrct2/core/String.cpp +++ b/src/openrct2/core/String.cpp @@ -469,7 +469,10 @@ namespace String if (firstNonWhitespace != nullptr && firstNonWhitespace != str) { - size_t newStringSize = ch - firstNonWhitespace; + // Take multibyte characters into account: use the last byte of the + // current character. + size_t newStringSize = (nextCh - 1) - firstNonWhitespace; + #ifdef DEBUG size_t currentStringSize = String::SizeOf(str); Guard::Assert(newStringSize < currentStringSize, GUARD_LINE); @@ -523,7 +526,10 @@ namespace String { startSubstr = ch; } - endSubstr = ch; + + // Take multibyte characters into account: move pointer towards + // the last byte of the current character. + endSubstr = nextCh - 1; } ch = nextCh; } diff --git a/test/tests/StringTest.cpp b/test/tests/StringTest.cpp index 7280eb6200..5b481c5535 100644 --- a/test/tests/StringTest.cpp +++ b/test/tests/StringTest.cpp @@ -16,6 +16,8 @@ INSTANTIATE_TEST_CASE_P(TrimData, StringTest, testing::Values( TCase("string ", "string"), TCase(" some string ", "some string"), TCase(" ", ""), + TCase(" ストリング", "ストリング"), + TCase("ストリング ", "ストリング"), TCase("", ""), TCase("\n", ""), TCase("\n\n\n\r\n", ""),