Update ooo320-m1
[ooovba.git] / i18npool / source / transliteration / transliteration_Ignore.cxx
blob6921cf4746a3e293d71649cbf4c7f5ee7444730a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: transliteration_Ignore.cxx,v $
10 * $Revision: 1.13 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_i18npool.hxx"
34 // prevent internal compiler error with MSVC6SP3
35 #include <utility>
37 #include <transliteration_Ignore.hxx>
39 using namespace com::sun::star::uno;
40 using namespace rtl;
42 namespace com { namespace sun { namespace star { namespace i18n {
44 inline sal_Int32 Min( sal_Int32 a, sal_Int32 b ) { return a > b ? b : a; }
46 sal_Bool SAL_CALL
47 transliteration_Ignore::equals(const OUString& str1, sal_Int32 pos1, sal_Int32 nCount1, sal_Int32& nMatch1,
48 const OUString& str2, sal_Int32 pos2, sal_Int32 nCount2, sal_Int32& nMatch2 ) throw(RuntimeException)
50 Sequence< sal_Int32 > offset1;
51 Sequence< sal_Int32 > offset2;
53 // The method folding is defined in a sub class.
54 OUString s1 = this->folding( str1, pos1, nCount1, offset1);
55 OUString s2 = this->folding( str2, pos2, nCount2, offset2);
57 const sal_Unicode * p1 = s1.getStr();
58 const sal_Unicode * p2 = s2.getStr();
59 sal_Int32 length = Min(s1.getLength(), s2.getLength());
60 sal_Int32 nmatch;
62 for ( nmatch = 0; nmatch < length; nmatch++)
63 if (*p1++ != *p2++)
64 break;
66 if (nmatch > 0) {
67 nMatch1 = offset1[ nmatch - 1 ] + 1; // Subtract 1 from nmatch because the index starts from zero.
68 nMatch2 = offset2[ nmatch - 1 ] + 1; // And then, add 1 to position because it means the number of character matched.
70 else {
71 nMatch1 = 0; // No character was matched.
72 nMatch2 = 0;
75 return (nmatch == s1.getLength()) && (nmatch == s2.getLength());
79 Sequence< OUString > SAL_CALL
80 transliteration_Ignore::transliterateRange( const OUString& str1, const OUString& str2 ) throw(RuntimeException)
82 if (str1.getLength() < 1 || str2.getLength() < 1)
83 throw RuntimeException();
85 Sequence< OUString > r(2);
86 r[0] = str1.copy(0, 1);
87 r[1] = str2.copy(0, 1);
88 return r;
92 sal_Int16 SAL_CALL
93 transliteration_Ignore::getType() throw(RuntimeException)
95 // The type is also defined in com/sun/star/util/TransliterationType.hdl
96 return TransliterationType::IGNORE;
100 OUString SAL_CALL
101 transliteration_Ignore::transliterate( const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
102 Sequence< sal_Int32 >& offset ) throw(RuntimeException)
104 // The method folding is defined in a sub class.
105 return this->folding( inStr, startPos, nCount, offset);
108 Sequence< OUString > SAL_CALL
109 transliteration_Ignore::transliterateRange( const OUString& str1, const OUString& str2,
110 XTransliteration& t1, XTransliteration& t2 ) throw(RuntimeException)
112 if (str1.getLength() < 1 || str2.getLength() < 1)
113 throw RuntimeException();
115 Sequence< sal_Int32 > offset;
116 OUString s11 = t1.transliterate( str1, 0, 1, offset );
117 OUString s12 = t1.transliterate( str2, 0, 1, offset );
118 OUString s21 = t2.transliterate( str1, 0, 1, offset );
119 OUString s22 = t2.transliterate( str2, 0, 1, offset );
121 if ( (s11 == s21) && (s12 == s22) ) {
122 Sequence< OUString > r(2);
123 r[0] = s11;
124 r[1] = s12;
125 return r;
128 Sequence< OUString > r(4);
129 r[0] = s11;
130 r[1] = s12;
131 r[2] = s21;
132 r[3] = s22;
133 return r;
136 OUString SAL_CALL
137 transliteration_Ignore::folding( const OUString& inStr, sal_Int32 startPos,
138 sal_Int32 nCount, Sequence< sal_Int32 >& offset)
139 throw(RuntimeException)
141 // Create a string buffer which can hold nCount + 1 characters.
142 // The reference count is 0 now.
143 rtl_uString * newStr = x_rtl_uString_new_WithLength( nCount ); // defined in x_rtl_ustring.h
144 sal_Unicode * dst = newStr->buffer;
145 const sal_Unicode * src = inStr.getStr() + startPos;
147 // Allocate nCount length to offset argument.
148 sal_Int32 *p = 0;
149 sal_Int32 position = 0;
150 if (useOffset) {
151 offset.realloc( nCount );
152 p = offset.getArray();
153 position = startPos;
156 if (map) {
157 sal_Unicode previousChar = *src ++;
158 sal_Unicode currentChar;
160 // Translation
161 while (-- nCount > 0) {
162 currentChar = *src ++;
164 Mapping *m;
165 for (m = map; m->replaceChar; m++) {
166 if (previousChar == m->previousChar && currentChar == m->currentChar ) {
167 if (useOffset) {
168 if (! m->two2one)
169 *p++ = position;
170 position++;
171 *p++ = position++;
173 *dst++ = m->replaceChar;
174 if (!m->two2one)
175 *dst++ = currentChar;
176 previousChar = *src++;
177 nCount--;
178 break;
182 if (! m->replaceChar) {
183 if (useOffset)
184 *p ++ = position ++;
185 *dst ++ = previousChar;
186 previousChar = currentChar;
190 if (nCount == 0) {
191 if (useOffset)
192 *p = position;
193 *dst ++ = previousChar;
195 } else {
196 // Translation
197 while (nCount -- > 0) {
198 sal_Unicode c = *src++;
199 c = func ? func( c) : (*table)[ c ];
200 if (c != 0xffff)
201 *dst ++ = c;
202 if (useOffset) {
203 if (c != 0xffff)
204 *p ++ = position;
205 position++;
209 newStr->length = sal_Int32(dst - newStr->buffer);
210 if (useOffset)
211 offset.realloc(newStr->length);
212 *dst = (sal_Unicode) 0;
214 return OUString( newStr ); // defined in rtl/usrting. The reference count is increased from 0 to 1.
217 sal_Unicode SAL_CALL
218 transliteration_Ignore::transliterateChar2Char( sal_Unicode inChar) throw(RuntimeException, MultipleCharsOutputException)
220 return func ? func( inChar) : table ? (*table)[ inChar ] : inChar;
223 } } } }