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 <sal/config.h>
22 #include <o3tl/temporary.hxx>
23 #include <rtl/ustrbuf.hxx>
24 #include <collatorImpl.hxx>
25 #include <indexentrysupplier_asian.hxx>
26 #include "data/indexdata_alphanumeric.h"
28 using namespace ::com::sun::star::uno
;
29 using namespace ::com::sun::star::lang
;
33 IndexEntrySupplier_asian::IndexEntrySupplier_asian(
34 const Reference
< XComponentContext
>& rxContext
) : IndexEntrySupplier_Common(rxContext
)
36 implementationName
= "com.sun.star.i18n.IndexEntrySupplier_asian";
39 IndexEntrySupplier_asian::~IndexEntrySupplier_asian()
44 IndexEntrySupplier_asian::getIndexCharacter( const OUString
& rIndexEntry
,
45 const Locale
& rLocale
, const OUString
& rAlgorithm
)
47 sal_uInt32 ch
= rIndexEntry
.iterateCodePoints(&o3tl::temporary(sal_Int32(0)), 0);
49 const sal_uInt16
** (*func
)(sal_Int16
&)=nullptr;
50 if ( rLocale
.Language
== "zh" && u
"TW HK MO"_ustr
.indexOf(rLocale
.Country
) >= 0 ) {
51 if ( rAlgorithm
== "radical" )
52 func
= get_indexdata_zh_TW_radical
;
53 else if ( rAlgorithm
== "stroke" )
54 func
= get_indexdata_zh_TW_stroke
;
57 if ( rLocale
.Language
== "ko" ) {
58 if ( rAlgorithm
== "dict" )
59 func
= get_indexdata_ko_dict
;
60 } else if ( rLocale
.Language
== "zh" ) {
61 if ( rAlgorithm
== "pinyin" )
62 func
= get_indexdata_zh_pinyin
;
63 else if ( rAlgorithm
== "radical" )
64 func
= get_indexdata_zh_radical
;
65 else if ( rAlgorithm
== "stroke" )
66 func
= get_indexdata_zh_stroke
;
67 else if ( rAlgorithm
== "zhuyin" )
68 func
= get_indexdata_zh_zhuyin
;
73 const sal_uInt16
** idx
=func(max_index
);
74 if (static_cast<sal_Int16
>(ch
>> 8) <= max_index
) {
75 sal_uInt16 address
=idx
[0][ch
>> 8];
76 if (address
!= 0xFFFF) {
77 address
=idx
[1][address
+(ch
& 0xFF)];
80 reinterpret_cast<const sal_Unicode
*>(&idx
[2][address
]))
81 : OUString(sal_Unicode(address
));
86 // using alphanumeric index for non-define string
87 return OUString(&idxStr
[(ch
& 0xFFFFFF00) ? 0 : ch
], 1);
91 IndexEntrySupplier_asian::getIndexKey( const OUString
& rIndexEntry
,
92 const OUString
& rPhoneticEntry
, const Locale
& rLocale
)
94 return getIndexCharacter(getEntry(rIndexEntry
, rPhoneticEntry
, rLocale
), rLocale
, aAlgorithm
);
98 IndexEntrySupplier_asian::compareIndexEntry(
99 const OUString
& rIndexEntry1
, const OUString
& rPhoneticEntry1
, const Locale
& rLocale1
,
100 const OUString
& rIndexEntry2
, const OUString
& rPhoneticEntry2
, const Locale
& rLocale2
)
102 sal_Int32 result
= collator
->compareString(getEntry(rIndexEntry1
, rPhoneticEntry1
, rLocale1
),
103 getEntry(rIndexEntry2
, rPhoneticEntry2
, rLocale2
));
105 // equivalent of phonetic entries does not mean equivalent of index entries.
106 // we have to continue comparing index entry here.
107 if (result
== 0 && usePhonetic
&& !rPhoneticEntry1
.isEmpty() &&
108 rLocale1
.Language
== rLocale2
.Language
&& rLocale1
.Country
== rLocale2
.Country
&&
109 rLocale1
.Variant
== rLocale2
.Variant
)
110 result
= collator
->compareString(rIndexEntry1
, rIndexEntry2
);
111 return sal::static_int_cast
< sal_Int16
>(result
); // result in { -1, 0, 1 }
115 IndexEntrySupplier_asian::getPhoneticCandidate( const OUString
& rIndexEntry
,
116 const Locale
& rLocale
)
118 sal_uInt16
const **(*func
)(sal_Int16
&)=nullptr;
119 if ( rLocale
.Language
== "zh" )
120 func
= (u
"TW HK MO"_ustr
.indexOf(rLocale
.Country
) >= 0) ? get_zh_zhuyin
: get_zh_pinyin
;
121 else if ( rLocale
.Language
== "ko" )
122 func
= get_ko_phonetic
;
125 OUStringBuffer candidate
;
127 sal_uInt16
const ** idx
=func(max_index
);
128 for (sal_Int32 i
=0,j
=0; i
< rIndexEntry
.getLength(); i
=j
) {
129 sal_uInt32 ch
= rIndexEntry
.iterateCodePoints(&j
);
130 if (static_cast<sal_Int16
>(ch
>>8) <= max_index
) {
131 sal_uInt16 address
= idx
[0][ch
>>8];
132 if (address
!= 0xFFFF) {
133 address
= idx
[1][address
+ (ch
& 0xFF)];
134 if ( i
> 0 && rLocale
.Language
== "zh" )
135 candidate
.append(" ");
138 reinterpret_cast<const sal_Unicode
*>(&idx
[2][address
]));
140 candidate
.append(sal_Unicode(address
));
142 candidate
.append(" ");
145 return candidate
.makeStringAndClear();
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */