[PTRun][UnitConverter]Support negative values (#30776)

This commit is contained in:
Dub1shu
2024-01-17 19:56:34 +09:00
committed by GitHub
parent 0be120d293
commit 3802e91a05
2 changed files with 30 additions and 5 deletions

View File

@@ -19,6 +19,12 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter.UnitTest
[DataRow(new string[] { "1'5\"" }, new string[] { "1", "'", "5", "\"" })] [DataRow(new string[] { "1'5\"" }, new string[] { "1", "'", "5", "\"" })]
[DataRow(new string[] { "5\"" }, new string[] { "5", "\"" })] [DataRow(new string[] { "5\"" }, new string[] { "5", "\"" })]
[DataRow(new string[] { "1'5" }, new string[] { "1", "'", "5" })] [DataRow(new string[] { "1'5" }, new string[] { "1", "'", "5" })]
[DataRow(new string[] { "-1,5'" }, new string[] { "-1,5", "'" })]
[DataRow(new string[] { "-1.5'" }, new string[] { "-1.5", "'" })]
[DataRow(new string[] { "-1'" }, new string[] { "-1", "'" })]
[DataRow(new string[] { "-1'5\"" }, new string[] { "-1", "'", "5", "\"" })]
[DataRow(new string[] { "-5\"" }, new string[] { "-5", "\"" })]
[DataRow(new string[] { "-1'5" }, new string[] { "-1", "'", "5" })]
public void RegexSplitsInput(string[] input, string[] expectedResult) public void RegexSplitsInput(string[] input, string[] expectedResult)
{ {
string[] shortsplit = InputInterpreter.RegexSplitter(input); string[] shortsplit = InputInterpreter.RegexSplitter(input);
@@ -27,6 +33,7 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter.UnitTest
[DataTestMethod] [DataTestMethod]
[DataRow(new string[] { "1cm", "to", "mm" }, new string[] { "1", "cm", "to", "mm" })] [DataRow(new string[] { "1cm", "to", "mm" }, new string[] { "1", "cm", "to", "mm" })]
[DataRow(new string[] { "-1cm", "to", "mm" }, new string[] { "-1", "cm", "to", "mm" })]
public void InsertsSpaces(string[] input, string[] expectedResult) public void InsertsSpaces(string[] input, string[] expectedResult)
{ {
InputInterpreter.InputSpaceInserter(ref input); InputInterpreter.InputSpaceInserter(ref input);
@@ -38,6 +45,10 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter.UnitTest
[DataRow(new string[] { "1\"", "in", "cm" }, new string[] { "1", "inch", "in", "cm" })] [DataRow(new string[] { "1\"", "in", "cm" }, new string[] { "1", "inch", "in", "cm" })]
[DataRow(new string[] { "1'6", "in", "cm" }, new string[] { "1.5", "foot", "in", "cm" })] [DataRow(new string[] { "1'6", "in", "cm" }, new string[] { "1.5", "foot", "in", "cm" })]
[DataRow(new string[] { "1'6\"", "in", "cm" }, new string[] { "1.5", "foot", "in", "cm" })] [DataRow(new string[] { "1'6\"", "in", "cm" }, new string[] { "1.5", "foot", "in", "cm" })]
[DataRow(new string[] { "-1'", "in", "cm" }, new string[] { "-1", "foot", "in", "cm" })]
[DataRow(new string[] { "-1\"", "in", "cm" }, new string[] { "-1", "inch", "in", "cm" })]
[DataRow(new string[] { "-1'6", "in", "cm" }, new string[] { "-1.5", "foot", "in", "cm" })]
[DataRow(new string[] { "-1'6\"", "in", "cm" }, new string[] { "-1.5", "foot", "in", "cm" })]
public void HandlesShorthandFeetInchNotation(string[] input, string[] expectedResult) public void HandlesShorthandFeetInchNotation(string[] input, string[] expectedResult)
{ {
InputInterpreter.ShorthandFeetInchHandler(ref input, CultureInfo.InvariantCulture); InputInterpreter.ShorthandFeetInchHandler(ref input, CultureInfo.InvariantCulture);
@@ -69,6 +80,8 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter.UnitTest
[DataTestMethod] [DataTestMethod]
[DataRow("a f in c")] [DataRow("a f in c")]
[DataRow("12 f in")] [DataRow("12 f in")]
[DataRow("1-2 f in c")]
[DataRow("12- f in c")]
public void ParseInvalidQueries(string queryString) public void ParseInvalidQueries(string queryString)
{ {
Query query = new Query(queryString); Query query = new Query(queryString);
@@ -79,6 +92,8 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter.UnitTest
[DataTestMethod] [DataTestMethod]
[DataRow("12 f in c", 12)] [DataRow("12 f in c", 12)]
[DataRow("10m to cm", 10)] [DataRow("10m to cm", 10)]
[DataRow("-12 f in c", -12)]
[DataRow("-10m to cm", -10)]
public void ParseValidQueries(string queryString, double result) public void ParseValidQueries(string queryString, double result)
{ {
Query query = new Query(queryString); Query query = new Query(queryString);

View File

@@ -14,11 +14,11 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
{ {
public static class InputInterpreter public static class InputInterpreter
{ {
private static string pattern = @"(?<=\d)(?![,.])(?=\D)|(?<=\D)(?<![,.])(?=\d)"; private static readonly string Pattern = @"(?<=\d)(?![,.\-])(?=[\D])|(?<=[\D])(?<![,.\-])(?=\d)";
public static string[] RegexSplitter(string[] split) public static string[] RegexSplitter(string[] split)
{ {
return Regex.Split(split[0], pattern); return Regex.Split(split[0], Pattern);
} }
/// <summary> /// <summary>
@@ -31,7 +31,7 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
return; return;
} }
string[] parseInputWithoutSpace = Regex.Split(split[0], pattern); string[] parseInputWithoutSpace = Regex.Split(split[0], Pattern);
if (parseInputWithoutSpace.Length > 1) if (parseInputWithoutSpace.Length > 1)
{ {
@@ -80,6 +80,12 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
// ex: 1'2 and 1'2" // ex: 1'2 and 1'2"
if (shortsplit[1] == "\'") if (shortsplit[1] == "\'")
{ {
bool isNegative = shortsplit[0].StartsWith('-');
if (isNegative)
{
shortsplit[0] = shortsplit[0].Remove(0, 1);
}
bool isFeet = double.TryParse(shortsplit[0], NumberStyles.AllowDecimalPoint, culture, out double feet); bool isFeet = double.TryParse(shortsplit[0], NumberStyles.AllowDecimalPoint, culture, out double feet);
bool isInches = double.TryParse(shortsplit[2], NumberStyles.AllowDecimalPoint, culture, out double inches); bool isInches = double.TryParse(shortsplit[2], NumberStyles.AllowDecimalPoint, culture, out double inches);
@@ -89,9 +95,13 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
break; break;
} }
string convertedTotalInFeet = Length.FromFeetInches(feet, inches).Feet.ToString(culture); double convertedTotalInFeet = Length.FromFeetInches(feet, inches).Feet;
if (isNegative)
{
convertedTotalInFeet *= -1;
}
string[] newInput = new string[] { convertedTotalInFeet, "foot", split[1], split[2] }; string[] newInput = new string[] { convertedTotalInFeet.ToString(culture), "foot", split[1], split[2] };
split = newInput; split = newInput;
} }