xref: /trunk/main/i18npool/source/transliteration/transliteration_Ignore.cxx (revision 9d37da743abb7db0a497688391f6e55a19f27c44)
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 
37 inline sal_Int32 Min( sal_Int32 a, sal_Int32 b ) { return a > b ? b : a; }
38 
39 sal_Bool SAL_CALL
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 )
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
73 transliteration_Ignore::transliterateRange( const OUString& str1, const OUString& str2 )
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
86 transliteration_Ignore::getType()
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
94 transliteration_Ignore::transliterate( const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
95         Sequence< sal_Int32 >& offset  )
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
102 transliteration_Ignore::transliterateRange( const OUString& str1, const OUString& str2,
103         XTransliteration& t1, XTransliteration& t2 )
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
130 transliteration_Ignore::folding( const OUString& inStr, sal_Int32 startPos,
131     sal_Int32 nCount, Sequence< sal_Int32 >& offset)
132 {
133     // Create a string buffer which can hold nCount + 1 characters.
134     // The reference count is 0 now.
135     rtl_uString * newStr = x_rtl_uString_new_WithLength( nCount ); // defined in x_rtl_ustring.h
136     sal_Unicode * dst = newStr->buffer;
137     const sal_Unicode * src = inStr.getStr() + startPos;
138 
139     // Allocate nCount length to offset argument.
140     sal_Int32 *p = 0;
141     sal_Int32 position = 0;
142     if (useOffset) {
143         offset.realloc( nCount );
144         p = offset.getArray();
145         position = startPos;
146     }
147 
148     if (map) {
149         sal_Unicode previousChar = *src ++;
150         sal_Unicode currentChar;
151 
152         // Translation
153         while (-- nCount > 0) {
154             currentChar = *src ++;
155 
156             Mapping *m;
157             for (m = map; m->replaceChar; m++) {
158                 if (previousChar == m->previousChar &&  currentChar == m->currentChar ) {
159                     if (useOffset) {
160                         if (! m->two2one)
161                             *p++ = position;
162                         position++;
163                         *p++ = position++;
164                     }
165                     *dst++ = m->replaceChar;
166                     if (!m->two2one)
167                         *dst++ = currentChar;
168                     previousChar = *src++;
169                     nCount--;
170                     break;
171                 }
172             }
173 
174             if (! m->replaceChar) {
175                 if (useOffset)
176                     *p ++ = position ++;
177                 *dst ++ = previousChar;
178                 previousChar = currentChar;
179             }
180         }
181 
182         if (nCount == 0) {
183             if (useOffset)
184                 *p = position;
185             *dst ++ = previousChar;
186         }
187     } else {
188         // Translation
189         while (nCount -- > 0) {
190             sal_Unicode c = *src++;
191             c = func ? func( c) : (*table)[ c ];
192             if (c != 0xffff)
193                 *dst ++ = c;
194             if (useOffset) {
195                 if (c != 0xffff)
196                     *p ++ = position;
197                 position++;
198             }
199         }
200     }
201     newStr->length = sal_Int32(dst - newStr->buffer);
202     if (useOffset)
203       offset.realloc(newStr->length);
204     *dst = (sal_Unicode) 0;
205 
206     return OUString( newStr, SAL_NO_ACQUIRE ); // take over ownership of <newStr>
207 }
208 
209 sal_Unicode SAL_CALL
210 transliteration_Ignore::transliterateChar2Char( sal_Unicode inChar)
211 {
212     return func ? func( inChar) : table ? (*table)[ inChar ] : inChar;
213 }
214 
215 } } } }
216