LibreOffice Module i18npool (master)  1
ignoreDiacritics_CTL.cxx
Go to the documentation of this file.
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>
13 #include <unicode/translit.h>
14 
15 namespace i18npool {
16 
18 {
19  func = nullptr;
20  table = nullptr;
21  map = nullptr;
22  transliterationName = "ignoreDiacritics_CTL";
23  implementationName = "com.sun.star.i18n.Transliteration.ignoreDiacritics_CTL";
24 
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;
30 }
31 
32 sal_Unicode SAL_CALL
34 {
35  if (!m_transliterator)
36  throw css::uno::RuntimeException();
37 
38  icu::UnicodeString aChar(nInChar);
39  m_transliterator->transliterate(aChar);
40 
41  if (aChar.isEmpty())
42  return 0xffff; // Skip this character.
43 
44  if (aChar.length() > 1)
45  return nInChar; // Don't know what to do here, return the original.
46 
47  return aChar[0];
48 }
49 
50 OUString
51 ignoreDiacritics_CTL::foldingImpl(const OUString& rInStr, sal_Int32 nStartPos,
52  sal_Int32 nCount, css::uno::Sequence<sal_Int32>& rOffset, bool useOffset)
53 {
54  if (!m_transliterator)
55  throw css::uno::RuntimeException();
56 
57  if (nStartPos < 0 || nStartPos + nCount > rInStr.getLength())
58  throw css::uno::RuntimeException();
59 
60  if (useOffset)
61  {
62  OUStringBuffer aOutBuf(nCount);
63 
64  std::vector<sal_Int32> aOffset;
65  aOffset.reserve(nCount);
66 
67  sal_Int32 nPosition = nStartPos;
68  while (nPosition < nStartPos + nCount)
69  {
70  sal_Int32 nIndex = nPosition;
71  UChar32 nChar = rInStr.iterateCodePoints(&nIndex);
72  icu::UnicodeString aUStr(nChar);
73  m_transliterator->transliterate(aUStr);
74 
75  aOutBuf.append(reinterpret_cast<const sal_Unicode*>(aUStr.getBuffer()), aUStr.length());
76 
77  std::fill_n(std::back_inserter(aOffset), aUStr.length(), nPosition);
78 
79  nPosition = nIndex;
80  }
81 
82  rOffset = comphelper::containerToSequence(aOffset);
83  return aOutBuf.makeStringAndClear();
84  }
85  else
86  {
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());
90  }
91 }
92 
93 }
94 
95 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 nIndex
sal_Unicode SAL_CALL transliterateChar2Char(sal_Unicode nInChar) override
sal_uInt16 sal_Unicode
Constant values shared between i18npool and, for example, the number formatter.
OUString foldingImpl(const OUString &rInStr, sal_Int32 nStartPos, sal_Int32 nCount, css::uno::Sequence< sal_Int32 > &rOffset, bool useOffset) override
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)