From ffd3afcf2fec718c86d664ede2fa3801b8fc746c Mon Sep 17 00:00:00 2001
From: lat9nq <lat9nq@gmail.com>
Date: Sun, 15 May 2022 18:06:33 -0400
Subject: [PATCH 1/4] string_util: Add U16StringFromBuffer

Qt's QString::toStdU16String doesn't work when compiling against the
latest libstdc++, at least when using Clang. This function effectively
does the same thing as the aforementioned one.
---
 src/common/string_util.cpp | 4 ++++
 src/common/string_util.h   | 2 ++
 2 files changed, 6 insertions(+)

diff --git a/src/common/string_util.cpp b/src/common/string_util.cpp
index 703aa5db82..7a495bc798 100644
--- a/src/common/string_util.cpp
+++ b/src/common/string_util.cpp
@@ -178,6 +178,10 @@ std::wstring UTF8ToUTF16W(const std::string& input) {
 
 #endif
 
+std::u16string U16StringFromBuffer(const u16* input, std::size_t length) {
+    return std::u16string(reinterpret_cast<const char16_t*>(input), length);
+}
+
 std::string StringFromFixedZeroTerminatedBuffer(std::string_view buffer, std::size_t max_len) {
     std::size_t len = 0;
     while (len < buffer.length() && len < max_len && buffer[len] != '\0') {
diff --git a/src/common/string_util.h b/src/common/string_util.h
index a33830aec1..ce18a33cfe 100644
--- a/src/common/string_util.h
+++ b/src/common/string_util.h
@@ -44,6 +44,8 @@ bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _
 
 #endif
 
+[[nodiscard]] std::u16string U16StringFromBuffer(const u16* input, std::size_t length);
+
 /**
  * Compares the string defined by the range [`begin`, `end`) to the null-terminated C-string
  * `other` for equality.

From f7908eebb91cf41e90c369809a0f89a165b14217 Mon Sep 17 00:00:00 2001
From: lat9nq <lat9nq@gmail.com>
Date: Sun, 15 May 2022 18:08:08 -0400
Subject: [PATCH 2/4] qt_software_keyboard: Use Common::U16StringFromBuffer

See ffd3afcf2
---
 src/yuzu/applets/qt_software_keyboard.cpp | 29 ++++++++++++-----------
 1 file changed, 15 insertions(+), 14 deletions(-)

diff --git a/src/yuzu/applets/qt_software_keyboard.cpp b/src/yuzu/applets/qt_software_keyboard.cpp
index d3cf0b43b7..135e060851 100644
--- a/src/yuzu/applets/qt_software_keyboard.cpp
+++ b/src/yuzu/applets/qt_software_keyboard.cpp
@@ -411,11 +411,11 @@ void QtSoftwareKeyboardDialog::ShowTextCheckDialog(
             break;
         }
 
-        auto text = ui->topOSK->currentIndex() == 1
-                        ? ui->text_edit_osk->toPlainText().toStdU16String()
-                        : ui->line_edit_osk->text().toStdU16String();
+        const auto& text = ui->topOSK->currentIndex() == 1 ? ui->text_edit_osk->toPlainText()
+                                                           : ui->line_edit_osk->text();
+        std::u16string text_s = Common::U16StringFromBuffer(text.utf16(), text.size());
 
-        emit SubmitNormalText(SwkbdResult::Ok, std::move(text), true);
+        emit SubmitNormalText(SwkbdResult::Ok, std::move(text_s), true);
         break;
     }
     }
@@ -562,7 +562,7 @@ void QtSoftwareKeyboardDialog::keyPressEvent(QKeyEvent* event) {
         return;
     }
 
-    InlineTextInsertString(entered_text.toStdU16String());
+    InlineTextInsertString(Common::U16StringFromBuffer(entered_text.utf16(), entered_text.size()));
 }
 
 void QtSoftwareKeyboardDialog::MoveAndResizeWindow(QPoint pos, QSize size) {
@@ -1119,11 +1119,11 @@ void QtSoftwareKeyboardDialog::NormalKeyboardButtonClicked(QPushButton* button)
     }
 
     if (button == ui->button_ok || button == ui->button_ok_shift || button == ui->button_ok_num) {
-        auto text = ui->topOSK->currentIndex() == 1
-                        ? ui->text_edit_osk->toPlainText().toStdU16String()
-                        : ui->line_edit_osk->text().toStdU16String();
+        const auto& text = ui->topOSK->currentIndex() == 1 ? ui->text_edit_osk->toPlainText()
+                                                           : ui->line_edit_osk->text();
+        std::u16string text_s = Common::U16StringFromBuffer(text.utf16(), text.size());
 
-        emit SubmitNormalText(SwkbdResult::Ok, std::move(text));
+        emit SubmitNormalText(SwkbdResult::Ok, std::move(text_s));
         return;
     }
 
@@ -1189,7 +1189,8 @@ void QtSoftwareKeyboardDialog::InlineKeyboardButtonClicked(QPushButton* button)
         return;
     }
 
-    InlineTextInsertString(button->text().toStdU16String());
+    InlineTextInsertString(
+        Common::U16StringFromBuffer(button->text().utf16(), button->text().size()));
 
     // Revert the keyboard to lowercase if the shift key is active.
     if (bottom_osk_index == BottomOSKIndex::UpperCase && !caps_lock_enabled) {
@@ -1282,11 +1283,11 @@ void QtSoftwareKeyboardDialog::TranslateButtonPress(Core::HID::NpadButton button
         if (is_inline) {
             emit SubmitInlineText(SwkbdReplyType::DecidedCancel, current_text, cursor_position);
         } else {
-            auto text = ui->topOSK->currentIndex() == 1
-                            ? ui->text_edit_osk->toPlainText().toStdU16String()
-                            : ui->line_edit_osk->text().toStdU16String();
+            const auto& text = ui->topOSK->currentIndex() == 1 ? ui->text_edit_osk->toPlainText()
+                                                               : ui->line_edit_osk->text();
+            std::u16string text_s = Common::U16StringFromBuffer(text.utf16(), text.size());
 
-            emit SubmitNormalText(SwkbdResult::Cancel, std::move(text));
+            emit SubmitNormalText(SwkbdResult::Cancel, std::move(text_s));
         }
         break;
     case Core::HID::NpadButton::Y:

From 3ca3254b9f75545d1678f3e9ef116b554f31add8 Mon Sep 17 00:00:00 2001
From: lat9nq <lat9nq@gmail.com>
Date: Sun, 15 May 2022 18:09:19 -0400
Subject: [PATCH 3/4] main: Use Common::U16StringFromBuffer

See ffd3afcf2
---
 src/yuzu/main.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp
index f607f464a6..f4a9a7171a 100644
--- a/src/yuzu/main.cpp
+++ b/src/yuzu/main.cpp
@@ -1401,7 +1401,8 @@ void GMainWindow::BootGame(const QString& filename, u64 program_id, std::size_t
     if (loader != nullptr && loader->ReadProgramId(title_id) == Loader::ResultStatus::Success &&
         type == StartGameType::Normal) {
         // Load per game settings
-        const auto file_path = std::filesystem::path{filename.toStdU16String()};
+        const auto file_path =
+            std::filesystem::path{Common::U16StringFromBuffer(filename.utf16(), filename.size())};
         const auto config_file_name = title_id == 0
                                           ? Common::FS::PathToUTF8String(file_path.filename())
                                           : fmt::format("{:016X}", title_id);
@@ -1482,7 +1483,8 @@ void GMainWindow::BootGame(const QString& filename, u64 program_id, std::size_t
     }
     if (res != Loader::ResultStatus::Success || title_name.empty()) {
         title_name = Common::FS::PathToUTF8String(
-            std::filesystem::path{filename.toStdU16String()}.filename());
+            std::filesystem::path{Common::U16StringFromBuffer(filename.utf16(), filename.size())}
+                .filename());
     }
     const bool is_64bit = system->Kernel().CurrentProcess()->Is64BitProcess();
     const auto instruction_set_suffix = is_64bit ? tr("(64-bit)") : tr("(32-bit)");

From 5035df27c357d5807432aa0056cc4e428884d988 Mon Sep 17 00:00:00 2001
From: lat9nq <lat9nq@gmail.com>
Date: Sun, 15 May 2022 20:19:24 -0400
Subject: [PATCH 4/4] qt_software_keyboard: Address review feedback

Use auto and a more descriptive variable name.

Secondly, fix some C++ misconceptions or constructing too many objects.

Co-authored-by: Morph <39850852+Morph1984@users.noreply.github.com>
Co-authored-by: Lioncash <mathew1800@gmail.com>
---
 src/yuzu/applets/qt_software_keyboard.cpp | 28 +++++++++++------------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/src/yuzu/applets/qt_software_keyboard.cpp b/src/yuzu/applets/qt_software_keyboard.cpp
index 135e060851..e8b217d90e 100644
--- a/src/yuzu/applets/qt_software_keyboard.cpp
+++ b/src/yuzu/applets/qt_software_keyboard.cpp
@@ -411,11 +411,11 @@ void QtSoftwareKeyboardDialog::ShowTextCheckDialog(
             break;
         }
 
-        const auto& text = ui->topOSK->currentIndex() == 1 ? ui->text_edit_osk->toPlainText()
-                                                           : ui->line_edit_osk->text();
-        std::u16string text_s = Common::U16StringFromBuffer(text.utf16(), text.size());
+        const auto text = ui->topOSK->currentIndex() == 1 ? ui->text_edit_osk->toPlainText()
+                                                          : ui->line_edit_osk->text();
+        auto text_str = Common::U16StringFromBuffer(text.utf16(), text.size());
 
-        emit SubmitNormalText(SwkbdResult::Ok, std::move(text_s), true);
+        emit SubmitNormalText(SwkbdResult::Ok, std::move(text_str), true);
         break;
     }
     }
@@ -1119,11 +1119,11 @@ void QtSoftwareKeyboardDialog::NormalKeyboardButtonClicked(QPushButton* button)
     }
 
     if (button == ui->button_ok || button == ui->button_ok_shift || button == ui->button_ok_num) {
-        const auto& text = ui->topOSK->currentIndex() == 1 ? ui->text_edit_osk->toPlainText()
-                                                           : ui->line_edit_osk->text();
-        std::u16string text_s = Common::U16StringFromBuffer(text.utf16(), text.size());
+        const auto text = ui->topOSK->currentIndex() == 1 ? ui->text_edit_osk->toPlainText()
+                                                          : ui->line_edit_osk->text();
+        auto text_str = Common::U16StringFromBuffer(text.utf16(), text.size());
 
-        emit SubmitNormalText(SwkbdResult::Ok, std::move(text_s));
+        emit SubmitNormalText(SwkbdResult::Ok, std::move(text_str));
         return;
     }
 
@@ -1189,8 +1189,8 @@ void QtSoftwareKeyboardDialog::InlineKeyboardButtonClicked(QPushButton* button)
         return;
     }
 
-    InlineTextInsertString(
-        Common::U16StringFromBuffer(button->text().utf16(), button->text().size()));
+    const auto button_text = button->text();
+    InlineTextInsertString(Common::U16StringFromBuffer(button_text.utf16(), button_text.size()));
 
     // Revert the keyboard to lowercase if the shift key is active.
     if (bottom_osk_index == BottomOSKIndex::UpperCase && !caps_lock_enabled) {
@@ -1283,11 +1283,11 @@ void QtSoftwareKeyboardDialog::TranslateButtonPress(Core::HID::NpadButton button
         if (is_inline) {
             emit SubmitInlineText(SwkbdReplyType::DecidedCancel, current_text, cursor_position);
         } else {
-            const auto& text = ui->topOSK->currentIndex() == 1 ? ui->text_edit_osk->toPlainText()
-                                                               : ui->line_edit_osk->text();
-            std::u16string text_s = Common::U16StringFromBuffer(text.utf16(), text.size());
+            const auto text = ui->topOSK->currentIndex() == 1 ? ui->text_edit_osk->toPlainText()
+                                                              : ui->line_edit_osk->text();
+            auto text_str = Common::U16StringFromBuffer(text.utf16(), text.size());
 
-            emit SubmitNormalText(SwkbdResult::Cancel, std::move(text_s));
+            emit SubmitNormalText(SwkbdResult::Cancel, std::move(text_str));
         }
         break;
     case Core::HID::NpadButton::Y: