]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ****************************************************************************** | |
73c04bcf | 3 | * Copyright (C) 1997-2006, International Business Machines |
b75a7d8f A |
4 | * Corporation and others. All Rights Reserved. |
5 | ****************************************************************************** | |
6 | * file name: nfrule.cpp | |
7 | * encoding: US-ASCII | |
8 | * tab size: 8 (not used) | |
9 | * indentation:4 | |
10 | * | |
11 | * Modification history | |
12 | * Date Name Comments | |
13 | * 10/11/2001 Doug Ported from ICU4J | |
14 | */ | |
15 | ||
16 | #include "nfrule.h" | |
17 | ||
18 | #if U_HAVE_RBNF | |
19 | ||
20 | #include "unicode/rbnf.h" | |
21 | #include "unicode/tblcoll.h" | |
22 | #include "unicode/coleitr.h" | |
23 | #include "unicode/uchar.h" | |
24 | #include "nfrs.h" | |
25 | #include "nfrlist.h" | |
26 | #include "nfsubs.h" | |
27 | ||
374ca955 | 28 | #include "util.h" |
b75a7d8f A |
29 | |
30 | U_NAMESPACE_BEGIN | |
31 | ||
b75a7d8f A |
32 | NFRule::NFRule(const RuleBasedNumberFormat* _rbnf) |
33 | : baseValue((int32_t)0) | |
34 | , radix(0) | |
35 | , exponent(0) | |
36 | , ruleText() | |
37 | , sub1(NULL) | |
38 | , sub2(NULL) | |
39 | , formatter(_rbnf) | |
40 | { | |
41 | } | |
42 | ||
43 | NFRule::~NFRule() | |
44 | { | |
45 | delete sub1; | |
46 | delete sub2; | |
47 | } | |
48 | ||
49 | static const UChar gLeftBracket = 0x005b; | |
50 | static const UChar gRightBracket = 0x005d; | |
51 | static const UChar gColon = 0x003a; | |
52 | static const UChar gZero = 0x0030; | |
53 | static const UChar gNine = 0x0039; | |
54 | static const UChar gSpace = 0x0020; | |
55 | static const UChar gSlash = 0x002f; | |
56 | static const UChar gGreaterThan = 0x003e; | |
73c04bcf | 57 | static const UChar gLessThan = 0x003c; |
b75a7d8f A |
58 | static const UChar gComma = 0x002c; |
59 | static const UChar gDot = 0x002e; | |
60 | static const UChar gTick = 0x0027; | |
73c04bcf | 61 | //static const UChar gMinus = 0x002d; |
b75a7d8f A |
62 | static const UChar gSemicolon = 0x003b; |
63 | ||
64 | static const UChar gMinusX[] = {0x2D, 0x78, 0}; /* "-x" */ | |
65 | static const UChar gXDotX[] = {0x78, 0x2E, 0x78, 0}; /* "x.x" */ | |
66 | static const UChar gXDotZero[] = {0x78, 0x2E, 0x30, 0}; /* "x.0" */ | |
67 | static const UChar gZeroDotX[] = {0x30, 0x2E, 0x78, 0}; /* "0.x" */ | |
68 | ||
69 | static const UChar gLessLess[] = {0x3C, 0x3C, 0}; /* "<<" */ | |
70 | static const UChar gLessPercent[] = {0x3C, 0x25, 0}; /* "<%" */ | |
71 | static const UChar gLessHash[] = {0x3C, 0x23, 0}; /* "<#" */ | |
72 | static const UChar gLessZero[] = {0x3C, 0x30, 0}; /* "<0" */ | |
73 | static const UChar gGreaterGreater[] = {0x3E, 0x3E, 0}; /* ">>" */ | |
74 | static const UChar gGreaterPercent[] = {0x3E, 0x25, 0}; /* ">%" */ | |
75 | static const UChar gGreaterHash[] = {0x3E, 0x23, 0}; /* ">#" */ | |
76 | static const UChar gGreaterZero[] = {0x3E, 0x30, 0}; /* ">0" */ | |
77 | static const UChar gEqualPercent[] = {0x3D, 0x25, 0}; /* "=%" */ | |
78 | static const UChar gEqualHash[] = {0x3D, 0x23, 0}; /* "=#" */ | |
79 | static const UChar gEqualZero[] = {0x3D, 0x30, 0}; /* "=0" */ | |
80 | static const UChar gEmptyString[] = {0}; /* "" */ | |
81 | static const UChar gGreaterGreaterGreater[] = {0x3E, 0x3E, 0x3E, 0}; /* ">>>" */ | |
82 | ||
83 | static const UChar * const tokenStrings[] = { | |
84 | gLessLess, gLessPercent, gLessHash, gLessZero, | |
85 | gGreaterGreater, gGreaterPercent,gGreaterHash, gGreaterZero, | |
86 | gEqualPercent, gEqualHash, gEqualZero, NULL | |
87 | }; | |
88 | ||
89 | void | |
90 | NFRule::makeRules(UnicodeString& description, | |
91 | const NFRuleSet *ruleSet, | |
92 | const NFRule *predecessor, | |
93 | const RuleBasedNumberFormat *rbnf, | |
94 | NFRuleList& rules, | |
95 | UErrorCode& status) | |
96 | { | |
97 | // we know we're making at least one rule, so go ahead and | |
98 | // new it up and initialize its basevalue and divisor | |
99 | // (this also strips the rule descriptor, if any, off the | |
100 | // descripton string) | |
101 | NFRule* rule1 = new NFRule(rbnf); | |
102 | /* test for NULL */ | |
103 | if (rule1 == 0) { | |
104 | status = U_MEMORY_ALLOCATION_ERROR; | |
105 | return; | |
106 | } | |
107 | rule1->parseRuleDescriptor(description, status); | |
108 | ||
109 | // check the description to see whether there's text enclosed | |
110 | // in brackets | |
111 | int32_t brack1 = description.indexOf(gLeftBracket); | |
112 | int32_t brack2 = description.indexOf(gRightBracket); | |
113 | ||
114 | // if the description doesn't contain a matched pair of brackets, | |
115 | // or if it's of a type that doesn't recognize bracketed text, | |
116 | // then leave the description alone, initialize the rule's | |
117 | // rule text and substitutions, and return that rule | |
118 | if (brack1 == -1 || brack2 == -1 || brack1 > brack2 | |
119 | || rule1->getType() == kProperFractionRule | |
120 | || rule1->getType() == kNegativeNumberRule) { | |
121 | rule1->ruleText = description; | |
122 | rule1->extractSubstitutions(ruleSet, predecessor, rbnf, status); | |
123 | rules.add(rule1); | |
124 | } else { | |
125 | // if the description does contain a matched pair of brackets, | |
126 | // then it's really shorthand for two rules (with one exception) | |
127 | NFRule* rule2 = NULL; | |
128 | UnicodeString sbuf; | |
129 | ||
130 | // we'll actually only split the rule into two rules if its | |
131 | // base value is an even multiple of its divisor (or it's one | |
132 | // of the special rules) | |
133 | if ((rule1->baseValue > 0 | |
134 | && (rule1->baseValue % util64_pow(rule1->radix, rule1->exponent)) == 0) | |
135 | || rule1->getType() == kImproperFractionRule | |
136 | || rule1->getType() == kMasterRule) { | |
137 | ||
138 | // if it passes that test, new up the second rule. If the | |
139 | // rule set both rules will belong to is a fraction rule | |
140 | // set, they both have the same base value; otherwise, | |
141 | // increment the original rule's base value ("rule1" actually | |
142 | // goes SECOND in the rule set's rule list) | |
143 | rule2 = new NFRule(rbnf); | |
144 | /* test for NULL */ | |
145 | if (rule2 == 0) { | |
146 | status = U_MEMORY_ALLOCATION_ERROR; | |
147 | return; | |
148 | } | |
149 | if (rule1->baseValue >= 0) { | |
150 | rule2->baseValue = rule1->baseValue; | |
151 | if (!ruleSet->isFractionRuleSet()) { | |
152 | ++rule1->baseValue; | |
153 | } | |
154 | } | |
155 | ||
156 | // if the description began with "x.x" and contains bracketed | |
157 | // text, it describes both the improper fraction rule and | |
158 | // the proper fraction rule | |
159 | else if (rule1->getType() == kImproperFractionRule) { | |
160 | rule2->setType(kProperFractionRule); | |
161 | } | |
162 | ||
163 | // if the description began with "x.0" and contains bracketed | |
164 | // text, it describes both the master rule and the | |
165 | // improper fraction rule | |
166 | else if (rule1->getType() == kMasterRule) { | |
167 | rule2->baseValue = rule1->baseValue; | |
168 | rule1->setType(kImproperFractionRule); | |
169 | } | |
170 | ||
171 | // both rules have the same radix and exponent (i.e., the | |
172 | // same divisor) | |
173 | rule2->radix = rule1->radix; | |
174 | rule2->exponent = rule1->exponent; | |
175 | ||
176 | // rule2's rule text omits the stuff in brackets: initalize | |
177 | // its rule text and substitutions accordingly | |
178 | sbuf.append(description, 0, brack1); | |
179 | if (brack2 + 1 < description.length()) { | |
180 | sbuf.append(description, brack2 + 1, description.length() - brack2 - 1); | |
181 | } | |
182 | rule2->ruleText.setTo(sbuf); | |
183 | rule2->extractSubstitutions(ruleSet, predecessor, rbnf, status); | |
184 | } | |
185 | ||
186 | // rule1's text includes the text in the brackets but omits | |
187 | // the brackets themselves: initialize _its_ rule text and | |
188 | // substitutions accordingly | |
189 | sbuf.setTo(description, 0, brack1); | |
190 | sbuf.append(description, brack1 + 1, brack2 - brack1 - 1); | |
191 | if (brack2 + 1 < description.length()) { | |
192 | sbuf.append(description, brack2 + 1, description.length() - brack2 - 1); | |
193 | } | |
194 | rule1->ruleText.setTo(sbuf); | |
195 | rule1->extractSubstitutions(ruleSet, predecessor, rbnf, status); | |
196 | ||
197 | // if we only have one rule, return it; if we have two, return | |
198 | // a two-element array containing them (notice that rule2 goes | |
199 | // BEFORE rule1 in the list: in all cases, rule2 OMITS the | |
200 | // material in the brackets and rule1 INCLUDES the material | |
201 | // in the brackets) | |
202 | if (rule2 != NULL) { | |
203 | rules.add(rule2); | |
204 | } | |
205 | rules.add(rule1); | |
206 | } | |
207 | } | |
208 | ||
209 | /** | |
210 | * This function parses the rule's rule descriptor (i.e., the base | |
211 | * value and/or other tokens that precede the rule's rule text | |
212 | * in the description) and sets the rule's base value, radix, and | |
213 | * exponent according to the descriptor. (If the description doesn't | |
214 | * include a rule descriptor, then this function sets everything to | |
215 | * default values and the rule set sets the rule's real base value). | |
216 | * @param description The rule's description | |
217 | * @return If "description" included a rule descriptor, this is | |
218 | * "description" with the descriptor and any trailing whitespace | |
219 | * stripped off. Otherwise; it's "descriptor" unchangd. | |
220 | */ | |
221 | void | |
222 | NFRule::parseRuleDescriptor(UnicodeString& description, UErrorCode& status) | |
223 | { | |
224 | // the description consists of a rule descriptor and a rule body, | |
225 | // separated by a colon. The rule descriptor is optional. If | |
226 | // it's omitted, just set the base value to 0. | |
227 | int32_t p = description.indexOf(gColon); | |
228 | if (p == -1) { | |
374ca955 | 229 | setBaseValue((int32_t)0, status); |
b75a7d8f A |
230 | } else { |
231 | // copy the descriptor out into its own string and strip it, | |
232 | // along with any trailing whitespace, out of the original | |
233 | // description | |
234 | UnicodeString descriptor; | |
235 | descriptor.setTo(description, 0, p); | |
236 | ||
237 | ++p; | |
238 | while (p < description.length() && uprv_isRuleWhiteSpace(description.charAt(p))) { | |
239 | ++p; | |
240 | } | |
241 | description.removeBetween(0, p); | |
242 | ||
243 | // check first to see if the rule descriptor matches the token | |
244 | // for one of the special rules. If it does, set the base | |
245 | // value to the correct identfier value | |
246 | if (descriptor == gMinusX) { | |
247 | setType(kNegativeNumberRule); | |
248 | } | |
249 | else if (descriptor == gXDotX) { | |
250 | setType(kImproperFractionRule); | |
251 | } | |
252 | else if (descriptor == gZeroDotX) { | |
253 | setType(kProperFractionRule); | |
254 | } | |
255 | else if (descriptor == gXDotZero) { | |
256 | setType(kMasterRule); | |
257 | } | |
258 | ||
259 | // if the rule descriptor begins with a digit, it's a descriptor | |
260 | // for a normal rule | |
261 | // since we don't have Long.parseLong, and this isn't much work anyway, | |
262 | // just build up the value as we encounter the digits. | |
263 | else if (descriptor.charAt(0) >= gZero && descriptor.charAt(0) <= gNine) { | |
264 | int64_t val = 0; | |
265 | p = 0; | |
266 | UChar c = gSpace; | |
267 | ||
268 | // begin parsing the descriptor: copy digits | |
269 | // into "tempValue", skip periods, commas, and spaces, | |
270 | // stop on a slash or > sign (or at the end of the string), | |
271 | // and throw an exception on any other character | |
272 | int64_t ll_10 = 10; | |
273 | while (p < descriptor.length()) { | |
274 | c = descriptor.charAt(p); | |
275 | if (c >= gZero && c <= gNine) { | |
276 | val = val * ll_10 + (int32_t)(c - gZero); | |
277 | } | |
278 | else if (c == gSlash || c == gGreaterThan) { | |
279 | break; | |
280 | } | |
281 | else if (uprv_isRuleWhiteSpace(c) || c == gComma || c == gDot) { | |
282 | } | |
283 | else { | |
284 | // throw new IllegalArgumentException("Illegal character in rule descriptor"); | |
285 | status = U_PARSE_ERROR; | |
286 | return; | |
287 | } | |
288 | ++p; | |
289 | } | |
290 | ||
291 | // we have the base value, so set it | |
374ca955 | 292 | setBaseValue(val, status); |
b75a7d8f A |
293 | |
294 | // if we stopped the previous loop on a slash, we're | |
295 | // now parsing the rule's radix. Again, accumulate digits | |
296 | // in tempValue, skip punctuation, stop on a > mark, and | |
297 | // throw an exception on anything else | |
298 | if (c == gSlash) { | |
299 | val = 0; | |
300 | ++p; | |
301 | int64_t ll_10 = 10; | |
302 | while (p < descriptor.length()) { | |
303 | c = descriptor.charAt(p); | |
304 | if (c >= gZero && c <= gNine) { | |
305 | val = val * ll_10 + (int32_t)(c - gZero); | |
306 | } | |
307 | else if (c == gGreaterThan) { | |
308 | break; | |
309 | } | |
310 | else if (uprv_isRuleWhiteSpace(c) || c == gComma || c == gDot) { | |
311 | } | |
312 | else { | |
313 | // throw new IllegalArgumentException("Illegal character is rule descriptor"); | |
314 | status = U_PARSE_ERROR; | |
315 | return; | |
316 | } | |
317 | ++p; | |
318 | } | |
319 | ||
320 | // tempValue now contain's the rule's radix. Set it | |
321 | // accordingly, and recalculate the rule's exponent | |
374ca955 | 322 | radix = (int32_t)val; |
b75a7d8f A |
323 | if (radix == 0) { |
324 | // throw new IllegalArgumentException("Rule can't have radix of 0"); | |
325 | status = U_PARSE_ERROR; | |
326 | } | |
327 | ||
328 | exponent = expectedExponent(); | |
329 | } | |
330 | ||
331 | // if we stopped the previous loop on a > sign, then continue | |
332 | // for as long as we still see > signs. For each one, | |
333 | // decrement the exponent (unless the exponent is already 0). | |
334 | // If we see another character before reaching the end of | |
335 | // the descriptor, that's also a syntax error. | |
336 | if (c == gGreaterThan) { | |
337 | while (p < descriptor.length()) { | |
338 | c = descriptor.charAt(p); | |
339 | if (c == gGreaterThan && exponent > 0) { | |
340 | --exponent; | |
341 | } else { | |
342 | // throw new IllegalArgumentException("Illegal character in rule descriptor"); | |
343 | status = U_PARSE_ERROR; | |
344 | return; | |
345 | } | |
346 | ++p; | |
347 | } | |
348 | } | |
349 | } | |
350 | } | |
351 | ||
352 | // finally, if the rule body begins with an apostrophe, strip it off | |
353 | // (this is generally used to put whitespace at the beginning of | |
354 | // a rule's rule text) | |
355 | if (description.length() > 0 && description.charAt(0) == gTick) { | |
356 | description.removeBetween(0, 1); | |
357 | } | |
358 | ||
359 | // return the description with all the stuff we've just waded through | |
360 | // stripped off the front. It now contains just the rule body. | |
361 | // return description; | |
362 | } | |
363 | ||
364 | /** | |
365 | * Searches the rule's rule text for the substitution tokens, | |
366 | * creates the substitutions, and removes the substitution tokens | |
367 | * from the rule's rule text. | |
368 | * @param owner The rule set containing this rule | |
369 | * @param predecessor The rule preseding this one in "owners" rule list | |
370 | * @param ownersOwner The RuleBasedFormat that owns this rule | |
371 | */ | |
372 | void | |
373 | NFRule::extractSubstitutions(const NFRuleSet* ruleSet, | |
374 | const NFRule* predecessor, | |
375 | const RuleBasedNumberFormat* rbnf, | |
376 | UErrorCode& status) | |
377 | { | |
378 | if (U_SUCCESS(status)) { | |
379 | sub1 = extractSubstitution(ruleSet, predecessor, rbnf, status); | |
380 | sub2 = extractSubstitution(ruleSet, predecessor, rbnf, status); | |
381 | } | |
382 | } | |
383 | ||
384 | /** | |
385 | * Searches the rule's rule text for the first substitution token, | |
386 | * creates a substitution based on it, and removes the token from | |
387 | * the rule's rule text. | |
388 | * @param owner The rule set containing this rule | |
389 | * @param predecessor The rule preceding this one in the rule set's | |
390 | * rule list | |
391 | * @param ownersOwner The RuleBasedNumberFormat that owns this rule | |
392 | * @return The newly-created substitution. This is never null; if | |
393 | * the rule text doesn't contain any substitution tokens, this will | |
394 | * be a NullSubstitution. | |
395 | */ | |
396 | NFSubstitution * | |
397 | NFRule::extractSubstitution(const NFRuleSet* ruleSet, | |
398 | const NFRule* predecessor, | |
399 | const RuleBasedNumberFormat* rbnf, | |
400 | UErrorCode& status) | |
401 | { | |
402 | NFSubstitution* result = NULL; | |
403 | ||
404 | // search the rule's rule text for the first two characters of | |
405 | // a substitution token | |
406 | int32_t subStart = indexOfAny(tokenStrings); | |
407 | int32_t subEnd = subStart; | |
408 | ||
409 | // if we didn't find one, create a null substitution positioned | |
410 | // at the end of the rule text | |
411 | if (subStart == -1) { | |
412 | return NFSubstitution::makeSubstitution(ruleText.length(), this, predecessor, | |
413 | ruleSet, rbnf, gEmptyString, status); | |
414 | } | |
415 | ||
416 | // special-case the ">>>" token, since searching for the > at the | |
417 | // end will actually find the > in the middle | |
418 | if (ruleText.indexOf(gGreaterGreaterGreater) == subStart) { | |
419 | subEnd = subStart + 2; | |
420 | ||
421 | // otherwise the substitution token ends with the same character | |
422 | // it began with | |
423 | } else { | |
73c04bcf A |
424 | UChar c = ruleText.charAt(subStart); |
425 | subEnd = ruleText.indexOf(c, subStart + 1); | |
426 | // special case for '<%foo<<' | |
427 | if (c == gLessThan && subEnd != -1 && subEnd < ruleText.length() - 1 && ruleText.charAt(subEnd+1) == c) { | |
428 | // ordinals use "=#,##0==%abbrev=" as their rule. Notice that the '==' in the middle | |
429 | // occurs because of the juxtaposition of two different rules. The check for '<' is a hack | |
430 | // to get around this. Having the duplicate at the front would cause problems with | |
431 | // rules like "<<%" to format, say, percents... | |
432 | ++subEnd; | |
433 | } | |
434 | } | |
b75a7d8f A |
435 | |
436 | // if we don't find the end of the token (i.e., if we're on a single, | |
437 | // unmatched token character), create a null substitution positioned | |
438 | // at the end of the rule | |
439 | if (subEnd == -1) { | |
440 | return NFSubstitution::makeSubstitution(ruleText.length(), this, predecessor, | |
441 | ruleSet, rbnf, gEmptyString, status); | |
442 | } | |
443 | ||
444 | // if we get here, we have a real substitution token (or at least | |
445 | // some text bounded by substitution token characters). Use | |
446 | // makeSubstitution() to create the right kind of substitution | |
447 | UnicodeString subToken; | |
448 | subToken.setTo(ruleText, subStart, subEnd + 1 - subStart); | |
449 | result = NFSubstitution::makeSubstitution(subStart, this, predecessor, ruleSet, | |
450 | rbnf, subToken, status); | |
451 | ||
452 | // remove the substitution from the rule text | |
453 | ruleText.removeBetween(subStart, subEnd+1); | |
454 | ||
455 | return result; | |
456 | } | |
457 | ||
458 | /** | |
459 | * Sets the rule's base value, and causes the radix and exponent | |
460 | * to be recalculated. This is used during construction when we | |
461 | * don't know the rule's base value until after it's been | |
462 | * constructed. It should be used at any other time. | |
463 | * @param The new base value for the rule. | |
464 | */ | |
465 | void | |
374ca955 | 466 | NFRule::setBaseValue(int64_t newBaseValue, UErrorCode& status) |
b75a7d8f A |
467 | { |
468 | // set the base value | |
469 | baseValue = newBaseValue; | |
470 | ||
471 | // if this isn't a special rule, recalculate the radix and exponent | |
472 | // (the radix always defaults to 10; if it's supposed to be something | |
473 | // else, it's cleaned up by the caller and the exponent is | |
474 | // recalculated again-- the only function that does this is | |
475 | // NFRule.parseRuleDescriptor() ) | |
476 | if (baseValue >= 1) { | |
477 | radix = 10; | |
478 | exponent = expectedExponent(); | |
479 | ||
480 | // this function gets called on a fully-constructed rule whose | |
481 | // description didn't specify a base value. This means it | |
482 | // has substitutions, and some substitutions hold on to copies | |
483 | // of the rule's divisor. Fix their copies of the divisor. | |
484 | if (sub1 != NULL) { | |
374ca955 | 485 | sub1->setDivisor(radix, exponent, status); |
b75a7d8f A |
486 | } |
487 | if (sub2 != NULL) { | |
374ca955 | 488 | sub2->setDivisor(radix, exponent, status); |
b75a7d8f A |
489 | } |
490 | ||
491 | // if this is a special rule, its radix and exponent are basically | |
492 | // ignored. Set them to "safe" default values | |
493 | } else { | |
494 | radix = 10; | |
495 | exponent = 0; | |
496 | } | |
497 | } | |
498 | ||
499 | /** | |
500 | * This calculates the rule's exponent based on its radix and base | |
501 | * value. This will be the highest power the radix can be raised to | |
502 | * and still produce a result less than or equal to the base value. | |
503 | */ | |
504 | int16_t | |
505 | NFRule::expectedExponent() const | |
506 | { | |
507 | // since the log of 0, or the log base 0 of something, causes an | |
508 | // error, declare the exponent in these cases to be 0 (we also | |
509 | // deal with the special-rule identifiers here) | |
510 | if (radix == 0 || baseValue < 1) { | |
511 | return 0; | |
512 | } | |
513 | ||
514 | // we get rounding error in some cases-- for example, log 1000 / log 10 | |
515 | // gives us 1.9999999996 instead of 2. The extra logic here is to take | |
516 | // that into account | |
517 | int16_t tempResult = (int16_t)(uprv_log((double)baseValue) / uprv_log((double)radix)); | |
518 | int64_t temp = util64_pow(radix, tempResult + 1); | |
519 | if (temp <= baseValue) { | |
520 | tempResult += 1; | |
521 | } | |
522 | return tempResult; | |
523 | } | |
524 | ||
525 | /** | |
526 | * Searches the rule's rule text for any of the specified strings. | |
527 | * @param strings An array of strings to search the rule's rule | |
528 | * text for | |
529 | * @return The index of the first match in the rule's rule text | |
530 | * (i.e., the first substring in the rule's rule text that matches | |
531 | * _any_ of the strings in "strings"). If none of the strings in | |
532 | * "strings" is found in the rule's rule text, returns -1. | |
533 | */ | |
534 | int32_t | |
535 | NFRule::indexOfAny(const UChar* const strings[]) const | |
536 | { | |
537 | int result = -1; | |
538 | for (int i = 0; strings[i]; i++) { | |
539 | int32_t pos = ruleText.indexOf(*strings[i]); | |
540 | if (pos != -1 && (result == -1 || pos < result)) { | |
541 | result = pos; | |
542 | } | |
543 | } | |
544 | return result; | |
545 | } | |
546 | ||
547 | //----------------------------------------------------------------------- | |
548 | // boilerplate | |
549 | //----------------------------------------------------------------------- | |
550 | ||
551 | /** | |
552 | * Tests two rules for equality. | |
553 | * @param that The rule to compare this one against | |
554 | * @return True is the two rules are functionally equivalent | |
555 | */ | |
556 | UBool | |
557 | NFRule::operator==(const NFRule& rhs) const | |
558 | { | |
559 | return baseValue == rhs.baseValue | |
560 | && radix == rhs.radix | |
561 | && exponent == rhs.exponent | |
562 | && ruleText == rhs.ruleText | |
563 | && *sub1 == *rhs.sub1 | |
564 | && *sub2 == *rhs.sub2; | |
565 | } | |
566 | ||
567 | /** | |
568 | * Returns a textual representation of the rule. This won't | |
569 | * necessarily be the same as the description that this rule | |
570 | * was created with, but it will produce the same result. | |
571 | * @return A textual description of the rule | |
572 | */ | |
573 | static void util_append64(UnicodeString& result, int64_t n) | |
574 | { | |
575 | UChar buffer[256]; | |
576 | int32_t len = util64_tou(n, buffer, sizeof(buffer)); | |
577 | UnicodeString temp(buffer, len); | |
578 | result.append(temp); | |
579 | } | |
580 | ||
581 | void | |
73c04bcf | 582 | NFRule::_appendRuleText(UnicodeString& result) const |
b75a7d8f A |
583 | { |
584 | switch (getType()) { | |
585 | case kNegativeNumberRule: result.append(gMinusX); break; | |
586 | case kImproperFractionRule: result.append(gXDotX); break; | |
587 | case kProperFractionRule: result.append(gZeroDotX); break; | |
588 | case kMasterRule: result.append(gXDotZero); break; | |
589 | default: | |
590 | // for a normal rule, write out its base value, and if the radix is | |
591 | // something other than 10, write out the radix (with the preceding | |
592 | // slash, of course). Then calculate the expected exponent and if | |
593 | // if isn't the same as the actual exponent, write an appropriate | |
594 | // number of > signs. Finally, terminate the whole thing with | |
595 | // a colon. | |
596 | util_append64(result, baseValue); | |
597 | if (radix != 10) { | |
598 | result.append(gSlash); | |
599 | util_append64(result, radix); | |
600 | } | |
601 | int numCarets = expectedExponent() - exponent; | |
602 | for (int i = 0; i < numCarets; i++) { | |
603 | result.append(gGreaterThan); | |
604 | } | |
605 | break; | |
606 | } | |
607 | result.append(gColon); | |
608 | result.append(gSpace); | |
609 | ||
610 | // if the rule text begins with a space, write an apostrophe | |
611 | // (whitespace after the rule descriptor is ignored; the | |
612 | // apostrophe is used to make the whitespace significant) | |
613 | if (ruleText.startsWith(gSpace) && sub1->getPos() != 0) { | |
614 | result.append(gTick); | |
615 | } | |
616 | ||
617 | // now, write the rule's rule text, inserting appropriate | |
618 | // substitution tokens in the appropriate places | |
619 | UnicodeString ruleTextCopy; | |
620 | ruleTextCopy.setTo(ruleText); | |
621 | ||
622 | UnicodeString temp; | |
623 | sub2->toString(temp); | |
624 | ruleTextCopy.insert(sub2->getPos(), temp); | |
625 | sub1->toString(temp); | |
626 | ruleTextCopy.insert(sub1->getPos(), temp); | |
627 | ||
628 | result.append(ruleTextCopy); | |
629 | ||
630 | // and finally, top the whole thing off with a semicolon and | |
631 | // return the result | |
632 | result.append(gSemicolon); | |
633 | } | |
634 | ||
635 | //----------------------------------------------------------------------- | |
636 | // formatting | |
637 | //----------------------------------------------------------------------- | |
638 | ||
639 | /** | |
640 | * Formats the number, and inserts the resulting text into | |
641 | * toInsertInto. | |
642 | * @param number The number being formatted | |
643 | * @param toInsertInto The string where the resultant text should | |
644 | * be inserted | |
645 | * @param pos The position in toInsertInto where the resultant text | |
646 | * should be inserted | |
647 | */ | |
648 | void | |
649 | NFRule::doFormat(int64_t number, UnicodeString& toInsertInto, int32_t pos) const | |
650 | { | |
651 | // first, insert the rule's rule text into toInsertInto at the | |
652 | // specified position, then insert the results of the substitutions | |
653 | // into the right places in toInsertInto (notice we do the | |
654 | // substitutions in reverse order so that the offsets don't get | |
655 | // messed up) | |
656 | toInsertInto.insert(pos, ruleText); | |
657 | sub2->doSubstitution(number, toInsertInto, pos); | |
658 | sub1->doSubstitution(number, toInsertInto, pos); | |
659 | } | |
660 | ||
661 | /** | |
662 | * Formats the number, and inserts the resulting text into | |
663 | * toInsertInto. | |
664 | * @param number The number being formatted | |
665 | * @param toInsertInto The string where the resultant text should | |
666 | * be inserted | |
667 | * @param pos The position in toInsertInto where the resultant text | |
668 | * should be inserted | |
669 | */ | |
670 | void | |
671 | NFRule::doFormat(double number, UnicodeString& toInsertInto, int32_t pos) const | |
672 | { | |
673 | // first, insert the rule's rule text into toInsertInto at the | |
674 | // specified position, then insert the results of the substitutions | |
675 | // into the right places in toInsertInto | |
676 | // [again, we have two copies of this routine that do the same thing | |
677 | // so that we don't sacrifice precision in a long by casting it | |
678 | // to a double] | |
679 | toInsertInto.insert(pos, ruleText); | |
680 | sub2->doSubstitution(number, toInsertInto, pos); | |
681 | sub1->doSubstitution(number, toInsertInto, pos); | |
682 | } | |
683 | ||
684 | /** | |
685 | * Used by the owning rule set to determine whether to invoke the | |
686 | * rollback rule (i.e., whether this rule or the one that precedes | |
687 | * it in the rule set's list should be used to format the number) | |
688 | * @param The number being formatted | |
689 | * @return True if the rule set should use the rule that precedes | |
690 | * this one in its list; false if it should use this rule | |
691 | */ | |
692 | UBool | |
693 | NFRule::shouldRollBack(double number) const | |
694 | { | |
695 | // we roll back if the rule contains a modulus substitution, | |
696 | // the number being formatted is an even multiple of the rule's | |
697 | // divisor, and the rule's base value is NOT an even multiple | |
698 | // of its divisor | |
699 | // In other words, if the original description had | |
700 | // 100: << hundred[ >>]; | |
701 | // that expands into | |
702 | // 100: << hundred; | |
703 | // 101: << hundred >>; | |
704 | // internally. But when we're formatting 200, if we use the rule | |
705 | // at 101, which would normally apply, we get "two hundred zero". | |
706 | // To prevent this, we roll back and use the rule at 100 instead. | |
707 | // This is the logic that makes this happen: the rule at 101 has | |
708 | // a modulus substitution, its base value isn't an even multiple | |
709 | // of 100, and the value we're trying to format _is_ an even | |
710 | // multiple of 100. This is called the "rollback rule." | |
711 | if ((sub1->isModulusSubstitution()) || (sub2->isModulusSubstitution())) { | |
712 | int64_t re = util64_pow(radix, exponent); | |
713 | return uprv_fmod(number, (double)re) == 0 && (baseValue % re) != 0; | |
714 | } | |
715 | return FALSE; | |
716 | } | |
717 | ||
718 | //----------------------------------------------------------------------- | |
719 | // parsing | |
720 | //----------------------------------------------------------------------- | |
721 | ||
722 | /** | |
723 | * Attempts to parse the string with this rule. | |
724 | * @param text The string being parsed | |
725 | * @param parsePosition On entry, the value is ignored and assumed to | |
726 | * be 0. On exit, this has been updated with the position of the first | |
727 | * character not consumed by matching the text against this rule | |
728 | * (if this rule doesn't match the text at all, the parse position | |
729 | * if left unchanged (presumably at 0) and the function returns | |
730 | * new Long(0)). | |
731 | * @param isFractionRule True if this rule is contained within a | |
732 | * fraction rule set. This is only used if the rule has no | |
733 | * substitutions. | |
734 | * @return If this rule matched the text, this is the rule's base value | |
735 | * combined appropriately with the results of parsing the substitutions. | |
736 | * If nothing matched, this is new Long(0) and the parse position is | |
737 | * left unchanged. The result will be an instance of Long if the | |
738 | * result is an integer and Double otherwise. The result is never null. | |
739 | */ | |
740 | #ifdef RBNF_DEBUG | |
741 | #include <stdio.h> | |
742 | ||
743 | static void dumpUS(FILE* f, const UnicodeString& us) { | |
744 | int len = us.length(); | |
745 | char* buf = (char *)uprv_malloc((len+1)*sizeof(char)); //new char[len+1]; | |
746 | us.extract(0, len, buf); | |
747 | buf[len] = 0; | |
748 | fprintf(f, "%s", buf); | |
749 | uprv_free(buf); //delete[] buf; | |
750 | } | |
751 | #endif | |
752 | ||
753 | UBool | |
754 | NFRule::doParse(const UnicodeString& text, | |
755 | ParsePosition& parsePosition, | |
756 | UBool isFractionRule, | |
757 | double upperBound, | |
758 | Formattable& resVal) const | |
759 | { | |
760 | // internally we operate on a copy of the string being parsed | |
761 | // (because we're going to change it) and use our own ParsePosition | |
762 | ParsePosition pp; | |
763 | UnicodeString workText(text); | |
764 | ||
765 | // check to see whether the text before the first substitution | |
766 | // matches the text at the beginning of the string being | |
767 | // parsed. If it does, strip that off the front of workText; | |
768 | // otherwise, dump out with a mismatch | |
769 | UnicodeString prefix; | |
770 | prefix.setTo(ruleText, 0, sub1->getPos()); | |
771 | ||
772 | #ifdef RBNF_DEBUG | |
773 | fprintf(stderr, "doParse %x ", this); | |
774 | { | |
775 | UnicodeString rt; | |
73c04bcf | 776 | _appendRuleText(rt); |
b75a7d8f A |
777 | dumpUS(stderr, rt); |
778 | } | |
779 | ||
780 | fprintf(stderr, " text: '", this); | |
781 | dumpUS(stderr, text); | |
782 | fprintf(stderr, "' prefix: '"); | |
783 | dumpUS(stderr, prefix); | |
784 | #endif | |
785 | stripPrefix(workText, prefix, pp); | |
786 | int32_t prefixLength = text.length() - workText.length(); | |
787 | ||
788 | #ifdef RBNF_DEBUG | |
789 | fprintf(stderr, "' pl: %d ppi: %d s1p: %d\n", prefixLength, pp.getIndex(), sub1->getPos()); | |
790 | #endif | |
791 | ||
792 | if (pp.getIndex() == 0 && sub1->getPos() != 0) { | |
793 | // commented out because ParsePosition doesn't have error index in 1.1.x | |
794 | // restored for ICU4C port | |
795 | parsePosition.setErrorIndex(pp.getErrorIndex()); | |
796 | resVal.setLong(0); | |
797 | return TRUE; | |
798 | } | |
799 | ||
800 | // this is the fun part. The basic guts of the rule-matching | |
801 | // logic is matchToDelimiter(), which is called twice. The first | |
802 | // time it searches the input string for the rule text BETWEEN | |
803 | // the substitutions and tries to match the intervening text | |
804 | // in the input string with the first substitution. If that | |
805 | // succeeds, it then calls it again, this time to look for the | |
806 | // rule text after the second substitution and to match the | |
807 | // intervening input text against the second substitution. | |
808 | // | |
809 | // For example, say we have a rule that looks like this: | |
810 | // first << middle >> last; | |
811 | // and input text that looks like this: | |
812 | // first one middle two last | |
813 | // First we use stripPrefix() to match "first " in both places and | |
814 | // strip it off the front, leaving | |
815 | // one middle two last | |
816 | // Then we use matchToDelimiter() to match " middle " and try to | |
817 | // match "one" against a substitution. If it's successful, we now | |
818 | // have | |
819 | // two last | |
820 | // We use matchToDelimiter() a second time to match " last" and | |
821 | // try to match "two" against a substitution. If "two" matches | |
822 | // the substitution, we have a successful parse. | |
823 | // | |
824 | // Since it's possible in many cases to find multiple instances | |
825 | // of each of these pieces of rule text in the input string, | |
826 | // we need to try all the possible combinations of these | |
827 | // locations. This prevents us from prematurely declaring a mismatch, | |
828 | // and makes sure we match as much input text as we can. | |
829 | int highWaterMark = 0; | |
830 | double result = 0; | |
831 | int start = 0; | |
832 | double tempBaseValue = (double)(baseValue <= 0 ? 0 : baseValue); | |
833 | ||
834 | UnicodeString temp; | |
835 | do { | |
836 | // our partial parse result starts out as this rule's base | |
837 | // value. If it finds a successful match, matchToDelimiter() | |
838 | // will compose this in some way with what it gets back from | |
839 | // the substitution, giving us a new partial parse result | |
840 | pp.setIndex(0); | |
841 | ||
842 | temp.setTo(ruleText, sub1->getPos(), sub2->getPos() - sub1->getPos()); | |
843 | double partialResult = matchToDelimiter(workText, start, tempBaseValue, | |
844 | temp, pp, sub1, | |
845 | upperBound); | |
846 | ||
847 | // if we got a successful match (or were trying to match a | |
848 | // null substitution), pp is now pointing at the first unmatched | |
849 | // character. Take note of that, and try matchToDelimiter() | |
850 | // on the input text again | |
851 | if (pp.getIndex() != 0 || sub1->isNullSubstitution()) { | |
852 | start = pp.getIndex(); | |
853 | ||
854 | UnicodeString workText2; | |
855 | workText2.setTo(workText, pp.getIndex(), workText.length() - pp.getIndex()); | |
856 | ParsePosition pp2; | |
857 | ||
858 | // the second matchToDelimiter() will compose our previous | |
859 | // partial result with whatever it gets back from its | |
860 | // substitution if there's a successful match, giving us | |
861 | // a real result | |
862 | temp.setTo(ruleText, sub2->getPos(), ruleText.length() - sub2->getPos()); | |
863 | partialResult = matchToDelimiter(workText2, 0, partialResult, | |
864 | temp, pp2, sub2, | |
865 | upperBound); | |
866 | ||
867 | // if we got a successful match on this second | |
868 | // matchToDelimiter() call, update the high-water mark | |
869 | // and result (if necessary) | |
870 | if (pp2.getIndex() != 0 || sub2->isNullSubstitution()) { | |
871 | if (prefixLength + pp.getIndex() + pp2.getIndex() > highWaterMark) { | |
872 | highWaterMark = prefixLength + pp.getIndex() + pp2.getIndex(); | |
873 | result = partialResult; | |
874 | } | |
875 | } | |
876 | // commented out because ParsePosition doesn't have error index in 1.1.x | |
877 | // restored for ICU4C port | |
878 | else { | |
879 | int32_t temp = pp2.getErrorIndex() + sub1->getPos() + pp.getIndex(); | |
880 | if (temp> parsePosition.getErrorIndex()) { | |
881 | parsePosition.setErrorIndex(temp); | |
882 | } | |
883 | } | |
884 | } | |
885 | // commented out because ParsePosition doesn't have error index in 1.1.x | |
886 | // restored for ICU4C port | |
887 | else { | |
888 | int32_t temp = sub1->getPos() + pp.getErrorIndex(); | |
889 | if (temp > parsePosition.getErrorIndex()) { | |
890 | parsePosition.setErrorIndex(temp); | |
891 | } | |
892 | } | |
893 | // keep trying to match things until the outer matchToDelimiter() | |
894 | // call fails to make a match (each time, it picks up where it | |
895 | // left off the previous time) | |
896 | } while (sub1->getPos() != sub2->getPos() | |
897 | && pp.getIndex() > 0 | |
898 | && pp.getIndex() < workText.length() | |
899 | && pp.getIndex() != start); | |
900 | ||
901 | // update the caller's ParsePosition with our high-water mark | |
902 | // (i.e., it now points at the first character this function | |
903 | // didn't match-- the ParsePosition is therefore unchanged if | |
904 | // we didn't match anything) | |
905 | parsePosition.setIndex(highWaterMark); | |
906 | // commented out because ParsePosition doesn't have error index in 1.1.x | |
907 | // restored for ICU4C port | |
908 | if (highWaterMark > 0) { | |
909 | parsePosition.setErrorIndex(0); | |
910 | } | |
911 | ||
912 | // this is a hack for one unusual condition: Normally, whether this | |
913 | // rule belong to a fraction rule set or not is handled by its | |
914 | // substitutions. But if that rule HAS NO substitutions, then | |
915 | // we have to account for it here. By definition, if the matching | |
916 | // rule in a fraction rule set has no substitutions, its numerator | |
917 | // is 1, and so the result is the reciprocal of its base value. | |
918 | if (isFractionRule && | |
919 | highWaterMark > 0 && | |
920 | sub1->isNullSubstitution()) { | |
921 | result = 1 / result; | |
922 | } | |
923 | ||
924 | resVal.setDouble(result); | |
925 | return TRUE; // ??? do we need to worry if it is a long or a double? | |
926 | } | |
927 | ||
928 | /** | |
929 | * This function is used by parse() to match the text being parsed | |
930 | * against a possible prefix string. This function | |
931 | * matches characters from the beginning of the string being parsed | |
932 | * to characters from the prospective prefix. If they match, pp is | |
933 | * updated to the first character not matched, and the result is | |
934 | * the unparsed part of the string. If they don't match, the whole | |
935 | * string is returned, and pp is left unchanged. | |
936 | * @param text The string being parsed | |
937 | * @param prefix The text to match against | |
938 | * @param pp On entry, ignored and assumed to be 0. On exit, points | |
939 | * to the first unmatched character (assuming the whole prefix matched), | |
940 | * or is unchanged (if the whole prefix didn't match). | |
941 | * @return If things match, this is the unparsed part of "text"; | |
942 | * if they didn't match, this is "text". | |
943 | */ | |
944 | void | |
945 | NFRule::stripPrefix(UnicodeString& text, const UnicodeString& prefix, ParsePosition& pp) const | |
946 | { | |
947 | // if the prefix text is empty, dump out without doing anything | |
948 | if (prefix.length() != 0) { | |
949 | // use prefixLength() to match the beginning of | |
950 | // "text" against "prefix". This function returns the | |
951 | // number of characters from "text" that matched (or 0 if | |
952 | // we didn't match the whole prefix) | |
953 | int32_t pfl = prefixLength(text, prefix); | |
954 | if (pfl != 0) { | |
955 | // if we got a successful match, update the parse position | |
956 | // and strip the prefix off of "text" | |
957 | pp.setIndex(pp.getIndex() + pfl); | |
958 | text.remove(0, pfl); | |
959 | } | |
960 | } | |
961 | } | |
962 | ||
963 | /** | |
964 | * Used by parse() to match a substitution and any following text. | |
965 | * "text" is searched for instances of "delimiter". For each instance | |
966 | * of delimiter, the intervening text is tested to see whether it | |
967 | * matches the substitution. The longest match wins. | |
968 | * @param text The string being parsed | |
969 | * @param startPos The position in "text" where we should start looking | |
970 | * for "delimiter". | |
971 | * @param baseValue A partial parse result (often the rule's base value), | |
972 | * which is combined with the result from matching the substitution | |
973 | * @param delimiter The string to search "text" for. | |
974 | * @param pp Ignored and presumed to be 0 on entry. If there's a match, | |
975 | * on exit this will point to the first unmatched character. | |
976 | * @param sub If we find "delimiter" in "text", this substitution is used | |
977 | * to match the text between the beginning of the string and the | |
978 | * position of "delimiter." (If "delimiter" is the empty string, then | |
979 | * this function just matches against this substitution and updates | |
980 | * everything accordingly.) | |
981 | * @param upperBound When matching the substitution, it will only | |
982 | * consider rules with base values lower than this value. | |
983 | * @return If there's a match, this is the result of composing | |
984 | * baseValue with the result of matching the substitution. Otherwise, | |
985 | * this is new Long(0). It's never null. If the result is an integer, | |
986 | * this will be an instance of Long; otherwise, it's an instance of | |
987 | * Double. | |
988 | * | |
989 | * !!! note {dlf} in point of fact, in the java code the caller always converts | |
990 | * the result to a double, so we might as well return one. | |
991 | */ | |
992 | double | |
993 | NFRule::matchToDelimiter(const UnicodeString& text, | |
994 | int32_t startPos, | |
995 | double _baseValue, | |
996 | const UnicodeString& delimiter, | |
997 | ParsePosition& pp, | |
998 | const NFSubstitution* sub, | |
999 | double upperBound) const | |
1000 | { | |
1001 | // if "delimiter" contains real (i.e., non-ignorable) text, search | |
1002 | // it for "delimiter" beginning at "start". If that succeeds, then | |
1003 | // use "sub"'s doParse() method to match the text before the | |
1004 | // instance of "delimiter" we just found. | |
1005 | if (!allIgnorable(delimiter)) { | |
1006 | ParsePosition tempPP; | |
1007 | Formattable result; | |
1008 | ||
1009 | // use findText() to search for "delimiter". It returns a two- | |
1010 | // element array: element 0 is the position of the match, and | |
1011 | // element 1 is the number of characters that matched | |
1012 | // "delimiter". | |
1013 | int32_t dLen; | |
1014 | int32_t dPos = findText(text, delimiter, startPos, &dLen); | |
1015 | ||
1016 | // if findText() succeeded, isolate the text preceding the | |
1017 | // match, and use "sub" to match that text | |
1018 | while (dPos >= 0) { | |
1019 | UnicodeString subText; | |
1020 | subText.setTo(text, 0, dPos); | |
1021 | if (subText.length() > 0) { | |
1022 | UBool success = sub->doParse(subText, tempPP, _baseValue, upperBound, | |
1023 | #if UCONFIG_NO_COLLATION | |
1024 | FALSE, | |
1025 | #else | |
1026 | formatter->isLenient(), | |
1027 | #endif | |
1028 | result); | |
1029 | ||
1030 | // if the substitution could match all the text up to | |
1031 | // where we found "delimiter", then this function has | |
1032 | // a successful match. Bump the caller's parse position | |
1033 | // to point to the first character after the text | |
1034 | // that matches "delimiter", and return the result | |
1035 | // we got from parsing the substitution. | |
1036 | if (success && tempPP.getIndex() == dPos) { | |
1037 | pp.setIndex(dPos + dLen); | |
1038 | return result.getDouble(); | |
1039 | } | |
1040 | // commented out because ParsePosition doesn't have error index in 1.1.x | |
1041 | // restored for ICU4C port | |
1042 | else { | |
1043 | if (tempPP.getErrorIndex() > 0) { | |
1044 | pp.setErrorIndex(tempPP.getErrorIndex()); | |
1045 | } else { | |
1046 | pp.setErrorIndex(tempPP.getIndex()); | |
1047 | } | |
1048 | } | |
1049 | } | |
1050 | ||
1051 | // if we didn't match the substitution, search for another | |
1052 | // copy of "delimiter" in "text" and repeat the loop if | |
1053 | // we find it | |
1054 | tempPP.setIndex(0); | |
1055 | dPos = findText(text, delimiter, dPos + dLen, &dLen); | |
1056 | } | |
1057 | // if we make it here, this was an unsuccessful match, and we | |
1058 | // leave pp unchanged and return 0 | |
1059 | pp.setIndex(0); | |
1060 | return 0; | |
1061 | ||
1062 | // if "delimiter" is empty, or consists only of ignorable characters | |
1063 | // (i.e., is semantically empty), thwe we obviously can't search | |
1064 | // for "delimiter". Instead, just use "sub" to parse as much of | |
1065 | // "text" as possible. | |
1066 | } else { | |
1067 | ParsePosition tempPP; | |
1068 | Formattable result; | |
1069 | ||
1070 | // try to match the whole string against the substitution | |
1071 | UBool success = sub->doParse(text, tempPP, _baseValue, upperBound, | |
1072 | #if UCONFIG_NO_COLLATION | |
1073 | FALSE, | |
1074 | #else | |
1075 | formatter->isLenient(), | |
1076 | #endif | |
1077 | result); | |
1078 | if (success && (tempPP.getIndex() != 0 || sub->isNullSubstitution())) { | |
1079 | // if there's a successful match (or it's a null | |
1080 | // substitution), update pp to point to the first | |
1081 | // character we didn't match, and pass the result from | |
1082 | // sub.doParse() on through to the caller | |
1083 | pp.setIndex(tempPP.getIndex()); | |
1084 | return result.getDouble(); | |
1085 | } | |
1086 | // commented out because ParsePosition doesn't have error index in 1.1.x | |
1087 | // restored for ICU4C port | |
1088 | else { | |
1089 | pp.setErrorIndex(tempPP.getErrorIndex()); | |
1090 | } | |
1091 | ||
1092 | // and if we get to here, then nothing matched, so we return | |
1093 | // 0 and leave pp alone | |
1094 | return 0; | |
1095 | } | |
1096 | } | |
1097 | ||
1098 | /** | |
1099 | * Used by stripPrefix() to match characters. If lenient parse mode | |
1100 | * is off, this just calls startsWith(). If lenient parse mode is on, | |
1101 | * this function uses CollationElementIterators to match characters in | |
1102 | * the strings (only primary-order differences are significant in | |
1103 | * determining whether there's a match). | |
1104 | * @param str The string being tested | |
1105 | * @param prefix The text we're hoping to see at the beginning | |
1106 | * of "str" | |
1107 | * @return If "prefix" is found at the beginning of "str", this | |
1108 | * is the number of characters in "str" that were matched (this | |
1109 | * isn't necessarily the same as the length of "prefix" when matching | |
1110 | * text with a collator). If there's no match, this is 0. | |
1111 | */ | |
1112 | int32_t | |
1113 | NFRule::prefixLength(const UnicodeString& str, const UnicodeString& prefix) const | |
1114 | { | |
1115 | // if we're looking for an empty prefix, it obviously matches | |
1116 | // zero characters. Just go ahead and return 0. | |
1117 | if (prefix.length() == 0) { | |
1118 | return 0; | |
1119 | } | |
1120 | ||
1121 | #if !UCONFIG_NO_COLLATION | |
1122 | // go through all this grief if we're in lenient-parse mode | |
1123 | if (formatter->isLenient()) { | |
1124 | // get the formatter's collator and use it to create two | |
1125 | // collation element iterators, one over the target string | |
1126 | // and another over the prefix (right now, we'll throw an | |
1127 | // exception if the collator we get back from the formatter | |
1128 | // isn't a RuleBasedCollator, because RuleBasedCollator defines | |
1129 | // the CollationElementIterator protocol. Hopefully, this | |
1130 | // will change someday.) | |
1131 | RuleBasedCollator* collator = (RuleBasedCollator*)formatter->getCollator(); | |
1132 | CollationElementIterator* strIter = collator->createCollationElementIterator(str); | |
1133 | CollationElementIterator* prefixIter = collator->createCollationElementIterator(prefix); | |
1134 | ||
1135 | UErrorCode err = U_ZERO_ERROR; | |
1136 | ||
1137 | // The original code was problematic. Consider this match: | |
1138 | // prefix = "fifty-" | |
1139 | // string = " fifty-7" | |
1140 | // The intent is to match string up to the '7', by matching 'fifty-' at position 1 | |
1141 | // in the string. Unfortunately, we were getting a match, and then computing where | |
1142 | // the match terminated by rematching the string. The rematch code was using as an | |
1143 | // initial guess the substring of string between 0 and prefix.length. Because of | |
1144 | // the leading space and trailing hyphen (both ignorable) this was succeeding, leaving | |
1145 | // the position before the hyphen in the string. Recursing down, we then parsed the | |
1146 | // remaining string '-7' as numeric. The resulting number turned out as 43 (50 - 7). | |
1147 | // This was not pretty, especially since the string "fifty-7" parsed just fine. | |
1148 | // | |
1149 | // We have newer APIs now, so we can use calls on the iterator to determine what we | |
1150 | // matched up to. If we terminate because we hit the last element in the string, | |
1151 | // our match terminates at this length. If we terminate because we hit the last element | |
1152 | // in the target, our match terminates at one before the element iterator position. | |
1153 | ||
1154 | // match collation elements between the strings | |
1155 | int32_t oStr = strIter->next(err); | |
1156 | int32_t oPrefix = prefixIter->next(err); | |
1157 | ||
1158 | while (oPrefix != CollationElementIterator::NULLORDER) { | |
1159 | // skip over ignorable characters in the target string | |
1160 | while (CollationElementIterator::primaryOrder(oStr) == 0 | |
1161 | && oStr != CollationElementIterator::NULLORDER) { | |
1162 | oStr = strIter->next(err); | |
1163 | } | |
1164 | ||
1165 | // skip over ignorable characters in the prefix | |
1166 | while (CollationElementIterator::primaryOrder(oPrefix) == 0 | |
1167 | && oPrefix != CollationElementIterator::NULLORDER) { | |
1168 | oPrefix = prefixIter->next(err); | |
1169 | } | |
1170 | ||
1171 | // dlf: move this above following test, if we consume the | |
1172 | // entire target, aren't we ok even if the source was also | |
1173 | // entirely consumed? | |
1174 | ||
1175 | // if skipping over ignorables brought to the end of | |
1176 | // the prefix, we DID match: drop out of the loop | |
1177 | if (oPrefix == CollationElementIterator::NULLORDER) { | |
1178 | break; | |
1179 | } | |
1180 | ||
1181 | // if skipping over ignorables brought us to the end | |
1182 | // of the target string, we didn't match and return 0 | |
1183 | if (oStr == CollationElementIterator::NULLORDER) { | |
1184 | delete prefixIter; | |
1185 | delete strIter; | |
1186 | return 0; | |
1187 | } | |
1188 | ||
1189 | // match collation elements from the two strings | |
1190 | // (considering only primary differences). If we | |
1191 | // get a mismatch, dump out and return 0 | |
1192 | if (CollationElementIterator::primaryOrder(oStr) | |
1193 | != CollationElementIterator::primaryOrder(oPrefix)) { | |
1194 | delete prefixIter; | |
1195 | delete strIter; | |
1196 | return 0; | |
1197 | ||
1198 | // otherwise, advance to the next character in each string | |
1199 | // and loop (we drop out of the loop when we exhaust | |
1200 | // collation elements in the prefix) | |
1201 | } else { | |
1202 | oStr = strIter->next(err); | |
1203 | oPrefix = prefixIter->next(err); | |
1204 | } | |
1205 | } | |
1206 | ||
1207 | int32_t result = strIter->getOffset(); | |
1208 | if (oStr != CollationElementIterator::NULLORDER) { | |
1209 | --result; // back over character that we don't want to consume; | |
1210 | } | |
1211 | ||
1212 | #ifdef RBNF_DEBUG | |
1213 | fprintf(stderr, "prefix length: %d\n", result); | |
1214 | #endif | |
1215 | delete prefixIter; | |
1216 | delete strIter; | |
1217 | ||
1218 | return result; | |
1219 | #if 0 | |
1220 | //---------------------------------------------------------------- | |
1221 | // JDK 1.2-specific API call | |
1222 | // return strIter.getOffset(); | |
1223 | //---------------------------------------------------------------- | |
1224 | // JDK 1.1 HACK (take out for 1.2-specific code) | |
1225 | ||
1226 | // if we make it to here, we have a successful match. Now we | |
1227 | // have to find out HOW MANY characters from the target string | |
1228 | // matched the prefix (there isn't necessarily a one-to-one | |
1229 | // mapping between collation elements and characters). | |
1230 | // In JDK 1.2, there's a simple getOffset() call we can use. | |
1231 | // In JDK 1.1, on the other hand, we have to go through some | |
1232 | // ugly contortions. First, use the collator to compare the | |
1233 | // same number of characters from the prefix and target string. | |
1234 | // If they're equal, we're done. | |
1235 | collator->setStrength(Collator::PRIMARY); | |
1236 | if (str.length() >= prefix.length()) { | |
1237 | UnicodeString temp; | |
1238 | temp.setTo(str, 0, prefix.length()); | |
1239 | if (collator->equals(temp, prefix)) { | |
1240 | #ifdef RBNF_DEBUG | |
1241 | fprintf(stderr, "returning: %d\n", prefix.length()); | |
1242 | #endif | |
1243 | return prefix.length(); | |
1244 | } | |
1245 | } | |
1246 | ||
1247 | // if they're not equal, then we have to compare successively | |
1248 | // larger and larger substrings of the target string until we | |
1249 | // get to one that matches the prefix. At that point, we know | |
1250 | // how many characters matched the prefix, and we can return. | |
1251 | int32_t p = 1; | |
1252 | while (p <= str.length()) { | |
1253 | UnicodeString temp; | |
1254 | temp.setTo(str, 0, p); | |
1255 | if (collator->equals(temp, prefix)) { | |
1256 | return p; | |
1257 | } else { | |
1258 | ++p; | |
1259 | } | |
1260 | } | |
1261 | ||
1262 | // SHOULD NEVER GET HERE!!! | |
1263 | return 0; | |
1264 | //---------------------------------------------------------------- | |
1265 | #endif | |
1266 | ||
1267 | // If lenient parsing is turned off, forget all that crap above. | |
1268 | // Just use String.startsWith() and be done with it. | |
1269 | } else | |
1270 | #endif | |
1271 | { | |
1272 | if (str.startsWith(prefix)) { | |
1273 | return prefix.length(); | |
1274 | } else { | |
1275 | return 0; | |
1276 | } | |
1277 | } | |
1278 | } | |
1279 | ||
1280 | /** | |
1281 | * Searches a string for another string. If lenient parsing is off, | |
1282 | * this just calls indexOf(). If lenient parsing is on, this function | |
1283 | * uses CollationElementIterator to match characters, and only | |
1284 | * primary-order differences are significant in determining whether | |
1285 | * there's a match. | |
1286 | * @param str The string to search | |
1287 | * @param key The string to search "str" for | |
1288 | * @param startingAt The index into "str" where the search is to | |
1289 | * begin | |
1290 | * @return A two-element array of ints. Element 0 is the position | |
1291 | * of the match, or -1 if there was no match. Element 1 is the | |
1292 | * number of characters in "str" that matched (which isn't necessarily | |
1293 | * the same as the length of "key") | |
1294 | */ | |
1295 | int32_t | |
1296 | NFRule::findText(const UnicodeString& str, | |
1297 | const UnicodeString& key, | |
1298 | int32_t startingAt, | |
1299 | int32_t* length) const | |
1300 | { | |
1301 | #if !UCONFIG_NO_COLLATION | |
1302 | // if lenient parsing is turned off, this is easy: just call | |
1303 | // String.indexOf() and we're done | |
1304 | if (!formatter->isLenient()) { | |
1305 | *length = key.length(); | |
1306 | return str.indexOf(key, startingAt); | |
1307 | ||
1308 | // but if lenient parsing is turned ON, we've got some work | |
1309 | // ahead of us | |
1310 | } else | |
1311 | #endif | |
1312 | { | |
1313 | //---------------------------------------------------------------- | |
1314 | // JDK 1.1 HACK (take out of 1.2-specific code) | |
1315 | ||
1316 | // in JDK 1.2, CollationElementIterator provides us with an | |
1317 | // API to map between character offsets and collation elements | |
1318 | // and we can do this by marching through the string comparing | |
1319 | // collation elements. We can't do that in JDK 1.1. Insted, | |
1320 | // we have to go through this horrible slow mess: | |
1321 | int32_t p = startingAt; | |
1322 | int32_t keyLen = 0; | |
1323 | ||
1324 | // basically just isolate smaller and smaller substrings of | |
1325 | // the target string (each running to the end of the string, | |
1326 | // and with the first one running from startingAt to the end) | |
1327 | // and then use prefixLength() to see if the search key is at | |
1328 | // the beginning of each substring. This is excruciatingly | |
1329 | // slow, but it will locate the key and tell use how long the | |
1330 | // matching text was. | |
1331 | UnicodeString temp; | |
1332 | while (p < str.length() && keyLen == 0) { | |
1333 | temp.setTo(str, p, str.length() - p); | |
1334 | keyLen = prefixLength(temp, key); | |
1335 | if (keyLen != 0) { | |
1336 | *length = keyLen; | |
1337 | return p; | |
1338 | } | |
1339 | ++p; | |
1340 | } | |
1341 | // if we make it to here, we didn't find it. Return -1 for the | |
1342 | // location. The length should be ignored, but set it to 0, | |
1343 | // which should be "safe" | |
1344 | *length = 0; | |
1345 | return -1; | |
1346 | ||
1347 | //---------------------------------------------------------------- | |
1348 | // JDK 1.2 version of this routine | |
1349 | //RuleBasedCollator collator = (RuleBasedCollator)formatter.getCollator(); | |
1350 | // | |
1351 | //CollationElementIterator strIter = collator.getCollationElementIterator(str); | |
1352 | //CollationElementIterator keyIter = collator.getCollationElementIterator(key); | |
1353 | // | |
1354 | //int keyStart = -1; | |
1355 | // | |
1356 | //str.setOffset(startingAt); | |
1357 | // | |
1358 | //int oStr = strIter.next(); | |
1359 | //int oKey = keyIter.next(); | |
1360 | //while (oKey != CollationElementIterator.NULLORDER) { | |
1361 | // while (oStr != CollationElementIterator.NULLORDER && | |
1362 | // CollationElementIterator.primaryOrder(oStr) == 0) | |
1363 | // oStr = strIter.next(); | |
1364 | // | |
1365 | // while (oKey != CollationElementIterator.NULLORDER && | |
1366 | // CollationElementIterator.primaryOrder(oKey) == 0) | |
1367 | // oKey = keyIter.next(); | |
1368 | // | |
1369 | // if (oStr == CollationElementIterator.NULLORDER) { | |
1370 | // return new int[] { -1, 0 }; | |
1371 | // } | |
1372 | // | |
1373 | // if (oKey == CollationElementIterator.NULLORDER) { | |
1374 | // break; | |
1375 | // } | |
1376 | // | |
1377 | // if (CollationElementIterator.primaryOrder(oStr) == | |
1378 | // CollationElementIterator.primaryOrder(oKey)) { | |
1379 | // keyStart = strIter.getOffset(); | |
1380 | // oStr = strIter.next(); | |
1381 | // oKey = keyIter.next(); | |
1382 | // } else { | |
1383 | // if (keyStart != -1) { | |
1384 | // keyStart = -1; | |
1385 | // keyIter.reset(); | |
1386 | // } else { | |
1387 | // oStr = strIter.next(); | |
1388 | // } | |
1389 | // } | |
1390 | //} | |
1391 | // | |
1392 | //if (oKey == CollationElementIterator.NULLORDER) { | |
1393 | // return new int[] { keyStart, strIter.getOffset() - keyStart }; | |
1394 | //} else { | |
1395 | // return new int[] { -1, 0 }; | |
1396 | //} | |
1397 | } | |
1398 | } | |
1399 | ||
1400 | /** | |
1401 | * Checks to see whether a string consists entirely of ignorable | |
1402 | * characters. | |
1403 | * @param str The string to test. | |
1404 | * @return true if the string is empty of consists entirely of | |
1405 | * characters that the number formatter's collator says are | |
1406 | * ignorable at the primary-order level. false otherwise. | |
1407 | */ | |
1408 | UBool | |
1409 | NFRule::allIgnorable(const UnicodeString& str) const | |
1410 | { | |
1411 | // if the string is empty, we can just return true | |
1412 | if (str.length() == 0) { | |
1413 | return TRUE; | |
1414 | } | |
1415 | ||
1416 | #if !UCONFIG_NO_COLLATION | |
1417 | // if lenient parsing is turned on, walk through the string with | |
1418 | // a collation element iterator and make sure each collation | |
1419 | // element is 0 (ignorable) at the primary level | |
1420 | if (formatter->isLenient()) { | |
1421 | RuleBasedCollator* collator = (RuleBasedCollator*)(formatter->getCollator()); | |
1422 | CollationElementIterator* iter = collator->createCollationElementIterator(str); | |
1423 | ||
1424 | UErrorCode err = U_ZERO_ERROR; | |
1425 | int32_t o = iter->next(err); | |
1426 | while (o != CollationElementIterator::NULLORDER | |
1427 | && CollationElementIterator::primaryOrder(o) == 0) { | |
1428 | o = iter->next(err); | |
1429 | } | |
1430 | ||
1431 | delete iter; | |
1432 | return o == CollationElementIterator::NULLORDER; | |
1433 | } | |
1434 | #endif | |
1435 | ||
1436 | // if lenient parsing is turned off, there is no such thing as | |
1437 | // an ignorable character: return true only if the string is empty | |
1438 | return FALSE; | |
1439 | } | |
1440 | ||
1441 | U_NAMESPACE_END | |
1442 | ||
1443 | /* U_HAVE_RBNF */ | |
1444 | #endif | |
1445 | ||
1446 |