Skip to content

Commit 7cfca07

Browse files
angularsentmilnthorp
authored andcommitted
Remove internal parse methods (#521)
* Remove redundant internal parse methods ParseInternal and TryParseInternal methods are redundant since the same logic is already in QuantityParser. * Remove internal ParseUnit methods Same as for Parse methods. DRY it up. * QuantityParser: Parse using given abbreviations cache Replace delegates for external unit parsing code with UnitParser. * Add null checking and make Trim() more explicit Trimming inline deep in the code is not easy to spot. Make it explicit, just after null checking. Fixes test that assumed ArgNullException.
1 parent c5c6e51 commit 7cfca07

10 files changed

+146
-830
lines changed

UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Information.WindowsRuntimeComponent.g.cs

Lines changed: 15 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ public static Information From(double value, InformationUnit fromUnit)
623623
/// </exception>
624624
public static Information Parse(string str)
625625
{
626-
return ParseInternal(str, null);
626+
return Parse(str, null);
627627
}
628628

629629
/// <summary>
@@ -652,7 +652,10 @@ public static Information Parse(string str)
652652
public static Information Parse(string str, [CanBeNull] string cultureName)
653653
{
654654
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
655-
return ParseInternal(str, provider);
655+
return QuantityParser.Default.Parse<Information, InformationUnit>(
656+
str,
657+
provider,
658+
From);
656659
}
657660

658661
/// <summary>
@@ -665,7 +668,7 @@ public static Information Parse(string str, [CanBeNull] string cultureName)
665668
/// </example>
666669
public static bool TryParse([CanBeNull] string str, out Information result)
667670
{
668-
return TryParseInternal(str, null, out result);
671+
return TryParse(str, null, out result);
669672
}
670673

671674
/// <summary>
@@ -681,7 +684,11 @@ public static bool TryParse([CanBeNull] string str, out Information result)
681684
public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Information result)
682685
{
683686
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
684-
return TryParseInternal(str, provider, out result);
687+
return QuantityParser.Default.TryParse<Information, InformationUnit>(
688+
str,
689+
provider,
690+
From,
691+
out result);
685692
}
686693

687694
/// <summary>
@@ -695,7 +702,7 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa
695702
/// <exception cref="UnitsNetException">Error parsing string.</exception>
696703
public static InformationUnit ParseUnit(string str)
697704
{
698-
return ParseUnitInternal(str, null);
705+
return ParseUnit(str, null);
699706
}
700707

701708
/// <summary>
@@ -711,12 +718,12 @@ public static InformationUnit ParseUnit(string str)
711718
public static InformationUnit ParseUnit(string str, [CanBeNull] string cultureName)
712719
{
713720
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
714-
return ParseUnitInternal(str, provider);
721+
return UnitParser.Default.Parse<InformationUnit>(str, provider);
715722
}
716723

717724
public static bool TryParseUnit(string str, out InformationUnit unit)
718725
{
719-
return TryParseUnitInternal(str, null, out unit);
726+
return TryParseUnit(str, null, out unit);
720727
}
721728

722729
/// <summary>
@@ -732,114 +739,7 @@ public static bool TryParseUnit(string str, out InformationUnit unit)
732739
public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out InformationUnit unit)
733740
{
734741
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
735-
return TryParseUnitInternal(str, provider, out unit);
736-
}
737-
738-
/// <summary>
739-
/// Parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
740-
/// </summary>
741-
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
742-
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" />.</param>
743-
/// <example>
744-
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
745-
/// </example>
746-
/// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
747-
/// <exception cref="ArgumentException">
748-
/// Expected string to have one or two pairs of quantity and unit in the format
749-
/// "&lt;quantity&gt; &lt;unit&gt;". Eg. "5.5 m" or "1ft 2in"
750-
/// </exception>
751-
/// <exception cref="AmbiguousUnitParseException">
752-
/// More than one unit is represented by the specified unit abbreviation.
753-
/// Example: Volume.Parse("1 cup") will throw, because it can refer to any of
754-
/// <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />.
755-
/// </exception>
756-
/// <exception cref="UnitsNetException">
757-
/// If anything else goes wrong, typically due to a bug or unhandled case.
758-
/// We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
759-
/// Units.NET exceptions from other exceptions.
760-
/// </exception>
761-
private static Information ParseInternal(string str, [CanBeNull] IFormatProvider provider)
762-
{
763-
if (str == null) throw new ArgumentNullException(nameof(str));
764-
765-
provider = provider ?? GlobalConfiguration.DefaultCulture;
766-
767-
return QuantityParser.Default.Parse<Information, InformationUnit>(str, provider, ParseUnitInternal, From);
768-
}
769-
770-
/// <summary>
771-
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
772-
/// </summary>
773-
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
774-
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" />.</param>
775-
/// <param name="result">Resulting unit quantity if successful.</param>
776-
/// <returns>True if successful, otherwise false.</returns>
777-
/// <example>
778-
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
779-
/// </example>
780-
private static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Information result)
781-
{
782-
result = default;
783-
784-
if(string.IsNullOrWhiteSpace(str))
785-
return false;
786-
787-
provider = provider ?? GlobalConfiguration.DefaultCulture;
788-
789-
return QuantityParser.Default.TryParse<Information, InformationUnit>(str, provider, TryParseUnitInternal, From, out result);
790-
}
791-
792-
/// <summary>
793-
/// Parse a unit string.
794-
/// </summary>
795-
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
796-
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" />.</param>
797-
/// <example>
798-
/// Length.ParseUnit("m", new CultureInfo("en-US"));
799-
/// </example>
800-
/// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
801-
/// <exception cref="UnitsNetException">Error parsing string.</exception>
802-
private static InformationUnit ParseUnitInternal(string str, IFormatProvider provider = null)
803-
{
804-
if (str == null) throw new ArgumentNullException(nameof(str));
805-
806-
var unit = UnitParser.Default.Parse<InformationUnit>(str.Trim(), provider);
807-
808-
if (unit == InformationUnit.Undefined)
809-
{
810-
var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized InformationUnit.");
811-
newEx.Data["input"] = str;
812-
newEx.Data["provider"] = provider?.ToString() ?? "(null)";
813-
throw newEx;
814-
}
815-
816-
return unit;
817-
}
818-
819-
/// <summary>
820-
/// Parse a unit string.
821-
/// </summary>
822-
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
823-
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" />.</param>
824-
/// <param name="unit">The parsed unit if successful.</param>
825-
/// <returns>True if successful, otherwise false.</returns>
826-
/// <example>
827-
/// Length.ParseUnit("m", new CultureInfo("en-US"));
828-
/// </example>
829-
private static bool TryParseUnitInternal(string str, IFormatProvider provider, out InformationUnit unit)
830-
{
831-
unit = InformationUnit.Undefined;
832-
833-
if(string.IsNullOrWhiteSpace(str))
834-
return false;
835-
836-
if(!UnitParser.Default.TryParse<InformationUnit>(str.Trim(), provider, out unit))
837-
return false;
838-
839-
if(unit == InformationUnit.Undefined)
840-
return false;
841-
842-
return true;
742+
return UnitParser.Default.TryParse<InformationUnit>(str, provider, out unit);
843743
}
844744

845745
#endregion

UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Length.WindowsRuntimeComponent.g.cs

Lines changed: 15 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ public static Length From(double value, LengthUnit fromUnit)
563563
/// </exception>
564564
public static Length Parse(string str)
565565
{
566-
return ParseInternal(str, null);
566+
return Parse(str, null);
567567
}
568568

569569
/// <summary>
@@ -592,7 +592,10 @@ public static Length Parse(string str)
592592
public static Length Parse(string str, [CanBeNull] string cultureName)
593593
{
594594
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
595-
return ParseInternal(str, provider);
595+
return QuantityParser.Default.Parse<Length, LengthUnit>(
596+
str,
597+
provider,
598+
From);
596599
}
597600

598601
/// <summary>
@@ -605,7 +608,7 @@ public static Length Parse(string str, [CanBeNull] string cultureName)
605608
/// </example>
606609
public static bool TryParse([CanBeNull] string str, out Length result)
607610
{
608-
return TryParseInternal(str, null, out result);
611+
return TryParse(str, null, out result);
609612
}
610613

611614
/// <summary>
@@ -621,7 +624,11 @@ public static bool TryParse([CanBeNull] string str, out Length result)
621624
public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Length result)
622625
{
623626
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
624-
return TryParseInternal(str, provider, out result);
627+
return QuantityParser.Default.TryParse<Length, LengthUnit>(
628+
str,
629+
provider,
630+
From,
631+
out result);
625632
}
626633

627634
/// <summary>
@@ -635,7 +642,7 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa
635642
/// <exception cref="UnitsNetException">Error parsing string.</exception>
636643
public static LengthUnit ParseUnit(string str)
637644
{
638-
return ParseUnitInternal(str, null);
645+
return ParseUnit(str, null);
639646
}
640647

641648
/// <summary>
@@ -651,12 +658,12 @@ public static LengthUnit ParseUnit(string str)
651658
public static LengthUnit ParseUnit(string str, [CanBeNull] string cultureName)
652659
{
653660
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
654-
return ParseUnitInternal(str, provider);
661+
return UnitParser.Default.Parse<LengthUnit>(str, provider);
655662
}
656663

