1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
/*
* (C) 1999 Lars Knoll (knoll@kde.org)
* Copyright (C) 2004-2023 Apple Inc. All rights reserved.
* Copyright (C) 2007-2009 Torch Mobile, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include <wtf/text/TextBreakIterator.h>
#include <wtf/Compiler.h>
#include <wtf/text/TextBreakIteratorInternalICU.h>
#include <wtf/text/icu/UTextProviderLatin1.h>
#include <wtf/text/icu/UTextProviderUTF16.h>
#include <atomic>
#include <unicode/ubrk.h>
namespace WTF {
TextBreakIteratorCache& TextBreakIteratorCache::singleton()
{
static LazyNeverDestroyed<TextBreakIteratorCache> cache;
static std::once_flag onceKey;
std::call_once(onceKey, [&] {
cache.construct();
});
return cache.get();
}
#if !PLATFORM(COCOA)
TextBreakIterator::Backing TextBreakIterator::mapModeToBackingIterator(StringView string, std::span<const UChar> priorContext, Mode mode, ContentAnalysis, const AtomString& locale)
{
return switchOn(mode, [string, priorContext, &locale](TextBreakIterator::LineMode lineMode) -> TextBreakIterator::Backing {
return TextBreakIteratorICU(string, priorContext, TextBreakIteratorICU::LineMode { lineMode.behavior }, locale);
}, [string, priorContext, &locale](TextBreakIterator::CaretMode) -> TextBreakIterator::Backing {
return TextBreakIteratorICU(string, priorContext, TextBreakIteratorICU::CharacterMode { }, locale);
}, [string, priorContext, &locale](TextBreakIterator::DeleteMode) -> TextBreakIterator::Backing {
return TextBreakIteratorICU(string, priorContext, TextBreakIteratorICU::CharacterMode { }, locale);
}, [string, priorContext, &locale](TextBreakIterator::CharacterMode) -> TextBreakIterator::Backing {
return TextBreakIteratorICU(string, priorContext, TextBreakIteratorICU::CharacterMode { }, locale);
});
}
TextBreakIterator::TextBreakIterator(StringView string, std::span<const UChar> priorContext, Mode mode, ContentAnalysis contentAnalysis, const AtomString& locale)
: m_backing(mapModeToBackingIterator(string, priorContext, mode, contentAnalysis, locale))
, m_mode(mode)
, m_locale(locale)
{
}
#endif
// Iterator initialization
static UBreakIterator* initializeIterator(UBreakIteratorType type, const char* locale = currentTextBreakLocaleID())
{
UErrorCode openStatus = U_ZERO_ERROR;
UBreakIterator* iterator = ubrk_open(type, locale, nullptr, 0, &openStatus);
ASSERT_WITH_MESSAGE(U_SUCCESS(openStatus), "ICU could not open a break iterator: %s (%d)", u_errorName(openStatus), openStatus);
return iterator;
}
// Iterator text setting
static UBreakIterator* setTextForIterator(UBreakIterator& iterator, StringView string)
{
if (string.is8Bit()) {
UTextWithBuffer textLocal;
textLocal.text = UTEXT_INITIALIZER;
textLocal.text.extraSize = sizeof(textLocal.buffer);
textLocal.text.pExtra = textLocal.buffer;
UErrorCode openStatus = U_ZERO_ERROR;
UText* text = openLatin1UTextProvider(&textLocal, string.span8(), &openStatus);
if (U_FAILURE(openStatus)) {
LOG_ERROR("uTextOpenLatin1 failed with status %d", openStatus);
return nullptr;
}
UErrorCode setTextStatus = U_ZERO_ERROR;
ubrk_setUText(&iterator, text, &setTextStatus);
if (U_FAILURE(setTextStatus)) {
LOG_ERROR("ubrk_setUText failed with status %d", setTextStatus);
return nullptr;
}
utext_close(text);
} else {
UErrorCode setTextStatus = U_ZERO_ERROR;
auto characters = string.span16();
ubrk_setText(&iterator, characters.data(), characters.size(), &setTextStatus);
if (U_FAILURE(setTextStatus))
return nullptr;
}
return &iterator;
}
// Static iterators
// FIXME: Delete this in favor of CachedTextBreakIterator.
UBreakIterator* wordBreakIterator(StringView string)
{
static UBreakIterator* staticWordBreakIterator = initializeIterator(UBRK_WORD);
if (!staticWordBreakIterator)
return nullptr;
return setTextForIterator(*staticWordBreakIterator, string);
}
// FIXME: Delete this in favor of CachedTextBreakIterator.
UBreakIterator* sentenceBreakIterator(StringView string)
{
static UBreakIterator* staticSentenceBreakIterator = initializeIterator(UBRK_SENTENCE);
if (!staticSentenceBreakIterator)
return nullptr;
return setTextForIterator(*staticSentenceBreakIterator, string);
}
ALLOW_DEPRECATED_PRAGMA_BEGIN
static std::atomic<UBreakIterator*> nonSharedCharacterBreakIterator = ATOMIC_VAR_INIT(nullptr);
ALLOW_DEPRECATED_PRAGMA_END
static inline UBreakIterator* getNonSharedCharacterBreakIterator()
{
if (auto *res = nonSharedCharacterBreakIterator.exchange(nullptr, std::memory_order_acquire))
return res;
return initializeIterator(UBRK_CHARACTER);
}
static inline void cacheNonSharedCharacterBreakIterator(UBreakIterator* cacheMe)
{
if (auto *old = nonSharedCharacterBreakIterator.exchange(cacheMe, std::memory_order_release))
ubrk_close(old);
}
NonSharedCharacterBreakIterator::NonSharedCharacterBreakIterator(StringView string)
{
if ((m_iterator = getNonSharedCharacterBreakIterator()))
m_iterator = setTextForIterator(*m_iterator, string);
}
NonSharedCharacterBreakIterator::~NonSharedCharacterBreakIterator()
{
if (m_iterator)
cacheNonSharedCharacterBreakIterator(m_iterator);
}
NonSharedCharacterBreakIterator::NonSharedCharacterBreakIterator(NonSharedCharacterBreakIterator&& other)
: m_iterator(nullptr)
{
std::swap(m_iterator, other.m_iterator);
}
// Iterator implemenation.
bool isWordTextBreak(UBreakIterator* iterator)
{
int ruleStatus = ubrk_getRuleStatus(iterator);
return ruleStatus != UBRK_WORD_NONE;
}
unsigned numGraphemeClusters(StringView string)
{
unsigned stringLength = string.length();
if (!stringLength)
return 0;
// The only Latin-1 Extended Grapheme Cluster is CRLF.
if (string.is8Bit()) {
auto characters = string.span8();
unsigned numCRLF = 0;
for (unsigned i = 1; i < stringLength; ++i)
numCRLF += characters[i - 1] == '\r' && characters[i] == '\n';
return stringLength - numCRLF;
}
NonSharedCharacterBreakIterator iterator { string };
if (!iterator) {
ASSERT_NOT_REACHED();
return stringLength;
}
unsigned numGraphemeClusters = 0;
while (ubrk_next(iterator) != UBRK_DONE)
++numGraphemeClusters;
return numGraphemeClusters;
}
unsigned numCodeUnitsInGraphemeClusters(StringView string, unsigned numGraphemeClusters)
{
unsigned stringLength = string.length();
if (stringLength <= numGraphemeClusters)
return stringLength;
// The only Latin-1 Extended Grapheme Cluster is CRLF.
if (string.is8Bit()) {
auto characters = string.span8();
unsigned i, j;
for (i = 0, j = 0; i < numGraphemeClusters && j + 1 < stringLength; ++i, ++j)
j += characters[j] == '\r' && characters[j + 1] == '\n';
return j + (i < numGraphemeClusters);
}
NonSharedCharacterBreakIterator iterator { string };
if (!iterator) {
ASSERT_NOT_REACHED();
return stringLength;
}
for (unsigned i = 0; i < numGraphemeClusters; ++i) {
if (ubrk_next(iterator) == UBRK_DONE)
return stringLength;
}
return ubrk_current(iterator);
}
} // namespace WTF
|