Debian Codegraph @

net/sf/saxon/expr/number/NumberFormatter.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.expr.number;
9
10 import net.sf.saxon.lib.Numberer;
11 import net.sf.saxon.regex.EmptyString;
12 import net.sf.saxon.regex.UnicodeString;
13 import net.sf.saxon.regex.charclass.Categories;
14 import net.sf.saxon.tree.util.FastStringBuffer;
15
16 import java.math.BigInteger;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.function.IntPredicate;
20
21 /**
22 * Class NumberFormatter defines a method to format a ArrayList of integers as a character
23 * string according to a supplied format specification.
24 *
25 * @author Michael H. Kay
26 */
27
28 public class NumberFormatter {
29
30 private ArrayList<UnicodeString> formatTokens;
31 private ArrayList<UnicodeString> punctuationTokens;
32 private boolean startsWithPunctuation;
33
34 /**
35 * Tokenize the format pattern.
36 *
37 * @param format the format specification. Contains one of the following values:<ul>
38 * <li>"1": conventional decimal numbering</li>
39 * <li>"a": sequence a, b, c, ... aa, ab, ac, ...</li>
40 * <li>"A": sequence A, B, C, ... AA, AB, AC, ...</li>
41 * <li>"i": sequence i, ii, iii, iv, v ...</li>
42 * <li>"I": sequence I, II, III, IV, V, ...</li>
43 * </ul>
44 * This symbol may be preceded and followed by punctuation (any other characters) which is
45 * copied to the output string.
46 */
47
48 public void prepare(String format) {
49
50 // Tokenize the format string into alternating alphanumeric and non-alphanumeric tokens
51
52 if (format.isEmpty()) {
53 format = "1";
54 }
55
56 formatTokens = new ArrayList<UnicodeString>(10);
57 punctuationTokens = new ArrayList<UnicodeString>(10);
58
59 UnicodeString uFormat = UnicodeString.makeUnicodeString(format);
60 int len = uFormat.uLength();
61 int i = 0;
62 int t;
63 boolean first = true;
64 startsWithPunctuation = true;
65
66 while (i < len) {
67 int c = uFormat.uCharAt(i);
68 t = i;
69 while (isLetterOrDigit(c)) {
70 i++;
71 if (i == len) break;
72 c = uFormat.uCharAt(i);
73 }
74 if (i > t) {
75 UnicodeString tok = uFormat.uSubstring(t, i);
76 formatTokens.add(tok);
77 if (first) {
78 punctuationTokens.add(UnicodeString.makeUnicodeString("."));
79 startsWithPunctuation = false;
80 first = false;
81 }
82 }
83 if (i == len) break;
84 t = i;
85 c = uFormat.uCharAt(i);
86 while (!isLetterOrDigit(c)) {
87 first = false;
88 i++;
89 if (i == len) break;
90 c = uFormat.uCharAt(i);
91 }
92 if (i > t) {
93 UnicodeString sep = uFormat.uSubstring(t, i);
94 punctuationTokens.add(sep);
95 }
96 }
97
98 if (formatTokens.isEmpty()) {
99 formatTokens.add(UnicodeString.makeUnicodeString("1"));
100 if (punctuationTokens.size() == 1) {
101 punctuationTokens.add(punctuationTokens.get(0));
102 }
103 }
104
105 }
106
107 /**
108 * Determine whether a (possibly non-BMP) character is a letter or digit.
109 *
110 * @param c the codepoint of the character to be tested
111 * @return true if this is a number or letter as defined in the XSLT rules for xsl:number pictures.
112 */
113
114 public static boolean isLetterOrDigit(int c) {
115 if (c <= 0x7F) {
116 // Fast path for ASCII characters
117 return (c >= 0x30 && c <= 0x39) || (c >= 0x41 && c <= 0x5A) || (c >= 0x61 && c <= 0x7A);
118 } else {
119 return alphanumeric.test(c);
120 }
121 }
122
123 private static IntPredicate alphanumeric =
124 Categories.getCategory("N").or(Categories.getCategory("L"));
125
126 /**
127 * Format a list of numbers.
128 *
129 * @param numbers the numbers to be formatted (a sequence of integer values; it may also contain
130 * preformatted strings as part of the error recovery fallback)
131 * @return the formatted output string.
132 */
133
134 public CharSequence format(List numbers, int groupSize, String groupSeparator,
135 String letterValue, String ordinal, /*@NotNull*/ Numberer numberer) {
136
137 FastStringBuffer sb = new FastStringBuffer(FastStringBuffer.C16);
138 int num = 0;
139 int tok = 0;
140 // output first punctuation token
141 if (startsWithPunctuation) {
142 sb.append(punctuationTokens.get(tok));
143 }
144 // output the list of numbers
145 while (num < numbers.size()) {
146 if (num > 0) {
147 if (tok == 0 && startsWithPunctuation) {
148 // The first punctuation token isn't a separator if it appears before the first
149 // formatting token. Such a punctuation token is used only once, at the start.
150 sb.append(".");
151 } else {
152 sb.append(punctuationTokens.get(tok));
153 }
154 }
155 Object o = numbers.get(num++);
156 String s;
157 if (o instanceof Long) {
158 long nr = (Long) o;
159 RegularGroupFormatter rgf = new RegularGroupFormatter(groupSize, groupSeparator, EmptyString.THE_INSTANCE);
160 s = numberer.format(nr, formatTokens.get(tok), rgf, letterValue, ordinal);
161 } else if (o instanceof BigInteger) {
162 // Saxon bug 2071; test case number-0111
163 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C64);
164 fsb.append(o.toString());
165 RegularGroupFormatter rgf = new RegularGroupFormatter(groupSize, groupSeparator, EmptyString.THE_INSTANCE);
166 s = rgf.format(fsb);
167 s = translateDigits(s, formatTokens.get(tok));
168 } else {
169 // Not sure this can happen
170 s = o.toString();
171 }
172 sb.append(s);
173 tok++;
174 if (tok == formatTokens.size()) {
175 tok--;
176 }
177 }
178 // output the final punctuation token
179 if (punctuationTokens.size() > formatTokens.size()) {
180 sb.append(punctuationTokens.get(punctuationTokens.size() - 1));
181 }
182 return sb.condense();
183 }
184
185 private String translateDigits(String in, UnicodeString picture) {
186 if (picture.length() == 0) {
187 return in;
188 }
189 int formchar = picture.uCharAt(0);
190 int digitValue = Alphanumeric.getDigitValue(formchar);
191 if (digitValue >= 0) {
192 int zero = formchar - digitValue;
193 if (zero == (int) '0') {
194 return in;
195 }
196 int[] digits = new int[10];
197 for (int z = 0; z <= 9; z++) {
198 digits[z] = zero + z;
199 }
200 FastStringBuffer sb = new FastStringBuffer(128);
201 for (int i = 0; i < in.length(); i++) {
202 char c = in.charAt(i);
203 if (c >= '0' && c <= '9') {
204 sb.appendWideChar(digits[c - '0']);
205 } else {
206 sb.append(c);
207 }
208 }
209 return sb.toString();
210 } else {
211 return in;
212 }
213 }
214
215
216 }
217