1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_i18npool.hxx"
26
27 #include <transliteration_Numeric.hxx>
28 #include <nativenumbersupplier.hxx>
29 #include <defaultnumberingprovider.hxx>
30
31 using namespace com::sun::star::uno;
32 using namespace rtl;
33
34 namespace com { namespace sun { namespace star { namespace i18n {
35
getType()36 sal_Int16 SAL_CALL transliteration_Numeric::getType()
37 {
38 return TransliterationType::NUMERIC;
39 }
40
41 OUString SAL_CALL
folding(const OUString &,sal_Int32,sal_Int32,Sequence<sal_Int32> &)42 transliteration_Numeric::folding( const OUString& /*inStr*/, sal_Int32 /*startPos*/, sal_Int32 /*nCount*/, Sequence< sal_Int32 >& /*offset*/ )
43 {
44 throw (new RuntimeException());
45 }
46
47 sal_Bool SAL_CALL
equals(const OUString &,sal_Int32,sal_Int32,sal_Int32 &,const OUString &,sal_Int32,sal_Int32,sal_Int32 &)48 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*/ )
49 {
50 throw (new RuntimeException());
51 }
52
53 Sequence< OUString > SAL_CALL
transliterateRange(const OUString &,const OUString &)54 transliteration_Numeric::transliterateRange( const OUString& /*str1*/, const OUString& /*str2*/ )
55 {
56 throw (new RuntimeException());
57 }
58
59
60 #define isNumber(c) ((c) >= 0x30 && (c) <= 0x39)
61 #define NUMBER_ZERO 0x30
62
63 OUString SAL_CALL
transliterateBullet(const OUString & inStr,sal_Int32 startPos,sal_Int32 nCount,Sequence<sal_Int32> & offset)64 transliteration_Numeric::transliterateBullet( const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
65 Sequence< sal_Int32 >& offset )
66 {
67 sal_Int32 number = -1, j = 0, endPos = startPos + nCount;
68
69 if (endPos > inStr.getLength())
70 endPos = inStr.getLength();
71
72 rtl_uString* pStr = x_rtl_uString_new_WithLength( nCount ); // our x_rtl_ustring.h
73 sal_Unicode* out = pStr->buffer;
74
75 if (useOffset)
76 offset.realloc(nCount);
77
78 for (sal_Int32 i = startPos; i < endPos; i++) {
79 if (i < endPos && isNumber(inStr[i])) {
80 if (number == -1) {
81 startPos = i;
82 number = (inStr[i] - NUMBER_ZERO);
83 } else {
84 number = number * 10 + (inStr[i] - NUMBER_ZERO);
85 }
86 } else {
87 if (number == 0) {
88 if (useOffset)
89 offset[j] = startPos;
90 out[j++] = NUMBER_ZERO;
91 } if (number > tableSize && !recycleSymbol) {
92 for (sal_Int32 k = startPos; k < i; k++) {
93 if (useOffset)
94 offset[j] = k;
95 out[j++] = inStr[k];
96 }
97 } else if (number > 0) {
98 if (useOffset)
99 offset[j] = startPos;
100 out[j++] = table[--number % tableSize];
101 } else if (i < endPos) {
102 if (useOffset)
103 offset[j] = i;
104 out[j++] = inStr[i];
105 }
106 number = -1;
107 }
108 }
109 out[j] = 0;
110
111 if (useOffset)
112 offset.realloc(j);
113
114 return OUString( pStr, SAL_NO_ACQUIRE ); // take over ownership of <pStr>
115 }
116
117 OUString SAL_CALL
transliterate(const OUString & inStr,sal_Int32 startPos,sal_Int32 nCount,Sequence<sal_Int32> & offset)118 transliteration_Numeric::transliterate( const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
119 Sequence< sal_Int32 >& offset )
120 {
121 if (tableSize)
122 return transliterateBullet( inStr, startPos, nCount, offset);
123 else
124 return NativeNumberSupplier(useOffset).getNativeNumberString( inStr.copy(startPos, nCount), aLocale, nNativeNumberMode, offset );
125 }
126
127 sal_Unicode SAL_CALL
transliterateChar2Char(sal_Unicode inChar)128 transliteration_Numeric::transliterateChar2Char( sal_Unicode inChar )
129 {
130 if (tableSize) {
131 if (isNumber(inChar)) {
132 sal_Int16 number = inChar - NUMBER_ZERO;
133 if (number <= tableSize || recycleSymbol)
134 return table[--number % tableSize];
135 }
136 return inChar;
137 }
138 else
139 return NativeNumberSupplier().getNativeNumberChar( inChar, aLocale, nNativeNumberMode );
140 }
141
142 } } } }
143