Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/unicode/dcfmtsym.h
Line
Count
Source (jump to first uncovered line)
1
// © 2016 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
/*
4
********************************************************************************
5
*   Copyright (C) 1997-2016, International Business Machines
6
*   Corporation and others.  All Rights Reserved.
7
********************************************************************************
8
*
9
* File DCFMTSYM.H
10
*
11
* Modification History:
12
*
13
*   Date        Name        Description
14
*   02/19/97    aliu        Converted from java.
15
*   03/18/97    clhuang     Updated per C++ implementation.
16
*   03/27/97    helena      Updated to pass the simple test after code review.
17
*   08/26/97    aliu        Added currency/intl currency symbol support.
18
*   07/22/98    stephen     Changed to match C++ style
19
*                            currencySymbol -> fCurrencySymbol
20
*                            Constants changed from CAPS to kCaps
21
*   06/24/99    helena      Integrated Alan's NF enhancements and Java2 bug fixes
22
*   09/22/00    grhoten     Marked deprecation tags with a pointer to replacement
23
*                            functions.
24
********************************************************************************
25
*/
26
27
#ifndef DCFMTSYM_H
28
#define DCFMTSYM_H
29
30
#include "unicode/utypes.h"
31
#include "unicode/uchar.h"
32
33
#if !UCONFIG_NO_FORMATTING
34
35
#include "unicode/uobject.h"
36
#include "unicode/locid.h"
37
#include "unicode/numsys.h"
38
#include "unicode/unum.h"
39
#include "unicode/unistr.h"
40
41
/**
42
 * \file
43
 * \brief C++ API: Symbols for formatting numbers.
44
 */
45
46
47
U_NAMESPACE_BEGIN
48
49
/**
50
 * This class represents the set of symbols needed by DecimalFormat
51
 * to format numbers. DecimalFormat creates for itself an instance of
52
 * DecimalFormatSymbols from its locale data.  If you need to change any
53
 * of these symbols, you can get the DecimalFormatSymbols object from
54
 * your DecimalFormat and modify it.
55
 * <P>
56
 * Here are the special characters used in the parts of the
57
 * subpattern, with notes on their usage.
58
 * <pre>
59
 * \code
60
 *        Symbol   Meaning
61
 *          0      a digit
62
 *          #      a digit, zero shows as absent
63
 *          .      placeholder for decimal separator
64
 *          ,      placeholder for grouping separator.
65
 *          ;      separates formats.
66
 *          -      default negative prefix.
67
 *          %      divide by 100 and show as percentage
68
 *          X      any other characters can be used in the prefix or suffix
69
 *          '      used to quote special characters in a prefix or suffix.
70
 * \endcode
71
 *  </pre>
72
 * [Notes]
73
 * <P>
74
 * If there is no explicit negative subpattern, - is prefixed to the
75
 * positive form. That is, "0.00" alone is equivalent to "0.00;-0.00".
76
 * <P>
77
 * The grouping separator is commonly used for thousands, but in some
78
 * countries for ten-thousands. The interval is a constant number of
79
 * digits between the grouping characters, such as 100,000,000 or 1,0000,0000.
80
 * If you supply a pattern with multiple grouping characters, the interval
81
 * between the last one and the end of the integer is the one that is
82
 * used. So "#,##,###,####" == "######,####" == "##,####,####".
83
 */