657664
public static bool TryParseUnit(string str, out LengthUnit unit)
658665
{
659-
return TryParseUnitInternal(str, null, out unit);
666+
return TryParseUnit(str, null, out unit);
660667
}
661668

662669
/// <summary>
@@ -672,114 +679,7 @@ public static bool TryParseUnit(string str, out LengthUnit unit)
672679
public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out LengthUnit unit)
673680
{
674681
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
675-
return TryParseUnitInternal(str, provider, out unit);
676-
}
677-
678-
/// <summary>
679-
/// Parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
680-
/// </summary>
681-
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
682-
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" />.</param>
683-
/// <example>
684-
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
685-
/// </example>
686-
/// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
687-
/// <exception cref="ArgumentException">
688-
/// Expected string to have one or two pairs of quantity and unit in the format
689-
/// "&lt;quantity&gt; &lt;unit&gt;". Eg. "5.5 m" or "1ft 2in"
690-
/// </exception>
691-
/// <exception cref="AmbiguousUnitParseException">
692-
/// More than one unit is represented by the specified unit abbreviation.
693-
/// Example: Volume.Parse("1 cup") will throw, because it can refer to any of
694-
/// <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />.
695-
/// </exception>
696-
/// <exception cref="UnitsNetException">
697-
/// If anything else goes wrong, typically due to a bug or unhandled case.
698-
/// We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
699-
/// Units.NET exceptions from other exceptions.
700-
/// </exception>
701-
private static Length ParseInternal(string str, [CanBeNull] IFormatProvider provider)
702-
{
703-
if (str == null) throw new ArgumentNullException(nameof(str));
704-
705-
provider = provider ?? GlobalConfiguration.DefaultCulture;
706-
707-
return QuantityParser.Default.Parse<Length, LengthUnit>(str, provider, ParseUnitInternal, From);
708-
}
709-
710-
/// <summary>
711-
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
712-
/// </summary>
713-
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
714-
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" />.</param>
715-
/// <param name="result">Resulting unit quantity if successful.</param>
716-
/// <returns>True if successful, otherwise false.</returns>
717-
/// <example>
718-
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
719-
/// </example>
720-
private static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Length result)
721-
{
722-
result = default;
723-
724-
if(string.IsNullOrWhiteSpace(str))
725-
return false;
726-
727-
provider = provider ?? GlobalConfiguration.DefaultCulture;
728-
729-
return QuantityParser.Default.TryParse<Length, LengthUnit>(str, provider, TryParseUnitInternal, From, out result);
730-
}
731-
732-
/// <summary>
733-
/// Parse a unit string.
734-
/// </summary>
735-
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
736-
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" />.</param>
737-
/// <example>
738-
/// Length.ParseUnit("m", new CultureInfo("en-US"));
739-
/// </example>
740-
/// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
741-
/// <exception cref="UnitsNetException">Error parsing string.</exception>
742-
private static LengthUnit ParseUnitInternal(string str, IFormatProvider provider = null)
743-
{
744-
if (str == null) throw new ArgumentNullException(nameof(str));
745-
746-
var unit = UnitParser.Default.Parse<LengthUnit>(str.Trim(), provider);
747-
748-
if (unit == LengthUnit.Undefined)
749-
{
750-
var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LengthUnit.");
751-
newEx.Data["input"] = str;
752-
newEx.Data["provider"] = provider?.ToString() ?? "(null)";
753-
throw newEx;
754-
}
755-
756-
return unit;
757-
}
758-
759-
/// <summary>
760-
/// Parse a unit string.
761-
/// </summary>
762-
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
763-
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" />.</param>
764-
/// <param name="unit">The parsed unit if successful.</param>
765-
/// <returns>True if successful, otherwise false.</returns>
766-
/// <example>
767-
/// Length.ParseUnit("m", new CultureInfo("en-US"));
768-
/// </example>
769-
private static bool TryParseUnitInternal(string str, IFormatProvider provider, out LengthUnit unit)
770-
{
771-
unit = LengthUnit.Undefined;
772-
773-
if(string.IsNullOrWhiteSpace(str))
774-
return false;
775-
776-
if(!UnitParser.Default.TryParse<LengthUnit>(str.Trim(), provider, out unit))
777-
return false;
778-
779-
if(unit == LengthUnit.Undefined)
780-
return false;
781-
782-
return true;
682+
return UnitParser.Default.TryParse<LengthUnit>(str, provider, out unit);
783683
}
784684

785685
#endregion

0 commit comments

Comments
 (0)