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