84
class U_I18N_API DecimalFormatSymbols : public UObject {
85
public:
86
    /**
87
     * Constants for specifying a number format symbol.
88
     * @stable ICU 2.0
89
     */
90
    enum ENumberFormatSymbol {
91
        /** The decimal separator */
92
        kDecimalSeparatorSymbol,
93
        /** The grouping separator */
94
        kGroupingSeparatorSymbol,
95
        /** The pattern separator */
96
        kPatternSeparatorSymbol,
97
        /** The percent sign */
98
        kPercentSymbol,
99
        /** Zero*/
100
        kZeroDigitSymbol,
101
        /** Character representing a digit in the pattern */
102
        kDigitSymbol,
103
        /** The minus sign */
104
        kMinusSignSymbol,
105
        /** The plus sign */
106
        kPlusSignSymbol,
107
        /** The currency symbol */
108
        kCurrencySymbol,
109
        /** The international currency symbol */
110
        kIntlCurrencySymbol,
111
        /** The monetary separator */
112
        kMonetarySeparatorSymbol,
113
        /** The exponential symbol */
114
        kExponentialSymbol,
115
        /** Per mill symbol - replaces kPermillSymbol */
116
        kPerMillSymbol,
117
        /** Escape padding character */
118
        kPadEscapeSymbol,
119
        /** Infinity symbol */
120
        kInfinitySymbol,
121
        /** Nan symbol */
122
        kNaNSymbol,
123
        /** Significant digit symbol
124
         * @stable ICU 3.0 */
125
        kSignificantDigitSymbol,
126
        /** The monetary grouping separator
127
         * @stable ICU 3.6
128
         */
129
        kMonetaryGroupingSeparatorSymbol,
130
        /** One
131
         * @stable ICU 4.6
132
         */
133
        kOneDigitSymbol,
134
        /** Two
135
         * @stable ICU 4.6
136
         */
137
        kTwoDigitSymbol,
138
        /** Three
139
         * @stable ICU 4.6
140
         */
141
        kThreeDigitSymbol,
142
        /** Four
143
         * @stable ICU 4.6
144
         */
145
        kFourDigitSymbol,
146
        /** Five
147
         * @stable ICU 4.6
148
         */
149
        kFiveDigitSymbol,
150
        /** Six
151
         * @stable ICU 4.6
152
         */
153
        kSixDigitSymbol,
154
        /** Seven
155
         * @stable ICU 4.6
156
         */
157
        kSevenDigitSymbol,
158
        /** Eight
159
         * @stable ICU 4.6
160
         */
161
        kEightDigitSymbol,
162
        /** Nine
163
         * @stable ICU 4.6
164
         */
165
        kNineDigitSymbol,
166
        /** Multiplication sign.
167
         * @stable ICU 54
168
         */
169
        kExponentMultiplicationSymbol,
170
        /** count symbol constants */
171
        kFormatSymbolCount = kNineDigitSymbol + 2
172
    };
173
174
    /**
175
     * Create a DecimalFormatSymbols object for the given locale.
176
     *
177
     * @param locale    The locale to get symbols for.
178
     * @param status    Input/output parameter, set to success or
179
     *                  failure code upon return.
180
     * @stable ICU 2.0
181
     */
182
    DecimalFormatSymbols(const Locale& locale, UErrorCode& status);
183
184
#ifndef U_HIDE_DRAFT_API
185
    /**
186
     * Creates a DecimalFormatSymbols instance for the given locale with digits and symbols
187
     * corresponding to the given NumberingSystem.
188
     *
189
     * This constructor behaves equivalently to the normal constructor called with a locale having a
190
     * "numbers=xxxx" keyword specifying the numbering system by name.
191
     *
192
     * In this constructor, the NumberingSystem argument will be used even if the locale has its own
193
     * "numbers=xxxx" keyword.
194
     *
195
     * @param locale    The locale to get symbols for.
196
     * @param ns        The numbering system.
197
     * @param status    Input/output parameter, set to success or
198
     *                  failure code upon return.
199
     * @draft ICU 60
200
     */
201
    DecimalFormatSymbols(const Locale& locale, const NumberingSystem& ns, UErrorCode& status);
202
#endif  /* U_HIDE_DRAFT_API */
203
204
    /**
205
     * Create a DecimalFormatSymbols object for the default locale.
206
     * This constructor will not fail.  If the resource file data is
207
     * not available, it will use hard-coded last-resort data and
208
     * set status to U_USING_FALLBACK_ERROR.
209
     *
210
     * @param status    Input/output parameter, set to success or
211
     *                  failure code upon return.
212
     * @stable ICU 2.0
213
     */
214
    DecimalFormatSymbols(UErrorCode& status);
215
216
    /**
217
     * Creates a DecimalFormatSymbols object with last-resort data.
218
     * Intended for callers who cache the symbols data and
219
     * set all symbols on the resulting object.
220
     *
221
     * The last-resort symbols are similar to those for the root data,
222
     * except that the grouping separators are empty,
223
     * the NaN symbol is U+FFFD rather than "NaN",
224
     * and the CurrencySpacing patterns are empty.
225
     *
226
     * @param status    Input/output parameter, set to success or
227
     *                  failure code upon return.
228
     * @return last-resort symbols
229
     * @stable ICU 52
230
     */
231
    static DecimalFormatSymbols* createWithLastResortData(UErrorCode& status);
232
233
    /**
234
     * Copy constructor.
235
     * @stable ICU 2.0
236
     */
237
    DecimalFormatSymbols(const DecimalFormatSymbols&);
238
239
    /**
240
     * Assignment operator.
241
     * @stable ICU 2.0
242
     */
243
    DecimalFormatSymbols& operator=(const DecimalFormatSymbols&);
244
245
    /**
246
     * Destructor.
247
     * @stable ICU 2.0
248
     */
249
    virtual ~DecimalFormatSymbols();
250
251
    /**
252
     * Return true if another object is semantically equal to this one.
253
     *
254
     * @param other    the object to be compared with.
255
     * @return         true if another object is semantically equal to this one.
256
     * @stable ICU 2.0
257
     */
258
    UBool operator==(const DecimalFormatSymbols& other) const;
259
260
    /**
261
     * Return true if another object is semantically unequal to this one.
262
     *
263
     * @param other    the object to be compared with.
264
     * @return         true if another object is semantically unequal to this one.
265
     * @stable ICU 2.0
266
     */
267
0
    UBool operator!=(const DecimalFormatSymbols& other) const { return !operator==(other); }
268
269
    /**
270
     * Get one of the format symbols by its enum constant.
271
     * Each symbol is stored as a string so that graphemes
272
     * (characters with modifier letters) can be used.
273
     *
274
     * @param symbol    Constant to indicate a number format symbol.
275
     * @return    the format symbols by the param 'symbol'
276
     * @stable ICU 2.0
277
     */
278
    inline UnicodeString getSymbol(ENumberFormatSymbol symbol) const;
279
280
    /**
281
     * Set one of the format symbols by its enum constant.
282
     * Each symbol is stored as a string so that graphemes
283
     * (characters with modifier letters) can be used.
284
     *
285
     * @param symbol    Constant to indicate a number format symbol.
286
     * @param value     value of the format symbol
287
     * @param propogateDigits If false, setting the zero digit will not automatically set 1-9.
288
     *     The default behavior is to automatically set 1-9 if zero is being set and the value
289
     *     it is being set to corresponds to a known Unicode zero digit.
290
     * @stable ICU 2.0
291
     */
292
    void setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits);
293
294
    /**
295
     * Returns the locale for which this object was constructed.
296
     * @stable ICU 2.6
297
     */
298
    inline Locale getLocale() const;
299
300
    /**
301
     * Returns the locale for this object. Two flavors are available:
302
     * valid and actual locale.
303
     * @stable ICU 2.8
304
     */
305
    Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
306
307
    /**
308
      * Get pattern string for 'CurrencySpacing' that can be applied to
309
      * currency format.
310
      * This API gets the CurrencySpacing data from ResourceBundle. The pattern can
311
      * be empty if there is no data from current locale and its parent locales.
312
      *
313
      * @param type :  UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
314
      * @param beforeCurrency : true if the pattern is for before currency symbol.
315
      *                         false if the pattern is for after currency symbol.
316
      * @param status: Input/output parameter, set to success or
317
      *                  failure code upon return.
318
      * @return pattern string for currencyMatch, surroundingMatch or spaceInsert.
319
      *     Return empty string if there is no data for this locale and its parent
320
      *     locales.
321
      * @stable ICU 4.8
322
      */
323
     const UnicodeString& getPatternForCurrencySpacing(UCurrencySpacing type,
324
                                                 UBool beforeCurrency,
325
                                                 UErrorCode& status) const;
326
     /**
327
       * Set pattern string for 'CurrencySpacing' that can be applied to
328
       * currency format.
329
       *
330
       * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
331
       * @param beforeCurrency : true if the pattern is for before currency symbol.
332
       *                         false if the pattern is for after currency symbol.
333
       * @param pattern : pattern string to override current setting.
334
       * @stable ICU 4.8
335
       */
336
     void setPatternForCurrencySpacing(UCurrencySpacing type,
337
                                       UBool beforeCurrency,
338
                                       const UnicodeString& pattern);
339
340
    /**
341
     * ICU "poor man's RTTI", returns a UClassID for the actual class.
342
     *
343
     * @stable ICU 2.2
344
     */
345
    virtual UClassID getDynamicClassID() const;
346
347
    /**
348
     * ICU "poor man's RTTI", returns a UClassID for this class.
349
     *
350
     * @stable ICU 2.2
351
     */
352
    static UClassID U_EXPORT2 getStaticClassID();
353
354
private:
355
    DecimalFormatSymbols();
356
357
    /**
358
     * Initializes the symbols from the LocaleElements resource bundle.
359
     * Note: The organization of LocaleElements badly needs to be
360
     * cleaned up.
361
     *
362
     * @param locale               The locale to get symbols for.
363
     * @param success              Input/output parameter, set to success or
364
     *                             failure code upon return.
365
     * @param useLastResortData    determine if use last resort data
366
     * @param ns                   The NumberingSystem to use; otherwise, fall
367
     *                             back to the locale.
368
     */
