From 941d92dc2af064905988f3371b2d994ffa992652 Mon Sep 17 00:00:00 2001 From: Ted John Date: Sun, 6 Nov 2016 18:50:54 +0000 Subject: [PATCH] Fix convert_multibyte_charset --- src/core/StringBuilder.hpp | 17 ++++++++++++++++- src/localisation/language.cpp | 24 ++++++++++-------------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/core/StringBuilder.hpp b/src/core/StringBuilder.hpp index a0fcd6ec72..032a1876e2 100644 --- a/src/core/StringBuilder.hpp +++ b/src/core/StringBuilder.hpp @@ -18,7 +18,6 @@ #include "../common.h" -#include "../localisation/localisation.h" #include "Math.hpp" #include "Memory.hpp" #include "String.hpp" @@ -121,6 +120,22 @@ public: } } + /** + * Resets the StringBuilder and returns the working buffer (resized to the string size). + */ + utf8 * StealString() + { + utf8 * result = _buffer; + result = Memory::ReallocateArray(result, _length + 1); + result[_length] = 0; + + _length = 0; + _capacity = 0; + _buffer = nullptr; + + return result; + } + /** * Returns the current string buffer as a new fire-and-forget string. */ diff --git a/src/localisation/language.cpp b/src/localisation/language.cpp index 14eb9f7e12..1cbb0d0af4 100644 --- a/src/localisation/language.cpp +++ b/src/localisation/language.cpp @@ -19,6 +19,7 @@ #include "../core/Memory.hpp" #include "../core/Path.hpp" #include "../core/String.hpp" +#include "../core/StringBuilder.hpp" #include "../object/ObjectManager.h" #include "LanguagePack.h" @@ -295,31 +296,26 @@ static utf8 * convert_multibyte_charset(const char * src, int languageId) { constexpr char CODEPOINT_DOUBLEBYTE = (char)0xFF; - size_t reservedLength = (String::LengthOf(src) * 4) + 1; - utf8 * buffer = Memory::Allocate(reservedLength); - utf8 * dst = buffer; + auto sb = StringBuilder(64); for (const char * ch = src; *ch != 0;) { if (*ch == CODEPOINT_DOUBLEBYTE) { ch++; - char a = *ch++; - char b = *ch++; - uint16 codepoint = (uint16)((a << 8) | b); + uint8 a = *ch++; + uint8 b = *ch++; + wchar_t codepoint16 = (wchar_t)((a << 8) | b); - codepoint = convert_specific_language_character_to_unicode(languageId, codepoint); - dst = String::WriteCodepoint(dst, codepoint); + codepoint16 = convert_specific_language_character_to_unicode(languageId, codepoint16); + sb.Append(codepoint16); } else { - dst = String::WriteCodepoint(dst, *ch++); + codepoint_t codepoint = (uint8)*ch++; + sb.Append(codepoint); } } - *dst++ = 0; - size_t actualLength = (size_t)(dst - buffer); - buffer = Memory::Reallocate(buffer, actualLength); - - return buffer; + return sb.StealString(); } static bool rct2_language_is_multibyte_charset(int languageId)