Skip to content

Commit ed944c3

Browse files
committed
Remove internal ParseUnit methods
Same as for Parse methods. DRY it up.
1 parent 2657459 commit ed944c3

File tree

7 files changed

+42
-413
lines changed

7 files changed

+42
-413
lines changed

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

Lines changed: 6 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ public static Information Parse(string str, [CanBeNull] string cultureName)
655655
return QuantityParser.Default.Parse<Information, InformationUnit>(
656656
str,
657657
provider,
658-
ParseUnitInternal,
658+
UnitParser.Default.Parse<InformationUnit>,
659659
From);
660660
}
661661

@@ -688,7 +688,7 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa
688688
return QuantityParser.Default.TryParse<Information, InformationUnit>(
689689
str,
690690
provider,
691-
TryParseUnitInternal,
691+
UnitParser.Default.TryParse<InformationUnit>,
692692
From,
693693
out result);
694694
}
@@ -704,7 +704,7 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa
704704
/// <exception cref="UnitsNetException">Error parsing string.</exception>
705705
public static InformationUnit ParseUnit(string str)
706706
{
707-
return ParseUnitInternal(str, null);
707+
return ParseUnit(str, null);
708708
}
709709

710710
/// <summary>
@@ -720,12 +720,12 @@ public static InformationUnit ParseUnit(string str)
720720
public static InformationUnit ParseUnit(string str, [CanBeNull] string cultureName)
721721
{
722722
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
723-
return ParseUnitInternal(str, provider);
723+
return UnitParser.Default.Parse<InformationUnit>(str, provider);
724724
}
725725

726726
public static bool TryParseUnit(string str, out InformationUnit unit)
727727
{
728-
return TryParseUnitInternal(str, null, out unit);
728+
return TryParseUnit(str, null, out unit);
729729
}
730730

731731
/// <summary>
@@ -741,60 +741,7 @@ public static bool TryParseUnit(string str, out InformationUnit unit)
741741
public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out InformationUnit unit)
742742
{
743743
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
744-
return TryParseUnitInternal(str, provider, out unit);
745-
}
746-
747-
/// <summary>
748-
/// Parse a unit string.
749-
/// </summary>
750-
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
751-
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" />.</param>
752-
/// <example>
753-
/// Length.ParseUnit("m", new CultureInfo("en-US"));
754-
/// </example>
755-
/// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
756-
/// <exception cref="UnitsNetException">Error parsing string.</exception>
757-
private static InformationUnit ParseUnitInternal(string str, IFormatProvider provider = null)
758-
{
759-
if (str == null) throw new ArgumentNullException(nameof(str));
760-
761-
var unit = UnitParser.Default.Parse<InformationUnit>(str.Trim(), provider);
762-
763-
if (unit == InformationUnit.Undefined)
764-
{
765-
var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized InformationUnit.");
766-
newEx.Data["input"] = str;
767-
newEx.Data["provider"] = provider?.ToString() ?? "(null)";
768-
throw newEx;
769-
}
770-
771-
return unit;
772-
}
773-
774-
/// <summary>
775-
/// Parse a unit string.
776-
/// </summary>
777-
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
778-
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" />.</param>
779-
/// <param name="unit">The parsed unit if successful.</param>
780-
/// <returns>True if successful, otherwise false.</returns>
781-
/// <example>
782-
/// Length.ParseUnit("m", new CultureInfo("en-US"));
783-
/// </example>
784-
private static bool TryParseUnitInternal(string str, IFormatProvider provider, out InformationUnit unit)
785-
{
786-
unit = InformationUnit.Undefined;
787-
788-
if(string.IsNullOrWhiteSpace(str))
789-
return false;
790-
791-
if(!UnitParser.Default.TryParse<InformationUnit>(str.Trim(), provider, out unit))
792-
return false;
793-
794-
if(unit == InformationUnit.Undefined)
795-
return false;
796-
797-
return true;
744+
return UnitParser.Default.TryParse<InformationUnit>(str, provider, out unit);
798745
}
799746

