[PTRun][Calc]Improve handling of non-base 10 numbers (#36700)

This commit is contained in:
PesBandi 2025-01-08 11:49:26 +01:00 коммит произвёл GitHub
Родитель 308c4b817e
Коммит cd2a88704d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
4 изменённых файлов: 9 добавлений и 4 удалений

Просмотреть файл

@ -197,6 +197,8 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
[DataRow("randi(0.5)", true)]
[DataRow("rand()", true)]
[DataRow("rand(0.5)", false)]
[DataRow("0X78AD+0o123", true)]
[DataRow("0o9", false)]
public void InputValid_TestValid_WhenCalled(string input, bool valid)
{
// Act
@ -234,6 +236,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
new object[] { "abs(2)", 2M },
new object[] { "0+(1*2)/(0+1)", 2M }, // Validate that division by "(0+1)" is not interpret as division by zero.
new object[] { "0+(1*2)/0.5", 4M }, // Validate that division by number with decimal digits is not interpret as division by zero.
new object[] { "0+(1*2)/0o004", 0.5M }, // Validate that division by an octal number with zeroes is not treated as division by zero.
};
[DataTestMethod]

Просмотреть файл

@ -41,6 +41,8 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
[DataRow("=5 / 0", "Expression contains division by zero")]
[DataRow("10+(8*9)/0+7", "Expression contains division by zero")]
[DataRow("10+(8*9)/0*7", "Expression contains division by zero")]
[DataRow("10+(8*9)/0x00", "Expression contains division by zero")]
[DataRow("10+(8*9)/0b0", "Expression contains division by zero")]
public void ErrorResultOnInvalidKeywordQuery(string typedString, string expectedResult)
{
Query expectedQuery = new(typedString, "=");

Просмотреть файл

@ -37,8 +37,8 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
}
// check for division by zero
// We check if the string contains a slash followed by space (optional) and zero. Whereas the zero must not be followed by a dot, comma, or 'x'/'X' as these indicate a number with decimal digits or a hexadecimal value respectively. The zero must also not be followed by other digits.
if (new Regex("\\/\\s*0(?![,\\.0-9xX])").Match(input).Success)
// We check if the string contains a slash followed by space (optional) and zero. Whereas the zero must not be followed by a dot, comma, 'b', 'o' or 'x' as these indicate a number with decimal digits or a binary/octal/hexadecimal value respectively. The zero must also not be followed by other digits.
if (new Regex("\\/\\s*0(?!(?:[,\\.0-9]|[box]0*[1-9a-f]))", RegexOptions.IgnoreCase).Match(input).Success)
{
error = Properties.Resources.wox_plugin_calculator_division_by_zero;
return default;

Просмотреть файл

@ -21,7 +21,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
@"pi|" +
@"==|~=|&&|\|\||" +
@"((-?(\d+(\.\d*)?)|-?(\.\d+))[Ee](-?\d+))|" + /* expression from CheckScientificNotation between parenthesis */
@"e|[0-9]|0x[0-9a-fA-F]+|0b[01]+|[\+\-\*\/\^\., ""]|[\(\)\|\!\[\]]" +
@"e|[0-9]|0[xX][0-9a-fA-F]+|0[bB][01]+|0[oO][0-7]+|[\+\-\*\/\^\., ""]|[\(\)\|\!\[\]]" +
@")+$",
RegexOptions.Compiled);