Skip to content

Commit 2c39cbf

Browse files
tmilnthorpangularsen
authored andcommitted
Implementing TryParse methods to not do try/catch. (#507)
1 parent 9a134f5 commit 2c39cbf

File tree

271 files changed

+14938
-7654
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

271 files changed

+14938
-7654
lines changed

Common/GeneratedCode/Quantities/Acceleration.Common.g.cs

+136-3
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ private double AsBaseNumericType(AccelerationUnit unit)
618618
/// </exception>
619619
public static Acceleration Parse(string str)
620620
{
621-
return Parse(str, null);
621+
return ParseInternal(str, null);
622622
}
623623

624624
/// <summary>
@@ -631,7 +631,7 @@ public static Acceleration Parse(string str)
631631
/// </example>
632632
public static bool TryParse([CanBeNull] string str, out Acceleration result)
633633
{
634-
return TryParse(str, null, out result);
634+
return TryParseInternal(str, null, out result);
635635
}
636636

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

651784
#endregion

Common/GeneratedCode/Quantities/AmountOfSubstance.Common.g.cs

+136-3
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ private double AsBaseNumericType(AmountOfSubstanceUnit unit)
640640
/// </exception>
641641
public static AmountOfSubstance Parse(string str)
642642
{
643-
return Parse(str, null);
643+
return ParseInternal(str, null);
644644
}
645645

646646
/// <summary>
@@ -653,7 +653,7 @@ public static AmountOfSubstance Parse(string str)
653653
/// </example>
654654
public static bool TryParse([CanBeNull] string str, out AmountOfSubstance result)
655655
{
656-
return TryParse(str, null, out result);
656+
return TryParseInternal(str, null, out result);
657657
}
658658

659659
/// <summary>
@@ -667,7 +667,140 @@ public static bool TryParse([CanBeNull] string str, out AmountOfSubstance result
667667
/// <exception cref="UnitsNetException">Error parsing string.</exception>
668668
public static AmountOfSubstanceUnit ParseUnit(string str)
669669
{
670-
return ParseUnit(str, (IFormatProvider)null);
670+
return ParseUnitInternal(str, null);
671+
}
672+
673+
public static bool TryParseUnit(string str, out AmountOfSubstanceUnit unit)
674+
{
675+
return TryParseUnitInternal(str, null, 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="UnitSystem.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+
internal static AmountOfSubstance ParseInternal(string str, [CanBeNull] IFormatProvider provider)
702+
{
703+
if (str == null) throw new ArgumentNullException(nameof(str));
704+
705+
provider = provider ?? UnitSystem.DefaultCulture;
706+
707+
return QuantityParser.Parse<AmountOfSubstance, AmountOfSubstanceUnit>(str, provider,
708+
delegate(string value, string unit, IFormatProvider formatProvider2)
709+
{
710+
var parsedValue = double.Parse(value, formatProvider2);
711+
var parsedUnit = ParseUnitInternal(unit, formatProvider2);
712+
return From(parsedValue, parsedUnit);
713+
}, (x, y) => From(x.Moles + y.Moles, BaseUnit));
714+
}
715+
716+
/// <summary>
717+
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
718+
/// </summary>
719+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
720+
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
721+
/// <param name="result">Resulting unit quantity if successful.</param>
722+
/// <returns>True if successful, otherwise false.</returns>
723+
/// <example>
724+
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
725+
/// </example>
726+
internal static bool TryParseInternal([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out AmountOfSubstance result)
727+
{
728+
result = default(AmountOfSubstance);
729+
730+
if(string.IsNullOrWhiteSpace(str))
731+
return false;
732+
733+
provider = provider ?? UnitSystem.DefaultCulture;
734+
735+
return QuantityParser.TryParse<AmountOfSubstance, AmountOfSubstanceUnit>(str, provider,
736+
delegate(string value, string unit, IFormatProvider formatProvider2, out AmountOfSubstance parsedAmountOfSubstance )
737+
{
738+
parsedAmountOfSubstance = default(AmountOfSubstance);
739+
740+
if(!double.TryParse(value, NumberStyles.Any, formatProvider2, out var parsedValue))
741+
return false;
742+
743+
if(!TryParseUnitInternal(unit, formatProvider2, out var parsedUnit))
744+
return false;
745+
746+
parsedAmountOfSubstance = From(parsedValue, parsedUnit);
747+
return true;
748+
}, (x, y) => From(x.Moles + y.Moles, BaseUnit), out result);
749+
}
750+
751+
/// <summary>
752+
/// Parse a unit string.
753+
/// </summary>
754+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
755+
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
756+
/// <example>
757+
/// Length.ParseUnit("m", new CultureInfo("en-US"));
758+
/// </example>
759+
/// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
760+
/// <exception cref="UnitsNetException">Error parsing string.</exception>
761+
internal static AmountOfSubstanceUnit ParseUnitInternal(string str, IFormatProvider provider = null)
762+
{
763+
if (str == null) throw new ArgumentNullException(nameof(str));
764+
765+
var unitSystem = UnitSystem.GetCached(provider);
766+
var unit = unitSystem.Parse<AmountOfSubstanceUnit>(str.Trim());
767+
768+
if (unit == AmountOfSubstanceUnit.Undefined)
769+
{
770+
var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AmountOfSubstanceUnit.");
771+
newEx.Data["input"] = str;
772+
newEx.Data["provider"] = provider?.ToString() ?? "(null)";
773+
throw newEx;
774+
}
775+
776+
return unit;
777+
}
778+
779+
/// <summary>
780+
/// Parse a unit string.
781+
/// </summary>
782+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
783+
/// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
784+
/// <param name="unit">The parsed unit if successful.</param>
785+
/// <returns>True if successful, otherwise false.</returns>
786+
/// <example>
787+
/// Length.ParseUnit("m", new CultureInfo("en-US"));
788+
/// </example>
789+
internal static bool TryParseUnitInternal(string str, IFormatProvider provider, out AmountOfSubstanceUnit unit)
790+
{
791+
unit = AmountOfSubstanceUnit.Undefined;
792+
793+
if(string.IsNullOrWhiteSpace(str))
794+
return false;
795+
796+
var unitSystem = UnitSystem.GetCached(provider);
797+
if(!unitSystem.TryParse<AmountOfSubstanceUnit>(str.Trim(), out unit))
798+
return false;
799+
800+
if(unit == AmountOfSubstanceUnit.Undefined)
801+
return false;
802+
803+
return true;
671804
}
672805

673806
#endregion

0 commit comments

Comments
 (0)