tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / include / rtl / ustrbuf.hxx
blob693ad905910f577d5c778c14cb339e7b76c9df2a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 * This file is part of LibreOffice published API.
24 #ifndef INCLUDED_RTL_USTRBUF_HXX
25 #define INCLUDED_RTL_USTRBUF_HXX
27 #include "sal/config.h"
29 #include <cassert>
30 #include <cstring>
31 #include <limits>
32 #include <new>
34 #if defined LIBO_INTERNAL_ONLY
35 #include <string_view>
36 #include <type_traits>
37 #include <utility>
38 #endif
40 #include "rtl/ustrbuf.h"
41 #include "rtl/ustring.hxx"
42 #include "rtl/stringutils.hxx"
43 #include "sal/types.h"
45 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
46 #include "o3tl/safeint.hxx"
47 #include "rtl/stringconcat.hxx"
48 #endif
50 #ifdef RTL_STRING_UNITTEST
51 extern bool rtl_string_unittest_invalid_conversion;
52 #endif
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
63 namespace rtl
66 #ifdef RTL_STRING_UNITTEST
67 #undef rtl
68 #endif
70 /** A string buffer implements a mutable sequence of characters.
72 class SAL_WARN_UNUSED OUStringBuffer
74 friend class OUString;
75 public:
76 /**
77 Constructs a string buffer with no characters in it and an
78 initial capacity of 16 characters.
80 OUStringBuffer()
81 : pData(NULL)
82 , nCapacity( 16 )
84 rtl_uString_new_WithLength( &pData, nCapacity );
87 /**
88 Allocates a new string buffer that contains the same sequence of
89 characters as the string buffer argument.
91 @param value a <code>OUStringBuffer</code>.
93 OUStringBuffer( const OUStringBuffer & value )
94 : pData(NULL)
95 , nCapacity( value.nCapacity )
97 rtl_uStringbuffer_newFromStringBuffer( &pData, value.nCapacity, value.pData );
101 Constructs a string buffer with no characters in it and an
102 initial capacity specified by the <code>length</code> argument.
104 @param length the initial capacity.
106 explicit OUStringBuffer(sal_Int32 length)
107 : pData(NULL)
108 , nCapacity( length )
110 rtl_uString_new_WithLength( &pData, length );
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))
117 assert(libreoffice_internal::IsValidStrLen(length));
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
131 Constructs a string buffer so that it represents the same
132 sequence of characters as the string argument.
134 The initial
135 capacity of the string buffer is <code>16</code> plus the length
136 of the string argument.
138 @param value the initial contents of the buffer.
140 #if defined LIBO_INTERNAL_ONLY
141 OUStringBuffer(std::u16string_view sv)
142 : pData(nullptr)
143 , nCapacity(libreoffice_internal::ThrowIfInvalidStrLen(sv.length(), 16) + 16)
145 rtl_uStringbuffer_newFromStr_WithLength( &pData, sv.data(), sv.length() );
147 #else
148 OUStringBuffer(const OUString& value)
149 : pData(NULL)
150 , nCapacity( value.getLength() + 16 )
152 rtl_uStringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
154 #endif
156 template< typename T >
157 OUStringBuffer( T& literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
158 : pData(NULL)
159 , nCapacity( libreoffice_internal::ConstCharArrayDetector<T>::length + 16 )
161 assert(
162 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
163 rtl_uString_newFromLiteral(
164 &pData,
165 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
166 libreoffice_internal::ConstCharArrayDetector<T>::length, 16);
167 #ifdef RTL_STRING_UNITTEST
168 rtl_string_unittest_const_literal = true;
169 #endif
172 #if defined LIBO_INTERNAL_ONLY
173 /** @overload @since LibreOffice 5.3 */
174 template<typename T>
175 OUStringBuffer(
176 T & literal,
177 typename libreoffice_internal::ConstCharArrayDetector<
178 T, libreoffice_internal::Dummy>::TypeUtf16
179 = libreoffice_internal::Dummy()):
180 pData(nullptr),
181 nCapacity(libreoffice_internal::ConstCharArrayDetector<T>::length + 16)
183 rtl_uStringbuffer_newFromStr_WithLength(
184 &pData,
185 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
186 libreoffice_internal::ConstCharArrayDetector<T>::length);
188 #endif
190 #if defined LIBO_INTERNAL_ONLY && defined RTL_STRING_UNITTEST
191 /// @cond INTERNAL
193 * Only used by unittests to detect incorrect conversions.
194 * @internal
196 template< typename T >
197 OUStringBuffer( T&, typename libreoffice_internal::ExceptConstCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
199 pData = NULL;
200 nCapacity = 10;
201 rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
202 rtl_string_unittest_invalid_conversion = true;
205 * Only used by unittests to detect incorrect conversions.
206 * @internal
208 template< typename T >
209 OUStringBuffer( const T&, typename libreoffice_internal::ExceptCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
211 pData = NULL;
212 nCapacity = 10;
213 rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
214 rtl_string_unittest_invalid_conversion = true;
216 /// @endcond
217 #endif
219 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
221 @overload
222 @internal
224 template< typename T1, typename T2 >
225 OUStringBuffer( OUStringConcat< T1, T2 >&& c )
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;
236 @overload
237 @internal
239 template< std::size_t N >
240 OUStringBuffer( OUStringNumber< N >&& n )
241 : OUStringBuffer(std::u16string_view(n))
244 #endif
246 #if defined LIBO_INTERNAL_ONLY
247 operator std::u16string_view() const { return {getStr(), sal_uInt32(getLength())}; }
248 #endif
250 /** Assign to this a copy of value.
252 OUStringBuffer& operator = ( const OUStringBuffer& value )
254 if (this != &value)
256 rtl_uStringbuffer_newFromStringBuffer(&pData,
257 value.nCapacity,
258 value.pData);
259 nCapacity = value.nCapacity;
261 return *this;
264 #if defined LIBO_INTERNAL_ONLY
265 /** Move assignment
266 * @since LibreOffice 7.3
268 OUStringBuffer& operator = ( OUStringBuffer&& value ) noexcept
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;
278 #endif
280 /** Assign from a string.
282 @since LibreOffice 5.3
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
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;
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
303 std::memcpy(
304 pData->buffer, string.pData->buffer,
305 (n + 1) * sizeof (sal_Unicode));
306 pData->length = n;
307 return *this;
309 #endif
311 /** Assign from a string literal.
313 @since LibreOffice 5.3
315 template<typename T>
316 typename
317 libreoffice_internal::ConstCharArrayDetector<T, OUStringBuffer &>::Type
318 operator =(T & literal) {
319 assert(
320 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
321 sal_Int32 const n
322 = libreoffice_internal::ConstCharArrayDetector<T>::length;
323 if (n >= nCapacity) {
324 ensureCapacity(n + 16); //TODO: check for overflow
326 char const * from
327 = libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
328 literal);
329 sal_Unicode * to = pData->buffer;
330 for (sal_Int32 i = 0; i <= n; ++i) {
331 to[i] = from[i];
333 pData->length = n;
334 return *this;
337 #if defined LIBO_INTERNAL_ONLY
338 /** @overload @since LibreOffice 5.3 */
339 template<typename T>
340 typename libreoffice_internal::ConstCharArrayDetector<
341 T, OUStringBuffer &>::TypeUtf16
342 operator =(T & literal) {
343 return operator=(
344 std::u16string_view(libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
345 libreoffice_internal::ConstCharArrayDetector<T>::length));
347 #endif
349 #if defined LIBO_INTERNAL_ONLY
350 /** @overload @since LibreOffice 5.3 */
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
357 *concat.addData(pData->buffer) = 0;
358 pData->length = n;
359 return *this;
362 /** @overload @internal */
363 template<std::size_t N>
364 OUStringBuffer & operator =(OUStringNumber<N> && n)
366 return operator =(std::u16string_view(n));
368 #endif
371 Release the string data.
373 ~OUStringBuffer()
375 rtl_uString_release( pData );
379 Fill the string data in the new string and clear the buffer.
381 This method is more efficient than the constructor of the string. It does
382 not copy the buffer.
384 @return the string previously contained in the buffer.
386 SAL_WARN_UNUSED_RESULT OUString makeStringAndClear()
388 return OUString(
389 rtl_uStringBuffer_makeStringAndClear( &pData, &nCapacity ),
390 SAL_NO_ACQUIRE );
394 Returns the length (character count) of this string buffer.
396 @return the number of characters in this string buffer.
398 sal_Int32 getLength() const
400 return pData->length;
404 Checks if a string buffer is empty.
406 @return true if the string buffer is empty;
407 false, otherwise.
409 @since LibreOffice 4.1
411 bool isEmpty() const
413 return pData->length == 0;
417 Returns the current capacity of the String buffer.
419 The capacity
420 is the amount of storage available for newly inserted
421 characters. The real buffer size is 2 bytes longer, because
422 all strings are 0 terminated.
424 @return the current capacity of this string buffer.
426 sal_Int32 getCapacity() const
428 return nCapacity;
432 Ensures that the capacity of the buffer is at least equal to the
433 specified minimum.
435 The new capacity will be at least as large as the maximum of the current
436 length (so that no contents of the buffer is destroyed) and the given
437 minimumCapacity. If the given minimumCapacity is negative, nothing is
438 changed.
440 @param minimumCapacity the minimum desired capacity.
442 void ensureCapacity(sal_Int32 minimumCapacity)
444 rtl_uStringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
448 Sets the length of this String buffer.
450 If the <code>newLength</code> argument is less than the current
451 length of the string buffer, the string buffer is truncated to
452 contain exactly the number of characters given by the
453 <code>newLength</code> argument.
455 If the <code>newLength</code> argument is greater than or equal
456 to the current length, sufficient null characters
457 (<code>'&#92;u0000'</code>) are appended to the string buffer so that
458 length becomes the <code>newLength</code> argument.
460 The <code>newLength</code> argument must be greater than or equal
461 to <code>0</code>.
463 @param newLength the new length of the buffer.
465 void setLength(sal_Int32 newLength)
467 assert(newLength >= 0);
468 // Avoid modifications if pData points to const empty string:
469 if( newLength != pData->length )
471 if( newLength > nCapacity )
472 rtl_uStringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
473 else
474 pData->buffer[newLength] = 0;
475 pData->length = newLength;
480 Returns the character at a specific index in this string buffer.
482 The first character of a string buffer is at index
483 <code>0</code>, the next at index <code>1</code>, and so on, for
484 array indexing.
486 The index argument must be greater than or equal to
487 <code>0</code>, and less than the length of this string buffer.
489 @param index the index of the desired character.
490 @return the character at the specified index of this string buffer.
492 SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
493 sal_Unicode charAt( sal_Int32 index ) const
495 assert(index >= 0 && index < pData->length);
496 return pData->buffer[ index ];
500 The character at the specified index of this string buffer is set
501 to <code>ch</code>.
503 The index argument must be greater than or equal to
504 <code>0</code>, and less than the length of this string buffer.
506 @param index the index of the character to modify.
507 @param ch the new character.
509 SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
510 OUStringBuffer & setCharAt(sal_Int32 index, sal_Unicode ch)
512 assert(index >= 0 && index < pData->length);
513 pData->buffer[ index ] = ch;
514 return *this;
518 Return a null terminated unicode character array.
520 const sal_Unicode* getStr() const SAL_RETURNS_NONNULL { return pData->buffer; }
523 Access to individual characters.
525 @param index must be non-negative and less than length.
527 @return a reference to the character at the given index.
529 @since LibreOffice 3.5
531 sal_Unicode & operator [](sal_Int32 index)
533 assert(index >= 0 && index < pData->length);
534 return pData->buffer[index];
538 Access to individual characters.
540 @param index must be non-negative and less than length.
542 @return a reference to the character at the given index.
544 @since LibreOffice 4.2
546 const sal_Unicode & operator [](sal_Int32 index) const
548 assert(index >= 0 && index < pData->length);
549 return pData->buffer[index];
553 Return an OUString instance reflecting the current content
554 of this OUStringBuffer.
556 OUString toString() const
558 return OUString(pData->buffer, pData->length);
562 Appends the string to this string buffer.
564 The characters of the <code>OUString</code> argument are appended, in
565 order, to the contents of this string buffer, increasing the
566 length of this string buffer by the length of the argument.
568 @param str a string.
569 @return this string buffer.
571 #if !defined LIBO_INTERNAL_ONLY
572 OUStringBuffer & append(const OUString &str)
573 #else
574 OUStringBuffer & append(std::u16string_view str)
575 #endif
577 return insert(getLength(), str);
580 #if !defined LIBO_INTERNAL_ONLY
582 Appends the content of a stringbuffer to this string buffer.
584 The characters of the <code>OUStringBuffer</code> argument are appended, in
585 order, to the contents of this string buffer, increasing the
586 length of this string buffer by the length of the argument.
588 @param str a string.
589 @return this string buffer.
591 @since LibreOffice 4.0
593 OUStringBuffer & append(const OUStringBuffer &str)
595 if(!str.isEmpty())
597 append( str.getStr(), str.getLength() );
599 return *this;
601 #endif
604 Appends the string representation of the <code>char</code> array
605 argument to this string buffer.
607 The characters of the array argument are appended, in order, to
608 the contents of this string buffer. The length of this string
609 buffer increases by the length of the argument.
611 @param str the characters to be appended.
612 @return this string buffer.
614 #if defined LIBO_INTERNAL_ONLY
615 template<typename T>
616 typename libreoffice_internal::CharPtrDetector<T, OUStringBuffer &>::TypeUtf16
617 append(T const & str)
618 #else
619 OUStringBuffer & append( const sal_Unicode * str )
620 #endif
622 return insert(getLength(), str);
626 Appends the string representation of the <code>char</code> array
627 argument to this string buffer.
629 Characters of the character array <code>str</code> are appended,
630 in order, to the contents of this string buffer. The length of this
631 string buffer increases by the value of <code>len</code>.
633 @param str the characters to be appended; must be non-null, and must
634 point to at least len characters
635 @param len the number of characters to append; must be non-negative
636 @return this string buffer.
638 OUStringBuffer & append( const sal_Unicode * str, sal_Int32 len)
640 return insert(getLength(), str, len);
644 @overload
645 This function accepts an ASCII string literal as its argument.
646 @since LibreOffice 3.6
648 template< typename T >
649 typename libreoffice_internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type append( T& literal )
651 return insert(getLength(), literal);
654 #if defined LIBO_INTERNAL_ONLY
655 template<typename T>
656 typename libreoffice_internal::NonConstCharArrayDetector<T, OUStringBuffer &>::TypeUtf16
657 append(T & value) { return append(static_cast<sal_Unicode *>(value)); }
659 /** @overload @since LibreOffice 5.3 */
660 template<typename T>
661 typename libreoffice_internal::ConstCharArrayDetector<
662 T, OUStringBuffer &>::TypeUtf16
663 append(T & literal) {
664 return insert(getLength(), literal);
666 #endif
668 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
670 @overload
671 @internal
673 template< typename T1, typename T2 >
674 OUStringBuffer& append( OUStringConcat< T1, T2 >&& c )
676 return insert(getLength(), std::move(c));
678 #endif
681 Appends a 8-Bit ASCII character string to this string buffer.
683 Since this method is optimized for performance. the ASCII
684 character values are not converted in any way. The caller
685 has to make sure that all ASCII characters are in the
686 allowed range between 0 and 127. The ASCII string must be
687 NULL-terminated.
689 The characters of the array argument are appended, in order, to
690 the contents of this string buffer. The length of this string
691 buffer increases by the length of the argument.
693 @param str the 8-Bit ASCII characters to be appended.
694 @return this string buffer.
696 OUStringBuffer & appendAscii( const char * str )
698 return appendAscii( str, rtl_str_getLength( str ) );
702 Appends a 8-Bit ASCII character string to this string buffer.
704 Since this method is optimized for performance. the ASCII
705 character values are not converted in any way. The caller
706 has to make sure that all ASCII characters are in the
707 allowed range between 0 and 127.
709 Characters of the character array <code>str</code> are appended,
710 in order, to the contents of this string buffer. The length of this
711 string buffer increases by the value of <code>len</code>.
713 @param str the 8-Bit ASCII characters to be appended; must be non-null,
714 and must point to at least len characters
715 @param len the number of characters to append; must be non-negative
716 @return this string buffer.
718 OUStringBuffer & appendAscii( const char * str, sal_Int32 len)
720 rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), str, len );
721 return *this;
725 Appends the string representation of the <code>bool</code>
726 argument to the string buffer.
728 The argument is converted to a string as if by the method
729 <code>String.valueOf</code>, and the characters of that
730 string are then appended to this string buffer.
732 @param b a <code>bool</code>.
733 @return this string buffer.
735 @since LibreOffice 4.1
737 OUStringBuffer & append(bool b)
739 return insert(getLength(), b);
742 /// @cond INTERNAL
743 // Pointer can be automatically converted to bool, which is unwanted here.
744 // Explicitly delete all pointer append() overloads to prevent this
745 // (except for char* and sal_Unicode* overloads, which are handled elsewhere).
746 template< typename T >
747 typename libreoffice_internal::Enable< void,
748 !libreoffice_internal::CharPtrDetector< T* >::ok && !libreoffice_internal::SalUnicodePtrDetector< T* >::ok >::Type
749 append( T* ) SAL_DELETED_FUNCTION;
750 /// @endcond
752 // This overload is needed because OUString has a ctor from rtl_uString*, but
753 // the bool overload above would be preferred to the conversion.
755 @internal
757 OUStringBuffer & append(rtl_uString* str)
759 return append( OUString::unacquired( &str ));
763 Appends the string representation of the <code>sal_Bool</code>
764 argument to the string buffer.
766 The argument is converted to a string as if by the method
767 <code>String.valueOf</code>, and the characters of that
768 string are then appended to this string buffer.
770 @param b a <code>sal_Bool</code>.
771 @return this string buffer.
773 OUStringBuffer & append(sal_Bool b)
775 return insert(getLength(), b);
779 Appends the string representation of the ASCII <code>char</code>
780 argument to this string buffer.
782 The argument is appended to the contents of this string buffer.
783 The length of this string buffer increases by <code>1</code>.
785 @param c an ASCII <code>char</code>.
786 @return this string buffer.
788 @since LibreOffice 3.5
790 OUStringBuffer & append(char c)
792 assert(static_cast< unsigned char >(c) <= 0x7F);
793 return insert(getLength(), c);
797 Appends the string representation of the <code>char</code>
798 argument to this string buffer.
800 The argument is appended to the contents of this string buffer.
801 The length of this string buffer increases by <code>1</code>.
803 @param c a <code>char</code>.
804 @return this string buffer.
806 OUStringBuffer & append(sal_Unicode c)
808 return insert(getLength(), c);
811 #if defined LIBO_INTERNAL_ONLY
812 void append(sal_uInt16) = delete;
813 #endif
816 Appends the string representation of the <code>sal_Int32</code>
817 argument to this string buffer.
819 The argument is converted to a string as if by the method
820 <code>String.valueOf</code>, and the characters of that
821 string are then appended to this string buffer.
823 @param i an <code>sal_Int32</code>.
824 @param radix the radix
825 @return this string buffer.
827 OUStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
829 return insert(getLength(), i, radix);
833 Appends the string representation of the <code>long</code>
834 argument to this string buffer.
836 The argument is converted to a string as if by the method
837 <code>String.valueOf</code>, and the characters of that
838 string are then appended to this string buffer.
840 @param l a <code>long</code>.
841 @param radix the radix
842 @return this string buffer.
844 OUStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
846 return insert(getLength(), l, radix);
850 Appends the string representation of the <code>float</code>
851 argument to this string buffer.
853 The argument is converted to a string as if by the method
854 <code>String.valueOf</code>, and the characters of that
855 string are then appended to this string buffer.
857 @param f a <code>float</code>.
858 @return this string buffer.
860 OUStringBuffer & append(float f)
862 return insert(getLength(), f);
866 Appends the string representation of the <code>double</code>
867 argument to this string buffer.
869 The argument is converted to a string as if by the method
870 <code>String.valueOf</code>, and the characters of that
871 string are then appended to this string buffer.
873 @param d a <code>double</code>.
874 @return this string buffer.
876 OUStringBuffer & append(double d)
878 return insert(getLength(), d);
882 Appends a single UTF-32 character to this string buffer.
884 <p>The single UTF-32 character will be represented within the string
885 buffer as either one or two UTF-16 code units.</p>
887 @param c a well-formed UTF-32 code unit (that is, a value in the range
888 <code>0</code>&ndash;<code>0x10FFFF</code>, but excluding
889 <code>0xD800</code>&ndash;<code>0xDFFF</code>)
891 @return
892 this string buffer
894 OUStringBuffer & appendUtf32(sal_uInt32 c) {
895 return insertUtf32(getLength(), c);
899 Unsafe way to make space for a fixed amount of characters to be appended
900 into this OUStringBuffer.
902 A call to this function must immediately be followed by code that
903 completely fills the uninitialized block pointed to by the return value.
905 @param length the length of the uninitialized block of sal_Unicode
906 entities; must be non-negative
908 @return a pointer to the start of the uninitialized block; only valid
909 until this OUStringBuffer's capacity changes
911 @since LibreOffice 4.4
913 sal_Unicode * appendUninitialized(sal_Int32 length) SAL_RETURNS_NONNULL {
914 sal_Int32 n = getLength();
915 rtl_uStringbuffer_insert(&pData, &nCapacity, n, NULL, length);
916 return pData->buffer + n;
919 #if defined LIBO_INTERNAL_ONLY
921 "Stream" operator to append a value to this OUStringBuffer.
923 @internal
924 @since LibreOffice 7.5
926 template<typename T>
927 OUStringBuffer& operator<<(T&& rValue)
929 return append(std::forward<T>(rValue));
931 #endif
934 Inserts the string into this string buffer.
936 The characters of the <code>String</code> argument are inserted, in
937 order, into this string buffer at the indicated offset. The length
938 of this string buffer is increased by the length of the argument.
940 The offset argument must be greater than or equal to
941 <code>0</code>, and less than or equal to the length of this
942 string buffer.
944 @param offset the offset.
945 @param str a string.
946 @return this string buffer.
948 #if defined LIBO_INTERNAL_ONLY
949 OUStringBuffer & insert(sal_Int32 offset, std::u16string_view str)
951 return insert(offset, str.data(), libreoffice_internal::ThrowIfInvalidStrLen(str.length()));
953 #else
954 OUStringBuffer & insert(sal_Int32 offset, const OUString & str)
956 return insert( offset, str.getStr(), str.getLength() );
958 #endif
960 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
962 @overload
963 @internal
965 template <typename T1, typename T2>
966 OUStringBuffer& insert(sal_Int32 offset, OUStringConcat<T1, T2>&& c)
968 const size_t l = c.length();
969 if (l == 0)
970 return *this;
971 if (l > o3tl::make_unsigned(std::numeric_limits<sal_Int32>::max() - pData->length))
972 throw std::bad_alloc();
974 rtl_uStringbuffer_insert(&pData, &nCapacity, offset, nullptr, l);
976 /* insert the new characters */
977 c.addData(pData->buffer + offset);
978 return *this;
980 #endif
983 Inserts the string representation of the <code>char</code> array
984 argument into this string buffer.
986 The characters of the array argument are inserted into the
987 contents of this string buffer at the position indicated by
988 <code>offset</code>. The length of this string buffer increases by
989 the length of the argument.
991 The offset argument must be greater than or equal to
992 <code>0</code>, and less than or equal to the length of this
993 string buffer.
995 @param offset the offset.
996 @param str a character array.
997 @return this string buffer.
999 OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str )
1001 return insert( offset, str, rtl_ustr_getLength( str ) );
1005 Inserts the string representation of the <code>char</code> array
1006 argument into this string buffer.
1008 The characters of the array argument are inserted into the
1009 contents of this string buffer at the position indicated by
1010 <code>offset</code>. The length of this string buffer increases by
1011 the length of the argument.
1013 The offset argument must be greater than or equal to
1014 <code>0</code>, and less than or equal to the length of this
1015 string buffer.
1017 @param offset the offset.
1018 @param str a character array.
1019 @param len the number of characters to append.
1020 @return this string buffer.
1022 OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str, sal_Int32 len)
1024 assert( len == 0 || str != NULL ); // cannot assert that in rtl_uStringbuffer_insert
1025 rtl_uStringbuffer_insert( &pData, &nCapacity, offset, str, len );
1026 return *this;
1030 @overload
1031 This function accepts an ASCII string literal as its argument.
1032 @since LibreOffice 3.6
1034 template< typename T >
1035 typename libreoffice_internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type insert( sal_Int32 offset, T& literal )
1037 assert(
1038 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1039 rtl_uStringbuffer_insert_ascii(
1040 &pData, &nCapacity, offset,
1041 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1042 libreoffice_internal::ConstCharArrayDetector<T>::length);
1043 return *this;
1046 #if defined LIBO_INTERNAL_ONLY
1047 /** @overload @since LibreOffice 5.3 */
1048 template<typename T>
1049 typename libreoffice_internal::ConstCharArrayDetector<
1050 T, OUStringBuffer &>::TypeUtf16
1051 insert(sal_Int32 offset, T & literal) {
1052 return insert(
1053 offset,
1054 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1055 libreoffice_internal::ConstCharArrayDetector<T>::length);
1057 #endif
1060 Inserts the string representation of the <code>sal_Bool</code>
1061 argument into this string buffer.
1063 The second argument is converted to a string as if by the method
1064 <code>String.valueOf</code>, and the characters of that
1065 string are then inserted into this string buffer at the indicated
1066 offset.
1068 The offset argument must be greater than or equal to
1069 <code>0</code>, and less than or equal to the length of this
1070 string buffer.
1072 @param offset the offset.
1073 @param b a <code>sal_Bool</code>.
1074 @return this string buffer.
1076 OUStringBuffer & insert(sal_Int32 offset, sal_Bool b)
1078 sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
1079 return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
1083 Inserts the string representation of the <code>bool</code>
1084 argument into this string buffer.
1086 The second argument is converted to a string as if by the method
1087 <code>OUString::boolean</code>, and the characters of that
1088 string are then inserted into this string buffer at the indicated
1089 offset.
1091 The offset argument must be greater than or equal to
1092 <code>0</code>, and less than or equal to the length of this
1093 string buffer.
1095 @param offset the offset.
1096 @param b a <code>bool</code>.
1097 @return this string buffer.
1099 @since LibreOffice 4.3
1101 OUStringBuffer & insert(sal_Int32 offset, bool b)
1103 sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
1104 return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
1108 Inserts the string representation of the <code>char</code>
1109 argument into this string buffer.
1111 The second argument is inserted into the contents of this string
1112 buffer at the position indicated by <code>offset</code>. The length
1113 of this string buffer increases by one.
1115 The offset argument must be greater than or equal to
1116 <code>0</code>, and less than or equal to the length of this
1117 string buffer.
1119 @param offset the offset.
1120 @param c a <code>char</code>.
1121 @return this string buffer.
1123 @since LibreOffice 3.6
1125 OUStringBuffer & insert(sal_Int32 offset, char c)
1127 return insert(offset, sal_Unicode(c));
1131 Inserts the string representation of the <code>char</code>
1132 argument into this string buffer.
1134 The second argument is inserted into the contents of this string
1135 buffer at the position indicated by <code>offset</code>. The length
1136 of this string buffer increases by one.
1138 The offset argument must be greater than or equal to
1139 <code>0</code>, and less than or equal to the length of this
1140 string buffer.
1142 @param offset the offset.
1143 @param c a <code>char</code>.
1144 @return this string buffer.
1146 OUStringBuffer & insert(sal_Int32 offset, sal_Unicode c)
1148 return insert( offset, &c, 1 );
1152 Inserts the string representation of the second <code>sal_Int32</code>
1153 argument into this string buffer.
1155 The second argument is converted to a string as if by the method
1156 <code>String.valueOf</code>, and the characters of that
1157 string are then inserted into this string buffer at the indicated
1158 offset.
1160 The offset argument must be greater than or equal to
1161 <code>0</code>, and less than or equal to the length of this
1162 string buffer.
1164 @param offset the offset.
1165 @param i an <code>sal_Int32</code>.
1166 @param radix the radix.
1167 @return this string buffer.
1168 @exception StringIndexOutOfBoundsException if the offset is invalid.
1170 OUStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
1172 sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT32];
1173 return insert( offset, sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
1177 Inserts the string representation of the <code>long</code>
1178 argument into this string buffer.
1180 The second argument is converted to a string as if by the method
1181 <code>String.valueOf</code>, and the characters of that
1182 string are then inserted into this string buffer at the indicated
1183 offset.
1185 The offset argument must be greater than or equal to
1186 <code>0</code>, and less than or equal to the length of this
1187 string buffer.
1189 @param offset the offset.
1190 @param l a <code>long</code>.
1191 @param radix the radix.
1192 @return this string buffer.
1193 @exception StringIndexOutOfBoundsException if the offset is invalid.
1195 OUStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
1197 sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT64];
1198 return insert( offset, sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
1202 Inserts the string representation of the <code>float</code>
1203 argument into this string buffer.
1205 The second argument is converted to a string as if by the method
1206 <code>String.valueOf</code>, and the characters of that
1207 string are then inserted into this string buffer at the indicated
1208 offset.
1210 The offset argument must be greater than or equal to
1211 <code>0</code>, and less than or equal to the length of this
1212 string buffer.
1214 @param offset the offset.
1215 @param f a <code>float</code>.
1216 @return this string buffer.
1217 @exception StringIndexOutOfBoundsException if the offset is invalid.
1219 OUStringBuffer & insert(sal_Int32 offset, float f)
1221 // Same as rtl::str::valueOfFP, used for rtl_ustr_valueOfFloat
1222 rtl_math_doubleToUString(&pData, &nCapacity, offset, f, rtl_math_StringFormat_G,
1223 RTL_USTR_MAX_VALUEOFFLOAT - SAL_N_ELEMENTS("-x.E-xxx") + 1, '.',
1224 NULL, 0, true);
1225 return *this;
1229 Inserts the string representation of the <code>double</code>
1230 argument into this string buffer.
1232 The second argument is converted to a string as if by the method
1233 <code>String.valueOf</code>, and the characters of that
1234 string are then inserted into this string buffer at the indicated
1235 offset.
1237 The offset argument must be greater than or equal to
1238 <code>0</code>, and less than or equal to the length of this
1239 string buffer.
1241 @param offset the offset.
1242 @param d a <code>double</code>.
1243 @return this string buffer.
1244 @exception StringIndexOutOfBoundsException if the offset is invalid.
1246 OUStringBuffer & insert(sal_Int32 offset, double d)
1248 // Same as rtl::str::valueOfFP, used for rtl_ustr_valueOfDouble
1249 rtl_math_doubleToUString(&pData, &nCapacity, offset, d, rtl_math_StringFormat_G,
1250 RTL_USTR_MAX_VALUEOFDOUBLE - SAL_N_ELEMENTS("-x.E-xxx") + 1, '.',
1251 NULL, 0, true);
1252 return *this;
1256 Inserts a single UTF-32 character into this string buffer.
1258 <p>The single UTF-32 character will be represented within the string
1259 buffer as either one or two UTF-16 code units.</p>
1261 @param offset the offset into this string buffer (from zero to the length
1262 of this string buffer, inclusive)
1264 @param c a well-formed UTF-32 code unit (that is, a value in the range
1265 <code>0</code>&ndash;<code>0x10FFFF</code>, but excluding
1266 <code>0xD800</code>&ndash;<code>0xDFFF</code>)
1268 @return this string buffer
1270 OUStringBuffer & insertUtf32(sal_Int32 offset, sal_uInt32 c) {
1271 rtl_uStringbuffer_insertUtf32(&pData, &nCapacity, offset, c);
1272 return *this;
1276 Removes the characters in a substring of this sequence.
1278 The substring begins at the specified <code>start</code> and
1279 is <code>len</code> characters long.
1281 start must be >= 0 && <= This->length
1283 @param start The beginning index, inclusive
1284 @param len The substring length
1285 @return this string buffer.
1287 OUStringBuffer & remove( sal_Int32 start, sal_Int32 len )
1289 rtl_uStringbuffer_remove( &pData, start, len );
1290 return *this;
1294 Removes the tail of a string buffer start at the indicate position
1296 start must be >= 0 && <= This->length
1298 @param start The beginning index, inclusive. default to 0
1299 @return this string buffer.
1301 @since LibreOffice 4.0
1303 OUStringBuffer & truncate( sal_Int32 start = 0 )
1305 rtl_uStringbuffer_remove( &pData, start, getLength() - start );
1306 return *this;
1310 Replace all occurrences of
1311 oldChar in this string buffer with newChar.
1313 @since LibreOffice 4.0
1315 @param oldChar the old character.
1316 @param newChar the new character.
1317 @return this string buffer
1319 OUStringBuffer& replace( sal_Unicode oldChar, sal_Unicode newChar )
1321 sal_Int32 index = 0;
1322 while((index = indexOf(oldChar, index)) >= 0)
1324 pData->buffer[ index ] = newChar;
1326 return *this;
1329 /** Allows access to the internal data of this OUStringBuffer, for effective
1330 manipulation.
1332 This method should be used with care. After you have called this
1333 method, you may use the returned pInternalData or pInternalCapacity only
1334 as long as you make no other method call on this OUStringBuffer.
1336 @param pInternalData
1337 This output parameter receives a pointer to the internal data
1338 (rtl_uString pointer). pInternalData itself must not be null.
1340 @param pInternalCapacity
1341 This output parameter receives a pointer to the internal capacity.
1342 pInternalCapacity itself must not be null.
1344 void accessInternals(rtl_uString *** pInternalData,
1345 sal_Int32 ** pInternalCapacity)
1347 *pInternalData = &pData;
1348 *pInternalCapacity = &nCapacity;
1353 Returns the index within this string of the first occurrence of the
1354 specified character, starting the search at the specified index.
1356 @since LibreOffice 4.0
1358 @param ch character to be located.
1359 @param fromIndex the index to start the search from.
1360 The index must be greater or equal than 0
1361 and less or equal as the string length.
1362 @return the index of the first occurrence of the character in the
1363 character sequence represented by this string that is
1364 greater than or equal to fromIndex, or
1365 -1 if the character does not occur.
1367 sal_Int32 indexOf( sal_Unicode ch, sal_Int32 fromIndex = 0 ) const
1369 assert( fromIndex >= 0 && fromIndex <= pData->length );
1370 sal_Int32 ret = rtl_ustr_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch );
1371 return (ret < 0 ? ret : ret+fromIndex);
1375 Returns the index within this string of the last occurrence of the
1376 specified character, searching backward starting at the end.
1378 @since LibreOffice 4.0
1380 @param ch character to be located.
1381 @return the index of the last occurrence of the character in the
1382 character sequence represented by this string, or
1383 -1 if the character does not occur.
1385 sal_Int32 lastIndexOf( sal_Unicode ch ) const
1387 return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch );
1391 Returns the index within this string of the last occurrence of the
1392 specified character, searching backward starting before the specified
1393 index.
1395 @since LibreOffice 4.0
1397 @param ch character to be located.
1398 @param fromIndex the index before which to start the search.
1399 @return the index of the last occurrence of the character in the
1400 character sequence represented by this string that
1401 is less than fromIndex, or -1
1402 if the character does not occur before that point.
1404 sal_Int32 lastIndexOf( sal_Unicode ch, sal_Int32 fromIndex ) const
1406 assert( fromIndex >= 0 && fromIndex <= pData->length );
1407 return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch );
1411 Returns the index within this string of the first occurrence of the
1412 specified substring, starting at the specified index.
1414 If str doesn't include any character, always -1 is
1415 returned. This is also the case, if both strings are empty.
1417 @since LibreOffice 4.0
1419 @param str the substring to search for.
1420 @param fromIndex the index to start the search from.
1421 @return If the string argument occurs one or more times as a substring
1422 within this string at the starting index, then the index
1423 of the first character of the first such substring is
1424 returned. If it does not occur as a substring starting
1425 at fromIndex or beyond, -1 is returned.
1427 #if defined LIBO_INTERNAL_ONLY
1428 sal_Int32 indexOf( std::u16string_view str, sal_Int32 fromIndex = 0 ) const
1430 assert( fromIndex >= 0 && fromIndex <= pData->length );
1431 sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1432 str.data(), str.length() );
1433 return (ret < 0 ? ret : ret+fromIndex);
1435 #else
1436 sal_Int32 indexOf( const OUString & str, sal_Int32 fromIndex = 0 ) const
1438 assert( fromIndex >= 0 && fromIndex <= pData->length );
1439 sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1440 str.pData->buffer, str.pData->length );
1441 return (ret < 0 ? ret : ret+fromIndex);
1443 #endif
1446 @overload
1447 This function accepts an ASCII string literal as its argument.
1449 @since LibreOffice 4.0
1451 template< typename T >
1452 typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const
1454 assert(
1455 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1456 sal_Int32 n = rtl_ustr_indexOfAscii_WithLength(
1457 pData->buffer + fromIndex, pData->length - fromIndex,
1458 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1459 libreoffice_internal::ConstCharArrayDetector<T>::length);
1460 return n < 0 ? n : n + fromIndex;
1463 #if defined LIBO_INTERNAL_ONLY
1464 /** @overload @since LibreOffice 5.3 */
1465 template<typename T>
1466 typename
1467 libreoffice_internal::ConstCharArrayDetector<T, sal_Int32>::TypeUtf16
1468 indexOf(T & literal, sal_Int32 fromIndex = 0) const {
1469 return indexOf(
1470 std::u16string_view(libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1471 libreoffice_internal::ConstCharArrayDetector<T>::length),
1472 fromIndex);
1474 #endif
1477 Returns the index within this string of the last occurrence of
1478 the specified substring, searching backward starting at the end.
1480 The returned index indicates the starting index of the substring
1481 in this string.
1482 If str doesn't include any character, always -1 is
1483 returned. This is also the case, if both strings are empty.
1485 @since LibreOffice 4.0
1487 @param str the substring to search for.
1488 @return If the string argument occurs one or more times as a substring
1489 within this string, then the index of the first character of
1490 the last such substring is returned. If it does not occur as
1491 a substring, -1 is returned.
1493 #if defined LIBO_INTERNAL_ONLY
1494 sal_Int32 lastIndexOf( std::u16string_view str ) const
1496 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1497 str.data(), str.length() );
1499 #else
1500 sal_Int32 lastIndexOf( const OUString & str ) const
1502 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1503 str.pData->buffer, str.pData->length );
1505 #endif
1508 Returns the index within this string of the last occurrence of
1509 the specified substring, searching backward starting before the specified
1510 index.
1512 The returned index indicates the starting index of the substring
1513 in this string.
1514 If str doesn't include any character, always -1 is
1515 returned. This is also the case, if both strings are empty.
1517 @since LibreOffice 4.0
1519 @param str the substring to search for.
1520 @param fromIndex the index before which to start the search.
1521 @return If the string argument occurs one or more times as a substring
1522 within this string before the starting index, then the index
1523 of the first character of the last such substring is
1524 returned. Otherwise, -1 is returned.
1526 #if defined LIBO_INTERNAL_ONLY
1527 sal_Int32 lastIndexOf( std::u16string_view str, sal_Int32 fromIndex ) const
1529 assert( fromIndex >= 0 && fromIndex <= pData->length );
1530 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1531 str.data(), str.length() );
1533 #else
1534 sal_Int32 lastIndexOf( const OUString & str, sal_Int32 fromIndex ) const
1536 assert( fromIndex >= 0 && fromIndex <= pData->length );
1537 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1538 str.pData->buffer, str.pData->length );
1540 #endif
1543 @overload
1544 This function accepts an ASCII string literal as its argument.
1545 @since LibreOffice 4.0
1547 template< typename T >
1548 typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type lastIndexOf( T& literal ) const
1550 assert(
1551 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1552 return rtl_ustr_lastIndexOfAscii_WithLength(
1553 pData->buffer, pData->length,
1554 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1555 libreoffice_internal::ConstCharArrayDetector<T>::length);
1558 #if defined LIBO_INTERNAL_ONLY
1559 /** @overload @since LibreOffice 5.3 */
1560 template<typename T>
1561 typename
1562 libreoffice_internal::ConstCharArrayDetector<T, sal_Int32>::TypeUtf16
1563 lastIndexOf(T & literal) const {
1564 return lastIndexOf(
1565 std::u16string_view(libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1566 libreoffice_internal::ConstCharArrayDetector<T>::length));
1568 #endif
1571 Strip the given character from the start of the buffer.
1573 @since LibreOffice 4.0
1575 @param c the character to strip
1576 @return The number of characters stripped
1579 sal_Int32 stripStart(sal_Unicode c = ' ')
1581 sal_Int32 index;
1582 for(index = 0; index < getLength() ; index++)
1584 if(pData->buffer[ index ] != c)
1586 break;
1589 if(index)
1591 remove(0, index);
1593 return index;
1597 Strip the given character from the end of the buffer.
1599 @since LibreOffice 4.0
1601 @param c the character to strip
1602 @return The number of characters stripped
1605 sal_Int32 stripEnd(sal_Unicode c = ' ')
1607 sal_Int32 result = getLength();
1608 sal_Int32 index;
1609 for(index = getLength(); index > 0 ; index--)
1611 if(pData->buffer[ index - 1 ] != c)
1613 break;
1616 if(index < getLength())
1618 truncate(index);
1620 return result - getLength();
1623 Strip the given character from the both end of the buffer.
1625 @since LibreOffice 4.0
1627 @param c the character to strip
1628 @return The number of characters stripped
1631 sal_Int32 strip(sal_Unicode c = ' ')
1633 return stripStart(c) + stripEnd(c);
1636 #if defined LIBO_INTERNAL_ONLY
1638 Returns a std::u16string_view that is a view of a substring of this string.
1640 The substring begins at the specified beginIndex. If
1641 beginIndex is negative or be greater than the length of
1642 this string, behaviour is undefined.
1644 @param beginIndex the beginning index, inclusive.
1645 @return the specified substring.
1647 SAL_WARN_UNUSED_RESULT std::u16string_view subView( sal_Int32 beginIndex ) const
1649 assert(beginIndex >= 0);
1650 assert(beginIndex <= getLength());
1651 return subView(beginIndex, getLength() - beginIndex);
1655 Returns a std::u16string_view that is a view of a substring of this string.
1657 The substring begins at the specified beginIndex and contains count
1658 characters. If either beginIndex or count are negative,
1659 or beginIndex + count are greater than the length of this string
1660 then behaviour is undefined.
1662 @param beginIndex the beginning index, inclusive.
1663 @param count the number of characters.
1664 @return the specified substring.
1666 SAL_WARN_UNUSED_RESULT std::u16string_view subView( sal_Int32 beginIndex, sal_Int32 count ) const
1668 assert(beginIndex >= 0);
1669 assert(count >= 0);
1670 assert(beginIndex <= getLength());
1671 assert(count <= getLength() - beginIndex);
1672 return std::u16string_view(pData->buffer, sal_uInt32(pData->length)).substr(beginIndex, count);
1674 #endif
1677 Returns a new string buffer that is a substring of this string.
1679 The substring begins at the specified beginIndex. If
1680 beginIndex is negative or be greater than the length of
1681 this string, behaviour is undefined.
1683 @param beginIndex the beginning index, inclusive.
1684 @return the specified substring.
1685 @since LibreOffice 4.1
1687 OUStringBuffer copy( sal_Int32 beginIndex ) const
1689 return copy( beginIndex, getLength() - beginIndex );
1693 Returns a new string buffer that is a substring of this string.
1695 The substring begins at the specified beginIndex and contains count
1696 characters. If either beginIndex or count are negative,
1697 or beginIndex + count are greater than the length of this string
1698 then behaviour is undefined.
1700 @param beginIndex the beginning index, inclusive.
1701 @param count the number of characters.
1702 @return the specified substring.
1703 @since LibreOffice 4.1
1705 OUStringBuffer copy( sal_Int32 beginIndex, sal_Int32 count ) const
1707 assert(beginIndex >= 0 && beginIndex <= getLength());
1708 assert(count >= 0 && count <= getLength() - beginIndex);
1709 rtl_uString *pNew = NULL;
1710 rtl_uStringbuffer_newFromStr_WithLength( &pNew, getStr() + beginIndex, count );
1711 return OUStringBuffer( pNew, count + 16 );
1714 private:
1715 OUStringBuffer( rtl_uString * value, const sal_Int32 capacity )
1717 pData = value;
1718 nCapacity = capacity;
1722 A pointer to the data structure which contains the data.
1724 rtl_uString * pData;
1727 The len of the pData->buffer.
1729 sal_Int32 nCapacity;
1732 #if defined LIBO_INTERNAL_ONLY
1733 template<> struct ToStringHelper<OUStringBuffer> {
1734 static std::size_t length(OUStringBuffer const & s) { return s.getLength(); }
1736 sal_Unicode * operator()(sal_Unicode * buffer, OUStringBuffer const & s) const SAL_RETURNS_NONNULL
1737 { return addDataHelper(buffer, s.getStr(), s.getLength()); }
1739 #endif
1741 #if defined LIBO_INTERNAL_ONLY
1742 // Define this here to avoid circular includes
1743 inline OUString & OUString::operator+=( const OUStringBuffer & str ) &
1745 // Call operator= if this is empty, otherwise rtl_uString_newConcat will attempt to
1746 // acquire() the str.pData buffer, which is part of the OUStringBuffer mutable state.
1747 if (isEmpty())
1748 return operator=(str.toString());
1749 else
1750 return internalAppend(str.pData);
1753 inline OUString const& OUString::unacquired(const OUStringBuffer& str)
1755 return unacquired(&str.pData);
1757 #endif
1760 #ifdef RTL_STRING_UNITTEST
1761 namespace rtl
1763 typedef rtlunittest::OUStringBuffer OUStringBuffer;
1765 #endif
1767 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
1768 using ::rtl::OUStringBuffer;
1769 #endif
1771 #endif // INCLUDED_RTL_USTRBUF_HXX
1773 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */