Skip to content

Commit ed1d35d

Browse files
authored
✨Make QuantityParser public (#1264)
Fixes #1263 This was initially made internal while we matured its design, but it has been stable for a long time now and ready for external use. Making it public makes it possible to parse custom units, as described in the wiki. ### Changes - Make `QuantityParser` and its `Parse`, `TryParse` methods public - Add xmldoc
1 parent aa1a999 commit ed1d35d

File tree

1 file changed

+61
-8
lines changed

1 file changed

+61
-8
lines changed

UnitsNet/CustomCode/QuantityParser.cs

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,24 @@
77
using System.Globalization;
88
using System.Linq;
99
using System.Text.RegularExpressions;
10+
using UnitsNet.Units;
1011

1112
// ReSharper disable once CheckNamespace
1213
namespace UnitsNet
1314
{
14-
15-
internal delegate TQuantity QuantityFromDelegate<out TQuantity, in TUnitType>(QuantityValue value, TUnitType fromUnit)
15+
/// <summary>
16+
/// A method signature for creating a quantity given a numeric value and a strongly typed unit, for example 1.0 and <see cref="LengthUnit.Meter"/>.
17+
/// </summary>
18+
/// <typeparam name="TQuantity">The type of quantity to create, such as <see cref="Length"/>.</typeparam>
19+
/// <typeparam name="TUnitType">The type of unit enum that belongs to this quantity, such as <see cref="LengthUnit"/> for <see cref="Length"/>.</typeparam>
20+
public delegate TQuantity QuantityFromDelegate<out TQuantity, in TUnitType>(QuantityValue value, TUnitType fromUnit)
1621
where TQuantity : IQuantity
1722
where TUnitType : Enum;
1823

19-
internal class QuantityParser
24+
/// <summary>
25+
/// Parses quantities from strings, such as "1.2 kg" to <see cref="Length"/> or "100 cm" to <see cref="Mass"/>.
26+
/// </summary>
27+
public class QuantityParser
2028
{
2129
/// <summary>
2230
/// Allow integer, floating point or exponential number formats.
@@ -26,9 +34,17 @@ internal class QuantityParser
2634
private readonly UnitAbbreviationsCache _unitAbbreviationsCache;
2735
private readonly UnitParser _unitParser;
2836

37+
/// <summary>
38+
/// The default instance of <see cref="QuantityParser"/>, which uses <see cref="UnitAbbreviationsCache.Default"/> unit abbreviations.
39+
/// </summary>
2940
public static QuantityParser Default { get; }
3041

31-
public QuantityParser(UnitAbbreviationsCache? unitAbbreviationsCache)
42+
/// <summary>
43+
/// Creates an instance of <see cref="QuantityParser"/>, optionally specifying an <see cref="UnitAbbreviationsCache"/>
44+
/// with unit abbreviations to use when parsing.
45+
/// </summary>
46+
/// <param name="unitAbbreviationsCache">(Optional) The unit abbreviations cache, or specify <c>null</c> to use <see cref="UnitAbbreviationsCache.Default"/>.</param>
47+
public QuantityParser(UnitAbbreviationsCache? unitAbbreviationsCache = null)
3248
{
3349
_unitAbbreviationsCache = unitAbbreviationsCache ?? UnitAbbreviationsCache.Default;
3450
_unitParser = new UnitParser(_unitAbbreviationsCache);
@@ -39,8 +55,19 @@ static QuantityParser()
3955
Default = new QuantityParser(UnitAbbreviationsCache.Default);
4056
}
4157

58+
/// <summary>
59+
/// Parses a quantity from a string, such as "1.2 kg" to <see cref="Length"/> or "100 cm" to <see cref="Mass"/>.
60+
/// </summary>
61+
/// <param name="str">The string to parse, such as "1.2 kg".</param>
62+
/// <param name="formatProvider">The culture for looking up localized unit abbreviations for a language, and for parsing the number formatted in this culture. Defaults to <see cref="CultureInfo.CurrentCulture"/>.</param>
63+
/// <param name="fromDelegate">A function to create a quantity given a numeric value and a unit enum value.</param>
64+
/// <typeparam name="TQuantity">The type of quantity to create, such as <see cref="Length"/>.</typeparam>
65+
/// <typeparam name="TUnitType">The type of unit enum that belongs to this quantity, such as <see cref="LengthUnit"/> for <see cref="Length"/>.</typeparam>
66+
/// <returns>The parsed quantity if successful.</returns>
67+
/// <exception cref="ArgumentNullException">The string was null.</exception>
68+
/// <exception cref="FormatException">Failed to parse quantity.</exception>
4269
[SuppressMessage("ReSharper", "UseStringInterpolation")]
43-
internal TQuantity Parse<TQuantity, TUnitType>(string str,
70+
public TQuantity Parse<TQuantity, TUnitType>(string str,
4471
IFormatProvider? formatProvider,
4572
QuantityFromDelegate<TQuantity, TUnitType> fromDelegate)
4673
where TQuantity : IQuantity
@@ -61,8 +88,20 @@ internal TQuantity Parse<TQuantity, TUnitType>(string str,
6188
return ParseWithRegex(valueString, unitString, fromDelegate, formatProvider);
6289
}
6390

91+
/// <summary>
92+
/// Tries to parse a quantity from a string, such as "1.2 kg" to <see cref="Length"/> or "100 cm" to <see cref="Mass"/>.
93+
/// </summary>
94+
/// <param name="str">The string to parse, such as "1.2 kg".</param>
95+
/// <param name="formatProvider">The culture for looking up localized unit abbreviations for a language, and for parsing the number formatted in this culture. Defaults to <see cref="CultureInfo.CurrentCulture"/>.</param>
96+
/// <param name="fromDelegate">A function to create a quantity given a numeric value and a unit enum value.</param>
97+
/// <param name="result">The parsed quantity if successful, otherwise null.</param>
98+
/// <typeparam name="TQuantity">The type of quantity to create, such as <see cref="Length"/>.</typeparam>
99+
/// <typeparam name="TUnitType">The type of unit enum that belongs to this quantity, such as <see cref="LengthUnit"/> for <see cref="Length"/>.</typeparam>
100+
/// <returns>True if successful.</returns>
101+
/// <exception cref="ArgumentNullException">The string was null.</exception>
102+
/// <exception cref="FormatException">Failed to parse quantity.</exception>
64103
[SuppressMessage("ReSharper", "UseStringInterpolation")]
65-
internal bool TryParse<TQuantity, TUnitType>(string? str,
104+
public bool TryParse<TQuantity, TUnitType>(string? str,
66105
IFormatProvider? formatProvider,
67106
QuantityFromDelegate<TQuantity, TUnitType> fromDelegate,
68107
out TQuantity result)
@@ -81,10 +120,24 @@ internal bool TryParse<TQuantity, TUnitType>(string? str,
81120
}
82121

83122
/// <summary>
84-
/// Workaround for C# not allowing to pass on 'out' param from type Length to IQuantity, even though the are compatible.
123+
/// Tries to parse a quantity from a string, such as "1.2 kg" to <see cref="Length"/> or "100 cm" to <see cref="Mass"/>.
85124
/// </summary>
125+
/// <remarks>
126+
/// Similar to <see cref="TryParse{TQuantity,TUnitType}(string?,System.IFormatProvider?,UnitsNet.QuantityFromDelegate{TQuantity,TUnitType},out TQuantity)"/>,
127+
/// but returns <see cref="IQuantity"/> instead. This is workaround for C# not allowing to pass on 'out' param from type Length to IQuantity,
128+
/// even though the are compatible.
129+
/// </remarks>
130+
/// <param name="str">The string to parse, such as "1.2 kg".</param>
131+
/// <param name="formatProvider">The culture for looking up localized unit abbreviations for a language, and for parsing the number formatted in this culture. Defaults to <see cref="CultureInfo.CurrentCulture"/>.</param>
132+
/// <param name="fromDelegate">A function to create a quantity given a numeric value and a unit enum value.</param>
133+
/// <param name="result">The parsed quantity if successful, otherwise null.</param>
134+
/// <typeparam name="TQuantity">The type of quantity to create, such as <see cref="Length"/>.</typeparam>
135+
/// <typeparam name="TUnitType">The type of unit enum that belongs to this quantity, such as <see cref="LengthUnit"/> for <see cref="Length"/>.</typeparam>
136+
/// <returns>True if successful.</returns>
137+
/// <exception cref="ArgumentNullException">The string was null.</exception>
138+
/// <exception cref="FormatException">Failed to parse quantity.</exception>
86139
[SuppressMessage("ReSharper", "UseStringInterpolation")]
87-
internal bool TryParse<TQuantity, TUnitType>(string str,
140+
public bool TryParse<TQuantity, TUnitType>(string str,
88141
IFormatProvider? formatProvider,
89142
QuantityFromDelegate<TQuantity, TUnitType> fromDelegate,
90143
out IQuantity? result)

0 commit comments

Comments
 (0)