ningshuxia
2022-12-12 4f1314cb69a47c22e52f1efdc4b4be6c1d55143d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// <copyright file="GlobalizationHelper.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2010 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.
// </copyright>
 
using System;
using System.Collections.Generic;
using System.Globalization;
 
namespace IStation.Numerics
{
    /// <summary>
    /// Globalized String Handling Helpers
    /// </summary>
    internal static class GlobalizationHelper
    {
        /// <summary>
        /// Tries to get a <see cref="CultureInfo"/> from the format provider,
        /// returning the current culture if it fails.
        /// </summary>
        /// <param name="formatProvider">
        /// An <see cref="IFormatProvider"/> that supplies culture-specific
        /// formatting information.
        /// </param>
        /// <returns>A <see cref="CultureInfo"/> instance.</returns>
        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;
        }
 
        /// <summary>
        /// Tries to get a <see cref="NumberFormatInfo"/> from the format
        /// provider, returning the current culture if it fails.
        /// </summary>
        /// <param name="formatProvider">
        /// An <see cref="IFormatProvider"/> that supplies culture-specific
        /// formatting information.
        /// </param>
        /// <returns>A <see cref="NumberFormatInfo"/> instance.</returns>
        internal static NumberFormatInfo GetNumberFormatInfo(this IFormatProvider formatProvider)
        {
            return NumberFormatInfo.GetInstance(formatProvider);
        }
 
        /// <summary>
        /// Tries to get a <see cref="TextInfo"/> from the format provider, returning the current culture if it fails.
        /// </summary>
        /// <param name="formatProvider">
        /// An <see cref="IFormatProvider"/> that supplies culture-specific
        /// formatting information.
        /// </param>
        /// <returns>A <see cref="TextInfo"/> instance.</returns>
        internal static TextInfo GetTextInfo(this IFormatProvider formatProvider)
        {
            if (formatProvider == null)
            {
                return CultureInfo.CurrentCulture.TextInfo;
            }
 
            return (formatProvider.GetFormat(typeof (TextInfo)) as TextInfo)
                ?? GetCultureInfo(formatProvider).TextInfo;
        }
 
        /// <summary>
        /// Globalized Parsing: Tokenize a node by splitting it into several nodes.
        /// </summary>
        /// <param name="node">Node that contains the trimmed string to be tokenized.</param>
        /// <param name="keywords">List of keywords to tokenize by.</param>
        /// <param name="skip">keywords to skip looking for (because they've already been handled).</param>
        internal static void Tokenize(LinkedListNode<string> 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, StringComparison.Ordinal)) >= 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;
                }
            }
        }
 
#if NETSTANDARD1_3
        /// <summary>
        /// Globalized Parsing: Parse a double number
        /// </summary>
        /// <param name="token">First token of the number.</param>
        /// <returns>The parsed double number using the current culture information.</returns>
        /// <exception cref="FormatException" />
        internal static double ParseDouble(ref LinkedListNode<string> token)
        {
            // in case the + and - in scientific notation are separated, join them back together.
            if (token.Value.EndsWith("e", StringComparison.CurrentCultureIgnoreCase))
            {
                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, CultureInfo.CurrentCulture, out value))
            {
                throw new FormatException();
            }
 
            token = token.Next;
            return value;
        }
 
        /// <summary>
        /// Globalized Parsing: Parse a float number
        /// </summary>
        /// <param name="token">First token of the number.</param>
        /// <returns>The parsed float number using the current culture information.</returns>
        /// <exception cref="FormatException" />
        internal static float ParseSingle(ref LinkedListNode<string> token)
        {
            // in case the + and - in scientific notation are separated, join them back together.
            if (token.Value.EndsWith("e", StringComparison.CurrentCultureIgnoreCase))
            {
                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);
            }
 
            float value;
            if (!Single.TryParse(token.Value, NumberStyles.Any, CultureInfo.CurrentCulture, out value))
            {
                throw new FormatException();
            }
 
            token = token.Next;
            return value;
        }
 
#else
        /// <summary>
        /// Globalized Parsing: Parse a double number
        /// </summary>
        /// <param name="token">First token of the number.</param>
        /// <param name="culture">Culture Info.</param>
        /// <returns>The parsed double number using the given culture information.</returns>
        /// <exception cref="FormatException" />
        internal static double ParseDouble(ref LinkedListNode<string> 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;
        }
 
        /// <summary>
        /// Globalized Parsing: Parse a float number
        /// </summary>
        /// <param name="token">First token of the number.</param>
        /// <param name="culture">Culture Info.</param>
        /// <returns>The parsed float number using the given culture information.</returns>
        /// <exception cref="FormatException" />
        internal static float ParseSingle(ref LinkedListNode<string> 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);
            }
 
            float value;
            if (!Single.TryParse(token.Value, NumberStyles.Any, culture, out value))
            {
                throw new FormatException();
            }
 
            token = token.Next;
            return value;
        }
#endif
    }
}