intl/icu/source/i18n/stsearch.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 **********************************************************************
michael@0 3 * Copyright (C) 2001-2008 IBM and others. All rights reserved.
michael@0 4 **********************************************************************
michael@0 5 * Date Name Description
michael@0 6 * 03/22/2000 helena Creation.
michael@0 7 **********************************************************************
michael@0 8 */
michael@0 9
michael@0 10 #include "unicode/utypes.h"
michael@0 11
michael@0 12 #if !UCONFIG_NO_COLLATION && !UCONFIG_NO_BREAK_ITERATION
michael@0 13
michael@0 14 #include "unicode/stsearch.h"
michael@0 15 #include "usrchimp.h"
michael@0 16 #include "cmemory.h"
michael@0 17
michael@0 18 U_NAMESPACE_BEGIN
michael@0 19
michael@0 20 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(StringSearch)
michael@0 21
michael@0 22 // public constructors and destructors -----------------------------------
michael@0 23
michael@0 24 StringSearch::StringSearch(const UnicodeString &pattern,
michael@0 25 const UnicodeString &text,
michael@0 26 const Locale &locale,
michael@0 27 BreakIterator *breakiter,
michael@0 28 UErrorCode &status) :
michael@0 29 SearchIterator(text, breakiter),
michael@0 30 m_collator_(),
michael@0 31 m_pattern_(pattern)
michael@0 32 {
michael@0 33 if (U_FAILURE(status)) {
michael@0 34 m_strsrch_ = NULL;
michael@0 35 return;
michael@0 36 }
michael@0 37
michael@0 38 m_strsrch_ = usearch_open(m_pattern_.getBuffer(), m_pattern_.length(),
michael@0 39 m_text_.getBuffer(), m_text_.length(),
michael@0 40 locale.getName(), (UBreakIterator *)breakiter,
michael@0 41 &status);
michael@0 42 uprv_free(m_search_);
michael@0 43 m_search_ = NULL;
michael@0 44
michael@0 45 // !!! dlf m_collator_ is an odd beast. basically it is an aliasing
michael@0 46 // wrapper around the internal collator and rules, which (here) are
michael@0 47 // owned by this stringsearch object. this means 1) it's destructor
michael@0 48 // _should not_ delete the ucollator or rules, and 2) changes made
michael@0 49 // to the exposed collator (setStrength etc) _should_ modify the
michael@0 50 // ucollator. thus the collator is not a copy-on-write alias, and it
michael@0 51 // needs to distinguish itself not merely from 'stand alone' colators
michael@0 52 // but also from copy-on-write ones. it needs additional state, which
michael@0 53 // setUCollator should set.
michael@0 54
michael@0 55 if (U_SUCCESS(status)) {
michael@0 56 // Alias the collator
michael@0 57 m_collator_.setUCollator((UCollator *)m_strsrch_->collator);
michael@0 58 // m_search_ has been created by the base SearchIterator class
michael@0 59 m_search_ = m_strsrch_->search;
michael@0 60 }
michael@0 61 }
michael@0 62
michael@0 63 StringSearch::StringSearch(const UnicodeString &pattern,
michael@0 64 const UnicodeString &text,
michael@0 65 RuleBasedCollator *coll,
michael@0 66 BreakIterator *breakiter,
michael@0 67 UErrorCode &status) :
michael@0 68 SearchIterator(text, breakiter),
michael@0 69 m_collator_(),
michael@0 70 m_pattern_(pattern)
michael@0 71 {
michael@0 72 if (U_FAILURE(status)) {
michael@0 73 m_strsrch_ = NULL;
michael@0 74 return;
michael@0 75 }
michael@0 76 if (coll == NULL) {
michael@0 77 status = U_ILLEGAL_ARGUMENT_ERROR;
michael@0 78 m_strsrch_ = NULL;
michael@0 79 return;
michael@0 80 }
michael@0 81 m_strsrch_ = usearch_openFromCollator(m_pattern_.getBuffer(),
michael@0 82 m_pattern_.length(),
michael@0 83 m_text_.getBuffer(),
michael@0 84 m_text_.length(), coll->ucollator,
michael@0 85 (UBreakIterator *)breakiter,
michael@0 86 &status);
michael@0 87 uprv_free(m_search_);
michael@0 88 m_search_ = NULL;
michael@0 89
michael@0 90 if (U_SUCCESS(status)) {
michael@0 91 // Alias the collator
michael@0 92 m_collator_.setUCollator((UCollator *)m_strsrch_->collator);
michael@0 93 // m_search_ has been created by the base SearchIterator class
michael@0 94 m_search_ = m_strsrch_->search;
michael@0 95 }
michael@0 96 }
michael@0 97
michael@0 98 StringSearch::StringSearch(const UnicodeString &pattern,
michael@0 99 CharacterIterator &text,
michael@0 100 const Locale &locale,
michael@0 101 BreakIterator *breakiter,
michael@0 102 UErrorCode &status) :
michael@0 103 SearchIterator(text, breakiter),
michael@0 104 m_collator_(),
michael@0 105 m_pattern_(pattern)
michael@0 106 {
michael@0 107 if (U_FAILURE(status)) {
michael@0 108 m_strsrch_ = NULL;
michael@0 109 return;
michael@0 110 }
michael@0 111 m_strsrch_ = usearch_open(m_pattern_.getBuffer(), m_pattern_.length(),
michael@0 112 m_text_.getBuffer(), m_text_.length(),
michael@0 113 locale.getName(), (UBreakIterator *)breakiter,
michael@0 114 &status);
michael@0 115 uprv_free(m_search_);
michael@0 116 m_search_ = NULL;
michael@0 117
michael@0 118 if (U_SUCCESS(status)) {
michael@0 119 // Alias the collator
michael@0 120 m_collator_.setUCollator((UCollator *)m_strsrch_->collator);
michael@0 121 // m_search_ has been created by the base SearchIterator class
michael@0 122 m_search_ = m_strsrch_->search;
michael@0 123 }
michael@0 124 }
michael@0 125
michael@0 126 StringSearch::StringSearch(const UnicodeString &pattern,
michael@0 127 CharacterIterator &text,
michael@0 128 RuleBasedCollator *coll,
michael@0 129 BreakIterator *breakiter,
michael@0 130 UErrorCode &status) :
michael@0 131 SearchIterator(text, breakiter),
michael@0 132 m_collator_(),
michael@0 133 m_pattern_(pattern)
michael@0 134 {
michael@0 135 if (U_FAILURE(status)) {
michael@0 136 m_strsrch_ = NULL;
michael@0 137 return;
michael@0 138 }
michael@0 139 if (coll == NULL) {
michael@0 140 status = U_ILLEGAL_ARGUMENT_ERROR;
michael@0 141 m_strsrch_ = NULL;
michael@0 142 return;
michael@0 143 }
michael@0 144 m_strsrch_ = usearch_openFromCollator(m_pattern_.getBuffer(),
michael@0 145 m_pattern_.length(),
michael@0 146 m_text_.getBuffer(),
michael@0 147 m_text_.length(), coll->ucollator,
michael@0 148 (UBreakIterator *)breakiter,
michael@0 149 &status);
michael@0 150 uprv_free(m_search_);
michael@0 151 m_search_ = NULL;
michael@0 152
michael@0 153 if (U_SUCCESS(status)) {
michael@0 154 // Alias the collator
michael@0 155 m_collator_.setUCollator((UCollator *)m_strsrch_->collator);
michael@0 156 // m_search_ has been created by the base SearchIterator class
michael@0 157 m_search_ = m_strsrch_->search;
michael@0 158 }
michael@0 159 }
michael@0 160
michael@0 161 StringSearch::StringSearch(const StringSearch &that) :
michael@0 162 SearchIterator(that.m_text_, that.m_breakiterator_),
michael@0 163 m_collator_(),
michael@0 164 m_pattern_(that.m_pattern_)
michael@0 165 {
michael@0 166 UErrorCode status = U_ZERO_ERROR;
michael@0 167
michael@0 168 // Free m_search_ from the superclass
michael@0 169 uprv_free(m_search_);
michael@0 170 m_search_ = NULL;
michael@0 171
michael@0 172 if (that.m_strsrch_ == NULL) {
michael@0 173 // This was not a good copy
michael@0 174 m_strsrch_ = NULL;
michael@0 175 }
michael@0 176 else {
michael@0 177 // Make a deep copy
michael@0 178 m_strsrch_ = usearch_openFromCollator(m_pattern_.getBuffer(),
michael@0 179 m_pattern_.length(),
michael@0 180 m_text_.getBuffer(),
michael@0 181 m_text_.length(),
michael@0 182 that.m_strsrch_->collator,
michael@0 183 (UBreakIterator *)that.m_breakiterator_,
michael@0 184 &status);
michael@0 185 if (U_SUCCESS(status)) {
michael@0 186 // Alias the collator
michael@0 187 m_collator_.setUCollator((UCollator *)m_strsrch_->collator);
michael@0 188 // m_search_ has been created by the base SearchIterator class
michael@0 189 m_search_ = m_strsrch_->search;
michael@0 190 }
michael@0 191 }
michael@0 192 }
michael@0 193
michael@0 194 StringSearch::~StringSearch()
michael@0 195 {
michael@0 196 if (m_strsrch_ != NULL) {
michael@0 197 usearch_close(m_strsrch_);
michael@0 198 m_search_ = NULL;
michael@0 199 }
michael@0 200 }
michael@0 201
michael@0 202 StringSearch *
michael@0 203 StringSearch::clone() const {
michael@0 204 return new StringSearch(*this);
michael@0 205 }
michael@0 206
michael@0 207 // operator overloading ---------------------------------------------
michael@0 208 StringSearch & StringSearch::operator=(const StringSearch &that)
michael@0 209 {
michael@0 210 if ((*this) != that) {
michael@0 211 UErrorCode status = U_ZERO_ERROR;
michael@0 212 m_text_ = that.m_text_;
michael@0 213 m_breakiterator_ = that.m_breakiterator_;
michael@0 214 m_pattern_ = that.m_pattern_;
michael@0 215 // all m_search_ in the parent class is linked up with m_strsrch_
michael@0 216 usearch_close(m_strsrch_);
michael@0 217 m_strsrch_ = usearch_openFromCollator(m_pattern_.getBuffer(),
michael@0 218 m_pattern_.length(),
michael@0 219 m_text_.getBuffer(),
michael@0 220 m_text_.length(),
michael@0 221 that.m_strsrch_->collator,
michael@0 222 NULL, &status);
michael@0 223 // Check null pointer
michael@0 224 if (m_strsrch_ != NULL) {
michael@0 225 // Alias the collator
michael@0 226 m_collator_.setUCollator((UCollator *)m_strsrch_->collator);
michael@0 227 m_search_ = m_strsrch_->search;
michael@0 228 }
michael@0 229 }
michael@0 230 return *this;
michael@0 231 }
michael@0 232
michael@0 233 UBool StringSearch::operator==(const SearchIterator &that) const
michael@0 234 {
michael@0 235 if (this == &that) {
michael@0 236 return TRUE;
michael@0 237 }
michael@0 238 if (SearchIterator::operator ==(that)) {
michael@0 239 StringSearch &thatsrch = (StringSearch &)that;
michael@0 240 return (this->m_pattern_ == thatsrch.m_pattern_ &&
michael@0 241 this->m_strsrch_->collator == thatsrch.m_strsrch_->collator);
michael@0 242 }
michael@0 243 return FALSE;
michael@0 244 }
michael@0 245
michael@0 246 // public get and set methods ----------------------------------------
michael@0 247
michael@0 248 void StringSearch::setOffset(int32_t position, UErrorCode &status)
michael@0 249 {
michael@0 250 // status checked in usearch_setOffset
michael@0 251 usearch_setOffset(m_strsrch_, position, &status);
michael@0 252 }
michael@0 253
michael@0 254 int32_t StringSearch::getOffset(void) const
michael@0 255 {
michael@0 256 return usearch_getOffset(m_strsrch_);
michael@0 257 }
michael@0 258
michael@0 259 void StringSearch::setText(const UnicodeString &text, UErrorCode &status)
michael@0 260 {
michael@0 261 if (U_SUCCESS(status)) {
michael@0 262 m_text_ = text;
michael@0 263 usearch_setText(m_strsrch_, text.getBuffer(), text.length(), &status);
michael@0 264 }
michael@0 265 }
michael@0 266
michael@0 267 void StringSearch::setText(CharacterIterator &text, UErrorCode &status)
michael@0 268 {
michael@0 269 if (U_SUCCESS(status)) {
michael@0 270 text.getText(m_text_);
michael@0 271 usearch_setText(m_strsrch_, m_text_.getBuffer(), m_text_.length(), &status);
michael@0 272 }
michael@0 273 }
michael@0 274
michael@0 275 RuleBasedCollator * StringSearch::getCollator() const
michael@0 276 {
michael@0 277 return (RuleBasedCollator *)&m_collator_;
michael@0 278 }
michael@0 279
michael@0 280 void StringSearch::setCollator(RuleBasedCollator *coll, UErrorCode &status)
michael@0 281 {
michael@0 282 if (U_SUCCESS(status)) {
michael@0 283 usearch_setCollator(m_strsrch_, coll->getUCollator(), &status);
michael@0 284 // Alias the collator
michael@0 285 m_collator_.setUCollator((UCollator *)m_strsrch_->collator);
michael@0 286 }
michael@0 287 }
michael@0 288
michael@0 289 void StringSearch::setPattern(const UnicodeString &pattern,
michael@0 290 UErrorCode &status)
michael@0 291 {
michael@0 292 if (U_SUCCESS(status)) {
michael@0 293 m_pattern_ = pattern;
michael@0 294 usearch_setPattern(m_strsrch_, m_pattern_.getBuffer(), m_pattern_.length(),
michael@0 295 &status);
michael@0 296 }
michael@0 297 }
michael@0 298
michael@0 299 const UnicodeString & StringSearch::getPattern() const
michael@0 300 {
michael@0 301 return m_pattern_;
michael@0 302 }
michael@0 303
michael@0 304 // public methods ----------------------------------------------------
michael@0 305
michael@0 306 void StringSearch::reset()
michael@0 307 {
michael@0 308 usearch_reset(m_strsrch_);
michael@0 309 }
michael@0 310
michael@0 311 SearchIterator * StringSearch::safeClone(void) const
michael@0 312 {
michael@0 313 UErrorCode status = U_ZERO_ERROR;
michael@0 314 StringSearch *result = new StringSearch(m_pattern_, m_text_,
michael@0 315 (RuleBasedCollator *)&m_collator_,
michael@0 316 m_breakiterator_,
michael@0 317 status);
michael@0 318 /* test for NULL */
michael@0 319 if (result == 0) {
michael@0 320 status = U_MEMORY_ALLOCATION_ERROR;
michael@0 321 return 0;
michael@0 322 }
michael@0 323 result->setOffset(getOffset(), status);
michael@0 324 result->setMatchStart(m_strsrch_->search->matchedIndex);
michael@0 325 result->setMatchLength(m_strsrch_->search->matchedLength);
michael@0 326 if (U_FAILURE(status)) {
michael@0 327 return NULL;
michael@0 328 }
michael@0 329 return result;
michael@0 330 }
michael@0 331
michael@0 332 // protected method -------------------------------------------------
michael@0 333
michael@0 334 int32_t StringSearch::handleNext(int32_t position, UErrorCode &status)
michael@0 335 {
michael@0 336 // values passed here are already in the pre-shift position
michael@0 337 if (U_SUCCESS(status)) {
michael@0 338 if (m_strsrch_->pattern.CELength == 0) {
michael@0 339 m_search_->matchedIndex =
michael@0 340 m_search_->matchedIndex == USEARCH_DONE ?
michael@0 341 getOffset() : m_search_->matchedIndex + 1;
michael@0 342 m_search_->matchedLength = 0;
michael@0 343 ucol_setOffset(m_strsrch_->textIter, m_search_->matchedIndex,
michael@0 344 &status);
michael@0 345 if (m_search_->matchedIndex == m_search_->textLength) {
michael@0 346 m_search_->matchedIndex = USEARCH_DONE;
michael@0 347 }
michael@0 348 }
michael@0 349 else {
michael@0 350 // looking at usearch.cpp, this part is shifted out to
michael@0 351 // StringSearch instead of SearchIterator because m_strsrch_ is
michael@0 352 // not accessible in SearchIterator
michael@0 353 #if 0
michael@0 354 if (position + m_strsrch_->pattern.defaultShiftSize
michael@0 355 > m_search_->textLength) {
michael@0 356 setMatchNotFound();
michael@0 357 return USEARCH_DONE;
michael@0 358 }
michael@0 359 #endif
michael@0 360 if (m_search_->matchedLength <= 0) {
michael@0 361 // the flipping direction issue has already been handled
michael@0 362 // in next()
michael@0 363 // for boundary check purposes. this will ensure that the
michael@0 364 // next match will not preceed the current offset
michael@0 365 // note search->matchedIndex will always be set to something
michael@0 366 // in the code
michael@0 367 m_search_->matchedIndex = position - 1;
michael@0 368 }
michael@0 369
michael@0 370 ucol_setOffset(m_strsrch_->textIter, position, &status);
michael@0 371
michael@0 372 #if 0
michael@0 373 for (;;) {
michael@0 374 if (m_search_->isCanonicalMatch) {
michael@0 375 // can't use exact here since extra accents are allowed.
michael@0 376 usearch_handleNextCanonical(m_strsrch_, &status);
michael@0 377 }
michael@0 378 else {
michael@0 379 usearch_handleNextExact(m_strsrch_, &status);
michael@0 380 }
michael@0 381 if (U_FAILURE(status)) {
michael@0 382 return USEARCH_DONE;
michael@0 383 }
michael@0 384 if (m_breakiterator_ == NULL
michael@0 385 #if !UCONFIG_NO_BREAK_ITERATION
michael@0 386 ||
michael@0 387 m_search_->matchedIndex == USEARCH_DONE ||
michael@0 388 (m_breakiterator_->isBoundary(m_search_->matchedIndex) &&
michael@0 389 m_breakiterator_->isBoundary(m_search_->matchedIndex +
michael@0 390 m_search_->matchedLength))
michael@0 391 #endif
michael@0 392 ) {
michael@0 393 if (m_search_->matchedIndex == USEARCH_DONE) {
michael@0 394 ucol_setOffset(m_strsrch_->textIter,
michael@0 395 m_search_->textLength, &status);
michael@0 396 }
michael@0 397 else {
michael@0 398 ucol_setOffset(m_strsrch_->textIter,
michael@0 399 m_search_->matchedIndex, &status);
michael@0 400 }
michael@0 401 return m_search_->matchedIndex;
michael@0 402 }
michael@0 403 }
michael@0 404 #else
michael@0 405 // if m_strsrch_->breakIter is always the same as m_breakiterator_
michael@0 406 // then we don't need to check the match boundaries here because
michael@0 407 // usearch_handleNextXXX will already have done it.
michael@0 408 if (m_search_->isCanonicalMatch) {
michael@0 409 // *could* actually use exact here 'cause no extra accents allowed...
michael@0 410 usearch_handleNextCanonical(m_strsrch_, &status);
michael@0 411 } else {
michael@0 412 usearch_handleNextExact(m_strsrch_, &status);
michael@0 413 }
michael@0 414
michael@0 415 if (U_FAILURE(status)) {
michael@0 416 return USEARCH_DONE;
michael@0 417 }
michael@0 418
michael@0 419 if (m_search_->matchedIndex == USEARCH_DONE) {
michael@0 420 ucol_setOffset(m_strsrch_->textIter, m_search_->textLength, &status);
michael@0 421 } else {
michael@0 422 ucol_setOffset(m_strsrch_->textIter, m_search_->matchedIndex, &status);
michael@0 423 }
michael@0 424
michael@0 425 return m_search_->matchedIndex;
michael@0 426 #endif
michael@0 427 }
michael@0 428 }
michael@0 429 return USEARCH_DONE;
michael@0 430 }
michael@0 431
michael@0 432 int32_t StringSearch::handlePrev(int32_t position, UErrorCode &status)
michael@0 433 {
michael@0 434 // values passed here are already in the pre-shift position
michael@0 435 if (U_SUCCESS(status)) {
michael@0 436 if (m_strsrch_->pattern.CELength == 0) {
michael@0 437 m_search_->matchedIndex =
michael@0 438 (m_search_->matchedIndex == USEARCH_DONE ? getOffset() :
michael@0 439 m_search_->matchedIndex);
michael@0 440 if (m_search_->matchedIndex == 0) {
michael@0 441 setMatchNotFound();
michael@0 442 }
michael@0 443 else {
michael@0 444 m_search_->matchedIndex --;
michael@0 445 ucol_setOffset(m_strsrch_->textIter, m_search_->matchedIndex,
michael@0 446 &status);
michael@0 447 m_search_->matchedLength = 0;
michael@0 448 }
michael@0 449 }
michael@0 450 else {
michael@0 451 // looking at usearch.cpp, this part is shifted out to
michael@0 452 // StringSearch instead of SearchIterator because m_strsrch_ is
michael@0 453 // not accessible in SearchIterator
michael@0 454 #if 0
michael@0 455 if (!m_search_->isOverlap &&
michael@0 456 position - m_strsrch_->pattern.defaultShiftSize < 0) {
michael@0 457 setMatchNotFound();
michael@0 458 return USEARCH_DONE;
michael@0 459 }
michael@0 460
michael@0 461 for (;;) {
michael@0 462 if (m_search_->isCanonicalMatch) {
michael@0 463 // can't use exact here since extra accents are allowed.
michael@0 464 usearch_handlePreviousCanonical(m_strsrch_, &status);
michael@0 465 }
michael@0 466 else {
michael@0 467 usearch_handlePreviousExact(m_strsrch_, &status);
michael@0 468 }
michael@0 469 if (U_FAILURE(status)) {
michael@0 470 return USEARCH_DONE;
michael@0 471 }
michael@0 472 if (m_breakiterator_ == NULL
michael@0 473 #if !UCONFIG_NO_BREAK_ITERATION
michael@0 474 ||
michael@0 475 m_search_->matchedIndex == USEARCH_DONE ||
michael@0 476 (m_breakiterator_->isBoundary(m_search_->matchedIndex) &&
michael@0 477 m_breakiterator_->isBoundary(m_search_->matchedIndex +
michael@0 478 m_search_->matchedLength))
michael@0 479 #endif
michael@0 480 ) {
michael@0 481 return m_search_->matchedIndex;
michael@0 482 }
michael@0 483 }
michael@0 484 #else
michael@0 485 ucol_setOffset(m_strsrch_->textIter, position, &status);
michael@0 486
michael@0 487 if (m_search_->isCanonicalMatch) {
michael@0 488 // *could* use exact match here since extra accents *not* allowed!
michael@0 489 usearch_handlePreviousCanonical(m_strsrch_, &status);
michael@0 490 } else {
michael@0 491 usearch_handlePreviousExact(m_strsrch_, &status);
michael@0 492 }
michael@0 493
michael@0 494 if (U_FAILURE(status)) {
michael@0 495 return USEARCH_DONE;
michael@0 496 }
michael@0 497
michael@0 498 return m_search_->matchedIndex;
michael@0 499 #endif
michael@0 500 }
michael@0 501
michael@0 502 return m_search_->matchedIndex;
michael@0 503 }
michael@0 504 return USEARCH_DONE;
michael@0 505 }
michael@0 506
michael@0 507 U_NAMESPACE_END
michael@0 508
michael@0 509 #endif /* #if !UCONFIG_NO_COLLATION */

mercurial