Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / i18npool / source / transliteration / transliteration_Ignore.cxx
blob0d7e8833da06336b1d55807e575e2779a0a68bab
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 .
21 #include <utility>
22 #include <comphelper/string.hxx>
23 #include <transliteration_Ignore.hxx>
25 using namespace com::sun::star::uno;
26 using ::rtl::OUString;
28 namespace com { namespace sun { namespace star { namespace i18n {
30 inline sal_Int32 Min( sal_Int32 a, sal_Int32 b ) { return a > b ? b : a; }
32 sal_Bool SAL_CALL
33 transliteration_Ignore::equals(const OUString& str1, sal_Int32 pos1, sal_Int32 nCount1, sal_Int32& nMatch1,
34 const OUString& str2, sal_Int32 pos2, sal_Int32 nCount2, sal_Int32& nMatch2 ) throw(RuntimeException)
36 Sequence< sal_Int32 > offset1;
37 Sequence< sal_Int32 > offset2;
39 // The method folding is defined in a sub class.
40 OUString s1 = this->folding( str1, pos1, nCount1, offset1);
41 OUString s2 = this->folding( str2, pos2, nCount2, offset2);
43 const sal_Unicode * p1 = s1.getStr();
44 const sal_Unicode * p2 = s2.getStr();
45 sal_Int32 length = Min(s1.getLength(), s2.getLength());
46 sal_Int32 nmatch;
48 for ( nmatch = 0; nmatch < length; nmatch++)
49 if (*p1++ != *p2++)
50 break;
52 if (nmatch > 0) {
53 nMatch1 = offset1[ nmatch - 1 ] + 1; // Subtract 1 from nmatch because the index starts from zero.
54 nMatch2 = offset2[ nmatch - 1 ] + 1; // And then, add 1 to position because it means the number of character matched.
56 else {
57 nMatch1 = 0; // No character was matched.
58 nMatch2 = 0;
61 return (nmatch == s1.getLength()) && (nmatch == s2.getLength());
65 Sequence< OUString > SAL_CALL
66 transliteration_Ignore::transliterateRange( const OUString& str1, const OUString& str2 ) throw(RuntimeException)
68 if (str1.isEmpty() || str2.isEmpty())
69 throw RuntimeException();
71 Sequence< OUString > r(2);
72 r[0] = str1.copy(0, 1);
73 r[1] = str2.copy(0, 1);
74 return r;
78 sal_Int16 SAL_CALL
79 transliteration_Ignore::getType() throw(RuntimeException)
81 // The type is also defined in com/sun/star/util/TransliterationType.hdl
82 return TransliterationType::IGNORE;
86 OUString SAL_CALL
87 transliteration_Ignore::transliterate( const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
88 Sequence< sal_Int32 >& offset ) throw(RuntimeException)
90 // The method folding is defined in a sub class.
91 return this->folding( inStr, startPos, nCount, offset);
94 Sequence< OUString > SAL_CALL
95 transliteration_Ignore::transliterateRange( const OUString& str1, const OUString& str2,
96 XTransliteration& t1, XTransliteration& t2 ) throw(RuntimeException)
98 if (str1.isEmpty() || str2.isEmpty())
99 throw RuntimeException();
101 Sequence< sal_Int32 > offset;
102 OUString s11 = t1.transliterate( str1, 0, 1, offset );
103 OUString s12 = t1.transliterate( str2, 0, 1, offset );
104 OUString s21 = t2.transliterate( str1, 0, 1, offset );
105 OUString s22 = t2.transliterate( str2, 0, 1, offset );
107 if ( (s11 == s21) && (s12 == s22) ) {
108 Sequence< OUString > r(2);
109 r[0] = s11;
110 r[1] = s12;
111 return r;
114 Sequence< OUString > r(4);
115 r[0] = s11;
116 r[1] = s12;
117 r[2] = s21;
118 r[3] = s22;
119 return r;
122 OUString SAL_CALL
123 transliteration_Ignore::folding( const OUString& inStr, sal_Int32 startPos,
124 sal_Int32 nCount, Sequence< sal_Int32 >& offset)
125 throw(RuntimeException)
127 // Create a string buffer which can hold nCount + 1 characters.
128 // The reference count is 1 now.
129 rtl_uString * newStr = comphelper::string::rtl_uString_alloc(nCount);
130 sal_Unicode * dst = newStr->buffer;
131 const sal_Unicode * src = inStr.getStr() + startPos;
133 // Allocate nCount length to offset argument.
134 sal_Int32 *p = 0;
135 sal_Int32 position = 0;
136 if (useOffset) {
137 offset.realloc( nCount );
138 p = offset.getArray();
139 position = startPos;
142 if (map) {
143 sal_Unicode previousChar = *src ++;
144 sal_Unicode currentChar;
146 // Translation
147 while (-- nCount > 0) {
148 currentChar = *src ++;
150 Mapping *m;
151 for (m = map; m->replaceChar; m++) {
152 if (previousChar == m->previousChar && currentChar == m->currentChar ) {
153 if (useOffset) {
154 if (! m->two2one)
155 *p++ = position;
156 position++;
157 *p++ = position++;
159 *dst++ = m->replaceChar;
160 if (!m->two2one)
161 *dst++ = currentChar;
162 previousChar = *src++;
163 nCount--;
164 break;
168 if (! m->replaceChar) {
169 if (useOffset)
170 *p ++ = position ++;
171 *dst ++ = previousChar;
172 previousChar = currentChar;
176 if (nCount == 0) {
177 if (useOffset)
178 *p = position;
179 *dst ++ = previousChar;
181 } else {
182 // Translation
183 while (nCount -- > 0) {
184 sal_Unicode c = *src++;
185 c = func ? func( c) : (*table)[ c ];
186 if (c != 0xffff)
187 *dst ++ = c;
188 if (useOffset) {
189 if (c != 0xffff)
190 *p ++ = position;
191 position++;
195 newStr->length = sal_Int32(dst - newStr->buffer);
196 if (useOffset)
197 offset.realloc(newStr->length);
198 *dst = (sal_Unicode) 0;
200 return OUString(newStr, SAL_NO_ACQUIRE); // take ownership
203 sal_Unicode SAL_CALL
204 transliteration_Ignore::transliterateChar2Char( sal_Unicode inChar) throw(RuntimeException, MultipleCharsOutputException)
206 return func ? func( inChar) : table ? (*table)[ inChar ] : inChar;
209 } } } }
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */