/src/icu/source/common/rbbi.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // © 2016 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | /* |
4 | | *************************************************************************** |
5 | | * Copyright (C) 1999-2016 International Business Machines Corporation |
6 | | * and others. All rights reserved. |
7 | | *************************************************************************** |
8 | | */ |
9 | | // |
10 | | // file: rbbi.cpp Contains the implementation of the rule based break iterator |
11 | | // runtime engine and the API implementation for |
12 | | // class RuleBasedBreakIterator |
13 | | // |
14 | | |
15 | | #include "utypeinfo.h" // for 'typeid' to work |
16 | | |
17 | | #include "unicode/utypes.h" |
18 | | |
19 | | #if !UCONFIG_NO_BREAK_ITERATION |
20 | | |
21 | | #include <cinttypes> |
22 | | |
23 | | #include "unicode/rbbi.h" |
24 | | #include "unicode/schriter.h" |
25 | | #include "unicode/uchriter.h" |
26 | | #include "unicode/uclean.h" |
27 | | #include "unicode/udata.h" |
28 | | |
29 | | #include "brkeng.h" |
30 | | #include "ucln_cmn.h" |
31 | | #include "cmemory.h" |
32 | | #include "cstring.h" |
33 | | #include "localsvc.h" |
34 | | #include "rbbidata.h" |
35 | | #include "rbbi_cache.h" |
36 | | #include "rbbirb.h" |
37 | | #include "uassert.h" |
38 | | #include "umutex.h" |
39 | | #include "uvectr32.h" |
40 | | |
41 | | #ifdef RBBI_DEBUG |
42 | | static UBool gTrace = FALSE; |
43 | | #endif |
44 | | |
45 | | U_NAMESPACE_BEGIN |
46 | | |
47 | | // The state number of the starting state |
48 | | constexpr int32_t START_STATE = 1; |
49 | | |
50 | | // The state-transition value indicating "stop" |
51 | | constexpr int32_t STOP_STATE = 0; |
52 | | |
53 | | |
54 | | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RuleBasedBreakIterator) |
55 | | |
56 | | |
57 | | //======================================================================= |
58 | | // constructors |
59 | | //======================================================================= |
60 | | |
61 | | /** |
62 | | * Constructs a RuleBasedBreakIterator that uses the already-created |
63 | | * tables object that is passed in as a parameter. |
64 | | */ |
65 | | RuleBasedBreakIterator::RuleBasedBreakIterator(RBBIDataHeader* data, UErrorCode &status) |
66 | 0 | : fSCharIter(UnicodeString()) |
67 | 0 | { |
68 | 0 | init(status); |
69 | 0 | fData = new RBBIDataWrapper(data, status); // status checked in constructor |
70 | 0 | if (U_FAILURE(status)) {return;} |
71 | 0 | if(fData == nullptr) { |
72 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
73 | 0 | return; |
74 | 0 | } |
75 | 0 | if (fData->fForwardTable->fLookAheadResultsSize > 0) { |
76 | 0 | fLookAheadMatches = static_cast<int32_t *>( |
77 | 0 | uprv_malloc(fData->fForwardTable->fLookAheadResultsSize * sizeof(int32_t))); |
78 | 0 | if (fLookAheadMatches == nullptr) { |
79 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
80 | 0 | return; |
81 | 0 | } |
82 | 0 | } |
83 | 0 | } |
84 | | |
85 | | // |
86 | | // Construct from precompiled binary rules (tables). This constructor is public API, |
87 | | // taking the rules as a (const uint8_t *) to match the type produced by getBinaryRules(). |
88 | | // |
89 | | RuleBasedBreakIterator::RuleBasedBreakIterator(const uint8_t *compiledRules, |
90 | | uint32_t ruleLength, |
91 | | UErrorCode &status) |
92 | 0 | : fSCharIter(UnicodeString()) |
93 | 0 | { |
94 | 0 | init(status); |
95 | 0 | if (U_FAILURE(status)) { |
96 | 0 | return; |
97 | 0 | } |
98 | 0 | if (compiledRules == NULL || ruleLength < sizeof(RBBIDataHeader)) { |
99 | 0 | status = U_ILLEGAL_ARGUMENT_ERROR; |
100 | 0 | return; |
101 | 0 | } |
102 | 0 | const RBBIDataHeader *data = (const RBBIDataHeader *)compiledRules; |
103 | 0 | if (data->fLength > ruleLength) { |
104 | 0 | status = U_ILLEGAL_ARGUMENT_ERROR; |
105 | 0 | return; |
106 | 0 | } |
107 | 0 | fData = new RBBIDataWrapper(data, RBBIDataWrapper::kDontAdopt, status); |
108 | 0 | if (U_FAILURE(status)) {return;} |
109 | 0 | if(fData == nullptr) { |
110 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
111 | 0 | return; |
112 | 0 | } |
113 | 0 | if (fData->fForwardTable->fLookAheadResultsSize > 0) { |
114 | 0 | fLookAheadMatches = static_cast<int32_t *>( |
115 | 0 | uprv_malloc(fData->fForwardTable->fLookAheadResultsSize * sizeof(int32_t))); |
116 | 0 | if (fLookAheadMatches == nullptr) { |
117 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
118 | 0 | return; |
119 | 0 | } |
120 | 0 | } |
121 | 0 | } |
122 | | |
123 | | |
124 | | //------------------------------------------------------------------------------- |
125 | | // |
126 | | // Constructor from a UDataMemory handle to precompiled break rules |
127 | | // stored in an ICU data file. |
128 | | // |
129 | | //------------------------------------------------------------------------------- |
130 | | RuleBasedBreakIterator::RuleBasedBreakIterator(UDataMemory* udm, UErrorCode &status) |
131 | 0 | : fSCharIter(UnicodeString()) |
132 | 0 | { |
133 | 0 | init(status); |
134 | 0 | fData = new RBBIDataWrapper(udm, status); // status checked in constructor |
135 | 0 | if (U_FAILURE(status)) {return;} |
136 | 0 | if(fData == nullptr) { |
137 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
138 | 0 | return; |
139 | 0 | } |
140 | 0 | if (fData->fForwardTable->fLookAheadResultsSize > 0) { |
141 | 0 | fLookAheadMatches = static_cast<int32_t *>( |
142 | 0 | uprv_malloc(fData->fForwardTable->fLookAheadResultsSize * sizeof(int32_t))); |
143 | 0 | if (fLookAheadMatches == nullptr) { |
144 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
145 | 0 | return; |
146 | 0 | } |
147 | 0 | } |
148 | 0 | } |
149 | | |
150 | | |
151 | | |
152 | | //------------------------------------------------------------------------------- |
153 | | // |
154 | | // Constructor from a set of rules supplied as a string. |
155 | | // |
156 | | //------------------------------------------------------------------------------- |
157 | | RuleBasedBreakIterator::RuleBasedBreakIterator( const UnicodeString &rules, |
158 | | UParseError &parseError, |
159 | | UErrorCode &status) |
160 | 0 | : fSCharIter(UnicodeString()) |
161 | 0 | { |
162 | 0 | init(status); |
163 | 0 | if (U_FAILURE(status)) {return;} |
164 | 0 | RuleBasedBreakIterator *bi = (RuleBasedBreakIterator *) |
165 | 0 | RBBIRuleBuilder::createRuleBasedBreakIterator(rules, &parseError, status); |
166 | | // Note: This is a bit awkward. The RBBI ruleBuilder has a factory method that |
167 | | // creates and returns a complete RBBI. From here, in a constructor, we |
168 | | // can't just return the object created by the builder factory, hence |
169 | | // the assignment of the factory created object to "this". |
170 | 0 | if (U_SUCCESS(status)) { |
171 | 0 | *this = *bi; |
172 | 0 | delete bi; |
173 | 0 | } |
174 | 0 | } |
175 | | |
176 | | |
177 | | //------------------------------------------------------------------------------- |
178 | | // |
179 | | // Default Constructor. Create an empty shell that can be set up later. |
180 | | // Used when creating a RuleBasedBreakIterator from a set |
181 | | // of rules. |
182 | | //------------------------------------------------------------------------------- |
183 | | RuleBasedBreakIterator::RuleBasedBreakIterator() |
184 | 0 | : fSCharIter(UnicodeString()) |
185 | 0 | { |
186 | 0 | UErrorCode status = U_ZERO_ERROR; |
187 | 0 | init(status); |
188 | 0 | } |
189 | | |
190 | | |
191 | | //------------------------------------------------------------------------------- |
192 | | // |
193 | | // Copy constructor. Will produce a break iterator with the same behavior, |
194 | | // and which iterates over the same text, as the one passed in. |
195 | | // |
196 | | //------------------------------------------------------------------------------- |
197 | | RuleBasedBreakIterator::RuleBasedBreakIterator(const RuleBasedBreakIterator& other) |
198 | 0 | : BreakIterator(other), |
199 | 0 | fSCharIter(UnicodeString()) |
200 | 0 | { |
201 | 0 | UErrorCode status = U_ZERO_ERROR; |
202 | 0 | this->init(status); |
203 | 0 | *this = other; |
204 | 0 | } |
205 | | |
206 | | |
207 | | /** |
208 | | * Destructor |
209 | | */ |
210 | 0 | RuleBasedBreakIterator::~RuleBasedBreakIterator() { |
211 | 0 | if (fCharIter != &fSCharIter) { |
212 | | // fCharIter was adopted from the outside. |
213 | 0 | delete fCharIter; |
214 | 0 | } |
215 | 0 | fCharIter = nullptr; |
216 | |
|
217 | 0 | utext_close(&fText); |
218 | |
|
219 | 0 | if (fData != nullptr) { |
220 | 0 | fData->removeReference(); |
221 | 0 | fData = nullptr; |
222 | 0 | } |
223 | 0 | delete fBreakCache; |
224 | 0 | fBreakCache = nullptr; |
225 | |
|
226 | 0 | delete fDictionaryCache; |
227 | 0 | fDictionaryCache = nullptr; |
228 | |
|
229 | 0 | delete fLanguageBreakEngines; |
230 | 0 | fLanguageBreakEngines = nullptr; |
231 | |
|
232 | 0 | delete fUnhandledBreakEngine; |
233 | 0 | fUnhandledBreakEngine = nullptr; |
234 | |
|
235 | 0 | uprv_free(fLookAheadMatches); |
236 | 0 | fLookAheadMatches = nullptr; |
237 | 0 | } |
238 | | |
239 | | /** |
240 | | * Assignment operator. Sets this iterator to have the same behavior, |
241 | | * and iterate over the same text, as the one passed in. |
242 | | * TODO: needs better handling of memory allocation errors. |
243 | | */ |
244 | | RuleBasedBreakIterator& |
245 | 0 | RuleBasedBreakIterator::operator=(const RuleBasedBreakIterator& that) { |
246 | 0 | if (this == &that) { |
247 | 0 | return *this; |
248 | 0 | } |
249 | 0 | BreakIterator::operator=(that); |
250 | |
|
251 | 0 | if (fLanguageBreakEngines != NULL) { |
252 | 0 | delete fLanguageBreakEngines; |
253 | 0 | fLanguageBreakEngines = NULL; // Just rebuild for now |
254 | 0 | } |
255 | | // TODO: clone fLanguageBreakEngines from "that" |
256 | 0 | UErrorCode status = U_ZERO_ERROR; |
257 | 0 | utext_clone(&fText, &that.fText, FALSE, TRUE, &status); |
258 | |
|
259 | 0 | if (fCharIter != &fSCharIter) { |
260 | 0 | delete fCharIter; |
261 | 0 | } |
262 | 0 | fCharIter = &fSCharIter; |
263 | |
|
264 | 0 | if (that.fCharIter != NULL && that.fCharIter != &that.fSCharIter) { |
265 | | // This is a little bit tricky - it will initially appear that |
266 | | // this->fCharIter is adopted, even if that->fCharIter was |
267 | | // not adopted. That's ok. |
268 | 0 | fCharIter = that.fCharIter->clone(); |
269 | 0 | } |
270 | 0 | fSCharIter = that.fSCharIter; |
271 | 0 | if (fCharIter == NULL) { |
272 | 0 | fCharIter = &fSCharIter; |
273 | 0 | } |
274 | |
|
275 | 0 | if (fData != NULL) { |
276 | 0 | fData->removeReference(); |
277 | 0 | fData = NULL; |
278 | 0 | } |
279 | 0 | if (that.fData != NULL) { |
280 | 0 | fData = that.fData->addReference(); |
281 | 0 | } |
282 | |
|
283 | 0 | uprv_free(fLookAheadMatches); |
284 | 0 | fLookAheadMatches = nullptr; |
285 | 0 | if (fData && fData->fForwardTable->fLookAheadResultsSize > 0) { |
286 | 0 | fLookAheadMatches = static_cast<int32_t *>( |
287 | 0 | uprv_malloc(fData->fForwardTable->fLookAheadResultsSize * sizeof(int32_t))); |
288 | 0 | } |
289 | | |
290 | |
|
291 | 0 | fPosition = that.fPosition; |
292 | 0 | fRuleStatusIndex = that.fRuleStatusIndex; |
293 | 0 | fDone = that.fDone; |
294 | | |
295 | | // TODO: both the dictionary and the main cache need to be copied. |
296 | | // Current position could be within a dictionary range. Trying to continue |
297 | | // the iteration without the caches present would go to the rules, with |
298 | | // the assumption that the current position is on a rule boundary. |
299 | 0 | fBreakCache->reset(fPosition, fRuleStatusIndex); |
300 | 0 | fDictionaryCache->reset(); |
301 | |
|
302 | 0 | return *this; |
303 | 0 | } |
304 | | |
305 | | |
306 | | |
307 | | //----------------------------------------------------------------------------- |
308 | | // |
309 | | // init() Shared initialization routine. Used by all the constructors. |
310 | | // Initializes all fields, leaving the object in a consistent state. |
311 | | // |
312 | | //----------------------------------------------------------------------------- |
313 | 0 | void RuleBasedBreakIterator::init(UErrorCode &status) { |
314 | 0 | fCharIter = nullptr; |
315 | 0 | fData = nullptr; |
316 | 0 | fPosition = 0; |
317 | 0 | fRuleStatusIndex = 0; |
318 | 0 | fDone = false; |
319 | 0 | fDictionaryCharCount = 0; |
320 | 0 | fLanguageBreakEngines = nullptr; |
321 | 0 | fUnhandledBreakEngine = nullptr; |
322 | 0 | fBreakCache = nullptr; |
323 | 0 | fDictionaryCache = nullptr; |
324 | 0 | fLookAheadMatches = nullptr; |
325 | | |
326 | | // Note: IBM xlC is unable to assign or initialize member fText from UTEXT_INITIALIZER. |
327 | | // fText = UTEXT_INITIALIZER; |
328 | 0 | static const UText initializedUText = UTEXT_INITIALIZER; |
329 | 0 | uprv_memcpy(&fText, &initializedUText, sizeof(UText)); |
330 | |
|
331 | 0 | if (U_FAILURE(status)) { |
332 | 0 | return; |
333 | 0 | } |
334 | | |
335 | 0 | utext_openUChars(&fText, NULL, 0, &status); |
336 | 0 | fDictionaryCache = new DictionaryCache(this, status); |
337 | 0 | fBreakCache = new BreakCache(this, status); |
338 | 0 | if (U_SUCCESS(status) && (fDictionaryCache == NULL || fBreakCache == NULL)) { |
339 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
340 | 0 | } |
341 | |
|
342 | | #ifdef RBBI_DEBUG |
343 | | static UBool debugInitDone = FALSE; |
344 | | if (debugInitDone == FALSE) { |
345 | | char *debugEnv = getenv("U_RBBIDEBUG"); |
346 | | if (debugEnv && uprv_strstr(debugEnv, "trace")) { |
347 | | gTrace = TRUE; |
348 | | } |
349 | | debugInitDone = TRUE; |
350 | | } |
351 | | #endif |
352 | 0 | } |
353 | | |
354 | | |
355 | | |
356 | | //----------------------------------------------------------------------------- |
357 | | // |
358 | | // clone - Returns a newly-constructed RuleBasedBreakIterator with the same |
359 | | // behavior, and iterating over the same text, as this one. |
360 | | // Virtual function: does the right thing with subclasses. |
361 | | // |
362 | | //----------------------------------------------------------------------------- |
363 | | RuleBasedBreakIterator* |
364 | 0 | RuleBasedBreakIterator::clone() const { |
365 | 0 | return new RuleBasedBreakIterator(*this); |
366 | 0 | } |
367 | | |
368 | | /** |
369 | | * Equality operator. Returns true if both BreakIterators are of the |
370 | | * same class, have the same behavior, and iterate over the same text. |
371 | | */ |
372 | | bool |
373 | 0 | RuleBasedBreakIterator::operator==(const BreakIterator& that) const { |
374 | 0 | if (typeid(*this) != typeid(that)) { |
375 | 0 | return FALSE; |
376 | 0 | } |
377 | 0 | if (this == &that) { |
378 | 0 | return TRUE; |
379 | 0 | } |
380 | | |
381 | | // The base class BreakIterator carries no state that participates in equality, |
382 | | // and does not implement an equality function that would otherwise be |
383 | | // checked at this point. |
384 | | |
385 | 0 | const RuleBasedBreakIterator& that2 = (const RuleBasedBreakIterator&) that; |
386 | |
|
387 | 0 | if (!utext_equals(&fText, &that2.fText)) { |
388 | | // The two break iterators are operating on different text, |
389 | | // or have a different iteration position. |
390 | | // Note that fText's position is always the same as the break iterator's position. |
391 | 0 | return FALSE; |
392 | 0 | } |
393 | | |
394 | 0 | if (!(fPosition == that2.fPosition && |
395 | 0 | fRuleStatusIndex == that2.fRuleStatusIndex && |
396 | 0 | fDone == that2.fDone)) { |
397 | 0 | return FALSE; |
398 | 0 | } |
399 | | |
400 | 0 | if (that2.fData == fData || |
401 | 0 | (fData != NULL && that2.fData != NULL && *that2.fData == *fData)) { |
402 | | // The two break iterators are using the same rules. |
403 | 0 | return TRUE; |
404 | 0 | } |
405 | 0 | return FALSE; |
406 | 0 | } |
407 | | |
408 | | /** |
409 | | * Compute a hash code for this BreakIterator |
410 | | * @return A hash code |
411 | | */ |
412 | | int32_t |
413 | 0 | RuleBasedBreakIterator::hashCode(void) const { |
414 | 0 | int32_t hash = 0; |
415 | 0 | if (fData != NULL) { |
416 | 0 | hash = fData->hashCode(); |
417 | 0 | } |
418 | 0 | return hash; |
419 | 0 | } |
420 | | |
421 | | |
422 | 0 | void RuleBasedBreakIterator::setText(UText *ut, UErrorCode &status) { |
423 | 0 | if (U_FAILURE(status)) { |
424 | 0 | return; |
425 | 0 | } |
426 | 0 | fBreakCache->reset(); |
427 | 0 | fDictionaryCache->reset(); |
428 | 0 | utext_clone(&fText, ut, FALSE, TRUE, &status); |
429 | | |
430 | | // Set up a dummy CharacterIterator to be returned if anyone |
431 | | // calls getText(). With input from UText, there is no reasonable |
432 | | // way to return a characterIterator over the actual input text. |
433 | | // Return one over an empty string instead - this is the closest |
434 | | // we can come to signaling a failure. |
435 | | // (GetText() is obsolete, this failure is sort of OK) |
436 | 0 | fSCharIter.setText(UnicodeString()); |
437 | |
|
438 | 0 | if (fCharIter != &fSCharIter) { |
439 | | // existing fCharIter was adopted from the outside. Delete it now. |
440 | 0 | delete fCharIter; |
441 | 0 | } |
442 | 0 | fCharIter = &fSCharIter; |
443 | |
|
444 | 0 | this->first(); |
445 | 0 | } |
446 | | |
447 | | |
448 | 0 | UText *RuleBasedBreakIterator::getUText(UText *fillIn, UErrorCode &status) const { |
449 | 0 | UText *result = utext_clone(fillIn, &fText, FALSE, TRUE, &status); |
450 | 0 | return result; |
451 | 0 | } |
452 | | |
453 | | |
454 | | //======================================================================= |
455 | | // BreakIterator overrides |
456 | | //======================================================================= |
457 | | |
458 | | /** |
459 | | * Return a CharacterIterator over the text being analyzed. |
460 | | */ |
461 | | CharacterIterator& |
462 | 0 | RuleBasedBreakIterator::getText() const { |
463 | 0 | return *fCharIter; |
464 | 0 | } |
465 | | |
466 | | /** |
467 | | * Set the iterator to analyze a new piece of text. This function resets |
468 | | * the current iteration position to the beginning of the text. |
469 | | * @param newText An iterator over the text to analyze. |
470 | | */ |
471 | | void |
472 | 0 | RuleBasedBreakIterator::adoptText(CharacterIterator* newText) { |
473 | | // If we are holding a CharacterIterator adopted from a |
474 | | // previous call to this function, delete it now. |
475 | 0 | if (fCharIter != &fSCharIter) { |
476 | 0 | delete fCharIter; |
477 | 0 | } |
478 | |
|
479 | 0 | fCharIter = newText; |
480 | 0 | UErrorCode status = U_ZERO_ERROR; |
481 | 0 | fBreakCache->reset(); |
482 | 0 | fDictionaryCache->reset(); |
483 | 0 | if (newText==NULL || newText->startIndex() != 0) { |
484 | | // startIndex !=0 wants to be an error, but there's no way to report it. |
485 | | // Make the iterator text be an empty string. |
486 | 0 | utext_openUChars(&fText, NULL, 0, &status); |
487 | 0 | } else { |
488 | 0 | utext_openCharacterIterator(&fText, newText, &status); |
489 | 0 | } |
490 | 0 | this->first(); |
491 | 0 | } |
492 | | |
493 | | /** |
494 | | * Set the iterator to analyze a new piece of text. This function resets |
495 | | * the current iteration position to the beginning of the text. |
496 | | * @param newText An iterator over the text to analyze. |
497 | | */ |
498 | | void |
499 | 0 | RuleBasedBreakIterator::setText(const UnicodeString& newText) { |
500 | 0 | UErrorCode status = U_ZERO_ERROR; |
501 | 0 | fBreakCache->reset(); |
502 | 0 | fDictionaryCache->reset(); |
503 | 0 | utext_openConstUnicodeString(&fText, &newText, &status); |
504 | | |
505 | | // Set up a character iterator on the string. |
506 | | // Needed in case someone calls getText(). |
507 | | // Can not, unfortunately, do this lazily on the (probably never) |
508 | | // call to getText(), because getText is const. |
509 | 0 | fSCharIter.setText(newText); |
510 | |
|
511 | 0 | if (fCharIter != &fSCharIter) { |
512 | | // old fCharIter was adopted from the outside. Delete it. |
513 | 0 | delete fCharIter; |
514 | 0 | } |
515 | 0 | fCharIter = &fSCharIter; |
516 | |
|
517 | 0 | this->first(); |
518 | 0 | } |
519 | | |
520 | | |
521 | | /** |
522 | | * Provide a new UText for the input text. Must reference text with contents identical |
523 | | * to the original. |
524 | | * Intended for use with text data originating in Java (garbage collected) environments |
525 | | * where the data may be moved in memory at arbitrary times. |
526 | | */ |
527 | 0 | RuleBasedBreakIterator &RuleBasedBreakIterator::refreshInputText(UText *input, UErrorCode &status) { |
528 | 0 | if (U_FAILURE(status)) { |
529 | 0 | return *this; |
530 | 0 | } |
531 | 0 | if (input == NULL) { |
532 | 0 | status = U_ILLEGAL_ARGUMENT_ERROR; |
533 | 0 | return *this; |
534 | 0 | } |
535 | 0 | int64_t pos = utext_getNativeIndex(&fText); |
536 | | // Shallow read-only clone of the new UText into the existing input UText |
537 | 0 | utext_clone(&fText, input, FALSE, TRUE, &status); |
538 | 0 | if (U_FAILURE(status)) { |
539 | 0 | return *this; |
540 | 0 | } |
541 | 0 | utext_setNativeIndex(&fText, pos); |
542 | 0 | if (utext_getNativeIndex(&fText) != pos) { |
543 | | // Sanity check. The new input utext is supposed to have the exact same |
544 | | // contents as the old. If we can't set to the same position, it doesn't. |
545 | | // The contents underlying the old utext might be invalid at this point, |
546 | | // so it's not safe to check directly. |
547 | 0 | status = U_ILLEGAL_ARGUMENT_ERROR; |
548 | 0 | } |
549 | 0 | return *this; |
550 | 0 | } |
551 | | |
552 | | |
553 | | /** |
554 | | * Sets the current iteration position to the beginning of the text, position zero. |
555 | | * @return The new iterator position, which is zero. |
556 | | */ |
557 | 0 | int32_t RuleBasedBreakIterator::first(void) { |
558 | 0 | UErrorCode status = U_ZERO_ERROR; |
559 | 0 | if (!fBreakCache->seek(0)) { |
560 | 0 | fBreakCache->populateNear(0, status); |
561 | 0 | } |
562 | 0 | fBreakCache->current(); |
563 | 0 | U_ASSERT(fPosition == 0); |
564 | 0 | return 0; |
565 | 0 | } |
566 | | |
567 | | /** |
568 | | * Sets the current iteration position to the end of the text. |
569 | | * @return The text's past-the-end offset. |
570 | | */ |
571 | 0 | int32_t RuleBasedBreakIterator::last(void) { |
572 | 0 | int32_t endPos = (int32_t)utext_nativeLength(&fText); |
573 | 0 | UBool endShouldBeBoundary = isBoundary(endPos); // Has side effect of setting iterator position. |
574 | 0 | (void)endShouldBeBoundary; |
575 | 0 | U_ASSERT(endShouldBeBoundary); |
576 | 0 | U_ASSERT(fPosition == endPos); |
577 | 0 | return endPos; |
578 | 0 | } |
579 | | |
580 | | /** |
581 | | * Advances the iterator either forward or backward the specified number of steps. |
582 | | * Negative values move backward, and positive values move forward. This is |
583 | | * equivalent to repeatedly calling next() or previous(). |
584 | | * @param n The number of steps to move. The sign indicates the direction |
585 | | * (negative is backwards, and positive is forwards). |
586 | | * @return The character offset of the boundary position n boundaries away from |
587 | | * the current one. |
588 | | */ |
589 | 0 | int32_t RuleBasedBreakIterator::next(int32_t n) { |
590 | 0 | int32_t result = 0; |
591 | 0 | if (n > 0) { |
592 | 0 | for (; n > 0 && result != UBRK_DONE; --n) { |
593 | 0 | result = next(); |
594 | 0 | } |
595 | 0 | } else if (n < 0) { |
596 | 0 | for (; n < 0 && result != UBRK_DONE; ++n) { |
597 | 0 | result = previous(); |
598 | 0 | } |
599 | 0 | } else { |
600 | 0 | result = current(); |
601 | 0 | } |
602 | 0 | return result; |
603 | 0 | } |
604 | | |
605 | | /** |
606 | | * Advances the iterator to the next boundary position. |
607 | | * @return The position of the first boundary after this one. |
608 | | */ |
609 | 0 | int32_t RuleBasedBreakIterator::next(void) { |
610 | 0 | fBreakCache->next(); |
611 | 0 | return fDone ? UBRK_DONE : fPosition; |
612 | 0 | } |
613 | | |
614 | | /** |
615 | | * Move the iterator backwards, to the boundary preceding the current one. |
616 | | * |
617 | | * Starts from the current position within fText. |
618 | | * Starting position need not be on a boundary. |
619 | | * |
620 | | * @return The position of the boundary position immediately preceding the starting position. |
621 | | */ |
622 | 0 | int32_t RuleBasedBreakIterator::previous(void) { |
623 | 0 | UErrorCode status = U_ZERO_ERROR; |
624 | 0 | fBreakCache->previous(status); |
625 | 0 | return fDone ? UBRK_DONE : fPosition; |
626 | 0 | } |
627 | | |
628 | | /** |
629 | | * Sets the iterator to refer to the first boundary position following |
630 | | * the specified position. |
631 | | * @param startPos The position from which to begin searching for a break position. |
632 | | * @return The position of the first break after the current position. |
633 | | */ |
634 | 0 | int32_t RuleBasedBreakIterator::following(int32_t startPos) { |
635 | | // if the supplied position is before the beginning, return the |
636 | | // text's starting offset |
637 | 0 | if (startPos < 0) { |
638 | 0 | return first(); |
639 | 0 | } |
640 | | |
641 | | // Move requested offset to a code point start. It might be on a trail surrogate, |
642 | | // or on a trail byte if the input is UTF-8. Or it may be beyond the end of the text. |
643 | 0 | utext_setNativeIndex(&fText, startPos); |
644 | 0 | startPos = (int32_t)utext_getNativeIndex(&fText); |
645 | |
|
646 | 0 | UErrorCode status = U_ZERO_ERROR; |
647 | 0 | fBreakCache->following(startPos, status); |
648 | 0 | return fDone ? UBRK_DONE : fPosition; |
649 | 0 | } |
650 | | |
651 | | /** |
652 | | * Sets the iterator to refer to the last boundary position before the |
653 | | * specified position. |
654 | | * @param offset The position to begin searching for a break from. |
655 | | * @return The position of the last boundary before the starting position. |
656 | | */ |
657 | 0 | int32_t RuleBasedBreakIterator::preceding(int32_t offset) { |
658 | 0 | if (offset > utext_nativeLength(&fText)) { |
659 | 0 | return last(); |
660 | 0 | } |
661 | | |
662 | | // Move requested offset to a code point start. It might be on a trail surrogate, |
663 | | // or on a trail byte if the input is UTF-8. |
664 | | |
665 | 0 | utext_setNativeIndex(&fText, offset); |
666 | 0 | int32_t adjustedOffset = static_cast<int32_t>(utext_getNativeIndex(&fText)); |
667 | |
|
668 | 0 | UErrorCode status = U_ZERO_ERROR; |
669 | 0 | fBreakCache->preceding(adjustedOffset, status); |
670 | 0 | return fDone ? UBRK_DONE : fPosition; |
671 | 0 | } |
672 | | |
673 | | /** |
674 | | * Returns true if the specified position is a boundary position. As a side |
675 | | * effect, leaves the iterator pointing to the first boundary position at |
676 | | * or after "offset". |
677 | | * |
678 | | * @param offset the offset to check. |
679 | | * @return True if "offset" is a boundary position. |
680 | | */ |
681 | 0 | UBool RuleBasedBreakIterator::isBoundary(int32_t offset) { |
682 | | // out-of-range indexes are never boundary positions |
683 | 0 | if (offset < 0) { |
684 | 0 | first(); // For side effects on current position, tag values. |
685 | 0 | return FALSE; |
686 | 0 | } |
687 | | |
688 | | // Adjust offset to be on a code point boundary and not beyond the end of the text. |
689 | | // Note that isBoundary() is always false for offsets that are not on code point boundaries. |
690 | | // But we still need the side effect of leaving iteration at the following boundary. |
691 | | |
692 | 0 | utext_setNativeIndex(&fText, offset); |
693 | 0 | int32_t adjustedOffset = static_cast<int32_t>(utext_getNativeIndex(&fText)); |
694 | |
|
695 | 0 | bool result = false; |
696 | 0 | UErrorCode status = U_ZERO_ERROR; |
697 | 0 | if (fBreakCache->seek(adjustedOffset) || fBreakCache->populateNear(adjustedOffset, status)) { |
698 | 0 | result = (fBreakCache->current() == offset); |
699 | 0 | } |
700 | |
|
701 | 0 | if (result && adjustedOffset < offset && utext_char32At(&fText, offset) == U_SENTINEL) { |
702 | | // Original offset is beyond the end of the text. Return FALSE, it's not a boundary, |
703 | | // but the iteration position remains set to the end of the text, which is a boundary. |
704 | 0 | return FALSE; |
705 | 0 | } |
706 | 0 | if (!result) { |
707 | | // Not on a boundary. isBoundary() must leave iterator on the following boundary. |
708 | | // Cache->seek(), above, left us on the preceding boundary, so advance one. |
709 | 0 | next(); |
710 | 0 | } |
711 | 0 | return result; |
712 | 0 | } |
713 | | |
714 | | |
715 | | /** |
716 | | * Returns the current iteration position. |
717 | | * @return The current iteration position. |
718 | | */ |
719 | 0 | int32_t RuleBasedBreakIterator::current(void) const { |
720 | 0 | return fPosition; |
721 | 0 | } |
722 | | |
723 | | |
724 | | //======================================================================= |
725 | | // implementation |
726 | | //======================================================================= |
727 | | |
728 | | // |
729 | | // RBBIRunMode - the state machine runs an extra iteration at the beginning and end |
730 | | // of user text. A variable with this enum type keeps track of where we |
731 | | // are. The state machine only fetches user input while in the RUN mode. |
732 | | // |
733 | | enum RBBIRunMode { |
734 | | RBBI_START, // state machine processing is before first char of input |
735 | | RBBI_RUN, // state machine processing is in the user text |
736 | | RBBI_END // state machine processing is after end of user text. |
737 | | }; |
738 | | |
739 | | |
740 | | // Wrapper functions to select the appropriate handleNext() or handleSafePrevious() |
741 | | // instantiation, based on whether an 8 or 16 bit table is required. |
742 | | // |
743 | | // These Trie access functions will be inlined within the handleNext()/Previous() instantions. |
744 | 0 | static inline uint16_t TrieFunc8(const UCPTrie *trie, UChar32 c) { |
745 | 0 | return UCPTRIE_FAST_GET(trie, UCPTRIE_8, c); |
746 | 0 | } |
747 | | |
748 | 0 | static inline uint16_t TrieFunc16(const UCPTrie *trie, UChar32 c) { |
749 | 0 | return UCPTRIE_FAST_GET(trie, UCPTRIE_16, c); |
750 | 0 | } |
751 | | |
752 | 0 | int32_t RuleBasedBreakIterator::handleNext() { |
753 | 0 | const RBBIStateTable *statetable = fData->fForwardTable; |
754 | 0 | bool use8BitsTrie = ucptrie_getValueWidth(fData->fTrie) == UCPTRIE_VALUE_BITS_8; |
755 | 0 | if (statetable->fFlags & RBBI_8BITS_ROWS) { |
756 | 0 | if (use8BitsTrie) { |
757 | 0 | return handleNext<RBBIStateTableRow8, TrieFunc8>(); |
758 | 0 | } else { |
759 | 0 | return handleNext<RBBIStateTableRow8, TrieFunc16>(); |
760 | 0 | } |
761 | 0 | } else { |
762 | 0 | if (use8BitsTrie) { |
763 | 0 | return handleNext<RBBIStateTableRow16, TrieFunc8>(); |
764 | 0 | } else { |
765 | 0 | return handleNext<RBBIStateTableRow16, TrieFunc16>(); |
766 | 0 | } |
767 | 0 | } |
768 | 0 | } |
769 | | |
770 | 0 | int32_t RuleBasedBreakIterator::handleSafePrevious(int32_t fromPosition) { |
771 | 0 | const RBBIStateTable *statetable = fData->fReverseTable; |
772 | 0 | bool use8BitsTrie = ucptrie_getValueWidth(fData->fTrie) == UCPTRIE_VALUE_BITS_8; |
773 | 0 | if (statetable->fFlags & RBBI_8BITS_ROWS) { |
774 | 0 | if (use8BitsTrie) { |
775 | 0 | return handleSafePrevious<RBBIStateTableRow8, TrieFunc8>(fromPosition); |
776 | 0 | } else { |
777 | 0 | return handleSafePrevious<RBBIStateTableRow8, TrieFunc16>(fromPosition); |
778 | 0 | } |
779 | 0 | } else { |
780 | 0 | if (use8BitsTrie) { |
781 | 0 | return handleSafePrevious<RBBIStateTableRow16, TrieFunc8>(fromPosition); |
782 | 0 | } else { |
783 | 0 | return handleSafePrevious<RBBIStateTableRow16, TrieFunc16>(fromPosition); |
784 | 0 | } |
785 | 0 | } |
786 | 0 | } |
787 | | |
788 | | |
789 | | //----------------------------------------------------------------------------------- |
790 | | // |
791 | | // handleNext() |
792 | | // Run the state machine to find a boundary |
793 | | // |
794 | | //----------------------------------------------------------------------------------- |
795 | | template <typename RowType, RuleBasedBreakIterator::PTrieFunc trieFunc> |
796 | 0 | int32_t RuleBasedBreakIterator::handleNext() { |
797 | 0 | int32_t state; |
798 | 0 | uint16_t category = 0; |
799 | 0 | RBBIRunMode mode; |
800 | |
|
801 | 0 | RowType *row; |
802 | 0 | UChar32 c; |
803 | 0 | int32_t result = 0; |
804 | 0 | int32_t initialPosition = 0; |
805 | 0 | const RBBIStateTable *statetable = fData->fForwardTable; |
806 | 0 | const char *tableData = statetable->fTableData; |
807 | 0 | uint32_t tableRowLen = statetable->fRowLen; |
808 | 0 | uint32_t dictStart = statetable->fDictCategoriesStart; |
809 | | #ifdef RBBI_DEBUG |
810 | | if (gTrace) { |
811 | | RBBIDebugPuts("Handle Next pos char state category"); |
812 | | } |
813 | | #endif |
814 | | |
815 | | // handleNext always sets the break tag value. |
816 | | // Set the default for it. |
817 | 0 | fRuleStatusIndex = 0; |
818 | |
|
819 | 0 | fDictionaryCharCount = 0; |
820 | | |
821 | | // if we're already at the end of the text, return DONE. |
822 | 0 | initialPosition = fPosition; |
823 | 0 | UTEXT_SETNATIVEINDEX(&fText, initialPosition); |
824 | 0 | result = initialPosition; |
825 | 0 | c = UTEXT_NEXT32(&fText); |
826 | 0 | if (c==U_SENTINEL) { |
827 | 0 | fDone = TRUE; |
828 | 0 | return UBRK_DONE; |
829 | 0 | } |
830 | | |
831 | | // Set the initial state for the state machine |
832 | 0 | state = START_STATE; |
833 | 0 | row = (RowType *) |
834 | | //(statetable->fTableData + (statetable->fRowLen * state)); |
835 | 0 | (tableData + tableRowLen * state); |
836 | | |
837 | |
|
838 | 0 | mode = RBBI_RUN; |
839 | 0 | if (statetable->fFlags & RBBI_BOF_REQUIRED) { |
840 | 0 | category = 2; |
841 | 0 | mode = RBBI_START; |
842 | 0 | } |
843 | | |
844 | | |
845 | | // loop until we reach the end of the text or transition to state 0 |
846 | | // |
847 | 0 | for (;;) { |
848 | 0 | if (c == U_SENTINEL) { |
849 | | // Reached end of input string. |
850 | 0 | if (mode == RBBI_END) { |
851 | | // We have already run the loop one last time with the |
852 | | // character set to the psueudo {eof} value. Now it is time |
853 | | // to unconditionally bail out. |
854 | 0 | break; |
855 | 0 | } |
856 | | // Run the loop one last time with the fake end-of-input character category. |
857 | 0 | mode = RBBI_END; |
858 | 0 | category = 1; |
859 | 0 | } |
860 | | |
861 | | // |
862 | | // Get the char category. An incoming category of 1 or 2 means that |
863 | | // we are preset for doing the beginning or end of input, and |
864 | | // that we shouldn't get a category from an actual text input character. |
865 | | // |
866 | 0 | if (mode == RBBI_RUN) { |
867 | | // look up the current character's character category, which tells us |
868 | | // which column in the state table to look at. |
869 | 0 | category = trieFunc(fData->fTrie, c); |
870 | 0 | fDictionaryCharCount += (category >= dictStart); |
871 | 0 | } |
872 | |
|
873 | | #ifdef RBBI_DEBUG |
874 | | if (gTrace) { |
875 | | RBBIDebugPrintf(" %4" PRId64 " ", utext_getNativeIndex(&fText)); |
876 | | if (0x20<=c && c<0x7f) { |
877 | | RBBIDebugPrintf("\"%c\" ", c); |
878 | | } else { |
879 | | RBBIDebugPrintf("%5x ", c); |
880 | | } |
881 | | RBBIDebugPrintf("%3d %3d\n", state, category); |
882 | | } |
883 | | #endif |
884 | | |
885 | | // State Transition - move machine to its next state |
886 | | // |
887 | | |
888 | | // fNextState is a variable-length array. |
889 | 0 | U_ASSERT(category<fData->fHeader->fCatCount); |
890 | 0 | state = row->fNextState[category]; /*Not accessing beyond memory*/ |
891 | 0 | row = (RowType *) |
892 | | // (statetable->fTableData + (statetable->fRowLen * state)); |
893 | 0 | (tableData + tableRowLen * state); |
894 | | |
895 | |
|
896 | 0 | uint16_t accepting = row->fAccepting; |
897 | 0 | if (accepting == ACCEPTING_UNCONDITIONAL) { |
898 | | // Match found, common case. |
899 | 0 | if (mode != RBBI_START) { |
900 | 0 | result = (int32_t)UTEXT_GETNATIVEINDEX(&fText); |
901 | 0 | } |
902 | 0 | fRuleStatusIndex = row->fTagsIdx; // Remember the break status (tag) values. |
903 | 0 | } else if (accepting > ACCEPTING_UNCONDITIONAL) { |
904 | | // Lookahead match is completed. |
905 | 0 | U_ASSERT(accepting < fData->fForwardTable->fLookAheadResultsSize); |
906 | 0 | int32_t lookaheadResult = fLookAheadMatches[accepting]; |
907 | 0 | if (lookaheadResult >= 0) { |
908 | 0 | fRuleStatusIndex = row->fTagsIdx; |
909 | 0 | fPosition = lookaheadResult; |
910 | 0 | return lookaheadResult; |
911 | 0 | } |
912 | 0 | } |
913 | | |
914 | | // If we are at the position of the '/' in a look-ahead (hard break) rule; |
915 | | // record the current position, to be returned later, if the full rule matches. |
916 | | // TODO: Move this check before the previous check of fAccepting. |
917 | | // This would enable hard-break rules with no following context. |
918 | | // But there are line break test failures when trying this. Investigate. |
919 | | // Issue ICU-20837 |
920 | 0 | uint16_t rule = row->fLookAhead; |
921 | 0 | U_ASSERT(rule == 0 || rule > ACCEPTING_UNCONDITIONAL); |
922 | 0 | U_ASSERT(rule == 0 || rule < fData->fForwardTable->fLookAheadResultsSize); |
923 | 0 | if (rule > ACCEPTING_UNCONDITIONAL) { |
924 | 0 | int32_t pos = (int32_t)UTEXT_GETNATIVEINDEX(&fText); |
925 | 0 | fLookAheadMatches[rule] = pos; |
926 | 0 | } |
927 | |
|
928 | 0 | if (state == STOP_STATE) { |
929 | | // This is the normal exit from the lookup state machine. |
930 | | // We have advanced through the string until it is certain that no |
931 | | // longer match is possible, no matter what characters follow. |
932 | 0 | break; |
933 | 0 | } |
934 | | |
935 | | // Advance to the next character. |
936 | | // If this is a beginning-of-input loop iteration, don't advance |
937 | | // the input position. The next iteration will be processing the |
938 | | // first real input character. |
939 | 0 | if (mode == RBBI_RUN) { |
940 | 0 | c = UTEXT_NEXT32(&fText); |
941 | 0 | } else { |
942 | 0 | if (mode == RBBI_START) { |
943 | 0 | mode = RBBI_RUN; |
944 | 0 | } |
945 | 0 | } |
946 | 0 | } |
947 | | |
948 | | // The state machine is done. Check whether it found a match... |
949 | | |
950 | | // If the iterator failed to advance in the match engine, force it ahead by one. |
951 | | // (This really indicates a defect in the break rules. They should always match |
952 | | // at least one character.) |
953 | 0 | if (result == initialPosition) { |
954 | 0 | utext_setNativeIndex(&fText, initialPosition); |
955 | 0 | utext_next32(&fText); |
956 | 0 | result = (int32_t)utext_getNativeIndex(&fText); |
957 | 0 | fRuleStatusIndex = 0; |
958 | 0 | } |
959 | | |
960 | | // Leave the iterator at our result position. |
961 | 0 | fPosition = result; |
962 | | #ifdef RBBI_DEBUG |
963 | | if (gTrace) { |
964 | | RBBIDebugPrintf("result = %d\n\n", result); |
965 | | } |
966 | | #endif |
967 | 0 | return result; |
968 | 0 | } Unexecuted instantiation: rbbi.cpp:int icu_70::RuleBasedBreakIterator::handleNext<icu_70::RBBIStateTableRowT<unsigned char>, &icu_70::TrieFunc8>() Unexecuted instantiation: rbbi.cpp:int icu_70::RuleBasedBreakIterator::handleNext<icu_70::RBBIStateTableRowT<unsigned char>, &icu_70::TrieFunc16>() Unexecuted instantiation: rbbi.cpp:int icu_70::RuleBasedBreakIterator::handleNext<icu_70::RBBIStateTableRowT<unsigned short>, &icu_70::TrieFunc8>() Unexecuted instantiation: rbbi.cpp:int icu_70::RuleBasedBreakIterator::handleNext<icu_70::RBBIStateTableRowT<unsigned short>, &icu_70::TrieFunc16>() |
969 | | |
970 | | |
971 | | //----------------------------------------------------------------------------------- |
972 | | // |
973 | | // handleSafePrevious() |
974 | | // |
975 | | // Iterate backwards using the safe reverse rules. |
976 | | // The logic of this function is similar to handleNext(), but simpler |
977 | | // because the safe table does not require as many options. |
978 | | // |
979 | | //----------------------------------------------------------------------------------- |
980 | | template <typename RowType, RuleBasedBreakIterator::PTrieFunc trieFunc> |
981 | 0 | int32_t RuleBasedBreakIterator::handleSafePrevious(int32_t fromPosition) { |
982 | |
|
983 | 0 | int32_t state; |
984 | 0 | uint16_t category = 0; |
985 | 0 | RowType *row; |
986 | 0 | UChar32 c; |
987 | 0 | int32_t result = 0; |
988 | |
|
989 | 0 | const RBBIStateTable *stateTable = fData->fReverseTable; |
990 | 0 | UTEXT_SETNATIVEINDEX(&fText, fromPosition); |
991 | | #ifdef RBBI_DEBUG |
992 | | if (gTrace) { |
993 | | RBBIDebugPuts("Handle Previous pos char state category"); |
994 | | } |
995 | | #endif |
996 | | |
997 | | // if we're already at the start of the text, return DONE. |
998 | 0 | if (fData == NULL || UTEXT_GETNATIVEINDEX(&fText)==0) { |
999 | 0 | return BreakIterator::DONE; |
1000 | 0 | } |
1001 | | |
1002 | | // Set the initial state for the state machine |
1003 | 0 | c = UTEXT_PREVIOUS32(&fText); |
1004 | 0 | state = START_STATE; |
1005 | 0 | row = (RowType *) |
1006 | 0 | (stateTable->fTableData + (stateTable->fRowLen * state)); |
1007 | | |
1008 | | // loop until we reach the start of the text or transition to state 0 |
1009 | | // |
1010 | 0 | for (; c != U_SENTINEL; c = UTEXT_PREVIOUS32(&fText)) { |
1011 | | |
1012 | | // look up the current character's character category, which tells us |
1013 | | // which column in the state table to look at. |
1014 | | // |
1015 | | // Off the dictionary flag bit. For reverse iteration it is not used. |
1016 | 0 | category = trieFunc(fData->fTrie, c); |
1017 | |
|
1018 | | #ifdef RBBI_DEBUG |
1019 | | if (gTrace) { |
1020 | | RBBIDebugPrintf(" %4d ", (int32_t)utext_getNativeIndex(&fText)); |
1021 | | if (0x20<=c && c<0x7f) { |
1022 | | RBBIDebugPrintf("\"%c\" ", c); |
1023 | | } else { |
1024 | | RBBIDebugPrintf("%5x ", c); |
1025 | | } |
1026 | | RBBIDebugPrintf("%3d %3d\n", state, category); |
1027 | | } |
1028 | | #endif |
1029 | | |
1030 | | // State Transition - move machine to its next state |
1031 | | // |
1032 | | // fNextState is a variable-length array. |
1033 | 0 | U_ASSERT(category<fData->fHeader->fCatCount); |
1034 | 0 | state = row->fNextState[category]; /*Not accessing beyond memory*/ |
1035 | 0 | row = (RowType *) |
1036 | 0 | (stateTable->fTableData + (stateTable->fRowLen * state)); |
1037 | |
|
1038 | 0 | if (state == STOP_STATE) { |
1039 | | // This is the normal exit from the lookup state machine. |
1040 | | // Transition to state zero means we have found a safe point. |
1041 | 0 | break; |
1042 | 0 | } |
1043 | 0 | } |
1044 | | |
1045 | | // The state machine is done. Check whether it found a match... |
1046 | 0 | result = (int32_t)UTEXT_GETNATIVEINDEX(&fText); |
1047 | | #ifdef RBBI_DEBUG |
1048 | | if (gTrace) { |
1049 | | RBBIDebugPrintf("result = %d\n\n", result); |
1050 | | } |
1051 | | #endif |
1052 | 0 | return result; |
1053 | 0 | } Unexecuted instantiation: rbbi.cpp:int icu_70::RuleBasedBreakIterator::handleSafePrevious<icu_70::RBBIStateTableRowT<unsigned char>, &icu_70::TrieFunc8>(int) Unexecuted instantiation: rbbi.cpp:int icu_70::RuleBasedBreakIterator::handleSafePrevious<icu_70::RBBIStateTableRowT<unsigned char>, &icu_70::TrieFunc16>(int) Unexecuted instantiation: rbbi.cpp:int icu_70::RuleBasedBreakIterator::handleSafePrevious<icu_70::RBBIStateTableRowT<unsigned short>, &icu_70::TrieFunc8>(int) Unexecuted instantiation: rbbi.cpp:int icu_70::RuleBasedBreakIterator::handleSafePrevious<icu_70::RBBIStateTableRowT<unsigned short>, &icu_70::TrieFunc16>(int) |
1054 | | |
1055 | | |
1056 | | //------------------------------------------------------------------------------- |
1057 | | // |
1058 | | // getRuleStatus() Return the break rule tag associated with the current |
1059 | | // iterator position. If the iterator arrived at its current |
1060 | | // position by iterating forwards, the value will have been |
1061 | | // cached by the handleNext() function. |
1062 | | // |
1063 | | //------------------------------------------------------------------------------- |
1064 | | |
1065 | 0 | int32_t RuleBasedBreakIterator::getRuleStatus() const { |
1066 | | |
1067 | | // fLastRuleStatusIndex indexes to the start of the appropriate status record |
1068 | | // (the number of status values.) |
1069 | | // This function returns the last (largest) of the array of status values. |
1070 | 0 | int32_t idx = fRuleStatusIndex + fData->fRuleStatusTable[fRuleStatusIndex]; |
1071 | 0 | int32_t tagVal = fData->fRuleStatusTable[idx]; |
1072 | |
|
1073 | 0 | return tagVal; |
1074 | 0 | } |
1075 | | |
1076 | | |
1077 | | int32_t RuleBasedBreakIterator::getRuleStatusVec( |
1078 | 0 | int32_t *fillInVec, int32_t capacity, UErrorCode &status) { |
1079 | 0 | if (U_FAILURE(status)) { |
1080 | 0 | return 0; |
1081 | 0 | } |
1082 | | |
1083 | 0 | int32_t numVals = fData->fRuleStatusTable[fRuleStatusIndex]; |
1084 | 0 | int32_t numValsToCopy = numVals; |
1085 | 0 | if (numVals > capacity) { |
1086 | 0 | status = U_BUFFER_OVERFLOW_ERROR; |
1087 | 0 | numValsToCopy = capacity; |
1088 | 0 | } |
1089 | 0 | int i; |
1090 | 0 | for (i=0; i<numValsToCopy; i++) { |
1091 | 0 | fillInVec[i] = fData->fRuleStatusTable[fRuleStatusIndex + i + 1]; |
1092 | 0 | } |
1093 | 0 | return numVals; |
1094 | 0 | } |
1095 | | |
1096 | | |
1097 | | |
1098 | | //------------------------------------------------------------------------------- |
1099 | | // |
1100 | | // getBinaryRules Access to the compiled form of the rules, |
1101 | | // for use by build system tools that save the data |
1102 | | // for standard iterator types. |
1103 | | // |
1104 | | //------------------------------------------------------------------------------- |
1105 | 0 | const uint8_t *RuleBasedBreakIterator::getBinaryRules(uint32_t &length) { |
1106 | 0 | const uint8_t *retPtr = NULL; |
1107 | 0 | length = 0; |
1108 | |
|
1109 | 0 | if (fData != NULL) { |
1110 | 0 | retPtr = (const uint8_t *)fData->fHeader; |
1111 | 0 | length = fData->fHeader->fLength; |
1112 | 0 | } |
1113 | 0 | return retPtr; |
1114 | 0 | } |
1115 | | |
1116 | | |
1117 | | RuleBasedBreakIterator *RuleBasedBreakIterator::createBufferClone( |
1118 | 0 | void * /*stackBuffer*/, int32_t &bufferSize, UErrorCode &status) { |
1119 | 0 | if (U_FAILURE(status)){ |
1120 | 0 | return NULL; |
1121 | 0 | } |
1122 | | |
1123 | 0 | if (bufferSize == 0) { |
1124 | 0 | bufferSize = 1; // preflighting for deprecated functionality |
1125 | 0 | return NULL; |
1126 | 0 | } |
1127 | | |
1128 | 0 | BreakIterator *clonedBI = clone(); |
1129 | 0 | if (clonedBI == NULL) { |
1130 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
1131 | 0 | } else { |
1132 | 0 | status = U_SAFECLONE_ALLOCATED_WARNING; |
1133 | 0 | } |
1134 | 0 | return (RuleBasedBreakIterator *)clonedBI; |
1135 | 0 | } |
1136 | | |
1137 | | U_NAMESPACE_END |
1138 | | |
1139 | | |
1140 | | static icu::UStack *gLanguageBreakFactories = nullptr; |
1141 | | static const icu::UnicodeString *gEmptyString = nullptr; |
1142 | | static icu::UInitOnce gLanguageBreakFactoriesInitOnce = U_INITONCE_INITIALIZER; |
1143 | | static icu::UInitOnce gRBBIInitOnce = U_INITONCE_INITIALIZER; |
1144 | | |
1145 | | /** |
1146 | | * Release all static memory held by breakiterator. |
1147 | | */ |
1148 | | U_CDECL_BEGIN |
1149 | 0 | UBool U_CALLCONV rbbi_cleanup(void) { |
1150 | 0 | delete gLanguageBreakFactories; |
1151 | 0 | gLanguageBreakFactories = nullptr; |
1152 | 0 | delete gEmptyString; |
1153 | 0 | gEmptyString = nullptr; |
1154 | 0 | gLanguageBreakFactoriesInitOnce.reset(); |
1155 | 0 | gRBBIInitOnce.reset(); |
1156 | 0 | return TRUE; |
1157 | 0 | } |
1158 | | U_CDECL_END |
1159 | | |
1160 | | U_CDECL_BEGIN |
1161 | 0 | static void U_CALLCONV _deleteFactory(void *obj) { |
1162 | 0 | delete (icu::LanguageBreakFactory *) obj; |
1163 | 0 | } |
1164 | | U_CDECL_END |
1165 | | U_NAMESPACE_BEGIN |
1166 | | |
1167 | 0 | static void U_CALLCONV rbbiInit() { |
1168 | 0 | gEmptyString = new UnicodeString(); |
1169 | 0 | ucln_common_registerCleanup(UCLN_COMMON_RBBI, rbbi_cleanup); |
1170 | 0 | } |
1171 | | |
1172 | 0 | static void U_CALLCONV initLanguageFactories() { |
1173 | 0 | UErrorCode status = U_ZERO_ERROR; |
1174 | 0 | U_ASSERT(gLanguageBreakFactories == NULL); |
1175 | 0 | gLanguageBreakFactories = new UStack(_deleteFactory, NULL, status); |
1176 | 0 | if (gLanguageBreakFactories != NULL && U_SUCCESS(status)) { |
1177 | 0 | ICULanguageBreakFactory *builtIn = new ICULanguageBreakFactory(status); |
1178 | 0 | gLanguageBreakFactories->push(builtIn, status); |
1179 | | #ifdef U_LOCAL_SERVICE_HOOK |
1180 | | LanguageBreakFactory *extra = (LanguageBreakFactory *)uprv_svc_hook("languageBreakFactory", &status); |
1181 | | if (extra != NULL) { |
1182 | | gLanguageBreakFactories->push(extra, status); |
1183 | | } |
1184 | | #endif |
1185 | 0 | } |
1186 | 0 | ucln_common_registerCleanup(UCLN_COMMON_RBBI, rbbi_cleanup); |
1187 | 0 | } |
1188 | | |
1189 | | |
1190 | | static const LanguageBreakEngine* |
1191 | | getLanguageBreakEngineFromFactory(UChar32 c) |
1192 | 0 | { |
1193 | 0 | umtx_initOnce(gLanguageBreakFactoriesInitOnce, &initLanguageFactories); |
1194 | 0 | if (gLanguageBreakFactories == NULL) { |
1195 | 0 | return NULL; |
1196 | 0 | } |
1197 | | |
1198 | 0 | int32_t i = gLanguageBreakFactories->size(); |
1199 | 0 | const LanguageBreakEngine *lbe = NULL; |
1200 | 0 | while (--i >= 0) { |
1201 | 0 | LanguageBreakFactory *factory = (LanguageBreakFactory *)(gLanguageBreakFactories->elementAt(i)); |
1202 | 0 | lbe = factory->getEngineFor(c); |
1203 | 0 | if (lbe != NULL) { |
1204 | 0 | break; |
1205 | 0 | } |
1206 | 0 | } |
1207 | 0 | return lbe; |
1208 | 0 | } |
1209 | | |
1210 | | |
1211 | | //------------------------------------------------------------------------------- |
1212 | | // |
1213 | | // getLanguageBreakEngine Find an appropriate LanguageBreakEngine for the |
1214 | | // the character c. |
1215 | | // |
1216 | | //------------------------------------------------------------------------------- |
1217 | | const LanguageBreakEngine * |
1218 | 0 | RuleBasedBreakIterator::getLanguageBreakEngine(UChar32 c) { |
1219 | 0 | const LanguageBreakEngine *lbe = NULL; |
1220 | 0 | UErrorCode status = U_ZERO_ERROR; |
1221 | |
|
1222 | 0 | if (fLanguageBreakEngines == NULL) { |
1223 | 0 | fLanguageBreakEngines = new UStack(status); |
1224 | 0 | if (fLanguageBreakEngines == NULL || U_FAILURE(status)) { |
1225 | 0 | delete fLanguageBreakEngines; |
1226 | 0 | fLanguageBreakEngines = 0; |
1227 | 0 | return NULL; |
1228 | 0 | } |
1229 | 0 | } |
1230 | | |
1231 | 0 | int32_t i = fLanguageBreakEngines->size(); |
1232 | 0 | while (--i >= 0) { |
1233 | 0 | lbe = (const LanguageBreakEngine *)(fLanguageBreakEngines->elementAt(i)); |
1234 | 0 | if (lbe->handles(c)) { |
1235 | 0 | return lbe; |
1236 | 0 | } |
1237 | 0 | } |
1238 | | |
1239 | | // No existing dictionary took the character. See if a factory wants to |
1240 | | // give us a new LanguageBreakEngine for this character. |
1241 | 0 | lbe = getLanguageBreakEngineFromFactory(c); |
1242 | | |
1243 | | // If we got one, use it and push it on our stack. |
1244 | 0 | if (lbe != NULL) { |
1245 | 0 | fLanguageBreakEngines->push((void *)lbe, status); |
1246 | | // Even if we can't remember it, we can keep looking it up, so |
1247 | | // return it even if the push fails. |
1248 | 0 | return lbe; |
1249 | 0 | } |
1250 | | |
1251 | | // No engine is forthcoming for this character. Add it to the |
1252 | | // reject set. Create the reject break engine if needed. |
1253 | 0 | if (fUnhandledBreakEngine == NULL) { |
1254 | 0 | fUnhandledBreakEngine = new UnhandledEngine(status); |
1255 | 0 | if (U_SUCCESS(status) && fUnhandledBreakEngine == NULL) { |
1256 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
1257 | 0 | return nullptr; |
1258 | 0 | } |
1259 | | // Put it last so that scripts for which we have an engine get tried |
1260 | | // first. |
1261 | 0 | fLanguageBreakEngines->insertElementAt(fUnhandledBreakEngine, 0, status); |
1262 | | // If we can't insert it, or creation failed, get rid of it |
1263 | 0 | if (U_FAILURE(status)) { |
1264 | 0 | delete fUnhandledBreakEngine; |
1265 | 0 | fUnhandledBreakEngine = 0; |
1266 | 0 | return NULL; |
1267 | 0 | } |
1268 | 0 | } |
1269 | | |
1270 | | // Tell the reject engine about the character; at its discretion, it may |
1271 | | // add more than just the one character. |
1272 | 0 | fUnhandledBreakEngine->handleCharacter(c); |
1273 | |
|
1274 | 0 | return fUnhandledBreakEngine; |
1275 | 0 | } |
1276 | | |
1277 | 0 | void RuleBasedBreakIterator::dumpCache() { |
1278 | 0 | fBreakCache->dumpCache(); |
1279 | 0 | } |
1280 | | |
1281 | 0 | void RuleBasedBreakIterator::dumpTables() { |
1282 | 0 | fData->printData(); |
1283 | 0 | } |
1284 | | |
1285 | | /** |
1286 | | * Returns the description used to create this iterator |
1287 | | */ |
1288 | | |
1289 | | const UnicodeString& |
1290 | 0 | RuleBasedBreakIterator::getRules() const { |
1291 | 0 | if (fData != NULL) { |
1292 | 0 | return fData->getRuleSourceString(); |
1293 | 0 | } else { |
1294 | 0 | umtx_initOnce(gRBBIInitOnce, &rbbiInit); |
1295 | 0 | return *gEmptyString; |
1296 | 0 | } |
1297 | 0 | } |
1298 | | |
1299 | | U_NAMESPACE_END |
1300 | | |
1301 | | #endif /* #if !UCONFIG_NO_BREAK_ITERATION */ |