From 0d9de0819445825c8a26c805e78c9d37e4f07e57 Mon Sep 17 00:00:00 2001 From: Lander Gallastegi Date: Thu, 10 Oct 2024 17:20:57 +0200 Subject: [PATCH] Use CString --- src/common/cstring.h | 8 ++++++ src/core/libraries/dialogs/ime_dialog_ui.cpp | 26 +++++++++----------- src/core/libraries/dialogs/ime_dialog_ui.h | 3 ++- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/common/cstring.h b/src/common/cstring.h index 070ed6fad..45c291c14 100644 --- a/src/common/cstring.h +++ b/src/common/cstring.h @@ -109,6 +109,14 @@ public: return data + N; } + constexpr std::size_t capacity() const { + return N; + } + + std::size_t size() const { + return std::char_traits::length(data); + } + T& operator[](size_t idx) { return data[idx]; } diff --git a/src/core/libraries/dialogs/ime_dialog_ui.cpp b/src/core/libraries/dialogs/ime_dialog_ui.cpp index e62187a74..17600eca7 100644 --- a/src/core/libraries/dialogs/ime_dialog_ui.cpp +++ b/src/core/libraries/dialogs/ime_dialog_ui.cpp @@ -72,7 +72,7 @@ ImeDialogState::ImeDialogState(const OrbisImeDialogParam* param, } std::size_t text_len = std::char_traits::length(text_buffer); - if (!ConvertOrbisToUTF8(text_buffer, text_len, current_text, + if (!ConvertOrbisToUTF8(text_buffer, text_len, current_text.begin(), ORBIS_IME_DIALOG_MAX_TEXT_LENGTH * 4)) { LOG_ERROR(Lib_ImeDialog, "Failed to convert text to utf8 encoding"); } @@ -83,13 +83,11 @@ ImeDialogState::~ImeDialogState() { } ImeDialogState::ImeDialogState(ImeDialogState&& other) noexcept - : userId(other.userId), is_multiLine(other.is_multiLine), is_numeric(other.is_numeric), + : input_changed(other.input_changed), userId(other.userId), is_multiLine(other.is_multiLine), is_numeric(other.is_numeric), type(other.type), enter_label(other.enter_label), text_filter(other.text_filter), keyboard_filter(other.keyboard_filter), max_text_length(other.max_text_length), text_buffer(other.text_buffer), title(std::move(other.title)), placeholder(std::move(other.placeholder)), - input_changed(other.input_changed) { - - std::memcpy(current_text, other.current_text, sizeof(current_text)); + current_text(other.current_text) { #ifndef _WIN32 orbis_to_utf8 = other.orbis_to_utf8; @@ -106,6 +104,7 @@ ImeDialogState& ImeDialogState::operator=(ImeDialogState&& other) { if (this != &other) { Free(); + input_changed = other.input_changed; userId = other.userId; is_multiLine = other.is_multiLine; is_numeric = other.is_numeric; @@ -117,9 +116,7 @@ ImeDialogState& ImeDialogState::operator=(ImeDialogState&& other) { text_buffer = other.text_buffer; title = std::move(other.title); placeholder = std::move(other.placeholder); - input_changed = other.input_changed; - - std::memcpy(current_text, other.current_text, sizeof(current_text)); + current_text = other.current_text; #ifndef _WIN32 orbis_to_utf8 = other.orbis_to_utf8; @@ -140,8 +137,7 @@ bool ImeDialogState::CopyTextToOrbisBuffer() { return false; } - std::size_t text_len = std::char_traits::length(current_text); - return ConvertUTF8ToOrbis(current_text, text_len, text_buffer, max_text_length); + return ConvertUTF8ToOrbis(current_text.begin(), current_text.capacity(), text_buffer, max_text_length); } bool ImeDialogState::CallTextFilter() { @@ -152,11 +148,11 @@ bool ImeDialogState::CallTextFilter() { input_changed = false; char16_t src_text[ORBIS_IME_DIALOG_MAX_TEXT_LENGTH + 1] = {0}; - u32 src_text_length = std::strlen(current_text); + u32 src_text_length = current_text.size(); char16_t out_text[ORBIS_IME_DIALOG_MAX_TEXT_LENGTH + 1] = {0}; u32 out_text_length = ORBIS_IME_DIALOG_MAX_TEXT_LENGTH; - if (!ConvertUTF8ToOrbis(current_text, src_text_length, src_text, + if (!ConvertUTF8ToOrbis(current_text.begin(), src_text_length, src_text, ORBIS_IME_DIALOG_MAX_TEXT_LENGTH)) { LOG_ERROR(Lib_ImeDialog, "Failed to convert text to orbis encoding"); return false; @@ -170,7 +166,7 @@ bool ImeDialogState::CallTextFilter() { return false; } - if (!ConvertOrbisToUTF8(out_text, out_text_length, current_text, + if (!ConvertOrbisToUTF8(out_text, out_text_length, current_text.begin(), ORBIS_IME_DIALOG_MAX_TEXT_LENGTH * 4)) { LOG_ERROR(Lib_ImeDialog, "Failed to convert text to utf8 encoding"); return false; @@ -408,7 +404,7 @@ void ImeDialogUi::DrawInputText() { SetKeyboardFocusHere(); } const char* placeholder = state->placeholder.empty() ? nullptr : state->placeholder.data(); - if (InputTextEx("##ImeDialogInput", placeholder, state->current_text, + if (InputTextEx("##ImeDialogInput", placeholder, state->current_text.begin(), state->max_text_length, input_size, ImGuiInputTextFlags_CallbackCharFilter, InputTextCallback, this)) { state->input_changed = true; @@ -424,7 +420,7 @@ void ImeDialogUi::DrawMultiLineInputText() { SetKeyboardFocusHere(); } const char* placeholder = state->placeholder.empty() ? nullptr : state->placeholder.data(); - if (InputTextEx("##ImeDialogInput", placeholder, state->current_text, + if (InputTextEx("##ImeDialogInput", placeholder, state->current_text.begin(), state->max_text_length, input_size, flags, InputTextCallback, this)) { state->input_changed = true; } diff --git a/src/core/libraries/dialogs/ime_dialog_ui.h b/src/core/libraries/dialogs/ime_dialog_ui.h index 457023bee..5d169e619 100644 --- a/src/core/libraries/dialogs/ime_dialog_ui.h +++ b/src/core/libraries/dialogs/ime_dialog_ui.h @@ -9,6 +9,7 @@ #include #endif #include +#include "common/cstring.h" #include "common/types.h" #include "core/libraries/dialogs/ime_dialog.h" #include "imgui/imgui_layer.h" @@ -35,7 +36,7 @@ class ImeDialogState final { std::vector placeholder; // A character can hold up to 4 bytes in UTF-8 - char current_text[ORBIS_IME_DIALOG_MAX_TEXT_LENGTH * 4 + 1] = {0}; + Common::CString current_text; #ifndef _WIN32 iconv_t orbis_to_utf8 = (iconv_t)-1; iconv_t utf8_to_orbis = (iconv_t)-1;