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