From 292d9d19250a81757128666c6b3784c41b3e1697 Mon Sep 17 00:00:00 2001 From: Alexander Lohnau Date: Wed, 13 Jan 2021 19:24:39 +0100 Subject: [PATCH] calculator runner: Fix handling of hexadecimal input/output - In case our query starts with 0x123abc we do not call the `userFriendlyMultiplication` method which replaces the x sign with the multiplication operator. We can be sure that the user wants to parse a hex value. Also the `=` is not mandatory anymore. - The hex=mydecimalnumber functionality was broken. The conversion of the hex value is only done inside of the if statement and before the values will be the same, unless we do some math operation. Test Plan: `=0xf` returns 15 `0xf` return 15 `7x7` returns 49 `hex=15` returns 0xF `hex=14+1` returns 0xF BUG: 431362 FIXED-IN: 5.21 --- runners/calculator/calculatorrunner.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/runners/calculator/calculatorrunner.cpp b/runners/calculator/calculatorrunner.cpp index d3b02aa57..effd7709c 100644 --- a/runners/calculator/calculatorrunner.cpp +++ b/runners/calculator/calculatorrunner.cpp @@ -232,14 +232,17 @@ void CalculatorRunner::match(Plasma::RunnerContext &context) bool toHex = cmd.startsWith(QLatin1String("hex=")); bool startsWithEquals = !toHex && cmd[0] == QLatin1Char('='); - - userFriendlyMultiplication(cmd); + const static QRegularExpression hexRegex(QStringLiteral("^=?0x.+"), QRegularExpression::CaseInsensitiveOption); + const bool parseHex = cmd.contains(hexRegex); + if (!parseHex) { + userFriendlyMultiplication(cmd); + } if (toHex || startsWithEquals) { cmd.remove(0, cmd.indexOf(QLatin1Char('=')) + 1); } else if (cmd.endsWith(QLatin1Char('='))) { cmd.chop(1); - } else { + } else if (!parseHex) { bool foundDigit = false; for (int i = 0; i < cmd.length(); ++i) { QChar c = cmd.at(i); @@ -268,7 +271,7 @@ void CalculatorRunner::match(Plasma::RunnerContext &context) bool isApproximate = false; QString result = calculate(cmd, &isApproximate); - if (!result.isEmpty() && result != cmd) { + if (!result.isEmpty() && (result != cmd || toHex)) { if (toHex) { result = QLatin1String("0x") + QString::number(result.toInt(), 16).toUpper(); }