2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2011-07-06 04:03:35 +00:00
|
|
|
/*
|
2007-05-02 23:23:31 +00:00
|
|
|
************************************************************************************
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 2006-2016, International Business Machines Corporation
|
2011-07-06 04:03:35 +00:00
|
|
|
* and others. All Rights Reserved.
|
2007-05-02 23:23:31 +00:00
|
|
|
************************************************************************************
|
2006-03-23 00:54:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
2006-04-21 00:30:22 +00:00
|
|
|
#include "unicode/ustring.h"
|
2007-05-02 23:07:12 +00:00
|
|
|
#include "unicode/uscript.h"
|
2012-08-16 23:01:49 +00:00
|
|
|
#include "unicode/ucharstrie.h"
|
|
|
|
#include "unicode/bytestrie.h"
|
2017-09-19 18:17:22 +00:00
|
|
|
|
|
|
|
#include "brkeng.h"
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "dictbe.h"
|
2012-10-17 23:54:34 +00:00
|
|
|
#include "charstr.h"
|
2012-08-16 23:01:49 +00:00
|
|
|
#include "dictionarydata.h"
|
2015-05-14 21:54:53 +00:00
|
|
|
#include "mutex.h"
|
2006-03-23 00:54:12 +00:00
|
|
|
#include "uvector.h"
|
2006-09-08 03:35:35 +00:00
|
|
|
#include "umutex.h"
|
2006-03-23 00:54:12 +00:00
|
|
|
#include "uresimp.h"
|
|
|
|
#include "ubrkimpl.h"
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
/*
|
|
|
|
******************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
LanguageBreakEngine::LanguageBreakEngine() {
|
|
|
|
}
|
|
|
|
|
|
|
|
LanguageBreakEngine::~LanguageBreakEngine() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
******************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
LanguageBreakFactory::LanguageBreakFactory() {
|
|
|
|
}
|
|
|
|
|
|
|
|
LanguageBreakFactory::~LanguageBreakFactory() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
******************************************************************
|
|
|
|
*/
|
|
|
|
|
2006-03-23 20:48:47 +00:00
|
|
|
UnhandledEngine::UnhandledEngine(UErrorCode &/*status*/) {
|
2016-02-23 10:40:09 +00:00
|
|
|
for (int32_t i = 0; i < UPRV_LENGTHOF(fHandled); ++i) {
|
2006-03-23 00:54:12 +00:00
|
|
|
fHandled[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UnhandledEngine::~UnhandledEngine() {
|
2016-02-23 10:40:09 +00:00
|
|
|
for (int32_t i = 0; i < UPRV_LENGTHOF(fHandled); ++i) {
|
2006-03-23 00:54:12 +00:00
|
|
|
if (fHandled[i] != 0) {
|
|
|
|
delete fHandled[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
|
|
|
UnhandledEngine::handles(UChar32 c, int32_t breakType) const {
|
2016-02-23 10:40:09 +00:00
|
|
|
return (breakType >= 0 && breakType < UPRV_LENGTHOF(fHandled)
|
2006-03-23 00:54:12 +00:00
|
|
|
&& fHandled[breakType] != 0 && fHandled[breakType]->contains(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
2006-04-22 05:29:27 +00:00
|
|
|
UnhandledEngine::findBreaks( UText *text,
|
2017-09-19 18:17:22 +00:00
|
|
|
int32_t /* startPos */,
|
|
|
|
int32_t endPos,
|
|
|
|
int32_t breakType,
|
|
|
|
UVector32 &/*foundBreaks*/ ) const {
|
2016-02-23 10:40:09 +00:00
|
|
|
if (breakType >= 0 && breakType < UPRV_LENGTHOF(fHandled)) {
|
2006-04-22 05:29:27 +00:00
|
|
|
UChar32 c = utext_current32(text);
|
2017-09-19 18:17:22 +00:00
|
|
|
while((int32_t)utext_getNativeIndex(text) < endPos && fHandled[breakType]->contains(c)) {
|
|
|
|
utext_next32(text); // TODO: recast loop to work with post-increment operations.
|
|
|
|
c = utext_current32(text);
|
2006-03-23 00:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnhandledEngine::handleCharacter(UChar32 c, int32_t breakType) {
|
2016-02-23 10:40:09 +00:00
|
|
|
if (breakType >= 0 && breakType < UPRV_LENGTHOF(fHandled)) {
|
2006-03-23 00:54:12 +00:00
|
|
|
if (fHandled[breakType] == 0) {
|
|
|
|
fHandled[breakType] = new UnicodeSet();
|
|
|
|
if (fHandled[breakType] == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!fHandled[breakType]->contains(c)) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
// Apply the entire script of the character.
|
|
|
|
int32_t script = u_getIntPropertyValue(c, UCHAR_SCRIPT);
|
|
|
|
fHandled[breakType]->applyIntPropertyValue(UCHAR_SCRIPT, script, status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
******************************************************************
|
|
|
|
*/
|
|
|
|
|
2006-03-23 20:48:47 +00:00
|
|
|
ICULanguageBreakFactory::ICULanguageBreakFactory(UErrorCode &/*status*/) {
|
2006-03-23 00:54:12 +00:00
|
|
|
fEngines = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ICULanguageBreakFactory::~ICULanguageBreakFactory() {
|
|
|
|
if (fEngines != 0) {
|
|
|
|
delete fEngines;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
U_CDECL_BEGIN
|
|
|
|
static void U_CALLCONV _deleteEngine(void *obj) {
|
2011-07-06 04:03:35 +00:00
|
|
|
delete (const icu::LanguageBreakEngine *) obj;
|
2006-03-23 00:54:12 +00:00
|
|
|
}
|
|
|
|
U_CDECL_END
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2015-05-14 21:54:53 +00:00
|
|
|
static UMutex gBreakEngineMutex = U_MUTEX_INITIALIZER;
|
|
|
|
|
2006-03-23 00:54:12 +00:00
|
|
|
const LanguageBreakEngine *
|
|
|
|
ICULanguageBreakFactory::getEngineFor(UChar32 c, int32_t breakType) {
|
2007-05-02 23:07:12 +00:00
|
|
|
const LanguageBreakEngine *lbe = NULL;
|
2006-03-23 00:54:12 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2007-05-02 23:07:12 +00:00
|
|
|
|
2015-05-14 21:54:53 +00:00
|
|
|
Mutex m(&gBreakEngineMutex);
|
|
|
|
|
|
|
|
if (fEngines == NULL) {
|
|
|
|
UStack *engines = new UStack(_deleteEngine, NULL, status);
|
|
|
|
if (U_FAILURE(status) || engines == NULL) {
|
|
|
|
// Note: no way to return error code to caller.
|
|
|
|
delete engines;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
fEngines = engines;
|
|
|
|
} else {
|
|
|
|
int32_t i = fEngines->size();
|
2007-05-02 23:07:12 +00:00
|
|
|
while (--i >= 0) {
|
|
|
|
lbe = (const LanguageBreakEngine *)(fEngines->elementAt(i));
|
|
|
|
if (lbe != NULL && lbe->handles(c, breakType)) {
|
2015-05-14 21:54:53 +00:00
|
|
|
return lbe;
|
2007-05-02 23:07:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-14 21:54:53 +00:00
|
|
|
// We didn't find an engine. Create one.
|
|
|
|
lbe = loadEngineFor(c, breakType);
|
2007-05-02 23:07:12 +00:00
|
|
|
if (lbe != NULL) {
|
2015-05-14 21:54:53 +00:00
|
|
|
fEngines->push((void *)lbe, status);
|
2007-05-02 23:07:12 +00:00
|
|
|
}
|
2006-03-23 00:54:12 +00:00
|
|
|
return lbe;
|
|
|
|
}
|
|
|
|
|
2007-05-02 23:07:12 +00:00
|
|
|
const LanguageBreakEngine *
|
|
|
|
ICULanguageBreakFactory::loadEngineFor(UChar32 c, int32_t breakType) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
UScriptCode code = uscript_getScript(c, &status);
|
|
|
|
if (U_SUCCESS(status)) {
|
2012-08-16 23:01:49 +00:00
|
|
|
DictionaryMatcher *m = loadDictionaryMatcherFor(code, breakType);
|
|
|
|
if (m != NULL) {
|
2007-05-02 23:07:12 +00:00
|
|
|
const LanguageBreakEngine *engine = NULL;
|
|
|
|
switch(code) {
|
|
|
|
case USCRIPT_THAI:
|
2012-08-16 23:01:49 +00:00
|
|
|
engine = new ThaiBreakEngine(m, status);
|
2007-05-02 23:07:12 +00:00
|
|
|
break;
|
2013-09-06 23:43:13 +00:00
|
|
|
case USCRIPT_LAO:
|
|
|
|
engine = new LaoBreakEngine(m, status);
|
|
|
|
break;
|
2014-09-08 22:16:21 +00:00
|
|
|
case USCRIPT_MYANMAR:
|
|
|
|
engine = new BurmeseBreakEngine(m, status);
|
|
|
|
break;
|
2011-05-04 13:25:37 +00:00
|
|
|
case USCRIPT_KHMER:
|
2012-08-16 23:01:49 +00:00
|
|
|
engine = new KhmerBreakEngine(m, status);
|
2011-05-04 13:25:37 +00:00
|
|
|
break;
|
2012-09-11 04:02:08 +00:00
|
|
|
|
|
|
|
#if !UCONFIG_NO_NORMALIZATION
|
|
|
|
// CJK not available w/o normalization
|
2012-08-16 23:01:49 +00:00
|
|
|
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
|
2012-09-11 04:02:08 +00:00
|
|
|
#endif
|
|
|
|
|
2007-05-02 23:07:12 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (engine == NULL) {
|
2012-08-16 23:01:49 +00:00
|
|
|
delete m;
|
2007-05-02 23:07:12 +00:00
|
|
|
}
|
|
|
|
else if (U_FAILURE(status)) {
|
|
|
|
delete engine;
|
|
|
|
engine = NULL;
|
|
|
|
}
|
|
|
|
return engine;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-16 23:01:49 +00:00
|
|
|
DictionaryMatcher *
|
|
|
|
ICULanguageBreakFactory::loadDictionaryMatcherFor(UScriptCode script, int32_t /* brkType */) {
|
2007-05-02 23:07:12 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2012-08-16 23:01:49 +00:00
|
|
|
// open root from brkitr tree.
|
2007-05-02 23:07:12 +00:00
|
|
|
UResourceBundle *b = ures_open(U_ICUDATA_BRKITR, "", &status);
|
|
|
|
b = ures_getByKeyWithFallback(b, "dictionaries", b, &status);
|
|
|
|
int32_t dictnlength = 0;
|
2012-10-17 23:54:34 +00:00
|
|
|
const UChar *dictfname =
|
|
|
|
ures_getStringByKeyWithFallback(b, uscript_getShortName(script), &dictnlength, &status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
ures_close(b);
|
|
|
|
return NULL;
|
2007-05-02 23:07:12 +00:00
|
|
|
}
|
2012-10-17 23:54:34 +00:00
|
|
|
CharString dictnbuf;
|
|
|
|
CharString ext;
|
|
|
|
const UChar *extStart = u_memrchr(dictfname, 0x002e, dictnlength); // last dot
|
|
|
|
if (extStart != NULL) {
|
|
|
|
int32_t len = (int32_t)(extStart - dictfname);
|
|
|
|
ext.appendInvariantChars(UnicodeString(FALSE, extStart + 1, dictnlength - len - 1), status);
|
|
|
|
dictnlength = len;
|
2007-05-02 23:07:12 +00:00
|
|
|
}
|
2012-10-17 23:54:34 +00:00
|
|
|
dictnbuf.appendInvariantChars(UnicodeString(FALSE, dictfname, dictnlength), status);
|
2007-05-02 23:07:12 +00:00
|
|
|
ures_close(b);
|
2012-08-16 23:01:49 +00:00
|
|
|
|
2012-10-17 23:54:34 +00:00
|
|
|
UDataMemory *file = udata_open(U_ICUDATA_BRKITR, ext.data(), dictnbuf.data(), &status);
|
2007-05-02 23:07:12 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
2012-08-16 23:01:49 +00:00
|
|
|
// 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;
|
|
|
|
DictionaryMatcher *m = NULL;
|
|
|
|
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);
|
2007-05-02 23:07:12 +00:00
|
|
|
}
|
2012-08-16 23:01:49 +00:00
|
|
|
else if (trieType == DictionaryData::TRIE_TYPE_UCHARS) {
|
|
|
|
const UChar *characters = (const UChar *)(data + offset);
|
|
|
|
m = new UCharsDictionaryMatcher(characters, file);
|
2007-05-02 23:07:12 +00:00
|
|
|
}
|
2012-08-16 23:01:49 +00:00
|
|
|
if (m == NULL) {
|
|
|
|
// no matcher exists to take ownership - either we are an invalid
|
|
|
|
// type or memory allocation failed
|
|
|
|
udata_close(file);
|
|
|
|
}
|
|
|
|
return m;
|
|
|
|
} else if (dictfname != NULL) {
|
|
|
|
// we don't have a dictionary matcher.
|
|
|
|
// returning NULL here will cause us to fail to find a dictionary break engine, as expected
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
return NULL;
|
2007-05-02 23:07:12 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-03-23 00:54:12 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_BREAK_ITERATION */
|