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
;
27 using namespace ::rtl
;
29 namespace com
{ namespace sun
{ namespace star
{ namespace i18n
{
31 IndexEntrySupplier_Unicode::IndexEntrySupplier_Unicode(
32 const com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
>& rxContext
) :
33 IndexEntrySupplier_Common(rxContext
)
35 implementationName
= "com.sun.star.i18n.IndexEntrySupplier_Unicode";
36 index
= new Index(rxContext
);
39 IndexEntrySupplier_Unicode::~IndexEntrySupplier_Unicode()
44 sal_Bool SAL_CALL
IndexEntrySupplier_Unicode::loadAlgorithm( const lang::Locale
& rLocale
,
45 const OUString
& rAlgorithm
, sal_Int32 collatorOptions
) throw (RuntimeException
, std::exception
)
47 index
->init(rLocale
, rAlgorithm
);
48 return IndexEntrySupplier_Common::loadAlgorithm(rLocale
, rAlgorithm
, collatorOptions
);
51 OUString SAL_CALL
IndexEntrySupplier_Unicode::getIndexKey( const OUString
& rIndexEntry
,
52 const OUString
& rPhoneticEntry
, const lang::Locale
& rLocale
) throw (RuntimeException
, std::exception
)
54 return index
->getIndexDescription(getEntry(rIndexEntry
, rPhoneticEntry
, rLocale
));
57 sal_Int16 SAL_CALL
IndexEntrySupplier_Unicode::compareIndexEntry(
58 const OUString
& rIndexEntry1
, const OUString
& rPhoneticEntry1
, const lang::Locale
& rLocale1
,
59 const OUString
& rIndexEntry2
, const OUString
& rPhoneticEntry2
, const lang::Locale
& rLocale2
)
60 throw (RuntimeException
, std::exception
)
63 index
->getIndexWeight(getEntry(rIndexEntry1
, rPhoneticEntry1
, rLocale1
)) -
64 index
->getIndexWeight(getEntry(rIndexEntry2
, rPhoneticEntry2
, rLocale2
));
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
) throw (RuntimeException
, std::exception
) {
75 if (loadAlgorithm( rLocale
, rAlgorithm
, CollatorOptions::CollatorOptions_IGNORE_CASE_ACCENT
))
76 return index
->getIndexDescription(rIndexEntry
);
78 return IndexEntrySupplier_Common::getIndexCharacter(rIndexEntry
, rLocale
, rAlgorithm
);
81 IndexTable::IndexTable()
88 IndexTable::~IndexTable()
90 if (table
) free(table
);
93 void IndexTable::init(sal_Unicode start_
, sal_Unicode end_
, IndexKey
*keys
, sal_Int16 key_count
, Index
*index
)
97 table
= (sal_uInt8
*) malloc((end
-start
+1)*sizeof(sal_uInt8
));
98 for (sal_Unicode i
= start
; i
<= end
; i
++) {
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
);
107 table
[i
-start
] = 0xFF;
111 Index::Index(const com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
>& rxContext
)
116 collator
= new CollatorImpl(rxContext
);
124 sal_Int16
Index::compare(sal_Unicode c1
, sal_Unicode c2
)
126 return sal::static_int_cast
<sal_Int16
>( collator
->compareString(OUString(&c1
, 1), OUString(&c2
, 1)) );
129 sal_Int16
Index::getIndexWeight(const OUString
& rIndexEntry
)
131 sal_Int32 startPos
=0;
132 if (!skipping_chars
.isEmpty())
133 while (skipping_chars
.indexOf(rIndexEntry
[startPos
]) >= 0)
135 if (mkey_count
> 0) {
136 for (sal_Int16 i
= 0; i
< mkey_count
; i
++) {
137 sal_Int32 len
= keys
[mkeys
[i
]].mkey
.getLength();
138 if (collator
->compareSubstring(rIndexEntry
, startPos
, len
,
139 keys
[mkeys
[i
]].mkey
, 0, len
) == 0)
143 sal_Unicode code
= rIndexEntry
[startPos
];
144 for (sal_Int16 i
= 0; i
< table_count
; i
++) {
145 if (tables
[i
].start
<= code
&& code
<= tables
[i
].end
)
146 return tables
[i
].table
[code
-tables
[i
].start
];
151 OUString
Index::getIndexDescription(const OUString
& rIndexEntry
)
153 sal_Int16 wgt
= getIndexWeight(rIndexEntry
);
154 if (wgt
< MAX_KEYS
) {
155 if (!keys
[wgt
].desc
.isEmpty())
156 return keys
[wgt
].desc
;
157 else if (keys
[wgt
].key
> 0)
158 return OUString(&keys
[wgt
].key
, 1);
160 return keys
[wgt
].mkey
;
163 sal_uInt32 indexChar
=rIndexEntry
.iterateCodePoints(&nPos
, 0);
164 return OUString(&indexChar
, 1);
167 #define LOCALE_EN lang::Locale(OUString("en"), OUString(), OUString())
169 void Index::makeIndexKeys(const lang::Locale
&rLocale
, const OUString
&algorithm
) throw (RuntimeException
)
171 OUString keyStr
= LocaleDataImpl().getIndexKeysByAlgorithm(rLocale
, algorithm
);
173 if (keyStr
.isEmpty()) {
174 keyStr
= LocaleDataImpl().getIndexKeysByAlgorithm(LOCALE_EN
,
175 LocaleDataImpl().getDefaultIndexAlgorithm(LOCALE_EN
));
176 if (keyStr
.isEmpty())
177 throw RuntimeException();
180 sal_Int16 len
= sal::static_int_cast
<sal_Int16
>( keyStr
.getLength() );
181 mkey_count
=key_count
=0;
182 skipping_chars
=OUString();
185 for (i
= 0; i
< len
&& key_count
< MAX_KEYS
; i
++)
187 sal_Unicode curr
= keyStr
[i
];
188 sal_Unicode close
= ')';
190 if (unicode::isWhiteSpace(curr
))
194 case sal_Unicode('-'):
195 if (key_count
> 0 && i
+ 1 < len
) {
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
= OUString();
201 throw RuntimeException();
203 case sal_Unicode('['):
204 for (i
++; i
< len
&& keyStr
[i
] != ']'; i
++) {
205 if (unicode::isWhiteSpace(keyStr
[i
])) {
207 } else if (keyStr
[i
] == '_') {
208 for (curr
=keyStr
[i
-1]+1; curr
<= keyStr
[i
+1]; curr
++)
209 skipping_chars
+=OUString(curr
);
212 skipping_chars
+=OUString(keyStr
[i
]);
216 case sal_Unicode('{'):
218 case sal_Unicode('('):
221 for (end
=i
+1; end
< len
&& keyStr
[end
] != close
; end
++) ;
223 if (end
>= len
) // no found
224 throw RuntimeException();
226 keys
[key_count
-1].desc
= keyStr
.copy(i
+1, end
-i
-1);
228 mkeys
[mkey_count
++]=key_count
;
229 keys
[key_count
].key
= 0;
230 keys
[key_count
].mkey
= keyStr
.copy(i
+1, end
-i
-1);
231 keys
[key_count
++].desc
=OUString();
235 throw RuntimeException();
238 keys
[key_count
].key
= curr
;
239 keys
[key_count
++].desc
= OUString();
243 for (i
= 0; i
< mkey_count
; i
++) {
244 for (j
=i
+1; j
< mkey_count
; j
++) {
245 if (keys
[mkeys
[i
]].mkey
.getLength() < keys
[mkeys
[j
]].mkey
.getLength()) {
246 sal_Int16 k
= mkeys
[i
];
254 void Index::init(const lang::Locale
&rLocale
, const OUString
& algorithm
) throw (RuntimeException
)
256 makeIndexKeys(rLocale
, algorithm
);
258 Sequence
< UnicodeScript
> scriptList
= LocaleDataImpl().getUnicodeScripts( rLocale
);
260 if (scriptList
.getLength() == 0) {
261 scriptList
= LocaleDataImpl().getUnicodeScripts(LOCALE_EN
);
262 if (scriptList
.getLength() == 0)
263 throw RuntimeException();
266 table_count
= sal::static_int_cast
<sal_Int16
>( scriptList
.getLength() );
267 if (table_count
> MAX_TABLES
)
268 throw RuntimeException();
270 collator
->loadCollatorAlgorithm(algorithm
, rLocale
, CollatorOptions::CollatorOptions_IGNORE_CASE_ACCENT
);
272 sal_Unicode start
= unicode::getUnicodeScriptStart((UnicodeScript
)0);
273 sal_Unicode end
= unicode::getUnicodeScriptEnd((UnicodeScript
)0);
274 for (sal_Int16 i
= (scriptList
[0] == (UnicodeScript
)0) ? 1 : 0; i
< scriptList
.getLength(); i
++) {
275 if (unicode::getUnicodeScriptStart(scriptList
[i
]) != end
+1) {
276 tables
[j
++].init(start
, end
, keys
, key_count
, this);
277 start
= unicode::getUnicodeScriptStart(scriptList
[i
]);
279 end
= unicode::getUnicodeScriptEnd(scriptList
[i
]);
281 tables
[j
++].init(start
, end
, keys
, key_count
, this);
287 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */