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