Skip to content

Commit

Permalink
bug fixes for cultures besides en-us
Browse files Browse the repository at this point in the history
  • Loading branch information
timothylcooke committed Nov 7, 2015
1 parent ac816c4 commit efb3cd5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
43 changes: 28 additions & 15 deletions MathConverter/MathConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,31 @@

namespace HexInnovation
{
/// <summary>
/// MathConverter is a WPF Converter class that does it all.
/// </summary>
public class MathConverter : IValueConverter, IMultiValueConverter
{
/// <summary>
/// Creates a new MathConverter object.
/// </summary>
public MathConverter()
{

}

/// <summary>
/// If <see cref="UseCache"/> is set to true, clears the cache of this MathConverter object; If <see cref="UseCache"/> is false, this method does nothing.
/// </summary>
public void ClearCache()
{
if (UseCache)
{
CachedResults.Clear();
}
}

private Dictionary<string, AbstractSyntaxTree[]> CachedResults = new Dictionary<string, AbstractSyntaxTree[]>();
private static readonly Regex NullableRegex = new Regex(@"^System.Nullable`1\[\[(\S*), mscorlib, Version=.*, Culture=neutral, PublicKeyToken=[a-f0-9]{16}\]\]$", RegexOptions.Compiled | RegexOptions.Singleline);

/// <summary>
/// The conversion for a single value.
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -290,10 +303,10 @@ public bool UseCache
}

/// <summary>
/// Converts a number to a double.
/// Converts a number to an object.
/// </summary>
/// <param name="parameter">The value we're converting to a double.</param>
/// <returns>The number, converted to a double.</returns>
/// <param name="parameter">The value we're converting to an object.</param>
/// <returns>The number, converted to an object.</returns>
public static object ConvertToObject(object parameter)
{
if (parameter == null)
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions MathConverter/Parser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -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
{
Expand All @@ -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:
Expand Down

0 comments on commit efb3cd5

Please sign in to comment.