From ee5643c31827ea3d5bb8c9bc835ea8f3165c1a92 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sun, 1 Feb 2026 16:57:36 -0600 Subject: [PATCH] StringUtil: Add UTF-16BE decoder class to implement UTF16BEToUTF8. --- Source/Core/Common/StringUtil.cpp | 34 ++++++++++++++----------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/Source/Core/Common/StringUtil.cpp b/Source/Core/Common/StringUtil.cpp index e6cede9f1d..c5d707a361 100644 --- a/Source/Core/Common/StringUtil.cpp +++ b/Source/Core/Common/StringUtil.cpp @@ -407,7 +407,7 @@ static constexpr bool IsSurrogateCodePoint(char32_t code_point) return (code_point & 0xf800u) == UNICODE_HIGH_SURROGATE; } -template +template class CodeUnitReader { public: @@ -429,7 +429,7 @@ protected: constexpr auto PeekCodeUnit() const { assert(RemainingCodeUnits() > 0); - return *m_ptr; + return TransformFunction(*m_ptr); } constexpr void AdvanceReader() { ++m_ptr; } @@ -598,11 +598,11 @@ private: } }; -template CharType> -class UTF16Decoder : public CodeUnitReader +template CharType, auto TransformFunction = std::identity{}> +class UTF16Decoder : public CodeUnitReader { public: - using CodeUnitReader::CodeUnitReader; + using CodeUnitReader::CodeUnitReader; constexpr char32_t operator()() { @@ -634,6 +634,9 @@ public: } }; +template +using UTF16BEDecoder = UTF16Decoder>; + class UTF16Encoder { public: @@ -747,14 +750,6 @@ std::string UTF8ToSHIFTJIS(std::string_view input) return UTF16ToCP(CODEPAGE_SHIFT_JIS, UTF8ToWString(input)); } -std::string UTF16BEToUTF8(const char16_t* str, size_t max_size) -{ - const char16_t* str_end = std::find(str, str + max_size, '\0'); - std::wstring result(static_cast(str_end - str), '\0'); - std::transform(str, str_end, result.begin(), static_cast(Common::swap16)); - return WStringToUTF8(result); -} - #else template @@ -831,12 +826,6 @@ std::string UTF8ToSHIFTJIS(std::string_view input) return CodeTo("SJIS", "UTF-8", input); } -std::string UTF16BEToUTF8(const char16_t* str, size_t max_size) -{ - const char16_t* str_end = std::find(str, str + max_size, '\0'); - return CodeToUTF8("UTF-16BE", std::u16string_view(str, static_cast(str_end - str))); -} - #endif std::string CP1252ToUTF8(std::string_view input) @@ -854,6 +843,13 @@ std::wstring UTF8ToWString(std::string_view input) return ReEncodeString, WCharEncoder, wchar_t>(input); } +std::string UTF16BEToUTF8(const char16_t* str, size_t max_size) +{ + const char16_t* str_end = std::find(str, str + max_size, '\0'); + return ReEncodeString, UTF8Encoder, char>( + std::basic_string_view{str, str_end}); +} + std::string UTF16ToUTF8(std::u16string_view input) { return ReEncodeString, UTF8Encoder, char>(input);