LEFT | RIGHT |
(no file at all) | |
1 // © 2017 and later: Unicode, Inc. and others. | 1 // © 2017 and later: Unicode, Inc. and others. |
2 // License & terms of use: http://www.unicode.org/copyright.html#License | 2 // License & terms of use: http://www.unicode.org/copyright.html#License |
3 package com.ibm.icu.impl.number; | 3 package com.ibm.icu.impl.number; |
4 | 4 |
5 import java.math.BigDecimal; | 5 import java.math.BigDecimal; |
6 | 6 |
7 import com.ibm.icu.impl.number.Padder.PadPosition; | 7 import com.ibm.icu.impl.number.Padder.PadPosition; |
8 import com.ibm.icu.text.DecimalFormatSymbols; | 8 import com.ibm.icu.text.DecimalFormatSymbols; |
9 | 9 |
10 /** | 10 /** |
11 * Assorted utilities relating to decimal formatting pattern strings. | 11 * Assorted utilities relating to decimal formatting pattern strings. |
12 */ | 12 */ |
13 public class PatternStringUtils { | 13 public class PatternStringUtils { |
14 | 14 |
15 /** | 15 /** |
16 * Creates a pattern string from a property bag. | 16 * Creates a pattern string from a property bag. |
17 * | 17 * |
18 * <p> | 18 * <p> |
19 * Since pattern strings support only a subset of the functionality availabl
e in a property bag, a new property bag | 19 * Since pattern strings support only a subset of the functionality availabl
e in a property bag, a |
20 * created from the string returned by this function may not be the same as
the original property bag. | 20 * new property bag created from the string returned by this function may no
t be the same as the |
| 21 * original property bag. |
21 * | 22 * |
22 * @param properties | 23 * @param properties |
23 * The property bag to serialize. | 24 * The property bag to serialize. |
24 * @return A pattern string approximately serializing the property bag. | 25 * @return A pattern string approximately serializing the property bag. |
25 */ | 26 */ |
26 public static String propertiesToPatternString(DecimalFormatProperties prope
rties) { | 27 public static String propertiesToPatternString(DecimalFormatProperties prope
rties) { |
27 StringBuilder sb = new StringBuilder(); | 28 StringBuilder sb = new StringBuilder(); |
28 | 29 |
29 // Convenience references | 30 // Convenience references |
30 // The Math.min() calls prevent DoS | 31 // The Math.min() calls prevent DoS |
(...skipping 23 matching lines...) Expand all Loading... |
54 | 55 |
55 // Prefixes | 56 // Prefixes |
56 if (ppp != null) { | 57 if (ppp != null) { |
57 sb.append(ppp); | 58 sb.append(ppp); |
58 } | 59 } |
59 AffixUtils.escape(pp, sb); | 60 AffixUtils.escape(pp, sb); |
60 int afterPrefixPos = sb.length(); | 61 int afterPrefixPos = sb.length(); |
61 | 62 |
62 // Figure out the grouping sizes. | 63 // Figure out the grouping sizes. |
63 int grouping1, grouping2, grouping; | 64 int grouping1, grouping2, grouping; |
64 if (groupingSize != Math.min(dosMax, -1) && firstGroupingSize != Math.mi
n(dosMax, -1) | 65 if (groupingSize != Math.min(dosMax, -1) |
| 66 && firstGroupingSize != Math.min(dosMax, -1) |
65 && groupingSize != firstGroupingSize) { | 67 && groupingSize != firstGroupingSize) { |
66 grouping = groupingSize; | 68 grouping = groupingSize; |
67 grouping1 = groupingSize; | 69 grouping1 = groupingSize; |
68 grouping2 = firstGroupingSize; | 70 grouping2 = firstGroupingSize; |
69 } else if (groupingSize != Math.min(dosMax, -1)) { | 71 } else if (groupingSize != Math.min(dosMax, -1)) { |
70 grouping = groupingSize; | 72 grouping = groupingSize; |
71 grouping1 = 0; | 73 grouping1 = 0; |
72 grouping2 = groupingSize; | 74 grouping2 = groupingSize; |
73 } else if (firstGroupingSize != Math.min(dosMax, -1)) { | 75 } else if (firstGroupingSize != Math.min(dosMax, -1)) { |
74 grouping = groupingSize; | 76 grouping = groupingSize; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 break; | 179 break; |
178 case AFTER_SUFFIX: | 180 case AFTER_SUFFIX: |
179 sb.append('*'); | 181 sb.append('*'); |
180 PatternStringUtils.escapePaddingString(paddingString, sb, sb.len
gth()); | 182 PatternStringUtils.escapePaddingString(paddingString, sb, sb.len
gth()); |
181 break; | 183 break; |
182 } | 184 } |
183 } | 185 } |
184 | 186 |
185 // Negative affixes | 187 // Negative affixes |
186 // Ignore if the negative prefix pattern is "-" and the negative suffix
is empty | 188 // Ignore if the negative prefix pattern is "-" and the negative suffix
is empty |
187 if (np != null || ns != null || (npp == null && nsp != null) | 189 if (np != null |
| 190 || ns != null |
| 191 || (npp == null && nsp != null) |
188 || (npp != null && (npp.length() != 1 || npp.charAt(0) != '-' ||
nsp.length() != 0))) { | 192 || (npp != null && (npp.length() != 1 || npp.charAt(0) != '-' ||
nsp.length() != 0))) { |
189 sb.append(';'); | 193 sb.append(';'); |
190 if (npp != null) | 194 if (npp != null) |
191 sb.append(npp); | 195 sb.append(npp); |
192 AffixUtils.escape(np, sb); | 196 AffixUtils.escape(np, sb); |
193 // Copy the positive digit format into the negative. | 197 // Copy the positive digit format into the negative. |
194 // This is optional; the pattern is the same as if '#' were appended
here instead. | 198 // This is optional; the pattern is the same as if '#' were appended
here instead. |
195 sb.append(sb, afterPrefixPos, beforeSuffixPos); | 199 sb.append(sb, afterPrefixPos, beforeSuffixPos); |
196 if (nsp != null) | 200 if (nsp != null) |
197 sb.append(nsp); | 201 sb.append(nsp); |
(...skipping 27 matching lines...) Expand all Loading... |
225 output.insert(startIndex + offset, ch); | 229 output.insert(startIndex + offset, ch); |
226 offset += 1; | 230 offset += 1; |
227 } | 231 } |
228 } | 232 } |
229 output.insert(startIndex + offset, '\''); | 233 output.insert(startIndex + offset, '\''); |
230 } | 234 } |
231 return output.length() - startLength; | 235 return output.length() - startLength; |
232 } | 236 } |
233 | 237 |
234 /** | 238 /** |
235 * Converts a pattern between standard notation and localized notation. Loca
lized notation means that instead of | 239 * Converts a pattern between standard notation and localized notation. Loca
lized notation means that |
236 * using generic placeholders in the pattern, you use the corresponding loca
le-specific characters instead. For | 240 * instead of using generic placeholders in the pattern, you use the corresp
onding locale-specific |
237 * example, in locale <em>fr-FR</em>, the period in the pattern "0.000" mean
s "decimal" in standard notation (as it | 241 * characters instead. For example, in locale <em>fr-FR</em>, the period in
the pattern "0.000" means |
238 * does in every other locale), but it means "grouping" in localized notatio
n. | 242 * "decimal" in standard notation (as it does in every other locale), but it
means "grouping" in |
| 243 * localized notation. |
239 * | 244 * |
240 * <p> | 245 * <p> |
241 * A greedy string-substitution strategy is used to substitute locale symbol
s. If two symbols are ambiguous or have | 246 * A greedy string-substitution strategy is used to substitute locale symbol
s. If two symbols are |
242 * the same prefix, the result is not well-defined. | 247 * ambiguous or have the same prefix, the result is not well-defined. |
243 * | 248 * |
244 * <p> | 249 * <p> |
245 * Locale symbols are not allowed to contain the ASCII quote character. | 250 * Locale symbols are not allowed to contain the ASCII quote character. |
246 * | 251 * |
247 * <p> | 252 * <p> |
248 * This method is provided for backwards compatibility and should not be use
d in any new code. | 253 * This method is provided for backwards compatibility and should not be use
d in any new code. |
249 * | 254 * |
250 * @param input | 255 * @param input |
251 * The pattern to convert. | 256 * The pattern to convert. |
252 * @param symbols | 257 * @param symbols |
253 * The symbols corresponding to the localized pattern. | 258 * The symbols corresponding to the localized pattern. |
254 * @param toLocalized | 259 * @param toLocalized |
255 * true to convert from standard to localized notation; false to
convert from localized to standard | 260 * true to convert from standard to localized notation; false to
convert from localized to |
256 * notation. | 261 * standard notation. |
257 * @return The pattern expressed in the other notation. | 262 * @return The pattern expressed in the other notation. |
258 */ | 263 */ |
259 public static String convertLocalized(String input, DecimalFormatSymbols sym
bols, boolean toLocalized) { | 264 public static String convertLocalized( |
| 265 String input, |
| 266 DecimalFormatSymbols symbols, |
| 267 boolean toLocalized) { |
260 if (input == null) | 268 if (input == null) |
261 return null; | 269 return null; |
262 | 270 |
263 // Construct a table of strings to be converted between localized and st
andard. | 271 // Construct a table of strings to be converted between localized and st
andard. |
264 String[][] table = new String[21][2]; | 272 String[][] table = new String[21][2]; |
265 int standIdx = toLocalized ? 0 : 1; | 273 int standIdx = toLocalized ? 0 : 1; |
266 int localIdx = toLocalized ? 1 : 0; | 274 int localIdx = toLocalized ? 1 : 0; |
267 table[0][standIdx] = "%"; | 275 table[0][standIdx] = "%"; |
268 table[0][localIdx] = symbols.getPercentString(); | 276 table[0][localIdx] = symbols.getPercentString(); |
269 table[1][standIdx] = "‰"; | 277 table[1][standIdx] = "‰"; |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 result.append('\''); | 392 result.append('\''); |
385 state = 0; | 393 state = 0; |
386 } | 394 } |
387 if (state != 0) { | 395 if (state != 0) { |
388 throw new IllegalArgumentException("Malformed localized pattern: unt
erminated quote"); | 396 throw new IllegalArgumentException("Malformed localized pattern: unt
erminated quote"); |
389 } | 397 } |
390 return result.toString(); | 398 return result.toString(); |
391 } | 399 } |
392 | 400 |
393 } | 401 } |
LEFT | RIGHT |