| OLD | NEW |
| 1 /* | 1 /* |
| 2 ****************************************************************************** | 2 ****************************************************************************** |
| 3 * Copyright (C) 2014, International Business Machines | 3 * Copyright (C) 2014-2015, International Business Machines |
| 4 * Corporation and others. All Rights Reserved. | 4 * Corporation and others. All Rights Reserved. |
| 5 ****************************************************************************** | 5 ****************************************************************************** |
| 6 * quantityformatter.cpp | 6 * quantityformatter.cpp |
| 7 */ | 7 */ |
| 8 #include "quantityformatter.h" | 8 #include "quantityformatter.h" |
| 9 #include "simplepatternformatter.h" | 9 #include "simplepatternformatter.h" |
| 10 #include "uassert.h" | 10 #include "uassert.h" |
| 11 #include "unicode/unistr.h" | 11 #include "unicode/unistr.h" |
| 12 #include "unicode/decimfmt.h" | 12 #include "unicode/decimfmt.h" |
| 13 #include "cstring.h" | 13 #include "cstring.h" |
| 14 #include "plurrule_impl.h" | |
| 15 #include "unicode/plurrule.h" | 14 #include "unicode/plurrule.h" |
| 16 #include "charstr.h" | 15 #include "charstr.h" |
| 17 #include "unicode/fmtable.h" | 16 #include "unicode/fmtable.h" |
| 18 #include "unicode/fieldpos.h" | 17 #include "unicode/fieldpos.h" |
| 18 #include "visibledigits.h" |
| 19 | 19 |
| 20 #if !UCONFIG_NO_FORMATTING | 20 #if !UCONFIG_NO_FORMATTING |
| 21 | 21 |
| 22 U_NAMESPACE_BEGIN | 22 U_NAMESPACE_BEGIN |
| 23 | 23 |
| 24 // other must always be first. | 24 // other must always be first. |
| 25 static const char * const gPluralForms[] = { | 25 static const char * const gPluralForms[] = { |
| 26 "other", "zero", "one", "two", "few", "many"}; | 26 "other", "zero", "one", "two", "few", "many"}; |
| 27 | 27 |
| 28 static int32_t getPluralIndex(const char *pluralForm) { | 28 static int32_t getPluralIndex(const char *pluralForm) { |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 const Formattable& quantity, | 129 const Formattable& quantity, |
| 130 const NumberFormat &fmt, | 130 const NumberFormat &fmt, |
| 131 const PluralRules &rules, | 131 const PluralRules &rules, |
| 132 UnicodeString &appendTo, | 132 UnicodeString &appendTo, |
| 133 FieldPosition &pos, | 133 FieldPosition &pos, |
| 134 UErrorCode &status) const { | 134 UErrorCode &status) const { |
| 135 if (U_FAILURE(status)) { | 135 if (U_FAILURE(status)) { |
| 136 return appendTo; | 136 return appendTo; |
| 137 } | 137 } |
| 138 UnicodeString count; | 138 UnicodeString count; |
| 139 VisibleDigitsWithExponent digits; |
| 139 const DecimalFormat *decFmt = dynamic_cast<const DecimalFormat *>(&fmt); | 140 const DecimalFormat *decFmt = dynamic_cast<const DecimalFormat *>(&fmt); |
| 140 if (decFmt != NULL) { | 141 if (decFmt != NULL) { |
| 141 FixedDecimal fd = decFmt->getFixedDecimal(quantity, status); | 142 decFmt->initVisibleDigitsWithExponent(quantity, digits, status); |
| 142 if (U_FAILURE(status)) { | 143 if (U_FAILURE(status)) { |
| 143 return appendTo; | 144 return appendTo; |
| 144 } | 145 } |
| 145 count = rules.select(fd); | 146 count = rules.select(digits); |
| 146 } else { | 147 } else { |
| 147 if (quantity.getType() == Formattable::kDouble) { | 148 if (quantity.getType() == Formattable::kDouble) { |
| 148 count = rules.select(quantity.getDouble()); | 149 count = rules.select(quantity.getDouble()); |
| 149 } else if (quantity.getType() == Formattable::kLong) { | 150 } else if (quantity.getType() == Formattable::kLong) { |
| 150 count = rules.select(quantity.getLong()); | 151 count = rules.select(quantity.getLong()); |
| 151 } else if (quantity.getType() == Formattable::kInt64) { | 152 } else if (quantity.getType() == Formattable::kInt64) { |
| 152 count = rules.select((double) quantity.getInt64()); | 153 count = rules.select((double) quantity.getInt64()); |
| 153 } else { | 154 } else { |
| 154 status = U_ILLEGAL_ARGUMENT_ERROR; | 155 status = U_ILLEGAL_ARGUMENT_ERROR; |
| 155 return appendTo; | 156 return appendTo; |
| 156 } | 157 } |
| 157 } | 158 } |
| 158 CharString buffer; | 159 CharString buffer; |
| 159 buffer.appendInvariantChars(count, status); | 160 buffer.appendInvariantChars(count, status); |
| 160 if (U_FAILURE(status)) { | 161 if (U_FAILURE(status)) { |
| 161 return appendTo; | 162 return appendTo; |
| 162 } | 163 } |
| 163 const SimplePatternFormatter *pattern = getByVariant(buffer.data()); | 164 const SimplePatternFormatter *pattern = getByVariant(buffer.data()); |
| 164 if (pattern == NULL) { | 165 if (pattern == NULL) { |
| 165 status = U_INVALID_STATE_ERROR; | 166 status = U_INVALID_STATE_ERROR; |
| 166 return appendTo; | 167 return appendTo; |
| 167 } | 168 } |
| 168 UnicodeString formattedNumber; | 169 UnicodeString formattedNumber; |
| 169 FieldPosition fpos(pos.getField()); | 170 FieldPosition fpos(pos.getField()); |
| 170 fmt.format(quantity, formattedNumber, fpos, status); | 171 if (decFmt != NULL) { |
| 172 decFmt->format(digits, formattedNumber, fpos, status); |
| 173 } else { |
| 174 fmt.format(quantity, formattedNumber, fpos, status); |
| 175 } |
| 171 const UnicodeString *params[1] = {&formattedNumber}; | 176 const UnicodeString *params[1] = {&formattedNumber}; |
| 172 int32_t offsets[1]; | 177 int32_t offsets[1]; |
| 173 pattern->format(params, UPRV_LENGTHOF(params), appendTo, offsets, UPRV_LENGT
HOF(offsets), status); | 178 pattern->formatAndAppend( |
| 179 params, |
| 180 UPRV_LENGTHOF(params), |
| 181 appendTo, |
| 182 offsets, |
| 183 UPRV_LENGTHOF(offsets), |
| 184 status); |
| 174 if (offsets[0] != -1) { | 185 if (offsets[0] != -1) { |
| 175 if (fpos.getBeginIndex() != 0 || fpos.getEndIndex() != 0) { | 186 if (fpos.getBeginIndex() != 0 || fpos.getEndIndex() != 0) { |
| 176 pos.setBeginIndex(fpos.getBeginIndex() + offsets[0]); | 187 pos.setBeginIndex(fpos.getBeginIndex() + offsets[0]); |
| 177 pos.setEndIndex(fpos.getEndIndex() + offsets[0]); | 188 pos.setEndIndex(fpos.getEndIndex() + offsets[0]); |
| 178 } | 189 } |
| 179 } | 190 } |
| 180 return appendTo; | 191 return appendTo; |
| 181 } | 192 } |
| 182 | 193 |
| 183 U_NAMESPACE_END | 194 U_NAMESPACE_END |
| 184 | 195 |
| 185 #endif /* #if !UCONFIG_NO_FORMATTING */ | 196 #endif /* #if !UCONFIG_NO_FORMATTING */ |
| OLD | NEW |