bump product version to 6.3.0.0.beta1
[LibreOffice.git] / i18npool / source / indexentry / indexentrysupplier_default.cxx
blob186f3a511f59872fb36217f1524178656c1545a6
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 .
20 #include <indexentrysupplier_default.hxx>
21 #include <collatorImpl.hxx>
22 #include <localedata.hxx>
23 #include <i18nutil/unicode.hxx>
24 #include <com/sun/star/i18n/CollatorOptions.hpp>
26 using namespace ::com::sun::star;
27 using namespace ::com::sun::star::uno;
28 using namespace ::com::sun::star::i18n;
29 using namespace ::com::sun::star::lang;
31 namespace i18npool {
33 IndexEntrySupplier_Unicode::IndexEntrySupplier_Unicode(
34 const css::uno::Reference < css::uno::XComponentContext >& rxContext ) :
35 IndexEntrySupplier_Common(rxContext)
37 implementationName = "com.sun.star.i18n.IndexEntrySupplier_Unicode";
38 index.reset( new Index(rxContext) );
41 IndexEntrySupplier_Unicode::~IndexEntrySupplier_Unicode()
45 sal_Bool SAL_CALL IndexEntrySupplier_Unicode::loadAlgorithm( const lang::Locale& rLocale,
46 const OUString& rAlgorithm, sal_Int32 collatorOptions )
48 index->init(rLocale, rAlgorithm);
49 return IndexEntrySupplier_Common::loadAlgorithm(rLocale, rAlgorithm, collatorOptions);
52 OUString SAL_CALL IndexEntrySupplier_Unicode::getIndexKey( const OUString& rIndexEntry,
53 const OUString& rPhoneticEntry, const lang::Locale& rLocale )
55 return index->getIndexDescription(getEntry(rIndexEntry, rPhoneticEntry, rLocale));
58 sal_Int16 SAL_CALL IndexEntrySupplier_Unicode::compareIndexEntry(
59 const OUString& rIndexEntry1, const OUString& rPhoneticEntry1, const lang::Locale& rLocale1,
60 const OUString& rIndexEntry2, const OUString& rPhoneticEntry2, const lang::Locale& rLocale2 )
62 sal_Int16 result =
63 index->getIndexWeight(getEntry(rIndexEntry1, rPhoneticEntry1, rLocale1)) -
64 index->getIndexWeight(getEntry(rIndexEntry2, rPhoneticEntry2, rLocale2));
65 if (result == 0)
66 return IndexEntrySupplier_Common::compareIndexEntry(
67 rIndexEntry1, rPhoneticEntry1, rLocale1,
68 rIndexEntry2, rPhoneticEntry2, rLocale2);
69 return result > 0 ? 1 : -1;
72 OUString SAL_CALL IndexEntrySupplier_Unicode::getIndexCharacter( const OUString& rIndexEntry,
73 const lang::Locale& rLocale, const OUString& rAlgorithm ) {
75 if (loadAlgorithm( rLocale, rAlgorithm, CollatorOptions::CollatorOptions_IGNORE_CASE_ACCENT))
76 return index->getIndexDescription(rIndexEntry);
77 else
78 return IndexEntrySupplier_Common::getIndexCharacter(rIndexEntry, rLocale, rAlgorithm);
81 IndexTable::IndexTable()
82 : start(0)
83 , end(0)
84 , table(nullptr)
88 IndexTable::~IndexTable()
90 if (table) free(table);
93 void IndexTable::init(sal_Unicode start_, sal_Unicode end_, IndexKey const *keys, sal_Int16 key_count, Index *index)
95 start=start_;
96 end=end_;
97 table = static_cast<sal_uInt8*>(malloc((end-start+1)*sizeof(sal_uInt8)));
98 for (sal_Unicode i = start; i <= end; i++) {
99 sal_Int16 j;
100 for (j = 0; j < key_count; j++) {
101 if (keys[j].key > 0 && (i == keys[j].key || index->compare(i, keys[j].key) == 0)) {
102 table[i-start] = sal::static_int_cast<sal_uInt8>(j);
103 break;
106 if (j == key_count)
107 table[i-start] = 0xFF;
111 Index::Index(const css::uno::Reference < css::uno::XComponentContext >& rxContext)
112 : table_count(0)
113 , key_count(0)
114 , mkey_count(0)
115 , collator( new CollatorImpl(rxContext) )
119 Index::~Index()
123 sal_Int16 Index::compare(sal_Unicode c1, sal_Unicode c2)
125 return sal::static_int_cast<sal_Int16>( collator->compareString(OUString(&c1, 1), OUString(&c2, 1)) );
128 sal_Int16 Index::getIndexWeight(const OUString& rIndexEntry)
130 sal_Int32 startPos=0;
131 if (!skipping_chars.isEmpty())
132 while (skipping_chars.indexOf(rIndexEntry[startPos]) >= 0)
133 startPos++;
134 if (mkey_count > 0) {
135 for (sal_Int16 i = 0; i < mkey_count; i++) {
136 sal_Int32 len = keys[mkeys[i]].mkey.getLength();
137 if (collator->compareSubstring(rIndexEntry, startPos, len,
138 keys[mkeys[i]].mkey, 0, len) == 0)
139 return mkeys[i];
142 sal_Unicode code = startPos < rIndexEntry.getLength() ? rIndexEntry[startPos] : 0;
143 for (sal_Int16 i = 0; i < table_count; i++) {
144 if (tables[i].start <= code && code <= tables[i].end)
145 return tables[i].table[code-tables[i].start];
147 return 0xFF;
150 OUString Index::getIndexDescription(const OUString& rIndexEntry)
152 sal_Int16 wgt = getIndexWeight(rIndexEntry);
153 if (wgt < MAX_KEYS) {
154 if (!keys[wgt].desc.isEmpty())
155 return keys[wgt].desc;
156 else if (keys[wgt].key > 0)
157 return OUString(&keys[wgt].key, 1);
158 else
159 return keys[wgt].mkey;
161 sal_Int32 nPos=0;
162 sal_uInt32 indexChar=rIndexEntry.iterateCodePoints(&nPos, 0);
163 return OUString(&indexChar, 1);
166 #define LOCALE_EN lang::Locale("en", OUString(), OUString())
168 void Index::makeIndexKeys(const lang::Locale &rLocale, const OUString &algorithm)
170 OUString keyStr = LocaleDataImpl::get()->getIndexKeysByAlgorithm(rLocale, algorithm);
172 if (keyStr.isEmpty()) {
173 keyStr = LocaleDataImpl::get()->getIndexKeysByAlgorithm(LOCALE_EN,
174 LocaleDataImpl::get()->getDefaultIndexAlgorithm(LOCALE_EN));
175 if (keyStr.isEmpty())
176 throw RuntimeException();
179 sal_Int16 len = sal::static_int_cast<sal_Int16>( keyStr.getLength() );
180 mkey_count=key_count=0;
181 skipping_chars=OUString();
182 sal_Int16 i, j;
184 for (i = 0; i < len && key_count < MAX_KEYS; i++)
186 sal_Unicode curr = keyStr[i];
187 sal_Unicode close = ')';
189 if (unicode::isWhiteSpace(curr))
190 continue;
192 switch(curr) {
193 case u'-': {
194 if (key_count <= 0 || i + 1 >= len)
195 throw RuntimeException();
196 for (curr = keyStr[++i]; key_count < MAX_KEYS && keys[key_count-1].key < curr; key_count++) {
197 keys[key_count].key = keys[key_count-1].key+1;
198 keys[key_count].desc.clear();
200 break;
202 case u'[':
203 for (i++; i < len && keyStr[i] != ']'; i++) {
204 if (unicode::isWhiteSpace(keyStr[i])) {
205 continue;
206 } else if (keyStr[i] == '_') {
207 for (curr=keyStr[i-1]+1; curr <= keyStr[i+1]; curr++)
208 skipping_chars+=OUStringLiteral1(curr);
209 i+=2;
210 } else {
211 skipping_chars+=OUStringLiteral1(keyStr[i]);
214 break;
215 case u'{':
216 close = '}';
217 [[fallthrough]];
218 case u'(': {
219 if (key_count <= 0)
220 throw RuntimeException();
222 sal_Int16 end = i+1;
223 for (; end < len && keyStr[end] != close; end++) ;
225 if (end >= len) // no found
226 throw RuntimeException();
227 if (close == ')')
228 keys[key_count-1].desc = keyStr.copy(i+1, end-i-1);
229 else {
230 mkeys[mkey_count++]=key_count;
231 keys[key_count].key = 0;
232 keys[key_count].mkey = keyStr.copy(i+1, end-i-1);
233 keys[key_count++].desc.clear();
235 i=end+1;
236 break;
238 default:
239 keys[key_count].key = curr;
240 keys[key_count++].desc.clear();
241 break;
244 for (i = 0; i < mkey_count; i++) {
245 for (j=i+1; j < mkey_count; j++) {
246 if (keys[mkeys[i]].mkey.getLength() < keys[mkeys[j]].mkey.getLength()) {
247 sal_Int16 k = mkeys[i];
248 mkeys[i] = mkeys[j];
249 mkeys[j] = k;
255 void Index::init(const lang::Locale &rLocale, const OUString& algorithm)
257 makeIndexKeys(rLocale, algorithm);
259 Sequence< UnicodeScript > scriptList = LocaleDataImpl::get()->getUnicodeScripts( rLocale );
261 if (!scriptList.hasElements()) {
262 scriptList = LocaleDataImpl::get()->getUnicodeScripts(LOCALE_EN);
263 if (!scriptList.hasElements())
264 throw RuntimeException();
267 table_count = sal::static_int_cast<sal_Int16>( scriptList.getLength() );
268 if (table_count > MAX_TABLES)
269 throw RuntimeException();
271 collator->loadCollatorAlgorithm(algorithm, rLocale, CollatorOptions::CollatorOptions_IGNORE_CASE_ACCENT);
272 sal_Int16 j=0;
273 sal_Unicode start = unicode::getUnicodeScriptStart(UnicodeScript(0));
274 sal_Unicode end = unicode::getUnicodeScriptEnd(UnicodeScript(0));
275 for (sal_Int32 i= (scriptList[0] == UnicodeScript(0)) ? 1 : 0; i< scriptList.getLength(); i++) {
276 if (unicode::getUnicodeScriptStart(scriptList[i]) != end+1) {
277 tables[j++].init(start, end, keys, key_count, this);
278 start = unicode::getUnicodeScriptStart(scriptList[i]);
280 end = unicode::getUnicodeScriptEnd(scriptList[i]);
282 tables[j++].init(start, end, keys, key_count, this);
283 table_count = j;
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */