1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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
; }
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());
48 for ( nmatch
= 0; nmatch
< length
; nmatch
++)
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.
57 nMatch1
= 0; // No character was matched.
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);
79 transliteration_Ignore::getType() throw(RuntimeException
)
81 // The type is also defined in com/sun/star/util/TransliterationType.hdl
82 return TransliterationType::IGNORE
;
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);
114 Sequence
< OUString
> r(4);
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.
135 sal_Int32 position
= 0;
137 offset
.realloc( nCount
);
138 p
= offset
.getArray();
143 sal_Unicode previousChar
= *src
++;
144 sal_Unicode currentChar
;
147 while (-- nCount
> 0) {
148 currentChar
= *src
++;
151 for (m
= map
; m
->replaceChar
; m
++) {
152 if (previousChar
== m
->previousChar
&& currentChar
== m
->currentChar
) {
159 *dst
++ = m
->replaceChar
;
161 *dst
++ = currentChar
;
162 previousChar
= *src
++;
168 if (! m
->replaceChar
) {
171 *dst
++ = previousChar
;
172 previousChar
= currentChar
;
179 *dst
++ = previousChar
;
183 while (nCount
-- > 0) {
184 sal_Unicode c
= *src
++;
185 c
= func
? func( c
) : (*table
)[ c
];
195 newStr
->length
= sal_Int32(dst
- newStr
->buffer
);
197 offset
.realloc(newStr
->length
);
198 *dst
= (sal_Unicode
) 0;
200 return OUString(newStr
, SAL_NO_ACQUIRE
); // take ownership
204 transliteration_Ignore::transliterateChar2Char( sal_Unicode inChar
) throw(RuntimeException
, MultipleCharsOutputException
)
206 return func
? func( inChar
) : table
? (*table
)[ inChar
] : inChar
;
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */