mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-24 00:48:00 +08:00
3137aaa660
* Refactored logic and made it unit testable * Changes after code review * Added to build steps, and modified bracket to new class with unittest. Validates complexer cases now. Co-authored-by: p-storm <paul.de.man@gmail.com>
48 lines
1.3 KiB
C#
48 lines
1.3 KiB
C#
// Copyright (c) Microsoft Corporation
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Microsoft.Plugin.Calculator
|
|
{
|
|
public static class CalculateHelper
|
|
{
|
|
private static readonly Regex RegValidExpressChar = new Regex(
|
|
@"^(" +
|
|
@"ceil|floor|exp|pi|e|max|min|det|abs|log|ln|sqrt|" +
|
|
@"sin|cos|tan|arcsin|arccos|arctan|" +
|
|
@"eigval|eigvec|eig|sum|polar|plot|round|sort|real|zeta|" +
|
|
@"bin2dec|hex2dec|oct2dec|" +
|
|
@"==|~=|&&|\|\||" +
|
|
@"[ei]|[0-9]|[\+\-\*\/\^\., ""]|[\(\)\|\!\[\]]" +
|
|
@")+$", RegexOptions.Compiled);
|
|
|
|
public static bool InputValid(string input)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(input))
|
|
{
|
|
throw new ArgumentNullException(paramName: nameof(input));
|
|
}
|
|
|
|
if (input.Length <= 2)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!RegValidExpressChar.IsMatch(input))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!BracketHelper.IsBracketComplete(input))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
}
|