Avoid potential negative array index access to cached text.
[LibreOffice.git] / include / rtl / ustrbuf.hxx
blob30aa1959a67dc92d5a48dd322bbfc65912baa26f
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(
118 length >= 0
119 && static_cast<std::make_unsigned_t<T>>(length)
120 <= static_cast<std::make_unsigned_t<sal_Int32>>(
121 std::numeric_limits<sal_Int32>::max()));
123 // avoid (obvious) bugs
124 explicit OUStringBuffer(bool) = delete;
125 explicit OUStringBuffer(char) = delete;
126 explicit OUStringBuffer(wchar_t) = delete;
127 #if !(defined _MSC_VER && _MSC_VER >= 1930 && _MSC_VER <= 1939 && defined _MANAGED)
128 explicit OUStringBuffer(char8_t) = delete;
129 #endif
130 explicit OUStringBuffer(char16_t) = delete;
131 explicit OUStringBuffer(char32_t) = delete;
132 #endif
135 Constructs a string buffer so that it represents the same
136 sequence of characters as the string argument.
138 The initial
139 capacity of the string buffer is <code>16</code> plus the length
140 of the string argument.
142 @param value the initial contents of the buffer.
144 #if defined LIBO_INTERNAL_ONLY
145 OUStringBuffer(std::u16string_view sv)
146 : pData(nullptr)
147 , nCapacity( sv.length() + 16 )
149 if (sv.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max())) {
150 throw std::bad_alloc();
152 rtl_uStringbuffer_newFromStr_WithLength( &pData, sv.data(), sv.length() );
154 #else
155 OUStringBuffer(const OUString& value)
156 : pData(NULL)
157 , nCapacity( value.getLength() + 16 )
159 rtl_uStringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
161 #endif
163 template< typename T >
164 OUStringBuffer( T& literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
165 : pData(NULL)
166 , nCapacity( libreoffice_internal::ConstCharArrayDetector<T>::length + 16 )
168 assert(
169 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
170 rtl_uString_newFromLiteral(
171 &pData,
172 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
173 libreoffice_internal::ConstCharArrayDetector<T>::length, 16);
174 #ifdef RTL_STRING_UNITTEST
175 rtl_string_unittest_const_literal = true;
176 #endif
179 #if defined LIBO_INTERNAL_ONLY
180 /** @overload @since LibreOffice 5.3 */
181 template<typename T>
182 OUStringBuffer(
183 T & literal,
184 typename libreoffice_internal::ConstCharArrayDetector<
185 T, libreoffice_internal::Dummy>::TypeUtf16
186 = libreoffice_internal::Dummy()):
187 pData(nullptr),
188 nCapacity(libreoffice_internal::ConstCharArrayDetector<T>::length + 16)
190 rtl_uStringbuffer_newFromStr_WithLength(
191 &pData,
192 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
193 libreoffice_internal::ConstCharArrayDetector<T>::length);
195 #endif
197 #if defined LIBO_INTERNAL_ONLY && defined RTL_STRING_UNITTEST
198 /// @cond INTERNAL
200 * Only used by unittests to detect incorrect conversions.
201 * @internal
203 template< typename T >
204 OUStringBuffer( T&, typename libreoffice_internal::ExceptConstCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
206 pData = NULL;
207 nCapacity = 10;
208 rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
209 rtl_string_unittest_invalid_conversion = true;
212 * Only used by unittests to detect incorrect conversions.
213 * @internal
215 template< typename T >
216 OUStringBuffer( const T&, typename libreoffice_internal::ExceptCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
218 pData = NULL;
219 nCapacity = 10;
220 rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
221 rtl_string_unittest_invalid_conversion = true;
223 /// @endcond
224 #endif
226 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
228 @overload
229 @internal
231 template< typename T1, typename T2 >
232 OUStringBuffer( OUStringConcat< T1, T2 >&& c )
234 const sal_Int32 l = c.length();
235 nCapacity = l + 16;
236 pData = rtl_uString_alloc( nCapacity );
237 sal_Unicode* end = c.addData( pData->buffer );
238 *end = '\0';
239 pData->length = l;
243 @overload
244 @internal
246 template< std::size_t N >
247 OUStringBuffer( OUStringNumber< N >&& n )
248 : pData(NULL)
249 , nCapacity( n.length + 16 )
251 rtl_uStringbuffer_newFromStr_WithLength( &pData, n.buf, n.length );
253 #endif
255 #if defined LIBO_INTERNAL_ONLY
256 operator std::u16string_view() const { return {getStr(), sal_uInt32(getLength())}; }
257 #endif
259 /** Assign to this a copy of value.
261 OUStringBuffer& operator = ( const OUStringBuffer& value )
263 if (this != &value)
265 rtl_uStringbuffer_newFromStringBuffer(&pData,
266 value.nCapacity,
267 value.pData);
268 nCapacity = value.nCapacity;
270 return *this;
273 #if defined LIBO_INTERNAL_ONLY
274 /** Move assignment
275 * @since LibreOffice 7.3
277 OUStringBuffer& operator = ( OUStringBuffer&& value ) noexcept
279 rtl_uString_release( pData );
280 pData = value.pData;
281 nCapacity = value.nCapacity;
282 value.pData = nullptr;
283 value.nCapacity = 0;
284 rtl_uString_new( &value.pData );
285 return *this;
287 #endif
289 /** Assign from a string.
291 @since LibreOffice 5.3
293 #if defined LIBO_INTERNAL_ONLY
294 OUStringBuffer & operator =(std::u16string_view string) {
295 sal_Int32 n = string.length();
296 if (n >= nCapacity) {
297 ensureCapacity(n + 16); //TODO: check for overflow
299 std::memcpy(
300 pData->buffer, string.data(),
301 n * sizeof (sal_Unicode));
302 pData->buffer[n] = '\0';
303 pData->length = n;
304 return *this;
306 #else
307 OUStringBuffer & operator =(OUString const & string) {
308 sal_Int32 n = string.getLength();
309 if (n >= nCapacity) {
310 ensureCapacity(n + 16); //TODO: check for overflow
312 std::memcpy(
313 pData->buffer, string.pData->buffer,
314 (n + 1) * sizeof (sal_Unicode));
315 pData->length = n;
316 return *this;
318 #endif
320 /** Assign from a string literal.
322 @since LibreOffice 5.3
324 template<typename T>
325 typename
326 libreoffice_internal::ConstCharArrayDetector<T, OUStringBuffer &>::Type
327 operator =(T & literal) {
328 assert(
329 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
330 sal_Int32 const n
331 = libreoffice_internal::ConstCharArrayDetector<T>::length;
332 if (n >= nCapacity) {
333 ensureCapacity(n + 16); //TODO: check for overflow
335 char const * from
336 = libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
337 literal);
338 sal_Unicode * to = pData->buffer;
339 for (sal_Int32 i = 0; i <= n; ++i) {
340 to[i] = from[i];
342 pData->length = n;
343 return *this;
346 #if defined LIBO_INTERNAL_ONLY
347 /** @overload @since LibreOffice 5.3 */
348 template<typename T>
349 typename libreoffice_internal::ConstCharArrayDetector<
350 T, OUStringBuffer &>::TypeUtf16
351 operator =(T & literal) {
352 sal_Int32 const n
353 = libreoffice_internal::ConstCharArrayDetector<T>::length;
354 if (n >= nCapacity) {
355 ensureCapacity(n + 16); //TODO: check for overflow
357 // For OUStringChar, which is covered by this template's ConstCharArrayDetector TypeUtf16
358 // check, toPointer does not return a NUL-terminated string, so we can't just memcpy n+1
359 // elements but rather need to add the terminating NUL manually:
360 std::memcpy(
361 pData->buffer,
362 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
363 n * sizeof (sal_Unicode));
364 pData->buffer[n] = '\0';
365 pData->length = n;
366 return *this;
368 #endif
370 #if defined LIBO_INTERNAL_ONLY
371 /** @overload @since LibreOffice 5.3 */
372 template<typename T1, typename T2>
373 OUStringBuffer & operator =(OUStringConcat<T1, T2> && concat) {
374 sal_Int32 const n = concat.length();
375 if (n >= nCapacity) {
376 ensureCapacity(n + 16); //TODO: check for overflow
378 *concat.addData(pData->buffer) = 0;
379 pData->length = n;
380 return *this;
383 /** @overload @internal */
384 template<std::size_t N>
385 OUStringBuffer & operator =(OUStringNumber<N> && n)
387 return operator =(std::u16string_view(n));
389 #endif
392 Release the string data.
394 ~OUStringBuffer()
396 rtl_uString_release( pData );
400 Fill the string data in the new string and clear the buffer.
402 This method is more efficient than the constructor of the string. It does
403 not copy the buffer.
405 @return the string previously contained in the buffer.
407 SAL_WARN_UNUSED_RESULT OUString makeStringAndClear()
409 return OUString(
410 rtl_uStringBuffer_makeStringAndClear( &pData, &nCapacity ),
411 SAL_NO_ACQUIRE );
415 Returns the length (character count) of this string buffer.
417 @return the number of characters in this string buffer.
419 sal_Int32 getLength() const
421 return pData->length;
425 Checks if a string buffer is empty.
427 @return true if the string buffer is empty;
428 false, otherwise.
430 @since LibreOffice 4.1
432 bool isEmpty() const
434 return pData->length == 0;
438 Returns the current capacity of the String buffer.
440 The capacity
441 is the amount of storage available for newly inserted
442 characters. The real buffer size is 2 bytes longer, because
443 all strings are 0 terminated.
445 @return the current capacity of this string buffer.
447 sal_Int32 getCapacity() const
449 return nCapacity;
453 Ensures that the capacity of the buffer is at least equal to the
454 specified minimum.
456 The new capacity will be at least as large as the maximum of the current
457 length (so that no contents of the buffer is destroyed) and the given
458 minimumCapacity. If the given minimumCapacity is negative, nothing is
459 changed.
461 @param minimumCapacity the minimum desired capacity.
463 void ensureCapacity(sal_Int32 minimumCapacity)
465 rtl_uStringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
469 Sets the length of this String buffer.
471 If the <code>newLength</code> argument is less than the current
472 length of the string buffer, the string buffer is truncated to
473 contain exactly the number of characters given by the
474 <code>newLength</code> argument.
476 If the <code>newLength</code> argument is greater than or equal
477 to the current length, sufficient null characters
478 (<code>'&#92;u0000'</code>) are appended to the string buffer so that
479 length becomes the <code>newLength</code> argument.
481 The <code>newLength</code> argument must be greater than or equal
482 to <code>0</code>.
484 @param newLength the new length of the buffer.
486 void setLength(sal_Int32 newLength)
488 assert(newLength >= 0);
489 // Avoid modifications if pData points to const empty string:
490 if( newLength != pData->length )
492 if( newLength > nCapacity )
493 rtl_uStringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
494 else
495 pData->buffer[newLength] = 0;
496 pData->length = newLength;
501 Returns the character at a specific index in this string buffer.
503 The first character of a string buffer is at index
504 <code>0</code>, the next at index <code>1</code>, and so on, for
505 array indexing.
507 The index argument must be greater than or equal to
508 <code>0</code>, and less than the length of this string buffer.
510 @param index the index of the desired character.
511 @return the character at the specified index of this string buffer.
513 SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
514 sal_Unicode charAt( sal_Int32 index ) const
516 assert(index >= 0 && index < pData->length);
517 return pData->buffer[ index ];
521 The character at the specified index of this string buffer is set
522 to <code>ch</code>.
524 The index argument must be greater than or equal to
525 <code>0</code>, and less than the length of this string buffer.
527 @param index the index of the character to modify.
528 @param ch the new character.
530 SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
531 OUStringBuffer & setCharAt(sal_Int32 index, sal_Unicode ch)
533 assert(index >= 0 && index < pData->length);
534 pData->buffer[ index ] = ch;
535 return *this;
539 Return a null terminated unicode character array.
541 const sal_Unicode* getStr() const SAL_RETURNS_NONNULL { return pData->buffer; }
544 Access to individual characters.
546 @param index must be non-negative and less than length.
548 @return a reference to the character at the given index.
550 @since LibreOffice 3.5
552 sal_Unicode & operator [](sal_Int32 index)
554 assert(index >= 0 && index < pData->length);
555 return pData->buffer[index];
559 Access to individual characters.
561 @param index must be non-negative and less than length.
563 @return a reference to the character at the given index.
565 @since LibreOffice 4.2
567 const sal_Unicode & operator [](sal_Int32 index) const
569 assert(index >= 0 && index < pData->length);
570 return pData->buffer[index];
574 Return an OUString instance reflecting the current content
575 of this OUStringBuffer.
577 OUString toString() const
579 return OUString(pData->buffer, pData->length);
583 Appends the string to this string buffer.
585 The characters of the <code>OUString</code> argument are appended, in
586 order, to the contents of this string buffer, increasing the
587 length of this string buffer by the length of the argument.
589 @param str a string.
590 @return this string buffer.
592 #if !defined LIBO_INTERNAL_ONLY
593 OUStringBuffer & append(const OUString &str)
594 #else
595 OUStringBuffer & append(std::u16string_view str)
596 #endif
598 return insert(getLength(), str);
601 #if !defined LIBO_INTERNAL_ONLY
603 Appends the content of a stringbuffer to this string buffer.
605 The characters of the <code>OUStringBuffer</code> argument are appended, in
606 order, to the contents of this string buffer, increasing the
607 length of this string buffer by the length of the argument.
609 @param str a string.
610 @return this string buffer.
612 @since LibreOffice 4.0
614 OUStringBuffer & append(const OUStringBuffer &str)
616 if(!str.isEmpty())
618 append( str.getStr(), str.getLength() );
620 return *this;
622 #endif
625 Appends the string representation of the <code>char</code> array
626 argument to this string buffer.
628 The characters of the array argument are appended, in order, to
629 the contents of this string buffer. The length of this string
630 buffer increases by the length of the argument.
632 @param str the characters to be appended.
633 @return this string buffer.
635 #if defined LIBO_INTERNAL_ONLY
636 template<typename T>
637 typename libreoffice_internal::CharPtrDetector<T, OUStringBuffer &>::TypeUtf16
638 append(T const & str)
639 #else
640 OUStringBuffer & append( const sal_Unicode * str )
641 #endif
643 return insert(getLength(), str);
647 Appends the string representation of the <code>char</code> array
648 argument to this string buffer.
650 Characters of the character array <code>str</code> are appended,
651 in order, to the contents of this string buffer. The length of this
652 string buffer increases by the value of <code>len</code>.
654 @param str the characters to be appended; must be non-null, and must
655 point to at least len characters
656 @param len the number of characters to append; must be non-negative
657 @return this string buffer.
659 OUStringBuffer & append( const sal_Unicode * str, sal_Int32 len)
661 return insert(getLength(), str, len);
665 @overload
666 This function accepts an ASCII string literal as its argument.
667 @since LibreOffice 3.6
669 template< typename T >
670 typename libreoffice_internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type append( T& literal )
672 return insert(getLength(), literal);
675 #if defined LIBO_INTERNAL_ONLY
676 template<typename T>
677 typename libreoffice_internal::NonConstCharArrayDetector<T, OUStringBuffer &>::TypeUtf16
678 append(T & value) { return append(static_cast<sal_Unicode *>(value)); }
680 /** @overload @since LibreOffice 5.3 */
681 template<typename T>
682 typename libreoffice_internal::ConstCharArrayDetector<
683 T, OUStringBuffer &>::TypeUtf16
684 append(T & literal) {
685 return insert(getLength(), literal);
687 #endif
689 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
691 @overload
692 @internal
694 template< typename T1, typename T2 >
695 OUStringBuffer& append( OUStringConcat< T1, T2 >&& c )
697 return insert(getLength(), std::move(c));
699 #endif
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. The ASCII string must be
708 NULL-terminated.
710 The characters of the array argument are appended, in order, to
711 the contents of this string buffer. The length of this string
712 buffer increases by the length of the argument.
714 @param str the 8-Bit ASCII characters to be appended.
715 @return this string buffer.
717 OUStringBuffer & appendAscii( const char * str )
719 return appendAscii( str, rtl_str_getLength( str ) );
723 Appends a 8-Bit ASCII character string to this string buffer.
725 Since this method is optimized for performance. the ASCII
726 character values are not converted in any way. The caller
727 has to make sure that all ASCII characters are in the
728 allowed range between 0 and 127.
730 Characters of the character array <code>str</code> are appended,
731 in order, to the contents of this string buffer. The length of this
732 string buffer increases by the value of <code>len</code>.
734 @param str the 8-Bit ASCII characters to be appended; must be non-null,
735 and must point to at least len characters
736 @param len the number of characters to append; must be non-negative
737 @return this string buffer.
739 OUStringBuffer & appendAscii( const char * str, sal_Int32 len)
741 rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), str, len );
742 return *this;
746 Appends the string representation of the <code>bool</code>
747 argument to the string buffer.
749 The argument is converted to a string as if by the method
750 <code>String.valueOf</code>, and the characters of that
751 string are then appended to this string buffer.
753 @param b a <code>bool</code>.
754 @return this string buffer.
756 @since LibreOffice 4.1
758 OUStringBuffer & append(bool b)
760 return insert(getLength(), b);
763 /// @cond INTERNAL
764 // Pointer can be automatically converted to bool, which is unwanted here.
765 // Explicitly delete all pointer append() overloads to prevent this
766 // (except for char* and sal_Unicode* overloads, which are handled elsewhere).
767 template< typename T >
768 typename libreoffice_internal::Enable< void,
769 !libreoffice_internal::CharPtrDetector< T* >::ok && !libreoffice_internal::SalUnicodePtrDetector< T* >::ok >::Type
770 append( T* ) SAL_DELETED_FUNCTION;
771 /// @endcond
773 // This overload is needed because OUString has a ctor from rtl_uString*, but
774 // the bool overload above would be preferred to the conversion.
776 @internal
778 OUStringBuffer & append(rtl_uString* str)
780 return append( OUString::unacquired( &str ));
784 Appends the string representation of the <code>sal_Bool</code>
785 argument to the string buffer.
787 The argument is converted to a string as if by the method
788 <code>String.valueOf</code>, and the characters of that
789 string are then appended to this string buffer.
791 @param b a <code>sal_Bool</code>.
792 @return this string buffer.
794 OUStringBuffer & append(sal_Bool b)
796 return insert(getLength(), b);
800 Appends the string representation of the ASCII <code>char</code>
801 argument to this string buffer.
803 The argument is appended to the contents of this string buffer.
804 The length of this string buffer increases by <code>1</code>.
806 @param c an ASCII <code>char</code>.
807 @return this string buffer.
809 @since LibreOffice 3.5
811 OUStringBuffer & append(char c)
813 assert(static_cast< unsigned char >(c) <= 0x7F);
814 return insert(getLength(), c);
818 Appends the string representation of the <code>char</code>
819 argument to this string buffer.
821 The argument is appended to the contents of this string buffer.
822 The length of this string buffer increases by <code>1</code>.
824 @param c a <code>char</code>.
825 @return this string buffer.
827 OUStringBuffer & append(sal_Unicode c)
829 return insert(getLength(), c);
832 #if defined LIBO_INTERNAL_ONLY
833 void append(sal_uInt16) = delete;
834 #endif
837 Appends the string representation of the <code>sal_Int32</code>
838 argument to this string buffer.
840 The argument is converted to a string as if by the method
841 <code>String.valueOf</code>, and the characters of that
842 string are then appended to this string buffer.
844 @param i an <code>sal_Int32</code>.
845 @param radix the radix
846 @return this string buffer.
848 OUStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
850 return insert(getLength(), i, radix);
854 Appends the string representation of the <code>long</code>
855 argument to this string buffer.
857 The argument is converted to a string as if by the method
858 <code>String.valueOf</code>, and the characters of that
859 string are then appended to this string buffer.
861 @param l a <code>long</code>.
862 @param radix the radix
863 @return this string buffer.
865 OUStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
867 return insert(getLength(), l, radix);
871 Appends the string representation of the <code>float</code>
872 argument to this string buffer.
874 The argument is converted to a string as if by the method
875 <code>String.valueOf</code>, and the characters of that
876 string are then appended to this string buffer.
878 @param f a <code>float</code>.
879 @return this string buffer.
881 OUStringBuffer & append(float f)
883 return insert(getLength(), f);
887 Appends the string representation of the <code>double</code>
888 argument to this string buffer.
890 The argument is converted to a string as if by the method
891 <code>String.valueOf</code>, and the characters of that
892 string are then appended to this string buffer.
894 @param d a <code>double</code>.
895 @return this string buffer.
897 OUStringBuffer & append(double d)
899 return insert(getLength(), d);
903 Appends a single UTF-32 character to this string buffer.
905 <p>The single UTF-32 character will be represented within the string
906 buffer as either one or two UTF-16 code units.</p>
908 @param c a well-formed UTF-32 code unit (that is, a value in the range
909 <code>0</code>&ndash;<code>0x10FFFF</code>, but excluding
910 <code>0xD800</code>&ndash;<code>0xDFFF</code>)
912 @return
913 this string buffer
915 OUStringBuffer & appendUtf32(sal_uInt32 c) {
916 return insertUtf32(getLength(), c);
920 Unsafe way to make space for a fixed amount of characters to be appended
921 into this OUStringBuffer.
923 A call to this function must immediately be followed by code that
924 completely fills the uninitialized block pointed to by the return value.
926 @param length the length of the uninitialized block of sal_Unicode
927 entities; must be non-negative
929 @return a pointer to the start of the uninitialized block; only valid
930 until this OUStringBuffer's capacity changes
932 @since LibreOffice 4.4
934 sal_Unicode * appendUninitialized(sal_Int32 length) SAL_RETURNS_NONNULL {
935 sal_Int32 n = getLength();
936 rtl_uStringbuffer_insert(&pData, &nCapacity, n, NULL, length);
937 return pData->buffer + n;
940 #if defined LIBO_INTERNAL_ONLY
942 "Stream" operator to append a value to this OUStringBuffer.
944 @internal
945 @since LibreOffice 7.5
947 template<typename T>
948 OUStringBuffer& operator<<(T&& rValue)
950 return append(std::forward<T>(rValue));
952 #endif
955 Inserts the string into this string buffer.
957 The characters of the <code>String</code> argument are inserted, in
958 order, into this string buffer at the indicated offset. The length
959 of this string buffer is increased by the length of the argument.
961 The offset argument must be greater than or equal to
962 <code>0</code>, and less than or equal to the length of this
963 string buffer.
965 @param offset the offset.
966 @param str a string.
967 @return this string buffer.
969 #if defined LIBO_INTERNAL_ONLY
970 OUStringBuffer & insert(sal_Int32 offset, std::u16string_view str)
972 if (str.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max())) {
973 throw std::bad_alloc();
975 return insert( offset, str.data(), str.length() );
977 #else
978 OUStringBuffer & insert(sal_Int32 offset, const OUString & str)
980 return insert( offset, str.getStr(), str.getLength() );
982 #endif
984 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
986 @overload
987 @internal
989 template <typename T1, typename T2>
990 OUStringBuffer& insert(sal_Int32 offset, OUStringConcat<T1, T2>&& c)
992 const size_t l = c.length();
993 if (l == 0)
994 return *this;
995 if (l > o3tl::make_unsigned(std::numeric_limits<sal_Int32>::max() - pData->length))
996 throw std::bad_alloc();
998 rtl_uStringbuffer_insert(&pData, &nCapacity, offset, nullptr, l);
1000 /* insert the new characters */
1001 c.addData(pData->buffer + offset);
1002 return *this;
1004 #endif
1007 Inserts the string representation of the <code>char</code> array
1008 argument into this string buffer.
1010 The characters of the array argument are inserted into the
1011 contents of this string buffer at the position indicated by
1012 <code>offset</code>. The length of this string buffer increases by
1013 the length of the argument.
1015 The offset argument must be greater than or equal to
1016 <code>0</code>, and less than or equal to the length of this
1017 string buffer.
1019 @param offset the offset.
1020 @param str a character array.
1021 @return this string buffer.
1023 OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str )
1025 return insert( offset, str, rtl_ustr_getLength( str ) );
1029 Inserts the string representation of the <code>char</code> array
1030 argument into this string buffer.
1032 The characters of the array argument are inserted into the
1033 contents of this string buffer at the position indicated by
1034 <code>offset</code>. The length of this string buffer increases by
1035 the length of the argument.
1037 The offset argument must be greater than or equal to
1038 <code>0</code>, and less than or equal to the length of this
1039 string buffer.
1041 @param offset the offset.
1042 @param str a character array.
1043 @param len the number of characters to append.
1044 @return this string buffer.
1046 OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str, sal_Int32 len)
1048 assert( len == 0 || str != NULL ); // cannot assert that in rtl_uStringbuffer_insert
1049 rtl_uStringbuffer_insert( &pData, &nCapacity, offset, str, len );
1050 return *this;
1054 @overload
1055 This function accepts an ASCII string literal as its argument.
1056 @since LibreOffice 3.6
1058 template< typename T >
1059 typename libreoffice_internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type insert( sal_Int32 offset, T& literal )
1061 assert(
1062 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1063 rtl_uStringbuffer_insert_ascii(
1064 &pData, &nCapacity, offset,
1065 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1066 libreoffice_internal::ConstCharArrayDetector<T>::length);
1067 return *this;
1070 #if defined LIBO_INTERNAL_ONLY
1071 /** @overload @since LibreOffice 5.3 */
1072 template<typename T>
1073 typename libreoffice_internal::ConstCharArrayDetector<
1074 T, OUStringBuffer &>::TypeUtf16
1075 insert(sal_Int32 offset, T & literal) {
1076 return insert(
1077 offset,
1078 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1079 libreoffice_internal::ConstCharArrayDetector<T>::length);
1081 #endif
1084 Inserts the string representation of the <code>sal_Bool</code>
1085 argument into this string buffer.
1087 The second argument is converted to a string as if by the method
1088 <code>String.valueOf</code>, and the characters of that
1089 string are then inserted into this string buffer at the indicated
1090 offset.
1092 The offset argument must be greater than or equal to
1093 <code>0</code>, and less than or equal to the length of this
1094 string buffer.
1096 @param offset the offset.
1097 @param b a <code>sal_Bool</code>.
1098 @return this string buffer.
1100 OUStringBuffer & insert(sal_Int32 offset, sal_Bool b)
1102 sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
1103 return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
1107 Inserts the string representation of the <code>bool</code>
1108 argument into this string buffer.
1110 The second argument is converted to a string as if by the method
1111 <code>OUString::boolean</code>, and the characters of that
1112 string are then inserted into this string buffer at the indicated
1113 offset.
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 b a <code>bool</code>.
1121 @return this string buffer.
1123 @since LibreOffice 4.3
1125 OUStringBuffer & insert(sal_Int32 offset, bool b)
1127 sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
1128 return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
1132 Inserts the string representation of the <code>char</code>
1133 argument into this string buffer.
1135 The second argument is inserted into the contents of this string
1136 buffer at the position indicated by <code>offset</code>. The length
1137 of this string buffer increases by one.
1139 The offset argument must be greater than or equal to
1140 <code>0</code>, and less than or equal to the length of this
1141 string buffer.
1143 @param offset the offset.
1144 @param c a <code>char</code>.
1145 @return this string buffer.
1147 @since LibreOffice 3.6
1149 OUStringBuffer & insert(sal_Int32 offset, char c)
1151 sal_Unicode u = c;
1152 return insert( offset, &u, 1 );
1156 Inserts the string representation of the <code>char</code>
1157 argument into this string buffer.
1159 The second argument is inserted into the contents of this string
1160 buffer at the position indicated by <code>offset</code>. The length
1161 of this string buffer increases by one.
1163 The offset argument must be greater than or equal to
1164 <code>0</code>, and less than or equal to the length of this
1165 string buffer.
1167 @param offset the offset.
1168 @param c a <code>char</code>.
1169 @return this string buffer.
1171 OUStringBuffer & insert(sal_Int32 offset, sal_Unicode c)
1173 return insert( offset, &c, 1 );
1177 Inserts the string representation of the second <code>sal_Int32</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 i an <code>sal_Int32</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_Int32 i, sal_Int16 radix = 10 )
1197 sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT32];
1198 return insert( offset, sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
1202 Inserts the string representation of the <code>long</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 l a <code>long</code>.
1216 @param radix the radix.
1217 @return this string buffer.
1218 @exception StringIndexOutOfBoundsException if the offset is invalid.
1220 OUStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
1222 sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT64];
1223 return insert( offset, sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
1227 Inserts the string representation of the <code>float</code>
1228 argument into this string buffer.
1230 The second argument is converted to a string as if by the method
1231 <code>String.valueOf</code>, and the characters of that
1232 string are then inserted into this string buffer at the indicated
1233 offset.
1235 The offset argument must be greater than or equal to
1236 <code>0</code>, and less than or equal to the length of this
1237 string buffer.
1239 @param offset the offset.
1240 @param f a <code>float</code>.
1241 @return this string buffer.
1242 @exception StringIndexOutOfBoundsException if the offset is invalid.
1244 OUStringBuffer & insert(sal_Int32 offset, float f)
1246 // Same as rtl::str::valueOfFP, used for rtl_ustr_valueOfFloat
1247 rtl_math_doubleToUString(&pData, &nCapacity, offset, f, rtl_math_StringFormat_G,
1248 RTL_USTR_MAX_VALUEOFFLOAT - SAL_N_ELEMENTS("-x.E-xxx") + 1, '.',
1249 NULL, 0, true);
1250 return *this;
1254 Inserts the string representation of the <code>double</code>
1255 argument into this string buffer.
1257 The second argument is converted to a string as if by the method
1258 <code>String.valueOf</code>, and the characters of that
1259 string are then inserted into this string buffer at the indicated
1260 offset.
1262 The offset argument must be greater than or equal to
1263 <code>0</code>, and less than or equal to the length of this
1264 string buffer.
1266 @param offset the offset.
1267 @param d a <code>double</code>.
1268 @return this string buffer.
1269 @exception StringIndexOutOfBoundsException if the offset is invalid.
1271 OUStringBuffer & insert(sal_Int32 offset, double d)
1273 // Same as rtl::str::valueOfFP, used for rtl_ustr_valueOfDouble
1274 rtl_math_doubleToUString(&pData, &nCapacity, offset, d, rtl_math_StringFormat_G,
1275 RTL_USTR_MAX_VALUEOFDOUBLE - SAL_N_ELEMENTS("-x.E-xxx") + 1, '.',
1276 NULL, 0, true);
1277 return *this;
1281 Inserts a single UTF-32 character into this string buffer.
1283 <p>The single UTF-32 character will be represented within the string
1284 buffer as either one or two UTF-16 code units.</p>
1286 @param offset the offset into this string buffer (from zero to the length
1287 of this string buffer, inclusive)
1289 @param c a well-formed UTF-32 code unit (that is, a value in the range
1290 <code>0</code>&ndash;<code>0x10FFFF</code>, but excluding
1291 <code>0xD800</code>&ndash;<code>0xDFFF</code>)
1293 @return this string buffer
1295 OUStringBuffer & insertUtf32(sal_Int32 offset, sal_uInt32 c) {
1296 rtl_uStringbuffer_insertUtf32(&pData, &nCapacity, offset, c);
1297 return *this;
1301 Removes the characters in a substring of this sequence.
1303 The substring begins at the specified <code>start</code> and
1304 is <code>len</code> characters long.
1306 start must be >= 0 && <= This->length
1308 @param start The beginning index, inclusive
1309 @param len The substring length
1310 @return this string buffer.
1312 OUStringBuffer & remove( sal_Int32 start, sal_Int32 len )
1314 rtl_uStringbuffer_remove( &pData, start, len );
1315 return *this;
1319 Removes the tail of a string buffer start at the indicate position
1321 start must be >= 0 && <= This->length
1323 @param start The beginning index, inclusive. default to 0
1324 @return this string buffer.
1326 @since LibreOffice 4.0
1328 OUStringBuffer & truncate( sal_Int32 start = 0 )
1330 rtl_uStringbuffer_remove( &pData, start, getLength() - start );
1331 return *this;
1335 Replace all occurrences of
1336 oldChar in this string buffer with newChar.
1338 @since LibreOffice 4.0
1340 @param oldChar the old character.
1341 @param newChar the new character.
1342 @return this string buffer
1344 OUStringBuffer& replace( sal_Unicode oldChar, sal_Unicode newChar )
1346 sal_Int32 index = 0;
1347 while((index = indexOf(oldChar, index)) >= 0)
1349 pData->buffer[ index ] = newChar;
1351 return *this;
1354 /** Allows access to the internal data of this OUStringBuffer, for effective
1355 manipulation.
1357 This method should be used with care. After you have called this
1358 method, you may use the returned pInternalData or pInternalCapacity only
1359 as long as you make no other method call on this OUStringBuffer.
1361 @param pInternalData
1362 This output parameter receives a pointer to the internal data
1363 (rtl_uString pointer). pInternalData itself must not be null.
1365 @param pInternalCapacity
1366 This output parameter receives a pointer to the internal capacity.
1367 pInternalCapacity itself must not be null.
1369 void accessInternals(rtl_uString *** pInternalData,
1370 sal_Int32 ** pInternalCapacity)
1372 *pInternalData = &pData;
1373 *pInternalCapacity = &nCapacity;
1378 Returns the index within this string of the first occurrence of the
1379 specified character, starting the search at the specified index.
1381 @since LibreOffice 4.0
1383 @param ch character to be located.
1384 @param fromIndex the index to start the search from.
1385 The index must be greater or equal than 0
1386 and less or equal as the string length.
1387 @return the index of the first occurrence of the character in the
1388 character sequence represented by this string that is
1389 greater than or equal to fromIndex, or
1390 -1 if the character does not occur.
1392 sal_Int32 indexOf( sal_Unicode ch, sal_Int32 fromIndex = 0 ) const
1394 assert( fromIndex >= 0 && fromIndex <= pData->length );
1395 sal_Int32 ret = rtl_ustr_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch );
1396 return (ret < 0 ? ret : ret+fromIndex);
1400 Returns the index within this string of the last occurrence of the
1401 specified character, searching backward starting at the end.
1403 @since LibreOffice 4.0
1405 @param ch character to be located.
1406 @return the index of the last occurrence of the character in the
1407 character sequence represented by this string, or
1408 -1 if the character does not occur.
1410 sal_Int32 lastIndexOf( sal_Unicode ch ) const
1412 return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch );
1416 Returns the index within this string of the last occurrence of the
1417 specified character, searching backward starting before the specified
1418 index.
1420 @since LibreOffice 4.0
1422 @param ch character to be located.
1423 @param fromIndex the index before which to start the search.
1424 @return the index of the last occurrence of the character in the
1425 character sequence represented by this string that
1426 is less than fromIndex, or -1
1427 if the character does not occur before that point.
1429 sal_Int32 lastIndexOf( sal_Unicode ch, sal_Int32 fromIndex ) const
1431 assert( fromIndex >= 0 && fromIndex <= pData->length );
1432 return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch );
1436 Returns the index within this string of the first occurrence of the
1437 specified substring, starting at the specified index.
1439 If str doesn't include any character, always -1 is
1440 returned. This is also the case, if both strings are empty.
1442 @since LibreOffice 4.0
1444 @param str the substring to search for.
1445 @param fromIndex the index to start the search from.
1446 @return If the string argument occurs one or more times as a substring
1447 within this string at the starting index, then the index
1448 of the first character of the first such substring is
1449 returned. If it does not occur as a substring starting
1450 at fromIndex or beyond, -1 is returned.
1452 #if defined LIBO_INTERNAL_ONLY
1453 sal_Int32 indexOf( std::u16string_view str, sal_Int32 fromIndex = 0 ) const
1455 assert( fromIndex >= 0 && fromIndex <= pData->length );
1456 sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1457 str.data(), str.length() );
1458 return (ret < 0 ? ret : ret+fromIndex);
1460 #else
1461 sal_Int32 indexOf( const OUString & str, sal_Int32 fromIndex = 0 ) const
1463 assert( fromIndex >= 0 && fromIndex <= pData->length );
1464 sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1465 str.pData->buffer, str.pData->length );
1466 return (ret < 0 ? ret : ret+fromIndex);
1468 #endif
1471 @overload
1472 This function accepts an ASCII string literal as its argument.
1474 @since LibreOffice 4.0
1476 template< typename T >
1477 typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const
1479 assert(
1480 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1481 sal_Int32 n = rtl_ustr_indexOfAscii_WithLength(
1482 pData->buffer + fromIndex, pData->length - fromIndex,
1483 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1484 libreoffice_internal::ConstCharArrayDetector<T>::length);
1485 return n < 0 ? n : n + fromIndex;
1488 #if defined LIBO_INTERNAL_ONLY
1489 /** @overload @since LibreOffice 5.3 */
1490 template<typename T>
1491 typename
1492 libreoffice_internal::ConstCharArrayDetector<T, sal_Int32>::TypeUtf16
1493 indexOf(T & literal, sal_Int32 fromIndex = 0) const {
1494 assert(fromIndex >= 0);
1495 auto n = rtl_ustr_indexOfStr_WithLength(
1496 pData->buffer + fromIndex, pData->length - fromIndex,
1497 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1498 libreoffice_internal::ConstCharArrayDetector<T>::length);
1499 return n < 0 ? n : n + fromIndex;
1501 #endif
1504 Returns the index within this string of the last occurrence of
1505 the specified substring, searching backward starting at the end.
1507 The returned index indicates the starting index of the substring
1508 in this string.
1509 If str doesn't include any character, always -1 is
1510 returned. This is also the case, if both strings are empty.
1512 @since LibreOffice 4.0
1514 @param str the substring to search for.
1515 @return If the string argument occurs one or more times as a substring
1516 within this string, then the index of the first character of
1517 the last such substring is returned. If it does not occur as
1518 a substring, -1 is returned.
1520 #if defined LIBO_INTERNAL_ONLY
1521 sal_Int32 lastIndexOf( std::u16string_view str ) const
1523 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1524 str.data(), str.length() );
1526 #else
1527 sal_Int32 lastIndexOf( const OUString & str ) const
1529 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1530 str.pData->buffer, str.pData->length );
1532 #endif
1535 Returns the index within this string of the last occurrence of
1536 the specified substring, searching backward starting before the specified
1537 index.
1539 The returned index indicates the starting index of the substring
1540 in this string.
1541 If str doesn't include any character, always -1 is
1542 returned. This is also the case, if both strings are empty.
1544 @since LibreOffice 4.0
1546 @param str the substring to search for.
1547 @param fromIndex the index before which to start the search.
1548 @return If the string argument occurs one or more times as a substring
1549 within this string before the starting index, then the index
1550 of the first character of the last such substring is
1551 returned. Otherwise, -1 is returned.
1553 #if defined LIBO_INTERNAL_ONLY
1554 sal_Int32 lastIndexOf( std::u16string_view str, sal_Int32 fromIndex ) const
1556 assert( fromIndex >= 0 && fromIndex <= pData->length );
1557 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1558 str.data(), str.length() );
1560 #else
1561 sal_Int32 lastIndexOf( const OUString & str, sal_Int32 fromIndex ) const
1563 assert( fromIndex >= 0 && fromIndex <= pData->length );
1564 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1565 str.pData->buffer, str.pData->length );
1567 #endif
1570 @overload
1571 This function accepts an ASCII string literal as its argument.
1572 @since LibreOffice 4.0
1574 template< typename T >
1575 typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type lastIndexOf( T& literal ) const
1577 assert(
1578 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1579 return rtl_ustr_lastIndexOfAscii_WithLength(
1580 pData->buffer, pData->length,
1581 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1582 libreoffice_internal::ConstCharArrayDetector<T>::length);
1585 #if defined LIBO_INTERNAL_ONLY
1586 /** @overload @since LibreOffice 5.3 */
1587 template<typename T>
1588 typename
1589 libreoffice_internal::ConstCharArrayDetector<T, sal_Int32>::TypeUtf16
1590 lastIndexOf(T & literal) const {
1591 return rtl_ustr_lastIndexOfStr_WithLength(
1592 pData->buffer, pData->length,
1593 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1594 libreoffice_internal::ConstCharArrayDetector<T>::length);
1596 #endif
1599 Strip the given character from the start of the buffer.
1601 @since LibreOffice 4.0
1603 @param c the character to strip
1604 @return The number of characters stripped
1607 sal_Int32 stripStart(sal_Unicode c = ' ')
1609 sal_Int32 index;
1610 for(index = 0; index < getLength() ; index++)
1612 if(pData->buffer[ index ] != c)
1614 break;
1617 if(index)
1619 remove(0, index);
1621 return index;
1625 Strip the given character from the end of the buffer.
1627 @since LibreOffice 4.0
1629 @param c the character to strip
1630 @return The number of characters stripped
1633 sal_Int32 stripEnd(sal_Unicode c = ' ')
1635 sal_Int32 result = getLength();
1636 sal_Int32 index;
1637 for(index = getLength(); index > 0 ; index--)
1639 if(pData->buffer[ index - 1 ] != c)
1641 break;
1644 if(index < getLength())
1646 truncate(index);
1648 return result - getLength();
1651 Strip the given character from the both end of the buffer.
1653 @since LibreOffice 4.0
1655 @param c the character to strip
1656 @return The number of characters stripped
1659 sal_Int32 strip(sal_Unicode c = ' ')
1661 return stripStart(c) + stripEnd(c);
1664 #if defined LIBO_INTERNAL_ONLY
1666 Returns a std::u16string_view that is a view of a substring of this string.
1668 The substring begins at the specified beginIndex. If
1669 beginIndex is negative or be greater than the length of
1670 this string, behaviour is undefined.
1672 @param beginIndex the beginning index, inclusive.
1673 @return the specified substring.
1675 SAL_WARN_UNUSED_RESULT std::u16string_view subView( sal_Int32 beginIndex ) const
1677 assert(beginIndex >= 0);
1678 assert(beginIndex <= getLength());
1679 return subView(beginIndex, getLength() - beginIndex);
1683 Returns a std::u16string_view that is a view of a substring of this string.
1685 The substring begins at the specified beginIndex and contains count
1686 characters. If either beginIndex or count are negative,
1687 or beginIndex + count are greater than the length of this string
1688 then behaviour is undefined.
1690 @param beginIndex the beginning index, inclusive.
1691 @param count the number of characters.
1692 @return the specified substring.
1694 SAL_WARN_UNUSED_RESULT std::u16string_view subView( sal_Int32 beginIndex, sal_Int32 count ) const
1696 assert(beginIndex >= 0);
1697 assert(count >= 0);
1698 assert(beginIndex <= getLength());
1699 assert(count <= getLength() - beginIndex);
1700 return std::u16string_view(pData->buffer, sal_uInt32(pData->length)).substr(beginIndex, count);
1702 #endif
1705 Returns a new string buffer that is a substring of this string.
1707 The substring begins at the specified beginIndex. If
1708 beginIndex is negative or be greater than the length of
1709 this string, behaviour is undefined.
1711 @param beginIndex the beginning index, inclusive.
1712 @return the specified substring.
1713 @since LibreOffice 4.1
1715 OUStringBuffer copy( sal_Int32 beginIndex ) const
1717 return copy( beginIndex, getLength() - beginIndex );
1721 Returns a new string buffer that is a substring of this string.
1723 The substring begins at the specified beginIndex and contains count
1724 characters. If either beginIndex or count are negative,
1725 or beginIndex + count are greater than the length of this string
1726 then behaviour is undefined.
1728 @param beginIndex the beginning index, inclusive.
1729 @param count the number of characters.
1730 @return the specified substring.
1731 @since LibreOffice 4.1
1733 OUStringBuffer copy( sal_Int32 beginIndex, sal_Int32 count ) const
1735 assert(beginIndex >= 0 && beginIndex <= getLength());
1736 assert(count >= 0 && count <= getLength() - beginIndex);
1737 rtl_uString *pNew = NULL;
1738 rtl_uStringbuffer_newFromStr_WithLength( &pNew, getStr() + beginIndex, count );
1739 return OUStringBuffer( pNew, count + 16 );
1742 private:
1743 OUStringBuffer( rtl_uString * value, const sal_Int32 capacity )
1745 pData = value;
1746 nCapacity = capacity;
1750 A pointer to the data structure which contains the data.
1752 rtl_uString * pData;
1755 The len of the pData->buffer.
1757 sal_Int32 nCapacity;
1760 #if defined LIBO_INTERNAL_ONLY
1761 template<> struct ToStringHelper<OUStringBuffer> {
1762 static std::size_t length(OUStringBuffer const & s) { return s.getLength(); }
1764 sal_Unicode * operator()(sal_Unicode * buffer, OUStringBuffer const & s) const SAL_RETURNS_NONNULL
1765 { return addDataHelper(buffer, s.getStr(), s.getLength()); }
1767 #endif
1769 #if defined LIBO_INTERNAL_ONLY
1770 // Define this here to avoid circular includes
1771 inline OUString & OUString::operator+=( const OUStringBuffer & str ) &
1773 // Call operator= if this is empty, otherwise rtl_uString_newConcat will attempt to
1774 // acquire() the str.pData buffer, which is part of the OUStringBuffer mutable state.
1775 if (isEmpty())
1776 return operator=(str.toString());
1777 else
1778 return internalAppend(str.pData);
1781 inline OUString const& OUString::unacquired(const OUStringBuffer& str)
1783 return unacquired(&str.pData);
1785 #endif
1788 #ifdef RTL_STRING_UNITTEST
1789 namespace rtl
1791 typedef rtlunittest::OUStringBuffer OUStringBuffer;
1793 #endif
1795 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
1796 using ::rtl::OUStringBuffer;
1797 #endif
1799 #endif // INCLUDED_RTL_USTRBUF_HXX
1801 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */