Files
PowerToys/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/NumberTranslator.cs

146 lines
5.3 KiB
C#
Raw Normal View History

2020-08-17 10:00:56 -07:00
// 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.Globalization;
using System.Text;
using System.Text.RegularExpressions;
namespace Microsoft.PowerToys.Run.Plugin.Calculator
2020-08-17 10:00:56 -07:00
{
/// <summary>
/// Tries to convert all numbers in a text from one culture format to another.
/// </summary>
public class NumberTranslator
{
private readonly CultureInfo sourceCulture;
private readonly CultureInfo targetCulture;
private readonly Regex splitRegexForSource;
private readonly Regex splitRegexForTarget;
private NumberTranslator(CultureInfo sourceCulture, CultureInfo targetCulture)
{
this.sourceCulture = sourceCulture;
this.targetCulture = targetCulture;
splitRegexForSource = GetSplitRegex(this.sourceCulture);
splitRegexForTarget = GetSplitRegex(this.targetCulture);
}
/// <summary>
/// Create a new <see cref="NumberTranslator"/>.
2020-08-17 10:00:56 -07:00
/// </summary>
/// <param name="sourceCulture">source culture</param>
/// <param name="targetCulture">target culture</param>
/// <returns>Number translator for target culture</returns>
public static NumberTranslator Create(CultureInfo sourceCulture, CultureInfo targetCulture)
{
🚧 [Dev][Build] .NET 8 Upgrade (#28655) * Upgraded projects to target .NET 8 * Updated .NET runtime package targets to use latest .NET 8 build * Updated PowerToys Interop to target .NET 8 * Switch to use ArgumentNullException.ThrowIfNull * ArgumentNullException.ThrowIfNull for CropAndLockViewModel * Switching to ObjectDisposedException.ThrowIf * Upgrade System.ComponentModel.Composition to 8.0 * ArgumentNullException.ThrowIfNull in Helper * Switch to StartsWith using StringComparison.Ordinal * Disabled CA1859, CA1716, SYSLIB1096 analyzers * Update RIDs to reflect breaking changes in .NET 8 * Updated Microsoft NuGet packages to RC1 * Updated Analyzer package to latest .NET 8 preview package * CA1854: Use TryGetValue instead of ContainsKey * [Build] Update TFM to .NET 8 for publish profiles * [Analyzers] Remove CA1309, CA1860-CA1865, CA1869, CA2208 from warning. * [Analyzers] Fix for C26495 * [Analyzers] Disable CS1615, CS9191 * [CI] Target .NET 8 in YAML * [CI] Add .NET preview version flag temporarily. * [FileLocksmith] Update TFM to .NET 8 * [CI] Switch to preview agent * [CI] Update NOTICE.md * [CI] Update Release to target .NET 8 and use Preview agent * [Analyzers] Disable CA1854 * Fix typo * Updated Microsoft.CodeAnalysis.NetAnalyzers to latest preview Updated packages to rc2 * [Analyzers][CPP] Turn off warning for 5271 * [Analyzers][CPP] Turn off warning for 26493 * [KeyboardListener] Add mutex include to resolve error * [PT Run][Folder] Use static SearchValues to resolve CA1870 * [PowerLauncher] Fix TryGetValue * [MouseJumpSettings] Use ArgumentNullException.ThrowIfNull * [Build] Disable parallel dotnet tool restore * [Build] No cache of dotnet tool packages * [Build] Temporarily move .NET 8 SDK task before XAML formatting * [Build][Temp] Try using .NET 7 prior to XAML formatting and then switch to .NET 8 after * [Build] Use .NET 6 for XAML Styler * [CI] Updated NOTICE.md * [FancyZones] Update TFM to .NET 8 * [EnvVar] Update TFM to .NET 8 and update RID * [EnvVar] Use ArgumentNullException.ThrowIfNull * [Dev] Updated packages to .NET 8 RTM version * [Dev] Updated Microsoft.CodeAnalysis.NetAnalyzers to latest * [CI] Updated NOTICE.md with latest package versions * Fix new utility target fameworks and runtimeids * Don't use preview images anymore * [CI] Add script to update VCToolsVersion environment variable * [CI] Add Step to Verify VCToolsVersion * [CI] Use latest flag for vswhere to set proper VCToolsVersion * Add VCToolsVersion checking to release.yml * Remove net publishing from local/ PR CI builds * Revert "Remove net publishing from local/ PR CI builds" This reverts commit f469778996c5053e8bf93233e8191858c46f6420. * Only publish necessary projects * Add verbosity to release pipelines builds of PowerTOys * Set VCToolsVersion for publish.cmd when called from installer * [Installer] Moved project publish logic to MSBuild Task * [CI] Revert using publish.cmd * [CI] Set VCToolsVersion and unset ClearDevCommandPromptEnvVars property * Installer publishes for x64 too * Revert "Add verbosity to release pipelines builds of PowerTOys" This reverts commit 654d4a7f7852e95e44df315c473c02d38b1f538b. * [Dev] Update CodeAnalysis library to non-preview package * Remove unneeded warning removal * Fix Notice.md * Rename VCToolsVersion file and task name * Remove unneeded mutex header include --------- Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
2023-11-22 12:46:59 -05:00
ArgumentNullException.ThrowIfNull(sourceCulture);
ArgumentNullException.ThrowIfNull(targetCulture);
2020-08-17 10:00:56 -07:00
return new NumberTranslator(sourceCulture, targetCulture);
2020-08-17 10:00:56 -07:00
}
/// <summary>
/// Translate from source to target culture.
/// </summary>
/// <param name="input">input string to translate</param>
/// <returns>translated string</returns>
public string Translate(string input)
{
return Translate(input, sourceCulture, targetCulture, splitRegexForSource);
}
/// <summary>
/// Translate from target to source culture.
/// </summary>
/// <param name="input">input string to translate back to source culture</param>
/// <returns>source culture string</returns>
public string TranslateBack(string input)
{
return Translate(input, targetCulture, sourceCulture, splitRegexForTarget);
}
private static string Translate(string input, CultureInfo cultureFrom, CultureInfo cultureTo, Regex splitRegex)
{
var outputBuilder = new StringBuilder();
[Run] Fix Scientific Notation Errors in Calculator Plugin (#28884) * Parse scientific notation properly After adding logic to replace `e` as a mathematical constant, there are bugs when trying to use expressions like `5e3`. This change parses the `<number>e<number>` format into expanded form to prevent replacement with the constant. Regex explanation: `(\d+\.*\d*)[eE](-*\d+) `(\d+\.*\d*)`: Match any number of digits, followed by an optional `.` and more optional digits. The expression is used to capture things like: `5.0`, `1.`, `1` before the `e` `[eE]`: Match either upper or lowercase `e` `(-*\d+)`: Capture an optional `-` sign as well as a number of digits * Update regex to be more tolerant of weird entries The new regex captures a wider variety of numbers. See this post for details on the regex used: https://stackoverflow.com/a/23872060 * Fix regular expression failing unit tests Using `[` didn't capture the expression properly. Had to use `(` instead. * Allow only for uppercase E * Only allow one decimal in second grouping * Support various decimal separator types Previous regular expression did not allow decimal separators that were not ".". The new expression uses the culture info decimal separator instead, which should work better. * Only allow integers after `E` * Remove single use variable * Update regex to only accept integers after `E` Missed this expression in my last update. Whoops * Update src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/CalculateHelper.cs * Update NumberTranslator to parse hex as a whole * Remove `hexRegex` as object member The hex regex stuff is only used once, there's no need to keep track of it in the object's state. Just create it during the translation process. * Add unit tests for sci notation in other cultures --------- Co-authored-by: Jaime Bernardo <jaime@janeasystems.com> Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com>
2024-01-03 06:22:09 -05:00
var hexRegex = new Regex(@"(?:(0x[\da-fA-F]+))");
2020-08-17 10:00:56 -07:00
[Run] Fix Scientific Notation Errors in Calculator Plugin (#28884) * Parse scientific notation properly After adding logic to replace `e` as a mathematical constant, there are bugs when trying to use expressions like `5e3`. This change parses the `<number>e<number>` format into expanded form to prevent replacement with the constant. Regex explanation: `(\d+\.*\d*)[eE](-*\d+) `(\d+\.*\d*)`: Match any number of digits, followed by an optional `.` and more optional digits. The expression is used to capture things like: `5.0`, `1.`, `1` before the `e` `[eE]`: Match either upper or lowercase `e` `(-*\d+)`: Capture an optional `-` sign as well as a number of digits * Update regex to be more tolerant of weird entries The new regex captures a wider variety of numbers. See this post for details on the regex used: https://stackoverflow.com/a/23872060 * Fix regular expression failing unit tests Using `[` didn't capture the expression properly. Had to use `(` instead. * Allow only for uppercase E * Only allow one decimal in second grouping * Support various decimal separator types Previous regular expression did not allow decimal separators that were not ".". The new expression uses the culture info decimal separator instead, which should work better. * Only allow integers after `E` * Remove single use variable * Update regex to only accept integers after `E` Missed this expression in my last update. Whoops * Update src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/CalculateHelper.cs * Update NumberTranslator to parse hex as a whole * Remove `hexRegex` as object member The hex regex stuff is only used once, there's no need to keep track of it in the object's state. Just create it during the translation process. * Add unit tests for sci notation in other cultures --------- Co-authored-by: Jaime Bernardo <jaime@janeasystems.com> Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com>
2024-01-03 06:22:09 -05:00
string[] hexTokens = hexRegex.Split(input);
foreach (string hexToken in hexTokens)
2020-08-17 10:00:56 -07:00
{
[Run] Fix Scientific Notation Errors in Calculator Plugin (#28884) * Parse scientific notation properly After adding logic to replace `e` as a mathematical constant, there are bugs when trying to use expressions like `5e3`. This change parses the `<number>e<number>` format into expanded form to prevent replacement with the constant. Regex explanation: `(\d+\.*\d*)[eE](-*\d+) `(\d+\.*\d*)`: Match any number of digits, followed by an optional `.` and more optional digits. The expression is used to capture things like: `5.0`, `1.`, `1` before the `e` `[eE]`: Match either upper or lowercase `e` `(-*\d+)`: Capture an optional `-` sign as well as a number of digits * Update regex to be more tolerant of weird entries The new regex captures a wider variety of numbers. See this post for details on the regex used: https://stackoverflow.com/a/23872060 * Fix regular expression failing unit tests Using `[` didn't capture the expression properly. Had to use `(` instead. * Allow only for uppercase E * Only allow one decimal in second grouping * Support various decimal separator types Previous regular expression did not allow decimal separators that were not ".". The new expression uses the culture info decimal separator instead, which should work better. * Only allow integers after `E` * Remove single use variable * Update regex to only accept integers after `E` Missed this expression in my last update. Whoops * Update src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/CalculateHelper.cs * Update NumberTranslator to parse hex as a whole * Remove `hexRegex` as object member The hex regex stuff is only used once, there's no need to keep track of it in the object's state. Just create it during the translation process. * Add unit tests for sci notation in other cultures --------- Co-authored-by: Jaime Bernardo <jaime@janeasystems.com> Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com>
2024-01-03 06:22:09 -05:00
if (hexToken.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase))
{
// Mages engine has issues processing large hex number (larger than 7 hex digits + 0x prefix = 9 characters). So we convert it to decimal and pass it to the engine.
if (hexToken.Length > 9)
{
try
{
long num = Convert.ToInt64(hexToken, 16);
string numStr = num.ToString(cultureFrom);
outputBuilder.Append(numStr);
}
catch (Exception)
{
outputBuilder.Append(hexToken);
}
}
else
{
outputBuilder.Append(hexToken);
}
[Run] Fix Scientific Notation Errors in Calculator Plugin (#28884) * Parse scientific notation properly After adding logic to replace `e` as a mathematical constant, there are bugs when trying to use expressions like `5e3`. This change parses the `<number>e<number>` format into expanded form to prevent replacement with the constant. Regex explanation: `(\d+\.*\d*)[eE](-*\d+) `(\d+\.*\d*)`: Match any number of digits, followed by an optional `.` and more optional digits. The expression is used to capture things like: `5.0`, `1.`, `1` before the `e` `[eE]`: Match either upper or lowercase `e` `(-*\d+)`: Capture an optional `-` sign as well as a number of digits * Update regex to be more tolerant of weird entries The new regex captures a wider variety of numbers. See this post for details on the regex used: https://stackoverflow.com/a/23872060 * Fix regular expression failing unit tests Using `[` didn't capture the expression properly. Had to use `(` instead. * Allow only for uppercase E * Only allow one decimal in second grouping * Support various decimal separator types Previous regular expression did not allow decimal separators that were not ".". The new expression uses the culture info decimal separator instead, which should work better. * Only allow integers after `E` * Remove single use variable * Update regex to only accept integers after `E` Missed this expression in my last update. Whoops * Update src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/CalculateHelper.cs * Update NumberTranslator to parse hex as a whole * Remove `hexRegex` as object member The hex regex stuff is only used once, there's no need to keep track of it in the object's state. Just create it during the translation process. * Add unit tests for sci notation in other cultures --------- Co-authored-by: Jaime Bernardo <jaime@janeasystems.com> Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com>
2024-01-03 06:22:09 -05:00
continue;
}
[Run] Fix Scientific Notation Errors in Calculator Plugin (#28884) * Parse scientific notation properly After adding logic to replace `e` as a mathematical constant, there are bugs when trying to use expressions like `5e3`. This change parses the `<number>e<number>` format into expanded form to prevent replacement with the constant. Regex explanation: `(\d+\.*\d*)[eE](-*\d+) `(\d+\.*\d*)`: Match any number of digits, followed by an optional `.` and more optional digits. The expression is used to capture things like: `5.0`, `1.`, `1` before the `e` `[eE]`: Match either upper or lowercase `e` `(-*\d+)`: Capture an optional `-` sign as well as a number of digits * Update regex to be more tolerant of weird entries The new regex captures a wider variety of numbers. See this post for details on the regex used: https://stackoverflow.com/a/23872060 * Fix regular expression failing unit tests Using `[` didn't capture the expression properly. Had to use `(` instead. * Allow only for uppercase E * Only allow one decimal in second grouping * Support various decimal separator types Previous regular expression did not allow decimal separators that were not ".". The new expression uses the culture info decimal separator instead, which should work better. * Only allow integers after `E` * Remove single use variable * Update regex to only accept integers after `E` Missed this expression in my last update. Whoops * Update src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/CalculateHelper.cs * Update NumberTranslator to parse hex as a whole * Remove `hexRegex` as object member The hex regex stuff is only used once, there's no need to keep track of it in the object's state. Just create it during the translation process. * Add unit tests for sci notation in other cultures --------- Co-authored-by: Jaime Bernardo <jaime@janeasystems.com> Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com>
2024-01-03 06:22:09 -05:00
string[] tokens = splitRegex.Split(hexToken);
foreach (string token in tokens)
{
[Run] Fix Scientific Notation Errors in Calculator Plugin (#28884) * Parse scientific notation properly After adding logic to replace `e` as a mathematical constant, there are bugs when trying to use expressions like `5e3`. This change parses the `<number>e<number>` format into expanded form to prevent replacement with the constant. Regex explanation: `(\d+\.*\d*)[eE](-*\d+) `(\d+\.*\d*)`: Match any number of digits, followed by an optional `.` and more optional digits. The expression is used to capture things like: `5.0`, `1.`, `1` before the `e` `[eE]`: Match either upper or lowercase `e` `(-*\d+)`: Capture an optional `-` sign as well as a number of digits * Update regex to be more tolerant of weird entries The new regex captures a wider variety of numbers. See this post for details on the regex used: https://stackoverflow.com/a/23872060 * Fix regular expression failing unit tests Using `[` didn't capture the expression properly. Had to use `(` instead. * Allow only for uppercase E * Only allow one decimal in second grouping * Support various decimal separator types Previous regular expression did not allow decimal separators that were not ".". The new expression uses the culture info decimal separator instead, which should work better. * Only allow integers after `E` * Remove single use variable * Update regex to only accept integers after `E` Missed this expression in my last update. Whoops * Update src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/CalculateHelper.cs * Update NumberTranslator to parse hex as a whole * Remove `hexRegex` as object member The hex regex stuff is only used once, there's no need to keep track of it in the object's state. Just create it during the translation process. * Add unit tests for sci notation in other cultures --------- Co-authored-by: Jaime Bernardo <jaime@janeasystems.com> Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com>
2024-01-03 06:22:09 -05:00
int leadingZeroCount = 0;
// Count leading zero characters.
foreach (char c in token)
{
[Run] Fix Scientific Notation Errors in Calculator Plugin (#28884) * Parse scientific notation properly After adding logic to replace `e` as a mathematical constant, there are bugs when trying to use expressions like `5e3`. This change parses the `<number>e<number>` format into expanded form to prevent replacement with the constant. Regex explanation: `(\d+\.*\d*)[eE](-*\d+) `(\d+\.*\d*)`: Match any number of digits, followed by an optional `.` and more optional digits. The expression is used to capture things like: `5.0`, `1.`, `1` before the `e` `[eE]`: Match either upper or lowercase `e` `(-*\d+)`: Capture an optional `-` sign as well as a number of digits * Update regex to be more tolerant of weird entries The new regex captures a wider variety of numbers. See this post for details on the regex used: https://stackoverflow.com/a/23872060 * Fix regular expression failing unit tests Using `[` didn't capture the expression properly. Had to use `(` instead. * Allow only for uppercase E * Only allow one decimal in second grouping * Support various decimal separator types Previous regular expression did not allow decimal separators that were not ".". The new expression uses the culture info decimal separator instead, which should work better. * Only allow integers after `E` * Remove single use variable * Update regex to only accept integers after `E` Missed this expression in my last update. Whoops * Update src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/CalculateHelper.cs * Update NumberTranslator to parse hex as a whole * Remove `hexRegex` as object member The hex regex stuff is only used once, there's no need to keep track of it in the object's state. Just create it during the translation process. * Add unit tests for sci notation in other cultures --------- Co-authored-by: Jaime Bernardo <jaime@janeasystems.com> Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com>
2024-01-03 06:22:09 -05:00
if (c != '0')
{
break;
}
[Run] Fix Scientific Notation Errors in Calculator Plugin (#28884) * Parse scientific notation properly After adding logic to replace `e` as a mathematical constant, there are bugs when trying to use expressions like `5e3`. This change parses the `<number>e<number>` format into expanded form to prevent replacement with the constant. Regex explanation: `(\d+\.*\d*)[eE](-*\d+) `(\d+\.*\d*)`: Match any number of digits, followed by an optional `.` and more optional digits. The expression is used to capture things like: `5.0`, `1.`, `1` before the `e` `[eE]`: Match either upper or lowercase `e` `(-*\d+)`: Capture an optional `-` sign as well as a number of digits * Update regex to be more tolerant of weird entries The new regex captures a wider variety of numbers. See this post for details on the regex used: https://stackoverflow.com/a/23872060 * Fix regular expression failing unit tests Using `[` didn't capture the expression properly. Had to use `(` instead. * Allow only for uppercase E * Only allow one decimal in second grouping * Support various decimal separator types Previous regular expression did not allow decimal separators that were not ".". The new expression uses the culture info decimal separator instead, which should work better. * Only allow integers after `E` * Remove single use variable * Update regex to only accept integers after `E` Missed this expression in my last update. Whoops * Update src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/CalculateHelper.cs * Update NumberTranslator to parse hex as a whole * Remove `hexRegex` as object member The hex regex stuff is only used once, there's no need to keep track of it in the object's state. Just create it during the translation process. * Add unit tests for sci notation in other cultures --------- Co-authored-by: Jaime Bernardo <jaime@janeasystems.com> Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com>
2024-01-03 06:22:09 -05:00
leadingZeroCount++;
}
[Run] Fix Scientific Notation Errors in Calculator Plugin (#28884) * Parse scientific notation properly After adding logic to replace `e` as a mathematical constant, there are bugs when trying to use expressions like `5e3`. This change parses the `<number>e<number>` format into expanded form to prevent replacement with the constant. Regex explanation: `(\d+\.*\d*)[eE](-*\d+) `(\d+\.*\d*)`: Match any number of digits, followed by an optional `.` and more optional digits. The expression is used to capture things like: `5.0`, `1.`, `1` before the `e` `[eE]`: Match either upper or lowercase `e` `(-*\d+)`: Capture an optional `-` sign as well as a number of digits * Update regex to be more tolerant of weird entries The new regex captures a wider variety of numbers. See this post for details on the regex used: https://stackoverflow.com/a/23872060 * Fix regular expression failing unit tests Using `[` didn't capture the expression properly. Had to use `(` instead. * Allow only for uppercase E * Only allow one decimal in second grouping * Support various decimal separator types Previous regular expression did not allow decimal separators that were not ".". The new expression uses the culture info decimal separator instead, which should work better. * Only allow integers after `E` * Remove single use variable * Update regex to only accept integers after `E` Missed this expression in my last update. Whoops * Update src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/CalculateHelper.cs * Update NumberTranslator to parse hex as a whole * Remove `hexRegex` as object member The hex regex stuff is only used once, there's no need to keep track of it in the object's state. Just create it during the translation process. * Add unit tests for sci notation in other cultures --------- Co-authored-by: Jaime Bernardo <jaime@janeasystems.com> Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com>
2024-01-03 06:22:09 -05:00
// number is all zero characters. no need to add zero characters at the end.
if (token.Length == leadingZeroCount)
{
leadingZeroCount = 0;
}
[Run] Fix Scientific Notation Errors in Calculator Plugin (#28884) * Parse scientific notation properly After adding logic to replace `e` as a mathematical constant, there are bugs when trying to use expressions like `5e3`. This change parses the `<number>e<number>` format into expanded form to prevent replacement with the constant. Regex explanation: `(\d+\.*\d*)[eE](-*\d+) `(\d+\.*\d*)`: Match any number of digits, followed by an optional `.` and more optional digits. The expression is used to capture things like: `5.0`, `1.`, `1` before the `e` `[eE]`: Match either upper or lowercase `e` `(-*\d+)`: Capture an optional `-` sign as well as a number of digits * Update regex to be more tolerant of weird entries The new regex captures a wider variety of numbers. See this post for details on the regex used: https://stackoverflow.com/a/23872060 * Fix regular expression failing unit tests Using `[` didn't capture the expression properly. Had to use `(` instead. * Allow only for uppercase E * Only allow one decimal in second grouping * Support various decimal separator types Previous regular expression did not allow decimal separators that were not ".". The new expression uses the culture info decimal separator instead, which should work better. * Only allow integers after `E` * Remove single use variable * Update regex to only accept integers after `E` Missed this expression in my last update. Whoops * Update src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/CalculateHelper.cs * Update NumberTranslator to parse hex as a whole * Remove `hexRegex` as object member The hex regex stuff is only used once, there's no need to keep track of it in the object's state. Just create it during the translation process. * Add unit tests for sci notation in other cultures --------- Co-authored-by: Jaime Bernardo <jaime@janeasystems.com> Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com>
2024-01-03 06:22:09 -05:00
decimal number;
[Run] Fix Scientific Notation Errors in Calculator Plugin (#28884) * Parse scientific notation properly After adding logic to replace `e` as a mathematical constant, there are bugs when trying to use expressions like `5e3`. This change parses the `<number>e<number>` format into expanded form to prevent replacement with the constant. Regex explanation: `(\d+\.*\d*)[eE](-*\d+) `(\d+\.*\d*)`: Match any number of digits, followed by an optional `.` and more optional digits. The expression is used to capture things like: `5.0`, `1.`, `1` before the `e` `[eE]`: Match either upper or lowercase `e` `(-*\d+)`: Capture an optional `-` sign as well as a number of digits * Update regex to be more tolerant of weird entries The new regex captures a wider variety of numbers. See this post for details on the regex used: https://stackoverflow.com/a/23872060 * Fix regular expression failing unit tests Using `[` didn't capture the expression properly. Had to use `(` instead. * Allow only for uppercase E * Only allow one decimal in second grouping * Support various decimal separator types Previous regular expression did not allow decimal separators that were not ".". The new expression uses the culture info decimal separator instead, which should work better. * Only allow integers after `E` * Remove single use variable * Update regex to only accept integers after `E` Missed this expression in my last update. Whoops * Update src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/CalculateHelper.cs * Update NumberTranslator to parse hex as a whole * Remove `hexRegex` as object member The hex regex stuff is only used once, there's no need to keep track of it in the object's state. Just create it during the translation process. * Add unit tests for sci notation in other cultures --------- Co-authored-by: Jaime Bernardo <jaime@janeasystems.com> Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com>
2024-01-03 06:22:09 -05:00
outputBuilder.Append(
decimal.TryParse(token, NumberStyles.Number, cultureFrom, out number)
? (new string('0', leadingZeroCount) + number.ToString(cultureTo))
: token);
}
2020-08-17 10:00:56 -07:00
}
return outputBuilder.ToString();
}
private static Regex GetSplitRegex(CultureInfo culture)
{
[Run] Fix Scientific Notation Errors in Calculator Plugin (#28884) * Parse scientific notation properly After adding logic to replace `e` as a mathematical constant, there are bugs when trying to use expressions like `5e3`. This change parses the `<number>e<number>` format into expanded form to prevent replacement with the constant. Regex explanation: `(\d+\.*\d*)[eE](-*\d+) `(\d+\.*\d*)`: Match any number of digits, followed by an optional `.` and more optional digits. The expression is used to capture things like: `5.0`, `1.`, `1` before the `e` `[eE]`: Match either upper or lowercase `e` `(-*\d+)`: Capture an optional `-` sign as well as a number of digits * Update regex to be more tolerant of weird entries The new regex captures a wider variety of numbers. See this post for details on the regex used: https://stackoverflow.com/a/23872060 * Fix regular expression failing unit tests Using `[` didn't capture the expression properly. Had to use `(` instead. * Allow only for uppercase E * Only allow one decimal in second grouping * Support various decimal separator types Previous regular expression did not allow decimal separators that were not ".". The new expression uses the culture info decimal separator instead, which should work better. * Only allow integers after `E` * Remove single use variable * Update regex to only accept integers after `E` Missed this expression in my last update. Whoops * Update src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/CalculateHelper.cs * Update NumberTranslator to parse hex as a whole * Remove `hexRegex` as object member The hex regex stuff is only used once, there's no need to keep track of it in the object's state. Just create it during the translation process. * Add unit tests for sci notation in other cultures --------- Co-authored-by: Jaime Bernardo <jaime@janeasystems.com> Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com>
2024-01-03 06:22:09 -05:00
var splitPattern = $"((?:\\d|{Regex.Escape(culture.NumberFormat.NumberDecimalSeparator)}";
2020-08-17 10:00:56 -07:00
if (!string.IsNullOrEmpty(culture.NumberFormat.NumberGroupSeparator))
{
splitPattern += $"|{Regex.Escape(culture.NumberFormat.NumberGroupSeparator)}";
}
splitPattern += ")+)";
return new Regex(splitPattern);
}
}
}