summaryrefslogtreecommitdiffstats
path: root/i18npool/source/transliteration/ignoreDiacritics_CTL.cxx
blob: ada76a735b80cfa53eeb549b03930dbee4ff447f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <comphelper/sequence.hxx>
#include <rtl/ustrbuf.hxx>
#include <transliteration_Ignore.hxx>
#include <unicode/translit.h>

namespace i18npool {

ignoreDiacritics_CTL::ignoreDiacritics_CTL()
{
    func = nullptr;
    table = nullptr;
    map = nullptr;
    transliterationName = "ignoreDiacritics_CTL";
    implementationName = "com.sun.star.i18n.Transliteration.ignoreDiacritics_CTL";

    UErrorCode nStatus = U_ZERO_ERROR;
    m_transliterator = icu::Transliterator::createInstance("NFD; [:M:] Remove; NFC",
            UTRANS_FORWARD, nStatus);
    if (U_FAILURE(nStatus))
        m_transliterator = nullptr;
}

sal_Unicode SAL_CALL
ignoreDiacritics_CTL::transliterateChar2Char(sal_Unicode nInChar)
{
    if (!m_transliterator)
        throw css::uno::RuntimeException();

    icu::UnicodeString aChar(nInChar);
    m_transliterator->transliterate(aChar);

    if (aChar.isEmpty())
        return 0xffff; // Skip this character.

    if (aChar.length() > 1)
        return nInChar; // Don't know what to do here, return the original.

    return aChar[0];
}

OUString
ignoreDiacritics_CTL::foldingImpl(const OUString& rInStr, sal_Int32 nStartPos,
    sal_Int32 nCount, css::uno::Sequence<sal_Int32>& rOffset, bool useOffset)
{
    if (!m_transliterator)
        throw css::uno::RuntimeException();

    if (nStartPos < 0 || nStartPos + nCount > rInStr.getLength())
        throw css::uno::RuntimeException();

    if (useOffset)
    {
        OUStringBuffer aOutBuf(nCount);

        std::vector<sal_Int32> aOffset;
        aOffset.reserve(nCount);

        sal_Int32 nPosition = nStartPos;
        while (nPosition < nStartPos + nCount)
        {
            sal_Int32 nIndex = nPosition;
            UChar32 nChar = rInStr.iterateCodePoints(&nIndex);
            icu::UnicodeString aUStr(nChar);
            m_transliterator->transliterate(aUStr);

            aOutBuf.append(reinterpret_cast<const sal_Unicode*>(aUStr.getBuffer()), aUStr.length());

            std::fill_n(std::back_inserter(aOffset), aUStr.length(), nPosition);

            nPosition = nIndex;
        }

        rOffset = comphelper::containerToSequence(aOffset);
        return aOutBuf.makeStringAndClear();
    }
    else
    {
        icu::UnicodeString aUStr(reinterpret_cast<const UChar*>(rInStr.getStr()) + nStartPos, nCount);
        m_transliterator->transliterate(aUStr);
        return OUString(reinterpret_cast<const sal_Unicode*>(aUStr.getBuffer()), aUStr.length());
    }
}

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */