Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 1 | // © 2016 and later: Unicode, Inc. and others. |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 2 | // License & terms of use: http://www.unicode.org/copyright.html |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 3 | /* |
| 4 | *************************************************************************** |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 5 | * Copyright (C) 1999-2016 International Business Machines Corporation |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 6 | * and others. All rights reserved. |
| 7 | *************************************************************************** |
| 8 | */ |
| 9 | // |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 10 | // file: rbbi.cpp Contains the implementation of the rule based break iterator |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 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 | |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 21 | #include <cinttypes> |
| 22 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 23 | #include "unicode/rbbi.h" |
| 24 | #include "unicode/schriter.h" |
| 25 | #include "unicode/uchriter.h" |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 26 | #include "unicode/uclean.h" |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 27 | #include "unicode/udata.h" |
| 28 | |
| 29 | #include "brkeng.h" |
| 30 | #include "ucln_cmn.h" |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 31 | #include "cmemory.h" |
| 32 | #include "cstring.h" |
Frank Tang | 35f7e13 | 2019-04-12 15:58:06 -0700 | [diff] [blame] | 33 | #include "localsvc.h" |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 34 | #include "rbbidata.h" |
| 35 | #include "rbbi_cache.h" |
| 36 | #include "rbbirb.h" |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 37 | #include "uassert.h" |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 38 | #include "umutex.h" |
| 39 | #include "uvectr32.h" |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 40 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 41 | #ifdef RBBI_DEBUG |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 42 | static UBool gTrace = false; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
| 45 | U_NAMESPACE_BEGIN |
| 46 | |
| 47 | // The state number of the starting state |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 48 | constexpr int32_t START_STATE = 1; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 49 | |
| 50 | // The state-transition value indicating "stop" |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 51 | constexpr int32_t STOP_STATE = 0; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 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 | */ |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 65 | RuleBasedBreakIterator::RuleBasedBreakIterator(RBBIDataHeader* data, UErrorCode &status) |
| 66 | : fSCharIter(UnicodeString()) |
| 67 | { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 68 | init(status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 69 | fData = new RBBIDataWrapper(data, status); // status checked in constructor |
| 70 | if (U_FAILURE(status)) {return;} |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 71 | if(fData == nullptr) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 72 | status = U_MEMORY_ALLOCATION_ERROR; |
| 73 | return; |
| 74 | } |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 75 | 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.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Frank Tang | d2858cb | 2022-04-08 20:34:12 -0700 | [diff] [blame] | 85 | //------------------------------------------------------------------------------- |
| 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 | //------------------------------------------------------------------------------- |
| 92 | RuleBasedBreakIterator::RuleBasedBreakIterator(UDataMemory* udm, UBool isPhraseBreaking, |
| 93 | UErrorCode &status) : RuleBasedBreakIterator(udm, status) |
| 94 | { |
| 95 | fIsPhraseBreaking = isPhraseBreaking; |
| 96 | } |
| 97 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 98 | // |
| 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 | // |
| 102 | RuleBasedBreakIterator::RuleBasedBreakIterator(const uint8_t *compiledRules, |
| 103 | uint32_t ruleLength, |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 104 | UErrorCode &status) |
| 105 | : fSCharIter(UnicodeString()) |
| 106 | { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 107 | init(status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 108 | 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 Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 120 | fData = new RBBIDataWrapper(data, RBBIDataWrapper::kDontAdopt, status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 121 | if (U_FAILURE(status)) {return;} |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 122 | if(fData == nullptr) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 123 | status = U_MEMORY_ALLOCATION_ERROR; |
| 124 | return; |
| 125 | } |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 126 | 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 Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 134 | } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 135 | |
| 136 | |
| 137 | //------------------------------------------------------------------------------- |
| 138 | // |
| 139 | // Constructor from a UDataMemory handle to precompiled break rules |
| 140 | // stored in an ICU data file. |
| 141 | // |
| 142 | //------------------------------------------------------------------------------- |
| 143 | RuleBasedBreakIterator::RuleBasedBreakIterator(UDataMemory* udm, UErrorCode &status) |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 144 | : fSCharIter(UnicodeString()) |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 145 | { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 146 | init(status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 147 | fData = new RBBIDataWrapper(udm, status); // status checked in constructor |
| 148 | if (U_FAILURE(status)) {return;} |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 149 | if(fData == nullptr) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 150 | status = U_MEMORY_ALLOCATION_ERROR; |
| 151 | return; |
| 152 | } |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 153 | 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.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | |
| 164 | |
| 165 | //------------------------------------------------------------------------------- |
| 166 | // |
| 167 | // Constructor from a set of rules supplied as a string. |
| 168 | // |
| 169 | //------------------------------------------------------------------------------- |
| 170 | RuleBasedBreakIterator::RuleBasedBreakIterator( const UnicodeString &rules, |
| 171 | UParseError &parseError, |
| 172 | UErrorCode &status) |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 173 | : fSCharIter(UnicodeString()) |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 174 | { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 175 | init(status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 176 | 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 Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 196 | RuleBasedBreakIterator::RuleBasedBreakIterator() |
| 197 | : fSCharIter(UnicodeString()) |
| 198 | { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 199 | UErrorCode status = U_ZERO_ERROR; |
| 200 | init(status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 201 | } |
| 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 | //------------------------------------------------------------------------------- |
| 210 | RuleBasedBreakIterator::RuleBasedBreakIterator(const RuleBasedBreakIterator& other) |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 211 | : BreakIterator(other), |
| 212 | fSCharIter(UnicodeString()) |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 213 | { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 214 | UErrorCode status = U_ZERO_ERROR; |
| 215 | this->init(status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 216 | *this = other; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | /** |
| 221 | * Destructor |
| 222 | */ |
| 223 | RuleBasedBreakIterator::~RuleBasedBreakIterator() { |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 224 | if (fCharIter != &fSCharIter) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 225 | // fCharIter was adopted from the outside. |
| 226 | delete fCharIter; |
| 227 | } |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 228 | fCharIter = nullptr; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 229 | |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 230 | utext_close(&fText); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 231 | |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 232 | if (fData != nullptr) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 233 | fData->removeReference(); |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 234 | fData = nullptr; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 235 | } |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 236 | delete fBreakCache; |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 237 | fBreakCache = nullptr; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 238 | |
| 239 | delete fDictionaryCache; |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 240 | fDictionaryCache = nullptr; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 241 | |
| 242 | delete fLanguageBreakEngines; |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 243 | fLanguageBreakEngines = nullptr; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 244 | |
| 245 | delete fUnhandledBreakEngine; |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 246 | fUnhandledBreakEngine = nullptr; |
| 247 | |
| 248 | uprv_free(fLookAheadMatches); |
| 249 | fLookAheadMatches = nullptr; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 250 | } |
| 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 Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 255 | * TODO: needs better handling of memory allocation errors. |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 256 | */ |
| 257 | RuleBasedBreakIterator& |
| 258 | RuleBasedBreakIterator::operator=(const RuleBasedBreakIterator& that) { |
| 259 | if (this == &that) { |
| 260 | return *this; |
| 261 | } |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 262 | BreakIterator::operator=(that); |
| 263 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 264 | 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 Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 270 | utext_clone(&fText, &that.fText, false, true, &status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 271 | |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 272 | if (fCharIter != &fSCharIter) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 273 | delete fCharIter; |
| 274 | } |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 275 | fCharIter = &fSCharIter; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 276 | |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 277 | if (that.fCharIter != NULL && that.fCharIter != &that.fSCharIter) { |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 278 | // This is a little bit tricky - it will initially appear that |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 279 | // this->fCharIter is adopted, even if that->fCharIter was |
| 280 | // not adopted. That's ok. |
| 281 | fCharIter = that.fCharIter->clone(); |
| 282 | } |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 283 | fSCharIter = that.fSCharIter; |
| 284 | if (fCharIter == NULL) { |
| 285 | fCharIter = &fSCharIter; |
| 286 | } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 287 | |
| 288 | if (fData != NULL) { |
| 289 | fData->removeReference(); |
| 290 | fData = NULL; |
| 291 | } |
| 292 | if (that.fData != NULL) { |
| 293 | fData = that.fData->addReference(); |
| 294 | } |
| 295 | |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 296 | 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 Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 304 | 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.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 315 | 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 Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 326 | void RuleBasedBreakIterator::init(UErrorCode &status) { |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 327 | fCharIter = nullptr; |
| 328 | fData = nullptr; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 329 | fPosition = 0; |
| 330 | fRuleStatusIndex = 0; |
| 331 | fDone = false; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 332 | fDictionaryCharCount = 0; |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 333 | fLanguageBreakEngines = nullptr; |
| 334 | fUnhandledBreakEngine = nullptr; |
| 335 | fBreakCache = nullptr; |
| 336 | fDictionaryCache = nullptr; |
| 337 | fLookAheadMatches = nullptr; |
Frank Tang | d2858cb | 2022-04-08 20:34:12 -0700 | [diff] [blame] | 338 | fIsPhraseBreaking = false; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 339 | |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 340 | // 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 Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 346 | return; |
| 347 | } |
| 348 | |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 349 | utext_openUChars(&fText, NULL, 0, &status); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 350 | fDictionaryCache = new DictionaryCache(this, status); |
| 351 | fBreakCache = new BreakCache(this, status); |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 352 | if (U_SUCCESS(status) && (fDictionaryCache == NULL || fBreakCache == NULL)) { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 353 | status = U_MEMORY_ALLOCATION_ERROR; |
| 354 | } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 355 | |
| 356 | #ifdef RBBI_DEBUG |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 357 | static UBool debugInitDone = false; |
| 358 | if (debugInitDone == false) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 359 | char *debugEnv = getenv("U_RBBIDEBUG"); |
| 360 | if (debugEnv && uprv_strstr(debugEnv, "trace")) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 361 | gTrace = true; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 362 | } |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 363 | debugInitDone = true; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 364 | } |
| 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 Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 377 | RuleBasedBreakIterator* |
| 378 | RuleBasedBreakIterator::clone() const { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 379 | return new RuleBasedBreakIterator(*this); |
| 380 | } |
| 381 | |
| 382 | /** |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 383 | * Equality operator. Returns true if both BreakIterators are of the |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 384 | * same class, have the same behavior, and iterate over the same text. |
| 385 | */ |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 386 | bool |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 387 | RuleBasedBreakIterator::operator==(const BreakIterator& that) const { |
| 388 | if (typeid(*this) != typeid(that)) { |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 389 | return false; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 390 | } |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 391 | if (this == &that) { |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 392 | return true; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 393 | } |
| 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.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 398 | |
| 399 | const RuleBasedBreakIterator& that2 = (const RuleBasedBreakIterator&) that; |
| 400 | |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 401 | if (!utext_equals(&fText, &that2.fText)) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 402 | // The two break iterators are operating on different text, |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 403 | // or have a different iteration position. |
| 404 | // Note that fText's position is always the same as the break iterator's position. |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 405 | return false; |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 406 | } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 407 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 408 | if (!(fPosition == that2.fPosition && |
| 409 | fRuleStatusIndex == that2.fRuleStatusIndex && |
| 410 | fDone == that2.fDone)) { |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 411 | return false; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 412 | } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 413 | |
| 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 Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 417 | return true; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 418 | } |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 419 | return false; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Compute a hash code for this BreakIterator |
| 424 | * @return A hash code |
| 425 | */ |
| 426 | int32_t |
| 427 | RuleBasedBreakIterator::hashCode(void) const { |
| 428 | int32_t hash = 0; |
| 429 | if (fData != NULL) { |
| 430 | hash = fData->hashCode(); |
| 431 | } |
| 432 | return hash; |
| 433 | } |
| 434 | |
| 435 | |
| 436 | void RuleBasedBreakIterator::setText(UText *ut, UErrorCode &status) { |
| 437 | if (U_FAILURE(status)) { |
| 438 | return; |
| 439 | } |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 440 | fBreakCache->reset(); |
| 441 | fDictionaryCache->reset(); |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 442 | utext_clone(&fText, ut, false, true, &status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 443 | |
| 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 Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 450 | fSCharIter.setText(UnicodeString()); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 451 | |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 452 | if (fCharIter != &fSCharIter) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 453 | // existing fCharIter was adopted from the outside. Delete it now. |
| 454 | delete fCharIter; |
| 455 | } |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 456 | fCharIter = &fSCharIter; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 457 | |
| 458 | this->first(); |
| 459 | } |
| 460 | |
| 461 | |
| 462 | UText *RuleBasedBreakIterator::getUText(UText *fillIn, UErrorCode &status) const { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 463 | UText *result = utext_clone(fillIn, &fText, false, true, &status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 464 | return result; |
| 465 | } |
| 466 | |
| 467 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 468 | //======================================================================= |
| 469 | // BreakIterator overrides |
| 470 | //======================================================================= |
| 471 | |
| 472 | /** |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 473 | * Return a CharacterIterator over the text being analyzed. |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 474 | */ |
| 475 | CharacterIterator& |
| 476 | RuleBasedBreakIterator::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 | */ |
| 485 | void |
| 486 | RuleBasedBreakIterator::adoptText(CharacterIterator* newText) { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 487 | // If we are holding a CharacterIterator adopted from a |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 488 | // previous call to this function, delete it now. |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 489 | if (fCharIter != &fSCharIter) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 490 | delete fCharIter; |
| 491 | } |
| 492 | |
| 493 | fCharIter = newText; |
| 494 | UErrorCode status = U_ZERO_ERROR; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 495 | fBreakCache->reset(); |
| 496 | fDictionaryCache->reset(); |
| 497 | if (newText==NULL || newText->startIndex() != 0) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 498 | // 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 Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 500 | utext_openUChars(&fText, NULL, 0, &status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 501 | } else { |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 502 | utext_openCharacterIterator(&fText, newText, &status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 503 | } |
| 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 | */ |
| 512 | void |
| 513 | RuleBasedBreakIterator::setText(const UnicodeString& newText) { |
| 514 | UErrorCode status = U_ZERO_ERROR; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 515 | fBreakCache->reset(); |
| 516 | fDictionaryCache->reset(); |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 517 | utext_openConstUnicodeString(&fText, &newText, &status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 518 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 519 | // Set up a character iterator on the string. |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 520 | // 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 Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 523 | fSCharIter.setText(newText); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 524 | |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 525 | if (fCharIter != &fSCharIter) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 526 | // old fCharIter was adopted from the outside. Delete it. |
| 527 | delete fCharIter; |
| 528 | } |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 529 | fCharIter = &fSCharIter; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 530 | |
| 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 | */ |
| 541 | RuleBasedBreakIterator &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 Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 549 | int64_t pos = utext_getNativeIndex(&fText); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 550 | // Shallow read-only clone of the new UText into the existing input UText |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 551 | utext_clone(&fText, input, false, true, &status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 552 | if (U_FAILURE(status)) { |
| 553 | return *this; |
| 554 | } |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 555 | utext_setNativeIndex(&fText, pos); |
| 556 | if (utext_getNativeIndex(&fText) != pos) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 557 | // 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) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 568 | * Sets the current iteration position to the beginning of the text, position zero. |
| 569 | * @return The new iterator position, which is zero. |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 570 | */ |
| 571 | int32_t RuleBasedBreakIterator::first(void) { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 572 | 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.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 578 | 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 | */ |
| 585 | int32_t RuleBasedBreakIterator::last(void) { |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 586 | int32_t endPos = (int32_t)utext_nativeLength(&fText); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 587 | 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.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 592 | } |
| 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 | */ |
| 603 | int32_t RuleBasedBreakIterator::next(int32_t n) { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 604 | 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.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 615 | } |
| 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 | */ |
| 623 | int32_t RuleBasedBreakIterator::next(void) { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 624 | fBreakCache->next(); |
| 625 | return fDone ? UBRK_DONE : fPosition; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | /** |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 629 | * 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.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 635 | */ |
| 636 | int32_t RuleBasedBreakIterator::previous(void) { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 637 | UErrorCode status = U_ZERO_ERROR; |
| 638 | fBreakCache->previous(status); |
| 639 | return fDone ? UBRK_DONE : fPosition; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /** |
| 643 | * Sets the iterator to refer to the first boundary position following |
| 644 | * the specified position. |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 645 | * @param startPos The position from which to begin searching for a break position. |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 646 | * @return The position of the first break after the current position. |
| 647 | */ |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 648 | int32_t RuleBasedBreakIterator::following(int32_t startPos) { |
| 649 | // if the supplied position is before the beginning, return the |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 650 | // text's starting offset |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 651 | if (startPos < 0) { |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 652 | return first(); |
| 653 | } |
| 654 | |
| 655 | // Move requested offset to a code point start. It might be on a trail surrogate, |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 656 | // or on a trail byte if the input is UTF-8. Or it may be beyond the end of the text. |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 657 | utext_setNativeIndex(&fText, startPos); |
| 658 | startPos = (int32_t)utext_getNativeIndex(&fText); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 659 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 660 | UErrorCode status = U_ZERO_ERROR; |
| 661 | fBreakCache->following(startPos, status); |
| 662 | return fDone ? UBRK_DONE : fPosition; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | /** |
| 666 | * Sets the iterator to refer to the last boundary position before the |
| 667 | * specified position. |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 668 | * @param offset The position to begin searching for a break from. |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 669 | * @return The position of the last boundary before the starting position. |
| 670 | */ |
| 671 | int32_t RuleBasedBreakIterator::preceding(int32_t offset) { |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 672 | if (offset > utext_nativeLength(&fText)) { |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 673 | return last(); |
| 674 | } |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 675 | |
| 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 Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 678 | |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 679 | utext_setNativeIndex(&fText, offset); |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 680 | int32_t adjustedOffset = static_cast<int32_t>(utext_getNativeIndex(&fText)); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 681 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 682 | UErrorCode status = U_ZERO_ERROR; |
| 683 | fBreakCache->preceding(adjustedOffset, status); |
| 684 | return fDone ? UBRK_DONE : fPosition; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /** |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 688 | * Returns true if the specified position is a boundary position. As a side |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 689 | * effect, leaves the iterator pointing to the first boundary position at |
| 690 | * or after "offset". |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 691 | * |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 692 | * @param offset the offset to check. |
| 693 | * @return True if "offset" is a boundary position. |
| 694 | */ |
| 695 | UBool RuleBasedBreakIterator::isBoundary(int32_t offset) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 696 | // out-of-range indexes are never boundary positions |
| 697 | if (offset < 0) { |
| 698 | first(); // For side effects on current position, tag values. |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 699 | return false; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 702 | // Adjust offset to be on a code point boundary and not beyond the end of the text. |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 703 | // Note that isBoundary() is always false for offsets that are not on code point boundaries. |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 704 | // But we still need the side effect of leaving iteration at the following boundary. |
| 705 | |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 706 | utext_setNativeIndex(&fText, offset); |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 707 | int32_t adjustedOffset = static_cast<int32_t>(utext_getNativeIndex(&fText)); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 708 | |
| 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.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 715 | if (result && adjustedOffset < offset && utext_char32At(&fText, offset) == U_SENTINEL) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 716 | // Original offset is beyond the end of the text. Return false, it's not a boundary, |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 717 | // but the iteration position remains set to the end of the text, which is a boundary. |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 718 | return false; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 719 | } |
| 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.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 725 | return result; |
| 726 | } |
| 727 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 728 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 729 | /** |
| 730 | * Returns the current iteration position. |
| 731 | * @return The current iteration position. |
| 732 | */ |
| 733 | int32_t RuleBasedBreakIterator::current(void) const { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 734 | return fPosition; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 735 | } |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 736 | |
| 737 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 738 | //======================================================================= |
| 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 | // |
| 747 | enum 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 Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 754 | // Wrapper functions to select the appropriate handleNext() or handleSafePrevious() |
| 755 | // instantiation, based on whether an 8 or 16 bit table is required. |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 756 | // |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 757 | // These Trie access functions will be inlined within the handleNext()/Previous() instantions. |
| 758 | static inline uint16_t TrieFunc8(const UCPTrie *trie, UChar32 c) { |
| 759 | return UCPTRIE_FAST_GET(trie, UCPTRIE_8, c); |
| 760 | } |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 761 | |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 762 | static inline uint16_t TrieFunc16(const UCPTrie *trie, UChar32 c) { |
| 763 | return UCPTRIE_FAST_GET(trie, UCPTRIE_16, c); |
| 764 | } |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 765 | |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 766 | int32_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 Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 774 | } |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 775 | } else { |
| 776 | if (use8BitsTrie) { |
| 777 | return handleNext<RBBIStateTableRow16, TrieFunc8>(); |
| 778 | } else { |
| 779 | return handleNext<RBBIStateTableRow16, TrieFunc16>(); |
| 780 | } |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 781 | } |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 782 | } |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 783 | |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 784 | int32_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 Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 792 | } |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 793 | } else { |
| 794 | if (use8BitsTrie) { |
| 795 | return handleSafePrevious<RBBIStateTableRow16, TrieFunc8>(fromPosition); |
| 796 | } else { |
| 797 | return handleSafePrevious<RBBIStateTableRow16, TrieFunc16>(fromPosition); |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 798 | } |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 799 | } |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 800 | } |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 801 | |
| 802 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 803 | //----------------------------------------------------------------------------------- |
| 804 | // |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 805 | // handleNext() |
| 806 | // Run the state machine to find a boundary |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 807 | // |
| 808 | //----------------------------------------------------------------------------------- |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 809 | template <typename RowType, RuleBasedBreakIterator::PTrieFunc trieFunc> |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 810 | int32_t RuleBasedBreakIterator::handleNext() { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 811 | int32_t state; |
| 812 | uint16_t category = 0; |
| 813 | RBBIRunMode mode; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 814 | |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 815 | RowType *row; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 816 | UChar32 c; |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 817 | int32_t result = 0; |
| 818 | int32_t initialPosition = 0; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 819 | const RBBIStateTable *statetable = fData->fForwardTable; |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 820 | const char *tableData = statetable->fTableData; |
| 821 | uint32_t tableRowLen = statetable->fRowLen; |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 822 | uint32_t dictStart = statetable->fDictCategoriesStart; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 823 | #ifdef RBBI_DEBUG |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 824 | if (gTrace) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 825 | RBBIDebugPuts("Handle Next pos char state category"); |
| 826 | } |
| 827 | #endif |
| 828 | |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 829 | // handleNext always sets the break tag value. |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 830 | // Set the default for it. |
| 831 | fRuleStatusIndex = 0; |
| 832 | |
| 833 | fDictionaryCharCount = 0; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 834 | |
| 835 | // if we're already at the end of the text, return DONE. |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 836 | initialPosition = fPosition; |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 837 | UTEXT_SETNATIVEINDEX(&fText, initialPosition); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 838 | result = initialPosition; |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 839 | c = UTEXT_NEXT32(&fText); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 840 | if (c==U_SENTINEL) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 841 | fDone = true; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 842 | return UBRK_DONE; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | // Set the initial state for the state machine |
| 846 | state = START_STATE; |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 847 | row = (RowType *) |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 848 | //(statetable->fTableData + (statetable->fRowLen * state)); |
| 849 | (tableData + tableRowLen * state); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 850 | |
| 851 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 852 | 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 Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 865 | // We have already run the loop one last time with the |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 866 | // character set to the psueudo {eof} value. Now it is time |
| 867 | // to unconditionally bail out. |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 868 | 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 Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 883 | category = trieFunc(fData->fTrie, c); |
| 884 | fDictionaryCharCount += (category >= dictStart); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | #ifdef RBBI_DEBUG |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 888 | if (gTrace) { |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 889 | RBBIDebugPrintf(" %4" PRId64 " ", utext_getNativeIndex(&fText)); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 890 | 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 Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 902 | // fNextState is a variable-length array. |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 903 | U_ASSERT(category<fData->fHeader->fCatCount); |
| 904 | state = row->fNextState[category]; /*Not accessing beyond memory*/ |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 905 | row = (RowType *) |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 906 | // (statetable->fTableData + (statetable->fRowLen * state)); |
| 907 | (tableData + tableRowLen * state); |
| 908 | |
| 909 | |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 910 | uint16_t accepting = row->fAccepting; |
| 911 | if (accepting == ACCEPTING_UNCONDITIONAL) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 912 | // Match found, common case. |
| 913 | if (mode != RBBI_START) { |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 914 | result = (int32_t)UTEXT_GETNATIVEINDEX(&fText); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 915 | } |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 916 | fRuleStatusIndex = row->fTagsIdx; // Remember the break status (tag) values. |
| 917 | } else if (accepting > ACCEPTING_UNCONDITIONAL) { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 918 | // Lookahead match is completed. |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 919 | U_ASSERT(accepting < fData->fForwardTable->fLookAheadResultsSize); |
| 920 | int32_t lookaheadResult = fLookAheadMatches[accepting]; |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 921 | if (lookaheadResult >= 0) { |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 922 | fRuleStatusIndex = row->fTagsIdx; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 923 | fPosition = lookaheadResult; |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 924 | return lookaheadResult; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 925 | } |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 926 | } |
Frank Tang | f222396 | 2020-04-27 18:25:29 -0700 | [diff] [blame] | 927 | |
| 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 Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 934 | 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 Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 938 | int32_t pos = (int32_t)UTEXT_GETNATIVEINDEX(&fText); |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 939 | fLookAheadMatches[rule] = pos; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 940 | } |
| 941 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 942 | 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 Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 948 | |
| 949 | // Advance to the next character. |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 950 | // 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 Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 954 | c = UTEXT_NEXT32(&fText); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 955 | } else { |
| 956 | if (mode == RBBI_START) { |
| 957 | mode = RBBI_RUN; |
| 958 | } |
| 959 | } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 960 | } |
| 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 Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 968 | utext_setNativeIndex(&fText, initialPosition); |
| 969 | utext_next32(&fText); |
| 970 | result = (int32_t)utext_getNativeIndex(&fText); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 971 | fRuleStatusIndex = 0; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | // Leave the iterator at our result position. |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 975 | fPosition = result; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 976 | #ifdef RBBI_DEBUG |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 977 | if (gTrace) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 978 | RBBIDebugPrintf("result = %d\n\n", result); |
| 979 | } |
| 980 | #endif |
| 981 | return result; |
| 982 | } |
| 983 | |
| 984 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 985 | //----------------------------------------------------------------------------------- |
| 986 | // |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 987 | // handleSafePrevious() |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 988 | // |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 989 | // Iterate backwards using the safe reverse rules. |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 990 | // The logic of this function is similar to handleNext(), but simpler |
| 991 | // because the safe table does not require as many options. |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 992 | // |
| 993 | //----------------------------------------------------------------------------------- |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 994 | template <typename RowType, RuleBasedBreakIterator::PTrieFunc trieFunc> |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 995 | int32_t RuleBasedBreakIterator::handleSafePrevious(int32_t fromPosition) { |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 996 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 997 | int32_t state; |
| 998 | uint16_t category = 0; |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 999 | RowType *row; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1000 | UChar32 c; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1001 | int32_t result = 0; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1002 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 1003 | const RBBIStateTable *stateTable = fData->fReverseTable; |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 1004 | UTEXT_SETNATIVEINDEX(&fText, fromPosition); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1005 | #ifdef RBBI_DEBUG |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1006 | if (gTrace) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1007 | RBBIDebugPuts("Handle Previous pos char state category"); |
| 1008 | } |
| 1009 | #endif |
| 1010 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1011 | // if we're already at the start of the text, return DONE. |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 1012 | if (fData == NULL || UTEXT_GETNATIVEINDEX(&fText)==0) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1013 | return BreakIterator::DONE; |
| 1014 | } |
| 1015 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1016 | // Set the initial state for the state machine |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 1017 | c = UTEXT_PREVIOUS32(&fText); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1018 | state = START_STATE; |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 1019 | row = (RowType *) |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1020 | (stateTable->fTableData + (stateTable->fRowLen * state)); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1021 | |
| 1022 | // loop until we reach the start of the text or transition to state 0 |
| 1023 | // |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 1024 | for (; c != U_SENTINEL; c = UTEXT_PREVIOUS32(&fText)) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1025 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 1026 | // look up the current character's character category, which tells us |
| 1027 | // which column in the state table to look at. |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1028 | // |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 1029 | // Off the dictionary flag bit. For reverse iteration it is not used. |
| 1030 | category = trieFunc(fData->fTrie, c); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1031 | |
| 1032 | #ifdef RBBI_DEBUG |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1033 | if (gTrace) { |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 1034 | RBBIDebugPrintf(" %4d ", (int32_t)utext_getNativeIndex(&fText)); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1035 | 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 Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 1046 | // fNextState is a variable-length array. |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1047 | U_ASSERT(category<fData->fHeader->fCatCount); |
| 1048 | state = row->fNextState[category]; /*Not accessing beyond memory*/ |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 1049 | row = (RowType *) |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1050 | (stateTable->fTableData + (stateTable->fRowLen * state)); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1051 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1052 | if (state == STOP_STATE) { |
| 1053 | // This is the normal exit from the lookup state machine. |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 1054 | // Transition to state zero means we have found a safe point. |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1055 | break; |
| 1056 | } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | // The state machine is done. Check whether it found a match... |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 1060 | result = (int32_t)UTEXT_GETNATIVEINDEX(&fText); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1061 | #ifdef RBBI_DEBUG |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1062 | if (gTrace) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1063 | RBBIDebugPrintf("result = %d\n\n", result); |
| 1064 | } |
| 1065 | #endif |
| 1066 | return result; |
| 1067 | } |
| 1068 | |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 1069 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1070 | //------------------------------------------------------------------------------- |
| 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.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1077 | //------------------------------------------------------------------------------- |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1078 | |
| 1079 | int32_t RuleBasedBreakIterator::getRuleStatus() const { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1080 | |
| 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 Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1084 | int32_t idx = fRuleStatusIndex + fData->fRuleStatusTable[fRuleStatusIndex]; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1085 | int32_t tagVal = fData->fRuleStatusTable[idx]; |
| 1086 | |
| 1087 | return tagVal; |
| 1088 | } |
| 1089 | |
| 1090 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1091 | int32_t RuleBasedBreakIterator::getRuleStatusVec( |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1092 | int32_t *fillInVec, int32_t capacity, UErrorCode &status) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1093 | if (U_FAILURE(status)) { |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1097 | int32_t numVals = fData->fRuleStatusTable[fRuleStatusIndex]; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1098 | 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 Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1105 | fillInVec[i] = fData->fRuleStatusTable[fRuleStatusIndex + i + 1]; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1106 | } |
| 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 | //------------------------------------------------------------------------------- |
| 1119 | const 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 Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 1131 | RuleBasedBreakIterator *RuleBasedBreakIterator::createBufferClone( |
| 1132 | void * /*stackBuffer*/, int32_t &bufferSize, UErrorCode &status) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1133 | 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.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1151 | U_NAMESPACE_END |
| 1152 | |
| 1153 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1154 | static icu::UStack *gLanguageBreakFactories = nullptr; |
| 1155 | static const icu::UnicodeString *gEmptyString = nullptr; |
Frank Tang | 1c67b4e | 2022-05-18 10:13:51 -0700 | [diff] [blame] | 1156 | static icu::UInitOnce gLanguageBreakFactoriesInitOnce {}; |
| 1157 | static icu::UInitOnce gRBBIInitOnce {}; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1158 | |
| 1159 | /** |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1160 | * Release all static memory held by breakiterator. |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1161 | */ |
| 1162 | U_CDECL_BEGIN |
Frank Tang | 13cfcd5 | 2020-03-16 13:49:12 -0700 | [diff] [blame] | 1163 | UBool U_CALLCONV rbbi_cleanup(void) { |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1164 | delete gLanguageBreakFactories; |
| 1165 | gLanguageBreakFactories = nullptr; |
| 1166 | delete gEmptyString; |
| 1167 | gEmptyString = nullptr; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1168 | gLanguageBreakFactoriesInitOnce.reset(); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1169 | gRBBIInitOnce.reset(); |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 1170 | return true; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1171 | } |
| 1172 | U_CDECL_END |
| 1173 | |
| 1174 | U_CDECL_BEGIN |
| 1175 | static void U_CALLCONV _deleteFactory(void *obj) { |
| 1176 | delete (icu::LanguageBreakFactory *) obj; |
| 1177 | } |
| 1178 | U_CDECL_END |
| 1179 | U_NAMESPACE_BEGIN |
| 1180 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1181 | static void U_CALLCONV rbbiInit() { |
| 1182 | gEmptyString = new UnicodeString(); |
| 1183 | ucln_common_registerCleanup(UCLN_COMMON_RBBI, rbbi_cleanup); |
| 1184 | } |
| 1185 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1186 | static 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 Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1200 | ucln_common_registerCleanup(UCLN_COMMON_RBBI, rbbi_cleanup); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | |
| 1204 | static const LanguageBreakEngine* |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 1205 | getLanguageBreakEngineFromFactory(UChar32 c) |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1206 | { |
| 1207 | umtx_initOnce(gLanguageBreakFactoriesInitOnce, &initLanguageFactories); |
| 1208 | if (gLanguageBreakFactories == NULL) { |
| 1209 | return NULL; |
| 1210 | } |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1211 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1212 | int32_t i = gLanguageBreakFactories->size(); |
| 1213 | const LanguageBreakEngine *lbe = NULL; |
| 1214 | while (--i >= 0) { |
| 1215 | LanguageBreakFactory *factory = (LanguageBreakFactory *)(gLanguageBreakFactories->elementAt(i)); |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 1216 | lbe = factory->getEngineFor(c); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1217 | 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 | //------------------------------------------------------------------------------- |
| 1231 | const LanguageBreakEngine * |
| 1232 | RuleBasedBreakIterator::getLanguageBreakEngine(UChar32 c) { |
| 1233 | const LanguageBreakEngine *lbe = NULL; |
| 1234 | UErrorCode status = U_ZERO_ERROR; |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1235 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1236 | 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 Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1244 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1245 | int32_t i = fLanguageBreakEngines->size(); |
| 1246 | while (--i >= 0) { |
| 1247 | lbe = (const LanguageBreakEngine *)(fLanguageBreakEngines->elementAt(i)); |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 1248 | if (lbe->handles(c)) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1249 | return lbe; |
| 1250 | } |
| 1251 | } |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1252 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1253 | // No existing dictionary took the character. See if a factory wants to |
| 1254 | // give us a new LanguageBreakEngine for this character. |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 1255 | lbe = getLanguageBreakEngineFromFactory(c); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1256 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1257 | // 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 Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1264 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1265 | // 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 Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 1271 | return nullptr; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1272 | } |
| 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 Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 1277 | U_ASSERT(!fLanguageBreakEngines->hasDeleter()); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1278 | if (U_FAILURE(status)) { |
| 1279 | delete fUnhandledBreakEngine; |
| 1280 | fUnhandledBreakEngine = 0; |
| 1281 | return NULL; |
| 1282 | } |
| 1283 | } |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1284 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1285 | // Tell the reject engine about the character; at its discretion, it may |
| 1286 | // add more than just the one character. |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 1287 | fUnhandledBreakEngine->handleCharacter(c); |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1288 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1289 | return fUnhandledBreakEngine; |
| 1290 | } |
| 1291 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1292 | void RuleBasedBreakIterator::dumpCache() { |
| 1293 | fBreakCache->dumpCache(); |
| 1294 | } |
| 1295 | |
Jungshik Shin | f61e46d | 2018-05-04 13:00:45 -0700 | [diff] [blame] | 1296 | void RuleBasedBreakIterator::dumpTables() { |
| 1297 | fData->printData(); |
| 1298 | } |
| 1299 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1300 | /** |
| 1301 | * Returns the description used to create this iterator |
| 1302 | */ |
| 1303 | |
| 1304 | const UnicodeString& |
| 1305 | RuleBasedBreakIterator::getRules() const { |
| 1306 | if (fData != NULL) { |
| 1307 | return fData->getRuleSourceString(); |
| 1308 | } else { |
| 1309 | umtx_initOnce(gRBBIInitOnce, &rbbiInit); |
| 1310 | return *gEmptyString; |
| 1311 | } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | U_NAMESPACE_END |
| 1315 | |
| 1316 | #endif /* #if !UCONFIG_NO_BREAK_ITERATION */ |