800747
#endregion

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

Lines changed: 6 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ public static Length Parse(string str, [CanBeNull] string cultureName)
595595
return QuantityParser.Default.Parse<Length, LengthUnit>(
596596
str,
597597
provider,
598-
ParseUnitInternal,
598+
UnitParser.Default.Parse<LengthUnit>,
599599
From);
600600
}
601601

@@ -628,7 +628,7 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa
628628
return QuantityParser.Default.TryParse<Length, LengthUnit>(
629629
str,
630630
provider,
631-
TryParseUnitInternal,
631+
UnitParser.Default.TryParse<LengthUnit>,
632632
From,
633633
out result);
634634
}
@@ -644,7 +644,7 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa
644644
/// <exception cref="UnitsNetException">Error parsing string.</exception>
645645
public static LengthUnit ParseUnit(string str)
646646
{
647-
return ParseUnitInternal(str, null);
647+
return ParseUnit(str, null);
648648
}
649649

650650
/// <summary>
@@ -660,12 +660,12 @@ public static LengthUnit ParseUnit(string str)
660660
public static LengthUnit ParseUnit(string str, [CanBeNull] string cultureName)
661661
{
662662
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
663-
return ParseUnitInternal(str, provider);
663+
return UnitParser.Default.Parse<LengthUnit>(str, provider);
664664
}
665665

666666
public static bool TryParseUnit(string str, out LengthUnit unit)
667667
{
668-
return TryParseUnitInternal(str, null, out unit);
668+
return TryParseUnit(str, null, out unit);
669669
}
670670

671671
/// <summary>
@@ -681,60 +681,7 @@ public static bool TryParseUnit(string str, out LengthUnit unit)
681681
public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out LengthUnit unit)
682682
{
683683
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
684-
return TryParseUnitInternal(str, provider, out unit);
685-
}
686-
687-
/// <summary>
688-
/// Parse a unit string.
689-
/// </summary>
690-
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
691-
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" />.</param>
692-
/// <example>
693-
/// Length.ParseUnit("m", new CultureInfo("en-US"));
694-
/// </example>
695-
/// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
696-
/// <exception cref="UnitsNetException">Error parsing string.</exception>
697-
private static LengthUnit ParseUnitInternal(string str, IFormatProvider provider = null)
698-
{
699-
if (str == null) throw new ArgumentNullException(nameof(str));
700-
701-
var unit = UnitParser.Default.Parse<LengthUnit>(str.Trim(), provider);
702-
703-
if (unit == LengthUnit.Undefined)
704-
{
705-
var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LengthUnit.");
706-
newEx.Data["input"] = str;
707-
newEx.Data["provider"] = provider?.ToString() ?? "(null)";
708-
throw newEx;
709-
}
710-
711-
return unit;
712-
}
713-
714-
/// <summary>
715-
/// Parse a unit string.
716-
/// </summary>
717-
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
718-
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" />.</param>
719-
/// <param name="unit">The parsed unit if successful.</param>
720-
/// <returns>True if successful, otherwise false.</returns>
721-
/// <example>
722-
/// Length.ParseUnit("m", new CultureInfo("en-US"));
723-
/// </example>
724-
private static bool TryParseUnitInternal(string str, IFormatProvider provider, out LengthUnit unit)
725-
{
726-
unit = LengthUnit.Undefined;
727-
728-
if(string.IsNullOrWhiteSpace(str))
729-
return false;
730-
731-
if(!UnitParser.Default.TryParse<LengthUnit>(str.Trim(), provider, out unit))
732-
return false;
733-
734-
if(unit == LengthUnit.Undefined)
735-
return false;
736-
737-
return true;
684+
return UnitParser.Default.TryParse<LengthUnit>(str, provider, out unit);
738685
}
739686

