Bump version to 6.0-36
[LibreOffice.git] / i18npool / source / breakiterator / breakiteratorImpl.cxx
blob24ca264e708b5fb4f757dc9c3a0bc787fdf24755
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #include <config_locales.h>
21 #include <breakiteratorImpl.hxx>
22 #include <cppuhelper/supportsservice.hxx>
23 #include <unicode/uchar.h>
24 #include <i18nutil/unicode.hxx>
25 #include <rtl/ustrbuf.hxx>
27 using namespace ::com::sun::star;
28 using namespace ::com::sun::star::uno;
29 using namespace ::com::sun::star::i18n;
30 using namespace ::com::sun::star::lang;
32 namespace i18npool {
34 BreakIteratorImpl::BreakIteratorImpl( const Reference < XComponentContext >& rxContext ) : m_xContext( rxContext )
38 BreakIteratorImpl::BreakIteratorImpl()
42 BreakIteratorImpl::~BreakIteratorImpl()
44 // Clear lookuptable
45 for (lookupTableItem* p : lookupTable)
46 delete p;
47 lookupTable.clear();
50 #define LBI getLocaleSpecificBreakIterator(rLocale)
52 sal_Int32 SAL_CALL BreakIteratorImpl::nextCharacters( const OUString& Text, sal_Int32 nStartPos,
53 const Locale &rLocale, sal_Int16 nCharacterIteratorMode, sal_Int32 nCount, sal_Int32& nDone )
55 if (nCount < 0) throw RuntimeException();
57 return LBI->nextCharacters( Text, nStartPos, rLocale, nCharacterIteratorMode, nCount, nDone);
60 sal_Int32 SAL_CALL BreakIteratorImpl::previousCharacters( const OUString& Text, sal_Int32 nStartPos,
61 const Locale& rLocale, sal_Int16 nCharacterIteratorMode, sal_Int32 nCount, sal_Int32& nDone )
63 if (nCount < 0) throw RuntimeException();
65 return LBI->previousCharacters( Text, nStartPos, rLocale, nCharacterIteratorMode, nCount, nDone);
68 #define isZWSP(c) (ch == 0x200B)
70 static sal_Int32 skipSpace(const OUString& Text, sal_Int32 nPos, sal_Int32 len, sal_Int16 rWordType, bool bDirection)
72 sal_uInt32 ch=0;
73 sal_Int32 pos=nPos;
74 switch (rWordType) {
75 case WordType::ANYWORD_IGNOREWHITESPACES:
76 if (bDirection)
77 while (nPos < len && (u_isWhitespace(ch = Text.iterateCodePoints(&pos)) || isZWSP(ch))) nPos=pos;
78 else
79 while (nPos > 0 && (u_isWhitespace(ch = Text.iterateCodePoints(&pos, -1)) || isZWSP(ch))) nPos=pos;
80 break;
81 case WordType::DICTIONARY_WORD:
82 if (bDirection)
83 while (nPos < len && (u_isWhitespace(ch = Text.iterateCodePoints(&pos)) || isZWSP(ch) ||
84 ! (ch == 0x002E || u_isalnum(ch)))) nPos=pos;
85 else
86 while (nPos > 0 && (u_isWhitespace(ch = Text.iterateCodePoints(&pos, -1)) || isZWSP(ch) ||
87 ! (ch == 0x002E || u_isalnum(ch)))) nPos=pos;
88 break;
89 case WordType::WORD_COUNT:
90 if (bDirection)
91 while (nPos < len && (u_isUWhiteSpace(ch = Text.iterateCodePoints(&pos)) || isZWSP(ch))) nPos=pos;
92 else
93 while (nPos > 0 && (u_isUWhiteSpace(ch = Text.iterateCodePoints(&pos, -1)) || isZWSP(ch))) nPos=pos;
94 break;
96 return nPos;
99 Boundary SAL_CALL BreakIteratorImpl::nextWord( const OUString& Text, sal_Int32 nStartPos,
100 const Locale& rLocale, sal_Int16 rWordType )
102 sal_Int32 len = Text.getLength();
103 if( nStartPos < 0 || len == 0 )
104 result.endPos = result.startPos = 0;
105 else if (nStartPos >= len)
106 result.endPos = result.startPos = len;
107 else {
108 result = LBI->nextWord(Text, nStartPos, rLocale, rWordType);
110 nStartPos = skipSpace(Text, result.startPos, len, rWordType, true);
112 if ( nStartPos != result.startPos) {
113 if( nStartPos >= len )
114 result.startPos = result.endPos = len;
115 else {
116 result = LBI->getWordBoundary(Text, nStartPos, rLocale, rWordType, true);
117 // i88041: avoid startPos goes back to nStartPos when switching between Latin and CJK scripts
118 if (result.startPos < nStartPos) result.startPos = nStartPos;
122 return result;
125 static inline bool SAL_CALL isCJK( const Locale& rLocale ) {
126 return rLocale.Language == "zh" || rLocale.Language == "ja" || rLocale.Language == "ko";
129 Boundary SAL_CALL BreakIteratorImpl::previousWord( const OUString& Text, sal_Int32 nStartPos,
130 const Locale& rLocale, sal_Int16 rWordType)
132 sal_Int32 len = Text.getLength();
133 if( nStartPos <= 0 || len == 0 ) {
134 result.endPos = result.startPos = 0;
135 return result;
136 } else if (nStartPos > len) {
137 result.endPos = result.startPos = len;
138 return result;
141 sal_Int32 nPos = skipSpace(Text, nStartPos, len, rWordType, false);
143 // if some spaces are skipped, and the script type is Asian with no CJK rLocale, we have to return
144 // (nStartPos, -1) for caller to send correct rLocale for loading correct dictionary.
145 result.startPos = nPos;
146 if (nPos != nStartPos && nPos > 0 && !isCJK(rLocale) && getScriptClass(Text.iterateCodePoints(&nPos, -1)) == ScriptType::ASIAN) {
147 result.endPos = -1;
148 return result;
151 return LBI->previousWord(Text, result.startPos, rLocale, rWordType);
155 Boundary SAL_CALL BreakIteratorImpl::getWordBoundary( const OUString& Text, sal_Int32 nPos, const Locale& rLocale,
156 sal_Int16 rWordType, sal_Bool bDirection )
158 sal_Int32 len = Text.getLength();
159 if( nPos < 0 || len == 0 )
160 result.endPos = result.startPos = 0;
161 else if (nPos > len)
162 result.endPos = result.startPos = len;
163 else {
164 sal_Int32 next, prev;
165 next = skipSpace(Text, nPos, len, rWordType, true);
166 prev = skipSpace(Text, nPos, len, rWordType, false);
167 if (prev == 0 && next == len) {
168 result.endPos = result.startPos = nPos;
169 } else if (prev == 0 && ! bDirection) {
170 result.endPos = result.startPos = 0;
171 } else if (next == len && bDirection) {
172 result.endPos = result.startPos = len;
173 } else {
174 if (next != prev) {
175 if (next == nPos && next != len)
176 bDirection = true;
177 else if (prev == nPos && prev != 0)
178 bDirection = false;
179 else
180 nPos = bDirection ? next : prev;
182 result = LBI->getWordBoundary(Text, nPos, rLocale, rWordType, bDirection);
185 return result;
188 sal_Bool SAL_CALL BreakIteratorImpl::isBeginWord( const OUString& Text, sal_Int32 nPos,
189 const Locale& rLocale, sal_Int16 rWordType )
191 sal_Int32 len = Text.getLength();
193 if (nPos < 0 || nPos >= len) return false;
195 sal_Int32 tmp = skipSpace(Text, nPos, len, rWordType, true);
197 if (tmp != nPos) return false;
199 result = getWordBoundary(Text, nPos, rLocale, rWordType, true);
201 return result.startPos == nPos;
204 sal_Bool SAL_CALL BreakIteratorImpl::isEndWord( const OUString& Text, sal_Int32 nPos,
205 const Locale& rLocale, sal_Int16 rWordType )
207 sal_Int32 len = Text.getLength();
209 if (nPos <= 0 || nPos > len) return false;
211 sal_Int32 tmp = skipSpace(Text, nPos, len, rWordType, false);
213 if (tmp != nPos) return false;
215 result = getWordBoundary(Text, nPos, rLocale, rWordType, false);
217 return result.endPos == nPos;
220 sal_Int32 SAL_CALL BreakIteratorImpl::beginOfSentence( const OUString& Text, sal_Int32 nStartPos,
221 const Locale &rLocale )
223 if (nStartPos < 0 || nStartPos > Text.getLength())
224 return -1;
225 if (Text.isEmpty()) return 0;
226 return LBI->beginOfSentence(Text, nStartPos, rLocale);
229 sal_Int32 SAL_CALL BreakIteratorImpl::endOfSentence( const OUString& Text, sal_Int32 nStartPos,
230 const Locale &rLocale )
232 if (nStartPos < 0 || nStartPos > Text.getLength())
233 return -1;
234 if (Text.isEmpty()) return 0;
235 return LBI->endOfSentence(Text, nStartPos, rLocale);
238 LineBreakResults SAL_CALL BreakIteratorImpl::getLineBreak( const OUString& Text, sal_Int32 nStartPos,
239 const Locale& rLocale, sal_Int32 nMinBreakPos, const LineBreakHyphenationOptions& hOptions,
240 const LineBreakUserOptions& bOptions )
242 return LBI->getLineBreak(Text, nStartPos, rLocale, nMinBreakPos, hOptions, bOptions);
245 sal_Int16 SAL_CALL BreakIteratorImpl::getScriptType( const OUString& Text, sal_Int32 nPos )
247 return (nPos < 0 || nPos >= Text.getLength()) ? ScriptType::WEAK :
248 getScriptClass(Text.iterateCodePoints(&nPos, 0));
252 /** Increments/decrements position first, then obtains character.
253 @return current position, may be -1 or text length if string was consumed.
255 static sal_Int32 SAL_CALL iterateCodePoints(const OUString& Text, sal_Int32 &nStartPos, sal_Int32 inc, sal_uInt32& ch) {
256 sal_Int32 nLen = Text.getLength();
257 if (nStartPos + inc < 0 || nStartPos + inc >= nLen) {
258 ch = 0;
259 nStartPos = nStartPos + inc < 0 ? -1 : nLen;
260 } else {
261 ch = Text.iterateCodePoints(&nStartPos, inc);
262 // Fix for #i80436#.
263 // erAck: 2009-06-30T21:52+0200 This logic looks somewhat
264 // suspicious as if it cures a symptom.. anyway, had to add
265 // nStartPos < Text.getLength() to silence the (correct) assertion
266 // in rtl_uString_iterateCodePoints() if Text was one character
267 // (codepoint) only, made up of a surrogate pair.
268 //if (inc > 0 && nStartPos < Text.getLength())
269 // ch = Text.iterateCodePoints(&nStartPos, 0);
270 // With surrogates, nStartPos may actually point behind string
271 // now, even if inc is only +1
272 if (inc > 0)
273 ch = (nStartPos < nLen ? Text.iterateCodePoints(&nStartPos, 0) : 0);
275 return nStartPos;
279 sal_Int32 SAL_CALL BreakIteratorImpl::beginOfScript( const OUString& Text,
280 sal_Int32 nStartPos, sal_Int16 ScriptType )
282 if (nStartPos < 0 || nStartPos >= Text.getLength())
283 return -1;
285 if(ScriptType != getScriptClass(Text.iterateCodePoints(&nStartPos, 0)))
286 return -1;
288 if (nStartPos == 0) return 0;
289 sal_uInt32 ch=0;
290 while (iterateCodePoints(Text, nStartPos, -1, ch) >= 0 && ScriptType == getScriptClass(ch)) {
291 if (nStartPos == 0) return 0;
294 return iterateCodePoints(Text, nStartPos, 1, ch);
297 sal_Int32 SAL_CALL BreakIteratorImpl::endOfScript( const OUString& Text,
298 sal_Int32 nStartPos, sal_Int16 ScriptType )
300 if (nStartPos < 0 || nStartPos >= Text.getLength())
301 return -1;
303 if(ScriptType != getScriptClass(Text.iterateCodePoints(&nStartPos, 0)))
304 return -1;
306 sal_Int32 strLen = Text.getLength();
307 sal_uInt32 ch=0;
308 while(iterateCodePoints(Text, nStartPos, 1, ch) < strLen ) {
309 sal_Int16 currentCharScriptType = getScriptClass(ch);
310 if(ScriptType != currentCharScriptType && currentCharScriptType != ScriptType::WEAK)
311 break;
313 return nStartPos;
316 sal_Int32 SAL_CALL BreakIteratorImpl::previousScript( const OUString& Text,
317 sal_Int32 nStartPos, sal_Int16 ScriptType )
319 if (nStartPos < 0)
320 return -1;
321 if (nStartPos > Text.getLength())
322 nStartPos = Text.getLength();
324 sal_Int16 numberOfChange = (ScriptType == getScriptClass(Text.iterateCodePoints(&nStartPos, 0))) ? 3 : 2;
326 sal_uInt32 ch=0;
327 while (numberOfChange > 0 && iterateCodePoints(Text, nStartPos, -1, ch) >= 0) {
328 if (((numberOfChange % 2) == 0) != (ScriptType != getScriptClass(ch)))
329 numberOfChange--;
330 else if (nStartPos == 0) {
331 return -1;
334 return numberOfChange == 0 ? iterateCodePoints(Text, nStartPos, 1, ch) : -1;
337 sal_Int32 SAL_CALL BreakIteratorImpl::nextScript( const OUString& Text, sal_Int32 nStartPos,
338 sal_Int16 ScriptType )
341 if (nStartPos < 0)
342 nStartPos = 0;
343 sal_Int32 strLen = Text.getLength();
344 if (nStartPos >= strLen)
345 return -1;
347 sal_Int16 numberOfChange = (ScriptType == getScriptClass(Text.iterateCodePoints(&nStartPos, 0))) ? 2 : 1;
349 sal_uInt32 ch=0;
350 while (numberOfChange > 0 && iterateCodePoints(Text, nStartPos, 1, ch) < strLen) {
351 sal_Int16 currentCharScriptType = getScriptClass(ch);
352 if ((numberOfChange == 1) ? (ScriptType == currentCharScriptType) :
353 (ScriptType != currentCharScriptType && currentCharScriptType != ScriptType::WEAK))
354 numberOfChange--;
356 return numberOfChange == 0 ? nStartPos : -1;
359 sal_Int32 SAL_CALL BreakIteratorImpl::beginOfCharBlock( const OUString& Text, sal_Int32 nStartPos,
360 const Locale& /*rLocale*/, sal_Int16 CharType )
362 if (CharType == CharType::ANY_CHAR) return 0;
363 if (nStartPos < 0 || nStartPos >= Text.getLength()) return -1;
364 if (CharType != (sal_Int16)u_charType( Text.iterateCodePoints(&nStartPos, 0))) return -1;
366 sal_Int32 nPos=nStartPos;
367 while(nStartPos > 0 && CharType == (sal_Int16)u_charType(Text.iterateCodePoints(&nPos, -1))) { nStartPos=nPos; }
368 return nStartPos; // begin of char block is inclusive
371 sal_Int32 SAL_CALL BreakIteratorImpl::endOfCharBlock( const OUString& Text, sal_Int32 nStartPos,
372 const Locale& /*rLocale*/, sal_Int16 CharType )
374 sal_Int32 strLen = Text.getLength();
376 if (CharType == CharType::ANY_CHAR) return strLen; // end of char block is exclusive
377 if (nStartPos < 0 || nStartPos >= strLen) return -1;
378 if (CharType != (sal_Int16)u_charType(Text.iterateCodePoints(&nStartPos, 0))) return -1;
380 sal_uInt32 ch=0;
381 while(iterateCodePoints(Text, nStartPos, 1, ch) < strLen && CharType == (sal_Int16)u_charType(ch)) {}
382 return nStartPos; // end of char block is exclusive
385 sal_Int32 SAL_CALL BreakIteratorImpl::nextCharBlock( const OUString& Text, sal_Int32 nStartPos,
386 const Locale& /*rLocale*/, sal_Int16 CharType )
388 if (CharType == CharType::ANY_CHAR) return -1;
389 if (nStartPos < 0 || nStartPos >= Text.getLength()) return -1;
391 sal_Int16 numberOfChange = (CharType == (sal_Int16)u_charType(Text.iterateCodePoints(&nStartPos, 0))) ? 2 : 1;
392 sal_Int32 strLen = Text.getLength();
394 sal_uInt32 ch=0;
395 while (numberOfChange > 0 && iterateCodePoints(Text, nStartPos, 1, ch) < strLen) {
396 if ((CharType != (sal_Int16)u_charType(ch)) != (numberOfChange == 1))
397 numberOfChange--;
399 return numberOfChange == 0 ? nStartPos : -1;
402 sal_Int32 SAL_CALL BreakIteratorImpl::previousCharBlock( const OUString& Text, sal_Int32 nStartPos,
403 const Locale& /*rLocale*/, sal_Int16 CharType )
405 if(CharType == CharType::ANY_CHAR) return -1;
406 if (nStartPos < 0 || nStartPos >= Text.getLength()) return -1;
408 sal_Int16 numberOfChange = (CharType == (sal_Int16)u_charType(Text.iterateCodePoints(&nStartPos, 0))) ? 3 : 2;
410 sal_uInt32 ch=0;
411 while (numberOfChange > 0 && iterateCodePoints(Text, nStartPos, -1, ch) >= 0) {
412 if (((numberOfChange % 2) == 0) != (CharType != (sal_Int16)u_charType(ch)))
413 numberOfChange--;
414 if (nStartPos == 0 && numberOfChange > 0) {
415 numberOfChange--;
416 if (numberOfChange == 0) return nStartPos;
419 return numberOfChange == 0 ? iterateCodePoints(Text, nStartPos, 1, ch) : -1;
423 sal_Int16 SAL_CALL BreakIteratorImpl::getWordType( const OUString& /*Text*/,
424 sal_Int32 /*nPos*/, const Locale& /*rLocale*/ )
426 return 0;
429 namespace
431 sal_Int16 getScriptClassByUAX24Script(sal_uInt32 currentChar)
433 int32_t script = u_getIntPropertyValue(currentChar, UCHAR_SCRIPT);
434 return unicode::getScriptClassFromUScriptCode(static_cast<UScriptCode>(script));
437 struct UBlock2Script
439 UBlockCode from;
440 UBlockCode to;
441 sal_Int16 script;
444 static const UBlock2Script scriptList[] =
446 {UBLOCK_NO_BLOCK, UBLOCK_NO_BLOCK, ScriptType::WEAK},
447 {UBLOCK_BASIC_LATIN, UBLOCK_SPACING_MODIFIER_LETTERS, ScriptType::LATIN},
448 {UBLOCK_GREEK, UBLOCK_ARMENIAN, ScriptType::LATIN},
449 {UBLOCK_HEBREW, UBLOCK_MYANMAR, ScriptType::COMPLEX},
450 {UBLOCK_GEORGIAN, UBLOCK_GEORGIAN, ScriptType::LATIN},
451 {UBLOCK_HANGUL_JAMO, UBLOCK_HANGUL_JAMO, ScriptType::ASIAN},
452 {UBLOCK_ETHIOPIC, UBLOCK_ETHIOPIC, ScriptType::COMPLEX},
453 {UBLOCK_CHEROKEE, UBLOCK_RUNIC, ScriptType::LATIN},
454 {UBLOCK_KHMER, UBLOCK_MONGOLIAN, ScriptType::COMPLEX},
455 {UBLOCK_LATIN_EXTENDED_ADDITIONAL, UBLOCK_GREEK_EXTENDED, ScriptType::LATIN},
456 {UBLOCK_NUMBER_FORMS, UBLOCK_NUMBER_FORMS, ScriptType::WEAK},
457 {UBLOCK_CJK_RADICALS_SUPPLEMENT, UBLOCK_HANGUL_SYLLABLES, ScriptType::ASIAN},
458 {UBLOCK_CJK_COMPATIBILITY_IDEOGRAPHS, UBLOCK_CJK_COMPATIBILITY_IDEOGRAPHS, ScriptType::ASIAN},
459 {UBLOCK_ARABIC_PRESENTATION_FORMS_A, UBLOCK_ARABIC_PRESENTATION_FORMS_A, ScriptType::COMPLEX},
460 {UBLOCK_CJK_COMPATIBILITY_FORMS, UBLOCK_CJK_COMPATIBILITY_FORMS, ScriptType::ASIAN},
461 {UBLOCK_ARABIC_PRESENTATION_FORMS_B, UBLOCK_ARABIC_PRESENTATION_FORMS_B, ScriptType::COMPLEX},
462 {UBLOCK_HALFWIDTH_AND_FULLWIDTH_FORMS, UBLOCK_HALFWIDTH_AND_FULLWIDTH_FORMS, ScriptType::ASIAN},
463 {UBLOCK_CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B, UBLOCK_CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT, ScriptType::ASIAN},
464 {UBLOCK_CJK_STROKES, UBLOCK_CJK_STROKES, ScriptType::ASIAN},
465 {UBLOCK_LATIN_EXTENDED_C, UBLOCK_LATIN_EXTENDED_D, ScriptType::LATIN}
468 #define scriptListCount SAL_N_ELEMENTS(scriptList)
470 //always sets rScriptType
472 //returns true for characters historically explicitly assigned to
473 //latin/weak/asian
475 //returns false for characters that historically implicitly assigned to
476 //weak as unknown
477 bool getCompatibilityScriptClassByBlock(sal_uInt32 currentChar, sal_Int16 &rScriptType)
479 bool bKnown = true;
480 //handle specific characters always as weak:
481 // 0x01 - this breaks a word
482 // 0x02 - this can be inside a word
483 // 0x20 & 0xA0 - Bug 102975, declare western space and non-break space as WEAK char.
484 if( 0x01 == currentChar || 0x02 == currentChar || 0x20 == currentChar || 0xA0 == currentChar)
485 rScriptType = ScriptType::WEAK;
486 // Few Spacing Modifier Letters that can be Bopomofo tonal marks.
487 else if ( 0x2CA == currentChar || 0x2CB == currentChar || 0x2C7 == currentChar || 0x2D9 == currentChar )
488 rScriptType = ScriptType::WEAK;
489 // workaround for Coptic
490 else if ( 0x2C80 <= currentChar && 0x2CE3 >= currentChar)
491 rScriptType = ScriptType::LATIN;
492 else
494 UBlockCode block=ublock_getCode(currentChar);
495 size_t i = 0;
496 while (i < scriptListCount)
498 if (block <= scriptList[i].to)
499 break;
500 ++i;
502 if (i < scriptListCount && block >= scriptList[i].from)
503 rScriptType = scriptList[i].script;
504 else
506 rScriptType = ScriptType::WEAK;
507 bKnown = false;
510 return bKnown;
514 sal_Int16 BreakIteratorImpl::getScriptClass(sal_uInt32 currentChar)
516 static sal_uInt32 lastChar = 0;
517 static sal_Int16 nRet = ScriptType::WEAK;
519 if (currentChar != lastChar)
521 lastChar = currentChar;
523 if (!getCompatibilityScriptClassByBlock(currentChar, nRet))
524 nRet = getScriptClassByUAX24Script(currentChar);
527 return nRet;
530 bool SAL_CALL BreakIteratorImpl::createLocaleSpecificBreakIterator(const OUString& aLocaleName)
532 // to share service between same Language but different Country code, like zh_CN and zh_TW
533 for (lookupTableItem* listItem : lookupTable) {
534 if (aLocaleName == listItem->aLocale.Language) {
535 xBI = listItem->xBI;
536 return true;
540 #if !WITH_LOCALE_ALL && !WITH_LOCALE_ja
541 if (aLocaleName == "ja")
542 return false;
543 #endif
544 #if !WITH_LOCALE_ALL && !WITH_LOCALE_zh
545 if (aLocaleName == "zh" || aLocaleName == "zh_TW")
546 return false;
547 #endif
548 #if !WITH_LOCALE_ALL && !WITH_LOCALE_ko
549 if (aLocaleName == "ko")
550 return false;
551 #endif
552 #if !WITH_LOCALE_ALL && !WITH_LOCALE_th
553 if (aLocaleName == "th")
554 return false;
555 #endif
557 Reference < uno::XInterface > xI = m_xContext->getServiceManager()->createInstanceWithContext(
558 "com.sun.star.i18n.BreakIterator_" + aLocaleName, m_xContext);
560 if ( xI.is() ) {
561 xBI.set(xI, UNO_QUERY);
562 if (xBI.is()) {
563 lookupTable.push_back(new lookupTableItem(Locale(aLocaleName, aLocaleName, aLocaleName), xBI));
564 return true;
567 return false;
570 Reference < XBreakIterator > SAL_CALL
571 BreakIteratorImpl::getLocaleSpecificBreakIterator(const Locale& rLocale)
573 if (xBI.is() && rLocale == aLocale)
574 return xBI;
575 else if (m_xContext.is()) {
576 aLocale = rLocale;
578 for (lookupTableItem* listItem : lookupTable) {
579 if (rLocale == listItem->aLocale)
580 return xBI = listItem->xBI;
583 sal_Unicode under = '_';
585 sal_Int32 l = rLocale.Language.getLength();
586 sal_Int32 c = rLocale.Country.getLength();
587 sal_Int32 v = rLocale.Variant.getLength();
588 OUStringBuffer aBuf(l+c+v+3);
590 if ((l > 0 && c > 0 && v > 0 &&
591 // load service with name <base>_<lang>_<country>_<variant>
592 createLocaleSpecificBreakIterator(aBuf.append(rLocale.Language).append(under).append(
593 rLocale.Country).append(under).append(rLocale.Variant).makeStringAndClear())) ||
594 (l > 0 && c > 0 &&
595 // load service with name <base>_<lang>_<country>
596 createLocaleSpecificBreakIterator(aBuf.append(rLocale.Language).append(under).append(
597 rLocale.Country).makeStringAndClear())) ||
598 (l > 0 && c > 0 && rLocale.Language == "zh" &&
599 (rLocale.Country == "HK" ||
600 rLocale.Country == "MO" ) &&
601 // if the country code is HK or MO, one more step to try TW.
602 createLocaleSpecificBreakIterator(aBuf.append(rLocale.Language).append(under).append(
603 "TW").makeStringAndClear())) ||
604 (l > 0 &&
605 // load service with name <base>_<lang>
606 createLocaleSpecificBreakIterator(rLocale.Language)) ||
607 // load default service with name <base>_Unicode
608 createLocaleSpecificBreakIterator("Unicode")) {
609 lookupTable.push_back( new lookupTableItem(aLocale, xBI) );
610 return xBI;
613 throw RuntimeException();
616 OUString SAL_CALL
617 BreakIteratorImpl::getImplementationName()
619 return OUString("com.sun.star.i18n.BreakIterator");
622 sal_Bool SAL_CALL
623 BreakIteratorImpl::supportsService(const OUString& rServiceName)
625 return cppu::supportsService(this, rServiceName);
628 Sequence< OUString > SAL_CALL
629 BreakIteratorImpl::getSupportedServiceNames()
631 Sequence< OUString > aRet { "com.sun.star.i18n.BreakIterator" };
632 return aRet;
637 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
638 com_sun_star_i18n_BreakIterator_get_implementation(
639 css::uno::XComponentContext *context,
640 css::uno::Sequence<css::uno::Any> const &)
642 return cppu::acquire(new i18npool::BreakIteratorImpl(context));
645 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */