Bump version to 6.0-36
[LibreOffice.git] / i18npool / source / transliteration / transliteration_body.cxx
blobbaafc85acb174cd669c2ebbd644b223afe149b36
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 .
20 #include <rtl/ustrbuf.hxx>
21 #include <rtl/ref.hxx>
22 #include <i18nutil/casefolding.hxx>
23 #include <i18nutil/unicode.hxx>
24 #include <com/sun/star/i18n/MultipleCharsOutputException.hpp>
25 #include <comphelper/processfactory.hxx>
26 #include <comphelper/string.hxx>
28 #include <string.h>
30 #include <characterclassificationImpl.hxx>
31 #include <breakiteratorImpl.hxx>
33 #include <transliteration_body.hxx>
34 #include <memory>
36 using namespace ::com::sun::star::uno;
37 using namespace ::com::sun::star::i18n;
38 using namespace ::com::sun::star::lang;
40 namespace i18npool {
42 Transliteration_body::Transliteration_body()
44 nMappingType = MappingType::NONE;
45 transliterationName = "Transliteration_body";
46 implementationName = "com.sun.star.i18n.Transliteration.Transliteration_body";
49 sal_Int16 SAL_CALL Transliteration_body::getType()
51 return TransliterationType::ONE_TO_ONE;
54 sal_Bool SAL_CALL Transliteration_body::equals(
55 const OUString& /*str1*/, sal_Int32 /*pos1*/, sal_Int32 /*nCount1*/, sal_Int32& /*nMatch1*/,
56 const OUString& /*str2*/, sal_Int32 /*pos2*/, sal_Int32 /*nCount2*/, sal_Int32& /*nMatch2*/)
58 throw RuntimeException();
61 Sequence< OUString > SAL_CALL
62 Transliteration_body::transliterateRange( const OUString& str1, const OUString& str2 )
64 Sequence< OUString > ostr(2);
65 ostr[0] = str1;
66 ostr[1] = str2;
67 return ostr;
70 static MappingType lcl_getMappingTypeForToggleCase( MappingType nMappingType, sal_Unicode cChar )
72 MappingType nRes = nMappingType;
74 // take care of TOGGLE_CASE transliteration:
75 // nMappingType should not be a combination of flags, thuse we decide now
76 // which one to use.
77 if (nMappingType == (MappingType::LowerToUpper | MappingType::UpperToLower))
79 const sal_Int16 nType = unicode::getUnicodeType( cChar );
80 if (nType & 0x02 /* lower case*/)
81 nRes = MappingType::LowerToUpper;
82 else
84 // should also work properly for non-upper characters like white spaces, numbers, ...
85 nRes = MappingType::UpperToLower;
89 return nRes;
92 OUString SAL_CALL
93 Transliteration_body::transliterate(
94 const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
95 Sequence< sal_Int32 >& offset)
97 const sal_Unicode *in = inStr.getStr() + startPos;
99 // Two different blocks to eliminate the if(useOffset) condition inside the
100 // inner k loop. Yes, on massive use even such small things do count.
101 if ( useOffset )
103 sal_Int32 nOffCount = 0, i;
104 for (i = 0; i < nCount; i++)
106 // take care of TOGGLE_CASE transliteration:
107 MappingType nTmpMappingType = nMappingType;
108 if (nMappingType == (MappingType::LowerToUpper | MappingType::UpperToLower))
109 nTmpMappingType = lcl_getMappingTypeForToggleCase( nMappingType, in[i] );
111 const i18nutil::Mapping &map = i18nutil::casefolding::getValue( in, i, nCount, aLocale, nTmpMappingType );
112 nOffCount += map.nmap;
114 rtl_uString* pStr = rtl_uString_alloc(nOffCount);
115 sal_Unicode* out = pStr->buffer;
117 if ( nOffCount != offset.getLength() )
118 offset.realloc( nOffCount );
120 sal_Int32 j = 0;
121 sal_Int32 * pArr = offset.getArray();
122 for (i = 0; i < nCount; i++)
124 // take care of TOGGLE_CASE transliteration:
125 MappingType nTmpMappingType = nMappingType;
126 if (nMappingType == (MappingType::LowerToUpper | MappingType::UpperToLower))
127 nTmpMappingType = lcl_getMappingTypeForToggleCase( nMappingType, in[i] );
129 const i18nutil::Mapping &map = i18nutil::casefolding::getValue( in, i, nCount, aLocale, nTmpMappingType );
130 for (sal_Int32 k = 0; k < map.nmap; k++)
132 pArr[j] = i + startPos;
133 out[j++] = map.map[k];
136 out[j] = 0;
138 return OUString( pStr, SAL_NO_ACQUIRE );
140 else
142 // In the simple case of no offset sequence used we can eliminate the
143 // first getValue() loop. We could also assume that most calls result
144 // in identical string lengths, thus using a preallocated
145 // OUStringBuffer could be an easy way to assemble the return string
146 // without too much hassle. However, for single characters the
147 // OUStringBuffer::append() method is quite expensive compared to a
148 // simple array operation, so it pays here to copy the final result
149 // instead.
151 // Allocate the max possible buffer. Try to use stack instead of heap,
152 // which would have to be reallocated most times anyways.
153 const sal_Int32 nLocalBuf = 2048;
154 sal_Unicode aLocalBuf[ nLocalBuf * NMAPPINGMAX ], *out = aLocalBuf;
155 std::unique_ptr<sal_Unicode[]> pHeapBuf;
156 if ( nCount > nLocalBuf ) {
157 pHeapBuf.reset(new sal_Unicode[ nCount * NMAPPINGMAX ]);
158 out = pHeapBuf.get();
161 sal_Int32 j = 0;
162 for ( sal_Int32 i = 0; i < nCount; i++)
164 // take care of TOGGLE_CASE transliteration:
165 MappingType nTmpMappingType = nMappingType;
166 if (nMappingType == (MappingType::LowerToUpper | MappingType::UpperToLower))
167 nTmpMappingType = lcl_getMappingTypeForToggleCase( nMappingType, in[i] );
169 const i18nutil::Mapping &map = i18nutil::casefolding::getValue( in, i, nCount, aLocale, nTmpMappingType );
170 for (sal_Int32 k = 0; k < map.nmap; k++)
172 out[j++] = map.map[k];
176 OUString aRet( out, j );
177 return aRet;
181 OUString SAL_CALL
182 Transliteration_body::transliterateChar2String( sal_Unicode inChar )
184 const i18nutil::Mapping &map = i18nutil::casefolding::getValue(&inChar, 0, 1, aLocale, nMappingType);
185 rtl_uString* pStr = rtl_uString_alloc(map.nmap);
186 sal_Unicode* out = pStr->buffer;
187 sal_Int32 i;
189 for (i = 0; i < map.nmap; i++)
190 out[i] = map.map[i];
191 out[i] = 0;
193 return OUString( pStr, SAL_NO_ACQUIRE );
196 sal_Unicode SAL_CALL
197 Transliteration_body::transliterateChar2Char( sal_Unicode inChar )
199 const i18nutil::Mapping &map = i18nutil::casefolding::getValue(&inChar, 0, 1, aLocale, nMappingType);
200 if (map.nmap > 1)
201 throw MultipleCharsOutputException();
202 return map.map[0];
205 OUString SAL_CALL
206 Transliteration_body::folding( const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
207 Sequence< sal_Int32 >& offset)
209 return transliterate(inStr, startPos, nCount, offset);
212 Transliteration_casemapping::Transliteration_casemapping()
214 nMappingType = MappingType::NONE;
215 transliterationName = "casemapping(generic)";
216 implementationName = "com.sun.star.i18n.Transliteration.Transliteration_casemapping";
219 void SAL_CALL
220 Transliteration_casemapping::setMappingType( const MappingType rMappingType, const Locale& rLocale )
222 nMappingType = rMappingType;
223 aLocale = rLocale;
226 Transliteration_u2l::Transliteration_u2l()
228 nMappingType = MappingType::UpperToLower;
229 transliterationName = "upper_to_lower(generic)";
230 implementationName = "com.sun.star.i18n.Transliteration.Transliteration_u2l";
233 Transliteration_l2u::Transliteration_l2u()
235 nMappingType = MappingType::LowerToUpper;
236 transliterationName = "lower_to_upper(generic)";
237 implementationName = "com.sun.star.i18n.Transliteration.Transliteration_l2u";
240 Transliteration_togglecase::Transliteration_togglecase()
242 // usually nMappingType must NOT be a combination of different flags here,
243 // but we take care of that problem in Transliteration_body::transliterate above
244 // before that value is used. There we will decide which of both is to be used on
245 // a per character basis.
246 nMappingType = MappingType::LowerToUpper | MappingType::UpperToLower;
247 transliterationName = "toggle(generic)";
248 implementationName = "com.sun.star.i18n.Transliteration.Transliteration_togglecase";
251 Transliteration_titlecase::Transliteration_titlecase()
253 nMappingType = MappingType::ToTitle;
254 transliterationName = "title(generic)";
255 implementationName = "com.sun.star.i18n.Transliteration.Transliteration_titlecase";
258 /// @throws RuntimeException
259 static OUString transliterate_titlecase_Impl(
260 const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
261 const Locale &rLocale,
262 Sequence< sal_Int32 >& offset )
264 const OUString aText( inStr.copy( startPos, nCount ) );
266 OUString aRes;
267 if (!aText.isEmpty())
269 Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
270 rtl::Reference< CharacterClassificationImpl > xCharClassImpl( new CharacterClassificationImpl( xContext ) );
272 // because xCharClassImpl.toTitle does not handle ligatures or Beta but will raise
273 // an exception we need to handle the first chara manually...
275 // we don't want to change surrogates by accident, thuse we use proper code point iteration
276 sal_Int32 nPos = 0;
277 sal_uInt32 cFirstChar = aText.iterateCodePoints( &nPos );
278 OUString aResolvedLigature( &cFirstChar, 1 );
279 // toUpper can be used to properly resolve ligatures and characters like Beta
280 aResolvedLigature = xCharClassImpl->toUpper( aResolvedLigature, 0, aResolvedLigature.getLength(), rLocale );
281 // since toTitle will leave all-uppercase text unchanged we first need to
282 // use toLower to bring possible 2nd and following chars in lowercase
283 aResolvedLigature = xCharClassImpl->toLower( aResolvedLigature, 0, aResolvedLigature.getLength(), rLocale );
284 sal_Int32 nResolvedLen = aResolvedLigature.getLength();
286 // now we can properly use toTitle to get the expected result for the resolved string.
287 // The rest of the text should just become lowercase.
288 aRes = xCharClassImpl->toTitle( aResolvedLigature, 0, nResolvedLen, rLocale );
289 aRes += xCharClassImpl->toLower( aText, 1, aText.getLength() - 1, rLocale );
290 offset.realloc( aRes.getLength() );
292 sal_Int32 *pOffset = offset.getArray();
293 sal_Int32 nLen = offset.getLength();
294 for (sal_Int32 i = 0; i < nLen; ++i)
296 sal_Int32 nIdx = 0;
297 if (i >= nResolvedLen)
298 nIdx = i - nResolvedLen + 1;
299 pOffset[i] = nIdx;
302 return aRes;
305 // this function expects to be called on a word-by-word basis,
306 // namely that startPos points to the first char of the word
307 OUString SAL_CALL Transliteration_titlecase::transliterate(
308 const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
309 Sequence< sal_Int32 >& offset )
311 return transliterate_titlecase_Impl( inStr, startPos, nCount, aLocale, offset );
314 Transliteration_sentencecase::Transliteration_sentencecase()
316 nMappingType = MappingType::ToTitle; // though only to be applied to the first word...
317 transliterationName = "sentence(generic)";
318 implementationName = "com.sun.star.i18n.Transliteration.Transliteration_sentencecase";
321 // this function expects to be called on a sentence-by-sentence basis,
322 // namely that startPos points to the first word (NOT first char!) in the sentence
323 OUString SAL_CALL Transliteration_sentencecase::transliterate(
324 const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
325 Sequence< sal_Int32 >& offset )
327 return transliterate_titlecase_Impl( inStr, startPos, nCount, aLocale, offset );
332 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */