diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Information.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Information.WindowsRuntimeComponent.g.cs index d7d30a6586..a2fd03bcc8 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Information.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Information.WindowsRuntimeComponent.g.cs @@ -623,7 +623,7 @@ public static Information From(double value, InformationUnit fromUnit) /// public static Information Parse(string str) { - return ParseInternal(str, null); + return Parse(str, null); } /// @@ -652,7 +652,10 @@ public static Information Parse(string str) public static Information Parse(string str, [CanBeNull] string cultureName) { IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); - return ParseInternal(str, provider); + return QuantityParser.Default.Parse( + str, + provider, + From); } /// @@ -665,7 +668,7 @@ public static Information Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, out Information result) { - return TryParseInternal(str, null, out result); + return TryParse(str, null, out result); } /// @@ -681,7 +684,11 @@ public static bool TryParse([CanBeNull] string str, out Information result) public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Information result) { IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); - return TryParseInternal(str, provider, out result); + return QuantityParser.Default.TryParse( + str, + provider, + From, + out result); } /// @@ -695,7 +702,7 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static InformationUnit ParseUnit(string str) { - return ParseUnitInternal(str, null); + return ParseUnit(str, null); } /// @@ -711,12 +718,12 @@ public static InformationUnit ParseUnit(string str) public static InformationUnit ParseUnit(string str, [CanBeNull] string cultureName) { IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); - return ParseUnitInternal(str, provider); + return UnitParser.Default.Parse(str, provider); } public static bool TryParseUnit(string str, out InformationUnit unit) { - return TryParseUnitInternal(str, null, out unit); + return TryParseUnit(str, null, out unit); } /// @@ -732,114 +739,7 @@ public static bool TryParseUnit(string str, out InformationUnit unit) public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out InformationUnit unit) { IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); - return TryParseUnitInternal(str, provider, out unit); - } - - /// - /// Parse a string with one or two quantities of the format "<quantity> <unit>". - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// - /// Length.Parse("5.5 m", new CultureInfo("en-US")); - /// - /// The value of 'str' cannot be null. - /// - /// Expected string to have one or two pairs of quantity and unit in the format - /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" - /// - /// - /// More than one unit is represented by the specified unit abbreviation. - /// Example: Volume.Parse("1 cup") will throw, because it can refer to any of - /// , and . - /// - /// - /// If anything else goes wrong, typically due to a bug or unhandled case. - /// We wrap exceptions in to allow you to distinguish - /// Units.NET exceptions from other exceptions. - /// - private static Information ParseInternal(string str, [CanBeNull] IFormatProvider provider) - { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? GlobalConfiguration.DefaultCulture; - - return QuantityParser.Default.Parse(str, provider, ParseUnitInternal, From); - } - - /// - /// Try to parse a string with one or two quantities of the format "<quantity> <unit>". - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// Resulting unit quantity if successful. - /// True if successful, otherwise false. - /// - /// Length.Parse("5.5 m", new CultureInfo("en-US")); - /// - private static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Information result) - { - result = default; - - if(string.IsNullOrWhiteSpace(str)) - return false; - - provider = provider ?? GlobalConfiguration.DefaultCulture; - - return QuantityParser.Default.TryParse(str, provider, TryParseUnitInternal, From, out result); - } - - /// - /// Parse a unit string. - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); - /// - /// The value of 'str' cannot be null. - /// Error parsing string. - private static InformationUnit ParseUnitInternal(string str, IFormatProvider provider = null) - { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unit = UnitParser.Default.Parse(str.Trim(), provider); - - if (unit == InformationUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized InformationUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; - } - - /// - /// Parse a unit string. - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// The parsed unit if successful. - /// True if successful, otherwise false. - /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); - /// - private static bool TryParseUnitInternal(string str, IFormatProvider provider, out InformationUnit unit) - { - unit = InformationUnit.Undefined; - - if(string.IsNullOrWhiteSpace(str)) - return false; - - if(!UnitParser.Default.TryParse(str.Trim(), provider, out unit)) - return false; - - if(unit == InformationUnit.Undefined) - return false; - - return true; + return UnitParser.Default.TryParse(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Length.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Length.WindowsRuntimeComponent.g.cs index 6b72f87fee..0fdbf139cc 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Length.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Length.WindowsRuntimeComponent.g.cs @@ -563,7 +563,7 @@ public static Length From(double value, LengthUnit fromUnit) /// public static Length Parse(string str) { - return ParseInternal(str, null); + return Parse(str, null); } /// @@ -592,7 +592,10 @@ public static Length Parse(string str) public static Length Parse(string str, [CanBeNull] string cultureName) { IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); - return ParseInternal(str, provider); + return QuantityParser.Default.Parse( + str, + provider, + From); } /// @@ -605,7 +608,7 @@ public static Length Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, out Length result) { - return TryParseInternal(str, null, out result); + return TryParse(str, null, out result); } /// @@ -621,7 +624,11 @@ public static bool TryParse([CanBeNull] string str, out Length result) public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Length result) { IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); - return TryParseInternal(str, provider, out result); + return QuantityParser.Default.TryParse( + str, + provider, + From, + out result); } /// @@ -635,7 +642,7 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static LengthUnit ParseUnit(string str) { - return ParseUnitInternal(str, null); + return ParseUnit(str, null); } /// @@ -651,12 +658,12 @@ public static LengthUnit ParseUnit(string str) public static LengthUnit ParseUnit(string str, [CanBeNull] string cultureName) { IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); - return ParseUnitInternal(str, provider); + return UnitParser.Default.Parse(str, provider); } public static bool TryParseUnit(string str, out LengthUnit unit) { - return TryParseUnitInternal(str, null, out unit); + return TryParseUnit(str, null, out unit); } /// @@ -672,114 +679,7 @@ public static bool TryParseUnit(string str, out LengthUnit unit) public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out LengthUnit unit) { IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); - return TryParseUnitInternal(str, provider, out unit); - } - - /// - /// Parse a string with one or two quantities of the format "<quantity> <unit>". - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// - /// Length.Parse("5.5 m", new CultureInfo("en-US")); - /// - /// The value of 'str' cannot be null. - /// - /// Expected string to have one or two pairs of quantity and unit in the format - /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" - /// - /// - /// More than one unit is represented by the specified unit abbreviation. - /// Example: Volume.Parse("1 cup") will throw, because it can refer to any of - /// , and . - /// - /// - /// If anything else goes wrong, typically due to a bug or unhandled case. - /// We wrap exceptions in to allow you to distinguish - /// Units.NET exceptions from other exceptions. - /// - private static Length ParseInternal(string str, [CanBeNull] IFormatProvider provider) - { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? GlobalConfiguration.DefaultCulture; - - return QuantityParser.Default.Parse(str, provider, ParseUnitInternal, From); - } - - /// - /// Try to parse a string with one or two quantities of the format "<quantity> <unit>". - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// Resulting unit quantity if successful. - /// True if successful, otherwise false. - /// - /// Length.Parse("5.5 m", new CultureInfo("en-US")); - /// - private static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Length result) - { - result = default; - - if(string.IsNullOrWhiteSpace(str)) - return false; - - provider = provider ?? GlobalConfiguration.DefaultCulture; - - return QuantityParser.Default.TryParse(str, provider, TryParseUnitInternal, From, out result); - } - - /// - /// Parse a unit string. - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); - /// - /// The value of 'str' cannot be null. - /// Error parsing string. - private static LengthUnit ParseUnitInternal(string str, IFormatProvider provider = null) - { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unit = UnitParser.Default.Parse(str.Trim(), provider); - - if (unit == LengthUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LengthUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; - } - - /// - /// Parse a unit string. - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// The parsed unit if successful. - /// True if successful, otherwise false. - /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); - /// - private static bool TryParseUnitInternal(string str, IFormatProvider provider, out LengthUnit unit) - { - unit = LengthUnit.Undefined; - - if(string.IsNullOrWhiteSpace(str)) - return false; - - if(!UnitParser.Default.TryParse(str.Trim(), provider, out unit)) - return false; - - if(unit == LengthUnit.Undefined) - return false; - - return true; + return UnitParser.Default.TryParse(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Level.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Level.WindowsRuntimeComponent.g.cs index 5149db52ad..a8d924885c 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Level.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Level.WindowsRuntimeComponent.g.cs @@ -263,7 +263,7 @@ public static Level From(double value, LevelUnit fromUnit) /// public static Level Parse(string str) { - return ParseInternal(str, null); + return Parse(str, null); } /// @@ -292,7 +292,10 @@ public static Level Parse(string str) public static Level Parse(string str, [CanBeNull] string cultureName) { IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); - return ParseInternal(str, provider); + return QuantityParser.Default.Parse( + str, + provider, + From); } /// @@ -305,7 +308,7 @@ public static Level Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, out Level result) { - return TryParseInternal(str, null, out result); + return TryParse(str, null, out result); } /// @@ -321,7 +324,11 @@ public static bool TryParse([CanBeNull] string str, out Level result) public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Level result) { IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); - return TryParseInternal(str, provider, out result); + return QuantityParser.Default.TryParse( + str, + provider, + From, + out result); } /// @@ -335,7 +342,7 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static LevelUnit ParseUnit(string str) { - return ParseUnitInternal(str, null); + return ParseUnit(str, null); } /// @@ -351,12 +358,12 @@ public static LevelUnit ParseUnit(string str) public static LevelUnit ParseUnit(string str, [CanBeNull] string cultureName) { IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); - return ParseUnitInternal(str, provider); + return UnitParser.Default.Parse(str, provider); } public static bool TryParseUnit(string str, out LevelUnit unit) { - return TryParseUnitInternal(str, null, out unit); + return TryParseUnit(str, null, out unit); } /// @@ -372,114 +379,7 @@ public static bool TryParseUnit(string str, out LevelUnit unit) public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out LevelUnit unit) { IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); - return TryParseUnitInternal(str, provider, out unit); - } - - /// - /// Parse a string with one or two quantities of the format "<quantity> <unit>". - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// - /// Length.Parse("5.5 m", new CultureInfo("en-US")); - /// - /// The value of 'str' cannot be null. - /// - /// Expected string to have one or two pairs of quantity and unit in the format - /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" - /// - /// - /// More than one unit is represented by the specified unit abbreviation. - /// Example: Volume.Parse("1 cup") will throw, because it can refer to any of - /// , and . - /// - /// - /// If anything else goes wrong, typically due to a bug or unhandled case. - /// We wrap exceptions in to allow you to distinguish - /// Units.NET exceptions from other exceptions. - /// - private static Level ParseInternal(string str, [CanBeNull] IFormatProvider provider) - { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? GlobalConfiguration.DefaultCulture; - - return QuantityParser.Default.Parse(str, provider, ParseUnitInternal, From); - } - - /// - /// Try to parse a string with one or two quantities of the format "<quantity> <unit>". - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// Resulting unit quantity if successful. - /// True if successful, otherwise false. - /// - /// Length.Parse("5.5 m", new CultureInfo("en-US")); - /// - private static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Level result) - { - result = default; - - if(string.IsNullOrWhiteSpace(str)) - return false; - - provider = provider ?? GlobalConfiguration.DefaultCulture; - - return QuantityParser.Default.TryParse(str, provider, TryParseUnitInternal, From, out result); - } - - /// - /// Parse a unit string. - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); - /// - /// The value of 'str' cannot be null. - /// Error parsing string. - private static LevelUnit ParseUnitInternal(string str, IFormatProvider provider = null) - { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unit = UnitParser.Default.Parse(str.Trim(), provider); - - if (unit == LevelUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LevelUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; - } - - /// - /// Parse a unit string. - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// The parsed unit if successful. - /// True if successful, otherwise false. - /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); - /// - private static bool TryParseUnitInternal(string str, IFormatProvider provider, out LevelUnit unit) - { - unit = LevelUnit.Undefined; - - if(string.IsNullOrWhiteSpace(str)) - return false; - - if(!UnitParser.Default.TryParse(str.Trim(), provider, out unit)) - return false; - - if(unit == LevelUnit.Undefined) - return false; - - return true; + return UnitParser.Default.TryParse(str, provider, out unit); } #endregion diff --git a/UnitsNet/CustomCode/Quantities/Length.extra.cs b/UnitsNet/CustomCode/Quantities/Length.extra.cs index 20bc2af780..8978af0364 100644 --- a/UnitsNet/CustomCode/Quantities/Length.extra.cs +++ b/UnitsNet/CustomCode/Quantities/Length.extra.cs @@ -80,8 +80,9 @@ public static Length FromFeetInches(double feet, double inches) /// /// Optionally specify the culture format numbers and localize unit abbreviations. Defaults to thread's culture. /// Parsed length. - public static Length ParseFeetInches(string str, IFormatProvider formatProvider = null) + public static Length ParseFeetInches([NotNull] string str, IFormatProvider formatProvider = null) { + if (str == null) throw new ArgumentNullException(nameof(str)); if (!TryParseFeetInches(str, out Length result, formatProvider)) { // A bit lazy, but I didn't want to duplicate this edge case implementation just to get more narrow exception descriptions. @@ -101,10 +102,18 @@ public static Length ParseFeetInches(string str, IFormatProvider formatProvider /// /// Parsed length. /// Optionally specify the culture format numbers and localize unit abbreviations. Defaults to thread's culture. - public static bool TryParseFeetInches(string str, out Length result, IFormatProvider formatProvider = null) + public static bool TryParseFeetInches([CanBeNull] string str, out Length result, IFormatProvider formatProvider = null) { + if (str == null) + { + result = default; + return false; + } + + str = str.Trim(); + // This succeeds if only feet or inches are given, not both - if (TryParseInternal(str, formatProvider, out result)) + if (TryParse(str, formatProvider, out result)) return true; var quantityParser = QuantityParser.Default; @@ -114,13 +123,13 @@ public static bool TryParseFeetInches(string str, out Length result, IFormatProv // Match entire string exactly string pattern = $@"^(?{footRegex})\s?(?{inchRegex})$"; - var match = new Regex(pattern, RegexOptions.Singleline).Match(str.Trim()); + var match = new Regex(pattern, RegexOptions.Singleline).Match(str); if (!match.Success) return false; var feetGroup = match.Groups["feet"]; var inchesGroup = match.Groups["inches"]; - if (TryParseInternal(feetGroup.Value, formatProvider, out Length feet) && - TryParseInternal(inchesGroup.Value, formatProvider, out Length inches)) + if (TryParse(feetGroup.Value, formatProvider, out Length feet) && + TryParse(inchesGroup.Value, formatProvider, out Length inches)) { result = feet + inches; return true; diff --git a/UnitsNet/CustomCode/QuantityParser.cs b/UnitsNet/CustomCode/QuantityParser.cs index 41c02aecd7..d6bedd5c92 100644 --- a/UnitsNet/CustomCode/QuantityParser.cs +++ b/UnitsNet/CustomCode/QuantityParser.cs @@ -30,8 +30,6 @@ // ReSharper disable once CheckNamespace namespace UnitsNet { - internal delegate TUnitType ParseUnitDelegate(string unitString, IFormatProvider formatProvider) where TUnitType : Enum; - internal delegate bool TryParseUnitDelegate(string unitString, IFormatProvider formatProvider, out TUnitType unit) where TUnitType : Enum; #if !WINDOWS_UWP internal delegate TQuantity QuantityFromDelegate(QuantityValue value, TUnitType fromUnit) @@ -51,12 +49,14 @@ internal class QuantityParser private const NumberStyles ParseNumberStyles = NumberStyles.Number | NumberStyles.Float | NumberStyles.AllowExponent; private readonly UnitAbbreviationsCache _unitAbbreviationsCache; + private UnitParser _unitParser; public static QuantityParser Default { get; } public QuantityParser(UnitAbbreviationsCache unitAbbreviationsCache) { _unitAbbreviationsCache = unitAbbreviationsCache ?? UnitAbbreviationsCache.Default; + _unitParser = new UnitParser(_unitAbbreviationsCache); } static QuantityParser() @@ -67,13 +67,12 @@ static QuantityParser() [SuppressMessage("ReSharper", "UseStringInterpolation")] internal TQuantity Parse([NotNull] string str, [CanBeNull] IFormatProvider formatProvider, - [NotNull] ParseUnitDelegate parseUnit, [NotNull] QuantityFromDelegate fromDelegate) where TQuantity : IQuantity where TUnitType : Enum { if (str == null) throw new ArgumentNullException(nameof(str)); - if (parseUnit == null) throw new ArgumentNullException(nameof(parseUnit)); + str = str.Trim(); var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof(NumberFormatInfo)) @@ -91,13 +90,12 @@ internal TQuantity Parse([NotNull] string str, throw ex; } - return ParseWithRegex(valueString, unitString, parseUnit, fromDelegate, formatProvider); + return ParseWithRegex(valueString, unitString, fromDelegate, formatProvider); } [SuppressMessage("ReSharper", "UseStringInterpolation")] internal bool TryParse([NotNull] string str, [CanBeNull] IFormatProvider formatProvider, - [NotNull] TryParseUnitDelegate parseUnit, [NotNull] QuantityFromDelegate fromDelegate, out TQuantity result) where TQuantity : IQuantity @@ -106,6 +104,7 @@ internal bool TryParse([NotNull] string str, result = default; if(string.IsNullOrWhiteSpace(str)) return false; + str = str.Trim(); var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof(NumberFormatInfo)) @@ -119,7 +118,7 @@ internal bool TryParse([NotNull] string str, if (!ExtractValueAndUnit(regex, str, out var valueString, out var unitString)) return false; - return TryParseWithRegex(valueString, unitString, parseUnit, fromDelegate, formatProvider, out result); + return TryParseWithRegex(valueString, unitString, fromDelegate, formatProvider, out result); } internal string CreateRegexPatternForUnit( @@ -148,16 +147,15 @@ private static string GetRegexPatternForUnitAbbreviations(IEnumerable ab /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static TQuantity ParseWithRegex(string valueString, + private TQuantity ParseWithRegex(string valueString, string unitString, - ParseUnitDelegate parseUnit, QuantityFromDelegate fromDelegate, IFormatProvider formatProvider) where TQuantity : IQuantity where TUnitType : Enum { var value = double.Parse(valueString, ParseNumberStyles, formatProvider); - var parsedUnit = parseUnit(unitString, formatProvider); + var parsedUnit = _unitParser.Parse(unitString, formatProvider); return fromDelegate(value, parsedUnit); } @@ -165,9 +163,8 @@ private static TQuantity ParseWithRegex(string valueString /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static bool TryParseWithRegex(string valueString, + private bool TryParseWithRegex(string valueString, string unitString, - TryParseUnitDelegate tryParseUnit, QuantityFromDelegate fromDelegate, IFormatProvider formatProvider, out TQuantity result) @@ -177,10 +174,10 @@ private static bool TryParseWithRegex(string valueString, result = default; if (!double.TryParse(valueString, ParseNumberStyles, formatProvider, out var value)) - return false; + return false; - if (!tryParseUnit(unitString, formatProvider, out var parsedUnit)) - return false; + if (!_unitParser.TryParse(unitString, formatProvider, out var parsedUnit)) + return false; result = fromDelegate(value, parsedUnit); return true; @@ -188,7 +185,7 @@ private static bool TryParseWithRegex(string valueString, private static bool ExtractValueAndUnit(Regex regex, string str, out string valueString, out string unitString) { - var match = regex.Match(str.Trim()); + var match = regex.Match(str); var groups = match.Groups; var valueGroup = groups["value"]; diff --git a/UnitsNet/CustomCode/UnitParser.cs b/UnitsNet/CustomCode/UnitParser.cs index cc265b585c..97d4bffda4 100644 --- a/UnitsNet/CustomCode/UnitParser.cs +++ b/UnitsNet/CustomCode/UnitParser.cs @@ -82,12 +82,15 @@ TUnitType Parse(string unitAbbreviation, [CanBeNull] IFormatProvider #else public #endif - object Parse(string unitAbbreviation, Type unitType, [CanBeNull] IFormatProvider formatProvider = null) + object Parse([NotNull] string unitAbbreviation, Type unitType, [CanBeNull] IFormatProvider formatProvider = null) { + if (unitAbbreviation == null) throw new ArgumentNullException(nameof(unitAbbreviation)); + unitAbbreviation = unitAbbreviation.Trim(); + 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.Trim()); + var unitIntValues = abbreviations.GetUnitsForAbbreviation(unitAbbreviation); switch (unitIntValues.Count) { @@ -176,12 +179,19 @@ public bool TryParse(string unitAbbreviation, Type unitType, out object unit) #endif bool TryParse(string unitAbbreviation, Type unitType, [CanBeNull] IFormatProvider formatProvider, out object unit) { + if (unitAbbreviation == null) + { + unit = default; + return false; + } + + unitAbbreviation = unitAbbreviation.Trim(); unit = GetDefault(unitType); if(!_unitAbbreviationsCache.TryGetUnitValueAbbreviationLookup(unitType, formatProvider, out var abbreviations)) return false; - var unitIntValues = abbreviations.GetUnitsForAbbreviation(unitAbbreviation.Trim()); + var unitIntValues = abbreviations.GetUnitsForAbbreviation(unitAbbreviation); if(unitIntValues.Count != 1) return false; diff --git a/UnitsNet/GeneratedCode/Quantities/Information.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Information.NetFramework.g.cs index c304c11ff8..f700290d38 100644 --- a/UnitsNet/GeneratedCode/Quantities/Information.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Information.NetFramework.g.cs @@ -580,7 +580,7 @@ public static Information From(QuantityValue value, InformationUnit fromUnit) /// public static Information Parse(string str) { - return ParseInternal(str, null); + return Parse(str, null); } /// @@ -608,7 +608,10 @@ public static Information Parse(string str) /// Format to use when parsing number and unit. Defaults to if null. public static Information Parse(string str, [CanBeNull] IFormatProvider provider) { - return ParseInternal(str, provider); + return QuantityParser.Default.Parse( + str, + provider, + From); } /// @@ -621,7 +624,7 @@ public static Information Parse(string str, [CanBeNull] IFormatProvider provider /// public static bool TryParse([CanBeNull] string str, out Information result) { - return TryParseInternal(str, null, out result); + return TryParse(str, null, out result); } /// @@ -636,7 +639,11 @@ public static bool TryParse([CanBeNull] string str, out Information result) /// Format to use when parsing number and unit. Defaults to if null. public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Information result) { - return TryParseInternal(str, provider, out result); + return QuantityParser.Default.TryParse( + str, + provider, + From, + out result); } /// @@ -650,7 +657,7 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider /// Error parsing string. public static InformationUnit ParseUnit(string str) { - return ParseUnitInternal(str, null); + return ParseUnit(str, null); } /// @@ -665,12 +672,12 @@ public static InformationUnit ParseUnit(string str) /// Format to use when parsing number and unit. Defaults to if null. public static InformationUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnitInternal(str, provider); + return UnitParser.Default.Parse(str, provider); } public static bool TryParseUnit(string str, out InformationUnit unit) { - return TryParseUnitInternal(str, null, out unit); + return TryParseUnit(str, null, out unit); } /// @@ -685,114 +692,7 @@ public static bool TryParseUnit(string str, out InformationUnit unit) /// Format to use when parsing number and unit. Defaults to if null. public static bool TryParseUnit(string str, IFormatProvider provider, out InformationUnit unit) { - return TryParseUnitInternal(str, provider, out unit); - } - - /// - /// Parse a string with one or two quantities of the format "<quantity> <unit>". - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// - /// Length.Parse("5.5 m", new CultureInfo("en-US")); - /// - /// The value of 'str' cannot be null. - /// - /// Expected string to have one or two pairs of quantity and unit in the format - /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" - /// - /// - /// More than one unit is represented by the specified unit abbreviation. - /// Example: Volume.Parse("1 cup") will throw, because it can refer to any of - /// , and . - /// - /// - /// If anything else goes wrong, typically due to a bug or unhandled case. - /// We wrap exceptions in to allow you to distinguish - /// Units.NET exceptions from other exceptions. - /// - private static Information ParseInternal(string str, [CanBeNull] IFormatProvider provider) - { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? GlobalConfiguration.DefaultCulture; - - return QuantityParser.Default.Parse(str, provider, ParseUnitInternal, From); - } - - /// - /// Try to parse a string with one or two quantities of the format "<quantity> <unit>". - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// Resulting unit quantity if successful. - /// True if successful, otherwise false. - /// - /// Length.Parse("5.5 m", new CultureInfo("en-US")); - /// - private static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Information result) - { - result = default; - - if(string.IsNullOrWhiteSpace(str)) - return false; - - provider = provider ?? GlobalConfiguration.DefaultCulture; - - return QuantityParser.Default.TryParse(str, provider, TryParseUnitInternal, From, out result); - } - - /// - /// Parse a unit string. - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); - /// - /// The value of 'str' cannot be null. - /// Error parsing string. - private static InformationUnit ParseUnitInternal(string str, IFormatProvider provider = null) - { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unit = UnitParser.Default.Parse(str.Trim(), provider); - - if (unit == InformationUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized InformationUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; - } - - /// - /// Parse a unit string. - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// The parsed unit if successful. - /// True if successful, otherwise false. - /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); - /// - private static bool TryParseUnitInternal(string str, IFormatProvider provider, out InformationUnit unit) - { - unit = InformationUnit.Undefined; - - if(string.IsNullOrWhiteSpace(str)) - return false; - - if(!UnitParser.Default.TryParse(str.Trim(), provider, out unit)) - return false; - - if(unit == InformationUnit.Undefined) - return false; - - return true; + return UnitParser.Default.TryParse(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Length.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Length.NetFramework.g.cs index 1dc73d885a..9aa0ce14f8 100644 --- a/UnitsNet/GeneratedCode/Quantities/Length.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Length.NetFramework.g.cs @@ -524,7 +524,7 @@ public static Length From(QuantityValue value, LengthUnit fromUnit) /// public static Length Parse(string str) { - return ParseInternal(str, null); + return Parse(str, null); } /// @@ -552,7 +552,10 @@ public static Length Parse(string str) /// Format to use when parsing number and unit. Defaults to if null. public static Length Parse(string str, [CanBeNull] IFormatProvider provider) { - return ParseInternal(str, provider); + return QuantityParser.Default.Parse( + str, + provider, + From); } /// @@ -565,7 +568,7 @@ public static Length Parse(string str, [CanBeNull] IFormatProvider provider) /// public static bool TryParse([CanBeNull] string str, out Length result) { - return TryParseInternal(str, null, out result); + return TryParse(str, null, out result); } /// @@ -580,7 +583,11 @@ public static bool TryParse([CanBeNull] string str, out Length result) /// Format to use when parsing number and unit. Defaults to if null. public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Length result) { - return TryParseInternal(str, provider, out result); + return QuantityParser.Default.TryParse( + str, + provider, + From, + out result); } /// @@ -594,7 +601,7 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider /// Error parsing string. public static LengthUnit ParseUnit(string str) { - return ParseUnitInternal(str, null); + return ParseUnit(str, null); } /// @@ -609,12 +616,12 @@ public static LengthUnit ParseUnit(string str) /// Format to use when parsing number and unit. Defaults to if null. public static LengthUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnitInternal(str, provider); + return UnitParser.Default.Parse(str, provider); } public static bool TryParseUnit(string str, out LengthUnit unit) { - return TryParseUnitInternal(str, null, out unit); + return TryParseUnit(str, null, out unit); } /// @@ -629,114 +636,7 @@ public static bool TryParseUnit(string str, out LengthUnit unit) /// Format to use when parsing number and unit. Defaults to if null. public static bool TryParseUnit(string str, IFormatProvider provider, out LengthUnit unit) { - return TryParseUnitInternal(str, provider, out unit); - } - - /// - /// Parse a string with one or two quantities of the format "<quantity> <unit>". - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// - /// Length.Parse("5.5 m", new CultureInfo("en-US")); - /// - /// The value of 'str' cannot be null. - /// - /// Expected string to have one or two pairs of quantity and unit in the format - /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" - /// - /// - /// More than one unit is represented by the specified unit abbreviation. - /// Example: Volume.Parse("1 cup") will throw, because it can refer to any of - /// , and . - /// - /// - /// If anything else goes wrong, typically due to a bug or unhandled case. - /// We wrap exceptions in to allow you to distinguish - /// Units.NET exceptions from other exceptions. - /// - private static Length ParseInternal(string str, [CanBeNull] IFormatProvider provider) - { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? GlobalConfiguration.DefaultCulture; - - return QuantityParser.Default.Parse(str, provider, ParseUnitInternal, From); - } - - /// - /// Try to parse a string with one or two quantities of the format "<quantity> <unit>". - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// Resulting unit quantity if successful. - /// True if successful, otherwise false. - /// - /// Length.Parse("5.5 m", new CultureInfo("en-US")); - /// - private static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Length result) - { - result = default; - - if(string.IsNullOrWhiteSpace(str)) - return false; - - provider = provider ?? GlobalConfiguration.DefaultCulture; - - return QuantityParser.Default.TryParse(str, provider, TryParseUnitInternal, From, out result); - } - - /// - /// Parse a unit string. - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); - /// - /// The value of 'str' cannot be null. - /// Error parsing string. - private static LengthUnit ParseUnitInternal(string str, IFormatProvider provider = null) - { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unit = UnitParser.Default.Parse(str.Trim(), provider); - - if (unit == LengthUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LengthUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; - } - - /// - /// Parse a unit string. - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// The parsed unit if successful. - /// True if successful, otherwise false. - /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); - /// - private static bool TryParseUnitInternal(string str, IFormatProvider provider, out LengthUnit unit) - { - unit = LengthUnit.Undefined; - - if(string.IsNullOrWhiteSpace(str)) - return false; - - if(!UnitParser.Default.TryParse(str.Trim(), provider, out unit)) - return false; - - if(unit == LengthUnit.Undefined) - return false; - - return true; + return UnitParser.Default.TryParse(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Level.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Level.NetFramework.g.cs index 0029fb65aa..2eb3c5b031 100644 --- a/UnitsNet/GeneratedCode/Quantities/Level.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Level.NetFramework.g.cs @@ -244,7 +244,7 @@ public static Level From(QuantityValue value, LevelUnit fromUnit) /// public static Level Parse(string str) { - return ParseInternal(str, null); + return Parse(str, null); } /// @@ -272,7 +272,10 @@ public static Level Parse(string str) /// Format to use when parsing number and unit. Defaults to if null. public static Level Parse(string str, [CanBeNull] IFormatProvider provider) { - return ParseInternal(str, provider); + return QuantityParser.Default.Parse( + str, + provider, + From); } /// @@ -285,7 +288,7 @@ public static Level Parse(string str, [CanBeNull] IFormatProvider provider) /// public static bool TryParse([CanBeNull] string str, out Level result) { - return TryParseInternal(str, null, out result); + return TryParse(str, null, out result); } /// @@ -300,7 +303,11 @@ public static bool TryParse([CanBeNull] string str, out Level result) /// Format to use when parsing number and unit. Defaults to if null. public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Level result) { - return TryParseInternal(str, provider, out result); + return QuantityParser.Default.TryParse( + str, + provider, + From, + out result); } /// @@ -314,7 +321,7 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider /// Error parsing string. public static LevelUnit ParseUnit(string str) { - return ParseUnitInternal(str, null); + return ParseUnit(str, null); } /// @@ -329,12 +336,12 @@ public static LevelUnit ParseUnit(string str) /// Format to use when parsing number and unit. Defaults to if null. public static LevelUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnitInternal(str, provider); + return UnitParser.Default.Parse(str, provider); } public static bool TryParseUnit(string str, out LevelUnit unit) { - return TryParseUnitInternal(str, null, out unit); + return TryParseUnit(str, null, out unit); } /// @@ -349,114 +356,7 @@ public static bool TryParseUnit(string str, out LevelUnit unit) /// Format to use when parsing number and unit. Defaults to if null. public static bool TryParseUnit(string str, IFormatProvider provider, out LevelUnit unit) { - return TryParseUnitInternal(str, provider, out unit); - } - - /// - /// Parse a string with one or two quantities of the format "<quantity> <unit>". - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// - /// Length.Parse("5.5 m", new CultureInfo("en-US")); - /// - /// The value of 'str' cannot be null. - /// - /// Expected string to have one or two pairs of quantity and unit in the format - /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" - /// - /// - /// More than one unit is represented by the specified unit abbreviation. - /// Example: Volume.Parse("1 cup") will throw, because it can refer to any of - /// , and . - /// - /// - /// If anything else goes wrong, typically due to a bug or unhandled case. - /// We wrap exceptions in to allow you to distinguish - /// Units.NET exceptions from other exceptions. - /// - private static Level ParseInternal(string str, [CanBeNull] IFormatProvider provider) - { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? GlobalConfiguration.DefaultCulture; - - return QuantityParser.Default.Parse(str, provider, ParseUnitInternal, From); - } - - /// - /// Try to parse a string with one or two quantities of the format "<quantity> <unit>". - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// Resulting unit quantity if successful. - /// True if successful, otherwise false. - /// - /// Length.Parse("5.5 m", new CultureInfo("en-US")); - /// - private static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Level result) - { - result = default; - - if(string.IsNullOrWhiteSpace(str)) - return false; - - provider = provider ?? GlobalConfiguration.DefaultCulture; - - return QuantityParser.Default.TryParse(str, provider, TryParseUnitInternal, From, out result); - } - - /// - /// Parse a unit string. - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); - /// - /// The value of 'str' cannot be null. - /// Error parsing string. - private static LevelUnit ParseUnitInternal(string str, IFormatProvider provider = null) - { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unit = UnitParser.Default.Parse(str.Trim(), provider); - - if (unit == LevelUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LevelUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; - } - - /// - /// Parse a unit string. - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// The parsed unit if successful. - /// True if successful, otherwise false. - /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); - /// - private static bool TryParseUnitInternal(string str, IFormatProvider provider, out LevelUnit unit) - { - unit = LevelUnit.Undefined; - - if(string.IsNullOrWhiteSpace(str)) - return false; - - if(!UnitParser.Default.TryParse(str.Trim(), provider, out unit)) - return false; - - if(unit == LevelUnit.Undefined) - return false; - - return true; + return UnitParser.Default.TryParse(str, provider, out unit); } #endregion diff --git a/UnitsNet/Scripts/Include-GenerateQuantitySourceCodeNetFramework.ps1 b/UnitsNet/Scripts/Include-GenerateQuantitySourceCodeNetFramework.ps1 index a2e693f463..6eff130e3a 100644 --- a/UnitsNet/Scripts/Include-GenerateQuantitySourceCodeNetFramework.ps1 +++ b/UnitsNet/Scripts/Include-GenerateQuantitySourceCodeNetFramework.ps1 @@ -521,7 +521,7 @@ function GenerateStaticParseMethods([GeneratorArgs]$genArgs) /// public static $quantityName Parse(string str) { - return ParseInternal(str, null); + return Parse(str, null); } /// @@ -557,7 +557,10 @@ if ($wrc) {@" public static $quantityName Parse(string str, [CanBeNull] IFormatProvider provider) { "@; }@" - return ParseInternal(str, provider); + return QuantityParser.Default.Parse<$quantityName, $unitEnumName>( + str, + provider, + From); } /// @@ -570,7 +573,7 @@ if ($wrc) {@" /// public static bool TryParse([CanBeNull] string str, out $quantityName result) { - return TryParseInternal(str, null, out result); + return TryParse(str, null, out result); } /// @@ -593,7 +596,11 @@ if ($wrc) {@" public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out $quantityName result) { "@; }@" - return TryParseInternal(str, provider, out result); + return QuantityParser.Default.TryParse<$quantityName, $unitEnumName>( + str, + provider, + From, + out result); } /// @@ -607,7 +614,7 @@ if ($wrc) {@" /// Error parsing string. public static $unitEnumName ParseUnit(string str) { - return ParseUnitInternal(str, null); + return ParseUnit(str, null); } /// @@ -630,12 +637,12 @@ if ($wrc) {@" public static $unitEnumName ParseUnit(string str, IFormatProvider provider = null) { "@; }@" - return ParseUnitInternal(str, provider); + return UnitParser.Default.Parse<$unitEnumName>(str, provider); } public static bool TryParseUnit(string str, out $unitEnumName unit) { - return TryParseUnitInternal(str, null, out unit); + return TryParseUnit(str, null, out unit); } /// @@ -658,114 +665,7 @@ if ($wrc) {@" public static bool TryParseUnit(string str, IFormatProvider provider, out $unitEnumName unit) { "@; }@" - return TryParseUnitInternal(str, provider, out unit); - } - - /// - /// Parse a string with one or two quantities of the format "<quantity> <unit>". - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// - /// Length.Parse("5.5 m", new CultureInfo("en-US")); - /// - /// The value of 'str' cannot be null. - /// - /// Expected string to have one or two pairs of quantity and unit in the format - /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" - /// - /// - /// More than one unit is represented by the specified unit abbreviation. - /// Example: Volume.Parse("1 cup") will throw, because it can refer to any of - /// , and . - /// - /// - /// If anything else goes wrong, typically due to a bug or unhandled case. - /// We wrap exceptions in to allow you to distinguish - /// Units.NET exceptions from other exceptions. - /// - private static $quantityName ParseInternal(string str, [CanBeNull] IFormatProvider provider) - { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? GlobalConfiguration.DefaultCulture; - - return QuantityParser.Default.Parse<$quantityName, $unitEnumName>(str, provider, ParseUnitInternal, From); - } - - /// - /// Try to parse a string with one or two quantities of the format "<quantity> <unit>". - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// Resulting unit quantity if successful. - /// True if successful, otherwise false. - /// - /// Length.Parse("5.5 m", new CultureInfo("en-US")); - /// - private static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out $quantityName result) - { - result = default; - - if(string.IsNullOrWhiteSpace(str)) - return false; - - provider = provider ?? GlobalConfiguration.DefaultCulture; - - return QuantityParser.Default.TryParse<$quantityName, $unitEnumName>(str, provider, TryParseUnitInternal, From, out result); - } - - /// - /// Parse a unit string. - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); - /// - /// The value of 'str' cannot be null. - /// Error parsing string. - private static $unitEnumName ParseUnitInternal(string str, IFormatProvider provider = null) - { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unit = UnitParser.Default.Parse<$unitEnumName>(str.Trim(), provider); - - if (unit == $unitEnumName.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized $unitEnumName."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; - } - - /// - /// Parse a unit string. - /// - /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . - /// The parsed unit if successful. - /// True if successful, otherwise false. - /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); - /// - private static bool TryParseUnitInternal(string str, IFormatProvider provider, out $unitEnumName unit) - { - unit = $unitEnumName.Undefined; - - if(string.IsNullOrWhiteSpace(str)) - return false; - - if(!UnitParser.Default.TryParse<$unitEnumName>(str.Trim(), provider, out unit)) - return false; - - if(unit == $unitEnumName.Undefined) - return false; - - return true; + return UnitParser.Default.TryParse<$unitEnumName>(str, provider, out unit); } #endregion