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 .
20 #ifndef INCLUDED_RTL_USTRBUF_HXX
21 #define INCLUDED_RTL_USTRBUF_HXX
23 #include "sal/config.h"
30 #if defined LIBO_INTERNAL_ONLY
31 #include <string_view>
34 #include "rtl/ustrbuf.h"
35 #include "rtl/ustring.hxx"
36 #include "rtl/stringutils.hxx"
37 #include "sal/types.h"
39 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
40 #include "rtl/stringconcat.hxx"
43 #ifdef RTL_STRING_UNITTEST
44 extern bool rtl_string_unittest_invalid_conversion
;
47 // The unittest uses slightly different code to help check that the proper
48 // calls are made. The class is put into a different namespace to make
49 // sure the compiler generates a different (if generating also non-inline)
50 // copy of the function and does not merge them together. The class
51 // is "brought" into the proper rtl namespace by a typedef below.
52 #ifdef RTL_STRING_UNITTEST
53 #define rtl rtlunittest
59 #ifdef RTL_STRING_UNITTEST
63 /** A string buffer implements a mutable sequence of characters.
65 class SAL_WARN_UNUSED OUStringBuffer
67 friend class OUString
;
70 Constructs a string buffer with no characters in it and an
71 initial capacity of 16 characters.
77 rtl_uString_new_WithLength( &pData
, nCapacity
);
81 Allocates a new string buffer that contains the same sequence of
82 characters as the string buffer argument.
84 @param value a <code>OUStringBuffer</code>.
86 OUStringBuffer( const OUStringBuffer
& value
)
88 , nCapacity( value
.nCapacity
)
90 rtl_uStringbuffer_newFromStringBuffer( &pData
, value
.nCapacity
, value
.pData
);
94 Constructs a string buffer with no characters in it and an
95 initial capacity specified by the <code>length</code> argument.
97 @param length the initial capacity.
99 explicit OUStringBuffer(int length
)
101 , nCapacity( length
)
103 rtl_uString_new_WithLength( &pData
, length
);
105 #if __cplusplus >= 201103L
106 explicit OUStringBuffer(unsigned int length
)
107 : OUStringBuffer(static_cast<int>(length
))
110 #if SAL_TYPES_SIZEOFLONG == 4
111 // additional overloads for sal_Int32 sal_uInt32
112 explicit OUStringBuffer(long length
)
113 : OUStringBuffer(static_cast<int>(length
))
116 explicit OUStringBuffer(unsigned long length
)
117 : OUStringBuffer(static_cast<int>(length
))
121 // avoid obvious bugs
122 explicit OUStringBuffer(char) = delete;
123 explicit OUStringBuffer(sal_Unicode
) = delete;
127 Constructs a string buffer so that it represents the same
128 sequence of characters as the string argument.
131 capacity of the string buffer is <code>16</code> plus the length
132 of the string argument.
134 @param value the initial contents of the buffer.
136 OUStringBuffer(const OUString
& value
)
138 , nCapacity( value
.getLength() + 16 )
140 rtl_uStringbuffer_newFromStr_WithLength( &pData
, value
.getStr(), value
.getLength() );
143 template< typename T
>
144 OUStringBuffer( T
& literal
, typename
libreoffice_internal::ConstCharArrayDetector
< T
, libreoffice_internal::Dummy
>::Type
= libreoffice_internal::Dummy() )
146 , nCapacity( libreoffice_internal::ConstCharArrayDetector
<T
>::length
+ 16 )
149 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
150 rtl_uString_newFromLiteral(
152 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
153 libreoffice_internal::ConstCharArrayDetector
<T
>::length
, 16);
154 #ifdef RTL_STRING_UNITTEST
155 rtl_string_unittest_const_literal
= true;
159 #if defined LIBO_INTERNAL_ONLY
160 /** @overload @since LibreOffice 5.3 */
164 typename
libreoffice_internal::ConstCharArrayDetector
<
165 T
, libreoffice_internal::Dummy
>::TypeUtf16
166 = libreoffice_internal::Dummy()):
168 nCapacity(libreoffice_internal::ConstCharArrayDetector
<T
>::length
+ 16)
170 rtl_uStringbuffer_newFromStr_WithLength(
172 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
173 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
176 /** @overload @since LibreOffice 5.4 */
177 OUStringBuffer(OUStringLiteral
const & literal
):
178 pData(nullptr), nCapacity(literal
.size
+ 16) //TODO: check for overflow
180 rtl_uString_newFromLiteral(&pData
, literal
.data
, literal
.size
, 16);
184 #ifdef RTL_STRING_UNITTEST
186 * Only used by unittests to detect incorrect conversions.
189 template< typename T
>
190 OUStringBuffer( T
&, typename
libreoffice_internal::ExceptConstCharArrayDetector
< T
>::Type
= libreoffice_internal::Dummy() )
194 rtl_uString_newFromLiteral( &pData
, "!!br0ken!!", 10, 0 ); // set to garbage
195 rtl_string_unittest_invalid_conversion
= true;
198 * Only used by unittests to detect incorrect conversions.
201 template< typename T
>
202 OUStringBuffer( const T
&, typename
libreoffice_internal::ExceptCharArrayDetector
< T
>::Type
= libreoffice_internal::Dummy() )
206 rtl_uString_newFromLiteral( &pData
, "!!br0ken!!", 10, 0 ); // set to garbage
207 rtl_string_unittest_invalid_conversion
= true;
211 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
216 template< typename T1
, typename T2
>
217 OUStringBuffer( OUStringConcat
< T1
, T2
>&& c
)
219 const sal_Int32 l
= c
.length();
221 pData
= rtl_uString_alloc( nCapacity
);
222 sal_Unicode
* end
= c
.addData( pData
->buffer
);
225 // TODO realloc in case pData->>length is noticeably smaller than l ?
228 /** Assign to this a copy of value.
230 OUStringBuffer
& operator = ( const OUStringBuffer
& value
)
234 rtl_uStringbuffer_newFromStringBuffer(&pData
,
237 nCapacity
= value
.nCapacity
;
242 /** Assign from a string.
244 @since LibreOffice 5.3
246 OUStringBuffer
& operator =(OUString
const & string
) {
247 sal_Int32 n
= string
.getLength();
248 if (n
>= nCapacity
) {
249 ensureCapacity(n
+ 16); //TODO: check for overflow
252 pData
->buffer
, string
.pData
->buffer
,
253 (n
+ 1) * sizeof (sal_Unicode
));
258 /** Assign from a string literal.
260 @since LibreOffice 5.3
264 libreoffice_internal::ConstCharArrayDetector
<T
, OUStringBuffer
&>::Type
265 operator =(T
& literal
) {
267 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
269 = libreoffice_internal::ConstCharArrayDetector
<T
>::length
;
270 if (n
>= nCapacity
) {
271 ensureCapacity(n
+ 16); //TODO: check for overflow
274 = libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
276 sal_Unicode
* to
= pData
->buffer
;
277 for (sal_Int32 i
= 0; i
<= n
; ++i
) {
284 #if defined LIBO_INTERNAL_ONLY
285 /** @overload @since LibreOffice 5.3 */
287 typename
libreoffice_internal::ConstCharArrayDetector
<
288 T
, OUStringBuffer
&>::TypeUtf16
289 operator =(T
& literal
) {
291 = libreoffice_internal::ConstCharArrayDetector
<T
>::length
;
292 if (n
>= nCapacity
) {
293 ensureCapacity(n
+ 16); //TODO: check for overflow
297 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
298 (n
+ 1) * sizeof (sal_Unicode
)); //TODO: check for overflow
303 /** @overload @since LibreOffice 5.4 */
304 OUStringBuffer
& operator =(OUStringLiteral
const & literal
) {
305 sal_Int32
const n
= literal
.size
;
306 if (n
>= nCapacity
) {
307 ensureCapacity(n
+ 16); //TODO: check for overflow
309 char const * from
= literal
.data
;
310 sal_Unicode
* to
= pData
->buffer
;
311 for (sal_Int32 i
= 0; i
<= n
; ++i
) {
319 #if defined LIBO_INTERNAL_ONLY
320 /** @overload @since LibreOffice 5.3 */
321 template<typename T1
, typename T2
>
322 OUStringBuffer
& operator =(OUStringConcat
<T1
, T2
> && concat
) {
323 sal_Int32
const n
= concat
.length();
324 if (n
>= nCapacity
) {
325 ensureCapacity(n
+ 16); //TODO: check for overflow
327 *concat
.addData(pData
->buffer
) = 0;
334 Release the string data.
338 rtl_uString_release( pData
);
342 Fill the string data in the new string and clear the buffer.
344 This method is more efficient than the constructor of the string. It does
347 @return the string previously contained in the buffer.
349 SAL_WARN_UNUSED_RESULT OUString
makeStringAndClear()
352 rtl_uStringBuffer_makeStringAndClear( &pData
, &nCapacity
),
357 Returns the length (character count) of this string buffer.
359 @return the number of characters in this string buffer.
361 sal_Int32
getLength() const
363 return pData
->length
;
367 Checks if a string buffer is empty.
369 @return true if the string buffer is empty;
372 @since LibreOffice 4.1
376 return pData
->length
== 0;
380 Returns the current capacity of the String buffer.
383 is the amount of storage available for newly inserted
384 characters. The real buffer size is 2 bytes longer, because
385 all strings are 0 terminated.
387 @return the current capacity of this string buffer.
389 sal_Int32
getCapacity() const
395 Ensures that the capacity of the buffer is at least equal to the
398 The new capacity will be at least as large as the maximum of the current
399 length (so that no contents of the buffer is destroyed) and the given
400 minimumCapacity. If the given minimumCapacity is negative, nothing is
403 @param minimumCapacity the minimum desired capacity.
405 void ensureCapacity(sal_Int32 minimumCapacity
)
407 rtl_uStringbuffer_ensureCapacity( &pData
, &nCapacity
, minimumCapacity
);
411 Sets the length of this String buffer.
413 If the <code>newLength</code> argument is less than the current
414 length of the string buffer, the string buffer is truncated to
415 contain exactly the number of characters given by the
416 <code>newLength</code> argument.
418 If the <code>newLength</code> argument is greater than or equal
419 to the current length, sufficient null characters
420 (<code>'\u0000'</code>) are appended to the string buffer so that
421 length becomes the <code>newLength</code> argument.
423 The <code>newLength</code> argument must be greater than or equal
426 @param newLength the new length of the buffer.
428 void setLength(sal_Int32 newLength
)
430 assert(newLength
>= 0);
431 // Avoid modifications if pData points to const empty string:
432 if( newLength
!= pData
->length
)
434 if( newLength
> nCapacity
)
435 rtl_uStringbuffer_ensureCapacity(&pData
, &nCapacity
, newLength
);
437 pData
->buffer
[newLength
] = 0;
438 pData
->length
= newLength
;
443 Returns the character at a specific index in this string buffer.
445 The first character of a string buffer is at index
446 <code>0</code>, the next at index <code>1</code>, and so on, for
449 The index argument must be greater than or equal to
450 <code>0</code>, and less than the length of this string buffer.
452 @param index the index of the desired character.
453 @return the character at the specified index of this string buffer.
455 SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
456 sal_Unicode
charAt( sal_Int32 index
) const
458 assert(index
>= 0 && index
< pData
->length
);
459 return pData
->buffer
[ index
];
463 The character at the specified index of this string buffer is set
466 The index argument must be greater than or equal to
467 <code>0</code>, and less than the length of this string buffer.
469 @param index the index of the character to modify.
470 @param ch the new character.
472 SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
473 OUStringBuffer
& setCharAt(sal_Int32 index
, sal_Unicode ch
)
475 assert(index
>= 0 && index
< pData
->length
);
476 pData
->buffer
[ index
] = ch
;
481 Return a null terminated unicode character array.
483 const sal_Unicode
* getStr() const SAL_RETURNS_NONNULL
{ return pData
->buffer
; }
486 Access to individual characters.
488 @param index must be non-negative and less than length.
490 @return a reference to the character at the given index.
492 @since LibreOffice 3.5
494 sal_Unicode
& operator [](sal_Int32 index
)
496 assert(index
>= 0 && index
< pData
->length
);
497 return pData
->buffer
[index
];
501 Access to individual characters.
503 @param index must be non-negative and less than length.
505 @return a reference to the character at the given index.
507 @since LibreOffice 4.2
509 const sal_Unicode
& operator [](sal_Int32 index
) const
511 assert(index
>= 0 && index
< pData
->length
);
512 return pData
->buffer
[index
];
516 Return a OUString instance reflecting the current content
517 of this OUStringBuffer.
519 const OUString
toString() const
521 return OUString(pData
->buffer
, pData
->length
);
525 Appends the string to this string buffer.
527 The characters of the <code>OUString</code> argument are appended, in
528 order, to the contents of this string buffer, increasing the
529 length of this string buffer by the length of the argument.
532 @return this string buffer.
534 OUStringBuffer
& append(const OUString
&str
)
536 return append( str
.getStr(), str
.getLength() );
539 #if defined LIBO_INTERNAL_ONLY
540 OUStringBuffer
& append(std::u16string_view sv
) {
541 if (sv
.size() > sal_uInt32(std::numeric_limits
<sal_Int32
>::max())) {
542 throw std::bad_alloc();
544 return append(sv
.data(), sv
.size());
549 Appends the content of a stringbuffer to this string buffer.
551 The characters of the <code>OUStringBuffer</code> argument are appended, in
552 order, to the contents of this string buffer, increasing the
553 length of this string buffer by the length of the argument.
556 @return this string buffer.
558 @since LibreOffice 4.0
560 OUStringBuffer
& append(const OUStringBuffer
&str
)
564 append( str
.getStr(), str
.getLength() );
570 Appends the string representation of the <code>char</code> array
571 argument to this string buffer.
573 The characters of the array argument are appended, in order, to
574 the contents of this string buffer. The length of this string
575 buffer increases by the length of the argument.
577 @param str the characters to be appended.
578 @return this string buffer.
580 OUStringBuffer
& append( const sal_Unicode
* str
)
582 return append( str
, rtl_ustr_getLength( str
) );
586 Appends the string representation of the <code>char</code> array
587 argument to this string buffer.
589 Characters of the character array <code>str</code> are appended,
590 in order, to the contents of this string buffer. The length of this
591 string buffer increases by the value of <code>len</code>.
593 @param str the characters to be appended; must be non-null, and must
594 point to at least len characters
595 @param len the number of characters to append; must be non-negative
596 @return this string buffer.
598 OUStringBuffer
& append( const sal_Unicode
* str
, sal_Int32 len
)
600 assert( len
== 0 || str
!= NULL
); // cannot assert that in rtl_uStringbuffer_insert
601 rtl_uStringbuffer_insert( &pData
, &nCapacity
, getLength(), str
, len
);
607 This function accepts an ASCII string literal as its argument.
608 @since LibreOffice 3.6
610 template< typename T
>
611 typename
libreoffice_internal::ConstCharArrayDetector
< T
, OUStringBuffer
& >::Type
append( T
& literal
)
614 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
615 rtl_uStringbuffer_insert_ascii(
616 &pData
, &nCapacity
, getLength(),
617 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
618 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
622 #if defined LIBO_INTERNAL_ONLY
623 /** @overload @since LibreOffice 5.3 */
625 typename
libreoffice_internal::ConstCharArrayDetector
<
626 T
, OUStringBuffer
&>::TypeUtf16
627 append(T
& literal
) {
628 rtl_uStringbuffer_insert(
629 &pData
, &nCapacity
, getLength(),
630 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
631 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
635 /** @overload @since LibreOffice 5.4 */
636 OUStringBuffer
& append(OUStringLiteral
const & literal
) {
637 rtl_uStringbuffer_insert_ascii(
638 &pData
, &nCapacity
, getLength(), literal
.data
, literal
.size
);
643 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
648 template< typename T1
, typename T2
>
649 OUStringBuffer
& append( OUStringConcat
< T1
, T2
>&& c
)
651 sal_Int32 l
= c
.length();
655 rtl_uStringbuffer_ensureCapacity( &pData
, &nCapacity
, l
);
656 sal_Unicode
* end
= c
.addData( pData
->buffer
+ pData
->length
);
664 Appends a 8-Bit ASCII character string to this string buffer.
666 Since this method is optimized for performance. the ASCII
667 character values are not converted in any way. The caller
668 has to make sure that all ASCII characters are in the
669 allowed range between 0 and 127. The ASCII string must be
672 The characters of the array argument are appended, in order, to
673 the contents of this string buffer. The length of this string
674 buffer increases by the length of the argument.
676 @param str the 8-Bit ASCII characters to be appended.
677 @return this string buffer.
679 OUStringBuffer
& appendAscii( const sal_Char
* str
)
681 return appendAscii( str
, rtl_str_getLength( str
) );
685 Appends a 8-Bit ASCII character string to this string buffer.
687 Since this method is optimized for performance. the ASCII
688 character values are not converted in any way. The caller
689 has to make sure that all ASCII characters are in the
690 allowed range between 0 and 127. The ASCII string must be
693 Characters of the character array <code>str</code> are appended,
694 in order, to the contents of this string buffer. The length of this
695 string buffer increases by the value of <code>len</code>.
697 @param str the 8-Bit ASCII characters to be appended; must be non-null,
698 and must point to at least len characters
699 @param len the number of characters to append; must be non-negative
700 @return this string buffer.
702 OUStringBuffer
& appendAscii( const sal_Char
* str
, sal_Int32 len
)
704 rtl_uStringbuffer_insert_ascii( &pData
, &nCapacity
, getLength(), str
, len
);
709 Appends the string representation of the <code>bool</code>
710 argument to the string buffer.
712 The argument is converted to a string as if by the method
713 <code>String.valueOf</code>, and the characters of that
714 string are then appended to this string buffer.
716 @param b a <code>bool</code>.
717 @return this string buffer.
719 @since LibreOffice 4.1
721 OUStringBuffer
& append(bool b
)
723 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFBOOLEAN
];
724 return append( sz
, rtl_ustr_valueOfBoolean( sz
, b
) );
728 // Pointer can be automatically converted to bool, which is unwanted here.
729 // Explicitly delete all pointer append() overloads to prevent this
730 // (except for char* and sal_Unicode* overloads, which are handled elsewhere).
731 template< typename T
>
732 typename
libreoffice_internal::Enable
< void,
733 !libreoffice_internal::CharPtrDetector
< T
* >::ok
&& !libreoffice_internal::SalUnicodePtrDetector
< T
* >::ok
>::Type
734 append( T
* ) SAL_DELETED_FUNCTION
;
737 // This overload is needed because OUString has a ctor from rtl_uString*, but
738 // the bool overload above would be preferred to the conversion.
742 OUStringBuffer
& append(rtl_uString
* str
)
744 return append( OUString( str
));
748 Appends the string representation of the <code>sal_Bool</code>
749 argument to the string buffer.
751 The argument is converted to a string as if by the method
752 <code>String.valueOf</code>, and the characters of that
753 string are then appended to this string buffer.
755 @param b a <code>sal_Bool</code>.
756 @return this string buffer.
758 OUStringBuffer
& append(sal_Bool b
)
760 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFBOOLEAN
];
761 return append( sz
, rtl_ustr_valueOfBoolean( sz
, b
) );
765 Appends the string representation of the ASCII <code>char</code>
766 argument to this string buffer.
768 The argument is appended to the contents of this string buffer.
769 The length of this string buffer increases by <code>1</code>.
771 @param c an ASCII <code>char</code>.
772 @return this string buffer.
774 @since LibreOffice 3.5
776 OUStringBuffer
& append(char c
)
778 assert(static_cast< unsigned char >(c
) <= 0x7F);
779 return append(sal_Unicode(c
));
783 Appends the string representation of the <code>char</code>
784 argument to this string buffer.
786 The argument is appended to the contents of this string buffer.
787 The length of this string buffer increases by <code>1</code>.
789 @param c a <code>char</code>.
790 @return this string buffer.
792 OUStringBuffer
& append(sal_Unicode c
)
794 return append( &c
, 1 );
797 #if defined LIBO_INTERNAL_ONLY
798 void append(sal_uInt16
) = delete;
802 Appends the string representation of the <code>sal_Int32</code>
803 argument to this string buffer.
805 The argument is converted to a string as if by the method
806 <code>String.valueOf</code>, and the characters of that
807 string are then appended to this string buffer.
809 @param i an <code>sal_Int32</code>.
810 @param radix the radix
811 @return this string buffer.
813 OUStringBuffer
& append(sal_Int32 i
, sal_Int16 radix
= 10 )
815 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFINT32
];
816 return append( sz
, rtl_ustr_valueOfInt32( sz
, i
, radix
) );
820 Appends the string representation of the <code>long</code>
821 argument to this string buffer.
823 The argument is converted to a string as if by the method
824 <code>String.valueOf</code>, and the characters of that
825 string are then appended to this string buffer.
827 @param l a <code>long</code>.
828 @param radix the radix
829 @return this string buffer.
831 OUStringBuffer
& append(sal_Int64 l
, sal_Int16 radix
= 10 )
833 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFINT64
];
834 return append( sz
, rtl_ustr_valueOfInt64( sz
, l
, radix
) );
838 Appends the string representation of the <code>float</code>
839 argument to this string buffer.
841 The argument is converted to a string as if by the method
842 <code>String.valueOf</code>, and the characters of that
843 string are then appended to this string buffer.
845 @param f a <code>float</code>.
846 @return this string buffer.
848 OUStringBuffer
& append(float f
)
850 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFFLOAT
];
851 return append( sz
, rtl_ustr_valueOfFloat( sz
, f
) );
855 Appends the string representation of the <code>double</code>
856 argument to this string buffer.
858 The argument is converted to a string as if by the method
859 <code>String.valueOf</code>, and the characters of that
860 string are then appended to this string buffer.
862 @param d a <code>double</code>.
863 @return this string buffer.
865 OUStringBuffer
& append(double d
)
867 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFDOUBLE
];
868 return append( sz
, rtl_ustr_valueOfDouble( sz
, d
) );
872 Appends a single UTF-32 character to this string buffer.
874 <p>The single UTF-32 character will be represented within the string
875 buffer as either one or two UTF-16 code units.</p>
877 @param c a well-formed UTF-32 code unit (that is, a value in the range
878 <code>0</code>–<code>0x10FFFF</code>, but excluding
879 <code>0xD800</code>–<code>0xDFFF</code>)
884 OUStringBuffer
& appendUtf32(sal_uInt32 c
) {
885 return insertUtf32(getLength(), c
);
889 Unsafe way to make space for a fixed amount of characters to be appended
890 into this OUStringBuffer.
892 A call to this function must immediately be followed by code that
893 completely fills the uninitialized block pointed to by the return value.
895 @param length the length of the uninitialized block of sal_Unicode
896 entities; must be non-negative
898 @return a pointer to the start of the uninitialized block; only valid
899 until this OUStringBuffer's capacity changes
901 @since LibreOffice 4.4
903 sal_Unicode
* appendUninitialized(sal_Int32 length
) SAL_RETURNS_NONNULL
{
904 sal_Int32 n
= getLength();
905 rtl_uStringbuffer_insert(&pData
, &nCapacity
, n
, NULL
, length
);
906 return pData
->buffer
+ n
;
910 Inserts the string into this string buffer.
912 The characters of the <code>String</code> argument are inserted, in
913 order, into this string buffer at the indicated offset. The length
914 of this string buffer is increased by the length of the argument.
916 The offset argument must be greater than or equal to
917 <code>0</code>, and less than or equal to the length of this
920 @param offset the offset.
922 @return this string buffer.
924 OUStringBuffer
& insert(sal_Int32 offset
, const OUString
& str
)
926 return insert( offset
, str
.getStr(), str
.getLength() );
930 Inserts the string representation of the <code>char</code> array
931 argument into this string buffer.
933 The characters of the array argument are inserted into the
934 contents of this string buffer at the position indicated by
935 <code>offset</code>. The length of this string buffer increases by
936 the length of the argument.
938 The offset argument must be greater than or equal to
939 <code>0</code>, and less than or equal to the length of this
942 @param offset the offset.
943 @param str a character array.
944 @return this string buffer.
946 OUStringBuffer
& insert( sal_Int32 offset
, const sal_Unicode
* str
)
948 return insert( offset
, str
, rtl_ustr_getLength( str
) );
952 Inserts the string representation of the <code>char</code> array
953 argument into this string buffer.
955 The characters of the array argument are inserted into the
956 contents of this string buffer at the position indicated by
957 <code>offset</code>. The length of this string buffer increases by
958 the length of the argument.
960 The offset argument must be greater than or equal to
961 <code>0</code>, and less than or equal to the length of this
964 @param offset the offset.
965 @param str a character array.
966 @param len the number of characters to append.
967 @return this string buffer.
969 OUStringBuffer
& insert( sal_Int32 offset
, const sal_Unicode
* str
, sal_Int32 len
)
971 assert( len
== 0 || str
!= NULL
); // cannot assert that in rtl_uStringbuffer_insert
972 rtl_uStringbuffer_insert( &pData
, &nCapacity
, offset
, str
, len
);
978 This function accepts an ASCII string literal as its argument.
979 @since LibreOffice 3.6
981 template< typename T
>
982 typename
libreoffice_internal::ConstCharArrayDetector
< T
, OUStringBuffer
& >::Type
insert( sal_Int32 offset
, T
& literal
)
985 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
986 rtl_uStringbuffer_insert_ascii(
987 &pData
, &nCapacity
, offset
,
988 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
989 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
993 #if defined LIBO_INTERNAL_ONLY
994 /** @overload @since LibreOffice 5.3 */
996 typename
libreoffice_internal::ConstCharArrayDetector
<
997 T
, OUStringBuffer
&>::TypeUtf16
998 insert(sal_Int32 offset
, T
& literal
) {
999 rtl_uStringbuffer_insert(
1000 &pData
, &nCapacity
, offset
,
1001 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
1002 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
1006 /** @overload @since LibreOffice 5.4 */
1007 OUStringBuffer
& insert(sal_Int32 offset
, OUStringLiteral
const & literal
) {
1008 rtl_uStringbuffer_insert_ascii(
1009 &pData
, &nCapacity
, offset
, literal
.data
, literal
.size
);
1015 Inserts the string representation of the <code>sal_Bool</code>
1016 argument into this string buffer.
1018 The second argument is converted to a string as if by the method
1019 <code>String.valueOf</code>, and the characters of that
1020 string are then inserted into this string buffer at the indicated
1023 The offset argument must be greater than or equal to
1024 <code>0</code>, and less than or equal to the length of this
1027 @param offset the offset.
1028 @param b a <code>sal_Bool</code>.
1029 @return this string buffer.
1031 OUStringBuffer
& insert(sal_Int32 offset
, sal_Bool b
)
1033 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFBOOLEAN
];
1034 return insert( offset
, sz
, rtl_ustr_valueOfBoolean( sz
, b
) );
1038 Inserts the string representation of the <code>bool</code>
1039 argument into this string buffer.
1041 The second argument is converted to a string as if by the method
1042 <code>OUString::boolean</code>, and the characters of that
1043 string are then inserted into this string buffer at the indicated
1046 The offset argument must be greater than or equal to
1047 <code>0</code>, and less than or equal to the length of this
1050 @param offset the offset.
1051 @param b a <code>bool</code>.
1052 @return this string buffer.
1054 @since LibreOffice 4.3
1056 OUStringBuffer
& insert(sal_Int32 offset
, bool b
)
1058 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFBOOLEAN
];
1059 return insert( offset
, sz
, rtl_ustr_valueOfBoolean( sz
, b
) );
1063 Inserts the string representation of the <code>char</code>
1064 argument into this string buffer.
1066 The second argument is inserted into the contents of this string
1067 buffer at the position indicated by <code>offset</code>. The length
1068 of this string buffer increases by one.
1070 The offset argument must be greater than or equal to
1071 <code>0</code>, and less than or equal to the length of this
1074 @param offset the offset.
1075 @param c a <code>char</code>.
1076 @return this string buffer.
1078 @since LibreOffice 3.6
1080 OUStringBuffer
& insert(sal_Int32 offset
, char c
)
1083 return insert( offset
, &u
, 1 );
1087 Inserts the string representation of the <code>char</code>
1088 argument into this string buffer.
1090 The second argument is inserted into the contents of this string
1091 buffer at the position indicated by <code>offset</code>. The length
1092 of this string buffer increases by one.
1094 The offset argument must be greater than or equal to
1095 <code>0</code>, and less than or equal to the length of this
1098 @param offset the offset.
1099 @param c a <code>char</code>.
1100 @return this string buffer.
1102 OUStringBuffer
& insert(sal_Int32 offset
, sal_Unicode c
)
1104 return insert( offset
, &c
, 1 );
1108 Inserts the string representation of the second <code>sal_Int32</code>
1109 argument into this string buffer.
1111 The second argument is converted to a string as if by the method
1112 <code>String.valueOf</code>, and the characters of that
1113 string are then inserted into this string buffer at the indicated
1116 The offset argument must be greater than or equal to
1117 <code>0</code>, and less than or equal to the length of this
1120 @param offset the offset.
1121 @param i an <code>sal_Int32</code>.
1122 @param radix the radix.
1123 @return this string buffer.
1124 @exception StringIndexOutOfBoundsException if the offset is invalid.
1126 OUStringBuffer
& insert(sal_Int32 offset
, sal_Int32 i
, sal_Int16 radix
= 10 )
1128 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFINT32
];
1129 return insert( offset
, sz
, rtl_ustr_valueOfInt32( sz
, i
, radix
) );
1133 Inserts the string representation of the <code>long</code>
1134 argument into this string buffer.
1136 The second argument is converted to a string as if by the method
1137 <code>String.valueOf</code>, and the characters of that
1138 string are then inserted into this string buffer at the indicated
1141 The offset argument must be greater than or equal to
1142 <code>0</code>, and less than or equal to the length of this
1145 @param offset the offset.
1146 @param l a <code>long</code>.
1147 @param radix the radix.
1148 @return this string buffer.
1149 @exception StringIndexOutOfBoundsException if the offset is invalid.
1151 OUStringBuffer
& insert(sal_Int32 offset
, sal_Int64 l
, sal_Int16 radix
= 10 )
1153 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFINT64
];
1154 return insert( offset
, sz
, rtl_ustr_valueOfInt64( sz
, l
, radix
) );
1158 Inserts the string representation of the <code>float</code>
1159 argument into this string buffer.
1161 The second argument is converted to a string as if by the method
1162 <code>String.valueOf</code>, and the characters of that
1163 string are then inserted into this string buffer at the indicated
1166 The offset argument must be greater than or equal to
1167 <code>0</code>, and less than or equal to the length of this
1170 @param offset the offset.
1171 @param f a <code>float</code>.
1172 @return this string buffer.
1173 @exception StringIndexOutOfBoundsException if the offset is invalid.
1175 OUStringBuffer
insert(sal_Int32 offset
, float f
)
1177 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFFLOAT
];
1178 return insert( offset
, sz
, rtl_ustr_valueOfFloat( sz
, f
) );
1182 Inserts the string representation of the <code>double</code>
1183 argument into this string buffer.
1185 The second argument is converted to a string as if by the method
1186 <code>String.valueOf</code>, and the characters of that
1187 string are then inserted into this string buffer at the indicated
1190 The offset argument must be greater than or equal to
1191 <code>0</code>, and less than or equal to the length of this
1194 @param offset the offset.
1195 @param d a <code>double</code>.
1196 @return this string buffer.
1197 @exception StringIndexOutOfBoundsException if the offset is invalid.
1199 OUStringBuffer
& insert(sal_Int32 offset
, double d
)
1201 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFDOUBLE
];
1202 return insert( offset
, sz
, rtl_ustr_valueOfDouble( sz
, d
) );
1206 Inserts a single UTF-32 character into this string buffer.
1208 <p>The single UTF-32 character will be represented within the string
1209 buffer as either one or two UTF-16 code units.</p>
1211 @param offset the offset into this string buffer (from zero to the length
1212 of this string buffer, inclusive)
1214 @param c a well-formed UTF-32 code unit (that is, a value in the range
1215 <code>0</code>–<code>0x10FFFF</code>, but excluding
1216 <code>0xD800</code>–<code>0xDFFF</code>)
1218 @return this string buffer
1220 OUStringBuffer
& insertUtf32(sal_Int32 offset
, sal_uInt32 c
) {
1221 rtl_uStringbuffer_insertUtf32(&pData
, &nCapacity
, offset
, c
);
1226 Removes the characters in a substring of this sequence.
1228 The substring begins at the specified <code>start</code> and
1229 is <code>len</code> characters long.
1231 start must be >= 0 && <= This->length
1233 @param start The beginning index, inclusive
1234 @param len The substring length
1235 @return this string buffer.
1237 OUStringBuffer
& remove( sal_Int32 start
, sal_Int32 len
)
1239 rtl_uStringbuffer_remove( &pData
, start
, len
);
1244 Removes the tail of a string buffer start at the indicate position
1246 start must be >= 0 && <= This->length
1248 @param start The beginning index, inclusive. default to 0
1249 @return this string buffer.
1251 @since LibreOffice 4.0
1253 OUStringBuffer
& truncate( sal_Int32 start
= 0 )
1255 rtl_uStringbuffer_remove( &pData
, start
, getLength() - start
);
1260 Replace all occurrences of
1261 oldChar in this string buffer with newChar.
1263 @since LibreOffice 4.0
1265 @param oldChar the old character.
1266 @param newChar the new character.
1267 @return this string buffer
1269 OUStringBuffer
& replace( sal_Unicode oldChar
, sal_Unicode newChar
)
1271 sal_Int32 index
= 0;
1272 while((index
= indexOf(oldChar
, index
)) >= 0)
1274 pData
->buffer
[ index
] = newChar
;
1279 /** Allows access to the internal data of this OUStringBuffer, for effective
1282 This method should be used with care. After you have called this
1283 method, you may use the returned pInternalData or pInternalCapacity only
1284 as long as you make no other method call on this OUStringBuffer.
1286 @param pInternalData
1287 This output parameter receives a pointer to the internal data
1288 (rtl_uString pointer). pInternalData itself must not be null.
1290 @param pInternalCapacity
1291 This output parameter receives a pointer to the internal capacity.
1292 pInternalCapacity itself must not be null.
1294 void accessInternals(rtl_uString
*** pInternalData
,
1295 sal_Int32
** pInternalCapacity
)
1297 *pInternalData
= &pData
;
1298 *pInternalCapacity
= &nCapacity
;
1303 Returns the index within this string of the first occurrence of the
1304 specified character, starting the search at the specified index.
1306 @since LibreOffice 4.0
1308 @param ch character to be located.
1309 @param fromIndex the index to start the search from.
1310 The index must be greater or equal than 0
1311 and less or equal as the string length.
1312 @return the index of the first occurrence of the character in the
1313 character sequence represented by this string that is
1314 greater than or equal to fromIndex, or
1315 -1 if the character does not occur.
1317 sal_Int32
indexOf( sal_Unicode ch
, sal_Int32 fromIndex
= 0 ) const
1319 assert( fromIndex
>= 0 && fromIndex
<= pData
->length
);
1320 sal_Int32 ret
= rtl_ustr_indexOfChar_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
, ch
);
1321 return (ret
< 0 ? ret
: ret
+fromIndex
);
1325 Returns the index within this string of the last occurrence of the
1326 specified character, searching backward starting at the end.
1328 @since LibreOffice 4.0
1330 @param ch character to be located.
1331 @return the index of the last occurrence of the character in the
1332 character sequence represented by this string, or
1333 -1 if the character does not occur.
1335 sal_Int32
lastIndexOf( sal_Unicode ch
) const
1337 return rtl_ustr_lastIndexOfChar_WithLength( pData
->buffer
, pData
->length
, ch
);
1341 Returns the index within this string of the last occurrence of the
1342 specified character, searching backward starting before the specified
1345 @since LibreOffice 4.0
1347 @param ch character to be located.
1348 @param fromIndex the index before which to start the search.
1349 @return the index of the last occurrence of the character in the
1350 character sequence represented by this string that
1351 is less than fromIndex, or -1
1352 if the character does not occur before that point.
1354 sal_Int32
lastIndexOf( sal_Unicode ch
, sal_Int32 fromIndex
) const
1356 assert( fromIndex
>= 0 && fromIndex
<= pData
->length
);
1357 return rtl_ustr_lastIndexOfChar_WithLength( pData
->buffer
, fromIndex
, ch
);
1361 Returns the index within this string of the first occurrence of the
1362 specified substring, starting at the specified index.
1364 If str doesn't include any character, always -1 is
1365 returned. This is also the case, if both strings are empty.
1367 @since LibreOffice 4.0
1369 @param str the substring to search for.
1370 @param fromIndex the index to start the search from.
1371 @return If the string argument occurs one or more times as a substring
1372 within this string at the starting index, then the index
1373 of the first character of the first such substring is
1374 returned. If it does not occur as a substring starting
1375 at fromIndex or beyond, -1 is returned.
1377 sal_Int32
indexOf( const OUString
& str
, sal_Int32 fromIndex
= 0 ) const
1379 assert( fromIndex
>= 0 && fromIndex
<= pData
->length
);
1380 sal_Int32 ret
= rtl_ustr_indexOfStr_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
1381 str
.pData
->buffer
, str
.pData
->length
);
1382 return (ret
< 0 ? ret
: ret
+fromIndex
);
1387 This function accepts an ASCII string literal as its argument.
1389 @since LibreOffice 4.0
1391 template< typename T
>
1392 typename
libreoffice_internal::ConstCharArrayDetector
< T
, sal_Int32
>::Type
indexOf( T
& literal
, sal_Int32 fromIndex
= 0 ) const
1395 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
1396 sal_Int32 n
= rtl_ustr_indexOfAscii_WithLength(
1397 pData
->buffer
+ fromIndex
, pData
->length
- fromIndex
,
1398 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
1399 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
1400 return n
< 0 ? n
: n
+ fromIndex
;
1403 #if defined LIBO_INTERNAL_ONLY
1404 /** @overload @since LibreOffice 5.3 */
1405 template<typename T
>
1407 libreoffice_internal::ConstCharArrayDetector
<T
, sal_Int32
>::TypeUtf16
1408 indexOf(T
& literal
, sal_Int32 fromIndex
= 0) const {
1409 assert(fromIndex
>= 0);
1410 auto n
= rtl_ustr_indexOfStr_WithLength(
1411 pData
->buffer
+ fromIndex
, pData
->length
- fromIndex
,
1412 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
1413 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
1414 return n
< 0 ? n
: n
+ fromIndex
;
1417 /** @overload @since LibreOffice 5.4 */
1418 sal_Int32
indexOf(OUStringLiteral
const & literal
, sal_Int32 fromIndex
= 0)
1421 sal_Int32 n
= rtl_ustr_indexOfAscii_WithLength(
1422 pData
->buffer
+ fromIndex
, pData
->length
- fromIndex
, literal
.data
,
1424 return n
< 0 ? n
: n
+ fromIndex
;
1429 Returns the index within this string of the last occurrence of
1430 the specified substring, searching backward starting at the end.
1432 The returned index indicates the starting index of the substring
1434 If str doesn't include any character, always -1 is
1435 returned. This is also the case, if both strings are empty.
1437 @since LibreOffice 4.0
1439 @param str the substring to search for.
1440 @return If the string argument occurs one or more times as a substring
1441 within this string, then the index of the first character of
1442 the last such substring is returned. If it does not occur as
1443 a substring, -1 is returned.
1445 sal_Int32
lastIndexOf( const OUString
& str
) const
1447 return rtl_ustr_lastIndexOfStr_WithLength( pData
->buffer
, pData
->length
,
1448 str
.pData
->buffer
, str
.pData
->length
);
1452 Returns the index within this string of the last occurrence of
1453 the specified substring, searching backward starting before the specified
1456 The returned index indicates the starting index of the substring
1458 If str doesn't include any character, always -1 is
1459 returned. This is also the case, if both strings are empty.
1461 @since LibreOffice 4.0
1463 @param str the substring to search for.
1464 @param fromIndex the index before which to start the search.
1465 @return If the string argument occurs one or more times as a substring
1466 within this string before the starting index, then the index
1467 of the first character of the last such substring is
1468 returned. Otherwise, -1 is returned.
1470 sal_Int32
lastIndexOf( const OUString
& str
, sal_Int32 fromIndex
) const
1472 assert( fromIndex
>= 0 && fromIndex
<= pData
->length
);
1473 return rtl_ustr_lastIndexOfStr_WithLength( pData
->buffer
, fromIndex
,
1474 str
.pData
->buffer
, str
.pData
->length
);
1479 This function accepts an ASCII string literal as its argument.
1480 @since LibreOffice 4.0
1482 template< typename T
>
1483 typename
libreoffice_internal::ConstCharArrayDetector
< T
, sal_Int32
>::Type
lastIndexOf( T
& literal
) const
1486 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
1487 return rtl_ustr_lastIndexOfAscii_WithLength(
1488 pData
->buffer
, pData
->length
,
1489 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
1490 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
1493 #if defined LIBO_INTERNAL_ONLY
1494 /** @overload @since LibreOffice 5.3 */
1495 template<typename T
>
1497 libreoffice_internal::ConstCharArrayDetector
<T
, sal_Int32
>::TypeUtf16
1498 lastIndexOf(T
& literal
) const {
1499 return rtl_ustr_lastIndexOfStr_WithLength(
1500 pData
->buffer
, pData
->length
,
1501 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
1502 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
1505 /** @overload @since LibreOffice 5.4 */
1506 sal_Int32
lastIndexOf(OUStringLiteral
const & literal
) const {
1507 return rtl_ustr_lastIndexOfAscii_WithLength(
1508 pData
->buffer
, pData
->length
, literal
.data
, literal
.size
);
1513 Strip the given character from the start of the buffer.
1515 @since LibreOffice 4.0
1517 @param c the character to strip
1518 @return The number of characters stripped
1521 sal_Int32
stripStart(sal_Unicode c
= ' ')
1524 for(index
= 0; index
< getLength() ; index
++)
1526 if(pData
->buffer
[ index
] != c
)
1539 Strip the given character from the end of the buffer.
1541 @since LibreOffice 4.0
1543 @param c the character to strip
1544 @return The number of characters stripped
1547 sal_Int32
stripEnd(sal_Unicode c
= ' ')
1549 sal_Int32 result
= getLength();
1551 for(index
= getLength(); index
> 0 ; index
--)
1553 if(pData
->buffer
[ index
- 1 ] != c
)
1558 if(index
< getLength())
1562 return result
- getLength();
1565 Strip the given character from the both end of the buffer.
1567 @since LibreOffice 4.0
1569 @param c the character to strip
1570 @return The number of characters stripped
1573 sal_Int32
strip(sal_Unicode c
= ' ')
1575 return stripStart(c
) + stripEnd(c
);
1578 Returns a new string buffer that is a substring of this string.
1580 The substring begins at the specified beginIndex. If
1581 beginIndex is negative or be greater than the length of
1582 this string, behaviour is undefined.
1584 @param beginIndex the beginning index, inclusive.
1585 @return the specified substring.
1586 @since LibreOffice 4.1
1588 OUStringBuffer
copy( sal_Int32 beginIndex
) const
1590 return copy( beginIndex
, getLength() - beginIndex
);
1594 Returns a new string buffer that is a substring of this string.
1596 The substring begins at the specified beginIndex and contains count
1597 characters. If either beginIndex or count are negative,
1598 or beginIndex + count are greater than the length of this string
1599 then behaviour is undefined.
1601 @param beginIndex the beginning index, inclusive.
1602 @param count the number of characters.
1603 @return the specified substring.
1604 @since LibreOffice 4.1
1606 OUStringBuffer
copy( sal_Int32 beginIndex
, sal_Int32 count
) const
1608 assert(beginIndex
>= 0 && beginIndex
<= getLength());
1609 assert(count
>= 0 && count
<= getLength() - beginIndex
);
1610 rtl_uString
*pNew
= NULL
;
1611 rtl_uStringbuffer_newFromStr_WithLength( &pNew
, getStr() + beginIndex
, count
);
1612 return OUStringBuffer( pNew
, count
+ 16 );
1616 OUStringBuffer( rtl_uString
* value
, const sal_Int32 capacity
)
1619 nCapacity
= capacity
;
1623 A pointer to the data structure which contains the data.
1625 rtl_uString
* pData
;
1628 The len of the pData->buffer.
1630 sal_Int32 nCapacity
;
1633 #if defined LIBO_INTERNAL_ONLY
1634 // Define this here to avoid circular includes
1635 inline OUString
& OUString::operator+=( const OUStringBuffer
& str
) &
1637 // Call operator= if this is empty, otherwise rtl_uString_newConcat will attempt to
1638 // acquire() the str.pData buffer, which is part of the OUStringBuffer mutable state.
1640 return operator=(str
.toString());
1642 return internalAppend(str
.pData
);
1647 #ifdef RTL_STRING_UNITTEST
1650 typedef rtlunittest::OUStringBuffer OUStringBuffer
;
1654 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
1655 using ::rtl::OUStringBuffer
;
1658 #endif // INCLUDED_RTL_USTRBUF_HXX
1660 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */