diff --git a/Gu.Units.Generator/Descriptors/Settings.cs b/Gu.Units.Generator/Descriptors/Settings.cs index f8d8f877..d040ce27 100644 --- a/Gu.Units.Generator/Descriptors/Settings.cs +++ b/Gu.Units.Generator/Descriptors/Settings.cs @@ -134,7 +134,7 @@ public static Settings FromFile(string fullFileName) return settings; } } - + private void Initialize() { foreach (var unit in SiUnits) @@ -158,20 +158,14 @@ private void Initialize() } } } - foreach (var quantity in Quantities.Where(x => x.Unit is DerivedUnit)) + foreach (var left in Quantities) { - var derivedUnit = quantity.Unit as DerivedUnit; - var unitParts = derivedUnit.Parts; - foreach (var up in unitParts) + var tmp = left; + foreach (var result in Quantities.Where(x => x != tmp)) { - if (up.Power > 0) + if (OperatorOverload.CanCreate(left, result)) { - var left = up.Unit.Quantity; - if (OperatorOverload.CanCreate(left, quantity)) - { - left.OperatorOverloads.Add(new OperatorOverload(left, quantity)); - quantity.OperatorOverloads.Add(new OperatorOverload(quantity, left)); - } + left.OperatorOverloads.Add(new OperatorOverload(left, result)); } } } diff --git a/Gu.Units.Generator/WpfStuff/UnitPartsConverter.cs b/Gu.Units.Generator/WpfStuff/UnitPartsConverter.cs index 671cdcb1..4f1b7665 100644 --- a/Gu.Units.Generator/WpfStuff/UnitPartsConverter.cs +++ b/Gu.Units.Generator/WpfStuff/UnitPartsConverter.cs @@ -1,6 +1,7 @@ namespace Gu.Units.Generator.WpfStuff { using System; + using System.Collections.Generic; using System.ComponentModel; using System.Globalization; using System.Linq; @@ -10,7 +11,7 @@ public class UnitPartsConverter : TypeConverter { - private static string[] superscripts = { "¹", "²", "³" }; + private static readonly string[] Superscripts = { "¹", "²", "³" }; public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { return sourceType == typeof(string); @@ -27,31 +28,14 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c { return null; } - int sign = 1; var s = (string)value; - //s = s.Replace("⋅", "*"); if (string.IsNullOrWhiteSpace(s)) { return null; } - var symbols = UnitBase.AllUnitsStatic.Select(x => x.Symbol).ToArray(); - var symbolsPattern = string.Join("|", new[] { "1" }.Concat(symbols)); - var superscriptsPattern = string.Join("|", superscripts); - var pattern = string.Format( -@"(? - (?({0})) - (? - ((?:\^)[\+\-]?\d+) - | - (⁻?({1})) - )? - | - (?[⋅\*\/]) -)", symbolsPattern, superscriptsPattern); - var matches = Regex.Matches(s, pattern, RegexOptions.IgnorePatternWhitespace) - .OfType() - .ToArray(); + var matches = Parse(s); var parts = new UnitParts(); + int sign = 1; bool expectsSymbol = true; foreach (Match match in matches) { @@ -84,12 +68,32 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c } expectsSymbol = true; } - //capture["Unit"] - //parts.Add([""]); } return parts; } + private static IEnumerable Parse(string s) + { + var symbols = UnitBase.AllUnitsStatic.Select(x => x.Symbol).ToArray(); + var symbolsPattern = string.Join("|", new[] { "1" }.Concat(symbols)); + var superscriptsPattern = string.Join("|", Superscripts); + var pattern = string.Format( + @"(? + (?({0})) + (? + ((?:\^)[\+\-]?\d+) + | + (⁻?({1})) + )? + | + (?[⋅\*\/]) +)", symbolsPattern, superscriptsPattern); + var matches = Regex.Matches(s, pattern, RegexOptions.IgnorePatternWhitespace) + .OfType() + .ToArray(); + return matches; + } + public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { return value == null ? null : ((UnitParts)value).Expression; @@ -103,14 +107,14 @@ private int ParsePower(string power) } if (power[0] == '⁻') { - var indexOf = Array.IndexOf(superscripts, power.Substring(1)); + var indexOf = Array.IndexOf(Superscripts, power.Substring(1)); if (indexOf < 0) { throw new FormatException(); } return -1*(indexOf + 1); } - int p = Array.IndexOf(superscripts, power) + 1; + int p = Array.IndexOf(Superscripts, power) + 1; if (p > 0) { return p;