Path: blob/master/thirdparty/icu4c/common/brkeng.cpp
10278 views
// © 2016 and later: Unicode, Inc. and others.1// License & terms of use: http://www.unicode.org/copyright.html2/*3************************************************************************************4* Copyright (C) 2006-2016, International Business Machines Corporation5* and others. All Rights Reserved.6************************************************************************************7*/89#include "unicode/utypes.h"1011#if !UCONFIG_NO_BREAK_ITERATION1213#include "unicode/uchar.h"14#include "unicode/uniset.h"15#include "unicode/chariter.h"16#include "unicode/ures.h"17#include "unicode/udata.h"18#include "unicode/putil.h"19#include "unicode/ustring.h"20#include "unicode/uscript.h"21#include "unicode/ucharstrie.h"22#include "unicode/bytestrie.h"23#include "unicode/rbbi.h"2425#include "brkeng.h"26#include "cmemory.h"27#include "dictbe.h"28#include "lstmbe.h"29#include "charstr.h"30#include "dictionarydata.h"31#include "mutex.h"32#include "uvector.h"33#include "umutex.h"34#include "uresimp.h"35#include "ubrkimpl.h"3637U_NAMESPACE_BEGIN3839/*40******************************************************************41*/4243LanguageBreakEngine::LanguageBreakEngine() {44}4546LanguageBreakEngine::~LanguageBreakEngine() {47}4849/*50******************************************************************51*/5253LanguageBreakFactory::LanguageBreakFactory() {54}5556LanguageBreakFactory::~LanguageBreakFactory() {57}5859/*60******************************************************************61*/6263UnhandledEngine::UnhandledEngine(UErrorCode &status) : fHandled(nullptr) {64(void)status;65}6667UnhandledEngine::~UnhandledEngine() {68delete fHandled;69fHandled = nullptr;70}7172UBool73UnhandledEngine::handles(UChar32 c, const char* locale) const {74(void)locale; // Unused75return fHandled && fHandled->contains(c);76}7778int32_t79UnhandledEngine::findBreaks( UText *text,80int32_t startPos,81int32_t endPos,82UVector32 &/*foundBreaks*/,83UBool /* isPhraseBreaking */,84UErrorCode &status) const {85if (U_FAILURE(status)) return 0;86utext_setNativeIndex(text, startPos);87UChar32 c = utext_current32(text);88while (static_cast<int32_t>(utext_getNativeIndex(text)) < endPos && fHandled->contains(c)) {89utext_next32(text); // TODO: recast loop to work with post-increment operations.90c = utext_current32(text);91}92return 0;93}9495void96UnhandledEngine::handleCharacter(UChar32 c) {97if (fHandled == nullptr) {98fHandled = new UnicodeSet();99if (fHandled == nullptr) {100return;101}102}103if (!fHandled->contains(c)) {104UErrorCode status = U_ZERO_ERROR;105// Apply the entire script of the character.106int32_t script = u_getIntPropertyValue(c, UCHAR_SCRIPT);107fHandled->applyIntPropertyValue(UCHAR_SCRIPT, script, status);108}109}110111/*112******************************************************************113*/114115ICULanguageBreakFactory::ICULanguageBreakFactory(UErrorCode &/*status*/) {116fEngines = nullptr;117}118119ICULanguageBreakFactory::~ICULanguageBreakFactory() {120delete fEngines;121}122123void ICULanguageBreakFactory::ensureEngines(UErrorCode& status) {124static UMutex gBreakEngineMutex;125Mutex m(&gBreakEngineMutex);126if (fEngines == nullptr) {127LocalPointer<UStack> engines(new UStack(uprv_deleteUObject, nullptr, status), status);128if (U_SUCCESS(status)) {129fEngines = engines.orphan();130}131}132}133134const LanguageBreakEngine *135ICULanguageBreakFactory::getEngineFor(UChar32 c, const char* locale) {136const LanguageBreakEngine *lbe = nullptr;137UErrorCode status = U_ZERO_ERROR;138ensureEngines(status);139if (U_FAILURE(status) ) {140// Note: no way to return error code to caller.141return nullptr;142}143144static UMutex gBreakEngineMutex;145Mutex m(&gBreakEngineMutex);146int32_t i = fEngines->size();147while (--i >= 0) {148lbe = static_cast<const LanguageBreakEngine*>(fEngines->elementAt(i));149if (lbe != nullptr && lbe->handles(c, locale)) {150return lbe;151}152}153154// We didn't find an engine. Create one.155lbe = loadEngineFor(c, locale);156if (lbe != nullptr) {157fEngines->push((void *)lbe, status);158}159return U_SUCCESS(status) ? lbe : nullptr;160}161162const LanguageBreakEngine *163ICULanguageBreakFactory::loadEngineFor(UChar32 c, const char*) {164UErrorCode status = U_ZERO_ERROR;165UScriptCode code = uscript_getScript(c, &status);166if (U_SUCCESS(status)) {167const LanguageBreakEngine *engine = nullptr;168// Try to use LSTM first169const LSTMData *data = CreateLSTMDataForScript(code, status);170if (U_SUCCESS(status)) {171if (data != nullptr) {172engine = CreateLSTMBreakEngine(code, data, status);173if (U_SUCCESS(status) && engine != nullptr) {174return engine;175}176if (engine != nullptr) {177delete engine;178engine = nullptr;179} else {180DeleteLSTMData(data);181}182}183}184status = U_ZERO_ERROR; // fallback to dictionary based185DictionaryMatcher *m = loadDictionaryMatcherFor(code);186if (m != nullptr) {187switch(code) {188case USCRIPT_THAI:189engine = new ThaiBreakEngine(m, status);190break;191case USCRIPT_LAO:192engine = new LaoBreakEngine(m, status);193break;194case USCRIPT_MYANMAR:195engine = new BurmeseBreakEngine(m, status);196break;197case USCRIPT_KHMER:198engine = new KhmerBreakEngine(m, status);199break;200201#if !UCONFIG_NO_NORMALIZATION202// CJK not available w/o normalization203case USCRIPT_HANGUL:204engine = new CjkBreakEngine(m, kKorean, status);205break;206207// use same BreakEngine and dictionary for both Chinese and Japanese208case USCRIPT_HIRAGANA:209case USCRIPT_KATAKANA:210case USCRIPT_HAN:211engine = new CjkBreakEngine(m, kChineseJapanese, status);212break;213#if 0214// TODO: Have to get some characters with script=common handled215// by CjkBreakEngine (e.g. U+309B). Simply subjecting216// them to CjkBreakEngine does not work. The engine has to217// special-case them.218case USCRIPT_COMMON:219{220UBlockCode block = ublock_getCode(code);221if (block == UBLOCK_HIRAGANA || block == UBLOCK_KATAKANA)222engine = new CjkBreakEngine(dict, kChineseJapanese, status);223break;224}225#endif226#endif227228default:229break;230}231if (engine == nullptr) {232delete m;233}234else if (U_FAILURE(status)) {235delete engine;236engine = nullptr;237}238return engine;239}240}241return nullptr;242}243244DictionaryMatcher *245ICULanguageBreakFactory::loadDictionaryMatcherFor(UScriptCode script) {246UErrorCode status = U_ZERO_ERROR;247// open root from brkitr tree.248UResourceBundle *b = ures_open(U_ICUDATA_BRKITR, "", &status);249b = ures_getByKeyWithFallback(b, "dictionaries", b, &status);250int32_t dictnlength = 0;251const char16_t *dictfname =252ures_getStringByKeyWithFallback(b, uscript_getShortName(script), &dictnlength, &status);253if (U_FAILURE(status)) {254ures_close(b);255return nullptr;256}257CharString dictnbuf;258CharString ext;259const char16_t *extStart = u_memrchr(dictfname, 0x002e, dictnlength); // last dot260if (extStart != nullptr) {261int32_t len = static_cast<int32_t>(extStart - dictfname);262ext.appendInvariantChars(UnicodeString(false, extStart + 1, dictnlength - len - 1), status);263dictnlength = len;264}265dictnbuf.appendInvariantChars(UnicodeString(false, dictfname, dictnlength), status);266ures_close(b);267268UDataMemory *file = udata_open(U_ICUDATA_BRKITR, ext.data(), dictnbuf.data(), &status);269if (U_SUCCESS(status)) {270// build trie271const uint8_t* data = static_cast<const uint8_t*>(udata_getMemory(file));272const int32_t* indexes = reinterpret_cast<const int32_t*>(data);273const int32_t offset = indexes[DictionaryData::IX_STRING_TRIE_OFFSET];274const int32_t trieType = indexes[DictionaryData::IX_TRIE_TYPE] & DictionaryData::TRIE_TYPE_MASK;275DictionaryMatcher *m = nullptr;276if (trieType == DictionaryData::TRIE_TYPE_BYTES) {277const int32_t transform = indexes[DictionaryData::IX_TRANSFORM];278const char* characters = reinterpret_cast<const char*>(data + offset);279m = new BytesDictionaryMatcher(characters, transform, file);280}281else if (trieType == DictionaryData::TRIE_TYPE_UCHARS) {282const char16_t* characters = reinterpret_cast<const char16_t*>(data + offset);283m = new UCharsDictionaryMatcher(characters, file);284}285if (m == nullptr) {286// no matcher exists to take ownership - either we are an invalid287// type or memory allocation failed288udata_close(file);289}290return m;291} else if (dictfname != nullptr) {292// we don't have a dictionary matcher.293// returning nullptr here will cause us to fail to find a dictionary break engine, as expected294status = U_ZERO_ERROR;295return nullptr;296}297return nullptr;298}299300301void ICULanguageBreakFactory::addExternalEngine(302ExternalBreakEngine* external, UErrorCode& status) {303LocalPointer<ExternalBreakEngine> engine(external, status);304ensureEngines(status);305LocalPointer<BreakEngineWrapper> wrapper(306new BreakEngineWrapper(engine.orphan(), status), status);307static UMutex gBreakEngineMutex;308Mutex m(&gBreakEngineMutex);309fEngines->push(wrapper.getAlias(), status);310wrapper.orphan();311}312313BreakEngineWrapper::BreakEngineWrapper(314ExternalBreakEngine* engine, UErrorCode &status) : delegate(engine, status) {315}316317BreakEngineWrapper::~BreakEngineWrapper() {318}319320UBool BreakEngineWrapper::handles(UChar32 c, const char* locale) const {321return delegate->isFor(c, locale);322}323324int32_t BreakEngineWrapper::findBreaks(325UText *text,326int32_t startPos,327int32_t endPos,328UVector32 &foundBreaks,329UBool /* isPhraseBreaking */,330UErrorCode &status) const {331if (U_FAILURE(status)) return 0;332int32_t result = 0;333334// Find the span of characters included in the set.335// The span to break begins at the current position in the text, and336// extends towards the start or end of the text, depending on 'reverse'.337338utext_setNativeIndex(text, startPos);339int32_t start = static_cast<int32_t>(utext_getNativeIndex(text));340int32_t current;341int32_t rangeStart;342int32_t rangeEnd;343UChar32 c = utext_current32(text);344while ((current = static_cast<int32_t>(utext_getNativeIndex(text))) < endPos && delegate->handles(c)) {345utext_next32(text); // TODO: recast loop for postincrement346c = utext_current32(text);347}348rangeStart = start;349rangeEnd = current;350int32_t beforeSize = foundBreaks.size();351int32_t additionalCapacity = rangeEnd - rangeStart + 1;352// enlarge to contains (rangeEnd-rangeStart+1) more items353foundBreaks.ensureCapacity(beforeSize+additionalCapacity, status);354if (U_FAILURE(status)) return 0;355foundBreaks.setSize(beforeSize + beforeSize+additionalCapacity);356result = delegate->fillBreaks(text, rangeStart, rangeEnd, foundBreaks.getBuffer()+beforeSize,357additionalCapacity, status);358if (U_FAILURE(status)) return 0;359foundBreaks.setSize(beforeSize + result);360utext_setNativeIndex(text, current);361return result;362}363364U_NAMESPACE_END365366#endif /* #if !UCONFIG_NO_BREAK_ITERATION */367368369