LibreOffice
LibreOffice 7.2 SDK C/C++ API Reference
ustrbuf.hxx
Go to the documentation of this file.
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/*
21 * This file is part of LibreOffice published API.
22 */
23
24#ifndef INCLUDED_RTL_USTRBUF_HXX
25#define INCLUDED_RTL_USTRBUF_HXX
26
27#include "sal/config.h"
28
29#include <cassert>
30#include <cstring>
31#include <limits>
32#include <new>
33
34#if defined LIBO_INTERNAL_ONLY
35#include <string_view>
36#endif
37
38#include "rtl/ustrbuf.h"
39#include "rtl/ustring.hxx"
40#include "rtl/stringutils.hxx"
41#include "sal/types.h"
42
43#ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
44#include "rtl/stringconcat.hxx"
45#endif
46
47#ifdef RTL_STRING_UNITTEST
48extern bool rtl_string_unittest_invalid_conversion;
49#endif
50
51// The unittest uses slightly different code to help check that the proper
52// calls are made. The class is put into a different namespace to make
53// sure the compiler generates a different (if generating also non-inline)
54// copy of the function and does not merge them together. The class
55// is "brought" into the proper rtl namespace by a typedef below.
56#ifdef RTL_STRING_UNITTEST
57#define rtl rtlunittest
58#endif
59
60namespace rtl
61{
62
63#ifdef RTL_STRING_UNITTEST
64#undef rtl
65#endif
66
70{
71friend class OUString;
72public:
78 : pData(NULL)
79 , nCapacity( 16 )
80 {
81 rtl_uString_new_WithLength( &pData, nCapacity );
82 }
83
91 : pData(NULL)
92 , nCapacity( value.nCapacity )
93 {
94 rtl_uStringbuffer_newFromStringBuffer( &pData, value.nCapacity, value.pData );
95 }
96
103 explicit OUStringBuffer(int length)
104 : pData(NULL)
105 , nCapacity( length )
106 {
107 rtl_uString_new_WithLength( &pData, length );
108 }
109#if __cplusplus >= 201103L
110 explicit OUStringBuffer(unsigned int length)
111 : OUStringBuffer(static_cast<int>(length))
112 {
113 }
114#if SAL_TYPES_SIZEOFLONG == 4
115 // additional overloads for sal_Int32 sal_uInt32
116 explicit OUStringBuffer(long length)
117 : OUStringBuffer(static_cast<int>(length))
118 {
119 }
120 explicit OUStringBuffer(unsigned long length)
121 : OUStringBuffer(static_cast<int>(length))
122 {
123 }
124#endif
125 // avoid obvious bugs
126 explicit OUStringBuffer(char) = delete;
127 explicit OUStringBuffer(sal_Unicode) = delete;
128#endif
129
140#if defined LIBO_INTERNAL_ONLY
141 explicit OUStringBuffer(std::u16string_view sv)
142 : pData(nullptr)
143 , nCapacity( sv.length() + 16 )
144 {
145 if (sv.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max())) {
146 throw std::bad_alloc();
147 }
148 rtl_uStringbuffer_newFromStr_WithLength( &pData, sv.data(), sv.length() );
149 }
150#endif
152 : pData(NULL)
153 , nCapacity( value.getLength() + 16 )
154 {
155 rtl_uStringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
156 }
157
158 template< typename T >
160 : pData(NULL)
161 , nCapacity( libreoffice_internal::ConstCharArrayDetector<T>::length + 16 )
162 {
163 assert(
166 &pData,
169#ifdef RTL_STRING_UNITTEST
170 rtl_string_unittest_const_literal = true;
171#endif
172 }
173
174#if defined LIBO_INTERNAL_ONLY
176 template<typename T>
178 T & literal,
180 T, libreoffice_internal::Dummy>::TypeUtf16
182 pData(nullptr),
183 nCapacity(libreoffice_internal::ConstCharArrayDetector<T>::length + 16)
184 {
186 &pData,
189 }
190
192 template<std::size_t N> OUStringBuffer(OUStringLiteral<N> const & literal):
193 pData(nullptr), nCapacity(literal.getLength() + 16) //TODO: check for overflow
194 {
195 rtl_uStringbuffer_newFromStr_WithLength(&pData, literal.getStr(), literal.getLength());
196 }
197#endif
198
199#if defined LIBO_INTERNAL_ONLY && defined RTL_STRING_UNITTEST
201
205 template< typename T >
206 OUStringBuffer( T&, typename libreoffice_internal::ExceptConstCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
207 {
208 pData = NULL;
209 nCapacity = 10;
210 rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
211 rtl_string_unittest_invalid_conversion = true;
212 }
217 template< typename T >
218 OUStringBuffer( const T&, typename libreoffice_internal::ExceptCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
219 {
220 pData = NULL;
221 nCapacity = 10;
222 rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
223 rtl_string_unittest_invalid_conversion = true;
224 }
226#endif
227
228#ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
233 template< typename T1, typename T2 >
234 OUStringBuffer( OUStringConcat< T1, T2 >&& c )
235 {
236 const sal_Int32 l = c.length();
237 nCapacity = l + 16;
238 pData = rtl_uString_alloc( nCapacity );
239 sal_Unicode* end = c.addData( pData->buffer );
240 *end = '\0';
241 pData->length = l;
242 }
243
248 template< typename T >
249 OUStringBuffer( OUStringNumber< T >&& n )
250 : pData(NULL)
251 , nCapacity( n.length + 16 )
252 {
253 rtl_uStringbuffer_newFromStr_WithLength( &pData, n.buf, n.length );
254 }
255#endif
256
257#if defined LIBO_INTERNAL_ONLY
258 operator std::u16string_view() const { return {getStr(), sal_uInt32(getLength())}; }
259#endif
260
263 OUStringBuffer& operator = ( const OUStringBuffer& value )
264 {
265 if (this != &value)
266 {
268 value.nCapacity,
269 value.pData);
270 nCapacity = value.nCapacity;
271 }
272 return *this;
273 }
274
279#if defined LIBO_INTERNAL_ONLY
280 OUStringBuffer & operator =(std::u16string_view string) {
281 sal_Int32 n = string.length();
282 if (n >= nCapacity) {
283 ensureCapacity(n + 16); //TODO: check for overflow
284 }
285 std::memcpy(
286 pData->buffer, string.data(),
287 (n + 1) * sizeof (sal_Unicode));
288 pData->length = n;
289 return *this;
290 }
291#endif
292 OUStringBuffer & operator =(OUString const & string) {
293 sal_Int32 n = string.getLength();
294 if (n >= nCapacity) {
295 ensureCapacity(n + 16); //TODO: check for overflow
296 }
297 std::memcpy(
298 pData->buffer, string.pData->buffer,
299 (n + 1) * sizeof (sal_Unicode));
300 pData->length = n;
301 return *this;
302 }
303
308 template<typename T>
309 typename
311 operator =(T & literal) {
312 assert(
314 sal_Int32 const n
316 if (n >= nCapacity) {
317 ensureCapacity(n + 16); //TODO: check for overflow
318 }
319 char const * from
321 literal);
322 sal_Unicode * to = pData->buffer;
323 for (sal_Int32 i = 0; i <= n; ++i) {
324 to[i] = from[i];
325 }
326 pData->length = n;
327 return *this;
328 }
329
330#if defined LIBO_INTERNAL_ONLY
332 template<typename T>
334 T, OUStringBuffer &>::TypeUtf16
335 operator =(T & literal) {
336 sal_Int32 const n
338 if (n >= nCapacity) {
339 ensureCapacity(n + 16); //TODO: check for overflow
340 }
341 std::memcpy(
342 pData->buffer,
343 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
344 (n + 1) * sizeof (sal_Unicode)); //TODO: check for overflow
345 pData->length = n;
346 return *this;
347 }
348
350 template<std::size_t N> OUStringBuffer & operator =(OUStringLiteral<N> const & literal) {
351 sal_Int32 const n = literal.getLength();
352 if (n >= nCapacity) {
353 ensureCapacity(n + 16); //TODO: check for overflow
354 }
355 std::memcpy(
356 pData->buffer, literal.getStr(),
357 (n + 1) * sizeof (sal_Unicode)); //TODO: check for overflow
358 pData->length = n;
359 return *this;
360 }
361#endif
362
363#if defined LIBO_INTERNAL_ONLY
365 template<typename T1, typename T2>
366 OUStringBuffer & operator =(OUStringConcat<T1, T2> && concat) {
367 sal_Int32 const n = concat.length();
368 if (n >= nCapacity) {
369 ensureCapacity(n + 16); //TODO: check for overflow
370 }
371 *concat.addData(pData->buffer) = 0;
372 pData->length = n;
373 return *this;
374 }
375
377 template<typename T>
378 OUStringBuffer & operator =(OUStringNumber<T> && n)
379 {
380 *this = OUStringBuffer( std::move( n ) );
381 return *this;
382 }
383#endif
384
389 {
390 rtl_uString_release( pData );
391 }
392
402 {
403 return OUString(
404 rtl_uStringBuffer_makeStringAndClear( &pData, &nCapacity ),
406 }
407
413 sal_Int32 getLength() const
414 {
415 return pData->length;
416 }
417
426 bool isEmpty() const
427 {
428 return pData->length == 0;
429 }
430
441 sal_Int32 getCapacity() const
442 {
443 return nCapacity;
444 }
445
457 void ensureCapacity(sal_Int32 minimumCapacity)
458 {
459 rtl_uStringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
460 }
461
480 void setLength(sal_Int32 newLength)
481 {
482 assert(newLength >= 0);
483 // Avoid modifications if pData points to const empty string:
484 if( newLength != pData->length )
485 {
486 if( newLength > nCapacity )
487 rtl_uStringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
488 else
489 pData->buffer[newLength] = 0;
490 pData->length = newLength;
491 }
492 }
493
507 SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
508 sal_Unicode charAt( sal_Int32 index ) const
509 {
510 assert(index >= 0 && index < pData->length);
511 return pData->buffer[ index ];
512 }
513
524 SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
525 OUStringBuffer & setCharAt(sal_Int32 index, sal_Unicode ch)
526 {
527 assert(index >= 0 && index < pData->length);
528 pData->buffer[ index ] = ch;
529 return *this;
530 }
531
535 const sal_Unicode* getStr() const SAL_RETURNS_NONNULL { return pData->buffer; }
536
546 sal_Unicode & operator [](sal_Int32 index)
547 {
548 assert(index >= 0 && index < pData->length);
549 return pData->buffer[index];
550 }
551
561 const sal_Unicode & operator [](sal_Int32 index) const
562 {
563 assert(index >= 0 && index < pData->length);
564 return pData->buffer[index];
565 }
566
572 {
573 return OUString(pData->buffer, pData->length);
574 }
575
587 {
588 return append( str.getStr(), str.getLength() );
589 }
590
591#if defined LIBO_INTERNAL_ONLY
592 OUStringBuffer & append(std::u16string_view sv) {
593 if (sv.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max())) {
594 throw std::bad_alloc();
595 }
596 return append(sv.data(), sv.size());
597 }
598#endif
599
613 {
614 if(!str.isEmpty())
615 {
616 append( str.getStr(), str.getLength() );
617 }
618 return *this;
619 }
620
632#if defined LIBO_INTERNAL_ONLY
633 template<typename T>
635 append(T const & str)
636#else
638#endif
639 {
640 return append( str, rtl_ustr_getLength( str ) );
641 }
642
656 OUStringBuffer & append( const sal_Unicode * str, sal_Int32 len)
657 {
658 assert( len == 0 || str != NULL ); // cannot assert that in rtl_uStringbuffer_insert
659 rtl_uStringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
660 return *this;
661 }
662
668 template< typename T >
670 {
671 assert(
673 return appendAscii(
676 }
677
678#if defined LIBO_INTERNAL_ONLY
679 template<typename T>
681 append(T & value) { return append(static_cast<sal_Unicode *>(value)); }
682
684 template<typename T>
685 typename libreoffice_internal::ConstCharArrayDetector<
686 T, OUStringBuffer &>::TypeUtf16
687 append(T & literal) {
688 return append(
689 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
690 libreoffice_internal::ConstCharArrayDetector<T>::length);
691 }
692
694 template<std::size_t N> OUStringBuffer & append(OUStringLiteral<N> const & literal) {
695 return append(literal.getStr(), literal.getLength());
696 }
697#endif
698
699#ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
704 template< typename T1, typename T2 >
705 OUStringBuffer& append( OUStringConcat< T1, T2 >&& c )
706 {
707 sal_Int32 l = c.length();
708 if( l == 0 )
709 return *this;
710 l += pData->length;
711 rtl_uStringbuffer_ensureCapacity( &pData, &nCapacity, l );
712 sal_Unicode* end = c.addData( pData->buffer + pData->length );
713 *end = '\0';
714 pData->length = l;
715 return *this;
716 }
717
722 template< typename T >
723 OUStringBuffer& append( OUStringNumber< T >&& c )
724 {
725 return append( c.buf, c.length );
726 }
727#endif
728
745 OUStringBuffer & appendAscii( const char * str )
746 {
747 return appendAscii( str, rtl_str_getLength( str ) );
748 }
749
768 OUStringBuffer & appendAscii( const char * str, sal_Int32 len)
769 {
770 rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), str, len );
771 return *this;
772 }
773
788 {
790 return append( sz, rtl_ustr_valueOfBoolean( sz, b ) );
791 }
792
794 // Pointer can be automatically converted to bool, which is unwanted here.
795 // Explicitly delete all pointer append() overloads to prevent this
796 // (except for char* and sal_Unicode* overloads, which are handled elsewhere).
797 template< typename T >
798 typename libreoffice_internal::Enable< void,
800 append( T* ) SAL_DELETED_FUNCTION;
802
803 // This overload is needed because OUString has a ctor from rtl_uString*, but
804 // the bool overload above would be preferred to the conversion.
808 OUStringBuffer & append(rtl_uString* str)
809 {
810 return append( OUString( str ));
811 }
812
825 {
827 return append( sz, rtl_ustr_valueOfBoolean( sz, b ) );
828 }
829
843 {
844 assert(static_cast< unsigned char >(c) <= 0x7F);
845 return append(sal_Unicode(c));
846 }
847
859 {
860 return append( &c, 1 );
861 }
862
863#if defined LIBO_INTERNAL_ONLY
864 void append(sal_uInt16) = delete;
865#endif
866
879 OUStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
880 {
882 return append( sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
883 }
884
897 OUStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
898 {
900 return append( sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
901 }
902
915 {
917 return append( sz, rtl_ustr_valueOfFloat( sz, f ) );
918 }
919
932 {
934 return append( sz, rtl_ustr_valueOfDouble( sz, d ) );
935 }
936
950 OUStringBuffer & appendUtf32(sal_uInt32 c) {
951 return insertUtf32(getLength(), c);
952 }
953
969 sal_Unicode * appendUninitialized(sal_Int32 length) SAL_RETURNS_NONNULL {
970 sal_Int32 n = getLength();
971 rtl_uStringbuffer_insert(&pData, &nCapacity, n, NULL, length);
972 return pData->buffer + n;
973 }
974
990#if defined LIBO_INTERNAL_ONLY
991 OUStringBuffer & insert(sal_Int32 offset, std::u16string_view str)
992 {
993 return insert( offset, str.data(), str.length() );
994 }
995#endif
996 OUStringBuffer & insert(sal_Int32 offset, const OUString & str)
997 {
998 return insert( offset, str.getStr(), str.getLength() );
999 }
1000
1018 OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str )
1019 {
1020 return insert( offset, str, rtl_ustr_getLength( str ) );
1021 }
1022
1041 OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str, sal_Int32 len)
1042 {
1043 assert( len == 0 || str != NULL ); // cannot assert that in rtl_uStringbuffer_insert
1044 rtl_uStringbuffer_insert( &pData, &nCapacity, offset, str, len );
1045 return *this;
1046 }
1047
1053 template< typename T >
1055 {
1056 assert(
1059 &pData, &nCapacity, offset,
1062 return *this;
1063 }
1064
1065#if defined LIBO_INTERNAL_ONLY
1067 template<typename T>
1069 T, OUStringBuffer &>::TypeUtf16
1070 insert(sal_Int32 offset, T & literal) {
1071 return insert(
1072 offset,
1075 }
1076
1078 template<std::size_t N>
1079 OUStringBuffer & insert(sal_Int32 offset, OUStringLiteral<N> const & literal) {
1080 return insert(offset, literal.getStr(), literal.getLength());
1081 }
1082#endif
1083
1101 OUStringBuffer & insert(sal_Int32 offset, sal_Bool b)
1102 {
1104 return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
1105 }
1106
1126 OUStringBuffer & insert(sal_Int32 offset, bool b)
1127 {
1129 return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
1130 }
1131
1150 OUStringBuffer & insert(sal_Int32 offset, char c)
1151 {
1152 sal_Unicode u = c;
1153 return insert( offset, &u, 1 );
1154 }
1155
1172 OUStringBuffer & insert(sal_Int32 offset, sal_Unicode c)
1173 {
1174 return insert( offset, &c, 1 );
1175 }
1176
1196 OUStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
1197 {
1199 return insert( offset, sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
1200 }
1201
1221 OUStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
1222 {
1224 return insert( offset, sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
1225 }
1226
1245 OUStringBuffer insert(sal_Int32 offset, float f)
1246 {
1248 return insert( offset, sz, rtl_ustr_valueOfFloat( sz, f ) );
1249 }
1250
1269 OUStringBuffer & insert(sal_Int32 offset, double d)
1270 {
1272 return insert( offset, sz, rtl_ustr_valueOfDouble( sz, d ) );
1273 }
1274
1290 OUStringBuffer & insertUtf32(sal_Int32 offset, sal_uInt32 c) {
1291 rtl_uStringbuffer_insertUtf32(&pData, &nCapacity, offset, c);
1292 return *this;
1293 }
1294
1307 OUStringBuffer & remove( sal_Int32 start, sal_Int32 len )
1308 {
1309 rtl_uStringbuffer_remove( &pData, start, len );
1310 return *this;
1311 }
1312
1323 OUStringBuffer & truncate( sal_Int32 start = 0 )
1324 {
1325 rtl_uStringbuffer_remove( &pData, start, getLength() - start );
1326 return *this;
1327 }
1328
1340 {
1341 sal_Int32 index = 0;
1342 while((index = indexOf(oldChar, index)) >= 0)
1343 {
1344 pData->buffer[ index ] = newChar;
1345 }
1346 return *this;
1347 }
1348
1364 void accessInternals(rtl_uString *** pInternalData,
1365 sal_Int32 ** pInternalCapacity)
1366 {
1367 *pInternalData = &pData;
1368 *pInternalCapacity = &nCapacity;
1369 }
1370
1371
1387 sal_Int32 indexOf( sal_Unicode ch, sal_Int32 fromIndex = 0 ) const
1388 {
1389 assert( fromIndex >= 0 && fromIndex <= pData->length );
1390 sal_Int32 ret = rtl_ustr_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch );
1391 return (ret < 0 ? ret : ret+fromIndex);
1392 }
1393
1405 sal_Int32 lastIndexOf( sal_Unicode ch ) const
1406 {
1407 return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch );
1408 }
1409
1424 sal_Int32 lastIndexOf( sal_Unicode ch, sal_Int32 fromIndex ) const
1425 {
1426 assert( fromIndex >= 0 && fromIndex <= pData->length );
1427 return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch );
1428 }
1429
1447#if defined LIBO_INTERNAL_ONLY
1448 sal_Int32 indexOf( std::u16string_view str, sal_Int32 fromIndex = 0 ) const
1449 {
1450 assert( fromIndex >= 0 && fromIndex <= pData->length );
1451 sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1452 str.data(), str.length() );
1453 return (ret < 0 ? ret : ret+fromIndex);
1454 }
1455#endif
1456 sal_Int32 indexOf( const OUString & str, sal_Int32 fromIndex = 0 ) const
1457 {
1458 assert( fromIndex >= 0 && fromIndex <= pData->length );
1459 sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1460 str.pData->buffer, str.pData->length );
1461 return (ret < 0 ? ret : ret+fromIndex);
1462 }
1463
1470 template< typename T >
1471 typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const
1472 {
1473 assert(
1476 pData->buffer + fromIndex, pData->length - fromIndex,
1479 return n < 0 ? n : n + fromIndex;
1480 }
1481
1482#if defined LIBO_INTERNAL_ONLY
1484 template<typename T>
1485 typename
1487 indexOf(T & literal, sal_Int32 fromIndex = 0) const {
1488 assert(fromIndex >= 0);
1490 pData->buffer + fromIndex, pData->length - fromIndex,
1493 return n < 0 ? n : n + fromIndex;
1494 }
1495
1497 template<std::size_t N>
1498 sal_Int32 indexOf(OUStringLiteral<N> const & literal, sal_Int32 fromIndex = 0)
1499 const
1500 {
1501 sal_Int32 n = rtl_ustr_indexOfStr_WithLength(
1502 pData->buffer + fromIndex, pData->length - fromIndex, literal.getStr(),
1503 literal.getLength());
1504 return n < 0 ? n : n + fromIndex;
1505 }
1506#endif
1507
1525#if defined LIBO_INTERNAL_ONLY
1526 sal_Int32 lastIndexOf( std::u16string_view str ) const
1527 {
1528 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1529 str.data(), str.length() );
1530 }
1531#endif
1532 sal_Int32 lastIndexOf( const OUString & str ) const
1533 {
1534 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1535 str.pData->buffer, str.pData->length );
1536 }
1537
1557 sal_Int32 lastIndexOf( const OUString & str, sal_Int32 fromIndex ) const
1558 {
1559 assert( fromIndex >= 0 && fromIndex <= pData->length );
1560 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1561 str.pData->buffer, str.pData->length );
1562 }
1563
1569 template< typename T >
1571 {
1572 assert(
1575 pData->buffer, pData->length,
1578 }
1579
1580#if defined LIBO_INTERNAL_ONLY
1582 template<typename T>
1583 typename
1585 lastIndexOf(T & literal) const {
1587 pData->buffer, pData->length,
1590 }
1591
1593 template<std::size_t N> sal_Int32 lastIndexOf(OUStringLiteral<N> const & literal) const {
1595 pData->buffer, pData->length, literal.getStr(), literal.getLength());
1596 }
1597#endif
1598
1608 sal_Int32 stripStart(sal_Unicode c = ' ')
1609 {
1610 sal_Int32 index;
1611 for(index = 0; index < getLength() ; index++)
1612 {
1613 if(pData->buffer[ index ] != c)
1614 {
1615 break;
1616 }
1617 }
1618 if(index)
1619 {
1620 remove(0, index);
1621 }
1622 return index;
1623 }
1624
1634 sal_Int32 stripEnd(sal_Unicode c = ' ')
1635 {
1636 sal_Int32 result = getLength();
1637 sal_Int32 index;
1638 for(index = getLength(); index > 0 ; index--)
1639 {
1640 if(pData->buffer[ index - 1 ] != c)
1641 {
1642 break;
1643 }
1644 }
1645 if(index < getLength())
1646 {
1647 truncate(index);
1648 }
1649 return result - getLength();
1650 }
1660 sal_Int32 strip(sal_Unicode c = ' ')
1661 {
1662 return stripStart(c) + stripEnd(c);
1663 }
1664
1665#if defined LIBO_INTERNAL_ONLY
1676 SAL_WARN_UNUSED_RESULT std::u16string_view subView( sal_Int32 beginIndex ) const
1677 {
1678 assert(beginIndex >= 0);
1679 assert(beginIndex <= getLength());
1680 return subView(beginIndex, getLength() - beginIndex);
1681 }
1682
1695 SAL_WARN_UNUSED_RESULT std::u16string_view subView( sal_Int32 beginIndex, sal_Int32 count ) const
1696 {
1697 assert(beginIndex >= 0);
1698 assert(count >= 0);
1699 assert(beginIndex <= getLength());
1700 assert(count <= getLength() - beginIndex);
1701 return std::u16string_view(pData->buffer, sal_uInt32(pData->length)).substr(beginIndex, count);
1702 }
1703#endif
1704
1716 OUStringBuffer copy( sal_Int32 beginIndex ) const
1717 {
1718 return copy( beginIndex, getLength() - beginIndex );
1719 }
1720
1734 OUStringBuffer copy( sal_Int32 beginIndex, sal_Int32 count ) const
1735 {
1736 assert(beginIndex >= 0 && beginIndex <= getLength());
1737 assert(count >= 0 && count <= getLength() - beginIndex);
1738 rtl_uString *pNew = NULL;
1739 rtl_uStringbuffer_newFromStr_WithLength( &pNew, getStr() + beginIndex, count );
1740 return OUStringBuffer( pNew, count + 16 );
1741 }
1742
1743private:
1744 OUStringBuffer( rtl_uString * value, const sal_Int32 capacity )
1745 {
1746 pData = value;
1747 nCapacity = capacity;
1748 }
1749
1753 rtl_uString * pData;
1754
1758 sal_Int32 nCapacity;
1759};
1760
1761#if defined LIBO_INTERNAL_ONLY
1762template<> struct ToStringHelper<OUStringBuffer> {
1763 static std::size_t length(OUStringBuffer const & s) { return s.getLength(); }
1764
1765 static sal_Unicode * addData(sal_Unicode * buffer, OUStringBuffer const & s) SAL_RETURNS_NONNULL
1766 { return addDataHelper(buffer, s.getStr(), s.getLength()); }
1767
1768 static constexpr bool allowOStringConcat = false;
1769 static constexpr bool allowOUStringConcat = true;
1770};
1771#endif
1772
1773#if defined LIBO_INTERNAL_ONLY
1774 // Define this here to avoid circular includes
1775 inline OUString & OUString::operator+=( const OUStringBuffer & str ) &
1776 {
1777 // Call operator= if this is empty, otherwise rtl_uString_newConcat will attempt to
1778 // acquire() the str.pData buffer, which is part of the OUStringBuffer mutable state.
1779 if (isEmpty())
1780 return operator=(str.toString());
1781 else
1782 return internalAppend(str.pData);
1783 }
1784#endif
1785}
1786
1787#ifdef RTL_STRING_UNITTEST
1788namespace rtl
1789{
1790typedef rtlunittest::OUStringBuffer OUStringBuffer;
1791}
1792#endif
1793
1794#if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
1795using ::rtl::OUStringBuffer;
1796#endif
1797
1798#endif // INCLUDED_RTL_USTRBUF_HXX
1799
1800/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define SAL_DEPRECATED(message)
Use as follows: SAL_DEPRECATED("Don't use, it's evil.") void doit(int nPara);.
Definition: types.h:474
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:378
@ SAL_NO_ACQUIRE
definition of a no acquire enum for ctors
Definition: types.h:356
unsigned char sal_Bool
Definition: types.h:38
sal_uInt16 sal_Unicode
Definition: types.h:123
#define SAL_WARN_UNUSED_RESULT
Use this as markup for functions and methods whose return value must be checked.
Definition: types.h:284
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:587
SAL_DLLPUBLIC sal_Int32 rtl_str_getLength(const char *str) SAL_THROW_EXTERN_C()
Return the length of a string.
SAL_DLLPUBLIC void rtl_uStringbuffer_insert_ascii(rtl_uString **This, sal_Int32 *capacity, sal_Int32 offset, const char *str, sal_Int32 len)
Inserts the 8-Bit ASCII string representation of the str array argument into this string buffer.
SAL_DLLPUBLIC void rtl_uStringbuffer_remove(rtl_uString **This, sal_Int32 start, sal_Int32 len)
Removes the characters in a substring of this sequence.
SAL_DLLPUBLIC rtl_uString * rtl_uStringBuffer_makeStringAndClear(rtl_uString **ppThis, sal_Int32 *nCapacity) SAL_RETURNS_NONNULL
Returns an immutable rtl_uString object, while clearing the string buffer.
SAL_DLLPUBLIC void rtl_uStringbuffer_insertUtf32(rtl_uString **pThis, sal_Int32 *capacity, sal_Int32 offset, sal_uInt32 c) SAL_THROW_EXTERN_C()
Inserts a single UTF-32 character into this string buffer.
SAL_DLLPUBLIC void rtl_uStringbuffer_newFromStr_WithLength(rtl_uString **newStr, const sal_Unicode *value, sal_Int32 count)
Allocates a new String that contains characters from the character array argument.
SAL_DLLPUBLIC sal_Int32 rtl_uStringbuffer_newFromStringBuffer(rtl_uString **newStr, sal_Int32 capacity, rtl_uString *oldStr)
Allocates a new String that contains the same sequence of characters as the string argument.
SAL_DLLPUBLIC void rtl_uStringbuffer_ensureCapacity(rtl_uString **This, sal_Int32 *capacity, sal_Int32 minimumCapacity)
Ensures that the capacity of the buffer is at least equal to the specified minimum.
SAL_DLLPUBLIC void rtl_uStringbuffer_insert(rtl_uString **This, sal_Int32 *capacity, sal_Int32 offset, const sal_Unicode *str, sal_Int32 len)
Inserts the string representation of the str array argument into this string buffer.
#define RTL_USTR_MAX_VALUEOFFLOAT
Definition: ustring.h:1026
#define RTL_USTR_MAX_VALUEOFDOUBLE
Definition: ustring.h:1045
SAL_DLLPUBLIC void rtl_uString_newFromLiteral(rtl_uString **newStr, const char *value, sal_Int32 len, sal_Int32 allocExtra) SAL_THROW_EXTERN_C()
SAL_DLLPUBLIC sal_Int32 rtl_ustr_valueOfBoolean(sal_Unicode *str, sal_Bool b) SAL_THROW_EXTERN_C()
Create the string representation of a boolean.
SAL_DLLPUBLIC sal_Int32 rtl_ustr_valueOfFloat(sal_Unicode *str, float f) SAL_THROW_EXTERN_C()
Create the string representation of a float.
SAL_DLLPUBLIC sal_Int32 rtl_ustr_valueOfDouble(sal_Unicode *str, double d) SAL_THROW_EXTERN_C()
Create the string representation of a double.
#define RTL_USTR_MAX_VALUEOFINT64
Definition: ustring.h:984
SAL_DLLPUBLIC sal_Int32 rtl_ustr_indexOfStr_WithLength(const sal_Unicode *str, sal_Int32 len, const sal_Unicode *subStr, sal_Int32 subLen) SAL_THROW_EXTERN_C()
Search for the first occurrence of a substring within a string.
SAL_DLLPUBLIC void rtl_uString_release(rtl_uString *str) SAL_THROW_EXTERN_C() SAL_HOT
Decrement the reference count of a string.
SAL_DLLPUBLIC sal_Int32 rtl_ustr_getLength(const sal_Unicode *str) SAL_THROW_EXTERN_C()
Return the length of a string.
SAL_DLLPUBLIC sal_Int32 rtl_ustr_valueOfInt64(sal_Unicode *str, sal_Int64 l, sal_Int16 radix) SAL_THROW_EXTERN_C()
Create the string representation of a long integer.
SAL_DLLPUBLIC sal_Int32 rtl_ustr_lastIndexOfAscii_WithLength(sal_Unicode const *str, sal_Int32 len, char const *subStr, sal_Int32 subLen) SAL_THROW_EXTERN_C()
Search for the last occurrence of an ASCII substring within a string.
SAL_DLLPUBLIC sal_Int32 rtl_ustr_indexOfAscii_WithLength(sal_Unicode const *str, sal_Int32 len, char const *subStr, sal_Int32 subLen) SAL_THROW_EXTERN_C()
Search for the first occurrence of an ASCII substring within a string.
SAL_DLLPUBLIC rtl_uString * rtl_uString_alloc(sal_Int32 nLen) SAL_THROW_EXTERN_C()
Allocate a new string containing space for a given number of characters.
SAL_DLLPUBLIC void rtl_uString_new_WithLength(rtl_uString **newStr, sal_Int32 nLen) SAL_THROW_EXTERN_C()
Allocate a new string containing space for a given number of characters.
SAL_DLLPUBLIC sal_Int32 rtl_ustr_lastIndexOfChar_WithLength(const sal_Unicode *str, sal_Int32 len, sal_Unicode ch) SAL_THROW_EXTERN_C()
Search for the last occurrence of a character within a string.
#define RTL_USTR_MAX_VALUEOFBOOLEAN
Definition: ustring.h:919
SAL_DLLPUBLIC sal_Int32 rtl_ustr_valueOfInt32(sal_Unicode *str, sal_Int32 i, sal_Int16 radix) SAL_THROW_EXTERN_C()
Create the string representation of an integer.
SAL_DLLPUBLIC sal_Int32 rtl_ustr_lastIndexOfStr_WithLength(const sal_Unicode *str, sal_Int32 len, const sal_Unicode *subStr, sal_Int32 subLen) SAL_THROW_EXTERN_C()
Search for the last occurrence of a substring within a string.
SAL_DLLPUBLIC sal_Int32 rtl_ustr_indexOfChar_WithLength(const sal_Unicode *str, sal_Int32 len, sal_Unicode ch) SAL_THROW_EXTERN_C()
Search for the first occurrence of a character within a string.
#define RTL_USTR_MAX_VALUEOFINT32
Definition: ustring.h:961
Definition: unotype.hxx:47
Definition: stringutils.hxx:136
Definition: stringutils.hxx:139
Definition: stringutils.hxx:371
A string buffer implements a mutable sequence of characters.
Definition: ustrbuf.hxx:70
sal_Int32 lastIndexOf(sal_Unicode ch, sal_Int32 fromIndex) const
Returns the index within this string of the last occurrence of the specified character,...
Definition: ustrbuf.hxx:1424
sal_Int32 stripStart(sal_Unicode c=' ')
Strip the given character from the start of the buffer.
Definition: ustrbuf.hxx:1608
libreoffice_internal::ConstCharArrayDetector< T, OUStringBuffer & >::Type insert(sal_Int32 offset, T &literal)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: ustrbuf.hxx:1054
OUStringBuffer & append(rtl_uString *str)
Definition: ustrbuf.hxx:808
OUStringBuffer & append(bool b)
Appends the string representation of the bool argument to the string buffer.
Definition: ustrbuf.hxx:787
OUStringBuffer copy(sal_Int32 beginIndex) const
Returns a new string buffer that is a substring of this string.
Definition: ustrbuf.hxx:1716
bool isEmpty() const
Checks if a string buffer is empty.
Definition: ustrbuf.hxx:426
OUStringBuffer copy(sal_Int32 beginIndex, sal_Int32 count) const
Returns a new string buffer that is a substring of this string.
Definition: ustrbuf.hxx:1734
OUStringBuffer & append(char c)
Appends the string representation of the ASCII char argument to this string buffer.
Definition: ustrbuf.hxx:842
sal_Int32 getCapacity() const
Returns the current capacity of the String buffer.
Definition: ustrbuf.hxx:441
sal_Int32 getLength() const
Returns the length (character count) of this string buffer.
Definition: ustrbuf.hxx:413
OUStringBuffer & insert(sal_Int32 offset, bool b)
Inserts the string representation of the bool argument into this string buffer.
Definition: ustrbuf.hxx:1126
OUStringBuffer & replace(sal_Unicode oldChar, sal_Unicode newChar)
Replace all occurrences of oldChar in this string buffer with newChar.
Definition: ustrbuf.hxx:1339
sal_Int32 strip(sal_Unicode c=' ')
Strip the given character from the both end of the buffer.
Definition: ustrbuf.hxx:1660
OUStringBuffer()
Constructs a string buffer with no characters in it and an initial capacity of 16 characters.
Definition: ustrbuf.hxx:77
sal_Int32 indexOf(const OUString &str, sal_Int32 fromIndex=0) const
Returns the index within this string of the first occurrence of the specified substring,...
Definition: ustrbuf.hxx:1456
OUStringBuffer & appendAscii(const char *str, sal_Int32 len)
Appends a 8-Bit ASCII character string to this string buffer.
Definition: ustrbuf.hxx:768
OUStringBuffer & append(sal_Bool b)
Appends the string representation of the sal_Bool argument to the string buffer.
Definition: ustrbuf.hxx:824
void setLength(sal_Int32 newLength)
Sets the length of this String buffer.
Definition: ustrbuf.hxx:480
OUStringBuffer(const OUStringBuffer &value)
Allocates a new string buffer that contains the same sequence of characters as the string buffer argu...
Definition: ustrbuf.hxx:90
OUStringBuffer(T &literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type=libreoffice_internal::Dummy())
Definition: ustrbuf.hxx:159
OUStringBuffer & append(double d)
Appends the string representation of the double argument to this string buffer.
Definition: ustrbuf.hxx:931
sal_Int32 indexOf(sal_Unicode ch, sal_Int32 fromIndex=0) const
Returns the index within this string of the first occurrence of the specified character,...
Definition: ustrbuf.hxx:1387
OUString toString() const
Return an OUString instance reflecting the current content of this OUStringBuffer.
Definition: ustrbuf.hxx:571
OUStringBuffer & append(sal_Int64 l, sal_Int16 radix=10)
Appends the string representation of the long argument to this string buffer.
Definition: ustrbuf.hxx:897
OUStringBuffer & append(sal_Unicode c)
Appends the string representation of the char argument to this string buffer.
Definition: ustrbuf.hxx:858
OUStringBuffer & append(const OUString &str)
Appends the string to this string buffer.
Definition: ustrbuf.hxx:586
OUStringBuffer & remove(sal_Int32 start, sal_Int32 len)
Removes the characters in a substring of this sequence.
Definition: ustrbuf.hxx:1307
OUStringBuffer & append(sal_Int32 i, sal_Int16 radix=10)
Appends the string representation of the sal_Int32 argument to this string buffer.
Definition: ustrbuf.hxx:879
sal_Unicode * appendUninitialized(sal_Int32 length) SAL_RETURNS_NONNULL
Unsafe way to make space for a fixed amount of characters to be appended into this OUStringBuffer.
Definition: ustrbuf.hxx:969
OUStringBuffer insert(sal_Int32 offset, float f)
Inserts the string representation of the float argument into this string buffer.
Definition: ustrbuf.hxx:1245
sal_Int32 lastIndexOf(const OUString &str, sal_Int32 fromIndex) const
Returns the index within this string of the last occurrence of the specified substring,...
Definition: ustrbuf.hxx:1557
sal_Int32 stripEnd(sal_Unicode c=' ')
Strip the given character from the end of the buffer.
Definition: ustrbuf.hxx:1634
OUStringBuffer & insert(sal_Int32 offset, sal_Unicode c)
Inserts the string representation of the char argument into this string buffer.
Definition: ustrbuf.hxx:1172
OUStringBuffer & insert(sal_Int32 offset, sal_Bool b)
Inserts the string representation of the sal_Bool argument into this string buffer.
Definition: ustrbuf.hxx:1101
libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf(T &literal, sal_Int32 fromIndex=0) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: ustrbuf.hxx:1471
OUStringBuffer & append(const OUStringBuffer &str)
Appends the content of a stringbuffer to this string buffer.
Definition: ustrbuf.hxx:612
OUStringBuffer & append(const sal_Unicode *str, sal_Int32 len)
Appends the string representation of the char array argument to this string buffer.
Definition: ustrbuf.hxx:656
OUStringBuffer & appendUtf32(sal_uInt32 c)
Appends a single UTF-32 character to this string buffer.
Definition: ustrbuf.hxx:950
OUStringBuffer & appendAscii(const char *str)
Appends a 8-Bit ASCII character string to this string buffer.
Definition: ustrbuf.hxx:745
sal_Int32 lastIndexOf(sal_Unicode ch) const
Returns the index within this string of the last occurrence of the specified character,...
Definition: ustrbuf.hxx:1405
OUStringBuffer(const OUString &value)
Constructs a string buffer so that it represents the same sequence of characters as the string argume...
Definition: ustrbuf.hxx:151
~OUStringBuffer()
Release the string data.
Definition: ustrbuf.hxx:388
libreoffice_internal::ConstCharArrayDetector< T, OUStringBuffer & >::Type append(T &literal)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: ustrbuf.hxx:669
OUStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix=10)
Inserts the string representation of the second sal_Int32 argument into this string buffer.
Definition: ustrbuf.hxx:1196
OUStringBuffer & insert(sal_Int32 offset, const sal_Unicode *str)
Inserts the string representation of the char array argument into this string buffer.
Definition: ustrbuf.hxx:1018
OUStringBuffer & truncate(sal_Int32 start=0)
Removes the tail of a string buffer start at the indicate position.
Definition: ustrbuf.hxx:1323
OUStringBuffer & append(float f)
Appends the string representation of the float argument to this string buffer.
Definition: ustrbuf.hxx:914
libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type lastIndexOf(T &literal) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: ustrbuf.hxx:1570
void ensureCapacity(sal_Int32 minimumCapacity)
Ensures that the capacity of the buffer is at least equal to the specified minimum.
Definition: ustrbuf.hxx:457
OUStringBuffer(int length)
Constructs a string buffer with no characters in it and an initial capacity specified by the length a...
Definition: ustrbuf.hxx:103
OUStringBuffer & insert(sal_Int32 offset, const OUString &str)
Inserts the string into this string buffer.
Definition: ustrbuf.hxx:996
OUStringBuffer & insert(sal_Int32 offset, const sal_Unicode *str, sal_Int32 len)
Inserts the string representation of the char array argument into this string buffer.
Definition: ustrbuf.hxx:1041
OUStringBuffer & insert(sal_Int32 offset, double d)
Inserts the string representation of the double argument into this string buffer.
Definition: ustrbuf.hxx:1269
OUStringBuffer & append(const sal_Unicode *str)
Appends the string representation of the char array argument to this string buffer.
Definition: ustrbuf.hxx:637
OUStringBuffer & insert(sal_Int32 offset, char c)
Inserts the string representation of the char argument into this string buffer.
Definition: ustrbuf.hxx:1150
const sal_Unicode * getStr() const SAL_RETURNS_NONNULL
Return a null terminated unicode character array.
Definition: ustrbuf.hxx:535
OUStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix=10)
Inserts the string representation of the long argument into this string buffer.
Definition: ustrbuf.hxx:1221
sal_Int32 lastIndexOf(const OUString &str) const
Returns the index within this string of the last occurrence of the specified substring,...
Definition: ustrbuf.hxx:1532
SAL_WARN_UNUSED_RESULT OUString makeStringAndClear()
Fill the string data in the new string and clear the buffer.
Definition: ustrbuf.hxx:401
OUStringBuffer & insertUtf32(sal_Int32 offset, sal_uInt32 c)
Inserts a single UTF-32 character into this string buffer.
Definition: ustrbuf.hxx:1290
void accessInternals(rtl_uString ***pInternalData, sal_Int32 **pInternalCapacity)
Allows access to the internal data of this OUStringBuffer, for effective manipulation.
Definition: ustrbuf.hxx:1364
This String class provides base functionality for C++ like Unicode character array handling.
Definition: ustring.hxx:169
const sal_Unicode * getStr() const SAL_RETURNS_NONNULL
Returns a pointer to the Unicode character buffer for this string.
Definition: ustring.hxx:781
sal_Int32 getLength() const
Returns the length of this string.
Definition: ustring.hxx:759
OUString & operator+=(const OUString &str)
Append a string to this string.
Definition: ustring.hxx:630