/src/icu/icu4c/source/common/brkeng.cpp
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | // © 2016 and later: Unicode, Inc. and others.  | 
2  |  | // License & terms of use: http://www.unicode.org/copyright.html  | 
3  |  | /*  | 
4  |  |  ************************************************************************************  | 
5  |  |  * Copyright (C) 2006-2016, International Business Machines Corporation  | 
6  |  |  * and others. All Rights Reserved.  | 
7  |  |  ************************************************************************************  | 
8  |  |  */  | 
9  |  |  | 
10  |  | #include "unicode/utypes.h"  | 
11  |  |  | 
12  |  | #if !UCONFIG_NO_BREAK_ITERATION  | 
13  |  |  | 
14  |  | #include "unicode/uchar.h"  | 
15  |  | #include "unicode/uniset.h"  | 
16  |  | #include "unicode/chariter.h"  | 
17  |  | #include "unicode/ures.h"  | 
18  |  | #include "unicode/udata.h"  | 
19  |  | #include "unicode/putil.h"  | 
20  |  | #include "unicode/ustring.h"  | 
21  |  | #include "unicode/uscript.h"  | 
22  |  | #include "unicode/ucharstrie.h"  | 
23  |  | #include "unicode/bytestrie.h"  | 
24  |  | #include "unicode/rbbi.h"  | 
25  |  |  | 
26  |  | #include "brkeng.h"  | 
27  |  | #include "cmemory.h"  | 
28  |  | #include "dictbe.h"  | 
29  |  | #include "lstmbe.h"  | 
30  |  | #include "charstr.h"  | 
31  |  | #include "dictionarydata.h"  | 
32  |  | #include "mutex.h"  | 
33  |  | #include "uvector.h"  | 
34  |  | #include "umutex.h"  | 
35  |  | #include "uresimp.h"  | 
36  |  | #include "ubrkimpl.h"  | 
37  |  |  | 
38  |  | U_NAMESPACE_BEGIN  | 
39  |  |  | 
40  |  | /*  | 
41  |  |  ******************************************************************  | 
42  |  |  */  | 
43  |  |  | 
44  | 0  | LanguageBreakEngine::LanguageBreakEngine() { | 
45  | 0  | }  | 
46  |  |  | 
47  | 0  | LanguageBreakEngine::~LanguageBreakEngine() { | 
48  | 0  | }  | 
49  |  |  | 
50  |  | /*  | 
51  |  |  ******************************************************************  | 
52  |  |  */  | 
53  |  |  | 
54  | 0  | LanguageBreakFactory::LanguageBreakFactory() { | 
55  | 0  | }  | 
56  |  |  | 
57  | 0  | LanguageBreakFactory::~LanguageBreakFactory() { | 
58  | 0  | }  | 
59  |  |  | 
60  |  | /*  | 
61  |  |  ******************************************************************  | 
62  |  |  */  | 
63  |  |  | 
64  | 0  | UnhandledEngine::UnhandledEngine(UErrorCode &status) : fHandled(nullptr) { | 
65  | 0  |     (void)status;  | 
66  | 0  | }  | 
67  |  |  | 
68  | 0  | UnhandledEngine::~UnhandledEngine() { | 
69  | 0  |     delete fHandled;  | 
70  | 0  |     fHandled = nullptr;  | 
71  | 0  | }  | 
72  |  |  | 
73  |  | UBool  | 
74  | 0  | UnhandledEngine::handles(UChar32 c, const char* locale) const { | 
75  | 0  |     (void)locale; // Unused  | 
76  | 0  |     return fHandled && fHandled->contains(c);  | 
77  | 0  | }  | 
78  |  |  | 
79  |  | int32_t  | 
80  |  | UnhandledEngine::findBreaks( UText *text,  | 
81  |  |                              int32_t startPos,  | 
82  |  |                              int32_t endPos,  | 
83  |  |                              UVector32 &/*foundBreaks*/,  | 
84  |  |                              UBool /* isPhraseBreaking */,  | 
85  | 0  |                              UErrorCode &status) const { | 
86  | 0  |     if (U_FAILURE(status)) return 0;  | 
87  | 0  |     utext_setNativeIndex(text, startPos);  | 
88  | 0  |     UChar32 c = utext_current32(text);  | 
89  | 0  |     while (static_cast<int32_t>(utext_getNativeIndex(text)) < endPos && fHandled->contains(c)) { | 
90  | 0  |         utext_next32(text);            // TODO:  recast loop to work with post-increment operations.  | 
91  | 0  |         c = utext_current32(text);  | 
92  | 0  |     }  | 
93  | 0  |     return 0;  | 
94  | 0  | }  | 
95  |  |  | 
96  |  | void  | 
97  | 0  | UnhandledEngine::handleCharacter(UChar32 c) { | 
98  | 0  |     if (fHandled == nullptr) { | 
99  | 0  |         fHandled = new UnicodeSet();  | 
100  | 0  |         if (fHandled == nullptr) { | 
101  | 0  |             return;  | 
102  | 0  |         }  | 
103  | 0  |     }  | 
104  | 0  |     if (!fHandled->contains(c)) { | 
105  | 0  |         UErrorCode status = U_ZERO_ERROR;  | 
106  |  |         // Apply the entire script of the character.  | 
107  | 0  |         int32_t script = u_getIntPropertyValue(c, UCHAR_SCRIPT);  | 
108  | 0  |         fHandled->applyIntPropertyValue(UCHAR_SCRIPT, script, status);  | 
109  | 0  |     }  | 
110  | 0  | }  | 
111  |  |  | 
112  |  | /*  | 
113  |  |  ******************************************************************  | 
114  |  |  */  | 
115  |  |  | 
116  | 0  | ICULanguageBreakFactory::ICULanguageBreakFactory(UErrorCode &/*status*/) { | 
117  | 0  |     fEngines = nullptr;  | 
118  | 0  | }  | 
119  |  |  | 
120  | 0  | ICULanguageBreakFactory::~ICULanguageBreakFactory() { | 
121  | 0  |     delete fEngines;  | 
122  | 0  | }  | 
123  |  |  | 
124  | 0  | void ICULanguageBreakFactory::ensureEngines(UErrorCode& status) { | 
125  | 0  |     static UMutex gBreakEngineMutex;  | 
126  | 0  |     Mutex m(&gBreakEngineMutex);  | 
127  | 0  |     if (fEngines == nullptr) { | 
128  | 0  |         LocalPointer<UStack>  engines(new UStack(uprv_deleteUObject, nullptr, status), status);  | 
129  | 0  |         if (U_SUCCESS(status)) { | 
130  | 0  |             fEngines = engines.orphan();  | 
131  | 0  |         }  | 
132  | 0  |     }  | 
133  | 0  | }  | 
134  |  |  | 
135  |  | const LanguageBreakEngine *  | 
136  | 0  | ICULanguageBreakFactory::getEngineFor(UChar32 c, const char* locale) { | 
137  | 0  |     const LanguageBreakEngine *lbe = nullptr;  | 
138  | 0  |     UErrorCode  status = U_ZERO_ERROR;  | 
139  | 0  |     ensureEngines(status);  | 
140  | 0  |     if (U_FAILURE(status) ) { | 
141  |  |         // Note: no way to return error code to caller.  | 
142  | 0  |         return nullptr;  | 
143  | 0  |     }  | 
144  |  |  | 
145  | 0  |     static UMutex gBreakEngineMutex;  | 
146  | 0  |     Mutex m(&gBreakEngineMutex);  | 
147  | 0  |     int32_t i = fEngines->size();  | 
148  | 0  |     while (--i >= 0) { | 
149  | 0  |         lbe = static_cast<const LanguageBreakEngine*>(fEngines->elementAt(i));  | 
150  | 0  |         if (lbe != nullptr && lbe->handles(c, locale)) { | 
151  | 0  |             return lbe;  | 
152  | 0  |         }  | 
153  | 0  |     }  | 
154  |  |  | 
155  |  |     // We didn't find an engine. Create one.  | 
156  | 0  |     lbe = loadEngineFor(c, locale);  | 
157  | 0  |     if (lbe != nullptr) { | 
158  | 0  |         fEngines->push((void *)lbe, status);  | 
159  | 0  |     }  | 
160  | 0  |     return U_SUCCESS(status) ? lbe : nullptr;  | 
161  | 0  | }  | 
162  |  |  | 
163  |  | const LanguageBreakEngine *  | 
164  | 0  | ICULanguageBreakFactory::loadEngineFor(UChar32 c, const char*) { | 
165  | 0  |     UErrorCode status = U_ZERO_ERROR;  | 
166  | 0  |     UScriptCode code = uscript_getScript(c, &status);  | 
167  | 0  |     if (U_SUCCESS(status)) { | 
168  | 0  |         const LanguageBreakEngine *engine = nullptr;  | 
169  |  |         // Try to use LSTM first  | 
170  | 0  |         const LSTMData *data = CreateLSTMDataForScript(code, status);  | 
171  | 0  |         if (U_SUCCESS(status)) { | 
172  | 0  |             if (data != nullptr) { | 
173  | 0  |                 engine = CreateLSTMBreakEngine(code, data, status);  | 
174  | 0  |                 if (U_SUCCESS(status) && engine != nullptr) { | 
175  | 0  |                     return engine;  | 
176  | 0  |                 }  | 
177  | 0  |                 if (engine != nullptr) { | 
178  | 0  |                     delete engine;  | 
179  | 0  |                     engine = nullptr;  | 
180  | 0  |                 } else { | 
181  | 0  |                     DeleteLSTMData(data);  | 
182  | 0  |                 }  | 
183  | 0  |             }  | 
184  | 0  |         }  | 
185  | 0  |         status = U_ZERO_ERROR;  // fallback to dictionary based  | 
186  | 0  |         DictionaryMatcher *m = loadDictionaryMatcherFor(code);  | 
187  | 0  |         if (m != nullptr) { | 
188  | 0  |             switch(code) { | 
189  | 0  |             case USCRIPT_THAI:  | 
190  | 0  |                 engine = new ThaiBreakEngine(m, status);  | 
191  | 0  |                 break;  | 
192  | 0  |             case USCRIPT_LAO:  | 
193  | 0  |                 engine = new LaoBreakEngine(m, status);  | 
194  | 0  |                 break;  | 
195  | 0  |             case USCRIPT_MYANMAR:  | 
196  | 0  |                 engine = new BurmeseBreakEngine(m, status);  | 
197  | 0  |                 break;  | 
198  | 0  |             case USCRIPT_KHMER:  | 
199  | 0  |                 engine = new KhmerBreakEngine(m, status);  | 
200  | 0  |                 break;  | 
201  |  |  | 
202  | 0  | #if !UCONFIG_NO_NORMALIZATION  | 
203  |  |                 // CJK not available w/o normalization  | 
204  | 0  |             case USCRIPT_HANGUL:  | 
205  | 0  |                 engine = new CjkBreakEngine(m, kKorean, status);  | 
206  | 0  |                 break;  | 
207  |  |  | 
208  |  |             // use same BreakEngine and dictionary for both Chinese and Japanese  | 
209  | 0  |             case USCRIPT_HIRAGANA:  | 
210  | 0  |             case USCRIPT_KATAKANA:  | 
211  | 0  |             case USCRIPT_HAN:  | 
212  | 0  |                 engine = new CjkBreakEngine(m, kChineseJapanese, status);  | 
213  | 0  |                 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  | 0  | #endif  | 
228  |  |  | 
229  | 0  |             default:  | 
230  | 0  |                 break;  | 
231  | 0  |             }  | 
232  | 0  |             if (engine == nullptr) { | 
233  | 0  |                 delete m;  | 
234  | 0  |             }  | 
235  | 0  |             else if (U_FAILURE(status)) { | 
236  | 0  |                 delete engine;  | 
237  | 0  |                 engine = nullptr;  | 
238  | 0  |             }  | 
239  | 0  |             return engine;  | 
240  | 0  |         }  | 
241  | 0  |     }  | 
242  | 0  |     return nullptr;  | 
243  | 0  | }  | 
244  |  |  | 
245  |  | DictionaryMatcher *  | 
246  | 0  | ICULanguageBreakFactory::loadDictionaryMatcherFor(UScriptCode script) {  | 
247  | 0  |     UErrorCode status = U_ZERO_ERROR;  | 
248  |  |     // open root from brkitr tree.  | 
249  | 0  |     UResourceBundle *b = ures_open(U_ICUDATA_BRKITR, "", &status);  | 
250  | 0  |     b = ures_getByKeyWithFallback(b, "dictionaries", b, &status);  | 
251  | 0  |     int32_t dictnlength = 0;  | 
252  | 0  |     const char16_t *dictfname =  | 
253  | 0  |         ures_getStringByKeyWithFallback(b, uscript_getShortName(script), &dictnlength, &status);  | 
254  | 0  |     if (U_FAILURE(status)) { | 
255  | 0  |         ures_close(b);  | 
256  | 0  |         return nullptr;  | 
257  | 0  |     }  | 
258  | 0  |     CharString dictnbuf;  | 
259  | 0  |     CharString ext;  | 
260  | 0  |     const char16_t *extStart = u_memrchr(dictfname, 0x002e, dictnlength);  // last dot  | 
261  | 0  |     if (extStart != nullptr) { | 
262  | 0  |         int32_t len = static_cast<int32_t>(extStart - dictfname);  | 
263  | 0  |         ext.appendInvariantChars(UnicodeString(false, extStart + 1, dictnlength - len - 1), status);  | 
264  | 0  |         dictnlength = len;  | 
265  | 0  |     }  | 
266  | 0  |     dictnbuf.appendInvariantChars(UnicodeString(false, dictfname, dictnlength), status);  | 
267  | 0  |     ures_close(b);  | 
268  |  | 
  | 
269  | 0  |     UDataMemory *file = udata_open(U_ICUDATA_BRKITR, ext.data(), dictnbuf.data(), &status);  | 
270  | 0  |     if (U_SUCCESS(status)) { | 
271  |  |         // build trie  | 
272  | 0  |         const uint8_t* data = static_cast<const uint8_t*>(udata_getMemory(file));  | 
273  | 0  |         const int32_t* indexes = reinterpret_cast<const int32_t*>(data);  | 
274  | 0  |         const int32_t offset = indexes[DictionaryData::IX_STRING_TRIE_OFFSET];  | 
275  | 0  |         const int32_t trieType = indexes[DictionaryData::IX_TRIE_TYPE] & DictionaryData::TRIE_TYPE_MASK;  | 
276  | 0  |         DictionaryMatcher *m = nullptr;  | 
277  | 0  |         if (trieType == DictionaryData::TRIE_TYPE_BYTES) { | 
278  | 0  |             const int32_t transform = indexes[DictionaryData::IX_TRANSFORM];  | 
279  | 0  |             const char* characters = reinterpret_cast<const char*>(data + offset);  | 
280  | 0  |             m = new BytesDictionaryMatcher(characters, transform, file);  | 
281  | 0  |         }  | 
282  | 0  |         else if (trieType == DictionaryData::TRIE_TYPE_UCHARS) { | 
283  | 0  |             const char16_t* characters = reinterpret_cast<const char16_t*>(data + offset);  | 
284  | 0  |             m = new UCharsDictionaryMatcher(characters, file);  | 
285  | 0  |         }  | 
286  | 0  |         if (m == nullptr) { | 
287  |  |             // no matcher exists to take ownership - either we are an invalid   | 
288  |  |             // type or memory allocation failed  | 
289  | 0  |             udata_close(file);  | 
290  | 0  |         }  | 
291  | 0  |         return m;  | 
292  | 0  |     } else if (dictfname != nullptr) { | 
293  |  |         // we don't have a dictionary matcher.  | 
294  |  |         // returning nullptr here will cause us to fail to find a dictionary break engine, as expected  | 
295  | 0  |         status = U_ZERO_ERROR;  | 
296  | 0  |         return nullptr;  | 
297  | 0  |     }  | 
298  | 0  |     return nullptr;  | 
299  | 0  | }  | 
300  |  |  | 
301  |  |  | 
302  |  | void ICULanguageBreakFactory::addExternalEngine(  | 
303  | 0  |         ExternalBreakEngine* external, UErrorCode& status) { | 
304  | 0  |     LocalPointer<ExternalBreakEngine> engine(external, status);  | 
305  | 0  |     ensureEngines(status);  | 
306  | 0  |     LocalPointer<BreakEngineWrapper> wrapper(  | 
307  | 0  |         new BreakEngineWrapper(engine.orphan(), status), status);  | 
308  | 0  |     static UMutex gBreakEngineMutex;  | 
309  | 0  |     Mutex m(&gBreakEngineMutex);  | 
310  | 0  |     fEngines->push(wrapper.getAlias(), status);  | 
311  | 0  |     wrapper.orphan();  | 
312  | 0  | }  | 
313  |  |  | 
314  |  | BreakEngineWrapper::BreakEngineWrapper(  | 
315  | 0  |     ExternalBreakEngine* engine, UErrorCode &status) : delegate(engine, status) { | 
316  | 0  | }  | 
317  |  |  | 
318  | 0  | BreakEngineWrapper::~BreakEngineWrapper() { | 
319  | 0  | }  | 
320  |  |  | 
321  | 0  | UBool BreakEngineWrapper::handles(UChar32 c, const char* locale) const { | 
322  | 0  |     return delegate->isFor(c, locale);  | 
323  | 0  | }  | 
324  |  |  | 
325  |  | int32_t BreakEngineWrapper::findBreaks(  | 
326  |  |     UText *text,  | 
327  |  |     int32_t startPos,  | 
328  |  |     int32_t endPos,  | 
329  |  |     UVector32 &foundBreaks,  | 
330  |  |     UBool /* isPhraseBreaking */,  | 
331  | 0  |     UErrorCode &status) const { | 
332  | 0  |     if (U_FAILURE(status)) return 0;  | 
333  | 0  |     int32_t result = 0;  | 
334  |  |  | 
335  |  |     // Find the span of characters included in the set.  | 
336  |  |     //   The span to break begins at the current position in the text, and  | 
337  |  |     //   extends towards the start or end of the text, depending on 'reverse'.  | 
338  |  | 
  | 
339  | 0  |     utext_setNativeIndex(text, startPos);  | 
340  | 0  |     int32_t start = static_cast<int32_t>(utext_getNativeIndex(text));  | 
341  | 0  |     int32_t current;  | 
342  | 0  |     int32_t rangeStart;  | 
343  | 0  |     int32_t rangeEnd;  | 
344  | 0  |     UChar32 c = utext_current32(text);  | 
345  | 0  |     while ((current = static_cast<int32_t>(utext_getNativeIndex(text))) < endPos && delegate->handles(c)) { | 
346  | 0  |         utext_next32(text);         // TODO:  recast loop for postincrement  | 
347  | 0  |         c = utext_current32(text);  | 
348  | 0  |     }  | 
349  | 0  |     rangeStart = start;  | 
350  | 0  |     rangeEnd = current;  | 
351  | 0  |     int32_t beforeSize = foundBreaks.size();  | 
352  | 0  |     int32_t additionalCapacity = rangeEnd - rangeStart + 1;  | 
353  |  |     // enlarge to contains (rangeEnd-rangeStart+1) more items  | 
354  | 0  |     foundBreaks.ensureCapacity(beforeSize+additionalCapacity, status);  | 
355  | 0  |     if (U_FAILURE(status)) return 0;  | 
356  | 0  |     foundBreaks.setSize(beforeSize + beforeSize+additionalCapacity);  | 
357  | 0  |     result = delegate->fillBreaks(text, rangeStart, rangeEnd, foundBreaks.getBuffer()+beforeSize,  | 
358  | 0  |                                   additionalCapacity, status);  | 
359  | 0  |     if (U_FAILURE(status)) return 0;  | 
360  | 0  |     foundBreaks.setSize(beforeSize + result);  | 
361  | 0  |     utext_setNativeIndex(text, current);  | 
362  | 0  |     return result;  | 
363  | 0  | }  | 
364  |  |  | 
365  |  | U_NAMESPACE_END  | 
366  |  |  | 
367  |  | #endif /* #if !UCONFIG_NO_BREAK_ITERATION */  |