Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 1 | // © 2016 and later: Unicode, Inc. and others. |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 2 | // License & terms of use: http://www.unicode.org/copyright.html |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 3 | /* |
| 4 | ****************************************************************************** |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 5 | * Copyright (C) 2014-2016, International Business Machines |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 6 | * Corporation and others. All Rights Reserved. |
| 7 | ****************************************************************************** |
| 8 | * quantityformatter.cpp |
| 9 | */ |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 10 | |
| 11 | #include "unicode/utypes.h" |
| 12 | |
| 13 | #if !UCONFIG_NO_FORMATTING |
| 14 | |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 15 | #include "unicode/simpleformatter.h" |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 16 | #include "quantityformatter.h" |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 17 | #include "uassert.h" |
| 18 | #include "unicode/unistr.h" |
| 19 | #include "unicode/decimfmt.h" |
| 20 | #include "cstring.h" |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 21 | #include "unicode/plurrule.h" |
| 22 | #include "charstr.h" |
| 23 | #include "unicode/fmtable.h" |
| 24 | #include "unicode/fieldpos.h" |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 25 | #include "standardplural.h" |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 26 | #include "uassert.h" |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 27 | #include "number_decimalquantity.h" |
Frank Tang | 69c72a6 | 2019-04-03 21:41:21 -0700 | [diff] [blame] | 28 | #include "number_utypes.h" |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 29 | #include "formatted_string_builder.h" |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 30 | |
| 31 | U_NAMESPACE_BEGIN |
| 32 | |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 33 | QuantityFormatter::QuantityFormatter() { |
| 34 | for (int32_t i = 0; i < UPRV_LENGTHOF(formatters); ++i) { |
| 35 | formatters[i] = NULL; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | QuantityFormatter::QuantityFormatter(const QuantityFormatter &other) { |
| 40 | for (int32_t i = 0; i < UPRV_LENGTHOF(formatters); ++i) { |
| 41 | if (other.formatters[i] == NULL) { |
| 42 | formatters[i] = NULL; |
| 43 | } else { |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 44 | formatters[i] = new SimpleFormatter(*other.formatters[i]); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | QuantityFormatter &QuantityFormatter::operator=( |
| 50 | const QuantityFormatter& other) { |
| 51 | if (this == &other) { |
| 52 | return *this; |
| 53 | } |
| 54 | for (int32_t i = 0; i < UPRV_LENGTHOF(formatters); ++i) { |
| 55 | delete formatters[i]; |
| 56 | if (other.formatters[i] == NULL) { |
| 57 | formatters[i] = NULL; |
| 58 | } else { |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 59 | formatters[i] = new SimpleFormatter(*other.formatters[i]); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | return *this; |
| 63 | } |
| 64 | |
| 65 | QuantityFormatter::~QuantityFormatter() { |
| 66 | for (int32_t i = 0; i < UPRV_LENGTHOF(formatters); ++i) { |
| 67 | delete formatters[i]; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void QuantityFormatter::reset() { |
| 72 | for (int32_t i = 0; i < UPRV_LENGTHOF(formatters); ++i) { |
| 73 | delete formatters[i]; |
| 74 | formatters[i] = NULL; |
| 75 | } |
| 76 | } |
| 77 | |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 78 | UBool QuantityFormatter::addIfAbsent( |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 79 | const char *variant, |
| 80 | const UnicodeString &rawPattern, |
| 81 | UErrorCode &status) { |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 82 | int32_t pluralIndex = StandardPlural::indexFromString(variant, status); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 83 | if (U_FAILURE(status)) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 84 | return false; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 85 | } |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 86 | if (formatters[pluralIndex] != NULL) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 87 | return true; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 88 | } |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 89 | SimpleFormatter *newFmt = new SimpleFormatter(rawPattern, 0, 1, status); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 90 | if (newFmt == NULL) { |
| 91 | status = U_MEMORY_ALLOCATION_ERROR; |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 92 | return false; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 93 | } |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 94 | if (U_FAILURE(status)) { |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 95 | delete newFmt; |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 96 | return false; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 97 | } |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 98 | formatters[pluralIndex] = newFmt; |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 99 | return true; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | UBool QuantityFormatter::isValid() const { |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 103 | return formatters[StandardPlural::OTHER] != NULL; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 106 | const SimpleFormatter *QuantityFormatter::getByVariant( |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 107 | const char *variant) const { |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 108 | U_ASSERT(isValid()); |
| 109 | int32_t pluralIndex = StandardPlural::indexOrOtherIndexFromString(variant); |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 110 | const SimpleFormatter *pattern = formatters[pluralIndex]; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 111 | if (pattern == NULL) { |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 112 | pattern = formatters[StandardPlural::OTHER]; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 113 | } |
| 114 | return pattern; |
| 115 | } |
| 116 | |
| 117 | UnicodeString &QuantityFormatter::format( |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 118 | const Formattable &number, |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 119 | const NumberFormat &fmt, |
| 120 | const PluralRules &rules, |
| 121 | UnicodeString &appendTo, |
| 122 | FieldPosition &pos, |
| 123 | UErrorCode &status) const { |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 124 | UnicodeString formattedNumber; |
| 125 | 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] | 126 | if (U_FAILURE(status)) { |
| 127 | return appendTo; |
| 128 | } |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 129 | const SimpleFormatter *pattern = formatters[p]; |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 130 | if (pattern == NULL) { |
| 131 | pattern = formatters[StandardPlural::OTHER]; |
| 132 | if (pattern == NULL) { |
| 133 | status = U_INVALID_STATE_ERROR; |
| 134 | return appendTo; |
| 135 | } |
| 136 | } |
| 137 | return format(*pattern, formattedNumber, appendTo, pos, status); |
| 138 | } |
| 139 | |
| 140 | // The following methods live here so that class PluralRules does not depend on number formatting, |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 141 | // and the SimpleFormatter does not depend on FieldPosition. |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 142 | |
| 143 | StandardPlural::Form QuantityFormatter::selectPlural( |
| 144 | const Formattable &number, |
| 145 | const NumberFormat &fmt, |
| 146 | const PluralRules &rules, |
| 147 | UnicodeString &formattedNumber, |
| 148 | FieldPosition &pos, |
| 149 | UErrorCode &status) { |
| 150 | if (U_FAILURE(status)) { |
| 151 | return StandardPlural::OTHER; |
| 152 | } |
| 153 | UnicodeString pluralKeyword; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 154 | const DecimalFormat *decFmt = dynamic_cast<const DecimalFormat *>(&fmt); |
| 155 | if (decFmt != NULL) { |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 156 | number::impl::DecimalQuantity dq; |
| 157 | decFmt->formatToDecimalQuantity(number, dq, status); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 158 | if (U_FAILURE(status)) { |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 159 | return StandardPlural::OTHER; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 160 | } |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 161 | pluralKeyword = rules.select(dq); |
| 162 | decFmt->format(number, formattedNumber, pos, status); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 163 | } else { |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 164 | if (number.getType() == Formattable::kDouble) { |
| 165 | pluralKeyword = rules.select(number.getDouble()); |
| 166 | } else if (number.getType() == Formattable::kLong) { |
| 167 | pluralKeyword = rules.select(number.getLong()); |
| 168 | } else if (number.getType() == Formattable::kInt64) { |
| 169 | pluralKeyword = rules.select((double) number.getInt64()); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 170 | } else { |
| 171 | status = U_ILLEGAL_ARGUMENT_ERROR; |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 172 | return StandardPlural::OTHER; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 173 | } |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 174 | fmt.format(number, formattedNumber, pos, status); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 175 | } |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 176 | return StandardPlural::orOtherFromString(pluralKeyword); |
| 177 | } |
| 178 | |
Frank Tang | 69c72a6 | 2019-04-03 21:41:21 -0700 | [diff] [blame] | 179 | void QuantityFormatter::formatAndSelect( |
| 180 | double quantity, |
| 181 | const NumberFormat& fmt, |
| 182 | const PluralRules& rules, |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 183 | FormattedStringBuilder& output, |
Frank Tang | 69c72a6 | 2019-04-03 21:41:21 -0700 | [diff] [blame] | 184 | StandardPlural::Form& pluralForm, |
| 185 | UErrorCode& status) { |
| 186 | UnicodeString pluralKeyword; |
| 187 | const DecimalFormat* df = dynamic_cast<const DecimalFormat*>(&fmt); |
| 188 | if (df != nullptr) { |
| 189 | number::impl::UFormattedNumberData fn; |
| 190 | fn.quantity.setToDouble(quantity); |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 191 | const number::LocalizedNumberFormatter* lnf = df->toNumberFormatter(status); |
| 192 | if (U_FAILURE(status)) { |
| 193 | return; |
| 194 | } |
| 195 | lnf->formatImpl(&fn, status); |
Frank Tang | 69c72a6 | 2019-04-03 21:41:21 -0700 | [diff] [blame] | 196 | if (U_FAILURE(status)) { |
| 197 | return; |
| 198 | } |
| 199 | output = std::move(fn.getStringRef()); |
| 200 | pluralKeyword = rules.select(fn.quantity); |
| 201 | } else { |
| 202 | UnicodeString result; |
| 203 | fmt.format(quantity, result, status); |
| 204 | if (U_FAILURE(status)) { |
| 205 | return; |
| 206 | } |
Frank Tang | f222396 | 2020-04-27 18:25:29 -0700 | [diff] [blame] | 207 | // This code path is probably RBNF. Use the generic numeric field. |
| 208 | output.append(result, kGeneralNumericField, status); |
Frank Tang | 69c72a6 | 2019-04-03 21:41:21 -0700 | [diff] [blame] | 209 | if (U_FAILURE(status)) { |
| 210 | return; |
| 211 | } |
| 212 | pluralKeyword = rules.select(quantity); |
| 213 | } |
| 214 | pluralForm = StandardPlural::orOtherFromString(pluralKeyword); |
| 215 | } |
| 216 | |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 217 | UnicodeString &QuantityFormatter::format( |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 218 | const SimpleFormatter &pattern, |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 219 | const UnicodeString &value, |
| 220 | UnicodeString &appendTo, |
| 221 | FieldPosition &pos, |
| 222 | UErrorCode &status) { |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 223 | if (U_FAILURE(status)) { |
| 224 | return appendTo; |
| 225 | } |
Jungshik Shin | 825221b | 2016-01-29 01:05:51 -0800 | [diff] [blame] | 226 | const UnicodeString *param = &value; |
| 227 | int32_t offset; |
| 228 | pattern.formatAndAppend(¶m, 1, appendTo, &offset, 1, status); |
| 229 | if (pos.getBeginIndex() != 0 || pos.getEndIndex() != 0) { |
| 230 | if (offset >= 0) { |
| 231 | pos.setBeginIndex(pos.getBeginIndex() + offset); |
| 232 | pos.setEndIndex(pos.getEndIndex() + offset); |
| 233 | } else { |
| 234 | pos.setBeginIndex(0); |
| 235 | pos.setEndIndex(0); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | return appendTo; |
| 239 | } |
| 240 | |
| 241 | U_NAMESPACE_END |
| 242 | |
| 243 | #endif /* #if !UCONFIG_NO_FORMATTING */ |