mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-09 04:37:30 +02:00
[PT Run][Calculator] Input validation improvements (#20067)
* improve input validation * update test * update tests * simplify test case * fix devision by zero check
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text.RegularExpressions;
|
||||
using Mages.Core;
|
||||
|
||||
namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
@@ -34,6 +35,14 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
return default;
|
||||
}
|
||||
|
||||
// check for division by zero
|
||||
// We check if the string contains a slash followed by space (optional) and zero. Whereas the zero must not followed by dot or comma as this indicates a number with decimal digits.
|
||||
if (new Regex("\\/\\s*0(?![,\\.])").Match(input).Success)
|
||||
{
|
||||
error = Properties.Resources.wox_plugin_calculator_division_by_zero;
|
||||
return default;
|
||||
}
|
||||
|
||||
// mages has quirky log representation
|
||||
// mage has log == ln vs log10
|
||||
input = input.
|
||||
|
||||
@@ -38,9 +38,9 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
return false;
|
||||
}
|
||||
|
||||
// If the input ends with a binary operator then it is not a valid input to mages and the Interpret function would throw an exception.
|
||||
// If the input ends with a binary operator then it is not a valid input to mages and the Interpret function would throw an exception. Because we expect here that the user has not finished typing we block those inputs.
|
||||
string trimmedInput = input.TrimEnd();
|
||||
if (trimmedInput.EndsWith('+') || trimmedInput.EndsWith('-') || trimmedInput.EndsWith('*') || trimmedInput.EndsWith('|') || trimmedInput.EndsWith('\\') || trimmedInput.EndsWith('^') || trimmedInput.EndsWith('=') || trimmedInput.EndsWith('&'))
|
||||
if (trimmedInput.EndsWith('+') || trimmedInput.EndsWith('-') || trimmedInput.EndsWith('*') || trimmedInput.EndsWith('|') || trimmedInput.EndsWith('\\') || trimmedInput.EndsWith('^') || trimmedInput.EndsWith('=') || trimmedInput.EndsWith('&') || trimmedInput.EndsWith('/') || trimmedInput.EndsWith('%'))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -87,6 +87,15 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.Properties {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Expression contains division by zero.
|
||||
/// </summary>
|
||||
public static string wox_plugin_calculator_division_by_zero {
|
||||
get {
|
||||
return ResourceManager.GetString("wox_plugin_calculator_division_by_zero", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Unsupported use of square brackets.
|
||||
/// </summary>
|
||||
|
||||
@@ -158,4 +158,7 @@
|
||||
<value>Ignores your system setting and returns numbers in the format '{0}'.</value>
|
||||
<comment>{0} is a placeholder and will be replaced in code.</comment>
|
||||
</data>
|
||||
<data name="wox_plugin_calculator_division_by_zero" xml:space="preserve">
|
||||
<value>Expression contains division by zero</value>
|
||||
</data>
|
||||
</root>
|
||||
Reference in New Issue
Block a user