| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006 Lars Knoll <lars@trolltech.com> | 2 * Copyright (C) 2006 Lars Knoll <lars@trolltech.com> |
| 3 * Copyright (C) 2007, 2011, 2012 Apple Inc. All rights reserved. | 3 * Copyright (C) 2007, 2011, 2012 Apple Inc. All rights reserved. |
| 4 * | 4 * |
| 5 * This library is free software; you can redistribute it and/or | 5 * This library is free software; you can redistribute it and/or |
| 6 * modify it under the terms of the GNU Library General Public | 6 * modify it under the terms of the GNU Library General Public |
| 7 * License as published by the Free Software Foundation; either | 7 * License as published by the Free Software Foundation; either |
| 8 * version 2 of the License, or (at your option) any later version. | 8 * version 2 of the License, or (at your option) any later version. |
| 9 * | 9 * |
| 10 * This library is distributed in the hope that it will be useful, | 10 * This library is distributed in the hope that it will be useful, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 #include "core/platform/text/LineBreakIteratorPoolICU.h" | 25 #include "core/platform/text/LineBreakIteratorPoolICU.h" |
| 26 #include "wtf/ThreadingPrimitives.h" | 26 #include "wtf/ThreadingPrimitives.h" |
| 27 #include "wtf/text/WTFString.h" | 27 #include "wtf/text/WTFString.h" |
| 28 | 28 |
| 29 using namespace WTF; | 29 using namespace WTF; |
| 30 using namespace std; | 30 using namespace std; |
| 31 | 31 |
| 32 namespace WebCore { | 32 namespace WebCore { |
| 33 | 33 |
| 34 static TextBreakIterator* ensureIterator(bool& createdIterator, TextBreakIterato
r*& iterator, UBreakIteratorType type) | |
| 35 { | |
| 36 if (!createdIterator) { | |
| 37 UErrorCode openStatus = U_ZERO_ERROR; | |
| 38 iterator = reinterpret_cast<TextBreakIterator*>(ubrk_open(type, currentT
extBreakLocaleID(), 0, 0, &openStatus)); | |
| 39 createdIterator = true; | |
| 40 ASSERT_WITH_MESSAGE(U_SUCCESS(openStatus), "ICU could not open a break i
terator: %s (%d)", u_errorName(openStatus), openStatus); | |
| 41 } | |
| 42 return iterator; | |
| 43 } | |
| 44 | |
| 45 enum TextContext { NoContext, PriorContext, PrimaryContext }; | 34 enum TextContext { NoContext, PriorContext, PrimaryContext }; |
| 46 | 35 |
| 47 const int textBufferCapacity = 16; | 36 const int textBufferCapacity = 16; |
| 48 | 37 |
| 49 typedef struct { | 38 typedef struct { |
| 50 UText text; | 39 UText text; |
| 51 UChar buffer[textBufferCapacity]; | 40 UChar buffer[textBufferCapacity]; |
| 52 } UTextWithBuffer; | 41 } UTextWithBuffer; |
| 53 | 42 |
| 54 static inline int64_t textPinIndex(int64_t& index, int64_t limit) | 43 static inline int64_t textPinIndex(int64_t& index, int64_t limit) |
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 if (U_FAILURE(*status)) { | 392 if (U_FAILURE(*status)) { |
| 404 ASSERT(!text); | 393 ASSERT(!text); |
| 405 return 0; | 394 return 0; |
| 406 } | 395 } |
| 407 textInit(text, &textUTF16Funcs, string, length, priorContext, priorContextLe
ngth); | 396 textInit(text, &textUTF16Funcs, string, length, priorContext, priorContextLe
ngth); |
| 408 return text; | 397 return text; |
| 409 } | 398 } |
| 410 | 399 |
| 411 static UText emptyText = UTEXT_INITIALIZER; | 400 static UText emptyText = UTEXT_INITIALIZER; |
| 412 | 401 |
| 413 static TextBreakIterator* setUpIterator(bool& createdIterator, TextBreakIterator
*& iterator, UBreakIteratorType type, const UChar* string, int length) | 402 static TextBreakIterator* wordBreakIterator(const LChar* string, int length) |
| 414 { | 403 { |
| 415 if (!string) | 404 UErrorCode errorCode = U_ZERO_ERROR; |
| 416 return 0; | 405 static TextBreakIterator* breakIter = 0; |
| 417 | 406 if (!breakIter) { |
| 418 iterator = ensureIterator(createdIterator, iterator, type); | 407 breakIter = icu::BreakIterator::createWordInstance(currentTextBreakLocal
eID(), errorCode); |
| 419 if (!iterator) | 408 ASSERT_WITH_MESSAGE(U_SUCCESS(errorCode), "ICU could not open a break it
erator: %s (%d)", u_errorName(errorCode), errorCode); |
| 420 return 0; | 409 if (!breakIter) |
| 421 | 410 return 0; |
| 422 UErrorCode setTextStatus = U_ZERO_ERROR; | 411 } |
| 423 ubrk_setText(reinterpret_cast<UBreakIterator*>(iterator), string, length, &s
etTextStatus); | |
| 424 if (U_FAILURE(setTextStatus)) | |
| 425 return 0; | |
| 426 | |
| 427 return iterator; | |
| 428 } | |
| 429 | |
| 430 static TextBreakIterator* setUpIterator(bool& createdIterator, TextBreakIterator
*& iterator, UBreakIteratorType type, const LChar* string, int length) | |
| 431 { | |
| 432 if (!string) | |
| 433 return 0; | |
| 434 | |
| 435 iterator = ensureIterator(createdIterator, iterator, type); | |
| 436 if (!iterator) | |
| 437 return 0; | |
| 438 | 412 |
| 439 UTextWithBuffer textLocal; | 413 UTextWithBuffer textLocal; |
| 440 textLocal.text = emptyText; | 414 textLocal.text = emptyText; |
| 441 textLocal.text.extraSize = sizeof(textLocal.buffer); | 415 textLocal.text.extraSize = sizeof(textLocal.buffer); |
| 442 textLocal.text.pExtra = textLocal.buffer; | 416 textLocal.text.pExtra = textLocal.buffer; |
| 443 | 417 |
| 444 UErrorCode openStatus = U_ZERO_ERROR; | 418 UErrorCode openStatus = U_ZERO_ERROR; |
| 445 UText* text = textOpenLatin1(&textLocal, string, length, 0, 0, &openStatus); | 419 UText* text = textOpenLatin1(&textLocal, string, length, 0, 0, &openStatus); |
| 446 if (U_FAILURE(openStatus)) { | 420 if (U_FAILURE(openStatus)) { |
| 447 LOG_ERROR("textOpenLatin1 failed with status %d", openStatus); | 421 LOG_ERROR("textOpenLatin1 failed with status %d", openStatus); |
| 448 return 0; | 422 return 0; |
| 449 } | 423 } |
| 450 | 424 |
| 451 UErrorCode setTextStatus = U_ZERO_ERROR; | 425 UErrorCode setTextStatus = U_ZERO_ERROR; |
| 452 ubrk_setUText(reinterpret_cast<UBreakIterator*>(iterator), text, &setTextSta
tus); | 426 breakIter->setText(text, setTextStatus); |
| 453 if (U_FAILURE(setTextStatus)) { | 427 if (U_FAILURE(setTextStatus)) { |
| 454 LOG_ERROR("ubrk_setUText failed with status %d", setTextStatus); | 428 LOG_ERROR("ubrk_setUText failed with status %d", setTextStatus); |
| 455 // FIXME: Do we need to call utext_close(text) here? | 429 delete breakIter; |
| 456 return 0; | |
| 457 } | 430 } |
| 458 | 431 |
| 459 utext_close(text); | 432 utext_close(text); |
| 460 | 433 |
| 461 return iterator; | 434 return breakIter; |
| 462 } | |
| 463 | |
| 464 static TextBreakIterator* wordBreakIterator(const LChar* string, int length) | |
| 465 { | |
| 466 static bool createdWordBreakIterator8 = false; | |
| 467 static TextBreakIterator* staticWordBreakIterator8; | |
| 468 return setUpIterator(createdWordBreakIterator8, | |
| 469 staticWordBreakIterator8, UBRK_WORD, string, length); | |
| 470 } | 435 } |
| 471 | 436 |
| 472 TextBreakIterator* wordBreakIterator(const UChar* string, int length) | 437 TextBreakIterator* wordBreakIterator(const UChar* string, int length) |
| 473 { | 438 { |
| 474 static bool createdWordBreakIterator16 = false; | 439 UErrorCode errorCode = U_ZERO_ERROR; |
| 475 static TextBreakIterator* staticWordBreakIterator16; | 440 static TextBreakIterator* breakIter = 0; |
| 476 return setUpIterator(createdWordBreakIterator16, | 441 if (!breakIter) { |
| 477 staticWordBreakIterator16, UBRK_WORD, string, length); | 442 breakIter = icu::BreakIterator::createWordInstance(currentTextBreakLocal
eID(), errorCode); |
| 443 ASSERT_WITH_MESSAGE(U_SUCCESS(errorCode), "ICU could not open a break it
erator: %s (%d)", u_errorName(errorCode), errorCode); |
| 444 if (!breakIter) |
| 445 return 0; |
| 446 } |
| 447 |
| 448 breakIter->setText(icu::UnicodeString(string, length)); |
| 449 return breakIter; |
| 478 } | 450 } |
| 479 | 451 |
| 480 TextBreakIterator* wordBreakIterator(const String& string, int start, int length
) | 452 TextBreakIterator* wordBreakIterator(const String& string, int start, int length
) |
| 481 { | 453 { |
| 482 if (string.isEmpty()) | 454 if (string.isEmpty()) |
| 483 return 0; | 455 return 0; |
| 484 if (string.is8Bit()) | 456 if (string.is8Bit()) |
| 485 return wordBreakIterator(string.characters8() + start, length); | 457 return wordBreakIterator(string.characters8() + start, length); |
| 486 return wordBreakIterator(string.characters16() + start, length); | 458 return wordBreakIterator(string.characters16() + start, length); |
| 487 } | 459 } |
| 488 | 460 |
| 489 TextBreakIterator* acquireLineBreakIterator(const LChar* string, int length, con
st AtomicString& locale, const UChar* priorContext, unsigned priorContextLength) | 461 TextBreakIterator* acquireLineBreakIterator(const LChar* string, int length, con
st AtomicString& locale, const UChar* priorContext, unsigned priorContextLength) |
| 490 { | 462 { |
| 491 UBreakIterator* iterator = LineBreakIteratorPool::sharedPool().take(locale); | 463 TextBreakIterator* iterator = LineBreakIteratorPool::sharedPool().take(local
e); |
| 492 if (!iterator) | 464 if (!iterator) |
| 493 return 0; | 465 return 0; |
| 494 | 466 |
| 495 UTextWithBuffer textLocal; | 467 UTextWithBuffer textLocal; |
| 496 textLocal.text = emptyText; | 468 textLocal.text = emptyText; |
| 497 textLocal.text.extraSize = sizeof(textLocal.buffer); | 469 textLocal.text.extraSize = sizeof(textLocal.buffer); |
| 498 textLocal.text.pExtra = textLocal.buffer; | 470 textLocal.text.pExtra = textLocal.buffer; |
| 499 | 471 |
| 500 UErrorCode openStatus = U_ZERO_ERROR; | 472 UErrorCode openStatus = U_ZERO_ERROR; |
| 501 UText* text = textOpenLatin1(&textLocal, string, length, priorContext, prior
ContextLength, &openStatus); | 473 UText* text = textOpenLatin1(&textLocal, string, length, priorContext, prior
ContextLength, &openStatus); |
| 502 if (U_FAILURE(openStatus)) { | 474 if (U_FAILURE(openStatus)) { |
| 503 LOG_ERROR("textOpenLatin1 failed with status %d", openStatus); | 475 LOG_ERROR("textOpenLatin1 failed with status %d", openStatus); |
| 504 return 0; | 476 return 0; |
| 505 } | 477 } |
| 506 | 478 |
| 507 UErrorCode setTextStatus = U_ZERO_ERROR; | 479 UErrorCode setTextStatus = U_ZERO_ERROR; |
| 508 ubrk_setUText(iterator, text, &setTextStatus); | 480 iterator->setText(text, setTextStatus); |
| 509 if (U_FAILURE(setTextStatus)) { | 481 if (U_FAILURE(setTextStatus)) { |
| 510 // FIXME: Do we need to call utext_close(text) here? | |
| 511 LOG_ERROR("ubrk_setUText failed with status %d", setTextStatus); | 482 LOG_ERROR("ubrk_setUText failed with status %d", setTextStatus); |
| 512 return 0; | 483 return 0; |
| 513 } | 484 } |
| 514 | 485 |
| 515 utext_close(text); | 486 utext_close(text); |
| 516 | 487 |
| 517 return reinterpret_cast<TextBreakIterator*>(iterator); | 488 return iterator; |
| 518 } | 489 } |
| 519 | 490 |
| 520 TextBreakIterator* acquireLineBreakIterator(const UChar* string, int length, con
st AtomicString& locale, const UChar* priorContext, unsigned priorContextLength) | 491 TextBreakIterator* acquireLineBreakIterator(const UChar* string, int length, con
st AtomicString& locale, const UChar* priorContext, unsigned priorContextLength) |
| 521 { | 492 { |
| 522 UBreakIterator* iterator = LineBreakIteratorPool::sharedPool().take(locale); | 493 TextBreakIterator* iterator = LineBreakIteratorPool::sharedPool().take(local
e); |
| 523 if (!iterator) | 494 if (!iterator) |
| 524 return 0; | 495 return 0; |
| 525 | 496 |
| 526 UText textLocal = UTEXT_INITIALIZER; | 497 UText textLocal = UTEXT_INITIALIZER; |
| 527 | 498 |
| 528 UErrorCode openStatus = U_ZERO_ERROR; | 499 UErrorCode openStatus = U_ZERO_ERROR; |
| 529 UText* text = textOpenUTF16(&textLocal, string, length, priorContext, priorC
ontextLength, &openStatus); | 500 UText* text = textOpenUTF16(&textLocal, string, length, priorContext, priorC
ontextLength, &openStatus); |
| 530 if (U_FAILURE(openStatus)) { | 501 if (U_FAILURE(openStatus)) { |
| 531 LOG_ERROR("textOpenUTF16 failed with status %d", openStatus); | 502 LOG_ERROR("textOpenUTF16 failed with status %d", openStatus); |
| 532 return 0; | 503 return 0; |
| 533 } | 504 } |
| 534 | 505 |
| 535 UErrorCode setTextStatus = U_ZERO_ERROR; | 506 UErrorCode setTextStatus = U_ZERO_ERROR; |
| 536 ubrk_setUText(iterator, text, &setTextStatus); | 507 iterator->setText(text, setTextStatus); |
| 537 if (U_FAILURE(setTextStatus)) { | 508 if (U_FAILURE(setTextStatus)) { |
| 538 // FIXME: Do we need to call utext_close(text) here? | |
| 539 LOG_ERROR("ubrk_setUText failed with status %d", setTextStatus); | 509 LOG_ERROR("ubrk_setUText failed with status %d", setTextStatus); |
| 540 return 0; | 510 return 0; |
| 541 } | 511 } |
| 542 | 512 |
| 543 utext_close(text); | 513 utext_close(text); |
| 544 | 514 |
| 545 return reinterpret_cast<TextBreakIterator*>(iterator); | 515 return iterator; |
| 546 } | 516 } |
| 547 | 517 |
| 548 void releaseLineBreakIterator(TextBreakIterator* iterator) | 518 void releaseLineBreakIterator(TextBreakIterator* iterator) |
| 549 { | 519 { |
| 550 ASSERT_ARG(iterator, iterator); | 520 ASSERT_ARG(iterator, iterator); |
| 551 | 521 |
| 552 LineBreakIteratorPool::sharedPool().put(reinterpret_cast<UBreakIterator*>(it
erator)); | 522 LineBreakIteratorPool::sharedPool().put(iterator); |
| 553 } | 523 } |
| 554 | 524 |
| 555 static TextBreakIterator* nonSharedCharacterBreakIterator; | 525 static TextBreakIterator* nonSharedCharacterBreakIterator; |
| 556 | 526 |
| 557 static inline bool compareAndSwapNonSharedCharacterBreakIterator(TextBreakIterat
or* expected, TextBreakIterator* newValue) | 527 static inline bool compareAndSwapNonSharedCharacterBreakIterator(TextBreakIterat
or* expected, TextBreakIterator* newValue) |
| 558 { | 528 { |
| 559 DEFINE_STATIC_LOCAL(Mutex, nonSharedCharacterBreakIteratorMutex, ()); | 529 DEFINE_STATIC_LOCAL(Mutex, nonSharedCharacterBreakIteratorMutex, ()); |
| 560 MutexLocker locker(nonSharedCharacterBreakIteratorMutex); | 530 MutexLocker locker(nonSharedCharacterBreakIteratorMutex); |
| 561 if (nonSharedCharacterBreakIterator != expected) | 531 if (nonSharedCharacterBreakIterator != expected) |
| 562 return false; | 532 return false; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 593 , m_length(0) | 563 , m_length(0) |
| 594 , m_iterator(0) | 564 , m_iterator(0) |
| 595 { | 565 { |
| 596 createIteratorForBuffer(buffer, length); | 566 createIteratorForBuffer(buffer, length); |
| 597 } | 567 } |
| 598 | 568 |
| 599 void NonSharedCharacterBreakIterator::createIteratorForBuffer(const UChar* buffe
r, unsigned length) | 569 void NonSharedCharacterBreakIterator::createIteratorForBuffer(const UChar* buffe
r, unsigned length) |
| 600 { | 570 { |
| 601 m_iterator = nonSharedCharacterBreakIterator; | 571 m_iterator = nonSharedCharacterBreakIterator; |
| 602 bool createdIterator = m_iterator && compareAndSwapNonSharedCharacterBreakIt
erator(m_iterator, 0); | 572 bool createdIterator = m_iterator && compareAndSwapNonSharedCharacterBreakIt
erator(m_iterator, 0); |
| 603 m_iterator = setUpIterator(createdIterator, m_iterator, UBRK_CHARACTER, buff
er, length); | 573 if (!createdIterator) { |
| 574 UErrorCode errorCode = U_ZERO_ERROR; |
| 575 m_iterator = icu::BreakIterator::createCharacterInstance(currentTextBrea
kLocaleID(), errorCode); |
| 576 ASSERT_WITH_MESSAGE(U_SUCCESS(errorCode), "ICU could not open a break it
erator: %s (%d)", u_errorName(errorCode), errorCode); |
| 577 } |
| 578 |
| 579 m_iterator->setText(icu::UnicodeString(buffer, length)); |
| 604 } | 580 } |
| 605 | 581 |
| 606 NonSharedCharacterBreakIterator::~NonSharedCharacterBreakIterator() | 582 NonSharedCharacterBreakIterator::~NonSharedCharacterBreakIterator() |
| 607 { | 583 { |
| 608 if (m_is8Bit) | 584 if (m_is8Bit) |
| 609 return; | 585 return; |
| 610 if (!compareAndSwapNonSharedCharacterBreakIterator(0, m_iterator)) | 586 if (!compareAndSwapNonSharedCharacterBreakIterator(0, m_iterator)) |
| 611 ubrk_close(reinterpret_cast<UBreakIterator*>(m_iterator)); | 587 delete m_iterator; |
| 612 } | 588 } |
| 613 | 589 |
| 614 int NonSharedCharacterBreakIterator::next() | 590 int NonSharedCharacterBreakIterator::next() |
| 615 { | 591 { |
| 616 if (!m_is8Bit) | 592 if (!m_is8Bit) |
| 617 return textBreakNext(m_iterator); | 593 return m_iterator->next(); |
| 618 | 594 |
| 619 if (m_offset >= m_length) | 595 if (m_offset >= m_length) |
| 620 return TextBreakDone; | 596 return TextBreakDone; |
| 621 | 597 |
| 622 m_offset += clusterLengthStartingAt(m_offset); | 598 m_offset += clusterLengthStartingAt(m_offset); |
| 623 return m_offset; | 599 return m_offset; |
| 624 } | 600 } |
| 625 | 601 |
| 626 int NonSharedCharacterBreakIterator::current() | 602 int NonSharedCharacterBreakIterator::current() |
| 627 { | 603 { |
| 628 if (!m_is8Bit) | 604 if (!m_is8Bit) |
| 629 return textBreakCurrent(m_iterator); | 605 return m_iterator->current(); |
| 630 return m_offset; | 606 return m_offset; |
| 631 } | 607 } |
| 632 | 608 |
| 633 bool NonSharedCharacterBreakIterator::isBreak(int offset) const | 609 bool NonSharedCharacterBreakIterator::isBreak(int offset) const |
| 634 { | 610 { |
| 635 if (!m_is8Bit) | 611 if (!m_is8Bit) |
| 636 return isTextBreak(m_iterator, offset); | 612 return m_iterator->isBoundary(offset); |
| 637 return !isLFAfterCR(offset); | 613 return !isLFAfterCR(offset); |
| 638 } | 614 } |
| 639 | 615 |
| 640 int NonSharedCharacterBreakIterator::preceding(int offset) const | 616 int NonSharedCharacterBreakIterator::preceding(int offset) const |
| 641 { | 617 { |
| 642 if (!m_is8Bit) | 618 if (!m_is8Bit) |
| 643 return textBreakPreceding(m_iterator, offset); | 619 return m_iterator->preceding(offset); |
| 644 if (offset <= 0) | 620 if (offset <= 0) |
| 645 return TextBreakDone; | 621 return TextBreakDone; |
| 646 if (isLFAfterCR(offset)) | 622 if (isLFAfterCR(offset)) |
| 647 return offset - 2; | 623 return offset - 2; |
| 648 return offset - 1; | 624 return offset - 1; |
| 649 } | 625 } |
| 650 | 626 |
| 651 int NonSharedCharacterBreakIterator::following(int offset) const | 627 int NonSharedCharacterBreakIterator::following(int offset) const |
| 652 { | 628 { |
| 653 if (!m_is8Bit) | 629 if (!m_is8Bit) |
| 654 return textBreakFollowing(m_iterator, offset); | 630 return m_iterator->following(offset); |
| 655 if (static_cast<unsigned>(offset) >= m_length) | 631 if (static_cast<unsigned>(offset) >= m_length) |
| 656 return TextBreakDone; | 632 return TextBreakDone; |
| 657 return offset + clusterLengthStartingAt(offset); | 633 return offset + clusterLengthStartingAt(offset); |
| 658 } | 634 } |
| 659 | 635 |
| 660 TextBreakIterator* sentenceBreakIterator(const UChar* string, int length) | 636 TextBreakIterator* sentenceBreakIterator(const UChar* string, int length) |
| 661 { | 637 { |
| 662 static bool createdSentenceBreakIterator = false; | 638 UErrorCode openStatus = U_ZERO_ERROR; |
| 663 static TextBreakIterator* staticSentenceBreakIterator; | 639 static TextBreakIterator* iterator = 0; |
| 664 return setUpIterator(createdSentenceBreakIterator, | 640 if (!iterator) { |
| 665 staticSentenceBreakIterator, UBRK_SENTENCE, string, length); | 641 iterator = icu::BreakIterator::createSentenceInstance(currentTextBreakL
ocaleID(), openStatus); |
| 666 } | 642 ASSERT_WITH_MESSAGE(U_SUCCESS(openStatus), "ICU could not open a break i
terator: %s (%d)", u_errorName(openStatus), openStatus); |
| 643 if (!iterator) |
| 644 return 0; |
| 645 } |
| 667 | 646 |
| 668 int textBreakFirst(TextBreakIterator* iterator) | 647 iterator->setText(icu::UnicodeString(string, length)); |
| 669 { | 648 return iterator; |
| 670 return ubrk_first(reinterpret_cast<UBreakIterator*>(iterator)); | |
| 671 } | |
| 672 | |
| 673 int textBreakLast(TextBreakIterator* iterator) | |
| 674 { | |
| 675 return ubrk_last(reinterpret_cast<UBreakIterator*>(iterator)); | |
| 676 } | |
| 677 | |
| 678 int textBreakNext(TextBreakIterator* iterator) | |
| 679 { | |
| 680 return ubrk_next(reinterpret_cast<UBreakIterator*>(iterator)); | |
| 681 } | |
| 682 | |
| 683 int textBreakPrevious(TextBreakIterator* iterator) | |
| 684 { | |
| 685 return ubrk_previous(reinterpret_cast<UBreakIterator*>(iterator)); | |
| 686 } | |
| 687 | |
| 688 int textBreakPreceding(TextBreakIterator* iterator, int pos) | |
| 689 { | |
| 690 return ubrk_preceding(reinterpret_cast<UBreakIterator*>(iterator), pos); | |
| 691 } | |
| 692 | |
| 693 int textBreakFollowing(TextBreakIterator* iterator, int pos) | |
| 694 { | |
| 695 return ubrk_following(reinterpret_cast<UBreakIterator*>(iterator), pos); | |
| 696 } | |
| 697 | |
| 698 int textBreakCurrent(TextBreakIterator* iterator) | |
| 699 { | |
| 700 return ubrk_current(reinterpret_cast<UBreakIterator*>(iterator)); | |
| 701 } | |
| 702 | |
| 703 bool isTextBreak(TextBreakIterator* iterator, int position) | |
| 704 { | |
| 705 return ubrk_isBoundary(reinterpret_cast<UBreakIterator*>(iterator), position
); | |
| 706 } | 649 } |
| 707 | 650 |
| 708 bool isWordTextBreak(TextBreakIterator* iterator) | 651 bool isWordTextBreak(TextBreakIterator* iterator) |
| 709 { | 652 { |
| 710 int ruleStatus = ubrk_getRuleStatus(reinterpret_cast<UBreakIterator*>(iterat
or)); | 653 int ruleStatus = iterator->getRuleStatus(); |
| 711 return ruleStatus != UBRK_WORD_NONE; | 654 return ruleStatus != UBRK_WORD_NONE; |
| 712 } | 655 } |
| 713 | 656 |
| 714 static TextBreakIterator* setUpIteratorWithRules(bool& createdIterator, TextBrea
kIterator*& iterator, | 657 static TextBreakIterator* setUpIteratorWithRules(const char* breakRules, const U
Char* string, int length) |
| 715 const char* breakRules, const UChar* string, int length) | |
| 716 { | 658 { |
| 717 if (!string) | 659 if (!string) |
| 718 return 0; | 660 return 0; |
| 719 | 661 |
| 720 if (!createdIterator) { | 662 static TextBreakIterator* iterator = 0; |
| 721 UParseError parseStatus; | 663 UParseError parseStatus; |
| 722 UErrorCode openStatus = U_ZERO_ERROR; | 664 UErrorCode openStatus = U_ZERO_ERROR; |
| 723 Vector<UChar> rules; | 665 Vector<UChar> rules; |
| 724 String(breakRules).appendTo(rules); | 666 String(breakRules).appendTo(rules); |
| 725 iterator = reinterpret_cast<TextBreakIterator*>(ubrk_openRules(rules.dat
a(), rules.size(), 0, 0, &parseStatus, &openStatus)); | 667 if (!iterator) { |
| 726 createdIterator = true; | 668 iterator = new icu::RuleBasedBreakIterator(icu::UnicodeString(rules.data
(), rules.size()), parseStatus, openStatus); |
| 727 ASSERT_WITH_MESSAGE(U_SUCCESS(openStatus), "ICU could not open a break i
terator: %s (%d)", u_errorName(openStatus), openStatus); | 669 ASSERT_WITH_MESSAGE(U_SUCCESS(openStatus), "ICU could not open a break i
terator: %s (%d)", u_errorName(openStatus), openStatus); |
| 670 if (!iterator) |
| 671 return 0; |
| 728 } | 672 } |
| 729 if (!iterator) | |
| 730 return 0; | |
| 731 | 673 |
| 732 UErrorCode setTextStatus = U_ZERO_ERROR; | 674 iterator->setText(icu::UnicodeString(string, length)); |
| 733 ubrk_setText(reinterpret_cast<UBreakIterator*>(iterator), string, length, &s
etTextStatus); | |
| 734 if (U_FAILURE(setTextStatus)) | |
| 735 return 0; | |
| 736 | |
| 737 return iterator; | 675 return iterator; |
| 738 } | 676 } |
| 739 | 677 |
| 740 TextBreakIterator* cursorMovementIterator(const UChar* string, int length) | 678 TextBreakIterator* cursorMovementIterator(const UChar* string, int length) |
| 741 { | 679 { |
| 742 // This rule set is based on character-break iterator rules of ICU 4.0 | 680 // This rule set is based on character-break iterator rules of ICU 4.0 |
| 743 // <http://source.icu-project.org/repos/icu/icu/tags/release-4-0/source/data
/brkitr/char.txt>. | 681 // <http://source.icu-project.org/repos/icu/icu/tags/release-4-0/source/data
/brkitr/char.txt>. |
| 744 // The major differences from the original ones are listed below: | 682 // The major differences from the original ones are listed below: |
| 745 // * Replaced '[\p{Grapheme_Cluster_Break = SpacingMark}]' with '[\p{General
_Category = Spacing Mark} - $Extend]' for ICU 3.8 or earlier; | 683 // * Replaced '[\p{Grapheme_Cluster_Break = SpacingMark}]' with '[\p{General
_Category = Spacing Mark} - $Extend]' for ICU 3.8 or earlier; |
| 746 // * Removed rules that prevent a cursor from moving after prepend character
s (Bug 24342); | 684 // * Removed rules that prevent a cursor from moving after prepend character
s (Bug 24342); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 814 "$Hin1 $HinV $Hin0;" // Devanagari Virama (backward) | 752 "$Hin1 $HinV $Hin0;" // Devanagari Virama (backward) |
| 815 "$Ben1 $BenV $Ben0;" // Bengali Virama (backward) | 753 "$Ben1 $BenV $Ben0;" // Bengali Virama (backward) |
| 816 "$Pan1 $PanV $Pan0;" // Gurmukhi Virama (backward) | 754 "$Pan1 $PanV $Pan0;" // Gurmukhi Virama (backward) |
| 817 "$Guj1 $GujV $Guj0;" // Gujarati Virama (backward) | 755 "$Guj1 $GujV $Guj0;" // Gujarati Virama (backward) |
| 818 "$Ori1 $OriV $Ori0;" // Gujarati Virama (backward) | 756 "$Ori1 $OriV $Ori0;" // Gujarati Virama (backward) |
| 819 "$Tel1 $TelV $Tel0;" // Telugu Virama (backward) | 757 "$Tel1 $TelV $Tel0;" // Telugu Virama (backward) |
| 820 "$Kan1 $KanV $Kan0;" // Kannada Virama (backward) | 758 "$Kan1 $KanV $Kan0;" // Kannada Virama (backward) |
| 821 "$Mal1 $MalV $Mal0;" // Malayalam Virama (backward) | 759 "$Mal1 $MalV $Mal0;" // Malayalam Virama (backward) |
| 822 "!!safe_reverse;" | 760 "!!safe_reverse;" |
| 823 "!!safe_forward;"; | 761 "!!safe_forward;"; |
| 824 static bool createdCursorMovementIterator = false; | 762 |
| 825 static TextBreakIterator* staticCursorMovementIterator; | 763 return setUpIteratorWithRules(kRules, string, length); |
| 826 return setUpIteratorWithRules(createdCursorMovementIterator, staticCursorMov
ementIterator, kRules, string, length); | |
| 827 } | 764 } |
| 828 | 765 |
| 829 } | 766 } |
| OLD | NEW |