From d7fbc1ca2c91f8c47d8b6f82e6645958f03fefd5 Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Mon, 8 Apr 2019 22:34:21 +0800 Subject: [PATCH] common/string_util: Move TextFromBuffer to string_util --- src/citra_qt/applets/mii_selector.cpp | 16 +--------------- src/common/string_util.h | 16 ++++++++++++++++ src/core/hle/applets/mii_selector.cpp | 16 +--------------- src/core/hle/applets/swkbd.cpp | 22 ++++------------------ 4 files changed, 22 insertions(+), 48 deletions(-) diff --git a/src/citra_qt/applets/mii_selector.cpp b/src/citra_qt/applets/mii_selector.cpp index b51a33fb7..3ee25805f 100644 --- a/src/citra_qt/applets/mii_selector.cpp +++ b/src/citra_qt/applets/mii_selector.cpp @@ -14,20 +14,6 @@ #include "core/file_sys/file_backend.h" #include "core/hle/service/ptm/ptm.h" -/** - * Converts a UTF-16 text in a container to a UTF-8 std::string. - */ -template -std::string TextFromBuffer(const T& text) { - const auto text_end = std::find(text.begin(), text.end(), u'\0'); - const std::size_t text_size = std::distance(text.begin(), text_end); - std::u16string buffer(text_size, 0); - std::transform(text.begin(), text_end, buffer.begin(), [](u16_le character) { - return static_cast(static_cast(character)); - }); - return Common::UTF16ToUTF8(buffer); -} - QtMiiSelectorDialog::QtMiiSelectorDialog(QWidget* parent, QtMiiSelector* mii_selector_) : QDialog(parent), mii_selector(mii_selector_) { using namespace Frontend; @@ -71,7 +57,7 @@ QtMiiSelectorDialog::QtMiiSelectorDialog(QWidget* parent, QtMiiSelector* mii_sel file->Read(saved_miis_offset, sizeof(mii), mii_raw.data()); std::memcpy(&mii, mii_raw.data(), sizeof(mii)); if (mii.mii_id != 0) { - std::string name = TextFromBuffer(mii.mii_name); + std::string name = Common::UTF16BufferToUTF8(mii.mii_name); miis.push_back(mii); combobox->addItem(QString::fromStdString(name)); } diff --git a/src/common/string_util.h b/src/common/string_util.h index 110715dce..8c1894621 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -4,10 +4,12 @@ #pragma once +#include #include #include #include #include "common/common_types.h" +#include "common/swap.h" namespace Common { @@ -58,6 +60,20 @@ bool ComparePartialString(InIt begin, InIt end, const char* other) { return (begin == end) == (*other == '\0'); } +/** + * Converts a UTF-16 text in a container to a UTF-8 std::string. + */ +template +std::string UTF16BufferToUTF8(const T& text) { + const auto text_end = std::find(text.begin(), text.end(), u'\0'); + const std::size_t text_size = std::distance(text.begin(), text_end); + std::u16string buffer(text_size, 0); + std::transform(text.begin(), text_end, buffer.begin(), [](u16_le character) { + return static_cast(static_cast(character)); + }); + return UTF16ToUTF8(buffer); +} + /** * Creates a std::string from a fixed-size NUL-terminated char buffer. If the buffer isn't * NUL-terminated then the string ends at max_len characters. diff --git a/src/core/hle/applets/mii_selector.cpp b/src/core/hle/applets/mii_selector.cpp index 596e216c0..74473f2c5 100644 --- a/src/core/hle/applets/mii_selector.cpp +++ b/src/core/hle/applets/mii_selector.cpp @@ -19,20 +19,6 @@ namespace HLE::Applets { -/** - * Converts a UTF-16 text in a container to a UTF-8 std::string. - */ -template -std::string TextFromBuffer(const T& text) { - const auto text_end = std::find(text.begin(), text.end(), u'\0'); - const std::size_t text_size = std::distance(text.begin(), text_end); - std::u16string buffer(text_size, 0); - std::transform(text.begin(), text_end, buffer.begin(), [](u16_le character) { - return static_cast(static_cast(character)); - }); - return Common::UTF16ToUTF8(buffer); -} - ResultCode MiiSelector::ReceiveParameter(const Service::APT::MessageParameter& parameter) { if (parameter.signal != Service::APT::SignalType::Request) { LOG_ERROR(Service_APT, "unsupported signal {}", static_cast(parameter.signal)); @@ -156,7 +142,7 @@ MiiResult MiiSelector::GetStandardMiiResult() { Frontend::MiiSelectorConfig MiiSelector::ToFrontendConfig(const MiiConfig& config) const { Frontend::MiiSelectorConfig frontend_config; frontend_config.enable_cancel_button = config.enable_cancel_button == 1; - frontend_config.title = TextFromBuffer(config.title); + frontend_config.title = Common::UTF16BufferToUTF8(config.title); frontend_config.initially_selected_mii_index = config.initially_selected_mii_index; return frontend_config; } diff --git a/src/core/hle/applets/swkbd.cpp b/src/core/hle/applets/swkbd.cpp index da4d0bf0a..74a542df4 100644 --- a/src/core/hle/applets/swkbd.cpp +++ b/src/core/hle/applets/swkbd.cpp @@ -21,20 +21,6 @@ namespace HLE::Applets { -/** - * Converts a UTF-16 text in a container to a UTF-8 std::string. - */ -template -inline std::string TextFromBuffer(const T& text) { - const auto text_end = std::find(text.begin(), text.end(), u'\0'); - const std::size_t text_size = std::distance(text.begin(), text_end); - std::u16string buffer(text_size, 0); - std::transform(text.begin(), text_end, buffer.begin(), [](u16_le character) { - return static_cast(static_cast(character)); - }); - return Common::UTF16ToUTF8(buffer); -} - ResultCode SoftwareKeyboard::ReceiveParameter(Service::APT::MessageParameter const& parameter) { switch (parameter.signal) { case Service::APT::SignalType::Request: { @@ -79,7 +65,7 @@ ResultCode SoftwareKeyboard::ReceiveParameter(Service::APT::MessageParameter con case SoftwareKeyboardCallbackResult::Close: // Let the frontend display error and quit - frontend_applet->ShowError(TextFromBuffer(config.callback_msg)); + frontend_applet->ShowError(Common::UTF16BufferToUTF8(config.callback_msg)); config.return_code = SoftwareKeyboardResult::BannedInput; config.text_offset = config.text_length = 0; Finalize(); @@ -88,7 +74,7 @@ ResultCode SoftwareKeyboard::ReceiveParameter(Service::APT::MessageParameter con case SoftwareKeyboardCallbackResult::Continue: // Let the frontend display error and get input again // The input will be sent for validation again on next Update(). - frontend_applet->ShowError(TextFromBuffer(config.callback_msg)); + frontend_applet->ShowError(Common::UTF16BufferToUTF8(config.callback_msg)); frontend_applet->Execute(ToFrontendConfig(config)); return RESULT_SUCCESS; @@ -209,7 +195,7 @@ Frontend::KeyboardConfig SoftwareKeyboard::ToFrontendConfig( frontend_config.multiline_mode = config.multiline; frontend_config.max_text_length = config.max_text_length; frontend_config.max_digits = config.max_digits; - frontend_config.hint_text = TextFromBuffer(config.hint_text); + frontend_config.hint_text = Common::UTF16BufferToUTF8(config.hint_text); frontend_config.has_custom_button_text = !std::all_of(config.button_text.begin(), config.button_text.end(), [](std::array x) { @@ -217,7 +203,7 @@ Frontend::KeyboardConfig SoftwareKeyboard::ToFrontendConfig( }); if (frontend_config.has_custom_button_text) { for (const auto& text : config.button_text) { - frontend_config.button_text.push_back(TextFromBuffer(text)); + frontend_config.button_text.push_back(Common::UTF16BufferToUTF8(text)); } } frontend_config.filters.prevent_digit =