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