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 :
40 : /* ======================================================================= */
41 :
42 : /* static data to be referenced by all empty strings
43 : * the refCount is predefined to 1 and must never become 0 !
44 : */
45 : static rtl_String const aImplEmpty_rtl_String =
46 : {
47 : SAL_STRING_STATIC_FLAG|1,
48 : /* sal_Int32 refCount; */
49 : 0, /* sal_Int32 length; */
50 : { 0 } /* sal_Char buffer[1]; */
51 : };
52 :
53 : /* ======================================================================= */
54 : /* These macros are for the "poor-man templates" included from
55 : * the strtmpl.cxx just below, used to share code between here and
56 : * ustring.cxx
57 : */
58 : #define IMPL_RTL_STRCODE sal_Char
59 : #define IMPL_RTL_USTRCODE( c ) ((unsigned char)c)
60 : #define IMPL_RTL_STRNAME( n ) rtl_str_ ## n
61 :
62 : #define IMPL_RTL_STRINGNAME( n ) rtl_string_ ## n
63 : #define IMPL_RTL_STRINGDATA rtl_String
64 : #define IMPL_RTL_EMPTYSTRING aImplEmpty_rtl_String
65 :
66 : #if USE_SDT_PROBES
67 : #define RTL_LOG_STRING_BITS 8
68 : #endif
69 :
70 : /* ======================================================================= */
71 :
72 : /* Include String/UString template code */
73 :
74 : #include "strtmpl.cxx"
75 :
76 204 : sal_Int32 SAL_CALL rtl_str_valueOfFloat(sal_Char * pStr, float f)
77 : SAL_THROW_EXTERN_C()
78 : {
79 204 : rtl_String * pResult = NULL;
80 : sal_Int32 nLen;
81 : rtl_math_doubleToString(
82 : &pResult, 0, 0, f, rtl_math_StringFormat_G,
83 : RTL_STR_MAX_VALUEOFFLOAT - RTL_CONSTASCII_LENGTH("-x.E-xxx"), '.', 0, 0,
84 204 : sal_True);
85 204 : nLen = pResult->length;
86 : OSL_ASSERT(nLen < RTL_STR_MAX_VALUEOFFLOAT);
87 204 : memcpy(pStr, pResult->buffer, (nLen + 1) * sizeof(sal_Char));
88 204 : rtl_string_release(pResult);
89 204 : return nLen;
90 : }
91 :
92 9264 : sal_Int32 SAL_CALL rtl_str_valueOfDouble(sal_Char * pStr, double d)
93 : SAL_THROW_EXTERN_C()
94 : {
95 9264 : rtl_String * pResult = NULL;
96 : sal_Int32 nLen;
97 : rtl_math_doubleToString(
98 : &pResult, 0, 0, d, rtl_math_StringFormat_G,
99 : RTL_STR_MAX_VALUEOFDOUBLE - RTL_CONSTASCII_LENGTH("-x.E-xxx"), '.', 0,
100 9264 : 0, sal_True);
101 9264 : nLen = pResult->length;
102 : OSL_ASSERT(nLen < RTL_STR_MAX_VALUEOFDOUBLE);
103 9264 : memcpy(pStr, pResult->buffer, (nLen + 1) * sizeof(sal_Char));
104 9264 : rtl_string_release(pResult);
105 9264 : return nLen;
106 : }
107 :
108 1496 : float SAL_CALL rtl_str_toFloat(sal_Char const * pStr) SAL_THROW_EXTERN_C()
109 : {
110 1496 : return (float) rtl_math_stringToDouble(pStr, pStr + rtl_str_getLength(pStr),
111 1496 : '.', 0, 0, 0);
112 : }
113 :
114 69612 : double SAL_CALL rtl_str_toDouble(sal_Char const * pStr) SAL_THROW_EXTERN_C()
115 : {
116 69612 : return rtl_math_stringToDouble(pStr, pStr + rtl_str_getLength(pStr), '.', 0,
117 69612 : 0, 0);
118 : }
119 :
120 : /* ======================================================================= */
121 :
122 15696923 : static int rtl_ImplGetFastUTF8ByteLen( const sal_Unicode* pStr, sal_Int32 nLen )
123 : {
124 : int n;
125 : sal_Unicode c;
126 : sal_uInt32 nUCS4Char;
127 : const sal_Unicode* pEndStr;
128 :
129 15696923 : n = 0;
130 15696923 : pEndStr = pStr+nLen;
131 384563760 : while ( pStr < pEndStr )
132 : {
133 353169843 : c = *pStr;
134 :
135 353169843 : if ( c < 0x80 )
136 353071647 : n++;
137 98196 : else if ( c < 0x800 )
138 9364 : n += 2;
139 : else
140 : {
141 88832 : if ( !isHighSurrogate(c) )
142 88903 : n += 3;
143 : else
144 : {
145 0 : nUCS4Char = c;
146 :
147 0 : if ( pStr+1 < pEndStr )
148 : {
149 0 : c = *(pStr+1);
150 0 : if ( isLowSurrogate(c) )
151 : {
152 0 : nUCS4Char = combineSurrogates(nUCS4Char, c);
153 0 : pStr++;
154 : }
155 : }
156 :
157 0 : if ( nUCS4Char < 0x10000 )
158 0 : n += 3;
159 0 : else if ( nUCS4Char < 0x200000 )
160 0 : n += 4;
161 0 : else if ( nUCS4Char < 0x4000000 )
162 0 : n += 5;
163 : else
164 0 : n += 6;
165 : }
166 : }
167 :
168 353169914 : pStr++;
169 : }
170 :
171 15696994 : return n;
172 : }
173 :
174 : /* ----------------------------------------------------------------------- */
175 :
176 17162232 : bool SAL_CALL rtl_impl_convertUStringToString(rtl_String ** pTarget,
177 : sal_Unicode const * pSource,
178 : sal_Int32 nLength,
179 : rtl_TextEncoding nEncoding,
180 : sal_uInt32 nFlags,
181 : bool bCheckErrors)
182 : {
183 : OSL_ASSERT(pTarget != NULL
184 : && (pSource != NULL || nLength == 0)
185 : && nLength >= 0
186 : && (nLength == 0 || rtl_isOctetTextEncoding(nEncoding)));
187 :
188 17162232 : if ( !nLength )
189 389890 : rtl_string_new( pTarget );
190 : else
191 : {
192 : rtl_String* pTemp;
193 : rtl_UnicodeToTextConverter hConverter;
194 : sal_uInt32 nInfo;
195 : sal_Size nSrcChars;
196 : sal_Size nDestBytes;
197 : sal_Size nNewLen;
198 : sal_Size nNotConvertedChars;
199 : sal_Size nMaxCharLen;
200 :
201 : /* Optimization for UTF-8 - we try to calculate the exact length */
202 : /* For all other encoding we try an good estimation */
203 16772342 : if ( nEncoding == RTL_TEXTENCODING_UTF8 )
204 : {
205 15696999 : nNewLen = rtl_ImplGetFastUTF8ByteLen( pSource, nLength );
206 : /* Includes the string only ASCII, then we could copy
207 : the buffer faster */
208 15696993 : if ( nNewLen == (sal_Size)nLength )
209 : {
210 : sal_Char* pBuffer;
211 15661261 : if ( *pTarget )
212 919895 : rtl_string_release( *pTarget );
213 15661261 : *pTarget = rtl_string_ImplAlloc( nLength );
214 : OSL_ASSERT(*pTarget != NULL);
215 15661268 : pBuffer = (*pTarget)->buffer;
216 353044639 : do
217 : {
218 : /* Check ASCII range */
219 : OSL_ENSURE( *pSource <= 127,
220 : "rtl_uString2String() - UTF8 test is encoding is wrong" );
221 :
222 353044639 : *pBuffer = (sal_Char)(unsigned char)*pSource;
223 353044639 : pBuffer++;
224 353044639 : pSource++;
225 353044639 : nLength--;
226 : }
227 : while ( nLength );
228 31322558 : return true;
229 : }
230 :
231 35732 : nMaxCharLen = 4;
232 : }
233 : else
234 : {
235 : rtl_TextEncodingInfo aTextEncInfo;
236 1075343 : aTextEncInfo.StructSize = sizeof( aTextEncInfo );
237 1075343 : if ( !rtl_getTextEncodingInfo( nEncoding, &aTextEncInfo ) )
238 : {
239 36 : aTextEncInfo.AverageCharSize = 1;
240 36 : aTextEncInfo.MaximumCharSize = 8;
241 : }
242 :
243 1075343 : nNewLen = nLength * static_cast<sal_Size>(aTextEncInfo.AverageCharSize);
244 1075343 : nMaxCharLen = aTextEncInfo.MaximumCharSize;
245 : }
246 :
247 1111075 : nFlags |= RTL_UNICODETOTEXT_FLAGS_FLUSH;
248 1111075 : hConverter = rtl_createUnicodeToTextConverter( nEncoding );
249 :
250 : for (;;)
251 : {
252 1111081 : pTemp = rtl_string_ImplAlloc( nNewLen );
253 : OSL_ASSERT(pTemp != NULL);
254 : nDestBytes = rtl_convertUnicodeToText( hConverter, 0,
255 : pSource, nLength,
256 : pTemp->buffer, nNewLen,
257 : nFlags,
258 1111081 : &nInfo, &nSrcChars );
259 1111081 : if (bCheckErrors && (nInfo & RTL_UNICODETOTEXT_INFO_ERROR) != 0)
260 : {
261 22 : rtl_freeMemory(pTemp);
262 22 : rtl_destroyUnicodeToTextConverter(hConverter);
263 22 : return false;
264 : }
265 :
266 1111059 : if ((nInfo & RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL) == 0)
267 1111053 : break;
268 :
269 : /* Buffer not big enough, try again with enough space */
270 6 : rtl_freeMemory( pTemp );
271 :
272 : /* Try with the max. count of characters with
273 : additional overhead for replacing functionality */
274 6 : nNotConvertedChars = nLength-nSrcChars;
275 6 : nNewLen = nDestBytes+(nNotConvertedChars*nMaxCharLen)+nNotConvertedChars+4;
276 : }
277 :
278 : /* Set the buffer to the correct size or is there to
279 : much overhead, reallocate to the correct size */
280 1111053 : if ( nNewLen > nDestBytes+8 )
281 : {
282 40 : rtl_String* pTemp2 = rtl_string_ImplAlloc( nDestBytes );
283 : OSL_ASSERT(pTemp2 != NULL);
284 40 : rtl_str_ImplCopy( pTemp2->buffer, pTemp->buffer, nDestBytes );
285 40 : rtl_freeMemory( pTemp );
286 40 : pTemp = pTemp2;
287 : }
288 : else
289 : {
290 1111013 : pTemp->length = nDestBytes;
291 1111013 : pTemp->buffer[nDestBytes] = 0;
292 : }
293 :
294 1111053 : rtl_destroyUnicodeToTextConverter( hConverter );
295 1111053 : if ( *pTarget )
296 79223 : rtl_string_release( *pTarget );
297 1111053 : *pTarget = pTemp;
298 :
299 : /* Results the conversion in an empty buffer -
300 : create an empty string */
301 1111053 : if ( pTemp && !nDestBytes )
302 8 : rtl_string_new( pTarget );
303 : }
304 1500943 : return true;
305 : }
306 :
307 16144848 : void SAL_CALL rtl_uString2String( rtl_String** ppThis,
308 : const sal_Unicode* pUStr,
309 : sal_Int32 nULen,
310 : rtl_TextEncoding eTextEncoding,
311 : sal_uInt32 nCvtFlags )
312 : SAL_THROW_EXTERN_C()
313 : {
314 : rtl_impl_convertUStringToString(ppThis, pUStr, nULen, eTextEncoding,
315 16144848 : nCvtFlags, false);
316 16144848 : }
317 :
318 1017387 : sal_Bool SAL_CALL rtl_convertUStringToString(rtl_String ** pTarget,
319 : sal_Unicode const * pSource,
320 : sal_Int32 nLength,
321 : rtl_TextEncoding nEncoding,
322 : sal_uInt32 nFlags)
323 : SAL_THROW_EXTERN_C()
324 : {
325 : return rtl_impl_convertUStringToString(pTarget, pSource, nLength, nEncoding,
326 1017387 : nFlags, true);
327 : }
328 :
329 107670 : void rtl_string_newReplaceFirst(
330 : rtl_String ** newStr, rtl_String * str, char const * from,
331 : sal_Int32 fromLength, char const * to, sal_Int32 toLength,
332 : sal_Int32 * index) SAL_THROW_EXTERN_C()
333 : {
334 : assert(str != 0);
335 : assert(index != 0);
336 : assert(*index >= 0 && *index <= str->length);
337 : assert(fromLength >= 0);
338 : assert(toLength >= 0);
339 : sal_Int32 i = rtl_str_indexOfStr_WithLength(
340 107670 : str->buffer + *index, str->length - *index, from, fromLength);
341 107670 : if (i == -1) {
342 95198 : rtl_string_assign(newStr, str);
343 : } else {
344 : assert(i <= str->length - *index);
345 12472 : i += *index;
346 : assert(fromLength <= str->length);
347 12472 : if (str->length - fromLength > SAL_MAX_INT32 - toLength) {
348 0 : std::abort();
349 : }
350 12472 : sal_Int32 n = str->length - fromLength + toLength;
351 12472 : rtl_string_acquire(str); // in case *newStr == str
352 12472 : rtl_string_new_WithLength(newStr, n);
353 12472 : if (n != 0) {
354 12470 : (*newStr)->length = n;
355 : assert(i >= 0 && i < str->length);
356 12470 : memcpy((*newStr)->buffer, str->buffer, i);
357 12470 : memcpy((*newStr)->buffer + i, to, toLength);
358 : memcpy(
359 12470 : (*newStr)->buffer + i + toLength, str->buffer + i + fromLength,
360 24940 : str->length - i - fromLength);
361 : }
362 12472 : rtl_string_release(str);
363 : }
364 107670 : *index = i;
365 107670 : }
366 :
367 71352 : void rtl_string_newReplaceAll(
368 : rtl_String ** newStr, rtl_String * str, char const * from,
369 : sal_Int32 fromLength, char const * to, sal_Int32 toLength)
370 : SAL_THROW_EXTERN_C()
371 : {
372 71352 : rtl_string_assign(newStr, str);
373 83818 : for (sal_Int32 i = 0;; i += toLength) {
374 : rtl_string_newReplaceFirst(
375 83818 : newStr, *newStr, from, fromLength, to, toLength, &i);
376 83818 : if (i == -1) {
377 71352 : break;
378 : }
379 12466 : }
380 71352 : }
381 :
382 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|