]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/brkeng.cpp
ICU-59131.0.1.tar.gz
[apple/icu.git] / icuSources / common / brkeng.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
4388f060 3/*
46f4442e 4 ************************************************************************************
2ca993e8 5 * Copyright (C) 2006-2016, International Business Machines Corporation
4388f060 6 * and others. All Rights Reserved.
46f4442e 7 ************************************************************************************
73c04bcf
A
8 */
9
10#include "unicode/utypes.h"
11
12#if !UCONFIG_NO_BREAK_ITERATION
13
14#include "brkeng.h"
2ca993e8 15#include "cmemory.h"
73c04bcf 16#include "dictbe.h"
73c04bcf
A
17#include "unicode/uchar.h"
18#include "unicode/uniset.h"
19#include "unicode/chariter.h"
20#include "unicode/ures.h"
21#include "unicode/udata.h"
22#include "unicode/putil.h"
23#include "unicode/ustring.h"
24#include "unicode/uscript.h"
51004dcb
A
25#include "unicode/ucharstrie.h"
26#include "unicode/bytestrie.h"
27#include "charstr.h"
28#include "dictionarydata.h"
2ca993e8 29#include "mutex.h"
73c04bcf 30#include "uvector.h"
46f4442e 31#include "umutex.h"
73c04bcf
A
32#include "uresimp.h"
33#include "ubrkimpl.h"
34
35U_NAMESPACE_BEGIN
36
37/*
38 ******************************************************************
39 */
40
41LanguageBreakEngine::LanguageBreakEngine() {
42}
43
44LanguageBreakEngine::~LanguageBreakEngine() {
45}
46
47/*
48 ******************************************************************
49 */
50
51LanguageBreakFactory::LanguageBreakFactory() {
52}
53
54LanguageBreakFactory::~LanguageBreakFactory() {
55}
56
57/*
58 ******************************************************************
59 */
60
61UnhandledEngine::UnhandledEngine(UErrorCode &/*status*/) {
2ca993e8 62 for (int32_t i = 0; i < UPRV_LENGTHOF(fHandled); ++i) {
73c04bcf
A
63 fHandled[i] = 0;
64 }
65}
66
67UnhandledEngine::~UnhandledEngine() {
2ca993e8 68 for (int32_t i = 0; i < UPRV_LENGTHOF(fHandled); ++i) {
73c04bcf
A
69 if (fHandled[i] != 0) {
70 delete fHandled[i];
71 }
72 }
73}
74
75UBool
76UnhandledEngine::handles(UChar32 c, int32_t breakType) const {
2ca993e8 77 return (breakType >= 0 && breakType < UPRV_LENGTHOF(fHandled)
73c04bcf
A
78 && fHandled[breakType] != 0 && fHandled[breakType]->contains(c));
79}
80
81int32_t
82UnhandledEngine::findBreaks( UText *text,
83 int32_t startPos,
84 int32_t endPos,
85 UBool reverse,
86 int32_t breakType,
87 UStack &/*foundBreaks*/ ) const {
2ca993e8 88 if (breakType >= 0 && breakType < UPRV_LENGTHOF(fHandled)) {
73c04bcf
A
89 UChar32 c = utext_current32(text);
90 if (reverse) {
91 while((int32_t)utext_getNativeIndex(text) > startPos && fHandled[breakType]->contains(c)) {
92 c = utext_previous32(text);
93 }
94 }
95 else {
96 while((int32_t)utext_getNativeIndex(text) < endPos && fHandled[breakType]->contains(c)) {
97 utext_next32(text); // TODO: recast loop to work with post-increment operations.
98 c = utext_current32(text);
99 }
100 }
101 }
102 return 0;
103}
104
105void
106UnhandledEngine::handleCharacter(UChar32 c, int32_t breakType) {
2ca993e8 107 if (breakType >= 0 && breakType < UPRV_LENGTHOF(fHandled)) {
73c04bcf
A
108 if (fHandled[breakType] == 0) {
109 fHandled[breakType] = new UnicodeSet();
110 if (fHandled[breakType] == 0) {
111 return;
112 }
113 }
114 if (!fHandled[breakType]->contains(c)) {
115 UErrorCode status = U_ZERO_ERROR;
116 // Apply the entire script of the character.
117 int32_t script = u_getIntPropertyValue(c, UCHAR_SCRIPT);
118 fHandled[breakType]->applyIntPropertyValue(UCHAR_SCRIPT, script, status);
119 }
120 }
121}
122
123/*
124 ******************************************************************
125 */
126
127ICULanguageBreakFactory::ICULanguageBreakFactory(UErrorCode &/*status*/) {
128 fEngines = 0;
129}
130
131ICULanguageBreakFactory::~ICULanguageBreakFactory() {
132 if (fEngines != 0) {
133 delete fEngines;
134 }
135}
136
137U_NAMESPACE_END
138U_CDECL_BEGIN
139static void U_CALLCONV _deleteEngine(void *obj) {
4388f060 140 delete (const icu::LanguageBreakEngine *) obj;
73c04bcf
A
141}
142U_CDECL_END
143U_NAMESPACE_BEGIN
144
2ca993e8
A
145static UMutex gBreakEngineMutex = U_MUTEX_INITIALIZER;
146
73c04bcf
A
147const LanguageBreakEngine *
148ICULanguageBreakFactory::getEngineFor(UChar32 c, int32_t breakType) {
73c04bcf
A
149 const LanguageBreakEngine *lbe = NULL;
150 UErrorCode status = U_ZERO_ERROR;
151
2ca993e8
A
152 Mutex m(&gBreakEngineMutex);
153
154 if (fEngines == NULL) {
155 UStack *engines = new UStack(_deleteEngine, NULL, status);
156 if (U_FAILURE(status) || engines == NULL) {
157 // Note: no way to return error code to caller.
158 delete engines;
159 return NULL;
160 }
161 fEngines = engines;
162 } else {
163 int32_t i = fEngines->size();
73c04bcf
A
164 while (--i >= 0) {
165 lbe = (const LanguageBreakEngine *)(fEngines->elementAt(i));
166 if (lbe != NULL && lbe->handles(c, breakType)) {
2ca993e8 167 return lbe;
73c04bcf 168 }
73c04bcf
A
169 }
170 }
73c04bcf 171
2ca993e8
A
172 // We didn't find an engine. Create one.
173 lbe = loadEngineFor(c, breakType);
73c04bcf 174 if (lbe != NULL) {
2ca993e8 175 fEngines->push((void *)lbe, status);
73c04bcf 176 }
73c04bcf
A
177 return lbe;
178}
179
180const LanguageBreakEngine *
181ICULanguageBreakFactory::loadEngineFor(UChar32 c, int32_t breakType) {
182 UErrorCode status = U_ZERO_ERROR;
183 UScriptCode code = uscript_getScript(c, &status);
184 if (U_SUCCESS(status)) {
51004dcb
A
185 DictionaryMatcher *m = loadDictionaryMatcherFor(code, breakType);
186 if (m != NULL) {
73c04bcf
A
187 const LanguageBreakEngine *engine = NULL;
188 switch(code) {
189 case USCRIPT_THAI:
51004dcb 190 engine = new ThaiBreakEngine(m, status);
73c04bcf 191 break;
57a6839d
A
192 case USCRIPT_LAO:
193 engine = new LaoBreakEngine(m, status);
194 break;
b331163b
A
195 case USCRIPT_MYANMAR:
196 engine = new BurmeseBreakEngine(m, status);
197 break;
4388f060 198 case USCRIPT_KHMER:
51004dcb 199 engine = new KhmerBreakEngine(m, status);
4388f060 200 break;
51004dcb
A
201
202#if !UCONFIG_NO_NORMALIZATION
203 // CJK not available w/o normalization
204 case USCRIPT_HANGUL:
205 engine = new CjkBreakEngine(m, kKorean, status);
206 break;
207
208 // use same BreakEngine and dictionary for both Chinese and Japanese
209 case USCRIPT_HIRAGANA:
210 case USCRIPT_KATAKANA:
211 case USCRIPT_HAN:
212 engine = new CjkBreakEngine(m, kChineseJapanese, status);
213 break;
214#if 0
215 // TODO: Have to get some characters with script=common handled
216 // by CjkBreakEngine (e.g. U+309B). Simply subjecting
217 // them to CjkBreakEngine does not work. The engine has to
218 // special-case them.
219 case USCRIPT_COMMON:
220 {
221 UBlockCode block = ublock_getCode(code);
222 if (block == UBLOCK_HIRAGANA || block == UBLOCK_KATAKANA)
223 engine = new CjkBreakEngine(dict, kChineseJapanese, status);
224 break;
225 }
226#endif
227#endif
228
73c04bcf
A
229 default:
230 break;
231 }
232 if (engine == NULL) {
51004dcb 233 delete m;
73c04bcf
A
234 }
235 else if (U_FAILURE(status)) {
236 delete engine;
237 engine = NULL;
238 }
239 return engine;
240 }
241 }
242 return NULL;
243}
244
51004dcb
A
245DictionaryMatcher *
246ICULanguageBreakFactory::loadDictionaryMatcherFor(UScriptCode script, int32_t /* brkType */) {
73c04bcf 247 UErrorCode status = U_ZERO_ERROR;
51004dcb 248 // open root from brkitr tree.
73c04bcf
A
249 UResourceBundle *b = ures_open(U_ICUDATA_BRKITR, "", &status);
250 b = ures_getByKeyWithFallback(b, "dictionaries", b, &status);
73c04bcf 251 int32_t dictnlength = 0;
51004dcb
A
252 const UChar *dictfname =
253 ures_getStringByKeyWithFallback(b, uscript_getShortName(script), &dictnlength, &status);
254 if (U_FAILURE(status)) {
255 ures_close(b);
256 return NULL;
73c04bcf 257 }
51004dcb
A
258 CharString dictnbuf;
259 CharString ext;
260 const UChar *extStart = u_memrchr(dictfname, 0x002e, dictnlength); // last dot
261 if (extStart != NULL) {
262 int32_t len = (int32_t)(extStart - dictfname);
263 ext.appendInvariantChars(UnicodeString(FALSE, extStart + 1, dictnlength - len - 1), status);
264 dictnlength = len;
73c04bcf 265 }
51004dcb 266 dictnbuf.appendInvariantChars(UnicodeString(FALSE, dictfname, dictnlength), status);
73c04bcf 267 ures_close(b);
51004dcb
A
268
269 UDataMemory *file = udata_open(U_ICUDATA_BRKITR, ext.data(), dictnbuf.data(), &status);
73c04bcf 270 if (U_SUCCESS(status)) {
51004dcb
A
271 // build trie
272 const uint8_t *data = (const uint8_t *)udata_getMemory(file);
273 const int32_t *indexes = (const int32_t *)data;
274 const int32_t offset = indexes[DictionaryData::IX_STRING_TRIE_OFFSET];
275 const int32_t trieType = indexes[DictionaryData::IX_TRIE_TYPE] & DictionaryData::TRIE_TYPE_MASK;
276 DictionaryMatcher *m = NULL;
277 if (trieType == DictionaryData::TRIE_TYPE_BYTES) {
278 const int32_t transform = indexes[DictionaryData::IX_TRANSFORM];
279 const char *characters = (const char *)(data + offset);
280 m = new BytesDictionaryMatcher(characters, transform, file);
281 }
282 else if (trieType == DictionaryData::TRIE_TYPE_UCHARS) {
283 const UChar *characters = (const UChar *)(data + offset);
284 m = new UCharsDictionaryMatcher(characters, file);
73c04bcf 285 }
51004dcb
A
286 if (m == NULL) {
287 // no matcher exists to take ownership - either we are an invalid
288 // type or memory allocation failed
289 udata_close(file);
73c04bcf 290 }
51004dcb
A
291 return m;
292 } else if (dictfname != NULL) {
293 // we don't have a dictionary matcher.
294 // returning NULL here will cause us to fail to find a dictionary break engine, as expected
295 status = U_ZERO_ERROR;
296 return NULL;
73c04bcf
A
297 }
298 return NULL;
299}
300
301U_NAMESPACE_END
302
303#endif /* #if !UCONFIG_NO_BREAK_ITERATION */