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