740687
#endregion

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

Lines changed: 6 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public static Level Parse(string str, [CanBeNull] string cultureName)
295295
return QuantityParser.Default.Parse<Level, LevelUnit>(
296296
str,
297297
provider,
298-
ParseUnitInternal,
298+
UnitParser.Default.Parse<LevelUnit>,
299299
From);
300300
}
301301

@@ -328,7 +328,7 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa
328328
return QuantityParser.Default.TryParse<Level, LevelUnit>(
329329
str,
330330
provider,
331-
TryParseUnitInternal,
331+
UnitParser.Default.TryParse<LevelUnit>,
332332
From,
333333
out result);
334334
}
@@ -344,7 +344,7 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa
344344
/// <exception cref="UnitsNetException">Error parsing string.</exception>
345345
public static LevelUnit ParseUnit(string str)
346346
{
347-
return ParseUnitInternal(str, null);
347+
return ParseUnit(str, null);
348348
}
349349

350350
/// <summary>
@@ -360,12 +360,12 @@ public static LevelUnit ParseUnit(string str)
360360
public static LevelUnit ParseUnit(string str, [CanBeNull] string cultureName)
361361
{
362362
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
363-
return ParseUnitInternal(str, provider);
363+
return UnitParser.Default.Parse<LevelUnit>(str, provider);
364364
}
365365

366366
public static bool TryParseUnit(string str, out LevelUnit unit)
367367
{
368-
return TryParseUnitInternal(str, null, out unit);
368+
return TryParseUnit(str, null, out unit);
369369
}
370370

371371
/// <summary>
@@ -381,60 +381,7 @@ public static bool TryParseUnit(string str, out LevelUnit unit)
381381
public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out LevelUnit unit)
382382
{
383383
IFormatProvider provider = GetFormatProviderFromCultureName(cultureName);
384-
return TryParseUnitInternal(str, provider, out unit);
385-
}
386-
387-
/// <summary>
388-
/// Parse a unit string.
389-
/// </summary>
390-
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
391-
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" />.</param>
392-
/// <example>
393-
/// Length.ParseUnit("m", new CultureInfo("en-US"));
394-
/// </example>
395-
/// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
396-
/// <exception cref="UnitsNetException">Error parsing string.</exception>
397-
private static LevelUnit ParseUnitInternal(string str, IFormatProvider provider = null)
398-
{
399-
if (str == null) throw new ArgumentNullException(nameof(str));
400-
401-
var unit = UnitParser.Default.Parse<LevelUnit>(str.Trim(), provider);
402-
403-
if (unit == LevelUnit.Undefined)
404-
{
405-
var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LevelUnit.");
406-
newEx.Data["input"] = str;
407-
newEx.Data["provider"] = provider?.ToString() ?? "(null)";
408-
throw newEx;
409-
}
410-
411-
return unit;
412-
}
413-
414-
/// <summary>
415-
/// Parse a unit string.
416-
/// </summary>
417-
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
418-
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" />.</param>
419-
/// <param name="unit">The parsed unit if successful.</param>
420-
/// <returns>True if successful, otherwise false.</returns>
421-
/// <example>
422-
/// Length.ParseUnit("m", new CultureInfo("en-US"));
423-
/// </example>
424-
private static bool TryParseUnitInternal(string str, IFormatProvider provider, out LevelUnit unit)
425-
{
426-
unit = LevelUnit.Undefined;
427-
428-
if(string.IsNullOrWhiteSpace(str))
429-
return false;
430-
431-
if(!UnitParser.Default.TryParse<LevelUnit>(str.Trim(), provider, out unit))
432-
return false;
433-
434-
if(unit == LevelUnit.Undefined)
435-
return false;
436-
437-
return true;
384+
return UnitParser.Default.TryParse<LevelUnit>(str, provider, out unit);
438385
}
439386

440387
#endregion

0 commit comments

Comments
 (0)