TextBreakIterator* acquireLineBreakIterator(const LChar* string, int length, const AtomicString& locale, const UChar* priorContext, unsigned priorContextLength) { TextBreakIterator* iterator = LineBreakIteratorPool::sharedPool().take(locale); if (!iterator) return 0; UTextWithBuffer textLocal; textLocal.text = emptyText; textLocal.text.extraSize = sizeof(textLocal.buffer); textLocal.text.pExtra = textLocal.buffer; UErrorCode openStatus = U_ZERO_ERROR; UText* text = textOpenLatin1(&textLocal, string, length, priorContext, priorContextLength, &openStatus); if (U_FAILURE(openStatus)) { WTF_LOG_ERROR("textOpenLatin1 failed with status %d", openStatus); return 0; } UErrorCode setTextStatus = U_ZERO_ERROR; iterator->setText(text, setTextStatus); if (U_FAILURE(setTextStatus)) { WTF_LOG_ERROR("ubrk_setUText failed with status %d", setTextStatus); return 0; } utext_close(text); return iterator; }
TextBreakIterator* acquireLineBreakIterator(const UChar* string, int length, const AtomicString& locale, const UChar* priorContext, unsigned priorContextLength) { TextBreakIterator* iterator = LineBreakIteratorPool::sharedPool().take(locale); if (!iterator) return 0; UText textLocal = UTEXT_INITIALIZER; UErrorCode openStatus = U_ZERO_ERROR; UText* text = textOpenUTF16(&textLocal, string, length, priorContext, priorContextLength, &openStatus); if (U_FAILURE(openStatus)) { WTF_LOG_ERROR("textOpenUTF16 failed with status %d", openStatus); return 0; } UErrorCode setTextStatus = U_ZERO_ERROR; iterator->setText(text, setTextStatus); if (U_FAILURE(setTextStatus)) { WTF_LOG_ERROR("ubrk_setUText failed with status %d", setTextStatus); return 0; } utext_close(text); return iterator; }
int findNextWordFromIndex(const UChar* chars, int len, int position, bool forward) { TextBreakIterator* it = wordBreakIterator(chars, len); if (forward) { position = it->following(position); while (position != TextBreakDone) { // We stop searching when the character preceeding the break // is alphanumeric. if (position < len && isAlphanumeric(chars[position - 1])) return position; position = it->following(position); } return len; } else { position = it->preceding(position); while (position != TextBreakDone) { // We stop searching when the character following the break // is alphanumeric. if (position > 0 && isAlphanumeric(chars[position])) return position; position = it->preceding(position); } return 0; } }
static void findMisspellings(TextCheckerClient& client, const UChar* text, int start, int length, Vector<TextCheckingResult>& results) { TextBreakIterator* iterator = wordBreakIterator(text + start, length); if (!iterator) return; int wordStart = iterator->current(); while (0 <= wordStart) { int wordEnd = iterator->next(); if (wordEnd < 0) break; int wordLength = wordEnd - wordStart; int misspellingLocation = -1; int misspellingLength = 0; client.checkSpellingOfString(String(text + start + wordStart, wordLength), &misspellingLocation, &misspellingLength); if (0 < misspellingLength) { ASSERT(0 <= misspellingLocation && misspellingLocation <= wordLength); ASSERT(0 < misspellingLength && misspellingLocation + misspellingLength <= wordLength); TextCheckingResult misspelling; misspelling.decoration = TextDecorationTypeSpelling; misspelling.location = start + wordStart + misspellingLocation; misspelling.length = misspellingLength; results.append(misspelling); } wordStart = wordEnd; } }
Dart_Handle Paragraph::getWordBoundary(unsigned offset) { String text; int start = 0, end = 0; for (RenderObject* object = m_renderView.get(); object; object = object->nextInPreOrder()) { if (!object->isText()) continue; RenderText* renderText = toRenderText(object); text.append(renderText->text()); } TextBreakIterator* it = wordBreakIterator(text, 0, text.length()); if (it) { end = it->following(offset); if (end < 0) end = it->last(); start = it->previous(); } Dart_Handle result = Dart_NewList(2); Dart_ListSetAt(result, 0, ToDart(start)); Dart_ListSetAt(result, 1, ToDart(end)); return result; }
void findWordBoundary(const UChar* chars, int len, int position, int* start, int* end) { TextBreakIterator* it = wordBreakIterator(chars, len); *end = it->following(position); if (*end < 0) *end = it->last(); *start = it->previous(); }
static inline unsigned countGraphemesInCluster(const UChar* str, unsigned strLength, uint16_t startIndex, uint16_t endIndex) { if (startIndex > endIndex) { uint16_t tempIndex = startIndex; startIndex = endIndex; endIndex = tempIndex; } uint16_t length = endIndex - startIndex; ASSERT(static_cast<unsigned>(startIndex + length) <= strLength); TextBreakIterator* cursorPosIterator = cursorMovementIterator(&str[startIndex], length); int cursorPos = cursorPosIterator->current(); int numGraphemes = -1; while (0 <= cursorPos) { cursorPos = cursorPosIterator->next(); numGraphemes++; } return std::max(0, numGraphemes); }
void AbstractInlineTextBox::wordBoundaries(Vector<WordBoundaries>& words) const { if (!m_inlineTextBox) return; String text = this->text(); int len = text.length(); TextBreakIterator* iterator = wordBreakIterator(text, 0, len); // FIXME: When http://crbug.com/411764 is fixed, replace this with an ASSERT. if (!iterator) return; int pos = iterator->first(); while (pos >= 0 && pos < len) { int next = iterator->next(); if (isWordTextBreak(iterator)) words.append(WordBoundaries(pos, next)); pos = next; } }
static inline int nextBreakablePositionKeepAllInternal(LazyLineBreakIterator& lazyBreakIterator, const UChar* str, unsigned length, int pos) { int len = static_cast<int>(length); int nextBreak = -1; UChar lastLastCh = pos > 1 ? str[pos - 2] : static_cast<UChar>(lazyBreakIterator.secondToLastCharacter()); UChar lastCh = pos > 0 ? str[pos - 1] : static_cast<UChar>(lazyBreakIterator.lastCharacter()); unsigned priorContextLength = lazyBreakIterator.priorContextLength(); for (int i = pos; i < len; i++) { UChar ch = str[i]; if (isBreakableSpace(ch) || shouldBreakAfter(lastLastCh, lastCh, ch)) return i; if (!shouldKeepAfter(lastLastCh, lastCh, ch) && (needsLineBreakIterator(ch) || needsLineBreakIterator(lastCh))) { if (nextBreak < i) { // Don't break if positioned at start of primary context and there is no prior context. if (i || priorContextLength) { TextBreakIterator* breakIterator = lazyBreakIterator.get(priorContextLength); if (breakIterator) { nextBreak = breakIterator->following(i - 1 + priorContextLength); if (nextBreak >= 0) { nextBreak -= priorContextLength; } } } } if (i == nextBreak && !isBreakableSpace(lastCh)) return i; } lastLastCh = lastCh; lastCh = ch; } return len; }
int findWordEndBoundary(const UChar* chars, int len, int position) { TextBreakIterator* it = wordBreakIterator(chars, len); int end = it->following(position); return end < 0 ? it->last() : end; }
// Helper function to determine whether text is a single word. static bool isASingleWord(const String& text) { TextBreakIterator* it = wordBreakIterator(text, 0, text.length()); return it && it->next() == static_cast<int>(text.length()); }