369
    void initialize(const Locale& locale, UErrorCode& success,
370
        UBool useLastResortData = FALSE, const NumberingSystem* ns = nullptr);
371
372
    /**
373
     * Initialize the symbols with default values.
374
     */
375
    void initialize();
376
377
    void setCurrencyForSymbols();
378
379
public:
380
381
#ifndef U_HIDE_INTERNAL_API
382
    /**
383
     * @internal For ICU use only
384
     */
385
0
    inline UBool isCustomCurrencySymbol() const {
386
0
        return fIsCustomCurrencySymbol;
387
0
    }
388
389
    /**
390
     * @internal For ICU use only
391
     */
392
0
    inline UBool isCustomIntlCurrencySymbol() const {
393
0
        return fIsCustomIntlCurrencySymbol;
394
0
    }
395
396
    /**
397
     * @internal For ICU use only
398
     */
399
0
    inline UChar32 getCodePointZero() const {
400
0
        return fCodePointZero;
401
0
    }
402
#endif  /* U_HIDE_INTERNAL_API */
403
404
    /**
405
     * _Internal_ function - more efficient version of getSymbol,
406
     * returning a const reference to one of the symbol strings.
407
     * The returned reference becomes invalid when the symbol is changed
408
     * or when the DecimalFormatSymbols are destroyed.
409
     * Note: moved #ifndef U_HIDE_INTERNAL_API after this, since this is needed for inline in DecimalFormat
410
     *
411
     * This is not currently stable API, but if you think it should be stable,
412
     * post a comment on the following ticket and the ICU team will take a look:
413
     * http://bugs.icu-project.org/trac/ticket/13580
414
     *
415
     * @param symbol Constant to indicate a number format symbol.
416
     * @return the format symbol by the param 'symbol'
417
     * @internal
418
     */
419
    inline const UnicodeString& getConstSymbol(ENumberFormatSymbol symbol) const;
420
421
#ifndef U_HIDE_INTERNAL_API
422
    /**
423
     * Returns the const UnicodeString reference, like getConstSymbol,
424
     * corresponding to the digit with the given value.  This is equivalent
425
     * to accessing the symbol from getConstSymbol with the corresponding
426
     * key, such as kZeroDigitSymbol or kOneDigitSymbol.
427
     *
428
     * This is not currently stable API, but if you think it should be stable,
429
     * post a comment on the following ticket and the ICU team will take a look:
430
     * http://bugs.icu-project.org/trac/ticket/13580
431
     *
432
     * @param digit The digit, an integer between 0 and 9 inclusive.
433
     *              If outside the range 0 to 9, the zero digit is returned.
434
     * @return the format symbol for the given digit.
435
     * @internal This API is currently for ICU use only.
436
     */
437
    inline const UnicodeString& getConstDigitSymbol(int32_t digit) const;
438
439
    /**
440
     * Returns that pattern stored in currecy info. Internal API for use by NumberFormat API.
441
     * @internal
442
     */
443
    inline const char16_t* getCurrencyPattern(void) const;
444
#endif  /* U_HIDE_INTERNAL_API */
445
446
private:
447
    /**
448
     * Private symbol strings.
449
     * They are either loaded from a resource bundle or otherwise owned.
450
     * setSymbol() clones the symbol string.
451
     * Readonly aliases can only come from a resource bundle, so that we can always
452
     * use fastCopyFrom() with them.
453
     *
454
     * If DecimalFormatSymbols becomes subclassable and the status of fSymbols changes
455
     * from private to protected,
456
     * or when fSymbols can be set any other way that allows them to be readonly aliases
457
     * to non-resource bundle strings,
458
     * then regular UnicodeString copies must be used instead of fastCopyFrom().
459
     *
460
     * @internal
461
     */
462
    UnicodeString fSymbols[kFormatSymbolCount];
463
464
    /**
465
     * Non-symbol variable for getConstSymbol(). Always empty.
466
     * @internal
467
     */
468
    UnicodeString fNoSymbol;
469
470
    /**
471
     * Dealing with code points is faster than dealing with strings when formatting. Because of
472
     * this, we maintain a value containing the zero code point that is used whenever digitStrings
473
     * represents a sequence of ten code points in order.
474
     *
475
     * <p>If the value stored here is positive, it means that the code point stored in this value
476
     * corresponds to the digitStrings array, and codePointZero can be used instead of the
477
     * digitStrings array for the purposes of efficient formatting; if -1, then digitStrings does
478
     * *not* contain a sequence of code points, and it must be used directly.
479
     *
480
     * <p>It is assumed that codePointZero always shadows the value in digitStrings. codePointZero
481
     * should never be set directly; rather, it should be updated only when digitStrings mutates.
482
     * That is, the flow of information is digitStrings -> codePointZero, not the other way.
483
     */
