diff --git a/MathConverter/MathConverter.cs b/MathConverter/MathConverter.cs index 80a6973..6eae51d 100644 --- a/MathConverter/MathConverter.cs +++ b/MathConverter/MathConverter.cs @@ -9,10 +9,31 @@ namespace HexInnovation { + /// + /// MathConverter is a WPF Converter class that does it all. + /// public class MathConverter : IValueConverter, IMultiValueConverter { + /// + /// Creates a new MathConverter object. + /// + public MathConverter() + { + + } + + /// + /// If is set to true, clears the cache of this MathConverter object; If is false, this method does nothing. + /// + public void ClearCache() + { + if (UseCache) + { + CachedResults.Clear(); + } + } + private Dictionary CachedResults = new Dictionary(); - private static readonly Regex NullableRegex = new Regex(@"^System.Nullable`1\[\[(\S*), mscorlib, Version=.*, Culture=neutral, PublicKeyToken=[a-f0-9]{16}\]\]$", RegexOptions.Compiled | RegexOptions.Singleline); /// /// The conversion for a single value. @@ -235,17 +256,9 @@ public object Convert(object[] values, Type targetType, object parameter, Cultur throw new NotSupportedException(string.Format("You supplied {0} values; string supports only one", values.Length)); } default: - var matches = NullableRegex.Matches(targetType.FullName); - - if (matches.Count == 1) + if (targetType == typeof(double?)) { - switch (matches[0].Groups[1].Value) - { - case "System.Double": - return ConvertToDouble(values[0]); - default: - throw new NotSupportedException(string.Format("You cannot convert to a System.Nullable<{0}>", matches[0].Groups[1].Value)); - } + return ConvertToDouble(values[0]); } throw new NotSupportedException(string.Format("You cannot convert to a {0}", targetType.Name)); @@ -290,10 +303,10 @@ public bool UseCache } /// - /// Converts a number to a double. + /// Converts a number to an object. /// - /// The value we're converting to a double. - /// The number, converted to a double. + /// The value we're converting to an object. + /// The number, converted to an object. public static object ConvertToObject(object parameter) { if (parameter == null) @@ -350,7 +363,7 @@ public static object ConvertToObject(object parameter) return null; double v; - if (parameter is string && double.TryParse(parameter as string, out v)) + if (parameter is string && double.TryParse(parameter as string, NumberStyles.Number, CultureInfo.InvariantCulture, out v)) { return v; } diff --git a/MathConverter/Parser.cs b/MathConverter/Parser.cs index 50fcaf4..a4d0792 100644 --- a/MathConverter/Parser.cs +++ b/MathConverter/Parser.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -206,7 +207,7 @@ private AbstractSyntaxTree Exponent(AbstractSyntaxTree e) case TokenType.Number: if (e is VariableNode) { - return Exponent(new ExponentNode(e, new ConstantNumberNode(double.Parse((t as LexicalToken).Lex)))); + return Exponent(new ExponentNode(e, new ConstantNumberNode(double.Parse((t as LexicalToken).Lex, NumberStyles.Number, CultureInfo.InvariantCulture)))); } else { @@ -225,7 +226,7 @@ private AbstractSyntaxTree Primary() switch (t.TokenType) { case TokenType.Number: - return new ConstantNumberNode(double.Parse((t as LexicalToken).Lex)); + return new ConstantNumberNode(double.Parse((t as LexicalToken).Lex, NumberStyles.Number, CultureInfo.InvariantCulture)); case TokenType.Minus: return new NegativeNode(Primary()); case TokenType.Not: