LibreOffice
LibreOffice 7.1 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#ifndef INCLUDED_RTL_USTRBUF_HXX
21#define INCLUDED_RTL_USTRBUF_HXX
22
23#include "sal/config.h"
24
25#include <cassert>
26#include <cstring>
27#include <limits>
28#include <new>
29
30#if defined LIBO_INTERNAL_ONLY
31#include <string_view>
32#endif
33
34#include "rtl/ustrbuf.h"
35#include "rtl/ustring.hxx"
36#include "rtl/stringutils.hxx"
37#include "sal/types.h"
38
39#ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
40#include "rtl/stringconcat.hxx"
41#endif
42
43#ifdef RTL_STRING_UNITTEST
44extern bool rtl_string_unittest_invalid_conversion;
45#endif
46
47// The unittest uses slightly different code to help check that the proper
48// calls are made. The class is put into a different namespace to make
49// sure the compiler generates a different (if generating also non-inline)
50// copy of the function and does not merge them together. The class
51// is "brought" into the proper rtl namespace by a typedef below.
52#ifdef RTL_STRING_UNITTEST
53#define rtl rtlunittest
54#endif
55
56namespace rtl
57{
58
59#ifdef RTL_STRING_UNITTEST
60#undef rtl
61#endif
62
66{
67friend class OUString;
68public:
74 : pData(NULL)
75 , nCapacity( 16 )
76 {
77 rtl_uString_new_WithLength( &pData, nCapacity );
78 }
79
87 : pData(NULL)
88 , nCapacity( value.nCapacity )
89 {
90 rtl_uStringbuffer_newFromStringBuffer( &pData, value.nCapacity, value.pData );
91 }
92
99 explicit OUStringBuffer(int length)
100 : pData(NULL)
101 , nCapacity( length )
102 {
103 rtl_uString_new_WithLength( &pData, length );
104 }
105#if __cplusplus >= 201103L
106 explicit OUStringBuffer(unsigned int length)
107 : OUStringBuffer(static_cast<int>(length))
108 {
109 }
110#if SAL_TYPES_SIZEOFLONG == 4
111 // additional overloads for sal_Int32 sal_uInt32
112 explicit OUStringBuffer(long length)
113 : OUStringBuffer(static_cast<int>(length))
114 {
115 }
116 explicit OUStringBuffer(unsigned long length)
117 : OUStringBuffer(static_cast<int>(length))
118 {
119 }
120#endif
121 // avoid obvious bugs
122 explicit OUStringBuffer(char) = delete;
123 explicit OUStringBuffer(sal_Unicode) = delete;
124#endif
125
137 : pData(NULL)
138 , nCapacity( value.getLength() + 16 )
139 {
140 rtl_uStringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
141 }
142
143 template< typename T >
145 : pData(NULL)
146 , nCapacity( libreoffice_internal::ConstCharArrayDetector<T>::length + 16 )
147 {
148 assert(
151 &pData,
154#ifdef RTL_STRING_UNITTEST
155 rtl_string_unittest_const_literal = true;
156#endif
157 }
158
159#if defined LIBO_INTERNAL_ONLY
161 template<typename T>
163 T & literal,
165 T, libreoffice_internal::Dummy>::TypeUtf16
167 pData(nullptr),
168 nCapacity(libreoffice_internal::ConstCharArrayDetector<T>::length + 16)
169 {
171 &pData,
174 }
175
177 template<std::size_t N> OUStringBuffer(OUStringLiteral<N> const & literal):
178 pData(nullptr), nCapacity(literal.getLength() + 16) //TODO: check for overflow
179 {
180 rtl_uStringbuffer_newFromStr_WithLength(&pData, literal.getStr(), literal.getLength());
181 }
182#endif
183
184#if defined LIBO_INTERNAL_ONLY && defined RTL_STRING_UNITTEST
186
190 template< typename T >
191 OUStringBuffer( T&, typename libreoffice_internal::ExceptConstCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
192 {
193 pData = NULL;
194 nCapacity = 10;
195 rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
196 rtl_string_unittest_invalid_conversion = true;
197 }
202 template< typename T >
203 OUStringBuffer( const T&, typename libreoffice_internal::ExceptCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
204 {
205 pData = NULL;
206 nCapacity = 10;
207 rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
208 rtl_string_unittest_invalid_conversion = true;
209 }
211#endif
212
213#ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
218 template< typename T1, typename T2 >
219 OUStringBuffer( OUStringConcat< T1, T2 >&& c )
220 {
221 const sal_Int32 l = c.length();
222 nCapacity = l + 16;
223 pData = rtl_uString_alloc( nCapacity );
224 sal_Unicode* end = c.addData( pData->buffer );
225 *end = '\0';
226 pData->length = l;
227 // TODO realloc in case pData->>length is noticeably smaller than l ?
228 }
229
234 template< typename T >
235 OUStringBuffer( OUStringNumber< T >&& n )
236 : pData(NULL)
237 , nCapacity( n.length + 16 )
238 {
239 rtl_uStringbuffer_newFromStr_WithLength( &pData, n.buf, n.length );
240 }
241#endif
244 OUStringBuffer& operator = ( const OUStringBuffer& value )
245 {
246 if (this != &value)
247 {
249 value.nCapacity,
250 value.pData);
251 nCapacity = value.nCapacity;
252 }
253 return *this;
254 }
255
260 OUStringBuffer & operator =(OUString const & string) {
261 sal_Int32 n = string.getLength();
262 if (n >= nCapacity) {
263 ensureCapacity(n + 16); //TODO: check for overflow
264 }
265 std::memcpy(
266 pData->buffer, string.pData->buffer,
267 (n + 1) * sizeof (sal_Unicode));
268 pData->length = n;
269 return *this;
270 }
271
276 template<typename T>
277 typename
279 operator =(T & literal) {
280 assert(
282 sal_Int32 const n
284 if (n >= nCapacity) {
285 ensureCapacity(n + 16); //TODO: check for overflow
286 }
287 char const * from
289 literal);
290 sal_Unicode * to = pData->buffer;
291 for (sal_Int32 i = 0; i <= n; ++i) {
292 to[i] = from[i];
293 }
294 pData->length = n;
295 return *this;
296 }
297
298#if defined LIBO_INTERNAL_ONLY
300 template<typename T>
302 T, OUStringBuffer &>::TypeUtf16
303 operator =(T & literal) {
304 sal_Int32 const n
306 if (n >= nCapacity) {
307 ensureCapacity(n + 16); //TODO: check for overflow
308 }
309 std::memcpy(
310 pData->buffer,
311 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
312 (n + 1) * sizeof (sal_Unicode)); //TODO: check for overflow
313 pData->length = n;
314 return *this;
315 }
316
318 template<std::size_t N> OUStringBuffer & operator =(OUStringLiteral<N> const & literal) {
319 sal_Int32 const n = literal.getLength();
320 if (n >= nCapacity) {
321 ensureCapacity(n + 16); //TODO: check for overflow
322 }
323 std::memcpy(
324 pData->buffer, literal.getStr(),
325 (n + 1) * sizeof (sal_Unicode)); //TODO: check for overflow
326 pData->length = n;
327 return *this;
328 }
329#endif
330
331#if defined LIBO_INTERNAL_ONLY
333 template<typename T1, typename T2>
334 OUStringBuffer & operator =(OUStringConcat<T1, T2> && concat) {
335 sal_Int32 const n = concat.length();
336 if (n >= nCapacity) {
337 ensureCapacity(n + 16); //TODO: check for overflow
338 }
339 *concat.addData(pData->buffer) = 0;
340 pData->length = n;
341 return *this;
342 }
343
345 template<typename T>
346 OUStringBuffer & operator =(OUStringNumber<T> && n)
347 {
348 *this = OUStringBuffer( std::move( n ) );
349 return *this;
350 }
351#endif
352
357 {
358 rtl_uString_release( pData );
359 }
360
370 {
371 return OUString(
372 rtl_uStringBuffer_makeStringAndClear( &pData, &nCapacity ),
374 }
375
381 sal_Int32 getLength() const
382 {
383 return pData->length;
384 }
385
394 bool isEmpty() const
395 {
396 return pData->length == 0;
397 }
398
409 sal_Int32 getCapacity() const
410 {
411 return nCapacity;
412 }
413
425 void ensureCapacity(sal_Int32 minimumCapacity)
426 {
427 rtl_uStringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
428 }
429
448 void setLength(sal_Int32 newLength)
449 {
450 assert(newLength >= 0);
451 // Avoid modifications if pData points to const empty string:
452 if( newLength != pData->length )
453 {
454 if( newLength > nCapacity )
455 rtl_uStringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
456 else
457 pData->buffer[newLength] = 0;
458 pData->length = newLength;
459 }
460 }
461
475 SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
476 sal_Unicode charAt( sal_Int32 index ) const
477 {
478 assert(index >= 0 && index < pData->length);
479 return pData->buffer[ index ];
480 }
481
492 SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
493 OUStringBuffer & setCharAt(sal_Int32 index, sal_Unicode ch)
494 {
495 assert(index >= 0 && index < pData->length);
496 pData->buffer[ index ] = ch;
497 return *this;
498 }
499
503 const sal_Unicode* getStr() const SAL_RETURNS_NONNULL { return pData->buffer; }
504
514 sal_Unicode & operator [](sal_Int32 index)
515 {
516 assert(index >= 0 && index < pData->length);
517 return pData->buffer[index];
518 }
519
529 const sal_Unicode & operator [](sal_Int32 index) const
530 {
531 assert(index >= 0 && index < pData->length);
532 return pData->buffer[index];
533 }
534
539 const OUString toString() const
540 {
541 return OUString(pData->buffer, pData->length);
542 }
543
555 {
556 return append( str.getStr(), str.getLength() );
557 }
558
559#if defined LIBO_INTERNAL_ONLY
560 OUStringBuffer & append(std::u16string_view sv) {
561 if (sv.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max())) {
562 throw std::bad_alloc();
563 }
564 return append(sv.data(), sv.size());
565 }
566#endif
567
581 {
582 if(!str.isEmpty())
583 {
584 append( str.getStr(), str.getLength() );
585 }
586 return *this;
587 }
588
600#if defined LIBO_INTERNAL_ONLY
601 template<typename T>
603 append(T const & str)
604#else
606#endif
607 {
608 return append( str, rtl_ustr_getLength( str ) );
609 }
610
624 OUStringBuffer & append( const sal_Unicode * str, sal_Int32 len)
625 {
626 assert( len == 0 || str != NULL ); // cannot assert that in rtl_uStringbuffer_insert
627 rtl_uStringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
628 return *this;
629 }
630
636 template< typename T >
638 {
639 assert(
641 return appendAscii(
644 }
645
646#if defined LIBO_INTERNAL_ONLY
647 template<typename T>
649 append(T & value) { return append(static_cast<sal_Unicode *>(value)); }
650
652 template<typename T>
653 typename libreoffice_internal::ConstCharArrayDetector<
654 T, OUStringBuffer &>::TypeUtf16
655 append(T & literal) {
656 return append(
657 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
658 libreoffice_internal::ConstCharArrayDetector<T>::length);
659 }
660
662 template<std::size_t N> OUStringBuffer & append(OUStringLiteral<N> const & literal) {
663 return append(literal.getStr(), literal.getLength());
664 }
665#endif
666
667#ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
672 template< typename T1, typename T2 >
673 OUStringBuffer& append( OUStringConcat< T1, T2 >&& c )
674 {
675 sal_Int32 l = c.length();
676 if( l == 0 )
677 return *this;
678 l += pData->length;
679 rtl_uStringbuffer_ensureCapacity( &pData, &nCapacity, l );
680 sal_Unicode* end = c.addData( pData->buffer + pData->length );
681 *end = '\0';
682 pData->length = l;
683 return *this;
684 }
685
690 template< typename T >
691 OUStringBuffer& append( OUStringNumber< T >&& c )
692 {
693 return append( c.buf, c.length );
694 }
695#endif
696
713 OUStringBuffer & appendAscii( const char * str )
714 {
715 return appendAscii( str, rtl_str_getLength( str ) );
716 }
717
736 OUStringBuffer & appendAscii( const char * str, sal_Int32 len)
737 {
738 rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), str, len );
739 return *this;
740 }
741
756 {
758 return append( sz, rtl_ustr_valueOfBoolean( sz, b ) );
759 }
760
762 // Pointer can be automatically converted to bool, which is unwanted here.
763 // Explicitly delete all pointer append() overloads to prevent this
764 // (except for char* and sal_Unicode* overloads, which are handled elsewhere).
765 template< typename T >
766 typename libreoffice_internal::Enable< void,
768 append( T* ) SAL_DELETED_FUNCTION;
770
771 // This overload is needed because OUString has a ctor from rtl_uString*, but
772 // the bool overload above would be preferred to the conversion.
776 OUStringBuffer & append(rtl_uString* str)
777 {
778 return append( OUString( str ));
779 }
780
793 {
795 return append( sz, rtl_ustr_valueOfBoolean( sz, b ) );
796 }
797
811 {
812 assert(static_cast< unsigned char >(c) <= 0x7F);
813 return append(sal_Unicode(c));
814 }
815
827 {
828 return append( &c, 1 );
829 }
830
831#if defined LIBO_INTERNAL_ONLY
832 void append(sal_uInt16) = delete;
833#endif
834
847 OUStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
848 {
850 return append( sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
851 }
852
865 OUStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
866 {
868 return append( sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
869 }
870
883 {
885 return append( sz, rtl_ustr_valueOfFloat( sz, f ) );
886 }
887
900 {
902 return append( sz, rtl_ustr_valueOfDouble( sz, d ) );
903 }
904
918 OUStringBuffer & appendUtf32(sal_uInt32 c) {
919 return insertUtf32(getLength(), c);
920 }
921
937 sal_Unicode * appendUninitialized(sal_Int32 length) SAL_RETURNS_NONNULL {
938 sal_Int32 n = getLength();
939 rtl_uStringbuffer_insert(&pData, &nCapacity, n, NULL, length);
940 return pData->buffer + n;
941 }
942
958 OUStringBuffer & insert(sal_Int32 offset, const OUString & str)
959 {
960 return insert( offset, str.getStr(), str.getLength() );
961 }
962
980 OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str )
981 {
982 return insert( offset, str, rtl_ustr_getLength( str ) );
983 }
984
1003 OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str, sal_Int32 len)
1004 {
1005 assert( len == 0 || str != NULL ); // cannot assert that in rtl_uStringbuffer_insert
1006 rtl_uStringbuffer_insert( &pData, &nCapacity, offset, str, len );
1007 return *this;
1008 }
1009
1015 template< typename T >
1017 {
1018 assert(
1021 &pData, &nCapacity, offset,
1024 return *this;
1025 }
1026
1027#if defined LIBO_INTERNAL_ONLY
1029 template<typename T>
1031 T, OUStringBuffer &>::TypeUtf16
1032 insert(sal_Int32 offset, T & literal) {
1033 return insert(
1034 offset,
1037 }
1038
1040 template<std::size_t N>
1041 OUStringBuffer & insert(sal_Int32 offset, OUStringLiteral<N> const & literal) {
1042 return insert(offset, literal.getStr(), literal.getLength());
1043 }
1044#endif
1045
1063 OUStringBuffer & insert(sal_Int32 offset, sal_Bool b)
1064 {
1066 return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
1067 }
1068
1088 OUStringBuffer & insert(sal_Int32 offset, bool b)
1089 {
1091 return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
1092 }
1093
1112 OUStringBuffer & insert(sal_Int32 offset, char c)
1113 {
1114 sal_Unicode u = c;
1115 return insert( offset, &u, 1 );
1116 }
1117
1134 OUStringBuffer & insert(sal_Int32 offset, sal_Unicode c)
1135 {
1136 return insert( offset, &c, 1 );
1137 }
1138
1158 OUStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
1159 {
1161 return insert( offset, sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
1162 }
1163
1183 OUStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
1184 {
1186 return insert( offset, sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
1187 }
1188
1207 OUStringBuffer insert(sal_Int32 offset, float f)
1208 {
1210 return insert( offset, sz, rtl_ustr_valueOfFloat( sz, f ) );
1211 }
1212
1231 OUStringBuffer & insert(sal_Int32 offset, double d)
1232 {
1234 return insert( offset, sz, rtl_ustr_valueOfDouble( sz, d ) );
1235 }
1236
1252 OUStringBuffer & insertUtf32(sal_Int32 offset, sal_uInt32 c) {
1253 rtl_uStringbuffer_insertUtf32(&pData, &nCapacity, offset, c);
1254 return *this;
1255 }
1256
1269 OUStringBuffer & remove( sal_Int32 start, sal_Int32 len )
1270 {
1271 rtl_uStringbuffer_remove( &pData, start, len );
1272 return *this;
1273 }
1274
1285 OUStringBuffer & truncate( sal_Int32 start = 0 )
1286 {
1287 rtl_uStringbuffer_remove( &pData, start, getLength() - start );
1288 return *this;
1289 }
1290
1302 {
1303 sal_Int32 index = 0;
1304 while((index = indexOf(oldChar, index)) >= 0)
1305 {
1306 pData->buffer[ index ] = newChar;
1307 }
1308 return *this;
1309 }
1310
1326 void accessInternals(rtl_uString *** pInternalData,
1327 sal_Int32 ** pInternalCapacity)
1328 {
1329 *pInternalData = &pData;
1330 *pInternalCapacity = &nCapacity;
1331 }
1332
1333
1349 sal_Int32 indexOf( sal_Unicode ch, sal_Int32 fromIndex = 0 ) const
1350 {
1351 assert( fromIndex >= 0 && fromIndex <= pData->length );
1352 sal_Int32 ret = rtl_ustr_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch );
1353 return (ret < 0 ? ret : ret+fromIndex);
1354 }
1355
1367 sal_Int32 lastIndexOf( sal_Unicode ch ) const
1368 {
1369 return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch );
1370 }
1371
1386 sal_Int32 lastIndexOf( sal_Unicode ch, sal_Int32 fromIndex ) const
1387 {
1388 assert( fromIndex >= 0 && fromIndex <= pData->length );
1389 return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch );
1390 }
1391
1409 sal_Int32 indexOf( const OUString & str, sal_Int32 fromIndex = 0 ) const
1410 {
1411 assert( fromIndex >= 0 && fromIndex <= pData->length );
1412 sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1413 str.pData->buffer, str.pData->length );
1414 return (ret < 0 ? ret : ret+fromIndex);
1415 }
1416
1423 template< typename T >
1424 typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const
1425 {
1426 assert(
1429 pData->buffer + fromIndex, pData->length - fromIndex,
1432 return n < 0 ? n : n + fromIndex;
1433 }
1434
1435#if defined LIBO_INTERNAL_ONLY
1437 template<typename T>
1438 typename
1440 indexOf(T & literal, sal_Int32 fromIndex = 0) const {
1441 assert(fromIndex >= 0);
1443 pData->buffer + fromIndex, pData->length - fromIndex,
1446 return n < 0 ? n : n + fromIndex;
1447 }
1448
1450 template<std::size_t N>
1451 sal_Int32 indexOf(OUStringLiteral<N> const & literal, sal_Int32 fromIndex = 0)
1452 const
1453 {
1454 sal_Int32 n = rtl_ustr_indexOfStr_WithLength(
1455 pData->buffer + fromIndex, pData->length - fromIndex, literal.getStr(),
1456 literal.getLength());
1457 return n < 0 ? n : n + fromIndex;
1458 }
1459#endif
1460
1478 sal_Int32 lastIndexOf( const OUString & str ) const
1479 {
1480 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1481 str.pData->buffer, str.pData->length );
1482 }
1483
1503 sal_Int32 lastIndexOf( const OUString & str, sal_Int32 fromIndex ) const
1504 {
1505 assert( fromIndex >= 0 && fromIndex <= pData->length );
1506 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1507 str.pData->buffer, str.pData->length );
1508 }
1509
1515 template< typename T >
1517 {
1518 assert(
1521 pData->buffer, pData->length,
1524 }
1525
1526#if defined LIBO_INTERNAL_ONLY
1528 template<typename T>
1529 typename
1531 lastIndexOf(T & literal) const {
1533 pData->buffer, pData->length,
1536 }
1537
1539 template<std::size_t N> sal_Int32 lastIndexOf(OUStringLiteral<N> const & literal) const {
1541 pData->buffer, pData->length, literal.getStr(), literal.getLength());
1542 }
1543#endif
1544
1554 sal_Int32 stripStart(sal_Unicode c = ' ')
1555 {
1556 sal_Int32 index;
1557 for(index = 0; index < getLength() ; index++)
1558 {
1559 if(pData->buffer[ index ] != c)
1560 {
1561 break;
1562 }
1563 }
1564 if(index)
1565 {
1566 remove(0, index);
1567 }
1568 return index;
1569 }
1570
1580 sal_Int32 stripEnd(sal_Unicode c = ' ')
1581 {
1582 sal_Int32 result = getLength();
1583 sal_Int32 index;
1584 for(index = getLength(); index > 0 ; index--)
1585 {
1586 if(pData->buffer[ index - 1 ] != c)
1587 {
1588 break;
1589 }
1590 }
1591 if(index < getLength())
1592 {
1593 truncate(index);
1594 }
1595 return result - getLength();
1596 }
1606 sal_Int32 strip(sal_Unicode c = ' ')
1607 {
1608 return stripStart(c) + stripEnd(c);
1609 }
1621 OUStringBuffer copy( sal_Int32 beginIndex ) const
1622 {
1623 return copy( beginIndex, getLength() - beginIndex );
1624 }
1625
1639 OUStringBuffer copy( sal_Int32 beginIndex, sal_Int32 count ) const
1640 {
1641 assert(beginIndex >= 0 && beginIndex <= getLength());
1642 assert(count >= 0 && count <= getLength() - beginIndex);
1643 rtl_uString *pNew = NULL;
1644 rtl_uStringbuffer_newFromStr_WithLength( &pNew, getStr() + beginIndex, count );
1645 return OUStringBuffer( pNew, count + 16 );
1646 }
1647
1648private:
1649 OUStringBuffer( rtl_uString * value, const sal_Int32 capacity )
1650 {
1651 pData = value;
1652 nCapacity = capacity;
1653 }
1654
1658 rtl_uString * pData;
1659
1663 sal_Int32 nCapacity;
1664};
1665
1666#if defined LIBO_INTERNAL_ONLY
1667template<> struct ToStringHelper<OUStringBuffer> {
1668 static std::size_t length(OUStringBuffer const & s) { return s.getLength(); }
1669
1670 static sal_Unicode * addData(sal_Unicode * buffer, OUStringBuffer const & s) SAL_RETURNS_NONNULL
1671 { return addDataHelper(buffer, s.getStr(), s.getLength()); }
1672
1673 static constexpr bool allowOStringConcat = false;
1674 static constexpr bool allowOUStringConcat = true;
1675};
1676#endif
1677
1678#if defined LIBO_INTERNAL_ONLY
1679 // Define this here to avoid circular includes
1680 inline OUString & OUString::operator+=( const OUStringBuffer & str ) &
1681 {
1682 // Call operator= if this is empty, otherwise rtl_uString_newConcat will attempt to
1683 // acquire() the str.pData buffer, which is part of the OUStringBuffer mutable state.
1684 if (isEmpty())
1685 return operator=(str.toString());
1686 else
1687 return internalAppend(str.pData);
1688 }
1689#endif
1690}
1691
1692#ifdef RTL_STRING_UNITTEST
1693namespace rtl
1694{
1695typedef rtlunittest::OUStringBuffer OUStringBuffer;
1696}
1697#endif
1698
1699#if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
1700using ::rtl::OUStringBuffer;
1701#endif
1702
1703#endif // INCLUDED_RTL_USTRBUF_HXX
1704
1705/* 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:445
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:374
@ SAL_NO_ACQUIRE
definition of a no acquire enum for ctors
Definition: types.h:352
unsigned char sal_Bool
Definition: types.h:34
sal_uInt16 sal_Unicode
Definition: types.h:119
#define SAL_WARN_UNUSED_RESULT
Use this as markup for functions and methods whose return value must be checked.
Definition: types.h:280
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:558
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:1022
#define RTL_USTR_MAX_VALUEOFDOUBLE
Definition: ustring.h:1041
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:980
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:915
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:957
Definition: bootstrap.hxx:30
Definition: stringutils.hxx:131
Definition: stringutils.hxx:134
Definition: stringutils.hxx:366
A string buffer implements a mutable sequence of characters.
Definition: ustrbuf.hxx:66
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:1386
sal_Int32 stripStart(sal_Unicode c=' ')
Strip the given character from the start of the buffer.
Definition: ustrbuf.hxx:1554
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:1016
OUStringBuffer & append(rtl_uString *str)
Definition: ustrbuf.hxx:776
OUStringBuffer & append(bool b)
Appends the string representation of the bool argument to the string buffer.
Definition: ustrbuf.hxx:755
OUStringBuffer copy(sal_Int32 beginIndex) const
Returns a new string buffer that is a substring of this string.
Definition: ustrbuf.hxx:1621
bool isEmpty() const
Checks if a string buffer is empty.
Definition: ustrbuf.hxx:394
OUStringBuffer copy(sal_Int32 beginIndex, sal_Int32 count) const
Returns a new string buffer that is a substring of this string.
Definition: ustrbuf.hxx:1639
OUStringBuffer & append(char c)
Appends the string representation of the ASCII char argument to this string buffer.
Definition: ustrbuf.hxx:810
sal_Int32 getCapacity() const
Returns the current capacity of the String buffer.
Definition: ustrbuf.hxx:409
sal_Int32 getLength() const
Returns the length (character count) of this string buffer.
Definition: ustrbuf.hxx:381
OUStringBuffer & insert(sal_Int32 offset, bool b)
Inserts the string representation of the bool argument into this string buffer.
Definition: ustrbuf.hxx:1088
OUStringBuffer & replace(sal_Unicode oldChar, sal_Unicode newChar)
Replace all occurrences of oldChar in this string buffer with newChar.
Definition: ustrbuf.hxx:1301
sal_Int32 strip(sal_Unicode c=' ')
Strip the given character from the both end of the buffer.
Definition: ustrbuf.hxx:1606
OUStringBuffer()
Constructs a string buffer with no characters in it and an initial capacity of 16 characters.
Definition: ustrbuf.hxx:73
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:1409
OUStringBuffer & appendAscii(const char *str, sal_Int32 len)
Appends a 8-Bit ASCII character string to this string buffer.
Definition: ustrbuf.hxx:736
OUStringBuffer & append(sal_Bool b)
Appends the string representation of the sal_Bool argument to the string buffer.
Definition: ustrbuf.hxx:792
void setLength(sal_Int32 newLength)
Sets the length of this String buffer.
Definition: ustrbuf.hxx:448
OUStringBuffer(const OUStringBuffer &value)
Allocates a new string buffer that contains the same sequence of characters as the string buffer argu...
Definition: ustrbuf.hxx:86
OUStringBuffer(T &literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type=libreoffice_internal::Dummy())
Definition: ustrbuf.hxx:144
OUStringBuffer & append(double d)
Appends the string representation of the double argument to this string buffer.
Definition: ustrbuf.hxx:899
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:1349
OUStringBuffer & append(sal_Int64 l, sal_Int16 radix=10)
Appends the string representation of the long argument to this string buffer.
Definition: ustrbuf.hxx:865
OUStringBuffer & append(sal_Unicode c)
Appends the string representation of the char argument to this string buffer.
Definition: ustrbuf.hxx:826
OUStringBuffer & append(const OUString &str)
Appends the string to this string buffer.
Definition: ustrbuf.hxx:554
OUStringBuffer & remove(sal_Int32 start, sal_Int32 len)
Removes the characters in a substring of this sequence.
Definition: ustrbuf.hxx:1269
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:847
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:937
OUStringBuffer insert(sal_Int32 offset, float f)
Inserts the string representation of the float argument into this string buffer.
Definition: ustrbuf.hxx:1207
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:1503
sal_Int32 stripEnd(sal_Unicode c=' ')
Strip the given character from the end of the buffer.
Definition: ustrbuf.hxx:1580
OUStringBuffer & insert(sal_Int32 offset, sal_Unicode c)
Inserts the string representation of the char argument into this string buffer.
Definition: ustrbuf.hxx:1134
OUStringBuffer & insert(sal_Int32 offset, sal_Bool b)
Inserts the string representation of the sal_Bool argument into this string buffer.
Definition: ustrbuf.hxx:1063
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:1424
OUStringBuffer & append(const OUStringBuffer &str)
Appends the content of a stringbuffer to this string buffer.
Definition: ustrbuf.hxx:580
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:624
OUStringBuffer & appendUtf32(sal_uInt32 c)
Appends a single UTF-32 character to this string buffer.
Definition: ustrbuf.hxx:918
OUStringBuffer & appendAscii(const char *str)
Appends a 8-Bit ASCII character string to this string buffer.
Definition: ustrbuf.hxx:713
sal_Int32 lastIndexOf(sal_Unicode ch) const
Returns the index within this string of the last occurrence of the specified character,...
Definition: ustrbuf.hxx:1367
OUStringBuffer(const OUString &value)
Constructs a string buffer so that it represents the same sequence of characters as the string argume...
Definition: ustrbuf.hxx:136
~OUStringBuffer()
Release the string data.
Definition: ustrbuf.hxx:356
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:637
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:1158
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:980
OUStringBuffer & truncate(sal_Int32 start=0)
Removes the tail of a string buffer start at the indicate position.
Definition: ustrbuf.hxx:1285
OUStringBuffer & append(float f)
Appends the string representation of the float argument to this string buffer.
Definition: ustrbuf.hxx:882
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:1516
void ensureCapacity(sal_Int32 minimumCapacity)
Ensures that the capacity of the buffer is at least equal to the specified minimum.
Definition: ustrbuf.hxx:425
OUStringBuffer(int length)
Constructs a string buffer with no characters in it and an initial capacity specified by the length a...
Definition: ustrbuf.hxx:99
const OUString toString() const
Return an OUString instance reflecting the current content of this OUStringBuffer.
Definition: ustrbuf.hxx:539
OUStringBuffer & insert(sal_Int32 offset, const OUString &str)
Inserts the string into this string buffer.
Definition: ustrbuf.hxx:958
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:1003
OUStringBuffer & insert(sal_Int32 offset, double d)
Inserts the string representation of the double argument into this string buffer.
Definition: ustrbuf.hxx:1231
OUStringBuffer & append(const sal_Unicode *str)
Appends the string representation of the char array argument to this string buffer.
Definition: ustrbuf.hxx:605
OUStringBuffer & insert(sal_Int32 offset, char c)
Inserts the string representation of the char argument into this string buffer.
Definition: ustrbuf.hxx:1112
const sal_Unicode * getStr() const SAL_RETURNS_NONNULL
Return a null terminated unicode character array.
Definition: ustrbuf.hxx:503
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:1183
sal_Int32 lastIndexOf(const OUString &str) const
Returns the index within this string of the last occurrence of the specified substring,...
Definition: ustrbuf.hxx:1478
SAL_WARN_UNUSED_RESULT OUString makeStringAndClear()
Fill the string data in the new string and clear the buffer.
Definition: ustrbuf.hxx:369
OUStringBuffer & insertUtf32(sal_Int32 offset, sal_uInt32 c)
Inserts a single UTF-32 character into this string buffer.
Definition: ustrbuf.hxx:1252
void accessInternals(rtl_uString ***pInternalData, sal_Int32 **pInternalCapacity)
Allows access to the internal data of this OUStringBuffer, for effective manipulation.
Definition: ustrbuf.hxx:1326
This String class provides base functionality for C++ like Unicode character array handling.
Definition: ustring.hxx:161
const sal_Unicode * getStr() const SAL_RETURNS_NONNULL
Returns a pointer to the Unicode character buffer for this string.
Definition: ustring.hxx:774
sal_Int32 getLength() const
Returns the length of this string.
Definition: ustring.hxx:752
OUString & operator+=(const OUString &str)
Append a string to this string.
Definition: ustring.hxx:623