Skip to content

Try parse #507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 136 additions & 3 deletions Common/GeneratedCode/Quantities/Acceleration.Common.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ private double AsBaseNumericType(AccelerationUnit unit)
/// </exception>
public static Acceleration Parse(string str)
{
return Parse(str, null);
return ParseInternal(str, null);
}

/// <summary>
Expand All @@ -631,7 +631,7 @@ public static Acceleration Parse(string str)
/// </example>
public static bool TryParse([CanBeNull] string str, out Acceleration result)
{
return TryParse(str, null, out result);
return TryParseInternal(str, null, out result);
}

/// <summary>
Expand All @@ -645,7 +645,140 @@ public static bool TryParse([CanBeNull] string str, out Acceleration result)
/// <exception cref="UnitsNetException">Error parsing string.</exception>
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);
}

/// <summary>
/// Parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
/// </summary>
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
/// <example>
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
/// </example>
/// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
/// <exception cref="ArgumentException">
/// Expected string to have one or two pairs of quantity and unit in the format
/// "&lt;quantity&gt; &lt;unit&gt;". Eg. "5.5 m" or "1ft 2in"
/// </exception>
/// <exception cref="AmbiguousUnitParseException">
/// 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
/// <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />.
/// </exception>
/// <exception cref="UnitsNetException">
/// If anything else goes wrong, typically due to a bug or unhandled case.
/// We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
/// Units.NET exceptions from other exceptions.
/// </exception>
internal static Acceleration ParseInternal(string str, [CanBeNull] IFormatProvider provider)
{
if (str == null) throw new ArgumentNullException(nameof(str));

provider = provider ?? UnitSystem.DefaultCulture;

return QuantityParser.Parse<Acceleration, AccelerationUnit>(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));
}

/// <summary>
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
/// </summary>
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
/// <param name="result">Resulting unit quantity if successful.</param>
/// <returns>True if successful, otherwise false.</returns>
/// <example>
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
/// </example>
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<Acceleration, AccelerationUnit>(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);
}

/// <summary>
/// Parse a unit string.
/// </summary>
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
/// <example>
/// Length.ParseUnit("m", new CultureInfo("en-US"));
/// </example>
/// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
/// <exception cref="UnitsNetException">Error parsing string.</exception>
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<AccelerationUnit>(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;
}

/// <summary>
/// Parse a unit string.
/// </summary>
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
/// <param name="unit">The parsed unit if successful.</param>
/// <returns>True if successful, otherwise false.</returns>
/// <example>
/// Length.ParseUnit("m", new CultureInfo("en-US"));
/// </example>
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<AccelerationUnit>(str.Trim(), out unit))
return false;

if(unit == AccelerationUnit.Undefined)
return false;

return true;
}

#endregion
Expand Down
139 changes: 136 additions & 3 deletions Common/GeneratedCode/Quantities/AmountOfSubstance.Common.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ private double AsBaseNumericType(AmountOfSubstanceUnit unit)
/// </exception>
public static AmountOfSubstance Parse(string str)
{
return Parse(str, null);
return ParseInternal(str, null);
}

/// <summary>
Expand All @@ -653,7 +653,7 @@ public static AmountOfSubstance Parse(string str)
/// </example>
public static bool TryParse([CanBeNull] string str, out AmountOfSubstance result)
{
return TryParse(str, null, out result);
return TryParseInternal(str, null, out result);
}

/// <summary>
Expand All @@ -667,7 +667,140 @@ public static bool TryParse([CanBeNull] string str, out AmountOfSubstance result
/// <exception cref="UnitsNetException">Error parsing string.</exception>
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);
}

/// <summary>
/// Parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
/// </summary>
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
/// <example>
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
/// </example>
/// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
/// <exception cref="ArgumentException">
/// Expected string to have one or two pairs of quantity and unit in the format
/// "&lt;quantity&gt; &lt;unit&gt;". Eg. "5.5 m" or "1ft 2in"
/// </exception>
/// <exception cref="AmbiguousUnitParseException">
/// 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
/// <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />.
/// </exception>
/// <exception cref="UnitsNetException">
/// If anything else goes wrong, typically due to a bug or unhandled case.
/// We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
/// Units.NET exceptions from other exceptions.
/// </exception>
internal static AmountOfSubstance ParseInternal(string str, [CanBeNull] IFormatProvider provider)
{
if (str == null) throw new ArgumentNullException(nameof(str));

provider = provider ?? UnitSystem.DefaultCulture;

return QuantityParser.Parse<AmountOfSubstance, AmountOfSubstanceUnit>(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));
}

/// <summary>
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
/// </summary>
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
/// <param name="result">Resulting unit quantity if successful.</param>
/// <returns>True if successful, otherwise false.</returns>
/// <example>
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
/// </example>
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<AmountOfSubstance, AmountOfSubstanceUnit>(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);
}

/// <summary>
/// Parse a unit string.
/// </summary>
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
/// <example>
/// Length.ParseUnit("m", new CultureInfo("en-US"));
/// </example>
/// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
/// <exception cref="UnitsNetException">Error parsing string.</exception>
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<AmountOfSubstanceUnit>(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;
}

/// <summary>
/// Parse a unit string.
/// </summary>
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
/// <param name="unit">The parsed unit if successful.</param>
/// <returns>True if successful, otherwise false.</returns>
/// <example>
/// Length.ParseUnit("m", new CultureInfo("en-US"));
/// </example>
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<AmountOfSubstanceUnit>(str.Trim(), out unit))
return false;

if(unit == AmountOfSubstanceUnit.Undefined)
return false;

return true;
}

#endregion
Expand Down
Loading