Skip to content

Remove internal parse methods #521

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 4 commits into from
Oct 15, 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
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ public static Information From(double value, InformationUnit fromUnit)
/// </exception>
public static Information Parse(string str)
{
return ParseInternal(str, null);
return Parse(str, null);
}

/// <summary>
Expand Down Expand Up @@ -652,7 +652,10 @@ public static Information Parse(string str)
public static Information Parse(string str, [CanBeNull] string cultureName)
{
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
return ParseInternal(str, provider);
return QuantityParser.Default.Parse<Information, InformationUnit>(
str,
provider,
From);
}

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

/// <summary>
Expand All @@ -681,7 +684,11 @@ public static bool TryParse([CanBeNull] string str, out Information result)
public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Information result)
{
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
return TryParseInternal(str, provider, out result);
return QuantityParser.Default.TryParse<Information, InformationUnit>(
str,
provider,
From,
out result);
}

/// <summary>
Expand All @@ -695,7 +702,7 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa
/// <exception cref="UnitsNetException">Error parsing string.</exception>
public static InformationUnit ParseUnit(string str)
{
return ParseUnitInternal(str, null);
return ParseUnit(str, null);
}

/// <summary>
Expand All @@ -711,12 +718,12 @@ public static InformationUnit ParseUnit(string str)
public static InformationUnit ParseUnit(string str, [CanBeNull] string cultureName)
{
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
return ParseUnitInternal(str, provider);
return UnitParser.Default.Parse<InformationUnit>(str, provider);
}

public static bool TryParseUnit(string str, out InformationUnit unit)
{
return TryParseUnitInternal(str, null, out unit);
return TryParseUnit(str, null, out unit);
}

/// <summary>
Expand All @@ -732,114 +739,7 @@ public static bool TryParseUnit(string str, out InformationUnit unit)
public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out InformationUnit unit)
{
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
return TryParseUnitInternal(str, provider, out unit);
}

/// <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="GlobalConfiguration.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>
private static Information ParseInternal(string str, [CanBeNull] IFormatProvider provider)
{
if (str == null) throw new ArgumentNullException(nameof(str));

provider = provider ?? GlobalConfiguration.DefaultCulture;

return QuantityParser.Default.Parse<Information, InformationUnit>(str, provider, ParseUnitInternal, From);
}

/// <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="GlobalConfiguration.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>
private static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Information result)
{
result = default;

if(string.IsNullOrWhiteSpace(str))
return false;

provider = provider ?? GlobalConfiguration.DefaultCulture;

return QuantityParser.Default.TryParse<Information, InformationUnit>(str, provider, TryParseUnitInternal, From, 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="GlobalConfiguration.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>
private static InformationUnit ParseUnitInternal(string str, IFormatProvider provider = null)
{
if (str == null) throw new ArgumentNullException(nameof(str));

var unit = UnitParser.Default.Parse<InformationUnit>(str.Trim(), provider);

if (unit == InformationUnit.Undefined)
{
var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized InformationUnit.");
newEx.Data["input"] = str;
newEx.Data["provider"] = provider?.ToString() ?? "(null)";
throw newEx;
}

return unit;
}

/// <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="GlobalConfiguration.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>
private static bool TryParseUnitInternal(string str, IFormatProvider provider, out InformationUnit unit)
{
unit = InformationUnit.Undefined;

if(string.IsNullOrWhiteSpace(str))
return false;

if(!UnitParser.Default.TryParse<InformationUnit>(str.Trim(), provider, out unit))
return false;

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

return true;
return UnitParser.Default.TryParse<InformationUnit>(str, provider, out unit);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ public static Length From(double value, LengthUnit fromUnit)
/// </exception>
public static Length Parse(string str)
{
return ParseInternal(str, null);
return Parse(str, null);
}

/// <summary>
Expand Down Expand Up @@ -592,7 +592,10 @@ public static Length Parse(string str)
public static Length Parse(string str, [CanBeNull] string cultureName)
{
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
return ParseInternal(str, provider);
return QuantityParser.Default.Parse<Length, LengthUnit>(
str,
provider,
From);
}

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

/// <summary>
Expand All @@ -621,7 +624,11 @@ public static bool TryParse([CanBeNull] string str, out Length result)
public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Length result)
{
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
return TryParseInternal(str, provider, out result);
return QuantityParser.Default.TryParse<Length, LengthUnit>(
str,
provider,
From,
out result);
}

/// <summary>
Expand All @@ -635,7 +642,7 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa
/// <exception cref="UnitsNetException">Error parsing string.</exception>
public static LengthUnit ParseUnit(string str)
{
return ParseUnitInternal(str, null);
return ParseUnit(str, null);
}

/// <summary>
Expand All @@ -651,12 +658,12 @@ public static LengthUnit ParseUnit(string str)
public static LengthUnit ParseUnit(string str, [CanBeNull] string cultureName)
{
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
return ParseUnitInternal(str, provider);
return UnitParser.Default.Parse<LengthUnit>(str, provider);
}

public static bool TryParseUnit(string str, out LengthUnit unit)
{
return TryParseUnitInternal(str, null, out unit);
return TryParseUnit(str, null, out unit);
}

/// <summary>
Expand All @@ -672,114 +679,7 @@ public static bool TryParseUnit(string str, out LengthUnit unit)
public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out LengthUnit unit)
{
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
return TryParseUnitInternal(str, provider, out unit);
}

/// <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="GlobalConfiguration.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>
private static Length ParseInternal(string str, [CanBeNull] IFormatProvider provider)
{
if (str == null) throw new ArgumentNullException(nameof(str));

provider = provider ?? GlobalConfiguration.DefaultCulture;

return QuantityParser.Default.Parse<Length, LengthUnit>(str, provider, ParseUnitInternal, From);
}

/// <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="GlobalConfiguration.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>
private static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Length result)
{
result = default;

if(string.IsNullOrWhiteSpace(str))
return false;

provider = provider ?? GlobalConfiguration.DefaultCulture;

return QuantityParser.Default.TryParse<Length, LengthUnit>(str, provider, TryParseUnitInternal, From, 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="GlobalConfiguration.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>
private static LengthUnit ParseUnitInternal(string str, IFormatProvider provider = null)
{
if (str == null) throw new ArgumentNullException(nameof(str));

var unit = UnitParser.Default.Parse<LengthUnit>(str.Trim(), provider);

if (unit == LengthUnit.Undefined)
{
var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LengthUnit.");
newEx.Data["input"] = str;
newEx.Data["provider"] = provider?.ToString() ?? "(null)";
throw newEx;
}

return unit;
}

/// <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="GlobalConfiguration.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>
private static bool TryParseUnitInternal(string str, IFormatProvider provider, out LengthUnit unit)
{
unit = LengthUnit.Undefined;

if(string.IsNullOrWhiteSpace(str))
return false;

if(!UnitParser.Default.TryParse<LengthUnit>(str.Trim(), provider, out unit))
return false;

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

return true;
return UnitParser.Default.TryParse<LengthUnit>(str, provider, out unit);
}

#endregion
Expand Down
Loading