From 2ff5f329ac96fd760051b0e9829497ea9247ab3a Mon Sep 17 00:00:00 2001 From: Maher Kassim Date: Mon, 18 May 2015 11:31:21 -0400 Subject: [PATCH 1/9] Updated parsing to allow multiple quantities (eg. 1ft 1in) --- UnitsNet.Tests/CustomCode/ParseTests.cs | 10 ++++ .../Include-GenerateUnitClassSourceCode.ps1 | 58 +++++++++++-------- 2 files changed, 43 insertions(+), 25 deletions(-) diff --git a/UnitsNet.Tests/CustomCode/ParseTests.cs b/UnitsNet.Tests/CustomCode/ParseTests.cs index 22a662733b..1415a18b94 100644 --- a/UnitsNet.Tests/CustomCode/ParseTests.cs +++ b/UnitsNet.Tests/CustomCode/ParseTests.cs @@ -52,6 +52,16 @@ public double ParseLengthToMetersUsEnglish(string s) return Length.Parse(s, usEnglish).Meters; } + [TestCase("1 ft 1 in", Result = 13)] + [TestCase("1ft 1in", Result = 13)] + [TestCase("1ft1in", Result = 13)] + public double ParseImperialLengthInchesUsEnglish(string s) + { + var usEnglish = CultureInfo.GetCultureInfo("en-US"); + + return Length.Parse(s, usEnglish).Inches; + } + /// Error parsing string. [TestCase("5.5 m", Result = 5.5)] [TestCase("500 005 m", Result = 500005)] diff --git a/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 b/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 index b2346a374a..a88bdb8870 100644 --- a/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 +++ b/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 @@ -33,6 +33,7 @@ function GenerateUnitClassSourceCode($unitClass) // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -265,15 +266,15 @@ namespace UnitsNet #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static $className Parse(string str, IFormatProvider formatProvider = null) @@ -287,41 +288,48 @@ namespace UnitsNet var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List<$className>(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + $unitEnumName unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - $unitEnumName unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// From 9495043b9469d00deaba1e2b6a5205b14b4546fd Mon Sep 17 00:00:00 2001 From: Maher Kassim Date: Mon, 18 May 2015 11:31:50 -0400 Subject: [PATCH 2/9] Generated Code for Updated parsing to allow multiple quantities --- .../UnitClasses/Acceleration.g.cs | 58 +++++++++++-------- .../UnitClasses/AmplitudeRatio.g.cs | 58 +++++++++++-------- UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs | 58 +++++++++++-------- UnitsNet/GeneratedCode/UnitClasses/Area.g.cs | 58 +++++++++++-------- .../GeneratedCode/UnitClasses/Density.g.cs | 58 +++++++++++-------- .../GeneratedCode/UnitClasses/Duration.g.cs | 58 +++++++++++-------- .../UnitClasses/ElectricCurrent.g.cs | 58 +++++++++++-------- .../UnitClasses/ElectricPotential.g.cs | 58 +++++++++++-------- .../UnitClasses/ElectricResistance.g.cs | 58 +++++++++++-------- .../GeneratedCode/UnitClasses/Energy.g.cs | 58 +++++++++++-------- UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs | 58 +++++++++++-------- UnitsNet/GeneratedCode/UnitClasses/Force.g.cs | 58 +++++++++++-------- .../GeneratedCode/UnitClasses/Frequency.g.cs | 58 +++++++++++-------- .../UnitClasses/Information.g.cs | 58 +++++++++++-------- .../UnitClasses/KinematicViscosity.g.cs | 58 +++++++++++-------- .../GeneratedCode/UnitClasses/Length.g.cs | 58 +++++++++++-------- UnitsNet/GeneratedCode/UnitClasses/Level.g.cs | 58 +++++++++++-------- UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs | 58 +++++++++++-------- UnitsNet/GeneratedCode/UnitClasses/Power.g.cs | 58 +++++++++++-------- .../GeneratedCode/UnitClasses/PowerRatio.g.cs | 58 +++++++++++-------- .../GeneratedCode/UnitClasses/Pressure.g.cs | 58 +++++++++++-------- UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs | 58 +++++++++++-------- .../UnitClasses/RotationalSpeed.g.cs | 58 +++++++++++-------- .../UnitClasses/SpecificWeight.g.cs | 58 +++++++++++-------- UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs | 58 +++++++++++-------- .../UnitClasses/Temperature.g.cs | 58 +++++++++++-------- .../GeneratedCode/UnitClasses/Torque.g.cs | 58 +++++++++++-------- .../GeneratedCode/UnitClasses/Volume.g.cs | 58 +++++++++++-------- 28 files changed, 924 insertions(+), 700 deletions(-) diff --git a/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs index 13a1c39159..2c3421f11d 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -232,15 +233,15 @@ public double As(AccelerationUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static Acceleration Parse(string str, IFormatProvider formatProvider = null) @@ -254,41 +255,48 @@ public static Acceleration Parse(string str, IFormatProvider formatProvider = nu var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + AccelerationUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - AccelerationUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs b/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs index da92e1910d..a6eff82303 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -280,15 +281,15 @@ public double As(AmplitudeRatioUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = null) @@ -302,41 +303,48 @@ public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + AmplitudeRatioUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - AmplitudeRatioUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs index 36e0c9e2dd..c1e73c9675 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -272,15 +273,15 @@ public double As(AngleUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static Angle Parse(string str, IFormatProvider formatProvider = null) @@ -294,41 +295,48 @@ public static Angle Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + AngleUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - AngleUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs index ffc2e3b545..f46f2beef2 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -392,15 +393,15 @@ public double As(AreaUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static Area Parse(string str, IFormatProvider formatProvider = null) @@ -414,41 +415,48 @@ public static Area Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + AreaUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - AreaUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs index 0693da3b03..47762c8389 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -412,15 +413,15 @@ public double As(DensityUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static Density Parse(string str, IFormatProvider formatProvider = null) @@ -434,41 +435,48 @@ public static Density Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + DensityUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - DensityUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs index 3f44d89f11..b9cdbae3e6 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -412,15 +413,15 @@ public double As(DurationUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static Duration Parse(string str, IFormatProvider formatProvider = null) @@ -434,41 +435,48 @@ public static Duration Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + DurationUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - DurationUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs b/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs index 99328f11f4..d573422710 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -332,15 +333,15 @@ public double As(ElectricCurrentUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static ElectricCurrent Parse(string str, IFormatProvider formatProvider = null) @@ -354,41 +355,48 @@ public static ElectricCurrent Parse(string str, IFormatProvider formatProvider = var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + ElectricCurrentUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - ElectricCurrentUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs b/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs index 54ead91545..743b9302f3 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -312,15 +313,15 @@ public double As(ElectricPotentialUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static ElectricPotential Parse(string str, IFormatProvider formatProvider = null) @@ -334,41 +335,48 @@ public static ElectricPotential Parse(string str, IFormatProvider formatProvider var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + ElectricPotentialUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - ElectricPotentialUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs b/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs index 377bf7cff9..57b53bbbec 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -272,15 +273,15 @@ public double As(ElectricResistanceUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static ElectricResistance Parse(string str, IFormatProvider formatProvider = null) @@ -294,41 +295,48 @@ public static ElectricResistance Parse(string str, IFormatProvider formatProvide var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + ElectricResistanceUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - ElectricResistanceUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs index 269a18222e..21ef714734 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -272,15 +273,15 @@ public double As(EnergyUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static Energy Parse(string str, IFormatProvider formatProvider = null) @@ -294,41 +295,48 @@ public static Energy Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + EnergyUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - EnergyUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs index 54b238db62..de8d31f6ba 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -292,15 +293,15 @@ public double As(FlowUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static Flow Parse(string str, IFormatProvider formatProvider = null) @@ -314,41 +315,48 @@ public static Flow Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + FlowUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - FlowUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs index 8d18db0b1c..19b401680d 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -372,15 +373,15 @@ public double As(ForceUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static Force Parse(string str, IFormatProvider formatProvider = null) @@ -394,41 +395,48 @@ public static Force Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + ForceUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - ForceUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs index 10dfa22e23..63741b5739 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -312,15 +313,15 @@ public double As(FrequencyUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static Frequency Parse(string str, IFormatProvider formatProvider = null) @@ -334,41 +335,48 @@ public static Frequency Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + FrequencyUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - FrequencyUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs index ea87e1b325..213ae65320 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -732,15 +733,15 @@ public double As(InformationUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static Information Parse(string str, IFormatProvider formatProvider = null) @@ -754,41 +755,48 @@ public static Information Parse(string str, IFormatProvider formatProvider = nul var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + InformationUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - InformationUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs b/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs index 2172ced19c..589c05ab4a 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -372,15 +373,15 @@ public double As(KinematicViscosityUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static KinematicViscosity Parse(string str, IFormatProvider formatProvider = null) @@ -394,41 +395,48 @@ public static KinematicViscosity Parse(string str, IFormatProvider formatProvide var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + KinematicViscosityUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - KinematicViscosityUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs index f2256f0fff..83e76d5b0d 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -472,15 +473,15 @@ public double As(LengthUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static Length Parse(string str, IFormatProvider formatProvider = null) @@ -494,41 +495,48 @@ public static Length Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + LengthUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - LengthUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs index cf5d3a2df1..bc234bc122 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -260,15 +261,15 @@ public double As(LevelUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static Level Parse(string str, IFormatProvider formatProvider = null) @@ -282,41 +283,48 @@ public static Level Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + LevelUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - LevelUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs index b653b3bcbd..2c42dcc776 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -552,15 +553,15 @@ public double As(MassUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static Mass Parse(string str, IFormatProvider formatProvider = null) @@ -574,41 +575,48 @@ public static Mass Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + MassUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - MassUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs index 11e88ee66c..355577e939 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -532,15 +533,15 @@ public double As(PowerUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static Power Parse(string str, IFormatProvider formatProvider = null) @@ -554,41 +555,48 @@ public static Power Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + PowerUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - PowerUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs b/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs index 9381ad71ad..b5db71191c 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -260,15 +261,15 @@ public double As(PowerRatioUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static PowerRatio Parse(string str, IFormatProvider formatProvider = null) @@ -282,41 +283,48 @@ public static PowerRatio Parse(string str, IFormatProvider formatProvider = null var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + PowerRatioUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - PowerRatioUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs index 2c0030051f..01b250717c 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -872,15 +873,15 @@ public double As(PressureUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static Pressure Parse(string str, IFormatProvider formatProvider = null) @@ -894,41 +895,48 @@ public static Pressure Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + PressureUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - PressureUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs index d48d2184ad..b580a889d3 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -332,15 +333,15 @@ public double As(RatioUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static Ratio Parse(string str, IFormatProvider formatProvider = null) @@ -354,41 +355,48 @@ public static Ratio Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + RatioUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - RatioUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs b/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs index f8e8be0a1d..9d8abc1fca 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -252,15 +253,15 @@ public double As(RotationalSpeedUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static RotationalSpeed Parse(string str, IFormatProvider formatProvider = null) @@ -274,41 +275,48 @@ public static RotationalSpeed Parse(string str, IFormatProvider formatProvider = var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + RotationalSpeedUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - RotationalSpeedUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs b/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs index 78651275a2..57e0deaa65 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -532,15 +533,15 @@ public double As(SpecificWeightUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static SpecificWeight Parse(string str, IFormatProvider formatProvider = null) @@ -554,41 +555,48 @@ public static SpecificWeight Parse(string str, IFormatProvider formatProvider = var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + SpecificWeightUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - SpecificWeightUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs index 4086050605..822d7ede08 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -332,15 +333,15 @@ public double As(SpeedUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static Speed Parse(string str, IFormatProvider formatProvider = null) @@ -354,41 +355,48 @@ public static Speed Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + SpeedUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - SpeedUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs index 0f71d7a087..74e4297275 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -372,15 +373,15 @@ public double As(TemperatureUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static Temperature Parse(string str, IFormatProvider formatProvider = null) @@ -394,41 +395,48 @@ public static Temperature Parse(string str, IFormatProvider formatProvider = nul var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + TemperatureUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - TemperatureUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs index 9650533e82..a822c55d3a 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -532,15 +533,15 @@ public double As(TorqueUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static Torque Parse(string str, IFormatProvider formatProvider = null) @@ -554,41 +555,48 @@ public static Torque Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + TorqueUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - TorqueUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs index 4827012d0a..2e68e21c67 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.Linq; @@ -612,15 +613,15 @@ public double As(VolumeUnit unit) #region Parsing /// - /// Parse a string of the format "<quantity> <unit>". + /// Parse a string with at least one quantity of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected 2 words. Input string needs to be in the format "<quantity> <unit - /// >". + /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format + /// "<quantity> <unit>". /// /// Error parsing string. public static Volume Parse(string str, IFormatProvider formatProvider = null) @@ -634,41 +635,48 @@ public static Volume Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format("(?[-+]?{0}{1}{2}{3}", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str.Trim()).Groups; + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); - var valueString = groups["value"].Value; - var unitString = groups["unit"].Value; + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + try + { + VolumeUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch (Exception e) + { + var newEx = new UnitsNetException("Error parsing string.", e); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } - if (valueString == "" || unitString == "") + if(converted.Count == 0) // No valid matches { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); + "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } - - try - { - VolumeUnit unit = ParseUnit(unitString, formatProvider); - double value = double.Parse(valueString, formatProvider); - - return From(value, unit); - } - catch (Exception e) - { - var newEx = new UnitsNetException("Error parsing string.", e); - newEx.Data["input"] = str; - newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw newEx; - } + return converted.Aggregate((x, y) => x + y); } /// From 2cb8e04f2969c6fb9c2c44e78f598d0ad916170b Mon Sep 17 00:00:00 2001 From: Maher Kassim Date: Mon, 18 May 2015 15:29:30 -0400 Subject: [PATCH 3/9] Updated parsing, Added ft/in symbols, and minor formatting --- UnitsNet.Tests/CustomCode/ParseTests.cs | 2 +- .../Include-GenerateUnitClassSourceCode.ps1 | 16 +++++++++------- UnitsNet/Scripts/UnitDefinitions/Length.json | 4 ++-- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/UnitsNet.Tests/CustomCode/ParseTests.cs b/UnitsNet.Tests/CustomCode/ParseTests.cs index 1415a18b94..a255862d32 100644 --- a/UnitsNet.Tests/CustomCode/ParseTests.cs +++ b/UnitsNet.Tests/CustomCode/ParseTests.cs @@ -54,7 +54,7 @@ public double ParseLengthToMetersUsEnglish(string s) [TestCase("1 ft 1 in", Result = 13)] [TestCase("1ft 1in", Result = 13)] - [TestCase("1ft1in", Result = 13)] + [TestCase("1\' 1\"", Result = 13)] public double ParseImperialLengthInchesUsEnglish(string s) { var usEnglish = CultureInfo.GetCultureInfo("en-US"); diff --git a/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 b/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 index a88bdb8870..40672e4f17 100644 --- a/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 +++ b/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 @@ -266,15 +266,15 @@ namespace UnitsNet #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static $className Parse(string str, IFormatProvider formatProvider = null) @@ -288,11 +288,12 @@ namespace UnitsNet var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -321,10 +322,11 @@ namespace UnitsNet } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/Scripts/UnitDefinitions/Length.json b/UnitsNet/Scripts/UnitDefinitions/Length.json index 72e66a4111..9ad60c4a0b 100644 --- a/UnitsNet/Scripts/UnitDefinitions/Length.json +++ b/UnitsNet/Scripts/UnitDefinitions/Length.json @@ -61,7 +61,7 @@ "Localization": [ { "Culture": "en-US", - "Abbreviations": ["ft"] + "Abbreviations": ["ft","\\\'"] }, { "Culture": "ru-RU", @@ -77,7 +77,7 @@ "Localization": [ { "Culture": "en-US", - "Abbreviations": ["in"] + "Abbreviations": ["in","\\\""] }, { "Culture": "ru-RU", From 42fd425f0c4096a5d6483bbfd404fa225c2dbe16 Mon Sep 17 00:00:00 2001 From: Maher Kassim Date: Mon, 18 May 2015 15:29:44 -0400 Subject: [PATCH 4/9] Generated Code for Updated parsing, Added ft/in symbols, and minor formatting --- .../GeneratedCode/UnitClasses/Acceleration.g.cs | 16 +++++++++------- .../UnitClasses/AmplitudeRatio.g.cs | 16 +++++++++------- UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs | 16 +++++++++------- UnitsNet/GeneratedCode/UnitClasses/Area.g.cs | 16 +++++++++------- UnitsNet/GeneratedCode/UnitClasses/Density.g.cs | 16 +++++++++------- UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs | 16 +++++++++------- .../UnitClasses/ElectricCurrent.g.cs | 16 +++++++++------- .../UnitClasses/ElectricPotential.g.cs | 16 +++++++++------- .../UnitClasses/ElectricResistance.g.cs | 16 +++++++++------- UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs | 16 +++++++++------- UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs | 16 +++++++++------- UnitsNet/GeneratedCode/UnitClasses/Force.g.cs | 16 +++++++++------- .../GeneratedCode/UnitClasses/Frequency.g.cs | 16 +++++++++------- .../GeneratedCode/UnitClasses/Information.g.cs | 16 +++++++++------- .../UnitClasses/KinematicViscosity.g.cs | 16 +++++++++------- UnitsNet/GeneratedCode/UnitClasses/Length.g.cs | 16 +++++++++------- UnitsNet/GeneratedCode/UnitClasses/Level.g.cs | 16 +++++++++------- UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs | 16 +++++++++------- UnitsNet/GeneratedCode/UnitClasses/Power.g.cs | 16 +++++++++------- .../GeneratedCode/UnitClasses/PowerRatio.g.cs | 16 +++++++++------- UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs | 16 +++++++++------- UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs | 16 +++++++++------- .../UnitClasses/RotationalSpeed.g.cs | 16 +++++++++------- .../UnitClasses/SpecificWeight.g.cs | 16 +++++++++------- UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs | 16 +++++++++------- .../GeneratedCode/UnitClasses/Temperature.g.cs | 16 +++++++++------- UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs | 16 +++++++++------- UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs | 16 +++++++++------- UnitsNet/GeneratedCode/UnitSystem.Default.g.cs | 4 ++-- 29 files changed, 254 insertions(+), 198 deletions(-) diff --git a/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs index 2c3421f11d..5e6078fab0 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs @@ -233,15 +233,15 @@ public double As(AccelerationUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static Acceleration Parse(string str, IFormatProvider formatProvider = null) @@ -255,11 +255,12 @@ public static Acceleration Parse(string str, IFormatProvider formatProvider = nu var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -288,10 +289,11 @@ public static Acceleration Parse(string str, IFormatProvider formatProvider = nu } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs b/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs index a6eff82303..2645da7cc9 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs @@ -281,15 +281,15 @@ public double As(AmplitudeRatioUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = null) @@ -303,11 +303,12 @@ public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -336,10 +337,11 @@ public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs index c1e73c9675..1902ef4c7c 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs @@ -273,15 +273,15 @@ public double As(AngleUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static Angle Parse(string str, IFormatProvider formatProvider = null) @@ -295,11 +295,12 @@ public static Angle Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -328,10 +329,11 @@ public static Angle Parse(string str, IFormatProvider formatProvider = null) } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs index f46f2beef2..735f9e15f9 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs @@ -393,15 +393,15 @@ public double As(AreaUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static Area Parse(string str, IFormatProvider formatProvider = null) @@ -415,11 +415,12 @@ public static Area Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -448,10 +449,11 @@ public static Area Parse(string str, IFormatProvider formatProvider = null) } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs index 47762c8389..315463c97d 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs @@ -413,15 +413,15 @@ public double As(DensityUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static Density Parse(string str, IFormatProvider formatProvider = null) @@ -435,11 +435,12 @@ public static Density Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -468,10 +469,11 @@ public static Density Parse(string str, IFormatProvider formatProvider = null) } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs index b9cdbae3e6..3fc9f28faf 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs @@ -413,15 +413,15 @@ public double As(DurationUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static Duration Parse(string str, IFormatProvider formatProvider = null) @@ -435,11 +435,12 @@ public static Duration Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -468,10 +469,11 @@ public static Duration Parse(string str, IFormatProvider formatProvider = null) } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs b/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs index d573422710..fd5fb8edaa 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs @@ -333,15 +333,15 @@ public double As(ElectricCurrentUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static ElectricCurrent Parse(string str, IFormatProvider formatProvider = null) @@ -355,11 +355,12 @@ public static ElectricCurrent Parse(string str, IFormatProvider formatProvider = var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -388,10 +389,11 @@ public static ElectricCurrent Parse(string str, IFormatProvider formatProvider = } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs b/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs index 743b9302f3..770d5455f8 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs @@ -313,15 +313,15 @@ public double As(ElectricPotentialUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static ElectricPotential Parse(string str, IFormatProvider formatProvider = null) @@ -335,11 +335,12 @@ public static ElectricPotential Parse(string str, IFormatProvider formatProvider var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -368,10 +369,11 @@ public static ElectricPotential Parse(string str, IFormatProvider formatProvider } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs b/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs index 57b53bbbec..9dff668352 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs @@ -273,15 +273,15 @@ public double As(ElectricResistanceUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static ElectricResistance Parse(string str, IFormatProvider formatProvider = null) @@ -295,11 +295,12 @@ public static ElectricResistance Parse(string str, IFormatProvider formatProvide var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -328,10 +329,11 @@ public static ElectricResistance Parse(string str, IFormatProvider formatProvide } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs index 21ef714734..c8600eae8a 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs @@ -273,15 +273,15 @@ public double As(EnergyUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static Energy Parse(string str, IFormatProvider formatProvider = null) @@ -295,11 +295,12 @@ public static Energy Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -328,10 +329,11 @@ public static Energy Parse(string str, IFormatProvider formatProvider = null) } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs index de8d31f6ba..54d9e3d8d9 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs @@ -293,15 +293,15 @@ public double As(FlowUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static Flow Parse(string str, IFormatProvider formatProvider = null) @@ -315,11 +315,12 @@ public static Flow Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -348,10 +349,11 @@ public static Flow Parse(string str, IFormatProvider formatProvider = null) } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs index 19b401680d..9e0b69b128 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs @@ -373,15 +373,15 @@ public double As(ForceUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static Force Parse(string str, IFormatProvider formatProvider = null) @@ -395,11 +395,12 @@ public static Force Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -428,10 +429,11 @@ public static Force Parse(string str, IFormatProvider formatProvider = null) } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs index 63741b5739..91e9391910 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs @@ -313,15 +313,15 @@ public double As(FrequencyUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static Frequency Parse(string str, IFormatProvider formatProvider = null) @@ -335,11 +335,12 @@ public static Frequency Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -368,10 +369,11 @@ public static Frequency Parse(string str, IFormatProvider formatProvider = null) } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs index 213ae65320..6b76903b01 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs @@ -733,15 +733,15 @@ public double As(InformationUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static Information Parse(string str, IFormatProvider formatProvider = null) @@ -755,11 +755,12 @@ public static Information Parse(string str, IFormatProvider formatProvider = nul var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -788,10 +789,11 @@ public static Information Parse(string str, IFormatProvider formatProvider = nul } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs b/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs index 589c05ab4a..da167e8861 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs @@ -373,15 +373,15 @@ public double As(KinematicViscosityUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static KinematicViscosity Parse(string str, IFormatProvider formatProvider = null) @@ -395,11 +395,12 @@ public static KinematicViscosity Parse(string str, IFormatProvider formatProvide var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -428,10 +429,11 @@ public static KinematicViscosity Parse(string str, IFormatProvider formatProvide } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs index 83e76d5b0d..34d97294a8 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs @@ -473,15 +473,15 @@ public double As(LengthUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static Length Parse(string str, IFormatProvider formatProvider = null) @@ -495,11 +495,12 @@ public static Length Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -528,10 +529,11 @@ public static Length Parse(string str, IFormatProvider formatProvider = null) } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs index bc234bc122..bc83de8188 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs @@ -261,15 +261,15 @@ public double As(LevelUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static Level Parse(string str, IFormatProvider formatProvider = null) @@ -283,11 +283,12 @@ public static Level Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -316,10 +317,11 @@ public static Level Parse(string str, IFormatProvider formatProvider = null) } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs index 2c42dcc776..010aa90790 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs @@ -553,15 +553,15 @@ public double As(MassUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static Mass Parse(string str, IFormatProvider formatProvider = null) @@ -575,11 +575,12 @@ public static Mass Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -608,10 +609,11 @@ public static Mass Parse(string str, IFormatProvider formatProvider = null) } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs index 355577e939..aac79c3797 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs @@ -533,15 +533,15 @@ public double As(PowerUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static Power Parse(string str, IFormatProvider formatProvider = null) @@ -555,11 +555,12 @@ public static Power Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -588,10 +589,11 @@ public static Power Parse(string str, IFormatProvider formatProvider = null) } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs b/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs index b5db71191c..71bf968ed2 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs @@ -261,15 +261,15 @@ public double As(PowerRatioUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static PowerRatio Parse(string str, IFormatProvider formatProvider = null) @@ -283,11 +283,12 @@ public static PowerRatio Parse(string str, IFormatProvider formatProvider = null var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -316,10 +317,11 @@ public static PowerRatio Parse(string str, IFormatProvider formatProvider = null } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs index 01b250717c..6680b34bf2 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs @@ -873,15 +873,15 @@ public double As(PressureUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static Pressure Parse(string str, IFormatProvider formatProvider = null) @@ -895,11 +895,12 @@ public static Pressure Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -928,10 +929,11 @@ public static Pressure Parse(string str, IFormatProvider formatProvider = null) } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs index b580a889d3..51ac6e4922 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs @@ -333,15 +333,15 @@ public double As(RatioUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static Ratio Parse(string str, IFormatProvider formatProvider = null) @@ -355,11 +355,12 @@ public static Ratio Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -388,10 +389,11 @@ public static Ratio Parse(string str, IFormatProvider formatProvider = null) } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs b/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs index 9d8abc1fca..5686544412 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs @@ -253,15 +253,15 @@ public double As(RotationalSpeedUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static RotationalSpeed Parse(string str, IFormatProvider formatProvider = null) @@ -275,11 +275,12 @@ public static RotationalSpeed Parse(string str, IFormatProvider formatProvider = var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -308,10 +309,11 @@ public static RotationalSpeed Parse(string str, IFormatProvider formatProvider = } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs b/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs index 57e0deaa65..e3bff4b70d 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs @@ -533,15 +533,15 @@ public double As(SpecificWeightUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static SpecificWeight Parse(string str, IFormatProvider formatProvider = null) @@ -555,11 +555,12 @@ public static SpecificWeight Parse(string str, IFormatProvider formatProvider = var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -588,10 +589,11 @@ public static SpecificWeight Parse(string str, IFormatProvider formatProvider = } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs index 822d7ede08..7e71601973 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs @@ -333,15 +333,15 @@ public double As(SpeedUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static Speed Parse(string str, IFormatProvider formatProvider = null) @@ -355,11 +355,12 @@ public static Speed Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -388,10 +389,11 @@ public static Speed Parse(string str, IFormatProvider formatProvider = null) } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs index 74e4297275..6b5933ba7f 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs @@ -373,15 +373,15 @@ public double As(TemperatureUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static Temperature Parse(string str, IFormatProvider formatProvider = null) @@ -395,11 +395,12 @@ public static Temperature Parse(string str, IFormatProvider formatProvider = nul var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -428,10 +429,11 @@ public static Temperature Parse(string str, IFormatProvider formatProvider = nul } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs index a822c55d3a..15ab515f78 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs @@ -533,15 +533,15 @@ public double As(TorqueUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static Torque Parse(string str, IFormatProvider formatProvider = null) @@ -555,11 +555,12 @@ public static Torque Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -588,10 +589,11 @@ public static Torque Parse(string str, IFormatProvider formatProvider = null) } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs index 2e68e21c67..34229ae333 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs @@ -613,15 +613,15 @@ public double As(VolumeUnit unit) #region Parsing /// - /// Parse a string with at least one quantity of the format "<quantity> <unit>". + /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// /// /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// /// The value of 'str' cannot be null. /// - /// Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format - /// "<quantity> <unit>". + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// /// Error parsing string. public static Volume Parse(string str, IFormatProvider formatProvider = null) @@ -635,11 +635,12 @@ public static Volume Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?)+?", + var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?[^\s\d]+)"); // capture Unit (non-numeric, non-whitespace) input + @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -668,10 +669,11 @@ public static Volume Parse(string str, IFormatProvider formatProvider = null) } } - if(converted.Count == 0) // No valid matches + // Check if there were no valid matches + if (converted.Count == 0) { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to have at least one valid quantity in the format \" or \".", "str"); + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs b/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs index ab7567c128..63061ebc9d 100644 --- a/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs +++ b/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs @@ -678,13 +678,13 @@ private static readonly ReadOnlyCollection DefaultLocalization new CulturesForEnumValue((int) LengthUnit.Foot, new[] { - new AbbreviationsForCulture("en-US", "ft"), + new AbbreviationsForCulture("en-US", "ft", "\'"), new AbbreviationsForCulture("ru-RU", "фут"), }), new CulturesForEnumValue((int) LengthUnit.Inch, new[] { - new AbbreviationsForCulture("en-US", "in"), + new AbbreviationsForCulture("en-US", "in", "\""), new AbbreviationsForCulture("ru-RU", "дюйм"), }), new CulturesForEnumValue((int) LengthUnit.Kilometer, From b82bb293a403dd32befac8c5180bd17615b4cf1b Mon Sep 17 00:00:00 2001 From: Maher Kassim Date: Thu, 21 May 2015 14:53:26 -0400 Subject: [PATCH 5/9] #72 Added second pass in parsing to allow 1'1" --- UnitsNet.Tests/CustomCode/ParseTests.cs | 5 +- .../Include-GenerateUnitClassSourceCode.ps1 | 69 ++++++++++++++----- 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/UnitsNet.Tests/CustomCode/ParseTests.cs b/UnitsNet.Tests/CustomCode/ParseTests.cs index a255862d32..fc6cfce352 100644 --- a/UnitsNet.Tests/CustomCode/ParseTests.cs +++ b/UnitsNet.Tests/CustomCode/ParseTests.cs @@ -19,7 +19,6 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; using System.Globalization; using NUnit.Framework; using UnitsNet.Units; @@ -55,6 +54,10 @@ public double ParseLengthToMetersUsEnglish(string s) [TestCase("1 ft 1 in", Result = 13)] [TestCase("1ft 1in", Result = 13)] [TestCase("1\' 1\"", Result = 13)] + [TestCase("1\'1\"", Result = 13)] + [TestCase("1ft1in", Result = 13)] + [TestCase("1ft and 1in", Result = 13)] + [TestCase("1ft 1invalid", ExpectedExceptionName = "UnitsNet.UnitsNetException")] public double ParseImperialLengthInchesUsEnglish(string s) { var usEnglish = CultureInfo.GetCultureInfo("en-US"); diff --git a/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 b/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 index 40672e4f17..200f147cfe 100644 --- a/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 +++ b/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 @@ -276,7 +276,6 @@ namespace UnitsNet /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static $className Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -288,13 +287,47 @@ namespace UnitsNet var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List<$className> ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List<$className>(); @@ -313,25 +346,23 @@ namespace UnitsNet converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// From 898141476ded5eef435612b1c96513af412862a9 Mon Sep 17 00:00:00 2001 From: Maher Kassim Date: Thu, 21 May 2015 14:53:47 -0400 Subject: [PATCH 6/9] Generated Code for Added second pass in parsing to allow 1'1" --- .../UnitClasses/Acceleration.g.cs | 69 ++++++++++++++----- .../UnitClasses/AmplitudeRatio.g.cs | 69 ++++++++++++++----- UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs | 69 ++++++++++++++----- UnitsNet/GeneratedCode/UnitClasses/Area.g.cs | 69 ++++++++++++++----- .../GeneratedCode/UnitClasses/Density.g.cs | 69 ++++++++++++++----- .../GeneratedCode/UnitClasses/Duration.g.cs | 69 ++++++++++++++----- .../UnitClasses/ElectricCurrent.g.cs | 69 ++++++++++++++----- .../UnitClasses/ElectricPotential.g.cs | 69 ++++++++++++++----- .../UnitClasses/ElectricResistance.g.cs | 69 ++++++++++++++----- .../GeneratedCode/UnitClasses/Energy.g.cs | 69 ++++++++++++++----- UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs | 69 ++++++++++++++----- UnitsNet/GeneratedCode/UnitClasses/Force.g.cs | 69 ++++++++++++++----- .../GeneratedCode/UnitClasses/Frequency.g.cs | 69 ++++++++++++++----- .../UnitClasses/Information.g.cs | 69 ++++++++++++++----- .../UnitClasses/KinematicViscosity.g.cs | 69 ++++++++++++++----- .../GeneratedCode/UnitClasses/Length.g.cs | 69 ++++++++++++++----- UnitsNet/GeneratedCode/UnitClasses/Level.g.cs | 69 ++++++++++++++----- UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs | 69 ++++++++++++++----- UnitsNet/GeneratedCode/UnitClasses/Power.g.cs | 69 ++++++++++++++----- .../GeneratedCode/UnitClasses/PowerRatio.g.cs | 69 ++++++++++++++----- .../GeneratedCode/UnitClasses/Pressure.g.cs | 69 ++++++++++++++----- UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs | 69 ++++++++++++++----- .../UnitClasses/RotationalSpeed.g.cs | 69 ++++++++++++++----- .../UnitClasses/SpecificWeight.g.cs | 69 ++++++++++++++----- UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs | 69 ++++++++++++++----- .../UnitClasses/Temperature.g.cs | 69 ++++++++++++++----- .../GeneratedCode/UnitClasses/Torque.g.cs | 69 ++++++++++++++----- .../GeneratedCode/UnitClasses/Volume.g.cs | 69 ++++++++++++++----- 28 files changed, 1400 insertions(+), 532 deletions(-) diff --git a/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs index 5e6078fab0..5eca0fc113 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs @@ -243,7 +243,6 @@ public double As(AccelerationUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static Acceleration Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -255,13 +254,47 @@ public static Acceleration Parse(string str, IFormatProvider formatProvider = nu var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -280,25 +313,23 @@ public static Acceleration Parse(string str, IFormatProvider formatProvider = nu converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs b/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs index 2645da7cc9..596ccb1ee6 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs @@ -291,7 +291,6 @@ public double As(AmplitudeRatioUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -303,13 +302,47 @@ public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -328,25 +361,23 @@ public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs index 1902ef4c7c..d9d292b36b 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs @@ -283,7 +283,6 @@ public double As(AngleUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static Angle Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -295,13 +294,47 @@ public static Angle Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -320,25 +353,23 @@ public static Angle Parse(string str, IFormatProvider formatProvider = null) converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs index 735f9e15f9..4b9784b07e 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs @@ -403,7 +403,6 @@ public double As(AreaUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static Area Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -415,13 +414,47 @@ public static Area Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -440,25 +473,23 @@ public static Area Parse(string str, IFormatProvider formatProvider = null) converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs index 315463c97d..06f5fc81db 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs @@ -423,7 +423,6 @@ public double As(DensityUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static Density Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -435,13 +434,47 @@ public static Density Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -460,25 +493,23 @@ public static Density Parse(string str, IFormatProvider formatProvider = null) converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs index 3fc9f28faf..a9103f6fd5 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs @@ -423,7 +423,6 @@ public double As(DurationUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static Duration Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -435,13 +434,47 @@ public static Duration Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -460,25 +493,23 @@ public static Duration Parse(string str, IFormatProvider formatProvider = null) converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs b/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs index fd5fb8edaa..20e9ce74bd 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs @@ -343,7 +343,6 @@ public double As(ElectricCurrentUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static ElectricCurrent Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -355,13 +354,47 @@ public static ElectricCurrent Parse(string str, IFormatProvider formatProvider = var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -380,25 +413,23 @@ public static ElectricCurrent Parse(string str, IFormatProvider formatProvider = converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs b/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs index 770d5455f8..f6ae746967 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs @@ -323,7 +323,6 @@ public double As(ElectricPotentialUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static ElectricPotential Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -335,13 +334,47 @@ public static ElectricPotential Parse(string str, IFormatProvider formatProvider var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -360,25 +393,23 @@ public static ElectricPotential Parse(string str, IFormatProvider formatProvider converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs b/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs index 9dff668352..8707b9a82e 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs @@ -283,7 +283,6 @@ public double As(ElectricResistanceUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static ElectricResistance Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -295,13 +294,47 @@ public static ElectricResistance Parse(string str, IFormatProvider formatProvide var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -320,25 +353,23 @@ public static ElectricResistance Parse(string str, IFormatProvider formatProvide converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs index c8600eae8a..cd997ca758 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs @@ -283,7 +283,6 @@ public double As(EnergyUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static Energy Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -295,13 +294,47 @@ public static Energy Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -320,25 +353,23 @@ public static Energy Parse(string str, IFormatProvider formatProvider = null) converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs index 54d9e3d8d9..e6819ffdbe 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs @@ -303,7 +303,6 @@ public double As(FlowUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static Flow Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -315,13 +314,47 @@ public static Flow Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -340,25 +373,23 @@ public static Flow Parse(string str, IFormatProvider formatProvider = null) converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs index 9e0b69b128..a1e048215a 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs @@ -383,7 +383,6 @@ public double As(ForceUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static Force Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -395,13 +394,47 @@ public static Force Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -420,25 +453,23 @@ public static Force Parse(string str, IFormatProvider formatProvider = null) converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs index 91e9391910..8be55e286b 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs @@ -323,7 +323,6 @@ public double As(FrequencyUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static Frequency Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -335,13 +334,47 @@ public static Frequency Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -360,25 +393,23 @@ public static Frequency Parse(string str, IFormatProvider formatProvider = null) converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs index 6b76903b01..f04d3b70b2 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs @@ -743,7 +743,6 @@ public double As(InformationUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static Information Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -755,13 +754,47 @@ public static Information Parse(string str, IFormatProvider formatProvider = nul var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -780,25 +813,23 @@ public static Information Parse(string str, IFormatProvider formatProvider = nul converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs b/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs index da167e8861..d660c80092 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs @@ -383,7 +383,6 @@ public double As(KinematicViscosityUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static KinematicViscosity Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -395,13 +394,47 @@ public static KinematicViscosity Parse(string str, IFormatProvider formatProvide var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -420,25 +453,23 @@ public static KinematicViscosity Parse(string str, IFormatProvider formatProvide converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs index 34d97294a8..25e0bb59d1 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs @@ -483,7 +483,6 @@ public double As(LengthUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static Length Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -495,13 +494,47 @@ public static Length Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -520,25 +553,23 @@ public static Length Parse(string str, IFormatProvider formatProvider = null) converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs index bc83de8188..2e63cebd8a 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs @@ -271,7 +271,6 @@ public double As(LevelUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static Level Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -283,13 +282,47 @@ public static Level Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -308,25 +341,23 @@ public static Level Parse(string str, IFormatProvider formatProvider = null) converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs index 010aa90790..f0fb4d8c6e 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs @@ -563,7 +563,6 @@ public double As(MassUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static Mass Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -575,13 +574,47 @@ public static Mass Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -600,25 +633,23 @@ public static Mass Parse(string str, IFormatProvider formatProvider = null) converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs index aac79c3797..39e7cbbe9d 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs @@ -543,7 +543,6 @@ public double As(PowerUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static Power Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -555,13 +554,47 @@ public static Power Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -580,25 +613,23 @@ public static Power Parse(string str, IFormatProvider formatProvider = null) converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs b/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs index 71bf968ed2..8e81c9be05 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs @@ -271,7 +271,6 @@ public double As(PowerRatioUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static PowerRatio Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -283,13 +282,47 @@ public static PowerRatio Parse(string str, IFormatProvider formatProvider = null var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -308,25 +341,23 @@ public static PowerRatio Parse(string str, IFormatProvider formatProvider = null converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs index 6680b34bf2..2131792755 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs @@ -883,7 +883,6 @@ public double As(PressureUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static Pressure Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -895,13 +894,47 @@ public static Pressure Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -920,25 +953,23 @@ public static Pressure Parse(string str, IFormatProvider formatProvider = null) converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs index 51ac6e4922..ec81e3f95d 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs @@ -343,7 +343,6 @@ public double As(RatioUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static Ratio Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -355,13 +354,47 @@ public static Ratio Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -380,25 +413,23 @@ public static Ratio Parse(string str, IFormatProvider formatProvider = null) converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs b/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs index 5686544412..1bd9341551 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs @@ -263,7 +263,6 @@ public double As(RotationalSpeedUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static RotationalSpeed Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -275,13 +274,47 @@ public static RotationalSpeed Parse(string str, IFormatProvider formatProvider = var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -300,25 +333,23 @@ public static RotationalSpeed Parse(string str, IFormatProvider formatProvider = converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs b/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs index e3bff4b70d..64228d8b5a 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs @@ -543,7 +543,6 @@ public double As(SpecificWeightUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static SpecificWeight Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -555,13 +554,47 @@ public static SpecificWeight Parse(string str, IFormatProvider formatProvider = var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -580,25 +613,23 @@ public static SpecificWeight Parse(string str, IFormatProvider formatProvider = converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs index 7e71601973..7ffc89f0d9 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs @@ -343,7 +343,6 @@ public double As(SpeedUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static Speed Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -355,13 +354,47 @@ public static Speed Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -380,25 +413,23 @@ public static Speed Parse(string str, IFormatProvider formatProvider = null) converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs index 6b5933ba7f..86616d9d18 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs @@ -383,7 +383,6 @@ public double As(TemperatureUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static Temperature Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -395,13 +394,47 @@ public static Temperature Parse(string str, IFormatProvider formatProvider = nul var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -420,25 +453,23 @@ public static Temperature Parse(string str, IFormatProvider formatProvider = nul converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs index 15ab515f78..dc9bac54a1 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs @@ -543,7 +543,6 @@ public double As(TorqueUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static Torque Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -555,13 +554,47 @@ public static Torque Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -580,25 +613,23 @@ public static Torque Parse(string str, IFormatProvider formatProvider = null) converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// diff --git a/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs index 34229ae333..41d9ae3f8c 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs @@ -623,7 +623,6 @@ public double As(VolumeUnit unit) /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// - /// Error parsing string. public static Volume Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); @@ -635,13 +634,47 @@ public static Volume Parse(string str, IFormatProvider formatProvider = null) var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. 1ft 2in) + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)", // capture Unit (non-whitespace) input + @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") + + var quantities = ParseWithRegex(regexString, str, formatProvider, false); + + // Check if there were no valid matches + if (quantities.Count == 0) + { + // Try parsing again, but this time stop parsing units if a number is reached. + // This is used to parse input similar to 1'1" (ie. no space between quantities) + var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", + numRegex, exponentialRegex, @"\s?", + @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input + @"{1,2}?"); + quantities = ParseWithRegex(imperialRegex, str, formatProvider); + + // Check if there were still no valid matches (invalid input) + if (quantities.Count == 0) + { + var ex = new ArgumentException( + "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", + "str"); + ex.Data["input"] = str; + ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw ex; + } + } + return quantities.Aggregate((x, y) => x + y); + } + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List(); @@ -660,25 +693,23 @@ public static Volume Parse(string str, IFormatProvider formatProvider = null) converted.Add(From(value, unit)); } - catch (Exception e) + catch (Exception ex) { - var newEx = new UnitsNetException("Error parsing string.", e); + if (!validateUnit && ex is UnitsNetException) + { + // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. + // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. + // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd + converted.Clear(); + return converted; + } + var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } - - // Check if there were no valid matches - if (converted.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } - return converted.Aggregate((x, y) => x + y); + return converted; } /// From b9fb93ac7922a4cee018160e90b6611c3003025a Mon Sep 17 00:00:00 2001 From: Maher Kassim Date: Fri, 26 Jun 2015 14:45:00 -0400 Subject: [PATCH 7/9] Simplified parsing, captured invalid input, added parsing for separator --- UnitsNet.Tests/CustomCode/ParseTests.cs | 5 +- .../Include-GenerateUnitClassSourceCode.ps1 | 62 ++++++++----------- 2 files changed, 28 insertions(+), 39 deletions(-) diff --git a/UnitsNet.Tests/CustomCode/ParseTests.cs b/UnitsNet.Tests/CustomCode/ParseTests.cs index fc6cfce352..28a5b9a785 100644 --- a/UnitsNet.Tests/CustomCode/ParseTests.cs +++ b/UnitsNet.Tests/CustomCode/ParseTests.cs @@ -35,14 +35,14 @@ namespace UnitsNet.Tests.CustomCode [TestFixture] public class ParseTests { - [TestCase("1km", Result=1000)] + [TestCase("1km", Result = 1000)] [TestCase("1 km", Result = 1000)] [TestCase("1e-3 km", Result = 1)] [TestCase("5.5 m", Result = 5.5)] [TestCase("500,005 m", Result = 500005)] [TestCase(null, ExpectedExceptionName = "System.ArgumentNullException")] [TestCase("1", ExpectedExceptionName = "System.ArgumentException")] - [TestCase("km", ExpectedExceptionName = "System.ArgumentException")] + [TestCase("km", ExpectedExceptionName = "UnitsNet.UnitsNetException")] [TestCase("1 kg", ExpectedExceptionName = "UnitsNet.UnitsNetException")] public double ParseLengthToMetersUsEnglish(string s) { @@ -57,6 +57,7 @@ public double ParseLengthToMetersUsEnglish(string s) [TestCase("1\'1\"", Result = 13)] [TestCase("1ft1in", Result = 13)] [TestCase("1ft and 1in", Result = 13)] + [TestCase("1ft monkey 1in", ExpectedExceptionName = "UnitsNet.UnitsNetException")] [TestCase("1ft 1invalid", ExpectedExceptionName = "UnitsNet.UnitsNetException")] public double ParseImperialLengthInchesUsEnglish(string s) { diff --git a/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 b/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 index 200f147cfe..5417c4ddee 100644 --- a/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 +++ b/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 @@ -288,36 +288,20 @@ namespace UnitsNet numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -326,7 +310,7 @@ namespace UnitsNet /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List<$className> ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List<$className> ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -338,6 +322,16 @@ namespace UnitsNet var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -348,16 +342,10 @@ namespace UnitsNet } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } From 234bbd85f31eb7f374cff3d7efefeef537930e49 Mon Sep 17 00:00:00 2001 From: Maher Kassim Date: Fri, 26 Jun 2015 14:45:33 -0400 Subject: [PATCH 8/9] Generated Code for Simplified parsing, captured invalid input, added parsing for separator --- .../UnitClasses/Acceleration.g.cs | 62 ++++++++----------- .../UnitClasses/AmplitudeRatio.g.cs | 62 ++++++++----------- UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs | 62 ++++++++----------- UnitsNet/GeneratedCode/UnitClasses/Area.g.cs | 62 ++++++++----------- .../GeneratedCode/UnitClasses/Density.g.cs | 62 ++++++++----------- .../GeneratedCode/UnitClasses/Duration.g.cs | 62 ++++++++----------- .../UnitClasses/ElectricCurrent.g.cs | 62 ++++++++----------- .../UnitClasses/ElectricPotential.g.cs | 62 ++++++++----------- .../UnitClasses/ElectricResistance.g.cs | 62 ++++++++----------- .../GeneratedCode/UnitClasses/Energy.g.cs | 62 ++++++++----------- UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs | 62 ++++++++----------- UnitsNet/GeneratedCode/UnitClasses/Force.g.cs | 62 ++++++++----------- .../GeneratedCode/UnitClasses/Frequency.g.cs | 62 ++++++++----------- .../UnitClasses/Information.g.cs | 62 ++++++++----------- .../UnitClasses/KinematicViscosity.g.cs | 62 ++++++++----------- .../GeneratedCode/UnitClasses/Length.g.cs | 62 ++++++++----------- UnitsNet/GeneratedCode/UnitClasses/Level.g.cs | 62 ++++++++----------- UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs | 62 ++++++++----------- UnitsNet/GeneratedCode/UnitClasses/Power.g.cs | 62 ++++++++----------- .../GeneratedCode/UnitClasses/PowerRatio.g.cs | 62 ++++++++----------- .../GeneratedCode/UnitClasses/Pressure.g.cs | 62 ++++++++----------- UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs | 62 ++++++++----------- .../UnitClasses/RotationalSpeed.g.cs | 62 ++++++++----------- .../UnitClasses/SpecificWeight.g.cs | 62 ++++++++----------- UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs | 62 ++++++++----------- .../UnitClasses/Temperature.g.cs | 62 ++++++++----------- .../GeneratedCode/UnitClasses/Torque.g.cs | 62 ++++++++----------- .../GeneratedCode/UnitClasses/Volume.g.cs | 62 ++++++++----------- 28 files changed, 700 insertions(+), 1036 deletions(-) diff --git a/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs index 5eca0fc113..39bcaeef10 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs @@ -255,36 +255,20 @@ public static Acceleration Parse(string str, IFormatProvider formatProvider = nu numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -293,7 +277,7 @@ public static Acceleration Parse(string str, IFormatProvider formatProvider = nu /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -305,6 +289,16 @@ private static List ParseWithRegex(string regexString, string str, var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -315,16 +309,10 @@ private static List ParseWithRegex(string regexString, string str, } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs b/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs index 596ccb1ee6..415bfd80fc 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs @@ -303,36 +303,20 @@ public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -341,7 +325,7 @@ public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -353,6 +337,16 @@ private static List ParseWithRegex(string regexString, string st var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -363,16 +357,10 @@ private static List ParseWithRegex(string regexString, string st } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs index d9d292b36b..fcefc4b82a 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs @@ -295,36 +295,20 @@ public static Angle Parse(string str, IFormatProvider formatProvider = null) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -333,7 +317,7 @@ public static Angle Parse(string str, IFormatProvider formatProvider = null) /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -345,6 +329,16 @@ private static List ParseWithRegex(string regexString, string str, IForma var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -355,16 +349,10 @@ private static List ParseWithRegex(string regexString, string str, IForma } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs index 4b9784b07e..746d1ca247 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs @@ -415,36 +415,20 @@ public static Area Parse(string str, IFormatProvider formatProvider = null) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -453,7 +437,7 @@ public static Area Parse(string str, IFormatProvider formatProvider = null) /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -465,6 +449,16 @@ private static List ParseWithRegex(string regexString, string str, IFormat var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -475,16 +469,10 @@ private static List ParseWithRegex(string regexString, string str, IFormat } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs index 06f5fc81db..c0892ed6c1 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Density.g.cs @@ -435,36 +435,20 @@ public static Density Parse(string str, IFormatProvider formatProvider = null) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -473,7 +457,7 @@ public static Density Parse(string str, IFormatProvider formatProvider = null) /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -485,6 +469,16 @@ private static List ParseWithRegex(string regexString, string str, IFor var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -495,16 +489,10 @@ private static List ParseWithRegex(string regexString, string str, IFor } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs index a9103f6fd5..cfcbb1e617 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs @@ -435,36 +435,20 @@ public static Duration Parse(string str, IFormatProvider formatProvider = null) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -473,7 +457,7 @@ public static Duration Parse(string str, IFormatProvider formatProvider = null) /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -485,6 +469,16 @@ private static List ParseWithRegex(string regexString, string str, IFo var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -495,16 +489,10 @@ private static List ParseWithRegex(string regexString, string str, IFo } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs b/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs index 20e9ce74bd..c3cafee71e 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs @@ -355,36 +355,20 @@ public static ElectricCurrent Parse(string str, IFormatProvider formatProvider = numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -393,7 +377,7 @@ public static ElectricCurrent Parse(string str, IFormatProvider formatProvider = /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -405,6 +389,16 @@ private static List ParseWithRegex(string regexString, string s var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -415,16 +409,10 @@ private static List ParseWithRegex(string regexString, string s } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs b/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs index f6ae746967..8e7a03f632 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs @@ -335,36 +335,20 @@ public static ElectricPotential Parse(string str, IFormatProvider formatProvider numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -373,7 +357,7 @@ public static ElectricPotential Parse(string str, IFormatProvider formatProvider /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -385,6 +369,16 @@ private static List ParseWithRegex(string regexString, string var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -395,16 +389,10 @@ private static List ParseWithRegex(string regexString, string } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs b/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs index 8707b9a82e..d466d08420 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs @@ -295,36 +295,20 @@ public static ElectricResistance Parse(string str, IFormatProvider formatProvide numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -333,7 +317,7 @@ public static ElectricResistance Parse(string str, IFormatProvider formatProvide /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -345,6 +329,16 @@ private static List ParseWithRegex(string regexString, strin var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -355,16 +349,10 @@ private static List ParseWithRegex(string regexString, strin } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs index cd997ca758..07563408a0 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Energy.g.cs @@ -295,36 +295,20 @@ public static Energy Parse(string str, IFormatProvider formatProvider = null) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -333,7 +317,7 @@ public static Energy Parse(string str, IFormatProvider formatProvider = null) /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -345,6 +329,16 @@ private static List ParseWithRegex(string regexString, string str, IForm var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -355,16 +349,10 @@ private static List ParseWithRegex(string regexString, string str, IForm } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs index e6819ffdbe..c1b6d0e565 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs @@ -315,36 +315,20 @@ public static Flow Parse(string str, IFormatProvider formatProvider = null) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -353,7 +337,7 @@ public static Flow Parse(string str, IFormatProvider formatProvider = null) /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -365,6 +349,16 @@ private static List ParseWithRegex(string regexString, string str, IFormat var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -375,16 +369,10 @@ private static List ParseWithRegex(string regexString, string str, IFormat } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs index a1e048215a..16e0e24240 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs @@ -395,36 +395,20 @@ public static Force Parse(string str, IFormatProvider formatProvider = null) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -433,7 +417,7 @@ public static Force Parse(string str, IFormatProvider formatProvider = null) /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -445,6 +429,16 @@ private static List ParseWithRegex(string regexString, string str, IForma var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -455,16 +449,10 @@ private static List ParseWithRegex(string regexString, string str, IForma } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs index 8be55e286b..3132aea747 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs @@ -335,36 +335,20 @@ public static Frequency Parse(string str, IFormatProvider formatProvider = null) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -373,7 +357,7 @@ public static Frequency Parse(string str, IFormatProvider formatProvider = null) /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -385,6 +369,16 @@ private static List ParseWithRegex(string regexString, string str, IF var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -395,16 +389,10 @@ private static List ParseWithRegex(string regexString, string str, IF } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs index f04d3b70b2..e2daeb800c 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs @@ -755,36 +755,20 @@ public static Information Parse(string str, IFormatProvider formatProvider = nul numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -793,7 +777,7 @@ public static Information Parse(string str, IFormatProvider formatProvider = nul /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -805,6 +789,16 @@ private static List ParseWithRegex(string regexString, string str, var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -815,16 +809,10 @@ private static List ParseWithRegex(string regexString, string str, } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs b/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs index d660c80092..f18649eff5 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/KinematicViscosity.g.cs @@ -395,36 +395,20 @@ public static KinematicViscosity Parse(string str, IFormatProvider formatProvide numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -433,7 +417,7 @@ public static KinematicViscosity Parse(string str, IFormatProvider formatProvide /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -445,6 +429,16 @@ private static List ParseWithRegex(string regexString, strin var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -455,16 +449,10 @@ private static List ParseWithRegex(string regexString, strin } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs index 25e0bb59d1..cd5a641b73 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs @@ -495,36 +495,20 @@ public static Length Parse(string str, IFormatProvider formatProvider = null) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -533,7 +517,7 @@ public static Length Parse(string str, IFormatProvider formatProvider = null) /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -545,6 +529,16 @@ private static List ParseWithRegex(string regexString, string str, IForm var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -555,16 +549,10 @@ private static List ParseWithRegex(string regexString, string str, IForm } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs index 2e63cebd8a..ab78ee9fc8 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs @@ -283,36 +283,20 @@ public static Level Parse(string str, IFormatProvider formatProvider = null) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -321,7 +305,7 @@ public static Level Parse(string str, IFormatProvider formatProvider = null) /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -333,6 +317,16 @@ private static List ParseWithRegex(string regexString, string str, IForma var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -343,16 +337,10 @@ private static List ParseWithRegex(string regexString, string str, IForma } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs index f0fb4d8c6e..7c52b1591c 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs @@ -575,36 +575,20 @@ public static Mass Parse(string str, IFormatProvider formatProvider = null) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -613,7 +597,7 @@ public static Mass Parse(string str, IFormatProvider formatProvider = null) /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -625,6 +609,16 @@ private static List ParseWithRegex(string regexString, string str, IFormat var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -635,16 +629,10 @@ private static List ParseWithRegex(string regexString, string str, IFormat } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs index 39e7cbbe9d..d2a21e49ca 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs @@ -555,36 +555,20 @@ public static Power Parse(string str, IFormatProvider formatProvider = null) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -593,7 +577,7 @@ public static Power Parse(string str, IFormatProvider formatProvider = null) /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -605,6 +589,16 @@ private static List ParseWithRegex(string regexString, string str, IForma var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -615,16 +609,10 @@ private static List ParseWithRegex(string regexString, string str, IForma } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs b/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs index 8e81c9be05..a428bd4e9c 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs @@ -283,36 +283,20 @@ public static PowerRatio Parse(string str, IFormatProvider formatProvider = null numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -321,7 +305,7 @@ public static PowerRatio Parse(string str, IFormatProvider formatProvider = null /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -333,6 +317,16 @@ private static List ParseWithRegex(string regexString, string str, I var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -343,16 +337,10 @@ private static List ParseWithRegex(string regexString, string str, I } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs index 2131792755..0b7b83095d 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs @@ -895,36 +895,20 @@ public static Pressure Parse(string str, IFormatProvider formatProvider = null) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -933,7 +917,7 @@ public static Pressure Parse(string str, IFormatProvider formatProvider = null) /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -945,6 +929,16 @@ private static List ParseWithRegex(string regexString, string str, IFo var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -955,16 +949,10 @@ private static List ParseWithRegex(string regexString, string str, IFo } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs index ec81e3f95d..77960770fa 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs @@ -355,36 +355,20 @@ public static Ratio Parse(string str, IFormatProvider formatProvider = null) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -393,7 +377,7 @@ public static Ratio Parse(string str, IFormatProvider formatProvider = null) /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -405,6 +389,16 @@ private static List ParseWithRegex(string regexString, string str, IForma var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -415,16 +409,10 @@ private static List ParseWithRegex(string regexString, string str, IForma } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs b/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs index 1bd9341551..4123a89591 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs @@ -275,36 +275,20 @@ public static RotationalSpeed Parse(string str, IFormatProvider formatProvider = numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -313,7 +297,7 @@ public static RotationalSpeed Parse(string str, IFormatProvider formatProvider = /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -325,6 +309,16 @@ private static List ParseWithRegex(string regexString, string s var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -335,16 +329,10 @@ private static List ParseWithRegex(string regexString, string s } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs b/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs index 64228d8b5a..84cb1f23ab 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/SpecificWeight.g.cs @@ -555,36 +555,20 @@ public static SpecificWeight Parse(string str, IFormatProvider formatProvider = numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -593,7 +577,7 @@ public static SpecificWeight Parse(string str, IFormatProvider formatProvider = /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -605,6 +589,16 @@ private static List ParseWithRegex(string regexString, string st var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -615,16 +609,10 @@ private static List ParseWithRegex(string regexString, string st } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs index 7ffc89f0d9..d764ba9f55 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs @@ -355,36 +355,20 @@ public static Speed Parse(string str, IFormatProvider formatProvider = null) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -393,7 +377,7 @@ public static Speed Parse(string str, IFormatProvider formatProvider = null) /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -405,6 +389,16 @@ private static List ParseWithRegex(string regexString, string str, IForma var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -415,16 +409,10 @@ private static List ParseWithRegex(string regexString, string str, IForma } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs index 86616d9d18..d244edb327 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs @@ -395,36 +395,20 @@ public static Temperature Parse(string str, IFormatProvider formatProvider = nul numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -433,7 +417,7 @@ public static Temperature Parse(string str, IFormatProvider formatProvider = nul /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -445,6 +429,16 @@ private static List ParseWithRegex(string regexString, string str, var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -455,16 +449,10 @@ private static List ParseWithRegex(string regexString, string str, } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs index dc9bac54a1..ad1819fb35 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs @@ -555,36 +555,20 @@ public static Torque Parse(string str, IFormatProvider formatProvider = null) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -593,7 +577,7 @@ public static Torque Parse(string str, IFormatProvider formatProvider = null) /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -605,6 +589,16 @@ private static List ParseWithRegex(string regexString, string str, IForm var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -615,16 +609,10 @@ private static List ParseWithRegex(string regexString, string str, IForm } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } diff --git a/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs index 41d9ae3f8c..593de21697 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs @@ -635,36 +635,20 @@ public static Volume Parse(string str, IFormatProvider formatProvider = null) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; - var regexString = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, // capture base (integral) Quantity value - exponentialRegex, // capture exponential (if any), end of Quantity capturing - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)", // capture Unit (non-whitespace) input - @"{1,2}?"); // capture one or two quantities (eg. "1kg" or "1ft 2in") - - var quantities = ParseWithRegex(regexString, str, formatProvider, false); - - // Check if there were no valid matches + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); if (quantities.Count == 0) { - // Try parsing again, but this time stop parsing units if a number is reached. - // This is used to parse input similar to 1'1" (ie. no space between quantities) - var imperialRegex = string.Format(@"(?:(?[-+]?{0}{1}{2}{3}\s?){4}", - numRegex, exponentialRegex, @"\s?", - @"(?[^\d\s]+)", // capture Unit (non-numeric, non-whitespace) input - @"{1,2}?"); - quantities = ParseWithRegex(imperialRegex, str, formatProvider); - - // Check if there were still no valid matches (invalid input) - if (quantities.Count == 0) - { - var ex = new ArgumentException( - "Expected string to have one or two pairs of quantity and unit in the format \" or \". Eg. \"5.5 m\" or \"1ft 2in\"", - "str"); - ex.Data["input"] = str; - ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); - throw ex; - } + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); } return quantities.Aggregate((x, y) => x + y); } @@ -673,7 +657,7 @@ public static Volume Parse(string str, IFormatProvider formatProvider = null) /// Parse a string given a particular regular expression. /// /// Error parsing string. - private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null, bool validateUnit = true) + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); @@ -685,6 +669,16 @@ private static List ParseWithRegex(string regexString, string str, IForm var valueString = groups["value"].Value; var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; try { @@ -695,16 +689,10 @@ private static List ParseWithRegex(string regexString, string str, IForm } catch (Exception ex) { - if (!validateUnit && ex is UnitsNetException) - { - // Prevents throwing exception when we'll be attempting a potentially valid match with next regex. - // Instead, stops trying to match the current regex and returns an empty list to avoid returning partially parsed result. - // eg. 1'1" will throw UnitsNetException for unit: '1" from 1st regex but not the 2nd - converted.Clear(); - return converted; - } var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } From 1f1b8c7e893ba8287da2714d7e9fd8243b6e869e Mon Sep 17 00:00:00 2001 From: Maher Kassim Date: Fri, 26 Jun 2015 15:46:48 -0400 Subject: [PATCH 9/9] Removed unnecessary escape from parse tests --- UnitsNet.Tests/CustomCode/ParseTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/UnitsNet.Tests/CustomCode/ParseTests.cs b/UnitsNet.Tests/CustomCode/ParseTests.cs index 28a5b9a785..37113abb0b 100644 --- a/UnitsNet.Tests/CustomCode/ParseTests.cs +++ b/UnitsNet.Tests/CustomCode/ParseTests.cs @@ -53,8 +53,8 @@ public double ParseLengthToMetersUsEnglish(string s) [TestCase("1 ft 1 in", Result = 13)] [TestCase("1ft 1in", Result = 13)] - [TestCase("1\' 1\"", Result = 13)] - [TestCase("1\'1\"", Result = 13)] + [TestCase("1' 1\"", Result = 13)] + [TestCase("1'1\"", Result = 13)] [TestCase("1ft1in", Result = 13)] [TestCase("1ft and 1in", Result = 13)] [TestCase("1ft monkey 1in", ExpectedExceptionName = "UnitsNet.UnitsNetException")]