Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / i18nutil / source / utility / casefolding.cxx
blob0b01565a446623123dfd2962f3d7c5de573aa43f
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 <i18nutil/casefolding.hxx>
21 #include "casefolding_data.h"
22 #include <i18nutil/oneToOneMapping.hxx>
23 #include <i18nutil/widthfolding.hxx>
24 #include <i18nutil/transliteration.hxx>
25 #include <com/sun/star/lang/Locale.hpp>
26 #include <com/sun/star/uno/RuntimeException.hpp>
28 using namespace com::sun::star::lang;
29 using namespace com::sun::star::uno;
31 namespace i18nutil {
33 static const Mapping mapping_03a3[] = {{0, 1, {0x03c2, 0, 0}},{0, 1, {0x03c3, 0, 0}}};
34 static const Mapping mapping_0307[] = {{0, 0, {0, 0, 0}},{0, 1, {0x0307, 0, 0}}};
35 static const Mapping mapping_004a[] = {{0, 2, {0x006a, 0x0307, 0}},{0, 1, {0x006a, 0, 0}}};
36 static const Mapping mapping_012e[] = {{0, 2, {0x012f, 0x0307, 0}},{0, 1, {0x012f, 0, 0}}};
37 static const Mapping mapping_00cc[] = {{0, 3, {0x0069, 0x0307, 0x0300}},{0, 1, {0x00ec, 0, 0}}};
38 static const Mapping mapping_00cd[] = {{0, 3, {0x0069, 0x0307, 0x0301}},{0, 1, {0x00ed, 0, 0}}};
39 static const Mapping mapping_0128[] = {{0, 3, {0x0069, 0x0307, 0x0303}},{0, 1, {0x0129, 0, 0}}};
40 static const Mapping mapping_0049[] = {{0, 2, {0x0069, 0x0307, 0}},{0, 1, {0x0131, 0, 0}},{0, 1, {0x0069, 0, 0}}};
41 static const Mapping mapping_0069[] = {{0, 1, {0x0130, 0, 0}},{0, 1, {0x0049, 0, 0}}};
42 static const Mapping mapping_0130[] = {{0, 1, {0x0069, 0, 0}},{0, 1, {0x0130, 0, 0}}};
44 #define langIs(lang) (aLocale.Language == lang)
46 // only check simple case, there is more complicated case need to be checked.
47 #define type_i(ch) ((ch) == 0x0069 || (ch) == 0x006a)
49 static bool cased_letter(sal_Unicode ch)
51 int msb = ch >> 8;
52 int cmi = CaseMappingIndex[msb];
53 if (cmi < 0)
54 return false;
55 int cmv_idx = (cmi << 8) + (ch & 0xff);
56 return bool(static_cast<MappingType>(CaseMappingValue[cmv_idx].type) & MappingType::CasedLetterMask);
59 // for Lithuanian, condition to make explicit dot above when lowercasing capital I's and J's
60 // whenever there are more accents above.
61 #define accent_above(ch) (((ch) >= 0x0300 && (ch) <= 0x0314) || ((ch) >= 0x033D && (ch) <= 0x0344) || (ch) == 0x0346 || ((ch) >= 0x034A && (ch) <= 0x034C))
63 const Mapping& casefolding::getConditionalValue(const sal_Unicode* str, sal_Int32 pos, sal_Int32 len, Locale const & aLocale, MappingType nMappingType)
65 switch(str[pos]) {
66 case 0x03a3:
67 // final_sigma (not followed by cased and preceded by cased character)
68 // DOES NOT check ignorable sequence yet (more complicated implementation).
69 return !(pos < len && cased_letter(str[pos+1])) && (pos > 0 && cased_letter(str[pos-1])) ?
70 mapping_03a3[0] : mapping_03a3[1];
71 case 0x0307:
72 return (((nMappingType == MappingType::LowerToUpper && langIs("lt")) ||
73 (nMappingType == MappingType::UpperToLower && (langIs("tr") || langIs("az")))) &&
74 (pos > 0 && type_i(str[pos-1]))) ? // after_i
75 mapping_0307[0] : mapping_0307[1];
76 case 0x0130:
77 return (langIs("tr") || langIs("az")) ? mapping_0130[0] : mapping_0130[1];
78 case 0x0069:
79 return (langIs("tr") || langIs("az")) ? mapping_0069[0] : mapping_0069[1];
80 case 0x0049: return langIs("lt") && pos > len && accent_above(str[pos+1]) ? mapping_0049[0] :
81 (langIs("tr") || langIs("az")) ? mapping_0049[1] : mapping_0049[2];
82 case 0x004a: return langIs("lt") && pos > len && accent_above(str[pos+1]) ? mapping_004a[0] : mapping_004a[1];
83 case 0x012e: return langIs("lt") && pos > len && accent_above(str[pos+1]) ? mapping_012e[0] : mapping_012e[1];
84 case 0x00cc: return langIs("lt") ? mapping_00cc[0] : mapping_00cc[1];
85 case 0x00cd: return langIs("lt") ? mapping_00cd[0] : mapping_00cd[1];
86 case 0x0128: return langIs("lt") ? mapping_0128[0] : mapping_0128[1];
88 // Should not come here
89 throw RuntimeException();
92 Mapping casefolding::getValue(const sal_Unicode* str, sal_Int32 pos, sal_Int32 len, Locale const & aLocale, MappingType nMappingType)
94 Mapping dummy = { 0, 1, { 0, 0, 0 } };
95 sal_Int16 address = CaseMappingIndex[str[pos] >> 8];
97 dummy.map[0] = str[pos];
99 if (address >= 0) {
100 address = (address << 8) + (str[pos] & 0xFF);
101 if (static_cast<MappingType>(CaseMappingValue[address].type) & nMappingType) {
102 MappingType type = static_cast<MappingType>(CaseMappingValue[address].type);
103 if (type & MappingType::NotValue) {
104 if (CaseMappingValue[address].value == 0)
105 return getConditionalValue(str, pos, len, aLocale, nMappingType);
106 else {
107 for (int map = CaseMappingValue[address].value;
108 map < CaseMappingValue[address].value + MaxCaseMappingExtras; map++) {
109 if (static_cast<MappingType>(CaseMappingExtra[map].type) & nMappingType) {
110 if (static_cast<MappingType>(CaseMappingExtra[map].type) & MappingType::NotValue)
111 return getConditionalValue(str, pos, len, aLocale, nMappingType);
112 else
113 return CaseMappingExtra[map];
116 // Should not come here
117 throw RuntimeException();
119 } else
120 dummy.map[0] = CaseMappingValue[address].value;
123 return dummy;
126 static bool
127 is_ja_voice_sound_mark(sal_Unicode& current, sal_Unicode next)
129 sal_Unicode c = 0;
131 if ((next == 0x3099 || next == 0x309a) && ( (c = widthfolding::getCompositionChar(current, next)) != 0 ))
132 current = c;
133 return c != 0;
136 sal_Unicode casefolding::getNextChar(const sal_Unicode *str, sal_Int32& idx, sal_Int32 len, MappingElement& e, Locale const & aLocale, MappingType nMappingType, TransliterationFlags moduleLoaded)
138 if( idx >= len )
140 e = MappingElement();
141 return 0;
144 sal_Unicode c;
146 if (moduleLoaded & TransliterationFlags::IGNORE_CASE) {
147 if( e.current >= e.element.nmap ) {
148 e.element = getValue(str, idx++, len, aLocale, nMappingType);
149 e.current = 0;
151 c = e.element.map[e.current++];
152 } else {
153 c = *(str + idx++);
156 if (moduleLoaded & TransliterationFlags::IGNORE_KANA) {
157 if ((0x3040 <= c && c <= 0x3094) || (0x309d <= c && c <= 0x309f))
158 c += 0x60;
161 // composition: KA + voice-mark --> GA. see halfwidthToFullwidth.cxx for detail
162 if (moduleLoaded & TransliterationFlags::IGNORE_WIDTH) {
163 static oneToOneMapping& half2fullTable = widthfolding::gethalf2fullTable();
164 c = half2fullTable[c];
165 if (0x3040 <= c && c <= 0x30ff && idx < len &&
166 is_ja_voice_sound_mark(c, half2fullTable[*(str + idx)]))
167 idx++;
170 return c;
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */