Line data Source code
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 : #include "sal/config.h"
21 :
22 : #ifdef _MSC_VER
23 : #pragma warning(disable:4738) // storing 32-bit float result in memory, possible loss of performance
24 : #endif
25 :
26 : #include <cassert>
27 : #include <cstdlib>
28 :
29 : #include <osl/interlck.h>
30 : #include <rtl/alloc.h>
31 : #include <osl/diagnose.h>
32 : #include <rtl/tencinfo.h>
33 :
34 : #include "strimp.hxx"
35 : #include "surrogates.hxx"
36 : #include <rtl/string.h>
37 :
38 : #include "rtl/math.h"
39 : #include "rtl/tencinfo.h"
40 :
41 : /* ======================================================================= */
42 :
43 : /* static data to be referenced by all empty strings
44 : * the refCount is predefined to 1 and must never become 0 !
45 : */
46 : static rtl_String const aImplEmpty_rtl_String =
47 : {
48 : SAL_STRING_STATIC_FLAG|1,
49 : /* sal_Int32 refCount; */
50 : 0, /* sal_Int32 length; */
51 : { 0 } /* sal_Char buffer[1]; */
52 : };
53 :
54 : /* ======================================================================= */
55 :
56 : #define IMPL_RTL_STRCODE sal_Char
57 : #define IMPL_RTL_USTRCODE( c ) ((unsigned char)c)
58 : #define IMPL_RTL_STRNAME( n ) rtl_str_ ## n
59 :
60 : #define IMPL_RTL_STRINGNAME( n ) rtl_string_ ## n
61 : #define IMPL_RTL_STRINGDATA rtl_String
62 : #define IMPL_RTL_EMPTYSTRING aImplEmpty_rtl_String
63 :
64 : #undef RTL_LOG_STRING_NEW
65 : #define RTL_LOG_STRING_NEW(s)
66 : #undef RTL_LOG_STRING_DELETE
67 : #define RTL_LOG_STRING_DELETE(s)
68 :
69 : /* ======================================================================= */
70 :
71 : /* Include String/UString template code */
72 :
73 : #include "strtmpl.cxx"
74 :
75 140 : sal_Int32 SAL_CALL rtl_str_valueOfFloat(sal_Char * pStr, float f)
76 : SAL_THROW_EXTERN_C()
77 : {
78 140 : rtl_String * pResult = NULL;
79 : sal_Int32 nLen;
80 : rtl_math_doubleToString(
81 : &pResult, 0, 0, f, rtl_math_StringFormat_G,
82 : RTL_STR_MAX_VALUEOFFLOAT - RTL_CONSTASCII_LENGTH("-x.E-xxx"), '.', 0, 0,
83 140 : sal_True);
84 140 : nLen = pResult->length;
85 : OSL_ASSERT(nLen < RTL_STR_MAX_VALUEOFFLOAT);
86 140 : memcpy(pStr, pResult->buffer, (nLen + 1) * sizeof(sal_Char));
87 140 : rtl_string_release(pResult);
88 140 : return nLen;
89 : }
90 :
91 475 : sal_Int32 SAL_CALL rtl_str_valueOfDouble(sal_Char * pStr, double d)
92 : SAL_THROW_EXTERN_C()
93 : {
94 475 : rtl_String * pResult = NULL;
95 : sal_Int32 nLen;
96 : rtl_math_doubleToString(
97 : &pResult, 0, 0, d, rtl_math_StringFormat_G,
98 : RTL_STR_MAX_VALUEOFDOUBLE - RTL_CONSTASCII_LENGTH("-x.E-xxx"), '.', 0,
99 475 : 0, sal_True);
100 475 : nLen = pResult->length;
101 : OSL_ASSERT(nLen < RTL_STR_MAX_VALUEOFDOUBLE);
102 475 : memcpy(pStr, pResult->buffer, (nLen + 1) * sizeof(sal_Char));
103 475 : rtl_string_release(pResult);
104 475 : return nLen;
105 : }
106 :
107 0 : float SAL_CALL rtl_str_toFloat(sal_Char const * pStr) SAL_THROW_EXTERN_C()
108 : {
109 0 : return (float) rtl_math_stringToDouble(pStr, pStr + rtl_str_getLength(pStr),
110 0 : '.', 0, 0, 0);
111 : }
112 :
113 5381 : double SAL_CALL rtl_str_toDouble(sal_Char const * pStr) SAL_THROW_EXTERN_C()
114 : {
115 5381 : return rtl_math_stringToDouble(pStr, pStr + rtl_str_getLength(pStr), '.', 0,
116 5381 : 0, 0);
117 : }
118 :
119 : /* ======================================================================= */
120 :
121 3421171 : static int rtl_ImplGetFastUTF8ByteLen( const sal_Unicode* pStr, sal_Int32 nLen )
122 : {
123 : int n;
124 : sal_Unicode c;
125 : sal_uInt32 nUCS4Char;
126 : const sal_Unicode* pEndStr;
127 :
128 3421171 : n = 0;
129 3421171 : pEndStr = pStr+nLen;
130 70375775 : while ( pStr < pEndStr )
131 : {
132 63533433 : c = *pStr;
133 :
134 63533433 : if ( c < 0x80 )
135 63530219 : n++;
136 3214 : else if ( c < 0x800 )
137 681 : n += 2;
138 : else
139 : {
140 2533 : if ( !SAL_RTL_IS_HIGH_SURROGATE(c) )
141 2533 : n += 3;
142 : else
143 : {
144 0 : nUCS4Char = c;
145 :
146 0 : if ( pStr+1 < pEndStr )
147 : {
148 0 : c = *(pStr+1);
149 0 : if ( SAL_RTL_IS_LOW_SURROGATE(c) )
150 : {
151 0 : nUCS4Char = SAL_RTL_COMBINE_SURROGATES(nUCS4Char, c);
152 0 : pStr++;
153 : }
154 : }
155 :
156 0 : if ( nUCS4Char < 0x10000 )
157 0 : n += 3;
158 0 : else if ( nUCS4Char < 0x200000 )
159 0 : n += 4;
160 0 : else if ( nUCS4Char < 0x4000000 )
161 0 : n += 5;
162 : else
163 0 : n += 6;
164 : }
165 : }
166 :
167 63533433 : pStr++;
168 : }
169 :
170 3421171 : return n;
171 : }
172 :
173 : /* ----------------------------------------------------------------------- */
174 :
175 3693405 : sal_Bool SAL_CALL rtl_impl_convertUStringToString(rtl_String ** pTarget,
176 : sal_Unicode const * pSource,
177 : sal_Int32 nLength,
178 : rtl_TextEncoding nEncoding,
179 : sal_uInt32 nFlags,
180 : sal_Bool bCheckErrors)
181 : {
182 : OSL_ASSERT(pTarget != NULL
183 : && (pSource != NULL || nLength == 0)
184 : && nLength >= 0
185 : && (nLength == 0 || rtl_isOctetTextEncoding(nEncoding)));
186 :
187 3693405 : if ( !nLength )
188 169470 : rtl_string_new( pTarget );
189 : else
190 : {
191 : rtl_String* pTemp;
192 : rtl_UnicodeToTextConverter hConverter;
193 : sal_uInt32 nInfo;
194 : sal_Size nSrcChars;
195 : sal_Size nDestBytes;
196 : sal_Size nNewLen;
197 : sal_Size nNotConvertedChars;
198 : sal_Size nMaxCharLen;
199 :
200 : /* Optimization for UTF-8 - we try to calculate the exact length */
201 : /* For all other encoding we try an good estimation */
202 3523935 : if ( nEncoding == RTL_TEXTENCODING_UTF8 )
203 : {
204 3421171 : nNewLen = rtl_ImplGetFastUTF8ByteLen( pSource, nLength );
205 : /* Includes the string only ASCII, then we could copy
206 : the buffer faster */
207 3421171 : if ( nNewLen == (sal_Size)nLength )
208 : {
209 : IMPL_RTL_STRCODE* pBuffer;
210 3419411 : if ( *pTarget )
211 122543 : IMPL_RTL_STRINGNAME( release )( *pTarget );
212 3419411 : *pTarget = IMPL_RTL_STRINGNAME( ImplAlloc )( nLength );
213 : OSL_ASSERT(*pTarget != NULL);
214 3419411 : pBuffer = (*pTarget)->buffer;
215 63529931 : do
216 : {
217 : /* Check ASCII range */
218 : OSL_ENSURE( *pSource <= 127,
219 : "rtl_uString2String() - UTF8 test is encoding is wrong" );
220 :
221 63529931 : *pBuffer = (IMPL_RTL_STRCODE)(unsigned char)*pSource;
222 63529931 : pBuffer++;
223 63529931 : pSource++;
224 63529931 : nLength--;
225 : }
226 : while ( nLength );
227 3419411 : return sal_True;
228 : }
229 :
230 1760 : nMaxCharLen = 4;
231 : }
232 : else
233 : {
234 : rtl_TextEncodingInfo aTextEncInfo;
235 102764 : aTextEncInfo.StructSize = sizeof( aTextEncInfo );
236 102764 : if ( !rtl_getTextEncodingInfo( nEncoding, &aTextEncInfo ) )
237 : {
238 0 : aTextEncInfo.AverageCharSize = 1;
239 0 : aTextEncInfo.MaximumCharSize = 8;
240 : }
241 :
242 102764 : nNewLen = nLength*aTextEncInfo.AverageCharSize;
243 102764 : nMaxCharLen = aTextEncInfo.MaximumCharSize;
244 : }
245 :
246 104524 : nFlags |= RTL_UNICODETOTEXT_FLAGS_FLUSH;
247 104524 : hConverter = rtl_createUnicodeToTextConverter( nEncoding );
248 :
249 2 : for (;;)
250 : {
251 104526 : pTemp = IMPL_RTL_STRINGNAME( ImplAlloc )( nNewLen );
252 : OSL_ASSERT(pTemp != NULL);
253 : nDestBytes = rtl_convertUnicodeToText( hConverter, 0,
254 : pSource, nLength,
255 : pTemp->buffer, nNewLen,
256 : nFlags,
257 104526 : &nInfo, &nSrcChars );
258 104526 : if (bCheckErrors && (nInfo & RTL_UNICODETOTEXT_INFO_ERROR) != 0)
259 : {
260 2 : rtl_freeMemory(pTemp);
261 2 : rtl_destroyUnicodeToTextConverter(hConverter);
262 2 : return sal_False;
263 : }
264 :
265 104524 : if ((nInfo & RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL) == 0)
266 104522 : break;
267 :
268 : /* Buffer not big enough, try again with enough space */
269 2 : rtl_freeMemory( pTemp );
270 :
271 : /* Try with the max. count of characters with
272 : additional overhead for replacing functionality */
273 2 : nNotConvertedChars = nLength-nSrcChars;
274 2 : nNewLen = nDestBytes+(nNotConvertedChars*nMaxCharLen)+nNotConvertedChars+4;
275 : }
276 :
277 : /* Set the buffer to the correct size or is there to
278 : much overhead, reallocate to the correct size */
279 104522 : if ( nNewLen > nDestBytes+8 )
280 : {
281 2 : rtl_String* pTemp2 = IMPL_RTL_STRINGNAME( ImplAlloc )( nDestBytes );
282 : OSL_ASSERT(pTemp2 != NULL);
283 2 : rtl_str_ImplCopy( pTemp2->buffer, pTemp->buffer, nDestBytes );
284 2 : rtl_freeMemory( pTemp );
285 2 : pTemp = pTemp2;
286 : }
287 : else
288 : {
289 104520 : pTemp->length = nDestBytes;
290 104520 : pTemp->buffer[nDestBytes] = 0;
291 : }
292 :
293 104522 : rtl_destroyUnicodeToTextConverter( hConverter );
294 104522 : if ( *pTarget )
295 115 : IMPL_RTL_STRINGNAME( release )( *pTarget );
296 104522 : *pTarget = pTemp;
297 :
298 : /* Results the conversion in an empty buffer -
299 : create an empty string */
300 104522 : if ( pTemp && !nDestBytes )
301 1 : rtl_string_new( pTarget );
302 : }
303 273992 : return sal_True;
304 : }
305 :
306 3570600 : void SAL_CALL rtl_uString2String( rtl_String** ppThis,
307 : const sal_Unicode* pUStr,
308 : sal_Int32 nULen,
309 : rtl_TextEncoding eTextEncoding,
310 : sal_uInt32 nCvtFlags )
311 : SAL_THROW_EXTERN_C()
312 : {
313 : rtl_impl_convertUStringToString(ppThis, pUStr, nULen, eTextEncoding,
314 3570600 : nCvtFlags, sal_False);
315 3570600 : }
316 :
317 122805 : sal_Bool SAL_CALL rtl_convertUStringToString(rtl_String ** pTarget,
318 : sal_Unicode const * pSource,
319 : sal_Int32 nLength,
320 : rtl_TextEncoding nEncoding,
321 : sal_uInt32 nFlags)
322 : SAL_THROW_EXTERN_C()
323 : {
324 : return rtl_impl_convertUStringToString(pTarget, pSource, nLength, nEncoding,
325 122805 : nFlags, sal_True);
326 : }
327 :
328 71606 : void rtl_string_newReplaceFirst(
329 : rtl_String ** newStr, rtl_String * str, char const * from,
330 : sal_Int32 fromLength, char const * to, sal_Int32 toLength,
331 : sal_Int32 * index) SAL_THROW_EXTERN_C()
332 : {
333 : assert(str != 0);
334 : assert(index != 0);
335 : assert(*index >= 0 && *index <= str->length);
336 : assert(fromLength >= 0);
337 : assert(toLength >= 0);
338 : sal_Int32 i = rtl_str_indexOfStr_WithLength(
339 71606 : str->buffer + *index, str->length - *index, from, fromLength);
340 71606 : if (i == -1) {
341 55510 : rtl_string_assign(newStr, str);
342 : } else {
343 : assert(i <= str->length - *index);
344 16096 : i += *index;
345 : assert(fromLength <= str->length);
346 16096 : if (str->length - fromLength > SAL_MAX_INT32 - toLength) {
347 0 : std::abort();
348 : }
349 16096 : sal_Int32 n = str->length - fromLength + toLength;
350 16096 : rtl_string_acquire(str); // in case *newStr == str
351 16096 : rtl_string_new_WithLength(newStr, n);
352 16096 : if (n != 0) {
353 16080 : (*newStr)->length = n;
354 : assert(i >= 0 && i < str->length);
355 16080 : memcpy((*newStr)->buffer, str->buffer, i);
356 16080 : memcpy((*newStr)->buffer + i, to, toLength);
357 : memcpy(
358 16080 : (*newStr)->buffer + i + toLength, str->buffer + i + fromLength,
359 32160 : str->length - i - fromLength);
360 : }
361 16096 : rtl_string_release(str);
362 : }
363 71606 : *index = i;
364 71606 : }
365 :
366 23050 : void rtl_string_newReplaceAll(
367 : rtl_String ** newStr, rtl_String * str, char const * from,
368 : sal_Int32 fromLength, char const * to, sal_Int32 toLength)
369 : SAL_THROW_EXTERN_C()
370 : {
371 23050 : rtl_string_assign(newStr, str);
372 38711 : for (sal_Int32 i = 0;; i += toLength) {
373 : rtl_string_newReplaceFirst(
374 38711 : newStr, *newStr, from, fromLength, to, toLength, &i);
375 38711 : if (i == -1) {
376 23050 : break;
377 : }
378 : }
379 23050 : }
380 :
381 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|