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>
28 #include <osl/diagnose.h>
29 #include <rtl/ustrbuf.h>
30 #include <rtl/ustring.hxx>
31 #include <rtl/stringutils.hxx>
32 #include <sal/types.h>
34 #ifdef RTL_FAST_STRING
35 #include <rtl/stringconcat.hxx>
38 // The unittest uses slightly different code to help check that the proper
39 // calls are made. The class is put into a different namespace to make
40 // sure the compiler generates a different (if generating also non-inline)
41 // copy of the function and does not merge them together. The class
42 // is "brought" into the proper rtl namespace by a typedef below.
43 #ifdef RTL_STRING_UNITTEST
44 #define rtl rtlunittest
50 #ifdef RTL_STRING_UNITTEST
54 /** A string buffer implements a mutable sequence of characters.
56 String buffers are safe for use by multiple threads. The methods
57 are synchronized where necessary so that all the operations on any
58 particular instance behave as if they occur in some serial order.
60 String buffers are used by the compiler to implement the binary
61 string concatenation operator <code>+</code>. For example, the code:
64 </pre></blockquote><p>
65 is compiled to the equivalent of:
67 x = new OUStringBuffer().append("a").append(4).append("c")
69 </pre></blockquote><p>
70 The principal operations on a <code>OUStringBuffer</code> are the
71 <code>append</code> and <code>insert</code> methods, which are
72 overloaded so as to accept data of any type. Each effectively
73 converts a given datum to a string and then appends or inserts the
74 characters of that string to the string buffer. The
75 <code>append</code> method always adds these characters at the end
76 of the buffer; the <code>insert</code> method adds the characters at
79 For example, if <code>z</code> refers to a string buffer object
80 whose current contents are "<code>start</code>", then
81 the method call <code>z.append("le")</code> would cause the string
82 buffer to contain "<code>startle</code>", whereas
83 <code>z.insert(4, "le")</code> would alter the string buffer to
84 contain "<code>starlet</code>".
86 Every string buffer has a capacity. As long as the length of the
87 character sequence contained in the string buffer does not exceed
88 the capacity, it is not necessary to allocate a new internal
89 buffer array. If the internal buffer overflows, it is
90 automatically made larger.
92 class SAL_WARN_UNUSED OUStringBuffer
96 Constructs a string buffer with no characters in it and an
97 initial capacity of 16 characters.
103 rtl_uString_new_WithLength( &pData
, nCapacity
);
107 Allocates a new string buffer that contains the same sequence of
108 characters as the string buffer argument.
110 @param value a <code>OUStringBuffer</code>.
112 OUStringBuffer( const OUStringBuffer
& value
)
114 , nCapacity( value
.nCapacity
)
116 rtl_uStringbuffer_newFromStringBuffer( &pData
, value
.nCapacity
, value
.pData
);
120 Constructs a string buffer with no characters in it and an
121 initial capacity specified by the <code>length</code> argument.
123 @param length the initial capacity.
125 explicit OUStringBuffer(int length
)
127 , nCapacity( length
)
129 rtl_uString_new_WithLength( &pData
, length
);
133 Constructs a string buffer so that it represents the same
134 sequence of characters as the string argument.
137 capacity of the string buffer is <code>16</code> plus the length
138 of the string argument.
140 @param value the initial contents of the buffer.
142 OUStringBuffer(const OUString
& value
)
144 , nCapacity( value
.getLength() + 16 )
146 rtl_uStringbuffer_newFromStr_WithLength( &pData
, value
.getStr(), value
.getLength() );
149 template< typename T
>
150 OUStringBuffer( T
& literal
, typename
internal::ConstCharArrayDetector
< T
, internal::Dummy
>::Type
= internal::Dummy() )
152 , nCapacity( internal::ConstCharArrayDetector
< T
, void >::size
- 1 + 16 )
154 assert( strlen( literal
) == internal::ConstCharArrayDetector
< T
>::size
- 1 );
155 rtl_uString_newFromLiteral( &pData
, literal
, internal::ConstCharArrayDetector
< T
, void >::size
- 1, 16 );
156 #ifdef RTL_STRING_UNITTEST
157 rtl_string_unittest_const_literal
= true;
161 #ifdef RTL_STRING_UNITTEST
163 * Only used by unittests to detect incorrect conversions.
166 template< typename T
>
167 OUStringBuffer( T
&, typename
internal::ExceptConstCharArrayDetector
< T
>::Type
= internal::Dummy() )
171 rtl_uString_newFromLiteral( &pData
, "!!br0ken!!", 10, 0 ); // set to garbage
172 rtl_string_unittest_invalid_conversion
= true;
175 * Only used by unittests to detect incorrect conversions.
178 template< typename T
>
179 OUStringBuffer( const T
&, typename
internal::ExceptCharArrayDetector
< T
>::Type
= internal::Dummy() )
183 rtl_uString_newFromLiteral( &pData
, "!!br0ken!!", 10, 0 ); // set to garbage
184 rtl_string_unittest_invalid_conversion
= true;
188 #ifdef RTL_FAST_STRING
193 template< typename T1
, typename T2
>
194 OUStringBuffer( const OUStringConcat
< T1
, T2
>& c
)
196 const sal_Int32 l
= c
.length();
198 pData
= rtl_uString_alloc( nCapacity
);
199 sal_Unicode
* end
= c
.addData( pData
->buffer
);
201 pData
->length
= end
- pData
->buffer
;
202 // TODO realloc in case pData->>length is noticeably smaller than l ?
205 /** Assign to this a copy of value.
207 OUStringBuffer
& operator = ( const OUStringBuffer
& value
)
211 rtl_uStringbuffer_newFromStringBuffer(&pData
,
214 nCapacity
= value
.nCapacity
;
220 Release the string data.
224 rtl_uString_release( pData
);
228 Fill the string data in the new string and clear the buffer.
230 This method is more efficient than the contructor of the string. It does
233 @return the string previously contained in the buffer.
235 OUString
makeStringAndClear()
238 rtl_uStringBuffer_makeStringAndClear( &pData
, &nCapacity
),
243 Returns the length (character count) of this string buffer.
245 @return the number of characters in this string buffer.
247 sal_Int32
getLength() const
249 return pData
->length
;
253 Checks if a string buffer is empty.
255 @return true if the string buffer is empty;
258 @since LibreOffice 4.1
260 bool isEmpty() const SAL_THROW(())
262 return pData
->length
== 0;
266 Returns the current capacity of the String buffer.
269 is the amount of storage available for newly inserted
270 characters. The real buffer size is 2 bytes longer, because
271 all strings are 0 terminated.
273 @return the current capacity of this string buffer.
275 sal_Int32
getCapacity() const
281 Ensures that the capacity of the buffer is at least equal to the
284 The new capacity will be at least as large as the maximum of the current
285 length (so that no contents of the buffer is destroyed) and the given
286 minimumCapacity. If the given minimumCapacity is negative, nothing is
289 @param minimumCapacity the minimum desired capacity.
291 void ensureCapacity(sal_Int32 minimumCapacity
)
293 rtl_uStringbuffer_ensureCapacity( &pData
, &nCapacity
, minimumCapacity
);
297 Sets the length of this String buffer.
299 If the <code>newLength</code> argument is less than the current
300 length of the string buffer, the string buffer is truncated to
301 contain exactly the number of characters given by the
302 <code>newLength</code> argument.
304 If the <code>newLength</code> argument is greater than or equal
305 to the current length, sufficient null characters
306 (<code>'\u0000'</code>) are appended to the string buffer so that
307 length becomes the <code>newLength</code> argument.
309 The <code>newLength</code> argument must be greater than or equal
312 @param newLength the new length of the buffer.
314 void setLength(sal_Int32 newLength
)
316 assert(newLength
>= 0);
317 // Avoid modifications if pData points to const empty string:
318 if( newLength
!= pData
->length
)
320 if( newLength
> nCapacity
)
321 rtl_uStringbuffer_ensureCapacity(&pData
, &nCapacity
, newLength
);
323 pData
->buffer
[newLength
] = 0;
324 pData
->length
= newLength
;
329 Returns the character at a specific index in this string buffer.
331 The first character of a string buffer is at index
332 <code>0</code>, the next at index <code>1</code>, and so on, for
335 The index argument must be greater than or equal to
336 <code>0</code>, and less than the length of this string buffer.
338 @param index the index of the desired character.
339 @return the character at the specified index of this string buffer.
341 SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
342 sal_Unicode
charAt( sal_Int32 index
) const
344 assert(index
>= 0 && index
< pData
->length
);
345 return pData
->buffer
[ index
];
349 The character at the specified index of this string buffer is set
352 The index argument must be greater than or equal to
353 <code>0</code>, and less than the length of this string buffer.
355 @param index the index of the character to modify.
356 @param ch the new character.
358 SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
359 OUStringBuffer
& setCharAt(sal_Int32 index
, sal_Unicode ch
)
361 assert(index
>= 0 && index
< pData
->length
);
362 pData
->buffer
[ index
] = ch
;
367 Return a null terminated unicode character array.
369 const sal_Unicode
* getStr() const { return pData
->buffer
; }
372 Access to individual characters.
374 @param index must be non-negative and less than length.
376 @return a reference to the character at the given index.
378 @since LibreOffice 3.5
380 sal_Unicode
& operator [](sal_Int32 index
)
382 assert(index
>= 0 && index
< pData
->length
);
383 return pData
->buffer
[index
];
387 Access to individual characters.
389 @param index must be non-negative and less than length.
391 @return a reference to the character at the given index.
393 @since LibreOffice 4.2
395 const sal_Unicode
& operator [](sal_Int32 index
) const
397 assert(index
>= 0 && index
< pData
->length
);
398 return pData
->buffer
[index
];
402 Return a OUString instance reflecting the current content
403 of this OUStringBuffer.
405 const OUString
toString() const
407 return OUString(pData
->buffer
, pData
->length
);
411 Appends the string to this string buffer.
413 The characters of the <code>OUString</code> argument are appended, in
414 order, to the contents of this string buffer, increasing the
415 length of this string buffer by the length of the argument.
418 @return this string buffer.
420 OUStringBuffer
& append(const OUString
&str
)
422 return append( str
.getStr(), str
.getLength() );
426 Appends the content of a stringbuffer to this string buffer.
428 The characters of the <code>OUStringBuffer</code> argument are appended, in
429 order, to the contents of this string buffer, increasing the
430 length of this string buffer by the length of the argument.
433 @return this string buffer.
435 @since LibreOffice 4.0
437 OUStringBuffer
& append(const OUStringBuffer
&str
)
441 append( str
.getStr(), str
.getLength() );
447 Appends the string representation of the <code>char</code> array
448 argument to this string buffer.
450 The characters of the array argument are appended, in order, to
451 the contents of this string buffer. The length of this string
452 buffer increases by the length of the argument.
454 @param str the characters to be appended.
455 @return this string buffer.
457 OUStringBuffer
& append( const sal_Unicode
* str
)
459 return append( str
, rtl_ustr_getLength( str
) );
463 Appends the string representation of the <code>char</code> array
464 argument to this string buffer.
466 Characters of the character array <code>str</code> are appended,
467 in order, to the contents of this string buffer. The length of this
468 string buffer increases by the value of <code>len</code>.
470 @param str the characters to be appended; must be non-null, and must
471 point to at least len characters
472 @param len the number of characters to append; must be non-negative
473 @return this string buffer.
475 OUStringBuffer
& append( const sal_Unicode
* str
, sal_Int32 len
)
477 // insert behind the last character
478 rtl_uStringbuffer_insert( &pData
, &nCapacity
, getLength(), str
, len
);
484 This function accepts an ASCII string literal as its argument.
485 @since LibreOffice 3.6
487 template< typename T
>
488 typename
internal::ConstCharArrayDetector
< T
, OUStringBuffer
& >::Type
append( T
& literal
)
490 assert( strlen( literal
) == internal::ConstCharArrayDetector
< T
>::size
- 1 );
491 rtl_uStringbuffer_insert_ascii( &pData
, &nCapacity
, getLength(), literal
,
492 internal::ConstCharArrayDetector
< T
, void >::size
- 1 );
496 #ifdef RTL_FAST_STRING
501 template< typename T1
, typename T2
>
502 OUStringBuffer
& append( const OUStringConcat
< T1
, T2
>& c
)
504 const int l
= c
.length();
507 rtl_uStringbuffer_ensureCapacity( &pData
, &nCapacity
, pData
->length
+ l
);
508 sal_Unicode
* end
= c
.addData( pData
->buffer
+ pData
->length
);
510 pData
->length
= end
- pData
->buffer
;
516 Appends a 8-Bit ASCII character string to this string buffer.
518 Since this method is optimized for performance. the ASCII
519 character values are not converted in any way. The caller
520 has to make sure that all ASCII characters are in the
521 allowed range between 0 and 127. The ASCII string must be
524 The characters of the array argument are appended, in order, to
525 the contents of this string buffer. The length of this string
526 buffer increases by the length of the argument.
528 @param str the 8-Bit ASCII characters to be appended.
529 @return this string buffer.
531 OUStringBuffer
& appendAscii( const sal_Char
* str
)
533 return appendAscii( str
, rtl_str_getLength( str
) );
537 Appends a 8-Bit ASCII character string to this string buffer.
539 Since this method is optimized for performance. the ASCII
540 character values are not converted in any way. The caller
541 has to make sure that all ASCII characters are in the
542 allowed range between 0 and 127. The ASCII string must be
545 Characters of the character array <code>str</code> are appended,
546 in order, to the contents of this string buffer. The length of this
547 string buffer increases by the value of <code>len</code>.
549 @param str the 8-Bit ASCII characters to be appended; must be non-null,
550 and must point to at least len characters
551 @param len the number of characters to append; must be non-negative
552 @return this string buffer.
554 OUStringBuffer
& appendAscii( const sal_Char
* str
, sal_Int32 len
)
556 rtl_uStringbuffer_insert_ascii( &pData
, &nCapacity
, getLength(), str
, len
);
561 Appends the string representation of the <code>bool</code>
562 argument to the string buffer.
564 The argument is converted to a string as if by the method
565 <code>String.valueOf</code>, and the characters of that
566 string are then appended to this string buffer.
568 @param b a <code>bool</code>.
569 @return this string buffer.
571 @since LibreOffice 4.1
573 OUStringBuffer
& append(bool b
)
575 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFBOOLEAN
];
576 return append( sz
, rtl_ustr_valueOfBoolean( sz
, b
) );
579 // Pointer can be automatically converted to bool, which is unwanted here.
580 // Explicitly delete all pointer append() overloads to prevent this
581 // (except for char* and sal_Unicode* overloads, which are handled elsewhere).
582 template< typename T
>
583 typename
internal::Enable
< void,
584 !internal::CharPtrDetector
< T
* >::ok
&& !internal::SalUnicodePtrDetector
< T
* >::ok
>::Type
585 append( T
* ) SAL_DELETED_FUNCTION
;
587 // This overload is needed because OUString has a ctor from rtl_uString*, but
588 // the bool overload above would be prefered to the conversion.
592 OUStringBuffer
& append(rtl_uString
* str
)
594 return append( OUString( str
));
598 Appends the string representation of the <code>sal_Bool</code>
599 argument to the string buffer.
601 The argument is converted to a string as if by the method
602 <code>String.valueOf</code>, and the characters of that
603 string are then appended to this string buffer.
605 @param b a <code>sal_Bool</code>.
606 @return this string buffer.
608 OUStringBuffer
& append(sal_Bool b
)
610 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFBOOLEAN
];
611 return append( sz
, rtl_ustr_valueOfBoolean( sz
, b
) );
615 Appends the string representation of the ASCII <code>char</code>
616 argument to this string buffer.
618 The argument is appended to the contents of this string buffer.
619 The length of this string buffer increases by <code>1</code>.
621 @param c an ASCII <code>char</code>.
622 @return this string buffer.
624 @since LibreOffice 3.5
626 OUStringBuffer
& append(char c
)
628 assert(static_cast< unsigned char >(c
) <= 0x7F);
629 return append(sal_Unicode(c
));
633 Appends the string representation of the <code>char</code>
634 argument to this string buffer.
636 The argument is appended to the contents of this string buffer.
637 The length of this string buffer increases by <code>1</code>.
639 @param c a <code>char</code>.
640 @return this string buffer.
642 OUStringBuffer
& append(sal_Unicode c
)
644 return append( &c
, 1 );
648 Appends the string representation of the <code>sal_Int32</code>
649 argument to this string buffer.
651 The argument is converted to a string as if by the method
652 <code>String.valueOf</code>, and the characters of that
653 string are then appended to this string buffer.
655 @param i an <code>sal_Int32</code>.
656 @param radix the radix
657 @return this string buffer.
659 OUStringBuffer
& append(sal_Int32 i
, sal_Int16 radix
= 10 )
661 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFINT32
];
662 return append( sz
, rtl_ustr_valueOfInt32( sz
, i
, radix
) );
666 Appends the string representation of the <code>long</code>
667 argument to this string buffer.
669 The argument is converted to a string as if by the method
670 <code>String.valueOf</code>, and the characters of that
671 string are then appended to this string buffer.
673 @param l a <code>long</code>.
674 @param radix the radix
675 @return this string buffer.
677 OUStringBuffer
& append(sal_Int64 l
, sal_Int16 radix
= 10 )
679 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFINT64
];
680 return append( sz
, rtl_ustr_valueOfInt64( sz
, l
, radix
) );
684 Appends the string representation of the <code>float</code>
685 argument to this string buffer.
687 The argument is converted to a string as if by the method
688 <code>String.valueOf</code>, and the characters of that
689 string are then appended to this string buffer.
691 @param f a <code>float</code>.
692 @return this string buffer.
694 OUStringBuffer
& append(float f
)
696 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFFLOAT
];
697 return append( sz
, rtl_ustr_valueOfFloat( sz
, f
) );
701 Appends the string representation of the <code>double</code>
702 argument to this string buffer.
704 The argument is converted to a string as if by the method
705 <code>String.valueOf</code>, and the characters of that
706 string are then appended to this string buffer.
708 @param d a <code>double</code>.
709 @return this string buffer.
711 OUStringBuffer
& append(double d
)
713 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFDOUBLE
];
714 return append( sz
, rtl_ustr_valueOfDouble( sz
, d
) );
718 Appends a single UTF-32 character to this string buffer.
720 <p>The single UTF-32 character will be represented within the string
721 buffer as either one or two UTF-16 code units.</p>
723 @param c a well-formed UTF-32 code unit (that is, a value in the range
724 <code>0</code>–<code>0x10FFFF</code>, but excluding
725 <code>0xD800</code>–<code>0xDFFF</code>)
730 OUStringBuffer
& appendUtf32(sal_uInt32 c
) {
731 return insertUtf32(getLength(), c
);
735 Inserts the string into this string buffer.
737 The characters of the <code>String</code> argument are inserted, in
738 order, into this string buffer at the indicated offset. The length
739 of this string buffer is increased by the length of the argument.
741 The offset argument must be greater than or equal to
742 <code>0</code>, and less than or equal to the length of this
745 @param offset the offset.
747 @return this string buffer.
749 OUStringBuffer
& insert(sal_Int32 offset
, const OUString
& str
)
751 return insert( offset
, str
.getStr(), str
.getLength() );
755 Inserts the string representation of the <code>char</code> array
756 argument into this string buffer.
758 The characters of the array argument are inserted into the
759 contents of this string buffer at the position indicated by
760 <code>offset</code>. The length of this string buffer increases by
761 the length of the argument.
763 The offset argument must be greater than or equal to
764 <code>0</code>, and less than or equal to the length of this
767 @param offset the offset.
768 @param str a character array.
769 @return this string buffer.
771 OUStringBuffer
& insert( sal_Int32 offset
, const sal_Unicode
* str
)
773 return insert( offset
, str
, rtl_ustr_getLength( str
) );
777 Inserts the string representation of the <code>char</code> array
778 argument into this string buffer.
780 The characters of the array argument are inserted into the
781 contents of this string buffer at the position indicated by
782 <code>offset</code>. The length of this string buffer increases by
783 the length of the argument.
785 The offset argument must be greater than or equal to
786 <code>0</code>, and less than or equal to the length of this
789 @param offset the offset.
790 @param str a character array.
791 @param len the number of characters to append.
792 @return this string buffer.
794 OUStringBuffer
& insert( sal_Int32 offset
, const sal_Unicode
* str
, sal_Int32 len
)
796 // insert behind the last character
797 rtl_uStringbuffer_insert( &pData
, &nCapacity
, offset
, str
, len
);
803 This function accepts an ASCII string literal as its argument.
804 @since LibreOffice 3.6
806 template< typename T
>
807 typename
internal::ConstCharArrayDetector
< T
, OUStringBuffer
& >::Type
insert( sal_Int32 offset
, T
& literal
)
809 assert( strlen( literal
) == internal::ConstCharArrayDetector
< T
>::size
- 1 );
810 rtl_uStringbuffer_insert_ascii( &pData
, &nCapacity
, offset
, literal
,
811 internal::ConstCharArrayDetector
< T
, void >::size
- 1 );
816 Inserts the string representation of the <code>sal_Bool</code>
817 argument into this string buffer.
819 The second argument is converted to a string as if by the method
820 <code>String.valueOf</code>, and the characters of that
821 string are then inserted into this string buffer at the indicated
824 The offset argument must be greater than or equal to
825 <code>0</code>, and less than or equal to the length of this
828 @param offset the offset.
829 @param b a <code>sal_Bool</code>.
830 @return this string buffer.
832 OUStringBuffer
& insert(sal_Int32 offset
, sal_Bool b
)
834 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFBOOLEAN
];
835 return insert( offset
, sz
, rtl_ustr_valueOfBoolean( sz
, b
) );
839 Inserts the string representation of the <code>char</code>
840 argument into this string buffer.
842 The second argument is inserted into the contents of this string
843 buffer at the position indicated by <code>offset</code>. The length
844 of this string buffer increases by one.
846 The offset argument must be greater than or equal to
847 <code>0</code>, and less than or equal to the length of this
850 @param offset the offset.
851 @param c a <code>char</code>.
852 @return this string buffer.
854 @since LibreOffice 3.6
856 OUStringBuffer
& insert(sal_Int32 offset
, char c
)
859 return insert( offset
, &u
, 1 );
863 Inserts the string representation of the <code>char</code>
864 argument into this string buffer.
866 The second argument is inserted into the contents of this string
867 buffer at the position indicated by <code>offset</code>. The length
868 of this string buffer increases by one.
870 The offset argument must be greater than or equal to
871 <code>0</code>, and less than or equal to the length of this
874 @param offset the offset.
875 @param c a <code>char</code>.
876 @return this string buffer.
878 OUStringBuffer
& insert(sal_Int32 offset
, sal_Unicode c
)
880 return insert( offset
, &c
, 1 );
884 Inserts the string representation of the second <code>sal_Int32</code>
885 argument into this string buffer.
887 The second argument is converted to a string as if by the method
888 <code>String.valueOf</code>, and the characters of that
889 string are then inserted into this string buffer at the indicated
892 The offset argument must be greater than or equal to
893 <code>0</code>, and less than or equal to the length of this
896 @param offset the offset.
897 @param i an <code>sal_Int32</code>.
898 @param radix the radix.
899 @return this string buffer.
900 @exception StringIndexOutOfBoundsException if the offset is invalid.
902 OUStringBuffer
& insert(sal_Int32 offset
, sal_Int32 i
, sal_Int16 radix
= 10 )
904 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFINT32
];
905 return insert( offset
, sz
, rtl_ustr_valueOfInt32( sz
, i
, radix
) );
909 Inserts the string representation of the <code>long</code>
910 argument into this string buffer.
912 The second argument is converted to a string as if by the method
913 <code>String.valueOf</code>, and the characters of that
914 string are then inserted into this string buffer at the indicated
917 The offset argument must be greater than or equal to
918 <code>0</code>, and less than or equal to the length of this
921 @param offset the offset.
922 @param l a <code>long</code>.
923 @param radix the radix.
924 @return this string buffer.
925 @exception StringIndexOutOfBoundsException if the offset is invalid.
927 OUStringBuffer
& insert(sal_Int32 offset
, sal_Int64 l
, sal_Int16 radix
= 10 )
929 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFINT64
];
930 return insert( offset
, sz
, rtl_ustr_valueOfInt64( sz
, l
, radix
) );
934 Inserts the string representation of the <code>float</code>
935 argument into this string buffer.
937 The second argument is converted to a string as if by the method
938 <code>String.valueOf</code>, and the characters of that
939 string are then inserted into this string buffer at the indicated
942 The offset argument must be greater than or equal to
943 <code>0</code>, and less than or equal to the length of this
946 @param offset the offset.
947 @param f a <code>float</code>.
948 @return this string buffer.
949 @exception StringIndexOutOfBoundsException if the offset is invalid.
951 OUStringBuffer
insert(sal_Int32 offset
, float f
)
953 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFFLOAT
];
954 return insert( offset
, sz
, rtl_ustr_valueOfFloat( sz
, f
) );
958 Inserts the string representation of the <code>double</code>
959 argument into this string buffer.
961 The second argument is converted to a string as if by the method
962 <code>String.valueOf</code>, and the characters of that
963 string are then inserted into this string buffer at the indicated
966 The offset argument must be greater than or equal to
967 <code>0</code>, and less than or equal to the length of this
970 @param offset the offset.
971 @param d a <code>double</code>.
972 @return this string buffer.
973 @exception StringIndexOutOfBoundsException if the offset is invalid.
975 OUStringBuffer
& insert(sal_Int32 offset
, double d
)
977 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFDOUBLE
];
978 return insert( offset
, sz
, rtl_ustr_valueOfDouble( sz
, d
) );
982 Inserts a single UTF-32 character into this string buffer.
984 <p>The single UTF-32 character will be represented within the string
985 buffer as either one or two UTF-16 code units.</p>
987 @param offset the offset into this string buffer (from zero to the length
988 of this string buffer, inclusive)
990 @param c a well-formed UTF-32 code unit (that is, a value in the range
991 <code>0</code>–<code>0x10FFFF</code>, but excluding
992 <code>0xD800</code>–<code>0xDFFF</code>)
994 @return this string buffer
996 OUStringBuffer
& insertUtf32(sal_Int32 offset
, sal_uInt32 c
) {
997 rtl_uStringbuffer_insertUtf32(&pData
, &nCapacity
, offset
, c
);
1002 Removes the characters in a substring of this sequence.
1004 The substring begins at the specified <code>start</code> and
1005 is <code>len</code> characters long.
1007 start must be >= 0 && <= This->length
1009 @param start The beginning index, inclusive
1010 @param len The substring length
1011 @return this string buffer.
1013 OUStringBuffer
& remove( sal_Int32 start
, sal_Int32 len
)
1015 rtl_uStringbuffer_remove( &pData
, start
, len
);
1020 Removes the tail of a string buffer start at the indicate position
1022 start must be >= 0 && <= This->length
1024 @param start The beginning index, inclusive. default to 0
1025 @return this string buffer.
1027 @since LibreOffice 4.0
1029 OUStringBuffer
& truncate( sal_Int32 start
= 0 )
1031 rtl_uStringbuffer_remove( &pData
, start
, getLength() - start
);
1036 Replace all occurrences of
1037 oldChar in this string buffer with newChar.
1039 @since LibreOffice 4.0
1041 @param oldChar the old character.
1042 @param newChar the new character.
1043 @return this string buffer
1045 OUStringBuffer
& replace( sal_Unicode oldChar
, sal_Unicode newChar
)
1047 sal_Int32 index
= 0;
1048 while((index
= indexOf(oldChar
, index
)) >= 0)
1050 pData
->buffer
[ index
] = newChar
;
1055 /** Allows access to the internal data of this OUStringBuffer, for effective
1058 This method should be used with care. After you have called this
1059 method, you may use the returned pInternalData or pInternalCapacity only
1060 as long as you make no other method call on this OUStringBuffer.
1062 @param pInternalData
1063 This output parameter receives a pointer to the internal data
1064 (rtl_uString pointer). pInternalData itself must not be null.
1066 @param pInternalCapacity
1067 This output parameter receives a pointer to the internal capacity.
1068 pInternalCapacity itself must not be null.
1070 inline void accessInternals(rtl_uString
*** pInternalData
,
1071 sal_Int32
** pInternalCapacity
)
1073 *pInternalData
= &pData
;
1074 *pInternalCapacity
= &nCapacity
;
1079 Returns the index within this string of the first occurrence of the
1080 specified character, starting the search at the specified index.
1082 @since LibreOffice 4.0
1084 @param ch character to be located.
1085 @param fromIndex the index to start the search from.
1086 The index must be greater or equal than 0
1087 and less or equal as the string length.
1088 @return the index of the first occurrence of the character in the
1089 character sequence represented by this string that is
1090 greater than or equal to fromIndex, or
1091 -1 if the character does not occur.
1093 sal_Int32
indexOf( sal_Unicode ch
, sal_Int32 fromIndex
= 0 ) const SAL_THROW(())
1095 sal_Int32 ret
= rtl_ustr_indexOfChar_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
, ch
);
1096 return (ret
< 0 ? ret
: ret
+fromIndex
);
1100 Returns the index within this string of the last occurrence of the
1101 specified character, searching backward starting at the end.
1103 @since LibreOffice 4.0
1105 @param ch character to be located.
1106 @return the index of the last occurrence of the character in the
1107 character sequence represented by this string, or
1108 -1 if the character does not occur.
1110 sal_Int32
lastIndexOf( sal_Unicode ch
) const SAL_THROW(())
1112 return rtl_ustr_lastIndexOfChar_WithLength( pData
->buffer
, pData
->length
, ch
);
1116 Returns the index within this string of the last occurrence of the
1117 specified character, searching backward starting before the specified
1120 @since LibreOffice 4.0
1122 @param ch character to be located.
1123 @param fromIndex the index before which to start the search.
1124 @return the index of the last occurrence of the character in the
1125 character sequence represented by this string that
1126 is less than fromIndex, or -1
1127 if the character does not occur before that point.
1129 sal_Int32
lastIndexOf( sal_Unicode ch
, sal_Int32 fromIndex
) const SAL_THROW(())
1131 return rtl_ustr_lastIndexOfChar_WithLength( pData
->buffer
, fromIndex
, ch
);
1135 Returns the index within this string of the first occurrence of the
1136 specified substring, starting at the specified index.
1138 If str doesn't include any character, always -1 is
1139 returned. This is also the case, if both strings are empty.
1141 @since LibreOffice 4.0
1143 @param str the substring to search for.
1144 @param fromIndex the index to start the search from.
1145 @return If the string argument occurs one or more times as a substring
1146 within this string at the starting index, then the index
1147 of the first character of the first such substring is
1148 returned. If it does not occur as a substring starting
1149 at fromIndex or beyond, -1 is returned.
1151 sal_Int32
indexOf( const OUString
& str
, sal_Int32 fromIndex
= 0 ) const SAL_THROW(())
1153 sal_Int32 ret
= rtl_ustr_indexOfStr_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
1154 str
.pData
->buffer
, str
.pData
->length
);
1155 return (ret
< 0 ? ret
: ret
+fromIndex
);
1160 This function accepts an ASCII string literal as its argument.
1162 @since LibreOffice 4.0
1164 template< typename T
>
1165 typename
internal::ConstCharArrayDetector
< T
, sal_Int32
>::Type
indexOf( T
& literal
, sal_Int32 fromIndex
= 0 ) const SAL_THROW(())
1167 assert( strlen( literal
) == internal::ConstCharArrayDetector
< T
>::size
- 1 );
1168 sal_Int32 ret
= rtl_ustr_indexOfAscii_WithLength(
1169 pData
->buffer
+ fromIndex
, pData
->length
- fromIndex
, literal
,
1170 internal::ConstCharArrayDetector
< T
, void >::size
- 1);
1171 return ret
< 0 ? ret
: ret
+ fromIndex
;
1175 Returns the index within this string of the last occurrence of
1176 the specified substring, searching backward starting at the end.
1178 The returned index indicates the starting index of the substring
1180 If str doesn't include any character, always -1 is
1181 returned. This is also the case, if both strings are empty.
1183 @since LibreOffice 4.0
1185 @param str the substring to search for.
1186 @return If the string argument occurs one or more times as a substring
1187 within this string, then the index of the first character of
1188 the last such substring is returned. If it does not occur as
1189 a substring, -1 is returned.
1191 sal_Int32
lastIndexOf( const OUString
& str
) const SAL_THROW(())
1193 return rtl_ustr_lastIndexOfStr_WithLength( pData
->buffer
, pData
->length
,
1194 str
.pData
->buffer
, str
.pData
->length
);
1198 Returns the index within this string of the last occurrence of
1199 the specified substring, searching backward starting before the specified
1202 The returned index indicates the starting index of the substring
1204 If str doesn't include any character, always -1 is
1205 returned. This is also the case, if both strings are empty.
1207 @since LibreOffice 4.0
1209 @param str the substring to search for.
1210 @param fromIndex the index before which to start the search.
1211 @return If the string argument occurs one or more times as a substring
1212 within this string before the starting index, then the index
1213 of the first character of the last such substring is
1214 returned. Otherwise, -1 is returned.
1216 sal_Int32
lastIndexOf( const OUString
& str
, sal_Int32 fromIndex
) const SAL_THROW(())
1218 return rtl_ustr_lastIndexOfStr_WithLength( pData
->buffer
, fromIndex
,
1219 str
.pData
->buffer
, str
.pData
->length
);
1224 This function accepts an ASCII string literal as its argument.
1225 @since LibreOffice 4.0
1227 template< typename T
>
1228 typename
internal::ConstCharArrayDetector
< T
, sal_Int32
>::Type
lastIndexOf( T
& literal
) const SAL_THROW(())
1230 assert( strlen( literal
) == internal::ConstCharArrayDetector
< T
>::size
- 1 );
1231 return rtl_ustr_lastIndexOfAscii_WithLength(
1232 pData
->buffer
, pData
->length
, literal
, internal::ConstCharArrayDetector
< T
, void >::size
- 1);
1236 Strip the given character from the start of the buffer.
1238 @since LibreOffice 4.0
1240 @param c the character to strip
1241 @return The number of characters stripped
1244 sal_Int32
stripStart(sal_Unicode c
= (sal_Unicode
)' ')
1247 for(index
= 0; index
< getLength() ; index
++)
1249 if(pData
->buffer
[ index
] != c
)
1262 Strip the given character from the end of the buffer.
1264 @since LibreOffice 4.0
1266 @param c the character to strip
1267 @return The number of characters stripped
1270 sal_Int32
stripEnd(sal_Unicode c
= (sal_Unicode
)' ')
1272 sal_Int32 result
= getLength();
1274 for(index
= getLength(); index
> 0 ; index
--)
1276 if(pData
->buffer
[ index
- 1 ] != c
)
1281 if(index
< getLength())
1285 return result
- getLength();
1288 Strip the given character from the both end of the buffer.
1290 @since LibreOffice 4.0
1292 @param c the character to strip
1293 @return The number of characters stripped
1296 sal_Int32
strip(sal_Unicode c
= (sal_Unicode
)' ')
1298 return stripStart(c
) + stripEnd(c
);
1301 Returns a new string buffer that is a substring of this string.
1303 The substring begins at the specified beginIndex. If
1304 beginIndex is negative or be greater than the length of
1305 this string, behaviour is undefined.
1307 @param beginIndex the beginning index, inclusive.
1308 @return the specified substring.
1309 @since LibreOffice 4.1
1311 OUStringBuffer
copy( sal_Int32 beginIndex
) const SAL_THROW(())
1313 assert(beginIndex
>= 0 && beginIndex
<= getLength());
1314 return copy( beginIndex
, getLength() - beginIndex
);
1318 Returns a new string buffer that is a substring of this string.
1320 The substring begins at the specified beginIndex and contains count
1321 characters. If either beginIndex or count are negative,
1322 or beginIndex + count are greater than the length of this string
1323 then behaviour is undefined.
1325 @param beginIndex the beginning index, inclusive.
1326 @param count the number of characters.
1327 @return the specified substring.
1328 @since LibreOffice 4.1
1330 OUStringBuffer
copy( sal_Int32 beginIndex
, sal_Int32 count
) const SAL_THROW(())
1332 assert(beginIndex
>= 0 && beginIndex
<= getLength());
1333 assert(count
>= 0 && count
<= getLength() - beginIndex
);
1334 rtl_uString
*pNew
= 0;
1335 rtl_uStringbuffer_newFromStr_WithLength( &pNew
, getStr() + beginIndex
, count
);
1336 return OUStringBuffer( pNew
, count
+ 16 );
1339 #ifdef LIBO_INTERNAL_ONLY
1340 // This is to complement the RTL_FAST_STRING operator+, which allows any combination of valid operands,
1341 // even two buffers. It's intentional it returns OUString, just like the operator+ would in the fast variant.
1342 #ifndef RTL_FAST_STRING
1345 @since LibreOffice 4.1
1347 friend OUString
operator+( const OUStringBuffer
& str1
, const OUStringBuffer
& str2
) SAL_THROW(())
1349 return OUString( str1
.pData
).concat( str2
.pData
);
1355 OUStringBuffer( rtl_uString
* value
, const sal_Int32 capacity
)
1358 nCapacity
= capacity
;
1362 A pointer to the data structur which contains the data.
1364 rtl_uString
* pData
;
1367 The len of the pData->buffer.
1369 sal_Int32 nCapacity
;
1372 #ifdef RTL_FAST_STRING
1377 struct ToStringHelper
< OUStringBuffer
>
1379 static int length( const OUStringBuffer
& s
) { return s
.getLength(); }
1380 static sal_Unicode
* addData( sal_Unicode
* buffer
, const OUStringBuffer
& s
) { return addDataHelper( buffer
, s
.getStr(), s
.getLength()); }
1381 static const bool allowOStringConcat
= false;
1382 static const bool allowOUStringConcat
= true;
1388 #ifdef RTL_STRING_UNITTEST
1391 typedef rtlunittest::OUStringBuffer OUStringBuffer
;
1396 using ::rtl::OUStringBuffer
;
1399 #endif // INCLUDED_RTL_USTRBUF_HXX
1401 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */