LibreOffice
LibreOffice 7.3 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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
48 extern 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 
60 namespace rtl
61 {
62 
63 #ifdef RTL_STRING_UNITTEST
64 #undef rtl
65 #endif
66 
70 {
71 friend class OUString;
72 public:
78  : pData(NULL)
79  , nCapacity( 16 )
80  {
81  rtl_uString_new_WithLength( &pData, nCapacity );
82  }
83 
90  OUStringBuffer( const OUStringBuffer & value )
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  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 #else
151  OUStringBuffer(const OUString& value)
152  : pData(NULL)
153  , nCapacity( value.getLength() + 16 )
154  {
155  rtl_uStringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
156  }
157 #endif
158 
159  template< typename T >
161  : pData(NULL)
162  , nCapacity( libreoffice_internal::ConstCharArrayDetector<T>::length + 16 )
163  {
164  assert(
167  &pData,
170 #ifdef RTL_STRING_UNITTEST
171  rtl_string_unittest_const_literal = true;
172 #endif
173  }
174 
175 #if defined LIBO_INTERNAL_ONLY
176 
177  template<typename T>
179  T & literal,
181  T, libreoffice_internal::Dummy>::TypeUtf16
183  pData(nullptr),
184  nCapacity(libreoffice_internal::ConstCharArrayDetector<T>::length + 16)
185  {
187  &pData,
190  }
191 #endif
192 
193 #if defined LIBO_INTERNAL_ONLY && defined RTL_STRING_UNITTEST
194 
199  template< typename T >
200  OUStringBuffer( T&, typename libreoffice_internal::ExceptConstCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
201  {
202  pData = NULL;
203  nCapacity = 10;
204  rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
205  rtl_string_unittest_invalid_conversion = true;
206  }
211  template< typename T >
212  OUStringBuffer( const T&, typename libreoffice_internal::ExceptCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
213  {
214  pData = NULL;
215  nCapacity = 10;
216  rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
217  rtl_string_unittest_invalid_conversion = true;
218  }
220 #endif
221 
222 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
223 
227  template< typename T1, typename T2 >
228  OUStringBuffer( OUStringConcat< T1, T2 >&& c )
229  {
230  const sal_Int32 l = c.length();
231  nCapacity = l + 16;
232  pData = rtl_uString_alloc( nCapacity );
233  sal_Unicode* end = c.addData( pData->buffer );
234  *end = '\0';
235  pData->length = l;
236  }
237 
242  template< typename T >
243  OUStringBuffer( OUStringNumber< T >&& n )
244  : pData(NULL)
245  , nCapacity( n.length + 16 )
246  {
247  rtl_uStringbuffer_newFromStr_WithLength( &pData, n.buf, n.length );
248  }
249 #endif
250 
251 #if defined LIBO_INTERNAL_ONLY
252  operator std::u16string_view() const { return {getStr(), sal_uInt32(getLength())}; }
253 #endif
254 
257  OUStringBuffer& operator = ( const OUStringBuffer& value )
258  {
259  if (this != &value)
260  {
262  value.nCapacity,
263  value.pData);
264  nCapacity = value.nCapacity;
265  }
266  return *this;
267  }
268 
269 #if defined LIBO_INTERNAL_ONLY
270 
273  OUStringBuffer& operator = ( OUStringBuffer&& value ) noexcept
274  {
275  rtl_uString_release( pData );
276  pData = value.pData;
277  nCapacity = value.nCapacity;
278  value.pData = nullptr;
279  value.nCapacity = 0;
280  rtl_uString_new( &value.pData );
281  return *this;
282  }
283 #endif
284 
289 #if defined LIBO_INTERNAL_ONLY
290  OUStringBuffer & operator =(std::u16string_view string) {
291  sal_Int32 n = string.length();
292  if (n >= nCapacity) {
293  ensureCapacity(n + 16); //TODO: check for overflow
294  }
295  std::memcpy(
296  pData->buffer, string.data(),
297  (n + 1) * sizeof (sal_Unicode));
298  pData->length = n;
299  return *this;
300  }
301 #else
302  OUStringBuffer & operator =(OUString const & string) {
303  sal_Int32 n = string.getLength();
304  if (n >= nCapacity) {
305  ensureCapacity(n + 16); //TODO: check for overflow
306  }
307  std::memcpy(
308  pData->buffer, string.pData->buffer,
309  (n + 1) * sizeof (sal_Unicode));
310  pData->length = n;
311  return *this;
312  }
313 #endif
314 
319  template<typename T>
320  typename
322  operator =(T & literal) {
323  assert(
325  sal_Int32 const n
327  if (n >= nCapacity) {
328  ensureCapacity(n + 16); //TODO: check for overflow
329  }
330  char const * from
332  literal);
333  sal_Unicode * to = pData->buffer;
334  for (sal_Int32 i = 0; i <= n; ++i) {
335  to[i] = from[i];
336  }
337  pData->length = n;
338  return *this;
339  }
340 
341 #if defined LIBO_INTERNAL_ONLY
342 
343  template<typename T>
345  T, OUStringBuffer &>::TypeUtf16
346  operator =(T & literal) {
347  sal_Int32 const n
349  if (n >= nCapacity) {
350  ensureCapacity(n + 16); //TODO: check for overflow
351  }
352  std::memcpy(
353  pData->buffer,
354  libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
355  (n + 1) * sizeof (sal_Unicode)); //TODO: check for overflow
356  pData->length = n;
357  return *this;
358  }
359 #endif
360 
361 #if defined LIBO_INTERNAL_ONLY
362 
363  template<typename T1, typename T2>
364  OUStringBuffer & operator =(OUStringConcat<T1, T2> && concat) {
365  sal_Int32 const n = concat.length();
366  if (n >= nCapacity) {
367  ensureCapacity(n + 16); //TODO: check for overflow
368  }
369  *concat.addData(pData->buffer) = 0;
370  pData->length = n;
371  return *this;
372  }
373 
375  template<typename T>
376  OUStringBuffer & operator =(OUStringNumber<T> && n)
377  {
378  *this = OUStringBuffer( std::move( n ) );
379  return *this;
380  }
381 #endif
382 
387  {
388  rtl_uString_release( pData );
389  }
390 
400  {
401  return OUString(
402  rtl_uStringBuffer_makeStringAndClear( &pData, &nCapacity ),
403  SAL_NO_ACQUIRE );
404  }
405 
411  sal_Int32 getLength() const
412  {
413  return pData->length;
414  }
415 
424  bool isEmpty() const
425  {
426  return pData->length == 0;
427  }
428 
439  sal_Int32 getCapacity() const
440  {
441  return nCapacity;
442  }
443 
455  void ensureCapacity(sal_Int32 minimumCapacity)
456  {
457  rtl_uStringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
458  }
459 
478  void setLength(sal_Int32 newLength)
479  {
480  assert(newLength >= 0);
481  // Avoid modifications if pData points to const empty string:
482  if( newLength != pData->length )
483  {
484  if( newLength > nCapacity )
485  rtl_uStringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
486  else
487  pData->buffer[newLength] = 0;
488  pData->length = newLength;
489  }
490  }
491 
505  SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
506  sal_Unicode charAt( sal_Int32 index ) const
507  {
508  assert(index >= 0 && index < pData->length);
509  return pData->buffer[ index ];
510  }
511 
522  SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
523  OUStringBuffer & setCharAt(sal_Int32 index, sal_Unicode ch)
524  {
525  assert(index >= 0 && index < pData->length);
526  pData->buffer[ index ] = ch;
527  return *this;
528  }
529 
533  const sal_Unicode* getStr() const SAL_RETURNS_NONNULL { return pData->buffer; }
534 
544  sal_Unicode & operator [](sal_Int32 index)
545  {
546  assert(index >= 0 && index < pData->length);
547  return pData->buffer[index];
548  }
549 
559  const sal_Unicode & operator [](sal_Int32 index) const
560  {
561  assert(index >= 0 && index < pData->length);
562  return pData->buffer[index];
563  }
564 
570  {
571  return OUString(pData->buffer, pData->length);
572  }
573 
584 #if !defined LIBO_INTERNAL_ONLY
586  {
587  return append( str.getStr(), str.getLength() );
588  }
589 #else
590  OUStringBuffer & append(std::u16string_view sv) {
591  if (sv.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max())) {
592  throw std::bad_alloc();
593  }
594  return append(sv.data(), sv.size());
595  }
596 #endif
597 
598 #if !defined LIBO_INTERNAL_ONLY
599 
612  {
613  if(!str.isEmpty())
614  {
615  append( str.getStr(), str.getLength() );
616  }
617  return *this;
618  }
619 #endif
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 #endif
693 
694 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
695 
699  template< typename T1, typename T2 >
700  OUStringBuffer& append( OUStringConcat< T1, T2 >&& c )
701  {
702  sal_Int32 l = c.length();
703  if( l == 0 )
704  return *this;
705  l += pData->length;
706  rtl_uStringbuffer_ensureCapacity( &pData, &nCapacity, l );
707  sal_Unicode* end = c.addData( pData->buffer + pData->length );
708  *end = '\0';
709  pData->length = l;
710  return *this;
711  }
712 
717  template< typename T >
718  OUStringBuffer& append( OUStringNumber< T >&& c )
719  {
720  return append( c.buf, c.length );
721  }
722 #endif
723 
740  OUStringBuffer & appendAscii( const char * str )
741  {
742  return appendAscii( str, rtl_str_getLength( str ) );
743  }
744 
763  OUStringBuffer & appendAscii( const char * str, sal_Int32 len)
764  {
765  rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), str, len );
766  return *this;
767  }
768 
783  {
785  return append( sz, rtl_ustr_valueOfBoolean( sz, b ) );
786  }
787 
789  // Pointer can be automatically converted to bool, which is unwanted here.
790  // Explicitly delete all pointer append() overloads to prevent this
791  // (except for char* and sal_Unicode* overloads, which are handled elsewhere).
792  template< typename T >
793  typename libreoffice_internal::Enable< void,
795  append( T* ) SAL_DELETED_FUNCTION;
797 
798  // This overload is needed because OUString has a ctor from rtl_uString*, but
799  // the bool overload above would be preferred to the conversion.
803  OUStringBuffer & append(rtl_uString* str)
804  {
805  return append( OUString( str ));
806  }
807 
820  {
822  return append( sz, rtl_ustr_valueOfBoolean( sz, b ) );
823  }
824 
838  {
839  assert(static_cast< unsigned char >(c) <= 0x7F);
840  return append(sal_Unicode(c));
841  }
842 
854  {
855  return append( &c, 1 );
856  }
857 
858 #if defined LIBO_INTERNAL_ONLY
859  void append(sal_uInt16) = delete;
860 #endif
861 
874  OUStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
875  {
877  return append( sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
878  }
879 
892  OUStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
893  {
895  return append( sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
896  }
897 
910  {
912  return append( sz, rtl_ustr_valueOfFloat( sz, f ) );
913  }
914 
926  OUStringBuffer & append(double d)
927  {
929  return append( sz, rtl_ustr_valueOfDouble( sz, d ) );
930  }
931 
945  OUStringBuffer & appendUtf32(sal_uInt32 c) {
946  return insertUtf32(getLength(), c);
947  }
948 
964  sal_Unicode * appendUninitialized(sal_Int32 length) SAL_RETURNS_NONNULL {
965  sal_Int32 n = getLength();
966  rtl_uStringbuffer_insert(&pData, &nCapacity, n, NULL, length);
967  return pData->buffer + n;
968  }
969 
985 #if defined LIBO_INTERNAL_ONLY
986  OUStringBuffer & insert(sal_Int32 offset, std::u16string_view str)
987  {
988  return insert( offset, str.data(), str.length() );
989  }
990 #else
991  OUStringBuffer & insert(sal_Int32 offset, const OUString & str)
992  {
993  return insert( offset, str.getStr(), str.getLength() );
994  }
995 #endif
996 
1014  OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str )
1015  {
1016  return insert( offset, str, rtl_ustr_getLength( str ) );
1017  }
1018 
1037  OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str, sal_Int32 len)
1038  {
1039  assert( len == 0 || str != NULL ); // cannot assert that in rtl_uStringbuffer_insert
1040  rtl_uStringbuffer_insert( &pData, &nCapacity, offset, str, len );
1041  return *this;
1042  }
1043 
1049  template< typename T >
1051  {
1052  assert(
1055  &pData, &nCapacity, offset,
1058  return *this;
1059  }
1060 
1061 #if defined LIBO_INTERNAL_ONLY
1062 
1063  template<typename T>
1065  T, OUStringBuffer &>::TypeUtf16
1066  insert(sal_Int32 offset, T & literal) {
1067  return insert(
1068  offset,
1071  }
1072 #endif
1073 
1091  OUStringBuffer & insert(sal_Int32 offset, sal_Bool b)
1092  {
1094  return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
1095  }
1096 
1116  OUStringBuffer & insert(sal_Int32 offset, bool b)
1117  {
1119  return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
1120  }
1121 
1140  OUStringBuffer & insert(sal_Int32 offset, char c)
1141  {
1142  sal_Unicode u = c;
1143  return insert( offset, &u, 1 );
1144  }
1145 
1162  OUStringBuffer & insert(sal_Int32 offset, sal_Unicode c)
1163  {
1164  return insert( offset, &c, 1 );
1165  }
1166 
1186  OUStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
1187  {
1189  return insert( offset, sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
1190  }
1191 
1211  OUStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
1212  {
1214  return insert( offset, sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
1215  }
1216 
1235  OUStringBuffer insert(sal_Int32 offset, float f)
1236  {
1238  return insert( offset, sz, rtl_ustr_valueOfFloat( sz, f ) );
1239  }
1240 
1259  OUStringBuffer & insert(sal_Int32 offset, double d)
1260  {
1262  return insert( offset, sz, rtl_ustr_valueOfDouble( sz, d ) );
1263  }
1264 
1280  OUStringBuffer & insertUtf32(sal_Int32 offset, sal_uInt32 c) {
1281  rtl_uStringbuffer_insertUtf32(&pData, &nCapacity, offset, c);
1282  return *this;
1283  }
1284 
1297  OUStringBuffer & remove( sal_Int32 start, sal_Int32 len )
1298  {
1299  rtl_uStringbuffer_remove( &pData, start, len );
1300  return *this;
1301  }
1302 
1313  OUStringBuffer & truncate( sal_Int32 start = 0 )
1314  {
1315  rtl_uStringbuffer_remove( &pData, start, getLength() - start );
1316  return *this;
1317  }
1318 
1330  {
1331  sal_Int32 index = 0;
1332  while((index = indexOf(oldChar, index)) >= 0)
1333  {
1334  pData->buffer[ index ] = newChar;
1335  }
1336  return *this;
1337  }
1338 
1354  void accessInternals(rtl_uString *** pInternalData,
1355  sal_Int32 ** pInternalCapacity)
1356  {
1357  *pInternalData = &pData;
1358  *pInternalCapacity = &nCapacity;
1359  }
1360 
1361 
1377  sal_Int32 indexOf( sal_Unicode ch, sal_Int32 fromIndex = 0 ) const
1378  {
1379  assert( fromIndex >= 0 && fromIndex <= pData->length );
1380  sal_Int32 ret = rtl_ustr_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch );
1381  return (ret < 0 ? ret : ret+fromIndex);
1382  }
1383 
1395  sal_Int32 lastIndexOf( sal_Unicode ch ) const
1396  {
1397  return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch );
1398  }
1399 
1414  sal_Int32 lastIndexOf( sal_Unicode ch, sal_Int32 fromIndex ) const
1415  {
1416  assert( fromIndex >= 0 && fromIndex <= pData->length );
1417  return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch );
1418  }
1419 
1437 #if defined LIBO_INTERNAL_ONLY
1438  sal_Int32 indexOf( std::u16string_view str, sal_Int32 fromIndex = 0 ) const
1439  {
1440  assert( fromIndex >= 0 && fromIndex <= pData->length );
1441  sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1442  str.data(), str.length() );
1443  return (ret < 0 ? ret : ret+fromIndex);
1444  }
1445 #else
1446  sal_Int32 indexOf( const OUString & str, sal_Int32 fromIndex = 0 ) const
1447  {
1448  assert( fromIndex >= 0 && fromIndex <= pData->length );
1449  sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1450  str.pData->buffer, str.pData->length );
1451  return (ret < 0 ? ret : ret+fromIndex);
1452  }
1453 #endif
1454 
1461  template< typename T >
1462  typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const
1463  {
1464  assert(
1466  sal_Int32 n = rtl_ustr_indexOfAscii_WithLength(
1467  pData->buffer + fromIndex, pData->length - fromIndex,
1470  return n < 0 ? n : n + fromIndex;
1471  }
1472 
1473 #if defined LIBO_INTERNAL_ONLY
1474 
1475  template<typename T>
1476  typename
1478  indexOf(T & literal, sal_Int32 fromIndex = 0) const {
1479  assert(fromIndex >= 0);
1481  pData->buffer + fromIndex, pData->length - fromIndex,
1484  return n < 0 ? n : n + fromIndex;
1485  }
1486 #endif
1487 
1505 #if defined LIBO_INTERNAL_ONLY
1506  sal_Int32 lastIndexOf( std::u16string_view str ) const
1507  {
1508  return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1509  str.data(), str.length() );
1510  }
1511 #else
1512  sal_Int32 lastIndexOf( const OUString & str ) const
1513  {
1514  return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1515  str.pData->buffer, str.pData->length );
1516  }
1517 #endif
1518 
1538 #if defined LIBO_INTERNAL_ONLY
1539  sal_Int32 lastIndexOf( std::u16string_view str, sal_Int32 fromIndex ) const
1540  {
1541  assert( fromIndex >= 0 && fromIndex <= pData->length );
1542  return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1543  str.data(), str.length() );
1544  }
1545 #else
1546  sal_Int32 lastIndexOf( const OUString & str, sal_Int32 fromIndex ) const
1547  {
1548  assert( fromIndex >= 0 && fromIndex <= pData->length );
1549  return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1550  str.pData->buffer, str.pData->length );
1551  }
1552 #endif
1553 
1559  template< typename T >
1561  {
1562  assert(
1565  pData->buffer, pData->length,
1568  }
1569 
1570 #if defined LIBO_INTERNAL_ONLY
1571 
1572  template<typename T>
1573  typename
1575  lastIndexOf(T & literal) const {
1577  pData->buffer, pData->length,
1580  }
1581 #endif
1582 
1592  sal_Int32 stripStart(sal_Unicode c = ' ')
1593  {
1594  sal_Int32 index;
1595  for(index = 0; index < getLength() ; index++)
1596  {
1597  if(pData->buffer[ index ] != c)
1598  {
1599  break;
1600  }
1601  }
1602  if(index)
1603  {
1604  remove(0, index);
1605  }
1606  return index;
1607  }
1608 
1618  sal_Int32 stripEnd(sal_Unicode c = ' ')
1619  {
1620  sal_Int32 result = getLength();
1621  sal_Int32 index;
1622  for(index = getLength(); index > 0 ; index--)
1623  {
1624  if(pData->buffer[ index - 1 ] != c)
1625  {
1626  break;
1627  }
1628  }
1629  if(index < getLength())
1630  {
1631  truncate(index);
1632  }
1633  return result - getLength();
1634  }
1644  sal_Int32 strip(sal_Unicode c = ' ')
1645  {
1646  return stripStart(c) + stripEnd(c);
1647  }
1648 
1649 #if defined LIBO_INTERNAL_ONLY
1650 
1660  SAL_WARN_UNUSED_RESULT std::u16string_view subView( sal_Int32 beginIndex ) const
1661  {
1662  assert(beginIndex >= 0);
1663  assert(beginIndex <= getLength());
1664  return subView(beginIndex, getLength() - beginIndex);
1665  }
1666 
1679  SAL_WARN_UNUSED_RESULT std::u16string_view subView( sal_Int32 beginIndex, sal_Int32 count ) const
1680  {
1681  assert(beginIndex >= 0);
1682  assert(count >= 0);
1683  assert(beginIndex <= getLength());
1684  assert(count <= getLength() - beginIndex);
1685  return std::u16string_view(pData->buffer, sal_uInt32(pData->length)).substr(beginIndex, count);
1686  }
1687 #endif
1688 
1700  OUStringBuffer copy( sal_Int32 beginIndex ) const
1701  {
1702  return copy( beginIndex, getLength() - beginIndex );
1703  }
1704 
1718  OUStringBuffer copy( sal_Int32 beginIndex, sal_Int32 count ) const
1719  {
1720  assert(beginIndex >= 0 && beginIndex <= getLength());
1721  assert(count >= 0 && count <= getLength() - beginIndex);
1722  rtl_uString *pNew = NULL;
1723  rtl_uStringbuffer_newFromStr_WithLength( &pNew, getStr() + beginIndex, count );
1724  return OUStringBuffer( pNew, count + 16 );
1725  }
1726 
1727 private:
1728  OUStringBuffer( rtl_uString * value, const sal_Int32 capacity )
1729  {
1730  pData = value;
1731  nCapacity = capacity;
1732  }
1733 
1737  rtl_uString * pData;
1738 
1742  sal_Int32 nCapacity;
1743 };
1744 
1745 #if defined LIBO_INTERNAL_ONLY
1746 template<> struct ToStringHelper<OUStringBuffer> {
1747  static std::size_t length(OUStringBuffer const & s) { return s.getLength(); }
1748 
1749  static sal_Unicode * addData(sal_Unicode * buffer, OUStringBuffer const & s) SAL_RETURNS_NONNULL
1750  { return addDataHelper(buffer, s.getStr(), s.getLength()); }
1751 
1752  static constexpr bool allowOStringConcat = false;
1753  static constexpr bool allowOUStringConcat = true;
1754 };
1755 #endif
1756 
1757 #if defined LIBO_INTERNAL_ONLY
1758  // Define this here to avoid circular includes
1759  inline OUString & OUString::operator+=( const OUStringBuffer & str ) &
1760  {
1761  // Call operator= if this is empty, otherwise rtl_uString_newConcat will attempt to
1762  // acquire() the str.pData buffer, which is part of the OUStringBuffer mutable state.
1763  if (isEmpty())
1764  return operator=(str.toString());
1765  else
1766  return internalAppend(str.pData);
1767  }
1768 #endif
1769 }
1770 
1771 #ifdef RTL_STRING_UNITTEST
1772 namespace rtl
1773 {
1774 typedef rtlunittest::OUStringBuffer OUStringBuffer;
1775 }
1776 #endif
1777 
1778 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
1779 using ::rtl::OUStringBuffer;
1780 #endif
1781 
1782 #endif // INCLUDED_RTL_USTRBUF_HXX
1783 
1784 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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.
OUString & operator+=(const OUString &str)
Append a string to this string.
Definition: ustring.hxx:663
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_VALUEOFINT32
Definition: ustring.h:961
#define RTL_USTR_MAX_VALUEOFBOOLEAN
Definition: ustring.h:919
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 & append(sal_Bool b)
Appends the string representation of the sal_Bool argument to the string buffer.
Definition: ustrbuf.hxx:819
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:874
SAL_DLLPUBLIC sal_Int32 rtl_ustr_valueOfFloat(sal_Unicode *str, float f) SAL_THROW_EXTERN_C()
Create the string representation of a float.
OUStringBuffer & insert(sal_Int32 offset, sal_Bool b)
Inserts the string representation of the sal_Bool argument into this string buffer.
Definition: ustrbuf.hxx:1091
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.
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
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:1462
OUStringBuffer & insert(sal_Int32 offset, const OUString &str)
Inserts the string into this string buffer.
Definition: ustrbuf.hxx:991
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.
OUStringBuffer & append(double d)
Appends the string representation of the double argument to this string buffer.
Definition: ustrbuf.hxx:926
#define SAL_DEPRECATED(message)
Use as follows: SAL_DEPRECATED(&quot;Don&#39;t use, it&#39;s evil.&quot;) void doit(int nPara);.
Definition: types.h:474
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.
OUStringBuffer & insertUtf32(sal_Int32 offset, sal_uInt32 c)
Inserts a single UTF-32 character into this string buffer.
Definition: ustrbuf.hxx:1280
void setLength(sal_Int32 newLength)
Sets the length of this String buffer.
Definition: ustrbuf.hxx:478
OUStringBuffer & insert(sal_Int32 offset, bool b)
Inserts the string representation of the bool argument into this string buffer.
Definition: ustrbuf.hxx:1116
sal_Int32 lastIndexOf(const OUString &str, sal_Int32 fromIndex) const
Returns the index within this string of the last occurrence of the specified substring, searching backward starting before the specified index.
Definition: ustrbuf.hxx:1546
OUStringBuffer & append(const OUString &str)
Appends the string to this string buffer.
Definition: ustrbuf.hxx:585
This String class provides base functionality for C++ like Unicode character array handling...
Definition: ustring.hxx:203
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.
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, double d)
Inserts the string representation of the double argument into this string buffer. ...
Definition: ustrbuf.hxx:1259
#define RTL_USTR_MAX_VALUEOFINT64
Definition: ustring.h:984
Definition: stringutils.hxx:370
void accessInternals(rtl_uString ***pInternalData, sal_Int32 **pInternalCapacity)
Allows access to the internal data of this OUStringBuffer, for effective manipulation.
Definition: ustrbuf.hxx:1354
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
sal_Int32 getLength() const
Returns the length (character count) of this string buffer.
Definition: ustrbuf.hxx:411
#define SAL_WARN_UNUSED_RESULT
Use this as markup for functions and methods whose return value must be checked.
Definition: types.h:284
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...
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:1037
sal_Int32 stripStart(sal_Unicode c= ' ')
Strip the given character from the start of the buffer.
Definition: ustrbuf.hxx:1592
definition of a no acquire enum for ctors
Definition: types.h:356
OUString toString() const
Return an OUString instance reflecting the current content of this OUStringBuffer.
Definition: ustrbuf.hxx:569
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 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.
OUStringBuffer & insert(sal_Int32 offset, sal_Unicode c)
Inserts the string representation of the char argument into this string buffer.
Definition: ustrbuf.hxx:1162
SAL_DLLPUBLIC sal_Int32 rtl_ustr_valueOfBoolean(sal_Unicode *str, sal_Bool b) SAL_THROW_EXTERN_C()
Create the string representation of a boolean.
const sal_Unicode * getStr() const SAL_RETURNS_NONNULL
Returns a pointer to the Unicode character buffer for this string.
Definition: ustring.hxx:814
OUStringBuffer & appendAscii(const char *str)
Appends a 8-Bit ASCII character string to this string buffer.
Definition: ustrbuf.hxx:740
sal_Int32 getCapacity() const
Returns the current capacity of the String buffer.
Definition: ustrbuf.hxx:439
OUStringBuffer & appendUtf32(sal_uInt32 c)
Appends a single UTF-32 character to this string buffer.
Definition: ustrbuf.hxx:945
pData
Definition: ustring.hxx:355
OUStringBuffer & append(const OUStringBuffer &str)
Appends the content of a stringbuffer to this string buffer.
Definition: ustrbuf.hxx:611
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:964
OUStringBuffer copy(sal_Int32 beginIndex, sal_Int32 count) const
Returns a new string buffer that is a substring of this string.
Definition: ustrbuf.hxx:1718
OUStringBuffer copy(sal_Int32 beginIndex) const
Returns a new string buffer that is a substring of this string.
Definition: ustrbuf.hxx:1700
Definition: stringutils.hxx:138
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.
bool isEmpty() const
Checks if a string buffer is empty.
Definition: ustrbuf.hxx:424
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 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_Int32 lastIndexOf(const OUString &str) const
Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the end.
Definition: ustrbuf.hxx:1512
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:1211
sal_Int32 lastIndexOf(sal_Unicode ch, sal_Int32 fromIndex) const
Returns the index within this string of the last occurrence of the specified character, searching backward starting before the specified index.
Definition: ustrbuf.hxx:1414
SAL_DLLPUBLIC sal_Int32 rtl_ustr_valueOfDouble(sal_Unicode *str, double d) SAL_THROW_EXTERN_C()
Create the string representation of a double.
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, starting the search at the specified index.
Definition: ustrbuf.hxx:1377
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:1050
OUStringBuffer()
Constructs a string buffer with no characters in it and an initial capacity of 16 characters...
Definition: ustrbuf.hxx:77
SAL_DLLPUBLIC void rtl_uString_release(rtl_uString *str) SAL_THROW_EXTERN_C() SAL_HOT
Decrement the reference count of a string.
unsigned char sal_Bool
Definition: types.h:38
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, starting at the specified index.
Definition: ustrbuf.hxx:1446
sal_Int32 strip(sal_Unicode c= ' ')
Strip the given character from the both end of the buffer.
Definition: ustrbuf.hxx:1644
void ensureCapacity(sal_Int32 minimumCapacity)
Ensures that the capacity of the buffer is at least equal to the specified minimum.
Definition: ustrbuf.hxx:455
SAL_WARN_UNUSED_RESULT OUString makeStringAndClear()
Fill the string data in the new string and clear the buffer.
Definition: ustrbuf.hxx:399
OUStringBuffer & append(sal_Int64 l, sal_Int16 radix=10)
Appends the string representation of the long argument to this string buffer.
Definition: ustrbuf.hxx:892
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_valueOfInt32(sal_Unicode *str, sal_Int32 i, sal_Int16 radix) SAL_THROW_EXTERN_C()
Create the string representation of an integer.
sal_Int32 getLength() const
Returns the length of this string.
Definition: ustring.hxx:792
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:1014
OUStringBuffer & append(float f)
Appends the string representation of the float argument to this string buffer.
Definition: ustrbuf.hxx:909
#define RTL_USTR_MAX_VALUEOFFLOAT
Definition: ustring.h:1026
SAL_DLLPUBLIC sal_Int32 rtl_ustr_getLength(const sal_Unicode *str) SAL_THROW_EXTERN_C()
Return the length of a string.
SAL_DLLPUBLIC void rtl_uString_newFromLiteral(rtl_uString **newStr, const char *value, sal_Int32 len, sal_Int32 allocExtra) SAL_THROW_EXTERN_C()
~OUStringBuffer()
Release the string data.
Definition: ustrbuf.hxx:386
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:378
OUStringBuffer & append(sal_Unicode c)
Appends the string representation of the char argument to this string buffer.
Definition: ustrbuf.hxx:853
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 & append(bool b)
Appends the string representation of the bool argument to the string buffer.
Definition: ustrbuf.hxx:782
OUStringBuffer & replace(sal_Unicode oldChar, sal_Unicode newChar)
Replace all occurrences of oldChar in this string buffer with newChar.
Definition: ustrbuf.hxx:1329
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:587
Definition: stringutils.hxx:136
#define RTL_USTR_MAX_VALUEOFDOUBLE
Definition: ustring.h:1045
sal_Int32 stripEnd(sal_Unicode c= ' ')
Strip the given character from the end of the buffer.
Definition: ustrbuf.hxx:1618
OUStringBuffer & insert(sal_Int32 offset, char c)
Inserts the string representation of the char argument into this string buffer.
Definition: ustrbuf.hxx:1140
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:1186
SAL_DLLPUBLIC void rtl_uStringbuffer_remove(rtl_uString **This, sal_Int32 start, sal_Int32 len)
Removes the characters in a substring of this sequence.
OUStringBuffer & appendAscii(const char *str, sal_Int32 len)
Appends a 8-Bit ASCII character string to this string buffer.
Definition: ustrbuf.hxx:763
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:1560
OUStringBuffer insert(sal_Int32 offset, float f)
Inserts the string representation of the float argument into this string buffer.
Definition: ustrbuf.hxx:1235
const sal_Unicode * getStr() const SAL_RETURNS_NONNULL
Return a null terminated unicode character array.
Definition: ustrbuf.hxx:533
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...
OUStringBuffer & truncate(sal_Int32 start=0)
Removes the tail of a string buffer start at the indicate position.
Definition: ustrbuf.hxx:1313
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_uString_new(rtl_uString **newStr) SAL_THROW_EXTERN_C()
Allocate a new string containing no characters.
OUStringBuffer & append(const sal_Unicode *str)
Appends the string representation of the char array argument to this string buffer.
Definition: ustrbuf.hxx:637
sal_uInt16 sal_Unicode
Definition: types.h:123
OUStringBuffer & append(char c)
Appends the string representation of the ASCII char argument to this string buffer.
Definition: ustrbuf.hxx:837
sal_Int32 lastIndexOf(sal_Unicode ch) const
Returns the index within this string of the last occurrence of the specified character, searching backward starting at the end.
Definition: ustrbuf.hxx:1395
A string buffer implements a mutable sequence of characters.
Definition: ustrbuf.hxx:69