clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -O2 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name nfrule.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -ffp-contract=off -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/config/external/icu/i18n -fcoverage-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/config/external/icu/i18n -resource-dir /usr/lib/llvm-23/lib/clang/23 -include /root/firefox-clang/config/gcc_hidden.h -include /root/firefox-clang/obj-x86_64-pc-linux-gnu/mozilla-config.h -D _GLIBCXX_ASSERTIONS=1 -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/system_wrappers -U _FORTIFY_SOURCE -D _FORTIFY_SOURCE=2 -D DEBUG=1 -D U_I18N_IMPLEMENTATION -D _LIBCPP_DISABLE_DEPRECATION_WARNINGS -D U_USING_ICU_NAMESPACE=0 -D U_NO_DEFAULT_INCLUDE_UTF_HEADERS=1 -D U_HIDE_OBSOLETE_UTF_OLD_H=1 -D UCONFIG_NO_LEGACY_CONVERSION -D UCONFIG_NO_TRANSLITERATION -D UCONFIG_NO_REGULAR_EXPRESSIONS -D UCONFIG_NO_BREAK_ITERATION -D UCONFIG_NO_IDNA -D UCONFIG_NO_MF2 -D UCONFIG_NO_NORMALIZATION -D UCONFIG_NO_COLLATION -D U_CHARSET_IS_UTF8 -D UNISTR_FROM_CHAR_EXPLICIT=explicit -D UNISTR_FROM_STRING_EXPLICIT=explicit -D U_ENABLE_DYLOAD=0 -D U_DEBUG=1 -I /root/firefox-clang/config/external/icu/i18n -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/config/external/icu/i18n -I /root/firefox-clang/intl/icu/source/common -I /root/firefox-clang/mfbt/double-conversion -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nspr -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nss -D MOZILLA_CLIENT -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/x86_64-linux-gnu/c++/16 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16/backward -internal-isystem /usr/lib/llvm-23/lib/clang/23/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-error=pessimizing-move -Wno-error=large-by-value-copy=128 -Wno-error=implicit-int-float-conversion -Wno-error=thread-safety-analysis -Wno-error=tautological-type-limit-compare -Wno-invalid-offsetof -Wno-range-loop-analysis -Wno-deprecated-anon-enum-enum-conversion -Wno-deprecated-enum-enum-conversion -Wno-inline-new-delete -Wno-error=deprecated-declarations -Wno-error=array-bounds -Wno-error=free-nonheap-object -Wno-error=atomic-alignment -Wno-error=deprecated-builtins -Wno-psabi -Wno-error=builtin-macro-redefined -Wno-vla-cxx-extension -Wno-unknown-warning-option -Wno-character-conversion -Wno-comma -Wno-implicit-const-int-float-conversion -Wno-macro-redefined -Wno-microsoft-include -Wno-tautological-unsigned-enum-zero-compare -Wno-unreachable-code-loop-increment -Wno-unreachable-code-return -std=gnu++20 -fdeprecated-macro -ferror-limit 19 -fstrict-flex-arrays=1 -stack-protector 2 -fstack-clash-protection -ftrivial-auto-var-init=pattern -fgnuc-version=4.2.1 -fno-implicit-modules -fskip-odr-check-in-gmf -fno-sized-deallocation -fno-aligned-allocation -fdiagnostics-absolute-paths -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -mllvm -dwarf-linkage-names=Abstract -faddrsig -fdwarf2-cfi-asm -o /tmp/scan-build-2026-09-01-224014-2642839-1 -x c++ /root/firefox-clang/intl/icu/source/i18n/nfrule.cpp
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | #include "nfrule.h" |
| 19 | |
| 20 | #if U_HAVE_RBNF |
| 21 | |
| 22 | #include "unicode/localpointer.h" |
| 23 | #include "unicode/rbnf.h" |
| 24 | #include "unicode/tblcoll.h" |
| 25 | #include "unicode/plurfmt.h" |
| 26 | #include "unicode/upluralrules.h" |
| 27 | #include "unicode/coleitr.h" |
| 28 | #include "unicode/uchar.h" |
| 29 | #include "nfrs.h" |
| 30 | #include "nfrlist.h" |
| 31 | #include "nfsubs.h" |
| 32 | #include "patternprops.h" |
| 33 | #include "putilimp.h" |
| 34 | |
| 35 | U_NAMESPACE_BEGIN |
| 36 | |
| 37 | NFRule::NFRule(const RuleBasedNumberFormat* _rbnf, const UnicodeString &_ruleText, UErrorCode &status) |
| 38 | : baseValue(static_cast<int32_t>(0)) |
| 39 | , radix(10) |
| 40 | , exponent(0) |
| 41 | , decimalPoint(0) |
| 42 | , fRuleText(_ruleText) |
| 43 | , sub1(nullptr) |
| 44 | , sub2(nullptr) |
| 45 | , formatter(_rbnf) |
| 46 | , rulePatternFormat(nullptr) |
| 47 | { |
| 48 | if (!fRuleText.isEmpty()) { |
| 49 | parseRuleDescriptor(fRuleText, status); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | NFRule::~NFRule() |
| 54 | { |
| 55 | if (sub1 != sub2) { |
| 56 | delete sub2; |
| 57 | sub2 = nullptr; |
| 58 | } |
| 59 | delete sub1; |
| 60 | sub1 = nullptr; |
| 61 | delete rulePatternFormat; |
| 62 | rulePatternFormat = nullptr; |
| 63 | } |
| 64 | |
| 65 | static const char16_t gLeftBracket = 0x005b; |
| 66 | static const char16_t gRightBracket = 0x005d; |
| 67 | static const char16_t gVerticalLine = 0x007C; |
| 68 | static const char16_t gColon = 0x003a; |
| 69 | static const char16_t gZero = 0x0030; |
| 70 | static const char16_t gNine = 0x0039; |
| 71 | static const char16_t gSpace = 0x0020; |
| 72 | static const char16_t gSlash = 0x002f; |
| 73 | static const char16_t gGreaterThan = 0x003e; |
| 74 | static const char16_t gLessThan = 0x003c; |
| 75 | static const char16_t gComma = 0x002c; |
| 76 | static const char16_t gDot = 0x002e; |
| 77 | static const char16_t gTick = 0x0027; |
| 78 | |
| 79 | static const char16_t gSemicolon = 0x003b; |
| 80 | static const char16_t gX = 0x0078; |
| 81 | |
| 82 | static const char16_t gMinusX[] = {0x2D, 0x78, 0}; |
| 83 | static const char16_t gInf[] = {0x49, 0x6E, 0x66, 0}; |
| 84 | static const char16_t gNaN[] = {0x4E, 0x61, 0x4E, 0}; |
| 85 | |
| 86 | static const char16_t gDollarOpenParenthesis[] = {0x24, 0x28, 0}; |
| 87 | static const char16_t gClosedParenthesisDollar[] = {0x29, 0x24, 0}; |
| 88 | |
| 89 | static const char16_t gLessLess[] = {0x3C, 0x3C, 0}; |
| 90 | static const char16_t gLessPercent[] = {0x3C, 0x25, 0}; |
| 91 | static const char16_t gLessHash[] = {0x3C, 0x23, 0}; |
| 92 | static const char16_t gLessZero[] = {0x3C, 0x30, 0}; |
| 93 | static const char16_t gGreaterGreater[] = {0x3E, 0x3E, 0}; |
| 94 | static const char16_t gGreaterPercent[] = {0x3E, 0x25, 0}; |
| 95 | static const char16_t gGreaterHash[] = {0x3E, 0x23, 0}; |
| 96 | static const char16_t gGreaterZero[] = {0x3E, 0x30, 0}; |
| 97 | static const char16_t gEqualPercent[] = {0x3D, 0x25, 0}; |
| 98 | static const char16_t gEqualHash[] = {0x3D, 0x23, 0}; |
| 99 | static const char16_t gEqualZero[] = {0x3D, 0x30, 0}; |
| 100 | static const char16_t gGreaterGreaterGreater[] = {0x3E, 0x3E, 0x3E, 0}; |
| 101 | |
| 102 | static const char16_t * const RULE_PREFIXES[] = { |
| 103 | gLessLess, gLessPercent, gLessHash, gLessZero, |
| 104 | gGreaterGreater, gGreaterPercent,gGreaterHash, gGreaterZero, |
| 105 | gEqualPercent, gEqualHash, gEqualZero, nullptr |
| 106 | }; |
| 107 | |
| 108 | void |
| 109 | NFRule::makeRules(UnicodeString& description, |
| 110 | NFRuleSet *owner, |
| 111 | const NFRule *predecessor, |
| 112 | const RuleBasedNumberFormat *rbnf, |
| 113 | NFRuleList& rules, |
| 114 | UErrorCode& status) |
| 115 | { |
| 116 | if (U_FAILURE(status)) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | LocalPointer<NFRule> rule1(new NFRule(rbnf, description, status)); |
| 124 | if (U_FAILURE(status)) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | if (rule1.isNull()) { |
| 129 | status = U_MEMORY_ALLOCATION_ERROR; |
| 130 | return; |
| 131 | } |
| 132 | description = rule1->fRuleText; |
| 133 | |
| 134 | |
| 135 | |
| 136 | int32_t brack1 = description.indexOf(gLeftBracket); |
| 137 | int32_t brack2 = brack1 < 0 ? -1 : description.indexOf(gRightBracket); |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | if (brack2 < 0 || brack1 > brack2 |
| 144 | || rule1->getType() == kProperFractionRule |
| 145 | || rule1->getType() == kNegativeNumberRule |
| 146 | || rule1->getType() == kInfinityRule |
| 147 | || rule1->getType() == kNaNRule) |
| 148 | { |
| 149 | rule1->extractSubstitutions(owner, description, predecessor, status); |
| 150 | if (U_FAILURE(status)) { |
| 151 | return; |
| 152 | } |
| 153 | } |
| 154 | else { |
| 155 | |
| 156 | |
| 157 | LocalPointer<NFRule> rule2; |
| 158 | UnicodeString sbuf; |
| 159 | int32_t orElseOp = description.indexOf(gVerticalLine); |
| 160 | |
| 161 | uint64_t mod = util64_pow(rule1->radix, rule1->exponent); |
| 162 | |
| 163 | |
| 164 | |
| 165 | if (rule1->baseValue > 0 && rule1->radix != 0 && mod == 0) { |
| 166 | status = U_NUMBER_ARG_OUTOFBOUNDS_ERROR; |
| 167 | return; |
| 168 | } |
| 169 | if ((rule1->baseValue > 0 |
| 170 | && (rule1->radix != 0) |
| 171 | && (rule1->baseValue % mod == 0)) |
| 172 | || rule1->getType() == kImproperFractionRule |
| 173 | || rule1->getType() == kDefaultRule) { |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | rule2.adoptInstead(new NFRule(rbnf, UnicodeString(), status)); |
| 181 | if (U_FAILURE(status)) { |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | if (rule2.isNull()) { |
| 186 | status = U_MEMORY_ALLOCATION_ERROR; |
| 187 | return; |
| 188 | } |
| 189 | if (rule1->baseValue >= 0) { |
| 190 | rule2->baseValue = rule1->baseValue; |
| 191 | if (!owner->isFractionRuleSet()) { |
| 192 | ++rule1->baseValue; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | else if (rule1->getType() == kImproperFractionRule) { |
| 200 | rule2->setType(kProperFractionRule); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | else if (rule1->getType() == kDefaultRule) { |
| 207 | rule2->baseValue = rule1->baseValue; |
| 208 | rule1->setType(kImproperFractionRule); |
| 209 | } |
| 210 | |
| 211 | |
| 212 | |
| 213 | rule2->radix = rule1->radix; |
| 214 | rule2->exponent = rule1->exponent; |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | sbuf.append(description, 0, brack1); |
| 220 | if (orElseOp >= 0) { |
| 221 | sbuf.append(description, orElseOp + 1, brack2 - orElseOp - 1); |
| 222 | } |
| 223 | if (brack2 + 1 < description.length()) { |
| 224 | sbuf.append(description, brack2 + 1, description.length() - brack2 - 1); |
| 225 | } |
| 226 | rule2->extractSubstitutions(owner, sbuf, predecessor, status); |
| 227 | if (U_FAILURE(status)) { |
| 228 | return; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | sbuf.setTo(description, 0, brack1); |
| 236 | if (orElseOp >= 0) { |
| 237 | sbuf.append(description, brack1 + 1, orElseOp - brack1 - 1); |
| 238 | } |
| 239 | else { |
| 240 | sbuf.append(description, brack1 + 1, brack2 - brack1 - 1); |
| 241 | } |
| 242 | if (brack2 + 1 < description.length()) { |
| 243 | sbuf.append(description, brack2 + 1, description.length() - brack2 - 1); |
| 244 | } |
| 245 | rule1->extractSubstitutions(owner, sbuf, predecessor, status); |
| 246 | if (U_FAILURE(status)) { |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | if (!rule2.isNull()) { |
| 256 | if (rule2->baseValue >= kNoBase) { |
| 257 | rules.add(rule2.orphan()); |
| 258 | } |
| 259 | else { |
| 260 | owner->setNonNumericalRule(rule2.orphan()); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | if (rule1->baseValue >= kNoBase) { |
| 265 | rules.add(rule1.orphan()); |
| 266 | } |
| 267 | else { |
| 268 | owner->setNonNumericalRule(rule1.orphan()); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | void |
| 285 | NFRule::parseRuleDescriptor(UnicodeString& description, UErrorCode& status) |
| 286 | { |
| 287 | |
| 288 | |
| 289 | |
| 290 | int32_t p = description.indexOf(gColon); |
| 291 | if (p != -1) { |
| 292 | |
| 293 | |
| 294 | |
| 295 | UnicodeString descriptor; |
| 296 | descriptor.setTo(description, 0, p); |
| 297 | |
| 298 | ++p; |
| 299 | while (p < description.length() && PatternProps::isWhiteSpace(description.charAt(p))) { |
| 300 | ++p; |
| 301 | } |
| 302 | description.removeBetween(0, p); |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | int descriptorLength = descriptor.length(); |
| 308 | char16_t firstChar = descriptor.charAt(0); |
| 309 | char16_t lastChar = descriptor.charAt(descriptorLength - 1); |
| 310 | if (firstChar >= gZero && firstChar <= gNine && lastChar != gX) { |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | int64_t val = 0; |
| 316 | p = 0; |
| 317 | char16_t c = gSpace; |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | while (p < descriptorLength) { |
| 324 | c = descriptor.charAt(p); |
| 325 | if (c >= gZero && c <= gNine) { |
| 326 | int64_t digit = static_cast<int64_t>(c - gZero); |
| 327 | if ((val > 0 && val > (INT64_MAX - digit) / 10) || |
| 328 | (val < 0 && val < (INT64_MIN - digit) / 10)) { |
| 329 | |
| 330 | status = U_PARSE_ERROR; |
| 331 | return; |
| 332 | } |
| 333 | val = val * 10 + digit; |
| 334 | } |
| 335 | else if (c == gSlash || c == gGreaterThan) { |
| 336 | break; |
| 337 | } |
| 338 | else if (PatternProps::isWhiteSpace(c) || c == gComma || c == gDot) { |
| 339 | } |
| 340 | else { |
| 341 | |
| 342 | status = U_PARSE_ERROR; |
| 343 | return; |
| 344 | } |
| 345 | ++p; |
| 346 | } |
| 347 | |
| 348 | |
| 349 | setBaseValue(val, status); |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | if (c == gSlash) { |
| 356 | val = 0; |
| 357 | ++p; |
| 358 | while (p < descriptorLength) { |
| 359 | c = descriptor.charAt(p); |
| 360 | if (c >= gZero && c <= gNine) { |
| 361 | int64_t digit = static_cast<int64_t>(c - gZero); |
| 362 | if ((val > 0 && val > (INT64_MAX - digit) / 10) || |
| 363 | (val < 0 && val < (INT64_MIN - digit) / 10)) { |
| 364 | |
| 365 | status = U_PARSE_ERROR; |
| 366 | return; |
| 367 | } |
| 368 | val = val * 10 + digit; |
| 369 | } |
| 370 | else if (c == gGreaterThan) { |
| 371 | break; |
| 372 | } |
| 373 | else if (PatternProps::isWhiteSpace(c) || c == gComma || c == gDot) { |
| 374 | } |
| 375 | else { |
| 376 | |
| 377 | status = U_PARSE_ERROR; |
| 378 | return; |
| 379 | } |
| 380 | ++p; |
| 381 | } |
| 382 | |
| 383 | |
| 384 | |
| 385 | radix = static_cast<int32_t>(val); |
| 386 | if (radix == 0) { |
| 387 | |
| 388 | status = U_PARSE_ERROR; |
| 389 | } |
| 390 | |
| 391 | exponent = expectedExponent(); |
| 392 | } |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | if (c == gGreaterThan) { |
| 400 | while (p < descriptor.length()) { |
| 401 | c = descriptor.charAt(p); |
| 402 | if (c == gGreaterThan && exponent > 0) { |
| 403 | --exponent; |
| 404 | } else { |
| 405 | |
| 406 | status = U_PARSE_ERROR; |
| 407 | return; |
| 408 | } |
| 409 | ++p; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | else if (0 == descriptor.compare(gMinusX, 2)) { |
| 414 | setType(kNegativeNumberRule); |
| 415 | } |
| 416 | else if (descriptorLength == 3) { |
| 417 | if (firstChar == gZero && lastChar == gX) { |
| 418 | setBaseValue(kProperFractionRule, status); |
| 419 | decimalPoint = descriptor.charAt(1); |
| 420 | } |
| 421 | else if (firstChar == gX && lastChar == gX) { |
| 422 | setBaseValue(kImproperFractionRule, status); |
| 423 | decimalPoint = descriptor.charAt(1); |
| 424 | } |
| 425 | else if (firstChar == gX && lastChar == gZero) { |
| 426 | setBaseValue(kDefaultRule, status); |
| 427 | decimalPoint = descriptor.charAt(1); |
| 428 | } |
| 429 | else if (descriptor.compare(gNaN, 3) == 0) { |
| 430 | setBaseValue(kNaNRule, status); |
| 431 | } |
| 432 | else if (descriptor.compare(gInf, 3) == 0) { |
| 433 | setBaseValue(kInfinityRule, status); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | if (!description.isEmpty() && description.charAt(0) == gTick) { |
| 443 | description.removeBetween(0, 1); |
| 444 | } |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | } |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | void |
| 460 | NFRule::extractSubstitutions(const NFRuleSet* ruleSet, |
| 461 | const UnicodeString &ruleText, |
| 462 | const NFRule* predecessor, |
| 463 | UErrorCode& status) |
| 464 | { |
| 465 | if (U_FAILURE(status)) { |
| 466 | return; |
| 467 | } |
| 468 | fRuleText = ruleText; |
| 469 | sub1 = extractSubstitution(ruleSet, predecessor, status); |
| 470 | if (sub1 == nullptr) { |
| 471 | |
| 472 | sub2 = nullptr; |
| 473 | } |
| 474 | else { |
| 475 | sub2 = extractSubstitution(ruleSet, predecessor, status); |
| 476 | } |
| 477 | int32_t pluralRuleStart = fRuleText.indexOf(gDollarOpenParenthesis, -1, 0); |
| 478 | int32_t pluralRuleEnd = (pluralRuleStart >= 0 ? fRuleText.indexOf(gClosedParenthesisDollar, -1, pluralRuleStart) : -1); |
| 479 | if (pluralRuleEnd >= 0) { |
| 480 | int32_t endType = fRuleText.indexOf(gComma, pluralRuleStart); |
| 481 | if (endType < 0) { |
| 482 | status = U_PARSE_ERROR; |
| 483 | return; |
| 484 | } |
| 485 | UnicodeString type(fRuleText.tempSubString(pluralRuleStart + 2, endType - pluralRuleStart - 2)); |
| 486 | UPluralType pluralType; |
| 487 | if (type.startsWith(UNICODE_STRING_SIMPLE("cardinal"))) { |
| 488 | pluralType = UPLURAL_TYPE_CARDINAL; |
| 489 | } |
| 490 | else if (type.startsWith(UNICODE_STRING_SIMPLE("ordinal"))) { |
| 491 | pluralType = UPLURAL_TYPE_ORDINAL; |
| 492 | } |
| 493 | else { |
| 494 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 495 | return; |
| 496 | } |
| 497 | rulePatternFormat = formatter->createPluralFormat(pluralType, |
| 498 | fRuleText.tempSubString(endType + 1, pluralRuleEnd - endType - 1), status); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | NFSubstitution * |
| 515 | NFRule::extractSubstitution(const NFRuleSet* ruleSet, |
| 516 | const NFRule* predecessor, |
| 517 | UErrorCode& status) |
| 518 | { |
| 519 | NFSubstitution* result = nullptr; |
| 520 | |
| 521 | |
| 522 | |
| 523 | int32_t subStart = indexOfAnyRulePrefix(); |
| 524 | int32_t subEnd = subStart; |
| 525 | |
| 526 | |
| 527 | |
| 528 | if (subStart == -1) { |
| 529 | return nullptr; |
| 530 | } |
| 531 | |
| 532 | |
| 533 | |
| 534 | if (fRuleText.indexOf(gGreaterGreaterGreater, 3, 0) == subStart) { |
| 535 | subEnd = subStart + 2; |
| 536 | |
| 537 | |
| 538 | |
| 539 | } else { |
| 540 | char16_t c = fRuleText.charAt(subStart); |
| 541 | subEnd = fRuleText.indexOf(c, subStart + 1); |
| 542 | |
| 543 | if (c == gLessThan && subEnd != -1 && subEnd < fRuleText.length() - 1 && fRuleText.charAt(subEnd+1) == c) { |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | ++subEnd; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | if (subEnd == -1) { |
| 556 | return nullptr; |
| 557 | } |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | UnicodeString subToken; |
| 563 | subToken.setTo(fRuleText, subStart, subEnd + 1 - subStart); |
| 564 | result = NFSubstitution::makeSubstitution(subStart, this, predecessor, ruleSet, |
| 565 | this->formatter, subToken, status); |
| 566 | |
| 567 | |
| 568 | fRuleText.removeBetween(subStart, subEnd+1); |
| 569 | |
| 570 | return result; |
| 571 | } |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | void |
| 581 | NFRule::setBaseValue(int64_t newBaseValue, UErrorCode& status) |
| 582 | { |
| 583 | |
| 584 | baseValue = newBaseValue; |
| 585 | radix = 10; |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | if (baseValue >= 1) { |
| 593 | exponent = expectedExponent(); |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | if (sub1 != nullptr) { |
| 600 | sub1->setDivisor(radix, exponent, status); |
| 601 | } |
| 602 | if (sub2 != nullptr) { |
| 603 | sub2->setDivisor(radix, exponent, status); |
| 604 | } |
| 605 | |
| 606 | |
| 607 | |
| 608 | } else { |
| 609 | exponent = 0; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | int16_t |
| 619 | NFRule::expectedExponent() const |
| 620 | { |
| 621 | |
| 622 | |
| 623 | |
| 624 | if (radix == 0 || baseValue < 1) { |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | int16_t tempResult = static_cast<int16_t>(uprv_log(static_cast<double>(baseValue)) / |
| 632 | uprv_log(static_cast<double>(radix))); |
| 633 | int64_t temp = util64_pow(radix, tempResult + 1); |
| 634 | if (temp <= baseValue) { |
| 635 | tempResult += 1; |
| 636 | } |
| 637 | return tempResult; |
| 638 | } |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | int32_t |
| 648 | NFRule::indexOfAnyRulePrefix() const |
| 649 | { |
| 650 | int result = -1; |
| 651 | for (int i = 0; RULE_PREFIXES[i]; i++) { |
| 652 | int32_t pos = fRuleText.indexOf(*RULE_PREFIXES[i]); |
| 653 | if (pos != -1 && (result == -1 || pos < result)) { |
| 654 | result = pos; |
| 655 | } |
| 656 | } |
| 657 | return result; |
| 658 | } |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | static UBool |
| 665 | util_equalSubstitutions(const NFSubstitution* sub1, const NFSubstitution* sub2) |
| 666 | { |
| 667 | if (sub1) { |
| 668 | if (sub2) { |
| 669 | return *sub1 == *sub2; |
| 670 | } |
| 671 | } else if (!sub2) { |
| 672 | return true; |
| 673 | } |
| 674 | return false; |
| 675 | } |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | bool |
| 683 | NFRule::operator==(const NFRule& rhs) const |
| 684 | { |
| 685 | return baseValue == rhs.baseValue |
| 686 | && radix == rhs.radix |
| 687 | && exponent == rhs.exponent |
| 688 | && fRuleText == rhs.fRuleText |
| 689 | && util_equalSubstitutions(sub1, rhs.sub1) |
| 690 | && util_equalSubstitutions(sub2, rhs.sub2); |
| 691 | } |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | static void util_append64(UnicodeString& result, int64_t n) |
| 700 | { |
| 701 | char16_t buffer[256]; |
| 702 | int32_t len = util64_tou(n, buffer, sizeof(buffer)); |
| 703 | UnicodeString temp(buffer, len); |
| 704 | result.append(temp); |
| 705 | } |
| 706 | |
| 707 | void |
| 708 | NFRule::_appendRuleText(UnicodeString& result) const |
| 709 | { |
| 710 | switch (getType()) { |
| 711 | case kNegativeNumberRule: result.append(gMinusX, 2); break; |
| 712 | case kImproperFractionRule: result.append(gX).append(decimalPoint == 0 ? gDot : decimalPoint).append(gX); break; |
| 713 | case kProperFractionRule: result.append(gZero).append(decimalPoint == 0 ? gDot : decimalPoint).append(gX); break; |
| 714 | case kDefaultRule: result.append(gX).append(decimalPoint == 0 ? gDot : decimalPoint).append(gZero); break; |
| 715 | case kInfinityRule: result.append(gInf, 3); break; |
| 716 | case kNaNRule: result.append(gNaN, 3); break; |
| 717 | default: |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | util_append64(result, baseValue); |
| 725 | if (radix != 10) { |
| 726 | result.append(gSlash); |
| 727 | util_append64(result, radix); |
| 728 | } |
| 729 | int numCarets = expectedExponent() - exponent; |
| 730 | for (int i = 0; i < numCarets; i++) { |
| 731 | result.append(gGreaterThan); |
| 732 | } |
| 733 | break; |
| 734 | } |
| 735 | result.append(gColon); |
| 736 | result.append(gSpace); |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | if (fRuleText.charAt(0) == gSpace && (sub1 == nullptr || sub1->getPos() != 0)) { |
| 742 | result.append(gTick); |
| 743 | } |
| 744 | |
| 745 | |
| 746 | |
| 747 | UnicodeString ruleTextCopy; |
| 748 | ruleTextCopy.setTo(fRuleText); |
| 749 | |
| 750 | UnicodeString temp; |
| 751 | if (sub2 != nullptr) { |
| 752 | sub2->toString(temp); |
| 753 | ruleTextCopy.insert(sub2->getPos(), temp); |
| 754 | } |
| 755 | if (sub1 != nullptr) { |
| 756 | sub1->toString(temp); |
| 757 | ruleTextCopy.insert(sub1->getPos(), temp); |
| 758 | } |
| 759 | |
| 760 | result.append(ruleTextCopy); |
| 761 | |
| 762 | |
| 763 | |
| 764 | result.append(gSemicolon); |
| 765 | } |
| 766 | |
| 767 | int64_t NFRule::getDivisor() const |
| 768 | { |
| 769 | return util64_pow(radix, exponent); |
| 770 | } |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | bool NFRule::hasModulusSubstitution() const |
| 776 | { |
| 777 | return (sub1 != nullptr && sub1->isModulusSubstitution()) || (sub2 != nullptr && sub2->isModulusSubstitution()); |
| 778 | } |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | void |
| 795 | NFRule::doFormat(int64_t number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const |
| 796 | { |
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | int32_t pluralRuleStart = fRuleText.length(); |
| 803 | int32_t lengthOffset = 0; |
| 804 | if (!rulePatternFormat) { |
| 805 | toInsertInto.insert(pos, fRuleText); |
| 806 | } |
| 807 | else { |
| 808 | pluralRuleStart = fRuleText.indexOf(gDollarOpenParenthesis, -1, 0); |
| 809 | int pluralRuleEnd = fRuleText.indexOf(gClosedParenthesisDollar, -1, pluralRuleStart); |
| 810 | int initialLength = toInsertInto.length(); |
| 811 | if (pluralRuleEnd < fRuleText.length() - 1) { |
| 812 | toInsertInto.insert(pos, fRuleText.tempSubString(pluralRuleEnd + 2)); |
| 813 | } |
| 814 | toInsertInto.insert(pos, |
| 815 | rulePatternFormat->format(static_cast<int32_t>(number / util64_pow(radix, exponent)), status)); |
| 816 | if (pluralRuleStart > 0) { |
| 817 | toInsertInto.insert(pos, fRuleText.tempSubString(0, pluralRuleStart)); |
| 818 | } |
| 819 | lengthOffset = fRuleText.length() - (toInsertInto.length() - initialLength); |
| 820 | } |
| 821 | |
| 822 | if (sub2 != nullptr) { |
| 823 | sub2->doSubstitution(number, toInsertInto, pos - (sub2->getPos() > pluralRuleStart ? lengthOffset : 0), recursionCount, status); |
| 824 | } |
| 825 | if (sub1 != nullptr) { |
| 826 | sub1->doSubstitution(number, toInsertInto, pos - (sub1->getPos() > pluralRuleStart ? lengthOffset : 0), recursionCount, status); |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | |
| 838 | |
| 839 | void |
| 840 | NFRule::doFormat(double number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const |
| 841 | { |
| 842 | |
| 843 | |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | int32_t pluralRuleStart = fRuleText.length(); |
| 849 | int32_t lengthOffset = 0; |
| 850 | if (!rulePatternFormat) { |
| 851 | toInsertInto.insert(pos, fRuleText); |
| 852 | } |
| 853 | else { |
| 854 | pluralRuleStart = fRuleText.indexOf(gDollarOpenParenthesis, -1, 0); |
| 855 | int pluralRuleEnd = fRuleText.indexOf(gClosedParenthesisDollar, -1, pluralRuleStart); |
| 856 | int initialLength = toInsertInto.length(); |
| 857 | if (pluralRuleEnd < fRuleText.length() - 1) { |
| 858 | toInsertInto.insert(pos, fRuleText.tempSubString(pluralRuleEnd + 2)); |
| 859 | } |
| 860 | double pluralVal = number; |
| 861 | if (0 <= pluralVal && pluralVal < 1) { |
| 862 | |
| 863 | |
| 864 | pluralVal = uprv_round(pluralVal * util64_pow(radix, exponent)); |
| 865 | } |
| 866 | else { |
| 867 | pluralVal = pluralVal / util64_pow(radix, exponent); |
| 868 | } |
| 869 | toInsertInto.insert(pos, rulePatternFormat->format(static_cast<int32_t>(pluralVal), status)); |
| 870 | if (pluralRuleStart > 0) { |
| 871 | toInsertInto.insert(pos, fRuleText.tempSubString(0, pluralRuleStart)); |
| 872 | } |
| 873 | lengthOffset = fRuleText.length() - (toInsertInto.length() - initialLength); |
| 874 | } |
| 875 | |
| 876 | if (sub2 != nullptr) { |
| 877 | sub2->doSubstitution(number, toInsertInto, pos - (sub2->getPos() > pluralRuleStart ? lengthOffset : 0), recursionCount, status); |
| 878 | } |
| 879 | if (sub1 != nullptr) { |
| 880 | sub1->doSubstitution(number, toInsertInto, pos - (sub1->getPos() > pluralRuleStart ? lengthOffset : 0), recursionCount, status); |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | |
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | |
| 891 | |
| 892 | UBool |
| 893 | NFRule::shouldRollBack(int64_t number) const |
| 894 | { |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
| 902 | |
| 903 | |
| 904 | |
| 905 | |
| 906 | |
| 907 | |
| 908 | |
| 909 | |
| 910 | |
| 911 | if ((sub1 != nullptr && sub1->isModulusSubstitution()) || (sub2 != nullptr && sub2->isModulusSubstitution())) { |
| 912 | int64_t re = util64_pow(radix, exponent); |
| 913 | return (number % re) == 0 && (baseValue % re) != 0; |
| 914 | } |
| 915 | return false; |
| 916 | } |
| 917 | |
| 918 | |
| 919 | |
| 920 | |
| 921 | |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | |
| 929 | |
| 930 | |
| 931 | |
| 932 | |
| 933 | |
| 934 | |
| 935 | |
| 936 | |
| 937 | |
| 938 | |
| 939 | |
| 940 | #ifdef RBNF_DEBUG |
| 941 | #include <stdio.h> |
| 942 | |
| 943 | static void dumpUS(FILE* f, const UnicodeString& us) { |
| 944 | int len = us.length(); |
| 945 | char* buf = (char *)uprv_malloc((len+1)*sizeof(char)); |
| 946 | if (buf != nullptr) { |
| 947 | us.extract(0, len, buf); |
| 948 | buf[len] = 0; |
| 949 | fprintf(f, "%s", buf); |
| 950 | uprv_free(buf); |
| 951 | } |
| 952 | } |
| 953 | #endif |
| 954 | UBool |
| 955 | NFRule::doParse(const UnicodeString& text, |
| 956 | ParsePosition& parsePosition, |
| 957 | UBool isFractionRule, |
| 958 | double upperBound, |
| 959 | uint32_t nonNumericalExecutedRuleMask, |
| 960 | int32_t recursionCount, |
| 961 | Formattable& resVal) const |
| 962 | { |
| 963 | |
| 964 | |
| 965 | ParsePosition pp; |
| 966 | UnicodeString workText(text); |
| 967 | |
| 968 | int32_t sub1Pos = sub1 != nullptr ? sub1->getPos() : fRuleText.length(); |
| 1 | Assuming pointer value is null | |
|
| |
| 969 | int32_t sub2Pos = sub2 != nullptr ? sub2->getPos() : fRuleText.length(); |
| 3 | | Assuming the condition is false | |
|
| |
| 970 | |
| 971 | |
| 972 | |
| 973 | |
| 974 | |
| 975 | UnicodeString prefix; |
| 976 | prefix.setTo(fRuleText, 0, sub1Pos); |
| 977 | |
| 978 | #ifdef RBNF_DEBUG |
| 979 | fprintf(stderr, "doParse %p ", this); |
| 980 | { |
| 981 | UnicodeString rt; |
| 982 | _appendRuleText(rt); |
| 983 | dumpUS(stderr, rt); |
| 984 | } |
| 985 | |
| 986 | fprintf(stderr, " text: '"); |
| 987 | dumpUS(stderr, text); |
| 988 | fprintf(stderr, "' prefix: '"); |
| 989 | dumpUS(stderr, prefix); |
| 990 | #endif |
| 991 | stripPrefix(workText, prefix, pp); |
| 992 | int32_t prefixLength = text.length() - workText.length(); |
| 993 | |
| 994 | #ifdef RBNF_DEBUG |
| 995 | fprintf(stderr, "' pl: %d ppi: %d s1p: %d\n", prefixLength, pp.getIndex(), sub1Pos); |
| 996 | #endif |
| 997 | |
| 998 | if (pp.getIndex() == 0 && sub1Pos != 0) { |
| 5 | | Assuming 'sub1Pos' is equal to 0 | |
|
| |
| 999 | |
| 1000 | |
| 1001 | parsePosition.setErrorIndex(pp.getErrorIndex()); |
| 1002 | resVal.setLong(0); |
| 1003 | return true; |
| 1004 | } |
| 1005 | if (baseValue == kInfinityRule) { |
| 7 | | Assuming field 'baseValue' is not equal to kInfinityRule | |
|
| |
| 1006 | |
| 1007 | parsePosition.setIndex(pp.getIndex()); |
| 1008 | resVal.setDouble(uprv_getInfinity()); |
| 1009 | return true; |
| 1010 | } |
| 1011 | if (baseValue == kNaNRule) { |
| 9 | | Assuming field 'baseValue' is not equal to kNaNRule | |
|
| |
| 1012 | |
| 1013 | parsePosition.setIndex(pp.getIndex()); |
| 1014 | resVal.setDouble(uprv_getNaN()); |
| 1015 | return true; |
| 1016 | } |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | |
| 1034 | |
| 1035 | |
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | |
| 1044 | |
| 1045 | |
| 1046 | |
| 1047 | int highWaterMark = 0; |
| 1048 | double result = 0; |
| 1049 | int start = 0; |
| 1050 | double tempBaseValue = static_cast<double>(baseValue <= 0 ? 0 : baseValue); |
| 11 | | Assuming field 'baseValue' is <= 0 | |
|
| |
| 1051 | |
| 1052 | UnicodeString temp; |
| 1053 | do { |
| 1054 | |
| 1055 | |
| 1056 | |
| 1057 | |
| 1058 | pp.setIndex(0); |
| 1059 | |
| 1060 | temp.setTo(fRuleText, sub1Pos, sub2Pos - sub1Pos); |
| 1061 | double partialResult = matchToDelimiter(workText, start, tempBaseValue, |
| 14 | | Calling 'NFRule::matchToDelimiter' | |
|
| 1062 | temp, pp, sub1, |
| 13 | | Passing null pointer value via 6th parameter 'sub' | |
|
| 1063 | nonNumericalExecutedRuleMask, |
| 1064 | recursionCount, |
| 1065 | upperBound); |
| 1066 | |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | |
| 1071 | if (pp.getIndex() != 0 || sub1 == nullptr) { |
| 1072 | start = pp.getIndex(); |
| 1073 | |
| 1074 | UnicodeString workText2; |
| 1075 | workText2.setTo(workText, pp.getIndex(), workText.length() - pp.getIndex()); |
| 1076 | ParsePosition pp2; |
| 1077 | |
| 1078 | |
| 1079 | |
| 1080 | |
| 1081 | |
| 1082 | temp.setTo(fRuleText, sub2Pos, fRuleText.length() - sub2Pos); |
| 1083 | partialResult = matchToDelimiter(workText2, 0, partialResult, |
| 1084 | temp, pp2, sub2, |
| 1085 | nonNumericalExecutedRuleMask, |
| 1086 | recursionCount, |
| 1087 | upperBound); |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | |
| 1092 | if (pp2.getIndex() != 0 || sub2 == nullptr) { |
| 1093 | if (prefixLength + pp.getIndex() + pp2.getIndex() > highWaterMark) { |
| 1094 | highWaterMark = prefixLength + pp.getIndex() + pp2.getIndex(); |
| 1095 | result = partialResult; |
| 1096 | } |
| 1097 | } |
| 1098 | else { |
| 1099 | |
| 1100 | |
| 1101 | int32_t i_temp = pp2.getErrorIndex() + sub1Pos + pp.getIndex(); |
| 1102 | if (i_temp> parsePosition.getErrorIndex()) { |
| 1103 | parsePosition.setErrorIndex(i_temp); |
| 1104 | } |
| 1105 | } |
| 1106 | } |
| 1107 | else { |
| 1108 | |
| 1109 | |
| 1110 | int32_t i_temp = sub1Pos + pp.getErrorIndex(); |
| 1111 | if (i_temp > parsePosition.getErrorIndex()) { |
| 1112 | parsePosition.setErrorIndex(i_temp); |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | |
| 1117 | |
| 1118 | } while (sub1Pos != sub2Pos |
| 1119 | && pp.getIndex() > 0 |
| 1120 | && pp.getIndex() < workText.length() |
| 1121 | && pp.getIndex() != start); |
| 1122 | |
| 1123 | |
| 1124 | |
| 1125 | |
| 1126 | |
| 1127 | parsePosition.setIndex(highWaterMark); |
| 1128 | |
| 1129 | |
| 1130 | if (highWaterMark > 0) { |
| 1131 | parsePosition.setErrorIndex(0); |
| 1132 | } |
| 1133 | |
| 1134 | |
| 1135 | |
| 1136 | |
| 1137 | |
| 1138 | |
| 1139 | |
| 1140 | if (isFractionRule && highWaterMark > 0 && sub1 == nullptr) { |
| 1141 | result = 1 / result; |
| 1142 | } |
| 1143 | |
| 1144 | resVal.setDouble(result); |
| 1145 | return true; |
| 1146 | } |
| 1147 | |
| 1148 | |
| 1149 | |
| 1150 | |
| 1151 | |
| 1152 | |
| 1153 | |
| 1154 | |
| 1155 | |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | |
| 1160 | |
| 1161 | |
| 1162 | |
| 1163 | |
| 1164 | void |
| 1165 | NFRule::stripPrefix(UnicodeString& text, const UnicodeString& prefix, ParsePosition& pp) const |
| 1166 | { |
| 1167 | |
| 1168 | if (prefix.length() != 0) { |
| 1169 | UErrorCode status = U_ZERO_ERROR; |
| 1170 | |
| 1171 | |
| 1172 | |
| 1173 | |
| 1174 | int32_t pfl = prefixLength(text, prefix, status); |
| 1175 | if (U_FAILURE(status)) { |
| 1176 | return; |
| 1177 | } |
| 1178 | if (pfl != 0) { |
| 1179 | |
| 1180 | |
| 1181 | pp.setIndex(pp.getIndex() + pfl); |
| 1182 | text.remove(0, pfl); |
| 1183 | } |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | |
| 1188 | |
| 1189 | |
| 1190 | |
| 1191 | |
| 1192 | |
| 1193 | |
| 1194 | |
| 1195 | |
| 1196 | |
| 1197 | |
| 1198 | |
| 1199 | |
| 1200 | |
| 1201 | |
| 1202 | |
| 1203 | |
| 1204 | |
| 1205 | |
| 1206 | |
| 1207 | |
| 1208 | |
| 1209 | |
| 1210 | |
| 1211 | |
| 1212 | |
| 1213 | |
| 1214 | |
| 1215 | |
| 1216 | double |
| 1217 | NFRule::matchToDelimiter(const UnicodeString& text, |
| 1218 | int32_t startPos, |
| 1219 | double _baseValue, |
| 1220 | const UnicodeString& delimiter, |
| 1221 | ParsePosition& pp, |
| 1222 | const NFSubstitution* sub, |
| 1223 | uint32_t nonNumericalExecutedRuleMask, |
| 1224 | int32_t recursionCount, |
| 1225 | double upperBound) const |
| 1226 | { |
| 1227 | UErrorCode status = U_ZERO_ERROR; |
| 1228 | |
| 1229 | |
| 1230 | |
| 1231 | |
| 1232 | if (!allIgnorable(delimiter, status)) { |
| |
| 1233 | if (U_FAILURE(status)) { |
| |
| 1234 | return 0; |
| 1235 | } |
| 1236 | ParsePosition tempPP; |
| 1237 | Formattable result; |
| 1238 | |
| 1239 | |
| 1240 | |
| 1241 | |
| 1242 | |
| 1243 | int32_t dLen; |
| 1244 | int32_t dPos = findText(text, delimiter, startPos, &dLen); |
| 1245 | |
| 1246 | |
| 1247 | |
| 1248 | while (dPos >= 0) { |
| 17 | | Assuming 'dPos' is >= 0 | |
|
| 18 | | Loop condition is true. Entering loop body | |
|
| 1249 | UnicodeString subText; |
| 1250 | subText.setTo(text, 0, dPos); |
| 1251 | if (subText.length() > 0) { |
| 19 | | Assuming the condition is true | |
|
| |
| 1252 | UBool success = sub->doParse(subText, tempPP, _baseValue, upperBound, |
| 21 | | Called C++ object pointer is null |
|
| 1253 | #if UCONFIG_NO_COLLATION |
| 1254 | false, |
| 1255 | #else |
| 1256 | formatter->isLenient(), |
| 1257 | #endif |
| 1258 | nonNumericalExecutedRuleMask, |
| 1259 | recursionCount, |
| 1260 | result); |
| 1261 | |
| 1262 | |
| 1263 | |
| 1264 | |
| 1265 | |
| 1266 | |
| 1267 | |
| 1268 | if (success && tempPP.getIndex() == dPos) { |
| 1269 | pp.setIndex(dPos + dLen); |
| 1270 | return result.getDouble(); |
| 1271 | } |
| 1272 | else { |
| 1273 | |
| 1274 | |
| 1275 | if (tempPP.getErrorIndex() > 0) { |
| 1276 | pp.setErrorIndex(tempPP.getErrorIndex()); |
| 1277 | } else { |
| 1278 | pp.setErrorIndex(tempPP.getIndex()); |
| 1279 | } |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | |
| 1284 | |
| 1285 | |
| 1286 | tempPP.setIndex(0); |
| 1287 | dPos = findText(text, delimiter, dPos + dLen, &dLen); |
| 1288 | } |
| 1289 | |
| 1290 | |
| 1291 | pp.setIndex(0); |
| 1292 | return 0; |
| 1293 | |
| 1294 | |
| 1295 | |
| 1296 | |
| 1297 | |
| 1298 | } |
| 1299 | else if (sub == nullptr) { |
| 1300 | return _baseValue; |
| 1301 | } |
| 1302 | else { |
| 1303 | ParsePosition tempPP; |
| 1304 | Formattable result; |
| 1305 | |
| 1306 | |
| 1307 | UBool success = sub->doParse(text, tempPP, _baseValue, upperBound, |
| 1308 | #if UCONFIG_NO_COLLATION |
| 1309 | false, |
| 1310 | #else |
| 1311 | formatter->isLenient(), |
| 1312 | #endif |
| 1313 | nonNumericalExecutedRuleMask, |
| 1314 | recursionCount, |
| 1315 | result); |
| 1316 | if (success && (tempPP.getIndex() != 0)) { |
| 1317 | |
| 1318 | |
| 1319 | |
| 1320 | |
| 1321 | pp.setIndex(tempPP.getIndex()); |
| 1322 | return result.getDouble(); |
| 1323 | } |
| 1324 | else { |
| 1325 | |
| 1326 | |
| 1327 | pp.setErrorIndex(tempPP.getErrorIndex()); |
| 1328 | } |
| 1329 | |
| 1330 | |
| 1331 | |
| 1332 | return 0; |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | |
| 1337 | |
| 1338 | |
| 1339 | |
| 1340 | |
| 1341 | |
| 1342 | |
| 1343 | |
| 1344 | |
| 1345 | |
| 1346 | |
| 1347 | |
| 1348 | |
| 1349 | |
| 1350 | int32_t |
| 1351 | NFRule::prefixLength(const UnicodeString& str, const UnicodeString& prefix, UErrorCode& status) const |
| 1352 | { |
| 1353 | |
| 1354 | |
| 1355 | if (prefix.length() == 0) { |
| 1356 | return 0; |
| 1357 | } |
| 1358 | |
| 1359 | #if !UCONFIG_NO_COLLATION |
| 1360 | |
| 1361 | if (formatter->isLenient()) { |
| 1362 | |
| 1363 | if (str.startsWith(prefix)) { |
| 1364 | return prefix.length(); |
| 1365 | } |
| 1366 | |
| 1367 | |
| 1368 | |
| 1369 | |
| 1370 | |
| 1371 | |
| 1372 | |
| 1373 | const RuleBasedCollator* collator = formatter->getCollator(); |
| 1374 | if (collator == nullptr) { |
| 1375 | status = U_MEMORY_ALLOCATION_ERROR; |
| 1376 | return 0; |
| 1377 | } |
| 1378 | LocalPointer<CollationElementIterator> strIter(collator->createCollationElementIterator(str)); |
| 1379 | LocalPointer<CollationElementIterator> prefixIter(collator->createCollationElementIterator(prefix)); |
| 1380 | |
| 1381 | if (strIter.isNull() || prefixIter.isNull()) { |
| 1382 | status = U_MEMORY_ALLOCATION_ERROR; |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
| 1386 | UErrorCode err = U_ZERO_ERROR; |
| 1387 | |
| 1388 | |
| 1389 | |
| 1390 | |
| 1391 | |
| 1392 | |
| 1393 | |
| 1394 | |
| 1395 | |
| 1396 | |
| 1397 | |
| 1398 | |
| 1399 | |
| 1400 | |
| 1401 | |
| 1402 | |
| 1403 | |
| 1404 | |
| 1405 | |
| 1406 | int32_t oStr = strIter->next(err); |
| 1407 | int32_t oPrefix = prefixIter->next(err); |
| 1408 | |
| 1409 | while (oPrefix != CollationElementIterator::NULLORDER) { |
| 1410 | |
| 1411 | while (CollationElementIterator::primaryOrder(oStr) == 0 |
| 1412 | && oStr != CollationElementIterator::NULLORDER) { |
| 1413 | oStr = strIter->next(err); |
| 1414 | } |
| 1415 | |
| 1416 | |
| 1417 | while (CollationElementIterator::primaryOrder(oPrefix) == 0 |
| 1418 | && oPrefix != CollationElementIterator::NULLORDER) { |
| 1419 | oPrefix = prefixIter->next(err); |
| 1420 | } |
| 1421 | |
| 1422 | |
| 1423 | |
| 1424 | |
| 1425 | |
| 1426 | |
| 1427 | |
| 1428 | if (oPrefix == CollationElementIterator::NULLORDER) { |
| 1429 | break; |
| 1430 | } |
| 1431 | |
| 1432 | |
| 1433 | |
| 1434 | if (oStr == CollationElementIterator::NULLORDER) { |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
| 1438 | |
| 1439 | |
| 1440 | |
| 1441 | if (CollationElementIterator::primaryOrder(oStr) |
| 1442 | != CollationElementIterator::primaryOrder(oPrefix)) { |
| 1443 | return 0; |
| 1444 | |
| 1445 | |
| 1446 | |
| 1447 | |
| 1448 | } else { |
| 1449 | oStr = strIter->next(err); |
| 1450 | oPrefix = prefixIter->next(err); |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | int32_t result = strIter->getOffset(); |
| 1455 | if (oStr != CollationElementIterator::NULLORDER) { |
| 1456 | --result; |
| 1457 | } |
| 1458 | |
| 1459 | #ifdef RBNF_DEBUG |
| 1460 | fprintf(stderr, "prefix length: %d\n", result); |
| 1461 | #endif |
| 1462 | return result; |
| 1463 | #if 0 |
| 1464 | |
| 1465 | |
| 1466 | |
| 1467 | |
| 1468 | |
| 1469 | |
| 1470 | |
| 1471 | |
| 1472 | |
| 1473 | |
| 1474 | |
| 1475 | |
| 1476 | |
| 1477 | |
| 1478 | |
| 1479 | collator->setStrength(Collator::PRIMARY); |
| 1480 | if (str.length() >= prefix.length()) { |
| 1481 | UnicodeString temp; |
| 1482 | temp.setTo(str, 0, prefix.length()); |
| 1483 | if (collator->equals(temp, prefix)) { |
| 1484 | #ifdef RBNF_DEBUG |
| 1485 | fprintf(stderr, "returning: %d\n", prefix.length()); |
| 1486 | #endif |
| 1487 | return prefix.length(); |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | |
| 1492 | |
| 1493 | |
| 1494 | |
| 1495 | int32_t p = 1; |
| 1496 | while (p <= str.length()) { |
| 1497 | UnicodeString temp; |
| 1498 | temp.setTo(str, 0, p); |
| 1499 | if (collator->equals(temp, prefix)) { |
| 1500 | return p; |
| 1501 | } else { |
| 1502 | ++p; |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | |
| 1507 | return 0; |
| 1508 | |
| 1509 | #endif |
| 1510 | |
| 1511 | |
| 1512 | |
| 1513 | } else |
| 1514 | #endif |
| 1515 | { |
| 1516 | if (str.startsWith(prefix)) { |
| 1517 | return prefix.length(); |
| 1518 | } else { |
| 1519 | return 0; |
| 1520 | } |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | |
| 1525 | |
| 1526 | |
| 1527 | |
| 1528 | |
| 1529 | |
| 1530 | |
| 1531 | |
| 1532 | |
| 1533 | |
| 1534 | |
| 1535 | |
| 1536 | |
| 1537 | |
| 1538 | |
| 1539 | int32_t |
| 1540 | NFRule::findText(const UnicodeString& str, |
| 1541 | const UnicodeString& key, |
| 1542 | int32_t startingAt, |
| 1543 | int32_t* length) const |
| 1544 | { |
| 1545 | if (rulePatternFormat) { |
| 1546 | Formattable result; |
| 1547 | FieldPosition position(UNUM_INTEGER_FIELD); |
| 1548 | position.setBeginIndex(startingAt); |
| 1549 | rulePatternFormat->parseType(str, this, result, position); |
| 1550 | int start = position.getBeginIndex(); |
| 1551 | if (start >= 0) { |
| 1552 | int32_t pluralRuleStart = fRuleText.indexOf(gDollarOpenParenthesis, -1, 0); |
| 1553 | int32_t pluralRuleSuffix = fRuleText.indexOf(gClosedParenthesisDollar, -1, pluralRuleStart) + 2; |
| 1554 | int32_t matchLen = position.getEndIndex() - start; |
| 1555 | UnicodeString prefix(fRuleText.tempSubString(0, pluralRuleStart)); |
| 1556 | UnicodeString suffix(fRuleText.tempSubString(pluralRuleSuffix)); |
| 1557 | if (str.compare(start - prefix.length(), prefix.length(), prefix, 0, prefix.length()) == 0 |
| 1558 | && str.compare(start + matchLen, suffix.length(), suffix, 0, suffix.length()) == 0) |
| 1559 | { |
| 1560 | *length = matchLen + prefix.length() + suffix.length(); |
| 1561 | return start - prefix.length(); |
| 1562 | } |
| 1563 | } |
| 1564 | *length = 0; |
| 1565 | return -1; |
| 1566 | } |
| 1567 | if (!formatter->isLenient()) { |
| 1568 | |
| 1569 | |
| 1570 | *length = key.length(); |
| 1571 | return str.indexOf(key, startingAt); |
| 1572 | } |
| 1573 | else { |
| 1574 | |
| 1575 | *length = key.length(); |
| 1576 | int32_t pos = str.indexOf(key, startingAt); |
| 1577 | if(pos >= 0) { |
| 1578 | return pos; |
| 1579 | } else { |
| 1580 | |
| 1581 | return findTextLenient(str, key, startingAt, length); |
| 1582 | } |
| 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | int32_t |
| 1587 | NFRule::findTextLenient(const UnicodeString& str, |
| 1588 | const UnicodeString& key, |
| 1589 | int32_t startingAt, |
| 1590 | int32_t* length) const |
| 1591 | { |
| 1592 | |
| 1593 | |
| 1594 | |
| 1595 | |
| 1596 | |
| 1597 | |
| 1598 | |
| 1599 | |
| 1600 | int32_t p = startingAt; |
| 1601 | int32_t keyLen = 0; |
| 1602 | |
| 1603 | |
| 1604 | |
| 1605 | |
| 1606 | |
| 1607 | |
| 1608 | |
| 1609 | |
| 1610 | UnicodeString temp; |
| 1611 | UErrorCode status = U_ZERO_ERROR; |
| 1612 | while (p < str.length() && keyLen == 0) { |
| 1613 | temp.setTo(str, p, str.length() - p); |
| 1614 | keyLen = prefixLength(temp, key, status); |
| 1615 | if (U_FAILURE(status)) { |
| 1616 | break; |
| 1617 | } |
| 1618 | if (keyLen != 0) { |
| 1619 | *length = keyLen; |
| 1620 | return p; |
| 1621 | } |
| 1622 | ++p; |
| 1623 | } |
| 1624 | |
| 1625 | |
| 1626 | |
| 1627 | *length = 0; |
| 1628 | return -1; |
| 1629 | } |
| 1630 | |
| 1631 | |
| 1632 | |
| 1633 | |
| 1634 | |
| 1635 | |
| 1636 | |
| 1637 | |
| 1638 | |
| 1639 | UBool |
| 1640 | NFRule::allIgnorable(const UnicodeString& str, UErrorCode& status) const |
| 1641 | { |
| 1642 | |
| 1643 | if (str.length() == 0) { |
| 1644 | return true; |
| 1645 | } |
| 1646 | |
| 1647 | #if !UCONFIG_NO_COLLATION |
| 1648 | |
| 1649 | |
| 1650 | |
| 1651 | if (formatter->isLenient()) { |
| 1652 | const RuleBasedCollator* collator = formatter->getCollator(); |
| 1653 | if (collator == nullptr) { |
| 1654 | status = U_MEMORY_ALLOCATION_ERROR; |
| 1655 | return false; |
| 1656 | } |
| 1657 | LocalPointer<CollationElementIterator> iter(collator->createCollationElementIterator(str)); |
| 1658 | |
| 1659 | |
| 1660 | if (iter.isNull()) { |
| 1661 | status = U_MEMORY_ALLOCATION_ERROR; |
| 1662 | return false; |
| 1663 | } |
| 1664 | |
| 1665 | UErrorCode err = U_ZERO_ERROR; |
| 1666 | int32_t o = iter->next(err); |
| 1667 | while (o != CollationElementIterator::NULLORDER |
| 1668 | && CollationElementIterator::primaryOrder(o) == 0) { |
| 1669 | o = iter->next(err); |
| 1670 | } |
| 1671 | |
| 1672 | return o == CollationElementIterator::NULLORDER; |
| 1673 | } |
| 1674 | #endif |
| 1675 | |
| 1676 | |
| 1677 | |
| 1678 | return false; |
| 1679 | } |
| 1680 | |
| 1681 | void |
| 1682 | NFRule::setDecimalFormatSymbols(const DecimalFormatSymbols& newSymbols, UErrorCode& status) { |
| 1683 | if (sub1 != nullptr) { |
| 1684 | sub1->setDecimalFormatSymbols(newSymbols, status); |
| 1685 | } |
| 1686 | if (sub2 != nullptr) { |
| 1687 | sub2->setDecimalFormatSymbols(newSymbols, status); |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | U_NAMESPACE_END |
| 1692 | |
| 1693 | |
| 1694 | #endif |