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