Skip to content

Commit 960662a

Browse files
committed
Fix readUnicodeString to properly convert UTF-8 to WideChar
1 parent 78d09ac commit 960662a

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

Core/GameEngine/Source/Common/System/JSONChunkInput.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
#include "Common/JSONChunkInput.h"
2222
#include "Common/NameKeyGenerator.h"
2323
#include "Common/Errors.h"
24+
#include "Common/UnicodeString.h"
25+
#include "Common/AsciiString.h"
26+
#ifdef WIN32
27+
#include <windows.h>
28+
#endif
2429

2530
JSONChunkInput::JSONChunkInput( const char* jsonData, size_t jsonSize ) :
2631
m_parserList(NULL),
@@ -343,7 +348,27 @@ UnicodeString JSONChunkInput::readUnicodeString(void)
343348
}
344349

345350
std::string str = (*m_chunkStack->data)[m_chunkStack->dataIndex++].get<std::string>();
346-
return UnicodeString(str.c_str());
351+
if (str.empty()) {
352+
return UnicodeString::TheEmptyString;
353+
}
354+
355+
#ifdef WIN32
356+
int wideLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
357+
if (wideLen <= 0) {
358+
DEBUG_CRASH(("Failed to convert UTF-8 string to wide string."));
359+
return UnicodeString::TheEmptyString;
360+
}
361+
WideChar* wideStr = (WideChar*)TheDynamicMemoryAllocator->allocBytes(wideLen * sizeof(WideChar));
362+
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, wideStr, wideLen);
363+
UnicodeString result(wideStr);
364+
TheDynamicMemoryAllocator->freeBytes(wideStr);
365+
return result;
366+
#else
367+
AsciiString asciiStr(str.c_str());
368+
UnicodeString result;
369+
result.translate(asciiStr);
370+
return result;
371+
#endif
347372
}
348373

349374
NameKeyType JSONChunkInput::readNameKey(void)

0 commit comments

Comments
 (0)