diff --git a/src/openrct2/core/String.cpp b/src/openrct2/core/String.cpp index d7e153f7fd..ef5cbba059 100644 --- a/src/openrct2/core/String.cpp +++ b/src/openrct2/core/String.cpp @@ -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(&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(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 diff --git a/src/openrct2/core/String.hpp b/src/openrct2/core/String.hpp index 182ced6c71..9238598118 100644 --- a/src/openrct2/core/String.hpp +++ b/src/openrct2/core/String.hpp @@ -119,9 +119,9 @@ namespace String std::string Trim(const std::string& s); /** - * Converts a multi-byte string from one code page to another. + * Converts a multi-byte string from one code page to UTF-8. */ - std::string Convert(std::string_view src, int32_t srcCodePage, int32_t dstCodePage); + std::string ConvertToUtf8(std::string_view src, int32_t srcCodePage); /** * Returns an uppercased version of a UTF-8 string. diff --git a/src/openrct2/localisation/Convert.cpp b/src/openrct2/localisation/Convert.cpp index 7321a7d049..489a5dc613 100644 --- a/src/openrct2/localisation/Convert.cpp +++ b/src/openrct2/localisation/Convert.cpp @@ -121,5 +121,5 @@ std::string rct2_to_utf8(std::string_view src, RCT2LanguageId languageId) } auto decoded = DecodeToMultiByte(src); - return String::Convert(decoded, codePage, CODE_PAGE::CP_UTF8); + return String::ConvertToUtf8(decoded, codePage); } diff --git a/src/openrct2/object/ObjectRepository.cpp b/src/openrct2/object/ObjectRepository.cpp index 50681d5b02..aaba30bd6e 100644 --- a/src/openrct2/object/ObjectRepository.cpp +++ b/src/openrct2/object/ObjectRepository.cpp @@ -599,7 +599,7 @@ private: } // Convert to UTF-8 filename - return String::Convert(normalisedName, CODE_PAGE::CP_1252, CODE_PAGE::CP_UTF8); + return String::ConvertToUtf8(normalisedName, CODE_PAGE::CP_1252); } else {