From 995bbffb8419de47edc52efb82d13b4d899eaacc Mon Sep 17 00:00:00 2001 From: Ted John Date: Wed, 29 Jun 2016 18:47:15 +0100 Subject: [PATCH] trim string entries --- src/core/String.cpp | 43 ++++++++++++++++++++++++++++++++++++++ src/core/String.hpp | 2 ++ src/object/StringTable.cpp | 9 ++++++-- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/core/String.cpp b/src/core/String.cpp index 3a2a6df7f4..edc22e9b3b 100644 --- a/src/core/String.cpp +++ b/src/core/String.cpp @@ -14,6 +14,8 @@ *****************************************************************************/ #pragma endregion +#include + extern "C" { #include "../localisation/localisation.h" @@ -218,4 +220,45 @@ namespace String { return utf8_write_codepoint(dst, codepoint); } + + utf8 * Trim(utf8 * str) + { + utf8 * firstNonWhitespace = nullptr; + utf8 * lastNonWhitespace = nullptr; + + codepoint_t codepoint; + utf8 * ch = str; + utf8 * nextCh; + while ((codepoint = GetNextCodepoint(ch, &nextCh)) != '\0') + { + if (codepoint <= WCHAR_MAX && !iswspace((wchar_t)codepoint)) + { + if (firstNonWhitespace == nullptr) + { + firstNonWhitespace = ch; + } + lastNonWhitespace = ch; + } + ch = nextCh; + } + + if (firstNonWhitespace != nullptr && + firstNonWhitespace != str) + { + size_t newStringSize = ch - firstNonWhitespace; +#if DEBUG + size_t currentStringSize = String::SizeOf(str); + assert(newStringSize < currentStringSize); +#endif + + Memory::Copy(str, firstNonWhitespace, newStringSize); + str[newStringSize] = '\0'; + } + else + { + *ch = '\0'; + } + + return str; + } } diff --git a/src/core/String.hpp b/src/core/String.hpp index ce734bc818..5a140dc1d2 100644 --- a/src/core/String.hpp +++ b/src/core/String.hpp @@ -64,4 +64,6 @@ namespace String codepoint_t GetNextCodepoint(utf8 * ptr, utf8 * * nextPtr = nullptr); codepoint_t GetNextCodepoint(const utf8 * ptr, const utf8 * * nextPtr = nullptr); utf8 * WriteCodepoint(utf8 * dst, codepoint_t codepoint); + + utf8 * Trim(utf8 * str); } diff --git a/src/object/StringTable.cpp b/src/object/StringTable.cpp index 4939aefb21..b5c46e36f9 100644 --- a/src/object/StringTable.cpp +++ b/src/object/StringTable.cpp @@ -52,13 +52,18 @@ void StringTable::Read(IStream * stream, uint8 id) StringTableEntry entry; entry.Id = id; entry.LanguageId = languageId; - entry.Text = stream->ReadString(); - if (StringIsBlank(entry.Text)) + char * win1252 = stream->ReadString(); + if (StringIsBlank(win1252)) { entry.LanguageId = RCT2_LANGUAGE_ID_BLANK; } + entry.Text = win1252_to_utf8_alloc(win1252); + Memory::Free(win1252); + + String::Trim(entry.Text); + _strings.push_back(entry); } Sort();