sw a11y: clang-format SidebarWinAccessible code
[LibreOffice.git] / sal / rtl / string.cxx
blob33d7f8d076f27ad96812fa3f78a346f408a185fe
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/diagnose.h>
26 #include <rtl/tencinfo.h>
28 #include "strimp.hxx"
29 #include <rtl/character.hxx>
30 #include <rtl/string.h>
32 #include <rtl/math.h>
34 /* ======================================================================= */
36 #if USE_SDT_PROBES
37 #define RTL_LOG_STRING_BITS 8
38 #endif
40 #include "strtmpl.hxx"
42 /* ======================================================================= */
44 sal_Int32 SAL_CALL rtl_str_valueOfFloat(char * pStr, float f) noexcept
46 return rtl::str::valueOfFP<RTL_STR_MAX_VALUEOFFLOAT>(pStr, f);
49 sal_Int32 SAL_CALL rtl_str_valueOfDouble(char * pStr, double d) noexcept
51 return rtl::str::valueOfFP<RTL_STR_MAX_VALUEOFDOUBLE>(pStr, d);
54 float SAL_CALL rtl_str_toFloat(char const * pStr) noexcept
56 assert(pStr);
57 return static_cast<float>(rtl_math_stringToDouble(pStr, pStr + rtl_str_getLength(pStr),
58 '.', 0, nullptr, nullptr));
61 double SAL_CALL rtl_str_toDouble(char const * pStr) noexcept
63 assert(pStr);
64 return rtl_math_stringToDouble(pStr, pStr + rtl_str_getLength(pStr), '.', 0,
65 nullptr, nullptr);
68 /* ======================================================================= */
70 static int rtl_ImplGetFastUTF8ByteLen( const sal_Unicode* pStr, sal_Int32 nLen )
72 int n;
73 sal_Unicode c;
74 sal_uInt32 nUCS4Char;
75 const sal_Unicode* pEndStr;
77 n = 0;
78 pEndStr = pStr+nLen;
79 while ( pStr < pEndStr )
81 c = *pStr;
83 if ( c < 0x80 )
84 n++;
85 else if ( c < 0x800 )
86 n += 2;
87 else
89 if ( !rtl::isHighSurrogate(c) )
90 n += 3;
91 else
93 nUCS4Char = c;
95 if ( pStr+1 < pEndStr )
97 c = *(pStr+1);
98 if ( rtl::isLowSurrogate(c) )
100 nUCS4Char = rtl::combineSurrogates(nUCS4Char, c);
101 pStr++;
105 if ( nUCS4Char < 0x10000 )
106 n += 3;
107 else if ( nUCS4Char < 0x200000 )
108 n += 4;
109 else if ( nUCS4Char < 0x4000000 )
110 n += 5;
111 else
112 n += 6;
116 pStr++;
119 return n;
122 /* ----------------------------------------------------------------------- */
124 static bool rtl_impl_convertUStringToString(rtl_String ** pTarget,
125 sal_Unicode const * pSource,
126 sal_Int32 nLength,
127 rtl_TextEncoding nEncoding,
128 sal_uInt32 nFlags,
129 bool bCheckErrors)
131 assert(pTarget != nullptr);
132 assert(pSource != nullptr || nLength == 0);
133 assert(nLength >= 0);
134 OSL_ASSERT(nLength == 0 || rtl_isOctetTextEncoding(nEncoding));
136 if ( !nLength )
137 rtl_string_new( pTarget );
138 else
140 rtl_String* pTemp;
141 rtl_UnicodeToTextConverter hConverter;
142 sal_uInt32 nInfo;
143 sal_Size nSrcChars;
144 sal_Size nDestBytes;
145 sal_Size nNewLen;
146 sal_Size nNotConvertedChars;
147 sal_Size nMaxCharLen;
149 /* Optimization for UTF-8 - we try to calculate the exact length */
150 /* For all other encoding we try a good estimation */
151 if ( nEncoding == RTL_TEXTENCODING_UTF8 )
153 nNewLen = rtl_ImplGetFastUTF8ByteLen( pSource, nLength );
154 /* Includes the string only ASCII, then we could copy
155 the buffer faster */
156 if ( nNewLen == static_cast<sal_Size>(nLength) )
158 char* pBuffer;
159 if ( *pTarget )
160 rtl_string_release( *pTarget );
161 *pTarget = rtl_string_ImplAlloc( nLength );
162 assert(*pTarget != nullptr);
163 pBuffer = (*pTarget)->buffer;
166 /* Check ASCII range */
167 OSL_ENSURE( *pSource <= 127,
168 "rtl_uString2String() - UTF8 test is encoding is wrong" );
170 *pBuffer = static_cast<char>(static_cast<unsigned char>(*pSource));
171 pBuffer++;
172 pSource++;
173 nLength--;
175 while ( nLength );
176 return true;
179 nMaxCharLen = 4;
181 else
183 rtl_TextEncodingInfo aTextEncInfo;
184 aTextEncInfo.StructSize = sizeof( aTextEncInfo );
185 if ( !rtl_getTextEncodingInfo( nEncoding, &aTextEncInfo ) )
187 aTextEncInfo.AverageCharSize = 1;
188 aTextEncInfo.MaximumCharSize = 8;
191 nNewLen = nLength * static_cast<sal_Size>(aTextEncInfo.AverageCharSize);
192 nMaxCharLen = aTextEncInfo.MaximumCharSize;
195 nFlags |= RTL_UNICODETOTEXT_FLAGS_FLUSH;
196 hConverter = rtl_createUnicodeToTextConverter( nEncoding );
198 for (;;)
200 pTemp = rtl_string_ImplAlloc( nNewLen );
201 assert(pTemp != nullptr);
202 nDestBytes = rtl_convertUnicodeToText( hConverter, nullptr,
203 pSource, nLength,
204 pTemp->buffer, nNewLen,
205 nFlags,
206 &nInfo, &nSrcChars );
207 if (bCheckErrors && (nInfo & RTL_UNICODETOTEXT_INFO_ERROR) != 0)
209 rtl_freeString(pTemp);
210 rtl_destroyUnicodeToTextConverter(hConverter);
211 return false;
214 if ((nInfo & RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL) == 0)
215 break;
217 /* Buffer not big enough, try again with enough space */
218 rtl_freeString( pTemp );
220 /* Try with the max. count of characters with
221 additional overhead for replacing functionality */
222 nNotConvertedChars = nLength-nSrcChars;
223 nNewLen = nDestBytes+(nNotConvertedChars*nMaxCharLen)+nNotConvertedChars+4;
226 /* Set the buffer to the correct size or is there to
227 much overhead, reallocate to the correct size */
228 if ( nNewLen > nDestBytes+8 )
230 rtl_String* pTemp2 = rtl_string_ImplAlloc( nDestBytes );
231 assert(pTemp2 != nullptr);
232 rtl::str::Copy(pTemp2->buffer, pTemp->buffer, nDestBytes);
233 rtl_freeString( pTemp );
234 pTemp = pTemp2;
236 else
238 pTemp->length = nDestBytes;
239 pTemp->buffer[nDestBytes] = 0;
242 rtl_destroyUnicodeToTextConverter( hConverter );
243 if ( *pTarget )
244 rtl_string_release( *pTarget );
245 *pTarget = pTemp;
247 /* Results the conversion in an empty buffer -
248 create an empty string */
249 if ( pTemp && !nDestBytes )
250 rtl_string_new( pTarget );
252 return true;
255 void SAL_CALL rtl_uString2String( rtl_String** ppThis,
256 const sal_Unicode* pUStr,
257 sal_Int32 nULen,
258 rtl_TextEncoding eTextEncoding,
259 sal_uInt32 nCvtFlags ) noexcept
261 rtl_impl_convertUStringToString(ppThis, pUStr, nULen, eTextEncoding,
262 nCvtFlags, false);
265 sal_Bool SAL_CALL rtl_convertUStringToString(rtl_String ** pTarget,
266 sal_Unicode const * pSource,
267 sal_Int32 nLength,
268 rtl_TextEncoding nEncoding,
269 sal_uInt32 nFlags) noexcept
271 return rtl_impl_convertUStringToString(pTarget, pSource, nLength, nEncoding,
272 nFlags, true);
275 void rtl_string_newReplaceFirst(
276 rtl_String ** newStr, rtl_String * str, char const * from,
277 sal_Int32 fromLength, char const * to, sal_Int32 toLength,
278 sal_Int32 * index) noexcept
280 assert(str != nullptr);
281 assert(index != nullptr);
282 assert(*index >= 0 && *index <= str->length);
283 assert(fromLength >= 0);
284 assert(toLength >= 0);
285 sal_Int32 i = rtl_str_indexOfStr_WithLength(
286 str->buffer + *index, str->length - *index, from, fromLength);
287 if (i == -1) {
288 rtl_string_assign(newStr, str);
289 } else {
290 assert(i <= str->length - *index);
291 i += *index;
292 assert(fromLength <= str->length);
293 if (str->length - fromLength > SAL_MAX_INT32 - toLength) {
294 std::abort();
296 rtl::str::newReplaceStrAt(newStr, str, i, fromLength, to, toLength);
298 *index = i;
301 void rtl_string_newReplaceAll(
302 rtl_String ** newStr, rtl_String * str, char const * from,
303 sal_Int32 fromLength, char const * to, sal_Int32 toLength) noexcept
305 rtl::str::newReplaceAllFromIndex(newStr, str, from, fromLength, to, toLength, 0);
308 sal_Int32 SAL_CALL rtl_str_getLength(const char* pStr) noexcept
310 return rtl::str::getLength(pStr);
313 sal_Int32 SAL_CALL rtl_str_compare(const char* pStr1, const char* pStr2) noexcept
315 return rtl::str::compare(rtl::str::null_terminated(pStr1), rtl::str::null_terminated(pStr2),
316 rtl::str::CompareNormal(), rtl::str::noShortening);
319 sal_Int32 SAL_CALL rtl_str_compare_WithLength(const char* pStr1, sal_Int32 nStr1Len,
320 const char* pStr2, sal_Int32 nStr2Len) noexcept
322 return rtl::str::compare(rtl::str::with_length(pStr1, nStr1Len),
323 rtl::str::with_length(pStr2, nStr2Len),
324 rtl::str::CompareNormal(), rtl::str::noShortening);
327 sal_Int32 SAL_CALL rtl_str_shortenedCompare_WithLength(const char* pStr1, sal_Int32 nStr1Len,
328 const char* pStr2, sal_Int32 nStr2Len,
329 sal_Int32 nShortenedLength) noexcept
331 return rtl::str::compare(rtl::str::with_length(pStr1, nStr1Len),
332 rtl::str::with_length(pStr2, nStr2Len),
333 rtl::str::CompareNormal(), nShortenedLength);
336 sal_Int32 SAL_CALL rtl_str_reverseCompare_WithLength(const char* pStr1, sal_Int32 nStr1Len,
337 const char* pStr2, sal_Int32 nStr2Len) noexcept
339 return rtl::str::reverseCompare_WithLengths(pStr1, nStr1Len, pStr2, nStr2Len,
340 rtl::str::CompareNormal());
343 sal_Int32 SAL_CALL rtl_str_compareIgnoreAsciiCase(const char* pStr1, const char* pStr2) noexcept
345 return rtl::str::compare(rtl::str::null_terminated(pStr1), rtl::str::null_terminated(pStr2),
346 rtl::str::CompareIgnoreAsciiCase(), rtl::str::noShortening);
349 sal_Int32 SAL_CALL rtl_str_compareIgnoreAsciiCase_WithLength(const char* pStr1, sal_Int32 nStr1Len,
350 const char* pStr2, sal_Int32 nStr2Len) noexcept
352 return rtl::str::compare(rtl::str::with_length(pStr1, nStr1Len),
353 rtl::str::with_length(pStr2, nStr2Len),
354 rtl::str::CompareIgnoreAsciiCase(), rtl::str::noShortening);
357 sal_Int32 SAL_CALL rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
358 const char* pStr1, sal_Int32 nStr1Len, const char* pStr2, sal_Int32 nStr2Len,
359 sal_Int32 nShortenedLength) noexcept
361 return rtl::str::compare(rtl::str::with_length(pStr1, nStr1Len),
362 rtl::str::with_length(pStr2, nStr2Len),
363 rtl::str::CompareIgnoreAsciiCase(), nShortenedLength);
366 sal_Int32 SAL_CALL rtl_str_hashCode(const char* pStr) noexcept
368 return rtl::str::hashCode(pStr);
371 sal_Int32 SAL_CALL rtl_str_hashCode_WithLength(const char* pStr, sal_Int32 nLen) noexcept
373 return rtl::str::hashCode_WithLength(pStr, nLen);
376 sal_Int32 SAL_CALL rtl_str_indexOfChar(const char* pStr, char c) noexcept
378 return rtl::str::indexOfChar(pStr, c);
381 sal_Int32 SAL_CALL rtl_str_indexOfChar_WithLength(const char* pStr, sal_Int32 nLen, char c) noexcept
383 return rtl::str::indexOfChar_WithLength(pStr, nLen, c);
386 sal_Int32 SAL_CALL rtl_str_lastIndexOfChar(const char* pStr, char c) noexcept
388 return rtl::str::lastIndexOfChar(pStr, c);
391 sal_Int32 SAL_CALL rtl_str_lastIndexOfChar_WithLength(const char* pStr, sal_Int32 nLen, char c) noexcept
393 return rtl::str::lastIndexOfChar_WithLength(pStr, nLen, c);
396 sal_Int32 SAL_CALL rtl_str_indexOfStr(const char* pStr, const char* pSubStr) noexcept
398 return rtl::str::indexOfStr(pStr, pSubStr);
401 sal_Int32 SAL_CALL rtl_str_indexOfStr_WithLength(const char* pStr, sal_Int32 nStrLen,
402 const char* pSubStr, sal_Int32 nSubLen) noexcept
404 return rtl::str::indexOfStr_WithLength(pStr, nStrLen, pSubStr, nSubLen);
407 sal_Int32 SAL_CALL rtl_str_lastIndexOfStr(const char* pStr, const char* pSubStr) noexcept
409 return rtl::str::lastIndexOfStr(pStr, pSubStr);
412 sal_Int32 SAL_CALL rtl_str_lastIndexOfStr_WithLength(const char* pStr, sal_Int32 nStrLen,
413 const char* pSubStr, sal_Int32 nSubLen) noexcept
415 return rtl::str::lastIndexOfStr_WithLength(pStr, nStrLen, pSubStr, nSubLen);
418 void SAL_CALL rtl_str_replaceChar(char* pStr, char cOld, char cNew) noexcept
420 return rtl::str::replaceChars(rtl::str::null_terminated(pStr), rtl::str::FromTo(cOld, cNew));
423 void SAL_CALL rtl_str_replaceChar_WithLength(char* pStr, sal_Int32 nLen, char cOld, char cNew) noexcept
425 return rtl::str::replaceChars(rtl::str::with_length(pStr, nLen), rtl::str::FromTo(cOld, cNew));
428 void SAL_CALL rtl_str_toAsciiLowerCase(char* pStr) noexcept
430 return rtl::str::replaceChars(rtl::str::null_terminated(pStr), rtl::str::toAsciiLower);
433 void SAL_CALL rtl_str_toAsciiLowerCase_WithLength(char* pStr, sal_Int32 nLen) noexcept
435 return rtl::str::replaceChars(rtl::str::with_length(pStr, nLen), rtl::str::toAsciiLower);
438 void SAL_CALL rtl_str_toAsciiUpperCase(char* pStr) noexcept
440 return rtl::str::replaceChars(rtl::str::null_terminated(pStr), rtl::str::toAsciiUpper);
443 void SAL_CALL rtl_str_toAsciiUpperCase_WithLength(char* pStr, sal_Int32 nLen) noexcept
445 return rtl::str::replaceChars(rtl::str::with_length(pStr, nLen), rtl::str::toAsciiUpper);
448 sal_Int32 SAL_CALL rtl_str_trim(char* pStr) noexcept { return rtl::str::trim(pStr); }
450 sal_Int32 SAL_CALL rtl_str_trim_WithLength(char* pStr, sal_Int32 nLen) noexcept
452 return rtl::str::trim_WithLength(pStr, nLen);
455 sal_Int32 SAL_CALL rtl_str_valueOfBoolean(char* pStr, sal_Bool b) noexcept
457 return rtl::str::valueOfBoolean(pStr, b);
460 sal_Int32 SAL_CALL rtl_str_valueOfChar(char* pStr, char c) noexcept
462 return rtl::str::valueOfChar(pStr, c);
465 sal_Int32 SAL_CALL rtl_str_valueOfInt32(char* pStr, sal_Int32 n, sal_Int16 nRadix) noexcept
467 return rtl::str::valueOfInt<RTL_STR_MAX_VALUEOFINT32>(pStr, n, nRadix);
470 sal_Int32 SAL_CALL rtl_str_valueOfInt64(char* pStr, sal_Int64 n, sal_Int16 nRadix) noexcept
472 return rtl::str::valueOfInt<RTL_STR_MAX_VALUEOFINT64>(pStr, n, nRadix);
475 sal_Int32 SAL_CALL rtl_str_valueOfUInt64(char* pStr, sal_uInt64 n, sal_Int16 nRadix) noexcept
477 return rtl::str::valueOfInt<RTL_STR_MAX_VALUEOFUINT64>(pStr, n, nRadix);
480 sal_Bool SAL_CALL rtl_str_toBoolean(const char* pStr) noexcept
482 return rtl::str::toBoolean(pStr);
485 sal_Int32 SAL_CALL rtl_str_toInt32(const char* pStr, sal_Int16 nRadix) noexcept
487 return rtl::str::toInt<sal_Int32>(rtl::str::null_terminated(pStr), nRadix);
490 sal_Int64 SAL_CALL rtl_str_toInt64(const char* pStr, sal_Int16 nRadix) noexcept
492 return rtl::str::toInt<sal_Int64>(rtl::str::null_terminated(pStr), nRadix);
495 sal_Int64 SAL_CALL rtl_str_toInt64_WithLength(const char* pStr, sal_Int16 nRadix,
496 sal_Int32 nStrLength) noexcept
498 return rtl::str::toInt<sal_Int64>(rtl::str::with_length(pStr, nStrLength), nRadix);
501 sal_uInt32 SAL_CALL rtl_str_toUInt32(const char* pStr, sal_Int16 nRadix) noexcept
503 return rtl::str::toInt<sal_uInt32>(rtl::str::null_terminated(pStr), nRadix);
506 sal_uInt64 SAL_CALL rtl_str_toUInt64(const char* pStr, sal_Int16 nRadix) noexcept
508 return rtl::str::toInt<sal_uInt64>(rtl::str::null_terminated(pStr), nRadix);
511 rtl_String* rtl_string_ImplAlloc(sal_Int32 nLen) { return rtl::str::Alloc<rtl_String>(nLen); }
513 void SAL_CALL rtl_string_acquire(rtl_String* pThis) noexcept
515 return rtl::str::acquire(pThis);
518 void SAL_CALL rtl_string_release(rtl_String* pThis) noexcept
520 return rtl::str::release(pThis);
523 void SAL_CALL rtl_string_new(rtl_String** ppThis) noexcept
525 return rtl::str::new_(ppThis);
528 rtl_String* SAL_CALL rtl_string_alloc(sal_Int32 nLen) noexcept
530 assert(nLen >= 0);
531 return rtl::str::Alloc<rtl_String>(nLen);
534 void SAL_CALL rtl_string_new_WithLength(rtl_String** ppThis, sal_Int32 nLen) noexcept
536 rtl::str::new_WithLength(ppThis, nLen);
539 void SAL_CALL rtl_string_newFromString(rtl_String** ppThis, const rtl_String* pStr) noexcept
541 rtl::str::newFromString(ppThis, pStr);
544 void SAL_CALL rtl_string_newFromStr(rtl_String** ppThis, const char* pCharStr) noexcept
546 rtl::str::newFromStr(ppThis, pCharStr);
549 void SAL_CALL rtl_string_newFromStr_WithLength(rtl_String** ppThis, const char* pCharStr,
550 sal_Int32 nLen) noexcept
552 rtl::str::newFromStr_WithLength(ppThis, pCharStr, nLen);
555 void SAL_CALL rtl_string_newFromSubString(rtl_String** ppThis, const rtl_String* pFrom,
556 sal_Int32 beginIndex, sal_Int32 count) noexcept
558 rtl::str::newFromSubString(ppThis, pFrom, beginIndex, count);
561 // Used when creating from string literals.
562 void SAL_CALL rtl_string_newFromLiteral(rtl_String** ppThis, const char* pCharStr, sal_Int32 nLen,
563 sal_Int32 allocExtra) noexcept
565 rtl::str::newFromStr_WithLength(ppThis, pCharStr, nLen, allocExtra);
568 void SAL_CALL rtl_string_assign(rtl_String** ppThis, rtl_String* pStr) noexcept
570 rtl::str::assign(ppThis, pStr);
573 sal_Int32 SAL_CALL rtl_string_getLength(const rtl_String* pThis) noexcept
575 return rtl::str::getLength(pThis);
578 char* SAL_CALL rtl_string_getStr(rtl_String* pThis) noexcept
580 return rtl::str::getStr(pThis);
583 void SAL_CALL rtl_string_newConcat(rtl_String** ppThis, rtl_String* pLeft, rtl_String* pRight) noexcept
585 rtl::str::newConcat(ppThis, pLeft, pRight);
588 void SAL_CALL rtl_string_ensureCapacity(rtl_String** ppThis, sal_Int32 size) noexcept
590 rtl::str::ensureCapacity(ppThis, size);
593 void SAL_CALL rtl_string_newReplaceStrAt(rtl_String** ppThis, rtl_String* pStr, sal_Int32 nIndex,
594 sal_Int32 nCount, rtl_String* pNewSubStr) noexcept
596 rtl::str::newReplaceStrAt(ppThis, pStr, nIndex, nCount, pNewSubStr);
599 void SAL_CALL rtl_string_newReplaceStrAt_WithLength(rtl_String** ppThis, rtl_String* pStr, sal_Int32 nIndex,
600 sal_Int32 nCount, char const * subStr, sal_Int32 substrLen) noexcept
602 rtl::str::newReplaceStrAt(ppThis, pStr, nIndex, nCount, subStr, substrLen);
605 void SAL_CALL rtl_string_newReplace(rtl_String** ppThis, rtl_String* pStr, char cOld, char cNew) noexcept
607 rtl::str::newReplaceChars(ppThis, pStr, rtl::str::FromTo(cOld, cNew));
610 void SAL_CALL rtl_string_newToAsciiLowerCase(rtl_String** ppThis, rtl_String* pStr) noexcept
612 rtl::str::newReplaceChars(ppThis, pStr, rtl::str::toAsciiLower);
615 void SAL_CALL rtl_string_newToAsciiUpperCase(rtl_String** ppThis, rtl_String* pStr) noexcept
617 rtl::str::newReplaceChars(ppThis, pStr, rtl::str::toAsciiUpper);
620 void SAL_CALL rtl_string_newTrim(rtl_String** ppThis, rtl_String* pStr) noexcept
622 rtl::str::newTrim(ppThis, pStr);
625 sal_Int32 SAL_CALL rtl_string_getToken(rtl_String** ppThis, rtl_String* pStr, sal_Int32 nToken,
626 char cTok, sal_Int32 nIndex) noexcept
628 return rtl::str::getToken(ppThis, pStr, nToken, cTok, nIndex);
631 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */