Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / i18npool / source / transliteration / ignoreDiacritics_CTL.cxx
blobada76a735b80cfa53eeb549b03930dbee4ff447f
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/.
8 */
10 #include <comphelper/sequence.hxx>
11 #include <rtl/ustrbuf.hxx>
12 #include <transliteration_Ignore.hxx>
13 #include <unicode/translit.h>
15 namespace i18npool {
17 ignoreDiacritics_CTL::ignoreDiacritics_CTL()
19 func = nullptr;
20 table = nullptr;
21 map = nullptr;
22 transliterationName = "ignoreDiacritics_CTL";
23 implementationName = "com.sun.star.i18n.Transliteration.ignoreDiacritics_CTL";
25 UErrorCode nStatus = U_ZERO_ERROR;
26 m_transliterator = icu::Transliterator::createInstance("NFD; [:M:] Remove; NFC",
27 UTRANS_FORWARD, nStatus);
28 if (U_FAILURE(nStatus))
29 m_transliterator = nullptr;
32 sal_Unicode SAL_CALL
33 ignoreDiacritics_CTL::transliterateChar2Char(sal_Unicode nInChar)
35 if (!m_transliterator)
36 throw css::uno::RuntimeException();
38 icu::UnicodeString aChar(nInChar);
39 m_transliterator->transliterate(aChar);
41 if (aChar.isEmpty())
42 return 0xffff; // Skip this character.
44 if (aChar.length() > 1)
45 return nInChar; // Don't know what to do here, return the original.
47 return aChar[0];
50 OUString
51 ignoreDiacritics_CTL::foldingImpl(const OUString& rInStr, sal_Int32 nStartPos,
52 sal_Int32 nCount, css::uno::Sequence<sal_Int32>& rOffset, bool useOffset)
54 if (!m_transliterator)
55 throw css::uno::RuntimeException();
57 if (nStartPos < 0 || nStartPos + nCount > rInStr.getLength())
58 throw css::uno::RuntimeException();
60 if (useOffset)
62 OUStringBuffer aOutBuf(nCount);
64 std::vector<sal_Int32> aOffset;
65 aOffset.reserve(nCount);
67 sal_Int32 nPosition = nStartPos;
68 while (nPosition < nStartPos + nCount)
70 sal_Int32 nIndex = nPosition;
71 UChar32 nChar = rInStr.iterateCodePoints(&nIndex);
72 icu::UnicodeString aUStr(nChar);
73 m_transliterator->transliterate(aUStr);
75 aOutBuf.append(reinterpret_cast<const sal_Unicode*>(aUStr.getBuffer()), aUStr.length());
77 std::fill_n(std::back_inserter(aOffset), aUStr.length(), nPosition);
79 nPosition = nIndex;
82 rOffset = comphelper::containerToSequence(aOffset);
83 return aOutBuf.makeStringAndClear();
85 else
87 icu::UnicodeString aUStr(reinterpret_cast<const UChar*>(rInStr.getStr()) + nStartPos, nCount);
88 m_transliterator->transliterate(aUStr);
89 return OUString(reinterpret_cast<const sal_Unicode*>(aUStr.getBuffer()), aUStr.length());
95 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */