LibreOfficeDev
LibreOfficeDev 26.8 SDK C/C++ API Reference
ustrbuf.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 /*
21  * This file is part of LibreOffice published API.
22  */
23 
24 #ifndef INCLUDED_RTL_USTRBUF_HXX
25 #define INCLUDED_RTL_USTRBUF_HXX
26 
27 #include "sal/config.h"
28 
29 #include <cassert>
30 #include <cstring>
31 #include <limits>
32 #include <new>
33 
34 #if defined LIBO_INTERNAL_ONLY
35 #include <string_view>
36 #include <type_traits>
37 #include <utility>
38 #endif
39 
40 #include "rtl/ustrbuf.h"
41 #include "rtl/ustring.hxx"
42 #include "rtl/stringutils.hxx"
43 #include "sal/types.h"
44 
45 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
46 #include "o3tl/safeint.hxx"
47 #include "rtl/stringconcat.hxx"
48 #endif
49 
50 #ifdef RTL_STRING_UNITTEST
51 extern bool rtl_string_unittest_invalid_conversion;
52 #endif
53 
54 // The unittest uses slightly different code to help check that the proper
55 // calls are made. The class is put into a different namespace to make
56 // sure the compiler generates a different (if generating also non-inline)
57 // copy of the function and does not merge them together. The class
58 // is "brought" into the proper rtl namespace by a typedef below.
59 #ifdef RTL_STRING_UNITTEST
60 #define rtl rtlunittest
61 #endif
62 
63 namespace rtl
64 {
65 
66 #ifdef RTL_STRING_UNITTEST
67 #undef rtl
68 #endif
69 
73 {
74 friend class OUString;
75 public:
81  : pData(NULL)
82  , nCapacity( 16 )
83  {
84  rtl_uString_new_WithLength( &pData, nCapacity );
85  }
86 
93  OUStringBuffer( const OUStringBuffer & value )
94  : pData(NULL)
95  , nCapacity( value.nCapacity )
96  {
97  rtl_uStringbuffer_newFromStringBuffer( &pData, value.nCapacity, value.pData );
98  }
99 
106  explicit OUStringBuffer(sal_Int32 length)
107  : pData(NULL)
108  , nCapacity( length )
109  {
110  rtl_uString_new_WithLength( &pData, length );
111  }
112 #if defined LIBO_INTERNAL_ONLY
113  template<typename T>
114  explicit OUStringBuffer(T length, std::enable_if_t<std::is_integral_v<T>, int> = 0)
115  : OUStringBuffer(static_cast<sal_Int32>(length))
116  {
117  assert(libreoffice_internal::IsValidStrLen(length));
118  }
119  // avoid (obvious) bugs
120  explicit OUStringBuffer(bool) = delete;
121  explicit OUStringBuffer(char) = delete;
122  explicit OUStringBuffer(wchar_t) = delete;
123 #if !(defined _MSC_VER && _MSC_VER >= 1930 && _MSC_VER <= 1939 && defined _MANAGED)
124  explicit OUStringBuffer(char8_t) = delete;
125 #endif
126  explicit OUStringBuffer(char16_t) = delete;
127  explicit OUStringBuffer(char32_t) = delete;
128 #endif
129 
140 #if defined LIBO_INTERNAL_ONLY
141  OUStringBuffer(std::u16string_view sv)
142  : pData(nullptr)
143  , nCapacity(libreoffice_internal::ThrowIfInvalidStrLen(sv.length(), 16) + 16)
144  {
145  rtl_uStringbuffer_newFromStr_WithLength( &pData, sv.data(), sv.length() );
146  }
147 #else
148  OUStringBuffer(const OUString& value)
149  : pData(NULL)
150  , nCapacity( value.getLength() + 16 )
151  {
152  rtl_uStringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
153  }
154 #endif
155 
156  template< typename T >
158  : pData(NULL)
159  , nCapacity( libreoffice_internal::ConstCharArrayDetector<T>::length + 16 )
160  {
161  assert(
164  &pData,
167 #ifdef RTL_STRING_UNITTEST
168  rtl_string_unittest_const_literal = true;
169 #endif
170  }
171 
172 #if defined LIBO_INTERNAL_ONLY
174  template<typename T>
176  T & literal,
178  T, libreoffice_internal::Dummy>::TypeUtf16
180  pData(nullptr),
181  nCapacity(libreoffice_internal::ConstCharArrayDetector<T>::length + 16)
182  {
184  &pData,
187  }
188 #endif
189 
190 #if defined LIBO_INTERNAL_ONLY && defined RTL_STRING_UNITTEST
192 
196  template< typename T >
197  OUStringBuffer( T&, typename libreoffice_internal::ExceptConstCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
198  {
199  pData = NULL;
200  nCapacity = 10;
201  rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
202  rtl_string_unittest_invalid_conversion = true;
203  }
208  template< typename T >
209  OUStringBuffer( const T&, typename libreoffice_internal::ExceptCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
210  {
211  pData = NULL;
212  nCapacity = 10;
213  rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
214  rtl_string_unittest_invalid_conversion = true;
215  }
217 #endif
218 
219 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
224  template< typename T1, typename T2 >
225  OUStringBuffer( OUStringConcat< T1, T2 >&& c )
226  {
227  const sal_Int32 l = c.length();
228  nCapacity = l + 16;
229  pData = rtl_uString_alloc( nCapacity );
230  sal_Unicode* end = c.addData( pData->buffer );
231  *end = '\0';
232  pData->length = l;
233  }
234 
239  template< std::size_t N >
240  OUStringBuffer( OUStringNumber< N >&& n )
241  : OUStringBuffer(std::u16string_view(n))
242  {
243  }
244 #endif
245 
246 #if defined LIBO_INTERNAL_ONLY
247  operator std::u16string_view() const { return {getStr(), sal_uInt32(getLength())}; }
248 #endif
249 
252  OUStringBuffer& operator = ( const OUStringBuffer& value )
253  {
254  if (this != &value)
255  {
257  value.nCapacity,
258  value.pData);
259  nCapacity = value.nCapacity;
260  }
261  return *this;
262  }
263 
264 #if defined LIBO_INTERNAL_ONLY
268  OUStringBuffer& operator = ( OUStringBuffer&& value ) noexcept
269  {
270  rtl_uString_release( pData );
271  pData = value.pData;
272  nCapacity = value.nCapacity;
273  value.pData = nullptr;
274  value.nCapacity = 0;
275  rtl_uString_new( &value.pData );
276  return *this;
277  }
278 #endif
279 
284 #if defined LIBO_INTERNAL_ONLY
285  OUStringBuffer & operator =(std::u16string_view string) {
286  sal_Int32 n = string.length();
287  if (n >= nCapacity) {
288  ensureCapacity(n + 16); //TODO: check for overflow
289  }
290  std::memcpy(
291  pData->buffer, string.data(),
292  n * sizeof (sal_Unicode));
293  pData->buffer[n] = '\0';
294  pData->length = n;
295  return *this;
296  }
297 #else
298  OUStringBuffer & operator =(OUString const & string) {
299  sal_Int32 n = string.getLength();
300  if (n >= nCapacity) {
301  ensureCapacity(n + 16); //TODO: check for overflow
302  }
303  std::memcpy(
304  pData->buffer, string.pData->buffer,
305  (n + 1) * sizeof (sal_Unicode));
306  pData->length = n;
307  return *this;
308  }
309 #endif
310 
315  template<typename T>
316  typename
318  operator =(T & literal) {
319  assert(
321  sal_Int32 const n
323  if (n >= nCapacity) {
324  ensureCapacity(n + 16); //TODO: check for overflow
325  }
326  char const * from
328  literal);
329  sal_Unicode * to = pData->buffer;
330  for (sal_Int32 i = 0; i <= n; ++i) {
331  to[i] = from[i];
332  }
333  pData->length = n;
334  return *this;
335  }
336 
337 #if defined LIBO_INTERNAL_ONLY
339  template<typename T>
341  T, OUStringBuffer &>::TypeUtf16
342  operator =(T & literal) {
343  return operator=(
346  }
347 #endif
348 
349 #if defined LIBO_INTERNAL_ONLY
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<std::size_t N>
364  OUStringBuffer & operator =(OUStringNumber<N> && n)
365  {
366  return operator =(std::u16string_view(n));
367  }
368 #endif
369 
374  {
375  rtl_uString_release( pData );
376  }
377 
387  {
388  return OUString(
389  rtl_uStringBuffer_makeStringAndClear( &pData, &nCapacity ),
390  SAL_NO_ACQUIRE );
391  }
392 
398  sal_Int32 getLength() const
399  {
400  return pData->length;
401  }
402 
411  bool isEmpty() const
412  {
413  return pData->length == 0;
414  }
415 
426  sal_Int32 getCapacity() const
427  {
428  return nCapacity;
429  }
430 
442  void ensureCapacity(sal_Int32 minimumCapacity)
443  {
444  rtl_uStringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
445  }
446 
465  void setLength(sal_Int32 newLength)
466  {
467  assert(newLength >= 0);
468  // Avoid modifications if pData points to const empty string:
469  if( newLength != pData->length )
470  {
471  if( newLength > nCapacity )
472  rtl_uStringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
473  else
474  pData->buffer[newLength] = 0;
475  pData->length = newLength;
476  }
477  }
478 
492  SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
493  sal_Unicode charAt( sal_Int32 index ) const
494  {
495  assert(index >= 0 && index < pData->length);
496  return pData->buffer[ index ];
497  }
498 
509  SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
510  OUStringBuffer & setCharAt(sal_Int32 index, sal_Unicode ch)
511  {
512  assert(index >= 0 && index < pData->length);
513  pData->buffer[ index ] = ch;
514  return *this;
515  }
516 
520  const sal_Unicode* getStr() const SAL_RETURNS_NONNULL { return pData->buffer; }
521 
522 #if defined LIBO_INTERNAL_ONLY
523  // Provide unsafe non-const access to the null-terminated string. Callers can mutate the
524  // contents of the string buffer (including introducing embedded null characters), but cannot
525  // modify its length.
526  sal_Unicode * getMutableStr() SAL_RETURNS_NONNULL { return pData->buffer; }
527 #endif
528 
538  sal_Unicode & operator [](sal_Int32 index)
539  {
540  assert(index >= 0 && index < pData->length);
541  return pData->buffer[index];
542  }
543 
553  const sal_Unicode & operator [](sal_Int32 index) const
554  {
555  assert(index >= 0 && index < pData->length);
556  return pData->buffer[index];
557  }
558 
564  {
565  return OUString(pData->buffer, pData->length);
566  }
567 
578 #if !defined LIBO_INTERNAL_ONLY
580 #else
581  OUStringBuffer & append(std::u16string_view str)
582 #endif
583  {
584  return insert(getLength(), str);
585  }
586 
587 #if !defined LIBO_INTERNAL_ONLY
601  {
602  if(!str.isEmpty())
603  {
604  append( str.getStr(), str.getLength() );
605  }
606  return *this;
607  }
608 #endif
609 
621 #if defined LIBO_INTERNAL_ONLY
622  template<typename T>
624  append(T const & str)
625 #else
627 #endif
628  {
629  return insert(getLength(), str);
630  }
631 
645  OUStringBuffer & append( const sal_Unicode * str, sal_Int32 len)
646  {
647  return insert(getLength(), str, len);
648  }
649 
655  template< typename T >
657  {
658  return insert(getLength(), literal);
659  }
660 
661 #if defined LIBO_INTERNAL_ONLY
662  template<typename T>
664  append(T & value) { return append(static_cast<sal_Unicode *>(value)); }
665 
667  template<typename T>
668  typename libreoffice_internal::ConstCharArrayDetector<
669  T, OUStringBuffer &>::TypeUtf16
670  append(T & literal) {
671  return insert(getLength(), literal);
672  }
673 #endif
674 
675 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
680  template< typename T1, typename T2 >
681  OUStringBuffer& append( OUStringConcat< T1, T2 >&& c )
682  {
683  return insert(getLength(), std::move(c));
684  }
685 #endif
686 
703  OUStringBuffer & appendAscii( const char * str )
704  {
705  return appendAscii( str, rtl_str_getLength( str ) );
706  }
707 
725  OUStringBuffer & appendAscii( const char * str, sal_Int32 len)
726  {
727  rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), str, len );
728  return *this;
729  }
730 
745  {
746  return insert(getLength(), b);
747  }
748 
750  // Pointer can be automatically converted to bool, which is unwanted here.
751  // Explicitly delete all pointer append() overloads to prevent this
752  // (except for char* and sal_Unicode* overloads, which are handled elsewhere).
753  template< typename T >
754  typename libreoffice_internal::Enable< void,
756  append( T* ) SAL_DELETED_FUNCTION;
758 
759  // This overload is needed because OUString has a ctor from rtl_uString*, but
760  // the bool overload above would be preferred to the conversion.
764  OUStringBuffer & append(rtl_uString* str)
765  {
766  return append( OUString::unacquired( &str ));
767  }
768 
781  {
782  return insert(getLength(), b);
783  }
784 
798  {
799  assert(static_cast< unsigned char >(c) <= 0x7F);
800  return insert(getLength(), c);
801  }
802 
814  {
815  return insert(getLength(), c);
816  }
817 
818 #if defined LIBO_INTERNAL_ONLY
819  void append(sal_uInt16) = delete;
820 #endif
821 
834  OUStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
835  {
836  return insert(getLength(), i, radix);
837  }
838 
851  OUStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
852  {
853  return insert(getLength(), l, radix);
854  }
855 
868  {
869  return insert(getLength(), f);
870  }
871 
883  OUStringBuffer & append(double d)
884  {
885  return insert(getLength(), d);
886  }
887 
901  OUStringBuffer & appendUtf32(sal_uInt32 c) {
902  return insertUtf32(getLength(), c);
903  }
904 
920  sal_Unicode * appendUninitialized(sal_Int32 length) SAL_RETURNS_NONNULL {
921  sal_Int32 n = getLength();
922  rtl_uStringbuffer_insert(&pData, &nCapacity, n, NULL, length);
923  return pData->buffer + n;
924  }
925 
926 #if defined LIBO_INTERNAL_ONLY
933  template<typename T>
934  OUStringBuffer& operator<<(T&& rValue)
935  {
936  return append(std::forward<T>(rValue));
937  }
938 #endif
939 
955 #if defined LIBO_INTERNAL_ONLY
956  OUStringBuffer & insert(sal_Int32 offset, std::u16string_view str)
957  {
958  return insert(offset, str.data(), libreoffice_internal::ThrowIfInvalidStrLen(str.length()));
959  }
960 #else
961  OUStringBuffer & insert(sal_Int32 offset, const OUString & str)
962  {
963  return insert( offset, str.getStr(), str.getLength() );
964  }
965 #endif
966 
967 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
972  template <typename T1, typename T2>
973  OUStringBuffer& insert(sal_Int32 offset, OUStringConcat<T1, T2>&& c)
974  {
975  const size_t l = c.length();
976  if (l == 0)
977  return *this;
978  if (l > o3tl::make_unsigned(std::numeric_limits<sal_Int32>::max() - pData->length))
979  throw std::bad_alloc();
980 
981  rtl_uStringbuffer_insert(&pData, &nCapacity, offset, nullptr, l);
982 
983  /* insert the new characters */
984  c.addData(pData->buffer + offset);
985  return *this;
986  }
987 #endif
988 
1006  OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str )
1007  {
1008  return insert( offset, str, rtl_ustr_getLength( str ) );
1009  }
1010 
1029  OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str, sal_Int32 len)
1030  {
1031  assert( len == 0 || str != NULL ); // cannot assert that in rtl_uStringbuffer_insert
1032  rtl_uStringbuffer_insert( &pData, &nCapacity, offset, str, len );
1033  return *this;
1034  }
1035 
1041  template< typename T >
1043  {
1044  assert(
1047  &pData, &nCapacity, offset,
1050  return *this;
1051  }
1052 
1053 #if defined LIBO_INTERNAL_ONLY
1055  template<typename T>
1057  T, OUStringBuffer &>::TypeUtf16
1058  insert(sal_Int32 offset, T & literal) {
1059  return insert(
1060  offset,
1063  }
1064 #endif
1065 
1083  OUStringBuffer & insert(sal_Int32 offset, sal_Bool b)
1084  {
1086  return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
1087  }
1088 
1108  OUStringBuffer & insert(sal_Int32 offset, bool b)
1109  {
1111  return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
1112  }
1113 
1132  OUStringBuffer & insert(sal_Int32 offset, char c)
1133  {
1134  return insert(offset, sal_Unicode(c));
1135  }
1136 
1153  OUStringBuffer & insert(sal_Int32 offset, sal_Unicode c)
1154  {
1155  return insert( offset, &c, 1 );
1156  }
1157 
1177  OUStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
1178  {
1180  return insert( offset, sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
1181  }
1182 
1202  OUStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
1203  {
1205  return insert( offset, sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
1206  }
1207 
1226  OUStringBuffer & insert(sal_Int32 offset, float f)
1227  {
1228  // Same as rtl::str::valueOfFP, used for rtl_ustr_valueOfFloat
1229  rtl_math_doubleToUString(&pData, &nCapacity, offset, f, rtl_math_StringFormat_G,
1230  RTL_USTR_MAX_VALUEOFFLOAT - SAL_N_ELEMENTS("-x.E-xxx") + 1, '.',
1231  NULL, 0, true);
1232  return *this;
1233  }
1234 
1253  OUStringBuffer & insert(sal_Int32 offset, double d)
1254  {
1255  // Same as rtl::str::valueOfFP, used for rtl_ustr_valueOfDouble
1256  rtl_math_doubleToUString(&pData, &nCapacity, offset, d, rtl_math_StringFormat_G,
1257  RTL_USTR_MAX_VALUEOFDOUBLE - SAL_N_ELEMENTS("-x.E-xxx") + 1, '.',
1258  NULL, 0, true);
1259  return *this;
1260  }
1261 
1277  OUStringBuffer & insertUtf32(sal_Int32 offset, sal_uInt32 c) {
1278  rtl_uStringbuffer_insertUtf32(&pData, &nCapacity, offset, c);
1279  return *this;
1280  }
1281 
1294  OUStringBuffer & remove( sal_Int32 start, sal_Int32 len )
1295  {
1296  rtl_uStringbuffer_remove( &pData, start, len );
1297  return *this;
1298  }
1299 
1310  OUStringBuffer & truncate( sal_Int32 start = 0 )
1311  {
1312  rtl_uStringbuffer_remove( &pData, start, getLength() - start );
1313  return *this;
1314  }
1315 
1327  {
1328  sal_Int32 index = 0;
1329  while((index = indexOf(oldChar, index)) >= 0)
1330  {
1331  pData->buffer[ index ] = newChar;
1332  }
1333  return *this;
1334  }
1335 
1351  void accessInternals(rtl_uString *** pInternalData,
1352  sal_Int32 ** pInternalCapacity)
1353  {
1354  *pInternalData = &pData;
1355  *pInternalCapacity = &nCapacity;
1356  }
1357 
1358 
1374  sal_Int32 indexOf( sal_Unicode ch, sal_Int32 fromIndex = 0 ) const
1375  {
1376  assert( fromIndex >= 0 && fromIndex <= pData->length );
1377  sal_Int32 ret = rtl_ustr_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch );
1378  return (ret < 0 ? ret : ret+fromIndex);
1379  }
1380 
1392  sal_Int32 lastIndexOf( sal_Unicode ch ) const
1393  {
1394  return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch );
1395  }
1396 
1411  sal_Int32 lastIndexOf( sal_Unicode ch, sal_Int32 fromIndex ) const
1412  {
1413  assert( fromIndex >= 0 && fromIndex <= pData->length );
1414  return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch );
1415  }
1416 
1434 #if defined LIBO_INTERNAL_ONLY
1435  sal_Int32 indexOf( std::u16string_view str, sal_Int32 fromIndex = 0 ) const
1436  {
1437  assert( fromIndex >= 0 && fromIndex <= pData->length );
1438  sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1439  str.data(), str.length() );
1440  return (ret < 0 ? ret : ret+fromIndex);
1441  }
1442 #else
1443  sal_Int32 indexOf( const OUString & str, sal_Int32 fromIndex = 0 ) const
1444  {
1445  assert( fromIndex >= 0 && fromIndex <= pData->length );
1446  sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1447  str.pData->buffer, str.pData->length );
1448  return (ret < 0 ? ret : ret+fromIndex);
1449  }
1450 #endif
1451 
1458  template< typename T >
1459  typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const
1460  {
1461  assert(
1463  sal_Int32 n = rtl_ustr_indexOfAscii_WithLength(
1464  pData->buffer + fromIndex, pData->length - fromIndex,
1467  return n < 0 ? n : n + fromIndex;
1468  }
1469 
1470 #if defined LIBO_INTERNAL_ONLY
1472  template<typename T>
1473  typename
1475  indexOf(T & literal, sal_Int32 fromIndex = 0) const {
1476  return indexOf(
1479  fromIndex);
1480  }
1481 #endif
1482 
1500 #if defined LIBO_INTERNAL_ONLY
1501  sal_Int32 lastIndexOf( std::u16string_view str ) const
1502  {
1503  return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1504  str.data(), str.length() );
1505  }
1506 #else
1507  sal_Int32 lastIndexOf( const OUString & str ) const
1508  {
1509  return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1510  str.pData->buffer, str.pData->length );
1511  }
1512 #endif
1513 
1533 #if defined LIBO_INTERNAL_ONLY
1534  sal_Int32 lastIndexOf( std::u16string_view str, sal_Int32 fromIndex ) const
1535  {
1536  assert( fromIndex >= 0 && fromIndex <= pData->length );
1537  return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1538  str.data(), str.length() );
1539  }
1540 #else
1541  sal_Int32 lastIndexOf( const OUString & str, sal_Int32 fromIndex ) const
1542  {
1543  assert( fromIndex >= 0 && fromIndex <= pData->length );
1544  return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1545  str.pData->buffer, str.pData->length );
1546  }
1547 #endif
1548 
1554  template< typename T >
1556  {
1557  assert(
1560  pData->buffer, pData->length,
1563  }
1564 
1565 #if defined LIBO_INTERNAL_ONLY
1567  template<typename T>
1568  typename
1570  lastIndexOf(T & literal) const {
1571  return lastIndexOf(
1574  }
1575 #endif
1576 
1586  sal_Int32 stripStart(sal_Unicode c = ' ')
1587  {
1588  sal_Int32 index;
1589  for(index = 0; index < getLength() ; index++)
1590  {
1591  if(pData->buffer[ index ] != c)
1592  {
1593  break;
1594  }
1595  }
1596  if(index)
1597  {
1598  remove(0, index);
1599  }
1600  return index;
1601  }
1602 
1612  sal_Int32 stripEnd(sal_Unicode c = ' ')
1613  {
1614  sal_Int32 result = getLength();
1615  sal_Int32 index;
1616  for(index = getLength(); index > 0 ; index--)
1617  {
1618  if(pData->buffer[ index - 1 ] != c)
1619  {
1620  break;
1621  }
1622  }
1623  if(index < getLength())
1624  {
1625  truncate(index);
1626  }
1627  return result - getLength();
1628  }
1638  sal_Int32 strip(sal_Unicode c = ' ')
1639  {
1640  return stripStart(c) + stripEnd(c);
1641  }
1642 
1643 #if defined LIBO_INTERNAL_ONLY
1654  SAL_WARN_UNUSED_RESULT std::u16string_view subView( sal_Int32 beginIndex ) const
1655  {
1656  assert(beginIndex >= 0);
1657  assert(beginIndex <= getLength());
1658  return subView(beginIndex, getLength() - beginIndex);
1659  }
1660 
1673  SAL_WARN_UNUSED_RESULT std::u16string_view subView( sal_Int32 beginIndex, sal_Int32 count ) const
1674  {
1675  assert(beginIndex >= 0);
1676  assert(count >= 0);
1677  assert(beginIndex <= getLength());
1678  assert(count <= getLength() - beginIndex);
1679  return std::u16string_view(*this).substr(beginIndex, count);
1680  }
1681 #endif
1682 
1694  OUStringBuffer copy( sal_Int32 beginIndex ) const
1695  {
1696  return copy( beginIndex, getLength() - beginIndex );
1697  }
1698 
1712  OUStringBuffer copy( sal_Int32 beginIndex, sal_Int32 count ) const
1713  {
1714  assert(beginIndex >= 0 && beginIndex <= getLength());
1715  assert(count >= 0 && count <= getLength() - beginIndex);
1716  rtl_uString *pNew = NULL;
1717  rtl_uStringbuffer_newFromStr_WithLength( &pNew, getStr() + beginIndex, count );
1718  return OUStringBuffer( pNew, count + 16 );
1719  }
1720 
1721 private:
1722  OUStringBuffer( rtl_uString * value, const sal_Int32 capacity )
1723  {
1724  pData = value;
1725  nCapacity = capacity;
1726  }
1727 
1731  rtl_uString * pData;
1732 
1736  sal_Int32 nCapacity;
1737 };
1738 
1739 #if defined LIBO_INTERNAL_ONLY
1740 template<> struct ToStringHelper<OUStringBuffer> {
1741  static std::size_t length(OUStringBuffer const & s) { return s.getLength(); }
1742 
1743  sal_Unicode * operator()(sal_Unicode * buffer, OUStringBuffer const & s) const SAL_RETURNS_NONNULL
1744  { return addDataHelper(buffer, s.getStr(), s.getLength()); }
1745 };
1746 #endif
1747 
1748 #if defined LIBO_INTERNAL_ONLY
1749  // Define this here to avoid circular includes
1750  inline OUString & OUString::operator+=( const OUStringBuffer & str ) &
1751  {
1752  // Call operator= if this is empty, otherwise rtl_uString_newConcat will attempt to
1753  // acquire() the str.pData buffer, which is part of the OUStringBuffer mutable state.
1754  if (isEmpty())
1755  return operator=(str.toString());
1756  else
1757  return internalAppend(str.pData);
1758  }
1759 
1760  inline OUString const& OUString::unacquired(const OUStringBuffer& str)
1761  {
1762  return unacquired(&str.pData);
1763  }
1764 #endif
1765 }
1766 
1767 #ifdef RTL_STRING_UNITTEST
1768 namespace rtl
1769 {
1770 typedef rtlunittest::OUStringBuffer OUStringBuffer;
1771 }
1772 #endif
1773 
1774 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
1775 using ::rtl::OUStringBuffer;
1776 #endif
1777 
1778 #endif // INCLUDED_RTL_USTRBUF_HXX
1779 
1780 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define SAL_N_ELEMENTS(arr)
Definition: macros.h:51
#define SAL_DEPRECATED(message)
Use as follows: SAL_DEPRECATED("Don't use, it's evil.") void doit(int nPara);.
Definition: types.h:492
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:396
@ SAL_NO_ACQUIRE
definition of a no acquire enum for ctors
Definition: types.h:374
unsigned char sal_Bool
Definition: types.h:38
sal_uInt16 sal_Unicode
Definition: types.h:123
#define SAL_WARN_UNUSED_RESULT
Use this as markup for functions and methods whose return value must be used.
Definition: types.h:288
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:611
SAL_DLLPUBLIC void rtl_math_doubleToUString(rtl_uString **pResult, sal_Int32 *pResultCapacity, sal_Int32 nResultOffset, double fValue, enum rtl_math_StringFormat eFormat, sal_Int32 nDecPlaces, sal_Unicode cDecSeparator, sal_Int32 const *pGroups, sal_Unicode cGroupSeparator, sal_Bool bEraseTrailingDecZeros) SAL_THROW_EXTERN_C()
Conversions analogous to sprintf() using internal rounding.
@ rtl_math_StringFormat_G
Like sprintf() G, 'F' or 'E' format is used depending on which one is more compact.
Definition: math.h:53
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 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 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 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.
SAL_DLLPUBLIC void rtl_uString_new(rtl_uString **newStr) SAL_THROW_EXTERN_C()
Allocate a new string containing no characters.
#define RTL_USTR_MAX_VALUEOFFLOAT
Definition: ustring.h:1026
#define RTL_USTR_MAX_VALUEOFDOUBLE
Definition: ustring.h:1045
SAL_DLLPUBLIC void rtl_uString_newFromLiteral(rtl_uString **newStr, const char *value, sal_Int32 len, sal_Int32 allocExtra) SAL_THROW_EXTERN_C()
SAL_DLLPUBLIC sal_Int32 rtl_ustr_valueOfBoolean(sal_Unicode *str, sal_Bool b) SAL_THROW_EXTERN_C()
Create the string representation of a boolean.
#define RTL_USTR_MAX_VALUEOFINT64
Definition: ustring.h:984
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 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 void rtl_uString_new_WithLength(rtl_uString **newStr, sal_Int32 nLen) SAL_THROW_EXTERN_C()
Allocate a new string containing space for a given number of characters.
SAL_DLLPUBLIC sal_Int32 rtl_ustr_lastIndexOfChar_WithLength(const sal_Unicode *str, sal_Int32 len, sal_Unicode ch) SAL_THROW_EXTERN_C()
Search for the last occurrence of a character within a string.
#define RTL_USTR_MAX_VALUEOFBOOLEAN
Definition: ustring.h:919
SAL_DLLPUBLIC sal_Int32 rtl_ustr_valueOfInt32(sal_Unicode *str, sal_Int32 i, sal_Int16 radix) SAL_THROW_EXTERN_C()
Create the string representation of an integer.
SAL_DLLPUBLIC sal_Int32 rtl_ustr_lastIndexOfStr_WithLength(const sal_Unicode *str, sal_Int32 len, const sal_Unicode *subStr, sal_Int32 subLen) SAL_THROW_EXTERN_C()
Search for the last occurrence of a substring within a string.
SAL_DLLPUBLIC sal_Int32 rtl_ustr_indexOfChar_WithLength(const sal_Unicode *str, sal_Int32 len, sal_Unicode ch) SAL_THROW_EXTERN_C()
Search for the first occurrence of a character within a string.
#define RTL_USTR_MAX_VALUEOFINT32
Definition: ustring.h:961
Definition: bootstrap.hxx:34
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,...
Definition: string.hxx:2834
Definition: stringutils.hxx:178
Definition: stringutils.hxx:181
Definition: stringutils.hxx:397
A string buffer implements a mutable sequence of characters.
Definition: ustrbuf.hxx:73
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:1411
sal_Int32 stripStart(sal_Unicode c=' ')
Strip the given character from the start of the buffer.
Definition: ustrbuf.hxx:1586
OUStringBuffer & appendUtf32(sal_uInt32 c)
Appends a single UTF-32 character to this string buffer.
Definition: ustrbuf.hxx:901
OUStringBuffer copy(sal_Int32 beginIndex) const
Returns a new string buffer that is a substring of this string.
Definition: ustrbuf.hxx:1694
OUStringBuffer & append(const OUStringBuffer &str)
Appends the content of a stringbuffer to this string buffer.
Definition: ustrbuf.hxx:600
OUStringBuffer & replace(sal_Unicode oldChar, sal_Unicode newChar)
Replace all occurrences of oldChar in this string buffer with newChar.
Definition: ustrbuf.hxx:1326
OUStringBuffer & insert(sal_Int32 offset, float f)
Inserts the string representation of the float argument into this string buffer.
Definition: ustrbuf.hxx:1226
bool isEmpty() const
Checks if a string buffer is empty.
Definition: ustrbuf.hxx:411
OUStringBuffer & insert(sal_Int32 offset, const OUString &str)
Inserts the string into this string buffer.
Definition: ustrbuf.hxx:961
OUStringBuffer copy(sal_Int32 beginIndex, sal_Int32 count) const
Returns a new string buffer that is a substring of this string.
Definition: ustrbuf.hxx:1712
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:1006
sal_Int32 getCapacity() const
Returns the current capacity of the String buffer.
Definition: ustrbuf.hxx:426
sal_Int32 getLength() const
Returns the length (character count) of this string buffer.
Definition: ustrbuf.hxx:398
OUStringBuffer & append(rtl_uString *str)
Definition: ustrbuf.hxx:764
OUStringBuffer & append(float f)
Appends the string representation of the float argument to this string buffer.
Definition: ustrbuf.hxx:867
OUStringBuffer & append(double d)
Appends the string representation of the double argument to this string buffer.
Definition: ustrbuf.hxx:883
OUStringBuffer & append(const sal_Unicode *str)
Appends the string representation of the char array argument to this string buffer.
Definition: ustrbuf.hxx:626
OUStringBuffer & appendAscii(const char *str, sal_Int32 len)
Appends a 8-Bit ASCII character string to this string buffer.
Definition: ustrbuf.hxx:725
sal_Int32 strip(sal_Unicode c=' ')
Strip the given character from the both end of the buffer.
Definition: ustrbuf.hxx:1638
OUStringBuffer()
Constructs a string buffer with no characters in it and an initial capacity of 16 characters.
Definition: ustrbuf.hxx:80
OUStringBuffer & insertUtf32(sal_Int32 offset, sal_uInt32 c)
Inserts a single UTF-32 character into this string buffer.
Definition: ustrbuf.hxx:1277
OUStringBuffer & append(sal_Int64 l, sal_Int16 radix=10)
Appends the string representation of the long argument to this string buffer.
Definition: ustrbuf.hxx:851
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:1443
const sal_Unicode * getStr() const SAL_RETURNS_NONNULL
Return a null terminated unicode character array.
Definition: ustrbuf.hxx:520
OUStringBuffer & append(const OUString &str)
Appends the string to this string buffer.
Definition: ustrbuf.hxx:579
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:1202
OUStringBuffer & appendAscii(const char *str)
Appends a 8-Bit ASCII character string to this string buffer.
Definition: ustrbuf.hxx:703
void setLength(sal_Int32 newLength)
Sets the length of this String buffer.
Definition: ustrbuf.hxx:465
OUStringBuffer(const OUStringBuffer &value)
Allocates a new string buffer that contains the same sequence of characters as the string buffer argu...
Definition: ustrbuf.hxx:93
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:1029
OUStringBuffer(T &literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type=libreoffice_internal::Dummy())
Definition: ustrbuf.hxx:157
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:1374
OUStringBuffer & insert(sal_Int32 offset, double d)
Inserts the string representation of the double argument into this string buffer.
Definition: ustrbuf.hxx:1253
OUString toString() const
Return an OUString instance reflecting the current content of this OUStringBuffer.
Definition: ustrbuf.hxx:563
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:645
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:106
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:1541
sal_Int32 stripEnd(sal_Unicode c=' ')
Strip the given character from the end of the buffer.
Definition: ustrbuf.hxx:1612
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:1177
OUStringBuffer & append(sal_Unicode c)
Appends the string representation of the char argument to this string buffer.
Definition: ustrbuf.hxx:813
OUStringBuffer & remove(sal_Int32 start, sal_Int32 len)
Removes the characters in a substring of this sequence.
Definition: ustrbuf.hxx:1294
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:1459
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:656
OUStringBuffer & insert(sal_Int32 offset, char c)
Inserts the string representation of the char argument into this string buffer.
Definition: ustrbuf.hxx:1132
OUStringBuffer & truncate(sal_Int32 start=0)
Removes the tail of a string buffer start at the indicate position.
Definition: ustrbuf.hxx:1310
sal_Int32 lastIndexOf(sal_Unicode ch) const
Returns the index within this string of the last occurrence of the specified character,...
Definition: ustrbuf.hxx:1392
OUStringBuffer(const OUString &value)
Constructs a string buffer so that it represents the same sequence of characters as the string argume...
Definition: ustrbuf.hxx:148
~OUStringBuffer()
Release the string data.
Definition: ustrbuf.hxx:373
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:1042
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:1555
void ensureCapacity(sal_Int32 minimumCapacity)
Ensures that the capacity of the buffer is at least equal to the specified minimum.
Definition: ustrbuf.hxx:442
OUStringBuffer & insert(sal_Int32 offset, sal_Bool b)
Inserts the string representation of the sal_Bool argument into this string buffer.
Definition: ustrbuf.hxx:1083
OUStringBuffer & append(char c)
Appends the string representation of the ASCII char argument to this string buffer.
Definition: ustrbuf.hxx:797
OUStringBuffer & insert(sal_Int32 offset, sal_Unicode c)
Inserts the string representation of the char argument into this string buffer.
Definition: ustrbuf.hxx:1153
OUStringBuffer & insert(sal_Int32 offset, bool b)
Inserts the string representation of the bool argument into this string buffer.
Definition: ustrbuf.hxx:1108
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:834
sal_Int32 lastIndexOf(const OUString &str) const
Returns the index within this string of the last occurrence of the specified substring,...
Definition: ustrbuf.hxx:1507
OUStringBuffer & append(sal_Bool b)
Appends the string representation of the sal_Bool argument to the string buffer.
Definition: ustrbuf.hxx:780
SAL_WARN_UNUSED_RESULT OUString makeStringAndClear()
Fill the string data in the new string and clear the buffer.
Definition: ustrbuf.hxx:386
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:920
void accessInternals(rtl_uString ***pInternalData, sal_Int32 **pInternalCapacity)
Allows access to the internal data of this OUStringBuffer, for effective manipulation.
Definition: ustrbuf.hxx:1351
OUStringBuffer & append(bool b)
Appends the string representation of the bool argument to the string buffer.
Definition: ustrbuf.hxx:744
This String class provides base functionality for C++ like Unicode character array handling.
Definition: ustring.hxx:168
const sal_Unicode * getStr() const SAL_RETURNS_NONNULL
Returns a pointer to the Unicode character buffer for this string.
Definition: ustring.hxx:845
OUString & operator+=(const OUString &str)
Append a string to this string.
Definition: ustring.hxx:694
sal_Int32 getLength() const
Returns the length of this string.
Definition: ustring.hxx:823
static OUString const & unacquired(rtl_uString *const *ppHandle)
Provides an OUString const & passing a storage pointer of an rtl_uString * handle.
Definition: ustring.hxx:562