Skip to content

Commit

Permalink
[PTRun][UnitConverter]Support negative values (#30776)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dub1shu authored Jan 17, 2024
1 parent 0be120d commit 3802e91
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public class InputInterpreterTests
[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" })]
[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)
{
string[] shortsplit = InputInterpreter.RegexSplitter(input);
Expand All @@ -27,6 +33,7 @@ public void RegexSplitsInput(string[] input, string[] expectedResult)

[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" })]
public void InsertsSpaces(string[] input, string[] expectedResult)
{
InputInterpreter.InputSpaceInserter(ref input);
Expand All @@ -38,6 +45,10 @@ public void InsertsSpaces(string[] input, string[] expectedResult)
[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'", "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)
{
InputInterpreter.ShorthandFeetInchHandler(ref input, CultureInfo.InvariantCulture);
Expand Down Expand Up @@ -69,6 +80,8 @@ public void PrefixesDegrees(string[] input, string[] expectedResult)
[DataTestMethod]
[DataRow("a f in c")]
[DataRow("12 f in")]
[DataRow("1-2 f in c")]
[DataRow("12- f in c")]
public void ParseInvalidQueries(string queryString)
{
Query query = new Query(queryString);
Expand All @@ -79,6 +92,8 @@ public void ParseInvalidQueries(string queryString)
[DataTestMethod]
[DataRow("12 f in c", 12)]
[DataRow("10m to cm", 10)]
[DataRow("-12 f in c", -12)]
[DataRow("-10m to cm", -10)]
public void ParseValidQueries(string queryString, double result)
{
Query query = new Query(queryString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
{
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)
{
return Regex.Split(split[0], pattern);
return Regex.Split(split[0], Pattern);
}

/// <summary>
Expand All @@ -31,7 +31,7 @@ public static void InputSpaceInserter(ref string[] split)
return;
}

string[] parseInputWithoutSpace = Regex.Split(split[0], pattern);
string[] parseInputWithoutSpace = Regex.Split(split[0], Pattern);

if (parseInputWithoutSpace.Length > 1)
{
Expand Down Expand Up @@ -80,6 +80,12 @@ public static void ShorthandFeetInchHandler(ref string[] split, CultureInfo cult
// ex: 1'2 and 1'2"
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 isInches = double.TryParse(shortsplit[2], NumberStyles.AllowDecimalPoint, culture, out double inches);

Expand All @@ -89,9 +95,13 @@ public static void ShorthandFeetInchHandler(ref string[] split, CultureInfo cult
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;
}

Expand Down

0 comments on commit 3802e91

Please sign in to comment.