484
    UChar32 fCodePointZero;
485
486
    Locale locale;
487
488
    char actualLocale[ULOC_FULLNAME_CAPACITY];
489
    char validLocale[ULOC_FULLNAME_CAPACITY];
490
    const char16_t* currPattern;
491
492
    UnicodeString currencySpcBeforeSym[UNUM_CURRENCY_SPACING_COUNT];
493
    UnicodeString currencySpcAfterSym[UNUM_CURRENCY_SPACING_COUNT];
494
    UBool fIsCustomCurrencySymbol;
495
    UBool fIsCustomIntlCurrencySymbol;
496
};
497
498
// -------------------------------------
499
500
inline UnicodeString
501
0
DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol) const {
502
0
    const UnicodeString *strPtr;
503
0
    if(symbol < kFormatSymbolCount) {
504
0
        strPtr = &fSymbols[symbol];
505
0
    } else {
506
0
        strPtr = &fNoSymbol;
507
0
    }
508
0
    return *strPtr;
509
0
}
510
511
// See comments above for this function. Not hidden with #ifdef U_HIDE_INTERNAL_API
512
inline const UnicodeString &
513
0
DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol) const {
514
0
    const UnicodeString *strPtr;
515
0
    if(symbol < kFormatSymbolCount) {
516
0
        strPtr = &fSymbols[symbol];
517
0
    } else {
518
0
        strPtr = &fNoSymbol;
519
0
    }
520
0
    return *strPtr;
521
0
}
522
523
#ifndef U_HIDE_INTERNAL_API
524
0
inline const UnicodeString& DecimalFormatSymbols::getConstDigitSymbol(int32_t digit) const {
525
0
    if (digit < 0 || digit > 9) {
526
0
        digit = 0;
527
0
    }
528
0
    if (digit == 0) {
529
0
        return fSymbols[kZeroDigitSymbol];
530
0
    }
531
0
    ENumberFormatSymbol key = static_cast<ENumberFormatSymbol>(kOneDigitSymbol + digit - 1);
532
0
    return fSymbols[key];
533
0
}
534
#endif
535
536
// -------------------------------------
537
538
inline void
539
0
DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits = TRUE) {
540
0
    if (symbol == kCurrencySymbol) {
541
0
        fIsCustomCurrencySymbol = TRUE;
542
0
    }
543
0
    else if (symbol == kIntlCurrencySymbol) {
544
0
        fIsCustomIntlCurrencySymbol = TRUE;
545
0
    }
546
0
    if(symbol<kFormatSymbolCount) {
547
0
        fSymbols[symbol]=value;
548
0
    }
549
0
550
0
    // If the zero digit is being set to a known zero digit according to Unicode,
551
0
    // then we automatically set the corresponding 1-9 digits
552
0
    // Also record updates to fCodePointZero. Be conservative if in doubt.
553
0
    if (symbol == kZeroDigitSymbol) {
554
0
        UChar32 sym = value.char32At(0);
555
0
        if ( propogateDigits && u_charDigitValue(sym) == 0 && value.countChar32() == 1 ) {
556
0
            fCodePointZero = sym;
557
0
            for ( int8_t i = 1 ; i<= 9 ; i++ ) {
558
0
                sym++;
559
0
                fSymbols[(int)kOneDigitSymbol+i-1] = UnicodeString(sym);
560
0
            }
561
0
        } else {
562
0
            fCodePointZero = -1;
563
0
        }
564
0
    } else if (symbol >= kOneDigitSymbol && symbol <= kNineDigitSymbol) {
565
0
        fCodePointZero = -1;
566
0
    }
567
0
}
568
569
// -------------------------------------
570
571
inline Locale
572
0
DecimalFormatSymbols::getLocale() const {
573
0
    return locale;
574
0
}
575
576
#ifndef U_HIDE_INTERNAL_API
577
inline const char16_t*
578
0
DecimalFormatSymbols::getCurrencyPattern() const {
579
0
    return currPattern;
580
0
}
581
#endif /* U_HIDE_INTERNAL_API */
582
583
U_NAMESPACE_END
584
585
#endif /* #if !UCONFIG_NO_FORMATTING */
586
587
#endif // _DCFMTSYM
588
//eof