diff --git a/Common/GeneratedCode/Quantities/Acceleration.Common.g.cs b/Common/GeneratedCode/Quantities/Acceleration.Common.g.cs index a8e5593371..44dc8104cc 100644 --- a/Common/GeneratedCode/Quantities/Acceleration.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Acceleration.Common.g.cs @@ -618,7 +618,7 @@ private double AsBaseNumericType(AccelerationUnit unit) /// public static Acceleration Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -631,7 +631,7 @@ public static Acceleration Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Acceleration result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -645,7 +645,140 @@ public static bool TryParse([CanBeNull] string str, out Acceleration result) /// Error parsing string. public static AccelerationUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out AccelerationUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Acceleration ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.MetersPerSecondSquared + y.MetersPerSecondSquared, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Acceleration result) + { + result = default(Acceleration); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Acceleration parsedAcceleration ) + { + parsedAcceleration = default(Acceleration); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedAcceleration = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.MetersPerSecondSquared + y.MetersPerSecondSquared, BaseUnit), 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. + internal static AccelerationUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == AccelerationUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AccelerationUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out AccelerationUnit unit) + { + unit = AccelerationUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == AccelerationUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/AmountOfSubstance.Common.g.cs b/Common/GeneratedCode/Quantities/AmountOfSubstance.Common.g.cs index d1fe0633cf..bd5e39bcd5 100644 --- a/Common/GeneratedCode/Quantities/AmountOfSubstance.Common.g.cs +++ b/Common/GeneratedCode/Quantities/AmountOfSubstance.Common.g.cs @@ -640,7 +640,7 @@ private double AsBaseNumericType(AmountOfSubstanceUnit unit) /// public static AmountOfSubstance Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -653,7 +653,7 @@ public static AmountOfSubstance Parse(string str) /// public static bool TryParse([CanBeNull] string str, out AmountOfSubstance result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -667,7 +667,140 @@ public static bool TryParse([CanBeNull] string str, out AmountOfSubstance result /// Error parsing string. public static AmountOfSubstanceUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out AmountOfSubstanceUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static AmountOfSubstance ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Moles + y.Moles, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out AmountOfSubstance result) + { + result = default(AmountOfSubstance); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out AmountOfSubstance parsedAmountOfSubstance ) + { + parsedAmountOfSubstance = default(AmountOfSubstance); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedAmountOfSubstance = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Moles + y.Moles, BaseUnit), 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. + internal static AmountOfSubstanceUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == AmountOfSubstanceUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AmountOfSubstanceUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out AmountOfSubstanceUnit unit) + { + unit = AmountOfSubstanceUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == AmountOfSubstanceUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/AmplitudeRatio.Common.g.cs b/Common/GeneratedCode/Quantities/AmplitudeRatio.Common.g.cs index 4048cfb0d5..5da1f9d3d7 100644 --- a/Common/GeneratedCode/Quantities/AmplitudeRatio.Common.g.cs +++ b/Common/GeneratedCode/Quantities/AmplitudeRatio.Common.g.cs @@ -419,7 +419,7 @@ private double AsBaseNumericType(AmplitudeRatioUnit unit) /// public static AmplitudeRatio Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -432,7 +432,7 @@ public static AmplitudeRatio Parse(string str) /// public static bool TryParse([CanBeNull] string str, out AmplitudeRatio result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -446,7 +446,140 @@ public static bool TryParse([CanBeNull] string str, out AmplitudeRatio result) /// Error parsing string. public static AmplitudeRatioUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out AmplitudeRatioUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static AmplitudeRatio ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.DecibelVolts + y.DecibelVolts, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out AmplitudeRatio result) + { + result = default(AmplitudeRatio); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out AmplitudeRatio parsedAmplitudeRatio ) + { + parsedAmplitudeRatio = default(AmplitudeRatio); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedAmplitudeRatio = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.DecibelVolts + y.DecibelVolts, BaseUnit), 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. + internal static AmplitudeRatioUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == AmplitudeRatioUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AmplitudeRatioUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out AmplitudeRatioUnit unit) + { + unit = AmplitudeRatioUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == AmplitudeRatioUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Angle.Common.g.cs b/Common/GeneratedCode/Quantities/Angle.Common.g.cs index 4c09a3ccea..6ac9137610 100644 --- a/Common/GeneratedCode/Quantities/Angle.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Angle.Common.g.cs @@ -639,7 +639,7 @@ private double AsBaseNumericType(AngleUnit unit) /// public static Angle Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -652,7 +652,7 @@ public static Angle Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Angle result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -666,7 +666,140 @@ public static bool TryParse([CanBeNull] string str, out Angle result) /// Error parsing string. public static AngleUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out AngleUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Angle ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Degrees + y.Degrees, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Angle result) + { + result = default(Angle); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Angle parsedAngle ) + { + parsedAngle = default(Angle); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedAngle = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Degrees + y.Degrees, BaseUnit), 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. + internal static AngleUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == AngleUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AngleUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out AngleUnit unit) + { + unit = AngleUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == AngleUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ApparentEnergy.Common.g.cs b/Common/GeneratedCode/Quantities/ApparentEnergy.Common.g.cs index b981fce2f8..9ec6fc65e1 100644 --- a/Common/GeneratedCode/Quantities/ApparentEnergy.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ApparentEnergy.Common.g.cs @@ -398,7 +398,7 @@ private double AsBaseNumericType(ApparentEnergyUnit unit) /// public static ApparentEnergy Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -411,7 +411,7 @@ public static ApparentEnergy Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ApparentEnergy result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -425,7 +425,140 @@ public static bool TryParse([CanBeNull] string str, out ApparentEnergy result) /// Error parsing string. public static ApparentEnergyUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ApparentEnergyUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ApparentEnergy ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.VoltampereHours + y.VoltampereHours, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ApparentEnergy result) + { + result = default(ApparentEnergy); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ApparentEnergy parsedApparentEnergy ) + { + parsedApparentEnergy = default(ApparentEnergy); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedApparentEnergy = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.VoltampereHours + y.VoltampereHours, BaseUnit), 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. + internal static ApparentEnergyUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ApparentEnergyUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ApparentEnergyUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ApparentEnergyUnit unit) + { + unit = ApparentEnergyUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ApparentEnergyUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ApparentPower.Common.g.cs b/Common/GeneratedCode/Quantities/ApparentPower.Common.g.cs index 64adcc030f..948e8e1d8d 100644 --- a/Common/GeneratedCode/Quantities/ApparentPower.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ApparentPower.Common.g.cs @@ -420,7 +420,7 @@ private double AsBaseNumericType(ApparentPowerUnit unit) /// public static ApparentPower Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -433,7 +433,7 @@ public static ApparentPower Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ApparentPower result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -447,7 +447,140 @@ public static bool TryParse([CanBeNull] string str, out ApparentPower result) /// Error parsing string. public static ApparentPowerUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ApparentPowerUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ApparentPower ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Voltamperes + y.Voltamperes, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ApparentPower result) + { + result = default(ApparentPower); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ApparentPower parsedApparentPower ) + { + parsedApparentPower = default(ApparentPower); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedApparentPower = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Voltamperes + y.Voltamperes, BaseUnit), 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. + internal static ApparentPowerUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ApparentPowerUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ApparentPowerUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ApparentPowerUnit unit) + { + unit = ApparentPowerUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ApparentPowerUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Area.Common.g.cs b/Common/GeneratedCode/Quantities/Area.Common.g.cs index bfe37ddb0f..94a4cf7e2b 100644 --- a/Common/GeneratedCode/Quantities/Area.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Area.Common.g.cs @@ -618,7 +618,7 @@ private double AsBaseNumericType(AreaUnit unit) /// public static Area Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -631,7 +631,7 @@ public static Area Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Area result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -645,7 +645,140 @@ public static bool TryParse([CanBeNull] string str, out Area result) /// Error parsing string. public static AreaUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out AreaUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Area ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.SquareMeters + y.SquareMeters, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Area result) + { + result = default(Area); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Area parsedArea ) + { + parsedArea = default(Area); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedArea = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.SquareMeters + y.SquareMeters, BaseUnit), 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. + internal static AreaUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == AreaUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AreaUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out AreaUnit unit) + { + unit = AreaUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == AreaUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/AreaDensity.Common.g.cs b/Common/GeneratedCode/Quantities/AreaDensity.Common.g.cs index 4805aa43fe..939ca16520 100644 --- a/Common/GeneratedCode/Quantities/AreaDensity.Common.g.cs +++ b/Common/GeneratedCode/Quantities/AreaDensity.Common.g.cs @@ -354,7 +354,7 @@ private double AsBaseNumericType(AreaDensityUnit unit) /// public static AreaDensity Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -367,7 +367,7 @@ public static AreaDensity Parse(string str) /// public static bool TryParse([CanBeNull] string str, out AreaDensity result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -381,7 +381,140 @@ public static bool TryParse([CanBeNull] string str, out AreaDensity result) /// Error parsing string. public static AreaDensityUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out AreaDensityUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static AreaDensity ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.KilogramsPerSquareMeter + y.KilogramsPerSquareMeter, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out AreaDensity result) + { + result = default(AreaDensity); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out AreaDensity parsedAreaDensity ) + { + parsedAreaDensity = default(AreaDensity); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedAreaDensity = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.KilogramsPerSquareMeter + y.KilogramsPerSquareMeter, BaseUnit), 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. + internal static AreaDensityUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == AreaDensityUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AreaDensityUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out AreaDensityUnit unit) + { + unit = AreaDensityUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == AreaDensityUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/AreaMomentOfInertia.Common.g.cs b/Common/GeneratedCode/Quantities/AreaMomentOfInertia.Common.g.cs index 5cc7ada85c..81907ff61c 100644 --- a/Common/GeneratedCode/Quantities/AreaMomentOfInertia.Common.g.cs +++ b/Common/GeneratedCode/Quantities/AreaMomentOfInertia.Common.g.cs @@ -464,7 +464,7 @@ private double AsBaseNumericType(AreaMomentOfInertiaUnit unit) /// public static AreaMomentOfInertia Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -477,7 +477,7 @@ public static AreaMomentOfInertia Parse(string str) /// public static bool TryParse([CanBeNull] string str, out AreaMomentOfInertia result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -491,7 +491,140 @@ public static bool TryParse([CanBeNull] string str, out AreaMomentOfInertia resu /// Error parsing string. public static AreaMomentOfInertiaUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out AreaMomentOfInertiaUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static AreaMomentOfInertia ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.MetersToTheFourth + y.MetersToTheFourth, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out AreaMomentOfInertia result) + { + result = default(AreaMomentOfInertia); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out AreaMomentOfInertia parsedAreaMomentOfInertia ) + { + parsedAreaMomentOfInertia = default(AreaMomentOfInertia); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedAreaMomentOfInertia = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.MetersToTheFourth + y.MetersToTheFourth, BaseUnit), 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. + internal static AreaMomentOfInertiaUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == AreaMomentOfInertiaUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AreaMomentOfInertiaUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out AreaMomentOfInertiaUnit unit) + { + unit = AreaMomentOfInertiaUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == AreaMomentOfInertiaUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/BitRate.Common.g.cs b/Common/GeneratedCode/Quantities/BitRate.Common.g.cs index 81ef3af1f2..9ea16d6800 100644 --- a/Common/GeneratedCode/Quantities/BitRate.Common.g.cs +++ b/Common/GeneratedCode/Quantities/BitRate.Common.g.cs @@ -903,7 +903,7 @@ private decimal AsBaseNumericType(BitRateUnit unit) /// public static BitRate Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -916,7 +916,7 @@ public static BitRate Parse(string str) /// public static bool TryParse([CanBeNull] string str, out BitRate result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -930,7 +930,140 @@ public static bool TryParse([CanBeNull] string str, out BitRate result) /// Error parsing string. public static BitRateUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out BitRateUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static BitRate ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.BitsPerSecond + y.BitsPerSecond, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out BitRate result) + { + result = default(BitRate); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out BitRate parsedBitRate ) + { + parsedBitRate = default(BitRate); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedBitRate = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.BitsPerSecond + y.BitsPerSecond, BaseUnit), 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. + internal static BitRateUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == BitRateUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized BitRateUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out BitRateUnit unit) + { + unit = BitRateUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == BitRateUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/BrakeSpecificFuelConsumption.Common.g.cs b/Common/GeneratedCode/Quantities/BrakeSpecificFuelConsumption.Common.g.cs index 6a68250d36..a0968da781 100644 --- a/Common/GeneratedCode/Quantities/BrakeSpecificFuelConsumption.Common.g.cs +++ b/Common/GeneratedCode/Quantities/BrakeSpecificFuelConsumption.Common.g.cs @@ -398,7 +398,7 @@ private double AsBaseNumericType(BrakeSpecificFuelConsumptionUnit unit) /// public static BrakeSpecificFuelConsumption Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -411,7 +411,7 @@ public static BrakeSpecificFuelConsumption Parse(string str) /// public static bool TryParse([CanBeNull] string str, out BrakeSpecificFuelConsumption result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -425,7 +425,140 @@ public static bool TryParse([CanBeNull] string str, out BrakeSpecificFuelConsump /// Error parsing string. public static BrakeSpecificFuelConsumptionUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out BrakeSpecificFuelConsumptionUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static BrakeSpecificFuelConsumption ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.KilogramsPerJoule + y.KilogramsPerJoule, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out BrakeSpecificFuelConsumption result) + { + result = default(BrakeSpecificFuelConsumption); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out BrakeSpecificFuelConsumption parsedBrakeSpecificFuelConsumption ) + { + parsedBrakeSpecificFuelConsumption = default(BrakeSpecificFuelConsumption); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedBrakeSpecificFuelConsumption = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.KilogramsPerJoule + y.KilogramsPerJoule, BaseUnit), 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. + internal static BrakeSpecificFuelConsumptionUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == BrakeSpecificFuelConsumptionUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized BrakeSpecificFuelConsumptionUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out BrakeSpecificFuelConsumptionUnit unit) + { + unit = BrakeSpecificFuelConsumptionUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == BrakeSpecificFuelConsumptionUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Capacitance.Common.g.cs b/Common/GeneratedCode/Quantities/Capacitance.Common.g.cs index c3ca20dd2b..07e602b6c1 100644 --- a/Common/GeneratedCode/Quantities/Capacitance.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Capacitance.Common.g.cs @@ -354,7 +354,7 @@ private double AsBaseNumericType(CapacitanceUnit unit) /// public static Capacitance Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -367,7 +367,7 @@ public static Capacitance Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Capacitance result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -381,7 +381,140 @@ public static bool TryParse([CanBeNull] string str, out Capacitance result) /// Error parsing string. public static CapacitanceUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out CapacitanceUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Capacitance ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Farads + y.Farads, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Capacitance result) + { + result = default(Capacitance); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Capacitance parsedCapacitance ) + { + parsedCapacitance = default(Capacitance); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedCapacitance = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Farads + y.Farads, BaseUnit), 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. + internal static CapacitanceUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == CapacitanceUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized CapacitanceUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out CapacitanceUnit unit) + { + unit = CapacitanceUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == CapacitanceUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Density.Common.g.cs b/Common/GeneratedCode/Quantities/Density.Common.g.cs index 1dcd99a874..1518011407 100644 --- a/Common/GeneratedCode/Quantities/Density.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Density.Common.g.cs @@ -1168,7 +1168,7 @@ private double AsBaseNumericType(DensityUnit unit) /// public static Density Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -1181,7 +1181,7 @@ public static Density Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Density result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -1195,7 +1195,140 @@ public static bool TryParse([CanBeNull] string str, out Density result) /// Error parsing string. public static DensityUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out DensityUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Density ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.KilogramsPerCubicMeter + y.KilogramsPerCubicMeter, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Density result) + { + result = default(Density); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Density parsedDensity ) + { + parsedDensity = default(Density); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedDensity = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.KilogramsPerCubicMeter + y.KilogramsPerCubicMeter, BaseUnit), 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. + internal static DensityUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == DensityUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized DensityUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out DensityUnit unit) + { + unit = DensityUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == DensityUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Duration.Common.g.cs b/Common/GeneratedCode/Quantities/Duration.Common.g.cs index 0a0f8a7223..5b689b53d3 100644 --- a/Common/GeneratedCode/Quantities/Duration.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Duration.Common.g.cs @@ -552,7 +552,7 @@ private double AsBaseNumericType(DurationUnit unit) /// public static Duration Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -565,7 +565,7 @@ public static Duration Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Duration result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -579,7 +579,140 @@ public static bool TryParse([CanBeNull] string str, out Duration result) /// Error parsing string. public static DurationUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out DurationUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Duration ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Seconds + y.Seconds, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Duration result) + { + result = default(Duration); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Duration parsedDuration ) + { + parsedDuration = default(Duration); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedDuration = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Seconds + y.Seconds, BaseUnit), 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. + internal static DurationUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == DurationUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized DurationUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out DurationUnit unit) + { + unit = DurationUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == DurationUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/DynamicViscosity.Common.g.cs b/Common/GeneratedCode/Quantities/DynamicViscosity.Common.g.cs index 7526692cb4..5dd82ed68f 100644 --- a/Common/GeneratedCode/Quantities/DynamicViscosity.Common.g.cs +++ b/Common/GeneratedCode/Quantities/DynamicViscosity.Common.g.cs @@ -464,7 +464,7 @@ private double AsBaseNumericType(DynamicViscosityUnit unit) /// public static DynamicViscosity Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -477,7 +477,7 @@ public static DynamicViscosity Parse(string str) /// public static bool TryParse([CanBeNull] string str, out DynamicViscosity result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -491,7 +491,140 @@ public static bool TryParse([CanBeNull] string str, out DynamicViscosity result) /// Error parsing string. public static DynamicViscosityUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out DynamicViscosityUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static DynamicViscosity ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.NewtonSecondsPerMeterSquared + y.NewtonSecondsPerMeterSquared, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out DynamicViscosity result) + { + result = default(DynamicViscosity); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out DynamicViscosity parsedDynamicViscosity ) + { + parsedDynamicViscosity = default(DynamicViscosity); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedDynamicViscosity = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.NewtonSecondsPerMeterSquared + y.NewtonSecondsPerMeterSquared, BaseUnit), 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. + internal static DynamicViscosityUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == DynamicViscosityUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized DynamicViscosityUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out DynamicViscosityUnit unit) + { + unit = DynamicViscosityUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == DynamicViscosityUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ElectricAdmittance.Common.g.cs b/Common/GeneratedCode/Quantities/ElectricAdmittance.Common.g.cs index bd7b8dcf11..4be3969368 100644 --- a/Common/GeneratedCode/Quantities/ElectricAdmittance.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ElectricAdmittance.Common.g.cs @@ -420,7 +420,7 @@ private double AsBaseNumericType(ElectricAdmittanceUnit unit) /// public static ElectricAdmittance Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -433,7 +433,7 @@ public static ElectricAdmittance Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ElectricAdmittance result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -447,7 +447,140 @@ public static bool TryParse([CanBeNull] string str, out ElectricAdmittance resul /// Error parsing string. public static ElectricAdmittanceUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ElectricAdmittanceUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ElectricAdmittance ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Siemens + y.Siemens, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricAdmittance result) + { + result = default(ElectricAdmittance); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ElectricAdmittance parsedElectricAdmittance ) + { + parsedElectricAdmittance = default(ElectricAdmittance); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedElectricAdmittance = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Siemens + y.Siemens, BaseUnit), 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. + internal static ElectricAdmittanceUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ElectricAdmittanceUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricAdmittanceUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ElectricAdmittanceUnit unit) + { + unit = ElectricAdmittanceUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ElectricAdmittanceUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ElectricCharge.Common.g.cs b/Common/GeneratedCode/Quantities/ElectricCharge.Common.g.cs index 7ef87f20ea..cca446beed 100644 --- a/Common/GeneratedCode/Quantities/ElectricCharge.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ElectricCharge.Common.g.cs @@ -354,7 +354,7 @@ private double AsBaseNumericType(ElectricChargeUnit unit) /// public static ElectricCharge Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -367,7 +367,7 @@ public static ElectricCharge Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ElectricCharge result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -381,7 +381,140 @@ public static bool TryParse([CanBeNull] string str, out ElectricCharge result) /// Error parsing string. public static ElectricChargeUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ElectricChargeUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ElectricCharge ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Coulombs + y.Coulombs, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricCharge result) + { + result = default(ElectricCharge); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ElectricCharge parsedElectricCharge ) + { + parsedElectricCharge = default(ElectricCharge); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedElectricCharge = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Coulombs + y.Coulombs, BaseUnit), 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. + internal static ElectricChargeUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ElectricChargeUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricChargeUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ElectricChargeUnit unit) + { + unit = ElectricChargeUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ElectricChargeUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ElectricChargeDensity.Common.g.cs b/Common/GeneratedCode/Quantities/ElectricChargeDensity.Common.g.cs index bfbdc89bec..e0a41c7261 100644 --- a/Common/GeneratedCode/Quantities/ElectricChargeDensity.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ElectricChargeDensity.Common.g.cs @@ -354,7 +354,7 @@ private double AsBaseNumericType(ElectricChargeDensityUnit unit) /// public static ElectricChargeDensity Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -367,7 +367,7 @@ public static ElectricChargeDensity Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ElectricChargeDensity result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -381,7 +381,140 @@ public static bool TryParse([CanBeNull] string str, out ElectricChargeDensity re /// Error parsing string. public static ElectricChargeDensityUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ElectricChargeDensityUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ElectricChargeDensity ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.CoulombsPerCubicMeter + y.CoulombsPerCubicMeter, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricChargeDensity result) + { + result = default(ElectricChargeDensity); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ElectricChargeDensity parsedElectricChargeDensity ) + { + parsedElectricChargeDensity = default(ElectricChargeDensity); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedElectricChargeDensity = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.CoulombsPerCubicMeter + y.CoulombsPerCubicMeter, BaseUnit), 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. + internal static ElectricChargeDensityUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ElectricChargeDensityUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricChargeDensityUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ElectricChargeDensityUnit unit) + { + unit = ElectricChargeDensityUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ElectricChargeDensityUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ElectricConductance.Common.g.cs b/Common/GeneratedCode/Quantities/ElectricConductance.Common.g.cs index ce2c3adec5..9afad9ed94 100644 --- a/Common/GeneratedCode/Quantities/ElectricConductance.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ElectricConductance.Common.g.cs @@ -398,7 +398,7 @@ private double AsBaseNumericType(ElectricConductanceUnit unit) /// public static ElectricConductance Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -411,7 +411,7 @@ public static ElectricConductance Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ElectricConductance result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -425,7 +425,140 @@ public static bool TryParse([CanBeNull] string str, out ElectricConductance resu /// Error parsing string. public static ElectricConductanceUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ElectricConductanceUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ElectricConductance ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Siemens + y.Siemens, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricConductance result) + { + result = default(ElectricConductance); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ElectricConductance parsedElectricConductance ) + { + parsedElectricConductance = default(ElectricConductance); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedElectricConductance = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Siemens + y.Siemens, BaseUnit), 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. + internal static ElectricConductanceUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ElectricConductanceUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricConductanceUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ElectricConductanceUnit unit) + { + unit = ElectricConductanceUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ElectricConductanceUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ElectricConductivity.Common.g.cs b/Common/GeneratedCode/Quantities/ElectricConductivity.Common.g.cs index 058190485c..1c9763be5e 100644 --- a/Common/GeneratedCode/Quantities/ElectricConductivity.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ElectricConductivity.Common.g.cs @@ -354,7 +354,7 @@ private double AsBaseNumericType(ElectricConductivityUnit unit) /// public static ElectricConductivity Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -367,7 +367,7 @@ public static ElectricConductivity Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ElectricConductivity result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -381,7 +381,140 @@ public static bool TryParse([CanBeNull] string str, out ElectricConductivity res /// Error parsing string. public static ElectricConductivityUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ElectricConductivityUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ElectricConductivity ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.SiemensPerMeter + y.SiemensPerMeter, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricConductivity result) + { + result = default(ElectricConductivity); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ElectricConductivity parsedElectricConductivity ) + { + parsedElectricConductivity = default(ElectricConductivity); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedElectricConductivity = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.SiemensPerMeter + y.SiemensPerMeter, BaseUnit), 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. + internal static ElectricConductivityUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ElectricConductivityUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricConductivityUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ElectricConductivityUnit unit) + { + unit = ElectricConductivityUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ElectricConductivityUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ElectricCurrent.Common.g.cs b/Common/GeneratedCode/Quantities/ElectricCurrent.Common.g.cs index 7426711345..54219f2138 100644 --- a/Common/GeneratedCode/Quantities/ElectricCurrent.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ElectricCurrent.Common.g.cs @@ -508,7 +508,7 @@ private double AsBaseNumericType(ElectricCurrentUnit unit) /// public static ElectricCurrent Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -521,7 +521,7 @@ public static ElectricCurrent Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ElectricCurrent result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -535,7 +535,140 @@ public static bool TryParse([CanBeNull] string str, out ElectricCurrent result) /// Error parsing string. public static ElectricCurrentUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ElectricCurrentUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ElectricCurrent ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Amperes + y.Amperes, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricCurrent result) + { + result = default(ElectricCurrent); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ElectricCurrent parsedElectricCurrent ) + { + parsedElectricCurrent = default(ElectricCurrent); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedElectricCurrent = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Amperes + y.Amperes, BaseUnit), 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. + internal static ElectricCurrentUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ElectricCurrentUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricCurrentUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ElectricCurrentUnit unit) + { + unit = ElectricCurrentUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ElectricCurrentUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ElectricCurrentDensity.Common.g.cs b/Common/GeneratedCode/Quantities/ElectricCurrentDensity.Common.g.cs index dc541c4669..91b9fe8bf3 100644 --- a/Common/GeneratedCode/Quantities/ElectricCurrentDensity.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ElectricCurrentDensity.Common.g.cs @@ -354,7 +354,7 @@ private double AsBaseNumericType(ElectricCurrentDensityUnit unit) /// public static ElectricCurrentDensity Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -367,7 +367,7 @@ public static ElectricCurrentDensity Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ElectricCurrentDensity result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -381,7 +381,140 @@ public static bool TryParse([CanBeNull] string str, out ElectricCurrentDensity r /// Error parsing string. public static ElectricCurrentDensityUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ElectricCurrentDensityUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ElectricCurrentDensity ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.AmperesPerSquareMeter + y.AmperesPerSquareMeter, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricCurrentDensity result) + { + result = default(ElectricCurrentDensity); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ElectricCurrentDensity parsedElectricCurrentDensity ) + { + parsedElectricCurrentDensity = default(ElectricCurrentDensity); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedElectricCurrentDensity = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.AmperesPerSquareMeter + y.AmperesPerSquareMeter, BaseUnit), 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. + internal static ElectricCurrentDensityUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ElectricCurrentDensityUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricCurrentDensityUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ElectricCurrentDensityUnit unit) + { + unit = ElectricCurrentDensityUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ElectricCurrentDensityUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ElectricCurrentGradient.Common.g.cs b/Common/GeneratedCode/Quantities/ElectricCurrentGradient.Common.g.cs index 7ed7bacd30..9087f1d84e 100644 --- a/Common/GeneratedCode/Quantities/ElectricCurrentGradient.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ElectricCurrentGradient.Common.g.cs @@ -354,7 +354,7 @@ private double AsBaseNumericType(ElectricCurrentGradientUnit unit) /// public static ElectricCurrentGradient Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -367,7 +367,7 @@ public static ElectricCurrentGradient Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ElectricCurrentGradient result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -381,7 +381,140 @@ public static bool TryParse([CanBeNull] string str, out ElectricCurrentGradient /// Error parsing string. public static ElectricCurrentGradientUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ElectricCurrentGradientUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ElectricCurrentGradient ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.AmperesPerSecond + y.AmperesPerSecond, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricCurrentGradient result) + { + result = default(ElectricCurrentGradient); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ElectricCurrentGradient parsedElectricCurrentGradient ) + { + parsedElectricCurrentGradient = default(ElectricCurrentGradient); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedElectricCurrentGradient = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.AmperesPerSecond + y.AmperesPerSecond, BaseUnit), 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. + internal static ElectricCurrentGradientUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ElectricCurrentGradientUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricCurrentGradientUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ElectricCurrentGradientUnit unit) + { + unit = ElectricCurrentGradientUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ElectricCurrentGradientUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ElectricField.Common.g.cs b/Common/GeneratedCode/Quantities/ElectricField.Common.g.cs index eeb5b8a477..976ff9665b 100644 --- a/Common/GeneratedCode/Quantities/ElectricField.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ElectricField.Common.g.cs @@ -354,7 +354,7 @@ private double AsBaseNumericType(ElectricFieldUnit unit) /// public static ElectricField Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -367,7 +367,7 @@ public static ElectricField Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ElectricField result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -381,7 +381,140 @@ public static bool TryParse([CanBeNull] string str, out ElectricField result) /// Error parsing string. public static ElectricFieldUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ElectricFieldUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ElectricField ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.VoltsPerMeter + y.VoltsPerMeter, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricField result) + { + result = default(ElectricField); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ElectricField parsedElectricField ) + { + parsedElectricField = default(ElectricField); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedElectricField = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.VoltsPerMeter + y.VoltsPerMeter, BaseUnit), 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. + internal static ElectricFieldUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ElectricFieldUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricFieldUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ElectricFieldUnit unit) + { + unit = ElectricFieldUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ElectricFieldUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ElectricInductance.Common.g.cs b/Common/GeneratedCode/Quantities/ElectricInductance.Common.g.cs index 09d9b21491..a777c367bb 100644 --- a/Common/GeneratedCode/Quantities/ElectricInductance.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ElectricInductance.Common.g.cs @@ -354,7 +354,7 @@ private double AsBaseNumericType(ElectricInductanceUnit unit) /// public static ElectricInductance Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -367,7 +367,7 @@ public static ElectricInductance Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ElectricInductance result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -381,7 +381,140 @@ public static bool TryParse([CanBeNull] string str, out ElectricInductance resul /// Error parsing string. public static ElectricInductanceUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ElectricInductanceUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ElectricInductance ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Henries + y.Henries, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricInductance result) + { + result = default(ElectricInductance); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ElectricInductance parsedElectricInductance ) + { + parsedElectricInductance = default(ElectricInductance); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedElectricInductance = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Henries + y.Henries, BaseUnit), 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. + internal static ElectricInductanceUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ElectricInductanceUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricInductanceUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ElectricInductanceUnit unit) + { + unit = ElectricInductanceUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ElectricInductanceUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ElectricPotential.Common.g.cs b/Common/GeneratedCode/Quantities/ElectricPotential.Common.g.cs index 3154a2aa61..3068a1e051 100644 --- a/Common/GeneratedCode/Quantities/ElectricPotential.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ElectricPotential.Common.g.cs @@ -442,7 +442,7 @@ private double AsBaseNumericType(ElectricPotentialUnit unit) /// public static ElectricPotential Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -455,7 +455,7 @@ public static ElectricPotential Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ElectricPotential result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -469,7 +469,140 @@ public static bool TryParse([CanBeNull] string str, out ElectricPotential result /// Error parsing string. public static ElectricPotentialUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ElectricPotentialUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ElectricPotential ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Volts + y.Volts, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricPotential result) + { + result = default(ElectricPotential); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ElectricPotential parsedElectricPotential ) + { + parsedElectricPotential = default(ElectricPotential); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedElectricPotential = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Volts + y.Volts, BaseUnit), 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. + internal static ElectricPotentialUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ElectricPotentialUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricPotentialUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ElectricPotentialUnit unit) + { + unit = ElectricPotentialUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ElectricPotentialUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ElectricPotentialAc.Common.g.cs b/Common/GeneratedCode/Quantities/ElectricPotentialAc.Common.g.cs index ac85ea5c43..2240bddba4 100644 --- a/Common/GeneratedCode/Quantities/ElectricPotentialAc.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ElectricPotentialAc.Common.g.cs @@ -441,7 +441,7 @@ private double AsBaseNumericType(ElectricPotentialAcUnit unit) /// public static ElectricPotentialAc Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -454,7 +454,7 @@ public static ElectricPotentialAc Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ElectricPotentialAc result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -468,7 +468,140 @@ public static bool TryParse([CanBeNull] string str, out ElectricPotentialAc resu /// Error parsing string. public static ElectricPotentialAcUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ElectricPotentialAcUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ElectricPotentialAc ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.VoltsAc + y.VoltsAc, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricPotentialAc result) + { + result = default(ElectricPotentialAc); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ElectricPotentialAc parsedElectricPotentialAc ) + { + parsedElectricPotentialAc = default(ElectricPotentialAc); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedElectricPotentialAc = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.VoltsAc + y.VoltsAc, BaseUnit), 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. + internal static ElectricPotentialAcUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ElectricPotentialAcUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricPotentialAcUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ElectricPotentialAcUnit unit) + { + unit = ElectricPotentialAcUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ElectricPotentialAcUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ElectricPotentialDc.Common.g.cs b/Common/GeneratedCode/Quantities/ElectricPotentialDc.Common.g.cs index 054cf090b3..623378c8ba 100644 --- a/Common/GeneratedCode/Quantities/ElectricPotentialDc.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ElectricPotentialDc.Common.g.cs @@ -441,7 +441,7 @@ private double AsBaseNumericType(ElectricPotentialDcUnit unit) /// public static ElectricPotentialDc Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -454,7 +454,7 @@ public static ElectricPotentialDc Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ElectricPotentialDc result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -468,7 +468,140 @@ public static bool TryParse([CanBeNull] string str, out ElectricPotentialDc resu /// Error parsing string. public static ElectricPotentialDcUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ElectricPotentialDcUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ElectricPotentialDc ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.VoltsDc + y.VoltsDc, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricPotentialDc result) + { + result = default(ElectricPotentialDc); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ElectricPotentialDc parsedElectricPotentialDc ) + { + parsedElectricPotentialDc = default(ElectricPotentialDc); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedElectricPotentialDc = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.VoltsDc + y.VoltsDc, BaseUnit), 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. + internal static ElectricPotentialDcUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ElectricPotentialDcUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricPotentialDcUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ElectricPotentialDcUnit unit) + { + unit = ElectricPotentialDcUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ElectricPotentialDcUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ElectricResistance.Common.g.cs b/Common/GeneratedCode/Quantities/ElectricResistance.Common.g.cs index 97800ecf71..0659067ba1 100644 --- a/Common/GeneratedCode/Quantities/ElectricResistance.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ElectricResistance.Common.g.cs @@ -420,7 +420,7 @@ private double AsBaseNumericType(ElectricResistanceUnit unit) /// public static ElectricResistance Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -433,7 +433,7 @@ public static ElectricResistance Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ElectricResistance result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -447,7 +447,140 @@ public static bool TryParse([CanBeNull] string str, out ElectricResistance resul /// Error parsing string. public static ElectricResistanceUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ElectricResistanceUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ElectricResistance ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Ohms + y.Ohms, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricResistance result) + { + result = default(ElectricResistance); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ElectricResistance parsedElectricResistance ) + { + parsedElectricResistance = default(ElectricResistance); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedElectricResistance = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Ohms + y.Ohms, BaseUnit), 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. + internal static ElectricResistanceUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ElectricResistanceUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricResistanceUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ElectricResistanceUnit unit) + { + unit = ElectricResistanceUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ElectricResistanceUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ElectricResistivity.Common.g.cs b/Common/GeneratedCode/Quantities/ElectricResistivity.Common.g.cs index 943897b0d9..0fda40944f 100644 --- a/Common/GeneratedCode/Quantities/ElectricResistivity.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ElectricResistivity.Common.g.cs @@ -420,7 +420,7 @@ private double AsBaseNumericType(ElectricResistivityUnit unit) /// public static ElectricResistivity Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -433,7 +433,7 @@ public static ElectricResistivity Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ElectricResistivity result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -447,7 +447,140 @@ public static bool TryParse([CanBeNull] string str, out ElectricResistivity resu /// Error parsing string. public static ElectricResistivityUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ElectricResistivityUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ElectricResistivity ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.OhmMeters + y.OhmMeters, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricResistivity result) + { + result = default(ElectricResistivity); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ElectricResistivity parsedElectricResistivity ) + { + parsedElectricResistivity = default(ElectricResistivity); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedElectricResistivity = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.OhmMeters + y.OhmMeters, BaseUnit), 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. + internal static ElectricResistivityUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ElectricResistivityUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricResistivityUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ElectricResistivityUnit unit) + { + unit = ElectricResistivityUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ElectricResistivityUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Energy.Common.g.cs b/Common/GeneratedCode/Quantities/Energy.Common.g.cs index cf89bec60b..f1f840a76c 100644 --- a/Common/GeneratedCode/Quantities/Energy.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Energy.Common.g.cs @@ -816,7 +816,7 @@ private double AsBaseNumericType(EnergyUnit unit) /// public static Energy Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -829,7 +829,7 @@ public static Energy Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Energy result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -843,7 +843,140 @@ public static bool TryParse([CanBeNull] string str, out Energy result) /// Error parsing string. public static EnergyUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out EnergyUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Energy ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Joules + y.Joules, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Energy result) + { + result = default(Energy); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Energy parsedEnergy ) + { + parsedEnergy = default(Energy); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedEnergy = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Joules + y.Joules, BaseUnit), 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. + internal static EnergyUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == EnergyUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized EnergyUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out EnergyUnit unit) + { + unit = EnergyUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == EnergyUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Entropy.Common.g.cs b/Common/GeneratedCode/Quantities/Entropy.Common.g.cs index 9ab2a50a45..e216af68f9 100644 --- a/Common/GeneratedCode/Quantities/Entropy.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Entropy.Common.g.cs @@ -486,7 +486,7 @@ private double AsBaseNumericType(EntropyUnit unit) /// public static Entropy Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -499,7 +499,7 @@ public static Entropy Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Entropy result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -513,7 +513,140 @@ public static bool TryParse([CanBeNull] string str, out Entropy result) /// Error parsing string. public static EntropyUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out EntropyUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Entropy ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.JoulesPerKelvin + y.JoulesPerKelvin, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Entropy result) + { + result = default(Entropy); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Entropy parsedEntropy ) + { + parsedEntropy = default(Entropy); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedEntropy = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.JoulesPerKelvin + y.JoulesPerKelvin, BaseUnit), 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. + internal static EntropyUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == EntropyUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized EntropyUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out EntropyUnit unit) + { + unit = EntropyUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == EntropyUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Force.Common.g.cs b/Common/GeneratedCode/Quantities/Force.Common.g.cs index 542712a8ad..5f939f5860 100644 --- a/Common/GeneratedCode/Quantities/Force.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Force.Common.g.cs @@ -552,7 +552,7 @@ private double AsBaseNumericType(ForceUnit unit) /// public static Force Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -565,7 +565,7 @@ public static Force Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Force result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -579,7 +579,140 @@ public static bool TryParse([CanBeNull] string str, out Force result) /// Error parsing string. public static ForceUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ForceUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Force ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Newtons + y.Newtons, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Force result) + { + result = default(Force); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Force parsedForce ) + { + parsedForce = default(Force); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedForce = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Newtons + y.Newtons, BaseUnit), 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. + internal static ForceUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ForceUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ForceUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ForceUnit unit) + { + unit = ForceUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ForceUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ForceChangeRate.Common.g.cs b/Common/GeneratedCode/Quantities/ForceChangeRate.Common.g.cs index 1787168a08..3ff3ad25c4 100644 --- a/Common/GeneratedCode/Quantities/ForceChangeRate.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ForceChangeRate.Common.g.cs @@ -574,7 +574,7 @@ private double AsBaseNumericType(ForceChangeRateUnit unit) /// public static ForceChangeRate Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -587,7 +587,7 @@ public static ForceChangeRate Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ForceChangeRate result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -601,7 +601,140 @@ public static bool TryParse([CanBeNull] string str, out ForceChangeRate result) /// Error parsing string. public static ForceChangeRateUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ForceChangeRateUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ForceChangeRate ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.NewtonsPerSecond + y.NewtonsPerSecond, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ForceChangeRate result) + { + result = default(ForceChangeRate); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ForceChangeRate parsedForceChangeRate ) + { + parsedForceChangeRate = default(ForceChangeRate); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedForceChangeRate = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.NewtonsPerSecond + y.NewtonsPerSecond, BaseUnit), 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. + internal static ForceChangeRateUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ForceChangeRateUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ForceChangeRateUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ForceChangeRateUnit unit) + { + unit = ForceChangeRateUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ForceChangeRateUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ForcePerLength.Common.g.cs b/Common/GeneratedCode/Quantities/ForcePerLength.Common.g.cs index 4db68929fb..3f7b1f1523 100644 --- a/Common/GeneratedCode/Quantities/ForcePerLength.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ForcePerLength.Common.g.cs @@ -530,7 +530,7 @@ private double AsBaseNumericType(ForcePerLengthUnit unit) /// public static ForcePerLength Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -543,7 +543,7 @@ public static ForcePerLength Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ForcePerLength result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -557,7 +557,140 @@ public static bool TryParse([CanBeNull] string str, out ForcePerLength result) /// Error parsing string. public static ForcePerLengthUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ForcePerLengthUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ForcePerLength ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.NewtonsPerMeter + y.NewtonsPerMeter, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ForcePerLength result) + { + result = default(ForcePerLength); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ForcePerLength parsedForcePerLength ) + { + parsedForcePerLength = default(ForcePerLength); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedForcePerLength = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.NewtonsPerMeter + y.NewtonsPerMeter, BaseUnit), 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. + internal static ForcePerLengthUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ForcePerLengthUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ForcePerLengthUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ForcePerLengthUnit unit) + { + unit = ForcePerLengthUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ForcePerLengthUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Frequency.Common.g.cs b/Common/GeneratedCode/Quantities/Frequency.Common.g.cs index 58c7df8fe3..86c6cfa121 100644 --- a/Common/GeneratedCode/Quantities/Frequency.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Frequency.Common.g.cs @@ -508,7 +508,7 @@ private double AsBaseNumericType(FrequencyUnit unit) /// public static Frequency Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -521,7 +521,7 @@ public static Frequency Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Frequency result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -535,7 +535,140 @@ public static bool TryParse([CanBeNull] string str, out Frequency result) /// Error parsing string. public static FrequencyUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out FrequencyUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Frequency ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Hertz + y.Hertz, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Frequency result) + { + result = default(Frequency); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Frequency parsedFrequency ) + { + parsedFrequency = default(Frequency); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedFrequency = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Hertz + y.Hertz, BaseUnit), 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. + internal static FrequencyUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == FrequencyUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized FrequencyUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out FrequencyUnit unit) + { + unit = FrequencyUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == FrequencyUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/HeatFlux.Common.g.cs b/Common/GeneratedCode/Quantities/HeatFlux.Common.g.cs index 14db30b59c..428b14478b 100644 --- a/Common/GeneratedCode/Quantities/HeatFlux.Common.g.cs +++ b/Common/GeneratedCode/Quantities/HeatFlux.Common.g.cs @@ -684,7 +684,7 @@ private double AsBaseNumericType(HeatFluxUnit unit) /// public static HeatFlux Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -697,7 +697,7 @@ public static HeatFlux Parse(string str) /// public static bool TryParse([CanBeNull] string str, out HeatFlux result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -711,7 +711,140 @@ public static bool TryParse([CanBeNull] string str, out HeatFlux result) /// Error parsing string. public static HeatFluxUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out HeatFluxUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static HeatFlux ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.WattsPerSquareMeter + y.WattsPerSquareMeter, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out HeatFlux result) + { + result = default(HeatFlux); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out HeatFlux parsedHeatFlux ) + { + parsedHeatFlux = default(HeatFlux); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedHeatFlux = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.WattsPerSquareMeter + y.WattsPerSquareMeter, BaseUnit), 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. + internal static HeatFluxUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == HeatFluxUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized HeatFluxUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out HeatFluxUnit unit) + { + unit = HeatFluxUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == HeatFluxUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/HeatTransferCoefficient.Common.g.cs b/Common/GeneratedCode/Quantities/HeatTransferCoefficient.Common.g.cs index e86ec8d9ed..26f1fc97ac 100644 --- a/Common/GeneratedCode/Quantities/HeatTransferCoefficient.Common.g.cs +++ b/Common/GeneratedCode/Quantities/HeatTransferCoefficient.Common.g.cs @@ -376,7 +376,7 @@ private double AsBaseNumericType(HeatTransferCoefficientUnit unit) /// public static HeatTransferCoefficient Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -389,7 +389,7 @@ public static HeatTransferCoefficient Parse(string str) /// public static bool TryParse([CanBeNull] string str, out HeatTransferCoefficient result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -403,7 +403,140 @@ public static bool TryParse([CanBeNull] string str, out HeatTransferCoefficient /// Error parsing string. public static HeatTransferCoefficientUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out HeatTransferCoefficientUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static HeatTransferCoefficient ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.WattsPerSquareMeterKelvin + y.WattsPerSquareMeterKelvin, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out HeatTransferCoefficient result) + { + result = default(HeatTransferCoefficient); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out HeatTransferCoefficient parsedHeatTransferCoefficient ) + { + parsedHeatTransferCoefficient = default(HeatTransferCoefficient); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedHeatTransferCoefficient = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.WattsPerSquareMeterKelvin + y.WattsPerSquareMeterKelvin, BaseUnit), 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. + internal static HeatTransferCoefficientUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == HeatTransferCoefficientUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized HeatTransferCoefficientUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out HeatTransferCoefficientUnit unit) + { + unit = HeatTransferCoefficientUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == HeatTransferCoefficientUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Illuminance.Common.g.cs b/Common/GeneratedCode/Quantities/Illuminance.Common.g.cs index dd6cc24532..20cd40b396 100644 --- a/Common/GeneratedCode/Quantities/Illuminance.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Illuminance.Common.g.cs @@ -420,7 +420,7 @@ private double AsBaseNumericType(IlluminanceUnit unit) /// public static Illuminance Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -433,7 +433,7 @@ public static Illuminance Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Illuminance result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -447,7 +447,140 @@ public static bool TryParse([CanBeNull] string str, out Illuminance result) /// Error parsing string. public static IlluminanceUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out IlluminanceUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Illuminance ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Lux + y.Lux, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Illuminance result) + { + result = default(Illuminance); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Illuminance parsedIlluminance ) + { + parsedIlluminance = default(Illuminance); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedIlluminance = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Lux + y.Lux, BaseUnit), 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. + internal static IlluminanceUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == IlluminanceUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized IlluminanceUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out IlluminanceUnit unit) + { + unit = IlluminanceUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == IlluminanceUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Information.Common.g.cs b/Common/GeneratedCode/Quantities/Information.Common.g.cs index 5905735552..7c09535991 100644 --- a/Common/GeneratedCode/Quantities/Information.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Information.Common.g.cs @@ -903,7 +903,7 @@ private decimal AsBaseNumericType(InformationUnit unit) /// public static Information Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -916,7 +916,7 @@ public static Information Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Information result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -930,7 +930,140 @@ public static bool TryParse([CanBeNull] string str, out Information result) /// Error parsing string. public static InformationUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out InformationUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Information ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Bits + y.Bits, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Information result) + { + result = default(Information); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Information parsedInformation ) + { + parsedInformation = default(Information); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedInformation = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Bits + y.Bits, BaseUnit), 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. + internal static InformationUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out InformationUnit unit) + { + unit = InformationUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == InformationUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Irradiance.Common.g.cs b/Common/GeneratedCode/Quantities/Irradiance.Common.g.cs index 49ac93a5e3..df55015452 100644 --- a/Common/GeneratedCode/Quantities/Irradiance.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Irradiance.Common.g.cs @@ -376,7 +376,7 @@ private double AsBaseNumericType(IrradianceUnit unit) /// public static Irradiance Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -389,7 +389,7 @@ public static Irradiance Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Irradiance result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -403,7 +403,140 @@ public static bool TryParse([CanBeNull] string str, out Irradiance result) /// Error parsing string. public static IrradianceUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out IrradianceUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Irradiance ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.WattsPerSquareMeter + y.WattsPerSquareMeter, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Irradiance result) + { + result = default(Irradiance); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Irradiance parsedIrradiance ) + { + parsedIrradiance = default(Irradiance); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedIrradiance = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.WattsPerSquareMeter + y.WattsPerSquareMeter, BaseUnit), 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. + internal static IrradianceUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == IrradianceUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized IrradianceUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out IrradianceUnit unit) + { + unit = IrradianceUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == IrradianceUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Irradiation.Common.g.cs b/Common/GeneratedCode/Quantities/Irradiation.Common.g.cs index 7c8d78acba..90b7346376 100644 --- a/Common/GeneratedCode/Quantities/Irradiation.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Irradiation.Common.g.cs @@ -398,7 +398,7 @@ private double AsBaseNumericType(IrradiationUnit unit) /// public static Irradiation Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -411,7 +411,7 @@ public static Irradiation Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Irradiation result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -425,7 +425,140 @@ public static bool TryParse([CanBeNull] string str, out Irradiation result) /// Error parsing string. public static IrradiationUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out IrradiationUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Irradiation ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.JoulesPerSquareMeter + y.JoulesPerSquareMeter, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Irradiation result) + { + result = default(Irradiation); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Irradiation parsedIrradiation ) + { + parsedIrradiation = default(Irradiation); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedIrradiation = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.JoulesPerSquareMeter + y.JoulesPerSquareMeter, BaseUnit), 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. + internal static IrradiationUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == IrradiationUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized IrradiationUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out IrradiationUnit unit) + { + unit = IrradiationUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == IrradiationUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/KinematicViscosity.Common.g.cs b/Common/GeneratedCode/Quantities/KinematicViscosity.Common.g.cs index 625e8d8937..066cb6d6c5 100644 --- a/Common/GeneratedCode/Quantities/KinematicViscosity.Common.g.cs +++ b/Common/GeneratedCode/Quantities/KinematicViscosity.Common.g.cs @@ -508,7 +508,7 @@ private double AsBaseNumericType(KinematicViscosityUnit unit) /// public static KinematicViscosity Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -521,7 +521,7 @@ public static KinematicViscosity Parse(string str) /// public static bool TryParse([CanBeNull] string str, out KinematicViscosity result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -535,7 +535,140 @@ public static bool TryParse([CanBeNull] string str, out KinematicViscosity resul /// Error parsing string. public static KinematicViscosityUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out KinematicViscosityUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static KinematicViscosity ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.SquareMetersPerSecond + y.SquareMetersPerSecond, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out KinematicViscosity result) + { + result = default(KinematicViscosity); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out KinematicViscosity parsedKinematicViscosity ) + { + parsedKinematicViscosity = default(KinematicViscosity); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedKinematicViscosity = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.SquareMetersPerSecond + y.SquareMetersPerSecond, BaseUnit), 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. + internal static KinematicViscosityUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == KinematicViscosityUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized KinematicViscosityUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out KinematicViscosityUnit unit) + { + unit = KinematicViscosityUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == KinematicViscosityUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/LapseRate.Common.g.cs b/Common/GeneratedCode/Quantities/LapseRate.Common.g.cs index 50c2ea8b12..0843f8e0d8 100644 --- a/Common/GeneratedCode/Quantities/LapseRate.Common.g.cs +++ b/Common/GeneratedCode/Quantities/LapseRate.Common.g.cs @@ -354,7 +354,7 @@ private double AsBaseNumericType(LapseRateUnit unit) /// public static LapseRate Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -367,7 +367,7 @@ public static LapseRate Parse(string str) /// public static bool TryParse([CanBeNull] string str, out LapseRate result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -381,7 +381,140 @@ public static bool TryParse([CanBeNull] string str, out LapseRate result) /// Error parsing string. public static LapseRateUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out LapseRateUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static LapseRate ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.DegreesCelciusPerKilometer + y.DegreesCelciusPerKilometer, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out LapseRate result) + { + result = default(LapseRate); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out LapseRate parsedLapseRate ) + { + parsedLapseRate = default(LapseRate); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedLapseRate = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.DegreesCelciusPerKilometer + y.DegreesCelciusPerKilometer, BaseUnit), 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. + internal static LapseRateUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == LapseRateUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LapseRateUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out LapseRateUnit unit) + { + unit = LapseRateUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == LapseRateUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Length.Common.g.cs b/Common/GeneratedCode/Quantities/Length.Common.g.cs index 4982b78f36..eeb97d31e2 100644 --- a/Common/GeneratedCode/Quantities/Length.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Length.Common.g.cs @@ -816,7 +816,7 @@ private double AsBaseNumericType(LengthUnit unit) /// public static Length Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -829,7 +829,7 @@ public static Length Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Length result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -843,7 +843,140 @@ public static bool TryParse([CanBeNull] string str, out Length result) /// Error parsing string. public static LengthUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out LengthUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Length ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Meters + y.Meters, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Length result) + { + result = default(Length); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Length parsedLength ) + { + parsedLength = default(Length); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedLength = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Meters + y.Meters, BaseUnit), 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. + internal static LengthUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out LengthUnit unit) + { + unit = LengthUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == LengthUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Level.Common.g.cs b/Common/GeneratedCode/Quantities/Level.Common.g.cs index b4640f5b36..53db49c76f 100644 --- a/Common/GeneratedCode/Quantities/Level.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Level.Common.g.cs @@ -375,7 +375,7 @@ private double AsBaseNumericType(LevelUnit unit) /// public static Level Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -388,7 +388,7 @@ public static Level Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Level result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -402,7 +402,140 @@ public static bool TryParse([CanBeNull] string str, out Level result) /// Error parsing string. public static LevelUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out LevelUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Level ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Decibels + y.Decibels, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Level result) + { + result = default(Level); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Level parsedLevel ) + { + parsedLevel = default(Level); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedLevel = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Decibels + y.Decibels, BaseUnit), 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. + internal static LevelUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out LevelUnit unit) + { + unit = LevelUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == LevelUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/LinearDensity.Common.g.cs b/Common/GeneratedCode/Quantities/LinearDensity.Common.g.cs index f1be384a2c..58f0144fdf 100644 --- a/Common/GeneratedCode/Quantities/LinearDensity.Common.g.cs +++ b/Common/GeneratedCode/Quantities/LinearDensity.Common.g.cs @@ -398,7 +398,7 @@ private double AsBaseNumericType(LinearDensityUnit unit) /// public static LinearDensity Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -411,7 +411,7 @@ public static LinearDensity Parse(string str) /// public static bool TryParse([CanBeNull] string str, out LinearDensity result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -425,7 +425,140 @@ public static bool TryParse([CanBeNull] string str, out LinearDensity result) /// Error parsing string. public static LinearDensityUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out LinearDensityUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static LinearDensity ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.KilogramsPerMeter + y.KilogramsPerMeter, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out LinearDensity result) + { + result = default(LinearDensity); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out LinearDensity parsedLinearDensity ) + { + parsedLinearDensity = default(LinearDensity); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedLinearDensity = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.KilogramsPerMeter + y.KilogramsPerMeter, BaseUnit), 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. + internal static LinearDensityUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == LinearDensityUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LinearDensityUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out LinearDensityUnit unit) + { + unit = LinearDensityUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == LinearDensityUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/LuminousFlux.Common.g.cs b/Common/GeneratedCode/Quantities/LuminousFlux.Common.g.cs index 765ecf5c1b..9745b504fd 100644 --- a/Common/GeneratedCode/Quantities/LuminousFlux.Common.g.cs +++ b/Common/GeneratedCode/Quantities/LuminousFlux.Common.g.cs @@ -354,7 +354,7 @@ private double AsBaseNumericType(LuminousFluxUnit unit) /// public static LuminousFlux Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -367,7 +367,7 @@ public static LuminousFlux Parse(string str) /// public static bool TryParse([CanBeNull] string str, out LuminousFlux result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -381,7 +381,140 @@ public static bool TryParse([CanBeNull] string str, out LuminousFlux result) /// Error parsing string. public static LuminousFluxUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out LuminousFluxUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static LuminousFlux ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Lumens + y.Lumens, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out LuminousFlux result) + { + result = default(LuminousFlux); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out LuminousFlux parsedLuminousFlux ) + { + parsedLuminousFlux = default(LuminousFlux); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedLuminousFlux = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Lumens + y.Lumens, BaseUnit), 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. + internal static LuminousFluxUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == LuminousFluxUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LuminousFluxUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out LuminousFluxUnit unit) + { + unit = LuminousFluxUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == LuminousFluxUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/LuminousIntensity.Common.g.cs b/Common/GeneratedCode/Quantities/LuminousIntensity.Common.g.cs index af5593c6ee..782ec54fdf 100644 --- a/Common/GeneratedCode/Quantities/LuminousIntensity.Common.g.cs +++ b/Common/GeneratedCode/Quantities/LuminousIntensity.Common.g.cs @@ -354,7 +354,7 @@ private double AsBaseNumericType(LuminousIntensityUnit unit) /// public static LuminousIntensity Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -367,7 +367,7 @@ public static LuminousIntensity Parse(string str) /// public static bool TryParse([CanBeNull] string str, out LuminousIntensity result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -381,7 +381,140 @@ public static bool TryParse([CanBeNull] string str, out LuminousIntensity result /// Error parsing string. public static LuminousIntensityUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out LuminousIntensityUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static LuminousIntensity ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Candela + y.Candela, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out LuminousIntensity result) + { + result = default(LuminousIntensity); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out LuminousIntensity parsedLuminousIntensity ) + { + parsedLuminousIntensity = default(LuminousIntensity); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedLuminousIntensity = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Candela + y.Candela, BaseUnit), 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. + internal static LuminousIntensityUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == LuminousIntensityUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LuminousIntensityUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out LuminousIntensityUnit unit) + { + unit = LuminousIntensityUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == LuminousIntensityUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/MagneticField.Common.g.cs b/Common/GeneratedCode/Quantities/MagneticField.Common.g.cs index 259d3481cd..7607a1933e 100644 --- a/Common/GeneratedCode/Quantities/MagneticField.Common.g.cs +++ b/Common/GeneratedCode/Quantities/MagneticField.Common.g.cs @@ -354,7 +354,7 @@ private double AsBaseNumericType(MagneticFieldUnit unit) /// public static MagneticField Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -367,7 +367,7 @@ public static MagneticField Parse(string str) /// public static bool TryParse([CanBeNull] string str, out MagneticField result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -381,7 +381,140 @@ public static bool TryParse([CanBeNull] string str, out MagneticField result) /// Error parsing string. public static MagneticFieldUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out MagneticFieldUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static MagneticField ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Teslas + y.Teslas, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out MagneticField result) + { + result = default(MagneticField); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out MagneticField parsedMagneticField ) + { + parsedMagneticField = default(MagneticField); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedMagneticField = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Teslas + y.Teslas, BaseUnit), 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. + internal static MagneticFieldUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == MagneticFieldUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MagneticFieldUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out MagneticFieldUnit unit) + { + unit = MagneticFieldUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == MagneticFieldUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/MagneticFlux.Common.g.cs b/Common/GeneratedCode/Quantities/MagneticFlux.Common.g.cs index af6a381b1b..846167753e 100644 --- a/Common/GeneratedCode/Quantities/MagneticFlux.Common.g.cs +++ b/Common/GeneratedCode/Quantities/MagneticFlux.Common.g.cs @@ -354,7 +354,7 @@ private double AsBaseNumericType(MagneticFluxUnit unit) /// public static MagneticFlux Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -367,7 +367,7 @@ public static MagneticFlux Parse(string str) /// public static bool TryParse([CanBeNull] string str, out MagneticFlux result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -381,7 +381,140 @@ public static bool TryParse([CanBeNull] string str, out MagneticFlux result) /// Error parsing string. public static MagneticFluxUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out MagneticFluxUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static MagneticFlux ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Webers + y.Webers, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out MagneticFlux result) + { + result = default(MagneticFlux); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out MagneticFlux parsedMagneticFlux ) + { + parsedMagneticFlux = default(MagneticFlux); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedMagneticFlux = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Webers + y.Webers, BaseUnit), 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. + internal static MagneticFluxUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == MagneticFluxUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MagneticFluxUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out MagneticFluxUnit unit) + { + unit = MagneticFluxUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == MagneticFluxUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Magnetization.Common.g.cs b/Common/GeneratedCode/Quantities/Magnetization.Common.g.cs index 546065e358..a28f1051d3 100644 --- a/Common/GeneratedCode/Quantities/Magnetization.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Magnetization.Common.g.cs @@ -354,7 +354,7 @@ private double AsBaseNumericType(MagnetizationUnit unit) /// public static Magnetization Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -367,7 +367,7 @@ public static Magnetization Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Magnetization result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -381,7 +381,140 @@ public static bool TryParse([CanBeNull] string str, out Magnetization result) /// Error parsing string. public static MagnetizationUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out MagnetizationUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Magnetization ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.AmperesPerMeter + y.AmperesPerMeter, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Magnetization result) + { + result = default(Magnetization); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Magnetization parsedMagnetization ) + { + parsedMagnetization = default(Magnetization); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedMagnetization = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.AmperesPerMeter + y.AmperesPerMeter, BaseUnit), 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. + internal static MagnetizationUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == MagnetizationUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MagnetizationUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out MagnetizationUnit unit) + { + unit = MagnetizationUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == MagnetizationUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Mass.Common.g.cs b/Common/GeneratedCode/Quantities/Mass.Common.g.cs index de9eee1663..7daec50642 100644 --- a/Common/GeneratedCode/Quantities/Mass.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Mass.Common.g.cs @@ -794,7 +794,7 @@ private double AsBaseNumericType(MassUnit unit) /// public static Mass Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -807,7 +807,7 @@ public static Mass Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Mass result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -821,7 +821,140 @@ public static bool TryParse([CanBeNull] string str, out Mass result) /// Error parsing string. public static MassUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out MassUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Mass ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Kilograms + y.Kilograms, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Mass result) + { + result = default(Mass); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Mass parsedMass ) + { + parsedMass = default(Mass); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedMass = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Kilograms + y.Kilograms, BaseUnit), 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. + internal static MassUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == MassUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MassUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out MassUnit unit) + { + unit = MassUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == MassUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/MassFlow.Common.g.cs b/Common/GeneratedCode/Quantities/MassFlow.Common.g.cs index 118ac8d6c9..fb559f851f 100644 --- a/Common/GeneratedCode/Quantities/MassFlow.Common.g.cs +++ b/Common/GeneratedCode/Quantities/MassFlow.Common.g.cs @@ -662,7 +662,7 @@ private double AsBaseNumericType(MassFlowUnit unit) /// public static MassFlow Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -675,7 +675,7 @@ public static MassFlow Parse(string str) /// public static bool TryParse([CanBeNull] string str, out MassFlow result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -689,7 +689,140 @@ public static bool TryParse([CanBeNull] string str, out MassFlow result) /// Error parsing string. public static MassFlowUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out MassFlowUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static MassFlow ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.GramsPerSecond + y.GramsPerSecond, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out MassFlow result) + { + result = default(MassFlow); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out MassFlow parsedMassFlow ) + { + parsedMassFlow = default(MassFlow); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedMassFlow = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.GramsPerSecond + y.GramsPerSecond, BaseUnit), 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. + internal static MassFlowUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == MassFlowUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MassFlowUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out MassFlowUnit unit) + { + unit = MassFlowUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == MassFlowUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/MassFlux.Common.g.cs b/Common/GeneratedCode/Quantities/MassFlux.Common.g.cs index af499ab871..0eda7452d3 100644 --- a/Common/GeneratedCode/Quantities/MassFlux.Common.g.cs +++ b/Common/GeneratedCode/Quantities/MassFlux.Common.g.cs @@ -376,7 +376,7 @@ private double AsBaseNumericType(MassFluxUnit unit) /// public static MassFlux Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -389,7 +389,7 @@ public static MassFlux Parse(string str) /// public static bool TryParse([CanBeNull] string str, out MassFlux result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -403,7 +403,140 @@ public static bool TryParse([CanBeNull] string str, out MassFlux result) /// Error parsing string. public static MassFluxUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out MassFluxUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static MassFlux ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.KilogramsPerSecondPerSquareMeter + y.KilogramsPerSecondPerSquareMeter, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out MassFlux result) + { + result = default(MassFlux); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out MassFlux parsedMassFlux ) + { + parsedMassFlux = default(MassFlux); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedMassFlux = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.KilogramsPerSecondPerSquareMeter + y.KilogramsPerSecondPerSquareMeter, BaseUnit), 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. + internal static MassFluxUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == MassFluxUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MassFluxUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out MassFluxUnit unit) + { + unit = MassFluxUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == MassFluxUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/MassMomentOfInertia.Common.g.cs b/Common/GeneratedCode/Quantities/MassMomentOfInertia.Common.g.cs index b8811b4670..9eb4da362a 100644 --- a/Common/GeneratedCode/Quantities/MassMomentOfInertia.Common.g.cs +++ b/Common/GeneratedCode/Quantities/MassMomentOfInertia.Common.g.cs @@ -904,7 +904,7 @@ private double AsBaseNumericType(MassMomentOfInertiaUnit unit) /// public static MassMomentOfInertia Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -917,7 +917,7 @@ public static MassMomentOfInertia Parse(string str) /// public static bool TryParse([CanBeNull] string str, out MassMomentOfInertia result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -931,7 +931,140 @@ public static bool TryParse([CanBeNull] string str, out MassMomentOfInertia resu /// Error parsing string. public static MassMomentOfInertiaUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out MassMomentOfInertiaUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static MassMomentOfInertia ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.KilogramSquareMeters + y.KilogramSquareMeters, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out MassMomentOfInertia result) + { + result = default(MassMomentOfInertia); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out MassMomentOfInertia parsedMassMomentOfInertia ) + { + parsedMassMomentOfInertia = default(MassMomentOfInertia); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedMassMomentOfInertia = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.KilogramSquareMeters + y.KilogramSquareMeters, BaseUnit), 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. + internal static MassMomentOfInertiaUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == MassMomentOfInertiaUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MassMomentOfInertiaUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out MassMomentOfInertiaUnit unit) + { + unit = MassMomentOfInertiaUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == MassMomentOfInertiaUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/MolarEnergy.Common.g.cs b/Common/GeneratedCode/Quantities/MolarEnergy.Common.g.cs index 5a9f136423..76edcbb3d2 100644 --- a/Common/GeneratedCode/Quantities/MolarEnergy.Common.g.cs +++ b/Common/GeneratedCode/Quantities/MolarEnergy.Common.g.cs @@ -398,7 +398,7 @@ private double AsBaseNumericType(MolarEnergyUnit unit) /// public static MolarEnergy Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -411,7 +411,7 @@ public static MolarEnergy Parse(string str) /// public static bool TryParse([CanBeNull] string str, out MolarEnergy result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -425,7 +425,140 @@ public static bool TryParse([CanBeNull] string str, out MolarEnergy result) /// Error parsing string. public static MolarEnergyUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out MolarEnergyUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static MolarEnergy ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.JoulesPerMole + y.JoulesPerMole, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out MolarEnergy result) + { + result = default(MolarEnergy); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out MolarEnergy parsedMolarEnergy ) + { + parsedMolarEnergy = default(MolarEnergy); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedMolarEnergy = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.JoulesPerMole + y.JoulesPerMole, BaseUnit), 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. + internal static MolarEnergyUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == MolarEnergyUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MolarEnergyUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out MolarEnergyUnit unit) + { + unit = MolarEnergyUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == MolarEnergyUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/MolarEntropy.Common.g.cs b/Common/GeneratedCode/Quantities/MolarEntropy.Common.g.cs index 7a35c6dd34..8a09d28c7f 100644 --- a/Common/GeneratedCode/Quantities/MolarEntropy.Common.g.cs +++ b/Common/GeneratedCode/Quantities/MolarEntropy.Common.g.cs @@ -398,7 +398,7 @@ private double AsBaseNumericType(MolarEntropyUnit unit) /// public static MolarEntropy Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -411,7 +411,7 @@ public static MolarEntropy Parse(string str) /// public static bool TryParse([CanBeNull] string str, out MolarEntropy result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -425,7 +425,140 @@ public static bool TryParse([CanBeNull] string str, out MolarEntropy result) /// Error parsing string. public static MolarEntropyUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out MolarEntropyUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static MolarEntropy ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.JoulesPerMoleKelvin + y.JoulesPerMoleKelvin, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out MolarEntropy result) + { + result = default(MolarEntropy); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out MolarEntropy parsedMolarEntropy ) + { + parsedMolarEntropy = default(MolarEntropy); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedMolarEntropy = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.JoulesPerMoleKelvin + y.JoulesPerMoleKelvin, BaseUnit), 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. + internal static MolarEntropyUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == MolarEntropyUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MolarEntropyUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out MolarEntropyUnit unit) + { + unit = MolarEntropyUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == MolarEntropyUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/MolarMass.Common.g.cs b/Common/GeneratedCode/Quantities/MolarMass.Common.g.cs index 1a9a547d86..5bfeb81d45 100644 --- a/Common/GeneratedCode/Quantities/MolarMass.Common.g.cs +++ b/Common/GeneratedCode/Quantities/MolarMass.Common.g.cs @@ -596,7 +596,7 @@ private double AsBaseNumericType(MolarMassUnit unit) /// public static MolarMass Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -609,7 +609,7 @@ public static MolarMass Parse(string str) /// public static bool TryParse([CanBeNull] string str, out MolarMass result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -623,7 +623,140 @@ public static bool TryParse([CanBeNull] string str, out MolarMass result) /// Error parsing string. public static MolarMassUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out MolarMassUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static MolarMass ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.KilogramsPerMole + y.KilogramsPerMole, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out MolarMass result) + { + result = default(MolarMass); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out MolarMass parsedMolarMass ) + { + parsedMolarMass = default(MolarMass); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedMolarMass = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.KilogramsPerMole + y.KilogramsPerMole, BaseUnit), 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. + internal static MolarMassUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == MolarMassUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MolarMassUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out MolarMassUnit unit) + { + unit = MolarMassUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == MolarMassUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Molarity.Common.g.cs b/Common/GeneratedCode/Quantities/Molarity.Common.g.cs index d876910eae..94ff4ae771 100644 --- a/Common/GeneratedCode/Quantities/Molarity.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Molarity.Common.g.cs @@ -508,7 +508,7 @@ private double AsBaseNumericType(MolarityUnit unit) /// public static Molarity Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -521,7 +521,7 @@ public static Molarity Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Molarity result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -535,7 +535,140 @@ public static bool TryParse([CanBeNull] string str, out Molarity result) /// Error parsing string. public static MolarityUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out MolarityUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Molarity ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.MolesPerCubicMeter + y.MolesPerCubicMeter, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Molarity result) + { + result = default(Molarity); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Molarity parsedMolarity ) + { + parsedMolarity = default(Molarity); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedMolarity = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.MolesPerCubicMeter + y.MolesPerCubicMeter, BaseUnit), 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. + internal static MolarityUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == MolarityUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MolarityUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out MolarityUnit unit) + { + unit = MolarityUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == MolarityUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Permeability.Common.g.cs b/Common/GeneratedCode/Quantities/Permeability.Common.g.cs index bac1639711..46ed68e53d 100644 --- a/Common/GeneratedCode/Quantities/Permeability.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Permeability.Common.g.cs @@ -354,7 +354,7 @@ private double AsBaseNumericType(PermeabilityUnit unit) /// public static Permeability Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -367,7 +367,7 @@ public static Permeability Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Permeability result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -381,7 +381,140 @@ public static bool TryParse([CanBeNull] string str, out Permeability result) /// Error parsing string. public static PermeabilityUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out PermeabilityUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Permeability ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.HenriesPerMeter + y.HenriesPerMeter, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Permeability result) + { + result = default(Permeability); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Permeability parsedPermeability ) + { + parsedPermeability = default(Permeability); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedPermeability = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.HenriesPerMeter + y.HenriesPerMeter, BaseUnit), 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. + internal static PermeabilityUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == PermeabilityUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PermeabilityUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out PermeabilityUnit unit) + { + unit = PermeabilityUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == PermeabilityUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Permittivity.Common.g.cs b/Common/GeneratedCode/Quantities/Permittivity.Common.g.cs index 21ec46f21f..5100aa00f6 100644 --- a/Common/GeneratedCode/Quantities/Permittivity.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Permittivity.Common.g.cs @@ -354,7 +354,7 @@ private double AsBaseNumericType(PermittivityUnit unit) /// public static Permittivity Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -367,7 +367,7 @@ public static Permittivity Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Permittivity result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -381,7 +381,140 @@ public static bool TryParse([CanBeNull] string str, out Permittivity result) /// Error parsing string. public static PermittivityUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out PermittivityUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Permittivity ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.FaradsPerMeter + y.FaradsPerMeter, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Permittivity result) + { + result = default(Permittivity); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Permittivity parsedPermittivity ) + { + parsedPermittivity = default(Permittivity); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedPermittivity = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.FaradsPerMeter + y.FaradsPerMeter, BaseUnit), 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. + internal static PermittivityUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == PermittivityUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PermittivityUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out PermittivityUnit unit) + { + unit = PermittivityUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == PermittivityUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Power.Common.g.cs b/Common/GeneratedCode/Quantities/Power.Common.g.cs index c68df87cc6..94dede4a38 100644 --- a/Common/GeneratedCode/Quantities/Power.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Power.Common.g.cs @@ -772,7 +772,7 @@ private decimal AsBaseNumericType(PowerUnit unit) /// public static Power Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -785,7 +785,7 @@ public static Power Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Power result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -799,7 +799,140 @@ public static bool TryParse([CanBeNull] string str, out Power result) /// Error parsing string. public static PowerUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out PowerUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Power ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Watts + y.Watts, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Power result) + { + result = default(Power); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Power parsedPower ) + { + parsedPower = default(Power); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedPower = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Watts + y.Watts, BaseUnit), 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. + internal static PowerUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == PowerUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PowerUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out PowerUnit unit) + { + unit = PowerUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == PowerUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/PowerDensity.Common.g.cs b/Common/GeneratedCode/Quantities/PowerDensity.Common.g.cs index acc9d1ef2b..ea99def73b 100644 --- a/Common/GeneratedCode/Quantities/PowerDensity.Common.g.cs +++ b/Common/GeneratedCode/Quantities/PowerDensity.Common.g.cs @@ -1300,7 +1300,7 @@ private double AsBaseNumericType(PowerDensityUnit unit) /// public static PowerDensity Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -1313,7 +1313,7 @@ public static PowerDensity Parse(string str) /// public static bool TryParse([CanBeNull] string str, out PowerDensity result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -1327,7 +1327,140 @@ public static bool TryParse([CanBeNull] string str, out PowerDensity result) /// Error parsing string. public static PowerDensityUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out PowerDensityUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static PowerDensity ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.WattsPerCubicMeter + y.WattsPerCubicMeter, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out PowerDensity result) + { + result = default(PowerDensity); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out PowerDensity parsedPowerDensity ) + { + parsedPowerDensity = default(PowerDensity); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedPowerDensity = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.WattsPerCubicMeter + y.WattsPerCubicMeter, BaseUnit), 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. + internal static PowerDensityUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == PowerDensityUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PowerDensityUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out PowerDensityUnit unit) + { + unit = PowerDensityUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == PowerDensityUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/PowerRatio.Common.g.cs b/Common/GeneratedCode/Quantities/PowerRatio.Common.g.cs index 95a3cbbe3c..2b9975902b 100644 --- a/Common/GeneratedCode/Quantities/PowerRatio.Common.g.cs +++ b/Common/GeneratedCode/Quantities/PowerRatio.Common.g.cs @@ -375,7 +375,7 @@ private double AsBaseNumericType(PowerRatioUnit unit) /// public static PowerRatio Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -388,7 +388,7 @@ public static PowerRatio Parse(string str) /// public static bool TryParse([CanBeNull] string str, out PowerRatio result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -402,7 +402,140 @@ public static bool TryParse([CanBeNull] string str, out PowerRatio result) /// Error parsing string. public static PowerRatioUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out PowerRatioUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static PowerRatio ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.DecibelWatts + y.DecibelWatts, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out PowerRatio result) + { + result = default(PowerRatio); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out PowerRatio parsedPowerRatio ) + { + parsedPowerRatio = default(PowerRatio); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedPowerRatio = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.DecibelWatts + y.DecibelWatts, BaseUnit), 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. + internal static PowerRatioUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == PowerRatioUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PowerRatioUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out PowerRatioUnit unit) + { + unit = PowerRatioUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == PowerRatioUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Pressure.Common.g.cs b/Common/GeneratedCode/Quantities/Pressure.Common.g.cs index 7c7051c2da..657bff9fb9 100644 --- a/Common/GeneratedCode/Quantities/Pressure.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Pressure.Common.g.cs @@ -1146,7 +1146,7 @@ private double AsBaseNumericType(PressureUnit unit) /// public static Pressure Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -1159,7 +1159,7 @@ public static Pressure Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Pressure result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -1173,7 +1173,140 @@ public static bool TryParse([CanBeNull] string str, out Pressure result) /// Error parsing string. public static PressureUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out PressureUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Pressure ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Pascals + y.Pascals, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Pressure result) + { + result = default(Pressure); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Pressure parsedPressure ) + { + parsedPressure = default(Pressure); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedPressure = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Pascals + y.Pascals, BaseUnit), 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. + internal static PressureUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == PressureUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PressureUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out PressureUnit unit) + { + unit = PressureUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == PressureUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/PressureChangeRate.Common.g.cs b/Common/GeneratedCode/Quantities/PressureChangeRate.Common.g.cs index 5624876bbf..d83bba6e44 100644 --- a/Common/GeneratedCode/Quantities/PressureChangeRate.Common.g.cs +++ b/Common/GeneratedCode/Quantities/PressureChangeRate.Common.g.cs @@ -420,7 +420,7 @@ private double AsBaseNumericType(PressureChangeRateUnit unit) /// public static PressureChangeRate Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -433,7 +433,7 @@ public static PressureChangeRate Parse(string str) /// public static bool TryParse([CanBeNull] string str, out PressureChangeRate result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -447,7 +447,140 @@ public static bool TryParse([CanBeNull] string str, out PressureChangeRate resul /// Error parsing string. public static PressureChangeRateUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out PressureChangeRateUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static PressureChangeRate ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.PascalsPerSecond + y.PascalsPerSecond, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out PressureChangeRate result) + { + result = default(PressureChangeRate); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out PressureChangeRate parsedPressureChangeRate ) + { + parsedPressureChangeRate = default(PressureChangeRate); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedPressureChangeRate = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.PascalsPerSecond + y.PascalsPerSecond, BaseUnit), 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. + internal static PressureChangeRateUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == PressureChangeRateUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PressureChangeRateUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out PressureChangeRateUnit unit) + { + unit = PressureChangeRateUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == PressureChangeRateUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Ratio.Common.g.cs b/Common/GeneratedCode/Quantities/Ratio.Common.g.cs index 07aca3ebd8..06773052d1 100644 --- a/Common/GeneratedCode/Quantities/Ratio.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Ratio.Common.g.cs @@ -463,7 +463,7 @@ private double AsBaseNumericType(RatioUnit unit) /// public static Ratio Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -476,7 +476,7 @@ public static Ratio Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Ratio result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -490,7 +490,140 @@ public static bool TryParse([CanBeNull] string str, out Ratio result) /// Error parsing string. public static RatioUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out RatioUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Ratio ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.DecimalFractions + y.DecimalFractions, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Ratio result) + { + result = default(Ratio); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Ratio parsedRatio ) + { + parsedRatio = default(Ratio); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedRatio = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.DecimalFractions + y.DecimalFractions, BaseUnit), 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. + internal static RatioUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == RatioUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized RatioUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out RatioUnit unit) + { + unit = RatioUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == RatioUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ReactiveEnergy.Common.g.cs b/Common/GeneratedCode/Quantities/ReactiveEnergy.Common.g.cs index bcfbc66da1..bb8b829a39 100644 --- a/Common/GeneratedCode/Quantities/ReactiveEnergy.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ReactiveEnergy.Common.g.cs @@ -398,7 +398,7 @@ private double AsBaseNumericType(ReactiveEnergyUnit unit) /// public static ReactiveEnergy Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -411,7 +411,7 @@ public static ReactiveEnergy Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ReactiveEnergy result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -425,7 +425,140 @@ public static bool TryParse([CanBeNull] string str, out ReactiveEnergy result) /// Error parsing string. public static ReactiveEnergyUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ReactiveEnergyUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ReactiveEnergy ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.VoltampereReactiveHours + y.VoltampereReactiveHours, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ReactiveEnergy result) + { + result = default(ReactiveEnergy); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ReactiveEnergy parsedReactiveEnergy ) + { + parsedReactiveEnergy = default(ReactiveEnergy); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedReactiveEnergy = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.VoltampereReactiveHours + y.VoltampereReactiveHours, BaseUnit), 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. + internal static ReactiveEnergyUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ReactiveEnergyUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ReactiveEnergyUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ReactiveEnergyUnit unit) + { + unit = ReactiveEnergyUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ReactiveEnergyUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ReactivePower.Common.g.cs b/Common/GeneratedCode/Quantities/ReactivePower.Common.g.cs index 0af904f487..69faf15a50 100644 --- a/Common/GeneratedCode/Quantities/ReactivePower.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ReactivePower.Common.g.cs @@ -420,7 +420,7 @@ private double AsBaseNumericType(ReactivePowerUnit unit) /// public static ReactivePower Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -433,7 +433,7 @@ public static ReactivePower Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ReactivePower result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -447,7 +447,140 @@ public static bool TryParse([CanBeNull] string str, out ReactivePower result) /// Error parsing string. public static ReactivePowerUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ReactivePowerUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ReactivePower ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.VoltamperesReactive + y.VoltamperesReactive, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ReactivePower result) + { + result = default(ReactivePower); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ReactivePower parsedReactivePower ) + { + parsedReactivePower = default(ReactivePower); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedReactivePower = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.VoltamperesReactive + y.VoltamperesReactive, BaseUnit), 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. + internal static ReactivePowerUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ReactivePowerUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ReactivePowerUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ReactivePowerUnit unit) + { + unit = ReactivePowerUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ReactivePowerUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/RotationalAcceleration.Common.g.cs b/Common/GeneratedCode/Quantities/RotationalAcceleration.Common.g.cs index 8f19c5888d..11219a3681 100644 --- a/Common/GeneratedCode/Quantities/RotationalAcceleration.Common.g.cs +++ b/Common/GeneratedCode/Quantities/RotationalAcceleration.Common.g.cs @@ -398,7 +398,7 @@ private double AsBaseNumericType(RotationalAccelerationUnit unit) /// public static RotationalAcceleration Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -411,7 +411,7 @@ public static RotationalAcceleration Parse(string str) /// public static bool TryParse([CanBeNull] string str, out RotationalAcceleration result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -425,7 +425,140 @@ public static bool TryParse([CanBeNull] string str, out RotationalAcceleration r /// Error parsing string. public static RotationalAccelerationUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out RotationalAccelerationUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static RotationalAcceleration ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.RadiansPerSecondSquared + y.RadiansPerSecondSquared, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out RotationalAcceleration result) + { + result = default(RotationalAcceleration); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out RotationalAcceleration parsedRotationalAcceleration ) + { + parsedRotationalAcceleration = default(RotationalAcceleration); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedRotationalAcceleration = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.RadiansPerSecondSquared + y.RadiansPerSecondSquared, BaseUnit), 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. + internal static RotationalAccelerationUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == RotationalAccelerationUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized RotationalAccelerationUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out RotationalAccelerationUnit unit) + { + unit = RotationalAccelerationUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == RotationalAccelerationUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/RotationalSpeed.Common.g.cs b/Common/GeneratedCode/Quantities/RotationalSpeed.Common.g.cs index 6d68dc12bc..7648661d40 100644 --- a/Common/GeneratedCode/Quantities/RotationalSpeed.Common.g.cs +++ b/Common/GeneratedCode/Quantities/RotationalSpeed.Common.g.cs @@ -618,7 +618,7 @@ private double AsBaseNumericType(RotationalSpeedUnit unit) /// public static RotationalSpeed Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -631,7 +631,7 @@ public static RotationalSpeed Parse(string str) /// public static bool TryParse([CanBeNull] string str, out RotationalSpeed result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -645,7 +645,140 @@ public static bool TryParse([CanBeNull] string str, out RotationalSpeed result) /// Error parsing string. public static RotationalSpeedUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out RotationalSpeedUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static RotationalSpeed ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.RadiansPerSecond + y.RadiansPerSecond, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out RotationalSpeed result) + { + result = default(RotationalSpeed); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out RotationalSpeed parsedRotationalSpeed ) + { + parsedRotationalSpeed = default(RotationalSpeed); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedRotationalSpeed = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.RadiansPerSecond + y.RadiansPerSecond, BaseUnit), 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. + internal static RotationalSpeedUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == RotationalSpeedUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized RotationalSpeedUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out RotationalSpeedUnit unit) + { + unit = RotationalSpeedUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == RotationalSpeedUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/RotationalStiffness.Common.g.cs b/Common/GeneratedCode/Quantities/RotationalStiffness.Common.g.cs index e45e2e8ed7..242dfa7c16 100644 --- a/Common/GeneratedCode/Quantities/RotationalStiffness.Common.g.cs +++ b/Common/GeneratedCode/Quantities/RotationalStiffness.Common.g.cs @@ -398,7 +398,7 @@ private double AsBaseNumericType(RotationalStiffnessUnit unit) /// public static RotationalStiffness Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -411,7 +411,7 @@ public static RotationalStiffness Parse(string str) /// public static bool TryParse([CanBeNull] string str, out RotationalStiffness result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -425,7 +425,140 @@ public static bool TryParse([CanBeNull] string str, out RotationalStiffness resu /// Error parsing string. public static RotationalStiffnessUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out RotationalStiffnessUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static RotationalStiffness ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.NewtonMetersPerRadian + y.NewtonMetersPerRadian, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out RotationalStiffness result) + { + result = default(RotationalStiffness); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out RotationalStiffness parsedRotationalStiffness ) + { + parsedRotationalStiffness = default(RotationalStiffness); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedRotationalStiffness = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.NewtonMetersPerRadian + y.NewtonMetersPerRadian, BaseUnit), 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. + internal static RotationalStiffnessUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == RotationalStiffnessUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized RotationalStiffnessUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out RotationalStiffnessUnit unit) + { + unit = RotationalStiffnessUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == RotationalStiffnessUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/RotationalStiffnessPerLength.Common.g.cs b/Common/GeneratedCode/Quantities/RotationalStiffnessPerLength.Common.g.cs index b136447df9..e5da2f3769 100644 --- a/Common/GeneratedCode/Quantities/RotationalStiffnessPerLength.Common.g.cs +++ b/Common/GeneratedCode/Quantities/RotationalStiffnessPerLength.Common.g.cs @@ -398,7 +398,7 @@ private double AsBaseNumericType(RotationalStiffnessPerLengthUnit unit) /// public static RotationalStiffnessPerLength Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -411,7 +411,7 @@ public static RotationalStiffnessPerLength Parse(string str) /// public static bool TryParse([CanBeNull] string str, out RotationalStiffnessPerLength result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -425,7 +425,140 @@ public static bool TryParse([CanBeNull] string str, out RotationalStiffnessPerLe /// Error parsing string. public static RotationalStiffnessPerLengthUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out RotationalStiffnessPerLengthUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static RotationalStiffnessPerLength ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.NewtonMetersPerRadianPerMeter + y.NewtonMetersPerRadianPerMeter, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out RotationalStiffnessPerLength result) + { + result = default(RotationalStiffnessPerLength); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out RotationalStiffnessPerLength parsedRotationalStiffnessPerLength ) + { + parsedRotationalStiffnessPerLength = default(RotationalStiffnessPerLength); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedRotationalStiffnessPerLength = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.NewtonMetersPerRadianPerMeter + y.NewtonMetersPerRadianPerMeter, BaseUnit), 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. + internal static RotationalStiffnessPerLengthUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == RotationalStiffnessPerLengthUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized RotationalStiffnessPerLengthUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out RotationalStiffnessPerLengthUnit unit) + { + unit = RotationalStiffnessPerLengthUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == RotationalStiffnessPerLengthUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/SolidAngle.Common.g.cs b/Common/GeneratedCode/Quantities/SolidAngle.Common.g.cs index 216f1004ed..9d05fc10f5 100644 --- a/Common/GeneratedCode/Quantities/SolidAngle.Common.g.cs +++ b/Common/GeneratedCode/Quantities/SolidAngle.Common.g.cs @@ -353,7 +353,7 @@ private double AsBaseNumericType(SolidAngleUnit unit) /// public static SolidAngle Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -366,7 +366,7 @@ public static SolidAngle Parse(string str) /// public static bool TryParse([CanBeNull] string str, out SolidAngle result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -380,7 +380,140 @@ public static bool TryParse([CanBeNull] string str, out SolidAngle result) /// Error parsing string. public static SolidAngleUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out SolidAngleUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static SolidAngle ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Steradians + y.Steradians, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out SolidAngle result) + { + result = default(SolidAngle); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out SolidAngle parsedSolidAngle ) + { + parsedSolidAngle = default(SolidAngle); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedSolidAngle = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Steradians + y.Steradians, BaseUnit), 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. + internal static SolidAngleUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == SolidAngleUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized SolidAngleUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out SolidAngleUnit unit) + { + unit = SolidAngleUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == SolidAngleUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/SpecificEnergy.Common.g.cs b/Common/GeneratedCode/Quantities/SpecificEnergy.Common.g.cs index 4c17911621..95fbc1820b 100644 --- a/Common/GeneratedCode/Quantities/SpecificEnergy.Common.g.cs +++ b/Common/GeneratedCode/Quantities/SpecificEnergy.Common.g.cs @@ -508,7 +508,7 @@ private double AsBaseNumericType(SpecificEnergyUnit unit) /// public static SpecificEnergy Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -521,7 +521,7 @@ public static SpecificEnergy Parse(string str) /// public static bool TryParse([CanBeNull] string str, out SpecificEnergy result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -535,7 +535,140 @@ public static bool TryParse([CanBeNull] string str, out SpecificEnergy result) /// Error parsing string. public static SpecificEnergyUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out SpecificEnergyUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static SpecificEnergy ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.JoulesPerKilogram + y.JoulesPerKilogram, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out SpecificEnergy result) + { + result = default(SpecificEnergy); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out SpecificEnergy parsedSpecificEnergy ) + { + parsedSpecificEnergy = default(SpecificEnergy); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedSpecificEnergy = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.JoulesPerKilogram + y.JoulesPerKilogram, BaseUnit), 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. + internal static SpecificEnergyUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == SpecificEnergyUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized SpecificEnergyUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out SpecificEnergyUnit unit) + { + unit = SpecificEnergyUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == SpecificEnergyUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/SpecificEntropy.Common.g.cs b/Common/GeneratedCode/Quantities/SpecificEntropy.Common.g.cs index e18e5d9138..3036763f6d 100644 --- a/Common/GeneratedCode/Quantities/SpecificEntropy.Common.g.cs +++ b/Common/GeneratedCode/Quantities/SpecificEntropy.Common.g.cs @@ -508,7 +508,7 @@ private double AsBaseNumericType(SpecificEntropyUnit unit) /// public static SpecificEntropy Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -521,7 +521,7 @@ public static SpecificEntropy Parse(string str) /// public static bool TryParse([CanBeNull] string str, out SpecificEntropy result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -535,7 +535,140 @@ public static bool TryParse([CanBeNull] string str, out SpecificEntropy result) /// Error parsing string. public static SpecificEntropyUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out SpecificEntropyUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static SpecificEntropy ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.JoulesPerKilogramKelvin + y.JoulesPerKilogramKelvin, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out SpecificEntropy result) + { + result = default(SpecificEntropy); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out SpecificEntropy parsedSpecificEntropy ) + { + parsedSpecificEntropy = default(SpecificEntropy); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedSpecificEntropy = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.JoulesPerKilogramKelvin + y.JoulesPerKilogramKelvin, BaseUnit), 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. + internal static SpecificEntropyUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == SpecificEntropyUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized SpecificEntropyUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out SpecificEntropyUnit unit) + { + unit = SpecificEntropyUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == SpecificEntropyUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/SpecificVolume.Common.g.cs b/Common/GeneratedCode/Quantities/SpecificVolume.Common.g.cs index f1576aa45a..7abb0a56be 100644 --- a/Common/GeneratedCode/Quantities/SpecificVolume.Common.g.cs +++ b/Common/GeneratedCode/Quantities/SpecificVolume.Common.g.cs @@ -376,7 +376,7 @@ private double AsBaseNumericType(SpecificVolumeUnit unit) /// public static SpecificVolume Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -389,7 +389,7 @@ public static SpecificVolume Parse(string str) /// public static bool TryParse([CanBeNull] string str, out SpecificVolume result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -403,7 +403,140 @@ public static bool TryParse([CanBeNull] string str, out SpecificVolume result) /// Error parsing string. public static SpecificVolumeUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out SpecificVolumeUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static SpecificVolume ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.CubicMetersPerKilogram + y.CubicMetersPerKilogram, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out SpecificVolume result) + { + result = default(SpecificVolume); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out SpecificVolume parsedSpecificVolume ) + { + parsedSpecificVolume = default(SpecificVolume); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedSpecificVolume = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.CubicMetersPerKilogram + y.CubicMetersPerKilogram, BaseUnit), 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. + internal static SpecificVolumeUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == SpecificVolumeUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized SpecificVolumeUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out SpecificVolumeUnit unit) + { + unit = SpecificVolumeUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == SpecificVolumeUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/SpecificWeight.Common.g.cs b/Common/GeneratedCode/Quantities/SpecificWeight.Common.g.cs index dd444e0451..8dfe18a0dd 100644 --- a/Common/GeneratedCode/Quantities/SpecificWeight.Common.g.cs +++ b/Common/GeneratedCode/Quantities/SpecificWeight.Common.g.cs @@ -706,7 +706,7 @@ private double AsBaseNumericType(SpecificWeightUnit unit) /// public static SpecificWeight Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -719,7 +719,7 @@ public static SpecificWeight Parse(string str) /// public static bool TryParse([CanBeNull] string str, out SpecificWeight result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -733,7 +733,140 @@ public static bool TryParse([CanBeNull] string str, out SpecificWeight result) /// Error parsing string. public static SpecificWeightUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out SpecificWeightUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static SpecificWeight ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.NewtonsPerCubicMeter + y.NewtonsPerCubicMeter, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out SpecificWeight result) + { + result = default(SpecificWeight); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out SpecificWeight parsedSpecificWeight ) + { + parsedSpecificWeight = default(SpecificWeight); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedSpecificWeight = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.NewtonsPerCubicMeter + y.NewtonsPerCubicMeter, BaseUnit), 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. + internal static SpecificWeightUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == SpecificWeightUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized SpecificWeightUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out SpecificWeightUnit unit) + { + unit = SpecificWeightUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == SpecificWeightUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Speed.Common.g.cs b/Common/GeneratedCode/Quantities/Speed.Common.g.cs index 37510683ae..1b3c50decf 100644 --- a/Common/GeneratedCode/Quantities/Speed.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Speed.Common.g.cs @@ -1036,7 +1036,7 @@ private double AsBaseNumericType(SpeedUnit unit) /// public static Speed Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -1049,7 +1049,7 @@ public static Speed Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Speed result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -1063,7 +1063,140 @@ public static bool TryParse([CanBeNull] string str, out Speed result) /// Error parsing string. public static SpeedUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out SpeedUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Speed ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.MetersPerSecond + y.MetersPerSecond, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Speed result) + { + result = default(Speed); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Speed parsedSpeed ) + { + parsedSpeed = default(Speed); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedSpeed = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.MetersPerSecond + y.MetersPerSecond, BaseUnit), 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. + internal static SpeedUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == SpeedUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized SpeedUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out SpeedUnit unit) + { + unit = SpeedUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == SpeedUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Temperature.Common.g.cs b/Common/GeneratedCode/Quantities/Temperature.Common.g.cs index 1db9b0d530..c5075f99f9 100644 --- a/Common/GeneratedCode/Quantities/Temperature.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Temperature.Common.g.cs @@ -508,7 +508,7 @@ private double AsBaseNumericType(TemperatureUnit unit) /// public static Temperature Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -521,7 +521,7 @@ public static Temperature Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Temperature result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -535,7 +535,140 @@ public static bool TryParse([CanBeNull] string str, out Temperature result) /// Error parsing string. public static TemperatureUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out TemperatureUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Temperature ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Kelvins + y.Kelvins, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Temperature result) + { + result = default(Temperature); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Temperature parsedTemperature ) + { + parsedTemperature = default(Temperature); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedTemperature = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Kelvins + y.Kelvins, BaseUnit), 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. + internal static TemperatureUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == TemperatureUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized TemperatureUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out TemperatureUnit unit) + { + unit = TemperatureUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == TemperatureUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/TemperatureChangeRate.Common.g.cs b/Common/GeneratedCode/Quantities/TemperatureChangeRate.Common.g.cs index 2321cdf29d..8b8a518ebe 100644 --- a/Common/GeneratedCode/Quantities/TemperatureChangeRate.Common.g.cs +++ b/Common/GeneratedCode/Quantities/TemperatureChangeRate.Common.g.cs @@ -552,7 +552,7 @@ private double AsBaseNumericType(TemperatureChangeRateUnit unit) /// public static TemperatureChangeRate Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -565,7 +565,7 @@ public static TemperatureChangeRate Parse(string str) /// public static bool TryParse([CanBeNull] string str, out TemperatureChangeRate result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -579,7 +579,140 @@ public static bool TryParse([CanBeNull] string str, out TemperatureChangeRate re /// Error parsing string. public static TemperatureChangeRateUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out TemperatureChangeRateUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static TemperatureChangeRate ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.DegreesCelsiusPerSecond + y.DegreesCelsiusPerSecond, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out TemperatureChangeRate result) + { + result = default(TemperatureChangeRate); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out TemperatureChangeRate parsedTemperatureChangeRate ) + { + parsedTemperatureChangeRate = default(TemperatureChangeRate); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedTemperatureChangeRate = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.DegreesCelsiusPerSecond + y.DegreesCelsiusPerSecond, BaseUnit), 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. + internal static TemperatureChangeRateUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == TemperatureChangeRateUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized TemperatureChangeRateUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out TemperatureChangeRateUnit unit) + { + unit = TemperatureChangeRateUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == TemperatureChangeRateUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/TemperatureDelta.Common.g.cs b/Common/GeneratedCode/Quantities/TemperatureDelta.Common.g.cs index 0964975291..258f953247 100644 --- a/Common/GeneratedCode/Quantities/TemperatureDelta.Common.g.cs +++ b/Common/GeneratedCode/Quantities/TemperatureDelta.Common.g.cs @@ -507,7 +507,7 @@ private double AsBaseNumericType(TemperatureDeltaUnit unit) /// public static TemperatureDelta Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -520,7 +520,7 @@ public static TemperatureDelta Parse(string str) /// public static bool TryParse([CanBeNull] string str, out TemperatureDelta result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -534,7 +534,140 @@ public static bool TryParse([CanBeNull] string str, out TemperatureDelta result) /// Error parsing string. public static TemperatureDeltaUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out TemperatureDeltaUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static TemperatureDelta ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.Kelvins + y.Kelvins, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out TemperatureDelta result) + { + result = default(TemperatureDelta); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out TemperatureDelta parsedTemperatureDelta ) + { + parsedTemperatureDelta = default(TemperatureDelta); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedTemperatureDelta = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.Kelvins + y.Kelvins, BaseUnit), 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. + internal static TemperatureDeltaUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == TemperatureDeltaUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized TemperatureDeltaUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out TemperatureDeltaUnit unit) + { + unit = TemperatureDeltaUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == TemperatureDeltaUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ThermalConductivity.Common.g.cs b/Common/GeneratedCode/Quantities/ThermalConductivity.Common.g.cs index a0eae874d9..8f75cb0481 100644 --- a/Common/GeneratedCode/Quantities/ThermalConductivity.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ThermalConductivity.Common.g.cs @@ -376,7 +376,7 @@ private double AsBaseNumericType(ThermalConductivityUnit unit) /// public static ThermalConductivity Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -389,7 +389,7 @@ public static ThermalConductivity Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ThermalConductivity result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -403,7 +403,140 @@ public static bool TryParse([CanBeNull] string str, out ThermalConductivity resu /// Error parsing string. public static ThermalConductivityUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ThermalConductivityUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ThermalConductivity ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.WattsPerMeterKelvin + y.WattsPerMeterKelvin, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ThermalConductivity result) + { + result = default(ThermalConductivity); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ThermalConductivity parsedThermalConductivity ) + { + parsedThermalConductivity = default(ThermalConductivity); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedThermalConductivity = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.WattsPerMeterKelvin + y.WattsPerMeterKelvin, BaseUnit), 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. + internal static ThermalConductivityUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ThermalConductivityUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ThermalConductivityUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ThermalConductivityUnit unit) + { + unit = ThermalConductivityUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ThermalConductivityUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/ThermalResistance.Common.g.cs b/Common/GeneratedCode/Quantities/ThermalResistance.Common.g.cs index 347feafd51..2b4b999e51 100644 --- a/Common/GeneratedCode/Quantities/ThermalResistance.Common.g.cs +++ b/Common/GeneratedCode/Quantities/ThermalResistance.Common.g.cs @@ -442,7 +442,7 @@ private double AsBaseNumericType(ThermalResistanceUnit unit) /// public static ThermalResistance Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -455,7 +455,7 @@ public static ThermalResistance Parse(string str) /// public static bool TryParse([CanBeNull] string str, out ThermalResistance result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -469,7 +469,140 @@ public static bool TryParse([CanBeNull] string str, out ThermalResistance result /// Error parsing string. public static ThermalResistanceUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out ThermalResistanceUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static ThermalResistance ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.SquareMeterKelvinsPerKilowatt + y.SquareMeterKelvinsPerKilowatt, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ThermalResistance result) + { + result = default(ThermalResistance); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out ThermalResistance parsedThermalResistance ) + { + parsedThermalResistance = default(ThermalResistance); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedThermalResistance = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.SquareMeterKelvinsPerKilowatt + y.SquareMeterKelvinsPerKilowatt, BaseUnit), 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. + internal static ThermalResistanceUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ThermalResistanceUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ThermalResistanceUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out ThermalResistanceUnit unit) + { + unit = ThermalResistanceUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == ThermalResistanceUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Torque.Common.g.cs b/Common/GeneratedCode/Quantities/Torque.Common.g.cs index 7d53e59fbc..6183876f8e 100644 --- a/Common/GeneratedCode/Quantities/Torque.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Torque.Common.g.cs @@ -794,7 +794,7 @@ private double AsBaseNumericType(TorqueUnit unit) /// public static Torque Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -807,7 +807,7 @@ public static Torque Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Torque result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -821,7 +821,140 @@ public static bool TryParse([CanBeNull] string str, out Torque result) /// Error parsing string. public static TorqueUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out TorqueUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Torque ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.NewtonMeters + y.NewtonMeters, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Torque result) + { + result = default(Torque); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Torque parsedTorque ) + { + parsedTorque = default(Torque); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedTorque = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.NewtonMeters + y.NewtonMeters, BaseUnit), 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. + internal static TorqueUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == TorqueUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized TorqueUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out TorqueUnit unit) + { + unit = TorqueUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == TorqueUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/VitaminA.Common.g.cs b/Common/GeneratedCode/Quantities/VitaminA.Common.g.cs index aa5c351adc..a77e3c33c0 100644 --- a/Common/GeneratedCode/Quantities/VitaminA.Common.g.cs +++ b/Common/GeneratedCode/Quantities/VitaminA.Common.g.cs @@ -353,7 +353,7 @@ private double AsBaseNumericType(VitaminAUnit unit) /// public static VitaminA Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -366,7 +366,7 @@ public static VitaminA Parse(string str) /// public static bool TryParse([CanBeNull] string str, out VitaminA result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -380,7 +380,140 @@ public static bool TryParse([CanBeNull] string str, out VitaminA result) /// Error parsing string. public static VitaminAUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out VitaminAUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static VitaminA ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.InternationalUnits + y.InternationalUnits, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out VitaminA result) + { + result = default(VitaminA); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out VitaminA parsedVitaminA ) + { + parsedVitaminA = default(VitaminA); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedVitaminA = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.InternationalUnits + y.InternationalUnits, BaseUnit), 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. + internal static VitaminAUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == VitaminAUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized VitaminAUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out VitaminAUnit unit) + { + unit = VitaminAUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == VitaminAUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/Volume.Common.g.cs b/Common/GeneratedCode/Quantities/Volume.Common.g.cs index 1c10d748c5..1f378dc9ea 100644 --- a/Common/GeneratedCode/Quantities/Volume.Common.g.cs +++ b/Common/GeneratedCode/Quantities/Volume.Common.g.cs @@ -1256,7 +1256,7 @@ private double AsBaseNumericType(VolumeUnit unit) /// public static Volume Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -1269,7 +1269,7 @@ public static Volume Parse(string str) /// public static bool TryParse([CanBeNull] string str, out Volume result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -1283,7 +1283,140 @@ public static bool TryParse([CanBeNull] string str, out Volume result) /// Error parsing string. public static VolumeUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out VolumeUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static Volume ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.CubicMeters + y.CubicMeters, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Volume result) + { + result = default(Volume); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out Volume parsedVolume ) + { + parsedVolume = default(Volume); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedVolume = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.CubicMeters + y.CubicMeters, BaseUnit), 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. + internal static VolumeUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == VolumeUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized VolumeUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out VolumeUnit unit) + { + unit = VolumeUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == VolumeUnit.Undefined) + return false; + + return true; } #endregion diff --git a/Common/GeneratedCode/Quantities/VolumeFlow.Common.g.cs b/Common/GeneratedCode/Quantities/VolumeFlow.Common.g.cs index 0ee33107e6..7da599ead9 100644 --- a/Common/GeneratedCode/Quantities/VolumeFlow.Common.g.cs +++ b/Common/GeneratedCode/Quantities/VolumeFlow.Common.g.cs @@ -904,7 +904,7 @@ private double AsBaseNumericType(VolumeFlowUnit unit) /// public static VolumeFlow Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -917,7 +917,7 @@ public static VolumeFlow Parse(string str) /// public static bool TryParse([CanBeNull] string str, out VolumeFlow result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -931,7 +931,140 @@ public static bool TryParse([CanBeNull] string str, out VolumeFlow result) /// Error parsing string. public static VolumeFlowUnit ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out VolumeFlowUnit unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static VolumeFlow ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.CubicMetersPerSecond + y.CubicMetersPerSecond, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out VolumeFlow result) + { + result = default(VolumeFlow); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out VolumeFlow parsedVolumeFlow ) + { + parsedVolumeFlow = default(VolumeFlow); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsedVolumeFlow = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.CubicMetersPerSecond + y.CubicMetersPerSecond, BaseUnit), 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. + internal static VolumeFlowUnit ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == VolumeFlowUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized VolumeFlowUnit."); + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out VolumeFlowUnit unit) + { + unit = VolumeFlowUnit.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse(str.Trim(), out unit)) + return false; + + if(unit == VolumeFlowUnit.Undefined) + return false; + + return true; } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Acceleration.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Acceleration.WindowsRuntimeComponent.g.cs index 18746bc96e..36adbe7a7f 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Acceleration.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Acceleration.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Acceleration Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - AccelerationUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromMetersPerSecondSquared(x.MetersPerSecondSquared + y.MetersPerSecondSquared)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Acceleration Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Acceleration result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Acceleration); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static AccelerationUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static AccelerationUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out AccelerationUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == AccelerationUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AccelerationUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/AmountOfSubstance.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/AmountOfSubstance.WindowsRuntimeComponent.g.cs index bff21fd0dd..b8c3e51fe0 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/AmountOfSubstance.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/AmountOfSubstance.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static AmountOfSubstance Parse(string str, [CanBeNull] string cultureName // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - AmountOfSubstanceUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromMoles(x.Moles + y.Moles)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static AmountOfSubstance Parse(string str, [CanBeNull] string cultureName /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out AmountOfSubstance result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(AmountOfSubstance); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static AmountOfSubstanceUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static AmountOfSubstanceUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out AmountOfSubstanceUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == AmountOfSubstanceUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AmountOfSubstanceUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/AmplitudeRatio.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/AmplitudeRatio.WindowsRuntimeComponent.g.cs index e1430d991f..8c0c861b2a 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/AmplitudeRatio.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/AmplitudeRatio.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static AmplitudeRatio Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - AmplitudeRatioUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromDecibelVolts(x.DecibelVolts + y.DecibelVolts)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static AmplitudeRatio Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out AmplitudeRatio result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(AmplitudeRatio); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static AmplitudeRatioUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static AmplitudeRatioUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out AmplitudeRatioUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == AmplitudeRatioUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AmplitudeRatioUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Angle.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Angle.WindowsRuntimeComponent.g.cs index 13dfeb0c5e..9c66956bb0 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Angle.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Angle.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Angle Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - AngleUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromDegrees(x.Degrees + y.Degrees)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Angle Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Angle result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Angle); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static AngleUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static AngleUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out AngleUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == AngleUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AngleUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ApparentEnergy.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ApparentEnergy.WindowsRuntimeComponent.g.cs index 414dda709b..a514d06881 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ApparentEnergy.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ApparentEnergy.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ApparentEnergy Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ApparentEnergyUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromVoltampereHours(x.VoltampereHours + y.VoltampereHours)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ApparentEnergy Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ApparentEnergy result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ApparentEnergy); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ApparentEnergyUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ApparentEnergyUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ApparentEnergyUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ApparentEnergyUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ApparentEnergyUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ApparentPower.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ApparentPower.WindowsRuntimeComponent.g.cs index a00dbb607b..1236902125 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ApparentPower.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ApparentPower.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ApparentPower Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ApparentPowerUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromVoltamperes(x.Voltamperes + y.Voltamperes)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ApparentPower Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ApparentPower result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ApparentPower); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ApparentPowerUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ApparentPowerUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ApparentPowerUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ApparentPowerUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ApparentPowerUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Area.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Area.WindowsRuntimeComponent.g.cs index 74dd8cbcc6..30082a1026 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Area.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Area.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Area Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - AreaUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromSquareMeters(x.SquareMeters + y.SquareMeters)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Area Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Area result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Area); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static AreaUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static AreaUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out AreaUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == AreaUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AreaUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/AreaDensity.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/AreaDensity.WindowsRuntimeComponent.g.cs index 24ecfcfb23..cc0e65e6db 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/AreaDensity.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/AreaDensity.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static AreaDensity Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - AreaDensityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromKilogramsPerSquareMeter(x.KilogramsPerSquareMeter + y.KilogramsPerSquareMeter)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static AreaDensity Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out AreaDensity result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(AreaDensity); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static AreaDensityUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static AreaDensityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out AreaDensityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == AreaDensityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AreaDensityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/AreaMomentOfInertia.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/AreaMomentOfInertia.WindowsRuntimeComponent.g.cs index b39f364461..f67ed8a765 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/AreaMomentOfInertia.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/AreaMomentOfInertia.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static AreaMomentOfInertia Parse(string str, [CanBeNull] string cultureNa // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - AreaMomentOfInertiaUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromMetersToTheFourth(x.MetersToTheFourth + y.MetersToTheFourth)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static AreaMomentOfInertia Parse(string str, [CanBeNull] string cultureNa /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out AreaMomentOfInertia result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(AreaMomentOfInertia); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static AreaMomentOfInertiaUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static AreaMomentOfInertiaUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out AreaMomentOfInertiaUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == AreaMomentOfInertiaUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AreaMomentOfInertiaUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/BitRate.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/BitRate.WindowsRuntimeComponent.g.cs index 03b50ceab3..bdbc535ff5 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/BitRate.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/BitRate.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static BitRate Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - BitRateUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromBitsPerSecond(x.BitsPerSecond + y.BitsPerSecond)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static BitRate Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out BitRate result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(BitRate); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static BitRateUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static BitRateUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out BitRateUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == BitRateUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized BitRateUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/BrakeSpecificFuelConsumption.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/BrakeSpecificFuelConsumption.WindowsRuntimeComponent.g.cs index 006f32ec76..bd7b47a40f 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/BrakeSpecificFuelConsumption.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/BrakeSpecificFuelConsumption.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static BrakeSpecificFuelConsumption Parse(string str, [CanBeNull] string // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - BrakeSpecificFuelConsumptionUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromKilogramsPerJoule(x.KilogramsPerJoule + y.KilogramsPerJoule)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static BrakeSpecificFuelConsumption Parse(string str, [CanBeNull] string /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out BrakeSpecificFuelConsumption result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(BrakeSpecificFuelConsumption); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static BrakeSpecificFuelConsumptionUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static BrakeSpecificFuelConsumptionUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out BrakeSpecificFuelConsumptionUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == BrakeSpecificFuelConsumptionUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized BrakeSpecificFuelConsumptionUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Capacitance.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Capacitance.WindowsRuntimeComponent.g.cs index a10d6a61bd..2d7ed7bce0 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Capacitance.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Capacitance.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Capacitance Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - CapacitanceUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromFarads(x.Farads + y.Farads)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Capacitance Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Capacitance result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Capacitance); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static CapacitanceUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static CapacitanceUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out CapacitanceUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == CapacitanceUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized CapacitanceUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Density.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Density.WindowsRuntimeComponent.g.cs index c4b4771424..f6b2aa5d6a 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Density.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Density.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Density Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - DensityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromKilogramsPerCubicMeter(x.KilogramsPerCubicMeter + y.KilogramsPerCubicMeter)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Density Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Density result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Density); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static DensityUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static DensityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out DensityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == DensityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized DensityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Duration.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Duration.WindowsRuntimeComponent.g.cs index 010db722e3..857451b9cb 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Duration.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Duration.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Duration Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - DurationUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromSeconds(x.Seconds + y.Seconds)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Duration Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Duration result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Duration); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static DurationUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static DurationUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out DurationUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == DurationUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized DurationUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/DynamicViscosity.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/DynamicViscosity.WindowsRuntimeComponent.g.cs index fe01d15737..05a6bc7c93 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/DynamicViscosity.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/DynamicViscosity.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static DynamicViscosity Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - DynamicViscosityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromNewtonSecondsPerMeterSquared(x.NewtonSecondsPerMeterSquared + y.NewtonSecondsPerMeterSquared)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static DynamicViscosity Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out DynamicViscosity result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(DynamicViscosity); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static DynamicViscosityUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static DynamicViscosityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out DynamicViscosityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == DynamicViscosityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized DynamicViscosityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricAdmittance.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricAdmittance.WindowsRuntimeComponent.g.cs index 71783fb1ef..f1d7203013 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricAdmittance.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricAdmittance.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ElectricAdmittance Parse(string str, [CanBeNull] string cultureNam // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricAdmittanceUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromSiemens(x.Siemens + y.Siemens)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ElectricAdmittance Parse(string str, [CanBeNull] string cultureNam /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ElectricAdmittance result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ElectricAdmittance); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ElectricAdmittanceUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ElectricAdmittanceUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ElectricAdmittanceUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricAdmittanceUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricAdmittanceUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricCharge.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricCharge.WindowsRuntimeComponent.g.cs index 67d112ec66..030fea6f4b 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricCharge.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricCharge.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ElectricCharge Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricChargeUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromCoulombs(x.Coulombs + y.Coulombs)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ElectricCharge Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ElectricCharge result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ElectricCharge); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ElectricChargeUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ElectricChargeUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ElectricChargeUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricChargeUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricChargeUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricChargeDensity.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricChargeDensity.WindowsRuntimeComponent.g.cs index 79610f936e..93c916d3e5 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricChargeDensity.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricChargeDensity.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ElectricChargeDensity Parse(string str, [CanBeNull] string culture // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricChargeDensityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromCoulombsPerCubicMeter(x.CoulombsPerCubicMeter + y.CoulombsPerCubicMeter)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ElectricChargeDensity Parse(string str, [CanBeNull] string culture /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ElectricChargeDensity result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ElectricChargeDensity); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ElectricChargeDensityUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ElectricChargeDensityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ElectricChargeDensityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricChargeDensityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricChargeDensityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricConductance.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricConductance.WindowsRuntimeComponent.g.cs index b784e572d7..6dca359e02 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricConductance.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricConductance.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ElectricConductance Parse(string str, [CanBeNull] string cultureNa // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricConductanceUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromSiemens(x.Siemens + y.Siemens)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ElectricConductance Parse(string str, [CanBeNull] string cultureNa /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ElectricConductance result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ElectricConductance); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ElectricConductanceUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ElectricConductanceUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ElectricConductanceUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricConductanceUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricConductanceUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricConductivity.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricConductivity.WindowsRuntimeComponent.g.cs index 9a82c5b900..65c463f8fb 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricConductivity.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricConductivity.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ElectricConductivity Parse(string str, [CanBeNull] string cultureN // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricConductivityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromSiemensPerMeter(x.SiemensPerMeter + y.SiemensPerMeter)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ElectricConductivity Parse(string str, [CanBeNull] string cultureN /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ElectricConductivity result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ElectricConductivity); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ElectricConductivityUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ElectricConductivityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ElectricConductivityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricConductivityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricConductivityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricCurrent.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricCurrent.WindowsRuntimeComponent.g.cs index 1c0a2c1a9a..63e71a0ba4 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricCurrent.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricCurrent.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ElectricCurrent Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricCurrentUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromAmperes(x.Amperes + y.Amperes)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ElectricCurrent Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ElectricCurrent result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ElectricCurrent); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ElectricCurrentUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ElectricCurrentUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ElectricCurrentUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricCurrentUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricCurrentUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricCurrentDensity.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricCurrentDensity.WindowsRuntimeComponent.g.cs index 9755b633e4..a69f16d89b 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricCurrentDensity.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricCurrentDensity.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ElectricCurrentDensity Parse(string str, [CanBeNull] string cultur // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricCurrentDensityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromAmperesPerSquareMeter(x.AmperesPerSquareMeter + y.AmperesPerSquareMeter)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ElectricCurrentDensity Parse(string str, [CanBeNull] string cultur /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ElectricCurrentDensity result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ElectricCurrentDensity); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ElectricCurrentDensityUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ElectricCurrentDensityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ElectricCurrentDensityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricCurrentDensityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricCurrentDensityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricCurrentGradient.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricCurrentGradient.WindowsRuntimeComponent.g.cs index 5a367e848f..a46a284f90 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricCurrentGradient.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricCurrentGradient.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ElectricCurrentGradient Parse(string str, [CanBeNull] string cultu // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricCurrentGradientUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromAmperesPerSecond(x.AmperesPerSecond + y.AmperesPerSecond)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ElectricCurrentGradient Parse(string str, [CanBeNull] string cultu /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ElectricCurrentGradient result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ElectricCurrentGradient); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ElectricCurrentGradientUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ElectricCurrentGradientUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ElectricCurrentGradientUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricCurrentGradientUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricCurrentGradientUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricField.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricField.WindowsRuntimeComponent.g.cs index 7e512ad02e..54baf6e0a4 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricField.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricField.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ElectricField Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricFieldUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromVoltsPerMeter(x.VoltsPerMeter + y.VoltsPerMeter)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ElectricField Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ElectricField result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ElectricField); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ElectricFieldUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ElectricFieldUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ElectricFieldUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricFieldUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricFieldUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricInductance.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricInductance.WindowsRuntimeComponent.g.cs index 303ebcafa6..ac39bf3719 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricInductance.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricInductance.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ElectricInductance Parse(string str, [CanBeNull] string cultureNam // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricInductanceUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromHenries(x.Henries + y.Henries)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ElectricInductance Parse(string str, [CanBeNull] string cultureNam /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ElectricInductance result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ElectricInductance); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ElectricInductanceUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ElectricInductanceUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ElectricInductanceUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricInductanceUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricInductanceUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricPotential.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricPotential.WindowsRuntimeComponent.g.cs index ef1f260682..4134d167c3 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricPotential.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricPotential.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ElectricPotential Parse(string str, [CanBeNull] string cultureName // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricPotentialUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromVolts(x.Volts + y.Volts)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ElectricPotential Parse(string str, [CanBeNull] string cultureName /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ElectricPotential result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ElectricPotential); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ElectricPotentialUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ElectricPotentialUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ElectricPotentialUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricPotentialUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricPotentialUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricPotentialAc.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricPotentialAc.WindowsRuntimeComponent.g.cs index 9cb0fe0375..94133dafae 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricPotentialAc.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricPotentialAc.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ElectricPotentialAc Parse(string str, [CanBeNull] string cultureNa // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricPotentialAcUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromVoltsAc(x.VoltsAc + y.VoltsAc)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ElectricPotentialAc Parse(string str, [CanBeNull] string cultureNa /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ElectricPotentialAc result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ElectricPotentialAc); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ElectricPotentialAcUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ElectricPotentialAcUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ElectricPotentialAcUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricPotentialAcUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricPotentialAcUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricPotentialDc.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricPotentialDc.WindowsRuntimeComponent.g.cs index 03b22635cc..60cde2349f 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricPotentialDc.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricPotentialDc.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ElectricPotentialDc Parse(string str, [CanBeNull] string cultureNa // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricPotentialDcUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromVoltsDc(x.VoltsDc + y.VoltsDc)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ElectricPotentialDc Parse(string str, [CanBeNull] string cultureNa /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ElectricPotentialDc result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ElectricPotentialDc); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ElectricPotentialDcUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ElectricPotentialDcUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ElectricPotentialDcUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricPotentialDcUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricPotentialDcUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricResistance.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricResistance.WindowsRuntimeComponent.g.cs index 527ff133e9..1bf8d39d9f 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricResistance.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricResistance.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ElectricResistance Parse(string str, [CanBeNull] string cultureNam // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricResistanceUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromOhms(x.Ohms + y.Ohms)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ElectricResistance Parse(string str, [CanBeNull] string cultureNam /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ElectricResistance result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ElectricResistance); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ElectricResistanceUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ElectricResistanceUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ElectricResistanceUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricResistanceUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricResistanceUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricResistivity.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricResistivity.WindowsRuntimeComponent.g.cs index f0019b31a8..b782e93278 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricResistivity.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ElectricResistivity.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ElectricResistivity Parse(string str, [CanBeNull] string cultureNa // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricResistivityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromOhmMeters(x.OhmMeters + y.OhmMeters)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ElectricResistivity Parse(string str, [CanBeNull] string cultureNa /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ElectricResistivity result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ElectricResistivity); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ElectricResistivityUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ElectricResistivityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ElectricResistivityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricResistivityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricResistivityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Energy.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Energy.WindowsRuntimeComponent.g.cs index ba97efc1cc..8c0111ca47 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Energy.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Energy.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Energy Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - EnergyUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromJoules(x.Joules + y.Joules)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Energy Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Energy result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Energy); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static EnergyUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static EnergyUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out EnergyUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == EnergyUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized EnergyUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Entropy.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Entropy.WindowsRuntimeComponent.g.cs index 52e53931eb..bfc36363d6 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Entropy.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Entropy.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Entropy Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - EntropyUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromJoulesPerKelvin(x.JoulesPerKelvin + y.JoulesPerKelvin)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Entropy Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Entropy result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Entropy); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static EntropyUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static EntropyUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out EntropyUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == EntropyUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized EntropyUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Force.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Force.WindowsRuntimeComponent.g.cs index cb1c716a3d..d4d5189513 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Force.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Force.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Force Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ForceUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromNewtons(x.Newtons + y.Newtons)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Force Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Force result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Force); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ForceUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ForceUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ForceUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ForceUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ForceUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ForceChangeRate.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ForceChangeRate.WindowsRuntimeComponent.g.cs index efcb1c9178..fff218396e 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ForceChangeRate.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ForceChangeRate.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ForceChangeRate Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ForceChangeRateUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromNewtonsPerSecond(x.NewtonsPerSecond + y.NewtonsPerSecond)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ForceChangeRate Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ForceChangeRate result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ForceChangeRate); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ForceChangeRateUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ForceChangeRateUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ForceChangeRateUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ForceChangeRateUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ForceChangeRateUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ForcePerLength.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ForcePerLength.WindowsRuntimeComponent.g.cs index de92aafc6b..db107fe183 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ForcePerLength.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ForcePerLength.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ForcePerLength Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ForcePerLengthUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromNewtonsPerMeter(x.NewtonsPerMeter + y.NewtonsPerMeter)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ForcePerLength Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ForcePerLength result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ForcePerLength); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ForcePerLengthUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ForcePerLengthUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ForcePerLengthUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ForcePerLengthUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ForcePerLengthUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Frequency.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Frequency.WindowsRuntimeComponent.g.cs index 1d05349c9c..482a7e2568 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Frequency.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Frequency.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Frequency Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - FrequencyUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromHertz(x.Hertz + y.Hertz)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Frequency Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Frequency result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Frequency); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static FrequencyUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static FrequencyUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out FrequencyUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == FrequencyUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized FrequencyUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/HeatFlux.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/HeatFlux.WindowsRuntimeComponent.g.cs index 361072352d..aee2c60ae3 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/HeatFlux.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/HeatFlux.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static HeatFlux Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - HeatFluxUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromWattsPerSquareMeter(x.WattsPerSquareMeter + y.WattsPerSquareMeter)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static HeatFlux Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out HeatFlux result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(HeatFlux); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static HeatFluxUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static HeatFluxUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out HeatFluxUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == HeatFluxUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized HeatFluxUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/HeatTransferCoefficient.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/HeatTransferCoefficient.WindowsRuntimeComponent.g.cs index 4053939c81..038a858590 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/HeatTransferCoefficient.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/HeatTransferCoefficient.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static HeatTransferCoefficient Parse(string str, [CanBeNull] string cultu // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - HeatTransferCoefficientUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromWattsPerSquareMeterKelvin(x.WattsPerSquareMeterKelvin + y.WattsPerSquareMeterKelvin)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static HeatTransferCoefficient Parse(string str, [CanBeNull] string cultu /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out HeatTransferCoefficient result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(HeatTransferCoefficient); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static HeatTransferCoefficientUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static HeatTransferCoefficientUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out HeatTransferCoefficientUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == HeatTransferCoefficientUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized HeatTransferCoefficientUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Illuminance.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Illuminance.WindowsRuntimeComponent.g.cs index 940d567e73..2c0f8e99d8 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Illuminance.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Illuminance.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Illuminance Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - IlluminanceUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromLux(x.Lux + y.Lux)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Illuminance Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Illuminance result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Illuminance); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static IlluminanceUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static IlluminanceUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out IlluminanceUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == IlluminanceUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized IlluminanceUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Information.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Information.WindowsRuntimeComponent.g.cs index b10a5189c7..261900e2bd 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Information.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Information.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Information Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - InformationUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromBits(x.Bits + y.Bits)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Information Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Information result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Information); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static InformationUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static InformationUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out InformationUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - 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; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Irradiance.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Irradiance.WindowsRuntimeComponent.g.cs index 11b0484b66..0f2c1c6dfc 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Irradiance.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Irradiance.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Irradiance Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - IrradianceUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromWattsPerSquareMeter(x.WattsPerSquareMeter + y.WattsPerSquareMeter)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Irradiance Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Irradiance result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Irradiance); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static IrradianceUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static IrradianceUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out IrradianceUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == IrradianceUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized IrradianceUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Irradiation.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Irradiation.WindowsRuntimeComponent.g.cs index 0a6b23eefd..810eb6816f 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Irradiation.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Irradiation.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Irradiation Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - IrradiationUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromJoulesPerSquareMeter(x.JoulesPerSquareMeter + y.JoulesPerSquareMeter)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Irradiation Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Irradiation result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Irradiation); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static IrradiationUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static IrradiationUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out IrradiationUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == IrradiationUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized IrradiationUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/KinematicViscosity.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/KinematicViscosity.WindowsRuntimeComponent.g.cs index e1f7360402..597eb8825c 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/KinematicViscosity.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/KinematicViscosity.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static KinematicViscosity Parse(string str, [CanBeNull] string cultureNam // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - KinematicViscosityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromSquareMetersPerSecond(x.SquareMetersPerSecond + y.SquareMetersPerSecond)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static KinematicViscosity Parse(string str, [CanBeNull] string cultureNam /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out KinematicViscosity result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(KinematicViscosity); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static KinematicViscosityUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static KinematicViscosityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out KinematicViscosityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == KinematicViscosityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized KinematicViscosityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/LapseRate.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/LapseRate.WindowsRuntimeComponent.g.cs index a717e64840..8ae87aa8dc 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/LapseRate.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/LapseRate.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static LapseRate Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - LapseRateUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromDegreesCelciusPerKilometer(x.DegreesCelciusPerKilometer + y.DegreesCelciusPerKilometer)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static LapseRate Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out LapseRate result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(LapseRate); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static LapseRateUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static LapseRateUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out LapseRateUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == LapseRateUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LapseRateUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(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 bef41113d5..9455b150e6 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Length.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Length.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Length Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - LengthUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromMeters(x.Meters + y.Meters)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Length Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Length result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Length); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static LengthUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static LengthUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out LengthUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - 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; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(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 af0eb875e2..626f905c21 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Level.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Level.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Level Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - LevelUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromDecibels(x.Decibels + y.Decibels)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Level Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Level result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Level); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static LevelUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static LevelUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out LevelUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - 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; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/LinearDensity.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/LinearDensity.WindowsRuntimeComponent.g.cs index d7f76489c2..5dbc90f8a0 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/LinearDensity.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/LinearDensity.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static LinearDensity Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - LinearDensityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromKilogramsPerMeter(x.KilogramsPerMeter + y.KilogramsPerMeter)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static LinearDensity Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out LinearDensity result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(LinearDensity); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static LinearDensityUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static LinearDensityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out LinearDensityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == LinearDensityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LinearDensityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/LuminousFlux.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/LuminousFlux.WindowsRuntimeComponent.g.cs index 73c78d323c..c2c033c875 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/LuminousFlux.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/LuminousFlux.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static LuminousFlux Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - LuminousFluxUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromLumens(x.Lumens + y.Lumens)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static LuminousFlux Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out LuminousFlux result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(LuminousFlux); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static LuminousFluxUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static LuminousFluxUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out LuminousFluxUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == LuminousFluxUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LuminousFluxUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/LuminousIntensity.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/LuminousIntensity.WindowsRuntimeComponent.g.cs index c2d8e462da..a60457862e 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/LuminousIntensity.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/LuminousIntensity.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static LuminousIntensity Parse(string str, [CanBeNull] string cultureName // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - LuminousIntensityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromCandela(x.Candela + y.Candela)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static LuminousIntensity Parse(string str, [CanBeNull] string cultureName /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out LuminousIntensity result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(LuminousIntensity); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static LuminousIntensityUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static LuminousIntensityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out LuminousIntensityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == LuminousIntensityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LuminousIntensityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MagneticField.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MagneticField.WindowsRuntimeComponent.g.cs index 17172c2d98..e42d212939 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MagneticField.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MagneticField.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static MagneticField Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MagneticFieldUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromTeslas(x.Teslas + y.Teslas)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static MagneticField Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out MagneticField result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(MagneticField); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static MagneticFieldUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static MagneticFieldUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out MagneticFieldUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MagneticFieldUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MagneticFieldUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MagneticFlux.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MagneticFlux.WindowsRuntimeComponent.g.cs index 3e79d79033..bcdf967e0d 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MagneticFlux.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MagneticFlux.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static MagneticFlux Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MagneticFluxUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromWebers(x.Webers + y.Webers)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static MagneticFlux Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out MagneticFlux result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(MagneticFlux); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static MagneticFluxUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static MagneticFluxUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out MagneticFluxUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MagneticFluxUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MagneticFluxUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Magnetization.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Magnetization.WindowsRuntimeComponent.g.cs index 18ec44f9a6..d652d7e489 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Magnetization.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Magnetization.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Magnetization Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MagnetizationUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromAmperesPerMeter(x.AmperesPerMeter + y.AmperesPerMeter)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Magnetization Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Magnetization result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Magnetization); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static MagnetizationUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static MagnetizationUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out MagnetizationUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MagnetizationUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MagnetizationUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Mass.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Mass.WindowsRuntimeComponent.g.cs index fe7084ecbd..e5c34f63f5 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Mass.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Mass.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Mass Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MassUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromKilograms(x.Kilograms + y.Kilograms)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Mass Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Mass result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Mass); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static MassUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static MassUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out MassUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MassUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MassUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MassFlow.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MassFlow.WindowsRuntimeComponent.g.cs index 8dd07a0876..0e56510a1c 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MassFlow.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MassFlow.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static MassFlow Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MassFlowUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromGramsPerSecond(x.GramsPerSecond + y.GramsPerSecond)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static MassFlow Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out MassFlow result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(MassFlow); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static MassFlowUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static MassFlowUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out MassFlowUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MassFlowUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MassFlowUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MassFlux.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MassFlux.WindowsRuntimeComponent.g.cs index ec7f1dfbd6..458ee89ed8 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MassFlux.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MassFlux.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static MassFlux Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MassFluxUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromKilogramsPerSecondPerSquareMeter(x.KilogramsPerSecondPerSquareMeter + y.KilogramsPerSecondPerSquareMeter)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static MassFlux Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out MassFlux result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(MassFlux); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static MassFluxUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static MassFluxUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out MassFluxUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MassFluxUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MassFluxUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MassMomentOfInertia.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MassMomentOfInertia.WindowsRuntimeComponent.g.cs index aa25e45134..e432419055 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MassMomentOfInertia.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MassMomentOfInertia.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static MassMomentOfInertia Parse(string str, [CanBeNull] string cultureNa // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MassMomentOfInertiaUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromKilogramSquareMeters(x.KilogramSquareMeters + y.KilogramSquareMeters)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static MassMomentOfInertia Parse(string str, [CanBeNull] string cultureNa /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out MassMomentOfInertia result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(MassMomentOfInertia); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static MassMomentOfInertiaUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static MassMomentOfInertiaUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out MassMomentOfInertiaUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MassMomentOfInertiaUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MassMomentOfInertiaUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MolarEnergy.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MolarEnergy.WindowsRuntimeComponent.g.cs index 6afd487121..f2fee24aa1 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MolarEnergy.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MolarEnergy.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static MolarEnergy Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MolarEnergyUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromJoulesPerMole(x.JoulesPerMole + y.JoulesPerMole)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static MolarEnergy Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out MolarEnergy result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(MolarEnergy); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static MolarEnergyUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static MolarEnergyUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out MolarEnergyUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MolarEnergyUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MolarEnergyUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MolarEntropy.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MolarEntropy.WindowsRuntimeComponent.g.cs index ba306d4db6..da90f6e830 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MolarEntropy.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MolarEntropy.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static MolarEntropy Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MolarEntropyUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromJoulesPerMoleKelvin(x.JoulesPerMoleKelvin + y.JoulesPerMoleKelvin)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static MolarEntropy Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out MolarEntropy result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(MolarEntropy); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static MolarEntropyUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static MolarEntropyUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out MolarEntropyUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MolarEntropyUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MolarEntropyUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MolarMass.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MolarMass.WindowsRuntimeComponent.g.cs index a80752f3cb..f47ad6ba3f 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MolarMass.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/MolarMass.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static MolarMass Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MolarMassUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromKilogramsPerMole(x.KilogramsPerMole + y.KilogramsPerMole)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static MolarMass Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out MolarMass result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(MolarMass); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static MolarMassUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static MolarMassUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out MolarMassUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MolarMassUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MolarMassUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Molarity.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Molarity.WindowsRuntimeComponent.g.cs index 9728874cf2..19dcfc251d 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Molarity.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Molarity.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Molarity Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MolarityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromMolesPerCubicMeter(x.MolesPerCubicMeter + y.MolesPerCubicMeter)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Molarity Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Molarity result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Molarity); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static MolarityUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static MolarityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out MolarityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MolarityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MolarityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Permeability.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Permeability.WindowsRuntimeComponent.g.cs index e30d8049e0..49ba0d62f9 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Permeability.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Permeability.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Permeability Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - PermeabilityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromHenriesPerMeter(x.HenriesPerMeter + y.HenriesPerMeter)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Permeability Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Permeability result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Permeability); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static PermeabilityUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static PermeabilityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out PermeabilityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == PermeabilityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PermeabilityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Permittivity.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Permittivity.WindowsRuntimeComponent.g.cs index 81dfdbff6b..dfc3ae41a7 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Permittivity.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Permittivity.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Permittivity Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - PermittivityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromFaradsPerMeter(x.FaradsPerMeter + y.FaradsPerMeter)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Permittivity Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Permittivity result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Permittivity); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static PermittivityUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static PermittivityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out PermittivityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == PermittivityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PermittivityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Power.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Power.WindowsRuntimeComponent.g.cs index f7c601374f..b6e9b06086 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Power.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Power.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Power Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - PowerUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromWatts(x.Watts + y.Watts)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Power Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Power result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Power); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static PowerUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static PowerUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out PowerUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == PowerUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PowerUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/PowerDensity.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/PowerDensity.WindowsRuntimeComponent.g.cs index d972403d48..0578c19ed1 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/PowerDensity.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/PowerDensity.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static PowerDensity Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - PowerDensityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromWattsPerCubicMeter(x.WattsPerCubicMeter + y.WattsPerCubicMeter)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static PowerDensity Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out PowerDensity result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(PowerDensity); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static PowerDensityUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static PowerDensityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out PowerDensityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == PowerDensityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PowerDensityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/PowerRatio.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/PowerRatio.WindowsRuntimeComponent.g.cs index eef9ea4d32..e25d40f08a 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/PowerRatio.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/PowerRatio.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static PowerRatio Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - PowerRatioUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromDecibelWatts(x.DecibelWatts + y.DecibelWatts)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static PowerRatio Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out PowerRatio result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(PowerRatio); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static PowerRatioUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static PowerRatioUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out PowerRatioUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == PowerRatioUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PowerRatioUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Pressure.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Pressure.WindowsRuntimeComponent.g.cs index 7a856a0d3a..4e7090a06d 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Pressure.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Pressure.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Pressure Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - PressureUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromPascals(x.Pascals + y.Pascals)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Pressure Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Pressure result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Pressure); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static PressureUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static PressureUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out PressureUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == PressureUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PressureUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/PressureChangeRate.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/PressureChangeRate.WindowsRuntimeComponent.g.cs index 066d81fea6..37dc694287 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/PressureChangeRate.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/PressureChangeRate.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static PressureChangeRate Parse(string str, [CanBeNull] string cultureNam // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - PressureChangeRateUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromPascalsPerSecond(x.PascalsPerSecond + y.PascalsPerSecond)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static PressureChangeRate Parse(string str, [CanBeNull] string cultureNam /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out PressureChangeRate result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(PressureChangeRate); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static PressureChangeRateUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static PressureChangeRateUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out PressureChangeRateUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == PressureChangeRateUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PressureChangeRateUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Ratio.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Ratio.WindowsRuntimeComponent.g.cs index 79f3e76917..fd9f15c9fc 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Ratio.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Ratio.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Ratio Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - RatioUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromDecimalFractions(x.DecimalFractions + y.DecimalFractions)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Ratio Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Ratio result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Ratio); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static RatioUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static RatioUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out RatioUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == RatioUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized RatioUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ReactiveEnergy.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ReactiveEnergy.WindowsRuntimeComponent.g.cs index 5fae7fcaa2..fde190ed38 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ReactiveEnergy.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ReactiveEnergy.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ReactiveEnergy Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ReactiveEnergyUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromVoltampereReactiveHours(x.VoltampereReactiveHours + y.VoltampereReactiveHours)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ReactiveEnergy Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ReactiveEnergy result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ReactiveEnergy); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ReactiveEnergyUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ReactiveEnergyUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ReactiveEnergyUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ReactiveEnergyUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ReactiveEnergyUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ReactivePower.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ReactivePower.WindowsRuntimeComponent.g.cs index f8b8de44b0..20625eb5ba 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ReactivePower.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ReactivePower.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ReactivePower Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ReactivePowerUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromVoltamperesReactive(x.VoltamperesReactive + y.VoltamperesReactive)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ReactivePower Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ReactivePower result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ReactivePower); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ReactivePowerUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ReactivePowerUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ReactivePowerUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ReactivePowerUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ReactivePowerUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/RotationalAcceleration.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/RotationalAcceleration.WindowsRuntimeComponent.g.cs index 2199b771cd..ea7eaf618b 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/RotationalAcceleration.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/RotationalAcceleration.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static RotationalAcceleration Parse(string str, [CanBeNull] string cultur // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - RotationalAccelerationUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromRadiansPerSecondSquared(x.RadiansPerSecondSquared + y.RadiansPerSecondSquared)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static RotationalAcceleration Parse(string str, [CanBeNull] string cultur /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out RotationalAcceleration result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(RotationalAcceleration); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static RotationalAccelerationUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static RotationalAccelerationUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out RotationalAccelerationUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == RotationalAccelerationUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized RotationalAccelerationUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/RotationalSpeed.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/RotationalSpeed.WindowsRuntimeComponent.g.cs index 05e4b877e9..0f3d419b1e 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/RotationalSpeed.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/RotationalSpeed.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static RotationalSpeed Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - RotationalSpeedUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromRadiansPerSecond(x.RadiansPerSecond + y.RadiansPerSecond)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static RotationalSpeed Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out RotationalSpeed result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(RotationalSpeed); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static RotationalSpeedUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static RotationalSpeedUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out RotationalSpeedUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == RotationalSpeedUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized RotationalSpeedUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/RotationalStiffness.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/RotationalStiffness.WindowsRuntimeComponent.g.cs index dceedfc1f5..f44d536789 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/RotationalStiffness.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/RotationalStiffness.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static RotationalStiffness Parse(string str, [CanBeNull] string cultureNa // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - RotationalStiffnessUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromNewtonMetersPerRadian(x.NewtonMetersPerRadian + y.NewtonMetersPerRadian)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static RotationalStiffness Parse(string str, [CanBeNull] string cultureNa /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out RotationalStiffness result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(RotationalStiffness); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static RotationalStiffnessUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static RotationalStiffnessUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out RotationalStiffnessUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == RotationalStiffnessUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized RotationalStiffnessUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/RotationalStiffnessPerLength.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/RotationalStiffnessPerLength.WindowsRuntimeComponent.g.cs index a79b01f8ff..bbf2841753 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/RotationalStiffnessPerLength.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/RotationalStiffnessPerLength.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static RotationalStiffnessPerLength Parse(string str, [CanBeNull] string // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - RotationalStiffnessPerLengthUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromNewtonMetersPerRadianPerMeter(x.NewtonMetersPerRadianPerMeter + y.NewtonMetersPerRadianPerMeter)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static RotationalStiffnessPerLength Parse(string str, [CanBeNull] string /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out RotationalStiffnessPerLength result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(RotationalStiffnessPerLength); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static RotationalStiffnessPerLengthUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static RotationalStiffnessPerLengthUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out RotationalStiffnessPerLengthUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == RotationalStiffnessPerLengthUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized RotationalStiffnessPerLengthUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/SolidAngle.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/SolidAngle.WindowsRuntimeComponent.g.cs index 99fd7e3df6..c7e3cadd98 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/SolidAngle.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/SolidAngle.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static SolidAngle Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - SolidAngleUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromSteradians(x.Steradians + y.Steradians)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static SolidAngle Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out SolidAngle result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(SolidAngle); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static SolidAngleUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static SolidAngleUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out SolidAngleUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == SolidAngleUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized SolidAngleUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/SpecificEnergy.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/SpecificEnergy.WindowsRuntimeComponent.g.cs index e67b373b20..14effbb767 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/SpecificEnergy.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/SpecificEnergy.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static SpecificEnergy Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - SpecificEnergyUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromJoulesPerKilogram(x.JoulesPerKilogram + y.JoulesPerKilogram)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static SpecificEnergy Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out SpecificEnergy result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(SpecificEnergy); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static SpecificEnergyUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static SpecificEnergyUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out SpecificEnergyUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == SpecificEnergyUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized SpecificEnergyUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/SpecificEntropy.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/SpecificEntropy.WindowsRuntimeComponent.g.cs index 59b9a8f2d9..1dbcdc540b 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/SpecificEntropy.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/SpecificEntropy.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static SpecificEntropy Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - SpecificEntropyUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromJoulesPerKilogramKelvin(x.JoulesPerKilogramKelvin + y.JoulesPerKilogramKelvin)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static SpecificEntropy Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out SpecificEntropy result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(SpecificEntropy); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static SpecificEntropyUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static SpecificEntropyUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out SpecificEntropyUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == SpecificEntropyUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized SpecificEntropyUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/SpecificVolume.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/SpecificVolume.WindowsRuntimeComponent.g.cs index 284823b572..46a406ae2d 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/SpecificVolume.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/SpecificVolume.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static SpecificVolume Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - SpecificVolumeUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromCubicMetersPerKilogram(x.CubicMetersPerKilogram + y.CubicMetersPerKilogram)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static SpecificVolume Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out SpecificVolume result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(SpecificVolume); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static SpecificVolumeUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static SpecificVolumeUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out SpecificVolumeUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == SpecificVolumeUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized SpecificVolumeUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/SpecificWeight.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/SpecificWeight.WindowsRuntimeComponent.g.cs index 98511176b3..512637bd7c 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/SpecificWeight.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/SpecificWeight.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static SpecificWeight Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - SpecificWeightUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromNewtonsPerCubicMeter(x.NewtonsPerCubicMeter + y.NewtonsPerCubicMeter)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static SpecificWeight Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out SpecificWeight result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(SpecificWeight); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static SpecificWeightUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static SpecificWeightUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out SpecificWeightUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == SpecificWeightUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized SpecificWeightUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Speed.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Speed.WindowsRuntimeComponent.g.cs index c09a619cde..ed8f5f1427 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Speed.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Speed.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Speed Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - SpeedUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromMetersPerSecond(x.MetersPerSecond + y.MetersPerSecond)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Speed Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Speed result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Speed); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static SpeedUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static SpeedUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out SpeedUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == SpeedUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized SpeedUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Temperature.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Temperature.WindowsRuntimeComponent.g.cs index bb30b7ccaf..27e76f6820 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Temperature.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Temperature.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Temperature Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - TemperatureUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromKelvins(x.Kelvins + y.Kelvins)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Temperature Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Temperature result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Temperature); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static TemperatureUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static TemperatureUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out TemperatureUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == TemperatureUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized TemperatureUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/TemperatureChangeRate.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/TemperatureChangeRate.WindowsRuntimeComponent.g.cs index 34ae85b29a..e90335b363 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/TemperatureChangeRate.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/TemperatureChangeRate.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static TemperatureChangeRate Parse(string str, [CanBeNull] string culture // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - TemperatureChangeRateUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromDegreesCelsiusPerSecond(x.DegreesCelsiusPerSecond + y.DegreesCelsiusPerSecond)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static TemperatureChangeRate Parse(string str, [CanBeNull] string culture /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out TemperatureChangeRate result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(TemperatureChangeRate); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static TemperatureChangeRateUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static TemperatureChangeRateUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out TemperatureChangeRateUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == TemperatureChangeRateUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized TemperatureChangeRateUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/TemperatureDelta.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/TemperatureDelta.WindowsRuntimeComponent.g.cs index 5becd4fccf..58eb389dd4 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/TemperatureDelta.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/TemperatureDelta.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static TemperatureDelta Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - TemperatureDeltaUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromKelvins(x.Kelvins + y.Kelvins)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static TemperatureDelta Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out TemperatureDelta result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(TemperatureDelta); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static TemperatureDeltaUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static TemperatureDeltaUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out TemperatureDeltaUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == TemperatureDeltaUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized TemperatureDeltaUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ThermalConductivity.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ThermalConductivity.WindowsRuntimeComponent.g.cs index eff5b5b651..1744861824 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ThermalConductivity.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ThermalConductivity.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ThermalConductivity Parse(string str, [CanBeNull] string cultureNa // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ThermalConductivityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromWattsPerMeterKelvin(x.WattsPerMeterKelvin + y.WattsPerMeterKelvin)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ThermalConductivity Parse(string str, [CanBeNull] string cultureNa /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ThermalConductivity result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ThermalConductivity); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ThermalConductivityUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ThermalConductivityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ThermalConductivityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ThermalConductivityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ThermalConductivityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ThermalResistance.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ThermalResistance.WindowsRuntimeComponent.g.cs index 2eb2fcfb5e..c09bb66716 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ThermalResistance.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/ThermalResistance.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static ThermalResistance Parse(string str, [CanBeNull] string cultureName // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ThermalResistanceUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromSquareMeterKelvinsPerKilowatt(x.SquareMeterKelvinsPerKilowatt + y.SquareMeterKelvinsPerKilowatt)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static ThermalResistance Parse(string str, [CanBeNull] string cultureName /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out ThermalResistance result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(ThermalResistance); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static ThermalResistanceUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static ThermalResistanceUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out ThermalResistanceUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ThermalResistanceUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ThermalResistanceUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Torque.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Torque.WindowsRuntimeComponent.g.cs index 42209d4c38..8165ec641f 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Torque.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Torque.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Torque Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - TorqueUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromNewtonMeters(x.NewtonMeters + y.NewtonMeters)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Torque Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Torque result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Torque); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static TorqueUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static TorqueUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out TorqueUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == TorqueUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized TorqueUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/VitaminA.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/VitaminA.WindowsRuntimeComponent.g.cs index ac12630410..926d723a6e 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/VitaminA.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/VitaminA.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static VitaminA Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - VitaminAUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromInternationalUnits(x.InternationalUnits + y.InternationalUnits)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static VitaminA Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out VitaminA result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(VitaminA); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static VitaminAUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static VitaminAUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out VitaminAUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == VitaminAUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized VitaminAUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Volume.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Volume.WindowsRuntimeComponent.g.cs index 8353a0926b..4770b1fd4a 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Volume.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Volume.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static Volume Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - VolumeUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromCubicMeters(x.CubicMeters + y.CubicMeters)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static Volume Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Volume result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(Volume); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static VolumeUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static VolumeUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out VolumeUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == VolumeUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized VolumeUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/VolumeFlow.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/VolumeFlow.WindowsRuntimeComponent.g.cs index 8bba24413d..b9273f6170 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/VolumeFlow.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/VolumeFlow.WindowsRuntimeComponent.g.cs @@ -121,13 +121,7 @@ public static VolumeFlow Parse(string str, [CanBeNull] string cultureName) // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - VolumeFlowUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromCubicMetersPerSecond(x.CubicMetersPerSecond + y.CubicMetersPerSecond)); + return ParseInternal(str, provider); } /// @@ -141,16 +135,10 @@ public static VolumeFlow Parse(string str, [CanBeNull] string cultureName) /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out VolumeFlow result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default(VolumeFlow); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -165,35 +153,28 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa /// Error parsing string. public static VolumeFlowUnit ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static VolumeFlowUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out VolumeFlowUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == VolumeFlowUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized VolumeFlowUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/CustomCode/QuantityParser.cs b/UnitsNet/CustomCode/QuantityParser.cs index a8d5ce9ec8..149d117349 100644 --- a/UnitsNet/CustomCode/QuantityParser.cs +++ b/UnitsNet/CustomCode/QuantityParser.cs @@ -31,6 +31,7 @@ namespace UnitsNet { internal delegate TQuantity ParseUnit(string value, string unit, IFormatProvider formatProvider = null); + internal delegate bool TryParseUnit(string valueString, string unit, IFormatProvider formatProvider, out TQuantity value); internal static class QuantityParser { @@ -44,14 +45,14 @@ internal static TQuantity Parse([NotNull] string str, if (parseUnit == null) throw new ArgumentNullException(nameof(parseUnit)); if (add == null) throw new ArgumentNullException(nameof(add)); - NumberFormatInfo numFormat = formatProvider != null + var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof(NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; if (numFormat == null) throw new InvalidOperationException($"No number format was found for the given format provider: {formatProvider}"); - string numRegex = string.Format(@"[\d., {0}{1}]*\d", + var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator @@ -66,7 +67,7 @@ internal static TQuantity Parse([NotNull] string str, string unitsRegex = $"({String.Join("|", unitAbbreviations)})"; - string regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", numRegex, // capture base (integral) Quantity value exponentialRegex, // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") @@ -74,7 +75,7 @@ internal static TQuantity Parse([NotNull] string str, @"(and)?,?", // allow "and" & "," separators between quantities @"(?[a-z]*)?"); // capture invalid input - List quantities = ParseWithRegex(regexString, str, parseUnit, formatProvider); + var quantities = ParseWithRegex(regexString, str, parseUnit, formatProvider); if (quantities.Count == 0) { throw new ArgumentException( @@ -132,5 +133,87 @@ private static List ParseWithRegex(string regexString, str } return converted; } + + [SuppressMessage("ReSharper", "UseStringInterpolation")] + internal static bool TryParse([NotNull] string str, + [CanBeNull] IFormatProvider formatProvider, + [NotNull] TryParseUnit parseUnit, + [NotNull] Func add, out TQuantity value) + { + value = default(TQuantity); + + if(string.IsNullOrWhiteSpace(str)) return false; + if(parseUnit == null) return false; + if(add == null) return false; + + var numFormat = formatProvider != null + ? (NumberFormatInfo) formatProvider.GetFormat(typeof(NumberFormatInfo)) + : NumberFormatInfo.CurrentInfo; + + if(numFormat == null) + return false; + + var numRegex = string.Format(@"[\d., {0}{1}]*\d", + // allows digits, dots, commas, and spaces in the quantity (must end in digit) + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + + const string exponentialRegex = @"(?:[eE][-+]?\d+)?)"; + + string[] unitAbbreviations = UnitSystem.GetCached(formatProvider) + .GetAllAbbreviations(typeof(TUnitType)) + .OrderByDescending(s => s.Length) // Important to order by length -- if "m" is before "mm" and the input is "mm", it will match just "m" and throw invalid string error + .Select(Regex.Escape) // Escape special regex characters + .ToArray(); + + string unitsRegex = $"({String.Join("|", unitAbbreviations)})"; + + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + $@"(?{unitsRegex})", // capture Unit by list of abbreviations + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + if(!TryParseWithRegex(regexString, str, parseUnit, formatProvider, out var quantities)) + return false; + + value = quantities.Aggregate(add); + return true; + } + + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static bool TryParseWithRegex(string regexString, string str, TryParseUnit tryParseUnit, + IFormatProvider formatProvider, out List converted ) + { + converted = new List(); + + var regex = new Regex(regexString); + var matches = regex.Matches(str.Trim()); + + foreach(Match match in matches) + { + var groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + if(groups["invalid"].Value != string.Empty) + return false; + + if((valueString == string.Empty) && (unitString == string.Empty)) + continue; + + if(!tryParseUnit(valueString, unitString, formatProvider, out var parsed)) + return false; + + converted.Add(parsed); + } + + return true; + } } } diff --git a/UnitsNet/GeneratedCode/Quantities/Acceleration.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Acceleration.NetFramework.g.cs index 1b07fe26ee..2325dadcee 100644 --- a/UnitsNet/GeneratedCode/Quantities/Acceleration.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Acceleration.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(AccelerationUnit unit, [CanBeNull] IFormatP /// public static Acceleration Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - AccelerationUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromMetersPerSecondSquared(x.MetersPerSecondSquared + y.MetersPerSecondSquared)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Acceleration Parse(string str, [CanBeNull] IFormatProvider provide /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Acceleration result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Acceleration); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static AccelerationUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static AccelerationUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static AccelerationUnit ParseUnit(string str, [CanBeNull] string cultureN /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static AccelerationUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out AccelerationUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == AccelerationUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AccelerationUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/AmountOfSubstance.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/AmountOfSubstance.NetFramework.g.cs index a644c32be9..3dde09393d 100644 --- a/UnitsNet/GeneratedCode/Quantities/AmountOfSubstance.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/AmountOfSubstance.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(AmountOfSubstanceUnit unit, [CanBeNull] IFo /// public static AmountOfSubstance Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - AmountOfSubstanceUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromMoles(x.Moles + y.Moles)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static AmountOfSubstance Parse(string str, [CanBeNull] IFormatProvider pr /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out AmountOfSubstance result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(AmountOfSubstance); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static AmountOfSubstanceUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static AmountOfSubstanceUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static AmountOfSubstanceUnit ParseUnit(string str, [CanBeNull] string cul /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static AmountOfSubstanceUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out AmountOfSubstanceUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == AmountOfSubstanceUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AmountOfSubstanceUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/AmplitudeRatio.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/AmplitudeRatio.NetFramework.g.cs index c70a98038d..aca0015b2e 100644 --- a/UnitsNet/GeneratedCode/Quantities/AmplitudeRatio.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/AmplitudeRatio.NetFramework.g.cs @@ -167,17 +167,7 @@ public static string GetAbbreviation(AmplitudeRatioUnit unit, [CanBeNull] IForma /// public static AmplitudeRatio Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - AmplitudeRatioUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromDecibelVolts(x.DecibelVolts + y.DecibelVolts)); + return ParseInternal(str, provider); } /// @@ -186,39 +176,28 @@ public static AmplitudeRatio Parse(string str, [CanBeNull] IFormatProvider provi /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out AmplitudeRatio result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(AmplitudeRatio); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static AmplitudeRatioUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static AmplitudeRatioUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -226,27 +205,14 @@ public static AmplitudeRatioUnit ParseUnit(string str, [CanBeNull] string cultur /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static AmplitudeRatioUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out AmplitudeRatioUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == AmplitudeRatioUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AmplitudeRatioUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Angle.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Angle.NetFramework.g.cs index 9fd7307afb..74594e0d60 100644 --- a/UnitsNet/GeneratedCode/Quantities/Angle.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Angle.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(AngleUnit unit, [CanBeNull] IFormatProvider /// public static Angle Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - AngleUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromDegrees(x.Degrees + y.Degrees)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Angle Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Angle result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Angle); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static AngleUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static AngleUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static AngleUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static AngleUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out AngleUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == AngleUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AngleUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ApparentEnergy.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ApparentEnergy.NetFramework.g.cs index 051f4c69cb..6befa51f75 100644 --- a/UnitsNet/GeneratedCode/Quantities/ApparentEnergy.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ApparentEnergy.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ApparentEnergyUnit unit, [CanBeNull] IForma /// public static ApparentEnergy Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ApparentEnergyUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromVoltampereHours(x.VoltampereHours + y.VoltampereHours)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ApparentEnergy Parse(string str, [CanBeNull] IFormatProvider provi /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ApparentEnergy result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ApparentEnergy); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ApparentEnergyUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ApparentEnergyUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ApparentEnergyUnit ParseUnit(string str, [CanBeNull] string cultur /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ApparentEnergyUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ApparentEnergyUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ApparentEnergyUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ApparentEnergyUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ApparentPower.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ApparentPower.NetFramework.g.cs index ac9a694fc2..79d9c80500 100644 --- a/UnitsNet/GeneratedCode/Quantities/ApparentPower.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ApparentPower.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ApparentPowerUnit unit, [CanBeNull] IFormat /// public static ApparentPower Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ApparentPowerUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromVoltamperes(x.Voltamperes + y.Voltamperes)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ApparentPower Parse(string str, [CanBeNull] IFormatProvider provid /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ApparentPower result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ApparentPower); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ApparentPowerUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ApparentPowerUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ApparentPowerUnit ParseUnit(string str, [CanBeNull] string culture /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ApparentPowerUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ApparentPowerUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ApparentPowerUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ApparentPowerUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Area.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Area.NetFramework.g.cs index 60a1dc3826..72a65906b4 100644 --- a/UnitsNet/GeneratedCode/Quantities/Area.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Area.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(AreaUnit unit, [CanBeNull] IFormatProvider /// public static Area Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - AreaUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromSquareMeters(x.SquareMeters + y.SquareMeters)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Area Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Area result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Area); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static AreaUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static AreaUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static AreaUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static AreaUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out AreaUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == AreaUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AreaUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/AreaDensity.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/AreaDensity.NetFramework.g.cs index bd75e73e5b..5dac998354 100644 --- a/UnitsNet/GeneratedCode/Quantities/AreaDensity.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/AreaDensity.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(AreaDensityUnit unit, [CanBeNull] IFormatPr /// public static AreaDensity Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - AreaDensityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromKilogramsPerSquareMeter(x.KilogramsPerSquareMeter + y.KilogramsPerSquareMeter)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static AreaDensity Parse(string str, [CanBeNull] IFormatProvider provider /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out AreaDensity result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(AreaDensity); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static AreaDensityUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static AreaDensityUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static AreaDensityUnit ParseUnit(string str, [CanBeNull] string cultureNa /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static AreaDensityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out AreaDensityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == AreaDensityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AreaDensityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/AreaMomentOfInertia.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/AreaMomentOfInertia.NetFramework.g.cs index 8148968622..48f99e9ebb 100644 --- a/UnitsNet/GeneratedCode/Quantities/AreaMomentOfInertia.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/AreaMomentOfInertia.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(AreaMomentOfInertiaUnit unit, [CanBeNull] I /// public static AreaMomentOfInertia Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - AreaMomentOfInertiaUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromMetersToTheFourth(x.MetersToTheFourth + y.MetersToTheFourth)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static AreaMomentOfInertia Parse(string str, [CanBeNull] IFormatProvider /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out AreaMomentOfInertia result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(AreaMomentOfInertia); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static AreaMomentOfInertiaUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static AreaMomentOfInertiaUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static AreaMomentOfInertiaUnit ParseUnit(string str, [CanBeNull] string c /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static AreaMomentOfInertiaUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out AreaMomentOfInertiaUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == AreaMomentOfInertiaUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AreaMomentOfInertiaUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/BitRate.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/BitRate.NetFramework.g.cs index a6a92ac3ca..8dd63b7d65 100644 --- a/UnitsNet/GeneratedCode/Quantities/BitRate.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/BitRate.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(BitRateUnit unit, [CanBeNull] IFormatProvid /// public static BitRate Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - BitRateUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromBitsPerSecond(x.BitsPerSecond + y.BitsPerSecond)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static BitRate Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out BitRate result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(BitRate); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static BitRateUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static BitRateUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static BitRateUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static BitRateUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out BitRateUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == BitRateUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized BitRateUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/BrakeSpecificFuelConsumption.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/BrakeSpecificFuelConsumption.NetFramework.g.cs index 5e7156515a..66b1ce63b3 100644 --- a/UnitsNet/GeneratedCode/Quantities/BrakeSpecificFuelConsumption.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/BrakeSpecificFuelConsumption.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(BrakeSpecificFuelConsumptionUnit unit, [Can /// public static BrakeSpecificFuelConsumption Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - BrakeSpecificFuelConsumptionUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromKilogramsPerJoule(x.KilogramsPerJoule + y.KilogramsPerJoule)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static BrakeSpecificFuelConsumption Parse(string str, [CanBeNull] IFormat /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out BrakeSpecificFuelConsumption result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(BrakeSpecificFuelConsumption); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static BrakeSpecificFuelConsumptionUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static BrakeSpecificFuelConsumptionUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static BrakeSpecificFuelConsumptionUnit ParseUnit(string str, [CanBeNull] /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static BrakeSpecificFuelConsumptionUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out BrakeSpecificFuelConsumptionUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == BrakeSpecificFuelConsumptionUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized BrakeSpecificFuelConsumptionUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Capacitance.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Capacitance.NetFramework.g.cs index af6e9c637b..2e85ccaab1 100644 --- a/UnitsNet/GeneratedCode/Quantities/Capacitance.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Capacitance.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(CapacitanceUnit unit, [CanBeNull] IFormatPr /// public static Capacitance Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - CapacitanceUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromFarads(x.Farads + y.Farads)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Capacitance Parse(string str, [CanBeNull] IFormatProvider provider /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Capacitance result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Capacitance); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static CapacitanceUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static CapacitanceUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static CapacitanceUnit ParseUnit(string str, [CanBeNull] string cultureNa /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static CapacitanceUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out CapacitanceUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == CapacitanceUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized CapacitanceUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Density.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Density.NetFramework.g.cs index e491b219c5..35b3ed857a 100644 --- a/UnitsNet/GeneratedCode/Quantities/Density.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Density.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(DensityUnit unit, [CanBeNull] IFormatProvid /// public static Density Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - DensityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromKilogramsPerCubicMeter(x.KilogramsPerCubicMeter + y.KilogramsPerCubicMeter)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Density Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Density result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Density); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static DensityUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static DensityUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static DensityUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static DensityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out DensityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == DensityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized DensityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Duration.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Duration.NetFramework.g.cs index 1d0d18b70a..d65f2ac0e0 100644 --- a/UnitsNet/GeneratedCode/Quantities/Duration.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Duration.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(DurationUnit unit, [CanBeNull] IFormatProvi /// public static Duration Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - DurationUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromSeconds(x.Seconds + y.Seconds)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Duration Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Duration result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Duration); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static DurationUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static DurationUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static DurationUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static DurationUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out DurationUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == DurationUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized DurationUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/DynamicViscosity.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/DynamicViscosity.NetFramework.g.cs index eb45c1c136..76413b4919 100644 --- a/UnitsNet/GeneratedCode/Quantities/DynamicViscosity.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/DynamicViscosity.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(DynamicViscosityUnit unit, [CanBeNull] IFor /// public static DynamicViscosity Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - DynamicViscosityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromNewtonSecondsPerMeterSquared(x.NewtonSecondsPerMeterSquared + y.NewtonSecondsPerMeterSquared)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static DynamicViscosity Parse(string str, [CanBeNull] IFormatProvider pro /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out DynamicViscosity result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(DynamicViscosity); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static DynamicViscosityUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static DynamicViscosityUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static DynamicViscosityUnit ParseUnit(string str, [CanBeNull] string cult /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static DynamicViscosityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out DynamicViscosityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == DynamicViscosityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized DynamicViscosityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ElectricAdmittance.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ElectricAdmittance.NetFramework.g.cs index 263c6de064..95cd9cf751 100644 --- a/UnitsNet/GeneratedCode/Quantities/ElectricAdmittance.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ElectricAdmittance.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ElectricAdmittanceUnit unit, [CanBeNull] IF /// public static ElectricAdmittance Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricAdmittanceUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromSiemens(x.Siemens + y.Siemens)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ElectricAdmittance Parse(string str, [CanBeNull] IFormatProvider p /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricAdmittance result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ElectricAdmittance); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ElectricAdmittanceUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ElectricAdmittanceUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ElectricAdmittanceUnit ParseUnit(string str, [CanBeNull] string cu /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ElectricAdmittanceUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ElectricAdmittanceUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricAdmittanceUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricAdmittanceUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ElectricCharge.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ElectricCharge.NetFramework.g.cs index b73452a549..8444e93d75 100644 --- a/UnitsNet/GeneratedCode/Quantities/ElectricCharge.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ElectricCharge.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ElectricChargeUnit unit, [CanBeNull] IForma /// public static ElectricCharge Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricChargeUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromCoulombs(x.Coulombs + y.Coulombs)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ElectricCharge Parse(string str, [CanBeNull] IFormatProvider provi /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricCharge result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ElectricCharge); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ElectricChargeUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ElectricChargeUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ElectricChargeUnit ParseUnit(string str, [CanBeNull] string cultur /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ElectricChargeUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ElectricChargeUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricChargeUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricChargeUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ElectricChargeDensity.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ElectricChargeDensity.NetFramework.g.cs index 7d546ced0d..c33625785d 100644 --- a/UnitsNet/GeneratedCode/Quantities/ElectricChargeDensity.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ElectricChargeDensity.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ElectricChargeDensityUnit unit, [CanBeNull] /// public static ElectricChargeDensity Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricChargeDensityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromCoulombsPerCubicMeter(x.CoulombsPerCubicMeter + y.CoulombsPerCubicMeter)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ElectricChargeDensity Parse(string str, [CanBeNull] IFormatProvide /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricChargeDensity result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ElectricChargeDensity); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ElectricChargeDensityUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ElectricChargeDensityUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ElectricChargeDensityUnit ParseUnit(string str, [CanBeNull] 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ElectricChargeDensityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ElectricChargeDensityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricChargeDensityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricChargeDensityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ElectricConductance.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ElectricConductance.NetFramework.g.cs index 5991df998d..3fe4b43514 100644 --- a/UnitsNet/GeneratedCode/Quantities/ElectricConductance.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ElectricConductance.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ElectricConductanceUnit unit, [CanBeNull] I /// public static ElectricConductance Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricConductanceUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromSiemens(x.Siemens + y.Siemens)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ElectricConductance Parse(string str, [CanBeNull] IFormatProvider /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricConductance result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ElectricConductance); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ElectricConductanceUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ElectricConductanceUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ElectricConductanceUnit ParseUnit(string str, [CanBeNull] string c /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ElectricConductanceUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ElectricConductanceUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricConductanceUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricConductanceUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ElectricConductivity.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ElectricConductivity.NetFramework.g.cs index 0cd21c684c..6e249a4a2a 100644 --- a/UnitsNet/GeneratedCode/Quantities/ElectricConductivity.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ElectricConductivity.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ElectricConductivityUnit unit, [CanBeNull] /// public static ElectricConductivity Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricConductivityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromSiemensPerMeter(x.SiemensPerMeter + y.SiemensPerMeter)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ElectricConductivity Parse(string str, [CanBeNull] IFormatProvider /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricConductivity result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ElectricConductivity); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ElectricConductivityUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ElectricConductivityUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ElectricConductivityUnit ParseUnit(string str, [CanBeNull] 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ElectricConductivityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ElectricConductivityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricConductivityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricConductivityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ElectricCurrent.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ElectricCurrent.NetFramework.g.cs index 1b4d40cfa9..10f4edcf23 100644 --- a/UnitsNet/GeneratedCode/Quantities/ElectricCurrent.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ElectricCurrent.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ElectricCurrentUnit unit, [CanBeNull] IForm /// public static ElectricCurrent Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricCurrentUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromAmperes(x.Amperes + y.Amperes)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ElectricCurrent Parse(string str, [CanBeNull] IFormatProvider prov /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricCurrent result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ElectricCurrent); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ElectricCurrentUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ElectricCurrentUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ElectricCurrentUnit ParseUnit(string str, [CanBeNull] string cultu /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ElectricCurrentUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ElectricCurrentUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricCurrentUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricCurrentUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ElectricCurrentDensity.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ElectricCurrentDensity.NetFramework.g.cs index e9f57b6edc..fe14e6b3aa 100644 --- a/UnitsNet/GeneratedCode/Quantities/ElectricCurrentDensity.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ElectricCurrentDensity.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ElectricCurrentDensityUnit unit, [CanBeNull /// public static ElectricCurrentDensity Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricCurrentDensityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromAmperesPerSquareMeter(x.AmperesPerSquareMeter + y.AmperesPerSquareMeter)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ElectricCurrentDensity Parse(string str, [CanBeNull] IFormatProvid /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricCurrentDensity result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ElectricCurrentDensity); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ElectricCurrentDensityUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ElectricCurrentDensityUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ElectricCurrentDensityUnit ParseUnit(string str, [CanBeNull] strin /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ElectricCurrentDensityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ElectricCurrentDensityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricCurrentDensityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricCurrentDensityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ElectricCurrentGradient.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ElectricCurrentGradient.NetFramework.g.cs index f65e48d975..257c4525f9 100644 --- a/UnitsNet/GeneratedCode/Quantities/ElectricCurrentGradient.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ElectricCurrentGradient.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ElectricCurrentGradientUnit unit, [CanBeNul /// public static ElectricCurrentGradient Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricCurrentGradientUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromAmperesPerSecond(x.AmperesPerSecond + y.AmperesPerSecond)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ElectricCurrentGradient Parse(string str, [CanBeNull] IFormatProvi /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricCurrentGradient result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ElectricCurrentGradient); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ElectricCurrentGradientUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ElectricCurrentGradientUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ElectricCurrentGradientUnit ParseUnit(string str, [CanBeNull] stri /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ElectricCurrentGradientUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ElectricCurrentGradientUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricCurrentGradientUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricCurrentGradientUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ElectricField.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ElectricField.NetFramework.g.cs index 4f889665e5..d4ff73246c 100644 --- a/UnitsNet/GeneratedCode/Quantities/ElectricField.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ElectricField.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ElectricFieldUnit unit, [CanBeNull] IFormat /// public static ElectricField Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricFieldUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromVoltsPerMeter(x.VoltsPerMeter + y.VoltsPerMeter)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ElectricField Parse(string str, [CanBeNull] IFormatProvider provid /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricField result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ElectricField); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ElectricFieldUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ElectricFieldUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ElectricFieldUnit ParseUnit(string str, [CanBeNull] string culture /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ElectricFieldUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ElectricFieldUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricFieldUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricFieldUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ElectricInductance.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ElectricInductance.NetFramework.g.cs index c70d46afb5..b2c8413319 100644 --- a/UnitsNet/GeneratedCode/Quantities/ElectricInductance.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ElectricInductance.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ElectricInductanceUnit unit, [CanBeNull] IF /// public static ElectricInductance Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricInductanceUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromHenries(x.Henries + y.Henries)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ElectricInductance Parse(string str, [CanBeNull] IFormatProvider p /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricInductance result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ElectricInductance); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ElectricInductanceUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ElectricInductanceUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ElectricInductanceUnit ParseUnit(string str, [CanBeNull] string cu /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ElectricInductanceUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ElectricInductanceUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricInductanceUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricInductanceUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ElectricPotential.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ElectricPotential.NetFramework.g.cs index f682141e0c..ea5f9c8a2c 100644 --- a/UnitsNet/GeneratedCode/Quantities/ElectricPotential.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ElectricPotential.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ElectricPotentialUnit unit, [CanBeNull] IFo /// public static ElectricPotential Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricPotentialUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromVolts(x.Volts + y.Volts)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ElectricPotential Parse(string str, [CanBeNull] IFormatProvider pr /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricPotential result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ElectricPotential); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ElectricPotentialUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ElectricPotentialUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ElectricPotentialUnit ParseUnit(string str, [CanBeNull] string cul /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ElectricPotentialUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ElectricPotentialUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricPotentialUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricPotentialUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ElectricPotentialAc.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ElectricPotentialAc.NetFramework.g.cs index f45d171288..cb9b5e3f19 100644 --- a/UnitsNet/GeneratedCode/Quantities/ElectricPotentialAc.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ElectricPotentialAc.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ElectricPotentialAcUnit unit, [CanBeNull] I /// public static ElectricPotentialAc Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricPotentialAcUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromVoltsAc(x.VoltsAc + y.VoltsAc)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ElectricPotentialAc Parse(string str, [CanBeNull] IFormatProvider /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricPotentialAc result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ElectricPotentialAc); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ElectricPotentialAcUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ElectricPotentialAcUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ElectricPotentialAcUnit ParseUnit(string str, [CanBeNull] string c /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ElectricPotentialAcUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ElectricPotentialAcUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricPotentialAcUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricPotentialAcUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ElectricPotentialDc.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ElectricPotentialDc.NetFramework.g.cs index 49b4cd99fc..157ed29891 100644 --- a/UnitsNet/GeneratedCode/Quantities/ElectricPotentialDc.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ElectricPotentialDc.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ElectricPotentialDcUnit unit, [CanBeNull] I /// public static ElectricPotentialDc Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricPotentialDcUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromVoltsDc(x.VoltsDc + y.VoltsDc)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ElectricPotentialDc Parse(string str, [CanBeNull] IFormatProvider /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricPotentialDc result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ElectricPotentialDc); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ElectricPotentialDcUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ElectricPotentialDcUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ElectricPotentialDcUnit ParseUnit(string str, [CanBeNull] string c /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ElectricPotentialDcUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ElectricPotentialDcUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricPotentialDcUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricPotentialDcUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ElectricResistance.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ElectricResistance.NetFramework.g.cs index 78999740f5..1fea365dab 100644 --- a/UnitsNet/GeneratedCode/Quantities/ElectricResistance.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ElectricResistance.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ElectricResistanceUnit unit, [CanBeNull] IF /// public static ElectricResistance Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricResistanceUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromOhms(x.Ohms + y.Ohms)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ElectricResistance Parse(string str, [CanBeNull] IFormatProvider p /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricResistance result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ElectricResistance); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ElectricResistanceUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ElectricResistanceUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ElectricResistanceUnit ParseUnit(string str, [CanBeNull] string cu /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ElectricResistanceUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ElectricResistanceUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricResistanceUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricResistanceUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ElectricResistivity.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ElectricResistivity.NetFramework.g.cs index 974ed78d36..fb6be91793 100644 --- a/UnitsNet/GeneratedCode/Quantities/ElectricResistivity.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ElectricResistivity.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ElectricResistivityUnit unit, [CanBeNull] I /// public static ElectricResistivity Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ElectricResistivityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromOhmMeters(x.OhmMeters + y.OhmMeters)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ElectricResistivity Parse(string str, [CanBeNull] IFormatProvider /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ElectricResistivity result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ElectricResistivity); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ElectricResistivityUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ElectricResistivityUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ElectricResistivityUnit ParseUnit(string str, [CanBeNull] string c /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ElectricResistivityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ElectricResistivityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ElectricResistivityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricResistivityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Energy.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Energy.NetFramework.g.cs index 858b4dab8e..3482ceb297 100644 --- a/UnitsNet/GeneratedCode/Quantities/Energy.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Energy.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(EnergyUnit unit, [CanBeNull] IFormatProvide /// public static Energy Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - EnergyUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromJoules(x.Joules + y.Joules)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Energy Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Energy result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Energy); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static EnergyUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static EnergyUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static EnergyUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static EnergyUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out EnergyUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == EnergyUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized EnergyUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Entropy.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Entropy.NetFramework.g.cs index 67aec88196..1635b3a002 100644 --- a/UnitsNet/GeneratedCode/Quantities/Entropy.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Entropy.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(EntropyUnit unit, [CanBeNull] IFormatProvid /// public static Entropy Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - EntropyUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromJoulesPerKelvin(x.JoulesPerKelvin + y.JoulesPerKelvin)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Entropy Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Entropy result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Entropy); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static EntropyUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static EntropyUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static EntropyUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static EntropyUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out EntropyUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == EntropyUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized EntropyUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Force.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Force.NetFramework.g.cs index f77b5517fc..7af1fe358d 100644 --- a/UnitsNet/GeneratedCode/Quantities/Force.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Force.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ForceUnit unit, [CanBeNull] IFormatProvider /// public static Force Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ForceUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromNewtons(x.Newtons + y.Newtons)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Force Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Force result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Force); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ForceUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ForceUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ForceUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ForceUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ForceUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ForceUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ForceUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ForceChangeRate.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ForceChangeRate.NetFramework.g.cs index fe2a36ad69..91ac577409 100644 --- a/UnitsNet/GeneratedCode/Quantities/ForceChangeRate.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ForceChangeRate.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ForceChangeRateUnit unit, [CanBeNull] IForm /// public static ForceChangeRate Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ForceChangeRateUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromNewtonsPerSecond(x.NewtonsPerSecond + y.NewtonsPerSecond)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ForceChangeRate Parse(string str, [CanBeNull] IFormatProvider prov /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ForceChangeRate result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ForceChangeRate); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ForceChangeRateUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ForceChangeRateUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ForceChangeRateUnit ParseUnit(string str, [CanBeNull] string cultu /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ForceChangeRateUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ForceChangeRateUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ForceChangeRateUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ForceChangeRateUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ForcePerLength.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ForcePerLength.NetFramework.g.cs index f99af625c2..60f2ae4175 100644 --- a/UnitsNet/GeneratedCode/Quantities/ForcePerLength.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ForcePerLength.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ForcePerLengthUnit unit, [CanBeNull] IForma /// public static ForcePerLength Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ForcePerLengthUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromNewtonsPerMeter(x.NewtonsPerMeter + y.NewtonsPerMeter)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ForcePerLength Parse(string str, [CanBeNull] IFormatProvider provi /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ForcePerLength result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ForcePerLength); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ForcePerLengthUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ForcePerLengthUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ForcePerLengthUnit ParseUnit(string str, [CanBeNull] string cultur /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ForcePerLengthUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ForcePerLengthUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ForcePerLengthUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ForcePerLengthUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Frequency.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Frequency.NetFramework.g.cs index 808ae842de..15372682d8 100644 --- a/UnitsNet/GeneratedCode/Quantities/Frequency.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Frequency.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(FrequencyUnit unit, [CanBeNull] IFormatProv /// public static Frequency Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - FrequencyUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromHertz(x.Hertz + y.Hertz)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Frequency Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Frequency result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Frequency); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static FrequencyUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static FrequencyUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static FrequencyUnit ParseUnit(string str, [CanBeNull] string cultureName /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static FrequencyUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out FrequencyUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == FrequencyUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized FrequencyUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/HeatFlux.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/HeatFlux.NetFramework.g.cs index 4a536af266..3e1993d621 100644 --- a/UnitsNet/GeneratedCode/Quantities/HeatFlux.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/HeatFlux.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(HeatFluxUnit unit, [CanBeNull] IFormatProvi /// public static HeatFlux Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - HeatFluxUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromWattsPerSquareMeter(x.WattsPerSquareMeter + y.WattsPerSquareMeter)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static HeatFlux Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out HeatFlux result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(HeatFlux); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static HeatFluxUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static HeatFluxUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static HeatFluxUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static HeatFluxUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out HeatFluxUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == HeatFluxUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized HeatFluxUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/HeatTransferCoefficient.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/HeatTransferCoefficient.NetFramework.g.cs index 782c372609..3331bad989 100644 --- a/UnitsNet/GeneratedCode/Quantities/HeatTransferCoefficient.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/HeatTransferCoefficient.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(HeatTransferCoefficientUnit unit, [CanBeNul /// public static HeatTransferCoefficient Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - HeatTransferCoefficientUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromWattsPerSquareMeterKelvin(x.WattsPerSquareMeterKelvin + y.WattsPerSquareMeterKelvin)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static HeatTransferCoefficient Parse(string str, [CanBeNull] IFormatProvi /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out HeatTransferCoefficient result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(HeatTransferCoefficient); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static HeatTransferCoefficientUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static HeatTransferCoefficientUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static HeatTransferCoefficientUnit ParseUnit(string str, [CanBeNull] stri /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static HeatTransferCoefficientUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out HeatTransferCoefficientUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == HeatTransferCoefficientUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized HeatTransferCoefficientUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Illuminance.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Illuminance.NetFramework.g.cs index 91f95e532d..dd15c12d66 100644 --- a/UnitsNet/GeneratedCode/Quantities/Illuminance.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Illuminance.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(IlluminanceUnit unit, [CanBeNull] IFormatPr /// public static Illuminance Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - IlluminanceUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromLux(x.Lux + y.Lux)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Illuminance Parse(string str, [CanBeNull] IFormatProvider provider /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Illuminance result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Illuminance); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static IlluminanceUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static IlluminanceUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static IlluminanceUnit ParseUnit(string str, [CanBeNull] string cultureNa /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static IlluminanceUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out IlluminanceUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == IlluminanceUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized IlluminanceUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Information.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Information.NetFramework.g.cs index fc2a2db57d..e78fe76622 100644 --- a/UnitsNet/GeneratedCode/Quantities/Information.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Information.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(InformationUnit unit, [CanBeNull] IFormatPr /// public static Information Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - InformationUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromBits(x.Bits + y.Bits)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Information Parse(string str, [CanBeNull] IFormatProvider provider /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Information result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Information); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static InformationUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static InformationUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static InformationUnit ParseUnit(string str, [CanBeNull] string cultureNa /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static InformationUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out InformationUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - 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; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Irradiance.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Irradiance.NetFramework.g.cs index 8125249276..6ff5a850bc 100644 --- a/UnitsNet/GeneratedCode/Quantities/Irradiance.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Irradiance.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(IrradianceUnit unit, [CanBeNull] IFormatPro /// public static Irradiance Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - IrradianceUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromWattsPerSquareMeter(x.WattsPerSquareMeter + y.WattsPerSquareMeter)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Irradiance Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Irradiance result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Irradiance); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static IrradianceUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static IrradianceUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static IrradianceUnit ParseUnit(string str, [CanBeNull] string cultureNam /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static IrradianceUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out IrradianceUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == IrradianceUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized IrradianceUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Irradiation.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Irradiation.NetFramework.g.cs index 9de210c9e1..e0be2e850c 100644 --- a/UnitsNet/GeneratedCode/Quantities/Irradiation.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Irradiation.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(IrradiationUnit unit, [CanBeNull] IFormatPr /// public static Irradiation Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - IrradiationUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromJoulesPerSquareMeter(x.JoulesPerSquareMeter + y.JoulesPerSquareMeter)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Irradiation Parse(string str, [CanBeNull] IFormatProvider provider /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Irradiation result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Irradiation); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static IrradiationUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static IrradiationUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static IrradiationUnit ParseUnit(string str, [CanBeNull] string cultureNa /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static IrradiationUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out IrradiationUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == IrradiationUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized IrradiationUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/KinematicViscosity.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/KinematicViscosity.NetFramework.g.cs index 89b7292c8d..f00c4de6c0 100644 --- a/UnitsNet/GeneratedCode/Quantities/KinematicViscosity.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/KinematicViscosity.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(KinematicViscosityUnit unit, [CanBeNull] IF /// public static KinematicViscosity Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - KinematicViscosityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromSquareMetersPerSecond(x.SquareMetersPerSecond + y.SquareMetersPerSecond)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static KinematicViscosity Parse(string str, [CanBeNull] IFormatProvider p /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out KinematicViscosity result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(KinematicViscosity); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static KinematicViscosityUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static KinematicViscosityUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static KinematicViscosityUnit ParseUnit(string str, [CanBeNull] string cu /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static KinematicViscosityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out KinematicViscosityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == KinematicViscosityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized KinematicViscosityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/LapseRate.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/LapseRate.NetFramework.g.cs index b8f2e7c54e..5890c5290e 100644 --- a/UnitsNet/GeneratedCode/Quantities/LapseRate.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/LapseRate.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(LapseRateUnit unit, [CanBeNull] IFormatProv /// public static LapseRate Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - LapseRateUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromDegreesCelciusPerKilometer(x.DegreesCelciusPerKilometer + y.DegreesCelciusPerKilometer)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static LapseRate Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out LapseRate result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(LapseRate); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static LapseRateUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static LapseRateUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static LapseRateUnit ParseUnit(string str, [CanBeNull] string cultureName /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static LapseRateUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out LapseRateUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == LapseRateUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LapseRateUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Length.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Length.NetFramework.g.cs index affe48ffb6..c5b210b857 100644 --- a/UnitsNet/GeneratedCode/Quantities/Length.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Length.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(LengthUnit unit, [CanBeNull] IFormatProvide /// public static Length Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - LengthUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromMeters(x.Meters + y.Meters)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Length Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Length result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Length); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static LengthUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static LengthUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static LengthUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static LengthUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out LengthUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - 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; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Level.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Level.NetFramework.g.cs index eddeb0487c..6451de8caa 100644 --- a/UnitsNet/GeneratedCode/Quantities/Level.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Level.NetFramework.g.cs @@ -167,17 +167,7 @@ public static string GetAbbreviation(LevelUnit unit, [CanBeNull] IFormatProvider /// public static Level Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - LevelUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromDecibels(x.Decibels + y.Decibels)); + return ParseInternal(str, provider); } /// @@ -186,39 +176,28 @@ public static Level Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Level result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Level); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static LevelUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static LevelUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -226,27 +205,14 @@ public static LevelUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static LevelUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out LevelUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - 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; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/LinearDensity.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/LinearDensity.NetFramework.g.cs index 9c7d0fa666..6c35aff755 100644 --- a/UnitsNet/GeneratedCode/Quantities/LinearDensity.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/LinearDensity.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(LinearDensityUnit unit, [CanBeNull] IFormat /// public static LinearDensity Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - LinearDensityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromKilogramsPerMeter(x.KilogramsPerMeter + y.KilogramsPerMeter)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static LinearDensity Parse(string str, [CanBeNull] IFormatProvider provid /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out LinearDensity result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(LinearDensity); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static LinearDensityUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static LinearDensityUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static LinearDensityUnit ParseUnit(string str, [CanBeNull] string culture /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static LinearDensityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out LinearDensityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == LinearDensityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LinearDensityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/LuminousFlux.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/LuminousFlux.NetFramework.g.cs index a7cdd9f6e3..4baca6ab60 100644 --- a/UnitsNet/GeneratedCode/Quantities/LuminousFlux.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/LuminousFlux.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(LuminousFluxUnit unit, [CanBeNull] IFormatP /// public static LuminousFlux Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - LuminousFluxUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromLumens(x.Lumens + y.Lumens)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static LuminousFlux Parse(string str, [CanBeNull] IFormatProvider provide /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out LuminousFlux result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(LuminousFlux); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static LuminousFluxUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static LuminousFluxUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static LuminousFluxUnit ParseUnit(string str, [CanBeNull] string cultureN /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static LuminousFluxUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out LuminousFluxUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == LuminousFluxUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LuminousFluxUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/LuminousIntensity.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/LuminousIntensity.NetFramework.g.cs index e4273f49a0..891bb06996 100644 --- a/UnitsNet/GeneratedCode/Quantities/LuminousIntensity.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/LuminousIntensity.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(LuminousIntensityUnit unit, [CanBeNull] IFo /// public static LuminousIntensity Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - LuminousIntensityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromCandela(x.Candela + y.Candela)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static LuminousIntensity Parse(string str, [CanBeNull] IFormatProvider pr /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out LuminousIntensity result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(LuminousIntensity); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static LuminousIntensityUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static LuminousIntensityUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static LuminousIntensityUnit ParseUnit(string str, [CanBeNull] string cul /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static LuminousIntensityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out LuminousIntensityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == LuminousIntensityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LuminousIntensityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/MagneticField.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/MagneticField.NetFramework.g.cs index 4f6410d595..072de3398b 100644 --- a/UnitsNet/GeneratedCode/Quantities/MagneticField.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/MagneticField.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(MagneticFieldUnit unit, [CanBeNull] IFormat /// public static MagneticField Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MagneticFieldUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromTeslas(x.Teslas + y.Teslas)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static MagneticField Parse(string str, [CanBeNull] IFormatProvider provid /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out MagneticField result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(MagneticField); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static MagneticFieldUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static MagneticFieldUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static MagneticFieldUnit ParseUnit(string str, [CanBeNull] string culture /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static MagneticFieldUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out MagneticFieldUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MagneticFieldUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MagneticFieldUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/MagneticFlux.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/MagneticFlux.NetFramework.g.cs index 004a649b85..066d3140ef 100644 --- a/UnitsNet/GeneratedCode/Quantities/MagneticFlux.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/MagneticFlux.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(MagneticFluxUnit unit, [CanBeNull] IFormatP /// public static MagneticFlux Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MagneticFluxUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromWebers(x.Webers + y.Webers)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static MagneticFlux Parse(string str, [CanBeNull] IFormatProvider provide /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out MagneticFlux result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(MagneticFlux); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static MagneticFluxUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static MagneticFluxUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static MagneticFluxUnit ParseUnit(string str, [CanBeNull] string cultureN /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static MagneticFluxUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out MagneticFluxUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MagneticFluxUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MagneticFluxUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Magnetization.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Magnetization.NetFramework.g.cs index 0b89647188..838dded44e 100644 --- a/UnitsNet/GeneratedCode/Quantities/Magnetization.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Magnetization.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(MagnetizationUnit unit, [CanBeNull] IFormat /// public static Magnetization Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MagnetizationUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromAmperesPerMeter(x.AmperesPerMeter + y.AmperesPerMeter)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Magnetization Parse(string str, [CanBeNull] IFormatProvider provid /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Magnetization result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Magnetization); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static MagnetizationUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static MagnetizationUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static MagnetizationUnit ParseUnit(string str, [CanBeNull] string culture /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static MagnetizationUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out MagnetizationUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MagnetizationUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MagnetizationUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Mass.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Mass.NetFramework.g.cs index ef690ea9e7..24f2b22b8f 100644 --- a/UnitsNet/GeneratedCode/Quantities/Mass.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Mass.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(MassUnit unit, [CanBeNull] IFormatProvider /// public static Mass Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MassUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromKilograms(x.Kilograms + y.Kilograms)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Mass Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Mass result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Mass); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static MassUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static MassUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static MassUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static MassUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out MassUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MassUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MassUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/MassFlow.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/MassFlow.NetFramework.g.cs index 1028bec32c..c13aa7b743 100644 --- a/UnitsNet/GeneratedCode/Quantities/MassFlow.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/MassFlow.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(MassFlowUnit unit, [CanBeNull] IFormatProvi /// public static MassFlow Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MassFlowUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromGramsPerSecond(x.GramsPerSecond + y.GramsPerSecond)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static MassFlow Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out MassFlow result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(MassFlow); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static MassFlowUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static MassFlowUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static MassFlowUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static MassFlowUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out MassFlowUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MassFlowUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MassFlowUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/MassFlux.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/MassFlux.NetFramework.g.cs index d158b77d95..591732027d 100644 --- a/UnitsNet/GeneratedCode/Quantities/MassFlux.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/MassFlux.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(MassFluxUnit unit, [CanBeNull] IFormatProvi /// public static MassFlux Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MassFluxUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromKilogramsPerSecondPerSquareMeter(x.KilogramsPerSecondPerSquareMeter + y.KilogramsPerSecondPerSquareMeter)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static MassFlux Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out MassFlux result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(MassFlux); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static MassFluxUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static MassFluxUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static MassFluxUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static MassFluxUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out MassFluxUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MassFluxUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MassFluxUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/MassMomentOfInertia.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/MassMomentOfInertia.NetFramework.g.cs index b1d3435bdf..e8025e9218 100644 --- a/UnitsNet/GeneratedCode/Quantities/MassMomentOfInertia.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/MassMomentOfInertia.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(MassMomentOfInertiaUnit unit, [CanBeNull] I /// public static MassMomentOfInertia Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MassMomentOfInertiaUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromKilogramSquareMeters(x.KilogramSquareMeters + y.KilogramSquareMeters)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static MassMomentOfInertia Parse(string str, [CanBeNull] IFormatProvider /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out MassMomentOfInertia result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(MassMomentOfInertia); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static MassMomentOfInertiaUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static MassMomentOfInertiaUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static MassMomentOfInertiaUnit ParseUnit(string str, [CanBeNull] string c /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static MassMomentOfInertiaUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out MassMomentOfInertiaUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MassMomentOfInertiaUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MassMomentOfInertiaUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/MolarEnergy.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/MolarEnergy.NetFramework.g.cs index 1bb371ad77..c19ce85b47 100644 --- a/UnitsNet/GeneratedCode/Quantities/MolarEnergy.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/MolarEnergy.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(MolarEnergyUnit unit, [CanBeNull] IFormatPr /// public static MolarEnergy Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MolarEnergyUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromJoulesPerMole(x.JoulesPerMole + y.JoulesPerMole)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static MolarEnergy Parse(string str, [CanBeNull] IFormatProvider provider /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out MolarEnergy result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(MolarEnergy); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static MolarEnergyUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static MolarEnergyUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static MolarEnergyUnit ParseUnit(string str, [CanBeNull] string cultureNa /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static MolarEnergyUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out MolarEnergyUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MolarEnergyUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MolarEnergyUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/MolarEntropy.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/MolarEntropy.NetFramework.g.cs index a65e4cda72..3e0c567f89 100644 --- a/UnitsNet/GeneratedCode/Quantities/MolarEntropy.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/MolarEntropy.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(MolarEntropyUnit unit, [CanBeNull] IFormatP /// public static MolarEntropy Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MolarEntropyUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromJoulesPerMoleKelvin(x.JoulesPerMoleKelvin + y.JoulesPerMoleKelvin)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static MolarEntropy Parse(string str, [CanBeNull] IFormatProvider provide /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out MolarEntropy result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(MolarEntropy); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static MolarEntropyUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static MolarEntropyUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static MolarEntropyUnit ParseUnit(string str, [CanBeNull] string cultureN /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static MolarEntropyUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out MolarEntropyUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MolarEntropyUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MolarEntropyUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/MolarMass.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/MolarMass.NetFramework.g.cs index 5ec81eb213..8359bcd7f0 100644 --- a/UnitsNet/GeneratedCode/Quantities/MolarMass.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/MolarMass.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(MolarMassUnit unit, [CanBeNull] IFormatProv /// public static MolarMass Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MolarMassUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromKilogramsPerMole(x.KilogramsPerMole + y.KilogramsPerMole)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static MolarMass Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out MolarMass result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(MolarMass); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static MolarMassUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static MolarMassUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static MolarMassUnit ParseUnit(string str, [CanBeNull] string cultureName /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static MolarMassUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out MolarMassUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MolarMassUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MolarMassUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Molarity.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Molarity.NetFramework.g.cs index 5a119af8d3..9f29e99f44 100644 --- a/UnitsNet/GeneratedCode/Quantities/Molarity.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Molarity.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(MolarityUnit unit, [CanBeNull] IFormatProvi /// public static Molarity Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - MolarityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromMolesPerCubicMeter(x.MolesPerCubicMeter + y.MolesPerCubicMeter)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Molarity Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Molarity result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Molarity); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static MolarityUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static MolarityUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static MolarityUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static MolarityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out MolarityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == MolarityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MolarityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Permeability.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Permeability.NetFramework.g.cs index 1d5ad93d6d..8c27d18663 100644 --- a/UnitsNet/GeneratedCode/Quantities/Permeability.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Permeability.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(PermeabilityUnit unit, [CanBeNull] IFormatP /// public static Permeability Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - PermeabilityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromHenriesPerMeter(x.HenriesPerMeter + y.HenriesPerMeter)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Permeability Parse(string str, [CanBeNull] IFormatProvider provide /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Permeability result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Permeability); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static PermeabilityUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static PermeabilityUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static PermeabilityUnit ParseUnit(string str, [CanBeNull] string cultureN /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static PermeabilityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out PermeabilityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == PermeabilityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PermeabilityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Permittivity.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Permittivity.NetFramework.g.cs index 70ff35b4bd..45a078a8be 100644 --- a/UnitsNet/GeneratedCode/Quantities/Permittivity.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Permittivity.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(PermittivityUnit unit, [CanBeNull] IFormatP /// public static Permittivity Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - PermittivityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromFaradsPerMeter(x.FaradsPerMeter + y.FaradsPerMeter)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Permittivity Parse(string str, [CanBeNull] IFormatProvider provide /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Permittivity result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Permittivity); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static PermittivityUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static PermittivityUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static PermittivityUnit ParseUnit(string str, [CanBeNull] string cultureN /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static PermittivityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out PermittivityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == PermittivityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PermittivityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Power.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Power.NetFramework.g.cs index 443733eac9..3621a1c702 100644 --- a/UnitsNet/GeneratedCode/Quantities/Power.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Power.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(PowerUnit unit, [CanBeNull] IFormatProvider /// public static Power Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - PowerUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromWatts(x.Watts + y.Watts)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Power Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Power result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Power); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static PowerUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static PowerUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static PowerUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static PowerUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out PowerUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == PowerUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PowerUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/PowerDensity.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/PowerDensity.NetFramework.g.cs index 5482e33348..31e670a0dd 100644 --- a/UnitsNet/GeneratedCode/Quantities/PowerDensity.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/PowerDensity.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(PowerDensityUnit unit, [CanBeNull] IFormatP /// public static PowerDensity Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - PowerDensityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromWattsPerCubicMeter(x.WattsPerCubicMeter + y.WattsPerCubicMeter)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static PowerDensity Parse(string str, [CanBeNull] IFormatProvider provide /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out PowerDensity result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(PowerDensity); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static PowerDensityUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static PowerDensityUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static PowerDensityUnit ParseUnit(string str, [CanBeNull] string cultureN /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static PowerDensityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out PowerDensityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == PowerDensityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PowerDensityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/PowerRatio.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/PowerRatio.NetFramework.g.cs index 77191aecd0..b2d1f1c0ae 100644 --- a/UnitsNet/GeneratedCode/Quantities/PowerRatio.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/PowerRatio.NetFramework.g.cs @@ -167,17 +167,7 @@ public static string GetAbbreviation(PowerRatioUnit unit, [CanBeNull] IFormatPro /// public static PowerRatio Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - PowerRatioUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromDecibelWatts(x.DecibelWatts + y.DecibelWatts)); + return ParseInternal(str, provider); } /// @@ -186,39 +176,28 @@ public static PowerRatio Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out PowerRatio result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(PowerRatio); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static PowerRatioUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static PowerRatioUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -226,27 +205,14 @@ public static PowerRatioUnit ParseUnit(string str, [CanBeNull] string cultureNam /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static PowerRatioUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out PowerRatioUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == PowerRatioUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PowerRatioUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Pressure.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Pressure.NetFramework.g.cs index e84ab74bc8..cafceace96 100644 --- a/UnitsNet/GeneratedCode/Quantities/Pressure.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Pressure.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(PressureUnit unit, [CanBeNull] IFormatProvi /// public static Pressure Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - PressureUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromPascals(x.Pascals + y.Pascals)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Pressure Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Pressure result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Pressure); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static PressureUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static PressureUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static PressureUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static PressureUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out PressureUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == PressureUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PressureUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/PressureChangeRate.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/PressureChangeRate.NetFramework.g.cs index 98bf53bbbd..a994f15ca0 100644 --- a/UnitsNet/GeneratedCode/Quantities/PressureChangeRate.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/PressureChangeRate.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(PressureChangeRateUnit unit, [CanBeNull] IF /// public static PressureChangeRate Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - PressureChangeRateUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromPascalsPerSecond(x.PascalsPerSecond + y.PascalsPerSecond)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static PressureChangeRate Parse(string str, [CanBeNull] IFormatProvider p /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out PressureChangeRate result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(PressureChangeRate); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static PressureChangeRateUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static PressureChangeRateUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static PressureChangeRateUnit ParseUnit(string str, [CanBeNull] string cu /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static PressureChangeRateUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out PressureChangeRateUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == PressureChangeRateUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PressureChangeRateUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Ratio.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Ratio.NetFramework.g.cs index a627e7be80..35b06ba59a 100644 --- a/UnitsNet/GeneratedCode/Quantities/Ratio.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Ratio.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(RatioUnit unit, [CanBeNull] IFormatProvider /// public static Ratio Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - RatioUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromDecimalFractions(x.DecimalFractions + y.DecimalFractions)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Ratio Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Ratio result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Ratio); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static RatioUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static RatioUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static RatioUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static RatioUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out RatioUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == RatioUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized RatioUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ReactiveEnergy.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ReactiveEnergy.NetFramework.g.cs index d92acc5f57..7db8605a22 100644 --- a/UnitsNet/GeneratedCode/Quantities/ReactiveEnergy.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ReactiveEnergy.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ReactiveEnergyUnit unit, [CanBeNull] IForma /// public static ReactiveEnergy Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ReactiveEnergyUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromVoltampereReactiveHours(x.VoltampereReactiveHours + y.VoltampereReactiveHours)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ReactiveEnergy Parse(string str, [CanBeNull] IFormatProvider provi /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ReactiveEnergy result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ReactiveEnergy); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ReactiveEnergyUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ReactiveEnergyUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ReactiveEnergyUnit ParseUnit(string str, [CanBeNull] string cultur /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ReactiveEnergyUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ReactiveEnergyUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ReactiveEnergyUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ReactiveEnergyUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ReactivePower.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ReactivePower.NetFramework.g.cs index 45ec1844c5..0fdceb9a43 100644 --- a/UnitsNet/GeneratedCode/Quantities/ReactivePower.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ReactivePower.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ReactivePowerUnit unit, [CanBeNull] IFormat /// public static ReactivePower Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ReactivePowerUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromVoltamperesReactive(x.VoltamperesReactive + y.VoltamperesReactive)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ReactivePower Parse(string str, [CanBeNull] IFormatProvider provid /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ReactivePower result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ReactivePower); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ReactivePowerUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ReactivePowerUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ReactivePowerUnit ParseUnit(string str, [CanBeNull] string culture /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ReactivePowerUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ReactivePowerUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ReactivePowerUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ReactivePowerUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/RotationalAcceleration.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/RotationalAcceleration.NetFramework.g.cs index 41b084d2d0..ace547f299 100644 --- a/UnitsNet/GeneratedCode/Quantities/RotationalAcceleration.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/RotationalAcceleration.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(RotationalAccelerationUnit unit, [CanBeNull /// public static RotationalAcceleration Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - RotationalAccelerationUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromRadiansPerSecondSquared(x.RadiansPerSecondSquared + y.RadiansPerSecondSquared)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static RotationalAcceleration Parse(string str, [CanBeNull] IFormatProvid /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out RotationalAcceleration result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(RotationalAcceleration); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static RotationalAccelerationUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static RotationalAccelerationUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static RotationalAccelerationUnit ParseUnit(string str, [CanBeNull] strin /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static RotationalAccelerationUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out RotationalAccelerationUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == RotationalAccelerationUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized RotationalAccelerationUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/RotationalSpeed.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/RotationalSpeed.NetFramework.g.cs index ab81a13b9a..8ff386ae37 100644 --- a/UnitsNet/GeneratedCode/Quantities/RotationalSpeed.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/RotationalSpeed.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(RotationalSpeedUnit unit, [CanBeNull] IForm /// public static RotationalSpeed Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - RotationalSpeedUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromRadiansPerSecond(x.RadiansPerSecond + y.RadiansPerSecond)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static RotationalSpeed Parse(string str, [CanBeNull] IFormatProvider prov /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out RotationalSpeed result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(RotationalSpeed); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static RotationalSpeedUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static RotationalSpeedUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static RotationalSpeedUnit ParseUnit(string str, [CanBeNull] string cultu /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static RotationalSpeedUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out RotationalSpeedUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == RotationalSpeedUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized RotationalSpeedUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/RotationalStiffness.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/RotationalStiffness.NetFramework.g.cs index 5529da419e..ea2a46733f 100644 --- a/UnitsNet/GeneratedCode/Quantities/RotationalStiffness.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/RotationalStiffness.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(RotationalStiffnessUnit unit, [CanBeNull] I /// public static RotationalStiffness Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - RotationalStiffnessUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromNewtonMetersPerRadian(x.NewtonMetersPerRadian + y.NewtonMetersPerRadian)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static RotationalStiffness Parse(string str, [CanBeNull] IFormatProvider /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out RotationalStiffness result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(RotationalStiffness); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static RotationalStiffnessUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static RotationalStiffnessUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static RotationalStiffnessUnit ParseUnit(string str, [CanBeNull] string c /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static RotationalStiffnessUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out RotationalStiffnessUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == RotationalStiffnessUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized RotationalStiffnessUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/RotationalStiffnessPerLength.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/RotationalStiffnessPerLength.NetFramework.g.cs index e5e2dd73a4..6195452621 100644 --- a/UnitsNet/GeneratedCode/Quantities/RotationalStiffnessPerLength.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/RotationalStiffnessPerLength.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(RotationalStiffnessPerLengthUnit unit, [Can /// public static RotationalStiffnessPerLength Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - RotationalStiffnessPerLengthUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromNewtonMetersPerRadianPerMeter(x.NewtonMetersPerRadianPerMeter + y.NewtonMetersPerRadianPerMeter)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static RotationalStiffnessPerLength Parse(string str, [CanBeNull] IFormat /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out RotationalStiffnessPerLength result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(RotationalStiffnessPerLength); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static RotationalStiffnessPerLengthUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static RotationalStiffnessPerLengthUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static RotationalStiffnessPerLengthUnit ParseUnit(string str, [CanBeNull] /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static RotationalStiffnessPerLengthUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out RotationalStiffnessPerLengthUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == RotationalStiffnessPerLengthUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized RotationalStiffnessPerLengthUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/SolidAngle.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/SolidAngle.NetFramework.g.cs index 096a0ff8d1..4aea67061b 100644 --- a/UnitsNet/GeneratedCode/Quantities/SolidAngle.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/SolidAngle.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(SolidAngleUnit unit, [CanBeNull] IFormatPro /// public static SolidAngle Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - SolidAngleUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromSteradians(x.Steradians + y.Steradians)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static SolidAngle Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out SolidAngle result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(SolidAngle); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static SolidAngleUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static SolidAngleUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static SolidAngleUnit ParseUnit(string str, [CanBeNull] string cultureNam /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static SolidAngleUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out SolidAngleUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == SolidAngleUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized SolidAngleUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/SpecificEnergy.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/SpecificEnergy.NetFramework.g.cs index c8c029e3c5..3b1a92cc76 100644 --- a/UnitsNet/GeneratedCode/Quantities/SpecificEnergy.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/SpecificEnergy.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(SpecificEnergyUnit unit, [CanBeNull] IForma /// public static SpecificEnergy Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - SpecificEnergyUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromJoulesPerKilogram(x.JoulesPerKilogram + y.JoulesPerKilogram)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static SpecificEnergy Parse(string str, [CanBeNull] IFormatProvider provi /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out SpecificEnergy result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(SpecificEnergy); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static SpecificEnergyUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static SpecificEnergyUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static SpecificEnergyUnit ParseUnit(string str, [CanBeNull] string cultur /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static SpecificEnergyUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out SpecificEnergyUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == SpecificEnergyUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized SpecificEnergyUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/SpecificEntropy.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/SpecificEntropy.NetFramework.g.cs index ee4e4a200c..e8aa120bbd 100644 --- a/UnitsNet/GeneratedCode/Quantities/SpecificEntropy.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/SpecificEntropy.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(SpecificEntropyUnit unit, [CanBeNull] IForm /// public static SpecificEntropy Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - SpecificEntropyUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromJoulesPerKilogramKelvin(x.JoulesPerKilogramKelvin + y.JoulesPerKilogramKelvin)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static SpecificEntropy Parse(string str, [CanBeNull] IFormatProvider prov /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out SpecificEntropy result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(SpecificEntropy); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static SpecificEntropyUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static SpecificEntropyUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static SpecificEntropyUnit ParseUnit(string str, [CanBeNull] string cultu /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static SpecificEntropyUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out SpecificEntropyUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == SpecificEntropyUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized SpecificEntropyUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/SpecificVolume.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/SpecificVolume.NetFramework.g.cs index 7fa18edac4..58b9339a91 100644 --- a/UnitsNet/GeneratedCode/Quantities/SpecificVolume.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/SpecificVolume.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(SpecificVolumeUnit unit, [CanBeNull] IForma /// public static SpecificVolume Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - SpecificVolumeUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromCubicMetersPerKilogram(x.CubicMetersPerKilogram + y.CubicMetersPerKilogram)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static SpecificVolume Parse(string str, [CanBeNull] IFormatProvider provi /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out SpecificVolume result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(SpecificVolume); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static SpecificVolumeUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static SpecificVolumeUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static SpecificVolumeUnit ParseUnit(string str, [CanBeNull] string cultur /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static SpecificVolumeUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out SpecificVolumeUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == SpecificVolumeUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized SpecificVolumeUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/SpecificWeight.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/SpecificWeight.NetFramework.g.cs index 2afcec90b2..2df6fdf98d 100644 --- a/UnitsNet/GeneratedCode/Quantities/SpecificWeight.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/SpecificWeight.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(SpecificWeightUnit unit, [CanBeNull] IForma /// public static SpecificWeight Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - SpecificWeightUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromNewtonsPerCubicMeter(x.NewtonsPerCubicMeter + y.NewtonsPerCubicMeter)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static SpecificWeight Parse(string str, [CanBeNull] IFormatProvider provi /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out SpecificWeight result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(SpecificWeight); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static SpecificWeightUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static SpecificWeightUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static SpecificWeightUnit ParseUnit(string str, [CanBeNull] string cultur /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static SpecificWeightUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out SpecificWeightUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == SpecificWeightUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized SpecificWeightUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Speed.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Speed.NetFramework.g.cs index 1c89926821..1acc6c2f5e 100644 --- a/UnitsNet/GeneratedCode/Quantities/Speed.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Speed.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(SpeedUnit unit, [CanBeNull] IFormatProvider /// public static Speed Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - SpeedUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromMetersPerSecond(x.MetersPerSecond + y.MetersPerSecond)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Speed Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Speed result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Speed); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static SpeedUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static SpeedUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static SpeedUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static SpeedUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out SpeedUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == SpeedUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized SpeedUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Temperature.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Temperature.NetFramework.g.cs index 2fd541ddba..56dc7ce9d9 100644 --- a/UnitsNet/GeneratedCode/Quantities/Temperature.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Temperature.NetFramework.g.cs @@ -120,17 +120,7 @@ public static string GetAbbreviation(TemperatureUnit unit, [CanBeNull] IFormatPr /// public static Temperature Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - TemperatureUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromKelvins(x.Kelvins + y.Kelvins)); + return ParseInternal(str, provider); } /// @@ -139,39 +129,28 @@ public static Temperature Parse(string str, [CanBeNull] IFormatProvider provider /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Temperature result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Temperature); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static TemperatureUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static TemperatureUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -179,27 +158,14 @@ public static TemperatureUnit ParseUnit(string str, [CanBeNull] string cultureNa /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static TemperatureUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out TemperatureUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == TemperatureUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized TemperatureUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/TemperatureChangeRate.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/TemperatureChangeRate.NetFramework.g.cs index 7e53c56fd0..bcb70fb0a3 100644 --- a/UnitsNet/GeneratedCode/Quantities/TemperatureChangeRate.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/TemperatureChangeRate.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(TemperatureChangeRateUnit unit, [CanBeNull] /// public static TemperatureChangeRate Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - TemperatureChangeRateUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromDegreesCelsiusPerSecond(x.DegreesCelsiusPerSecond + y.DegreesCelsiusPerSecond)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static TemperatureChangeRate Parse(string str, [CanBeNull] IFormatProvide /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out TemperatureChangeRate result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(TemperatureChangeRate); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static TemperatureChangeRateUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static TemperatureChangeRateUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static TemperatureChangeRateUnit ParseUnit(string str, [CanBeNull] 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static TemperatureChangeRateUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out TemperatureChangeRateUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == TemperatureChangeRateUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized TemperatureChangeRateUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/TemperatureDelta.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/TemperatureDelta.NetFramework.g.cs index db1081453e..a7f3141437 100644 --- a/UnitsNet/GeneratedCode/Quantities/TemperatureDelta.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/TemperatureDelta.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(TemperatureDeltaUnit unit, [CanBeNull] IFor /// public static TemperatureDelta Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - TemperatureDeltaUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromKelvins(x.Kelvins + y.Kelvins)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static TemperatureDelta Parse(string str, [CanBeNull] IFormatProvider pro /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out TemperatureDelta result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(TemperatureDelta); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static TemperatureDeltaUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static TemperatureDeltaUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static TemperatureDeltaUnit ParseUnit(string str, [CanBeNull] string cult /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static TemperatureDeltaUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out TemperatureDeltaUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == TemperatureDeltaUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized TemperatureDeltaUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ThermalConductivity.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ThermalConductivity.NetFramework.g.cs index 837c5cb743..7992a4505e 100644 --- a/UnitsNet/GeneratedCode/Quantities/ThermalConductivity.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ThermalConductivity.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ThermalConductivityUnit unit, [CanBeNull] I /// public static ThermalConductivity Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ThermalConductivityUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromWattsPerMeterKelvin(x.WattsPerMeterKelvin + y.WattsPerMeterKelvin)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ThermalConductivity Parse(string str, [CanBeNull] IFormatProvider /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ThermalConductivity result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ThermalConductivity); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ThermalConductivityUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ThermalConductivityUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ThermalConductivityUnit ParseUnit(string str, [CanBeNull] string c /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ThermalConductivityUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ThermalConductivityUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ThermalConductivityUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ThermalConductivityUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/ThermalResistance.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/ThermalResistance.NetFramework.g.cs index 3f292a7c8d..b97d9e6b7b 100644 --- a/UnitsNet/GeneratedCode/Quantities/ThermalResistance.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/ThermalResistance.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(ThermalResistanceUnit unit, [CanBeNull] IFo /// public static ThermalResistance Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - ThermalResistanceUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromSquareMeterKelvinsPerKilowatt(x.SquareMeterKelvinsPerKilowatt + y.SquareMeterKelvinsPerKilowatt)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static ThermalResistance Parse(string str, [CanBeNull] IFormatProvider pr /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out ThermalResistance result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(ThermalResistance); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static ThermalResistanceUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static ThermalResistanceUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static ThermalResistanceUnit ParseUnit(string str, [CanBeNull] string cul /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static ThermalResistanceUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out ThermalResistanceUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == ThermalResistanceUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ThermalResistanceUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Torque.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Torque.NetFramework.g.cs index 2af13bb0b7..d16002ecda 100644 --- a/UnitsNet/GeneratedCode/Quantities/Torque.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Torque.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(TorqueUnit unit, [CanBeNull] IFormatProvide /// public static Torque Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - TorqueUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromNewtonMeters(x.NewtonMeters + y.NewtonMeters)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Torque Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Torque result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Torque); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static TorqueUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static TorqueUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static TorqueUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static TorqueUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out TorqueUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == TorqueUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized TorqueUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/VitaminA.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/VitaminA.NetFramework.g.cs index 1f994609a1..2b230be959 100644 --- a/UnitsNet/GeneratedCode/Quantities/VitaminA.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/VitaminA.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(VitaminAUnit unit, [CanBeNull] IFormatProvi /// public static VitaminA Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - VitaminAUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromInternationalUnits(x.InternationalUnits + y.InternationalUnits)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static VitaminA Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out VitaminA result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(VitaminA); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static VitaminAUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static VitaminAUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static VitaminAUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static VitaminAUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out VitaminAUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == VitaminAUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized VitaminAUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/Volume.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Volume.NetFramework.g.cs index 333fb49dbb..4775fd8baf 100644 --- a/UnitsNet/GeneratedCode/Quantities/Volume.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Volume.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(VolumeUnit unit, [CanBeNull] IFormatProvide /// public static Volume Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - VolumeUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromCubicMeters(x.CubicMeters + y.CubicMeters)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static Volume Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Volume result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(Volume); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static VolumeUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static VolumeUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static VolumeUnit ParseUnit(string str, [CanBeNull] string cultureName) /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static VolumeUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out VolumeUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == VolumeUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized VolumeUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/GeneratedCode/Quantities/VolumeFlow.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/VolumeFlow.NetFramework.g.cs index 816a12a8f2..83f543ccb8 100644 --- a/UnitsNet/GeneratedCode/Quantities/VolumeFlow.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/VolumeFlow.NetFramework.g.cs @@ -159,17 +159,7 @@ public static string GetAbbreviation(VolumeFlowUnit unit, [CanBeNull] IFormatPro /// public static VolumeFlow Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - VolumeFlowUnit parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => FromCubicMetersPerSecond(x.CubicMetersPerSecond + y.CubicMetersPerSecond)); + return ParseInternal(str, provider); } /// @@ -178,39 +168,28 @@ public static VolumeFlow Parse(string str, [CanBeNull] IFormatProvider provider) /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out VolumeFlow result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default(VolumeFlow); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static VolumeFlowUnit ParseUnit(string str, [CanBeNull] string cultureName) + public static VolumeFlowUnit ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -218,27 +197,14 @@ public static VolumeFlowUnit ParseUnit(string str, [CanBeNull] string cultureNam /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static VolumeFlowUnit ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out VolumeFlowUnit unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse(str.Trim()); - - if (unit == VolumeFlowUnit.Undefined) - { - var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized VolumeFlowUnit."); - newEx.Data["input"] = str; - newEx.Data["provider"] = provider?.ToString() ?? "(null)"; - throw newEx; - } - - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/Scripts/Include-GenerateQuantitySourceCodeCommon.ps1 b/UnitsNet/Scripts/Include-GenerateQuantitySourceCodeCommon.ps1 index b39e85678a..b9eb6e5f01 100644 --- a/UnitsNet/Scripts/Include-GenerateQuantitySourceCodeCommon.ps1 +++ b/UnitsNet/Scripts/Include-GenerateQuantitySourceCodeCommon.ps1 @@ -5,6 +5,7 @@ $baseType = $quantity.BaseType; $baseUnit = $units | where { $_.SingularName -eq $quantity.BaseUnit } $baseUnitSingularName = $baseUnit.SingularName + $baseUnitPluralName = $baseUnit.PluralName $unitEnumName = "$quantityName" + "Unit" # Base dimensions @@ -417,7 +418,7 @@ if ($obsoleteAttribute) /// public static $quantityName Parse(string str) { - return Parse(str, null); + return ParseInternal(str, null); } /// @@ -430,7 +431,7 @@ if ($obsoleteAttribute) /// public static bool TryParse([CanBeNull] string str, out $quantityName result) { - return TryParse(str, null, out result); + return TryParseInternal(str, null, out result); } /// @@ -444,7 +445,140 @@ if ($obsoleteAttribute) /// Error parsing string. public static $unitEnumName ParseUnit(string str) { - return ParseUnit(str, (IFormatProvider)null); + return ParseUnitInternal(str, null); + } + + public static bool TryParseUnit(string str, out $unitEnumName unit) + { + return TryParseUnitInternal(str, null, 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. + /// + internal static $quantityName ParseInternal(string str, [CanBeNull] IFormatProvider provider) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.Parse<$quantityName, $unitEnumName>(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + var parsedValue = double.Parse(value, formatProvider2); + var parsedUnit = ParseUnitInternal(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => From(x.$baseUnitPluralName + y.$baseUnitPluralName, BaseUnit)); + } + + /// + /// 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")); + /// + internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out $quantityName result) + { + result = default($quantityName); + + if(string.IsNullOrWhiteSpace(str)) + return false; + + provider = provider ?? UnitSystem.DefaultCulture; + + return QuantityParser.TryParse<$quantityName, $unitEnumName>(str, provider, + delegate(string value, string unit, IFormatProvider formatProvider2, out $quantityName parsed$quantityName ) + { + parsed$quantityName = default($quantityName); + + if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue)) + return false; + + if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit)) + return false; + + parsed$quantityName = From(parsedValue, parsedUnit); + return true; + }, (x, y) => From(x.$baseUnitPluralName + y.$baseUnitPluralName, BaseUnit), 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. + internal static $unitEnumName ParseUnitInternal(string str, IFormatProvider provider = null) + { + if (str == null) throw new ArgumentNullException(nameof(str)); + + var unitSystem = UnitSystem.GetCached(provider); + var unit = unitSystem.Parse<$unitEnumName>(str.Trim()); + + 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")); + /// + internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out $unitEnumName unit) + { + unit = $unitEnumName.Undefined; + + if(string.IsNullOrWhiteSpace(str)) + return false; + + var unitSystem = UnitSystem.GetCached(provider); + if(!unitSystem.TryParse<$unitEnumName>(str.Trim(), out unit)) + return false; + + if(unit == $unitEnumName.Undefined) + return false; + + return true; } #endregion diff --git a/UnitsNet/Scripts/Include-GenerateQuantitySourceCodeNetFramework.ps1 b/UnitsNet/Scripts/Include-GenerateQuantitySourceCodeNetFramework.ps1 index a3fbb86bed..1e5fe12de7 100644 --- a/UnitsNet/Scripts/Include-GenerateQuantitySourceCodeNetFramework.ps1 +++ b/UnitsNet/Scripts/Include-GenerateQuantitySourceCodeNetFramework.ps1 @@ -178,17 +178,7 @@ namespace UnitsNet /// public static $quantityName Parse(string str, [CanBeNull] IFormatProvider provider) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - provider = provider ?? UnitSystem.DefaultCulture; - - return QuantityParser.Parse<$quantityName, $unitEnumName>(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - $unitEnumName parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => From$baseUnitPluralName(x.$baseUnitPluralName + y.$baseUnitPluralName)); + return ParseInternal(str, provider); } /// @@ -197,39 +187,28 @@ namespace UnitsNet /// 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")); /// public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out $quantityName result) { - provider = provider ?? UnitSystem.DefaultCulture; - - try - { - result = Parse(str, provider); - return true; - } - catch - { - result = default($quantityName); - return false; - } + return TryParseInternal(str, provider, out result); } /// /// Parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// 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. - [Obsolete("Use overload that takes IFormatProvider instead of culture name. This method was only added to support WindowsRuntimeComponent and will be removed from .NET Framework targets.")] - public static $unitEnumName ParseUnit(string str, [CanBeNull] string cultureName) + public static $unitEnumName ParseUnit(string str, IFormatProvider provider = null) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + return ParseUnitInternal(str, provider); } /// @@ -237,27 +216,14 @@ namespace UnitsNet /// /// 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")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - public static $unitEnumName ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, IFormatProvider provider, out $unitEnumName unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse<$unitEnumName>(str.Trim()); - - 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; + return TryParseUnitInternal(str, provider, out unit); } #endregion diff --git a/UnitsNet/Scripts/Include-GenerateQuantitySourceCodeWindowsRuntimeComponent.ps1 b/UnitsNet/Scripts/Include-GenerateQuantitySourceCodeWindowsRuntimeComponent.ps1 index f3e1538611..c644f60676 100644 --- a/UnitsNet/Scripts/Include-GenerateQuantitySourceCodeWindowsRuntimeComponent.ps1 +++ b/UnitsNet/Scripts/Include-GenerateQuantitySourceCodeWindowsRuntimeComponent.ps1 @@ -133,13 +133,7 @@ namespace UnitsNet // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return QuantityParser.Parse<$quantityName, $unitEnumName>(str, provider, - delegate(string value, string unit, IFormatProvider formatProvider2) - { - double parsedValue = double.Parse(value, formatProvider2); - $unitEnumName parsedUnit = ParseUnit(unit, formatProvider2); - return From(parsedValue, parsedUnit); - }, (x, y) => From$baseUnitPluralName(x.$baseUnitPluralName + y.$baseUnitPluralName)); + return ParseInternal(str, provider); } /// @@ -153,16 +147,10 @@ namespace UnitsNet /// public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out $quantityName result) { - try - { - result = Parse(str, cultureName); - return true; - } - catch - { - result = default($quantityName); - return false; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return TryParseInternal(str, provider, out result); } /// @@ -177,35 +165,28 @@ namespace UnitsNet /// Error parsing string. public static $unitEnumName ParseUnit(string str, [CanBeNull] string cultureName) { - return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); + + return ParseUnitInternal(str, provider); } /// - /// Parse a unit string. + /// Try to parse a unit string. /// /// String to parse. Typically in the form: {number} {unit} - /// Format to use when parsing number and unit. Defaults to . + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to 's default culture. + /// The parsed unit if successful. + /// True if successful, otherwise false. /// - /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// - /// The value of 'str' cannot be null. - /// Error parsing string. - internal static $unitEnumName ParseUnit(string str, IFormatProvider provider = null) + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out $unitEnumName unit) { - if (str == null) throw new ArgumentNullException(nameof(str)); - - var unitSystem = UnitSystem.GetCached(provider); - var unit = unitSystem.Parse<$unitEnumName>(str.Trim()); - - 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; - } + // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx + IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); - return unit; + return TryParseUnitInternal(str, provider, out unit); } #endregion