1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-20 21:43:06 +01:00

Rename String::Convert() to String::ConvertToUtf8()

This commit is contained in:
Gymnasiast
2022-05-01 17:57:51 +02:00
parent 22c44fac71
commit 6fcfd481ab
4 changed files with 8 additions and 55 deletions

View File

@@ -666,49 +666,9 @@ namespace String
throw std::runtime_error("Unsupported code page: " + std::to_string(codePage));
}
}
static std::string CodePageFromUnicode(icu::UnicodeString src, int32_t dstCodePage)
{
UConverter* conv;
UErrorCode status = U_ZERO_ERROR;
const char* codepage = GetIcuCodePage(dstCodePage);
conv = ucnv_open(codepage, &status);
if (U_FAILURE(status))
{
log_error("ICU error: %s", u_errorName(status));
return nullptr;
}
// Allocate buffer to convert to.
int8_t char_size = ucnv_getMaxCharSize(conv);
std::string buffer(char_size * src.length(), '\0');
char* buffer_limit = &buffer[0] + (char_size * src.length());
// Ready the source string as well...
const char16_t* source = src.getTerminatedBuffer();
const char16_t* source_limit = source + src.length();
// Convert the lot.
char* buffer_target = &buffer[0];
ucnv_fromUnicode(
conv, &buffer_target, buffer_limit, static_cast<const UChar**>(&source), source_limit, nullptr, true, &status);
if (U_FAILURE(status))
{
log_error("ICU error: %s", u_errorName(status));
return nullptr;
}
ucnv_close(conv);
return buffer;
}
#endif
std::string Convert(std::string_view src, int32_t srcCodePage, int32_t dstCodePage)
std::string ConvertToUtf8(std::string_view src, int32_t srcCodePage)
{
#ifdef _WIN32
// Convert from source code page to UTF-16
@@ -724,9 +684,9 @@ namespace String
std::string dst;
{
int srcLen = static_cast<int>(u16.size());
int sizeReq = WideCharToMultiByte(dstCodePage, 0, u16.data(), srcLen, nullptr, 0, nullptr, nullptr);
int sizeReq = WideCharToMultiByte(CODE_PAGE::CP_UTF8, 0, u16.data(), srcLen, nullptr, 0, nullptr, nullptr);
dst = std::string(sizeReq, 0);
WideCharToMultiByte(dstCodePage, 0, u16.data(), srcLen, dst.data(), sizeReq, nullptr, nullptr);
WideCharToMultiByte(CODE_PAGE::CP_UTF8, 0, u16.data(), srcLen, dst.data(), sizeReq, nullptr, nullptr);
}
return dst;
@@ -735,14 +695,7 @@ namespace String
icu::UnicodeString convertString(src.data(), codepage);
std::string result;
if (dstCodePage == CODE_PAGE::CP_UTF8)
{
convertString.toUTF8String(result);
}
else
{
result = CodePageFromUnicode(convertString, dstCodePage);
}
convertString.toUTF8String(result);
return result;
#endif