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/.
10 #include <comphelper/sequence.hxx>
11 #include <rtl/ustrbuf.hxx>
12 #include <transliteration_Ignore.hxx>
13 #include <unicode/translit.h>
17 ignoreDiacritics_CTL::ignoreDiacritics_CTL()
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;
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
);
42 return 0xffff; // Skip this character.
44 if (aChar
.length() > 1)
45 return nInChar
; // Don't know what to do here, return the original.
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();
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
);
82 rOffset
= comphelper::containerToSequence(aOffset
);
83 return aOutBuf
.makeStringAndClear();
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: */