AlterOffice
AlterOffice 3.4 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 
4 #ifndef INCLUDED_RTL_USTRBUF_HXX
5 #define INCLUDED_RTL_USTRBUF_HXX
6 
7 #include "sal/config.h"
8 
9 #include <cassert>
10 #include <cstring>
11 #include <limits>
12 #include <new>
13 
14 #if defined LIBO_INTERNAL_ONLY
15 #include <string_view>
16 #include <type_traits>
17 #include <utility>
18 #endif
19 
20 #include "rtl/ustrbuf.h"
21 #include "rtl/ustring.hxx"
22 #include "rtl/stringutils.hxx"
23 #include "sal/types.h"
24 
25 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
26 #include "rtl/stringconcat.hxx"
27 #endif
28 
29 #ifdef RTL_STRING_UNITTEST
30 extern bool rtl_string_unittest_invalid_conversion;
31 #endif
32 
33 // The unittest uses slightly different code to help check that the proper
34 // calls are made. The class is put into a different namespace to make
35 // sure the compiler generates a different (if generating also non-inline)
36 // copy of the function and does not merge them together. The class
37 // is "brought" into the proper rtl namespace by a typedef below.
38 #ifdef RTL_STRING_UNITTEST
39 #define rtl rtlunittest
40 #endif
41 
42 namespace rtl
43 {
44 
45 #ifdef RTL_STRING_UNITTEST
46 #undef rtl
47 #endif
48 
52 {
53 friend class OUString;
54 public:
60  : pData(NULL)
61  , nCapacity( 16 )
62  {
63  rtl_uString_new_WithLength( &pData, nCapacity );
64  }
65 
72  OUStringBuffer( const OUStringBuffer & value )
73  : pData(NULL)
74  , nCapacity( value.nCapacity )
75  {
76  rtl_uStringbuffer_newFromStringBuffer( &pData, value.nCapacity, value.pData );
77  }
78 
85  explicit OUStringBuffer(sal_Int32 length)
86  : pData(NULL)
87  , nCapacity( length )
88  {
89  rtl_uString_new_WithLength( &pData, length );
90  }
91 #if defined LIBO_INTERNAL_ONLY
92  template<typename T>
93  explicit OUStringBuffer(T length, std::enable_if_t<std::is_integral_v<T>, int> = 0)
94  : OUStringBuffer(static_cast<sal_Int32>(length))
95  {
96  assert(
97  length >= 0
98  && static_cast<std::make_unsigned_t<T>>(length)
99  <= static_cast<std::make_unsigned_t<sal_Int32>>(
100  std::numeric_limits<sal_Int32>::max()));
101  }
102  // avoid (obvious) bugs
103  explicit OUStringBuffer(bool) = delete;
104  explicit OUStringBuffer(char) = delete;
105  explicit OUStringBuffer(wchar_t) = delete;
106 #if defined __cpp_char8_t
107  explicit OUStringBuffer(char8_t) = delete;
108 #endif
109  explicit OUStringBuffer(char16_t) = delete;
110  explicit OUStringBuffer(char32_t) = delete;
111 #endif
112 
123 #if defined LIBO_INTERNAL_ONLY
124  OUStringBuffer(std::u16string_view sv)
125  : pData(nullptr)
126  , nCapacity( sv.length() + 16 )
127  {
128  if (sv.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max())) {
129  throw std::bad_alloc();
130  }
131  rtl_uStringbuffer_newFromStr_WithLength( &pData, sv.data(), sv.length() );
132  }
133 #else
134  OUStringBuffer(const OUString& value)
135  : pData(NULL)
136  , nCapacity( value.getLength() + 16 )
137  {
138  rtl_uStringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
139  }
140 #endif
141 
142  template< typename T >
144  : pData(NULL)
145  , nCapacity( libreoffice_internal::ConstCharArrayDetector<T>::length + 16 )
146  {
147  assert(
150  &pData,
153 #ifdef RTL_STRING_UNITTEST
154  rtl_string_unittest_const_literal = true;
155 #endif
156  }
157 
158 #if defined LIBO_INTERNAL_ONLY
159 
160  template<typename T>
162  T & literal,
164  T, libreoffice_internal::Dummy>::TypeUtf16
166  pData(nullptr),
167  nCapacity(libreoffice_internal::ConstCharArrayDetector<T>::length + 16)
168  {
170  &pData,
173  }
174 #endif
175 
176 #if defined LIBO_INTERNAL_ONLY && defined RTL_STRING_UNITTEST
177 
182  template< typename T >
183  OUStringBuffer( T&, typename libreoffice_internal::ExceptConstCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
184  {
185  pData = NULL;
186  nCapacity = 10;
187  rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
188  rtl_string_unittest_invalid_conversion = true;
189  }
194  template< typename T >
195  OUStringBuffer( const T&, typename libreoffice_internal::ExceptCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
196  {
197  pData = NULL;
198  nCapacity = 10;
199  rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
200  rtl_string_unittest_invalid_conversion = true;
201  }
203 #endif
204 
205 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
206 
210  template< typename T1, typename T2 >
211  OUStringBuffer( OUStringConcat< T1, T2 >&& c )
212  {
213  const sal_Int32 l = c.length();
214  nCapacity = l + 16;
215  pData = rtl_uString_alloc( nCapacity );
216  sal_Unicode* end = c.addData( pData->buffer );
217  *end = '\0';
218  pData->length = l;
219  }
220 
225  template< typename T, std::size_t N >
226  OUStringBuffer( StringNumberBase< sal_Unicode, T, N >&& n )
227  : pData(NULL)
228  , nCapacity( n.length + 16 )
229  {
230  rtl_uStringbuffer_newFromStr_WithLength( &pData, n.buf, n.length );
231  }
232 #endif
233 
234 #if defined LIBO_INTERNAL_ONLY
235  operator std::u16string_view() const { return {getStr(), sal_uInt32(getLength())}; }
236 #endif
237 
240  OUStringBuffer& operator = ( const OUStringBuffer& value )
241  {
242  if (this != &value)
243  {
245  value.nCapacity,
246  value.pData);
247  nCapacity = value.nCapacity;
248  }
249  return *this;
250  }
251 
252 #if defined LIBO_INTERNAL_ONLY
253 
256  OUStringBuffer& operator = ( OUStringBuffer&& value ) noexcept
257  {
258  rtl_uString_release( pData );
259  pData = value.pData;
260  nCapacity = value.nCapacity;
261  value.pData = nullptr;
262  value.nCapacity = 0;
263  rtl_uString_new( &value.pData );
264  return *this;
265  }
266 #endif
267 
272 #if defined LIBO_INTERNAL_ONLY
273  OUStringBuffer & operator =(std::u16string_view string) {
274  sal_Int32 n = string.length();
275  if (n >= nCapacity) {
276  ensureCapacity(n + 16); //TODO: check for overflow
277  }
278  std::memcpy(
279  pData->buffer, string.data(),
280  n * sizeof (sal_Unicode));
281  pData->buffer[n] = '\0';
282  pData->length = n;
283  return *this;
284  }
285 #else
286  OUStringBuffer & operator =(OUString const & string) {
287  sal_Int32 n = string.getLength();
288  if (n >= nCapacity) {
289  ensureCapacity(n + 16); //TODO: check for overflow
290  }
291  std::memcpy(
292  pData->buffer, string.pData->buffer,
293  (n + 1) * sizeof (sal_Unicode));
294  pData->length = n;
295  return *this;
296  }
297 #endif
298 
303  template<typename T>
304  typename
306  operator =(T & literal) {
307  assert(
309  sal_Int32 const n
311  if (n >= nCapacity) {
312  ensureCapacity(n + 16); //TODO: check for overflow
313  }
314  char const * from
316  literal);
317  sal_Unicode * to = pData->buffer;
318  for (sal_Int32 i = 0; i <= n; ++i) {
319  to[i] = from[i];
320  }
321  pData->length = n;
322  return *this;
323  }
324 
325 #if defined LIBO_INTERNAL_ONLY
326 
327  template<typename T>
329  T, OUStringBuffer &>::TypeUtf16
330  operator =(T & literal) {
331  sal_Int32 const n
333  if (n >= nCapacity) {
334  ensureCapacity(n + 16); //TODO: check for overflow
335  }
336  // For OUStringChar, which is covered by this template's ConstCharArrayDetector TypeUtf16
337  // check, toPointer does not return a NUL-terminated string, so we can't just memcpy n+1
338  // elements but rather need to add the terminating NUL manually:
339  std::memcpy(
340  pData->buffer,
341  libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
342  n * sizeof (sal_Unicode));
343  pData->buffer[n] = '\0';
344  pData->length = n;
345  return *this;
346  }
347 #endif
348 
349 #if defined LIBO_INTERNAL_ONLY
350 
351  template<typename T1, typename T2>
352  OUStringBuffer & operator =(OUStringConcat<T1, T2> && concat) {
353  sal_Int32 const n = concat.length();
354  if (n >= nCapacity) {
355  ensureCapacity(n + 16); //TODO: check for overflow
356  }
357  *concat.addData(pData->buffer) = 0;
358  pData->length = n;
359  return *this;
360  }
361 
363  template<typename T, std::size_t N>
364  OUStringBuffer & operator =(StringNumberBase<sal_Unicode, T, N> && n)
365  {
366  *this = OUStringBuffer( std::move( n ) );
367  return *this;
368  }
369 #endif
370 
375  {
376  rtl_uString_release( pData );
377  }
378 
388  {
389  return OUString(
390  rtl_uStringBuffer_makeStringAndClear( &pData, &nCapacity ),
391  SAL_NO_ACQUIRE );
392  }
393 
399  sal_Int32 getLength() const
400  {
401  return pData->length;
402  }
403 
412  bool isEmpty() const
413  {
414  return pData->length == 0;
415  }
416 
427  sal_Int32 getCapacity() const
428  {
429  return nCapacity;
430  }
431 
443  void ensureCapacity(sal_Int32 minimumCapacity)
444  {
445  rtl_uStringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
446  }
447 
466  void setLength(sal_Int32 newLength)
467  {
468  assert(newLength >= 0);
469  // Avoid modifications if pData points to const empty string:
470  if( newLength != pData->length )
471  {
472  if( newLength > nCapacity )
473  rtl_uStringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
474  else
475  pData->buffer[newLength] = 0;
476  pData->length = newLength;
477  }
478  }
479 
493  SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
494  sal_Unicode charAt( sal_Int32 index ) const
495  {
496  assert(index >= 0 && index < pData->length);
497  return pData->buffer[ index ];
498  }
499 
510  SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
511  OUStringBuffer & setCharAt(sal_Int32 index, sal_Unicode ch)
512  {
513  assert(index >= 0 && index < pData->length);
514  pData->buffer[ index ] = ch;
515  return *this;
516  }
517 
521  const sal_Unicode* getStr() const SAL_RETURNS_NONNULL { return pData->buffer; }
522 
532  sal_Unicode & operator [](sal_Int32 index)
533  {
534  assert(index >= 0 && index < pData->length);
535  return pData->buffer[index];
536  }
537 
547  const sal_Unicode & operator [](sal_Int32 index) const
548  {
549  assert(index >= 0 && index < pData->length);
550  return pData->buffer[index];
551  }
552 
558  {
559  return OUString(pData->buffer, pData->length);
560  }
561 
572 #if !defined LIBO_INTERNAL_ONLY
574  {
575  return append( str.getStr(), str.getLength() );
576  }
577 #else
578  OUStringBuffer & append(std::u16string_view sv) {
579  if (sv.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max())) {
580  throw std::bad_alloc();
581  }
582  return append(sv.data(), sv.size());
583  }
584 #endif
585 
586 #if !defined LIBO_INTERNAL_ONLY
587 
600  {
601  if(!str.isEmpty())
602  {
603  append( str.getStr(), str.getLength() );
604  }
605  return *this;
606  }
607 #endif
608 
620 #if defined LIBO_INTERNAL_ONLY
621  template<typename T>
623  append(T const & str)
624 #else
626 #endif
627  {
628  return append( str, rtl_ustr_getLength( str ) );
629  }
630 
644  OUStringBuffer & append( const sal_Unicode * str, sal_Int32 len)
645  {
646  assert( len == 0 || str != NULL ); // cannot assert that in rtl_uStringbuffer_insert
647  rtl_uStringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
648  return *this;
649  }
650 
656  template< typename T >
658  {
659  assert(
661  return appendAscii(
664  }
665 
666 #if defined LIBO_INTERNAL_ONLY
667  template<typename T>
669  append(T & value) { return append(static_cast<sal_Unicode *>(value)); }
670 
672  template<typename T>
673  typename libreoffice_internal::ConstCharArrayDetector<
674  T, OUStringBuffer &>::TypeUtf16
675  append(T & literal) {
676  return append(
677  libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
678  libreoffice_internal::ConstCharArrayDetector<T>::length);
679  }
680 #endif
681 
682 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
683 
687  template< typename T1, typename T2 >
688  OUStringBuffer& append( OUStringConcat< T1, T2 >&& c )
689  {
690  sal_Int32 l = c.length();
691  if( l == 0 )
692  return *this;
693  l += pData->length;
694  rtl_uStringbuffer_ensureCapacity( &pData, &nCapacity, l );
695  sal_Unicode* end = c.addData( pData->buffer + pData->length );
696  *end = '\0';
697  pData->length = l;
698  return *this;
699  }
700 
705  template< typename T, std::size_t N >
706  OUStringBuffer& append( StringNumberBase< sal_Unicode, T, N >&& c )
707  {
708  return append( c.buf, c.length );
709  }
710 #endif
711 
728  OUStringBuffer & appendAscii( const char * str )
729  {
730  return appendAscii( str, rtl_str_getLength( str ) );
731  }
732 
750  OUStringBuffer & appendAscii( const char * str, sal_Int32 len)
751  {
752  rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), str, len );
753  return *this;
754  }
755 
770  {
772  return append( sz, rtl_ustr_valueOfBoolean( sz, b ) );
773  }
774 
776  // Pointer can be automatically converted to bool, which is unwanted here.
777  // Explicitly delete all pointer append() overloads to prevent this
778  // (except for char* and sal_Unicode* overloads, which are handled elsewhere).
779  template< typename T >
780  typename libreoffice_internal::Enable< void,
782  append( T* ) SAL_DELETED_FUNCTION;
784 
785  // This overload is needed because OUString has a ctor from rtl_uString*, but
786  // the bool overload above would be preferred to the conversion.
790  OUStringBuffer & append(rtl_uString* str)
791  {
792  return append( OUString( str ));
793  }
794 
807  {
809  return append( sz, rtl_ustr_valueOfBoolean( sz, b ) );
810  }
811 
825  {
826  assert(static_cast< unsigned char >(c) <= 0x7F);
827  return append(sal_Unicode(c));
828  }
829 
841  {
842  return append( &c, 1 );
843  }
844 
845 #if defined LIBO_INTERNAL_ONLY
846  void append(sal_uInt16) = delete;
847 #endif
848 
861  OUStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
862  {
864  return append( sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
865  }
866 
879  OUStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
880  {
882  return append( sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
883  }
884 
897  {
899  return append( sz, rtl_ustr_valueOfFloat( sz, f ) );
900  }
901 
913  OUStringBuffer & append(double d)
914  {
916  return append( sz, rtl_ustr_valueOfDouble( sz, d ) );
917  }
918 
932  OUStringBuffer & appendUtf32(sal_uInt32 c) {
933  return insertUtf32(getLength(), c);
934  }
935 
951  sal_Unicode * appendUninitialized(sal_Int32 length) SAL_RETURNS_NONNULL {
952  sal_Int32 n = getLength();
953  rtl_uStringbuffer_insert(&pData, &nCapacity, n, NULL, length);
954  return pData->buffer + n;
955  }
956 
957 #if defined LIBO_INTERNAL_ONLY
958 
964  template<typename T>
965  OUStringBuffer& operator<<(T&& rValue)
966  {
967  return append(std::forward<T>(rValue));
968  }
969 #endif
970 
986 #if defined LIBO_INTERNAL_ONLY
987  OUStringBuffer & insert(sal_Int32 offset, std::u16string_view str)
988  {
989  return insert( offset, str.data(), str.length() );
990  }
991 #else
992  OUStringBuffer & insert(sal_Int32 offset, const OUString & str)
993  {
994  return insert( offset, str.getStr(), str.getLength() );
995  }
996 #endif
997 
1015  OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str )
1016  {
1017  return insert( offset, str, rtl_ustr_getLength( str ) );
1018  }
1019 
1038  OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str, sal_Int32 len)
1039  {
1040  assert( len == 0 || str != NULL ); // cannot assert that in rtl_uStringbuffer_insert
1041  rtl_uStringbuffer_insert( &pData, &nCapacity, offset, str, len );
1042  return *this;
1043  }
1044 
1050  template< typename T >
1052  {
1053  assert(
1056  &pData, &nCapacity, offset,
1059  return *this;
1060  }
1061 
1062 #if defined LIBO_INTERNAL_ONLY
1063 
1064  template<typename T>
1066  T, OUStringBuffer &>::TypeUtf16
1067  insert(sal_Int32 offset, T & literal) {
1068  return insert(
1069  offset,
1072  }
1073 #endif
1074 
1092  OUStringBuffer & insert(sal_Int32 offset, sal_Bool b)
1093  {
1095  return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
1096  }
1097 
1117  OUStringBuffer & insert(sal_Int32 offset, bool b)
1118  {
1120  return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
1121  }
1122 
1141  OUStringBuffer & insert(sal_Int32 offset, char c)
1142  {
1143  sal_Unicode u = c;
1144  return insert( offset, &u, 1 );
1145  }
1146 
1163  OUStringBuffer & insert(sal_Int32 offset, sal_Unicode c)
1164  {
1165  return insert( offset, &c, 1 );
1166  }
1167 
1187  OUStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
1188  {
1190  return insert( offset, sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
1191  }
1192 
1212  OUStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
1213  {
1215  return insert( offset, sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
1216  }
1217 
1236  OUStringBuffer insert(sal_Int32 offset, float f)
1237  {
1239  return insert( offset, sz, rtl_ustr_valueOfFloat( sz, f ) );
1240  }
1241 
1260  OUStringBuffer & insert(sal_Int32 offset, double d)
1261  {
1263  return insert( offset, sz, rtl_ustr_valueOfDouble( sz, d ) );
1264  }
1265 
1281  OUStringBuffer & insertUtf32(sal_Int32 offset, sal_uInt32 c) {
1282  rtl_uStringbuffer_insertUtf32(&pData, &nCapacity, offset, c);
1283  return *this;
1284  }
1285 
1298  OUStringBuffer & remove( sal_Int32 start, sal_Int32 len )
1299  {
1300  rtl_uStringbuffer_remove( &pData, start, len );
1301  return *this;
1302  }
1303 
1314  OUStringBuffer & truncate( sal_Int32 start = 0 )
1315  {
1316  rtl_uStringbuffer_remove( &pData, start, getLength() - start );
1317  return *this;
1318  }
1319 
1331  {
1332  sal_Int32 index = 0;
1333  while((index = indexOf(oldChar, index)) >= 0)
1334  {
1335  pData->buffer[ index ] = newChar;
1336  }
1337  return *this;
1338  }
1339 
1355  void accessInternals(rtl_uString *** pInternalData,
1356  sal_Int32 ** pInternalCapacity)
1357  {
1358  *pInternalData = &pData;
1359  *pInternalCapacity = &nCapacity;
1360  }
1361 
1362 
1378  sal_Int32 indexOf( sal_Unicode ch, sal_Int32 fromIndex = 0 ) const
1379  {
1380  assert( fromIndex >= 0 && fromIndex <= pData->length );
1381  sal_Int32 ret = rtl_ustr_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch );
1382  return (ret < 0 ? ret : ret+fromIndex);
1383  }
1384 
1396  sal_Int32 lastIndexOf( sal_Unicode ch ) const
1397  {
1398  return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch );
1399  }
1400 
1415  sal_Int32 lastIndexOf( sal_Unicode ch, sal_Int32 fromIndex ) const
1416  {
1417  assert( fromIndex >= 0 && fromIndex <= pData->length );
1418  return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch );
1419  }
1420 
1438 #if defined LIBO_INTERNAL_ONLY
1439  sal_Int32 indexOf( std::u16string_view str, sal_Int32 fromIndex = 0 ) const
1440  {
1441  assert( fromIndex >= 0 && fromIndex <= pData->length );
1442  sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1443  str.data(), str.length() );
1444  return (ret < 0 ? ret : ret+fromIndex);
1445  }
1446 #else
1447  sal_Int32 indexOf( const OUString & str, sal_Int32 fromIndex = 0 ) const
1448  {
1449  assert( fromIndex >= 0 && fromIndex <= pData->length );
1450  sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1451  str.pData->buffer, str.pData->length );
1452  return (ret < 0 ? ret : ret+fromIndex);
1453  }
1454 #endif
1455 
1462  template< typename T >
1463  typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const
1464  {
1465  assert(
1467  sal_Int32 n = rtl_ustr_indexOfAscii_WithLength(
1468  pData->buffer + fromIndex, pData->length - fromIndex,
1471  return n < 0 ? n : n + fromIndex;
1472  }
1473 
1474 #if defined LIBO_INTERNAL_ONLY
1475 
1476  template<typename T>
1477  typename
1479  indexOf(T & literal, sal_Int32 fromIndex = 0) const {
1480  assert(fromIndex >= 0);
1482  pData->buffer + fromIndex, pData->length - fromIndex,
1485  return n < 0 ? n : n + fromIndex;
1486  }
1487 #endif
1488 
1506 #if defined LIBO_INTERNAL_ONLY
1507  sal_Int32 lastIndexOf( std::u16string_view str ) const
1508  {
1509  return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1510  str.data(), str.length() );
1511  }
1512 #else
1513  sal_Int32 lastIndexOf( const OUString & str ) const
1514  {
1515  return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1516  str.pData->buffer, str.pData->length );
1517  }
1518 #endif
1519 
1539 #if defined LIBO_INTERNAL_ONLY
1540  sal_Int32 lastIndexOf( std::u16string_view str, sal_Int32 fromIndex ) const
1541  {
1542  assert( fromIndex >= 0 && fromIndex <= pData->length );
1543  return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1544  str.data(), str.length() );
1545  }
1546 #else
1547  sal_Int32 lastIndexOf( const OUString & str, sal_Int32 fromIndex ) const
1548  {
1549  assert( fromIndex >= 0 && fromIndex <= pData->length );
1550  return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1551  str.pData->buffer, str.pData->length );
1552  }
1553 #endif
1554 
1560  template< typename T >
1562  {
1563  assert(
1566  pData->buffer, pData->length,
1569  }
1570 
1571 #if defined LIBO_INTERNAL_ONLY
1572 
1573  template<typename T>
1574  typename
1576  lastIndexOf(T & literal) const {
1578  pData->buffer, pData->length,
1581  }
1582 #endif
1583 
1593  sal_Int32 stripStart(sal_Unicode c = ' ')
1594  {
1595  sal_Int32 index;
1596  for(index = 0; index < getLength() ; index++)
1597  {
1598  if(pData->buffer[ index ] != c)
1599  {
1600  break;
1601  }
1602  }
1603  if(index)
1604  {
1605  remove(0, index);
1606  }
1607  return index;
1608  }
1609 
1619  sal_Int32 stripEnd(sal_Unicode c = ' ')
1620  {
1621  sal_Int32 result = getLength();
1622  sal_Int32 index;
1623  for(index = getLength(); index > 0 ; index--)
1624  {
1625  if(pData->buffer[ index - 1 ] != c)
1626  {
1627  break;
1628  }
1629  }
1630  if(index < getLength())
1631  {
1632  truncate(index);
1633  }
1634  return result - getLength();
1635  }
1645  sal_Int32 strip(sal_Unicode c = ' ')
1646  {
1647  return stripStart(c) + stripEnd(c);
1648  }
1649 
1650 #if defined LIBO_INTERNAL_ONLY
1651 
1661  SAL_WARN_UNUSED_RESULT std::u16string_view subView( sal_Int32 beginIndex ) const
1662  {
1663  assert(beginIndex >= 0);
1664  assert(beginIndex <= getLength());
1665  return subView(beginIndex, getLength() - beginIndex);
1666  }
1667 
1680  SAL_WARN_UNUSED_RESULT std::u16string_view subView( sal_Int32 beginIndex, sal_Int32 count ) const
1681  {
1682  assert(beginIndex >= 0);
1683  assert(count >= 0);
1684  assert(beginIndex <= getLength());
1685  assert(count <= getLength() - beginIndex);
1686  return std::u16string_view(pData->buffer, sal_uInt32(pData->length)).substr(beginIndex, count);
1687  }
1688 #endif
1689 
1701  OUStringBuffer copy( sal_Int32 beginIndex ) const
1702  {
1703  return copy( beginIndex, getLength() - beginIndex );
1704  }
1705 
1719  OUStringBuffer copy( sal_Int32 beginIndex, sal_Int32 count ) const
1720  {
1721  assert(beginIndex >= 0 && beginIndex <= getLength());
1722  assert(count >= 0 && count <= getLength() - beginIndex);
1723  rtl_uString *pNew = NULL;
1724  rtl_uStringbuffer_newFromStr_WithLength( &pNew, getStr() + beginIndex, count );
1725  return OUStringBuffer( pNew, count + 16 );
1726  }
1727 
1728 private:
1729  OUStringBuffer( rtl_uString * value, const sal_Int32 capacity )
1730  {
1731  pData = value;
1732  nCapacity = capacity;
1733  }
1734 
1738  rtl_uString * pData;
1739 
1743  sal_Int32 nCapacity;
1744 };
1745 
1746 #if defined LIBO_INTERNAL_ONLY
1747 template<> struct ToStringHelper<OUStringBuffer> {
1748  static std::size_t length(OUStringBuffer const & s) { return s.getLength(); }
1749 
1750  sal_Unicode * operator()(sal_Unicode * buffer, OUStringBuffer const & s) const SAL_RETURNS_NONNULL
1751  { return addDataHelper(buffer, s.getStr(), s.getLength()); }
1752 };
1753 #endif
1754 
1755 #if defined LIBO_INTERNAL_ONLY
1756  // Define this here to avoid circular includes
1757  inline OUString & OUString::operator+=( const OUStringBuffer & str ) &
1758  {
1759  // Call operator= if this is empty, otherwise rtl_uString_newConcat will attempt to
1760  // acquire() the str.pData buffer, which is part of the OUStringBuffer mutable state.
1761  if (isEmpty())
1762  return operator=(str.toString());
1763  else
1764  return internalAppend(str.pData);
1765  }
1766 
1767  inline OUString const& OUString::unacquired(const OUStringBuffer& str)
1768  {
1769  return unacquired(&str.pData);
1770  }
1771 #endif
1772 }
1773 
1774 #ifdef RTL_STRING_UNITTEST
1775 namespace rtl
1776 {
1777 typedef rtlunittest::OUStringBuffer OUStringBuffer;
1778 }
1779 #endif
1780 
1781 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
1782 using ::rtl::OUStringBuffer;
1783 #endif
1784 
1785 #endif // INCLUDED_RTL_USTRBUF_HXX
1786 
1787 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString & operator+=(const OUString &str)
Append a string to this string.
Definition: ustring.hxx:655
SAL_DLLPUBLIC void rtl_uStringbuffer_remove(rtl_uString **This, sal_Int32 start, sal_Int32 len)
Removes the characters in a substring of this sequence.
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:657
OUStringBuffer & append(sal_Bool b)
Appends the string representation of the sal_Bool argument to the string buffer.
Definition: ustrbuf.hxx:806
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:861
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.
OUStringBuffer & insert(sal_Int32 offset, sal_Bool b)
Inserts the string representation of the sal_Bool argument into this string buffer.
Definition: ustrbuf.hxx:1092
OUStringBuffer(const OUString &value)
Constructs a string buffer so that it represents the same sequence of characters as the string argume...
Definition: ustrbuf.hxx:134
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:1463
OUStringBuffer & insert(sal_Int32 offset, const OUString &str)
Inserts the string into this string buffer.
Definition: ustrbuf.hxx:992
unsigned char sal_Bool
Definition: types.h:18
OUStringBuffer & append(double d)
Appends the string representation of the double argument to this string buffer.
Definition: ustrbuf.hxx:913
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 sal_Int32 rtl_ustr_getLength(const sal_Unicode *str) SAL_THROW_EXTERN_C()
Return the length of a string.
OUStringBuffer & insertUtf32(sal_Int32 offset, sal_uInt32 c)
Inserts a single UTF-32 character into this string buffer.
Definition: ustrbuf.hxx:1281
void setLength(sal_Int32 newLength)
Sets the length of this String buffer.
Definition: ustrbuf.hxx:466
OUStringBuffer & insert(sal_Int32 offset, bool b)
Inserts the string representation of the bool argument into this string buffer.
Definition: ustrbuf.hxx:1117
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:1547
OUStringBuffer & append(const OUString &str)
Appends the string to this string buffer.
Definition: ustrbuf.hxx:573
This String class provides base functionality for C++ like Unicode character array handling...
Definition: ustring.hxx:182
OUStringBuffer & insert(sal_Int32 offset, double d)
Inserts the string representation of the double argument into this string buffer. ...
Definition: ustrbuf.hxx:1260
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.
#define RTL_USTR_MAX_VALUEOFBOOLEAN
Definition: ustring.h:899
Definition: stringutils.hxx:374
void accessInternals(rtl_uString ***pInternalData, sal_Int32 **pInternalCapacity)
Allows access to the internal data of this OUStringBuffer, for effective manipulation.
Definition: ustrbuf.hxx:1355
OUStringBuffer(const OUStringBuffer &value)
Allocates a new string buffer that contains the same sequence of characters as the string buffer argu...
Definition: ustrbuf.hxx:72
sal_Int32 getLength() const
Returns the length (character count) of this string buffer.
Definition: ustrbuf.hxx:399
OUStringBuffer(sal_Int32 length)
Constructs a string buffer with no characters in it and an initial capacity specified by the length a...
Definition: ustrbuf.hxx:85
sal_uInt16 sal_Unicode
Definition: types.h:103
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:1038
sal_Int32 stripStart(sal_Unicode c= ' ')
Strip the given character from the start of the buffer.
Definition: ustrbuf.hxx:1593
OUString toString() const
Return an OUString instance reflecting the current content of this OUStringBuffer.
Definition: ustrbuf.hxx:557
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 void rtl_uString_release(rtl_uString *str) SAL_THROW_EXTERN_C() SAL_HOT
Decrement the reference count of 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.
OUStringBuffer & insert(sal_Int32 offset, sal_Unicode c)
Inserts the string representation of the char argument into this string buffer.
Definition: ustrbuf.hxx:1163
static OUString const & unacquired(rtl_uString *const *ppHandle)
Provides an OUString const &amp; passing a storage pointer of an rtl_uString * handle.
Definition: ustring.hxx:520
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.
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 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:454
const sal_Unicode * getStr() const SAL_RETURNS_NONNULL
Returns a pointer to the Unicode character buffer for this string.
Definition: ustring.hxx:806
OUStringBuffer & appendAscii(const char *str)
Appends a 8-Bit ASCII character string to this string buffer.
Definition: ustrbuf.hxx:728
sal_Int32 getCapacity() const
Returns the current capacity of the String buffer.
Definition: ustrbuf.hxx:427
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:567
OUStringBuffer & appendUtf32(sal_uInt32 c)
Appends a single UTF-32 character to this string buffer.
Definition: ustrbuf.hxx:932
pData
Definition: ustring.hxx:334
SAL_DLLPUBLIC sal_Int32 rtl_ustr_valueOfFloat(sal_Unicode *str, float f) SAL_THROW_EXTERN_C()
Create the string representation of a float.
#define RTL_USTR_MAX_VALUEOFINT64
Definition: ustring.h:964
OUStringBuffer & append(const OUStringBuffer &str)
Appends the content of a stringbuffer to this string buffer.
Definition: ustrbuf.hxx:599
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:951
OUStringBuffer copy(sal_Int32 beginIndex, sal_Int32 count) const
Returns a new string buffer that is a substring of this string.
Definition: ustrbuf.hxx:1719
OUStringBuffer copy(sal_Int32 beginIndex) const
Returns a new string buffer that is a substring of this string.
Definition: ustrbuf.hxx:1701
Definition: stringutils.hxx:142
bool isEmpty() const
Checks if a string buffer is empty.
Definition: ustrbuf.hxx:412
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_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:1513
SAL_DLLPUBLIC void rtl_uString_new(rtl_uString **newStr) SAL_THROW_EXTERN_C()
Allocate a new string containing no characters.
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.
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:1212
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:1415
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_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:1378
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.
definition of a no acquire enum for ctors
Definition: types.h:336
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:1051
OUStringBuffer()
Constructs a string buffer with no characters in it and an initial capacity of 16 characters...
Definition: ustrbuf.hxx:59
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...
#define SAL_WARN_UNUSED_RESULT
Use this as markup for functions and methods whose return value must be checked.
Definition: types.h:264
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:1447
std::basic_ostream< charT, traits > & operator<<(std::basic_ostream< charT, traits > &stream, OString const &rString)
Support for rtl::OString in std::ostream (and thus in CPPUNIT_ASSERT or SAL_INFO macros, for example).
Definition: string.hxx:2338
#define RTL_USTR_MAX_VALUEOFDOUBLE
Definition: ustring.h:1025
sal_Int32 strip(sal_Unicode c= ' ')
Strip the given character from the both end of the buffer.
Definition: ustrbuf.hxx:1645
void ensureCapacity(sal_Int32 minimumCapacity)
Ensures that the capacity of the buffer is at least equal to the specified minimum.
Definition: ustrbuf.hxx:443
SAL_WARN_UNUSED_RESULT OUString makeStringAndClear()
Fill the string data in the new string and clear the buffer.
Definition: ustrbuf.hxx:387
OUStringBuffer & append(sal_Int64 l, sal_Int16 radix=10)
Appends the string representation of the long argument to this string buffer.
Definition: ustrbuf.hxx:879
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_Int32 getLength() const
Returns the length of this string.
Definition: ustring.hxx:784
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:1015
OUStringBuffer & append(float f)
Appends the string representation of the float argument to this string buffer.
Definition: ustrbuf.hxx:896
~OUStringBuffer()
Release the string data.
Definition: ustrbuf.hxx:374
OUStringBuffer & append(sal_Unicode c)
Appends the string representation of the char argument to this string buffer.
Definition: ustrbuf.hxx:840
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:644
OUStringBuffer & append(bool b)
Appends the string representation of the bool argument to the string buffer.
Definition: ustrbuf.hxx:769
OUStringBuffer & replace(sal_Unicode oldChar, sal_Unicode newChar)
Replace all occurrences of oldChar in this string buffer with newChar.
Definition: ustrbuf.hxx:1330
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.
#define RTL_USTR_MAX_VALUEOFINT32
Definition: ustring.h:941
Definition: stringutils.hxx:140
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_valueOfBoolean(sal_Unicode *str, sal_Bool b) SAL_THROW_EXTERN_C()
Create the string representation of a boolean.
sal_Int32 stripEnd(sal_Unicode c= ' ')
Strip the given character from the end of the buffer.
Definition: ustrbuf.hxx:1619
OUStringBuffer & insert(sal_Int32 offset, char c)
Inserts the string representation of the char argument into this string buffer.
Definition: ustrbuf.hxx:1141
SAL_DLLPUBLIC sal_Int32 rtl_str_getLength(const char *str) SAL_THROW_EXTERN_C()
Return the length of a string.
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:1187
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.
OUStringBuffer & appendAscii(const char *str, sal_Int32 len)
Appends a 8-Bit ASCII character string to this string buffer.
Definition: ustrbuf.hxx:750
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:1561
OUStringBuffer insert(sal_Int32 offset, float f)
Inserts the string representation of the float argument into this string buffer.
Definition: ustrbuf.hxx:1236
const sal_Unicode * getStr() const SAL_RETURNS_NONNULL
Return a null terminated unicode character array.
Definition: ustrbuf.hxx:521
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.
OUStringBuffer & truncate(sal_Int32 start=0)
Removes the tail of a string buffer start at the indicate position.
Definition: ustrbuf.hxx:1314
#define RTL_USTR_MAX_VALUEOFFLOAT
Definition: ustring.h:1006
SAL_DLLPUBLIC void rtl_uString_newFromLiteral(rtl_uString **newStr, const char *value, sal_Int32 len, sal_Int32 allocExtra) SAL_THROW_EXTERN_C()
OUStringBuffer & append(const sal_Unicode *str)
Appends the string representation of the char array argument to this string buffer.
Definition: ustrbuf.hxx:625
OUStringBuffer & append(char c)
Appends the string representation of the ASCII char argument to this string buffer.
Definition: ustrbuf.hxx:824
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:1396
A string buffer implements a mutable sequence of characters.
Definition: ustrbuf.hxx:51
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:358
SAL_DLLPUBLIC sal_Int32 rtl_ustr_valueOfDouble(sal_Unicode *str, double d) SAL_THROW_EXTERN_C()
Create the string representation of a double.