Welcome to mirror list, hosted at ThFree Co, Russian Federation.

XslNumber.cs « Runtime « Xsl « Xml « System « System.Data.SqlXml « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 343dd36d75f9efffa0b0c22df76b02e04aee76fc (plain)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//------------------------------------------------------------------------------
// <copyright file="XslNumber.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Xml.XPath;

namespace System.Xml.Xsl.Runtime {

    internal class TokenInfo {
        public char     startChar;      // First element of numbering sequence for format token
        public int      startIdx;       // Start index of separator token
        public string   formatString;   // Format string for separator token
        public int      length;         // Length of separator token, or minimum length of decimal numbers for format token

        // Instances of this internal class must be created via CreateFormat and CreateSeparator
        private TokenInfo() {
        }

        [Conditional("DEBUG")]
        public void AssertSeparator(bool isSeparator) {
            Debug.Assert(isSeparator == (formatString != null), "AssertSeparator");
        }

        // Creates a TokenInfo for a separator token.
        public static TokenInfo CreateSeparator(string formatString, int startIdx, int tokLen) {
            Debug.Assert(startIdx >= 0 && tokLen > 0);
            TokenInfo token = new TokenInfo(); {
                token.startIdx     = startIdx;
                token.formatString = formatString;
                token.length       = tokLen;
            }
            return token;
        }

        // Maps a token of alphanumeric characters to a numbering format ID and a
        // minimum length bound.  Tokens specify the character(s) that begins a Unicode
        // numbering sequence.  For example, "i" specifies lower case roman numeral
        // numbering.  Leading "zeros" specify a minimum length to be maintained by
        // padding, if necessary.
        public static TokenInfo CreateFormat(string formatString, int startIdx, int tokLen) {
            Debug.Assert(startIdx >= 0 && tokLen > 0);
            TokenInfo token = new TokenInfo();
            token.formatString = null;
            token.length = 1;

            bool useDefault = false;
            char ch = formatString[startIdx];

            switch (ch) {
            case '1':
            case 'A':
            case 'I':
            case 'a':
            case 'i':
                break;
            default:
                // NOTE: We do not support Tamil and Ethiopic numbering systems having no zeros
                if (CharUtil.IsDecimalDigitOne(ch)) {
                    break;
                }
                if (CharUtil.IsDecimalDigitOne((char)(ch + 1))) {
                    // Leading zeros request padding.  Track how much.
                    int idx = startIdx;
                    do {
                        token.length++;
                    } while (--tokLen > 0 && ch == formatString[++idx]);

                    // Recognize the token only if the next character is "one"
                    if (formatString[idx] == ++ch) {
                        break;
                    }
                }
                useDefault = true;
                break;
            }

            if (tokLen != 1) {
                // If remaining token length is not 1, do not recognize the token
                useDefault = true;
            }

            if (useDefault) {
                // Default to Arabic numbering with no zero padding
                token.startChar = NumberFormatter.DefaultStartChar;
                token.length = 1;
            } else {
                token.startChar = ch;
            }
            return token;
        }
    }

    internal class NumberFormatter : NumberFormatterBase {
        private string          formatString;
        private int             lang;
        private string          letterValue;
        private string          groupingSeparator;
        private int             groupingSize;

        private List<TokenInfo> tokens;

        public const char                   DefaultStartChar = '1';
        private static readonly TokenInfo   DefaultFormat    = TokenInfo.CreateFormat   ("0", 0, 1);
        private static readonly TokenInfo   DefaultSeparator = TokenInfo.CreateSeparator(".", 0, 1);

        // Creates a Format object parsing format string into format tokens (alphanumeric) and separators (non-alphanumeric).
        public NumberFormatter(string formatString, int lang, string letterValue, string groupingSeparator, int groupingSize) {
            Debug.Assert(groupingSeparator.Length <= 1);
            this.formatString       = formatString;
            this.lang               = lang;
            this.letterValue        = letterValue;
            this.groupingSeparator  = groupingSeparator;
            this.groupingSize       = groupingSeparator.Length > 0 ? groupingSize : 0;

            if (formatString == "1" || formatString.Length == 0) {
                // Special case of the default format
                return;
            }

            this.tokens = new List<TokenInfo>();
            int idxStart = 0;
            bool isAlphaNumeric = CharUtil.IsAlphaNumeric(formatString[idxStart]);

            if (isAlphaNumeric) {
                // If the first one is alpha num add empty separator as a prefix
                tokens.Add(null);
            }

            for (int idx = 0; idx <= formatString.Length; idx++) {
                // Loop until a switch from formatString token to separator is detected (or vice-versa)
                if (idx == formatString.Length || isAlphaNumeric != CharUtil.IsAlphaNumeric(formatString[idx])) {
                    if (isAlphaNumeric) {
                        // Just finished a format token
                        tokens.Add(TokenInfo.CreateFormat(formatString, idxStart, idx - idxStart));
                    } else {
                        // Just finished a separator token
                        tokens.Add(TokenInfo.CreateSeparator(formatString, idxStart, idx - idxStart));
                    }

                    // Begin parsing the next format token or separator
                    idxStart = idx;

                    // Flip flag from format token to separator or vice-versa
                    isAlphaNumeric = !isAlphaNumeric;
                }
            }
        }

        /// <summary>
        /// Format the given xsl:number place marker
        /// </summary>
        /// <param name="val">Place marker - either a sequence of ints, or a double singleton</param>
        /// <returns>Formatted string</returns>
        public string FormatSequence(IList<XPathItem> val) {
            StringBuilder sb = new StringBuilder();

            // If the value was supplied directly, in the 'value' attribute, check its validity
            if (val.Count == 1 && val[0].ValueType == typeof(double)) {
                double dblVal = val[0].ValueAsDouble;
                if (!(0.5 <= dblVal && dblVal < double.PositiveInfinity)) {
                    // Errata E24: It is an error if the number is NaN, infinite or less than 0.5; an XSLT processor may signal
                    // the error; if it does not signal the error, it must recover by converting the number to a string as if
                    // by a call to the 'string' function and inserting the resulting string into the result tree.
                    return XPathConvert.DoubleToString(dblVal);
                }
            }

            if (tokens == null) {
                // Special case of the default format
                for (int idx = 0; idx < val.Count; idx++) {
                    if (idx > 0) {
                        sb.Append('.');
                    }
                    FormatItem(sb, val[idx], DefaultStartChar, 1);
                }
            } else {
                int cFormats = tokens.Count;
                TokenInfo prefix = tokens[0], suffix;

                if (cFormats % 2 == 0) {
                    suffix = null;
                } else {
                    suffix = tokens[--cFormats];
                }

                TokenInfo periodicSeparator = 2 < cFormats ? tokens[cFormats - 2] : DefaultSeparator;
                TokenInfo periodicFormat    = 0 < cFormats ? tokens[cFormats - 1] : DefaultFormat;

                if (prefix != null) {
                    prefix.AssertSeparator(true);
                    sb.Append(prefix.formatString, prefix.startIdx, prefix.length);
                }

                int valCount = val.Count;
                for (int i = 0; i < valCount; i++ ) {
                    int formatIndex = i * 2;
                    bool haveFormat = formatIndex < cFormats;

                    if (i > 0) {
                        TokenInfo thisSeparator = haveFormat ? tokens[formatIndex + 0] : periodicSeparator;
                        thisSeparator.AssertSeparator(true);
                        sb.Append(thisSeparator.formatString, thisSeparator.startIdx, thisSeparator.length);
                    }

                    TokenInfo thisFormat = haveFormat ? tokens[formatIndex + 1] : periodicFormat;
                    thisFormat.AssertSeparator(false);
                    FormatItem(sb, val[i], thisFormat.startChar, thisFormat.length);
                }

                if (suffix != null) {
                    suffix.AssertSeparator(true);
                    sb.Append(suffix.formatString, suffix.startIdx, suffix.length);
                }
            }
            return sb.ToString();
        }

