Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <utility>
22 : #include <comphelper/string.hxx>
23 : #include <transliteration_Ignore.hxx>
24 :
25 : using namespace com::sun::star::uno;
26 :
27 : namespace com { namespace sun { namespace star { namespace i18n {
28 :
29 0 : inline sal_Int32 Min( sal_Int32 a, sal_Int32 b ) { return a > b ? b : a; }
30 :
31 : sal_Bool SAL_CALL
32 0 : transliteration_Ignore::equals(const OUString& str1, sal_Int32 pos1, sal_Int32 nCount1, sal_Int32& nMatch1,
33 : const OUString& str2, sal_Int32 pos2, sal_Int32 nCount2, sal_Int32& nMatch2 ) throw(RuntimeException, std::exception)
34 : {
35 0 : Sequence< sal_Int32 > offset1;
36 0 : Sequence< sal_Int32 > offset2;
37 :
38 : // The method folding is defined in a sub class.
39 0 : OUString s1 = this->folding( str1, pos1, nCount1, offset1);
40 0 : OUString s2 = this->folding( str2, pos2, nCount2, offset2);
41 :
42 0 : const sal_Unicode * p1 = s1.getStr();
43 0 : const sal_Unicode * p2 = s2.getStr();
44 0 : sal_Int32 length = Min(s1.getLength(), s2.getLength());
45 : sal_Int32 nmatch;
46 :
47 0 : for ( nmatch = 0; nmatch < length; nmatch++)
48 0 : if (*p1++ != *p2++)
49 0 : break;
50 :
51 0 : if (nmatch > 0) {
52 0 : nMatch1 = offset1[ nmatch - 1 ] + 1; // Subtract 1 from nmatch because the index starts from zero.
53 0 : nMatch2 = offset2[ nmatch - 1 ] + 1; // And then, add 1 to position because it means the number of character matched.
54 : }
55 : else {
56 0 : nMatch1 = 0; // No character was matched.
57 0 : nMatch2 = 0;
58 : }
59 :
60 0 : return (nmatch == s1.getLength()) && (nmatch == s2.getLength());
61 : }
62 :
63 :
64 : Sequence< OUString > SAL_CALL
65 0 : transliteration_Ignore::transliterateRange( const OUString& str1, const OUString& str2 ) throw(RuntimeException, std::exception)
66 : {
67 0 : if (str1.isEmpty() || str2.isEmpty())
68 0 : throw RuntimeException();
69 :
70 0 : Sequence< OUString > r(2);
71 0 : r[0] = str1.copy(0, 1);
72 0 : r[1] = str2.copy(0, 1);
73 0 : return r;
74 : }
75 :
76 :
77 : sal_Int16 SAL_CALL
78 0 : transliteration_Ignore::getType() throw(RuntimeException, std::exception)
79 : {
80 : // The type is also defined in com/sun/star/util/TransliterationType.hdl
81 0 : return TransliterationType::IGNORE;
82 : }
83 :
84 :
85 : OUString SAL_CALL
86 0 : transliteration_Ignore::transliterate( const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
87 : Sequence< sal_Int32 >& offset ) throw(RuntimeException, std::exception)
88 : {
89 : // The method folding is defined in a sub class.
90 0 : return this->folding( inStr, startPos, nCount, offset);
91 : }
92 :
93 : Sequence< OUString > SAL_CALL
94 0 : transliteration_Ignore::transliterateRange( const OUString& str1, const OUString& str2,
95 : XTransliteration& t1, XTransliteration& t2 ) throw(RuntimeException)
96 : {
97 0 : if (str1.isEmpty() || str2.isEmpty())
98 0 : throw RuntimeException();
99 :
100 0 : Sequence< sal_Int32 > offset;
101 0 : OUString s11 = t1.transliterate( str1, 0, 1, offset );
102 0 : OUString s12 = t1.transliterate( str2, 0, 1, offset );
103 0 : OUString s21 = t2.transliterate( str1, 0, 1, offset );
104 0 : OUString s22 = t2.transliterate( str2, 0, 1, offset );
105 :
106 0 : if ( (s11 == s21) && (s12 == s22) ) {
107 0 : Sequence< OUString > r(2);
108 0 : r[0] = s11;
109 0 : r[1] = s12;
110 0 : return r;
111 : }
112 :
113 0 : Sequence< OUString > r(4);
114 0 : r[0] = s11;
115 0 : r[1] = s12;
116 0 : r[2] = s21;
117 0 : r[3] = s22;
118 0 : return r;
119 : }
120 :
121 : OUString SAL_CALL
122 0 : transliteration_Ignore::folding( const OUString& inStr, sal_Int32 startPos,
123 : sal_Int32 nCount, Sequence< sal_Int32 >& offset)
124 : throw(RuntimeException, std::exception)
125 : {
126 : // Create a string buffer which can hold nCount + 1 characters.
127 : // The reference count is 1 now.
128 0 : rtl_uString * newStr = rtl_uString_alloc(nCount);
129 0 : sal_Unicode * dst = newStr->buffer;
130 0 : const sal_Unicode * src = inStr.getStr() + startPos;
131 :
132 : // Allocate nCount length to offset argument.
133 0 : sal_Int32 *p = 0;
134 0 : sal_Int32 position = 0;
135 0 : if (useOffset) {
136 0 : offset.realloc( nCount );
137 0 : p = offset.getArray();
138 0 : position = startPos;
139 : }
140 :
141 0 : if (map) {
142 0 : sal_Unicode previousChar = *src ++;
143 : sal_Unicode currentChar;
144 :
145 : // Translation
146 0 : while (-- nCount > 0) {
147 0 : currentChar = *src ++;
148 :
149 : const Mapping *m;
150 0 : for (m = map; m->replaceChar; m++) {
151 0 : if (previousChar == m->previousChar && currentChar == m->currentChar ) {
152 0 : if (useOffset) {
153 0 : if (! m->two2one)
154 0 : *p++ = position;
155 0 : position++;
156 0 : *p++ = position++;
157 : }
158 0 : *dst++ = m->replaceChar;
159 0 : if (!m->two2one)
160 0 : *dst++ = currentChar;
161 0 : previousChar = *src++;
162 0 : nCount--;
163 0 : break;
164 : }
165 : }
166 :
167 0 : if (! m->replaceChar) {
168 0 : if (useOffset)
169 0 : *p ++ = position ++;
170 0 : *dst ++ = previousChar;
171 0 : previousChar = currentChar;
172 : }
173 : }
174 :
175 0 : if (nCount == 0) {
176 0 : if (useOffset)
177 0 : *p = position;
178 0 : *dst ++ = previousChar;
179 : }
180 : } else {
181 : // Translation
182 0 : while (nCount -- > 0) {
183 0 : sal_Unicode c = *src++;
184 0 : c = func ? func( c) : (*table)[ c ];
185 0 : if (c != 0xffff)
186 0 : *dst ++ = c;
187 0 : if (useOffset) {
188 0 : if (c != 0xffff)
189 0 : *p ++ = position;
190 0 : position++;
191 : }
192 : }
193 : }
194 0 : newStr->length = sal_Int32(dst - newStr->buffer);
195 0 : if (useOffset)
196 0 : offset.realloc(newStr->length);
197 0 : *dst = (sal_Unicode) 0;
198 :
199 0 : return OUString(newStr, SAL_NO_ACQUIRE); // take ownership
200 : }
201 :
202 : sal_Unicode SAL_CALL
203 0 : transliteration_Ignore::transliterateChar2Char( sal_Unicode inChar) throw(RuntimeException, MultipleCharsOutputException, std::exception)
204 : {
205 0 : return func ? func( inChar) : table ? (*table)[ inChar ] : inChar;
206 : }
207 :
208 : } } } }
209 :
210 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|