LibreOffice Module i18npool (master)  1
transliteration_Numeric.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  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 
21 #include <com/sun/star/i18n/TransliterationType.hpp>
22 
24 #include <nativenumbersupplier.hxx>
25 #include <rtl/ref.hxx>
26 
27 using namespace com::sun::star::i18n;
28 using namespace com::sun::star::uno;
29 
30 
31 namespace i18npool {
32 
33 sal_Int16 SAL_CALL transliteration_Numeric::getType()
34 {
35  return TransliterationType::NUMERIC;
36 }
37 
38 OUString
39  transliteration_Numeric::foldingImpl( const OUString& /*inStr*/, sal_Int32 /*startPos*/, sal_Int32 /*nCount*/, Sequence< sal_Int32 >* /*pOffset*/ )
40 {
41  throw RuntimeException();
42 }
43 
44 sal_Bool SAL_CALL
45  transliteration_Numeric::equals( const OUString& /*str1*/, sal_Int32 /*pos1*/, sal_Int32 /*nCount1*/, sal_Int32& /*nMatch1*/, const OUString& /*str2*/, sal_Int32 /*pos2*/, sal_Int32 /*nCount2*/, sal_Int32& /*nMatch2*/ )
46 {
47  throw RuntimeException();
48 }
49 
50 Sequence< OUString > SAL_CALL
51  transliteration_Numeric::transliterateRange( const OUString& /*str1*/, const OUString& /*str2*/ )
52 {
53  throw RuntimeException();
54 }
55 
56 
57 #define isNumber(c) ((c) >= 0x30 && (c) <= 0x39)
58 #define NUMBER_ZERO 0x30
59 
60 OUString
61 transliteration_Numeric::transliterateBullet( std::u16string_view inStr, sal_Int32 startPos, sal_Int32 nCount,
62  Sequence< sal_Int32 >* pOffset ) const
63 {
64  sal_Int32 number = -1, j = 0, endPos = startPos + nCount;
65 
66  if (endPos > static_cast<sal_Int32>(inStr.size()))
67  endPos = inStr.size();
68 
69  rtl_uString* pStr = rtl_uString_alloc(nCount);
70  sal_Unicode* out = pStr->buffer;
71 
72  if (pOffset)
73  pOffset->realloc(nCount);
74  auto ppOffset = pOffset ? pOffset->getArray() : nullptr;
75 
76  for (sal_Int32 i = startPos; i < endPos; i++) {
77  if (isNumber(inStr[i]))
78  {
79  if (number == -1) {
80  startPos = i;
81  number = (inStr[i] - NUMBER_ZERO);
82  } else {
83  number = number * 10 + (inStr[i] - NUMBER_ZERO);
84  }
85  } else {
86  if (number == 0) {
87  if (ppOffset)
88  ppOffset[j] = startPos;
89  out[j++] = NUMBER_ZERO;
90  } else if (number > tableSize && !recycleSymbol) {
91  for (sal_Int32 k = startPos; k < i; k++) {
92  if (ppOffset)
93  ppOffset[j] = k;
94  out[j++] = inStr[k];
95  }
96  } else if (number > 0) {
97  if (ppOffset)
98  ppOffset[j] = startPos;
99  out[j++] = table[--number % tableSize];
100  } else if (i < endPos) {
101  if (ppOffset)
102  ppOffset[j] = i;
103  out[j++] = inStr[i];
104  }
105  number = -1;
106  }
107  }
108  out[j] = 0;
109 
110  if (pOffset)
111  pOffset->realloc(j);
112 
113  return OUString( pStr, SAL_NO_ACQUIRE );
114 }
115 
116 OUString
117 transliteration_Numeric::transliterateImpl( const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
118  Sequence< sal_Int32 >* pOffset )
119 {
120  if (tableSize)
121  return transliterateBullet( inStr, startPos, nCount, pOffset);
122  else
123  return rtl::Reference(new NativeNumberSupplierService())->getNativeNumberString( inStr.copy(startPos, nCount), aLocale, nNativeNumberMode, pOffset );
124 }
125 
126 sal_Unicode SAL_CALL
127 transliteration_Numeric::transliterateChar2Char( sal_Unicode inChar )
128 {
129  if (tableSize) {
130  if (isNumber(inChar)) {
131  sal_Int16 number = inChar - NUMBER_ZERO;
132  if (number <= tableSize || recycleSymbol)
133  return table[--number % tableSize];
134  }
135  return inChar;
136  }
137  else
138  return NativeNumberSupplierService::getNativeNumberChar( inChar, aLocale, nNativeNumberMode );
139 }
140 
141 }
142 
143 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define isNumber(c)
exports com.sun.star. table
sal_uInt16 sal_Unicode
int i
unsigned char sal_Bool
Constant values shared between i18npool and, for example, the number formatter.
#define NUMBER_ZERO