1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-16 03:23:15 +01:00

Refactor string conversion between RCT2 and UTF8

Use Win32 API for conversion on Windows for non-1252 code pages instead of built-in tables.
This commit is contained in:
Ted John
2018-04-14 18:06:49 +01:00
committed by Aaron van Geffen
parent 2512e4959c
commit deaa60f8f1
17 changed files with 343 additions and 250 deletions

View File

@@ -18,6 +18,12 @@
#include <stdexcept>
#include <vector>
#ifdef _WIN32
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#include "../localisation/Language.h"
#include "../util/Util.h"
@@ -517,4 +523,32 @@ namespace String
size_t stringLength = endSubstr - startSubstr + 1;
return std::string(startSubstr, stringLength);
}
std::string Convert(const std::string_view& src, sint32 srcCodePage, sint32 dstCodePage)
{
#ifdef _WIN32
// Convert from source code page to UTF-16
std::wstring u16;
{
int srcLen = (int)src.size();
int sizeReq = MultiByteToWideChar(srcCodePage, 0, src.data(), srcLen, nullptr, 0);
u16 = std::wstring(sizeReq, 0);
MultiByteToWideChar(srcCodePage, 0, src.data(), srcLen, u16.data(), sizeReq);
}
// Convert from UTF-16 to destination code page
std::string dst;
{
int srcLen = (int)u16.size();
int sizeReq = WideCharToMultiByte(dstCodePage, 0, u16.data(), srcLen, nullptr, 0, nullptr, nullptr);
dst = std::string(sizeReq, 0);
WideCharToMultiByte(dstCodePage, 0, u16.data(), srcLen, dst.data(), sizeReq, nullptr, nullptr);
}
return dst;
#else
STUB();
return std::string(src);
#endif
}
}