diff --git a/src/MathNet.Numerics.4.5.resharper b/src/MathNet.Numerics.4.5.resharper index d6c48cd3..acbdd02a 100644 --- a/src/MathNet.Numerics.4.5.resharper +++ b/src/MathNet.Numerics.4.5.resharper @@ -790,7 +790,9 @@ Bluestein CHOP_ALWAYS CHOP_IF_LONG - + + True + diff --git a/src/Numerics/GlobalizationHelper.cs b/src/Numerics/GlobalizationHelper.cs new file mode 100644 index 00000000..b8149fab --- /dev/null +++ b/src/Numerics/GlobalizationHelper.cs @@ -0,0 +1,162 @@ +// +// Math.NET Numerics, part of the Math.NET Project +// http://mathnet.opensourcedotnet.info +// +// Copyright (c) 2009 Math.NET +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +namespace MathNet.Numerics +{ + using System; + using System.Collections.Generic; + using System.Globalization; + + /// + /// Globalized String Handling Helpers + /// + internal static class GlobalizationHelper + { + /// + /// Tries to get a from the format provider, + /// returning the current culture if it fails. + /// + /// + /// An that supplies culture-specific + /// formatting information. + /// + /// A instance. + internal static CultureInfo GetCultureInfo(this IFormatProvider formatProvider) + { + if (formatProvider == null) + { + return CultureInfo.CurrentCulture; + } + + return (formatProvider as CultureInfo) + ?? (formatProvider.GetFormat(typeof(CultureInfo)) as CultureInfo) + ?? CultureInfo.CurrentCulture; + } + + /// + /// Tries to get a from the format + /// provider, returning the current culture if it fails. + /// + /// + /// An that supplies culture-specific + /// formatting information. + /// + /// A instance. + internal static NumberFormatInfo GetNumberFormatInfo(this IFormatProvider formatProvider) + { + return NumberFormatInfo.GetInstance(formatProvider); + } + + /// + /// Tries to get a from the format provider, returning the current culture if it fails. + /// + /// + /// An that supplies culture-specific + /// formatting information. + /// + /// A instance. + internal static TextInfo GetTextInfo(this IFormatProvider formatProvider) + { + return (formatProvider as TextInfo) + ?? GetCultureInfo(formatProvider).TextInfo; + } + + /// + /// Globalized Parsing: Tokenize a node by splitting it into several nodes. + /// + /// Node that contains the trimmed string to be tokenized. + /// List of keywords to tokenize by. + /// keywords to skip looking for (because they've already been handled). + internal static void Tokenize(LinkedListNode node, string[] keywords, int skip) + { + for (int i = skip; i < keywords.Length; i++) + { + var keyword = keywords[i]; + int indexOfKeyword; + while ((indexOfKeyword = node.Value.IndexOf(keyword)) >= 0) + { + if (indexOfKeyword != 0) + { + // separate part before the token, process recursively + string partBeforeKeyword = node.Value.Substring(0, indexOfKeyword).Trim(); + Tokenize(node.List.AddBefore(node, partBeforeKeyword), keywords, i + 1); + + // continue processing the rest iteratively + node.Value = node.Value.Substring(indexOfKeyword); + } + + if (keyword.Length == node.Value.Length) + { + return; + } + + // separate the token, done + string partAfterKeyword = node.Value.Substring(keyword.Length).Trim(); + node.List.AddBefore(node, keyword); + + // continue processing the rest on the right iteratively + node.Value = partAfterKeyword; + } + } + } + + /// + /// Globalized Parsing: Parse a double number + /// + /// First token of the number. + /// Culture Info. + /// + /// + internal static double ParseDouble(ref LinkedListNode token, CultureInfo culture) + { + // in case the + and - in scientific notation are separated, join them back together. + if (token.Value.EndsWith("e", true, culture)) + { + if (token.Next == null || token.Next.Next == null) + { + throw new FormatException(); + } + + token.Value = token.Value + token.Next.Value + token.Next.Next.Value; + + var list = token.List; + list.Remove(token.Next.Next); + list.Remove(token.Next); + } + + double value; + if (!Double.TryParse(token.Value, NumberStyles.Any, culture, out value)) + { + throw new FormatException(); + } + + token = token.Next; + return value; + } + } +} \ No newline at end of file diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index 72547be0..145b3afd 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -63,6 +63,7 @@ +