mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 03:37:59 +01:00
[PTRun][Calc]Improve handling of non-base 10 numbers (#36700)
This commit is contained in:
@@ -197,6 +197,8 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
|
|||||||
[DataRow("randi(0.5)", true)]
|
[DataRow("randi(0.5)", true)]
|
||||||
[DataRow("rand()", true)]
|
[DataRow("rand()", true)]
|
||||||
[DataRow("rand(0.5)", false)]
|
[DataRow("rand(0.5)", false)]
|
||||||
|
[DataRow("0X78AD+0o123", true)]
|
||||||
|
[DataRow("0o9", false)]
|
||||||
public void InputValid_TestValid_WhenCalled(string input, bool valid)
|
public void InputValid_TestValid_WhenCalled(string input, bool valid)
|
||||||
{
|
{
|
||||||
// Act
|
// Act
|
||||||
@@ -233,7 +235,8 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
|
|||||||
new object[] { "abs(-2)", 2M },
|
new object[] { "abs(-2)", 2M },
|
||||||
new object[] { "abs(2)", 2M },
|
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+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)/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]
|
[DataTestMethod]
|
||||||
|
|||||||
@@ -41,6 +41,8 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
|
|||||||
[DataRow("=5 / 0", "Expression contains division by zero")]
|
[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)/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)
|
public void ErrorResultOnInvalidKeywordQuery(string typedString, string expectedResult)
|
||||||
{
|
{
|
||||||
Query expectedQuery = new(typedString, "=");
|
Query expectedQuery = new(typedString, "=");
|
||||||
|
|||||||
@@ -37,8 +37,8 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
|||||||
}
|
}
|
||||||
|
|
||||||
// check for division by zero
|
// 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.
|
// 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-9xX])").Match(input).Success)
|
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;
|
error = Properties.Resources.wox_plugin_calculator_division_by_zero;
|
||||||
return default;
|
return default;
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
|||||||
@"pi|" +
|
@"pi|" +
|
||||||
@"==|~=|&&|\|\||" +
|
@"==|~=|&&|\|\||" +
|
||||||
@"((-?(\d+(\.\d*)?)|-?(\.\d+))[Ee](-?\d+))|" + /* expression from CheckScientificNotation between parenthesis */
|
@"((-?(\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);
|
RegexOptions.Compiled);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user