Skip to content

Fix for negative zero with feet inches parsing. Added tests #675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions UnitsNet.Tests/CustomCode/LengthTests.FeetInches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public void FeetInchesRoundTrip()
[InlineData("1′", 1)] // Feet only
[InlineData("1\"", 0.08333333)] // Inches only
[InlineData("1″", 0.08333333)] // Inches only
[InlineData("0' 1\"", 0.08333333)] // Inches only
[InlineData("0' 1″", 0.08333333)] // Inches only
[InlineData("0′ 1\"", 0.08333333)] // Inches only
[InlineData("0′ 1″", 0.08333333)] // Inches only
[InlineData("1' 1\"", 1.08333333)] // Normal form
[InlineData("1′ 1″", 1.08333333)] // Normal form
[InlineData(" 1′ 1″ ", 1.08333333)] // Normal form, requires trimming
Expand All @@ -45,6 +49,10 @@ public void FeetInchesRoundTrip()
[InlineData("-1′", -1)] // Feet only
[InlineData("-1\"", -0.08333333)] // Inches only
[InlineData("-1″", -0.08333333)] // Inches only
[InlineData("-0' 1\"", -0.08333333)] // Inches only
[InlineData("-0' 1″", -0.08333333)] // Inches only
[InlineData("-0′ 1\"", -0.08333333)] // Inches only
[InlineData("-0′ 1″", -0.08333333)] // Inches only
[InlineData("-1' 1\"", -1.08333333)] // Normal form
[InlineData("-1′ 1″", -1.08333333)] // Normal form
[InlineData(" -1′ 1″ ", -1.08333333)] // Normal form, requires trimming
Expand Down
13 changes: 10 additions & 3 deletions UnitsNet/CustomCode/Quantities/Length.extra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,24 @@ public static bool TryParseFeetInches([CanBeNull] string str, out Length result,
string inchRegex = quantityParser.CreateRegexPatternForUnit(LengthUnit.Inch, formatProvider, matchEntireString: false);

// Match entire string exactly
string pattern = $@"^(?<feet>{footRegex})\s?(?<inches>{inchRegex})$";
string pattern = $@"^(?<negativeSign>\-?)(?<feet>{footRegex})\s?(?<inches>{inchRegex})$";

var match = new Regex(pattern, RegexOptions.Singleline).Match(str);
if (!match.Success) return false;
if (!match.Success)
return false;

var negativeSignGroup = match.Groups["negativeSign"];
var feetGroup = match.Groups["feet"];
var inchesGroup = match.Groups["inches"];

if (TryParse(feetGroup.Value, formatProvider, out Length feet) &&
TryParse(inchesGroup.Value, formatProvider, out Length inches))
{
result = feet.Value >= 0 ? feet + inches : feet - inches;
result = feet + inches;

if(negativeSignGroup.Length > 0)
result = -result;

return true;
}

Expand Down