From 6a776624ac2be07c4f90fc0600b94ac517f8c31b Mon Sep 17 00:00:00 2001
From: Elad Ashkenazi <18193363+elad335@users.noreply.github.com>
Date: Sat, 4 May 2024 12:36:35 +0300
Subject: [PATCH] Qt/Logs: Fixing spaces, optimize string to html conversion
---
rpcs3/rpcs3qt/log_frame.cpp | 31 ++++++++++++++++++++++++++++---
1 file changed, 28 insertions(+), 3 deletions(-)
diff --git a/rpcs3/rpcs3qt/log_frame.cpp b/rpcs3/rpcs3qt/log_frame.cpp
index d24abb1764..c8359e4190 100644
--- a/rpcs3/rpcs3qt/log_frame.cpp
+++ b/rpcs3/rpcs3qt/log_frame.cpp
@@ -693,9 +693,34 @@ void log_frame::UpdateUI()
const QString font_start_tag_stack = "";
static const QString font_end_tag = QStringLiteral("");
- static constexpr auto escaped = [](const QString& text)
+ static constexpr auto escaped = [](const QString& text, QString&& storage) -> const QString&
{
- return text.toHtmlEscaped().replace(QStringLiteral("\n"), QStringLiteral("
"));
+ const qsizetype nline = text.indexOf(QChar('\n'));
+ const qsizetype spaces = text.indexOf(QStringLiteral(" "));
+ const qsizetype html = std::max({ text.indexOf(QChar('<')), text.indexOf(QChar('>')), text.indexOf(QChar('&')), text.indexOf(QChar('\"')) });
+
+ const qsizetype pos = std::max({ html, nline, spaces });
+
+ if (pos < 0)
+ {
+ // Nothing to change, do not create copies of the string
+ return text;
+ }
+
+ // Allow to return reference of new string by using temporary storage provided by argument
+ storage = html < 0 ? text : text.toHtmlEscaped();
+
+ if (nline >= 0)
+ {
+ storage.replace(QChar('\n'), QStringLiteral("
"));
+ }
+
+ if (spaces >= 0)
+ {
+ storage.replace(QChar::Space, QChar::Nbsp);
+ }
+
+ return storage;
};
// Preserve capacity
@@ -866,7 +891,7 @@ void log_frame::UpdateUI()
}
// Print UTF-8 text.
- m_log_text += escaped(qstr(packet->msg));
+ m_log_text += escaped(qstr(packet->msg), QString{});
if (m_stack_log)
{