diff mbox series

[7/7] kconfig: qconf: simplify character replacement

Message ID 3592638.iIbC2pHGDl@devpool47.emlix.com (mailing list archive)
State New
Headers show
Series improve qconfig C++ code | expand

Commit Message

Rolf Eike Beer Oct. 23, 2024, 6:39 a.m. UTC
Replace the hand crafted lookup table with a QHash. This has the nice benefit
that the added offsets can not get out of sync with the length of the
replacement strings.

Signed-off-by: Rolf Eike Beer <eb@emlix.com>
---
 scripts/kconfig/qconf.cc | 33 ++++++++++++---------------------
 1 file changed, 12 insertions(+), 21 deletions(-)

Comments

Masahiro Yamada Oct. 23, 2024, 4:51 p.m. UTC | #1
On Wed, Oct 23, 2024 at 3:39 PM Rolf Eike Beer <eb@emlix.com> wrote:
>
> Replace the hand crafted lookup table with a QHash. This has the nice benefit
> that the added offsets can not get out of sync with the length of the
> replacement strings.
>
> Signed-off-by: Rolf Eike Beer <eb@emlix.com>
> ---

Applied to linux-kbuild. Thanks!
diff mbox series

Patch

diff --git a/scripts/kconfig/qconf.cc b/scripts/kconfig/qconf.cc
index 4b2f707c9203..f07a463c5760 100644
--- a/scripts/kconfig/qconf.cc
+++ b/scripts/kconfig/qconf.cc
@@ -1123,28 +1123,19 @@  QString ConfigInfoView::print_filter(const QString &str)
 {
 	QRegularExpression re("[<>&\"\\n]");
 	QString res = str;
+
+	QHash<QChar, QString> patterns;
+	patterns['<'] = "&lt;";
+	patterns['>'] = "&gt;";
+	patterns['&'] = "&amp;";
+	patterns['"'] = "&quot;";
+	patterns['\n'] = "<br>";
+
 	for (int i = 0; (i = res.indexOf(re, i)) >= 0;) {
-		switch (res[i].toLatin1()) {
-		case '<':
-			res.replace(i, 1, "&lt;");
-			i += 4;
-			break;
-		case '>':
-			res.replace(i, 1, "&gt;");
-			i += 4;
-			break;
-		case '&':
-			res.replace(i, 1, "&amp;");
-			i += 5;
-			break;
-		case '"':
-			res.replace(i, 1, "&quot;");
-			i += 6;
-			break;
-		case '\n':
-			res.replace(i, 1, "<br>");
-			i += 4;
-			break;
+		const QString n = patterns.value(res[i], QString());
+		if (!n.isEmpty()) {
+			res.replace(i, 1, n);
+			i += n.length();
 		}
 	}
 	return res;