common/string_util: Move TextFromBuffer to string_util

This commit is contained in:
zhupengfei 2019-04-08 22:34:21 +08:00
parent 25be09c7a3
commit d7fbc1ca2c
No known key found for this signature in database
GPG key ID: DD129E108BD09378
4 changed files with 22 additions and 48 deletions

View file

@ -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 <typename T>
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<char16_t>(static_cast<u16>(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));
}

View file

@ -4,10 +4,12 @@
#pragma once
#include <algorithm>
#include <cstddef>
#include <string>
#include <vector>
#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 <typename T>
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<char16_t>(static_cast<u16>(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.

View file

@ -19,20 +19,6 @@
namespace HLE::Applets {
/**
* Converts a UTF-16 text in a container to a UTF-8 std::string.
*/
template <typename T>
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<char16_t>(static_cast<u16>(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<u32>(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;
}

View file

@ -21,20 +21,6 @@
namespace HLE::Applets {
/**
* Converts a UTF-16 text in a container to a UTF-8 std::string.
*/
template <typename T>
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<char16_t>(static_cast<u16>(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<u16, HLE::Applets::MAX_BUTTON_TEXT_LEN + 1> 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 =