        private void FormatItem(StringBuilder sb, XPathItem item, char startChar, int length) {
            double dblVal;

            if (item.ValueType == typeof(int)) {
                dblVal = (double)item.ValueAsInt;
            } else {
                Debug.Assert(item.ValueType == typeof(double), "Item must be either of type int, or double");
                dblVal = XsltFunctions.Round(item.ValueAsDouble);
            }

            Debug.Assert(1 <= dblVal && dblVal < double.PositiveInfinity);
            char zero = '0';

            switch (startChar) {
            case '1':
                break;
            case 'A':
            case 'a':
                if (dblVal <= MaxAlphabeticValue) {
                    ConvertToAlphabetic(sb, dblVal, startChar, 26);
                    return;
                }
                break;
            case 'I':
            case 'i':
                if (dblVal <= MaxRomanValue) {
                    ConvertToRoman(sb, dblVal, /*upperCase:*/ startChar == 'I');
                    return;
                }
                break;
            default:
                Debug.Assert(CharUtil.IsDecimalDigitOne(startChar), "Unexpected startChar: " + startChar);
                zero = (char)(startChar - 1);
                break;
            }

            sb.Append(ConvertToDecimal(dblVal, length, zero, groupingSeparator, groupingSize));
        }

        private static string ConvertToDecimal(double val, int minLen, char zero, string groupSeparator, int groupSize) {
            Debug.Assert(val >= 0 && val == Math.Round(val), "ConvertToArabic operates on non-negative integer numbers only");
            string str = XPathConvert.DoubleToString(val);
            int  shift = zero - '0';

            // Figure out new string length without separators
            int oldLen = str.Length;
            int newLen = Math.Max(oldLen, minLen);

            // Calculate length of string with separators
            if (groupSize != 0) {
                Debug.Assert(groupSeparator.Length == 1);
                checked { newLen += (newLen - 1) / groupSize; }
            }

            // If the new number of characters equals the old one, no changes need to be made
            if (newLen == oldLen && shift == 0) {
                return str;
            }

            // If grouping is not needed, add zero padding only
            if (groupSize == 0 && shift == 0) {
                return str.PadLeft(newLen, zero);
            }

            // Add both grouping separators and zero padding to the string representation of a number
        #if true
            unsafe {
                char *result = stackalloc char[newLen];
                char separator = (groupSeparator.Length > 0) ? groupSeparator[0] : ' ';

                fixed (char *pin = str) {
                    char *pOldEnd = pin + oldLen - 1;
                    char *pNewEnd = result + newLen - 1;
                    int cnt = groupSize;

                    while (true) {
                        // Move digit to its new location (zero if we've run out of digits)
                        *pNewEnd-- = (pOldEnd >= pin) ? (char)(*pOldEnd-- + shift) : zero;
                        if (pNewEnd < result) {
                            break;
                        }
                        if (/*groupSize > 0 && */--cnt == 0) {
                            // Every groupSize digits insert the separator
                            *pNewEnd-- = separator;
                            cnt = groupSize;
                            Debug.Assert(pNewEnd >= result, "Separator cannot be the first character");
                        }
                    }
                }
                return new string(result, 0, newLen);
            }
        #else
            // Safe version is about 20% slower after NGEN
            char[] result = new char[newLen];
            char separator = (groupSeparator.Length > 0) ? groupSeparator[0] : ' ';

            int oldEnd = oldLen - 1;
            int newEnd = newLen - 1;
            int cnt = groupSize;

            while (true) {
                // Move digit to its new location (zero if we've run out of digits)
                result[newEnd--] = (oldEnd >= 0) ? (char)(str[oldEnd--] + shift) : zero;
                if (newEnd < 0) {
                    break;
                }
                if (/*groupSize > 0 && */--cnt == 0) {
                    // Every groupSize digits insert the separator
                    result[newEnd--] = separator;
                    cnt = groupSize;
                    Debug.Assert(newEnd >= 0, "Separator cannot be the first character");
                }
            }
            return new string(result, 0, newLen);
        #endif
        }
    }
}