Commit 292d9d19 authored by Alexander Lohnau's avatar Alexander Lohnau 💬

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
parent 10780187
......@@ -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();
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment