]> granicus.if.org Git - icu/commitdiff
ICU-10046 Expose Exponent Multiplication Symbol. ScientificFormatHelper class to...
authorTravis Keep <keep94@gmail.com>
Thu, 10 Apr 2014 18:13:29 +0000 (18:13 +0000)
committerTravis Keep <keep94@gmail.com>
Thu, 10 Apr 2014 18:13:29 +0000 (18:13 +0000)
X-SVN-Rev: 35614

icu4c/source/i18n/dcfmtsym.cpp
icu4c/source/i18n/scientificformathelper.cpp
icu4c/source/i18n/unicode/dcfmtsym.h
icu4c/source/i18n/unicode/scientificformathelper.h
icu4c/source/i18n/unicode/unum.h
icu4c/source/test/intltest/scientificformathelpertest.cpp

index 809e8a19074b1f5aee8bdf4c2d22f5b03cfadd6d..68d8238b2328aa0d5d3e2243c3f06bb2fa5677be 100644 (file)
@@ -1,6 +1,6 @@
 /*
 *******************************************************************************
-* Copyright (C) 1997-2013, International Business Machines Corporation and
+* Copyright (C) 1997-2014, International Business Machines Corporation and
 * others. All Rights Reserved.
 *******************************************************************************
 *
@@ -189,6 +189,7 @@ DecimalFormatSymbols::initialize(const Locale& loc, UErrorCode& status, UBool us
         NULL, /* seven digit - get it from the numbering system */
         NULL, /* eight digit - get it from the numbering system */
         NULL, /* nine digit - get it from the numbering system */
+        "superscriptingExponent", /* Multiplication (x) symbol for exponents */
     };
 
     static const char *gLatn =  "latn";
@@ -420,6 +421,7 @@ DecimalFormatSymbols::initialize() {
     fSymbols[kNaNSymbol] = (UChar)0xfffd;               // SUB NaN
     fSymbols[kSignificantDigitSymbol] = (UChar)0x0040;  // '@' significant digit
     fSymbols[kMonetaryGroupingSeparatorSymbol].remove(); // 
+    fSymbols[kExponentMultiplicationSymbol] = (UChar)0xd7; // 'x' multiplication symbol for exponents
 }
 
 Locale
index c9d7fdf58e321e8d51e7cf9ee807185fba46f655..8b07e69545a9f6780da397331b6725ad9ec16b30 100644 (file)
 #include "unicode/dcfmtsym.h"
 #include "unicode/fpositer.h"
 #include "unicode/utf16.h"
+#include "unicode/uniset.h"
+#include "decfmtst.h"
 
 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
 
-// TODO: Add U_DRAFT_API directives.
-// TODO: Add U_FORMATTING directives
-
 U_NAMESPACE_BEGIN
 
-static UChar kExponentDigits[] = {0x2070, 0xB9, 0xB2, 0xB3, 0x2074, 0x2075, 0x2076, 0x2077, 0x2078, 0x2079};
+static UChar kSuperscriptDigits[] = {0x2070, 0xB9, 0xB2, 0xB3, 0x2074, 0x2075, 0x2076, 0x2077, 0x2078, 0x2079};
+
+static UChar kSuperscriptPlusSign = 0x207A;
+static UChar kSuperscriptMinusSign = 0x207B;
 
 static UnicodeString getMultiplicationSymbol(const DecimalFormatSymbols &dfs) {
-    static UChar multSign = 0xD7;
-    return UnicodeString(FALSE, &multSign, 1);
+    return dfs.getConstSymbol(DecimalFormatSymbols::kExponentMultiplicationSymbol);
 }
 
 ScientificFormatHelper::ScientificFormatHelper(
-        const DecimalFormatSymbols &dfs, UErrorCode &status) : fPreExponent() {
+        const DecimalFormatSymbols &dfs, UErrorCode &status)
+        : fPreExponent(), fStaticSets(NULL) {
     if (U_FAILURE(status)) {
         return;
     }
-    fPreExponent.append(getMultiplicationSymbol(dfs));
+    fPreExponent.append(dfs.getConstSymbol(
+            DecimalFormatSymbols::kExponentMultiplicationSymbol));
     fPreExponent.append(dfs.getSymbol(DecimalFormatSymbols::kOneDigitSymbol));
     fPreExponent.append(dfs.getSymbol(DecimalFormatSymbols::kZeroDigitSymbol));
+    fStaticSets = DecimalFormatStaticSets::getStaticSets(status);
 }
 
 ScientificFormatHelper::ScientificFormatHelper(
-        const ScientificFormatHelper &other) : fPreExponent(other.fPreExponent) {
+        const ScientificFormatHelper &other)
+        : fPreExponent(other.fPreExponent), fStaticSets(other.fStaticSets) {
 }
 
 ScientificFormatHelper &ScientificFormatHelper::operator=(const ScientificFormatHelper &other) {
@@ -46,6 +51,7 @@ ScientificFormatHelper &ScientificFormatHelper::operator=(const ScientificFormat
         return *this;
     }
     fPreExponent = other.fPreExponent;
+    fStaticSets = other.fStaticSets;
     return *this;
 }
 
@@ -98,17 +104,12 @@ static UBool copyAsSuperscript(
             status = U_INVALID_CHAR_FOUND;
             return FALSE;
         }
-        result.append(kExponentDigits[digit]);
+        result.append(kSuperscriptDigits[digit]);
         i += U16_LENGTH(c);
     }
     return TRUE;
 }
 
-static UBool isMinusSign(UChar ch) {
-    // TODO: revisit this.
-    return (ch == 0x2D);
-}
-
 UnicodeString &ScientificFormatHelper::toSuperscriptExponentDigits(
         const UnicodeString &s,
         FieldPositionIterator &fpi,
@@ -127,9 +128,13 @@ UnicodeString &ScientificFormatHelper::toSuperscriptExponentDigits(
             {
                 int32_t beginIndex = fp.getBeginIndex();
                 int32_t endIndex = fp.getEndIndex();
-                if (endIndex - beginIndex == 1 && isMinusSign(s[beginIndex])) {
+                UChar32 aChar = s.char32At(beginIndex);
+                if (fStaticSets->fMinusSigns->contains(aChar)) {
+                    result.append(s, copyFromOffset, beginIndex - copyFromOffset);
+                    result.append(kSuperscriptMinusSign);
+                } else if (fStaticSets->fPlusSigns->contains(aChar)) {
                     result.append(s, copyFromOffset, beginIndex - copyFromOffset);
-                    result.append(0x207B);
+                    result.append(kSuperscriptPlusSign);
                 } else {
                     status = U_INVALID_CHAR_FOUND;
                     return result;
index 662d58b221bb073df4ada1b951666a784cc70c4f..b24ea9cd6b14445733477a23610156bd0f8b4af3 100644 (file)
@@ -163,6 +163,10 @@ public:
          * @stable ICU 4.6
          */
         kNineDigitSymbol,
+        /** Multiplication sign.
+         * @draft ICU 54
+         */
+        kExponentMultiplicationSymbol,
         /** count symbol constants */
         kFormatSymbolCount
     };
index 9498ca3d5f3ad9f88f95225ed8b68549be2a547e..0ce9fee7a8265451c15e2441c6e7fd70af28bb1d 100644 (file)
@@ -24,6 +24,7 @@ U_NAMESPACE_BEGIN
 
 class DecimalFormatSymbols;
 class FieldPositionIterator;
+class DecimalFormatStaticSets;
 
 /**
  * A helper class for formatting in user-friendly scientific notation.
@@ -121,6 +122,7 @@ class U_I18N_API ScientificFormatHelper : public UObject {
         UErrorCode &status) const;
  private:
     UnicodeString fPreExponent;
+    const DecimalFormatStaticSets *fStaticSets;
 };
 
 U_NAMESPACE_END
index 6071f2f4876f552ac65b5e36863c7f5782b7f760..c7d0e6e2f2d5ce26ec4eb3c8dab2ee0e67b0ce8b 100644 (file)
@@ -1190,8 +1190,14 @@ typedef enum UNumberFormatSymbol {
    * @stable ICU 4.6
    */
   UNUM_NINE_DIGIT_SYMBOL = 26,
+
+  /** Multiplication sign
+   * @draft ICU 54
+   */
+  UNUM_EXPONENT_MULTIPLICATION_SYMBOL = 27,
+
   /** count symbol constants */
-  UNUM_FORMAT_SYMBOL_COUNT = 27
+  UNUM_FORMAT_SYMBOL_COUNT = 28
 } UNumberFormatSymbol;
 
 /**
index f0855f8b768c07ce931b0de1b29cf3b4d41a7d1c..e1c1f665d38414e148ac5389dcbdb272089fe9f9 100644 (file)
@@ -29,6 +29,7 @@ public:
     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=0);
 private:
     void TestBasic();
+    void TestPlusSignInExponent();
 };
 
 void ScientificFormatHelperTest::runIndexedTest(
@@ -38,6 +39,7 @@ void ScientificFormatHelperTest::runIndexedTest(
     }
     TESTCASE_AUTO_BEGIN;
     TESTCASE_AUTO(TestBasic);
+    TESTCASE_AUTO(TestPlusSignInExponent);
     TESTCASE_AUTO_END;
 }
 
@@ -72,6 +74,24 @@ void ScientificFormatHelperTest::TestBasic() {
     }
 }
 
+void ScientificFormatHelperTest::TestPlusSignInExponent() {
+    UErrorCode status = U_ZERO_ERROR;
+    LocalPointer<DecimalFormat> decfmt((DecimalFormat *) NumberFormat::createScientificInstance("en", status));
+    decfmt->applyPattern("0.00E+0", status);
+    assertSuccess("1", status);
+    UnicodeString appendTo;
+    FieldPositionIterator fpositer;
+    decfmt->format(6.02e23, appendTo, &fpositer, status);
+    ScientificFormatHelper helper(*decfmt->getDecimalFormatSymbols(), status);
+    UnicodeString result;
+    const char *expected = "6.02\\u00d710\\u207a\\u00b2\\u00b3";
+    assertEquals(
+            "",
+            UnicodeString(expected).unescape(),
+            helper.toSuperscriptExponentDigits(appendTo, fpositer, result, status));
+    assertSuccess("", status);
+}
+
 extern IntlTest *createScientificFormatHelperTest() {
     return new ScientificFormatHelperTest();
 }