Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sal / rtl / string.cxx
blob1d2c5488a15a2abb06f81a81fdf855adbd67ee86
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <cassert>
23 #include <cstdlib>
25 #include <osl/interlck.h>
26 #include <rtl/alloc.h>
27 #include <osl/diagnose.h>
28 #include <rtl/tencinfo.h>
30 #include "strimp.hxx"
31 #include <rtl/character.hxx>
32 #include <rtl/string.h>
34 #include <rtl/math.h>
36 /* ======================================================================= */
38 /* static data to be referenced by all empty strings
39 * the refCount is predefined to 1 and must never become 0 !
41 static rtl_String const aImplEmpty_rtl_String =
43 SAL_STRING_STATIC_FLAG|1,
44 /* sal_Int32 refCount; */
45 0, /* sal_Int32 length; */
46 { 0 } /* sal_Char buffer[1]; */
49 /* ======================================================================= */
50 /* These macros are for the "poor-man templates" included from
51 * the strtmpl.cxx just below, used to share code between here and
52 * ustring.cxx
55 #define IMPL_RTL_IS_USTRING 0
57 #define IMPL_RTL_STRCODE sal_Char
58 #define IMPL_RTL_USTRCODE( c ) (static_cast<unsigned char>(c))
59 #define IMPL_RTL_STRNAME( n ) rtl_str_ ## n
61 #define IMPL_RTL_STRINGNAME( n ) rtl_string_ ## n
62 #define IMPL_RTL_STRINGDATA rtl_String
63 #define IMPL_RTL_EMPTYSTRING aImplEmpty_rtl_String
65 #if USE_SDT_PROBES
66 #define RTL_LOG_STRING_BITS 8
67 #endif
69 /* ======================================================================= */
71 /* Include String/UString template code */
73 #include "strtmpl.cxx"
75 #undef IMPL_RTL_EMPTYSTRING
76 #undef IMPL_RTL_IS_USTRING
77 #undef IMPL_RTL_STRCODE
78 #undef IMPL_RTL_STRINGDATA
79 #undef IMPL_RTL_STRINGNAME
80 #undef IMPL_RTL_STRNAME
81 #undef IMPL_RTL_USTRCODE
82 #undef RTL_LOG_STRING_BITS
84 sal_Int32 SAL_CALL rtl_str_valueOfFloat(sal_Char * pStr, float f)
85 SAL_THROW_EXTERN_C()
87 assert(pStr);
88 rtl_String * pResult = nullptr;
89 sal_Int32 nLen;
90 rtl_math_doubleToString(
91 &pResult, nullptr, 0, f, rtl_math_StringFormat_G,
92 RTL_STR_MAX_VALUEOFFLOAT - RTL_CONSTASCII_LENGTH("-x.E-xxx"), '.', nullptr, 0,
93 true);
94 nLen = pResult->length;
95 OSL_ASSERT(nLen < RTL_STR_MAX_VALUEOFFLOAT);
96 memcpy(pStr, pResult->buffer, (nLen + 1) * sizeof(sal_Char));
97 rtl_string_release(pResult);
98 return nLen;
101 sal_Int32 SAL_CALL rtl_str_valueOfDouble(sal_Char * pStr, double d)
102 SAL_THROW_EXTERN_C()
104 assert(pStr);
105 rtl_String * pResult = nullptr;
106 sal_Int32 nLen;
107 rtl_math_doubleToString(
108 &pResult, nullptr, 0, d, rtl_math_StringFormat_G,
109 RTL_STR_MAX_VALUEOFDOUBLE - RTL_CONSTASCII_LENGTH("-x.E-xxx"), '.', nullptr,
110 0, true);
111 nLen = pResult->length;
112 OSL_ASSERT(nLen < RTL_STR_MAX_VALUEOFDOUBLE);
113 memcpy(pStr, pResult->buffer, (nLen + 1) * sizeof(sal_Char));
114 rtl_string_release(pResult);
115 return nLen;
118 float SAL_CALL rtl_str_toFloat(sal_Char const * pStr) SAL_THROW_EXTERN_C()
120 assert(pStr);
121 return static_cast<float>(rtl_math_stringToDouble(pStr, pStr + rtl_str_getLength(pStr),
122 '.', 0, nullptr, nullptr));
125 double SAL_CALL rtl_str_toDouble(sal_Char const * pStr) SAL_THROW_EXTERN_C()
127 assert(pStr);
128 return rtl_math_stringToDouble(pStr, pStr + rtl_str_getLength(pStr), '.', 0,
129 nullptr, nullptr);
132 /* ======================================================================= */
134 static int rtl_ImplGetFastUTF8ByteLen( const sal_Unicode* pStr, sal_Int32 nLen )
136 int n;
137 sal_Unicode c;
138 sal_uInt32 nUCS4Char;
139 const sal_Unicode* pEndStr;
141 n = 0;
142 pEndStr = pStr+nLen;
143 while ( pStr < pEndStr )
145 c = *pStr;
147 if ( c < 0x80 )
148 n++;
149 else if ( c < 0x800 )
150 n += 2;
151 else
153 if ( !rtl::isHighSurrogate(c) )
154 n += 3;
155 else
157 nUCS4Char = c;
159 if ( pStr+1 < pEndStr )
161 c = *(pStr+1);
162 if ( rtl::isLowSurrogate(c) )
164 nUCS4Char = rtl::combineSurrogates(nUCS4Char, c);
165 pStr++;
169 if ( nUCS4Char < 0x10000 )
170 n += 3;
171 else if ( nUCS4Char < 0x200000 )
172 n += 4;
173 else if ( nUCS4Char < 0x4000000 )
174 n += 5;
175 else
176 n += 6;
180 pStr++;
183 return n;
186 /* ----------------------------------------------------------------------- */
188 static bool rtl_impl_convertUStringToString(rtl_String ** pTarget,
189 sal_Unicode const * pSource,
190 sal_Int32 nLength,
191 rtl_TextEncoding nEncoding,
192 sal_uInt32 nFlags,
193 bool bCheckErrors)
195 assert(pTarget != nullptr);
196 assert(pSource != nullptr || nLength == 0);
197 assert(nLength >= 0);
198 OSL_ASSERT(nLength == 0 || rtl_isOctetTextEncoding(nEncoding));
200 if ( !nLength )
201 rtl_string_new( pTarget );
202 else
204 rtl_String* pTemp;
205 rtl_UnicodeToTextConverter hConverter;
206 sal_uInt32 nInfo;
207 sal_Size nSrcChars;
208 sal_Size nDestBytes;
209 sal_Size nNewLen;
210 sal_Size nNotConvertedChars;
211 sal_Size nMaxCharLen;
213 /* Optimization for UTF-8 - we try to calculate the exact length */
214 /* For all other encoding we try a good estimation */
215 if ( nEncoding == RTL_TEXTENCODING_UTF8 )
217 nNewLen = rtl_ImplGetFastUTF8ByteLen( pSource, nLength );
218 /* Includes the string only ASCII, then we could copy
219 the buffer faster */
220 if ( nNewLen == static_cast<sal_Size>(nLength) )
222 sal_Char* pBuffer;
223 if ( *pTarget )
224 rtl_string_release( *pTarget );
225 *pTarget = rtl_string_ImplAlloc( nLength );
226 OSL_ASSERT(*pTarget != nullptr);
227 pBuffer = (*pTarget)->buffer;
230 /* Check ASCII range */
231 OSL_ENSURE( *pSource <= 127,
232 "rtl_uString2String() - UTF8 test is encoding is wrong" );
234 *pBuffer = static_cast<sal_Char>(static_cast<unsigned char>(*pSource));
235 pBuffer++;
236 pSource++;
237 nLength--;
239 while ( nLength );
240 return true;
243 nMaxCharLen = 4;
245 else
247 rtl_TextEncodingInfo aTextEncInfo;
248 aTextEncInfo.StructSize = sizeof( aTextEncInfo );
249 if ( !rtl_getTextEncodingInfo( nEncoding, &aTextEncInfo ) )
251 aTextEncInfo.AverageCharSize = 1;
252 aTextEncInfo.MaximumCharSize = 8;
255 nNewLen = nLength * static_cast<sal_Size>(aTextEncInfo.AverageCharSize);
256 nMaxCharLen = aTextEncInfo.MaximumCharSize;
259 nFlags |= RTL_UNICODETOTEXT_FLAGS_FLUSH;
260 hConverter = rtl_createUnicodeToTextConverter( nEncoding );
262 for (;;)
264 pTemp = rtl_string_ImplAlloc( nNewLen );
265 OSL_ASSERT(pTemp != nullptr);
266 nDestBytes = rtl_convertUnicodeToText( hConverter, nullptr,
267 pSource, nLength,
268 pTemp->buffer, nNewLen,
269 nFlags,
270 &nInfo, &nSrcChars );
271 if (bCheckErrors && (nInfo & RTL_UNICODETOTEXT_INFO_ERROR) != 0)
273 rtl_freeString(pTemp);
274 rtl_destroyUnicodeToTextConverter(hConverter);
275 return false;
278 if ((nInfo & RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL) == 0)
279 break;
281 /* Buffer not big enough, try again with enough space */
282 rtl_freeString( pTemp );
284 /* Try with the max. count of characters with
285 additional overhead for replacing functionality */
286 nNotConvertedChars = nLength-nSrcChars;
287 nNewLen = nDestBytes+(nNotConvertedChars*nMaxCharLen)+nNotConvertedChars+4;
290 /* Set the buffer to the correct size or is there to
291 much overhead, reallocate to the correct size */
292 if ( nNewLen > nDestBytes+8 )
294 rtl_String* pTemp2 = rtl_string_ImplAlloc( nDestBytes );
295 OSL_ASSERT(pTemp2 != nullptr);
296 rtl_str_ImplCopy( pTemp2->buffer, pTemp->buffer, nDestBytes );
297 rtl_freeString( pTemp );
298 pTemp = pTemp2;
300 else
302 pTemp->length = nDestBytes;
303 pTemp->buffer[nDestBytes] = 0;
306 rtl_destroyUnicodeToTextConverter( hConverter );
307 if ( *pTarget )
308 rtl_string_release( *pTarget );
309 *pTarget = pTemp;
311 /* Results the conversion in an empty buffer -
312 create an empty string */
313 if ( pTemp && !nDestBytes )
314 rtl_string_new( pTarget );
316 return true;
319 void SAL_CALL rtl_uString2String( rtl_String** ppThis,
320 const sal_Unicode* pUStr,
321 sal_Int32 nULen,
322 rtl_TextEncoding eTextEncoding,
323 sal_uInt32 nCvtFlags )
324 SAL_THROW_EXTERN_C()
326 rtl_impl_convertUStringToString(ppThis, pUStr, nULen, eTextEncoding,
327 nCvtFlags, false);
330 sal_Bool SAL_CALL rtl_convertUStringToString(rtl_String ** pTarget,
331 sal_Unicode const * pSource,
332 sal_Int32 nLength,
333 rtl_TextEncoding nEncoding,
334 sal_uInt32 nFlags)
335 SAL_THROW_EXTERN_C()
337 return rtl_impl_convertUStringToString(pTarget, pSource, nLength, nEncoding,
338 nFlags, true);
341 void rtl_string_newReplaceFirst(
342 rtl_String ** newStr, rtl_String * str, char const * from,
343 sal_Int32 fromLength, char const * to, sal_Int32 toLength,
344 sal_Int32 * index) SAL_THROW_EXTERN_C()
346 assert(str != nullptr);
347 assert(index != nullptr);
348 assert(*index >= 0 && *index <= str->length);
349 assert(fromLength >= 0);
350 assert(toLength >= 0);
351 sal_Int32 i = rtl_str_indexOfStr_WithLength(
352 str->buffer + *index, str->length - *index, from, fromLength);
353 if (i == -1) {
354 rtl_string_assign(newStr, str);
355 } else {
356 assert(i <= str->length - *index);
357 i += *index;
358 assert(fromLength <= str->length);
359 if (str->length - fromLength > SAL_MAX_INT32 - toLength) {
360 std::abort();
362 sal_Int32 n = str->length - fromLength + toLength;
363 rtl_string_acquire(str); // in case *newStr == str
364 rtl_string_new_WithLength(newStr, n);
365 if (n != 0) {
366 (*newStr)->length = n;
367 assert(i >= 0 && i < str->length);
368 memcpy((*newStr)->buffer, str->buffer, i);
369 memcpy((*newStr)->buffer + i, to, toLength);
370 memcpy(
371 (*newStr)->buffer + i + toLength, str->buffer + i + fromLength,
372 str->length - i - fromLength);
374 rtl_string_release(str);
376 *index = i;
379 void rtl_string_newReplaceAll(
380 rtl_String ** newStr, rtl_String * str, char const * from,
381 sal_Int32 fromLength, char const * to, sal_Int32 toLength)
382 SAL_THROW_EXTERN_C()
384 rtl_string_assign(newStr, str);
385 for (sal_Int32 i = 0;; i += toLength) {
386 rtl_string_newReplaceFirst(
387 newStr, *newStr, from, fromLength, to, toLength, &i);
388 if (i == -1) {
389 break;
394 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */