Skip to content

Commit d981c4c

Browse files
angularsenclaude
andauthored
Parse feet and inches when digit separators conflict with foot marks (#1754)
Follow-up to #1752. CLDR 48 (ICU on Ubuntu 26.04) changed the de-CH digit group separator from `’` to `'`, which is also a foot mark. On that data, `Length.TryParseFeetInches` had two problems: | Input (de-CH) | Before | After | |---|---|---| | `1'000' 6"` | `false` | 1000.5 ft | | `5'10"` | 42.5 ft (510 inches, wrong with no error) | 5 ft 10 in | ### Changes - Try each foot mark as the end of the feet part, instead of only the first. This also handles a foot-mark decimal separator in compound input, such as `1'5' 6"`. - When the culture's group separator is a foot mark, require digit groups of the culture's size. .NET number parsing ignores group placement, which is why `5'10` became 510. - Cultures whose group separator is not a foot mark keep their existing lenient number parsing. - Added tests with a pinned `'` separator, plus one using the host's real de-CH separator. ### Behavior to review - `1'000"` stays 1000 inches, and `5'100"` is 5100 inches, because those are correctly grouped numbers in that culture. Feet-and-inches notation with a 3-digit inch value is not recognized there. - `5'10'` in such a culture now returns `false`; it was 510 ft. - Only `ParseFeetInches` and `TryParseFeetInches` change. `Length.Parse("5'10\"", de-CH)` still returns 510 inches. - A culture with `'` as the *decimal* separator (none in CLDR, custom cultures only) still reads `5'5"` as 5.5 inches. ### Verified locally - `UnitsNet.slnx` builds. - New tests failed before the parser change (11 failures) and pass after. - Full `UnitsNet.Tests` on net10.0 and net48 pass. Local runs use Windows ICU, so the host-separator test exercises `’` here and `'` on CI. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 096ce0c commit d981c4c

2 files changed

Lines changed: 93 additions & 20 deletions

File tree

‎UnitsNet.Tests/CustomCode/LengthTests.FeetInches.cs‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,58 @@ public void TryParseFeetInches_GivenCultureWithRightQuoteDigitGroup_ReturnsLengt
8989
AssertEx.EqualTolerance(expectedFeet, result.Feet, 1e-5);
9090
}
9191

92+
[Theory]
93+
[InlineData("1'000′", 1000)] // Feet only, with separator
94+
[InlineData("1'000'", 1000)]
95+
[InlineData("1'000\"", 83.33333333)] // Inches only, with separator
96+
[InlineData("1'000′ 6\"", 1000.5)] // Normal form
97+
[InlineData("1'000' 6\"", 1000.5)]
98+
[InlineData("1'000'6\"", 1000.5)] // Without space
99+
[InlineData("-1'000' 6\"", -1000.5)]
100+
[InlineData("1'000'000' 6\"", 1000000.5)] // Multiple separators
101+
[InlineData("1'000' 1'200\"", 1100)] // Separators in both feet and inches
102+
[InlineData("5'10\"", 5.83333333)] // Foot mark, not a digit group
103+
[InlineData("5'10.5\"", 5.875)]
104+
[InlineData("-5'10\"", -5.83333333)]
105+
public void TryParseFeetInches_GivenApostropheDigitGroup_ReturnsLength(string str, double expectedFeet)
106+
{
107+
var formatProvider = new CultureInfo(GermanSwitzerland, false);
108+
formatProvider.NumberFormat.NumberGroupSeparator = "'";
109+
Assert.True(Length.TryParseFeetInches(str, out Length result, formatProvider));
110+
AssertEx.EqualTolerance(expectedFeet, result.Feet, 1e-5);
111+
}
112+
113+
[Theory]
114+
[InlineData("5'10'")] // Invalid digit group, and not feet followed by inches
115+
[InlineData("1'0' 6\"")]
116+
[InlineData("1'000' 1'")]
117+
public void TryParseFeetInches_GivenApostropheDigitGroupAndInvalidString_ReturnsFalseAndZeroOut(string str)
118+
{
119+
var formatProvider = new CultureInfo(GermanSwitzerland, false);
120+
formatProvider.NumberFormat.NumberGroupSeparator = "'";
121+
Assert.False(Length.TryParseFeetInches(str, out Length result, formatProvider));
122+
Assert.Equal(Length.Zero, result);
123+
}
124+
125+
[Fact]
126+
public void TryParseFeetInches_GivenApostropheDecimalSeparator_ReturnsLength()
127+
{
128+
var formatProvider = new CultureInfo(GermanSwitzerland, false);
129+
formatProvider.NumberFormat.NumberGroupSeparator = "’";
130+
formatProvider.NumberFormat.NumberDecimalSeparator = "'";
131+
Assert.True(Length.TryParseFeetInches("1'5' 6\"", out Length result, formatProvider));
132+
AssertEx.EqualTolerance(2, result.Feet, 1e-5);
133+
}
134+
135+
[Fact]
136+
public void TryParseFeetInches_GivenDefaultSwissDigitGroup_ReturnsLength()
137+
{
138+
var formatProvider = new CultureInfo(GermanSwitzerland, false);
139+
string gs = formatProvider.NumberFormat.NumberGroupSeparator;
140+
Assert.True(Length.TryParseFeetInches($"1{gs}000′ 6\"", out Length result, formatProvider));
141+
AssertEx.EqualTolerance(1000.5, result.Feet, 1e-5);
142+
}
143+
92144
public static IEnumerable<object[]> InvalidData
93145
{
94146
get =>

‎UnitsNet/CustomCode/Quantities/Length.extra.cs‎

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Globalization;
6+
using System.Linq;
67
using System.Numerics;
78
using System.Text.RegularExpressions;
89
using System.Threading;
@@ -76,39 +77,59 @@ public static bool TryParseFeetInches(string? str, out Length result, IFormatPro
7677

7778
str = str.Trim();
7879

80+
NumberFormatInfo numberFormat = NumberFormatInfo.GetInstance(formatProvider);
81+
bool groupSeparatorIsFootMark = UnitAbbreviationsCache.Default
82+
.GetUnitAbbreviations(LengthUnit.Foot, formatProvider)
83+
.Contains(numberFormat.NumberGroupSeparator);
84+
7985
// This succeeds if only feet or inches are given, not both
80-
if (TryParse(str, formatProvider, out result))
86+
if (TryParse(str, formatProvider, out result) && (!groupSeparatorIsFootMark || HasValidDigitGroups(str, numberFormat)))
8187
return true;
8288

8389
QuantityParser quantityParser = QuantityParser.Default;
8490
string footRegex = quantityParser.CreateRegexPatternForUnit(LengthUnit.Foot, formatProvider, matchEntireString: false);
85-
string inchRegex = quantityParser.CreateRegexPatternForUnit(LengthUnit.Inch, formatProvider, matchEntireString: false);
86-
87-
// Match entire string exactly
88-
string pattern = $@"^(?<negativeSign>\-?)(?<feet>{footRegex})\s?(?<inches>{inchRegex})$";
91+
string inchRegex = quantityParser.CreateRegexPatternForUnit(LengthUnit.Inch, formatProvider);
8992

90-
var match = new Regex(pattern, RegexOptions.Singleline).Match(str);
91-
if (!match.Success)
92-
return false;
93+
bool isNegative = str.StartsWith("-", StringComparison.Ordinal);
94+
string magnitude = isNegative ? str.Substring(1) : str;
9395

94-
var negativeSignGroup = match.Groups["negativeSign"];
95-
var feetGroup = match.Groups["feet"];
96-
var inchesGroup = match.Groups["inches"];
97-
98-
if (TryParse(feetGroup.Value, formatProvider, out Length feet) &&
99-
TryParse(inchesGroup.Value, formatProvider, out Length inches))
96+
// A foot mark can also be a separator in the number, such as 1'000' 6", so try each one as the end of the feet
97+
foreach (Match footMatch in new Regex(footRegex, RegexOptions.Singleline).Matches(magnitude))
10098
{
101-
result = feet + inches;
102-
103-
if (negativeSignGroup.Length > 0)
104-
result = -result;
105-
106-
return true;
99+
int feetLength = footMatch.Index + footMatch.Length;
100+
string feetText = magnitude.Substring(0, feetLength);
101+
string inchesText = magnitude.Substring(feetLength);
102+
103+
if (Regex.IsMatch(inchesText, inchRegex, RegexOptions.Singleline) &&
104+
(!groupSeparatorIsFootMark || (HasValidDigitGroups(feetText, numberFormat) && HasValidDigitGroups(inchesText, numberFormat))) &&
105+
TryParse(feetText, formatProvider, out Length feet) &&
106+
TryParse(inchesText, formatProvider, out Length inches))
107+
{
108+
result = isNegative ? -(feet + inches) : feet + inches;
109+
return true;
110+
}
107111
}
108112

109113
result = default;
110114
return false;
111115
}
116+
117+
/// <summary>
118+
/// Number parsing ignores where digit group separators are placed, so 5'10 would otherwise parse as 510.
119+
/// </summary>
120+
private static bool HasValidDigitGroups(string str, NumberFormatInfo numberFormat)
121+
{
122+
int groupSize = numberFormat.NumberGroupSizes.Length > 0 ? numberFormat.NumberGroupSizes[0] : 0;
123+
string digitGroupPattern = Regex.Escape(numberFormat.NumberGroupSeparator) + @"(?<digits>\d+)";
124+
125+
foreach (Match digitGroup in Regex.Matches(str, digitGroupPattern))
126+
{
127+
if (digitGroup.Groups["digits"].Length != groupSize)
128+
return false;
129+
}
130+
131+
return true;
132+
}
112133
}
113134

114135
/// <summary>

0 commit comments

Comments
 (0)