Coverage Report

Created: 2026-03-31 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/icu/icu4c/source/i18n/number_skeletons.cpp
Line
Count
Source
1
// © 2018 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
4
#include "unicode/utypes.h"
5
6
#if !UCONFIG_NO_FORMATTING
7
8
// Allow implicit conversion from char16_t* to UnicodeString for this file:
9
// Helpful in toString methods and elsewhere.
10
#define UNISTR_FROM_STRING_EXPLICIT
11
12
#include "number_decnum.h"
13
#include "number_roundingutils.h"
14
#include "number_skeletons.h"
15
#include "umutex.h"
16
#include "ucln_in.h"
17
#include "patternprops.h"
18
#include "unicode/ucharstriebuilder.h"
19
#include "number_utils.h"
20
#include "number_decimalquantity.h"
21
#include "unicode/numberformatter.h"
22
#include "uinvchar.h"
23
#include "charstr.h"
24
#include "string_segment.h"
25
#include "unicode/errorcode.h"
26
#include "util.h"
27
#include "measunit_impl.h"
28
29
using namespace icu;
30
using namespace icu::number;
31
using namespace icu::number::impl;
32
using namespace icu::number::impl::skeleton;
33
34
namespace {
35
36
icu::UInitOnce gNumberSkeletonsInitOnce {};
37
38
char16_t* kSerializedStemTrie = nullptr;
39
40
0
UBool U_CALLCONV cleanupNumberSkeletons() {
41
0
    uprv_free(kSerializedStemTrie);
42
0
    kSerializedStemTrie = nullptr;
43
0
    gNumberSkeletonsInitOnce.reset();
44
0
    return true;
45
0
}
46
47
1
void U_CALLCONV initNumberSkeletons(UErrorCode& status) {
48
1
    ucln_i18n_registerCleanup(UCLN_I18N_NUMBER_SKELETONS, cleanupNumberSkeletons);
49
50
1
    UCharsTrieBuilder b(status);
51
1
    if (U_FAILURE(status)) { return; }
52
53
    // Section 1:
54
1
    b.add(u"compact-short", STEM_COMPACT_SHORT, status);
55
1
    b.add(u"compact-long", STEM_COMPACT_LONG, status);
56
1
    b.add(u"scientific", STEM_SCIENTIFIC, status);
57
1
    b.add(u"engineering", STEM_ENGINEERING, status);
58
1
    b.add(u"notation-simple", STEM_NOTATION_SIMPLE, status);
59
1
    b.add(u"base-unit", STEM_BASE_UNIT, status);
60
1
    b.add(u"percent", STEM_PERCENT, status);
61
1
    b.add(u"permille", STEM_PERMILLE, status);
62
1
    b.add(u"precision-integer", STEM_PRECISION_INTEGER, status);
63
1
    b.add(u"precision-unlimited", STEM_PRECISION_UNLIMITED, status);
64
1
    b.add(u"precision-currency-standard", STEM_PRECISION_CURRENCY_STANDARD, status);
65
1
    b.add(u"precision-currency-cash", STEM_PRECISION_CURRENCY_CASH, status);
66
1
    b.add(u"rounding-mode-ceiling", STEM_ROUNDING_MODE_CEILING, status);
67
1
    b.add(u"rounding-mode-floor", STEM_ROUNDING_MODE_FLOOR, status);
68
1
    b.add(u"rounding-mode-down", STEM_ROUNDING_MODE_DOWN, status);
69
1
    b.add(u"rounding-mode-up", STEM_ROUNDING_MODE_UP, status);
70
1
    b.add(u"rounding-mode-half-even", STEM_ROUNDING_MODE_HALF_EVEN, status);
71
1
    b.add(u"rounding-mode-half-odd", STEM_ROUNDING_MODE_HALF_ODD, status);
72
1
    b.add(u"rounding-mode-half-ceiling", STEM_ROUNDING_MODE_HALF_CEILING, status);
73
1
    b.add(u"rounding-mode-half-floor", STEM_ROUNDING_MODE_HALF_FLOOR, status);
74
1
    b.add(u"rounding-mode-half-down", STEM_ROUNDING_MODE_HALF_DOWN, status);
75
1
    b.add(u"rounding-mode-half-up", STEM_ROUNDING_MODE_HALF_UP, status);
76
1
    b.add(u"rounding-mode-unnecessary", STEM_ROUNDING_MODE_UNNECESSARY, status);
77
1
    b.add(u"integer-width-trunc", STEM_INTEGER_WIDTH_TRUNC, status);
78
1
    b.add(u"group-off", STEM_GROUP_OFF, status);
79
1
    b.add(u"group-min2", STEM_GROUP_MIN2, status);
80
1
    b.add(u"group-auto", STEM_GROUP_AUTO, status);
81
1
    b.add(u"group-on-aligned", STEM_GROUP_ON_ALIGNED, status);
82
1
    b.add(u"group-thousands", STEM_GROUP_THOUSANDS, status);
83
1
    b.add(u"latin", STEM_LATIN, status);
84
1
    b.add(u"unit-width-narrow", STEM_UNIT_WIDTH_NARROW, status);
85
1
    b.add(u"unit-width-short", STEM_UNIT_WIDTH_SHORT, status);
86
1
    b.add(u"unit-width-full-name", STEM_UNIT_WIDTH_FULL_NAME, status);
87
1
    b.add(u"unit-width-iso-code", STEM_UNIT_WIDTH_ISO_CODE, status);
88
1
    b.add(u"unit-width-formal", STEM_UNIT_WIDTH_FORMAL, status);
89
1
    b.add(u"unit-width-variant", STEM_UNIT_WIDTH_VARIANT, status);
90
1
    b.add(u"unit-width-hidden", STEM_UNIT_WIDTH_HIDDEN, status);
91
1
    b.add(u"sign-auto", STEM_SIGN_AUTO, status);
92
1
    b.add(u"sign-always", STEM_SIGN_ALWAYS, status);
93
1
    b.add(u"sign-never", STEM_SIGN_NEVER, status);
94
1
    b.add(u"sign-accounting", STEM_SIGN_ACCOUNTING, status);
95
1
    b.add(u"sign-accounting-always", STEM_SIGN_ACCOUNTING_ALWAYS, status);
96
1
    b.add(u"sign-except-zero", STEM_SIGN_EXCEPT_ZERO, status);
97
1
    b.add(u"sign-accounting-except-zero", STEM_SIGN_ACCOUNTING_EXCEPT_ZERO, status);
98
1
    b.add(u"sign-negative", STEM_SIGN_NEGATIVE, status);
99
1
    b.add(u"sign-accounting-negative", STEM_SIGN_ACCOUNTING_NEGATIVE, status);
100
1
    b.add(u"decimal-auto", STEM_DECIMAL_AUTO, status);
101
1
    b.add(u"decimal-always", STEM_DECIMAL_ALWAYS, status);
102
1
    if (U_FAILURE(status)) { return; }
103
104
    // Section 2:
105
1
    b.add(u"precision-increment", STEM_PRECISION_INCREMENT, status);
106
1
    b.add(u"measure-unit", STEM_MEASURE_UNIT, status);
107
1
    b.add(u"per-measure-unit", STEM_PER_MEASURE_UNIT, status);
108
1
    b.add(u"unit", STEM_UNIT, status);
109
1
    b.add(u"usage", STEM_UNIT_USAGE, status);
110
1
    b.add(u"currency", STEM_CURRENCY, status);
111
1
    b.add(u"integer-width", STEM_INTEGER_WIDTH, status);
112
1
    b.add(u"numbering-system", STEM_NUMBERING_SYSTEM, status);
113
1
    b.add(u"scale", STEM_SCALE, status);
114
1
    if (U_FAILURE(status)) { return; }
115
116
    // Section 3 (concise tokens):
117
1
    b.add(u"K", STEM_COMPACT_SHORT, status);
118
1
    b.add(u"KK", STEM_COMPACT_LONG, status);
119
1
    b.add(u"%", STEM_PERCENT, status);
120
1
    b.add(u"%x100", STEM_PERCENT_100, status);
121
1
    b.add(u",_", STEM_GROUP_OFF, status);
122
1
    b.add(u",?", STEM_GROUP_MIN2, status);
123
1
    b.add(u",!", STEM_GROUP_ON_ALIGNED, status);
124
1
    b.add(u"+!", STEM_SIGN_ALWAYS, status);
125
1
    b.add(u"+_", STEM_SIGN_NEVER, status);
126
1
    b.add(u"()", STEM_SIGN_ACCOUNTING, status);
127
1
    b.add(u"()!", STEM_SIGN_ACCOUNTING_ALWAYS, status);
128
1
    b.add(u"+?", STEM_SIGN_EXCEPT_ZERO, status);
129
1
    b.add(u"()?", STEM_SIGN_ACCOUNTING_EXCEPT_ZERO, status);
130
1
    b.add(u"+-", STEM_SIGN_NEGATIVE, status);
131
1
    b.add(u"()-", STEM_SIGN_ACCOUNTING_NEGATIVE, status);
132
1
    if (U_FAILURE(status)) { return; }
133
134
    // Build the CharsTrie
135
    // TODO: Use SLOW or FAST here?
136
1
    UnicodeString result;
137
1
    b.buildUnicodeString(USTRINGTRIE_BUILD_FAST, result, status);
138
1
    if (U_FAILURE(status)) { return; }
139
140
    // Copy the result into the global constant pointer
141
1
    size_t numBytes = result.length() * sizeof(char16_t);
142
1
    kSerializedStemTrie = static_cast<char16_t*>(uprv_malloc(numBytes));
143
1
    uprv_memcpy(kSerializedStemTrie, result.getBuffer(), numBytes);
144
1
}
145
146
147
0
inline void appendMultiple(UnicodeString& sb, UChar32 cp, int32_t count) {
148
0
    for (int i = 0; i < count; i++) {
149
0
        sb.append(cp);
150
0
    }
151
0
}
152
153
154
5.58k
#define CHECK_NULL(seen, field, status) (void)(seen); /* for auto-format line wrapping */ \
155
5.58k
UPRV_BLOCK_MACRO_BEGIN { \
156
5.58k
    if ((seen).field) { \
157
38
        (status) = U_NUMBER_SKELETON_SYNTAX_ERROR; \
158
38
        return STATE_NULL; \
159
38
    } \
160
5.58k
    (seen).field = true; \
161
5.58k
} UPRV_BLOCK_MACRO_END
162
163
164
} // anonymous namespace
165
166
167
1.72k
Notation stem_to_object::notation(skeleton::StemEnum stem) {
168
1.72k
    switch (stem) {
169
1.65k
        case STEM_COMPACT_SHORT:
170
1.65k
            return Notation::compactShort();
171
65
        case STEM_COMPACT_LONG:
172
65
            return Notation::compactLong();
173
0
        case STEM_SCIENTIFIC:
174
0
            return Notation::scientific();
175
0
        case STEM_ENGINEERING:
176
0
            return Notation::engineering();
177
0
        case STEM_NOTATION_SIMPLE:
178
0
            return Notation::simple();
179
0
        default:
180
0
            UPRV_UNREACHABLE_EXIT;
181
1.72k
    }
182
1.72k
}
183
184
900
MeasureUnit stem_to_object::unit(skeleton::StemEnum stem) {
185
900
    switch (stem) {
186
0
        case STEM_BASE_UNIT:
187
0
            return {};
188
900
        case STEM_PERCENT:
189
900
            return MeasureUnit::getPercent();
190
0
        case STEM_PERMILLE:
191
0
            return MeasureUnit::getPermille();
192
0
        default:
193
0
            UPRV_UNREACHABLE_EXIT;
194
900
    }
195
900
}
196
197
0
Precision stem_to_object::precision(skeleton::StemEnum stem) {
198
0
    switch (stem) {
199
0
        case STEM_PRECISION_INTEGER:
200
0
            return Precision::integer();
201
0
        case STEM_PRECISION_UNLIMITED:
202
0
            return Precision::unlimited();
203
0
        case STEM_PRECISION_CURRENCY_STANDARD:
204
0
            return Precision::currency(UCURR_USAGE_STANDARD);
205
0
        case STEM_PRECISION_CURRENCY_CASH:
206
0
            return Precision::currency(UCURR_USAGE_CASH);
207
0
        default:
208
0
            UPRV_UNREACHABLE_EXIT;
209
0
    }
210
0
}
211
212
0
UNumberFormatRoundingMode stem_to_object::roundingMode(skeleton::StemEnum stem) {
213
0
    switch (stem) {
214
0
        case STEM_ROUNDING_MODE_CEILING:
215
0
            return UNUM_ROUND_CEILING;
216
0
        case STEM_ROUNDING_MODE_FLOOR:
217
0
            return UNUM_ROUND_FLOOR;
218
0
        case STEM_ROUNDING_MODE_DOWN:
219
0
            return UNUM_ROUND_DOWN;
220
0
        case STEM_ROUNDING_MODE_UP:
221
0
            return UNUM_ROUND_UP;
222
0
        case STEM_ROUNDING_MODE_HALF_EVEN:
223
0
            return UNUM_ROUND_HALFEVEN;
224
0
        case STEM_ROUNDING_MODE_HALF_ODD:
225
0
            return UNUM_ROUND_HALF_ODD;
226
0
        case STEM_ROUNDING_MODE_HALF_CEILING:
227
0
            return UNUM_ROUND_HALF_CEILING;
228
0
        case STEM_ROUNDING_MODE_HALF_FLOOR:
229
0
            return UNUM_ROUND_HALF_FLOOR;
230
0
        case STEM_ROUNDING_MODE_HALF_DOWN:
231
0
            return UNUM_ROUND_HALFDOWN;
232
0
        case STEM_ROUNDING_MODE_HALF_UP:
233
0
            return UNUM_ROUND_HALFUP;
234
0
        case STEM_ROUNDING_MODE_UNNECESSARY:
235
0
            return UNUM_ROUND_UNNECESSARY;
236
0
        default:
237
0
            UPRV_UNREACHABLE_EXIT;
238
0
    }
239
0
}
240
241
27
UNumberGroupingStrategy stem_to_object::groupingStrategy(skeleton::StemEnum stem) {
242
27
    switch (stem) {
243
10
        case STEM_GROUP_OFF:
244
10
            return UNUM_GROUPING_OFF;
245
2
        case STEM_GROUP_MIN2:
246
2
            return UNUM_GROUPING_MIN2;
247
0
        case STEM_GROUP_AUTO:
248
0
            return UNUM_GROUPING_AUTO;
249
15
        case STEM_GROUP_ON_ALIGNED:
250
15
            return UNUM_GROUPING_ON_ALIGNED;
251
0
        case STEM_GROUP_THOUSANDS:
252
0
            return UNUM_GROUPING_THOUSANDS;
253
0
        default:
254
0
            return UNUM_GROUPING_COUNT; // for objects, throw; for enums, return COUNT
255
27
    }
256
27
}
257
258
0
UNumberUnitWidth stem_to_object::unitWidth(skeleton::StemEnum stem) {
259
0
    switch (stem) {
260
0
        case STEM_UNIT_WIDTH_NARROW:
261
0
            return UNUM_UNIT_WIDTH_NARROW;
262
0
        case STEM_UNIT_WIDTH_SHORT:
263
0
            return UNUM_UNIT_WIDTH_SHORT;
264
0
        case STEM_UNIT_WIDTH_FULL_NAME:
265
0
            return UNUM_UNIT_WIDTH_FULL_NAME;
266
0
        case STEM_UNIT_WIDTH_ISO_CODE:
267
0
            return UNUM_UNIT_WIDTH_ISO_CODE;
268
0
        case STEM_UNIT_WIDTH_FORMAL:
269
0
            return UNUM_UNIT_WIDTH_FORMAL;
270
0
        case STEM_UNIT_WIDTH_VARIANT:
271
0
            return UNUM_UNIT_WIDTH_VARIANT;
272
0
        case STEM_UNIT_WIDTH_HIDDEN:
273
0
            return UNUM_UNIT_WIDTH_HIDDEN;
274
0
        default:
275
0
            return UNUM_UNIT_WIDTH_COUNT; // for objects, throw; for enums, return COUNT
276
0
    }
277
0
}
278
279
39
UNumberSignDisplay stem_to_object::signDisplay(skeleton::StemEnum stem) {
280
39
    switch (stem) {
281
0
        case STEM_SIGN_AUTO:
282
0
            return UNUM_SIGN_AUTO;
283
7
        case STEM_SIGN_ALWAYS:
284
7
            return UNUM_SIGN_ALWAYS;
285
9
        case STEM_SIGN_NEVER:
286
9
            return UNUM_SIGN_NEVER;
287
5
        case STEM_SIGN_ACCOUNTING:
288
5
            return UNUM_SIGN_ACCOUNTING;
289
2
        case STEM_SIGN_ACCOUNTING_ALWAYS:
290
2
            return UNUM_SIGN_ACCOUNTING_ALWAYS;
291
5
        case STEM_SIGN_EXCEPT_ZERO:
292
5
            return UNUM_SIGN_EXCEPT_ZERO;
293
4
        case STEM_SIGN_ACCOUNTING_EXCEPT_ZERO:
294
4
            return UNUM_SIGN_ACCOUNTING_EXCEPT_ZERO;
295
5
        case STEM_SIGN_NEGATIVE:
296
5
            return UNUM_SIGN_NEGATIVE;
297
2
        case STEM_SIGN_ACCOUNTING_NEGATIVE:
298
2
            return UNUM_SIGN_ACCOUNTING_NEGATIVE;
299
0
        default:
300
0
            return UNUM_SIGN_COUNT; // for objects, throw; for enums, return COUNT
301
39
    }
302
39
}
303
304
0
UNumberDecimalSeparatorDisplay stem_to_object::decimalSeparatorDisplay(skeleton::StemEnum stem) {
305
0
    switch (stem) {
306
0
        case STEM_DECIMAL_AUTO:
307
0
            return UNUM_DECIMAL_SEPARATOR_AUTO;
308
0
        case STEM_DECIMAL_ALWAYS:
309
0
            return UNUM_DECIMAL_SEPARATOR_ALWAYS;
310
0
        default:
311
0
            return UNUM_DECIMAL_SEPARATOR_COUNT; // for objects, throw; for enums, return COUNT
312
0
    }
313
0
}
314
315
316
0
void enum_to_stem_string::roundingMode(UNumberFormatRoundingMode value, UnicodeString& sb) {
317
0
    switch (value) {
318
0
        case UNUM_ROUND_CEILING:
319
0
            sb.append(u"rounding-mode-ceiling", -1);
320
0
            break;
321
0
        case UNUM_ROUND_FLOOR:
322
0
            sb.append(u"rounding-mode-floor", -1);
323
0
            break;
324
0
        case UNUM_ROUND_DOWN:
325
0
            sb.append(u"rounding-mode-down", -1);
326
0
            break;
327
0
        case UNUM_ROUND_UP:
328
0
            sb.append(u"rounding-mode-up", -1);
329
0
            break;
330
0
        case UNUM_ROUND_HALFEVEN:
331
0
            sb.append(u"rounding-mode-half-even", -1);
332
0
            break;
333
0
        case UNUM_ROUND_HALF_ODD:
334
0
            sb.append(u"rounding-mode-half-odd", -1);
335
0
            break;
336
0
        case UNUM_ROUND_HALF_CEILING:
337
0
            sb.append(u"rounding-mode-half-ceiling", -1);
338
0
            break;
339
0
        case UNUM_ROUND_HALF_FLOOR:
340
0
            sb.append(u"rounding-mode-half-floor", -1);
341
0
            break;
342
0
        case UNUM_ROUND_HALFDOWN:
343
0
            sb.append(u"rounding-mode-half-down", -1);
344
0
            break;
345
0
        case UNUM_ROUND_HALFUP:
346
0
            sb.append(u"rounding-mode-half-up", -1);
347
0
            break;
348
0
        case UNUM_ROUND_UNNECESSARY:
349
0
            sb.append(u"rounding-mode-unnecessary", -1);
350
0
            break;
351
0
        default:
352
0
            UPRV_UNREACHABLE_EXIT;
353
0
    }
354
0
}
355
356
0
void enum_to_stem_string::groupingStrategy(UNumberGroupingStrategy value, UnicodeString& sb) {
357
0
    switch (value) {
358
0
        case UNUM_GROUPING_OFF:
359
0
            sb.append(u"group-off", -1);
360
0
            break;
361
0
        case UNUM_GROUPING_MIN2:
362
0
            sb.append(u"group-min2", -1);
363
0
            break;
364
0
        case UNUM_GROUPING_AUTO:
365
0
            sb.append(u"group-auto", -1);
366
0
            break;
367
0
        case UNUM_GROUPING_ON_ALIGNED:
368
0
            sb.append(u"group-on-aligned", -1);
369
0
            break;
370
0
        case UNUM_GROUPING_THOUSANDS:
371
0
            sb.append(u"group-thousands", -1);
372
0
            break;
373
0
        default:
374
0
            UPRV_UNREACHABLE_EXIT;
375
0
    }
376
0
}
377
378
0
void enum_to_stem_string::unitWidth(UNumberUnitWidth value, UnicodeString& sb) {
379
0
    switch (value) {
380
0
        case UNUM_UNIT_WIDTH_NARROW:
381
0
            sb.append(u"unit-width-narrow", -1);
382
0
            break;
383
0
        case UNUM_UNIT_WIDTH_SHORT:
384
0
            sb.append(u"unit-width-short", -1);
385
0
            break;
386
0
        case UNUM_UNIT_WIDTH_FULL_NAME:
387
0
            sb.append(u"unit-width-full-name", -1);
388
0
            break;
389
0
        case UNUM_UNIT_WIDTH_ISO_CODE:
390
0
            sb.append(u"unit-width-iso-code", -1);
391
0
            break;
392
0
        case UNUM_UNIT_WIDTH_FORMAL:
393
0
            sb.append(u"unit-width-formal", -1);
394
0
            break;
395
0
        case UNUM_UNIT_WIDTH_VARIANT:
396
0
            sb.append(u"unit-width-variant", -1);
397
0
            break;
398
0
        case UNUM_UNIT_WIDTH_HIDDEN:
399
0
            sb.append(u"unit-width-hidden", -1);
400
0
            break;
401
0
        default:
402
0
            UPRV_UNREACHABLE_EXIT;
403
0
    }
404
0
}
405
406
0
void enum_to_stem_string::signDisplay(UNumberSignDisplay value, UnicodeString& sb) {
407
0
    switch (value) {
408
0
        case UNUM_SIGN_AUTO:
409
0
            sb.append(u"sign-auto", -1);
410
0
            break;
411
0
        case UNUM_SIGN_ALWAYS:
412
0
            sb.append(u"sign-always", -1);
413
0
            break;
414
0
        case UNUM_SIGN_NEVER:
415
0
            sb.append(u"sign-never", -1);
416
0
            break;
417
0
        case UNUM_SIGN_ACCOUNTING:
418
0
            sb.append(u"sign-accounting", -1);
419
0
            break;
420
0
        case UNUM_SIGN_ACCOUNTING_ALWAYS:
421
0
            sb.append(u"sign-accounting-always", -1);
422
0
            break;
423
0
        case UNUM_SIGN_EXCEPT_ZERO:
424
0
            sb.append(u"sign-except-zero", -1);
425
0
            break;
426
0
        case UNUM_SIGN_ACCOUNTING_EXCEPT_ZERO:
427
0
            sb.append(u"sign-accounting-except-zero", -1);
428
0
            break;
429
0
        case UNUM_SIGN_NEGATIVE:
430
0
            sb.append(u"sign-negative", -1);
431
0
            break;
432
0
        case UNUM_SIGN_ACCOUNTING_NEGATIVE:
433
0
            sb.append(u"sign-accounting-negative", -1);
434
0
            break;
435
0
        default:
436
0
            UPRV_UNREACHABLE_EXIT;
437
0
    }
438
0
}
439
440
void
441
0
enum_to_stem_string::decimalSeparatorDisplay(UNumberDecimalSeparatorDisplay value, UnicodeString& sb) {
442
0
    switch (value) {
443
0
        case UNUM_DECIMAL_SEPARATOR_AUTO:
444
0
            sb.append(u"decimal-auto", -1);
445
0
            break;
446
0
        case UNUM_DECIMAL_SEPARATOR_ALWAYS:
447
0
            sb.append(u"decimal-always", -1);
448
0
            break;
449
0
        default:
450
0
            UPRV_UNREACHABLE_EXIT;
451
0
    }
452
0
}
453
454
455
UnlocalizedNumberFormatter skeleton::create(
456
4.65k
        const UnicodeString& skeletonString, UParseError* perror, UErrorCode& status) {
457
458
    // Initialize perror
459
4.65k
    if (perror != nullptr) {
460
0
        perror->line = 0;
461
0
        perror->offset = -1;
462
0
        perror->preContext[0] = 0;
463
0
        perror->postContext[0] = 0;
464
0
    }
465
466
4.65k
    umtx_initOnce(gNumberSkeletonsInitOnce, &initNumberSkeletons, status);
467
4.65k
    if (U_FAILURE(status)) {
468
0
        return {};
469
0
    }
470
471
4.65k
    int32_t errOffset;
472
4.65k
    MacroProps macros = parseSkeleton(skeletonString, errOffset, status);
473
4.65k
    if (U_SUCCESS(status)) {
474
3.71k
        return NumberFormatter::with().macros(macros);
475
3.71k
    }
476
477
933
    if (perror == nullptr) {
478
933
        return {};
479
933
    }
480
481
    // Populate the UParseError with the error location
482
0
    perror->offset = errOffset;
483
0
    int32_t contextStart = uprv_max(0, errOffset - U_PARSE_CONTEXT_LEN + 1);
484
0
    int32_t contextEnd = uprv_min(skeletonString.length(), errOffset + U_PARSE_CONTEXT_LEN - 1);
485
0
    skeletonString.extract(contextStart, errOffset - contextStart, perror->preContext, 0);
486
0
    perror->preContext[errOffset - contextStart] = 0;
487
0
    skeletonString.extract(errOffset, contextEnd - errOffset, perror->postContext, 0);
488
0
    perror->postContext[contextEnd - errOffset] = 0;
489
0
    return {};
490
933
}
491
492
0
UnicodeString skeleton::generate(const MacroProps& macros, UErrorCode& status) {
493
0
    umtx_initOnce(gNumberSkeletonsInitOnce, &initNumberSkeletons, status);
494
0
    UnicodeString sb;
495
0
    GeneratorHelpers::generateSkeleton(macros, sb, status);
496
0
    return sb;
497
0
}
498
499
MacroProps skeleton::parseSkeleton(
500
4.65k
        const UnicodeString& skeletonString, int32_t& errOffset, UErrorCode& status) {
501
4.65k
    U_ASSERT(U_SUCCESS(status));
502
4.65k
    U_ASSERT(kSerializedStemTrie != nullptr);
503
504
    // Add a trailing whitespace to the end of the skeleton string to make code cleaner.
505
4.65k
    UnicodeString tempSkeletonString(skeletonString);
506
4.65k
    tempSkeletonString.append(u' ');
507
508
4.65k
    SeenMacroProps seen;
509
4.65k
    MacroProps macros;
510
4.65k
    StringSegment segment(tempSkeletonString, false);
511
4.65k
    UCharsTrie stemTrie(kSerializedStemTrie);
512
4.65k
    ParseState stem = STATE_NULL;
513
4.65k
    int32_t offset = 0;
514
515
    // Primary skeleton parse loop:
516
22.7M
    while (offset < segment.length()) {
517
22.7M
        UChar32 cp = segment.codePointAt(offset);
518
22.7M
        bool isTokenSeparator = PatternProps::isWhiteSpace(cp);
519
22.7M
        bool isOptionSeparator = (cp == u'/');
520
521
22.7M
        if (!isTokenSeparator && !isOptionSeparator) {
522
            // Non-separator token; consume it.
523
22.7M
            offset += U16_LENGTH(cp);
524
22.7M
            if (stem == STATE_NULL) {
525
                // We are currently consuming a stem.
526
                // Go to the next state in the stem trie.
527
9.07M
                stemTrie.nextForCodePoint(cp);
528
9.07M
            }
529
22.7M
            continue;
530
22.7M
        }
531
532
        // We are looking at a token or option separator.
533
        // If the segment is nonempty, parse it and reset the segment.
534
        // Otherwise, make sure it is a valid repeating separator.
535
9.15k
        if (offset != 0) {
536
7.78k
            segment.setLength(offset);
537
7.78k
            if (stem == STATE_NULL) {
538
                // The first separator after the start of a token. Parse it as a stem.
539
5.82k
                stem = parseStem(segment, stemTrie, seen, macros, status);
540
5.82k
                stemTrie.reset();
541
5.82k
            } else {
542
                // A separator after the first separator of a token. Parse it as an option.
543
1.96k
                stem = parseOption(stem, segment, macros, status);
544
1.96k
            }
545
7.78k
            segment.resetLength();
546
7.78k
            if (U_FAILURE(status)) {
547
918
                errOffset = segment.getOffset();
548
918
                return macros;
549
918
            }
550
551
            // Consume the segment:
552
6.87k
            segment.adjustOffset(offset);
553
6.87k
            offset = 0;
554
555
6.87k
        } else if (stem != STATE_NULL) {
556
            // A separator ('/' or whitespace) following an option separator ('/')
557
            // segment.setLength(U16_LENGTH(cp)); // for error message
558
            // throw new SkeletonSyntaxException("Unexpected separator character", segment);
559
5
            status = U_NUMBER_SKELETON_SYNTAX_ERROR;
560
5
            errOffset = segment.getOffset();
561
5
            return macros;
562
563
1.36k
        } else {
564
            // Two spaces in a row; this is OK.
565
1.36k
        }
566
567
        // Does the current stem forbid options?
568
8.23k
        if (isOptionSeparator && stem == STATE_NULL) {
569
            // segment.setLength(U16_LENGTH(cp)); // for error message
570
            // throw new SkeletonSyntaxException("Unexpected option separator", segment);
571
8
            status = U_NUMBER_SKELETON_SYNTAX_ERROR;
572
8
            errOffset = segment.getOffset();
573
8
            return macros;
574
8
        }
575
576
        // Does the current stem require an option?
577
8.22k
        if (isTokenSeparator && stem != STATE_NULL) {
578
597
            switch (stem) {
579
0
                case STATE_INCREMENT_PRECISION:
580
0
                case STATE_MEASURE_UNIT:
581
0
                case STATE_PER_MEASURE_UNIT:
582
0
                case STATE_IDENTIFIER_UNIT:
583
1
                case STATE_UNIT_USAGE:
584
1
                case STATE_CURRENCY_UNIT:
585
1
                case STATE_INTEGER_WIDTH:
586
1
                case STATE_NUMBERING_SYSTEM:
587
2
                case STATE_SCALE:
588
                    // segment.setLength(U16_LENGTH(cp)); // for error message
589
                    // throw new SkeletonSyntaxException("Stem requires an option", segment);
590
2
                    status = U_NUMBER_SKELETON_SYNTAX_ERROR;
591
2
                    errOffset = segment.getOffset();
592
2
                    return macros;
593
595
                default:
594
595
                    break;
595
597
            }
596
595
            stem = STATE_NULL;
597
595
        }
598
599
        // Consume the separator:
600
8.22k
        segment.adjustOffset(U16_LENGTH(cp));
601
8.22k
    }
602
3.71k
    U_ASSERT(stem == STATE_NULL);
603
3.71k
    return macros;
604
4.65k
}
605
606
ParseState
607
skeleton::parseStem(const StringSegment& segment, const UCharsTrie& stemTrie, SeenMacroProps& seen,
608
5.82k
                    MacroProps& macros, UErrorCode& status) {
609
5.82k
    U_ASSERT(U_SUCCESS(status));
610
611
    // First check for "blueprint" stems, which start with a "signal char"
612
5.82k
    switch (segment.charAt(0)) {
613
567
        case u'.':
614
567
            CHECK_NULL(seen, precision, status);
615
563
            blueprint_helpers::parseFractionStem(segment, macros, status);
616
563
            return STATE_FRACTION_PRECISION;
617
290
        case u'@':
618
290
            CHECK_NULL(seen, precision, status);
619
289
            blueprint_helpers::parseDigitsStem(segment, macros, status);
620
289
            return STATE_PRECISION;
621
186
        case u'E':
622
186
            CHECK_NULL(seen, notation, status);
623
184
            blueprint_helpers::parseScientificStem(segment, macros, status);
624
184
            return STATE_NULL;
625
159
        case u'0':
626
159
            CHECK_NULL(seen, integerWidth, status);
627
155
            blueprint_helpers::parseIntegerStem(segment, macros, status);
628
155
            return STATE_NULL;
629
4.61k
        default:
630
4.61k
            break;
631
5.82k
    }
632
633
    // Now look at the stemsTrie, which is already be pointing at our stem.
634
4.61k
    UStringTrieResult stemResult = stemTrie.current();
635
636
4.61k
    if (stemResult != USTRINGTRIE_INTERMEDIATE_VALUE && stemResult != USTRINGTRIE_FINAL_VALUE) {
637
        // throw new SkeletonSyntaxException("Unknown stem", segment);
638
249
        status = U_NUMBER_SKELETON_SYNTAX_ERROR;
639
249
        return STATE_NULL;
640
249
    }
641
642
4.36k
    auto stem = static_cast<StemEnum>(stemTrie.getValue());
643
4.36k
    switch (stem) {
644
645
        // Stems with meaning on their own, not requiring an option:
646
647
1.66k
        case STEM_COMPACT_SHORT:
648
1.72k
        case STEM_COMPACT_LONG:
649
1.72k
        case STEM_SCIENTIFIC:
650
1.72k
        case STEM_ENGINEERING:
651
1.72k
        case STEM_NOTATION_SIMPLE:
652
1.72k
            CHECK_NULL(seen, notation, status);
653
1.72k
            macros.notation = stem_to_object::notation(stem);
654
1.72k
            switch (stem) {
655
0
                case STEM_SCIENTIFIC:
656
0
                case STEM_ENGINEERING:
657
0
                    return STATE_SCIENTIFIC; // allows for scientific options
658
1.72k
                default:
659
1.72k
                    return STATE_NULL;
660
1.72k
            }
661
662
0
        case STEM_BASE_UNIT:
663
903
        case STEM_PERCENT:
664
903
        case STEM_PERMILLE:
665
903
            CHECK_NULL(seen, unit, status);
666
900
            macros.unit = stem_to_object::unit(stem);
667
900
            return STATE_NULL;
668
669
11
        case STEM_PERCENT_100:
670
11
            CHECK_NULL(seen, scale, status);
671
10
            CHECK_NULL(seen, unit, status);
672
9
            macros.scale = Scale::powerOfTen(2);
673
9
            macros.unit = NoUnit::percent();
674
9
            return STATE_NULL;
675
676
0
        case STEM_PRECISION_INTEGER:
677
0
        case STEM_PRECISION_UNLIMITED:
678
0
        case STEM_PRECISION_CURRENCY_STANDARD:
679
0
        case STEM_PRECISION_CURRENCY_CASH:
680
0
            CHECK_NULL(seen, precision, status);
681
0
            macros.precision = stem_to_object::precision(stem);
682
0
            switch (stem) {
683
0
                case STEM_PRECISION_INTEGER:
684
0
                    return STATE_FRACTION_PRECISION; // allows for "precision-integer/@##"
685
0
                default:
686
0
                    return STATE_PRECISION;
687
0
            }
688
689
0
        case STEM_ROUNDING_MODE_CEILING:
690
0
        case STEM_ROUNDING_MODE_FLOOR:
691
0
        case STEM_ROUNDING_MODE_DOWN:
692
0
        case STEM_ROUNDING_MODE_UP:
693
0
        case STEM_ROUNDING_MODE_HALF_EVEN:
694
0
        case STEM_ROUNDING_MODE_HALF_ODD:
695
0
        case STEM_ROUNDING_MODE_HALF_CEILING:
696
0
        case STEM_ROUNDING_MODE_HALF_FLOOR:
697
0
        case STEM_ROUNDING_MODE_HALF_DOWN:
698
0
        case STEM_ROUNDING_MODE_HALF_UP:
699
0
        case STEM_ROUNDING_MODE_UNNECESSARY:
700
0
            CHECK_NULL(seen, roundingMode, status);
701
0
            macros.roundingMode = stem_to_object::roundingMode(stem);
702
0
            return STATE_NULL;
703
704
0
        case STEM_INTEGER_WIDTH_TRUNC:
705
0
            CHECK_NULL(seen, integerWidth, status);
706
0
            macros.integerWidth = IntegerWidth::zeroFillTo(0).truncateAt(0);
707
0
            return STATE_NULL;
708
709
11
        case STEM_GROUP_OFF:
710
14
        case STEM_GROUP_MIN2:
711
14
        case STEM_GROUP_AUTO:
712
32
        case STEM_GROUP_ON_ALIGNED:
713
32
        case STEM_GROUP_THOUSANDS:
714
32
            CHECK_NULL(seen, grouper, status);
715
27
            macros.grouper = Grouper::forStrategy(stem_to_object::groupingStrategy(stem));
716
27
            return STATE_NULL;
717
718
0
        case STEM_LATIN:
719
0
            CHECK_NULL(seen, symbols, status);
720
0
            macros.symbols.setTo(NumberingSystem::createInstanceByName("latn", status));
721
0
            return STATE_NULL;
722
723
0
        case STEM_UNIT_WIDTH_NARROW:
724
0
        case STEM_UNIT_WIDTH_SHORT:
725
0
        case STEM_UNIT_WIDTH_FULL_NAME:
726
0
        case STEM_UNIT_WIDTH_ISO_CODE:
727
0
        case STEM_UNIT_WIDTH_FORMAL:
728
0
        case STEM_UNIT_WIDTH_VARIANT:
729
0
        case STEM_UNIT_WIDTH_HIDDEN:
730
0
            CHECK_NULL(seen, unitWidth, status);
731
0
            macros.unitWidth = stem_to_object::unitWidth(stem);
732
0
            return STATE_NULL;
733
734
0
        case STEM_SIGN_AUTO:
735
8
        case STEM_SIGN_ALWAYS:
736
19
        case STEM_SIGN_NEVER:
737
27
        case STEM_SIGN_ACCOUNTING:
738
30
        case STEM_SIGN_ACCOUNTING_ALWAYS:
739
36
        case STEM_SIGN_EXCEPT_ZERO:
740
41
        case STEM_SIGN_ACCOUNTING_EXCEPT_ZERO:
741
47
        case STEM_SIGN_NEGATIVE:
742
50
        case STEM_SIGN_ACCOUNTING_NEGATIVE:
743
50
            CHECK_NULL(seen, sign, status);
744
39
            macros.sign = stem_to_object::signDisplay(stem);
745
39
            return STATE_NULL;
746
747
0
        case STEM_DECIMAL_AUTO:
748
0
        case STEM_DECIMAL_ALWAYS:
749
0
            CHECK_NULL(seen, decimal, status);
750
0
            macros.decimal = stem_to_object::decimalSeparatorDisplay(stem);
751
0
            return STATE_NULL;
752
753
        // Stems requiring an option:
754
755
0
        case STEM_PRECISION_INCREMENT:
756
0
            CHECK_NULL(seen, precision, status);
757
0
            return STATE_INCREMENT_PRECISION;
758
759
0
        case STEM_MEASURE_UNIT:
760
0
            CHECK_NULL(seen, unit, status);
761
0
            return STATE_MEASURE_UNIT;
762
763
0
        case STEM_PER_MEASURE_UNIT:
764
0
            CHECK_NULL(seen, perUnit, status);
765
0
            return STATE_PER_MEASURE_UNIT;
766
767
0
        case STEM_UNIT:
768
0
            CHECK_NULL(seen, unit, status);
769
0
            CHECK_NULL(seen, perUnit, status);
770
0
            return STATE_IDENTIFIER_UNIT;
771
772
75
        case STEM_UNIT_USAGE:
773
75
            CHECK_NULL(seen, usage, status);
774
73
            return STATE_UNIT_USAGE;
775
776
0
        case STEM_CURRENCY:
777
0
            CHECK_NULL(seen, unit, status);
778
0
            CHECK_NULL(seen, perUnit, status);
779
0
            return STATE_CURRENCY_UNIT;
780
781
0
        case STEM_INTEGER_WIDTH:
782
0
            CHECK_NULL(seen, integerWidth, status);
783
0
            return STATE_INTEGER_WIDTH;
784
785
0
        case STEM_NUMBERING_SYSTEM:
786
0
            CHECK_NULL(seen, symbols, status);
787
0
            return STATE_NUMBERING_SYSTEM;
788
789
1.57k
        case STEM_SCALE:
790
1.57k
            CHECK_NULL(seen, scale, status);
791
1.57k
            return STATE_SCALE;
792
793
0
        default:
794
0
            UPRV_UNREACHABLE_EXIT;
795
4.36k
    }
796
4.36k
}
797
798
ParseState skeleton::parseOption(ParseState stem, const StringSegment& segment, MacroProps& macros,
799
1.96k
                                 UErrorCode& status) {
800
1.96k
    U_ASSERT(U_SUCCESS(status));
801
802
    ///// Required options: /////
803
804
1.96k
    switch (stem) {
805
0
        case STATE_CURRENCY_UNIT:
806
0
            blueprint_helpers::parseCurrencyOption(segment, macros, status);
807
0
            return STATE_NULL;
808
0
        case STATE_MEASURE_UNIT:
809
0
            blueprint_helpers::parseMeasureUnitOption(segment, macros, status);
810
0
            return STATE_NULL;
811
0
        case STATE_PER_MEASURE_UNIT:
812
0
            blueprint_helpers::parseMeasurePerUnitOption(segment, macros, status);
813
0
            return STATE_NULL;
814
0
        case STATE_IDENTIFIER_UNIT:
815
0
            blueprint_helpers::parseIdentifierUnitOption(segment, macros, status);
816
0
            return STATE_NULL;
817
72
        case STATE_UNIT_USAGE:
818
72
            blueprint_helpers::parseUnitUsageOption(segment, macros, status);
819
72
            return STATE_NULL;
820
0
        case STATE_INCREMENT_PRECISION:
821
0
            blueprint_helpers::parseIncrementOption(segment, macros, status);
822
0
            return STATE_PRECISION;
823
0
        case STATE_INTEGER_WIDTH:
824
0
            blueprint_helpers::parseIntegerWidthOption(segment, macros, status);
825
0
            return STATE_NULL;
826
0
        case STATE_NUMBERING_SYSTEM:
827
0
            blueprint_helpers::parseNumberingSystemOption(segment, macros, status);
828
0
            return STATE_NULL;
829
1.56k
        case STATE_SCALE:
830
1.56k
            blueprint_helpers::parseScaleOption(segment, macros, status);
831
1.56k
            return STATE_NULL;
832
327
        default:
833
327
            break;
834
1.96k
    }
835
836
    ///// Non-required options: /////
837
838
    // Scientific options
839
327
    switch (stem) {
840
0
        case STATE_SCIENTIFIC:
841
0
            if (blueprint_helpers::parseExponentWidthOption(segment, macros, status)) {
842
0
                return STATE_SCIENTIFIC;
843
0
            }
844
0
            if (U_FAILURE(status)) {
845
0
                return {};
846
0
            }
847
0
            if (blueprint_helpers::parseExponentSignOption(segment, macros, status)) {
848
0
                return STATE_SCIENTIFIC;
849
0
            }
850
0
            if (U_FAILURE(status)) {
851
0
                return {};
852
0
            }
853
0
            break;
854
327
        default:
855
327
            break;
856
327
    }
857
858
    // Frac-sig option
859
327
    switch (stem) {
860
308
        case STATE_FRACTION_PRECISION:
861
308
            if (blueprint_helpers::parseFracSigOption(segment, macros, status)) {
862
176
                return STATE_PRECISION;
863
176
            }
864
132
            if (U_FAILURE(status)) {
865
62
                return {};
866
62
            }
867
            // If the fracSig option was not found, try normal precision options.
868
70
            stem = STATE_PRECISION;
869
70
            break;
870
19
        default:
871
19
            break;
872
327
    }
873
874
    // Trailing zeros option
875
89
    switch (stem) {
876
89
        case STATE_PRECISION:
877
89
            if (blueprint_helpers::parseTrailingZeroOption(segment, macros, status)) {
878
30
                return STATE_NULL;
879
30
            }
880
59
            if (U_FAILURE(status)) {
881
0
                return {};
882
0
            }
883
59
            break;
884
59
        default:
885
0
            break;
886
89
    }
887
888
    // Unknown option
889
    // throw new SkeletonSyntaxException("Invalid option", segment);
890
59
    status = U_NUMBER_SKELETON_SYNTAX_ERROR;
891
59
    return STATE_NULL;
892
89
}
893
894
0
void GeneratorHelpers::generateSkeleton(const MacroProps& macros, UnicodeString& sb, UErrorCode& status) {
895
0
    if (U_FAILURE(status)) { return; }
896
897
    // Supported options
898
0
    if (GeneratorHelpers::notation(macros, sb, status)) {
899
0
        sb.append(u' ');
900
0
    }
901
0
    if (U_FAILURE(status)) { return; }
902
0
    if (GeneratorHelpers::unit(macros, sb, status)) {
903
0
        sb.append(u' ');
904
0
    }
905
0
    if (U_FAILURE(status)) { return; }
906
0
    if (GeneratorHelpers::usage(macros, sb, status)) {
907
0
        sb.append(u' ');
908
0
    }
909
0
    if (U_FAILURE(status)) { return; }
910
0
    if (GeneratorHelpers::precision(macros, sb, status)) {
911
0
        sb.append(u' ');
912
0
    }
913
0
    if (U_FAILURE(status)) { return; }
914
0
    if (GeneratorHelpers::roundingMode(macros, sb, status)) {
915
0
        sb.append(u' ');
916
0
    }
917
0
    if (U_FAILURE(status)) { return; }
918
0
    if (GeneratorHelpers::grouping(macros, sb, status)) {
919
0
        sb.append(u' ');
920
0
    }
921
0
    if (U_FAILURE(status)) { return; }
922
0
    if (GeneratorHelpers::integerWidth(macros, sb, status)) {
923
0
        sb.append(u' ');
924
0
    }
925
0
    if (U_FAILURE(status)) { return; }
926
0
    if (GeneratorHelpers::symbols(macros, sb, status)) {
927
0
        sb.append(u' ');
928
0
    }
929
0
    if (U_FAILURE(status)) { return; }
930
0
    if (GeneratorHelpers::unitWidth(macros, sb, status)) {
931
0
        sb.append(u' ');
932
0
    }
933
0
    if (U_FAILURE(status)) { return; }
934
0
    if (GeneratorHelpers::sign(macros, sb, status)) {
935
0
        sb.append(u' ');
936
0
    }
937
0
    if (U_FAILURE(status)) { return; }
938
0
    if (GeneratorHelpers::decimal(macros, sb, status)) {
939
0
        sb.append(u' ');
940
0
    }
941
0
    if (U_FAILURE(status)) { return; }
942
0
    if (GeneratorHelpers::scale(macros, sb, status)) {
943
0
        sb.append(u' ');
944
0
    }
945
0
    if (U_FAILURE(status)) { return; }
946
947
    // Unsupported options
948
0
    if (!macros.padder.isBogus()) {
949
0
        status = U_UNSUPPORTED_ERROR;
950
0
        return;
951
0
    }
952
0
    if (macros.unitDisplayCase.isSet()) {
953
0
        status = U_UNSUPPORTED_ERROR;
954
0
        return;
955
0
    }
956
0
    if (macros.affixProvider != nullptr) {
957
0
        status = U_UNSUPPORTED_ERROR;
958
0
        return;
959
0
    }
960
0
    if (macros.rules != nullptr) {
961
0
        status = U_UNSUPPORTED_ERROR;
962
0
        return;
963
0
    }
964
965
    // Remove the trailing space
966
0
    if (sb.length() > 0) {
967
0
        sb.truncate(sb.length() - 1);
968
0
    }
969
0
}
970
971
972
bool blueprint_helpers::parseExponentWidthOption(const StringSegment& segment, MacroProps& macros,
973
0
                                                 UErrorCode&) {
974
0
    if (!isWildcardChar(segment.charAt(0))) {
975
0
        return false;
976
0
    }
977
0
    int32_t offset = 1;
978
0
    int32_t minExp = 0;
979
0
    for (; offset < segment.length(); offset++) {
980
0
        if (segment.charAt(offset) == u'e') {
981
0
            minExp++;
982
0
        } else {
983
0
            break;
984
0
        }
985
0
    }
986
0
    if (offset < segment.length()) {
987
0
        return false;
988
0
    }
989
    // Use the public APIs to enforce bounds checking
990
0
    macros.notation = static_cast<ScientificNotation&>(macros.notation).withMinExponentDigits(minExp);
991
0
    return true;
992
0
}
993
994
void
995
0
blueprint_helpers::generateExponentWidthOption(int32_t minExponentDigits, UnicodeString& sb, UErrorCode&) {
996
0
    sb.append(kWildcardChar);
997
0
    appendMultiple(sb, u'e', minExponentDigits);
998
0
}
999
1000
bool
1001
0
blueprint_helpers::parseExponentSignOption(const StringSegment& segment, MacroProps& macros, UErrorCode&) {
1002
    // Get the sign display type out of the CharsTrie data structure.
1003
0
    UCharsTrie tempStemTrie(kSerializedStemTrie);
1004
0
    UStringTrieResult result = tempStemTrie.next(
1005
0
            segment.toTempUnicodeString().getBuffer(),
1006
0
            segment.length());
1007
0
    if (result != USTRINGTRIE_INTERMEDIATE_VALUE && result != USTRINGTRIE_FINAL_VALUE) {
1008
0
        return false;
1009
0
    }
1010
0
    auto sign = stem_to_object::signDisplay(static_cast<StemEnum>(tempStemTrie.getValue()));
1011
0
    if (sign == UNUM_SIGN_COUNT) {
1012
0
        return false;
1013
0
    }
1014
0
    macros.notation = static_cast<ScientificNotation&>(macros.notation).withExponentSignDisplay(sign);
1015
0
    return true;
1016
0
}
1017
1018
// The function is called by skeleton::parseOption which called by skeleton::parseSkeleton
1019
// the data pointed in the return macros.unit is stack allocated in the parseSkeleton function.
1020
#if U_GCC_MAJOR_MINOR >= 1204
1021
#pragma GCC diagnostic push
1022
#pragma GCC diagnostic ignored "-Wdangling-pointer"
1023
#endif
1024
void blueprint_helpers::parseCurrencyOption(const StringSegment& segment, MacroProps& macros,
1025
0
                                            UErrorCode& status) {
1026
    // Unlike ICU4J, have to check length manually because ICU4C CurrencyUnit does not check it for us
1027
0
    if (segment.length() != 3) {
1028
0
        status = U_NUMBER_SKELETON_SYNTAX_ERROR;
1029
0
        return;
1030
0
    }
1031
0
    const char16_t* currencyCode = segment.toTempUnicodeString().getBuffer();
1032
0
    UErrorCode localStatus = U_ZERO_ERROR;
1033
0
    CurrencyUnit currency(currencyCode, localStatus);
1034
0
    if (U_FAILURE(localStatus)) {
1035
        // Not 3 ascii chars
1036
        // throw new SkeletonSyntaxException("Invalid currency", segment);
1037
0
        status = U_NUMBER_SKELETON_SYNTAX_ERROR;
1038
0
        return;
1039
0
    }
1040
    // Slicing is OK
1041
0
    macros.unit = currency; // NOLINT
1042
0
}
1043
#if U_GCC_MAJOR_MINOR >= 1204
1044
#pragma GCC diagnostic pop
1045
#endif
1046
1047
void
1048
0
blueprint_helpers::generateCurrencyOption(const CurrencyUnit& currency, UnicodeString& sb, UErrorCode&) {
1049
0
    sb.append(currency.getISOCurrency(), -1);
1050
0
}
1051
1052
void blueprint_helpers::parseMeasureUnitOption(const StringSegment& segment, MacroProps& macros,
1053
0
                                               UErrorCode& status) {
1054
0
    U_ASSERT(U_SUCCESS(status));
1055
0
    const UnicodeString stemString = segment.toTempUnicodeString();
1056
1057
    // NOTE: The category (type) of the unit is guaranteed to be a valid subtag (alphanumeric)
1058
    // http://unicode.org/reports/tr35/#Validity_Data
1059
0
    int firstHyphen = 0;
1060
0
    while (firstHyphen < stemString.length() && stemString.charAt(firstHyphen) != '-') {
1061
0
        firstHyphen++;
1062
0
    }
1063
0
    if (firstHyphen == stemString.length()) {
1064
        // throw new SkeletonSyntaxException("Invalid measure unit option", segment);
1065
0
        status = U_NUMBER_SKELETON_SYNTAX_ERROR;
1066
0
        return;
1067
0
    }
1068
1069
    // Need to do char <-> char16_t conversion...
1070
0
    CharString type;
1071
0
    SKELETON_UCHAR_TO_CHAR(type, stemString, 0, firstHyphen, status);
1072
0
    CharString subType;
1073
0
    SKELETON_UCHAR_TO_CHAR(subType, stemString, firstHyphen + 1, stemString.length(), status);
1074
1075
0
    MeasureUnit unit;
1076
0
    if (MeasureUnit::validateAndGet(type.toStringPiece(), subType.toStringPiece(), unit)) {
1077
0
        macros.unit = unit;
1078
0
        return;
1079
0
    }
1080
    
1081
0
    status = U_NUMBER_SKELETON_SYNTAX_ERROR;
1082
0
}
1083
1084
void blueprint_helpers::parseMeasurePerUnitOption(const StringSegment& segment, MacroProps& macros,
1085
0
                                                  UErrorCode& status) {
1086
    // A little bit of a hack: save the current unit (numerator), call the main measure unit
1087
    // parsing code, put back the numerator unit, and put the new unit into per-unit.
1088
0
    MeasureUnit numerator = macros.unit;
1089
0
    parseMeasureUnitOption(segment, macros, status);
1090
0
    if (U_FAILURE(status)) { return; }
1091
0
    macros.perUnit = macros.unit;
1092
0
    macros.unit = numerator;
1093
0
}
1094
1095
void blueprint_helpers::parseIdentifierUnitOption(const StringSegment& segment, MacroProps& macros,
1096
0
                                                  UErrorCode& status) {
1097
    // Need to do char <-> char16_t conversion...
1098
0
    U_ASSERT(U_SUCCESS(status));
1099
0
    CharString buffer;
1100
0
    SKELETON_UCHAR_TO_CHAR(buffer, segment.toTempUnicodeString(), 0, segment.length(), status);
1101
1102
0
    ErrorCode internalStatus;
1103
0
    macros.unit = MeasureUnit::forIdentifier(buffer.toStringPiece(), internalStatus);
1104
0
    if (internalStatus.isFailure()) {
1105
        // throw new SkeletonSyntaxException("Invalid core unit identifier", segment, e);
1106
0
        status = U_NUMBER_SKELETON_SYNTAX_ERROR;
1107
0
        return;
1108
0
    }
1109
0
}
1110
1111
void blueprint_helpers::parseUnitUsageOption(const StringSegment &segment, MacroProps &macros,
1112
72
                                             UErrorCode &status) {
1113
    // Need to do char <-> char16_t conversion...
1114
72
    U_ASSERT(U_SUCCESS(status));
1115
72
    CharString buffer;
1116
72
    SKELETON_UCHAR_TO_CHAR(buffer, segment.toTempUnicodeString(), 0, segment.length(), status);
1117
68
    macros.usage.set(buffer.toStringPiece());
1118
    // We do not do any validation of the usage string: it depends on the
1119
    // unitPreferenceData in the units resources.
1120
68
}
1121
1122
void blueprint_helpers::parseFractionStem(const StringSegment& segment, MacroProps& macros,
1123
563
                                          UErrorCode& status) {
1124
563
    U_ASSERT(segment.charAt(0) == u'.');
1125
563
    int32_t offset = 1;
1126
563
    int32_t minFrac = 0;
1127
563
    int32_t maxFrac;
1128
199k
    for (; offset < segment.length(); offset++) {
1129
199k
        if (segment.charAt(offset) == u'0') {
1130
199k
            minFrac++;
1131
199k
        } else {
1132
259
            break;
1133
259
        }
1134
199k
    }
1135
563
    if (offset < segment.length()) {
1136
259
        if (isWildcardChar(segment.charAt(offset))) {
1137
197
            maxFrac = -1;
1138
197
            offset++;
1139
197
        } else {
1140
62
            maxFrac = minFrac;
1141
525
            for (; offset < segment.length(); offset++) {
1142
510
                if (segment.charAt(offset) == u'#') {
1143
463
                    maxFrac++;
1144
463
                } else {
1145
47
                    break;
1146
47
                }
1147
510
            }
1148
62
        }
1149
304
    } else {
1150
304
        maxFrac = minFrac;
1151
304
    }
1152
563
    if (offset < segment.length()) {
1153
        // throw new SkeletonSyntaxException("Invalid fraction stem", segment);
1154
47
        status = U_NUMBER_SKELETON_SYNTAX_ERROR;
1155
47
        return;
1156
47
    }
1157
    // Use the public APIs to enforce bounds checking
1158
516
    if (maxFrac == -1) {
1159
197
        if (minFrac == 0) {
1160
144
            macros.precision = Precision::unlimited();
1161
144
        } else {
1162
53
            macros.precision = Precision::minFraction(minFrac);
1163
53
        }
1164
319
    } else {
1165
319
        macros.precision = Precision::minMaxFraction(minFrac, maxFrac);
1166
319
    }
1167
516
}
1168
1169
void
1170
0
blueprint_helpers::generateFractionStem(int32_t minFrac, int32_t maxFrac, UnicodeString& sb, UErrorCode&) {
1171
0
    if (minFrac == 0 && maxFrac == 0) {
1172
0
        sb.append(u"precision-integer", -1);
1173
0
        return;
1174
0
    }
1175
0
    sb.append(u'.');
1176
0
    appendMultiple(sb, u'0', minFrac);
1177
0
    if (maxFrac == -1) {
1178
0
        sb.append(kWildcardChar);
1179
0
    } else {
1180
0
        appendMultiple(sb, u'#', maxFrac - minFrac);
1181
0
    }
1182
0
}
1183
1184
void
1185
289
blueprint_helpers::parseDigitsStem(const StringSegment& segment, MacroProps& macros, UErrorCode& status) {
1186
289
    U_ASSERT(segment.charAt(0) == u'@');
1187
289
    int32_t offset = 0;
1188
289
    int32_t minSig = 0;
1189
289
    int32_t maxSig;
1190
106k
    for (; offset < segment.length(); offset++) {
1191
106k
        if (segment.charAt(offset) == u'@') {
1192
106k
            minSig++;
1193
106k
        } else {
1194
219
            break;
1195
219
        }
1196
106k
    }
1197
289
    if (offset < segment.length()) {
1198
219
        if (isWildcardChar(segment.charAt(offset))) {
1199
155
            maxSig = -1;
1200
155
            offset++;
1201
155
        } else {
1202
64
            maxSig = minSig;
1203
3.02k
            for (; offset < segment.length(); offset++) {
1204
3.00k
                if (segment.charAt(offset) == u'#') {
1205
2.95k
                    maxSig++;
1206
2.95k
                } else {
1207
51
                    break;
1208
51
                }
1209
3.00k
            }
1210
64
        }
1211
219
    } else {
1212
70
        maxSig = minSig;
1213
70
    }
1214
289
    if (offset < segment.length()) {
1215
        // throw new SkeletonSyntaxException("Invalid significant digits stem", segment);
1216
54
        status = U_NUMBER_SKELETON_SYNTAX_ERROR;
1217
54
        return;
1218
54
    }
1219
    // Use the public APIs to enforce bounds checking
1220
235
    if (maxSig == -1) {
1221
152
        macros.precision = Precision::minSignificantDigits(minSig);
1222
152
    } else {
1223
83
        macros.precision = Precision::minMaxSignificantDigits(minSig, maxSig);
1224
83
    }
1225
235
}
1226
1227
void
1228
0
blueprint_helpers::generateDigitsStem(int32_t minSig, int32_t maxSig, UnicodeString& sb, UErrorCode&) {
1229
0
    appendMultiple(sb, u'@', minSig);
1230
0
    if (maxSig == -1) {
1231
0
        sb.append(kWildcardChar);
1232
0
    } else {
1233
0
        appendMultiple(sb, u'#', maxSig - minSig);
1234
0
    }
1235
0
}
1236
1237
184
void blueprint_helpers::parseScientificStem(const StringSegment& segment, MacroProps& macros, UErrorCode& status) {
1238
184
    U_ASSERT(segment.charAt(0) == u'E');
1239
184
    {
1240
184
        int32_t offset = 1;
1241
184
        if (segment.length() == offset) {
1242
1
            goto fail;
1243
1
        }
1244
183
        bool isEngineering = false;
1245
183
        if (segment.charAt(offset) == u'E') {
1246
43
            isEngineering = true;
1247
43
            offset++;
1248
43
            if (segment.length() == offset) {
1249
1
                goto fail;
1250
1
            }
1251
43
        }
1252
182
        UNumberSignDisplay signDisplay = UNUM_SIGN_AUTO;
1253
182
        if (segment.charAt(offset) == u'+') {
1254
70
            offset++;
1255
70
            if (segment.length() == offset) {
1256
1
                goto fail;
1257
1
            }
1258
69
            if (segment.charAt(offset) == u'!') {
1259
22
                signDisplay = UNUM_SIGN_ALWAYS;
1260
47
            } else if (segment.charAt(offset) == u'?') {
1261
34
                signDisplay = UNUM_SIGN_EXCEPT_ZERO;
1262
34
            } else {
1263
                // NOTE: Other sign displays are not included because they aren't useful in this context
1264
13
                goto fail;
1265
13
            }
1266
56
            offset++;
1267
56
            if (segment.length() == offset) {
1268
2
                goto fail;
1269
2
            }
1270
56
        }
1271
166
        int32_t minDigits = 0;
1272
12.2k
        for (; offset < segment.length(); offset++) {
1273
12.1k
            if (segment.charAt(offset) != u'0') {
1274
74
                goto fail;
1275
74
            }
1276
12.0k
            minDigits++;
1277
12.0k
        }
1278
92
        macros.notation = (isEngineering ? Notation::engineering() : Notation::scientific())
1279
92
            .withExponentSignDisplay(signDisplay)
1280
92
            .withMinExponentDigits(minDigits);
1281
92
        return;
1282
166
    }
1283
92
    fail: void();
1284
    // throw new SkeletonSyntaxException("Invalid scientific stem", segment);
1285
92
    status = U_NUMBER_SKELETON_SYNTAX_ERROR;
1286
92
}
1287
1288
155
void blueprint_helpers::parseIntegerStem(const StringSegment& segment, MacroProps& macros, UErrorCode& status) {
1289
155
    U_ASSERT(segment.charAt(0) == u'0');
1290
155
    int32_t offset = 1;
1291
52.5k
    for (; offset < segment.length(); offset++) {
1292
52.3k
        if (segment.charAt(offset) != u'0') {
1293
41
            offset--;
1294
41
            break;
1295
41
        }
1296
52.3k
    }
1297
155
    if (offset < segment.length()) {
1298
        // throw new SkeletonSyntaxException("Invalid integer stem", segment);
1299
41
        status = U_NUMBER_SKELETON_SYNTAX_ERROR;
1300
41
        return;
1301
41
    }
1302
114
    macros.integerWidth = IntegerWidth::zeroFillTo(offset);
1303
114
}
1304
1305
bool blueprint_helpers::parseFracSigOption(const StringSegment& segment, MacroProps& macros,
1306
308
                                           UErrorCode& status) {
1307
308
    if (segment.charAt(0) != u'@') {
1308
70
        return false;
1309
70
    }
1310
238
    int offset = 0;
1311
238
    int minSig = 0;
1312
238
    int maxSig;
1313
10.9k
    for (; offset < segment.length(); offset++) {
1314
10.8k
        if (segment.charAt(offset) == u'@') {
1315
10.6k
            minSig++;
1316
10.6k
        } else {
1317
198
            break;
1318
198
        }
1319
10.8k
    }
1320
238
    if (offset < segment.length()) {
1321
198
        if (isWildcardChar(segment.charAt(offset))) {
1322
            // @+, @@+, @@@+
1323
24
            maxSig = -1;
1324
24
            offset++;
1325
174
        } else {
1326
            // @#, @##, @###
1327
            // @@#, @@##, @@@#
1328
174
            maxSig = minSig;
1329
1.71M
            for (; offset < segment.length(); offset++) {
1330
1.71M
                if (segment.charAt(offset) == u'#') {
1331
1.71M
                    maxSig++;
1332
1.71M
                } else {
1333
146
                    break;
1334
146
                }
1335
1.71M
            }
1336
174
        }
1337
198
    } else {
1338
        // @, @@, @@@
1339
40
        maxSig = minSig;
1340
40
    }
1341
238
    const auto& oldPrecision = static_cast<const FractionPrecision&>(macros.precision);
1342
238
    if (offset < segment.length()) {
1343
147
        UNumberRoundingPriority priority;
1344
147
        if (maxSig == -1) {
1345
            // The wildcard character is not allowed with the priority annotation
1346
1
            status = U_NUMBER_SKELETON_SYNTAX_ERROR;
1347
1
            return false;
1348
1
        }
1349
146
        if (segment.codePointAt(offset) == u'r') {
1350
29
            priority = UNUM_ROUNDING_PRIORITY_RELAXED;
1351
29
            offset++;
1352
117
        } else if (segment.codePointAt(offset) == u's') {
1353
61
            priority = UNUM_ROUNDING_PRIORITY_STRICT;
1354
61
            offset++;
1355
61
        } else {
1356
            // Invalid digits option for fraction rounder
1357
56
            status = U_NUMBER_SKELETON_SYNTAX_ERROR;
1358
56
            return false;
1359
56
        }
1360
90
        if (offset < segment.length()) {
1361
            // Invalid digits option for fraction rounder
1362
1
            status = U_NUMBER_SKELETON_SYNTAX_ERROR;
1363
1
            return false;
1364
1
        }
1365
89
        macros.precision = oldPrecision.withSignificantDigits(minSig, maxSig, priority);
1366
91
    } else if (maxSig == -1) {
1367
        // withMinDigits
1368
23
        macros.precision = oldPrecision.withMinDigits(minSig);
1369
68
    } else if (minSig == 1) {
1370
        // withMaxDigits
1371
64
        macros.precision = oldPrecision.withMaxDigits(maxSig);
1372
64
    } else {
1373
        // Digits options with both min and max sig require the priority option
1374
4
        status = U_NUMBER_SKELETON_SYNTAX_ERROR;
1375
4
        return false;
1376
4
    }
1377
1378
176
    return true;
1379
238
}
1380
1381
89
bool blueprint_helpers::parseTrailingZeroOption(const StringSegment& segment, MacroProps& macros, UErrorCode&) {
1382
89
    if (segment == u"w") {
1383
30
        macros.precision = macros.precision.trailingZeroDisplay(UNUM_TRAILING_ZERO_HIDE_IF_WHOLE);
1384
30
        return true;
1385
30
    }
1386
59
    return false;
1387
89
}
1388
1389
void blueprint_helpers::parseIncrementOption(const StringSegment &segment, MacroProps &macros,
1390
0
                                             UErrorCode &status) {
1391
0
    number::impl::parseIncrementOption(segment, macros.precision, status);
1392
0
}
1393
1394
void blueprint_helpers::generateIncrementOption(
1395
        uint32_t increment,
1396
        digits_t incrementMagnitude,
1397
        int32_t minFrac,
1398
        UnicodeString& sb,
1399
0
        UErrorCode&) {
1400
    // Utilize DecimalQuantity/double_conversion to format this for us.
1401
0
    DecimalQuantity dq;
1402
0
    dq.setToLong(increment);
1403
0
    dq.adjustMagnitude(incrementMagnitude);
1404
0
    dq.setMinFraction(minFrac);
1405
0
    sb.append(dq.toPlainString());
1406
0
}
1407
1408
void blueprint_helpers::parseIntegerWidthOption(const StringSegment& segment, MacroProps& macros,
1409
0
                                                UErrorCode& status) {
1410
0
    int32_t offset = 0;
1411
0
    int32_t minInt = 0;
1412
0
    int32_t maxInt;
1413
0
    if (isWildcardChar(segment.charAt(0))) {
1414
0
        maxInt = -1;
1415
0
        offset++;
1416
0
    } else {
1417
0
        maxInt = 0;
1418
0
    }
1419
0
    for (; offset < segment.length(); offset++) {
1420
0
        if (maxInt != -1 && segment.charAt(offset) == u'#') {
1421
0
            maxInt++;
1422
0
        } else {
1423
0
            break;
1424
0
        }
1425
0
    }
1426
0
    if (offset < segment.length()) {
1427
0
        for (; offset < segment.length(); offset++) {
1428
0
            if (segment.charAt(offset) == u'0') {
1429
0
                minInt++;
1430
0
            } else {
1431
0
                break;
1432
0
            }
1433
0
        }
1434
0
    }
1435
0
    if (maxInt != -1) {
1436
0
        maxInt += minInt;
1437
0
    }
1438
0
    if (offset < segment.length()) {
1439
        // throw new SkeletonSyntaxException("Invalid integer width stem", segment);
1440
0
        status = U_NUMBER_SKELETON_SYNTAX_ERROR;
1441
0
        return;
1442
0
    }
1443
    // Use the public APIs to enforce bounds checking
1444
0
    if (maxInt == -1) {
1445
0
        macros.integerWidth = IntegerWidth::zeroFillTo(minInt);
1446
0
    } else {
1447
0
        macros.integerWidth = IntegerWidth::zeroFillTo(minInt).truncateAt(maxInt);
1448
0
    }
1449
0
}
1450
1451
void blueprint_helpers::generateIntegerWidthOption(int32_t minInt, int32_t maxInt, UnicodeString& sb,
1452
0
                                                   UErrorCode&) {
1453
0
    if (maxInt == -1) {
1454
0
        sb.append(kWildcardChar);
1455
0
    } else {
1456
0
        appendMultiple(sb, u'#', maxInt - minInt);
1457
0
    }
1458
0
    appendMultiple(sb, u'0', minInt);
1459
0
}
1460
1461
void blueprint_helpers::parseNumberingSystemOption(const StringSegment& segment, MacroProps& macros,
1462
0
                                                   UErrorCode& status) {
1463
    // Need to do char <-> char16_t conversion...
1464
0
    U_ASSERT(U_SUCCESS(status));
1465
0
    CharString buffer;
1466
0
    SKELETON_UCHAR_TO_CHAR(buffer, segment.toTempUnicodeString(), 0, segment.length(), status);
1467
1468
0
    NumberingSystem* ns = NumberingSystem::createInstanceByName(buffer.data(), status);
1469
0
    if (ns == nullptr || U_FAILURE(status)) {
1470
        // This is a skeleton syntax error; don't bubble up the low-level NumberingSystem error
1471
        // throw new SkeletonSyntaxException("Unknown numbering system", segment);
1472
0
        status = U_NUMBER_SKELETON_SYNTAX_ERROR;
1473
0
        return;
1474
0
    }
1475
0
    macros.symbols.setTo(ns);
1476
0
}
1477
1478
void blueprint_helpers::generateNumberingSystemOption(const NumberingSystem& ns, UnicodeString& sb,
1479
0
                                                      UErrorCode&) {
1480
    // Need to do char <-> char16_t conversion...
1481
0
    sb.append(UnicodeString(ns.getName(), -1, US_INV));
1482
0
}
1483
1484
void blueprint_helpers::parseScaleOption(const StringSegment& segment, MacroProps& macros,
1485
1.56k
                                              UErrorCode& status) {
1486
    // Need to do char <-> char16_t conversion...
1487
1.56k
    U_ASSERT(U_SUCCESS(status));
1488
1.56k
    CharString buffer;
1489
1.56k
    SKELETON_UCHAR_TO_CHAR(buffer, segment.toTempUnicodeString(), 0, segment.length(), status);
1490
1491
1.54k
    LocalPointer<DecNum> decnum(new DecNum(), status);
1492
1.54k
    if (U_FAILURE(status)) { return; }
1493
1.54k
    decnum->setTo({buffer.data(), buffer.length()}, status);
1494
1.54k
    if (U_FAILURE(status) || decnum->isSpecial()) {
1495
        // This is a skeleton syntax error; don't let the low-level decnum error bubble up
1496
243
        status = U_NUMBER_SKELETON_SYNTAX_ERROR;
1497
243
        return;
1498
243
    }
1499
1500
    // NOTE: The constructor will optimize the decnum for us if possible.
1501
1.29k
    macros.scale = {0, decnum.orphan()};
1502
1.29k
}
1503
1504
void blueprint_helpers::generateScaleOption(int32_t magnitude, const DecNum* arbitrary, UnicodeString& sb,
1505
0
                                            UErrorCode& status) {
1506
    // Utilize DecimalQuantity/double_conversion to format this for us.
1507
0
    DecimalQuantity dq;
1508
0
    if (arbitrary != nullptr) {
1509
0
        dq.setToDecNum(*arbitrary, status);
1510
0
        if (U_FAILURE(status)) { return; }
1511
0
    } else {
1512
0
        dq.setToInt(1);
1513
0
    }
1514
0
    dq.adjustMagnitude(magnitude);
1515
0
    dq.roundToInfinity();
1516
0
    sb.append(dq.toPlainString());
1517
0
}
1518
1519
1520
0
bool GeneratorHelpers::notation(const MacroProps& macros, UnicodeString& sb, UErrorCode& status) {
1521
0
    if (macros.notation.fType == Notation::NTN_COMPACT) {
1522
0
        UNumberCompactStyle style = macros.notation.fUnion.compactStyle;
1523
0
        if (style == UNumberCompactStyle::UNUM_LONG) {
1524
0
            sb.append(u"compact-long", -1);
1525
0
            return true;
1526
0
        } else if (style == UNumberCompactStyle::UNUM_SHORT) {
1527
0
            sb.append(u"compact-short", -1);
1528
0
            return true;
1529
0
        } else {
1530
            // Compact notation generated from custom data (not supported in skeleton)
1531
            // The other compact notations are literals
1532
0
            status = U_UNSUPPORTED_ERROR;
1533
0
            return false;
1534
0
        }
1535
0
    } else if (macros.notation.fType == Notation::NTN_SCIENTIFIC) {
1536
0
        const Notation::ScientificSettings& impl = macros.notation.fUnion.scientific;
1537
0
        if (impl.fEngineeringInterval == 3) {
1538
0
            sb.append(u"engineering", -1);
1539
0
        } else {
1540
0
            sb.append(u"scientific", -1);
1541
0
        }
1542
0
        if (impl.fMinExponentDigits > 1) {
1543
0
            sb.append(u'/');
1544
0
            blueprint_helpers::generateExponentWidthOption(impl.fMinExponentDigits, sb, status);
1545
0
            if (U_FAILURE(status)) {
1546
0
                return false;
1547
0
            }
1548
0
        }
1549
0
        if (impl.fExponentSignDisplay != UNUM_SIGN_AUTO) {
1550
0
            sb.append(u'/');
1551
0
            enum_to_stem_string::signDisplay(impl.fExponentSignDisplay, sb);
1552
0
        }
1553
0
        return true;
1554
0
    } else {
1555
        // Default value is not shown in normalized form
1556
0
        return false;
1557
0
    }
1558
0
}
1559
1560
0
bool GeneratorHelpers::unit(const MacroProps& macros, UnicodeString& sb, UErrorCode& status) {
1561
0
    MeasureUnit unit = macros.unit;
1562
0
    if (!utils::unitIsBaseUnit(macros.perUnit)) {
1563
0
        if (utils::unitIsCurrency(macros.unit) || utils::unitIsCurrency(macros.perUnit)) {
1564
0
            status = U_UNSUPPORTED_ERROR;
1565
0
            return false;
1566
0
        }
1567
0
        unit = unit.product(macros.perUnit.reciprocal(status), status);
1568
0
    }
1569
1570
0
    if (utils::unitIsCurrency(unit)) {
1571
0
        sb.append(u"currency/", -1);
1572
0
        CurrencyUnit currency(unit, status);
1573
0
        if (U_FAILURE(status)) {
1574
0
            return false;
1575
0
        }
1576
0
        blueprint_helpers::generateCurrencyOption(currency, sb, status);
1577
0
        return true;
1578
0
    } else if (utils::unitIsBaseUnit(unit)) {
1579
        // Default value is not shown in normalized form
1580
0
        return false;
1581
0
    } else if (utils::unitIsPercent(unit)) {
1582
0
        sb.append(u"percent", -1);
1583
0
        return true;
1584
0
    } else if (utils::unitIsPermille(unit)) {
1585
0
        sb.append(u"permille", -1);
1586
0
        return true;
1587
0
    } else {
1588
0
        sb.append(u"unit/", -1);
1589
0
        sb.append(unit.getIdentifier());
1590
0
        return true;
1591
0
    }
1592
0
}
1593
1594
0
bool GeneratorHelpers::usage(const MacroProps& macros, UnicodeString& sb, UErrorCode& /* status */) {
1595
0
    if (macros.usage.isSet()) {
1596
0
        sb.append(u"usage/", -1);
1597
0
        sb.append(UnicodeString(macros.usage.fValue, -1, US_INV));
1598
0
        return true;
1599
0
    }
1600
0
    return false;
1601
0
}
1602
1603
0
bool GeneratorHelpers::precision(const MacroProps& macros, UnicodeString& sb, UErrorCode& status) {
1604
0
    if (macros.precision.fType == Precision::RND_NONE) {
1605
0
        sb.append(u"precision-unlimited", -1);
1606
0
    } else if (macros.precision.fType == Precision::RND_FRACTION) {
1607
0
        const Precision::FractionSignificantSettings& impl = macros.precision.fUnion.fracSig;
1608
0
        blueprint_helpers::generateFractionStem(impl.fMinFrac, impl.fMaxFrac, sb, status);
1609
0
    } else if (macros.precision.fType == Precision::RND_SIGNIFICANT) {
1610
0
        const Precision::FractionSignificantSettings& impl = macros.precision.fUnion.fracSig;
1611
0
        blueprint_helpers::generateDigitsStem(impl.fMinSig, impl.fMaxSig, sb, status);
1612
0
    } else if (macros.precision.fType == Precision::RND_FRACTION_SIGNIFICANT) {
1613
0
        const Precision::FractionSignificantSettings& impl = macros.precision.fUnion.fracSig;
1614
0
        blueprint_helpers::generateFractionStem(impl.fMinFrac, impl.fMaxFrac, sb, status);
1615
0
        sb.append(u'/');
1616
0
        if (impl.fRetain) {
1617
0
            if (impl.fPriority == UNUM_ROUNDING_PRIORITY_RELAXED) {
1618
                // withMinDigits
1619
0
                blueprint_helpers::generateDigitsStem(impl.fMaxSig, -1, sb, status);
1620
0
            } else {
1621
                // withMaxDigits
1622
0
                blueprint_helpers::generateDigitsStem(1, impl.fMaxSig, sb, status);
1623
0
            }
1624
0
        } else {
1625
0
            blueprint_helpers::generateDigitsStem(impl.fMinSig, impl.fMaxSig, sb, status);
1626
0
            if (impl.fPriority == UNUM_ROUNDING_PRIORITY_RELAXED) {
1627
0
                sb.append(u'r');
1628
0
            } else {
1629
0
                sb.append(u's');
1630
0
            }
1631
0
        }
1632
0
    } else if (macros.precision.fType == Precision::RND_INCREMENT
1633
0
            || macros.precision.fType == Precision::RND_INCREMENT_ONE
1634
0
            || macros.precision.fType == Precision::RND_INCREMENT_FIVE) {
1635
0
        const Precision::IncrementSettings& impl = macros.precision.fUnion.increment;
1636
0
        sb.append(u"precision-increment/", -1);
1637
0
        blueprint_helpers::generateIncrementOption(
1638
0
                impl.fIncrement,
1639
0
                impl.fIncrementMagnitude,
1640
0
                impl.fMinFrac,
1641
0
                sb,
1642
0
                status);
1643
0
    } else if (macros.precision.fType == Precision::RND_CURRENCY) {
1644
0
        UCurrencyUsage usage = macros.precision.fUnion.currencyUsage;
1645
0
        if (usage == UCURR_USAGE_STANDARD) {
1646
0
            sb.append(u"precision-currency-standard", -1);
1647
0
        } else {
1648
0
            sb.append(u"precision-currency-cash", -1);
1649
0
        }
1650
0
    } else {
1651
        // Bogus or Error
1652
0
        return false;
1653
0
    }
1654
1655
0
    if (macros.precision.fTrailingZeroDisplay == UNUM_TRAILING_ZERO_HIDE_IF_WHOLE) {
1656
0
        sb.append(u"/w", -1);
1657
0
    }
1658
1659
    // NOTE: Always return true for rounding because the default value depends on other options.
1660
0
    return true;
1661
0
}
1662
1663
0
bool GeneratorHelpers::roundingMode(const MacroProps& macros, UnicodeString& sb, UErrorCode&) {
1664
0
    if (macros.roundingMode == kDefaultMode) {
1665
0
        return false; // Default
1666
0
    }
1667
0
    enum_to_stem_string::roundingMode(macros.roundingMode, sb);
1668
0
    return true;
1669
0
}
1670
1671
0
bool GeneratorHelpers::grouping(const MacroProps& macros, UnicodeString& sb, UErrorCode& status) {
1672
0
    if (macros.grouper.isBogus()) {
1673
0
        return false; // No value
1674
0
    } else if (macros.grouper.fStrategy == UNUM_GROUPING_COUNT) {
1675
0
        status = U_UNSUPPORTED_ERROR;
1676
0
        return false;
1677
0
    } else if (macros.grouper.fStrategy == UNUM_GROUPING_AUTO) {
1678
0
        return false; // Default value
1679
0
    } else {
1680
0
        enum_to_stem_string::groupingStrategy(macros.grouper.fStrategy, sb);
1681
0
        return true;
1682
0
    }
1683
0
}
1684
1685
0
bool GeneratorHelpers::integerWidth(const MacroProps& macros, UnicodeString& sb, UErrorCode& status) {
1686
0
    if (macros.integerWidth.fHasError || macros.integerWidth.isBogus() ||
1687
0
        macros.integerWidth == IntegerWidth::standard()) {
1688
        // Error or Default
1689
0
        return false;
1690
0
    }
1691
0
    const auto& minMaxInt = macros.integerWidth.fUnion.minMaxInt;
1692
0
    if (minMaxInt.fMinInt == 0 && minMaxInt.fMaxInt == 0) {
1693
0
        sb.append(u"integer-width-trunc", -1);
1694
0
        return true;
1695
0
    }
1696
0
    sb.append(u"integer-width/", -1);
1697
0
    blueprint_helpers::generateIntegerWidthOption(
1698
0
            minMaxInt.fMinInt,
1699
0
            minMaxInt.fMaxInt,
1700
0
            sb,
1701
0
            status);
1702
0
    return true;
1703
0
}
1704
1705
0
bool GeneratorHelpers::symbols(const MacroProps& macros, UnicodeString& sb, UErrorCode& status) {
1706
0
    if (macros.symbols.isNumberingSystem()) {
1707
0
        const NumberingSystem& ns = *macros.symbols.getNumberingSystem();
1708
0
        if (uprv_strcmp(ns.getName(), "latn") == 0) {
1709
0
            sb.append(u"latin", -1);
1710
0
        } else {
1711
0
            sb.append(u"numbering-system/", -1);
1712
0
            blueprint_helpers::generateNumberingSystemOption(ns, sb, status);
1713
0
        }
1714
0
        return true;
1715
0
    } else if (macros.symbols.isDecimalFormatSymbols()) {
1716
0
        status = U_UNSUPPORTED_ERROR;
1717
0
        return false;
1718
0
    } else {
1719
        // No custom symbols
1720
0
        return false;
1721
0
    }
1722
0
}
1723
1724
0
bool GeneratorHelpers::unitWidth(const MacroProps& macros, UnicodeString& sb, UErrorCode&) {
1725
0
    if (macros.unitWidth == UNUM_UNIT_WIDTH_SHORT || macros.unitWidth == UNUM_UNIT_WIDTH_COUNT) {
1726
0
        return false; // Default or Bogus
1727
0
    }
1728
0
    enum_to_stem_string::unitWidth(macros.unitWidth, sb);
1729
0
    return true;
1730
0
}
1731
1732
0
bool GeneratorHelpers::sign(const MacroProps& macros, UnicodeString& sb, UErrorCode&) {
1733
0
    if (macros.sign == UNUM_SIGN_AUTO || macros.sign == UNUM_SIGN_COUNT) {
1734
0
        return false; // Default or Bogus
1735
0
    }
1736
0
    enum_to_stem_string::signDisplay(macros.sign, sb);
1737
0
    return true;
1738
0
}
1739
1740
0
bool GeneratorHelpers::decimal(const MacroProps& macros, UnicodeString& sb, UErrorCode&) {
1741
0
    if (macros.decimal == UNUM_DECIMAL_SEPARATOR_AUTO || macros.decimal == UNUM_DECIMAL_SEPARATOR_COUNT) {
1742
0
        return false; // Default or Bogus
1743
0
    }
1744
0
    enum_to_stem_string::decimalSeparatorDisplay(macros.decimal, sb);
1745
0
    return true;
1746
0
}
1747
1748
0
bool GeneratorHelpers::scale(const MacroProps& macros, UnicodeString& sb, UErrorCode& status) {
1749
0
    if (!macros.scale.isValid()) {
1750
0
        return false; // Default or Bogus
1751
0
    }
1752
0
    sb.append(u"scale/", -1);
1753
0
    blueprint_helpers::generateScaleOption(
1754
0
            macros.scale.fMagnitude,
1755
0
            macros.scale.fArbitrary,
1756
0
            sb,
1757
0
            status);
1758
0
    return true;
1759
0
}
1760
1761
1762
// Definitions of public API methods (put here for dependency disentanglement)
1763
1764
#if (U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN) && defined(_MSC_VER)
1765
// Ignore MSVC warning 4661. This is generated for NumberFormatterSettings<>::toSkeleton() as this method
1766
// is defined elsewhere (in number_skeletons.cpp). The compiler is warning that the explicit template instantiation
1767
// inside this single translation unit (CPP file) is incomplete, and thus it isn't sure if the template class is
1768
// fully defined. However, since each translation unit explicitly instantiates all the necessary template classes,
1769
// they will all be passed to the linker, and the linker will still find and export all the class members.
1770
#pragma warning(push)
1771
#pragma warning(disable: 4661)
1772
#endif
1773
1774
template<typename Derived>
1775
0
UnicodeString NumberFormatterSettings<Derived>::toSkeleton(UErrorCode& status) const {
1776
0
    if (U_FAILURE(status)) {
1777
0
        return ICU_Utility::makeBogusString();
1778
0
    }
1779
0
    if (fMacros.copyErrorTo(status)) {
1780
0
        return ICU_Utility::makeBogusString();
1781
0
    }
1782
0
    return skeleton::generate(fMacros, status);
1783
0
}
Unexecuted instantiation: icu_79::number::NumberFormatterSettings<icu_79::number::UnlocalizedNumberFormatter>::toSkeleton(UErrorCode&) const
Unexecuted instantiation: icu_79::number::NumberFormatterSettings<icu_79::number::LocalizedNumberFormatter>::toSkeleton(UErrorCode&) const
1784
1785
// Declare all classes that implement NumberFormatterSettings
1786
// See https://stackoverflow.com/a/495056/1407170
1787
template
1788
class icu::number::NumberFormatterSettings<icu::number::UnlocalizedNumberFormatter>;
1789
template
1790
class icu::number::NumberFormatterSettings<icu::number::LocalizedNumberFormatter>;
1791
1792
UnlocalizedNumberFormatter
1793
4.65k
NumberFormatter::forSkeleton(const UnicodeString& skeleton, UErrorCode& status) {
1794
4.65k
    return skeleton::create(skeleton, nullptr, status);
1795
4.65k
}
1796
1797
UnlocalizedNumberFormatter
1798
0
NumberFormatter::forSkeleton(const UnicodeString& skeleton, UParseError& perror, UErrorCode& status) {
1799
0
    return skeleton::create(skeleton, &perror, status);
1800
0
}
1801
1802
#if (U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN) && defined(_MSC_VER)
1803
// Warning 4661.
1804
#pragma warning(pop)
1805
#endif
1806
1807
#endif /* #if !UCONFIG_NO_FORMATTING */