/src/libreoffice/i18npool/source/transliteration/ignoreDiacritics_CTL.cxx
Line | Count | Source |
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 | | |
10 | | #include <comphelper/sequence.hxx> |
11 | | #include <rtl/ustrbuf.hxx> |
12 | | #include <transliteration_Ignore.hxx> |
13 | | #include <unicode/translit.h> |
14 | | |
15 | | namespace i18npool { |
16 | | |
17 | | ignoreDiacritics_CTL::ignoreDiacritics_CTL() |
18 | 0 | { |
19 | 0 | func = nullptr; |
20 | 0 | table = nullptr; |
21 | 0 | map = nullptr; |
22 | 0 | transliterationName = "ignoreDiacritics_CTL"; |
23 | 0 | implementationName = "com.sun.star.i18n.Transliteration.ignoreDiacritics_CTL"; |
24 | |
|
25 | 0 | UErrorCode nStatus = U_ZERO_ERROR; |
26 | 0 | m_transliterator.reset( icu::Transliterator::createInstance("NFD; [:M:] Remove; NFC", |
27 | 0 | UTRANS_FORWARD, nStatus) ); |
28 | 0 | if (U_FAILURE(nStatus)) |
29 | 0 | m_transliterator = nullptr; |
30 | 0 | } |
31 | | |
32 | | sal_Unicode SAL_CALL |
33 | | ignoreDiacritics_CTL::transliterateChar2Char(sal_Unicode nInChar) |
34 | 0 | { |
35 | 0 | if (!m_transliterator) |
36 | 0 | throw css::uno::RuntimeException(); |
37 | | |
38 | 0 | icu::UnicodeString aChar(nInChar); |
39 | 0 | m_transliterator->transliterate(aChar); |
40 | |
|
41 | 0 | if (aChar.isEmpty()) |
42 | 0 | return 0xffff; // Skip this character. |
43 | | |
44 | 0 | if (aChar.length() > 1) |
45 | 0 | return nInChar; // Don't know what to do here, return the original. |
46 | | |
47 | 0 | return aChar[0]; |
48 | 0 | } |
49 | | |
50 | | OUString |
51 | | ignoreDiacritics_CTL::foldingImpl(const OUString& rInStr, sal_Int32 nStartPos, |
52 | | sal_Int32 nCount, css::uno::Sequence<sal_Int32>* pOffset) |
53 | 0 | { |
54 | 0 | if (!m_transliterator) |
55 | 0 | throw css::uno::RuntimeException(); |
56 | | |
57 | 0 | if (nStartPos < 0 || nStartPos + nCount > rInStr.getLength()) |
58 | 0 | throw css::uno::RuntimeException(); |
59 | | |
60 | 0 | if (pOffset) |
61 | 0 | { |
62 | 0 | OUStringBuffer aOutBuf(nCount); |
63 | |
|
64 | 0 | std::vector<sal_Int32> aOffset; |
65 | 0 | aOffset.reserve(nCount); |
66 | |
|
67 | 0 | sal_Int32 nPosition = nStartPos; |
68 | 0 | while (nPosition < nStartPos + nCount) |
69 | 0 | { |
70 | 0 | sal_Int32 nIndex = nPosition; |
71 | 0 | UChar32 nChar = rInStr.iterateCodePoints(&nIndex); |
72 | 0 | icu::UnicodeString aUStr(nChar); |
73 | 0 | m_transliterator->transliterate(aUStr); |
74 | |
|
75 | 0 | aOutBuf.append(reinterpret_cast<const sal_Unicode*>(aUStr.getBuffer()), aUStr.length()); |
76 | |
|
77 | 0 | std::fill_n(std::back_inserter(aOffset), aUStr.length(), nPosition); |
78 | |
|
79 | 0 | nPosition = nIndex; |
80 | 0 | } |
81 | |
|
82 | 0 | *pOffset = comphelper::containerToSequence(aOffset); |
83 | 0 | return aOutBuf.makeStringAndClear(); |
84 | 0 | } |
85 | 0 | else |
86 | 0 | { |
87 | 0 | icu::UnicodeString aUStr(reinterpret_cast<const UChar*>(rInStr.getStr()) + nStartPos, nCount); |
88 | 0 | m_transliterator->transliterate(aUStr); |
89 | 0 | return OUString(reinterpret_cast<const sal_Unicode*>(aUStr.getBuffer()), aUStr.length()); |
90 | 0 | } |
91 | 0 | } |
92 | | |
93 | | } |
94 | | |
95 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |