Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | ****************************************************************************** |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 3 | * Copyright (C) 2014-2015, International Business Machines |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 4 | * Corporation and others. All Rights Reserved. |
| 5 | ****************************************************************************** |
| 6 | * quantityformatter.cpp |
| 7 | */ |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 8 | |
| 9 | #include "unicode/utypes.h" |
| 10 | |
| 11 | #if !UCONFIG_NO_FORMATTING |
| 12 | |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 13 | #include "quantityformatter.h" |
| 14 | #include "simplepatternformatter.h" |
| 15 | #include "uassert.h" |
| 16 | #include "unicode/unistr.h" |
| 17 | #include "unicode/decimfmt.h" |
| 18 | #include "cstring.h" |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 19 | #include "unicode/plurrule.h" |
| 20 | #include "charstr.h" |
| 21 | #include "unicode/fmtable.h" |
| 22 | #include "unicode/fieldpos.h" |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 23 | #include "standardplural.h" |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 24 | #include "visibledigits.h" |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 25 | #include "uassert.h" |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 26 | |
| 27 | U_NAMESPACE_BEGIN |
| 28 | |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 29 | QuantityFormatter::QuantityFormatter() { |
| 30 | for (int32_t i = 0; i < UPRV_LENGTHOF(formatters); ++i) { |
| 31 | formatters[i] = NULL; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | QuantityFormatter::QuantityFormatter(const QuantityFormatter &other) { |
| 36 | for (int32_t i = 0; i < UPRV_LENGTHOF(formatters); ++i) { |
| 37 | if (other.formatters[i] == NULL) { |
| 38 | formatters[i] = NULL; |
| 39 | } else { |
| 40 | formatters[i] = new SimplePatternFormatter(*other.formatters[i]); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | QuantityFormatter &QuantityFormatter::operator=( |
| 46 | const QuantityFormatter& other) { |
| 47 | if (this == &other) { |
| 48 | return *this; |
| 49 | } |
| 50 | for (int32_t i = 0; i < UPRV_LENGTHOF(formatters); ++i) { |
| 51 | delete formatters[i]; |
| 52 | if (other.formatters[i] == NULL) { |
| 53 | formatters[i] = NULL; |
| 54 | } else { |
| 55 | formatters[i] = new SimplePatternFormatter(*other.formatters[i]); |
| 56 | } |
| 57 | } |
| 58 | return *this; |
| 59 | } |
| 60 | |
| 61 | QuantityFormatter::~QuantityFormatter() { |
| 62 | for (int32_t i = 0; i < UPRV_LENGTHOF(formatters); ++i) { |
| 63 | delete formatters[i]; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void QuantityFormatter::reset() { |
| 68 | for (int32_t i = 0; i < UPRV_LENGTHOF(formatters); ++i) { |
| 69 | delete formatters[i]; |
| 70 | formatters[i] = NULL; |
| 71 | } |
| 72 | } |
| 73 | |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 74 | UBool QuantityFormatter::addIfAbsent( |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 75 | const char *variant, |
| 76 | const UnicodeString &rawPattern, |
| 77 | UErrorCode &status) { |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 78 | int32_t pluralIndex = StandardPlural::indexFromString(variant, status); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 79 | if (U_FAILURE(status)) { |
| 80 | return FALSE; |
| 81 | } |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 82 | if (formatters[pluralIndex] != NULL) { |
| 83 | return TRUE; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 84 | } |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 85 | SimplePatternFormatter *newFmt = new SimplePatternFormatter(rawPattern, 0, 1, status); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 86 | if (newFmt == NULL) { |
| 87 | status = U_MEMORY_ALLOCATION_ERROR; |
| 88 | return FALSE; |
| 89 | } |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 90 | if (U_FAILURE(status)) { |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 91 | delete newFmt; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 92 | return FALSE; |
| 93 | } |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 94 | formatters[pluralIndex] = newFmt; |
| 95 | return TRUE; |
| 96 | } |
| 97 | |
| 98 | UBool QuantityFormatter::isValid() const { |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 99 | return formatters[StandardPlural::OTHER] != NULL; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | const SimplePatternFormatter *QuantityFormatter::getByVariant( |
| 103 | const char *variant) const { |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 104 | U_ASSERT(isValid()); |
| 105 | int32_t pluralIndex = StandardPlural::indexOrOtherIndexFromString(variant); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 106 | const SimplePatternFormatter *pattern = formatters[pluralIndex]; |
| 107 | if (pattern == NULL) { |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 108 | pattern = formatters[StandardPlural::OTHER]; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 109 | } |
| 110 | return pattern; |
| 111 | } |
| 112 | |
| 113 | UnicodeString &QuantityFormatter::format( |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 114 | const Formattable &number, |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 115 | const NumberFormat &fmt, |
| 116 | const PluralRules &rules, |
| 117 | UnicodeString &appendTo, |
| 118 | FieldPosition &pos, |
| 119 | UErrorCode &status) const { |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 120 | UnicodeString formattedNumber; |
| 121 | StandardPlural::Form p = selectPlural(number, fmt, rules, formattedNumber, pos, status); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 122 | if (U_FAILURE(status)) { |
| 123 | return appendTo; |
| 124 | } |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 125 | const SimplePatternFormatter *pattern = formatters[p]; |
| 126 | if (pattern == NULL) { |
| 127 | pattern = formatters[StandardPlural::OTHER]; |
| 128 | if (pattern == NULL) { |
| 129 | status = U_INVALID_STATE_ERROR; |
| 130 | return appendTo; |
| 131 | } |
| 132 | } |
| 133 | return format(*pattern, formattedNumber, appendTo, pos, status); |
| 134 | } |
| 135 | |
| 136 | // The following methods live here so that class PluralRules does not depend on number formatting, |
| 137 | // and the SimplePatternFormatter does not depend on FieldPosition. |
| 138 | |
| 139 | StandardPlural::Form QuantityFormatter::selectPlural( |
| 140 | const Formattable &number, |
| 141 | const NumberFormat &fmt, |
| 142 | const PluralRules &rules, |
| 143 | UnicodeString &formattedNumber, |
| 144 | FieldPosition &pos, |
| 145 | UErrorCode &status) { |
| 146 | if (U_FAILURE(status)) { |
| 147 | return StandardPlural::OTHER; |
| 148 | } |
| 149 | UnicodeString pluralKeyword; |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 150 | VisibleDigitsWithExponent digits; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 151 | const DecimalFormat *decFmt = dynamic_cast<const DecimalFormat *>(&fmt); |
| 152 | if (decFmt != NULL) { |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 153 | decFmt->initVisibleDigitsWithExponent(number, digits, status); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 154 | if (U_FAILURE(status)) { |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 155 | return StandardPlural::OTHER; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 156 | } |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 157 | pluralKeyword = rules.select(digits); |
| 158 | decFmt->format(digits, formattedNumber, pos, status); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 159 | } else { |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 160 | if (number.getType() == Formattable::kDouble) { |
| 161 | pluralKeyword = rules.select(number.getDouble()); |
| 162 | } else if (number.getType() == Formattable::kLong) { |
| 163 | pluralKeyword = rules.select(number.getLong()); |
| 164 | } else if (number.getType() == Formattable::kInt64) { |
| 165 | pluralKeyword = rules.select((double) number.getInt64()); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 166 | } else { |
| 167 | status = U_ILLEGAL_ARGUMENT_ERROR; |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 168 | return StandardPlural::OTHER; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 169 | } |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 170 | fmt.format(number, formattedNumber, pos, status); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 171 | } |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 172 | return StandardPlural::orOtherFromString(pluralKeyword); |
| 173 | } |
| 174 | |
| 175 | UnicodeString &QuantityFormatter::format( |
| 176 | const SimplePatternFormatter &pattern, |
| 177 | const UnicodeString &value, |
| 178 | UnicodeString &appendTo, |
| 179 | FieldPosition &pos, |
| 180 | UErrorCode &status) { |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 181 | if (U_FAILURE(status)) { |
| 182 | return appendTo; |
| 183 | } |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame^] | 184 | const UnicodeString *param = &value; |
| 185 | int32_t offset; |
| 186 | pattern.formatAndAppend(¶m, 1, appendTo, &offset, 1, status); |
| 187 | if (pos.getBeginIndex() != 0 || pos.getEndIndex() != 0) { |
| 188 | if (offset >= 0) { |
| 189 | pos.setBeginIndex(pos.getBeginIndex() + offset); |
| 190 | pos.setEndIndex(pos.getEndIndex() + offset); |
| 191 | } else { |
| 192 | pos.setBeginIndex(0); |
| 193 | pos.setEndIndex(0); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | return appendTo; |
| 197 | } |
| 198 | |
| 199 | U_NAMESPACE_END |
| 200 | |
| 201 | #endif /* #if !UCONFIG_NO_FORMATTING */ |