Bump version to 4.3-4
[LibreOffice.git] / i18npool / source / transliteration / transliteration_Ignore.cxx
blobdc2f251dd11f2616574b4aa20368ac65e6b8e80e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <transliteration_Ignore.hxx>
22 using namespace com::sun::star::uno;
24 namespace com { namespace sun { namespace star { namespace i18n {
26 inline sal_Int32 Min( sal_Int32 a, sal_Int32 b ) { return a > b ? b : a; }
28 sal_Bool SAL_CALL
29 transliteration_Ignore::equals(const OUString& str1, sal_Int32 pos1, sal_Int32 nCount1, sal_Int32& nMatch1,
30 const OUString& str2, sal_Int32 pos2, sal_Int32 nCount2, sal_Int32& nMatch2 ) throw(RuntimeException, std::exception)
32 Sequence< sal_Int32 > offset1;
33 Sequence< sal_Int32 > offset2;
35 // The method folding is defined in a sub class.
36 OUString s1 = this->folding( str1, pos1, nCount1, offset1);
37 OUString s2 = this->folding( str2, pos2, nCount2, offset2);
39 const sal_Unicode * p1 = s1.getStr();
40 const sal_Unicode * p2 = s2.getStr();
41 sal_Int32 length = Min(s1.getLength(), s2.getLength());
42 sal_Int32 nmatch;
44 for ( nmatch = 0; nmatch < length; nmatch++)
45 if (*p1++ != *p2++)
46 break;
48 if (nmatch > 0) {
49 nMatch1 = offset1[ nmatch - 1 ] + 1; // Subtract 1 from nmatch because the index starts from zero.
50 nMatch2 = offset2[ nmatch - 1 ] + 1; // And then, add 1 to position because it means the number of character matched.
52 else {
53 nMatch1 = 0; // No character was matched.
54 nMatch2 = 0;
57 return (nmatch == s1.getLength()) && (nmatch == s2.getLength());
61 Sequence< OUString > SAL_CALL
62 transliteration_Ignore::transliterateRange( const OUString& str1, const OUString& str2 ) throw(RuntimeException, std::exception)
64 if (str1.isEmpty() || str2.isEmpty())
65 throw RuntimeException();
67 Sequence< OUString > r(2);
68 r[0] = str1.copy(0, 1);
69 r[1] = str2.copy(0, 1);
70 return r;
74 sal_Int16 SAL_CALL
75 transliteration_Ignore::getType() throw(RuntimeException, std::exception)
77 // The type is also defined in com/sun/star/util/TransliterationType.hdl
78 return TransliterationType::IGNORE;
82 OUString SAL_CALL
83 transliteration_Ignore::transliterate( const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
84 Sequence< sal_Int32 >& offset ) throw(RuntimeException, std::exception)
86 // The method folding is defined in a sub class.
87 return this->folding( inStr, startPos, nCount, offset);
90 Sequence< OUString > SAL_CALL
91 transliteration_Ignore::transliterateRange( const OUString& str1, const OUString& str2,
92 XTransliteration& t1, XTransliteration& t2 ) throw(RuntimeException)
94 if (str1.isEmpty() || str2.isEmpty())
95 throw RuntimeException();
97 Sequence< sal_Int32 > offset;
98 OUString s11 = t1.transliterate( str1, 0, 1, offset );
99 OUString s12 = t1.transliterate( str2, 0, 1, offset );
100 OUString s21 = t2.transliterate( str1, 0, 1, offset );
101 OUString s22 = t2.transliterate( str2, 0, 1, offset );
103 if ( (s11 == s21) && (s12 == s22) ) {
104 Sequence< OUString > r(2);
105 r[0] = s11;
106 r[1] = s12;
107 return r;
110 Sequence< OUString > r(4);
111 r[0] = s11;
112 r[1] = s12;
113 r[2] = s21;
114 r[3] = s22;
115 return r;
118 OUString SAL_CALL
119 transliteration_Ignore::folding( const OUString& inStr, sal_Int32 startPos,
120 sal_Int32 nCount, Sequence< sal_Int32 >& offset)
121 throw(RuntimeException, std::exception)
123 // Create a string buffer which can hold nCount + 1 characters.
124 // The reference count is 1 now.
125 rtl_uString * newStr = rtl_uString_alloc(nCount);
126 sal_Unicode * dst = newStr->buffer;
127 const sal_Unicode * src = inStr.getStr() + startPos;
129 // Allocate nCount length to offset argument.
130 sal_Int32 *p = 0;
131 sal_Int32 position = 0;
132 if (useOffset) {
133 offset.realloc( nCount );
134 p = offset.getArray();
135 position = startPos;
138 if (map) {
139 sal_Unicode previousChar = *src ++;
140 sal_Unicode currentChar;
142 // Translation
143 while (-- nCount > 0) {
144 currentChar = *src ++;
146 const Mapping *m;
147 for (m = map; m->replaceChar; m++) {
148 if (previousChar == m->previousChar && currentChar == m->currentChar ) {
149 if (useOffset) {
150 if (! m->two2one)
151 *p++ = position;
152 position++;
153 *p++ = position++;
155 *dst++ = m->replaceChar;
156 if (!m->two2one)
157 *dst++ = currentChar;
158 previousChar = *src++;
159 nCount--;
160 break;
164 if (! m->replaceChar) {
165 if (useOffset)
166 *p ++ = position ++;
167 *dst ++ = previousChar;
168 previousChar = currentChar;
172 if (nCount == 0) {
173 if (useOffset)
174 *p = position;
175 *dst ++ = previousChar;
177 } else {
178 // Translation
179 while (nCount -- > 0) {
180 sal_Unicode c = *src++;
181 c = func ? func( c) : (*table)[ c ];
182 if (c != 0xffff)
183 *dst ++ = c;
184 if (useOffset) {
185 if (c != 0xffff)
186 *p ++ = position;
187 position++;
191 newStr->length = sal_Int32(dst - newStr->buffer);
192 if (useOffset)
193 offset.realloc(newStr->length);
194 *dst = (sal_Unicode) 0;
196 return OUString(newStr, SAL_NO_ACQUIRE); // take ownership
199 sal_Unicode SAL_CALL
200 transliteration_Ignore::transliterateChar2Char( sal_Unicode inChar) throw(RuntimeException, MultipleCharsOutputException, std::exception)
202 return func ? func( inChar) : table ? (*table)[ inChar ] : inChar;
205 } } } }
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */