From fe6a8ea125ce5a859a8cca89d41b0424db237c86 Mon Sep 17 00:00:00 2001 From: Maher Kassim Date: Fri, 20 Mar 2015 10:29:45 -0400 Subject: [PATCH 01/10] Switched parsing from splitting on space to capturing quantity and unit using Regex --- .../Include-GenerateUnitClassSourceCode.ps1 | 27 +++++++----- .../UnitClasses/Acceleration.g.cs | 27 +++++++----- .../UnitClasses/AmplitudeRatio.g.cs | 27 +++++++----- .../GeneratedCode/UnitClasses/Angle.g.cs | 27 +++++++----- .../GeneratedCode/UnitClasses/Area.g.cs | 27 +++++++----- .../GeneratedCode/UnitClasses/Duration.g.cs | 27 +++++++----- .../UnitClasses/ElectricCurrent.g.cs | 27 +++++++----- .../UnitClasses/ElectricPotential.g.cs | 27 +++++++----- .../UnitClasses/ElectricResistance.g.cs | 27 +++++++----- .../GeneratedCode/UnitClasses/Flow.g.cs | 27 +++++++----- .../GeneratedCode/UnitClasses/Force.g.cs | 27 +++++++----- .../GeneratedCode/UnitClasses/Frequency.g.cs | 27 +++++++----- .../UnitClasses/Information.g.cs | 27 +++++++----- .../GeneratedCode/UnitClasses/Length.g.cs | 27 +++++++----- .../GeneratedCode/UnitClasses/Level.g.cs | 27 +++++++----- .../GeneratedCode/UnitClasses/Mass.g.cs | 27 +++++++----- .../GeneratedCode/UnitClasses/Power.g.cs | 27 +++++++----- .../GeneratedCode/UnitClasses/PowerRatio.g.cs | 27 +++++++----- .../GeneratedCode/UnitClasses/Pressure.g.cs | 27 +++++++----- .../GeneratedCode/UnitClasses/Ratio.g.cs | 27 +++++++----- .../UnitClasses/RotationalSpeed.g.cs | 27 +++++++----- .../GeneratedCode/UnitClasses/Speed.g.cs | 27 +++++++----- .../UnitClasses/Temperature.g.cs | 27 +++++++----- .../GeneratedCode/UnitClasses/Torque.g.cs | 27 +++++++----- .../GeneratedCode/UnitClasses/Volume.g.cs | 27 +++++++----- Tests/CustomCode/ParseTests.cs | 44 ++++++++++++++----- 26 files changed, 434 insertions(+), 285 deletions(-) diff --git a/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 b/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 index 23edfc9a56..e2af3da923 100644 --- a/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 +++ b/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 @@ -34,10 +34,12 @@ function GenerateUnitClassSourceCode($unitClass) using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -278,11 +280,22 @@ namespace UnitsNet public static $className Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -290,14 +303,6 @@ namespace UnitsNet try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); $unitEnumName unit = unitSystem.Parse<$unitEnumName>(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs index 8b258d9410..7cf49917ba 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -245,11 +247,22 @@ public double As(AccelerationUnit unit) public static Acceleration Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -257,14 +270,6 @@ public static Acceleration Parse(string str, IFormatProvider formatProvider = nu try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); AccelerationUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs index 52289282f0..8e8770b3fb 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -293,11 +295,22 @@ public double As(AmplitudeRatioUnit unit) public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -305,14 +318,6 @@ public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); AmplitudeRatioUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs index dbe5b04253..aacf5ff32c 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -285,11 +287,22 @@ public double As(AngleUnit unit) public static Angle Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -297,14 +310,6 @@ public static Angle Parse(string str, IFormatProvider formatProvider = null) try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); AngleUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs index 0c3b5283cc..2ce99a461e 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -405,11 +407,22 @@ public double As(AreaUnit unit) public static Area Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -417,14 +430,6 @@ public static Area Parse(string str, IFormatProvider formatProvider = null) try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); AreaUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs index d99ea9847b..46b925737f 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -425,11 +427,22 @@ public double As(DurationUnit unit) public static Duration Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -437,14 +450,6 @@ public static Duration Parse(string str, IFormatProvider formatProvider = null) try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); DurationUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs index c40f0a3da1..af2bf378d8 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -345,11 +347,22 @@ public double As(ElectricCurrentUnit unit) public static ElectricCurrent Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -357,14 +370,6 @@ public static ElectricCurrent Parse(string str, IFormatProvider formatProvider = try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); ElectricCurrentUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs index 81db036507..93135a2bf9 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -325,11 +327,22 @@ public double As(ElectricPotentialUnit unit) public static ElectricPotential Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -337,14 +350,6 @@ public static ElectricPotential Parse(string str, IFormatProvider formatProvider try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); ElectricPotentialUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs index 8875f2d611..cc7ab338c5 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -285,11 +287,22 @@ public double As(ElectricResistanceUnit unit) public static ElectricResistance Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -297,14 +310,6 @@ public static ElectricResistance Parse(string str, IFormatProvider formatProvide try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); ElectricResistanceUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs index ccc50277e1..c36bbd0a46 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -305,11 +307,22 @@ public double As(FlowUnit unit) public static Flow Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -317,14 +330,6 @@ public static Flow Parse(string str, IFormatProvider formatProvider = null) try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); FlowUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs index 5afa6fcfcb..e3ed1f9d0f 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -365,11 +367,22 @@ public double As(ForceUnit unit) public static Force Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -377,14 +390,6 @@ public static Force Parse(string str, IFormatProvider formatProvider = null) try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); ForceUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs index 6a19bcd0d4..88cf90a428 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -325,11 +327,22 @@ public double As(FrequencyUnit unit) public static Frequency Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -337,14 +350,6 @@ public static Frequency Parse(string str, IFormatProvider formatProvider = null) try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); FrequencyUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs index b3c6dca3bb..75a589253f 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -745,11 +747,22 @@ public double As(InformationUnit unit) public static Information Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -757,14 +770,6 @@ public static Information Parse(string str, IFormatProvider formatProvider = nul try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); InformationUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs index bd0f6d964e..2a8a7f9f9c 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -485,11 +487,22 @@ public double As(LengthUnit unit) public static Length Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -497,14 +510,6 @@ public static Length Parse(string str, IFormatProvider formatProvider = null) try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); LengthUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs index 252ce3722c..80c034169e 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -273,11 +275,22 @@ public double As(LevelUnit unit) public static Level Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -285,14 +298,6 @@ public static Level Parse(string str, IFormatProvider formatProvider = null) try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); LevelUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs index 7a62c295fb..bd528ebdca 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -545,11 +547,22 @@ public double As(MassUnit unit) public static Mass Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -557,14 +570,6 @@ public static Mass Parse(string str, IFormatProvider formatProvider = null) try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); MassUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs index 66747420e8..e1b061614d 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -445,11 +447,22 @@ public double As(PowerUnit unit) public static Power Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -457,14 +470,6 @@ public static Power Parse(string str, IFormatProvider formatProvider = null) try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); PowerUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs index a0619b6c1d..c558fbb3bd 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -273,11 +275,22 @@ public double As(PowerRatioUnit unit) public static PowerRatio Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -285,14 +298,6 @@ public static PowerRatio Parse(string str, IFormatProvider formatProvider = null try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); PowerRatioUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs index 30817af35f..2a10751a4e 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -465,11 +467,22 @@ public double As(PressureUnit unit) public static Pressure Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -477,14 +490,6 @@ public static Pressure Parse(string str, IFormatProvider formatProvider = null) try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); PressureUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs index 193d867aaa..c6e5fadd87 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -345,11 +347,22 @@ public double As(RatioUnit unit) public static Ratio Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -357,14 +370,6 @@ public static Ratio Parse(string str, IFormatProvider formatProvider = null) try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); RatioUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs index f87b46d4fd..70e824bda0 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -265,11 +267,22 @@ public double As(RotationalSpeedUnit unit) public static RotationalSpeed Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -277,14 +290,6 @@ public static RotationalSpeed Parse(string str, IFormatProvider formatProvider = try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); RotationalSpeedUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs index 846512377c..c519991c27 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -325,11 +327,22 @@ public double As(SpeedUnit unit) public static Speed Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -337,14 +350,6 @@ public static Speed Parse(string str, IFormatProvider formatProvider = null) try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); SpeedUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs index 339e2a5b76..ea259743b4 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -385,11 +387,22 @@ public double As(TemperatureUnit unit) public static Temperature Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -397,14 +410,6 @@ public static Temperature Parse(string str, IFormatProvider formatProvider = nul try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); TemperatureUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs index bdbf5eb6bd..58cdfe35ec 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -245,11 +247,22 @@ public double As(TorqueUnit unit) public static Torque Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -257,14 +270,6 @@ public static Torque Parse(string str, IFormatProvider formatProvider = null) try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); TorqueUnit unit = unitSystem.Parse(unitString); diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs index e1692d9f3f..a99bc0740a 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs @@ -21,10 +21,12 @@ using System; using System.Globalization; +using System.Text.RegularExpressions; using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; + // ReSharper disable once CheckNamespace namespace UnitsNet @@ -625,11 +627,22 @@ public double As(VolumeUnit unit) public static Volume Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - string[] words = str.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries); - if (words.Length < 2) + + const string regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace + var regex = new Regex(regexString); + GroupCollection groups = regex.Match(str).Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + + if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected two or more words. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; @@ -637,14 +650,6 @@ public static Volume Parse(string str, IFormatProvider formatProvider = null) try { - // Unit string is the last word, since units added so far don't contain spaces. - // Value string is everything else since number formatting can contain spaces. - string[] allWordsButLast = words.Take(words.Length - 1).ToArray(); - string lastWord = words[words.Length - 1]; - - string unitString = lastWord; - string valueString = string.Join(" ", allWordsButLast); - var unitSystem = UnitSystem.GetCached(formatProvider); VolumeUnit unit = unitSystem.Parse(unitString); diff --git a/Tests/CustomCode/ParseTests.cs b/Tests/CustomCode/ParseTests.cs index 9e6a9b52bd..fa185fcaa6 100644 --- a/Tests/CustomCode/ParseTests.cs +++ b/Tests/CustomCode/ParseTests.cs @@ -19,6 +19,7 @@ // 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; @@ -34,24 +35,47 @@ namespace UnitsNet.Tests.CustomCode [TestFixture] public class ParseTests { - /// Error parsing string. - [Test] - public void ParseWithUsEnglishCulture() + [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("1 kg", ExpectedExceptionName = "UnitsNet.UnitsNetException")] + public double ParseLengthToMetersUsEnglish(string s) { var usEnglish = CultureInfo.GetCultureInfo("en-US"); - Assert.AreEqual(5.5, Length.Parse("5.5 m", usEnglish).Meters); - Assert.AreEqual(500005, Length.Parse("500,005 m", usEnglish).Meters); + + return Length.Parse(s, usEnglish).Meters; } /// Error parsing string. - [Test] - public void ParseWithCultureUsingSpaceAsThousandSeparators() + [TestCase("5.5 m", Result = 5.5)] + [TestCase("500 005 m", Result = 500005)] + [TestCase("500.005.050,001 m", ExpectedExceptionName = "UnitsNet.UnitsNetException")] // quantity doesn't match number format + public double ParseWithCultureUsingSpaceAsThousandSeparators(string s) { - var numberFormat = (NumberFormatInfo) CultureInfo.InvariantCulture.NumberFormat.Clone(); + var numberFormat = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone(); numberFormat.NumberGroupSeparator = " "; + numberFormat.NumberDecimalSeparator = "."; + + return Length.Parse(s, numberFormat).Meters; + } + + /// Error parsing string. + [TestCase("5,5 m", Result = 5.5)] + [TestCase("500.005.050,001 m", Result = 500005050.001)] + [TestCase("5.5 m", Result = 55)] // dot is group separator not decimal + [TestCase("500 005 m", ExpectedExceptionName = "UnitsNet.UnitsNetException")] // quantity doesn't match number format + public double ParseWithCultureUsingDotAsThousandSeparators(string s) + { + var numberFormat = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone(); + numberFormat.NumberGroupSeparator = "."; + numberFormat.NumberDecimalSeparator = ","; - Assert.AreEqual(5.5, Length.Parse("5.5 m", numberFormat).Meters); - Assert.AreEqual(500005, Length.Parse("500 005 m", numberFormat).Meters); + return Length.Parse(s, numberFormat).Meters; } } } \ No newline at end of file From 8592d2e156fd10b10c74e449623bdcd04078359f Mon Sep 17 00:00:00 2001 From: Maher Kassim Date: Fri, 20 Mar 2015 11:26:33 -0400 Subject: [PATCH 02/10] Updated Regex to allow custom group and decimal separators from FormatProvider --- .../Include-GenerateUnitClassSourceCode.ps1 | 18 +++++++++++++----- .../UnitClasses/Acceleration.g.cs | 18 +++++++++++++----- .../UnitClasses/AmplitudeRatio.g.cs | 18 +++++++++++++----- .../GeneratedCode/UnitClasses/Angle.g.cs | 18 +++++++++++++----- .../GeneratedCode/UnitClasses/Area.g.cs | 18 +++++++++++++----- .../GeneratedCode/UnitClasses/Duration.g.cs | 18 +++++++++++++----- .../UnitClasses/ElectricCurrent.g.cs | 18 +++++++++++++----- .../UnitClasses/ElectricPotential.g.cs | 18 +++++++++++++----- .../UnitClasses/ElectricResistance.g.cs | 18 +++++++++++++----- .../GeneratedCode/UnitClasses/Flow.g.cs | 18 +++++++++++++----- .../GeneratedCode/UnitClasses/Force.g.cs | 18 +++++++++++++----- .../GeneratedCode/UnitClasses/Frequency.g.cs | 18 +++++++++++++----- .../GeneratedCode/UnitClasses/Information.g.cs | 18 +++++++++++++----- .../GeneratedCode/UnitClasses/Length.g.cs | 18 +++++++++++++----- .../GeneratedCode/UnitClasses/Level.g.cs | 18 +++++++++++++----- .../GeneratedCode/UnitClasses/Mass.g.cs | 18 +++++++++++++----- .../GeneratedCode/UnitClasses/Power.g.cs | 18 +++++++++++++----- .../GeneratedCode/UnitClasses/PowerRatio.g.cs | 18 +++++++++++++----- .../GeneratedCode/UnitClasses/Pressure.g.cs | 18 +++++++++++++----- .../GeneratedCode/UnitClasses/Ratio.g.cs | 18 +++++++++++++----- .../UnitClasses/RotationalSpeed.g.cs | 18 +++++++++++++----- .../GeneratedCode/UnitClasses/Speed.g.cs | 18 +++++++++++++----- .../GeneratedCode/UnitClasses/Temperature.g.cs | 18 +++++++++++++----- .../GeneratedCode/UnitClasses/Torque.g.cs | 18 +++++++++++++----- .../GeneratedCode/UnitClasses/Volume.g.cs | 18 +++++++++++++----- 25 files changed, 325 insertions(+), 125 deletions(-) diff --git a/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 b/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 index e2af3da923..c04acfe63a 100644 --- a/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 +++ b/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 @@ -281,11 +281,19 @@ namespace UnitsNet { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs index 7cf49917ba..99420e932a 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs @@ -248,11 +248,19 @@ public static Acceleration Parse(string str, IFormatProvider formatProvider = nu { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs index 8e8770b3fb..6caf50a119 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs @@ -296,11 +296,19 @@ public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs index aacf5ff32c..0b386db671 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs @@ -288,11 +288,19 @@ public static Angle Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs index 2ce99a461e..5b5e657f91 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs @@ -408,11 +408,19 @@ public static Area Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs index 46b925737f..dfc9fe2d66 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs @@ -428,11 +428,19 @@ public static Duration Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs index af2bf378d8..25d2ab21e4 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs @@ -348,11 +348,19 @@ public static ElectricCurrent Parse(string str, IFormatProvider formatProvider = { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs index 93135a2bf9..13f81e2aa2 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs @@ -328,11 +328,19 @@ public static ElectricPotential Parse(string str, IFormatProvider formatProvider { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs index cc7ab338c5..170117bcbe 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs @@ -288,11 +288,19 @@ public static ElectricResistance Parse(string str, IFormatProvider formatProvide { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs index c36bbd0a46..78c13dd599 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs @@ -308,11 +308,19 @@ public static Flow Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs index e3ed1f9d0f..3d1d19e674 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs @@ -368,11 +368,19 @@ public static Force Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs index 88cf90a428..2d0de16ed0 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs @@ -328,11 +328,19 @@ public static Frequency Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs index 75a589253f..73f1dfef0f 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs @@ -748,11 +748,19 @@ public static Information Parse(string str, IFormatProvider formatProvider = nul { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs index 2a8a7f9f9c..7ac736c7d1 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs @@ -488,11 +488,19 @@ public static Length Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs index 80c034169e..01fd451a60 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs @@ -276,11 +276,19 @@ public static Level Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs index bd528ebdca..7cfb0f7203 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs @@ -548,11 +548,19 @@ public static Mass Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs index e1b061614d..fbb77a8499 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs @@ -448,11 +448,19 @@ public static Power Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs index c558fbb3bd..c00d840392 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs @@ -276,11 +276,19 @@ public static PowerRatio Parse(string str, IFormatProvider formatProvider = null { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs index 2a10751a4e..46e966a521 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs @@ -468,11 +468,19 @@ public static Pressure Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs index c6e5fadd87..3725673032 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs @@ -348,11 +348,19 @@ public static Ratio Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs index 70e824bda0..b1083c2e22 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs @@ -268,11 +268,19 @@ public static RotationalSpeed Parse(string str, IFormatProvider formatProvider = { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs index c519991c27..4749dfe73c 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs @@ -328,11 +328,19 @@ public static Speed Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs index ea259743b4..bf311fbc70 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs @@ -388,11 +388,19 @@ public static Temperature Parse(string str, IFormatProvider formatProvider = nul { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs index 58cdfe35ec..5dd22f62ae 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs @@ -248,11 +248,19 @@ public static Torque Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs index a99bc0740a..845d888b46 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs @@ -628,11 +628,19 @@ public static Volume Parse(string str, IFormatProvider formatProvider = null) { if (str == null) throw new ArgumentNullException("str"); - const string regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?[., \d]*\d(?:[eE][-+]?[., \d]*\d)?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + + var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + @"]*\d"; // ensures quantity ends in digit + var regexString = @"\s*" // ignore leading whitespace + + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input + + @"\s?" // ignore whitespace (if any) + + @"(?\S+)" // capture Unit (non-whitespace) input + + @"\s*"; // ignore trailing whitespace var regex = new Regex(regexString); GroupCollection groups = regex.Match(str).Groups; From 5413159a1cb2052c4374bbb9d2438245ad5bf597 Mon Sep 17 00:00:00 2001 From: Maher Kassim Date: Mon, 23 Mar 2015 13:43:54 -0400 Subject: [PATCH 03/10] Switched parsing to use Trim(), exponential regex to just match digits, and minor corrections --- .../Include-GenerateUnitClassSourceCode.ps1 | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 b/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 index c04acfe63a..de2b40d708 100644 --- a/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 +++ b/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 @@ -39,7 +39,6 @@ using System.Linq; using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -287,15 +286,14 @@ namespace UnitsNet var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -303,7 +301,7 @@ namespace UnitsNet if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; From bdb087b835b9bde001446f44b366a8df4f1563c4 Mon Sep 17 00:00:00 2001 From: Maher Kassim Date: Mon, 23 Mar 2015 13:45:08 -0400 Subject: [PATCH 04/10] GeneratedCode for parsing corrections --- .../GeneratedCode/UnitClasses/Acceleration.g.cs | 16 +++++++--------- .../UnitClasses/AmplitudeRatio.g.cs | 16 +++++++--------- .../GeneratedCode/UnitClasses/Angle.g.cs | 16 +++++++--------- Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs | 16 +++++++--------- .../GeneratedCode/UnitClasses/Duration.g.cs | 16 +++++++--------- .../UnitClasses/ElectricCurrent.g.cs | 16 +++++++--------- .../UnitClasses/ElectricPotential.g.cs | 16 +++++++--------- .../UnitClasses/ElectricResistance.g.cs | 16 +++++++--------- Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs | 16 +++++++--------- .../GeneratedCode/UnitClasses/Force.g.cs | 16 +++++++--------- .../GeneratedCode/UnitClasses/Frequency.g.cs | 16 +++++++--------- .../GeneratedCode/UnitClasses/Information.g.cs | 16 +++++++--------- .../GeneratedCode/UnitClasses/Length.g.cs | 16 +++++++--------- .../GeneratedCode/UnitClasses/Level.g.cs | 16 +++++++--------- Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs | 16 +++++++--------- .../GeneratedCode/UnitClasses/Power.g.cs | 16 +++++++--------- .../GeneratedCode/UnitClasses/PowerRatio.g.cs | 16 +++++++--------- .../GeneratedCode/UnitClasses/Pressure.g.cs | 16 +++++++--------- .../GeneratedCode/UnitClasses/Ratio.g.cs | 16 +++++++--------- .../UnitClasses/RotationalSpeed.g.cs | 16 +++++++--------- .../GeneratedCode/UnitClasses/Speed.g.cs | 16 +++++++--------- .../GeneratedCode/UnitClasses/Temperature.g.cs | 16 +++++++--------- .../GeneratedCode/UnitClasses/Torque.g.cs | 16 +++++++--------- .../GeneratedCode/UnitClasses/Volume.g.cs | 16 +++++++--------- 24 files changed, 168 insertions(+), 216 deletions(-) diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs index 99420e932a..f0a7dbbaf8 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -254,15 +253,14 @@ public static Acceleration Parse(string str, IFormatProvider formatProvider = nu var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -270,7 +268,7 @@ public static Acceleration Parse(string str, IFormatProvider formatProvider = nu if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs index 6caf50a119..21eb7fa7b4 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -302,15 +301,14 @@ public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -318,7 +316,7 @@ public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs index 0b386db671..854866139c 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -294,15 +293,14 @@ public static Angle Parse(string str, IFormatProvider formatProvider = null) var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -310,7 +308,7 @@ public static Angle Parse(string str, IFormatProvider formatProvider = null) if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs index 5b5e657f91..2617ce035e 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -414,15 +413,14 @@ public static Area Parse(string str, IFormatProvider formatProvider = null) var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -430,7 +428,7 @@ public static Area Parse(string str, IFormatProvider formatProvider = null) if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs index dfc9fe2d66..7a00b1cec1 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -434,15 +433,14 @@ public static Duration Parse(string str, IFormatProvider formatProvider = null) var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -450,7 +448,7 @@ public static Duration Parse(string str, IFormatProvider formatProvider = null) if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs index 25d2ab21e4..5248430b06 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -354,15 +353,14 @@ public static ElectricCurrent Parse(string str, IFormatProvider formatProvider = var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -370,7 +368,7 @@ public static ElectricCurrent Parse(string str, IFormatProvider formatProvider = if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs index 13f81e2aa2..e0c167b72c 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -334,15 +333,14 @@ public static ElectricPotential Parse(string str, IFormatProvider formatProvider var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -350,7 +348,7 @@ public static ElectricPotential Parse(string str, IFormatProvider formatProvider if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs index 170117bcbe..1659af465f 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -294,15 +293,14 @@ public static ElectricResistance Parse(string str, IFormatProvider formatProvide var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -310,7 +308,7 @@ public static ElectricResistance Parse(string str, IFormatProvider formatProvide if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs index 78c13dd599..4951944b7a 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -314,15 +313,14 @@ public static Flow Parse(string str, IFormatProvider formatProvider = null) var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -330,7 +328,7 @@ public static Flow Parse(string str, IFormatProvider formatProvider = null) if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs index 3d1d19e674..98674bb759 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -374,15 +373,14 @@ public static Force Parse(string str, IFormatProvider formatProvider = null) var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -390,7 +388,7 @@ public static Force Parse(string str, IFormatProvider formatProvider = null) if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs index 2d0de16ed0..d47c105214 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -334,15 +333,14 @@ public static Frequency Parse(string str, IFormatProvider formatProvider = null) var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -350,7 +348,7 @@ public static Frequency Parse(string str, IFormatProvider formatProvider = null) if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs index 73f1dfef0f..18fc554b0f 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -754,15 +753,14 @@ public static Information Parse(string str, IFormatProvider formatProvider = nul var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -770,7 +768,7 @@ public static Information Parse(string str, IFormatProvider formatProvider = nul if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs index 7ac736c7d1..cc3803911b 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -494,15 +493,14 @@ public static Length Parse(string str, IFormatProvider formatProvider = null) var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -510,7 +508,7 @@ public static Length Parse(string str, IFormatProvider formatProvider = null) if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs index 01fd451a60..7a53e11b9a 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -282,15 +281,14 @@ public static Level Parse(string str, IFormatProvider formatProvider = null) var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -298,7 +296,7 @@ public static Level Parse(string str, IFormatProvider formatProvider = null) if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs index 7cfb0f7203..83850e9cef 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -554,15 +553,14 @@ public static Mass Parse(string str, IFormatProvider formatProvider = null) var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -570,7 +568,7 @@ public static Mass Parse(string str, IFormatProvider formatProvider = null) if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs index fbb77a8499..e3b180099d 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -454,15 +453,14 @@ public static Power Parse(string str, IFormatProvider formatProvider = null) var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -470,7 +468,7 @@ public static Power Parse(string str, IFormatProvider formatProvider = null) if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs index c00d840392..1e08ec1da6 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -282,15 +281,14 @@ public static PowerRatio Parse(string str, IFormatProvider formatProvider = null var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -298,7 +296,7 @@ public static PowerRatio Parse(string str, IFormatProvider formatProvider = null if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs index 46e966a521..e0a86cf122 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -474,15 +473,14 @@ public static Pressure Parse(string str, IFormatProvider formatProvider = null) var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -490,7 +488,7 @@ public static Pressure Parse(string str, IFormatProvider formatProvider = null) if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs index 3725673032..deba38f841 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -354,15 +353,14 @@ public static Ratio Parse(string str, IFormatProvider formatProvider = null) var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -370,7 +368,7 @@ public static Ratio Parse(string str, IFormatProvider formatProvider = null) if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs index b1083c2e22..fb1e78cbfe 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -274,15 +273,14 @@ public static RotationalSpeed Parse(string str, IFormatProvider formatProvider = var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -290,7 +288,7 @@ public static RotationalSpeed Parse(string str, IFormatProvider formatProvider = if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs index 4749dfe73c..62eb3c6f9b 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -334,15 +333,14 @@ public static Speed Parse(string str, IFormatProvider formatProvider = null) var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -350,7 +348,7 @@ public static Speed Parse(string str, IFormatProvider formatProvider = null) if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs index bf311fbc70..e5493e48fb 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -394,15 +393,14 @@ public static Temperature Parse(string str, IFormatProvider formatProvider = nul var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -410,7 +408,7 @@ public static Temperature Parse(string str, IFormatProvider formatProvider = nul if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs index 5dd22f62ae..07ce099da3 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -254,15 +253,14 @@ public static Torque Parse(string str, IFormatProvider formatProvider = null) var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -270,7 +268,7 @@ public static Torque Parse(string str, IFormatProvider formatProvider = null) if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs index 845d888b46..0055084d18 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs @@ -26,7 +26,6 @@ using JetBrains.Annotations; using UnitsNet.Units; - // ReSharper disable once CheckNamespace namespace UnitsNet @@ -634,15 +633,14 @@ public static Volume Parse(string str, IFormatProvider formatProvider = null) var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's group separator + + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator + @"]*\d"; // ensures quantity ends in digit - var regexString = @"\s*" // ignore leading whitespace - + @"(?[-+]?" + numRegex + @"(?:[eE][-+]?" + numRegex + @")?)" // capture Quantity input - + @"\s?" // ignore whitespace (if any) - + @"(?\S+)" // capture Unit (non-whitespace) input - + @"\s*"; // ignore trailing whitespace + var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input + + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") + + @"(?\S+)"; // capture Unit (non-whitespace) input + var regex = new Regex(regexString); - GroupCollection groups = regex.Match(str).Groups; + GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; @@ -650,7 +648,7 @@ public static Volume Parse(string str, IFormatProvider formatProvider = null) if (valueString == "" || unitString == "") { var ex = new ArgumentException( - "Expected valid quantity and unit. Input string needs to be in the format \" \".", "str"); + "Expected valid quantity and unit. Input string needs to be in the format \" or \".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; From 718df8c1302fe3b8b971dfc310f88b6a636df2ad Mon Sep 17 00:00:00 2001 From: Maher Kassim Date: Mon, 23 Mar 2015 15:08:34 -0400 Subject: [PATCH 05/10] Separated out unit parsing and added unit parsing tests, Updated default parsing culture --- .../Include-GenerateUnitClassSourceCode.ps1 | 32 ++++++++++++++++--- Tests/CustomCode/ParseTests.cs | 13 +++++++- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 b/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 index de2b40d708..3e9f2d55b2 100644 --- a/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 +++ b/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 @@ -282,7 +282,7 @@ namespace UnitsNet var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -309,9 +309,7 @@ namespace UnitsNet try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - $unitEnumName unit = unitSystem.Parse<$unitEnumName>(unitString); + $unitEnumName unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -325,6 +323,32 @@ namespace UnitsNet } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static $unitEnumName ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse<$unitEnumName>(str.Trim()); + + if (unit == $unitEnumName.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized $unitEnumName."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Tests/CustomCode/ParseTests.cs b/Tests/CustomCode/ParseTests.cs index fa185fcaa6..22a662733b 100644 --- a/Tests/CustomCode/ParseTests.cs +++ b/Tests/CustomCode/ParseTests.cs @@ -22,6 +22,7 @@ using System; using System.Globalization; using NUnit.Framework; +using UnitsNet.Units; namespace UnitsNet.Tests.CustomCode { @@ -67,7 +68,7 @@ public double ParseWithCultureUsingSpaceAsThousandSeparators(string s) /// Error parsing string. [TestCase("5,5 m", Result = 5.5)] [TestCase("500.005.050,001 m", Result = 500005050.001)] - [TestCase("5.5 m", Result = 55)] // dot is group separator not decimal + [TestCase("5.555 m", Result = 5555)] // dot is group separator not decimal [TestCase("500 005 m", ExpectedExceptionName = "UnitsNet.UnitsNetException")] // quantity doesn't match number format public double ParseWithCultureUsingDotAsThousandSeparators(string s) { @@ -77,5 +78,15 @@ public double ParseWithCultureUsingDotAsThousandSeparators(string s) return Length.Parse(s, numberFormat).Meters; } + + [TestCase("m", Result = LengthUnit.Meter)] + [TestCase("kg", ExpectedExceptionName = "UnitsNet.UnitsNetException")] + [TestCase(null, ExpectedExceptionName = "System.ArgumentNullException")] + public LengthUnit ParseLengthUnitUsEnglish(string s) + { + var usEnglish = CultureInfo.GetCultureInfo("en-US"); + + return Length.ParseUnit(s, usEnglish); + } } } \ No newline at end of file From 78424662a36e105ca9471ea43ec0e690bf554b29 Mon Sep 17 00:00:00 2001 From: Maher Kassim Date: Mon, 23 Mar 2015 15:09:28 -0400 Subject: [PATCH 06/10] GeneratedCode for Unit Parsing and Culture Info changes --- .../UnitClasses/Acceleration.g.cs | 32 ++++++++++++++++--- .../UnitClasses/AmplitudeRatio.g.cs | 32 ++++++++++++++++--- .../GeneratedCode/UnitClasses/Angle.g.cs | 32 ++++++++++++++++--- .../GeneratedCode/UnitClasses/Area.g.cs | 32 ++++++++++++++++--- .../GeneratedCode/UnitClasses/Duration.g.cs | 32 ++++++++++++++++--- .../UnitClasses/ElectricCurrent.g.cs | 32 ++++++++++++++++--- .../UnitClasses/ElectricPotential.g.cs | 32 ++++++++++++++++--- .../UnitClasses/ElectricResistance.g.cs | 32 ++++++++++++++++--- .../GeneratedCode/UnitClasses/Flow.g.cs | 32 ++++++++++++++++--- .../GeneratedCode/UnitClasses/Force.g.cs | 32 ++++++++++++++++--- .../GeneratedCode/UnitClasses/Frequency.g.cs | 32 ++++++++++++++++--- .../UnitClasses/Information.g.cs | 32 ++++++++++++++++--- .../GeneratedCode/UnitClasses/Length.g.cs | 32 ++++++++++++++++--- .../GeneratedCode/UnitClasses/Level.g.cs | 32 ++++++++++++++++--- .../GeneratedCode/UnitClasses/Mass.g.cs | 32 ++++++++++++++++--- .../GeneratedCode/UnitClasses/Power.g.cs | 32 ++++++++++++++++--- .../GeneratedCode/UnitClasses/PowerRatio.g.cs | 32 ++++++++++++++++--- .../GeneratedCode/UnitClasses/Pressure.g.cs | 32 ++++++++++++++++--- .../GeneratedCode/UnitClasses/Ratio.g.cs | 32 ++++++++++++++++--- .../UnitClasses/RotationalSpeed.g.cs | 32 ++++++++++++++++--- .../GeneratedCode/UnitClasses/Speed.g.cs | 32 ++++++++++++++++--- .../UnitClasses/Temperature.g.cs | 32 ++++++++++++++++--- .../GeneratedCode/UnitClasses/Torque.g.cs | 32 ++++++++++++++++--- .../GeneratedCode/UnitClasses/Volume.g.cs | 32 ++++++++++++++++--- 24 files changed, 672 insertions(+), 96 deletions(-) diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs index f0a7dbbaf8..de965023fb 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs @@ -249,7 +249,7 @@ public static Acceleration Parse(string str, IFormatProvider formatProvider = nu var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -276,9 +276,7 @@ public static Acceleration Parse(string str, IFormatProvider formatProvider = nu try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - AccelerationUnit unit = unitSystem.Parse(unitString); + AccelerationUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -292,6 +290,32 @@ public static Acceleration Parse(string str, IFormatProvider formatProvider = nu } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static AccelerationUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == AccelerationUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AccelerationUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs index 21eb7fa7b4..b1429ba25f 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs @@ -297,7 +297,7 @@ public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -324,9 +324,7 @@ public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - AmplitudeRatioUnit unit = unitSystem.Parse(unitString); + AmplitudeRatioUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -340,6 +338,32 @@ public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static AmplitudeRatioUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == AmplitudeRatioUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AmplitudeRatioUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs index 854866139c..7fae5fd03c 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs @@ -289,7 +289,7 @@ public static Angle Parse(string str, IFormatProvider formatProvider = null) var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -316,9 +316,7 @@ public static Angle Parse(string str, IFormatProvider formatProvider = null) try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - AngleUnit unit = unitSystem.Parse(unitString); + AngleUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -332,6 +330,32 @@ public static Angle Parse(string str, IFormatProvider formatProvider = null) } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static AngleUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == AngleUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AngleUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs index 2617ce035e..535a5ecaa1 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs @@ -409,7 +409,7 @@ public static Area Parse(string str, IFormatProvider formatProvider = null) var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -436,9 +436,7 @@ public static Area Parse(string str, IFormatProvider formatProvider = null) try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - AreaUnit unit = unitSystem.Parse(unitString); + AreaUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -452,6 +450,32 @@ public static Area Parse(string str, IFormatProvider formatProvider = null) } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static AreaUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == AreaUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AreaUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs index 7a00b1cec1..8a9f202c50 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs @@ -429,7 +429,7 @@ public static Duration Parse(string str, IFormatProvider formatProvider = null) var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -456,9 +456,7 @@ public static Duration Parse(string str, IFormatProvider formatProvider = null) try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - DurationUnit unit = unitSystem.Parse(unitString); + DurationUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -472,6 +470,32 @@ public static Duration Parse(string str, IFormatProvider formatProvider = null) } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static DurationUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == DurationUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized DurationUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs index 5248430b06..c30d0d23f9 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs @@ -349,7 +349,7 @@ public static ElectricCurrent Parse(string str, IFormatProvider formatProvider = var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -376,9 +376,7 @@ public static ElectricCurrent Parse(string str, IFormatProvider formatProvider = try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - ElectricCurrentUnit unit = unitSystem.Parse(unitString); + ElectricCurrentUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -392,6 +390,32 @@ public static ElectricCurrent Parse(string str, IFormatProvider formatProvider = } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static ElectricCurrentUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ElectricCurrentUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricCurrentUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs index e0c167b72c..6409b73de7 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs @@ -329,7 +329,7 @@ public static ElectricPotential Parse(string str, IFormatProvider formatProvider var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -356,9 +356,7 @@ public static ElectricPotential Parse(string str, IFormatProvider formatProvider try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - ElectricPotentialUnit unit = unitSystem.Parse(unitString); + ElectricPotentialUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -372,6 +370,32 @@ public static ElectricPotential Parse(string str, IFormatProvider formatProvider } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static ElectricPotentialUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ElectricPotentialUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricPotentialUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs index 1659af465f..0e9bcaffc0 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs @@ -289,7 +289,7 @@ public static ElectricResistance Parse(string str, IFormatProvider formatProvide var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -316,9 +316,7 @@ public static ElectricResistance Parse(string str, IFormatProvider formatProvide try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - ElectricResistanceUnit unit = unitSystem.Parse(unitString); + ElectricResistanceUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -332,6 +330,32 @@ public static ElectricResistance Parse(string str, IFormatProvider formatProvide } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static ElectricResistanceUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ElectricResistanceUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ElectricResistanceUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs index 4951944b7a..f935f55f54 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs @@ -309,7 +309,7 @@ public static Flow Parse(string str, IFormatProvider formatProvider = null) var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -336,9 +336,7 @@ public static Flow Parse(string str, IFormatProvider formatProvider = null) try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - FlowUnit unit = unitSystem.Parse(unitString); + FlowUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -352,6 +350,32 @@ public static Flow Parse(string str, IFormatProvider formatProvider = null) } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static FlowUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == FlowUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized FlowUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs index 98674bb759..1f126a0d7c 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs @@ -369,7 +369,7 @@ public static Force Parse(string str, IFormatProvider formatProvider = null) var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -396,9 +396,7 @@ public static Force Parse(string str, IFormatProvider formatProvider = null) try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - ForceUnit unit = unitSystem.Parse(unitString); + ForceUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -412,6 +410,32 @@ public static Force Parse(string str, IFormatProvider formatProvider = null) } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static ForceUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == ForceUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized ForceUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs index d47c105214..79093367be 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs @@ -329,7 +329,7 @@ public static Frequency Parse(string str, IFormatProvider formatProvider = null) var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -356,9 +356,7 @@ public static Frequency Parse(string str, IFormatProvider formatProvider = null) try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - FrequencyUnit unit = unitSystem.Parse(unitString); + FrequencyUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -372,6 +370,32 @@ public static Frequency Parse(string str, IFormatProvider formatProvider = null) } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static FrequencyUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == FrequencyUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized FrequencyUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs index 18fc554b0f..839c24dfd5 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs @@ -749,7 +749,7 @@ public static Information Parse(string str, IFormatProvider formatProvider = nul var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -776,9 +776,7 @@ public static Information Parse(string str, IFormatProvider formatProvider = nul try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - InformationUnit unit = unitSystem.Parse(unitString); + InformationUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -792,6 +790,32 @@ public static Information Parse(string str, IFormatProvider formatProvider = nul } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static InformationUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == InformationUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized InformationUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs index cc3803911b..57aa95d41f 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs @@ -489,7 +489,7 @@ public static Length Parse(string str, IFormatProvider formatProvider = null) var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -516,9 +516,7 @@ public static Length Parse(string str, IFormatProvider formatProvider = null) try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - LengthUnit unit = unitSystem.Parse(unitString); + LengthUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -532,6 +530,32 @@ public static Length Parse(string str, IFormatProvider formatProvider = null) } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static LengthUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == LengthUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LengthUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs index 7a53e11b9a..9f1b708bca 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs @@ -277,7 +277,7 @@ public static Level Parse(string str, IFormatProvider formatProvider = null) var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -304,9 +304,7 @@ public static Level Parse(string str, IFormatProvider formatProvider = null) try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - LevelUnit unit = unitSystem.Parse(unitString); + LevelUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -320,6 +318,32 @@ public static Level Parse(string str, IFormatProvider formatProvider = null) } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static LevelUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == LevelUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized LevelUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs index 83850e9cef..7bef9f4d19 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs @@ -549,7 +549,7 @@ public static Mass Parse(string str, IFormatProvider formatProvider = null) var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -576,9 +576,7 @@ public static Mass Parse(string str, IFormatProvider formatProvider = null) try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - MassUnit unit = unitSystem.Parse(unitString); + MassUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -592,6 +590,32 @@ public static Mass Parse(string str, IFormatProvider formatProvider = null) } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static MassUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == MassUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized MassUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs index e3b180099d..2b73f52c88 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs @@ -449,7 +449,7 @@ public static Power Parse(string str, IFormatProvider formatProvider = null) var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -476,9 +476,7 @@ public static Power Parse(string str, IFormatProvider formatProvider = null) try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - PowerUnit unit = unitSystem.Parse(unitString); + PowerUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -492,6 +490,32 @@ public static Power Parse(string str, IFormatProvider formatProvider = null) } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static PowerUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == PowerUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PowerUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs index 1e08ec1da6..322f95119a 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs @@ -277,7 +277,7 @@ public static PowerRatio Parse(string str, IFormatProvider formatProvider = null var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -304,9 +304,7 @@ public static PowerRatio Parse(string str, IFormatProvider formatProvider = null try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - PowerRatioUnit unit = unitSystem.Parse(unitString); + PowerRatioUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -320,6 +318,32 @@ public static PowerRatio Parse(string str, IFormatProvider formatProvider = null } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static PowerRatioUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == PowerRatioUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PowerRatioUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs index e0a86cf122..5e72cf219a 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs @@ -469,7 +469,7 @@ public static Pressure Parse(string str, IFormatProvider formatProvider = null) var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -496,9 +496,7 @@ public static Pressure Parse(string str, IFormatProvider formatProvider = null) try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - PressureUnit unit = unitSystem.Parse(unitString); + PressureUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -512,6 +510,32 @@ public static Pressure Parse(string str, IFormatProvider formatProvider = null) } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static PressureUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == PressureUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized PressureUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs index deba38f841..29f4006162 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs @@ -349,7 +349,7 @@ public static Ratio Parse(string str, IFormatProvider formatProvider = null) var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -376,9 +376,7 @@ public static Ratio Parse(string str, IFormatProvider formatProvider = null) try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - RatioUnit unit = unitSystem.Parse(unitString); + RatioUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -392,6 +390,32 @@ public static Ratio Parse(string str, IFormatProvider formatProvider = null) } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static RatioUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == RatioUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized RatioUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs index fb1e78cbfe..b267037f08 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs @@ -269,7 +269,7 @@ public static RotationalSpeed Parse(string str, IFormatProvider formatProvider = var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -296,9 +296,7 @@ public static RotationalSpeed Parse(string str, IFormatProvider formatProvider = try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - RotationalSpeedUnit unit = unitSystem.Parse(unitString); + RotationalSpeedUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -312,6 +310,32 @@ public static RotationalSpeed Parse(string str, IFormatProvider formatProvider = } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static RotationalSpeedUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == RotationalSpeedUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized RotationalSpeedUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs index 62eb3c6f9b..7275f86e6b 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs @@ -329,7 +329,7 @@ public static Speed Parse(string str, IFormatProvider formatProvider = null) var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -356,9 +356,7 @@ public static Speed Parse(string str, IFormatProvider formatProvider = null) try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - SpeedUnit unit = unitSystem.Parse(unitString); + SpeedUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -372,6 +370,32 @@ public static Speed Parse(string str, IFormatProvider formatProvider = null) } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static SpeedUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == SpeedUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized SpeedUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs index e5493e48fb..f7d0d17c43 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs @@ -389,7 +389,7 @@ public static Temperature Parse(string str, IFormatProvider formatProvider = nul var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -416,9 +416,7 @@ public static Temperature Parse(string str, IFormatProvider formatProvider = nul try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - TemperatureUnit unit = unitSystem.Parse(unitString); + TemperatureUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -432,6 +430,32 @@ public static Temperature Parse(string str, IFormatProvider formatProvider = nul } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static TemperatureUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == TemperatureUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized TemperatureUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs index 07ce099da3..3066ea9c92 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs @@ -249,7 +249,7 @@ public static Torque Parse(string str, IFormatProvider formatProvider = null) var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -276,9 +276,7 @@ public static Torque Parse(string str, IFormatProvider formatProvider = null) try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - TorqueUnit unit = unitSystem.Parse(unitString); + TorqueUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -292,6 +290,32 @@ public static Torque Parse(string str, IFormatProvider formatProvider = null) } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static TorqueUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == TorqueUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized TorqueUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs index 0055084d18..7d02eb9efe 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs @@ -629,7 +629,7 @@ public static Volume Parse(string str, IFormatProvider formatProvider = null) var numFormat = formatProvider != null ? (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : - (NumberFormatInfo) CultureInfo.CurrentCulture.NumberFormat.Clone(); + NumberFormatInfo.CurrentInfo; var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator @@ -656,9 +656,7 @@ public static Volume Parse(string str, IFormatProvider formatProvider = null) try { - var unitSystem = UnitSystem.GetCached(formatProvider); - - VolumeUnit unit = unitSystem.Parse(unitString); + VolumeUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return From(value, unit); @@ -672,6 +670,32 @@ public static Volume Parse(string str, IFormatProvider formatProvider = null) } } + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static VolumeUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + var unitSystem = UnitSystem.GetCached(formatProvider); + + var unit = unitSystem.Parse(str.Trim()); + + if (unit == VolumeUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized VolumeUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + #endregion /// From 369e4c37c2c7fdabd3d3c40691ded07714bb8103 Mon Sep 17 00:00:00 2001 From: Maher Kassim Date: Tue, 24 Mar 2015 09:23:10 -0400 Subject: [PATCH 07/10] Switched to using string.Format instead of concatenation via "+" --- .../Include-GenerateUnitClassSourceCode.ps1 | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 b/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 index 3e9f2d55b2..3b46ab850a 100644 --- a/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 +++ b/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 @@ -284,13 +284,17 @@ namespace UnitsNet (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; From 3b5fd71872b16390cdd13127085d7542e17d3853 Mon Sep 17 00:00:00 2001 From: Maher Kassim Date: Tue, 24 Mar 2015 09:23:56 -0400 Subject: [PATCH 08/10] GeneratedCode for Switch to using string.Format --- .../UnitClasses/Acceleration.g.cs | 18 +++++++++++------- .../UnitClasses/AmplitudeRatio.g.cs | 18 +++++++++++------- .../GeneratedCode/UnitClasses/Angle.g.cs | 18 +++++++++++------- .../GeneratedCode/UnitClasses/Area.g.cs | 18 +++++++++++------- .../GeneratedCode/UnitClasses/Duration.g.cs | 18 +++++++++++------- .../UnitClasses/ElectricCurrent.g.cs | 18 +++++++++++------- .../UnitClasses/ElectricPotential.g.cs | 18 +++++++++++------- .../UnitClasses/ElectricResistance.g.cs | 18 +++++++++++------- .../GeneratedCode/UnitClasses/Flow.g.cs | 18 +++++++++++------- .../GeneratedCode/UnitClasses/Force.g.cs | 18 +++++++++++------- .../GeneratedCode/UnitClasses/Frequency.g.cs | 18 +++++++++++------- .../GeneratedCode/UnitClasses/Information.g.cs | 18 +++++++++++------- .../GeneratedCode/UnitClasses/Length.g.cs | 18 +++++++++++------- .../GeneratedCode/UnitClasses/Level.g.cs | 18 +++++++++++------- .../GeneratedCode/UnitClasses/Mass.g.cs | 18 +++++++++++------- .../GeneratedCode/UnitClasses/Power.g.cs | 18 +++++++++++------- .../GeneratedCode/UnitClasses/PowerRatio.g.cs | 18 +++++++++++------- .../GeneratedCode/UnitClasses/Pressure.g.cs | 18 +++++++++++------- .../GeneratedCode/UnitClasses/Ratio.g.cs | 18 +++++++++++------- .../UnitClasses/RotationalSpeed.g.cs | 18 +++++++++++------- .../GeneratedCode/UnitClasses/Speed.g.cs | 18 +++++++++++------- .../GeneratedCode/UnitClasses/Temperature.g.cs | 18 +++++++++++------- .../GeneratedCode/UnitClasses/Torque.g.cs | 18 +++++++++++------- .../GeneratedCode/UnitClasses/Volume.g.cs | 18 +++++++++++------- 24 files changed, 264 insertions(+), 168 deletions(-) diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs index de965023fb..3f97bb1f6f 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs @@ -251,13 +251,17 @@ public static Acceleration Parse(string str, IFormatProvider formatProvider = nu (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs index b1429ba25f..78a303aba0 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs @@ -299,13 +299,17 @@ public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs index 7fae5fd03c..98df22f2de 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs @@ -291,13 +291,17 @@ public static Angle Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs index 535a5ecaa1..8d1fb705b3 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs @@ -411,13 +411,17 @@ public static Area Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs index 8a9f202c50..9e360de433 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs @@ -431,13 +431,17 @@ public static Duration Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs index c30d0d23f9..74485f3928 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs @@ -351,13 +351,17 @@ public static ElectricCurrent Parse(string str, IFormatProvider formatProvider = (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs index 6409b73de7..9dc72cd8ee 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs @@ -331,13 +331,17 @@ public static ElectricPotential Parse(string str, IFormatProvider formatProvider (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs index 0e9bcaffc0..39df1cd7e8 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs @@ -291,13 +291,17 @@ public static ElectricResistance Parse(string str, IFormatProvider formatProvide (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs index f935f55f54..31fe288fcc 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs @@ -311,13 +311,17 @@ public static Flow Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs index 1f126a0d7c..746cf4de31 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs @@ -371,13 +371,17 @@ public static Force Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs index 79093367be..c45b70c53d 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs @@ -331,13 +331,17 @@ public static Frequency Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs index 839c24dfd5..83d88071e9 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs @@ -751,13 +751,17 @@ public static Information Parse(string str, IFormatProvider formatProvider = nul (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs index 57aa95d41f..71540d7fcb 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs @@ -491,13 +491,17 @@ public static Length Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs index 9f1b708bca..01695d4be9 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs @@ -279,13 +279,17 @@ public static Level Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs index 7bef9f4d19..5db71c1f98 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs @@ -551,13 +551,17 @@ public static Mass Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs index 2b73f52c88..7fd1ccbc40 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs @@ -451,13 +451,17 @@ public static Power Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs index 322f95119a..3ee84bc30d 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs @@ -279,13 +279,17 @@ public static PowerRatio Parse(string str, IFormatProvider formatProvider = null (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs index 5e72cf219a..0acff509ef 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs @@ -471,13 +471,17 @@ public static Pressure Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs index 29f4006162..047c9fbe2a 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs @@ -351,13 +351,17 @@ public static Ratio Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs index b267037f08..0da91cb892 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs @@ -271,13 +271,17 @@ public static RotationalSpeed Parse(string str, IFormatProvider formatProvider = (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs index 7275f86e6b..6d36ba61d4 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs @@ -331,13 +331,17 @@ public static Speed Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs index f7d0d17c43..f62ed14284 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs @@ -391,13 +391,17 @@ public static Temperature Parse(string str, IFormatProvider formatProvider = nul (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs index 3066ea9c92..2a4f9f4bb9 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs @@ -251,13 +251,17 @@ public static Torque Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs index 7d02eb9efe..23387fd3ac 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs @@ -631,13 +631,17 @@ public static Volume Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = @"[\d., " // allows digits, dots, commas, and spaces in the number by default - + numFormat.NumberGroupSeparator // adds provided (or current) culture's group separator - + numFormat.NumberDecimalSeparator // adds provided (or current) culture's decimal separator - + @"]*\d"; // ensures quantity ends in digit - var regexString = @"(?[-+]?" + numRegex + @"(?:[eE][-+]?\d+)?)" // capture Quantity input - + @"\s?" // ignore whitespace (allows both "1kg", "1 kg") - + @"(?\S+)"; // capture Unit (non-whitespace) input + var numRegex = string.Format("{0}{1}{2}{3}", + @"[\d., ", // allows digits, dots, commas, and spaces in the number by default + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator + @"]*\d"); // ensures quantity ends in digit + var regexString = string.Format("{0}{1}{2}{3}{4}", + @"(?[-+]?", // start capturing Quantity + numRegex, // parse base (integral) Quantity value + @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; From 7b0a1f0c3d5685e4e6140eeef83512c8782f76b8 Mon Sep 17 00:00:00 2001 From: Maher Kassim Date: Tue, 24 Mar 2015 16:04:33 -0400 Subject: [PATCH 09/10] Updated regex format strings --- .../Include-GenerateUnitClassSourceCode.ps1 | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 b/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 index 3b46ab850a..b2346a374a 100644 --- a/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 +++ b/Src/Scripts/Include-GenerateUnitClassSourceCode.ps1 @@ -284,17 +284,14 @@ namespace UnitsNet (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; From 6662d952e4d5fd5ad2d9ef2dcd8c77b37b115d37 Mon Sep 17 00:00:00 2001 From: Maher Kassim Date: Tue, 24 Mar 2015 16:04:55 -0400 Subject: [PATCH 10/10] GeneratedCode for updated regex format strings --- .../UnitClasses/Acceleration.g.cs | 19 ++++++++----------- .../UnitClasses/AmplitudeRatio.g.cs | 19 ++++++++----------- .../GeneratedCode/UnitClasses/Angle.g.cs | 19 ++++++++----------- .../GeneratedCode/UnitClasses/Area.g.cs | 19 ++++++++----------- .../GeneratedCode/UnitClasses/Duration.g.cs | 19 ++++++++----------- .../UnitClasses/ElectricCurrent.g.cs | 19 ++++++++----------- .../UnitClasses/ElectricPotential.g.cs | 19 ++++++++----------- .../UnitClasses/ElectricResistance.g.cs | 19 ++++++++----------- .../GeneratedCode/UnitClasses/Flow.g.cs | 19 ++++++++----------- .../GeneratedCode/UnitClasses/Force.g.cs | 19 ++++++++----------- .../GeneratedCode/UnitClasses/Frequency.g.cs | 19 ++++++++----------- .../UnitClasses/Information.g.cs | 19 ++++++++----------- .../GeneratedCode/UnitClasses/Length.g.cs | 19 ++++++++----------- .../GeneratedCode/UnitClasses/Level.g.cs | 19 ++++++++----------- .../GeneratedCode/UnitClasses/Mass.g.cs | 19 ++++++++----------- .../GeneratedCode/UnitClasses/Power.g.cs | 19 ++++++++----------- .../GeneratedCode/UnitClasses/PowerRatio.g.cs | 19 ++++++++----------- .../GeneratedCode/UnitClasses/Pressure.g.cs | 19 ++++++++----------- .../GeneratedCode/UnitClasses/Ratio.g.cs | 19 ++++++++----------- .../UnitClasses/RotationalSpeed.g.cs | 19 ++++++++----------- .../GeneratedCode/UnitClasses/Speed.g.cs | 19 ++++++++----------- .../UnitClasses/Temperature.g.cs | 19 ++++++++----------- .../GeneratedCode/UnitClasses/Torque.g.cs | 19 ++++++++----------- .../GeneratedCode/UnitClasses/Volume.g.cs | 19 ++++++++----------- 24 files changed, 192 insertions(+), 264 deletions(-) diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs index 3f97bb1f6f..13a1c39159 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs @@ -251,17 +251,14 @@ public static Acceleration Parse(string str, IFormatProvider formatProvider = nu (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs index 78a303aba0..da92e1910d 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs @@ -299,17 +299,14 @@ public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs index 98df22f2de..36e0c9e2dd 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs @@ -291,17 +291,14 @@ public static Angle Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs index 8d1fb705b3..ffc2e3b545 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Area.g.cs @@ -411,17 +411,14 @@ public static Area Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs index 9e360de433..3f44d89f11 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs @@ -431,17 +431,14 @@ public static Duration Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs index 74485f3928..99328f11f4 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs @@ -351,17 +351,14 @@ public static ElectricCurrent Parse(string str, IFormatProvider formatProvider = (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs index 9dc72cd8ee..54ead91545 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricPotential.g.cs @@ -331,17 +331,14 @@ public static ElectricPotential Parse(string str, IFormatProvider formatProvider (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs index 39df1cd7e8..377bf7cff9 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/ElectricResistance.g.cs @@ -291,17 +291,14 @@ public static ElectricResistance Parse(string str, IFormatProvider formatProvide (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs index 31fe288fcc..54b238db62 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Flow.g.cs @@ -311,17 +311,14 @@ public static Flow Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs index 746cf4de31..4bbac8ed19 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs @@ -371,17 +371,14 @@ public static Force Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs index c45b70c53d..10dfa22e23 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Frequency.g.cs @@ -331,17 +331,14 @@ public static Frequency Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs index 83d88071e9..ea87e1b325 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs @@ -751,17 +751,14 @@ public static Information Parse(string str, IFormatProvider formatProvider = nul (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs index 71540d7fcb..f2256f0fff 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs @@ -491,17 +491,14 @@ public static Length Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs index 01695d4be9..cf5d3a2df1 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Level.g.cs @@ -279,17 +279,14 @@ public static Level Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs index 5db71c1f98..90993edd74 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Mass.g.cs @@ -551,17 +551,14 @@ public static Mass Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs index 7fd1ccbc40..9a90ab0312 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Power.g.cs @@ -451,17 +451,14 @@ public static Power Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs index 3ee84bc30d..9381ad71ad 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/PowerRatio.g.cs @@ -279,17 +279,14 @@ public static PowerRatio Parse(string str, IFormatProvider formatProvider = null (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs index 0acff509ef..4ae1cbdbb8 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Pressure.g.cs @@ -471,17 +471,14 @@ public static Pressure Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs index 047c9fbe2a..d48d2184ad 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs @@ -351,17 +351,14 @@ public static Ratio Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs index 0da91cb892..f8e8be0a1d 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/RotationalSpeed.g.cs @@ -271,17 +271,14 @@ public static RotationalSpeed Parse(string str, IFormatProvider formatProvider = (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs index 6d36ba61d4..9afbec9a24 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Speed.g.cs @@ -331,17 +331,14 @@ public static Speed Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs index f62ed14284..0f71d7a087 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Temperature.g.cs @@ -391,17 +391,14 @@ public static Temperature Parse(string str, IFormatProvider formatProvider = nul (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs index 2a4f9f4bb9..e1700cca7f 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Torque.g.cs @@ -251,17 +251,14 @@ public static Torque Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs index 23387fd3ac..4827012d0a 100644 --- a/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Volume.g.cs @@ -631,17 +631,14 @@ public static Volume Parse(string str, IFormatProvider formatProvider = null) (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; - var numRegex = string.Format("{0}{1}{2}{3}", - @"[\d., ", // allows digits, dots, commas, and spaces in the number by default - numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator - numFormat.NumberDecimalSeparator, // adds provided (or current) culture's decimal separator - @"]*\d"); // ensures quantity ends in digit - var regexString = string.Format("{0}{1}{2}{3}{4}", - @"(?[-+]?", // start capturing Quantity - numRegex, // parse base (integral) Quantity value - @"(?:[eE][-+]?\d+)?)", // capture Quantity exponential (if any), end capturing Quantity - @"\s?", // ignore whitespace (allows both "1kg", "1 kg") - @"(?\S+)"); // capture Unit (non-whitespace) input + 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}", + 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 var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups;