@@ -623,7 +623,7 @@ public static Information From(double value, InformationUnit fromUnit)
623
623
/// </exception>
624
624
public static Information Parse ( string str )
625
625
{
626
- return ParseInternal ( str , null ) ;
626
+ return Parse ( str , null ) ;
627
627
}
628
628
629
629
/// <summary>
@@ -652,7 +652,10 @@ public static Information Parse(string str)
652
652
public static Information Parse ( string str , [ CanBeNull ] string cultureName )
653
653
{
654
654
IFormatProvider provider = GetFormatProviderFromCultureName ( cultureName ) ;
655
- return ParseInternal ( str , provider ) ;
655
+ return QuantityParser . Default . Parse < Information , InformationUnit > (
656
+ str ,
657
+ provider ,
658
+ From ) ;
656
659
}
657
660
658
661
/// <summary>
@@ -665,7 +668,7 @@ public static Information Parse(string str, [CanBeNull] string cultureName)
665
668
/// </example>
666
669
public static bool TryParse ( [ CanBeNull ] string str , out Information result )
667
670
{
668
- return TryParseInternal ( str , null , out result ) ;
671
+ return TryParse ( str , null , out result ) ;
669
672
}
670
673
671
674
/// <summary>
@@ -681,7 +684,11 @@ public static bool TryParse([CanBeNull] string str, out Information result)
681
684
public static bool TryParse ( [ CanBeNull ] string str , [ CanBeNull ] string cultureName , out Information result )
682
685
{
683
686
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 ) ;
685
692
}
686
693
687
694
/// <summary>
@@ -695,7 +702,7 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa
695
702
/// <exception cref="UnitsNetException">Error parsing string.</exception>
696
703
public static InformationUnit ParseUnit ( string str )
697
704
{
698
- return ParseUnitInternal ( str , null ) ;
705
+ return ParseUnit ( str , null ) ;
699
706
}
700
707
701
708
/// <summary>
@@ -711,12 +718,12 @@ public static InformationUnit ParseUnit(string str)
711
718
public static InformationUnit ParseUnit ( string str , [ CanBeNull ] string cultureName )
712
719
{
713
720
IFormatProvider provider = GetFormatProviderFromCultureName ( cultureName ) ;
714
- return ParseUnitInternal ( str , provider ) ;
721
+ return UnitParser . Default . Parse < InformationUnit > ( str , provider ) ;
715
722
}
716
723
717
724
public static bool TryParseUnit ( string str , out InformationUnit unit )
718
725
{
719
- return TryParseUnitInternal ( str , null , out unit ) ;
726
+ return TryParseUnit ( str , null , out unit ) ;
720
727
}
721
728
722
729
/// <summary>
@@ -732,114 +739,7 @@ public static bool TryParseUnit(string str, out InformationUnit unit)
732
739
public static bool TryParseUnit ( string str , [ CanBeNull ] string cultureName , out InformationUnit unit )
733
740
{
734
741
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 "<quantity> <unit>".
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
- /// "<quantity> <unit>". 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 "<quantity> <unit>".
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 ) ;
843
743
}
844
744
845
745
#endregion
0 commit comments