1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <localedata.hxx>
22 #include <i18nutil/unicode.hxx>
23 #include <com/sun/star/i18n/CollatorOptions.hpp>
25 using namespace ::com::sun::star::uno
;
26 using namespace ::com::sun::star::lang
;
28 namespace com
{ namespace sun
{ namespace star
{ namespace i18n
{
30 IndexEntrySupplier_Unicode::IndexEntrySupplier_Unicode(
31 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
) :
32 IndexEntrySupplier_Common(rxContext
)
34 implementationName
= "com.sun.star.i18n.IndexEntrySupplier_Unicode";
35 index
.reset( new Index(rxContext
) );
38 IndexEntrySupplier_Unicode::~IndexEntrySupplier_Unicode()
42 sal_Bool SAL_CALL
IndexEntrySupplier_Unicode::loadAlgorithm( const lang::Locale
& rLocale
,
43 const OUString
& rAlgorithm
, sal_Int32 collatorOptions
)
45 index
->init(rLocale
, rAlgorithm
);
46 return IndexEntrySupplier_Common::loadAlgorithm(rLocale
, rAlgorithm
, collatorOptions
);
49 OUString SAL_CALL
IndexEntrySupplier_Unicode::getIndexKey( const OUString
& rIndexEntry
,
50 const OUString
& rPhoneticEntry
, const lang::Locale
& rLocale
)
52 return index
->getIndexDescription(getEntry(rIndexEntry
, rPhoneticEntry
, rLocale
));
55 sal_Int16 SAL_CALL
IndexEntrySupplier_Unicode::compareIndexEntry(
56 const OUString
& rIndexEntry1
, const OUString
& rPhoneticEntry1
, const lang::Locale
& rLocale1
,
57 const OUString
& rIndexEntry2
, const OUString
& rPhoneticEntry2
, const lang::Locale
& rLocale2
)
60 index
->getIndexWeight(getEntry(rIndexEntry1
, rPhoneticEntry1
, rLocale1
)) -
61 index
->getIndexWeight(getEntry(rIndexEntry2
, rPhoneticEntry2
, rLocale2
));
63 return IndexEntrySupplier_Common::compareIndexEntry(
64 rIndexEntry1
, rPhoneticEntry1
, rLocale1
,
65 rIndexEntry2
, rPhoneticEntry2
, rLocale2
);
66 return result
> 0 ? 1 : -1;
69 OUString SAL_CALL
IndexEntrySupplier_Unicode::getIndexCharacter( const OUString
& rIndexEntry
,
70 const lang::Locale
& rLocale
, const OUString
& rAlgorithm
) {
72 if (loadAlgorithm( rLocale
, rAlgorithm
, CollatorOptions::CollatorOptions_IGNORE_CASE_ACCENT
))
73 return index
->getIndexDescription(rIndexEntry
);
75 return IndexEntrySupplier_Common::getIndexCharacter(rIndexEntry
, rLocale
, rAlgorithm
);
78 IndexTable::IndexTable()
85 IndexTable::~IndexTable()
87 if (table
) free(table
);
90 void IndexTable::init(sal_Unicode start_
, sal_Unicode end_
, IndexKey
*keys
, sal_Int16 key_count
, Index
*index
)
94 table
= static_cast<sal_uInt8
*>(malloc((end
-start
+1)*sizeof(sal_uInt8
)));
95 for (sal_Unicode i
= start
; i
<= end
; i
++) {
97 for (j
= 0; j
< key_count
; j
++) {
98 if (keys
[j
].key
> 0 && (i
== keys
[j
].key
|| index
->compare(i
, keys
[j
].key
) == 0)) {
99 table
[i
-start
] = sal::static_int_cast
<sal_uInt8
>(j
);
104 table
[i
-start
] = 0xFF;
108 Index::Index(const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
)
112 , collator( new CollatorImpl(rxContext
) )
120 sal_Int16
Index::compare(sal_Unicode c1
, sal_Unicode c2
)
122 return sal::static_int_cast
<sal_Int16
>( collator
->compareString(OUString(&c1
, 1), OUString(&c2
, 1)) );
125 sal_Int16
Index::getIndexWeight(const OUString
& rIndexEntry
)
127 sal_Int32 startPos
=0;
128 if (!skipping_chars
.isEmpty())
129 while (skipping_chars
.indexOf(rIndexEntry
[startPos
]) >= 0)
131 if (mkey_count
> 0) {
132 for (sal_Int16 i
= 0; i
< mkey_count
; i
++) {
133 sal_Int32 len
= keys
[mkeys
[i
]].mkey
.getLength();
134 if (collator
->compareSubstring(rIndexEntry
, startPos
, len
,
135 keys
[mkeys
[i
]].mkey
, 0, len
) == 0)
139 sal_Unicode code
= rIndexEntry
[startPos
];
140 for (sal_Int16 i
= 0; i
< table_count
; i
++) {
141 if (tables
[i
].start
<= code
&& code
<= tables
[i
].end
)
142 return tables
[i
].table
[code
-tables
[i
].start
];
147 OUString
Index::getIndexDescription(const OUString
& rIndexEntry
)
149 sal_Int16 wgt
= getIndexWeight(rIndexEntry
);
150 if (wgt
< MAX_KEYS
) {
151 if (!keys
[wgt
].desc
.isEmpty())
152 return keys
[wgt
].desc
;
153 else if (keys
[wgt
].key
> 0)
154 return OUString(&keys
[wgt
].key
, 1);
156 return keys
[wgt
].mkey
;
159 sal_uInt32 indexChar
=rIndexEntry
.iterateCodePoints(&nPos
, 0);
160 return OUString(&indexChar
, 1);
163 #define LOCALE_EN lang::Locale("en", OUString(), OUString())
165 void Index::makeIndexKeys(const lang::Locale
&rLocale
, const OUString
&algorithm
)
167 OUString keyStr
= LocaleDataImpl::get()->getIndexKeysByAlgorithm(rLocale
, algorithm
);
169 if (keyStr
.isEmpty()) {
170 keyStr
= LocaleDataImpl::get()->getIndexKeysByAlgorithm(LOCALE_EN
,
171 LocaleDataImpl::get()->getDefaultIndexAlgorithm(LOCALE_EN
));
172 if (keyStr
.isEmpty())
173 throw RuntimeException();
176 sal_Int16 len
= sal::static_int_cast
<sal_Int16
>( keyStr
.getLength() );
177 mkey_count
=key_count
=0;
178 skipping_chars
=OUString();
181 for (i
= 0; i
< len
&& key_count
< MAX_KEYS
; i
++)
183 sal_Unicode curr
= keyStr
[i
];
184 sal_Unicode close
= ')';
186 if (unicode::isWhiteSpace(curr
))
191 if (key_count
> 0 && i
+ 1 < len
) {
192 for (curr
= keyStr
[++i
]; key_count
< MAX_KEYS
&& keys
[key_count
-1].key
< curr
; key_count
++) {
193 keys
[key_count
].key
= keys
[key_count
-1].key
+1;
194 keys
[key_count
].desc
.clear();
197 throw RuntimeException();
200 for (i
++; i
< len
&& keyStr
[i
] != ']'; i
++) {
201 if (unicode::isWhiteSpace(keyStr
[i
])) {
203 } else if (keyStr
[i
] == '_') {
204 for (curr
=keyStr
[i
-1]+1; curr
<= keyStr
[i
+1]; curr
++)
205 skipping_chars
+=OUStringLiteral1(curr
);
208 skipping_chars
+=OUStringLiteral1(keyStr
[i
]);
218 for (; end
< len
&& keyStr
[end
] != close
; end
++) ;
220 if (end
>= len
) // no found
221 throw RuntimeException();
223 keys
[key_count
-1].desc
= keyStr
.copy(i
+1, end
-i
-1);
225 mkeys
[mkey_count
++]=key_count
;
226 keys
[key_count
].key
= 0;
227 keys
[key_count
].mkey
= keyStr
.copy(i
+1, end
-i
-1);
228 keys
[key_count
++].desc
.clear();
232 throw RuntimeException();
235 keys
[key_count
].key
= curr
;
236 keys
[key_count
++].desc
.clear();
240 for (i
= 0; i
< mkey_count
; i
++) {
241 for (j
=i
+1; j
< mkey_count
; j
++) {
242 if (keys
[mkeys
[i
]].mkey
.getLength() < keys
[mkeys
[j
]].mkey
.getLength()) {
243 sal_Int16 k
= mkeys
[i
];
251 void Index::init(const lang::Locale
&rLocale
, const OUString
& algorithm
)
253 makeIndexKeys(rLocale
, algorithm
);
255 Sequence
< UnicodeScript
> scriptList
= LocaleDataImpl::get()->getUnicodeScripts( rLocale
);
257 if (scriptList
.getLength() == 0) {
258 scriptList
= LocaleDataImpl::get()->getUnicodeScripts(LOCALE_EN
);
259 if (scriptList
.getLength() == 0)
260 throw RuntimeException();
263 table_count
= sal::static_int_cast
<sal_Int16
>( scriptList
.getLength() );
264 if (table_count
> MAX_TABLES
)
265 throw RuntimeException();
267 collator
->loadCollatorAlgorithm(algorithm
, rLocale
, CollatorOptions::CollatorOptions_IGNORE_CASE_ACCENT
);
269 sal_Unicode start
= unicode::getUnicodeScriptStart((UnicodeScript
)0);
270 sal_Unicode end
= unicode::getUnicodeScriptEnd((UnicodeScript
)0);
271 for (sal_Int32 i
= (scriptList
[0] == (UnicodeScript
)0) ? 1 : 0; i
< scriptList
.getLength(); i
++) {
272 if (unicode::getUnicodeScriptStart(scriptList
[i
]) != end
+1) {
273 tables
[j
++].init(start
, end
, keys
, key_count
, this);
274 start
= unicode::getUnicodeScriptStart(scriptList
[i
]);
276 end
= unicode::getUnicodeScriptEnd(scriptList
[i
]);
278 tables
[j
++].init(start
, end
, keys
, key_count
, this);
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */