1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
34 #if defined LIBO_INTERNAL_ONLY
35 #include <string_view>
36 #include <type_traits>
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"
50 #ifdef RTL_STRING_UNITTEST
51 extern bool rtl_string_unittest_invalid_conversion
;
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
66 #ifdef RTL_STRING_UNITTEST
70 /** A string buffer implements a mutable sequence of characters.
72 class SAL_WARN_UNUSED OUStringBuffer
74 friend class OUString
;
77 Constructs a string buffer with no characters in it and an
78 initial capacity of 16 characters.
84 rtl_uString_new_WithLength( &pData
, nCapacity
);
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
)
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
)
108 , nCapacity( length
)
110 rtl_uString_new_WithLength( &pData
, length
);
112 #if defined LIBO_INTERNAL_ONLY
114 explicit OUStringBuffer(T length
, std::enable_if_t
<std::is_integral_v
<T
>, int> = 0)
115 : OUStringBuffer(static_cast<sal_Int32
>(length
))
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;
130 explicit OUStringBuffer(char16_t
) = delete;
131 explicit OUStringBuffer(char32_t
) = delete;
135 Constructs a string buffer so that it represents the same
136 sequence of characters as the string argument.
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
)
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() );
155 OUStringBuffer(const OUString
& value
)
157 , nCapacity( value
.getLength() + 16 )
159 rtl_uStringbuffer_newFromStr_WithLength( &pData
, value
.getStr(), value
.getLength() );
163 template< typename T
>
164 OUStringBuffer( T
& literal
, typename
libreoffice_internal::ConstCharArrayDetector
< T
, libreoffice_internal::Dummy
>::Type
= libreoffice_internal::Dummy() )
166 , nCapacity( libreoffice_internal::ConstCharArrayDetector
<T
>::length
+ 16 )
169 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
170 rtl_uString_newFromLiteral(
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;
179 #if defined LIBO_INTERNAL_ONLY
180 /** @overload @since LibreOffice 5.3 */
184 typename
libreoffice_internal::ConstCharArrayDetector
<
185 T
, libreoffice_internal::Dummy
>::TypeUtf16
186 = libreoffice_internal::Dummy()):
188 nCapacity(libreoffice_internal::ConstCharArrayDetector
<T
>::length
+ 16)
190 rtl_uStringbuffer_newFromStr_WithLength(
192 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
193 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
197 #if defined LIBO_INTERNAL_ONLY && defined RTL_STRING_UNITTEST
200 * Only used by unittests to detect incorrect conversions.
203 template< typename T
>
204 OUStringBuffer( T
&, typename
libreoffice_internal::ExceptConstCharArrayDetector
< T
>::Type
= libreoffice_internal::Dummy() )
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.
215 template< typename T
>
216 OUStringBuffer( const T
&, typename
libreoffice_internal::ExceptCharArrayDetector
< T
>::Type
= libreoffice_internal::Dummy() )
220 rtl_uString_newFromLiteral( &pData
, "!!br0ken!!", 10, 0 ); // set to garbage
221 rtl_string_unittest_invalid_conversion
= true;
226 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
231 template< typename T1
, typename T2
>
232 OUStringBuffer( OUStringConcat
< T1
, T2
>&& c
)
234 const sal_Int32 l
= c
.length();
236 pData
= rtl_uString_alloc( nCapacity
);
237 sal_Unicode
* end
= c
.addData( pData
->buffer
);
246 template< std::size_t N
>
247 OUStringBuffer( OUStringNumber
< N
>&& n
)
249 , nCapacity( n
.length
+ 16 )
251 rtl_uStringbuffer_newFromStr_WithLength( &pData
, n
.buf
, n
.length
);
255 #if defined LIBO_INTERNAL_ONLY
256 operator std::u16string_view() const { return {getStr(), sal_uInt32(getLength())}; }
259 /** Assign to this a copy of value.
261 OUStringBuffer
& operator = ( const OUStringBuffer
& value
)
265 rtl_uStringbuffer_newFromStringBuffer(&pData
,
268 nCapacity
= value
.nCapacity
;
273 #if defined LIBO_INTERNAL_ONLY
275 * @since LibreOffice 7.3
277 OUStringBuffer
& operator = ( OUStringBuffer
&& value
) noexcept
279 rtl_uString_release( pData
);
281 nCapacity
= value
.nCapacity
;
282 value
.pData
= nullptr;
284 rtl_uString_new( &value
.pData
);
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
300 pData
->buffer
, string
.data(),
301 n
* sizeof (sal_Unicode
));
302 pData
->buffer
[n
] = '\0';
307 OUStringBuffer
& operator =(OUString
const & string
) {
308 sal_Int32 n
= string
.getLength();
309 if (n
>= nCapacity
) {
310 ensureCapacity(n
+ 16); //TODO: check for overflow
313 pData
->buffer
, string
.pData
->buffer
,
314 (n
+ 1) * sizeof (sal_Unicode
));
320 /** Assign from a string literal.
322 @since LibreOffice 5.3
326 libreoffice_internal::ConstCharArrayDetector
<T
, OUStringBuffer
&>::Type
327 operator =(T
& literal
) {
329 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
331 = libreoffice_internal::ConstCharArrayDetector
<T
>::length
;
332 if (n
>= nCapacity
) {
333 ensureCapacity(n
+ 16); //TODO: check for overflow
336 = libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
338 sal_Unicode
* to
= pData
->buffer
;
339 for (sal_Int32 i
= 0; i
<= n
; ++i
) {
346 #if defined LIBO_INTERNAL_ONLY
347 /** @overload @since LibreOffice 5.3 */
349 typename
libreoffice_internal::ConstCharArrayDetector
<
350 T
, OUStringBuffer
&>::TypeUtf16
351 operator =(T
& literal
) {
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:
362 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
363 n
* sizeof (sal_Unicode
));
364 pData
->buffer
[n
] = '\0';
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;
383 /** @overload @internal */
384 template<std::size_t N
>
385 OUStringBuffer
& operator =(OUStringNumber
<N
> && n
)
387 return operator =(std::u16string_view(n
));
392 Release the string data.
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
405 @return the string previously contained in the buffer.
407 SAL_WARN_UNUSED_RESULT OUString
makeStringAndClear()
410 rtl_uStringBuffer_makeStringAndClear( &pData
, &nCapacity
),
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;
430 @since LibreOffice 4.1
434 return pData
->length
== 0;
438 Returns the current capacity of the String buffer.
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
453 Ensures that the capacity of the buffer is at least equal to the
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
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>'\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
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
);
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
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
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
;
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.
590 @return this string buffer.
592 #if !defined LIBO_INTERNAL_ONLY
593 OUStringBuffer
& append(const OUString
&str
)
595 OUStringBuffer
& append(std::u16string_view str
)
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.
610 @return this string buffer.
612 @since LibreOffice 4.0
614 OUStringBuffer
& append(const OUStringBuffer
&str
)
618 append( str
.getStr(), str
.getLength() );
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
637 typename
libreoffice_internal::CharPtrDetector
<T
, OUStringBuffer
&>::TypeUtf16
638 append(T
const & str
)
640 OUStringBuffer
& append( const sal_Unicode
* str
)
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
);
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
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 */
682 typename
libreoffice_internal::ConstCharArrayDetector
<
683 T
, OUStringBuffer
&>::TypeUtf16
684 append(T
& literal
) {
685 return insert(getLength(), literal
);
689 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
694 template< typename T1
, typename T2
>
695 OUStringBuffer
& append( OUStringConcat
< T1
, T2
>&& c
)
697 return insert(getLength(), std::move(c
));
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
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
);
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
);
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
;
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.
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;
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>–<code>0x10FFFF</code>, but excluding
910 <code>0xD800</code>–<code>0xDFFF</code>)
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.
945 @since LibreOffice 7.5
948 OUStringBuffer
& operator<<(T
&& rValue
)
950 return append(std::forward
<T
>(rValue
));
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
965 @param offset the offset.
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() );
978 OUStringBuffer
& insert(sal_Int32 offset
, const OUString
& str
)
980 return insert( offset
, str
.getStr(), str
.getLength() );
984 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
989 template <typename T1
, typename T2
>
990 OUStringBuffer
& insert(sal_Int32 offset
, OUStringConcat
<T1
, T2
>&& c
)
992 const size_t l
= c
.length();
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
);
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
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
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
);
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
)
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
);
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
) {
1078 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
1079 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
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
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
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
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
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
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
)
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
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
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
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
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
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
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
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, '.',
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
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
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, '.',
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>–<code>0x10FFFF</code>, but excluding
1291 <code>0xD800</code>–<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
);
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
);
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
);
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
;
1354 /** Allows access to the internal data of this OUStringBuffer, for effective
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
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
);
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
);
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
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
>
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
;
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
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() );
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
);
1535 Returns the index within this string of the last occurrence of
1536 the specified substring, searching backward starting before the specified
1539 The returned index indicates the starting index of the substring
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() );
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
);
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
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
>
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
);
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
= ' ')
1610 for(index
= 0; index
< getLength() ; index
++)
1612 if(pData
->buffer
[ index
] != c
)
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();
1637 for(index
= getLength(); index
> 0 ; index
--)
1639 if(pData
->buffer
[ index
- 1 ] != c
)
1644 if(index
< getLength())
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);
1698 assert(beginIndex
<= getLength());
1699 assert(count
<= getLength() - beginIndex
);
1700 return std::u16string_view(pData
->buffer
, sal_uInt32(pData
->length
)).substr(beginIndex
, count
);
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 );
1743 OUStringBuffer( rtl_uString
* value
, const sal_Int32 capacity
)
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()); }
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.
1776 return operator=(str
.toString());
1778 return internalAppend(str
.pData
);
1781 inline OUString
const& OUString::unacquired(const OUStringBuffer
& str
)
1783 return unacquired(&str
.pData
);
1788 #ifdef RTL_STRING_UNITTEST
1791 typedef rtlunittest::OUStringBuffer OUStringBuffer
;
1795 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
1796 using ::rtl::OUStringBuffer
;
1799 #endif // INCLUDED_RTL_USTRBUF_HXX
1801 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */