UBreakIterator* openLineBreakIterator(const AtomicString& locale) { bool localeIsEmpty = locale.isEmpty(); UErrorCode openStatus = U_ZERO_ERROR; UBreakIterator* ubrkIter = ubrk_open(UBRK_LINE, localeIsEmpty ? currentTextBreakLocaleID() : locale.string().utf8().data(), 0, 0, &openStatus); // locale comes from a web page and it can be invalid, leading ICU // to fail, in which case we fall back to the default locale. if (!localeIsEmpty && U_FAILURE(openStatus)) { openStatus = U_ZERO_ERROR; ubrkIter = ubrk_open(UBRK_LINE, currentTextBreakLocaleID(), 0, 0, &openStatus); } if (U_FAILURE(openStatus)) { LOG_ERROR("ubrk_open failed with status %d", openStatus); return nullptr; } return ubrkIter; }
int KWQFindNextWordFromIndex(const QChar *chars, int len, int position, bool forward) { int pos = 0; UErrorCode status = U_ZERO_ERROR; UBreakIterator *boundary = ubrk_open(UBRK_WORD, (const char*)currentTextBreakLocaleID().c_str(), const_cast<UChar *>(reinterpret_cast<const UChar *>(chars)), len, &status); if ( boundary && U_SUCCESS(status) ) { if (forward) { pos = ubrk_following(boundary, position); if (pos == UBRK_DONE) pos = len; } else { pos = ubrk_preceding(boundary, position); if (pos == UBRK_DONE) pos = 0; } ubrk_close(boundary); } return pos; }
void KWQFindSentenceBoundary(const QChar *chars, int len, int position, int *start, int *end) { int startPos = 0; int endPos = 0; UErrorCode status = U_ZERO_ERROR; UBreakIterator *boundary = ubrk_open(UBRK_SENTENCE, (const char*)currentTextBreakLocaleID().c_str(), const_cast<UChar *>(reinterpret_cast<const UChar *>(chars)), len, &status); if ( boundary && U_SUCCESS(status) ) { startPos = ubrk_preceding(boundary, position); if (startPos == UBRK_DONE) { startPos = 0; } endPos = ubrk_following(boundary, startPos); if (endPos == UBRK_DONE) endPos = len; ubrk_close(boundary); } *start = startPos; *end = endPos; }
static TextBreakIterator* setUpIterator(bool& createdIterator, TextBreakIterator*& iterator, UBreakIteratorType type, const UChar* string, int length) { if (!string) return 0; if (!createdIterator) { UErrorCode openStatus = U_ZERO_ERROR; iterator = reinterpret_cast<TextBreakIterator*>(ubrk_open(type, currentTextBreakLocaleID(), 0, 0, &openStatus)); createdIterator = true; ASSERT_WITH_MESSAGE(U_SUCCESS(openStatus), "ICU could not open a break iterator: %s (%d)", u_errorName(openStatus), openStatus); } if (!iterator) return 0; UErrorCode setTextStatus = U_ZERO_ERROR; ubrk_setText(reinterpret_cast<UBreakIterator*>(iterator), string, length, &setTextStatus); if (U_FAILURE(setTextStatus)) return 0; return iterator; }
static UBreakIterator* initializeIterator(UBreakIteratorType type, const char* locale = currentTextBreakLocaleID()) { UErrorCode openStatus = U_ZERO_ERROR; UBreakIterator* iterator = ubrk_open(type, locale, 0, 0, &openStatus); ASSERT_WITH_MESSAGE(U_SUCCESS(openStatus), "ICU could not open a break iterator: %s (%d)", u_errorName(openStatus), openStatus); return iterator; }