Fix convert_multibyte_charset

This commit is contained in:
Ted John 2016-11-06 18:50:54 +00:00
parent c506f2730d
commit 941d92dc2a
2 changed files with 26 additions and 15 deletions

View File

@ -18,7 +18,6 @@
#include "../common.h" #include "../common.h"
#include "../localisation/localisation.h"
#include "Math.hpp" #include "Math.hpp"
#include "Memory.hpp" #include "Memory.hpp"
#include "String.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<utf8>(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. * Returns the current string buffer as a new fire-and-forget string.
*/ */

View File

@ -19,6 +19,7 @@
#include "../core/Memory.hpp" #include "../core/Memory.hpp"
#include "../core/Path.hpp" #include "../core/Path.hpp"
#include "../core/String.hpp" #include "../core/String.hpp"
#include "../core/StringBuilder.hpp"
#include "../object/ObjectManager.h" #include "../object/ObjectManager.h"
#include "LanguagePack.h" #include "LanguagePack.h"
@ -295,31 +296,26 @@ static utf8 * convert_multibyte_charset(const char * src, int languageId)
{ {
constexpr char CODEPOINT_DOUBLEBYTE = (char)0xFF; constexpr char CODEPOINT_DOUBLEBYTE = (char)0xFF;
size_t reservedLength = (String::LengthOf(src) * 4) + 1; auto sb = StringBuilder(64);
utf8 * buffer = Memory::Allocate<utf8>(reservedLength);
utf8 * dst = buffer;
for (const char * ch = src; *ch != 0;) for (const char * ch = src; *ch != 0;)
{ {
if (*ch == CODEPOINT_DOUBLEBYTE) if (*ch == CODEPOINT_DOUBLEBYTE)
{ {
ch++; ch++;
char a = *ch++; uint8 a = *ch++;
char b = *ch++; uint8 b = *ch++;
uint16 codepoint = (uint16)((a << 8) | b); wchar_t codepoint16 = (wchar_t)((a << 8) | b);
codepoint = convert_specific_language_character_to_unicode(languageId, codepoint); codepoint16 = convert_specific_language_character_to_unicode(languageId, codepoint16);
dst = String::WriteCodepoint(dst, codepoint); sb.Append(codepoint16);
} }
else else
{ {
dst = String::WriteCodepoint(dst, *ch++); codepoint_t codepoint = (uint8)*ch++;
sb.Append(codepoint);
} }
} }
*dst++ = 0; return sb.StealString();
size_t actualLength = (size_t)(dst - buffer);
buffer = Memory::Reallocate(buffer, actualLength);
return buffer;
} }
static bool rct2_language_is_multibyte_charset(int languageId) static bool rct2_language_is_multibyte_charset(int languageId)