| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 ********************************************************************** | |
| 3 * Copyright (c) 2014, International Business Machines | |
| 4 * Corporation and others. All Rights Reserved. | |
| 5 ********************************************************************** | |
| 6 */ | |
| 7 #include "unicode/utypes.h" | |
| 8 | |
| 9 #if !UCONFIG_NO_FORMATTING | |
| 10 | |
| 11 #include "unicode/scientificformathelper.h" | |
| 12 #include "unicode/dcfmtsym.h" | |
| 13 #include "unicode/fpositer.h" | |
| 14 #include "unicode/utf16.h" | |
| 15 #include "unicode/uniset.h" | |
| 16 #include "decfmtst.h" | |
| 17 | |
| 18 U_NAMESPACE_BEGIN | |
| 19 | |
| 20 static const UChar kSuperscriptDigits[] = {0x2070, 0xB9, 0xB2, 0xB3, 0x2074, 0x2
075, 0x2076, 0x2077, 0x2078, 0x2079}; | |
| 21 | |
| 22 static const UChar kSuperscriptPlusSign = 0x207A; | |
| 23 static const UChar kSuperscriptMinusSign = 0x207B; | |
| 24 | |
| 25 ScientificFormatHelper::ScientificFormatHelper( | |
| 26 const DecimalFormatSymbols &dfs, UErrorCode &status) | |
| 27 : fPreExponent(), fStaticSets(NULL) { | |
| 28 if (U_FAILURE(status)) { | |
| 29 return; | |
| 30 } | |
| 31 fPreExponent.append(dfs.getConstSymbol( | |
| 32 DecimalFormatSymbols::kExponentMultiplicationSymbol)); | |
| 33 fPreExponent.append(dfs.getSymbol(DecimalFormatSymbols::kOneDigitSymbol)); | |
| 34 fPreExponent.append(dfs.getSymbol(DecimalFormatSymbols::kZeroDigitSymbol)); | |
| 35 fStaticSets = DecimalFormatStaticSets::getStaticSets(status); | |
| 36 } | |
| 37 | |
| 38 ScientificFormatHelper::ScientificFormatHelper( | |
| 39 const ScientificFormatHelper &other) | |
| 40 : UObject(other), | |
| 41 fPreExponent(other.fPreExponent), | |
| 42 fStaticSets(other.fStaticSets) { | |
| 43 } | |
| 44 | |
| 45 ScientificFormatHelper &ScientificFormatHelper::operator=(const ScientificFormat
Helper &other) { | |
| 46 if (this == &other) { | |
| 47 return *this; | |
| 48 } | |
| 49 fPreExponent = other.fPreExponent; | |
| 50 fStaticSets = other.fStaticSets; | |
| 51 return *this; | |
| 52 } | |
| 53 | |
| 54 ScientificFormatHelper::~ScientificFormatHelper() { | |
| 55 } | |
| 56 | |
| 57 UnicodeString &ScientificFormatHelper::insertMarkup( | |
| 58 const UnicodeString &s, | |
| 59 FieldPositionIterator &fpi, | |
| 60 const UnicodeString &beginMarkup, | |
| 61 const UnicodeString &endMarkup, | |
| 62 UnicodeString &result, | |
| 63 UErrorCode &status) const { | |
| 64 if (U_FAILURE(status)) { | |
| 65 return result; | |
| 66 } | |
| 67 FieldPosition fp; | |
| 68 int32_t copyFromOffset = 0; | |
| 69 UBool exponentSymbolFieldPresent = FALSE; | |
| 70 UBool exponentFieldPresent = FALSE; | |
| 71 while (fpi.next(fp)) { | |
| 72 switch (fp.getField()) { | |
| 73 case UNUM_EXPONENT_SYMBOL_FIELD: | |
| 74 exponentSymbolFieldPresent = TRUE; | |
| 75 result.append(s, copyFromOffset, fp.getBeginIndex() - copyFromOffset
); | |
| 76 copyFromOffset = fp.getEndIndex(); | |
| 77 result.append(fPreExponent); | |
| 78 result.append(beginMarkup); | |
| 79 break; | |
| 80 case UNUM_EXPONENT_FIELD: | |
| 81 exponentFieldPresent = TRUE; | |
| 82 result.append(s, copyFromOffset, fp.getEndIndex() - copyFromOffset); | |
| 83 copyFromOffset = fp.getEndIndex(); | |
| 84 result.append(endMarkup); | |
| 85 break; | |
| 86 default: | |
| 87 break; | |
| 88 } | |
| 89 } | |
| 90 if (!exponentSymbolFieldPresent || !exponentFieldPresent) { | |
| 91 status = U_ILLEGAL_ARGUMENT_ERROR; | |
| 92 return result; | |
| 93 } | |
| 94 result.append(s, copyFromOffset, s.length() - copyFromOffset); | |
| 95 return result; | |
| 96 } | |
| 97 | |
| 98 static UBool copyAsSuperscript( | |
| 99 const UnicodeString &s, | |
| 100 int32_t beginIndex, | |
| 101 int32_t endIndex, | |
| 102 UnicodeString &result, | |
| 103 UErrorCode &status) { | |
| 104 if (U_FAILURE(status)) { | |
| 105 return FALSE; | |
| 106 } | |
| 107 for (int32_t i = beginIndex; i < endIndex;) { | |
| 108 UChar32 c = s.char32At(i); | |
| 109 int32_t digit = u_charDigitValue(c); | |
| 110 if (digit < 0) { | |
| 111 status = U_INVALID_CHAR_FOUND; | |
| 112 return FALSE; | |
| 113 } | |
| 114 result.append(kSuperscriptDigits[digit]); | |
| 115 i += U16_LENGTH(c); | |
| 116 } | |
| 117 return TRUE; | |
| 118 } | |
| 119 | |
| 120 UnicodeString &ScientificFormatHelper::toSuperscriptExponentDigits( | |
| 121 const UnicodeString &s, | |
| 122 FieldPositionIterator &fpi, | |
| 123 UnicodeString &result, | |
| 124 UErrorCode &status) const { | |
| 125 if (U_FAILURE(status)) { | |
| 126 return result; | |
| 127 } | |
| 128 FieldPosition fp; | |
| 129 int32_t copyFromOffset = 0; | |
| 130 UBool exponentSymbolFieldPresent = FALSE; | |
| 131 UBool exponentFieldPresent = FALSE; | |
| 132 while (fpi.next(fp)) { | |
| 133 switch (fp.getField()) { | |
| 134 case UNUM_EXPONENT_SYMBOL_FIELD: | |
| 135 exponentSymbolFieldPresent = TRUE; | |
| 136 result.append(s, copyFromOffset, fp.getBeginIndex() - copyFromOffset
); | |
| 137 copyFromOffset = fp.getEndIndex(); | |
| 138 result.append(fPreExponent); | |
| 139 break; | |
| 140 case UNUM_EXPONENT_SIGN_FIELD: | |
| 141 { | |
| 142 int32_t beginIndex = fp.getBeginIndex(); | |
| 143 int32_t endIndex = fp.getEndIndex(); | |
| 144 UChar32 aChar = s.char32At(beginIndex); | |
| 145 if (fStaticSets->fMinusSigns->contains(aChar)) { | |
| 146 result.append(s, copyFromOffset, beginIndex - copyFromOffset
); | |
| 147 result.append(kSuperscriptMinusSign); | |
| 148 } else if (fStaticSets->fPlusSigns->contains(aChar)) { | |
| 149 result.append(s, copyFromOffset, beginIndex - copyFromOffset
); | |
| 150 result.append(kSuperscriptPlusSign); | |
| 151 } else { | |
| 152 status = U_INVALID_CHAR_FOUND; | |
| 153 return result; | |
| 154 } | |
| 155 copyFromOffset = endIndex; | |
| 156 } | |
| 157 break; | |
| 158 case UNUM_EXPONENT_FIELD: | |
| 159 exponentFieldPresent = TRUE; | |
| 160 result.append(s, copyFromOffset, fp.getBeginIndex() - copyFromOffset
); | |
| 161 if (!copyAsSuperscript( | |
| 162 s, fp.getBeginIndex(), fp.getEndIndex(), result, status)) { | |
| 163 return result; | |
| 164 } | |
| 165 copyFromOffset = fp.getEndIndex(); | |
| 166 break; | |
| 167 default: | |
| 168 break; | |
| 169 } | |
| 170 } | |
| 171 if (!exponentSymbolFieldPresent || !exponentFieldPresent) { | |
| 172 status = U_ILLEGAL_ARGUMENT_ERROR; | |
| 173 return result; | |
| 174 } | |
| 175 result.append(s, copyFromOffset, s.length() - copyFromOffset); | |
| 176 return result; | |
| 177 } | |
| 178 | |
| 179 U_NAMESPACE_END | |
| 180 | |
| 181 #endif /* !UCONFIG_NO_FORMATTING */ | |
| OLD | NEW |