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 <transliteration_Ignore.hxx>
31
32 using namespace com::sun::star::uno;
33 using namespace rtl;
34
35 namespace com { namespace sun { namespace star { namespace i18n {
36
Min(sal_Int32 a,sal_Int32 b)37 inline sal_Int32 Min( sal_Int32 a, sal_Int32 b ) { return a > b ? b : a; }
38
39 sal_Bool SAL_CALL
equals(const OUString & str1,sal_Int32 pos1,sal_Int32 nCount1,sal_Int32 & nMatch1,const OUString & str2,sal_Int32 pos2,sal_Int32 nCount2,sal_Int32 & nMatch2)40 transliteration_Ignore::equals(const OUString& str1, sal_Int32 pos1, sal_Int32 nCount1, sal_Int32& nMatch1,
41 const OUString& str2, sal_Int32 pos2, sal_Int32 nCount2, sal_Int32& nMatch2 ) throw(RuntimeException)
42 {
43 Sequence< sal_Int32 > offset1;
44 Sequence< sal_Int32 > offset2;
45
46 // The method folding is defined in a sub class.
47 OUString s1 = this->folding( str1, pos1, nCount1, offset1);
48 OUString s2 = this->folding( str2, pos2, nCount2, offset2);
49
50 const sal_Unicode * p1 = s1.getStr();
51 const sal_Unicode * p2 = s2.getStr();
52 sal_Int32 length = Min(s1.getLength(), s2.getLength());
53 sal_Int32 nmatch;
54
55 for ( nmatch = 0; nmatch < length; nmatch++)
56 if (*p1++ != *p2++)
57 break;
58
59 if (nmatch > 0) {
60 nMatch1 = offset1[ nmatch - 1 ] + 1; // Subtract 1 from nmatch because the index starts from zero.
61 nMatch2 = offset2[ nmatch - 1 ] + 1; // And then, add 1 to position because it means the number of character matched.
62 }
63 else {
64 nMatch1 = 0; // No character was matched.
65 nMatch2 = 0;
66 }
67
68 return (nmatch == s1.getLength()) && (nmatch == s2.getLength());
69 }
70
71
72 Sequence< OUString > SAL_CALL
transliterateRange(const OUString & str1,const OUString & str2)73 transliteration_Ignore::transliterateRange( const OUString& str1, const OUString& str2 ) throw(RuntimeException)
74 {
75 if (str1.getLength() < 1 || str2.getLength() < 1)
76 throw RuntimeException();
77
78 Sequence< OUString > r(2);
79 r[0] = str1.copy(0, 1);
80 r[1] = str2.copy(0, 1);
81 return r;
82 }
83
84
85 sal_Int16 SAL_CALL
getType()86 transliteration_Ignore::getType() throw(RuntimeException)
87 {
88 // The type is also defined in com/sun/star/util/TransliterationType.hdl
89 return TransliterationType::IGNORE;
90 }
91
92
93 OUString SAL_CALL
transliterate(const OUString & inStr,sal_Int32 startPos,sal_Int32 nCount,Sequence<sal_Int32> & offset)94 transliteration_Ignore::transliterate( const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
95 Sequence< sal_Int32 >& offset ) throw(RuntimeException)
96 {
97 // The method folding is defined in a sub class.
98 return this->folding( inStr, startPos, nCount, offset);
99 }
100
101 Sequence< OUString > SAL_CALL
transliterateRange(const OUString & str1,const OUString & str2,XTransliteration & t1,XTransliteration & t2)102 transliteration_Ignore::transliterateRange( const OUString& str1, const OUString& str2,
103 XTransliteration& t1, XTransliteration& t2 ) throw(RuntimeException)
104 {
105 if (str1.getLength() < 1 || str2.getLength() < 1)
106 throw RuntimeException();
107
108 Sequence< sal_Int32 > offset;
109 OUString s11 = t1.transliterate( str1, 0, 1, offset );
110 OUString s12 = t1.transliterate( str2, 0, 1, offset );
111 OUString s21 = t2.transliterate( str1, 0, 1, offset );
112 OUString s22 = t2.transliterate( str2, 0, 1, offset );
113
114 if ( (s11 == s21) && (s12 == s22) ) {
115 Sequence< OUString > r(2);
116 r[0] = s11;
117 r[1] = s12;
118 return r;
119 }
120
121 Sequence< OUString > r(4);
122 r[0] = s11;
123 r[1] = s12;
124 r[2] = s21;
125 r[3] = s22;
126 return r;
127 }
128
129 OUString SAL_CALL
folding(const OUString & inStr,sal_Int32 startPos,sal_Int32 nCount,Sequence<sal_Int32> & offset)130 transliteration_Ignore::folding( const OUString& inStr, sal_Int32 startPos,
131 sal_Int32 nCount, Sequence< sal_Int32 >& offset)
132 throw(RuntimeException)
133 {
134 // Create a string buffer which can hold nCount + 1 characters.
135 // The reference count is 0 now.
136 rtl_uString * newStr = x_rtl_uString_new_WithLength( nCount ); // defined in x_rtl_ustring.h
137 sal_Unicode * dst = newStr->buffer;
138 const sal_Unicode * src = inStr.getStr() + startPos;
139
140 // Allocate nCount length to offset argument.
141 sal_Int32 *p = 0;
142 sal_Int32 position = 0;
143 if (useOffset) {
144 offset.realloc( nCount );
145 p = offset.getArray();
146 position = startPos;
147 }
148
149 if (map) {
150 sal_Unicode previousChar = *src ++;
151 sal_Unicode currentChar;
152
153 // Translation
154 while (-- nCount > 0) {
155 currentChar = *src ++;
156
157 Mapping *m;
158 for (m = map; m->replaceChar; m++) {
159 if (previousChar == m->previousChar && currentChar == m->currentChar ) {
160 if (useOffset) {
161 if (! m->two2one)
162 *p++ = position;
163 position++;
164 *p++ = position++;
165 }
166 *dst++ = m->replaceChar;
167 if (!m->two2one)
168 *dst++ = currentChar;
169 previousChar = *src++;
170 nCount--;
171 break;
172 }
173 }
174
175 if (! m->replaceChar) {
176 if (useOffset)
177 *p ++ = position ++;
178 *dst ++ = previousChar;
179 previousChar = currentChar;
180 }
181 }
182
183 if (nCount == 0) {
184 if (useOffset)
185 *p = position;
186 *dst ++ = previousChar;
187 }
188 } else {
189 // Translation
190 while (nCount -- > 0) {
191 sal_Unicode c = *src++;
192 c = func ? func( c) : (*table)[ c ];
193 if (c != 0xffff)
194 *dst ++ = c;
195 if (useOffset) {
196 if (c != 0xffff)
197 *p ++ = position;
198 position++;
199 }
200 }
201 }
202 newStr->length = sal_Int32(dst - newStr->buffer);
203 if (useOffset)
204 offset.realloc(newStr->length);
205 *dst = (sal_Unicode) 0;
206
207 return OUString( newStr, SAL_NO_ACQUIRE ); // take over ownership of <newStr>
208 }
209
210 sal_Unicode SAL_CALL
transliterateChar2Char(sal_Unicode inChar)211 transliteration_Ignore::transliterateChar2Char( sal_Unicode inChar) throw(RuntimeException, MultipleCharsOutputException)
212 {
213 return func ? func( inChar) : table ? (*table)[ inChar ] : inChar;
214 }
215
216 } } } }
217