Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1 | // © 2017 and later: Unicode, Inc. and others. |
| 2 | // License & terms of use: http://www.unicode.org/copyright.html |
| 3 | |
| 4 | #include "unicode/utypes.h" |
| 5 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 6 | #if !UCONFIG_NO_FORMATTING |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 7 | |
| 8 | #include "umutex.h" |
| 9 | #include "ucln_cmn.h" |
| 10 | #include "ucln_in.h" |
| 11 | #include "number_modifiers.h" |
| 12 | |
| 13 | using namespace icu; |
| 14 | using namespace icu::number; |
| 15 | using namespace icu::number::impl; |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | // TODO: This is copied from simpleformatter.cpp |
| 20 | const int32_t ARG_NUM_LIMIT = 0x100; |
| 21 | |
| 22 | // These are the default currency spacing UnicodeSets in CLDR. |
| 23 | // Pre-compute them for performance. |
| 24 | // The Java unit test testCurrencySpacingPatternStability() will start failing if these change in CLDR. |
Frank Tang | 1c67b4e | 2022-05-18 10:13:51 -0700 | [diff] [blame] | 25 | icu::UInitOnce gDefaultCurrencySpacingInitOnce {}; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 26 | |
| 27 | UnicodeSet *UNISET_DIGIT = nullptr; |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 28 | UnicodeSet *UNISET_NOTSZ = nullptr; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 29 | |
| 30 | UBool U_CALLCONV cleanupDefaultCurrencySpacing() { |
| 31 | delete UNISET_DIGIT; |
| 32 | UNISET_DIGIT = nullptr; |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 33 | delete UNISET_NOTSZ; |
| 34 | UNISET_NOTSZ = nullptr; |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 35 | gDefaultCurrencySpacingInitOnce.reset(); |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 36 | return true; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void U_CALLCONV initDefaultCurrencySpacing(UErrorCode &status) { |
| 40 | ucln_i18n_registerCleanup(UCLN_I18N_CURRENCY_SPACING, cleanupDefaultCurrencySpacing); |
| 41 | UNISET_DIGIT = new UnicodeSet(UnicodeString(u"[:digit:]"), status); |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 42 | UNISET_NOTSZ = new UnicodeSet(UnicodeString(u"[[:^S:]&[:^Z:]]"), status); |
| 43 | if (UNISET_DIGIT == nullptr || UNISET_NOTSZ == nullptr) { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 44 | status = U_MEMORY_ALLOCATION_ERROR; |
| 45 | return; |
| 46 | } |
| 47 | UNISET_DIGIT->freeze(); |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 48 | UNISET_NOTSZ->freeze(); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | } // namespace |
| 52 | |
| 53 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 54 | Modifier::~Modifier() = default; |
| 55 | |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 56 | Modifier::Parameters::Parameters() |
| 57 | : obj(nullptr) {} |
| 58 | |
| 59 | Modifier::Parameters::Parameters( |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 60 | const ModifierStore* _obj, Signum _signum, StandardPlural::Form _plural) |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 61 | : obj(_obj), signum(_signum), plural(_plural) {} |
| 62 | |
| 63 | ModifierStore::~ModifierStore() = default; |
| 64 | |
| 65 | AdoptingModifierStore::~AdoptingModifierStore() { |
| 66 | for (const Modifier *mod : mods) { |
| 67 | delete mod; |
| 68 | } |
| 69 | } |
| 70 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 71 | |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 72 | int32_t ConstantAffixModifier::apply(FormattedStringBuilder &output, int leftIndex, int rightIndex, |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 73 | UErrorCode &status) const { |
| 74 | // Insert the suffix first since inserting the prefix will change the rightIndex |
| 75 | int length = output.insert(rightIndex, fSuffix, fField, status); |
| 76 | length += output.insert(leftIndex, fPrefix, fField, status); |
| 77 | return length; |
| 78 | } |
| 79 | |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 80 | int32_t ConstantAffixModifier::getPrefixLength() const { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 81 | return fPrefix.length(); |
| 82 | } |
| 83 | |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 84 | int32_t ConstantAffixModifier::getCodePointCount() const { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 85 | return fPrefix.countChar32() + fSuffix.countChar32(); |
| 86 | } |
| 87 | |
| 88 | bool ConstantAffixModifier::isStrong() const { |
| 89 | return fStrong; |
| 90 | } |
| 91 | |
Frank Tang | f222396 | 2020-04-27 18:25:29 -0700 | [diff] [blame] | 92 | bool ConstantAffixModifier::containsField(Field field) const { |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 93 | (void)field; |
| 94 | // This method is not currently used. |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 95 | UPRV_UNREACHABLE_EXIT; |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void ConstantAffixModifier::getParameters(Parameters& output) const { |
| 99 | (void)output; |
| 100 | // This method is not currently used. |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 101 | UPRV_UNREACHABLE_EXIT; |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | bool ConstantAffixModifier::semanticallyEquivalent(const Modifier& other) const { |
| 105 | auto* _other = dynamic_cast<const ConstantAffixModifier*>(&other); |
| 106 | if (_other == nullptr) { |
| 107 | return false; |
| 108 | } |
| 109 | return fPrefix == _other->fPrefix |
| 110 | && fSuffix == _other->fSuffix |
| 111 | && fField == _other->fField |
| 112 | && fStrong == _other->fStrong; |
| 113 | } |
| 114 | |
| 115 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 116 | SimpleModifier::SimpleModifier(const SimpleFormatter &simpleFormatter, Field field, bool strong) |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 117 | : SimpleModifier(simpleFormatter, field, strong, {}) {} |
| 118 | |
| 119 | SimpleModifier::SimpleModifier(const SimpleFormatter &simpleFormatter, Field field, bool strong, |
| 120 | const Modifier::Parameters parameters) |
| 121 | : fCompiledPattern(simpleFormatter.compiledPattern), fField(field), fStrong(strong), |
| 122 | fParameters(parameters) { |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 123 | int32_t argLimit = SimpleFormatter::getArgumentLimit( |
| 124 | fCompiledPattern.getBuffer(), fCompiledPattern.length()); |
| 125 | if (argLimit == 0) { |
| 126 | // No arguments in compiled pattern |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 127 | fPrefixLength = fCompiledPattern.charAt(1) - ARG_NUM_LIMIT; |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 128 | U_ASSERT(2 + fPrefixLength == fCompiledPattern.length()); |
| 129 | // Set suffixOffset = -1 to indicate no arguments in compiled pattern. |
| 130 | fSuffixOffset = -1; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 131 | fSuffixLength = 0; |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 132 | } else { |
| 133 | U_ASSERT(argLimit == 1); |
| 134 | if (fCompiledPattern.charAt(1) != 0) { |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 135 | // Found prefix |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 136 | fPrefixLength = fCompiledPattern.charAt(1) - ARG_NUM_LIMIT; |
| 137 | fSuffixOffset = 3 + fPrefixLength; |
| 138 | } else { |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 139 | // No prefix |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 140 | fPrefixLength = 0; |
| 141 | fSuffixOffset = 2; |
| 142 | } |
| 143 | if (3 + fPrefixLength < fCompiledPattern.length()) { |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 144 | // Found suffix |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 145 | fSuffixLength = fCompiledPattern.charAt(fSuffixOffset) - ARG_NUM_LIMIT; |
| 146 | } else { |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 147 | // No suffix |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 148 | fSuffixLength = 0; |
| 149 | } |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | SimpleModifier::SimpleModifier() |
Frank Tang | f222396 | 2020-04-27 18:25:29 -0700 | [diff] [blame] | 154 | : fField(kUndefinedField), fStrong(false), fPrefixLength(0), fSuffixLength(0) { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 157 | int32_t SimpleModifier::apply(FormattedStringBuilder &output, int leftIndex, int rightIndex, |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 158 | UErrorCode &status) const { |
Frank Tang | 69c72a6 | 2019-04-03 21:41:21 -0700 | [diff] [blame] | 159 | return formatAsPrefixSuffix(output, leftIndex, rightIndex, status); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 162 | int32_t SimpleModifier::getPrefixLength() const { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 163 | return fPrefixLength; |
| 164 | } |
| 165 | |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 166 | int32_t SimpleModifier::getCodePointCount() const { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 167 | int32_t count = 0; |
| 168 | if (fPrefixLength > 0) { |
| 169 | count += fCompiledPattern.countChar32(2, fPrefixLength); |
| 170 | } |
| 171 | if (fSuffixLength > 0) { |
| 172 | count += fCompiledPattern.countChar32(1 + fSuffixOffset, fSuffixLength); |
| 173 | } |
| 174 | return count; |
| 175 | } |
| 176 | |
| 177 | bool SimpleModifier::isStrong() const { |
| 178 | return fStrong; |
| 179 | } |
| 180 | |
Frank Tang | f222396 | 2020-04-27 18:25:29 -0700 | [diff] [blame] | 181 | bool SimpleModifier::containsField(Field field) const { |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 182 | (void)field; |
| 183 | // This method is not currently used. |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 184 | UPRV_UNREACHABLE_EXIT; |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void SimpleModifier::getParameters(Parameters& output) const { |
| 188 | output = fParameters; |
| 189 | } |
| 190 | |
| 191 | bool SimpleModifier::semanticallyEquivalent(const Modifier& other) const { |
| 192 | auto* _other = dynamic_cast<const SimpleModifier*>(&other); |
| 193 | if (_other == nullptr) { |
| 194 | return false; |
| 195 | } |
| 196 | if (fParameters.obj != nullptr) { |
| 197 | return fParameters.obj == _other->fParameters.obj; |
| 198 | } |
| 199 | return fCompiledPattern == _other->fCompiledPattern |
| 200 | && fField == _other->fField |
| 201 | && fStrong == _other->fStrong; |
| 202 | } |
| 203 | |
| 204 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 205 | int32_t |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 206 | SimpleModifier::formatAsPrefixSuffix(FormattedStringBuilder &result, int32_t startIndex, int32_t endIndex, |
Frank Tang | 69c72a6 | 2019-04-03 21:41:21 -0700 | [diff] [blame] | 207 | UErrorCode &status) const { |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 208 | if (fSuffixOffset == -1 && fPrefixLength + fSuffixLength > 0) { |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 209 | // There is no argument for the inner number; overwrite the entire segment with our string. |
Frank Tang | 69c72a6 | 2019-04-03 21:41:21 -0700 | [diff] [blame] | 210 | return result.splice(startIndex, endIndex, fCompiledPattern, 2, 2 + fPrefixLength, fField, status); |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 211 | } else { |
| 212 | if (fPrefixLength > 0) { |
Frank Tang | 69c72a6 | 2019-04-03 21:41:21 -0700 | [diff] [blame] | 213 | result.insert(startIndex, fCompiledPattern, 2, 2 + fPrefixLength, fField, status); |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 214 | } |
| 215 | if (fSuffixLength > 0) { |
| 216 | result.insert( |
| 217 | endIndex + fPrefixLength, |
| 218 | fCompiledPattern, |
| 219 | 1 + fSuffixOffset, |
| 220 | 1 + fSuffixOffset + fSuffixLength, |
Frank Tang | 69c72a6 | 2019-04-03 21:41:21 -0700 | [diff] [blame] | 221 | fField, |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 222 | status); |
| 223 | } |
| 224 | return fPrefixLength + fSuffixLength; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 225 | } |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 228 | |
| 229 | int32_t |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 230 | SimpleModifier::formatTwoArgPattern(const SimpleFormatter& compiled, FormattedStringBuilder& result, |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 231 | int32_t index, int32_t* outPrefixLength, int32_t* outSuffixLength, |
| 232 | Field field, UErrorCode& status) { |
| 233 | const UnicodeString& compiledPattern = compiled.compiledPattern; |
| 234 | int32_t argLimit = SimpleFormatter::getArgumentLimit( |
| 235 | compiledPattern.getBuffer(), compiledPattern.length()); |
| 236 | if (argLimit != 2) { |
| 237 | status = U_INTERNAL_PROGRAM_ERROR; |
| 238 | return 0; |
| 239 | } |
| 240 | int32_t offset = 1; // offset into compiledPattern |
| 241 | int32_t length = 0; // chars added to result |
| 242 | |
| 243 | int32_t prefixLength = compiledPattern.charAt(offset); |
| 244 | offset++; |
| 245 | if (prefixLength < ARG_NUM_LIMIT) { |
| 246 | // No prefix |
| 247 | prefixLength = 0; |
| 248 | } else { |
| 249 | prefixLength -= ARG_NUM_LIMIT; |
| 250 | result.insert(index + length, compiledPattern, offset, offset + prefixLength, field, status); |
| 251 | offset += prefixLength; |
| 252 | length += prefixLength; |
| 253 | offset++; |
| 254 | } |
| 255 | |
| 256 | int32_t infixLength = compiledPattern.charAt(offset); |
| 257 | offset++; |
| 258 | if (infixLength < ARG_NUM_LIMIT) { |
| 259 | // No infix |
| 260 | infixLength = 0; |
| 261 | } else { |
| 262 | infixLength -= ARG_NUM_LIMIT; |
| 263 | result.insert(index + length, compiledPattern, offset, offset + infixLength, field, status); |
| 264 | offset += infixLength; |
| 265 | length += infixLength; |
| 266 | offset++; |
| 267 | } |
| 268 | |
| 269 | int32_t suffixLength; |
| 270 | if (offset == compiledPattern.length()) { |
| 271 | // No suffix |
| 272 | suffixLength = 0; |
| 273 | } else { |
| 274 | suffixLength = compiledPattern.charAt(offset) - ARG_NUM_LIMIT; |
| 275 | offset++; |
| 276 | result.insert(index + length, compiledPattern, offset, offset + suffixLength, field, status); |
| 277 | length += suffixLength; |
| 278 | } |
| 279 | |
| 280 | *outPrefixLength = prefixLength; |
| 281 | *outSuffixLength = suffixLength; |
| 282 | |
| 283 | return length; |
| 284 | } |
| 285 | |
| 286 | |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 287 | int32_t ConstantMultiFieldModifier::apply(FormattedStringBuilder &output, int leftIndex, int rightIndex, |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 288 | UErrorCode &status) const { |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 289 | int32_t length = output.insert(leftIndex, fPrefix, status); |
| 290 | if (fOverwrite) { |
| 291 | length += output.splice( |
| 292 | leftIndex + length, |
| 293 | rightIndex + length, |
| 294 | UnicodeString(), 0, 0, |
Frank Tang | f222396 | 2020-04-27 18:25:29 -0700 | [diff] [blame] | 295 | kUndefinedField, status); |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 296 | } |
| 297 | length += output.insert(rightIndex + length, fSuffix, status); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 298 | return length; |
| 299 | } |
| 300 | |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 301 | int32_t ConstantMultiFieldModifier::getPrefixLength() const { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 302 | return fPrefix.length(); |
| 303 | } |
| 304 | |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 305 | int32_t ConstantMultiFieldModifier::getCodePointCount() const { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 306 | return fPrefix.codePointCount() + fSuffix.codePointCount(); |
| 307 | } |
| 308 | |
| 309 | bool ConstantMultiFieldModifier::isStrong() const { |
| 310 | return fStrong; |
| 311 | } |
| 312 | |
Frank Tang | f222396 | 2020-04-27 18:25:29 -0700 | [diff] [blame] | 313 | bool ConstantMultiFieldModifier::containsField(Field field) const { |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 314 | return fPrefix.containsField(field) || fSuffix.containsField(field); |
| 315 | } |
| 316 | |
| 317 | void ConstantMultiFieldModifier::getParameters(Parameters& output) const { |
| 318 | output = fParameters; |
| 319 | } |
| 320 | |
| 321 | bool ConstantMultiFieldModifier::semanticallyEquivalent(const Modifier& other) const { |
| 322 | auto* _other = dynamic_cast<const ConstantMultiFieldModifier*>(&other); |
| 323 | if (_other == nullptr) { |
| 324 | return false; |
| 325 | } |
| 326 | if (fParameters.obj != nullptr) { |
| 327 | return fParameters.obj == _other->fParameters.obj; |
| 328 | } |
| 329 | return fPrefix.contentEquals(_other->fPrefix) |
| 330 | && fSuffix.contentEquals(_other->fSuffix) |
| 331 | && fOverwrite == _other->fOverwrite |
| 332 | && fStrong == _other->fStrong; |
| 333 | } |
| 334 | |
| 335 | |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 336 | CurrencySpacingEnabledModifier::CurrencySpacingEnabledModifier(const FormattedStringBuilder &prefix, |
| 337 | const FormattedStringBuilder &suffix, |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 338 | bool overwrite, |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 339 | bool strong, |
| 340 | const DecimalFormatSymbols &symbols, |
| 341 | UErrorCode &status) |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 342 | : ConstantMultiFieldModifier(prefix, suffix, overwrite, strong) { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 343 | // Check for currency spacing. Do not build the UnicodeSets unless there is |
| 344 | // a currency code point at a boundary. |
Frank Tang | f222396 | 2020-04-27 18:25:29 -0700 | [diff] [blame] | 345 | if (prefix.length() > 0 && prefix.fieldAt(prefix.length() - 1) == Field(UFIELD_CATEGORY_NUMBER, UNUM_CURRENCY_FIELD)) { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 346 | int prefixCp = prefix.getLastCodePoint(); |
| 347 | UnicodeSet prefixUnicodeSet = getUnicodeSet(symbols, IN_CURRENCY, PREFIX, status); |
| 348 | if (prefixUnicodeSet.contains(prefixCp)) { |
| 349 | fAfterPrefixUnicodeSet = getUnicodeSet(symbols, IN_NUMBER, PREFIX, status); |
| 350 | fAfterPrefixUnicodeSet.freeze(); |
| 351 | fAfterPrefixInsert = getInsertString(symbols, PREFIX, status); |
| 352 | } else { |
| 353 | fAfterPrefixUnicodeSet.setToBogus(); |
| 354 | fAfterPrefixInsert.setToBogus(); |
| 355 | } |
| 356 | } else { |
| 357 | fAfterPrefixUnicodeSet.setToBogus(); |
| 358 | fAfterPrefixInsert.setToBogus(); |
| 359 | } |
Frank Tang | f222396 | 2020-04-27 18:25:29 -0700 | [diff] [blame] | 360 | if (suffix.length() > 0 && suffix.fieldAt(0) == Field(UFIELD_CATEGORY_NUMBER, UNUM_CURRENCY_FIELD)) { |
| 361 | int suffixCp = suffix.getFirstCodePoint(); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 362 | UnicodeSet suffixUnicodeSet = getUnicodeSet(symbols, IN_CURRENCY, SUFFIX, status); |
| 363 | if (suffixUnicodeSet.contains(suffixCp)) { |
| 364 | fBeforeSuffixUnicodeSet = getUnicodeSet(symbols, IN_NUMBER, SUFFIX, status); |
| 365 | fBeforeSuffixUnicodeSet.freeze(); |
| 366 | fBeforeSuffixInsert = getInsertString(symbols, SUFFIX, status); |
| 367 | } else { |
| 368 | fBeforeSuffixUnicodeSet.setToBogus(); |
| 369 | fBeforeSuffixInsert.setToBogus(); |
| 370 | } |
| 371 | } else { |
| 372 | fBeforeSuffixUnicodeSet.setToBogus(); |
| 373 | fBeforeSuffixInsert.setToBogus(); |
| 374 | } |
| 375 | } |
| 376 | |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 377 | int32_t CurrencySpacingEnabledModifier::apply(FormattedStringBuilder &output, int leftIndex, int rightIndex, |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 378 | UErrorCode &status) const { |
| 379 | // Currency spacing logic |
| 380 | int length = 0; |
| 381 | if (rightIndex - leftIndex > 0 && !fAfterPrefixUnicodeSet.isBogus() && |
| 382 | fAfterPrefixUnicodeSet.contains(output.codePointAt(leftIndex))) { |
| 383 | // TODO: Should we use the CURRENCY field here? |
Frank Tang | f222396 | 2020-04-27 18:25:29 -0700 | [diff] [blame] | 384 | length += output.insert( |
| 385 | leftIndex, |
| 386 | fAfterPrefixInsert, |
| 387 | kUndefinedField, |
| 388 | status); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 389 | } |
| 390 | if (rightIndex - leftIndex > 0 && !fBeforeSuffixUnicodeSet.isBogus() && |
| 391 | fBeforeSuffixUnicodeSet.contains(output.codePointBefore(rightIndex))) { |
| 392 | // TODO: Should we use the CURRENCY field here? |
Frank Tang | f222396 | 2020-04-27 18:25:29 -0700 | [diff] [blame] | 393 | length += output.insert( |
| 394 | rightIndex + length, |
| 395 | fBeforeSuffixInsert, |
| 396 | kUndefinedField, |
| 397 | status); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | // Call super for the remaining logic |
| 401 | length += ConstantMultiFieldModifier::apply(output, leftIndex, rightIndex + length, status); |
| 402 | return length; |
| 403 | } |
| 404 | |
| 405 | int32_t |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 406 | CurrencySpacingEnabledModifier::applyCurrencySpacing(FormattedStringBuilder &output, int32_t prefixStart, |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 407 | int32_t prefixLen, int32_t suffixStart, |
| 408 | int32_t suffixLen, |
| 409 | const DecimalFormatSymbols &symbols, |
| 410 | UErrorCode &status) { |
| 411 | int length = 0; |
| 412 | bool hasPrefix = (prefixLen > 0); |
| 413 | bool hasSuffix = (suffixLen > 0); |
| 414 | bool hasNumber = (suffixStart - prefixStart - prefixLen > 0); // could be empty string |
| 415 | if (hasPrefix && hasNumber) { |
| 416 | length += applyCurrencySpacingAffix(output, prefixStart + prefixLen, PREFIX, symbols, status); |
| 417 | } |
| 418 | if (hasSuffix && hasNumber) { |
| 419 | length += applyCurrencySpacingAffix(output, suffixStart + length, SUFFIX, symbols, status); |
| 420 | } |
| 421 | return length; |
| 422 | } |
| 423 | |
| 424 | int32_t |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 425 | CurrencySpacingEnabledModifier::applyCurrencySpacingAffix(FormattedStringBuilder &output, int32_t index, |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 426 | EAffix affix, |
| 427 | const DecimalFormatSymbols &symbols, |
| 428 | UErrorCode &status) { |
| 429 | // NOTE: For prefix, output.fieldAt(index-1) gets the last field type in the prefix. |
| 430 | // This works even if the last code point in the prefix is 2 code units because the |
| 431 | // field value gets populated to both indices in the field array. |
| 432 | Field affixField = (affix == PREFIX) ? output.fieldAt(index - 1) : output.fieldAt(index); |
Frank Tang | f222396 | 2020-04-27 18:25:29 -0700 | [diff] [blame] | 433 | if (affixField != Field(UFIELD_CATEGORY_NUMBER, UNUM_CURRENCY_FIELD)) { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 434 | return 0; |
| 435 | } |
| 436 | int affixCp = (affix == PREFIX) ? output.codePointBefore(index) : output.codePointAt(index); |
| 437 | UnicodeSet affixUniset = getUnicodeSet(symbols, IN_CURRENCY, affix, status); |
| 438 | if (!affixUniset.contains(affixCp)) { |
| 439 | return 0; |
| 440 | } |
| 441 | int numberCp = (affix == PREFIX) ? output.codePointAt(index) : output.codePointBefore(index); |
| 442 | UnicodeSet numberUniset = getUnicodeSet(symbols, IN_NUMBER, affix, status); |
| 443 | if (!numberUniset.contains(numberCp)) { |
| 444 | return 0; |
| 445 | } |
| 446 | UnicodeString spacingString = getInsertString(symbols, affix, status); |
| 447 | |
| 448 | // NOTE: This next line *inserts* the spacing string, triggering an arraycopy. |
| 449 | // It would be more efficient if this could be done before affixes were attached, |
| 450 | // so that it could be prepended/appended instead of inserted. |
| 451 | // However, the build code path is more efficient, and this is the most natural |
| 452 | // place to put currency spacing in the non-build code path. |
| 453 | // TODO: Should we use the CURRENCY field here? |
Frank Tang | f222396 | 2020-04-27 18:25:29 -0700 | [diff] [blame] | 454 | return output.insert(index, spacingString, kUndefinedField, status); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | UnicodeSet |
| 458 | CurrencySpacingEnabledModifier::getUnicodeSet(const DecimalFormatSymbols &symbols, EPosition position, |
| 459 | EAffix affix, UErrorCode &status) { |
| 460 | // Ensure the static defaults are initialized: |
| 461 | umtx_initOnce(gDefaultCurrencySpacingInitOnce, &initDefaultCurrencySpacing, status); |
| 462 | if (U_FAILURE(status)) { |
| 463 | return UnicodeSet(); |
| 464 | } |
| 465 | |
| 466 | const UnicodeString& pattern = symbols.getPatternForCurrencySpacing( |
| 467 | position == IN_CURRENCY ? UNUM_CURRENCY_MATCH : UNUM_CURRENCY_SURROUNDING_MATCH, |
| 468 | affix == SUFFIX, |
| 469 | status); |
| 470 | if (pattern.compare(u"[:digit:]", -1) == 0) { |
| 471 | return *UNISET_DIGIT; |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 472 | } else if (pattern.compare(u"[[:^S:]&[:^Z:]]", -1) == 0) { |
| 473 | return *UNISET_NOTSZ; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 474 | } else { |
| 475 | return UnicodeSet(pattern, status); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | UnicodeString |
| 480 | CurrencySpacingEnabledModifier::getInsertString(const DecimalFormatSymbols &symbols, EAffix affix, |
| 481 | UErrorCode &status) { |
| 482 | return symbols.getPatternForCurrencySpacing(UNUM_CURRENCY_INSERT, affix == SUFFIX, status); |
| 483 | } |
| 484 | |
| 485 | #endif /* #if !UCONFIG_NO_FORMATTING */ |