Skip to content

Commit

Permalink
Figuring out all overloads now?
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanLarsson committed Oct 17, 2014
1 parent bca6a91 commit f921034
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
18 changes: 6 additions & 12 deletions Gu.Units.Generator/Descriptors/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static Settings FromFile(string fullFileName)
return settings;
}
}

private void Initialize()
{
foreach (var unit in SiUnits)
Expand All @@ -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));
}
}
}
Expand Down
52 changes: 28 additions & 24 deletions Gu.Units.Generator/WpfStuff/UnitPartsConverter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Gu.Units.Generator.WpfStuff
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
Expand All @@ -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);
Expand All @@ -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(
@"(?<Unit>
(?<Symbol>({0}))
(?<Power>
((?:\^)[\+\-]?\d+)
|
(⁻?({1}))
)?
|
(?<Op>[⋅\*\/])
)", symbolsPattern, superscriptsPattern);
var matches = Regex.Matches(s, pattern, RegexOptions.IgnorePatternWhitespace)
.OfType<Match>()
.ToArray();
var matches = Parse(s);
var parts = new UnitParts();
int sign = 1;
bool expectsSymbol = true;
foreach (Match match in matches)
{
Expand Down Expand Up @@ -84,12 +68,32 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
}
expectsSymbol = true;
}
//capture["Unit"]
//parts.Add([""]);
}
return parts;
}

private static IEnumerable<Match> 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(
@"(?<Unit>
(?<Symbol>({0}))
(?<Power>
((?:\^)[\+\-]?\d+)
|
(⁻?({1}))
)?
|
(?<Op>[⋅\*\/])
)", symbolsPattern, superscriptsPattern);
var matches = Regex.Matches(s, pattern, RegexOptions.IgnorePatternWhitespace)
.OfType<Match>()
.ToArray();
return matches;
}

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
return value == null ? null : ((UnitParts)value).Expression;
Expand All @@ -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;
Expand Down

0 comments on commit f921034

Please sign in to comment.