Skip to content

Fix parsing ambiguous abbreviations when lowercase #591

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
Feb 1, 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
7 changes: 7 additions & 0 deletions UnitsNet.Tests/UnitParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public void Parse_AbbreviationCaseInsensitive_Uppercase_Years()
Assert.Equal(expected, actual);
}

[Fact]
public void Parse_GivenAbbreviationsThatAreAmbiguousWhenLowerCase_ReturnsCorrectUnit()
{
Assert.Equal(PressureUnit.Megabar, Pressure.ParseUnit("Mbar"));
Assert.Equal(PressureUnit.Millibar, Pressure.ParseUnit("mbar"));
}

[Fact]
public void Parse_UnknownAbbreviationThrowsUnitNotFoundException()
{
Expand Down
13 changes: 11 additions & 2 deletions UnitsNet/CustomCode/UnitParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ object Parse([NotNull] string unitAbbreviation, Type unitType, [CanBeNull] IForm
if(!_unitAbbreviationsCache.TryGetUnitValueAbbreviationLookup(unitType, formatProvider, out var abbreviations))
throw new UnitNotFoundException($"No abbreviations defined for unit type [{unitType}] for culture [{formatProvider}].");

var unitIntValues = abbreviations.GetUnitsForAbbreviation(unitAbbreviation);
var unitIntValues = abbreviations.GetUnitsForAbbreviation(unitAbbreviation, ignoreCase: true);

// Narrow the search if too many hits, for example Megabar "Mbar" and Millibar "mbar" need to be distinguished
if (unitIntValues.Count > 1)
unitIntValues = abbreviations.GetUnitsForAbbreviation(unitAbbreviation, ignoreCase: false);

switch (unitIntValues.Count)
{
Expand Down Expand Up @@ -191,7 +195,12 @@ bool TryParse(string unitAbbreviation, Type unitType, [CanBeNull] IFormatProvide
if(!_unitAbbreviationsCache.TryGetUnitValueAbbreviationLookup(unitType, formatProvider, out var abbreviations))
return false;

var unitIntValues = abbreviations.GetUnitsForAbbreviation(unitAbbreviation);
var unitIntValues = abbreviations.GetUnitsForAbbreviation(unitAbbreviation, ignoreCase: true);

// Narrow the search if too many hits, for example Megabar "Mbar" and Millibar "mbar" need to be distinguished
if (unitIntValues.Count > 1)
unitIntValues = abbreviations.GetUnitsForAbbreviation(unitAbbreviation, ignoreCase: false);

if(unitIntValues.Count != 1)
return false;

Expand Down
36 changes: 21 additions & 15 deletions UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,9 @@ namespace UnitsNet
{
internal class UnitValueAbbreviationLookup
{
private UnitToAbbreviationMap unitToAbbreviationMap = new UnitToAbbreviationMap();
private AbbreviationToUnitMap abbreviationToUnitMap = new AbbreviationToUnitMap();

internal UnitValueAbbreviationLookup()
{
unitToAbbreviationMap = new UnitToAbbreviationMap();
abbreviationToUnitMap = new AbbreviationToUnitMap();
}
private readonly UnitToAbbreviationMap unitToAbbreviationMap = new UnitToAbbreviationMap();
private readonly AbbreviationToUnitMap abbreviationToUnitMap = new AbbreviationToUnitMap();
private readonly AbbreviationToUnitMap lowerCaseAbbreviationToUnitMap = new AbbreviationToUnitMap();

internal string[] GetAllUnitAbbreviationsForQuantity()
{
Expand All @@ -61,32 +56,43 @@ internal List<string> GetAbbreviationsForUnit(int unit)
return abbreviations.Distinct().ToList();
}

internal List<int> GetUnitsForAbbreviation(string abbreviation)
internal List<int> GetUnitsForAbbreviation(string abbreviation, bool ignoreCase)
{
var lowerCaseAbbreviation = abbreviation.ToLower();
if(!abbreviationToUnitMap.TryGetValue(lowerCaseAbbreviation, out var units))
abbreviationToUnitMap[lowerCaseAbbreviation] = units = new List<int>();
var key = ignoreCase ? lowerCaseAbbreviation : abbreviation;
var map = ignoreCase ? lowerCaseAbbreviationToUnitMap : abbreviationToUnitMap;

if(!map.TryGetValue(key, out List<int> units))
map[key] = units = new List<int>();

return units.Distinct().ToList();
}

internal void Add(int unit, string abbreviation, bool setAsDefault = false)
{
var lowerCaseAbbreviation = abbreviation.ToLower();

if(!unitToAbbreviationMap.TryGetValue(unit, out var abbreviationsForUnit))
abbreviationsForUnit = unitToAbbreviationMap[unit] = new List<string>();

if(!abbreviationToUnitMap.TryGetValue(lowerCaseAbbreviation, out var unitsForAbbreviation))
abbreviationToUnitMap[lowerCaseAbbreviation] = unitsForAbbreviation = new List<int>();
if(!abbreviationToUnitMap.TryGetValue(abbreviation, out var unitsForAbbreviation))
abbreviationToUnitMap[abbreviation] = unitsForAbbreviation = new List<int>();

if(!lowerCaseAbbreviationToUnitMap.TryGetValue(lowerCaseAbbreviation, out var unitsForLowerCaseAbbreviation))
lowerCaseAbbreviationToUnitMap[lowerCaseAbbreviation] = unitsForLowerCaseAbbreviation = new List<int>();

unitsForLowerCaseAbbreviation.Remove(unit);
unitsForLowerCaseAbbreviation.Add(unit);

abbreviationsForUnit.Remove(abbreviation);
unitsForAbbreviation.Remove(unit);
unitsForAbbreviation.Add(unit);

abbreviationsForUnit.Remove(abbreviation);
if (setAsDefault)
abbreviationsForUnit.Insert(0, abbreviation);
else
abbreviationsForUnit.Add(abbreviation);
unitsForAbbreviation.Add(unit);

}
}
}