2020-08-11 09:10:23 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
|
|
|
/*
|
|
|
|
************************************************************************************
|
|
|
|
* Copyright (C) 2006-2016, International Business Machines Corporation
|
|
|
|
* and others. All Rights Reserved.
|
|
|
|
************************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_BREAK_ITERATION
|
|
|
|
|
|
|
|
#include "unicode/uchar.h"
|
|
|
|
#include "unicode/uniset.h"
|
|
|
|
#include "unicode/chariter.h"
|
|
|
|
#include "unicode/ures.h"
|
|
|
|
#include "unicode/udata.h"
|
|
|
|
#include "unicode/putil.h"
|
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "unicode/uscript.h"
|
|
|
|
#include "unicode/ucharstrie.h"
|
|
|
|
#include "unicode/bytestrie.h"
|
2023-11-01 06:56:12 +00:00
|
|
|
#include "unicode/rbbi.h"
|
2020-08-11 09:10:23 +00:00
|
|
|
|
|
|
|
#include "brkeng.h"
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "dictbe.h"
|
2021-10-28 06:15:28 +00:00
|
|
|
#include "lstmbe.h"
|
2020-08-11 09:10:23 +00:00
|
|
|
#include "charstr.h"
|
|
|
|
#include "dictionarydata.h"
|
|
|
|
#include "mutex.h"
|
|
|
|
#include "uvector.h"
|
|
|
|
#include "umutex.h"
|
|
|
|
#include "uresimp.h"
|
|
|
|
#include "ubrkimpl.h"
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
/*
|
|
|
|
******************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
LanguageBreakEngine::LanguageBreakEngine() {
|
|
|
|
}
|
|
|
|
|
|
|
|
LanguageBreakEngine::~LanguageBreakEngine() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
******************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
LanguageBreakFactory::LanguageBreakFactory() {
|
|
|
|
}
|
|
|
|
|
|
|
|
LanguageBreakFactory::~LanguageBreakFactory() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
******************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
UnhandledEngine::UnhandledEngine(UErrorCode &status) : fHandled(nullptr) {
|
|
|
|
(void)status;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnhandledEngine::~UnhandledEngine() {
|
|
|
|
delete fHandled;
|
|
|
|
fHandled = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
2023-11-01 06:56:12 +00:00
|
|
|
UnhandledEngine::handles(UChar32 c, const char* locale) const {
|
|
|
|
(void)locale; // Unused
|
2020-08-11 09:10:23 +00:00
|
|
|
return fHandled && fHandled->contains(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
UnhandledEngine::findBreaks( UText *text,
|
2023-11-01 06:56:12 +00:00
|
|
|
int32_t startPos,
|
2020-08-11 09:10:23 +00:00
|
|
|
int32_t endPos,
|
2021-10-28 06:15:28 +00:00
|
|
|
UVector32 &/*foundBreaks*/,
|
2022-05-17 15:14:19 +00:00
|
|
|
UBool /* isPhraseBreaking */,
|
2021-10-28 06:15:28 +00:00
|
|
|
UErrorCode &status) const {
|
|
|
|
if (U_FAILURE(status)) return 0;
|
2023-11-01 06:56:12 +00:00
|
|
|
utext_setNativeIndex(text, startPos);
|
|
|
|
UChar32 c = utext_current32(text);
|
2020-08-11 09:10:23 +00:00
|
|
|
while((int32_t)utext_getNativeIndex(text) < endPos && fHandled->contains(c)) {
|
|
|
|
utext_next32(text); // TODO: recast loop to work with post-increment operations.
|
|
|
|
c = utext_current32(text);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnhandledEngine::handleCharacter(UChar32 c) {
|
|
|
|
if (fHandled == nullptr) {
|
|
|
|
fHandled = new UnicodeSet();
|
|
|
|
if (fHandled == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!fHandled->contains(c)) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
// Apply the entire script of the character.
|
|
|
|
int32_t script = u_getIntPropertyValue(c, UCHAR_SCRIPT);
|
|
|
|
fHandled->applyIntPropertyValue(UCHAR_SCRIPT, script, status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
******************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
ICULanguageBreakFactory::ICULanguageBreakFactory(UErrorCode &/*status*/) {
|
2024-05-14 08:41:19 +00:00
|
|
|
fEngines = nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ICULanguageBreakFactory::~ICULanguageBreakFactory() {
|
2024-05-14 08:41:19 +00:00
|
|
|
delete fEngines;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 06:56:12 +00:00
|
|
|
void ICULanguageBreakFactory::ensureEngines(UErrorCode& status) {
|
|
|
|
static UMutex gBreakEngineMutex;
|
|
|
|
Mutex m(&gBreakEngineMutex);
|
|
|
|
if (fEngines == nullptr) {
|
|
|
|
LocalPointer<UStack> engines(new UStack(uprv_deleteUObject, nullptr, status), status);
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
fEngines = engines.orphan();
|
|
|
|
}
|
|
|
|
}
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const LanguageBreakEngine *
|
2023-11-01 06:56:12 +00:00
|
|
|
ICULanguageBreakFactory::getEngineFor(UChar32 c, const char* locale) {
|
2023-05-23 00:05:01 +00:00
|
|
|
const LanguageBreakEngine *lbe = nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2023-11-01 06:56:12 +00:00
|
|
|
ensureEngines(status);
|
|
|
|
if (U_FAILURE(status) ) {
|
|
|
|
// Note: no way to return error code to caller.
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-08-11 09:10:23 +00:00
|
|
|
|
|
|
|
static UMutex gBreakEngineMutex;
|
|
|
|
Mutex m(&gBreakEngineMutex);
|
2023-11-01 06:56:12 +00:00
|
|
|
int32_t i = fEngines->size();
|
|
|
|
while (--i >= 0) {
|
|
|
|
lbe = (const LanguageBreakEngine *)(fEngines->elementAt(i));
|
|
|
|
if (lbe != nullptr && lbe->handles(c, locale)) {
|
|
|
|
return lbe;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-01 06:56:12 +00:00
|
|
|
|
2020-08-11 09:10:23 +00:00
|
|
|
// We didn't find an engine. Create one.
|
2023-11-01 06:56:12 +00:00
|
|
|
lbe = loadEngineFor(c, locale);
|
2021-10-28 06:15:28 +00:00
|
|
|
if (lbe != nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
fEngines->push((void *)lbe, status);
|
|
|
|
}
|
2021-10-28 06:15:28 +00:00
|
|
|
return U_SUCCESS(status) ? lbe : nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const LanguageBreakEngine *
|
2023-11-01 06:56:12 +00:00
|
|
|
ICULanguageBreakFactory::loadEngineFor(UChar32 c, const char*) {
|
2020-08-11 09:10:23 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
UScriptCode code = uscript_getScript(c, &status);
|
|
|
|
if (U_SUCCESS(status)) {
|
2021-10-28 06:15:28 +00:00
|
|
|
const LanguageBreakEngine *engine = nullptr;
|
|
|
|
// Try to use LSTM first
|
|
|
|
const LSTMData *data = CreateLSTMDataForScript(code, status);
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
if (data != nullptr) {
|
|
|
|
engine = CreateLSTMBreakEngine(code, data, status);
|
|
|
|
if (U_SUCCESS(status) && engine != nullptr) {
|
|
|
|
return engine;
|
|
|
|
}
|
|
|
|
if (engine != nullptr) {
|
|
|
|
delete engine;
|
|
|
|
engine = nullptr;
|
|
|
|
} else {
|
|
|
|
DeleteLSTMData(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
status = U_ZERO_ERROR; // fallback to dictionary based
|
2020-08-11 09:10:23 +00:00
|
|
|
DictionaryMatcher *m = loadDictionaryMatcherFor(code);
|
2023-05-23 00:05:01 +00:00
|
|
|
if (m != nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
switch(code) {
|
|
|
|
case USCRIPT_THAI:
|
|
|
|
engine = new ThaiBreakEngine(m, status);
|
|
|
|
break;
|
|
|
|
case USCRIPT_LAO:
|
|
|
|
engine = new LaoBreakEngine(m, status);
|
|
|
|
break;
|
|
|
|
case USCRIPT_MYANMAR:
|
|
|
|
engine = new BurmeseBreakEngine(m, status);
|
|
|
|
break;
|
|
|
|
case USCRIPT_KHMER:
|
|
|
|
engine = new KhmerBreakEngine(m, status);
|
|
|
|
break;
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_NORMALIZATION
|
|
|
|
// CJK not available w/o normalization
|
|
|
|
case USCRIPT_HANGUL:
|
|
|
|
engine = new CjkBreakEngine(m, kKorean, status);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// use same BreakEngine and dictionary for both Chinese and Japanese
|
|
|
|
case USCRIPT_HIRAGANA:
|
|
|
|
case USCRIPT_KATAKANA:
|
|
|
|
case USCRIPT_HAN:
|
|
|
|
engine = new CjkBreakEngine(m, kChineseJapanese, status);
|
|
|
|
break;
|
|
|
|
#if 0
|
|
|
|
// TODO: Have to get some characters with script=common handled
|
|
|
|
// by CjkBreakEngine (e.g. U+309B). Simply subjecting
|
|
|
|
// them to CjkBreakEngine does not work. The engine has to
|
|
|
|
// special-case them.
|
|
|
|
case USCRIPT_COMMON:
|
|
|
|
{
|
|
|
|
UBlockCode block = ublock_getCode(code);
|
|
|
|
if (block == UBLOCK_HIRAGANA || block == UBLOCK_KATAKANA)
|
|
|
|
engine = new CjkBreakEngine(dict, kChineseJapanese, status);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2023-05-23 00:05:01 +00:00
|
|
|
if (engine == nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
delete m;
|
|
|
|
}
|
|
|
|
else if (U_FAILURE(status)) {
|
|
|
|
delete engine;
|
2023-05-23 00:05:01 +00:00
|
|
|
engine = nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
return engine;
|
|
|
|
}
|
|
|
|
}
|
2023-05-23 00:05:01 +00:00
|
|
|
return nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DictionaryMatcher *
|
|
|
|
ICULanguageBreakFactory::loadDictionaryMatcherFor(UScriptCode script) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
// open root from brkitr tree.
|
|
|
|
UResourceBundle *b = ures_open(U_ICUDATA_BRKITR, "", &status);
|
|
|
|
b = ures_getByKeyWithFallback(b, "dictionaries", b, &status);
|
|
|
|
int32_t dictnlength = 0;
|
2023-05-23 00:05:01 +00:00
|
|
|
const char16_t *dictfname =
|
2020-08-11 09:10:23 +00:00
|
|
|
ures_getStringByKeyWithFallback(b, uscript_getShortName(script), &dictnlength, &status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
ures_close(b);
|
2023-05-23 00:05:01 +00:00
|
|
|
return nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
CharString dictnbuf;
|
|
|
|
CharString ext;
|
2023-05-23 00:05:01 +00:00
|
|
|
const char16_t *extStart = u_memrchr(dictfname, 0x002e, dictnlength); // last dot
|
|
|
|
if (extStart != nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
int32_t len = (int32_t)(extStart - dictfname);
|
2022-10-28 06:11:55 +00:00
|
|
|
ext.appendInvariantChars(UnicodeString(false, extStart + 1, dictnlength - len - 1), status);
|
2020-08-11 09:10:23 +00:00
|
|
|
dictnlength = len;
|
|
|
|
}
|
2022-10-28 06:11:55 +00:00
|
|
|
dictnbuf.appendInvariantChars(UnicodeString(false, dictfname, dictnlength), status);
|
2020-08-11 09:10:23 +00:00
|
|
|
ures_close(b);
|
|
|
|
|
|
|
|
UDataMemory *file = udata_open(U_ICUDATA_BRKITR, ext.data(), dictnbuf.data(), &status);
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
// build trie
|
|
|
|
const uint8_t *data = (const uint8_t *)udata_getMemory(file);
|
|
|
|
const int32_t *indexes = (const int32_t *)data;
|
|
|
|
const int32_t offset = indexes[DictionaryData::IX_STRING_TRIE_OFFSET];
|
|
|
|
const int32_t trieType = indexes[DictionaryData::IX_TRIE_TYPE] & DictionaryData::TRIE_TYPE_MASK;
|
2023-05-23 00:05:01 +00:00
|
|
|
DictionaryMatcher *m = nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
if (trieType == DictionaryData::TRIE_TYPE_BYTES) {
|
|
|
|
const int32_t transform = indexes[DictionaryData::IX_TRANSFORM];
|
|
|
|
const char *characters = (const char *)(data + offset);
|
|
|
|
m = new BytesDictionaryMatcher(characters, transform, file);
|
|
|
|
}
|
|
|
|
else if (trieType == DictionaryData::TRIE_TYPE_UCHARS) {
|
2023-05-23 00:05:01 +00:00
|
|
|
const char16_t *characters = (const char16_t *)(data + offset);
|
2020-08-11 09:10:23 +00:00
|
|
|
m = new UCharsDictionaryMatcher(characters, file);
|
|
|
|
}
|
2023-05-23 00:05:01 +00:00
|
|
|
if (m == nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
// no matcher exists to take ownership - either we are an invalid
|
|
|
|
// type or memory allocation failed
|
|
|
|
udata_close(file);
|
|
|
|
}
|
|
|
|
return m;
|
2023-05-23 00:05:01 +00:00
|
|
|
} else if (dictfname != nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
// we don't have a dictionary matcher.
|
2023-05-23 00:05:01 +00:00
|
|
|
// returning nullptr here will cause us to fail to find a dictionary break engine, as expected
|
2020-08-11 09:10:23 +00:00
|
|
|
status = U_ZERO_ERROR;
|
2023-05-23 00:05:01 +00:00
|
|
|
return nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
2023-05-23 00:05:01 +00:00
|
|
|
return nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 06:56:12 +00:00
|
|
|
|
|
|
|
void ICULanguageBreakFactory::addExternalEngine(
|
|
|
|
ExternalBreakEngine* external, UErrorCode& status) {
|
|
|
|
LocalPointer<ExternalBreakEngine> engine(external, status);
|
|
|
|
ensureEngines(status);
|
|
|
|
LocalPointer<BreakEngineWrapper> wrapper(
|
|
|
|
new BreakEngineWrapper(engine.orphan(), status), status);
|
|
|
|
static UMutex gBreakEngineMutex;
|
|
|
|
Mutex m(&gBreakEngineMutex);
|
|
|
|
fEngines->push(wrapper.getAlias(), status);
|
|
|
|
wrapper.orphan();
|
|
|
|
}
|
|
|
|
|
|
|
|
BreakEngineWrapper::BreakEngineWrapper(
|
|
|
|
ExternalBreakEngine* engine, UErrorCode &status) : delegate(engine, status) {
|
|
|
|
}
|
|
|
|
|
|
|
|
BreakEngineWrapper::~BreakEngineWrapper() {
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool BreakEngineWrapper::handles(UChar32 c, const char* locale) const {
|
|
|
|
return delegate->isFor(c, locale);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t BreakEngineWrapper::findBreaks(
|
|
|
|
UText *text,
|
|
|
|
int32_t startPos,
|
|
|
|
int32_t endPos,
|
|
|
|
UVector32 &foundBreaks,
|
|
|
|
UBool /* isPhraseBreaking */,
|
|
|
|
UErrorCode &status) const {
|
|
|
|
if (U_FAILURE(status)) return 0;
|
|
|
|
int32_t result = 0;
|
|
|
|
|
|
|
|
// Find the span of characters included in the set.
|
|
|
|
// The span to break begins at the current position in the text, and
|
|
|
|
// extends towards the start or end of the text, depending on 'reverse'.
|
|
|
|
|
|
|
|
utext_setNativeIndex(text, startPos);
|
|
|
|
int32_t start = (int32_t)utext_getNativeIndex(text);
|
|
|
|
int32_t current;
|
|
|
|
int32_t rangeStart;
|
|
|
|
int32_t rangeEnd;
|
|
|
|
UChar32 c = utext_current32(text);
|
|
|
|
while((current = (int32_t)utext_getNativeIndex(text)) < endPos && delegate->handles(c)) {
|
|
|
|
utext_next32(text); // TODO: recast loop for postincrement
|
|
|
|
c = utext_current32(text);
|
|
|
|
}
|
|
|
|
rangeStart = start;
|
|
|
|
rangeEnd = current;
|
|
|
|
int32_t beforeSize = foundBreaks.size();
|
|
|
|
int32_t additionalCapacity = rangeEnd - rangeStart + 1;
|
|
|
|
// enlarge to contains (rangeEnd-rangeStart+1) more items
|
|
|
|
foundBreaks.ensureCapacity(beforeSize+additionalCapacity, status);
|
|
|
|
if (U_FAILURE(status)) return 0;
|
|
|
|
foundBreaks.setSize(beforeSize + beforeSize+additionalCapacity);
|
|
|
|
result = delegate->fillBreaks(text, rangeStart, rangeEnd, foundBreaks.getBuffer()+beforeSize,
|
|
|
|
additionalCapacity, status);
|
|
|
|
if (U_FAILURE(status)) return 0;
|
|
|
|
foundBreaks.setSize(beforeSize + result);
|
|
|
|
utext_setNativeIndex(text, current);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-08-11 09:10:23 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_BREAK_ITERATION */
|