diff --git a/icu4c/source/i18n/unicode/dcfmtsym.h b/icu4c/source/i18n/unicode/dcfmtsym.h index c6da623034c..ebcaa4cb943 100644 --- a/icu4c/source/i18n/unicode/dcfmtsym.h +++ b/icu4c/source/i18n/unicode/dcfmtsym.h @@ -411,11 +411,21 @@ public: * * @param symbol Constant to indicate a number format symbol. * @return the format symbol by the param 'symbol' - * @internal + * @draft ICU 61 */ - inline const UnicodeString &getConstSymbol(ENumberFormatSymbol symbol) const; + inline const UnicodeString& getConstSymbol(ENumberFormatSymbol symbol) const; #ifndef U_HIDE_INTERNAL_API + /** + * Returns the const UnicodeString reference, like getConstSymbol, + * corresponding to the digit with the given value. This is equivalent + * to accessing the symbol from getConstSymbol with the corresponding + * key, such as kZeroDigitSymbol or kOneDigitSymbol. + * + * @internal This API is currently for ICU use only. + */ + inline const UnicodeString& getConstDigitSymbol(int32_t digit); + /** * Returns that pattern stored in currecy info. Internal API for use by NumberFormat API. * @internal @@ -500,6 +510,17 @@ DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol) const { return *strPtr; } +inline const UnicodeString& DecimalFormatSymbols::getConstDigitSymbol(int32_t digit) { + if (digit < 0 || digit > 9) { + digit = 0; + } + if (digit == 0) { + return fSymbols[kZeroDigitSymbol]; + } + ENumberFormatSymbol key = static_cast(kOneDigitSymbol + digit - 1); + return fSymbols[key]; +} + // ------------------------------------- inline void diff --git a/icu4c/source/test/intltest/tsdcfmsy.cpp b/icu4c/source/test/intltest/tsdcfmsy.cpp index 0cbd784ec87..a7462ae7533 100644 --- a/icu4c/source/test/intltest/tsdcfmsy.cpp +++ b/icu4c/source/test/intltest/tsdcfmsy.cpp @@ -252,7 +252,7 @@ void IntlTestDecimalFormatSymbols::testLastResortData() { void IntlTestDecimalFormatSymbols::testDigitSymbols() { // This test does more in ICU4J than in ICU4C right now. - // In ICU4C, it is basically just a test for codePointZero. + // In ICU4C, it is basically just a test for codePointZero and getConstDigitSymbol. UChar defZero = u'0'; UChar32 osmanyaZero = U'\U000104A0'; static const UChar* osmanyaDigitStrings[] = { @@ -266,13 +266,18 @@ void IntlTestDecimalFormatSymbols::testDigitSymbols() { if (defZero != symbols.getCodePointZero()) { errln("ERROR: Code point zero be ASCII 0"); } + for (int32_t i=0; i<=9; i++) { + assertEquals(UnicodeString("i. ASCII Digit at index ") + Int64ToUnicodeString(i), + UnicodeString(u'0' + i), + symbols.getConstDigitSymbol(i)); + } for (int32_t i=0; i<=9; i++) { DecimalFormatSymbols::ENumberFormatSymbol key = i == 0 ? DecimalFormatSymbols::kZeroDigitSymbol : static_cast - (DecimalFormatSymbols::kOneDigitSymbol + i); + (DecimalFormatSymbols::kOneDigitSymbol + i - 1); symbols.setSymbol(key, UnicodeString(osmanyaDigitStrings[i]), FALSE); } // NOTE: in ICU4J, the calculation of codePointZero is smarter; @@ -280,6 +285,11 @@ void IntlTestDecimalFormatSymbols::testDigitSymbols() { if (-1 != symbols.getCodePointZero()) { errln("ERROR: Code point zero be invalid"); } + for (int32_t i=0; i<=9; i++) { + assertEquals(UnicodeString("ii. Osmanya digit at index ") + Int64ToUnicodeString(i), + UnicodeString(osmanyaDigitStrings[i]), + symbols.getConstDigitSymbol(i)); + } // Check Osmanya codePointZero symbols.setSymbol( @@ -288,6 +298,11 @@ void IntlTestDecimalFormatSymbols::testDigitSymbols() { if (osmanyaZero != symbols.getCodePointZero()) { errln("ERROR: Code point zero be Osmanya code point zero"); } + for (int32_t i=0; i<=9; i++) { + assertEquals(UnicodeString("iii. Osmanya digit at index ") + Int64ToUnicodeString(i), + UnicodeString(osmanyaDigitStrings[i]), + symbols.getConstDigitSymbol(i)); + } // Reset digits to Latin symbols.setSymbol( @@ -296,6 +311,11 @@ void IntlTestDecimalFormatSymbols::testDigitSymbols() { if (defZero != symbols.getCodePointZero()) { errln("ERROR: Code point zero be ASCII 0"); } + for (int32_t i=0; i<=9; i++) { + assertEquals(UnicodeString("iv. ASCII Digit at index ") + Int64ToUnicodeString(i), + UnicodeString(u'0' + i), + symbols.getConstDigitSymbol(i)); + } } void IntlTestDecimalFormatSymbols::testNumberingSystem() {