blob: 2769263894b7bddf4a071d8dbae1f7e5d8e3a3a5 [file] [log] [blame]
Jungshik Shin87232d82017-05-13 21:10:13 -07001// © 2016 and later: Unicode, Inc. and others.
Jungshik Shin5feb9ad2016-10-21 12:52:48 -07002// License & terms of use: http://www.unicode.org/copyright.html
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00003/*
4***************************************************************************
Jungshik Shin5feb9ad2016-10-21 12:52:48 -07005* Copyright (C) 1999-2016 International Business Machines Corporation
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00006* and others. All rights reserved.
7***************************************************************************
8*/
9//
Jungshik Shinb3189662017-11-07 11:18:34 -080010// file: rbbi.cpp Contains the implementation of the rule based break iterator
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000011// 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
Jungshik Shin42d50272018-10-24 01:22:09 -070021#include <cinttypes>
22
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000023#include "unicode/rbbi.h"
24#include "unicode/schriter.h"
25#include "unicode/uchriter.h"
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000026#include "unicode/uclean.h"
Jungshik Shinb3189662017-11-07 11:18:34 -080027#include "unicode/udata.h"
28
29#include "brkeng.h"
30#include "ucln_cmn.h"
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000031#include "cmemory.h"
32#include "cstring.h"
Frank Tang35f7e132019-04-12 15:58:06 -070033#include "localsvc.h"
Jungshik Shinb3189662017-11-07 11:18:34 -080034#include "rbbidata.h"
35#include "rbbi_cache.h"
36#include "rbbirb.h"
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000037#include "uassert.h"
Jungshik Shinb3189662017-11-07 11:18:34 -080038#include "umutex.h"
39#include "uvectr32.h"
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000040
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000041#ifdef RBBI_DEBUG
Frank Tang1f164ee2022-11-08 12:31:27 -080042static UBool gTrace = false;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000043#endif
44
45U_NAMESPACE_BEGIN
46
47// The state number of the starting state
Jungshik Shinb3189662017-11-07 11:18:34 -080048constexpr int32_t START_STATE = 1;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000049
50// The state-transition value indicating "stop"
Jungshik Shinb3189662017-11-07 11:18:34 -080051constexpr int32_t STOP_STATE = 0;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000052
53
54UOBJECT_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 */
Jungshik Shinf61e46d2018-05-04 13:00:45 -070065RuleBasedBreakIterator::RuleBasedBreakIterator(RBBIDataHeader* data, UErrorCode &status)
66 : fSCharIter(UnicodeString())
67{
Jungshik Shinb3189662017-11-07 11:18:34 -080068 init(status);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000069 fData = new RBBIDataWrapper(data, status); // status checked in constructor
70 if (U_FAILURE(status)) {return;}
Frank Tangf90543d2020-10-30 19:02:04 -070071 if(fData == nullptr) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000072 status = U_MEMORY_ALLOCATION_ERROR;
73 return;
74 }
Frank Tangf90543d2020-10-30 19:02:04 -070075 if (fData->fForwardTable->fLookAheadResultsSize > 0) {
76 fLookAheadMatches = static_cast<int32_t *>(
77 uprv_malloc(fData->fForwardTable->fLookAheadResultsSize * sizeof(int32_t)));
78 if (fLookAheadMatches == nullptr) {
79 status = U_MEMORY_ALLOCATION_ERROR;
80 return;
81 }
82 }
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000083}
84
Frank Tangd2858cb2022-04-08 20:34:12 -070085//-------------------------------------------------------------------------------
86//
87// Constructor from a UDataMemory handle to precompiled break rules
88// stored in an ICU data file. This construcotr is private API,
89// only for internal use.
90//
91//-------------------------------------------------------------------------------
92RuleBasedBreakIterator::RuleBasedBreakIterator(UDataMemory* udm, UBool isPhraseBreaking,
93 UErrorCode &status) : RuleBasedBreakIterator(udm, status)
94{
95 fIsPhraseBreaking = isPhraseBreaking;
96}
97
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000098//
99// Construct from precompiled binary rules (tables). This constructor is public API,
100// taking the rules as a (const uint8_t *) to match the type produced by getBinaryRules().
101//
102RuleBasedBreakIterator::RuleBasedBreakIterator(const uint8_t *compiledRules,
103 uint32_t ruleLength,
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700104 UErrorCode &status)
105 : fSCharIter(UnicodeString())
106{
Jungshik Shinb3189662017-11-07 11:18:34 -0800107 init(status);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000108 if (U_FAILURE(status)) {
109 return;
110 }
111 if (compiledRules == NULL || ruleLength < sizeof(RBBIDataHeader)) {
112 status = U_ILLEGAL_ARGUMENT_ERROR;
113 return;
114 }
115 const RBBIDataHeader *data = (const RBBIDataHeader *)compiledRules;
116 if (data->fLength > ruleLength) {
117 status = U_ILLEGAL_ARGUMENT_ERROR;
118 return;
119 }
Jungshik Shinb3189662017-11-07 11:18:34 -0800120 fData = new RBBIDataWrapper(data, RBBIDataWrapper::kDontAdopt, status);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000121 if (U_FAILURE(status)) {return;}
Frank Tangf90543d2020-10-30 19:02:04 -0700122 if(fData == nullptr) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000123 status = U_MEMORY_ALLOCATION_ERROR;
124 return;
125 }
Frank Tangf90543d2020-10-30 19:02:04 -0700126 if (fData->fForwardTable->fLookAheadResultsSize > 0) {
127 fLookAheadMatches = static_cast<int32_t *>(
128 uprv_malloc(fData->fForwardTable->fLookAheadResultsSize * sizeof(int32_t)));
129 if (fLookAheadMatches == nullptr) {
130 status = U_MEMORY_ALLOCATION_ERROR;
131 return;
132 }
133 }
Jungshik Shinb3189662017-11-07 11:18:34 -0800134}
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000135
136
137//-------------------------------------------------------------------------------
138//
139// Constructor from a UDataMemory handle to precompiled break rules
140// stored in an ICU data file.
141//
142//-------------------------------------------------------------------------------
143RuleBasedBreakIterator::RuleBasedBreakIterator(UDataMemory* udm, UErrorCode &status)
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700144 : fSCharIter(UnicodeString())
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000145{
Jungshik Shinb3189662017-11-07 11:18:34 -0800146 init(status);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000147 fData = new RBBIDataWrapper(udm, status); // status checked in constructor
148 if (U_FAILURE(status)) {return;}
Frank Tangf90543d2020-10-30 19:02:04 -0700149 if(fData == nullptr) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000150 status = U_MEMORY_ALLOCATION_ERROR;
151 return;
152 }
Frank Tangf90543d2020-10-30 19:02:04 -0700153 if (fData->fForwardTable->fLookAheadResultsSize > 0) {
154 fLookAheadMatches = static_cast<int32_t *>(
155 uprv_malloc(fData->fForwardTable->fLookAheadResultsSize * sizeof(int32_t)));
156 if (fLookAheadMatches == nullptr) {
157 status = U_MEMORY_ALLOCATION_ERROR;
158 return;
159 }
160 }
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000161}
162
163
164
165//-------------------------------------------------------------------------------
166//
167// Constructor from a set of rules supplied as a string.
168//
169//-------------------------------------------------------------------------------
170RuleBasedBreakIterator::RuleBasedBreakIterator( const UnicodeString &rules,
171 UParseError &parseError,
172 UErrorCode &status)
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700173 : fSCharIter(UnicodeString())
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000174{
Jungshik Shinb3189662017-11-07 11:18:34 -0800175 init(status);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000176 if (U_FAILURE(status)) {return;}
177 RuleBasedBreakIterator *bi = (RuleBasedBreakIterator *)
178 RBBIRuleBuilder::createRuleBasedBreakIterator(rules, &parseError, status);
179 // Note: This is a bit awkward. The RBBI ruleBuilder has a factory method that
180 // creates and returns a complete RBBI. From here, in a constructor, we
181 // can't just return the object created by the builder factory, hence
182 // the assignment of the factory created object to "this".
183 if (U_SUCCESS(status)) {
184 *this = *bi;
185 delete bi;
186 }
187}
188
189
190//-------------------------------------------------------------------------------
191//
192// Default Constructor. Create an empty shell that can be set up later.
193// Used when creating a RuleBasedBreakIterator from a set
194// of rules.
195//-------------------------------------------------------------------------------
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700196RuleBasedBreakIterator::RuleBasedBreakIterator()
197 : fSCharIter(UnicodeString())
198{
Jungshik Shinb3189662017-11-07 11:18:34 -0800199 UErrorCode status = U_ZERO_ERROR;
200 init(status);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000201}
202
203
204//-------------------------------------------------------------------------------
205//
206// Copy constructor. Will produce a break iterator with the same behavior,
207// and which iterates over the same text, as the one passed in.
208//
209//-------------------------------------------------------------------------------
210RuleBasedBreakIterator::RuleBasedBreakIterator(const RuleBasedBreakIterator& other)
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700211: BreakIterator(other),
212 fSCharIter(UnicodeString())
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000213{
Jungshik Shinb3189662017-11-07 11:18:34 -0800214 UErrorCode status = U_ZERO_ERROR;
215 this->init(status);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000216 *this = other;
217}
218
219
220/**
221 * Destructor
222 */
223RuleBasedBreakIterator::~RuleBasedBreakIterator() {
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700224 if (fCharIter != &fSCharIter) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000225 // fCharIter was adopted from the outside.
226 delete fCharIter;
227 }
Frank Tangf90543d2020-10-30 19:02:04 -0700228 fCharIter = nullptr;
Jungshik Shinb3189662017-11-07 11:18:34 -0800229
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700230 utext_close(&fText);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000231
Frank Tangf90543d2020-10-30 19:02:04 -0700232 if (fData != nullptr) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000233 fData->removeReference();
Frank Tangf90543d2020-10-30 19:02:04 -0700234 fData = nullptr;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000235 }
Jungshik Shinb3189662017-11-07 11:18:34 -0800236 delete fBreakCache;
Frank Tangf90543d2020-10-30 19:02:04 -0700237 fBreakCache = nullptr;
Jungshik Shinb3189662017-11-07 11:18:34 -0800238
239 delete fDictionaryCache;
Frank Tangf90543d2020-10-30 19:02:04 -0700240 fDictionaryCache = nullptr;
Jungshik Shinb3189662017-11-07 11:18:34 -0800241
242 delete fLanguageBreakEngines;
Frank Tangf90543d2020-10-30 19:02:04 -0700243 fLanguageBreakEngines = nullptr;
Jungshik Shinb3189662017-11-07 11:18:34 -0800244
245 delete fUnhandledBreakEngine;
Frank Tangf90543d2020-10-30 19:02:04 -0700246 fUnhandledBreakEngine = nullptr;
247
248 uprv_free(fLookAheadMatches);
249 fLookAheadMatches = nullptr;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000250}
251
252/**
253 * Assignment operator. Sets this iterator to have the same behavior,
254 * and iterate over the same text, as the one passed in.
Frank Tangf90543d2020-10-30 19:02:04 -0700255 * TODO: needs better handling of memory allocation errors.
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000256 */
257RuleBasedBreakIterator&
258RuleBasedBreakIterator::operator=(const RuleBasedBreakIterator& that) {
259 if (this == &that) {
260 return *this;
261 }
Jungshik Shinb3189662017-11-07 11:18:34 -0800262 BreakIterator::operator=(that);
263
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000264 if (fLanguageBreakEngines != NULL) {
265 delete fLanguageBreakEngines;
266 fLanguageBreakEngines = NULL; // Just rebuild for now
267 }
268 // TODO: clone fLanguageBreakEngines from "that"
269 UErrorCode status = U_ZERO_ERROR;
Frank Tang1f164ee2022-11-08 12:31:27 -0800270 utext_clone(&fText, &that.fText, false, true, &status);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000271
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700272 if (fCharIter != &fSCharIter) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000273 delete fCharIter;
274 }
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700275 fCharIter = &fSCharIter;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000276
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700277 if (that.fCharIter != NULL && that.fCharIter != &that.fSCharIter) {
Frank Tang3e05d9d2021-11-08 14:04:04 -0800278 // This is a little bit tricky - it will initially appear that
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000279 // this->fCharIter is adopted, even if that->fCharIter was
280 // not adopted. That's ok.
281 fCharIter = that.fCharIter->clone();
282 }
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700283 fSCharIter = that.fSCharIter;
284 if (fCharIter == NULL) {
285 fCharIter = &fSCharIter;
286 }
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000287
288 if (fData != NULL) {
289 fData->removeReference();
290 fData = NULL;
291 }
292 if (that.fData != NULL) {
293 fData = that.fData->addReference();
294 }
295
Frank Tangf90543d2020-10-30 19:02:04 -0700296 uprv_free(fLookAheadMatches);
297 fLookAheadMatches = nullptr;
298 if (fData && fData->fForwardTable->fLookAheadResultsSize > 0) {
299 fLookAheadMatches = static_cast<int32_t *>(
300 uprv_malloc(fData->fForwardTable->fLookAheadResultsSize * sizeof(int32_t)));
301 }
302
303
Jungshik Shinb3189662017-11-07 11:18:34 -0800304 fPosition = that.fPosition;
305 fRuleStatusIndex = that.fRuleStatusIndex;
306 fDone = that.fDone;
307
308 // TODO: both the dictionary and the main cache need to be copied.
309 // Current position could be within a dictionary range. Trying to continue
310 // the iteration without the caches present would go to the rules, with
311 // the assumption that the current position is on a rule boundary.
312 fBreakCache->reset(fPosition, fRuleStatusIndex);
313 fDictionaryCache->reset();
314
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000315 return *this;
316}
317
318
319
320//-----------------------------------------------------------------------------
321//
322// init() Shared initialization routine. Used by all the constructors.
323// Initializes all fields, leaving the object in a consistent state.
324//
325//-----------------------------------------------------------------------------
Jungshik Shinb3189662017-11-07 11:18:34 -0800326void RuleBasedBreakIterator::init(UErrorCode &status) {
Frank Tangf90543d2020-10-30 19:02:04 -0700327 fCharIter = nullptr;
328 fData = nullptr;
Jungshik Shinb3189662017-11-07 11:18:34 -0800329 fPosition = 0;
330 fRuleStatusIndex = 0;
331 fDone = false;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000332 fDictionaryCharCount = 0;
Frank Tangf90543d2020-10-30 19:02:04 -0700333 fLanguageBreakEngines = nullptr;
334 fUnhandledBreakEngine = nullptr;
335 fBreakCache = nullptr;
336 fDictionaryCache = nullptr;
337 fLookAheadMatches = nullptr;
Frank Tangd2858cb2022-04-08 20:34:12 -0700338 fIsPhraseBreaking = false;
Jungshik Shinb3189662017-11-07 11:18:34 -0800339
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700340 // Note: IBM xlC is unable to assign or initialize member fText from UTEXT_INITIALIZER.
341 // fText = UTEXT_INITIALIZER;
342 static const UText initializedUText = UTEXT_INITIALIZER;
343 uprv_memcpy(&fText, &initializedUText, sizeof(UText));
344
345 if (U_FAILURE(status)) {
Jungshik Shinb3189662017-11-07 11:18:34 -0800346 return;
347 }
348
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700349 utext_openUChars(&fText, NULL, 0, &status);
Jungshik Shinb3189662017-11-07 11:18:34 -0800350 fDictionaryCache = new DictionaryCache(this, status);
351 fBreakCache = new BreakCache(this, status);
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700352 if (U_SUCCESS(status) && (fDictionaryCache == NULL || fBreakCache == NULL)) {
Jungshik Shinb3189662017-11-07 11:18:34 -0800353 status = U_MEMORY_ALLOCATION_ERROR;
354 }
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000355
356#ifdef RBBI_DEBUG
Frank Tang1f164ee2022-11-08 12:31:27 -0800357 static UBool debugInitDone = false;
358 if (debugInitDone == false) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000359 char *debugEnv = getenv("U_RBBIDEBUG");
360 if (debugEnv && uprv_strstr(debugEnv, "trace")) {
Frank Tang1f164ee2022-11-08 12:31:27 -0800361 gTrace = true;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000362 }
Frank Tang1f164ee2022-11-08 12:31:27 -0800363 debugInitDone = true;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000364 }
365#endif
366}
367
368
369
370//-----------------------------------------------------------------------------
371//
372// clone - Returns a newly-constructed RuleBasedBreakIterator with the same
373// behavior, and iterating over the same text, as this one.
374// Virtual function: does the right thing with subclasses.
375//
376//-----------------------------------------------------------------------------
Frank Tangb8696612019-10-25 14:58:21 -0700377RuleBasedBreakIterator*
378RuleBasedBreakIterator::clone() const {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000379 return new RuleBasedBreakIterator(*this);
380}
381
382/**
Frank Tang3e05d9d2021-11-08 14:04:04 -0800383 * Equality operator. Returns true if both BreakIterators are of the
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000384 * same class, have the same behavior, and iterate over the same text.
385 */
Frank Tang3e05d9d2021-11-08 14:04:04 -0800386bool
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000387RuleBasedBreakIterator::operator==(const BreakIterator& that) const {
388 if (typeid(*this) != typeid(that)) {
Frank Tang3e05d9d2021-11-08 14:04:04 -0800389 return false;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000390 }
Jungshik Shinb3189662017-11-07 11:18:34 -0800391 if (this == &that) {
Frank Tang3e05d9d2021-11-08 14:04:04 -0800392 return true;
Jungshik Shinb3189662017-11-07 11:18:34 -0800393 }
394
395 // The base class BreakIterator carries no state that participates in equality,
396 // and does not implement an equality function that would otherwise be
397 // checked at this point.
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000398
399 const RuleBasedBreakIterator& that2 = (const RuleBasedBreakIterator&) that;
400
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700401 if (!utext_equals(&fText, &that2.fText)) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000402 // The two break iterators are operating on different text,
Jungshik Shinb3189662017-11-07 11:18:34 -0800403 // or have a different iteration position.
404 // Note that fText's position is always the same as the break iterator's position.
Frank Tang3e05d9d2021-11-08 14:04:04 -0800405 return false;
Frank Tangb8696612019-10-25 14:58:21 -0700406 }
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000407
Jungshik Shinb3189662017-11-07 11:18:34 -0800408 if (!(fPosition == that2.fPosition &&
409 fRuleStatusIndex == that2.fRuleStatusIndex &&
410 fDone == that2.fDone)) {
Frank Tang3e05d9d2021-11-08 14:04:04 -0800411 return false;
Jungshik Shinb3189662017-11-07 11:18:34 -0800412 }
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000413
414 if (that2.fData == fData ||
415 (fData != NULL && that2.fData != NULL && *that2.fData == *fData)) {
416 // The two break iterators are using the same rules.
Frank Tang3e05d9d2021-11-08 14:04:04 -0800417 return true;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000418 }
Frank Tang3e05d9d2021-11-08 14:04:04 -0800419 return false;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000420}
421
422/**
423 * Compute a hash code for this BreakIterator
424 * @return A hash code
425 */
426int32_t
427RuleBasedBreakIterator::hashCode(void) const {
428 int32_t hash = 0;
429 if (fData != NULL) {
430 hash = fData->hashCode();
431 }
432 return hash;
433}
434
435
436void RuleBasedBreakIterator::setText(UText *ut, UErrorCode &status) {
437 if (U_FAILURE(status)) {
438 return;
439 }
Jungshik Shinb3189662017-11-07 11:18:34 -0800440 fBreakCache->reset();
441 fDictionaryCache->reset();
Frank Tang1f164ee2022-11-08 12:31:27 -0800442 utext_clone(&fText, ut, false, true, &status);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000443
444 // Set up a dummy CharacterIterator to be returned if anyone
445 // calls getText(). With input from UText, there is no reasonable
446 // way to return a characterIterator over the actual input text.
447 // Return one over an empty string instead - this is the closest
448 // we can come to signaling a failure.
449 // (GetText() is obsolete, this failure is sort of OK)
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700450 fSCharIter.setText(UnicodeString());
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000451
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700452 if (fCharIter != &fSCharIter) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000453 // existing fCharIter was adopted from the outside. Delete it now.
454 delete fCharIter;
455 }
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700456 fCharIter = &fSCharIter;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000457
458 this->first();
459}
460
461
462UText *RuleBasedBreakIterator::getUText(UText *fillIn, UErrorCode &status) const {
Frank Tang1f164ee2022-11-08 12:31:27 -0800463 UText *result = utext_clone(fillIn, &fText, false, true, &status);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000464 return result;
465}
466
467
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000468//=======================================================================
469// BreakIterator overrides
470//=======================================================================
471
472/**
Jungshik Shinb3189662017-11-07 11:18:34 -0800473 * Return a CharacterIterator over the text being analyzed.
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000474 */
475CharacterIterator&
476RuleBasedBreakIterator::getText() const {
477 return *fCharIter;
478}
479
480/**
481 * Set the iterator to analyze a new piece of text. This function resets
482 * the current iteration position to the beginning of the text.
483 * @param newText An iterator over the text to analyze.
484 */
485void
486RuleBasedBreakIterator::adoptText(CharacterIterator* newText) {
Jungshik Shinb3189662017-11-07 11:18:34 -0800487 // If we are holding a CharacterIterator adopted from a
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000488 // previous call to this function, delete it now.
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700489 if (fCharIter != &fSCharIter) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000490 delete fCharIter;
491 }
492
493 fCharIter = newText;
494 UErrorCode status = U_ZERO_ERROR;
Jungshik Shinb3189662017-11-07 11:18:34 -0800495 fBreakCache->reset();
496 fDictionaryCache->reset();
497 if (newText==NULL || newText->startIndex() != 0) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000498 // startIndex !=0 wants to be an error, but there's no way to report it.
499 // Make the iterator text be an empty string.
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700500 utext_openUChars(&fText, NULL, 0, &status);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000501 } else {
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700502 utext_openCharacterIterator(&fText, newText, &status);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000503 }
504 this->first();
505}
506
507/**
508 * Set the iterator to analyze a new piece of text. This function resets
509 * the current iteration position to the beginning of the text.
510 * @param newText An iterator over the text to analyze.
511 */
512void
513RuleBasedBreakIterator::setText(const UnicodeString& newText) {
514 UErrorCode status = U_ZERO_ERROR;
Jungshik Shinb3189662017-11-07 11:18:34 -0800515 fBreakCache->reset();
516 fDictionaryCache->reset();
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700517 utext_openConstUnicodeString(&fText, &newText, &status);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000518
Jungshik Shinb3189662017-11-07 11:18:34 -0800519 // Set up a character iterator on the string.
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000520 // Needed in case someone calls getText().
521 // Can not, unfortunately, do this lazily on the (probably never)
522 // call to getText(), because getText is const.
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700523 fSCharIter.setText(newText);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000524
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700525 if (fCharIter != &fSCharIter) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000526 // old fCharIter was adopted from the outside. Delete it.
527 delete fCharIter;
528 }
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700529 fCharIter = &fSCharIter;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000530
531 this->first();
532}
533
534
535/**
536 * Provide a new UText for the input text. Must reference text with contents identical
537 * to the original.
538 * Intended for use with text data originating in Java (garbage collected) environments
539 * where the data may be moved in memory at arbitrary times.
540 */
541RuleBasedBreakIterator &RuleBasedBreakIterator::refreshInputText(UText *input, UErrorCode &status) {
542 if (U_FAILURE(status)) {
543 return *this;
544 }
545 if (input == NULL) {
546 status = U_ILLEGAL_ARGUMENT_ERROR;
547 return *this;
548 }
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700549 int64_t pos = utext_getNativeIndex(&fText);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000550 // Shallow read-only clone of the new UText into the existing input UText
Frank Tang1f164ee2022-11-08 12:31:27 -0800551 utext_clone(&fText, input, false, true, &status);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000552 if (U_FAILURE(status)) {
553 return *this;
554 }
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700555 utext_setNativeIndex(&fText, pos);
556 if (utext_getNativeIndex(&fText) != pos) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000557 // Sanity check. The new input utext is supposed to have the exact same
558 // contents as the old. If we can't set to the same position, it doesn't.
559 // The contents underlying the old utext might be invalid at this point,
560 // so it's not safe to check directly.
561 status = U_ILLEGAL_ARGUMENT_ERROR;
562 }
563 return *this;
564}
565
566
567/**
Jungshik Shin (jungshik at google)0f8746a2015-01-08 15:46:45 -0800568 * Sets the current iteration position to the beginning of the text, position zero.
569 * @return The new iterator position, which is zero.
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000570 */
571int32_t RuleBasedBreakIterator::first(void) {
Jungshik Shinb3189662017-11-07 11:18:34 -0800572 UErrorCode status = U_ZERO_ERROR;
573 if (!fBreakCache->seek(0)) {
574 fBreakCache->populateNear(0, status);
575 }
576 fBreakCache->current();
577 U_ASSERT(fPosition == 0);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000578 return 0;
579}
580
581/**
582 * Sets the current iteration position to the end of the text.
583 * @return The text's past-the-end offset.
584 */
585int32_t RuleBasedBreakIterator::last(void) {
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700586 int32_t endPos = (int32_t)utext_nativeLength(&fText);
Jungshik Shinb3189662017-11-07 11:18:34 -0800587 UBool endShouldBeBoundary = isBoundary(endPos); // Has side effect of setting iterator position.
588 (void)endShouldBeBoundary;
589 U_ASSERT(endShouldBeBoundary);
590 U_ASSERT(fPosition == endPos);
591 return endPos;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000592}
593
594/**
595 * Advances the iterator either forward or backward the specified number of steps.
596 * Negative values move backward, and positive values move forward. This is
597 * equivalent to repeatedly calling next() or previous().
598 * @param n The number of steps to move. The sign indicates the direction
599 * (negative is backwards, and positive is forwards).
600 * @return The character offset of the boundary position n boundaries away from
601 * the current one.
602 */
603int32_t RuleBasedBreakIterator::next(int32_t n) {
Jungshik Shinb3189662017-11-07 11:18:34 -0800604 int32_t result = 0;
605 if (n > 0) {
606 for (; n > 0 && result != UBRK_DONE; --n) {
607 result = next();
608 }
609 } else if (n < 0) {
610 for (; n < 0 && result != UBRK_DONE; ++n) {
611 result = previous();
612 }
613 } else {
614 result = current();
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000615 }
616 return result;
617}
618
619/**
620 * Advances the iterator to the next boundary position.
621 * @return The position of the first boundary after this one.
622 */
623int32_t RuleBasedBreakIterator::next(void) {
Jungshik Shinb3189662017-11-07 11:18:34 -0800624 fBreakCache->next();
625 return fDone ? UBRK_DONE : fPosition;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000626}
627
628/**
Jungshik Shinb3189662017-11-07 11:18:34 -0800629 * Move the iterator backwards, to the boundary preceding the current one.
630 *
631 * Starts from the current position within fText.
632 * Starting position need not be on a boundary.
633 *
634 * @return The position of the boundary position immediately preceding the starting position.
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000635 */
636int32_t RuleBasedBreakIterator::previous(void) {
Jungshik Shinb3189662017-11-07 11:18:34 -0800637 UErrorCode status = U_ZERO_ERROR;
638 fBreakCache->previous(status);
639 return fDone ? UBRK_DONE : fPosition;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000640}
641
642/**
643 * Sets the iterator to refer to the first boundary position following
644 * the specified position.
Jungshik Shinb3189662017-11-07 11:18:34 -0800645 * @param startPos The position from which to begin searching for a break position.
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000646 * @return The position of the first break after the current position.
647 */
Jungshik Shinb3189662017-11-07 11:18:34 -0800648int32_t RuleBasedBreakIterator::following(int32_t startPos) {
649 // if the supplied position is before the beginning, return the
Jungshik Shin (jungshik at google)0f8746a2015-01-08 15:46:45 -0800650 // text's starting offset
Jungshik Shinb3189662017-11-07 11:18:34 -0800651 if (startPos < 0) {
Jungshik Shin (jungshik at google)0f8746a2015-01-08 15:46:45 -0800652 return first();
653 }
654
655 // Move requested offset to a code point start. It might be on a trail surrogate,
Jungshik Shinb3189662017-11-07 11:18:34 -0800656 // or on a trail byte if the input is UTF-8. Or it may be beyond the end of the text.
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700657 utext_setNativeIndex(&fText, startPos);
658 startPos = (int32_t)utext_getNativeIndex(&fText);
Jungshik Shin (jungshik at google)0f8746a2015-01-08 15:46:45 -0800659
Jungshik Shinb3189662017-11-07 11:18:34 -0800660 UErrorCode status = U_ZERO_ERROR;
661 fBreakCache->following(startPos, status);
662 return fDone ? UBRK_DONE : fPosition;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000663}
664
665/**
666 * Sets the iterator to refer to the last boundary position before the
667 * specified position.
Jungshik Shinb3189662017-11-07 11:18:34 -0800668 * @param offset The position to begin searching for a break from.
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000669 * @return The position of the last boundary before the starting position.
670 */
671int32_t RuleBasedBreakIterator::preceding(int32_t offset) {
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700672 if (offset > utext_nativeLength(&fText)) {
Jungshik Shin (jungshik at google)0f8746a2015-01-08 15:46:45 -0800673 return last();
674 }
Jungshik Shin (jungshik at google)0f8746a2015-01-08 15:46:45 -0800675
676 // Move requested offset to a code point start. It might be on a trail surrogate,
677 // or on a trail byte if the input is UTF-8.
Jungshik Shinb3189662017-11-07 11:18:34 -0800678
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700679 utext_setNativeIndex(&fText, offset);
Jungshik Shin42d50272018-10-24 01:22:09 -0700680 int32_t adjustedOffset = static_cast<int32_t>(utext_getNativeIndex(&fText));
Jungshik Shin (jungshik at google)0f8746a2015-01-08 15:46:45 -0800681
Jungshik Shinb3189662017-11-07 11:18:34 -0800682 UErrorCode status = U_ZERO_ERROR;
683 fBreakCache->preceding(adjustedOffset, status);
684 return fDone ? UBRK_DONE : fPosition;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000685}
686
687/**
Frank Tang3e05d9d2021-11-08 14:04:04 -0800688 * Returns true if the specified position is a boundary position. As a side
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000689 * effect, leaves the iterator pointing to the first boundary position at
690 * or after "offset".
Jungshik Shinb3189662017-11-07 11:18:34 -0800691 *
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000692 * @param offset the offset to check.
693 * @return True if "offset" is a boundary position.
694 */
695UBool RuleBasedBreakIterator::isBoundary(int32_t offset) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000696 // out-of-range indexes are never boundary positions
697 if (offset < 0) {
698 first(); // For side effects on current position, tag values.
Frank Tang1f164ee2022-11-08 12:31:27 -0800699 return false;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000700 }
701
Jungshik Shinb3189662017-11-07 11:18:34 -0800702 // Adjust offset to be on a code point boundary and not beyond the end of the text.
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700703 // Note that isBoundary() is always false for offsets that are not on code point boundaries.
Jungshik Shinb3189662017-11-07 11:18:34 -0800704 // But we still need the side effect of leaving iteration at the following boundary.
705
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700706 utext_setNativeIndex(&fText, offset);
Jungshik Shin42d50272018-10-24 01:22:09 -0700707 int32_t adjustedOffset = static_cast<int32_t>(utext_getNativeIndex(&fText));
Jungshik Shinb3189662017-11-07 11:18:34 -0800708
709 bool result = false;
710 UErrorCode status = U_ZERO_ERROR;
711 if (fBreakCache->seek(adjustedOffset) || fBreakCache->populateNear(adjustedOffset, status)) {
712 result = (fBreakCache->current() == offset);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000713 }
714
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700715 if (result && adjustedOffset < offset && utext_char32At(&fText, offset) == U_SENTINEL) {
Frank Tang1f164ee2022-11-08 12:31:27 -0800716 // Original offset is beyond the end of the text. Return false, it's not a boundary,
Jungshik Shinb3189662017-11-07 11:18:34 -0800717 // but the iteration position remains set to the end of the text, which is a boundary.
Frank Tang1f164ee2022-11-08 12:31:27 -0800718 return false;
Jungshik Shinb3189662017-11-07 11:18:34 -0800719 }
720 if (!result) {
721 // Not on a boundary. isBoundary() must leave iterator on the following boundary.
722 // Cache->seek(), above, left us on the preceding boundary, so advance one.
723 next();
724 }
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000725 return result;
726}
727
Jungshik Shinb3189662017-11-07 11:18:34 -0800728
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000729/**
730 * Returns the current iteration position.
731 * @return The current iteration position.
732 */
733int32_t RuleBasedBreakIterator::current(void) const {
Jungshik Shinb3189662017-11-07 11:18:34 -0800734 return fPosition;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000735}
Jungshik Shinb3189662017-11-07 11:18:34 -0800736
737
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000738//=======================================================================
739// implementation
740//=======================================================================
741
742//
743// RBBIRunMode - the state machine runs an extra iteration at the beginning and end
744// of user text. A variable with this enum type keeps track of where we
745// are. The state machine only fetches user input while in the RUN mode.
746//
747enum RBBIRunMode {
748 RBBI_START, // state machine processing is before first char of input
749 RBBI_RUN, // state machine processing is in the user text
750 RBBI_END // state machine processing is after end of user text.
751};
752
753
Frank Tangf90543d2020-10-30 19:02:04 -0700754// Wrapper functions to select the appropriate handleNext() or handleSafePrevious()
755// instantiation, based on whether an 8 or 16 bit table is required.
Jungshik Shin5feb9ad2016-10-21 12:52:48 -0700756//
Frank Tangf90543d2020-10-30 19:02:04 -0700757// These Trie access functions will be inlined within the handleNext()/Previous() instantions.
758static inline uint16_t TrieFunc8(const UCPTrie *trie, UChar32 c) {
759 return UCPTRIE_FAST_GET(trie, UCPTRIE_8, c);
760}
Jungshik Shin5feb9ad2016-10-21 12:52:48 -0700761
Frank Tangf90543d2020-10-30 19:02:04 -0700762static inline uint16_t TrieFunc16(const UCPTrie *trie, UChar32 c) {
763 return UCPTRIE_FAST_GET(trie, UCPTRIE_16, c);
764}
Jungshik Shin5feb9ad2016-10-21 12:52:48 -0700765
Frank Tangf90543d2020-10-30 19:02:04 -0700766int32_t RuleBasedBreakIterator::handleNext() {
767 const RBBIStateTable *statetable = fData->fForwardTable;
768 bool use8BitsTrie = ucptrie_getValueWidth(fData->fTrie) == UCPTRIE_VALUE_BITS_8;
769 if (statetable->fFlags & RBBI_8BITS_ROWS) {
770 if (use8BitsTrie) {
771 return handleNext<RBBIStateTableRow8, TrieFunc8>();
772 } else {
773 return handleNext<RBBIStateTableRow8, TrieFunc16>();
Jungshik Shin5feb9ad2016-10-21 12:52:48 -0700774 }
Frank Tangf90543d2020-10-30 19:02:04 -0700775 } else {
776 if (use8BitsTrie) {
777 return handleNext<RBBIStateTableRow16, TrieFunc8>();
778 } else {
779 return handleNext<RBBIStateTableRow16, TrieFunc16>();
780 }
Jungshik Shin5feb9ad2016-10-21 12:52:48 -0700781 }
Frank Tangf90543d2020-10-30 19:02:04 -0700782}
Jungshik Shin5feb9ad2016-10-21 12:52:48 -0700783
Frank Tangf90543d2020-10-30 19:02:04 -0700784int32_t RuleBasedBreakIterator::handleSafePrevious(int32_t fromPosition) {
785 const RBBIStateTable *statetable = fData->fReverseTable;
786 bool use8BitsTrie = ucptrie_getValueWidth(fData->fTrie) == UCPTRIE_VALUE_BITS_8;
787 if (statetable->fFlags & RBBI_8BITS_ROWS) {
788 if (use8BitsTrie) {
789 return handleSafePrevious<RBBIStateTableRow8, TrieFunc8>(fromPosition);
790 } else {
791 return handleSafePrevious<RBBIStateTableRow8, TrieFunc16>(fromPosition);
Jungshik Shin5feb9ad2016-10-21 12:52:48 -0700792 }
Frank Tangf90543d2020-10-30 19:02:04 -0700793 } else {
794 if (use8BitsTrie) {
795 return handleSafePrevious<RBBIStateTableRow16, TrieFunc8>(fromPosition);
796 } else {
797 return handleSafePrevious<RBBIStateTableRow16, TrieFunc16>(fromPosition);
Jungshik Shin5feb9ad2016-10-21 12:52:48 -0700798 }
Jungshik Shin5feb9ad2016-10-21 12:52:48 -0700799 }
Frank Tangf90543d2020-10-30 19:02:04 -0700800}
Jungshik Shin5feb9ad2016-10-21 12:52:48 -0700801
802
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000803//-----------------------------------------------------------------------------------
804//
Jungshik Shinb3189662017-11-07 11:18:34 -0800805// handleNext()
806// Run the state machine to find a boundary
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000807//
808//-----------------------------------------------------------------------------------
Frank Tangf90543d2020-10-30 19:02:04 -0700809template <typename RowType, RuleBasedBreakIterator::PTrieFunc trieFunc>
Jungshik Shinb3189662017-11-07 11:18:34 -0800810int32_t RuleBasedBreakIterator::handleNext() {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000811 int32_t state;
812 uint16_t category = 0;
813 RBBIRunMode mode;
Jungshik Shinb3189662017-11-07 11:18:34 -0800814
Frank Tangf90543d2020-10-30 19:02:04 -0700815 RowType *row;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000816 UChar32 c;
Jungshik Shin5feb9ad2016-10-21 12:52:48 -0700817 int32_t result = 0;
818 int32_t initialPosition = 0;
Jungshik Shinb3189662017-11-07 11:18:34 -0800819 const RBBIStateTable *statetable = fData->fForwardTable;
Jungshik Shin5feb9ad2016-10-21 12:52:48 -0700820 const char *tableData = statetable->fTableData;
821 uint32_t tableRowLen = statetable->fRowLen;
Frank Tangf90543d2020-10-30 19:02:04 -0700822 uint32_t dictStart = statetable->fDictCategoriesStart;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000823 #ifdef RBBI_DEBUG
Jungshik Shinb3189662017-11-07 11:18:34 -0800824 if (gTrace) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000825 RBBIDebugPuts("Handle Next pos char state category");
826 }
827 #endif
828
Frank Tang7e7574b2021-04-13 21:19:13 -0700829 // handleNext always sets the break tag value.
Jungshik Shinb3189662017-11-07 11:18:34 -0800830 // Set the default for it.
831 fRuleStatusIndex = 0;
832
833 fDictionaryCharCount = 0;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000834
835 // if we're already at the end of the text, return DONE.
Jungshik Shinb3189662017-11-07 11:18:34 -0800836 initialPosition = fPosition;
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700837 UTEXT_SETNATIVEINDEX(&fText, initialPosition);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000838 result = initialPosition;
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700839 c = UTEXT_NEXT32(&fText);
Jungshik Shinb3189662017-11-07 11:18:34 -0800840 if (c==U_SENTINEL) {
Frank Tang1f164ee2022-11-08 12:31:27 -0800841 fDone = true;
Jungshik Shinb3189662017-11-07 11:18:34 -0800842 return UBRK_DONE;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000843 }
844
845 // Set the initial state for the state machine
846 state = START_STATE;
Frank Tangf90543d2020-10-30 19:02:04 -0700847 row = (RowType *)
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000848 //(statetable->fTableData + (statetable->fRowLen * state));
849 (tableData + tableRowLen * state);
Jungshik Shinb3189662017-11-07 11:18:34 -0800850
851
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000852 mode = RBBI_RUN;
853 if (statetable->fFlags & RBBI_BOF_REQUIRED) {
854 category = 2;
855 mode = RBBI_START;
856 }
857
858
859 // loop until we reach the end of the text or transition to state 0
860 //
861 for (;;) {
862 if (c == U_SENTINEL) {
863 // Reached end of input string.
864 if (mode == RBBI_END) {
Jungshik Shinb3189662017-11-07 11:18:34 -0800865 // We have already run the loop one last time with the
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000866 // character set to the psueudo {eof} value. Now it is time
867 // to unconditionally bail out.
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000868 break;
869 }
870 // Run the loop one last time with the fake end-of-input character category.
871 mode = RBBI_END;
872 category = 1;
873 }
874
875 //
876 // Get the char category. An incoming category of 1 or 2 means that
877 // we are preset for doing the beginning or end of input, and
878 // that we shouldn't get a category from an actual text input character.
879 //
880 if (mode == RBBI_RUN) {
881 // look up the current character's character category, which tells us
882 // which column in the state table to look at.
Frank Tangf90543d2020-10-30 19:02:04 -0700883 category = trieFunc(fData->fTrie, c);
884 fDictionaryCharCount += (category >= dictStart);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000885 }
886
887 #ifdef RBBI_DEBUG
Jungshik Shinb3189662017-11-07 11:18:34 -0800888 if (gTrace) {
Jungshik Shin42d50272018-10-24 01:22:09 -0700889 RBBIDebugPrintf(" %4" PRId64 " ", utext_getNativeIndex(&fText));
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000890 if (0x20<=c && c<0x7f) {
891 RBBIDebugPrintf("\"%c\" ", c);
892 } else {
893 RBBIDebugPrintf("%5x ", c);
894 }
895 RBBIDebugPrintf("%3d %3d\n", state, category);
896 }
897 #endif
898
899 // State Transition - move machine to its next state
900 //
901
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700902 // fNextState is a variable-length array.
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000903 U_ASSERT(category<fData->fHeader->fCatCount);
904 state = row->fNextState[category]; /*Not accessing beyond memory*/
Frank Tangf90543d2020-10-30 19:02:04 -0700905 row = (RowType *)
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000906 // (statetable->fTableData + (statetable->fRowLen * state));
907 (tableData + tableRowLen * state);
908
909
Frank Tangf90543d2020-10-30 19:02:04 -0700910 uint16_t accepting = row->fAccepting;
911 if (accepting == ACCEPTING_UNCONDITIONAL) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000912 // Match found, common case.
913 if (mode != RBBI_START) {
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700914 result = (int32_t)UTEXT_GETNATIVEINDEX(&fText);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000915 }
Frank Tangf90543d2020-10-30 19:02:04 -0700916 fRuleStatusIndex = row->fTagsIdx; // Remember the break status (tag) values.
917 } else if (accepting > ACCEPTING_UNCONDITIONAL) {
Jungshik Shinb3189662017-11-07 11:18:34 -0800918 // Lookahead match is completed.
Frank Tangf90543d2020-10-30 19:02:04 -0700919 U_ASSERT(accepting < fData->fForwardTable->fLookAheadResultsSize);
920 int32_t lookaheadResult = fLookAheadMatches[accepting];
Jungshik Shin5feb9ad2016-10-21 12:52:48 -0700921 if (lookaheadResult >= 0) {
Frank Tangf90543d2020-10-30 19:02:04 -0700922 fRuleStatusIndex = row->fTagsIdx;
Jungshik Shinb3189662017-11-07 11:18:34 -0800923 fPosition = lookaheadResult;
Jungshik Shin5feb9ad2016-10-21 12:52:48 -0700924 return lookaheadResult;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000925 }
Jungshik Shin5feb9ad2016-10-21 12:52:48 -0700926 }
Frank Tangf2223962020-04-27 18:25:29 -0700927
928 // If we are at the position of the '/' in a look-ahead (hard break) rule;
929 // record the current position, to be returned later, if the full rule matches.
930 // TODO: Move this check before the previous check of fAccepting.
931 // This would enable hard-break rules with no following context.
932 // But there are line break test failures when trying this. Investigate.
933 // Issue ICU-20837
Frank Tangf90543d2020-10-30 19:02:04 -0700934 uint16_t rule = row->fLookAhead;
935 U_ASSERT(rule == 0 || rule > ACCEPTING_UNCONDITIONAL);
936 U_ASSERT(rule == 0 || rule < fData->fForwardTable->fLookAheadResultsSize);
937 if (rule > ACCEPTING_UNCONDITIONAL) {
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700938 int32_t pos = (int32_t)UTEXT_GETNATIVEINDEX(&fText);
Frank Tangf90543d2020-10-30 19:02:04 -0700939 fLookAheadMatches[rule] = pos;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000940 }
941
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000942 if (state == STOP_STATE) {
943 // This is the normal exit from the lookup state machine.
944 // We have advanced through the string until it is certain that no
945 // longer match is possible, no matter what characters follow.
946 break;
947 }
Jungshik Shinb3189662017-11-07 11:18:34 -0800948
949 // Advance to the next character.
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000950 // If this is a beginning-of-input loop iteration, don't advance
951 // the input position. The next iteration will be processing the
952 // first real input character.
953 if (mode == RBBI_RUN) {
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700954 c = UTEXT_NEXT32(&fText);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000955 } else {
956 if (mode == RBBI_START) {
957 mode = RBBI_RUN;
958 }
959 }
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000960 }
961
962 // The state machine is done. Check whether it found a match...
963
964 // If the iterator failed to advance in the match engine, force it ahead by one.
965 // (This really indicates a defect in the break rules. They should always match
966 // at least one character.)
967 if (result == initialPosition) {
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700968 utext_setNativeIndex(&fText, initialPosition);
969 utext_next32(&fText);
970 result = (int32_t)utext_getNativeIndex(&fText);
Jungshik Shinb3189662017-11-07 11:18:34 -0800971 fRuleStatusIndex = 0;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000972 }
973
974 // Leave the iterator at our result position.
Jungshik Shinb3189662017-11-07 11:18:34 -0800975 fPosition = result;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000976 #ifdef RBBI_DEBUG
Jungshik Shinb3189662017-11-07 11:18:34 -0800977 if (gTrace) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000978 RBBIDebugPrintf("result = %d\n\n", result);
979 }
980 #endif
981 return result;
982}
983
984
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000985//-----------------------------------------------------------------------------------
986//
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700987// handleSafePrevious()
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000988//
Jungshik Shinb3189662017-11-07 11:18:34 -0800989// Iterate backwards using the safe reverse rules.
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700990// The logic of this function is similar to handleNext(), but simpler
991// because the safe table does not require as many options.
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000992//
993//-----------------------------------------------------------------------------------
Frank Tangf90543d2020-10-30 19:02:04 -0700994template <typename RowType, RuleBasedBreakIterator::PTrieFunc trieFunc>
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700995int32_t RuleBasedBreakIterator::handleSafePrevious(int32_t fromPosition) {
Frank Tangf90543d2020-10-30 19:02:04 -0700996
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000997 int32_t state;
998 uint16_t category = 0;
Frank Tangf90543d2020-10-30 19:02:04 -0700999 RowType *row;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001000 UChar32 c;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001001 int32_t result = 0;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001002
Jungshik Shina9a2bd32018-07-07 03:36:01 -07001003 const RBBIStateTable *stateTable = fData->fReverseTable;
Jungshik Shinf61e46d2018-05-04 13:00:45 -07001004 UTEXT_SETNATIVEINDEX(&fText, fromPosition);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001005 #ifdef RBBI_DEBUG
Jungshik Shinb3189662017-11-07 11:18:34 -08001006 if (gTrace) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001007 RBBIDebugPuts("Handle Previous pos char state category");
1008 }
1009 #endif
1010
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001011 // if we're already at the start of the text, return DONE.
Jungshik Shinf61e46d2018-05-04 13:00:45 -07001012 if (fData == NULL || UTEXT_GETNATIVEINDEX(&fText)==0) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001013 return BreakIterator::DONE;
1014 }
1015
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001016 // Set the initial state for the state machine
Jungshik Shina9a2bd32018-07-07 03:36:01 -07001017 c = UTEXT_PREVIOUS32(&fText);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001018 state = START_STATE;
Frank Tangf90543d2020-10-30 19:02:04 -07001019 row = (RowType *)
Jungshik Shinb3189662017-11-07 11:18:34 -08001020 (stateTable->fTableData + (stateTable->fRowLen * state));
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001021
1022 // loop until we reach the start of the text or transition to state 0
1023 //
Jungshik Shina9a2bd32018-07-07 03:36:01 -07001024 for (; c != U_SENTINEL; c = UTEXT_PREVIOUS32(&fText)) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001025
Jungshik Shina9a2bd32018-07-07 03:36:01 -07001026 // look up the current character's character category, which tells us
1027 // which column in the state table to look at.
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001028 //
Frank Tangf90543d2020-10-30 19:02:04 -07001029 // Off the dictionary flag bit. For reverse iteration it is not used.
1030 category = trieFunc(fData->fTrie, c);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001031
1032 #ifdef RBBI_DEBUG
Jungshik Shinb3189662017-11-07 11:18:34 -08001033 if (gTrace) {
Jungshik Shinf61e46d2018-05-04 13:00:45 -07001034 RBBIDebugPrintf(" %4d ", (int32_t)utext_getNativeIndex(&fText));
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001035 if (0x20<=c && c<0x7f) {
1036 RBBIDebugPrintf("\"%c\" ", c);
1037 } else {
1038 RBBIDebugPrintf("%5x ", c);
1039 }
1040 RBBIDebugPrintf("%3d %3d\n", state, category);
1041 }
1042 #endif
1043
1044 // State Transition - move machine to its next state
1045 //
Jungshik Shinf61e46d2018-05-04 13:00:45 -07001046 // fNextState is a variable-length array.
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001047 U_ASSERT(category<fData->fHeader->fCatCount);
1048 state = row->fNextState[category]; /*Not accessing beyond memory*/
Frank Tangf90543d2020-10-30 19:02:04 -07001049 row = (RowType *)
Jungshik Shinb3189662017-11-07 11:18:34 -08001050 (stateTable->fTableData + (stateTable->fRowLen * state));
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001051
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001052 if (state == STOP_STATE) {
1053 // This is the normal exit from the lookup state machine.
Frank Tang3e05d9d2021-11-08 14:04:04 -08001054 // Transition to state zero means we have found a safe point.
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001055 break;
1056 }
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001057 }
1058
1059 // The state machine is done. Check whether it found a match...
Jungshik Shina9a2bd32018-07-07 03:36:01 -07001060 result = (int32_t)UTEXT_GETNATIVEINDEX(&fText);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001061 #ifdef RBBI_DEBUG
Jungshik Shinb3189662017-11-07 11:18:34 -08001062 if (gTrace) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001063 RBBIDebugPrintf("result = %d\n\n", result);
1064 }
1065 #endif
1066 return result;
1067}
1068
Frank Tangf90543d2020-10-30 19:02:04 -07001069
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001070//-------------------------------------------------------------------------------
1071//
1072// getRuleStatus() Return the break rule tag associated with the current
1073// iterator position. If the iterator arrived at its current
1074// position by iterating forwards, the value will have been
1075// cached by the handleNext() function.
1076//
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001077//-------------------------------------------------------------------------------
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001078
1079int32_t RuleBasedBreakIterator::getRuleStatus() const {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001080
1081 // fLastRuleStatusIndex indexes to the start of the appropriate status record
1082 // (the number of status values.)
1083 // This function returns the last (largest) of the array of status values.
Jungshik Shinb3189662017-11-07 11:18:34 -08001084 int32_t idx = fRuleStatusIndex + fData->fRuleStatusTable[fRuleStatusIndex];
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001085 int32_t tagVal = fData->fRuleStatusTable[idx];
1086
1087 return tagVal;
1088}
1089
1090
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001091int32_t RuleBasedBreakIterator::getRuleStatusVec(
Jungshik Shinb3189662017-11-07 11:18:34 -08001092 int32_t *fillInVec, int32_t capacity, UErrorCode &status) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001093 if (U_FAILURE(status)) {
1094 return 0;
1095 }
1096
Jungshik Shinb3189662017-11-07 11:18:34 -08001097 int32_t numVals = fData->fRuleStatusTable[fRuleStatusIndex];
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001098 int32_t numValsToCopy = numVals;
1099 if (numVals > capacity) {
1100 status = U_BUFFER_OVERFLOW_ERROR;
1101 numValsToCopy = capacity;
1102 }
1103 int i;
1104 for (i=0; i<numValsToCopy; i++) {
Jungshik Shinb3189662017-11-07 11:18:34 -08001105 fillInVec[i] = fData->fRuleStatusTable[fRuleStatusIndex + i + 1];
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001106 }
1107 return numVals;
1108}
1109
1110
1111
1112//-------------------------------------------------------------------------------
1113//
1114// getBinaryRules Access to the compiled form of the rules,
1115// for use by build system tools that save the data
1116// for standard iterator types.
1117//
1118//-------------------------------------------------------------------------------
1119const uint8_t *RuleBasedBreakIterator::getBinaryRules(uint32_t &length) {
1120 const uint8_t *retPtr = NULL;
1121 length = 0;
1122
1123 if (fData != NULL) {
1124 retPtr = (const uint8_t *)fData->fHeader;
1125 length = fData->fHeader->fLength;
1126 }
1127 return retPtr;
1128}
1129
1130
Frank Tangb8696612019-10-25 14:58:21 -07001131RuleBasedBreakIterator *RuleBasedBreakIterator::createBufferClone(
1132 void * /*stackBuffer*/, int32_t &bufferSize, UErrorCode &status) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001133 if (U_FAILURE(status)){
1134 return NULL;
1135 }
1136
1137 if (bufferSize == 0) {
1138 bufferSize = 1; // preflighting for deprecated functionality
1139 return NULL;
1140 }
1141
1142 BreakIterator *clonedBI = clone();
1143 if (clonedBI == NULL) {
1144 status = U_MEMORY_ALLOCATION_ERROR;
1145 } else {
1146 status = U_SAFECLONE_ALLOCATED_WARNING;
1147 }
1148 return (RuleBasedBreakIterator *)clonedBI;
1149}
1150
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001151U_NAMESPACE_END
1152
1153
Jungshik Shinb3189662017-11-07 11:18:34 -08001154static icu::UStack *gLanguageBreakFactories = nullptr;
1155static const icu::UnicodeString *gEmptyString = nullptr;
Frank Tang1c67b4e2022-05-18 10:13:51 -07001156static icu::UInitOnce gLanguageBreakFactoriesInitOnce {};
1157static icu::UInitOnce gRBBIInitOnce {};
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001158
1159/**
Jungshik Shinb3189662017-11-07 11:18:34 -08001160 * Release all static memory held by breakiterator.
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001161 */
1162U_CDECL_BEGIN
Frank Tang13cfcd52020-03-16 13:49:12 -07001163UBool U_CALLCONV rbbi_cleanup(void) {
Jungshik Shinb3189662017-11-07 11:18:34 -08001164 delete gLanguageBreakFactories;
1165 gLanguageBreakFactories = nullptr;
1166 delete gEmptyString;
1167 gEmptyString = nullptr;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001168 gLanguageBreakFactoriesInitOnce.reset();
Jungshik Shinb3189662017-11-07 11:18:34 -08001169 gRBBIInitOnce.reset();
Frank Tang1f164ee2022-11-08 12:31:27 -08001170 return true;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001171}
1172U_CDECL_END
1173
1174U_CDECL_BEGIN
1175static void U_CALLCONV _deleteFactory(void *obj) {
1176 delete (icu::LanguageBreakFactory *) obj;
1177}
1178U_CDECL_END
1179U_NAMESPACE_BEGIN
1180
Jungshik Shinb3189662017-11-07 11:18:34 -08001181static void U_CALLCONV rbbiInit() {
1182 gEmptyString = new UnicodeString();
1183 ucln_common_registerCleanup(UCLN_COMMON_RBBI, rbbi_cleanup);
1184}
1185
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001186static void U_CALLCONV initLanguageFactories() {
1187 UErrorCode status = U_ZERO_ERROR;
1188 U_ASSERT(gLanguageBreakFactories == NULL);
1189 gLanguageBreakFactories = new UStack(_deleteFactory, NULL, status);
1190 if (gLanguageBreakFactories != NULL && U_SUCCESS(status)) {
1191 ICULanguageBreakFactory *builtIn = new ICULanguageBreakFactory(status);
1192 gLanguageBreakFactories->push(builtIn, status);
1193#ifdef U_LOCAL_SERVICE_HOOK
1194 LanguageBreakFactory *extra = (LanguageBreakFactory *)uprv_svc_hook("languageBreakFactory", &status);
1195 if (extra != NULL) {
1196 gLanguageBreakFactories->push(extra, status);
1197 }
1198#endif
1199 }
Jungshik Shinb3189662017-11-07 11:18:34 -08001200 ucln_common_registerCleanup(UCLN_COMMON_RBBI, rbbi_cleanup);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001201}
1202
1203
1204static const LanguageBreakEngine*
Jungshik Shinf61e46d2018-05-04 13:00:45 -07001205getLanguageBreakEngineFromFactory(UChar32 c)
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001206{
1207 umtx_initOnce(gLanguageBreakFactoriesInitOnce, &initLanguageFactories);
1208 if (gLanguageBreakFactories == NULL) {
1209 return NULL;
1210 }
Jungshik Shinb3189662017-11-07 11:18:34 -08001211
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001212 int32_t i = gLanguageBreakFactories->size();
1213 const LanguageBreakEngine *lbe = NULL;
1214 while (--i >= 0) {
1215 LanguageBreakFactory *factory = (LanguageBreakFactory *)(gLanguageBreakFactories->elementAt(i));
Jungshik Shinf61e46d2018-05-04 13:00:45 -07001216 lbe = factory->getEngineFor(c);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001217 if (lbe != NULL) {
1218 break;
1219 }
1220 }
1221 return lbe;
1222}
1223
1224
1225//-------------------------------------------------------------------------------
1226//
1227// getLanguageBreakEngine Find an appropriate LanguageBreakEngine for the
1228// the character c.
1229//
1230//-------------------------------------------------------------------------------
1231const LanguageBreakEngine *
1232RuleBasedBreakIterator::getLanguageBreakEngine(UChar32 c) {
1233 const LanguageBreakEngine *lbe = NULL;
1234 UErrorCode status = U_ZERO_ERROR;
Jungshik Shinb3189662017-11-07 11:18:34 -08001235
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001236 if (fLanguageBreakEngines == NULL) {
1237 fLanguageBreakEngines = new UStack(status);
1238 if (fLanguageBreakEngines == NULL || U_FAILURE(status)) {
1239 delete fLanguageBreakEngines;
1240 fLanguageBreakEngines = 0;
1241 return NULL;
1242 }
1243 }
Jungshik Shinb3189662017-11-07 11:18:34 -08001244
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001245 int32_t i = fLanguageBreakEngines->size();
1246 while (--i >= 0) {
1247 lbe = (const LanguageBreakEngine *)(fLanguageBreakEngines->elementAt(i));
Jungshik Shinf61e46d2018-05-04 13:00:45 -07001248 if (lbe->handles(c)) {
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001249 return lbe;
1250 }
1251 }
Jungshik Shinb3189662017-11-07 11:18:34 -08001252
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001253 // No existing dictionary took the character. See if a factory wants to
1254 // give us a new LanguageBreakEngine for this character.
Jungshik Shinf61e46d2018-05-04 13:00:45 -07001255 lbe = getLanguageBreakEngineFromFactory(c);
Jungshik Shinb3189662017-11-07 11:18:34 -08001256
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001257 // If we got one, use it and push it on our stack.
1258 if (lbe != NULL) {
1259 fLanguageBreakEngines->push((void *)lbe, status);
1260 // Even if we can't remember it, we can keep looking it up, so
1261 // return it even if the push fails.
1262 return lbe;
1263 }
Jungshik Shinb3189662017-11-07 11:18:34 -08001264
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001265 // No engine is forthcoming for this character. Add it to the
1266 // reject set. Create the reject break engine if needed.
1267 if (fUnhandledBreakEngine == NULL) {
1268 fUnhandledBreakEngine = new UnhandledEngine(status);
1269 if (U_SUCCESS(status) && fUnhandledBreakEngine == NULL) {
1270 status = U_MEMORY_ALLOCATION_ERROR;
Jungshik Shinf61e46d2018-05-04 13:00:45 -07001271 return nullptr;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001272 }
1273 // Put it last so that scripts for which we have an engine get tried
1274 // first.
1275 fLanguageBreakEngines->insertElementAt(fUnhandledBreakEngine, 0, status);
1276 // If we can't insert it, or creation failed, get rid of it
Frank Tang3e05d9d2021-11-08 14:04:04 -08001277 U_ASSERT(!fLanguageBreakEngines->hasDeleter());
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001278 if (U_FAILURE(status)) {
1279 delete fUnhandledBreakEngine;
1280 fUnhandledBreakEngine = 0;
1281 return NULL;
1282 }
1283 }
Jungshik Shinb3189662017-11-07 11:18:34 -08001284
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001285 // Tell the reject engine about the character; at its discretion, it may
1286 // add more than just the one character.
Jungshik Shinf61e46d2018-05-04 13:00:45 -07001287 fUnhandledBreakEngine->handleCharacter(c);
Jungshik Shinb3189662017-11-07 11:18:34 -08001288
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001289 return fUnhandledBreakEngine;
1290}
1291
Jungshik Shinb3189662017-11-07 11:18:34 -08001292void RuleBasedBreakIterator::dumpCache() {
1293 fBreakCache->dumpCache();
1294}
1295
Jungshik Shinf61e46d2018-05-04 13:00:45 -07001296void RuleBasedBreakIterator::dumpTables() {
1297 fData->printData();
1298}
1299
Jungshik Shinb3189662017-11-07 11:18:34 -08001300/**
1301 * Returns the description used to create this iterator
1302 */
1303
1304const UnicodeString&
1305RuleBasedBreakIterator::getRules() const {
1306 if (fData != NULL) {
1307 return fData->getRuleSourceString();
1308 } else {
1309 umtx_initOnce(gRBBIInitOnce, &rbbiInit);
1310 return *gEmptyString;
1311 }
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00001312}
1313
1314U_NAMESPACE_END
1315
1316#endif /* #if !UCONFIG_NO_BREAK_ITERATION */