[PT Run][Calculator] Fix processing large hex numbers (#31135)

* [PT Run][Calculator] Fix processing large hex files

* Spellcheck

* Add tests
This commit is contained in:
Stefan Markovic 2024-01-26 15:09:38 +01:00 committed by GitHub
parent 1b20ba8af0
commit 421a8578e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 59 additions and 3 deletions

View File

@ -23,7 +23,7 @@
<PackageVersion Include="hyjiacan.pinyin4net" Version="4.1.1" />
<PackageVersion Include="Interop.Microsoft.Office.Interop.OneNote" Version="1.1.0.2" />
<PackageVersion Include="LazyCache" Version="2.4.0" />
<PackageVersion Include="Mages" Version="2.0.1" />
<PackageVersion Include="Mages" Version="2.0.2" />
<PackageVersion Include="Markdig.Signed" Version="0.34.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0" />
<PackageVersion Include="Microsoft.Data.Sqlite" Version="8.0.0" />

View File

@ -1313,7 +1313,7 @@ EXHIBIT A -Mozilla Public License.
- hyjiacan.pinyin4net 4.1.1
- Interop.Microsoft.Office.Interop.OneNote 1.1.0.2
- LazyCache 2.4.0
- Mages 2.0.1
- Mages 2.0.2
- Markdig.Signed 0.34.0
- Microsoft.CodeAnalysis.NetAnalyzers 8.0.0
- Microsoft.Data.Sqlite 8.0.0

View File

@ -162,5 +162,22 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
Assert.IsNotNull(result);
Assert.AreEqual(expectedResult, result);
}
[DataTestMethod]
[DataRow("en-US", "0xF000", "0xF000")]
[DataRow("en-US", "0xf4572220", "4099351072")]
[DataRow("en-US", "0x12345678", "305419896")]
public void Translate_LargeHexadecimalNumbersToDecimal(string sourceCultureName, string input, string expectedResult)
{
// Arrange
var translator = NumberTranslator.Create(new CultureInfo(sourceCultureName, false), new CultureInfo("en-US", false));
// Act
var result = translator.Translate(input);
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(expectedResult, result);
}
}
}

View File

@ -233,5 +233,26 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
Assert.AreEqual(answer.ToString(CultureInfo.CurrentCulture), result);
Assert.AreEqual(answer.ToString(CultureInfo.CurrentCulture), resultWithKeyword);
}
[DataTestMethod]
[DataRow("0x1234+0x1234", 9320)]
[DataRow("0x1234-0x1234", 0)]
[DataRow("0x12345678+0x12345678", 610839792)]
[DataRow("0xf4572220-0xf4572410", -496)]
public void RightAnswerForLargeHexadecimalNumbers(string typedString, double answer)
{
// Setup
Mock<Main> main = new();
Query expectedQuery = new(typedString);
Query expectedQueryWithKeyword = new("=" + typedString, "=");
// Act
var result = main.Object.Query(expectedQuery).FirstOrDefault()?.Title;
var resultWithKeyword = main.Object.Query(expectedQueryWithKeyword).FirstOrDefault()?.Title;
// Assert
Assert.AreEqual(answer.ToString(CultureInfo.CurrentCulture), result);
Assert.AreEqual(answer.ToString(CultureInfo.CurrentCulture), resultWithKeyword);
}
}
}

View File

@ -74,7 +74,25 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
{
if (hexToken.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase))
{
outputBuilder.Append(hexToken);
// 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);
}
continue;
}