Skip to content

Commit 2657459

Browse files
committed
Remove redundant internal parse methods
ParseInternal and TryParseInternal methods are redundant since the same logic is already in QuantityParser.
1 parent c5c6e51 commit 2657459

File tree

8 files changed

+94
-409
lines changed

8 files changed

+94
-409
lines changed

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

Lines changed: 13 additions & 58 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,11 @@ 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+
ParseUnitInternal,
659+
From);
656660
}
657661

658662
/// <summary>
@@ -665,7 +669,7 @@ public static Information Parse(string str, [CanBeNull] string cultureName)
665669
/// </example>
666670
public static bool TryParse([CanBeNull] string str, out Information result)
667671
{
668-
return TryParseInternal(str, null, out result);
672+
return TryParse(str, null, out result);
669673
}
670674

671675
/// <summary>
@@ -681,7 +685,12 @@ public static bool TryParse([CanBeNull] string str, out Information result)
681685
public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Information result)
682686
{
683687
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
684-
return TryParseInternal(str, provider, out result);
688+
return QuantityParser.Default.TryParse<Information, InformationUnit>(
689+
str,
690+
provider,
691+
TryParseUnitInternal,
692+
From,
693+
out result);
685694
}
686695

687696
/// <summary>
@@ -735,60 +744,6 @@ public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out
735744
return TryParseUnitInternal(str, provider, out unit);
736745
}
737746

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-
792747
/// <summary>
793748
/// Parse a unit string.
794749
/// </summary>

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

Lines changed: 13 additions & 58 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,11 @@ 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+
ParseUnitInternal,
599+
From);
596600
}
597601

598602
/// <summary>
@@ -605,7 +609,7 @@ public static Length Parse(string str, [CanBeNull] string cultureName)
605609
/// </example>
606610
public static bool TryParse([CanBeNull] string str, out Length result)
607611
{
608-
return TryParseInternal(str, null, out result);
612+
return TryParse(str, null, out result);
609613
}
610614

611615
/// <summary>
@@ -621,7 +625,12 @@ public static bool TryParse([CanBeNull] string str, out Length result)
621625
public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Length result)
622626
{
623627
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
624-
return TryParseInternal(str, provider, out result);
628+
return QuantityParser.Default.TryParse<Length, LengthUnit>(
629+
str,
630+
provider,
631+
TryParseUnitInternal,
632+
From,
633+
out result);
625634
}
626635

627636
/// <summary>
@@ -675,60 +684,6 @@ public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out
675684
return TryParseUnitInternal(str, provider, out unit);
676685
}
677686

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-
732687
/// <summary>
733688
/// Parse a unit string.
734689
/// </summary>

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

Lines changed: 13 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public static Level From(double value, LevelUnit fromUnit)
263263
/// </exception>
264264
public static Level Parse(string str)
265265
{
266-
return ParseInternal(str, null);
266+
return Parse(str, null);
267267
}
268268

269269
/// <summary>
@@ -292,7 +292,11 @@ public static Level Parse(string str)
292292
public static Level Parse(string str, [CanBeNull] string cultureName)
293293
{
294294
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
295-
return ParseInternal(str, provider);
295+
return QuantityParser.Default.Parse<Level, LevelUnit>(
296+
str,
297+
provider,
298+
ParseUnitInternal,
299+
From);
296300
}
297301

298302
/// <summary>
@@ -305,7 +309,7 @@ public static Level Parse(string str, [CanBeNull] string cultureName)
305309
/// </example>
306310
public static bool TryParse([CanBeNull] string str, out Level result)
307311
{
308-
return TryParseInternal(str, null, out result);
312+
return TryParse(str, null, out result);
309313
}
310314

311315
/// <summary>
@@ -321,7 +325,12 @@ public static bool TryParse([CanBeNull] string str, out Level result)
321325
public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out Level result)
322326
{
323327
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
324-
return TryParseInternal(str, provider, out result);
328+
return QuantityParser.Default.TryParse<Level, LevelUnit>(
329+
str,
330+
provider,
331+
TryParseUnitInternal,
332+
From,
333+
out result);
325334
}
326335

327336
/// <summary>
@@ -375,60 +384,6 @@ public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out
375384
return TryParseUnitInternal(str, provider, out unit);
376385
}
377386

378-
/// <summary>
379-
/// Parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
380-
/// </summary>
381-
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
382-
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" />.</param>
383-
/// <example>
384-
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
385-
/// </example>
386-
/// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
387-
/// <exception cref="ArgumentException">
388-
/// Expected string to have one or two pairs of quantity and unit in the format
389-
/// "&lt;quantity&gt; &lt;unit&gt;". Eg. "5.5 m" or "1ft 2in"
390-
/// </exception>
391-
/// <exception cref="AmbiguousUnitParseException">
392-
/// More than one unit is represented by the specified unit abbreviation.
393-
/// Example: Volume.Parse("1 cup") will throw, because it can refer to any of
394-
/// <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />.
395-
/// </exception>
396-
/// <exception cref="UnitsNetException">
397-
/// If anything else goes wrong, typically due to a bug or unhandled case.
398-
/// We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
399-
/// Units.NET exceptions from other exceptions.
400-
/// </exception>
401-
private static Level ParseInternal(string str, [CanBeNull] IFormatProvider provider)
402-
{
403-
if (str == null) throw new ArgumentNullException(nameof(str));
404-
405-
provider = provider ?? GlobalConfiguration.DefaultCulture;
406-
407-
return QuantityParser.Default.Parse<Level, LevelUnit>(str, provider, ParseUnitInternal, From);
408-
}
409-
410-
/// <summary>
411-
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
412-
/// </summary>
413-
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
414-
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" />.</param>
415-
/// <param name="result">Resulting unit quantity if successful.</param>
416-
/// <returns>True if successful, otherwise false.</returns>
417-
/// <example>
418-
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
419-
/// </example>
420-
private static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out Level result)
421-
{
422-
result = default;
423-
424-
if(string.IsNullOrWhiteSpace(str))
425-
return false;
426-
427-
provider = provider ?? GlobalConfiguration.DefaultCulture;
428-
429-
return QuantityParser.Default.TryParse<Level, LevelUnit>(str, provider, TryParseUnitInternal, From, out result);
430-
}
431-
432387
/// <summary>
433388
/// Parse a unit string.
434389
/// </summary>

UnitsNet/CustomCode/Quantities/Length.extra.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public static Length ParseFeetInches(string str, IFormatProvider formatProvider
104104
public static bool TryParseFeetInches(string str, out Length result, IFormatProvider formatProvider = null)
105105
{
106106
// This succeeds if only feet or inches are given, not both
107-
if (TryParseInternal(str, formatProvider, out result))
107+
if (TryParse(str, formatProvider, out result))
108108
return true;
109109

110110
var quantityParser = QuantityParser.Default;
@@ -119,8 +119,8 @@ public static bool TryParseFeetInches(string str, out Length result, IFormatProv
119119

120120
var feetGroup = match.Groups["feet"];
121121
var inchesGroup = match.Groups["inches"];
122-
if (TryParseInternal(feetGroup.Value, formatProvider, out Length feet) &&
123-
TryParseInternal(inchesGroup.Value, formatProvider, out Length inches))
122+
if (TryParse(feetGroup.Value, formatProvider, out Length feet) &&
123+
TryParse(inchesGroup.Value, formatProvider, out Length inches))
124124
{
125125
result = feet + inches;
126126
return true;

0 commit comments

Comments
 (0)