Skip to content

Commit

Permalink
QuantityParser: Parse using given abbreviations cache
Browse files Browse the repository at this point in the history
Replace delegates for external unit parsing code with UnitParser.
  • Loading branch information
angularsen committed Oct 15, 2018
1 parent 49c99c7 commit 193849a
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,6 @@ public static Information Parse(string str, [CanBeNull] string cultureName)
return QuantityParser.Default.Parse<Information, InformationUnit>(
str,
provider,
UnitParser.Default.Parse<InformationUnit>,
From,
(x, y) => From(x.Bits + y.Bits, BaseUnit));
}
Expand Down Expand Up @@ -689,7 +688,6 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa
return QuantityParser.Default.TryParse<Information, InformationUnit>(
str,
provider,
UnitParser.Default.TryParse<InformationUnit>,
From,
(x, y) => From(x.Bits + y.Bits, BaseUnit), out result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,6 @@ public static Length Parse(string str, [CanBeNull] string cultureName)
return QuantityParser.Default.Parse<Length, LengthUnit>(
str,
provider,
UnitParser.Default.Parse<LengthUnit>,
From,
(x, y) => From(x.Meters + y.Meters, BaseUnit));
}
Expand Down Expand Up @@ -629,7 +628,6 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa
return QuantityParser.Default.TryParse<Length, LengthUnit>(
str,
provider,
UnitParser.Default.TryParse<LengthUnit>,
From,
(x, y) => From(x.Meters + y.Meters, BaseUnit), out result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ public static Level Parse(string str, [CanBeNull] string cultureName)
return QuantityParser.Default.Parse<Level, LevelUnit>(
str,
provider,
UnitParser.Default.Parse<LevelUnit>,
From,
(x, y) => From(x.Decibels + y.Decibels, BaseUnit));
}
Expand Down Expand Up @@ -329,7 +328,6 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureNa
return QuantityParser.Default.TryParse<Level, LevelUnit>(
str,
provider,
UnitParser.Default.TryParse<LevelUnit>,
From,
(x, y) => From(x.Decibels + y.Decibels, BaseUnit), out result);
}
Expand Down
21 changes: 8 additions & 13 deletions UnitsNet/CustomCode/QuantityParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
// ReSharper disable once CheckNamespace
namespace UnitsNet
{
internal delegate TUnitType ParseUnitDelegate<out TUnitType>(string unitString, IFormatProvider formatProvider) where TUnitType : Enum;
internal delegate bool TryParseUnitDelegate<TUnitType>(string unitString, IFormatProvider formatProvider, out TUnitType unit) where TUnitType : Enum;

#if !WINDOWS_UWP
internal delegate TQuantity QuantityFromDelegate<out TQuantity, TUnitType>(QuantityValue value, TUnitType fromUnit)
where TQuantity : IQuantity
Expand All @@ -46,12 +43,14 @@ internal delegate TQuantity QuantityFromDelegate<out TQuantity, TUnitType>(doubl
internal class QuantityParser
{
private readonly UnitAbbreviationsCache unitAbbreviationsCache;
private UnitParser _unitParser;

public static QuantityParser Default { get; }

public QuantityParser(UnitAbbreviationsCache unitAbbreviationsCache)
{
this.unitAbbreviationsCache = unitAbbreviationsCache ?? UnitAbbreviationsCache.Default;
this._unitParser = new UnitParser(this.unitAbbreviationsCache);
}

static QuantityParser()
Expand All @@ -62,14 +61,12 @@ static QuantityParser()
[SuppressMessage("ReSharper", "UseStringInterpolation")]
internal TQuantity Parse<TQuantity, TUnitType>([NotNull] string str,
[CanBeNull] IFormatProvider formatProvider,
[NotNull] ParseUnitDelegate<TUnitType> parseUnit,
[NotNull] QuantityFromDelegate<TQuantity, TUnitType> fromDelegate,
[NotNull] Func<TQuantity, TQuantity, TQuantity> add)
where TQuantity : IQuantity
where TUnitType : Enum
{
if (str == null) throw new ArgumentNullException(nameof(str));
if (parseUnit == null) throw new ArgumentNullException(nameof(parseUnit));
if (add == null) throw new ArgumentNullException(nameof(add));

var numFormat = formatProvider != null
Expand Down Expand Up @@ -101,7 +98,7 @@ internal TQuantity Parse<TQuantity, TUnitType>([NotNull] string str,
@"(and)?,?", // allow "and" & "," separators between quantities
@"(?<invalid>[a-z]*)?"); // capture invalid input

var quantities = ParseWithRegex(regexString, str, parseUnit, fromDelegate, formatProvider);
var quantities = ParseWithRegex(regexString, str, fromDelegate, formatProvider);
if (quantities.Count == 0)
{
throw new ArgumentException(
Expand All @@ -116,7 +113,7 @@ internal TQuantity Parse<TQuantity, TUnitType>([NotNull] string str,
/// Parse a string given a particular regular expression.
/// </summary>
/// <exception cref="UnitsNetException">Error parsing string.</exception>
private static List<TQuantity> ParseWithRegex<TQuantity, TUnitType>(string regexString, string str, ParseUnitDelegate<TUnitType> parseUnit,
private List<TQuantity> ParseWithRegex<TQuantity, TUnitType>(string regexString, string str,
QuantityFromDelegate<TQuantity, TUnitType> fromDelegate, IFormatProvider formatProvider = null)
where TQuantity : IQuantity
where TUnitType : Enum
Expand Down Expand Up @@ -147,7 +144,7 @@ private static List<TQuantity> ParseWithRegex<TQuantity, TUnitType>(string regex
try
{
var value = double.Parse(valueString, formatProvider);
var parsedUnit = parseUnit(unitString, formatProvider);
var parsedUnit = _unitParser.Parse<TUnitType>(unitString, formatProvider);
var quantity = fromDelegate(value, parsedUnit);

converted.Add(quantity);
Expand All @@ -173,7 +170,6 @@ private static List<TQuantity> ParseWithRegex<TQuantity, TUnitType>(string regex
[SuppressMessage("ReSharper", "UseStringInterpolation")]
internal bool TryParse<TQuantity, TUnitType>([NotNull] string str,
[CanBeNull] IFormatProvider formatProvider,
[NotNull] TryParseUnitDelegate<TUnitType> parseUnit,
[NotNull] QuantityFromDelegate<TQuantity, TUnitType> fromDelegate,
[NotNull] Func<TQuantity, TQuantity, TQuantity> add, out TQuantity value)
where TQuantity : IQuantity
Expand All @@ -182,7 +178,6 @@ internal bool TryParse<TQuantity, TUnitType>([NotNull] string str,
value = default(TQuantity);

if(string.IsNullOrWhiteSpace(str)) return false;
if(parseUnit == null) return false;
if(add == null) return false;

var numFormat = formatProvider != null
Expand Down Expand Up @@ -214,7 +209,7 @@ internal bool TryParse<TQuantity, TUnitType>([NotNull] string str,
@"(and)?,?", // allow "and" & "," separators between quantities
@"(?<invalid>[a-z]*)?"); // capture invalid input

if(!TryParseWithRegex<TQuantity, TUnitType>(regexString, str, parseUnit, fromDelegate, formatProvider, out var quantities))
if(!TryParseWithRegex<TQuantity, TUnitType>(regexString, str, fromDelegate, formatProvider, out var quantities))
return false;

value = quantities.Aggregate(add);
Expand All @@ -225,7 +220,7 @@ internal bool TryParse<TQuantity, TUnitType>([NotNull] string str,
/// Parse a string given a particular regular expression.
/// </summary>
/// <exception cref="UnitsNetException">Error parsing string.</exception>
private static bool TryParseWithRegex<TQuantity, TUnitType>(string regexString, string str, TryParseUnitDelegate<TUnitType> tryParseUnit,
private bool TryParseWithRegex<TQuantity, TUnitType>(string regexString, string str,
QuantityFromDelegate<TQuantity, TUnitType> fromDelegate, IFormatProvider formatProvider, out List<TQuantity> converted)
where TQuantity : IQuantity
where TUnitType : Enum
Expand All @@ -250,7 +245,7 @@ private static bool TryParseWithRegex<TQuantity, TUnitType>(string regexString,
if(!double.TryParse(valueString, NumberStyles.Any, formatProvider, out var value))
return false;

if(!tryParseUnit(unitString, formatProvider, out var parsedUnit))
if(!_unitParser.TryParse<TUnitType>(unitString, formatProvider, out var parsedUnit))
return false;

var quantity = fromDelegate(value, parsedUnit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,6 @@ public static Information Parse(string str, [CanBeNull] IFormatProvider provider
return QuantityParser.Default.Parse<Information, InformationUnit>(
str,
provider,
UnitParser.Default.Parse<InformationUnit>,
From,
(x, y) => From(x.Bits + y.Bits, BaseUnit));
}
Expand Down Expand Up @@ -644,7 +643,6 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider
return QuantityParser.Default.TryParse<Information, InformationUnit>(
str,
provider,
UnitParser.Default.TryParse<InformationUnit>,
From,
(x, y) => From(x.Bits + y.Bits, BaseUnit), out result);
}
Expand Down
2 changes: 0 additions & 2 deletions UnitsNet/GeneratedCode/Quantities/Length.NetFramework.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,6 @@ public static Length Parse(string str, [CanBeNull] IFormatProvider provider)
return QuantityParser.Default.Parse<Length, LengthUnit>(
str,
provider,
UnitParser.Default.Parse<LengthUnit>,
From,
(x, y) => From(x.Meters + y.Meters, BaseUnit));
}
Expand Down Expand Up @@ -588,7 +587,6 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider
return QuantityParser.Default.TryParse<Length, LengthUnit>(
str,
provider,
UnitParser.Default.TryParse<LengthUnit>,
From,
(x, y) => From(x.Meters + y.Meters, BaseUnit), out result);
}
Expand Down
2 changes: 0 additions & 2 deletions UnitsNet/GeneratedCode/Quantities/Level.NetFramework.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ public static Level Parse(string str, [CanBeNull] IFormatProvider provider)
return QuantityParser.Default.Parse<Level, LevelUnit>(
str,
provider,
UnitParser.Default.Parse<LevelUnit>,
From,
(x, y) => From(x.Decibels + y.Decibels, BaseUnit));
}
Expand Down Expand Up @@ -308,7 +307,6 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider
return QuantityParser.Default.TryParse<Level, LevelUnit>(
str,
provider,
UnitParser.Default.TryParse<LevelUnit>,
From,
(x, y) => From(x.Decibels + y.Decibels, BaseUnit), out result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,6 @@ if ($wrc) {@"
return QuantityParser.Default.Parse<$quantityName, $unitEnumName>(
str,
provider,
UnitParser.Default.Parse<$unitEnumName>,
From,
(x, y) => From(x.$baseUnitPluralName + y.$baseUnitPluralName, BaseUnit));
}
Expand Down Expand Up @@ -601,7 +600,6 @@ if ($wrc) {@"
return QuantityParser.Default.TryParse<$quantityName, $unitEnumName>(
str,
provider,
UnitParser.Default.TryParse<$unitEnumName>,
From,
(x, y) => From(x.$baseUnitPluralName + y.$baseUnitPluralName, BaseUnit), out result);
}
Expand Down

0 comments on commit 193849a

Please sign in to comment.