1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_i18npool.hxx"
26 
27 // prevent internal compiler error with MSVC6SP3
28 #include <utility>
29 
30 #include <i18nutil/oneToOneMapping.hxx>
31 #include <i18nutil/casefolding.hxx>
32 #include "transliteration_caseignore.hxx"
33 
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::lang;
36 using namespace ::rtl;
37 
38 namespace com { namespace sun { namespace star { namespace i18n {
39 
Transliteration_caseignore()40 Transliteration_caseignore::Transliteration_caseignore()
41 {
42 	nMappingType = MappingTypeFullFolding;
43 	moduleLoaded = (TransliterationModules)0;
44 	transliterationName = "case ignore (generic)";
45 	implementationName = "com.sun.star.i18n.Transliteration.Transliteration_caseignore";
46 }
47 
48 #if 0
49 /* NOTE: We had this, derived from Transliteration_caseignore, but it was
50  * unused code. Deactivated with #i89580# but left for reference in case
51  * MappingTypeSimpleFolding would be needed at some time.
52  */
53 Transliteration_simplecaseignore::Transliteration_simplecaseignore()
54 {
55 	nMappingType = MappingTypeSimpleFolding;
56 	moduleLoaded = (TransliterationModules)0;
57 	transliterationName = "simple case ignore (generic)";
58 	implementationName = "com.sun.star.i18n.Transliteration.Transliteration_simplecaseignore";
59 }
60 #endif
61 
62 void SAL_CALL
loadModule(TransliterationModules modName,const Locale & rLocale)63 Transliteration_caseignore::loadModule( TransliterationModules modName, const Locale& rLocale )
64 	throw(RuntimeException)
65 {
66 	moduleLoaded = (TransliterationModules) (moduleLoaded|modName);
67 	aLocale = rLocale;
68 }
69 
getType()70 sal_Int16 SAL_CALL Transliteration_caseignore::getType() throw(RuntimeException)
71 {
72 	// It's NOT TransliterationType::ONE_TO_ONE because it's using casefolding
73 	return TransliterationType::IGNORE;
74 }
75 
76 
77 Sequence< OUString > SAL_CALL
transliterateRange(const OUString & str1,const OUString & str2)78 Transliteration_caseignore::transliterateRange( const OUString& str1, const OUString& str2 )
79 	throw( RuntimeException)
80 {
81 	if (str1.getLength() != 1 || str2.getLength() != 1)
82 	    throw RuntimeException();
83 
84 	static Transliteration_u2l u2l;
85 	static Transliteration_l2u l2u;
86 
87 	u2l.loadModule((TransliterationModules)0, aLocale);
88 	l2u.loadModule((TransliterationModules)0, aLocale);
89 
90 	OUString l1 = u2l.transliterateString2String(str1, 0, str1.getLength());
91 	OUString u1 = l2u.transliterateString2String(str1, 0, str1.getLength());
92 	OUString l2 = u2l.transliterateString2String(str2, 0, str2.getLength());
93 	OUString u2 = l2u.transliterateString2String(str2, 0, str2.getLength());
94 
95 	if ((l1 == u1) && (l2 == u2)) {
96 	    Sequence< OUString > r(2);
97 	    r[0] = l1;
98 	    r[1] = l2;
99 	    return r;
100 	} else {
101 	    Sequence< OUString > r(4);
102 	    r[0] = l1;
103 	    r[1] = l2;
104 	    r[2] = u1;
105 	    r[3] = u2;
106 	    return r;
107 	}
108 }
109 
110 sal_Bool SAL_CALL
equals(const::rtl::OUString & str1,sal_Int32 pos1,sal_Int32 nCount1,sal_Int32 & nMatch1,const::rtl::OUString & str2,sal_Int32 pos2,sal_Int32 nCount2,sal_Int32 & nMatch2)111 Transliteration_caseignore::equals(
112 	const ::rtl::OUString& str1, sal_Int32 pos1, sal_Int32 nCount1, sal_Int32& nMatch1,
113 	const ::rtl::OUString& str2, sal_Int32 pos2, sal_Int32 nCount2, sal_Int32& nMatch2)
114 	throw(::com::sun::star::uno::RuntimeException)
115 {
116 	return (compare(str1, pos1, nCount1, nMatch1, str2, pos2, nCount2, nMatch2) == 0);
117 }
118 
119 sal_Int32 SAL_CALL
compareSubstring(const::rtl::OUString & str1,sal_Int32 off1,sal_Int32 len1,const::rtl::OUString & str2,sal_Int32 off2,sal_Int32 len2)120 Transliteration_caseignore::compareSubstring(
121 	const ::rtl::OUString& str1, sal_Int32 off1, sal_Int32 len1,
122 	const ::rtl::OUString& str2, sal_Int32 off2, sal_Int32 len2)
123 	throw(RuntimeException)
124 {
125 	sal_Int32 nMatch1, nMatch2;
126 	return compare(str1, off1, len1, nMatch1, str2, off2, len2, nMatch2);
127 }
128 
129 
130 sal_Int32 SAL_CALL
compareString(const::rtl::OUString & str1,const::rtl::OUString & str2)131 Transliteration_caseignore::compareString(
132 	const ::rtl::OUString& str1,
133 	const ::rtl::OUString& str2)
134 	throw(RuntimeException)
135 {
136 	sal_Int32 nMatch1, nMatch2;
137 	return compare(str1, 0, str1.getLength(), nMatch1, str2, 0, str2.getLength(), nMatch2);
138 }
139 
140 sal_Int32 SAL_CALL
compare(const::rtl::OUString & str1,sal_Int32 pos1,sal_Int32 nCount1,sal_Int32 & nMatch1,const::rtl::OUString & str2,sal_Int32 pos2,sal_Int32 nCount2,sal_Int32 & nMatch2)141 Transliteration_caseignore::compare(
142 	const ::rtl::OUString& str1, sal_Int32 pos1, sal_Int32 nCount1, sal_Int32& nMatch1,
143 	const ::rtl::OUString& str2, sal_Int32 pos2, sal_Int32 nCount2, sal_Int32& nMatch2)
144 	throw(RuntimeException)
145 {
146 	const sal_Unicode *unistr1 = (sal_Unicode*) str1.getStr() + pos1;
147 	const sal_Unicode *unistr2 = (sal_Unicode*) str2.getStr() + pos2;
148 	sal_Unicode c1, c2;
149 	MappingElement e1, e2;
150 	nMatch1 = nMatch2 = 0;
151 
152 #define NOT_END_OF_STR1 (nMatch1 < nCount1 || e1.current < e1.element.nmap)
153 #define NOT_END_OF_STR2 (nMatch2 < nCount2 || e2.current < e2.element.nmap)
154 
155 	while (NOT_END_OF_STR1 && NOT_END_OF_STR2) {
156 	    c1 = casefolding::getNextChar(unistr1, nMatch1, nCount1, e1, aLocale, nMappingType, moduleLoaded);
157 	    c2 = casefolding::getNextChar(unistr2, nMatch2, nCount2, e2, aLocale, nMappingType, moduleLoaded);
158 	    if (c1 != c2) {
159 		nMatch1--; nMatch2--;
160 		return c1 > c2 ? 1 : -1;
161 	    }
162 	}
163 
164 	return (!NOT_END_OF_STR1 && !NOT_END_OF_STR2) ? 0
165 				: (NOT_END_OF_STR1 ? 1 : -1);
166 }
167 
168 } } } }
169