diff --git a/UnitsNet.Tests/UnitSystemTests.cs b/UnitsNet.Tests/UnitSystemTests.cs index d73bef5ce9..9cd6266668 100644 --- a/UnitsNet.Tests/UnitSystemTests.cs +++ b/UnitsNet.Tests/UnitSystemTests.cs @@ -71,6 +71,13 @@ private enum CustomUnit // ReSharper restore UnusedMember.Local } + private UnitSystem GetCachedUnitSystem() + { + var cultureInfo = CultureInfo.GetCultureInfo("en-US"); + var unitSystem = UnitSystem.GetCached(cultureInfo); + return unitSystem; + } + private static IEnumerable GetUnitTypesWithMissingAbbreviations(string cultureName, IEnumerable unitValues) where TUnit : /*Enum constraint hack*/ struct, IComparable, IFormattable @@ -161,12 +168,32 @@ public void DecimalPointDigitGroupingCultureFormatting(string culture) Assert.AreEqual("1.111 m", Length.FromMeters(1111).ToString(LengthUnit.Meter, new CultureInfo(culture))); } + [Test] + public void Parse_UnambiguousUnitsDoesNotThrow() + { + var unitSystem = GetCachedUnitSystem(); + var unit = Volume.Parse("1 l"); + + Assert.AreEqual(Volume.FromLiters(1), unit); + } + + [Test] + public void Parse_AmbiguousUnitsThrowsException() + { + var unitSystem = GetCachedUnitSystem(); + + // Act 1 + Assert.Throws( ()=>unitSystem.Parse("tsp")); + + // Act 2 + Assert.Throws(() => Volume.Parse("1 tsp")); + } + [TestCase("m^2", Result = AreaUnit.SquareMeter)] [TestCase("cm^2", Result = AreaUnit.Undefined)] public AreaUnit Parse_ReturnsUnitMappedByCustomAbbreviationOrUndefined(string unitAbbreviationToParse) { - CultureInfo cultureInfo = CultureInfo.GetCultureInfo("en-US"); - UnitSystem unitSystem = UnitSystem.GetCached(cultureInfo); + var unitSystem = GetCachedUnitSystem(); unitSystem.MapUnitToAbbreviation(AreaUnit.SquareMeter, "m^2"); return unitSystem.Parse(unitAbbreviationToParse); diff --git a/UnitsNet/AmbiguousUnitParseException.cs b/UnitsNet/AmbiguousUnitParseException.cs new file mode 100644 index 0000000000..c9683a780d --- /dev/null +++ b/UnitsNet/AmbiguousUnitParseException.cs @@ -0,0 +1,36 @@ +// Copyright(c) 2007 Andreas Gullberg Larsen +// https://github.com/anjdreas/UnitsNet +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System; + +namespace UnitsNet +{ + public class AmbiguousUnitParseException : UnitsNetException + { + public AmbiguousUnitParseException(string message) : base(message) + { + } + + public AmbiguousUnitParseException(string message, Exception innerException) : base(message, innerException) + { + } + } +} \ No newline at end of file diff --git a/UnitsNet/CustomCode/UnitSystem.cs b/UnitsNet/CustomCode/UnitSystem.cs index 433a01cb6e..acf625257c 100644 --- a/UnitsNet/CustomCode/UnitSystem.cs +++ b/UnitsNet/CustomCode/UnitSystem.cs @@ -28,6 +28,8 @@ // ReSharper disable once CheckNamespace + + namespace UnitsNet { [PublicAPI] @@ -41,7 +43,7 @@ public partial class UnitSystem /// Per-unit-type dictionary of enum values by abbreviation. This is the inverse of /// . /// - private readonly Dictionary> _unitTypeToAbbrevToUnitValue; + private readonly Dictionary _unitTypeToAbbrevToUnitValue; /// /// Per-unit-type dictionary of abbreviations by enum value. This is the inverse of @@ -71,7 +73,7 @@ public UnitSystem([CanBeNull] IFormatProvider cultureInfo = null) Culture = cultureInfo; _unitTypeToUnitValueToAbbrevs = new Dictionary>>(); - _unitTypeToAbbrevToUnitValue = new Dictionary>(); + _unitTypeToAbbrevToUnitValue = new Dictionary(); LoadDefaultAbbreviatons(cultureInfo); } @@ -122,17 +124,33 @@ public TUnit Parse(string unitAbbreviation) where TUnit : /*Enum constraint hack*/ struct, IComparable, IFormattable { Type unitType = typeof (TUnit); - Dictionary abbrevToUnitValue; + AbbreviationMap abbrevToUnitValue; if (!_unitTypeToAbbrevToUnitValue.TryGetValue(unitType, out abbrevToUnitValue)) throw new NotImplementedException( $"No abbreviations defined for unit type [{unitType}] for culture [{Culture}]."); - int unitValue; - TUnit result = abbrevToUnitValue.TryGetValue(unitAbbreviation, out unitValue) - ? (TUnit) (object) unitValue - : default(TUnit); + List unitValues; + List units; - return result; + if (abbrevToUnitValue.TryGetValue(unitAbbreviation, out unitValues)) + { + units = unitValues.Cast().Distinct().ToList(); + } + else + { + units = new List(); + } + + switch (units.Count) + { + case 1: + return units[0]; + case 0: + return default(TUnit); + default: + var unitsCsv = String.Join(", ", units.Select(x => x.ToString()).ToArray()); + throw new AmbiguousUnitParseException($"Cannot parse '{unitAbbreviation}' since it could be either of these: {unitsCsv}"); + } } [PublicAPI] @@ -187,15 +205,17 @@ public void MapUnitToAbbreviation(Type unitType, int unitValue, [NotNull] params unitValueToAbbrev[unitValue] = existingAbbreviations.Concat(abbreviations).Distinct().ToList(); foreach (string abbreviation in abbreviations) { - Dictionary abbrevToUnitValue; + AbbreviationMap abbrevToUnitValue; if (!_unitTypeToAbbrevToUnitValue.TryGetValue(unitType, out abbrevToUnitValue)) { - abbrevToUnitValue = _unitTypeToAbbrevToUnitValue[unitType] = - new Dictionary(); + abbrevToUnitValue = _unitTypeToAbbrevToUnitValue[unitType] = new AbbreviationMap(); } if (!abbrevToUnitValue.ContainsKey(abbreviation)) - abbrevToUnitValue[abbreviation] = unitValue; + { + abbrevToUnitValue[abbreviation] = new List(); + } + abbrevToUnitValue[abbreviation].Add(unitValue); } } @@ -205,11 +225,11 @@ public bool TryParse(string unitAbbreviation, out TUnit unit) { Type unitType = typeof (TUnit); - Dictionary abbrevToUnitValue; - int unitValue; + AbbreviationMap abbrevToUnitValue; + List unitValues; if (!_unitTypeToAbbrevToUnitValue.TryGetValue(unitType, out abbrevToUnitValue) || - !abbrevToUnitValue.TryGetValue(unitAbbreviation, out unitValue)) + !abbrevToUnitValue.TryGetValue(unitAbbreviation, out unitValues)) { if (IsDefaultCulture) { @@ -221,8 +241,16 @@ public bool TryParse(string unitAbbreviation, out TUnit unit) return GetCached(DefaultCulture).TryParse(unitAbbreviation, out unit); } - unit = (TUnit) (object) unitValue; - return true; + var maps = (List) (object) unitValues; + + switch (maps.Count) + { + case 1: unit = maps[0]; + return true; + default: + unit = default(TUnit); + return false; + } } /// @@ -279,5 +307,13 @@ private void LoadDefaultAbbreviatons([NotNull] IFormatProvider culture) } } } + + /// + /// Avoids having too many nested generics for code clarity + /// + class AbbreviationMap : Dictionary> + { + + } } } \ No newline at end of file diff --git a/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs index 1f693b5a27..3811c7281a 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs @@ -432,7 +432,11 @@ private static List ParseWithRegex(string regexString, string str, converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs b/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs index 665f7932cb..c3ac7b169c 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs @@ -360,7 +360,11 @@ private static List ParseWithRegex(string regexString, string st converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs index 0cc5293fae..362d03f770 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs @@ -492,7 +492,11 @@ private static List ParseWithRegex(string regexString, string str, IForma converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs index 8ae5edfda5..92509d04c4 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs @@ -492,7 +492,11 @@ private static List ParseWithRegex(string regexString, string str, IFormat converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs index 842783bed7..0ad0f6c551 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs @@ -492,7 +492,11 @@ private static List ParseWithRegex(string regexString, string str, IFor converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs index 5734d29371..2f137f548a 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs @@ -492,7 +492,11 @@ private static List ParseWithRegex(string regexString, string str, IFo converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs b/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs index b27d9ed508..729a05781e 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs @@ -412,7 +412,11 @@ private static List ParseWithRegex(string regexString, string s converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs b/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs index 2697432f2b..b49d51355c 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs @@ -392,7 +392,11 @@ private static List ParseWithRegex(string regexString, string converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs b/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs index c7f4dbb522..fcd876d21c 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs @@ -352,7 +352,11 @@ private static List ParseWithRegex(string regexString, strin converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs index 91d0404698..a4a9e57c4c 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs @@ -552,7 +552,11 @@ private static List ParseWithRegex(string regexString, string str, IForm converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs index fa590a1243..751df2f011 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs @@ -412,7 +412,11 @@ private static List ParseWithRegex(string regexString, string str, IFormat converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs index 4e84c3aaf2..df8642fc88 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs @@ -452,7 +452,11 @@ private static List ParseWithRegex(string regexString, string str, IForma converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/ForceChangeRate.g.cs b/UnitsNet/GeneratedCode/UnitClasses/ForceChangeRate.g.cs index 3f7d44cb17..91ffa8cbc1 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/ForceChangeRate.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/ForceChangeRate.g.cs @@ -312,7 +312,11 @@ private static List ParseWithRegex(string regexString, string s converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs index 4110be7e44..ae3cfa254e 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs @@ -452,7 +452,11 @@ private static List ParseWithRegex(string regexString, string str, IF converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs index dca8726316..f9ed8d0d30 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs @@ -812,7 +812,11 @@ private static List ParseWithRegex(string regexString, string str, converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs b/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs index 1f797d8697..e283aa4c74 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs @@ -452,7 +452,11 @@ private static List ParseWithRegex(string regexString, strin converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs index 04b1c8f347..9662f2f4c9 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs @@ -572,7 +572,11 @@ private static List ParseWithRegex(string regexString, string str, IForm converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs index 7138756d94..05f5c99ed2 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs @@ -340,7 +340,11 @@ private static List ParseWithRegex(string regexString, string str, IForma converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs index f224a1d60a..84472af149 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs @@ -632,7 +632,11 @@ private static List ParseWithRegex(string regexString, string str, IFormat converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/MassFlow.g.cs b/UnitsNet/GeneratedCode/UnitClasses/MassFlow.g.cs index 79ea970d28..23db325ebf 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/MassFlow.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/MassFlow.g.cs @@ -492,7 +492,11 @@ private static List ParseWithRegex(string regexString, string str, IFo converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs index a23d1db5f2..f0c8e28a3e 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs @@ -612,7 +612,11 @@ private static List ParseWithRegex(string regexString, string str, IForma converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs b/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs index e74ad6ff08..a86798bb38 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs @@ -340,7 +340,11 @@ private static List ParseWithRegex(string regexString, string str, I converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs index 88057cb0e3..6939237fa2 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs @@ -952,7 +952,11 @@ private static List ParseWithRegex(string regexString, string str, IFo converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/PressureChangeRate.g.cs b/UnitsNet/GeneratedCode/UnitClasses/PressureChangeRate.g.cs index 13cc2bde4a..e14034e9ea 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/PressureChangeRate.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/PressureChangeRate.g.cs @@ -372,7 +372,11 @@ private static List ParseWithRegex(string regexString, strin converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs index 00db256403..45341668d4 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs @@ -412,7 +412,11 @@ private static List ParseWithRegex(string regexString, string str, IForma converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs b/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs index f818695f20..515af51c6d 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs @@ -452,7 +452,11 @@ private static List ParseWithRegex(string regexString, string s converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/SpecificEnergy.g.cs b/UnitsNet/GeneratedCode/UnitClasses/SpecificEnergy.g.cs index 1c28a48375..d34b7da719 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/SpecificEnergy.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/SpecificEnergy.g.cs @@ -452,7 +452,11 @@ private static List ParseWithRegex(string regexString, string st converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs b/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs index 77901424d9..e8a7c01d74 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs @@ -612,7 +612,11 @@ private static List ParseWithRegex(string regexString, string st converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs index 989ae5eaba..e681291b8e 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs @@ -532,7 +532,11 @@ private static List ParseWithRegex(string regexString, string str, IForma converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs index c6e437c5b4..28ed3c1a68 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs @@ -452,7 +452,11 @@ private static List ParseWithRegex(string regexString, string str, converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/TemperatureChangeRate.g.cs b/UnitsNet/GeneratedCode/UnitClasses/TemperatureChangeRate.g.cs index 798bd9bc70..f006d552c1 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/TemperatureChangeRate.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/TemperatureChangeRate.g.cs @@ -472,7 +472,11 @@ private static List ParseWithRegex(string regexString, st converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs index 63a0344b56..5d7ab6370b 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs @@ -612,7 +612,11 @@ private static List ParseWithRegex(string regexString, string str, IForm converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/VitaminA.g.cs b/UnitsNet/GeneratedCode/UnitClasses/VitaminA.g.cs index 6521795793..5cdf698de2 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/VitaminA.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/VitaminA.g.cs @@ -312,7 +312,11 @@ private static List ParseWithRegex(string regexString, string str, IFo converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs index b3eec75d93..bb7b00a30c 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs @@ -874,7 +874,11 @@ private static List ParseWithRegex(string regexString, string str, IForm converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs b/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs index 60a4a68db5..52c32909d3 100644 --- a/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs +++ b/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs @@ -1980,7 +1980,7 @@ private static readonly ReadOnlyCollection DefaultLocalization new CulturesForEnumValue((int) VolumeUnit.MetricTeaspoon, new[] { - new AbbreviationsForCulture("en-US", ""), + new AbbreviationsForCulture("en-US", "tsp", "t", "ts", "tspn", "t.", "ts.", "tsp.", "tspn.", "teaspoon"), new AbbreviationsForCulture("ru-RU", ""), new AbbreviationsForCulture("nb-NO", ""), }), diff --git a/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 b/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 index 4b87bd05c9..0e27433a20 100644 --- a/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 +++ b/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 @@ -353,7 +353,11 @@ namespace UnitsNet converted.Add(From(value, unit)); } - catch (Exception ex) + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; diff --git a/UnitsNet/Scripts/UnitDefinitions/Volume.json b/UnitsNet/Scripts/UnitDefinitions/Volume.json index 4e5050791a..44aeb2ffcb 100644 --- a/UnitsNet/Scripts/UnitDefinitions/Volume.json +++ b/UnitsNet/Scripts/UnitDefinitions/Volume.json @@ -355,7 +355,7 @@ "Localization": [ { "Culture": "en-US", - "Abbreviations": [ ] + "Abbreviations": [ "tsp", "t", "ts", "tspn", "t.", "ts.", "tsp.", "tspn.", "teaspoon" ] }, { "Culture": "ru-RU",