1*449ab281SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*449ab281SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*449ab281SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*449ab281SAndrew Rist * distributed with this work for additional information 6*449ab281SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*449ab281SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*449ab281SAndrew Rist * "License"); you may not use this file except in compliance 9*449ab281SAndrew Rist * with the License. You may obtain a copy of the License at 10*449ab281SAndrew Rist * 11*449ab281SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*449ab281SAndrew Rist * 13*449ab281SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*449ab281SAndrew Rist * software distributed under the License is distributed on an 15*449ab281SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*449ab281SAndrew Rist * KIND, either express or implied. See the License for the 17*449ab281SAndrew Rist * specific language governing permissions and limitations 18*449ab281SAndrew Rist * under the License. 19*449ab281SAndrew Rist * 20*449ab281SAndrew Rist *************************************************************/ 21*449ab281SAndrew Rist 22*449ab281SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_i18npool.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir // prevent internal compiler error with MSVC6SP3 28cdf0e10cSrcweir #include <utility> 29cdf0e10cSrcweir 30cdf0e10cSrcweir #include <transliteration_Ignore.hxx> 31cdf0e10cSrcweir 32cdf0e10cSrcweir using namespace com::sun::star::uno; 33cdf0e10cSrcweir using namespace rtl; 34cdf0e10cSrcweir 35cdf0e10cSrcweir namespace com { namespace sun { namespace star { namespace i18n { 36cdf0e10cSrcweir 37cdf0e10cSrcweir inline sal_Int32 Min( sal_Int32 a, sal_Int32 b ) { return a > b ? b : a; } 38cdf0e10cSrcweir 39cdf0e10cSrcweir sal_Bool SAL_CALL 40cdf0e10cSrcweir transliteration_Ignore::equals(const OUString& str1, sal_Int32 pos1, sal_Int32 nCount1, sal_Int32& nMatch1, 41cdf0e10cSrcweir const OUString& str2, sal_Int32 pos2, sal_Int32 nCount2, sal_Int32& nMatch2 ) throw(RuntimeException) 42cdf0e10cSrcweir { 43cdf0e10cSrcweir Sequence< sal_Int32 > offset1; 44cdf0e10cSrcweir Sequence< sal_Int32 > offset2; 45cdf0e10cSrcweir 46cdf0e10cSrcweir // The method folding is defined in a sub class. 47cdf0e10cSrcweir OUString s1 = this->folding( str1, pos1, nCount1, offset1); 48cdf0e10cSrcweir OUString s2 = this->folding( str2, pos2, nCount2, offset2); 49cdf0e10cSrcweir 50cdf0e10cSrcweir const sal_Unicode * p1 = s1.getStr(); 51cdf0e10cSrcweir const sal_Unicode * p2 = s2.getStr(); 52cdf0e10cSrcweir sal_Int32 length = Min(s1.getLength(), s2.getLength()); 53cdf0e10cSrcweir sal_Int32 nmatch; 54cdf0e10cSrcweir 55cdf0e10cSrcweir for ( nmatch = 0; nmatch < length; nmatch++) 56cdf0e10cSrcweir if (*p1++ != *p2++) 57cdf0e10cSrcweir break; 58cdf0e10cSrcweir 59cdf0e10cSrcweir if (nmatch > 0) { 60cdf0e10cSrcweir nMatch1 = offset1[ nmatch - 1 ] + 1; // Subtract 1 from nmatch because the index starts from zero. 61cdf0e10cSrcweir nMatch2 = offset2[ nmatch - 1 ] + 1; // And then, add 1 to position because it means the number of character matched. 62cdf0e10cSrcweir } 63cdf0e10cSrcweir else { 64cdf0e10cSrcweir nMatch1 = 0; // No character was matched. 65cdf0e10cSrcweir nMatch2 = 0; 66cdf0e10cSrcweir } 67cdf0e10cSrcweir 68cdf0e10cSrcweir return (nmatch == s1.getLength()) && (nmatch == s2.getLength()); 69cdf0e10cSrcweir } 70cdf0e10cSrcweir 71cdf0e10cSrcweir 72cdf0e10cSrcweir Sequence< OUString > SAL_CALL 73cdf0e10cSrcweir transliteration_Ignore::transliterateRange( const OUString& str1, const OUString& str2 ) throw(RuntimeException) 74cdf0e10cSrcweir { 75cdf0e10cSrcweir if (str1.getLength() < 1 || str2.getLength() < 1) 76cdf0e10cSrcweir throw RuntimeException(); 77cdf0e10cSrcweir 78cdf0e10cSrcweir Sequence< OUString > r(2); 79cdf0e10cSrcweir r[0] = str1.copy(0, 1); 80cdf0e10cSrcweir r[1] = str2.copy(0, 1); 81cdf0e10cSrcweir return r; 82cdf0e10cSrcweir } 83cdf0e10cSrcweir 84cdf0e10cSrcweir 85cdf0e10cSrcweir sal_Int16 SAL_CALL 86cdf0e10cSrcweir transliteration_Ignore::getType() throw(RuntimeException) 87cdf0e10cSrcweir { 88cdf0e10cSrcweir // The type is also defined in com/sun/star/util/TransliterationType.hdl 89cdf0e10cSrcweir return TransliterationType::IGNORE; 90cdf0e10cSrcweir } 91cdf0e10cSrcweir 92cdf0e10cSrcweir 93cdf0e10cSrcweir OUString SAL_CALL 94cdf0e10cSrcweir transliteration_Ignore::transliterate( const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount, 95cdf0e10cSrcweir Sequence< sal_Int32 >& offset ) throw(RuntimeException) 96cdf0e10cSrcweir { 97cdf0e10cSrcweir // The method folding is defined in a sub class. 98cdf0e10cSrcweir return this->folding( inStr, startPos, nCount, offset); 99cdf0e10cSrcweir } 100cdf0e10cSrcweir 101cdf0e10cSrcweir Sequence< OUString > SAL_CALL 102cdf0e10cSrcweir transliteration_Ignore::transliterateRange( const OUString& str1, const OUString& str2, 103cdf0e10cSrcweir XTransliteration& t1, XTransliteration& t2 ) throw(RuntimeException) 104cdf0e10cSrcweir { 105cdf0e10cSrcweir if (str1.getLength() < 1 || str2.getLength() < 1) 106cdf0e10cSrcweir throw RuntimeException(); 107cdf0e10cSrcweir 108cdf0e10cSrcweir Sequence< sal_Int32 > offset; 109cdf0e10cSrcweir OUString s11 = t1.transliterate( str1, 0, 1, offset ); 110cdf0e10cSrcweir OUString s12 = t1.transliterate( str2, 0, 1, offset ); 111cdf0e10cSrcweir OUString s21 = t2.transliterate( str1, 0, 1, offset ); 112cdf0e10cSrcweir OUString s22 = t2.transliterate( str2, 0, 1, offset ); 113cdf0e10cSrcweir 114cdf0e10cSrcweir if ( (s11 == s21) && (s12 == s22) ) { 115cdf0e10cSrcweir Sequence< OUString > r(2); 116cdf0e10cSrcweir r[0] = s11; 117cdf0e10cSrcweir r[1] = s12; 118cdf0e10cSrcweir return r; 119cdf0e10cSrcweir } 120cdf0e10cSrcweir 121cdf0e10cSrcweir Sequence< OUString > r(4); 122cdf0e10cSrcweir r[0] = s11; 123cdf0e10cSrcweir r[1] = s12; 124cdf0e10cSrcweir r[2] = s21; 125cdf0e10cSrcweir r[3] = s22; 126cdf0e10cSrcweir return r; 127cdf0e10cSrcweir } 128cdf0e10cSrcweir 129cdf0e10cSrcweir OUString SAL_CALL 130cdf0e10cSrcweir transliteration_Ignore::folding( const OUString& inStr, sal_Int32 startPos, 131cdf0e10cSrcweir sal_Int32 nCount, Sequence< sal_Int32 >& offset) 132cdf0e10cSrcweir throw(RuntimeException) 133cdf0e10cSrcweir { 134cdf0e10cSrcweir // Create a string buffer which can hold nCount + 1 characters. 135cdf0e10cSrcweir // The reference count is 0 now. 136cdf0e10cSrcweir rtl_uString * newStr = x_rtl_uString_new_WithLength( nCount ); // defined in x_rtl_ustring.h 137cdf0e10cSrcweir sal_Unicode * dst = newStr->buffer; 138cdf0e10cSrcweir const sal_Unicode * src = inStr.getStr() + startPos; 139cdf0e10cSrcweir 140cdf0e10cSrcweir // Allocate nCount length to offset argument. 141cdf0e10cSrcweir sal_Int32 *p = 0; 142cdf0e10cSrcweir sal_Int32 position = 0; 143cdf0e10cSrcweir if (useOffset) { 144cdf0e10cSrcweir offset.realloc( nCount ); 145cdf0e10cSrcweir p = offset.getArray(); 146cdf0e10cSrcweir position = startPos; 147cdf0e10cSrcweir } 148cdf0e10cSrcweir 149cdf0e10cSrcweir if (map) { 150cdf0e10cSrcweir sal_Unicode previousChar = *src ++; 151cdf0e10cSrcweir sal_Unicode currentChar; 152cdf0e10cSrcweir 153cdf0e10cSrcweir // Translation 154cdf0e10cSrcweir while (-- nCount > 0) { 155cdf0e10cSrcweir currentChar = *src ++; 156cdf0e10cSrcweir 157cdf0e10cSrcweir Mapping *m; 158cdf0e10cSrcweir for (m = map; m->replaceChar; m++) { 159cdf0e10cSrcweir if (previousChar == m->previousChar && currentChar == m->currentChar ) { 160cdf0e10cSrcweir if (useOffset) { 161cdf0e10cSrcweir if (! m->two2one) 162cdf0e10cSrcweir *p++ = position; 163cdf0e10cSrcweir position++; 164cdf0e10cSrcweir *p++ = position++; 165cdf0e10cSrcweir } 166cdf0e10cSrcweir *dst++ = m->replaceChar; 167cdf0e10cSrcweir if (!m->two2one) 168cdf0e10cSrcweir *dst++ = currentChar; 169cdf0e10cSrcweir previousChar = *src++; 170cdf0e10cSrcweir nCount--; 171cdf0e10cSrcweir break; 172cdf0e10cSrcweir } 173cdf0e10cSrcweir } 174cdf0e10cSrcweir 175cdf0e10cSrcweir if (! m->replaceChar) { 176cdf0e10cSrcweir if (useOffset) 177cdf0e10cSrcweir *p ++ = position ++; 178cdf0e10cSrcweir *dst ++ = previousChar; 179cdf0e10cSrcweir previousChar = currentChar; 180cdf0e10cSrcweir } 181cdf0e10cSrcweir } 182cdf0e10cSrcweir 183cdf0e10cSrcweir if (nCount == 0) { 184cdf0e10cSrcweir if (useOffset) 185cdf0e10cSrcweir *p = position; 186cdf0e10cSrcweir *dst ++ = previousChar; 187cdf0e10cSrcweir } 188cdf0e10cSrcweir } else { 189cdf0e10cSrcweir // Translation 190cdf0e10cSrcweir while (nCount -- > 0) { 191cdf0e10cSrcweir sal_Unicode c = *src++; 192cdf0e10cSrcweir c = func ? func( c) : (*table)[ c ]; 193cdf0e10cSrcweir if (c != 0xffff) 194cdf0e10cSrcweir *dst ++ = c; 195cdf0e10cSrcweir if (useOffset) { 196cdf0e10cSrcweir if (c != 0xffff) 197cdf0e10cSrcweir *p ++ = position; 198cdf0e10cSrcweir position++; 199cdf0e10cSrcweir } 200cdf0e10cSrcweir } 201cdf0e10cSrcweir } 202cdf0e10cSrcweir newStr->length = sal_Int32(dst - newStr->buffer); 203cdf0e10cSrcweir if (useOffset) 204cdf0e10cSrcweir offset.realloc(newStr->length); 205cdf0e10cSrcweir *dst = (sal_Unicode) 0; 206cdf0e10cSrcweir 207cdf0e10cSrcweir return OUString( newStr ); // defined in rtl/usrting. The reference count is increased from 0 to 1. 208cdf0e10cSrcweir } 209cdf0e10cSrcweir 210cdf0e10cSrcweir sal_Unicode SAL_CALL 211cdf0e10cSrcweir transliteration_Ignore::transliterateChar2Char( sal_Unicode inChar) throw(RuntimeException, MultipleCharsOutputException) 212cdf0e10cSrcweir { 213cdf0e10cSrcweir return func ? func( inChar) : table ? (*table)[ inChar ] : inChar; 214cdf0e10cSrcweir } 215cdf0e10cSrcweir 216cdf0e10cSrcweir } } } } 217