1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_i18npool.hxx"
30 
31 // prevent internal compiler error with MSVC6SP3
32 #include <utility>
33 
34 #include <i18nutil/oneToOneMapping.hxx>
35 #include <i18nutil/casefolding.hxx>
36 #include "transliteration_caseignore.hxx"
37 
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::lang;
40 using namespace ::rtl;
41 
42 namespace com { namespace sun { namespace star { namespace i18n {
43 
44 Transliteration_caseignore::Transliteration_caseignore()
45 {
46 	nMappingType = MappingTypeFullFolding;
47 	moduleLoaded = (TransliterationModules)0;
48 	transliterationName = "case ignore (generic)";
49 	implementationName = "com.sun.star.i18n.Transliteration.Transliteration_caseignore";
50 }
51 
52 #if 0
53 /* NOTE: We had this, derived from Transliteration_caseignore, but it was
54  * unused code. Deactivated with #i89580# but left for reference in case
55  * MappingTypeSimpleFolding would be needed at some time.
56  */
57 Transliteration_simplecaseignore::Transliteration_simplecaseignore()
58 {
59 	nMappingType = MappingTypeSimpleFolding;
60 	moduleLoaded = (TransliterationModules)0;
61 	transliterationName = "simple case ignore (generic)";
62 	implementationName = "com.sun.star.i18n.Transliteration.Transliteration_simplecaseignore";
63 }
64 #endif
65 
66 void SAL_CALL
67 Transliteration_caseignore::loadModule( TransliterationModules modName, const Locale& rLocale )
68 	throw(RuntimeException)
69 {
70 	moduleLoaded = (TransliterationModules) (moduleLoaded|modName);
71 	aLocale = rLocale;
72 }
73 
74 sal_Int16 SAL_CALL Transliteration_caseignore::getType() throw(RuntimeException)
75 {
76 	// It's NOT TransliterationType::ONE_TO_ONE because it's using casefolding
77 	return TransliterationType::IGNORE;
78 }
79 
80 
81 Sequence< OUString > SAL_CALL
82 Transliteration_caseignore::transliterateRange( const OUString& str1, const OUString& str2 )
83 	throw( RuntimeException)
84 {
85 	if (str1.getLength() != 1 || str2.getLength() != 1)
86 	    throw RuntimeException();
87 
88 	static Transliteration_u2l u2l;
89 	static Transliteration_l2u l2u;
90 
91 	u2l.loadModule((TransliterationModules)0, aLocale);
92 	l2u.loadModule((TransliterationModules)0, aLocale);
93 
94 	OUString l1 = u2l.transliterateString2String(str1, 0, str1.getLength());
95 	OUString u1 = l2u.transliterateString2String(str1, 0, str1.getLength());
96 	OUString l2 = u2l.transliterateString2String(str2, 0, str2.getLength());
97 	OUString u2 = l2u.transliterateString2String(str2, 0, str2.getLength());
98 
99 	if ((l1 == u1) && (l2 == u2)) {
100 	    Sequence< OUString > r(2);
101 	    r[0] = l1;
102 	    r[1] = l2;
103 	    return r;
104 	} else {
105 	    Sequence< OUString > r(4);
106 	    r[0] = l1;
107 	    r[1] = l2;
108 	    r[2] = u1;
109 	    r[3] = u2;
110 	    return r;
111 	}
112 }
113 
114 sal_Bool SAL_CALL
115 Transliteration_caseignore::equals(
116 	const ::rtl::OUString& str1, sal_Int32 pos1, sal_Int32 nCount1, sal_Int32& nMatch1,
117 	const ::rtl::OUString& str2, sal_Int32 pos2, sal_Int32 nCount2, sal_Int32& nMatch2)
118 	throw(::com::sun::star::uno::RuntimeException)
119 {
120 	return (compare(str1, pos1, nCount1, nMatch1, str2, pos2, nCount2, nMatch2) == 0);
121 }
122 
123 sal_Int32 SAL_CALL
124 Transliteration_caseignore::compareSubstring(
125 	const ::rtl::OUString& str1, sal_Int32 off1, sal_Int32 len1,
126 	const ::rtl::OUString& str2, sal_Int32 off2, sal_Int32 len2)
127 	throw(RuntimeException)
128 {
129 	sal_Int32 nMatch1, nMatch2;
130 	return compare(str1, off1, len1, nMatch1, str2, off2, len2, nMatch2);
131 }
132 
133 
134 sal_Int32 SAL_CALL
135 Transliteration_caseignore::compareString(
136 	const ::rtl::OUString& str1,
137 	const ::rtl::OUString& str2)
138 	throw(RuntimeException)
139 {
140 	sal_Int32 nMatch1, nMatch2;
141 	return compare(str1, 0, str1.getLength(), nMatch1, str2, 0, str2.getLength(), nMatch2);
142 }
143 
144 sal_Int32 SAL_CALL
145 Transliteration_caseignore::compare(
146 	const ::rtl::OUString& str1, sal_Int32 pos1, sal_Int32 nCount1, sal_Int32& nMatch1,
147 	const ::rtl::OUString& str2, sal_Int32 pos2, sal_Int32 nCount2, sal_Int32& nMatch2)
148 	throw(RuntimeException)
149 {
150 	const sal_Unicode *unistr1 = (sal_Unicode*) str1.getStr() + pos1;
151 	const sal_Unicode *unistr2 = (sal_Unicode*) str2.getStr() + pos2;
152 	sal_Unicode c1, c2;
153 	MappingElement e1, e2;
154 	nMatch1 = nMatch2 = 0;
155 
156 #define NOT_END_OF_STR1 (nMatch1 < nCount1 || e1.current < e1.element.nmap)
157 #define NOT_END_OF_STR2 (nMatch2 < nCount2 || e2.current < e2.element.nmap)
158 
159 	while (NOT_END_OF_STR1 && NOT_END_OF_STR2) {
160 	    c1 = casefolding::getNextChar(unistr1, nMatch1, nCount1, e1, aLocale, nMappingType, moduleLoaded);
161 	    c2 = casefolding::getNextChar(unistr2, nMatch2, nCount2, e2, aLocale, nMappingType, moduleLoaded);
162 	    if (c1 != c2) {
163 		nMatch1--; nMatch2--;
164 		return c1 > c2 ? 1 : -1;
165 	    }
166 	}
167 
168 	return (!NOT_END_OF_STR1 && !NOT_END_OF_STR2) ? 0
169 				: (NOT_END_OF_STR1 ? 1 : -1);
170 }
171 
172 } } } }
173