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>
29 #include <rtl/ustrbuf.h>
30 #include <rtl/ustring.hxx>
31 #include <rtl/stringutils.hxx>
32 #include <sal/types.h>
34 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
35 #include <rtl/stringconcat.hxx>
38 #ifdef RTL_STRING_UNITTEST
39 extern bool rtl_string_unittest_invalid_conversion
;
42 // The unittest uses slightly different code to help check that the proper
43 // calls are made. The class is put into a different namespace to make
44 // sure the compiler generates a different (if generating also non-inline)
45 // copy of the function and does not merge them together. The class
46 // is "brought" into the proper rtl namespace by a typedef below.
47 #ifdef RTL_STRING_UNITTEST
48 #define rtl rtlunittest
54 #ifdef RTL_STRING_UNITTEST
58 /** A string buffer implements a mutable sequence of characters.
60 class SAL_WARN_UNUSED OUStringBuffer
64 Constructs a string buffer with no characters in it and an
65 initial capacity of 16 characters.
71 rtl_uString_new_WithLength( &pData
, nCapacity
);
75 Allocates a new string buffer that contains the same sequence of
76 characters as the string buffer argument.
78 @param value a <code>OUStringBuffer</code>.
80 OUStringBuffer( const OUStringBuffer
& value
)
82 , nCapacity( value
.nCapacity
)
84 rtl_uStringbuffer_newFromStringBuffer( &pData
, value
.nCapacity
, value
.pData
);
88 Constructs a string buffer with no characters in it and an
89 initial capacity specified by the <code>length</code> argument.
91 @param length the initial capacity.
93 explicit OUStringBuffer(int length
)
97 rtl_uString_new_WithLength( &pData
, length
);
99 #if __cplusplus >= 201103L
100 explicit OUStringBuffer(unsigned int length
)
101 : OUStringBuffer(static_cast<int>(length
))
104 #if SAL_TYPES_SIZEOFLONG == 4
105 // additional overloads for sal_Int32 sal_uInt32
106 explicit OUStringBuffer(long length
)
107 : OUStringBuffer(static_cast<int>(length
))
110 explicit OUStringBuffer(unsigned long length
)
111 : OUStringBuffer(static_cast<int>(length
))
115 // avoid obvious bugs
116 explicit OUStringBuffer(char) = delete;
117 explicit OUStringBuffer(sal_Unicode
) = delete;
121 Constructs a string buffer so that it represents the same
122 sequence of characters as the string argument.
125 capacity of the string buffer is <code>16</code> plus the length
126 of the string argument.
128 @param value the initial contents of the buffer.
130 OUStringBuffer(const OUString
& value
)
132 , nCapacity( value
.getLength() + 16 )
134 rtl_uStringbuffer_newFromStr_WithLength( &pData
, value
.getStr(), value
.getLength() );
137 template< typename T
>
138 OUStringBuffer( T
& literal
, typename
libreoffice_internal::ConstCharArrayDetector
< T
, libreoffice_internal::Dummy
>::Type
= libreoffice_internal::Dummy() )
140 , nCapacity( libreoffice_internal::ConstCharArrayDetector
<T
>::length
+ 16 )
143 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
144 rtl_uString_newFromLiteral(
146 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
147 libreoffice_internal::ConstCharArrayDetector
<T
>::length
, 16);
148 #ifdef RTL_STRING_UNITTEST
149 rtl_string_unittest_const_literal
= true;
153 #if defined LIBO_INTERNAL_ONLY
154 /** @overload @since LibreOffice 5.3 */
158 typename
libreoffice_internal::ConstCharArrayDetector
<
159 T
, libreoffice_internal::Dummy
>::TypeUtf16
160 = libreoffice_internal::Dummy()):
162 nCapacity(libreoffice_internal::ConstCharArrayDetector
<T
>::length
+ 16)
164 rtl_uStringbuffer_newFromStr_WithLength(
166 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
167 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
170 /** @overload @since LibreOffice 5.4 */
171 OUStringBuffer(OUStringLiteral
const & literal
):
172 pData(nullptr), nCapacity(literal
.size
+ 16) //TODO: check for overflow
174 rtl_uString_newFromLiteral(&pData
, literal
.data
, literal
.size
, 16);
178 #ifdef RTL_STRING_UNITTEST
180 * Only used by unittests to detect incorrect conversions.
183 template< typename T
>
184 OUStringBuffer( T
&, typename
libreoffice_internal::ExceptConstCharArrayDetector
< T
>::Type
= libreoffice_internal::Dummy() )
188 rtl_uString_newFromLiteral( &pData
, "!!br0ken!!", 10, 0 ); // set to garbage
189 rtl_string_unittest_invalid_conversion
= true;
192 * Only used by unittests to detect incorrect conversions.
195 template< typename T
>
196 OUStringBuffer( const T
&, typename
libreoffice_internal::ExceptCharArrayDetector
< T
>::Type
= libreoffice_internal::Dummy() )
200 rtl_uString_newFromLiteral( &pData
, "!!br0ken!!", 10, 0 ); // set to garbage
201 rtl_string_unittest_invalid_conversion
= true;
205 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
210 template< typename T1
, typename T2
>
211 OUStringBuffer( const OUStringConcat
< T1
, T2
>& c
)
213 const sal_Int32 l
= c
.length();
215 pData
= rtl_uString_alloc( nCapacity
);
216 sal_Unicode
* end
= c
.addData( pData
->buffer
);
219 // TODO realloc in case pData->>length is noticeably smaller than l ?
222 /** Assign to this a copy of value.
224 OUStringBuffer
& operator = ( const OUStringBuffer
& value
)
228 rtl_uStringbuffer_newFromStringBuffer(&pData
,
231 nCapacity
= value
.nCapacity
;
236 /** Assign from a string.
238 @since LibreOffice 5.3
240 OUStringBuffer
& operator =(OUString
const & string
) {
241 sal_Int32 n
= string
.getLength();
242 if (n
>= nCapacity
) {
243 ensureCapacity(n
+ 16); //TODO: check for overflow
246 pData
->buffer
, string
.pData
->buffer
,
247 (n
+ 1) * sizeof (sal_Unicode
));
252 /** Assign from a string literal.
254 @since LibreOffice 5.3
258 libreoffice_internal::ConstCharArrayDetector
<T
, OUStringBuffer
&>::Type
259 operator =(T
& literal
) {
261 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
263 = libreoffice_internal::ConstCharArrayDetector
<T
>::length
;
264 if (n
>= nCapacity
) {
265 ensureCapacity(n
+ 16); //TODO: check for overflow
268 = libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
270 sal_Unicode
* to
= pData
->buffer
;
271 for (sal_Int32 i
= 0; i
<= n
; ++i
) {
278 #if defined LIBO_INTERNAL_ONLY
279 /** @overload @since LibreOffice 5.3 */
281 typename
libreoffice_internal::ConstCharArrayDetector
<
282 T
, OUStringBuffer
&>::TypeUtf16
283 operator =(T
& literal
) {
285 = libreoffice_internal::ConstCharArrayDetector
<T
>::length
;
286 if (n
>= nCapacity
) {
287 ensureCapacity(n
+ 16); //TODO: check for overflow
291 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
292 (n
+ 1) * sizeof (sal_Unicode
)); //TODO: check for overflow
297 /** @overload @since LibreOffice 5.4 */
298 OUStringBuffer
& operator =(OUStringLiteral
const & literal
) {
299 sal_Int32
const n
= literal
.size
;
300 if (n
>= nCapacity
) {
301 ensureCapacity(n
+ 16); //TODO: check for overflow
303 char const * from
= literal
.data
;
304 sal_Unicode
* to
= pData
->buffer
;
305 for (sal_Int32 i
= 0; i
<= n
; ++i
) {
313 #if defined LIBO_INTERNAL_ONLY
314 /** @overload @since LibreOffice 5.3 */
315 template<typename T1
, typename T2
>
316 OUStringBuffer
& operator =(OUStringConcat
<T1
, T2
> const & concat
) {
317 sal_Int32
const n
= concat
.length();
318 if (n
>= nCapacity
) {
319 ensureCapacity(n
+ 16); //TODO: check for overflow
321 *concat
.addData(pData
->buffer
) = 0;
328 Release the string data.
332 rtl_uString_release( pData
);
336 Fill the string data in the new string and clear the buffer.
338 This method is more efficient than the constructor of the string. It does
341 @return the string previously contained in the buffer.
343 SAL_WARN_UNUSED_RESULT OUString
makeStringAndClear()
346 rtl_uStringBuffer_makeStringAndClear( &pData
, &nCapacity
),
351 Returns the length (character count) of this string buffer.
353 @return the number of characters in this string buffer.
355 sal_Int32
getLength() const
357 return pData
->length
;
361 Checks if a string buffer is empty.
363 @return true if the string buffer is empty;
366 @since LibreOffice 4.1
370 return pData
->length
== 0;
374 Returns the current capacity of the String buffer.
377 is the amount of storage available for newly inserted
378 characters. The real buffer size is 2 bytes longer, because
379 all strings are 0 terminated.
381 @return the current capacity of this string buffer.
383 sal_Int32
getCapacity() const
389 Ensures that the capacity of the buffer is at least equal to the
392 The new capacity will be at least as large as the maximum of the current
393 length (so that no contents of the buffer is destroyed) and the given
394 minimumCapacity. If the given minimumCapacity is negative, nothing is
397 @param minimumCapacity the minimum desired capacity.
399 void ensureCapacity(sal_Int32 minimumCapacity
)
401 rtl_uStringbuffer_ensureCapacity( &pData
, &nCapacity
, minimumCapacity
);
405 Sets the length of this String buffer.
407 If the <code>newLength</code> argument is less than the current
408 length of the string buffer, the string buffer is truncated to
409 contain exactly the number of characters given by the
410 <code>newLength</code> argument.
412 If the <code>newLength</code> argument is greater than or equal
413 to the current length, sufficient null characters
414 (<code>'\u0000'</code>) are appended to the string buffer so that
415 length becomes the <code>newLength</code> argument.
417 The <code>newLength</code> argument must be greater than or equal
420 @param newLength the new length of the buffer.
422 void setLength(sal_Int32 newLength
)
424 assert(newLength
>= 0);
425 // Avoid modifications if pData points to const empty string:
426 if( newLength
!= pData
->length
)
428 if( newLength
> nCapacity
)
429 rtl_uStringbuffer_ensureCapacity(&pData
, &nCapacity
, newLength
);
431 pData
->buffer
[newLength
] = 0;
432 pData
->length
= newLength
;
437 Returns the character at a specific index in this string buffer.
439 The first character of a string buffer is at index
440 <code>0</code>, the next at index <code>1</code>, and so on, for
443 The index argument must be greater than or equal to
444 <code>0</code>, and less than the length of this string buffer.
446 @param index the index of the desired character.
447 @return the character at the specified index of this string buffer.
449 SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
450 sal_Unicode
charAt( sal_Int32 index
) const
452 assert(index
>= 0 && index
< pData
->length
);
453 return pData
->buffer
[ index
];
457 The character at the specified index of this string buffer is set
460 The index argument must be greater than or equal to
461 <code>0</code>, and less than the length of this string buffer.
463 @param index the index of the character to modify.
464 @param ch the new character.
466 SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
467 OUStringBuffer
& setCharAt(sal_Int32 index
, sal_Unicode ch
)
469 assert(index
>= 0 && index
< pData
->length
);
470 pData
->buffer
[ index
] = ch
;
475 Return a null terminated unicode character array.
477 const sal_Unicode
* getStr() const { return pData
->buffer
; }
480 Access to individual characters.
482 @param index must be non-negative and less than length.
484 @return a reference to the character at the given index.
486 @since LibreOffice 3.5
488 sal_Unicode
& operator [](sal_Int32 index
)
490 assert(index
>= 0 && index
< pData
->length
);
491 return pData
->buffer
[index
];
495 Access to individual characters.
497 @param index must be non-negative and less than length.
499 @return a reference to the character at the given index.
501 @since LibreOffice 4.2
503 const sal_Unicode
& operator [](sal_Int32 index
) const
505 assert(index
>= 0 && index
< pData
->length
);
506 return pData
->buffer
[index
];
510 Return a OUString instance reflecting the current content
511 of this OUStringBuffer.
513 const OUString
toString() const
515 return OUString(pData
->buffer
, pData
->length
);
519 Appends the string to this string buffer.
521 The characters of the <code>OUString</code> argument are appended, in
522 order, to the contents of this string buffer, increasing the
523 length of this string buffer by the length of the argument.
526 @return this string buffer.
528 OUStringBuffer
& append(const OUString
&str
)
530 return append( str
.getStr(), str
.getLength() );
534 Appends the content of a stringbuffer to this string buffer.
536 The characters of the <code>OUStringBuffer</code> argument are appended, in
537 order, to the contents of this string buffer, increasing the
538 length of this string buffer by the length of the argument.
541 @return this string buffer.
543 @since LibreOffice 4.0
545 OUStringBuffer
& append(const OUStringBuffer
&str
)
549 append( str
.getStr(), str
.getLength() );
555 Appends the string representation of the <code>char</code> array
556 argument to this string buffer.
558 The characters of the array argument are appended, in order, to
559 the contents of this string buffer. The length of this string
560 buffer increases by the length of the argument.
562 @param str the characters to be appended.
563 @return this string buffer.
565 OUStringBuffer
& append( const sal_Unicode
* str
)
567 return append( str
, rtl_ustr_getLength( str
) );
571 Appends the string representation of the <code>char</code> array
572 argument to this string buffer.
574 Characters of the character array <code>str</code> are appended,
575 in order, to the contents of this string buffer. The length of this
576 string buffer increases by the value of <code>len</code>.
578 @param str the characters to be appended; must be non-null, and must
579 point to at least len characters
580 @param len the number of characters to append; must be non-negative
581 @return this string buffer.
583 OUStringBuffer
& append( const sal_Unicode
* str
, sal_Int32 len
)
585 assert( len
== 0 || str
!= NULL
); // cannot assert that in rtl_uStringbuffer_insert
586 rtl_uStringbuffer_insert( &pData
, &nCapacity
, getLength(), str
, len
);
592 This function accepts an ASCII string literal as its argument.
593 @since LibreOffice 3.6
595 template< typename T
>
596 typename
libreoffice_internal::ConstCharArrayDetector
< T
, OUStringBuffer
& >::Type
append( T
& literal
)
599 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
600 rtl_uStringbuffer_insert_ascii(
601 &pData
, &nCapacity
, getLength(),
602 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
603 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
607 #if defined LIBO_INTERNAL_ONLY
608 /** @overload @since LibreOffice 5.3 */
610 typename
libreoffice_internal::ConstCharArrayDetector
<
611 T
, OUStringBuffer
&>::TypeUtf16
612 append(T
& literal
) {
613 rtl_uStringbuffer_insert(
614 &pData
, &nCapacity
, getLength(),
615 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
616 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
620 /** @overload @since LibreOffice 5.4 */
621 OUStringBuffer
& append(OUStringLiteral
const & literal
) {
622 rtl_uStringbuffer_insert_ascii(
623 &pData
, &nCapacity
, getLength(), literal
.data
, literal
.size
);
628 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
633 template< typename T1
, typename T2
>
634 OUStringBuffer
& append( const OUStringConcat
< T1
, T2
>& c
)
636 sal_Int32 l
= c
.length();
640 rtl_uStringbuffer_ensureCapacity( &pData
, &nCapacity
, l
);
641 sal_Unicode
* end
= c
.addData( pData
->buffer
+ pData
->length
);
649 Appends a 8-Bit ASCII character string to this string buffer.
651 Since this method is optimized for performance. the ASCII
652 character values are not converted in any way. The caller
653 has to make sure that all ASCII characters are in the
654 allowed range between 0 and 127. The ASCII string must be
657 The characters of the array argument are appended, in order, to
658 the contents of this string buffer. The length of this string
659 buffer increases by the length of the argument.
661 @param str the 8-Bit ASCII characters to be appended.
662 @return this string buffer.
664 OUStringBuffer
& appendAscii( const sal_Char
* str
)
666 return appendAscii( str
, rtl_str_getLength( str
) );
670 Appends a 8-Bit ASCII character string to this string buffer.
672 Since this method is optimized for performance. the ASCII
673 character values are not converted in any way. The caller
674 has to make sure that all ASCII characters are in the
675 allowed range between 0 and 127. The ASCII string must be
678 Characters of the character array <code>str</code> are appended,
679 in order, to the contents of this string buffer. The length of this
680 string buffer increases by the value of <code>len</code>.
682 @param str the 8-Bit ASCII characters to be appended; must be non-null,
683 and must point to at least len characters
684 @param len the number of characters to append; must be non-negative
685 @return this string buffer.
687 OUStringBuffer
& appendAscii( const sal_Char
* str
, sal_Int32 len
)
689 rtl_uStringbuffer_insert_ascii( &pData
, &nCapacity
, getLength(), str
, len
);
694 Appends the string representation of the <code>bool</code>
695 argument to the string buffer.
697 The argument is converted to a string as if by the method
698 <code>String.valueOf</code>, and the characters of that
699 string are then appended to this string buffer.
701 @param b a <code>bool</code>.
702 @return this string buffer.
704 @since LibreOffice 4.1
706 OUStringBuffer
& append(bool b
)
708 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFBOOLEAN
];
709 return append( sz
, rtl_ustr_valueOfBoolean( sz
, b
) );
713 // Pointer can be automatically converted to bool, which is unwanted here.
714 // Explicitly delete all pointer append() overloads to prevent this
715 // (except for char* and sal_Unicode* overloads, which are handled elsewhere).
716 template< typename T
>
717 typename
libreoffice_internal::Enable
< void,
718 !libreoffice_internal::CharPtrDetector
< T
* >::ok
&& !libreoffice_internal::SalUnicodePtrDetector
< T
* >::ok
>::Type
719 append( T
* ) SAL_DELETED_FUNCTION
;
722 // This overload is needed because OUString has a ctor from rtl_uString*, but
723 // the bool overload above would be preferred to the conversion.
727 OUStringBuffer
& append(rtl_uString
* str
)
729 return append( OUString( str
));
733 Appends the string representation of the <code>sal_Bool</code>
734 argument to the string buffer.
736 The argument is converted to a string as if by the method
737 <code>String.valueOf</code>, and the characters of that
738 string are then appended to this string buffer.
740 @param b a <code>sal_Bool</code>.
741 @return this string buffer.
743 OUStringBuffer
& append(sal_Bool b
)
745 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFBOOLEAN
];
746 return append( sz
, rtl_ustr_valueOfBoolean( sz
, b
) );
750 Appends the string representation of the ASCII <code>char</code>
751 argument to this string buffer.
753 The argument is appended to the contents of this string buffer.
754 The length of this string buffer increases by <code>1</code>.
756 @param c an ASCII <code>char</code>.
757 @return this string buffer.
759 @since LibreOffice 3.5
761 OUStringBuffer
& append(char c
)
763 assert(static_cast< unsigned char >(c
) <= 0x7F);
764 return append(sal_Unicode(c
));
768 Appends the string representation of the <code>char</code>
769 argument to this string buffer.
771 The argument is appended to the contents of this string buffer.
772 The length of this string buffer increases by <code>1</code>.
774 @param c a <code>char</code>.
775 @return this string buffer.
777 OUStringBuffer
& append(sal_Unicode c
)
779 return append( &c
, 1 );
782 #if defined LIBO_INTERNAL_ONLY
783 void append(sal_uInt16
) = delete;
787 Appends the string representation of the <code>sal_Int32</code>
788 argument to this string buffer.
790 The argument is converted to a string as if by the method
791 <code>String.valueOf</code>, and the characters of that
792 string are then appended to this string buffer.
794 @param i an <code>sal_Int32</code>.
795 @param radix the radix
796 @return this string buffer.
798 OUStringBuffer
& append(sal_Int32 i
, sal_Int16 radix
= 10 )
800 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFINT32
];
801 return append( sz
, rtl_ustr_valueOfInt32( sz
, i
, radix
) );
805 Appends the string representation of the <code>long</code>
806 argument to this string buffer.
808 The argument is converted to a string as if by the method
809 <code>String.valueOf</code>, and the characters of that
810 string are then appended to this string buffer.
812 @param l a <code>long</code>.
813 @param radix the radix
814 @return this string buffer.
816 OUStringBuffer
& append(sal_Int64 l
, sal_Int16 radix
= 10 )
818 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFINT64
];
819 return append( sz
, rtl_ustr_valueOfInt64( sz
, l
, radix
) );
823 Appends the string representation of the <code>float</code>
824 argument to this string buffer.
826 The argument is converted to a string as if by the method
827 <code>String.valueOf</code>, and the characters of that
828 string are then appended to this string buffer.
830 @param f a <code>float</code>.
831 @return this string buffer.
833 OUStringBuffer
& append(float f
)
835 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFFLOAT
];
836 return append( sz
, rtl_ustr_valueOfFloat( sz
, f
) );
840 Appends the string representation of the <code>double</code>
841 argument to this string buffer.
843 The argument is converted to a string as if by the method
844 <code>String.valueOf</code>, and the characters of that
845 string are then appended to this string buffer.
847 @param d a <code>double</code>.
848 @return this string buffer.
850 OUStringBuffer
& append(double d
)
852 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFDOUBLE
];
853 return append( sz
, rtl_ustr_valueOfDouble( sz
, d
) );
857 Appends a single UTF-32 character to this string buffer.
859 <p>The single UTF-32 character will be represented within the string
860 buffer as either one or two UTF-16 code units.</p>
862 @param c a well-formed UTF-32 code unit (that is, a value in the range
863 <code>0</code>–<code>0x10FFFF</code>, but excluding
864 <code>0xD800</code>–<code>0xDFFF</code>)
869 OUStringBuffer
& appendUtf32(sal_uInt32 c
) {
870 return insertUtf32(getLength(), c
);
874 Unsafe way to make space for a fixed amount of characters to be appended
875 into this OUStringBuffer.
877 A call to this function must immediately be followed by code that
878 completely fills the uninitialized block pointed to by the return value.
880 @param length the length of the uninitialized block of sal_Unicode
881 entities; must be non-negative
883 @return a pointer to the start of the uninitialized block; only valid
884 until this OUStringBuffer's capacity changes
886 @since LibreOffice 4.4
888 sal_Unicode
* appendUninitialized(sal_Int32 length
) {
889 sal_Int32 n
= getLength();
890 rtl_uStringbuffer_insert(&pData
, &nCapacity
, n
, NULL
, length
);
891 return pData
->buffer
+ n
;
895 Inserts the string into this string buffer.
897 The characters of the <code>String</code> argument are inserted, in
898 order, into this string buffer at the indicated offset. The length
899 of this string buffer is increased by the length of the argument.
901 The offset argument must be greater than or equal to
902 <code>0</code>, and less than or equal to the length of this
905 @param offset the offset.
907 @return this string buffer.
909 OUStringBuffer
& insert(sal_Int32 offset
, const OUString
& str
)
911 return insert( offset
, str
.getStr(), str
.getLength() );
915 Inserts the string representation of the <code>char</code> array
916 argument into this string buffer.
918 The characters of the array argument are inserted into the
919 contents of this string buffer at the position indicated by
920 <code>offset</code>. The length of this string buffer increases by
921 the length of the argument.
923 The offset argument must be greater than or equal to
924 <code>0</code>, and less than or equal to the length of this
927 @param offset the offset.
928 @param str a character array.
929 @return this string buffer.
931 OUStringBuffer
& insert( sal_Int32 offset
, const sal_Unicode
* str
)
933 return insert( offset
, str
, rtl_ustr_getLength( str
) );
937 Inserts the string representation of the <code>char</code> array
938 argument into this string buffer.
940 The characters of the array argument are inserted into the
941 contents of this string buffer at the position indicated by
942 <code>offset</code>. The length of this string buffer increases by
943 the length of the argument.
945 The offset argument must be greater than or equal to
946 <code>0</code>, and less than or equal to the length of this
949 @param offset the offset.
950 @param str a character array.
951 @param len the number of characters to append.
952 @return this string buffer.
954 OUStringBuffer
& insert( sal_Int32 offset
, const sal_Unicode
* str
, sal_Int32 len
)
956 assert( len
== 0 || str
!= NULL
); // cannot assert that in rtl_uStringbuffer_insert
957 rtl_uStringbuffer_insert( &pData
, &nCapacity
, offset
, str
, len
);
963 This function accepts an ASCII string literal as its argument.
964 @since LibreOffice 3.6
966 template< typename T
>
967 typename
libreoffice_internal::ConstCharArrayDetector
< T
, OUStringBuffer
& >::Type
insert( sal_Int32 offset
, T
& literal
)
970 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
971 rtl_uStringbuffer_insert_ascii(
972 &pData
, &nCapacity
, offset
,
973 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
974 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
978 #if defined LIBO_INTERNAL_ONLY
979 /** @overload @since LibreOffice 5.3 */
981 typename
libreoffice_internal::ConstCharArrayDetector
<
982 T
, OUStringBuffer
&>::TypeUtf16
983 insert(sal_Int32 offset
, T
& literal
) {
984 rtl_uStringbuffer_insert(
985 &pData
, &nCapacity
, offset
,
986 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
987 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
991 /** @overload @since LibreOffice 5.4 */
992 OUStringBuffer
& insert(sal_Int32 offset
, OUStringLiteral
const & literal
) {
993 rtl_uStringbuffer_insert_ascii(
994 &pData
, &nCapacity
, offset
, literal
.data
, literal
.size
);
1000 Inserts the string representation of the <code>sal_Bool</code>
1001 argument into this string buffer.
1003 The second argument is converted to a string as if by the method
1004 <code>String.valueOf</code>, and the characters of that
1005 string are then inserted into this string buffer at the indicated
1008 The offset argument must be greater than or equal to
1009 <code>0</code>, and less than or equal to the length of this
1012 @param offset the offset.
1013 @param b a <code>sal_Bool</code>.
1014 @return this string buffer.
1016 OUStringBuffer
& insert(sal_Int32 offset
, sal_Bool b
)
1018 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFBOOLEAN
];
1019 return insert( offset
, sz
, rtl_ustr_valueOfBoolean( sz
, b
) );
1023 Inserts the string representation of the <code>bool</code>
1024 argument into this string buffer.
1026 The second argument is converted to a string as if by the method
1027 <code>OUString::boolean</code>, and the characters of that
1028 string are then inserted into this string buffer at the indicated
1031 The offset argument must be greater than or equal to
1032 <code>0</code>, and less than or equal to the length of this
1035 @param offset the offset.
1036 @param b a <code>bool</code>.
1037 @return this string buffer.
1039 @since LibreOffice 4.3
1041 OUStringBuffer
& insert(sal_Int32 offset
, bool b
)
1043 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFBOOLEAN
];
1044 return insert( offset
, sz
, rtl_ustr_valueOfBoolean( sz
, b
) );
1048 Inserts the string representation of the <code>char</code>
1049 argument into this string buffer.
1051 The second argument is inserted into the contents of this string
1052 buffer at the position indicated by <code>offset</code>. The length
1053 of this string buffer increases by one.
1055 The offset argument must be greater than or equal to
1056 <code>0</code>, and less than or equal to the length of this
1059 @param offset the offset.
1060 @param c a <code>char</code>.
1061 @return this string buffer.
1063 @since LibreOffice 3.6
1065 OUStringBuffer
& insert(sal_Int32 offset
, char c
)
1068 return insert( offset
, &u
, 1 );
1072 Inserts the string representation of the <code>char</code>
1073 argument into this string buffer.
1075 The second argument is inserted into the contents of this string
1076 buffer at the position indicated by <code>offset</code>. The length
1077 of this string buffer increases by one.
1079 The offset argument must be greater than or equal to
1080 <code>0</code>, and less than or equal to the length of this
1083 @param offset the offset.
1084 @param c a <code>char</code>.
1085 @return this string buffer.
1087 OUStringBuffer
& insert(sal_Int32 offset
, sal_Unicode c
)
1089 return insert( offset
, &c
, 1 );
1093 Inserts the string representation of the second <code>sal_Int32</code>
1094 argument into this string buffer.
1096 The second argument is converted to a string as if by the method
1097 <code>String.valueOf</code>, and the characters of that
1098 string are then inserted into this string buffer at the indicated
1101 The offset argument must be greater than or equal to
1102 <code>0</code>, and less than or equal to the length of this
1105 @param offset the offset.
1106 @param i an <code>sal_Int32</code>.
1107 @param radix the radix.
1108 @return this string buffer.
1109 @exception StringIndexOutOfBoundsException if the offset is invalid.
1111 OUStringBuffer
& insert(sal_Int32 offset
, sal_Int32 i
, sal_Int16 radix
= 10 )
1113 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFINT32
];
1114 return insert( offset
, sz
, rtl_ustr_valueOfInt32( sz
, i
, radix
) );
1118 Inserts the string representation of the <code>long</code>
1119 argument into this string buffer.
1121 The second argument is converted to a string as if by the method
1122 <code>String.valueOf</code>, and the characters of that
1123 string are then inserted into this string buffer at the indicated
1126 The offset argument must be greater than or equal to
1127 <code>0</code>, and less than or equal to the length of this
1130 @param offset the offset.
1131 @param l a <code>long</code>.
1132 @param radix the radix.
1133 @return this string buffer.
1134 @exception StringIndexOutOfBoundsException if the offset is invalid.
1136 OUStringBuffer
& insert(sal_Int32 offset
, sal_Int64 l
, sal_Int16 radix
= 10 )
1138 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFINT64
];
1139 return insert( offset
, sz
, rtl_ustr_valueOfInt64( sz
, l
, radix
) );
1143 Inserts the string representation of the <code>float</code>
1144 argument into this string buffer.
1146 The second argument is converted to a string as if by the method
1147 <code>String.valueOf</code>, and the characters of that
1148 string are then inserted into this string buffer at the indicated
1151 The offset argument must be greater than or equal to
1152 <code>0</code>, and less than or equal to the length of this
1155 @param offset the offset.
1156 @param f a <code>float</code>.
1157 @return this string buffer.
1158 @exception StringIndexOutOfBoundsException if the offset is invalid.
1160 OUStringBuffer
insert(sal_Int32 offset
, float f
)
1162 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFFLOAT
];
1163 return insert( offset
, sz
, rtl_ustr_valueOfFloat( sz
, f
) );
1167 Inserts the string representation of the <code>double</code>
1168 argument into this string buffer.
1170 The second argument is converted to a string as if by the method
1171 <code>String.valueOf</code>, and the characters of that
1172 string are then inserted into this string buffer at the indicated
1175 The offset argument must be greater than or equal to
1176 <code>0</code>, and less than or equal to the length of this
1179 @param offset the offset.
1180 @param d a <code>double</code>.
1181 @return this string buffer.
1182 @exception StringIndexOutOfBoundsException if the offset is invalid.
1184 OUStringBuffer
& insert(sal_Int32 offset
, double d
)
1186 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFDOUBLE
];
1187 return insert( offset
, sz
, rtl_ustr_valueOfDouble( sz
, d
) );
1191 Inserts a single UTF-32 character into this string buffer.
1193 <p>The single UTF-32 character will be represented within the string
1194 buffer as either one or two UTF-16 code units.</p>
1196 @param offset the offset into this string buffer (from zero to the length
1197 of this string buffer, inclusive)
1199 @param c a well-formed UTF-32 code unit (that is, a value in the range
1200 <code>0</code>–<code>0x10FFFF</code>, but excluding
1201 <code>0xD800</code>–<code>0xDFFF</code>)
1203 @return this string buffer
1205 OUStringBuffer
& insertUtf32(sal_Int32 offset
, sal_uInt32 c
) {
1206 rtl_uStringbuffer_insertUtf32(&pData
, &nCapacity
, offset
, c
);
1211 Removes the characters in a substring of this sequence.
1213 The substring begins at the specified <code>start</code> and
1214 is <code>len</code> characters long.
1216 start must be >= 0 && <= This->length
1218 @param start The beginning index, inclusive
1219 @param len The substring length
1220 @return this string buffer.
1222 OUStringBuffer
& remove( sal_Int32 start
, sal_Int32 len
)
1224 rtl_uStringbuffer_remove( &pData
, start
, len
);
1229 Removes the tail of a string buffer start at the indicate position
1231 start must be >= 0 && <= This->length
1233 @param start The beginning index, inclusive. default to 0
1234 @return this string buffer.
1236 @since LibreOffice 4.0
1238 OUStringBuffer
& truncate( sal_Int32 start
= 0 )
1240 rtl_uStringbuffer_remove( &pData
, start
, getLength() - start
);
1245 Replace all occurrences of
1246 oldChar in this string buffer with newChar.
1248 @since LibreOffice 4.0
1250 @param oldChar the old character.
1251 @param newChar the new character.
1252 @return this string buffer
1254 OUStringBuffer
& replace( sal_Unicode oldChar
, sal_Unicode newChar
)
1256 sal_Int32 index
= 0;
1257 while((index
= indexOf(oldChar
, index
)) >= 0)
1259 pData
->buffer
[ index
] = newChar
;
1264 /** Allows access to the internal data of this OUStringBuffer, for effective
1267 This method should be used with care. After you have called this
1268 method, you may use the returned pInternalData or pInternalCapacity only
1269 as long as you make no other method call on this OUStringBuffer.
1271 @param pInternalData
1272 This output parameter receives a pointer to the internal data
1273 (rtl_uString pointer). pInternalData itself must not be null.
1275 @param pInternalCapacity
1276 This output parameter receives a pointer to the internal capacity.
1277 pInternalCapacity itself must not be null.
1279 void accessInternals(rtl_uString
*** pInternalData
,
1280 sal_Int32
** pInternalCapacity
)
1282 *pInternalData
= &pData
;
1283 *pInternalCapacity
= &nCapacity
;
1288 Returns the index within this string of the first occurrence of the
1289 specified character, starting the search at the specified index.
1291 @since LibreOffice 4.0
1293 @param ch character to be located.
1294 @param fromIndex the index to start the search from.
1295 The index must be greater or equal than 0
1296 and less or equal as the string length.
1297 @return the index of the first occurrence of the character in the
1298 character sequence represented by this string that is
1299 greater than or equal to fromIndex, or
1300 -1 if the character does not occur.
1302 sal_Int32
indexOf( sal_Unicode ch
, sal_Int32 fromIndex
= 0 ) const
1304 assert( fromIndex
>= 0 && fromIndex
<= pData
->length
);
1305 sal_Int32 ret
= rtl_ustr_indexOfChar_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
, ch
);
1306 return (ret
< 0 ? ret
: ret
+fromIndex
);
1310 Returns the index within this string of the last occurrence of the
1311 specified character, searching backward starting at the end.
1313 @since LibreOffice 4.0
1315 @param ch character to be located.
1316 @return the index of the last occurrence of the character in the
1317 character sequence represented by this string, or
1318 -1 if the character does not occur.
1320 sal_Int32
lastIndexOf( sal_Unicode ch
) const
1322 return rtl_ustr_lastIndexOfChar_WithLength( pData
->buffer
, pData
->length
, ch
);
1326 Returns the index within this string of the last occurrence of the
1327 specified character, searching backward starting before the specified
1330 @since LibreOffice 4.0
1332 @param ch character to be located.
1333 @param fromIndex the index before which to start the search.
1334 @return the index of the last occurrence of the character in the
1335 character sequence represented by this string that
1336 is less than fromIndex, or -1
1337 if the character does not occur before that point.
1339 sal_Int32
lastIndexOf( sal_Unicode ch
, sal_Int32 fromIndex
) const
1341 assert( fromIndex
>= 0 && fromIndex
<= pData
->length
);
1342 return rtl_ustr_lastIndexOfChar_WithLength( pData
->buffer
, fromIndex
, ch
);
1346 Returns the index within this string of the first occurrence of the
1347 specified substring, starting at the specified index.
1349 If str doesn't include any character, always -1 is
1350 returned. This is also the case, if both strings are empty.
1352 @since LibreOffice 4.0
1354 @param str the substring to search for.
1355 @param fromIndex the index to start the search from.
1356 @return If the string argument occurs one or more times as a substring
1357 within this string at the starting index, then the index
1358 of the first character of the first such substring is
1359 returned. If it does not occur as a substring starting
1360 at fromIndex or beyond, -1 is returned.
1362 sal_Int32
indexOf( const OUString
& str
, sal_Int32 fromIndex
= 0 ) const
1364 assert( fromIndex
>= 0 && fromIndex
<= pData
->length
);
1365 sal_Int32 ret
= rtl_ustr_indexOfStr_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
1366 str
.pData
->buffer
, str
.pData
->length
);
1367 return (ret
< 0 ? ret
: ret
+fromIndex
);
1372 This function accepts an ASCII string literal as its argument.
1374 @since LibreOffice 4.0
1376 template< typename T
>
1377 typename
libreoffice_internal::ConstCharArrayDetector
< T
, sal_Int32
>::Type
indexOf( T
& literal
, sal_Int32 fromIndex
= 0 ) const
1380 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
1381 sal_Int32 n
= rtl_ustr_indexOfAscii_WithLength(
1382 pData
->buffer
+ fromIndex
, pData
->length
- fromIndex
,
1383 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
1384 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
1385 return n
< 0 ? n
: n
+ fromIndex
;
1388 #if defined LIBO_INTERNAL_ONLY
1389 /** @overload @since LibreOffice 5.3 */
1390 template<typename T
>
1392 libreoffice_internal::ConstCharArrayDetector
<T
, sal_Int32
>::TypeUtf16
1393 indexOf(T
& literal
, sal_Int32 fromIndex
= 0) const {
1394 assert(fromIndex
>= 0);
1395 auto n
= rtl_ustr_indexOfStr_WithLength(
1396 pData
->buffer
+ fromIndex
, pData
->length
- fromIndex
,
1397 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
1398 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
1399 return n
< 0 ? n
: n
+ fromIndex
;
1402 /** @overload @since LibreOffice 5.4 */
1403 sal_Int32
indexOf(OUStringLiteral
const & literal
, sal_Int32 fromIndex
= 0)
1406 sal_Int32 n
= rtl_ustr_indexOfAscii_WithLength(
1407 pData
->buffer
+ fromIndex
, pData
->length
- fromIndex
, literal
.data
,
1409 return n
< 0 ? n
: n
+ fromIndex
;
1414 Returns the index within this string of the last occurrence of
1415 the specified substring, searching backward starting at the end.
1417 The returned index indicates the starting index of the substring
1419 If str doesn't include any character, always -1 is
1420 returned. This is also the case, if both strings are empty.
1422 @since LibreOffice 4.0
1424 @param str the substring to search for.
1425 @return If the string argument occurs one or more times as a substring
1426 within this string, then the index of the first character of
1427 the last such substring is returned. If it does not occur as
1428 a substring, -1 is returned.
1430 sal_Int32
lastIndexOf( const OUString
& str
) const
1432 return rtl_ustr_lastIndexOfStr_WithLength( pData
->buffer
, pData
->length
,
1433 str
.pData
->buffer
, str
.pData
->length
);
1437 Returns the index within this string of the last occurrence of
1438 the specified substring, searching backward starting before the specified
1441 The returned index indicates the starting index of the substring
1443 If str doesn't include any character, always -1 is
1444 returned. This is also the case, if both strings are empty.
1446 @since LibreOffice 4.0
1448 @param str the substring to search for.
1449 @param fromIndex the index before which to start the search.
1450 @return If the string argument occurs one or more times as a substring
1451 within this string before the starting index, then the index
1452 of the first character of the last such substring is
1453 returned. Otherwise, -1 is returned.
1455 sal_Int32
lastIndexOf( const OUString
& str
, sal_Int32 fromIndex
) const
1457 assert( fromIndex
>= 0 && fromIndex
<= pData
->length
);
1458 return rtl_ustr_lastIndexOfStr_WithLength( pData
->buffer
, fromIndex
,
1459 str
.pData
->buffer
, str
.pData
->length
);
1464 This function accepts an ASCII string literal as its argument.
1465 @since LibreOffice 4.0
1467 template< typename T
>
1468 typename
libreoffice_internal::ConstCharArrayDetector
< T
, sal_Int32
>::Type
lastIndexOf( T
& literal
) const
1471 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
1472 return rtl_ustr_lastIndexOfAscii_WithLength(
1473 pData
->buffer
, pData
->length
,
1474 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
1475 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
1478 #if defined LIBO_INTERNAL_ONLY
1479 /** @overload @since LibreOffice 5.3 */
1480 template<typename T
>
1482 libreoffice_internal::ConstCharArrayDetector
<T
, sal_Int32
>::TypeUtf16
1483 lastIndexOf(T
& literal
) const {
1484 return rtl_ustr_lastIndexOfStr_WithLength(
1485 pData
->buffer
, pData
->length
,
1486 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
1487 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
1490 /** @overload @since LibreOffice 5.4 */
1491 sal_Int32
lastIndexOf(OUStringLiteral
const & literal
) const {
1492 return rtl_ustr_lastIndexOfAscii_WithLength(
1493 pData
->buffer
, pData
->length
, literal
.data
, literal
.size
);
1498 Strip the given character from the start of the buffer.
1500 @since LibreOffice 4.0
1502 @param c the character to strip
1503 @return The number of characters stripped
1506 sal_Int32
stripStart(sal_Unicode c
= ' ')
1509 for(index
= 0; index
< getLength() ; index
++)
1511 if(pData
->buffer
[ index
] != c
)
1524 Strip the given character from the end of the buffer.
1526 @since LibreOffice 4.0
1528 @param c the character to strip
1529 @return The number of characters stripped
1532 sal_Int32
stripEnd(sal_Unicode c
= ' ')
1534 sal_Int32 result
= getLength();
1536 for(index
= getLength(); index
> 0 ; index
--)
1538 if(pData
->buffer
[ index
- 1 ] != c
)
1543 if(index
< getLength())
1547 return result
- getLength();
1550 Strip the given character from the both end of the buffer.
1552 @since LibreOffice 4.0
1554 @param c the character to strip
1555 @return The number of characters stripped
1558 sal_Int32
strip(sal_Unicode c
= ' ')
1560 return stripStart(c
) + stripEnd(c
);
1563 Returns a new string buffer that is a substring of this string.
1565 The substring begins at the specified beginIndex. If
1566 beginIndex is negative or be greater than the length of
1567 this string, behaviour is undefined.
1569 @param beginIndex the beginning index, inclusive.
1570 @return the specified substring.
1571 @since LibreOffice 4.1
1573 OUStringBuffer
copy( sal_Int32 beginIndex
) const
1575 return copy( beginIndex
, getLength() - beginIndex
);
1579 Returns a new string buffer that is a substring of this string.
1581 The substring begins at the specified beginIndex and contains count
1582 characters. If either beginIndex or count are negative,
1583 or beginIndex + count are greater than the length of this string
1584 then behaviour is undefined.
1586 @param beginIndex the beginning index, inclusive.
1587 @param count the number of characters.
1588 @return the specified substring.
1589 @since LibreOffice 4.1
1591 OUStringBuffer
copy( sal_Int32 beginIndex
, sal_Int32 count
) const
1593 assert(beginIndex
>= 0 && beginIndex
<= getLength());
1594 assert(count
>= 0 && count
<= getLength() - beginIndex
);
1595 rtl_uString
*pNew
= NULL
;
1596 rtl_uStringbuffer_newFromStr_WithLength( &pNew
, getStr() + beginIndex
, count
);
1597 return OUStringBuffer( pNew
, count
+ 16 );
1601 OUStringBuffer( rtl_uString
* value
, const sal_Int32 capacity
)
1604 nCapacity
= capacity
;
1608 A pointer to the data structure which contains the data.
1610 rtl_uString
* pData
;
1613 The len of the pData->buffer.
1615 sal_Int32 nCapacity
;
1620 #ifdef RTL_STRING_UNITTEST
1623 typedef rtlunittest::OUStringBuffer OUStringBuffer
;
1627 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
1628 using ::rtl::OUStringBuffer
;
1631 #endif // INCLUDED_RTL_USTRBUF_HXX
1633 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */