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 <rtl/ustrbuf.h>
29 #include <rtl/ustring.hxx>
30 #include <rtl/stringutils.hxx>
31 #include <sal/types.h>
33 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
34 #include <rtl/stringconcat.hxx>
37 // The unittest uses slightly different code to help check that the proper
38 // calls are made. The class is put into a different namespace to make
39 // sure the compiler generates a different (if generating also non-inline)
40 // copy of the function and does not merge them together. The class
41 // is "brought" into the proper rtl namespace by a typedef below.
42 #ifdef RTL_STRING_UNITTEST
43 #define rtl rtlunittest
49 #ifdef RTL_STRING_UNITTEST
53 /** A string buffer implements a mutable sequence of characters.
55 class SAL_WARN_UNUSED OUStringBuffer
59 Constructs a string buffer with no characters in it and an
60 initial capacity of 16 characters.
66 rtl_uString_new_WithLength( &pData
, nCapacity
);
70 Allocates a new string buffer that contains the same sequence of
71 characters as the string buffer argument.
73 @param value a <code>OUStringBuffer</code>.
75 OUStringBuffer( const OUStringBuffer
& value
)
77 , nCapacity( value
.nCapacity
)
79 rtl_uStringbuffer_newFromStringBuffer( &pData
, value
.nCapacity
, value
.pData
);
83 Constructs a string buffer with no characters in it and an
84 initial capacity specified by the <code>length</code> argument.
86 @param length the initial capacity.
88 explicit OUStringBuffer(int length
)
92 rtl_uString_new_WithLength( &pData
, length
);
94 #if __cplusplus >= 201103L
95 explicit OUStringBuffer(unsigned int length
)
96 : OUStringBuffer(static_cast<int>(length
))
99 #if SAL_TYPES_SIZEOFLONG == 4
100 // additional overloads for sal_Int32 sal_uInt32
101 explicit OUStringBuffer(long length
)
102 : OUStringBuffer(static_cast<int>(length
))
105 explicit OUStringBuffer(unsigned long length
)
106 : OUStringBuffer(static_cast<int>(length
))
110 // avoid obvious bugs
111 explicit OUStringBuffer(char) = delete;
112 explicit OUStringBuffer(sal_Unicode
) = delete;
116 Constructs a string buffer so that it represents the same
117 sequence of characters as the string argument.
120 capacity of the string buffer is <code>16</code> plus the length
121 of the string argument.
123 @param value the initial contents of the buffer.
125 OUStringBuffer(const OUString
& value
)
127 , nCapacity( value
.getLength() + 16 )
129 rtl_uStringbuffer_newFromStr_WithLength( &pData
, value
.getStr(), value
.getLength() );
132 template< typename T
>
133 OUStringBuffer( T
& literal
, typename
libreoffice_internal::ConstCharArrayDetector
< T
, libreoffice_internal::Dummy
>::Type
= libreoffice_internal::Dummy() )
135 , nCapacity( libreoffice_internal::ConstCharArrayDetector
< T
, void >::size
- 1 + 16 )
137 assert( strlen( literal
) == libreoffice_internal::ConstCharArrayDetector
< T
>::size
- 1 );
138 rtl_uString_newFromLiteral( &pData
, literal
, libreoffice_internal::ConstCharArrayDetector
< T
, void >::size
- 1, 16 );
139 #ifdef RTL_STRING_UNITTEST
140 rtl_string_unittest_const_literal
= true;
144 #ifdef RTL_STRING_UNITTEST
146 * Only used by unittests to detect incorrect conversions.
149 template< typename T
>
150 OUStringBuffer( T
&, typename
libreoffice_internal::ExceptConstCharArrayDetector
< T
>::Type
= libreoffice_internal::Dummy() )
154 rtl_uString_newFromLiteral( &pData
, "!!br0ken!!", 10, 0 ); // set to garbage
155 rtl_string_unittest_invalid_conversion
= true;
158 * Only used by unittests to detect incorrect conversions.
161 template< typename T
>
162 OUStringBuffer( const T
&, typename
libreoffice_internal::ExceptCharArrayDetector
< T
>::Type
= libreoffice_internal::Dummy() )
166 rtl_uString_newFromLiteral( &pData
, "!!br0ken!!", 10, 0 ); // set to garbage
167 rtl_string_unittest_invalid_conversion
= true;
171 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
176 template< typename T1
, typename T2
>
177 OUStringBuffer( const OUStringConcat
< T1
, T2
>& c
)
179 const sal_Int32 l
= c
.length();
181 pData
= rtl_uString_alloc( nCapacity
);
182 sal_Unicode
* end
= c
.addData( pData
->buffer
);
184 pData
->length
= end
- pData
->buffer
;
185 // TODO realloc in case pData->>length is noticeably smaller than l ?
188 /** Assign to this a copy of value.
190 OUStringBuffer
& operator = ( const OUStringBuffer
& value
)
194 rtl_uStringbuffer_newFromStringBuffer(&pData
,
197 nCapacity
= value
.nCapacity
;
203 Release the string data.
207 rtl_uString_release( pData
);
211 Fill the string data in the new string and clear the buffer.
213 This method is more efficient than the constructor of the string. It does
216 @return the string previously contained in the buffer.
218 OUString
makeStringAndClear()
221 rtl_uStringBuffer_makeStringAndClear( &pData
, &nCapacity
),
226 Returns the length (character count) of this string buffer.
228 @return the number of characters in this string buffer.
230 sal_Int32
getLength() const
232 return pData
->length
;
236 Checks if a string buffer is empty.
238 @return true if the string buffer is empty;
241 @since LibreOffice 4.1
245 return pData
->length
== 0;
249 Returns the current capacity of the String buffer.
252 is the amount of storage available for newly inserted
253 characters. The real buffer size is 2 bytes longer, because
254 all strings are 0 terminated.
256 @return the current capacity of this string buffer.
258 sal_Int32
getCapacity() const
264 Ensures that the capacity of the buffer is at least equal to the
267 The new capacity will be at least as large as the maximum of the current
268 length (so that no contents of the buffer is destroyed) and the given
269 minimumCapacity. If the given minimumCapacity is negative, nothing is
272 @param minimumCapacity the minimum desired capacity.
274 void ensureCapacity(sal_Int32 minimumCapacity
)
276 rtl_uStringbuffer_ensureCapacity( &pData
, &nCapacity
, minimumCapacity
);
280 Sets the length of this String buffer.
282 If the <code>newLength</code> argument is less than the current
283 length of the string buffer, the string buffer is truncated to
284 contain exactly the number of characters given by the
285 <code>newLength</code> argument.
287 If the <code>newLength</code> argument is greater than or equal
288 to the current length, sufficient null characters
289 (<code>'\u0000'</code>) are appended to the string buffer so that
290 length becomes the <code>newLength</code> argument.
292 The <code>newLength</code> argument must be greater than or equal
295 @param newLength the new length of the buffer.
297 void setLength(sal_Int32 newLength
)
299 assert(newLength
>= 0);
300 // Avoid modifications if pData points to const empty string:
301 if( newLength
!= pData
->length
)
303 if( newLength
> nCapacity
)
304 rtl_uStringbuffer_ensureCapacity(&pData
, &nCapacity
, newLength
);
306 pData
->buffer
[newLength
] = 0;
307 pData
->length
= newLength
;
312 Returns the character at a specific index in this string buffer.
314 The first character of a string buffer is at index
315 <code>0</code>, the next at index <code>1</code>, and so on, for
318 The index argument must be greater than or equal to
319 <code>0</code>, and less than the length of this string buffer.
321 @param index the index of the desired character.
322 @return the character at the specified index of this string buffer.
324 SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
325 sal_Unicode
charAt( sal_Int32 index
) const
327 assert(index
>= 0 && index
< pData
->length
);
328 return pData
->buffer
[ index
];
332 The character at the specified index of this string buffer is set
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 character to modify.
339 @param ch the new character.
341 SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
342 OUStringBuffer
& setCharAt(sal_Int32 index
, sal_Unicode ch
)
344 assert(index
>= 0 && index
< pData
->length
);
345 pData
->buffer
[ index
] = ch
;
350 Return a null terminated unicode character array.
352 const sal_Unicode
* getStr() const { return pData
->buffer
; }
355 Access to individual characters.
357 @param index must be non-negative and less than length.
359 @return a reference to the character at the given index.
361 @since LibreOffice 3.5
363 sal_Unicode
& operator [](sal_Int32 index
)
365 assert(index
>= 0 && index
< pData
->length
);
366 return pData
->buffer
[index
];
370 Access to individual characters.
372 @param index must be non-negative and less than length.
374 @return a reference to the character at the given index.
376 @since LibreOffice 4.2
378 const sal_Unicode
& operator [](sal_Int32 index
) const
380 assert(index
>= 0 && index
< pData
->length
);
381 return pData
->buffer
[index
];
385 Return a OUString instance reflecting the current content
386 of this OUStringBuffer.
388 const OUString
toString() const
390 return OUString(pData
->buffer
, pData
->length
);
394 Appends the string to this string buffer.
396 The characters of the <code>OUString</code> argument are appended, in
397 order, to the contents of this string buffer, increasing the
398 length of this string buffer by the length of the argument.
401 @return this string buffer.
403 OUStringBuffer
& append(const OUString
&str
)
405 return append( str
.getStr(), str
.getLength() );
409 Appends the content of a stringbuffer to this string buffer.
411 The characters of the <code>OUStringBuffer</code> argument are appended, in
412 order, to the contents of this string buffer, increasing the
413 length of this string buffer by the length of the argument.
416 @return this string buffer.
418 @since LibreOffice 4.0
420 OUStringBuffer
& append(const OUStringBuffer
&str
)
424 append( str
.getStr(), str
.getLength() );
430 Appends the string representation of the <code>char</code> array
431 argument to this string buffer.
433 The characters of the array argument are appended, in order, to
434 the contents of this string buffer. The length of this string
435 buffer increases by the length of the argument.
437 @param str the characters to be appended.
438 @return this string buffer.
440 OUStringBuffer
& append( const sal_Unicode
* str
)
442 return append( str
, rtl_ustr_getLength( str
) );
446 Appends the string representation of the <code>char</code> array
447 argument to this string buffer.
449 Characters of the character array <code>str</code> are appended,
450 in order, to the contents of this string buffer. The length of this
451 string buffer increases by the value of <code>len</code>.
453 @param str the characters to be appended; must be non-null, and must
454 point to at least len characters
455 @param len the number of characters to append; must be non-negative
456 @return this string buffer.
458 OUStringBuffer
& append( const sal_Unicode
* str
, sal_Int32 len
)
460 assert( len
== 0 || str
!= 0 ); // cannot assert that in rtl_uStringbuffer_insert
461 rtl_uStringbuffer_insert( &pData
, &nCapacity
, getLength(), str
, len
);
467 This function accepts an ASCII string literal as its argument.
468 @since LibreOffice 3.6
470 template< typename T
>
471 typename
libreoffice_internal::ConstCharArrayDetector
< T
, OUStringBuffer
& >::Type
append( T
& literal
)
473 assert( strlen( literal
) == libreoffice_internal::ConstCharArrayDetector
< T
>::size
- 1 );
474 rtl_uStringbuffer_insert_ascii( &pData
, &nCapacity
, getLength(), literal
,
475 libreoffice_internal::ConstCharArrayDetector
< T
, void >::size
- 1 );
479 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
484 template< typename T1
, typename T2
>
485 OUStringBuffer
& append( const OUStringConcat
< T1
, T2
>& c
)
487 const int l
= c
.length();
490 rtl_uStringbuffer_ensureCapacity( &pData
, &nCapacity
, pData
->length
+ l
);
491 sal_Unicode
* end
= c
.addData( pData
->buffer
+ pData
->length
);
493 pData
->length
= end
- pData
->buffer
;
499 Appends a 8-Bit ASCII character string to this string buffer.
501 Since this method is optimized for performance. the ASCII
502 character values are not converted in any way. The caller
503 has to make sure that all ASCII characters are in the
504 allowed range between 0 and 127. The ASCII string must be
507 The characters of the array argument are appended, in order, to
508 the contents of this string buffer. The length of this string
509 buffer increases by the length of the argument.
511 @param str the 8-Bit ASCII characters to be appended.
512 @return this string buffer.
514 OUStringBuffer
& appendAscii( const sal_Char
* str
)
516 return appendAscii( str
, rtl_str_getLength( str
) );
520 Appends a 8-Bit ASCII character string to this string buffer.
522 Since this method is optimized for performance. the ASCII
523 character values are not converted in any way. The caller
524 has to make sure that all ASCII characters are in the
525 allowed range between 0 and 127. The ASCII string must be
528 Characters of the character array <code>str</code> are appended,
529 in order, to the contents of this string buffer. The length of this
530 string buffer increases by the value of <code>len</code>.
532 @param str the 8-Bit ASCII characters to be appended; must be non-null,
533 and must point to at least len characters
534 @param len the number of characters to append; must be non-negative
535 @return this string buffer.
537 OUStringBuffer
& appendAscii( const sal_Char
* str
, sal_Int32 len
)
539 rtl_uStringbuffer_insert_ascii( &pData
, &nCapacity
, getLength(), str
, len
);
544 Appends the string representation of the <code>bool</code>
545 argument to the string buffer.
547 The argument is converted to a string as if by the method
548 <code>String.valueOf</code>, and the characters of that
549 string are then appended to this string buffer.
551 @param b a <code>bool</code>.
552 @return this string buffer.
554 @since LibreOffice 4.1
556 OUStringBuffer
& append(bool b
)
558 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFBOOLEAN
];
559 return append( sz
, rtl_ustr_valueOfBoolean( sz
, b
) );
563 // Pointer can be automatically converted to bool, which is unwanted here.
564 // Explicitly delete all pointer append() overloads to prevent this
565 // (except for char* and sal_Unicode* overloads, which are handled elsewhere).
566 template< typename T
>
567 typename
libreoffice_internal::Enable
< void,
568 !libreoffice_internal::CharPtrDetector
< T
* >::ok
&& !libreoffice_internal::SalUnicodePtrDetector
< T
* >::ok
>::Type
569 append( T
* ) SAL_DELETED_FUNCTION
;
572 // This overload is needed because OUString has a ctor from rtl_uString*, but
573 // the bool overload above would be preferred to the conversion.
577 OUStringBuffer
& append(rtl_uString
* str
)
579 return append( OUString( str
));
583 Appends the string representation of the <code>sal_Bool</code>
584 argument to the string buffer.
586 The argument is converted to a string as if by the method
587 <code>String.valueOf</code>, and the characters of that
588 string are then appended to this string buffer.
590 @param b a <code>sal_Bool</code>.
591 @return this string buffer.
593 OUStringBuffer
& append(sal_Bool b
)
595 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFBOOLEAN
];
596 return append( sz
, rtl_ustr_valueOfBoolean( sz
, b
) );
600 Appends the string representation of the ASCII <code>char</code>
601 argument to this string buffer.
603 The argument is appended to the contents of this string buffer.
604 The length of this string buffer increases by <code>1</code>.
606 @param c an ASCII <code>char</code>.
607 @return this string buffer.
609 @since LibreOffice 3.5
611 OUStringBuffer
& append(char c
)
613 assert(static_cast< unsigned char >(c
) <= 0x7F);
614 return append(sal_Unicode(c
));
618 Appends the string representation of the <code>char</code>
619 argument to this string buffer.
621 The argument is appended to the contents of this string buffer.
622 The length of this string buffer increases by <code>1</code>.
624 @param c a <code>char</code>.
625 @return this string buffer.
627 OUStringBuffer
& append(sal_Unicode c
)
629 return append( &c
, 1 );
633 Appends the string representation of the <code>sal_Int32</code>
634 argument to this string buffer.
636 The argument is converted to a string as if by the method
637 <code>String.valueOf</code>, and the characters of that
638 string are then appended to this string buffer.
640 @param i an <code>sal_Int32</code>.
641 @param radix the radix
642 @return this string buffer.
644 OUStringBuffer
& append(sal_Int32 i
, sal_Int16 radix
= 10 )
646 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFINT32
];
647 return append( sz
, rtl_ustr_valueOfInt32( sz
, i
, radix
) );
651 Appends the string representation of the <code>long</code>
652 argument to this string buffer.
654 The argument is converted to a string as if by the method
655 <code>String.valueOf</code>, and the characters of that
656 string are then appended to this string buffer.
658 @param l a <code>long</code>.
659 @param radix the radix
660 @return this string buffer.
662 OUStringBuffer
& append(sal_Int64 l
, sal_Int16 radix
= 10 )
664 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFINT64
];
665 return append( sz
, rtl_ustr_valueOfInt64( sz
, l
, radix
) );
669 Appends the string representation of the <code>float</code>
670 argument to this string buffer.
672 The argument is converted to a string as if by the method
673 <code>String.valueOf</code>, and the characters of that
674 string are then appended to this string buffer.
676 @param f a <code>float</code>.
677 @return this string buffer.
679 OUStringBuffer
& append(float f
)
681 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFFLOAT
];
682 return append( sz
, rtl_ustr_valueOfFloat( sz
, f
) );
686 Appends the string representation of the <code>double</code>
687 argument to this string buffer.
689 The argument is converted to a string as if by the method
690 <code>String.valueOf</code>, and the characters of that
691 string are then appended to this string buffer.
693 @param d a <code>double</code>.
694 @return this string buffer.
696 OUStringBuffer
& append(double d
)
698 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFDOUBLE
];
699 return append( sz
, rtl_ustr_valueOfDouble( sz
, d
) );
703 Appends a single UTF-32 character to this string buffer.
705 <p>The single UTF-32 character will be represented within the string
706 buffer as either one or two UTF-16 code units.</p>
708 @param c a well-formed UTF-32 code unit (that is, a value in the range
709 <code>0</code>–<code>0x10FFFF</code>, but excluding
710 <code>0xD800</code>–<code>0xDFFF</code>)
715 OUStringBuffer
& appendUtf32(sal_uInt32 c
) {
716 return insertUtf32(getLength(), c
);
720 Unsafe way to make space for a fixed amount of characters to be appended
721 into this OUStringBuffer.
723 A call to this function must immediately be followed by code that
724 completely fills the uninitialized block pointed to by the return value.
726 @param length the length of the uninitialized block of sal_Unicode
727 entities; must be non-negative
729 @return a pointer to the start of the uninitialized block; only valid
730 until this OUStringBuffer's capacity changes
732 @since LibreOffice 4.4
734 sal_Unicode
* appendUninitialized(sal_Int32 length
) {
735 sal_Int32 n
= getLength();
736 rtl_uStringbuffer_insert(&pData
, &nCapacity
, n
, 0, length
);
737 return pData
->buffer
+ n
;
741 Inserts the string into this string buffer.
743 The characters of the <code>String</code> argument are inserted, in
744 order, into this string buffer at the indicated offset. The length
745 of this string buffer is increased by the length of the argument.
747 The offset argument must be greater than or equal to
748 <code>0</code>, and less than or equal to the length of this
751 @param offset the offset.
753 @return this string buffer.
755 OUStringBuffer
& insert(sal_Int32 offset
, const OUString
& str
)
757 return insert( offset
, str
.getStr(), str
.getLength() );
761 Inserts the string representation of the <code>char</code> array
762 argument into this string buffer.
764 The characters of the array argument are inserted into the
765 contents of this string buffer at the position indicated by
766 <code>offset</code>. The length of this string buffer increases by
767 the length of the argument.
769 The offset argument must be greater than or equal to
770 <code>0</code>, and less than or equal to the length of this
773 @param offset the offset.
774 @param str a character array.
775 @return this string buffer.
777 OUStringBuffer
& insert( sal_Int32 offset
, const sal_Unicode
* str
)
779 return insert( offset
, str
, rtl_ustr_getLength( str
) );
783 Inserts the string representation of the <code>char</code> array
784 argument into this string buffer.
786 The characters of the array argument are inserted into the
787 contents of this string buffer at the position indicated by
788 <code>offset</code>. The length of this string buffer increases by
789 the length of the argument.
791 The offset argument must be greater than or equal to
792 <code>0</code>, and less than or equal to the length of this
795 @param offset the offset.
796 @param str a character array.
797 @param len the number of characters to append.
798 @return this string buffer.
800 OUStringBuffer
& insert( sal_Int32 offset
, const sal_Unicode
* str
, sal_Int32 len
)
802 assert( len
== 0 || str
!= 0 ); // cannot assert that in rtl_uStringbuffer_insert
803 rtl_uStringbuffer_insert( &pData
, &nCapacity
, offset
, str
, len
);
809 This function accepts an ASCII string literal as its argument.
810 @since LibreOffice 3.6
812 template< typename T
>
813 typename
libreoffice_internal::ConstCharArrayDetector
< T
, OUStringBuffer
& >::Type
insert( sal_Int32 offset
, T
& literal
)
815 assert( strlen( literal
) == libreoffice_internal::ConstCharArrayDetector
< T
>::size
- 1 );
816 rtl_uStringbuffer_insert_ascii( &pData
, &nCapacity
, offset
, literal
,
817 libreoffice_internal::ConstCharArrayDetector
< T
, void >::size
- 1 );
822 Inserts the string representation of the <code>sal_Bool</code>
823 argument into this string buffer.
825 The second argument is converted to a string as if by the method
826 <code>String.valueOf</code>, and the characters of that
827 string are then inserted into this string buffer at the indicated
830 The offset argument must be greater than or equal to
831 <code>0</code>, and less than or equal to the length of this
834 @param offset the offset.
835 @param b a <code>sal_Bool</code>.
836 @return this string buffer.
838 OUStringBuffer
& insert(sal_Int32 offset
, sal_Bool b
)
840 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFBOOLEAN
];
841 return insert( offset
, sz
, rtl_ustr_valueOfBoolean( sz
, b
) );
845 Inserts the string representation of the <code>bool</code>
846 argument into this string buffer.
848 The second argument is converted to a string as if by the method
849 <code>OUString::boolean</code>, and the characters of that
850 string are then inserted into this string buffer at the indicated
853 The offset argument must be greater than or equal to
854 <code>0</code>, and less than or equal to the length of this
857 @param offset the offset.
858 @param b a <code>bool</code>.
859 @return this string buffer.
861 @since LibreOffice 4.3
863 OUStringBuffer
& insert(sal_Int32 offset
, bool b
)
865 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFBOOLEAN
];
866 return insert( offset
, sz
, rtl_ustr_valueOfBoolean( sz
, b
) );
870 Inserts the string representation of the <code>char</code>
871 argument into this string buffer.
873 The second argument is inserted into the contents of this string
874 buffer at the position indicated by <code>offset</code>. The length
875 of this string buffer increases by one.
877 The offset argument must be greater than or equal to
878 <code>0</code>, and less than or equal to the length of this
881 @param offset the offset.
882 @param c a <code>char</code>.
883 @return this string buffer.
885 @since LibreOffice 3.6
887 OUStringBuffer
& insert(sal_Int32 offset
, char c
)
890 return insert( offset
, &u
, 1 );
894 Inserts the string representation of the <code>char</code>
895 argument into this string buffer.
897 The second argument is inserted into the contents of this string
898 buffer at the position indicated by <code>offset</code>. The length
899 of this string buffer increases by one.
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.
906 @param c a <code>char</code>.
907 @return this string buffer.
909 OUStringBuffer
& insert(sal_Int32 offset
, sal_Unicode c
)
911 return insert( offset
, &c
, 1 );
915 Inserts the string representation of the second <code>sal_Int32</code>
916 argument into this string buffer.
918 The second argument is converted to a string as if by the method
919 <code>String.valueOf</code>, and the characters of that
920 string are then inserted into this string buffer at the indicated
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 i an <code>sal_Int32</code>.
929 @param radix the radix.
930 @return this string buffer.
931 @exception StringIndexOutOfBoundsException if the offset is invalid.
933 OUStringBuffer
& insert(sal_Int32 offset
, sal_Int32 i
, sal_Int16 radix
= 10 )
935 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFINT32
];
936 return insert( offset
, sz
, rtl_ustr_valueOfInt32( sz
, i
, radix
) );
940 Inserts the string representation of the <code>long</code>
941 argument into this string buffer.
943 The second argument is converted to a string as if by the method
944 <code>String.valueOf</code>, and the characters of that
945 string are then inserted into this string buffer at the indicated
948 The offset argument must be greater than or equal to
949 <code>0</code>, and less than or equal to the length of this
952 @param offset the offset.
953 @param l a <code>long</code>.
954 @param radix the radix.
955 @return this string buffer.
956 @exception StringIndexOutOfBoundsException if the offset is invalid.
958 OUStringBuffer
& insert(sal_Int32 offset
, sal_Int64 l
, sal_Int16 radix
= 10 )
960 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFINT64
];
961 return insert( offset
, sz
, rtl_ustr_valueOfInt64( sz
, l
, radix
) );
965 Inserts the string representation of the <code>float</code>
966 argument into this string buffer.
968 The second argument is converted to a string as if by the method
969 <code>String.valueOf</code>, and the characters of that
970 string are then inserted into this string buffer at the indicated
973 The offset argument must be greater than or equal to
974 <code>0</code>, and less than or equal to the length of this
977 @param offset the offset.
978 @param f a <code>float</code>.
979 @return this string buffer.
980 @exception StringIndexOutOfBoundsException if the offset is invalid.
982 OUStringBuffer
insert(sal_Int32 offset
, float f
)
984 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFFLOAT
];
985 return insert( offset
, sz
, rtl_ustr_valueOfFloat( sz
, f
) );
989 Inserts the string representation of the <code>double</code>
990 argument into this string buffer.
992 The second argument is converted to a string as if by the method
993 <code>String.valueOf</code>, and the characters of that
994 string are then inserted into this string buffer at the indicated
997 The offset argument must be greater than or equal to
998 <code>0</code>, and less than or equal to the length of this
1001 @param offset the offset.
1002 @param d a <code>double</code>.
1003 @return this string buffer.
1004 @exception StringIndexOutOfBoundsException if the offset is invalid.
1006 OUStringBuffer
& insert(sal_Int32 offset
, double d
)
1008 sal_Unicode sz
[RTL_USTR_MAX_VALUEOFDOUBLE
];
1009 return insert( offset
, sz
, rtl_ustr_valueOfDouble( sz
, d
) );
1013 Inserts a single UTF-32 character into this string buffer.
1015 <p>The single UTF-32 character will be represented within the string
1016 buffer as either one or two UTF-16 code units.</p>
1018 @param offset the offset into this string buffer (from zero to the length
1019 of this string buffer, inclusive)
1021 @param c a well-formed UTF-32 code unit (that is, a value in the range
1022 <code>0</code>–<code>0x10FFFF</code>, but excluding
1023 <code>0xD800</code>–<code>0xDFFF</code>)
1025 @return this string buffer
1027 OUStringBuffer
& insertUtf32(sal_Int32 offset
, sal_uInt32 c
) {
1028 rtl_uStringbuffer_insertUtf32(&pData
, &nCapacity
, offset
, c
);
1033 Removes the characters in a substring of this sequence.
1035 The substring begins at the specified <code>start</code> and
1036 is <code>len</code> characters long.
1038 start must be >= 0 && <= This->length
1040 @param start The beginning index, inclusive
1041 @param len The substring length
1042 @return this string buffer.
1044 OUStringBuffer
& remove( sal_Int32 start
, sal_Int32 len
)
1046 rtl_uStringbuffer_remove( &pData
, start
, len
);
1051 Removes the tail of a string buffer start at the indicate position
1053 start must be >= 0 && <= This->length
1055 @param start The beginning index, inclusive. default to 0
1056 @return this string buffer.
1058 @since LibreOffice 4.0
1060 OUStringBuffer
& truncate( sal_Int32 start
= 0 )
1062 rtl_uStringbuffer_remove( &pData
, start
, getLength() - start
);
1067 Replace all occurrences of
1068 oldChar in this string buffer with newChar.
1070 @since LibreOffice 4.0
1072 @param oldChar the old character.
1073 @param newChar the new character.
1074 @return this string buffer
1076 OUStringBuffer
& replace( sal_Unicode oldChar
, sal_Unicode newChar
)
1078 sal_Int32 index
= 0;
1079 while((index
= indexOf(oldChar
, index
)) >= 0)
1081 pData
->buffer
[ index
] = newChar
;
1086 /** Allows access to the internal data of this OUStringBuffer, for effective
1089 This method should be used with care. After you have called this
1090 method, you may use the returned pInternalData or pInternalCapacity only
1091 as long as you make no other method call on this OUStringBuffer.
1093 @param pInternalData
1094 This output parameter receives a pointer to the internal data
1095 (rtl_uString pointer). pInternalData itself must not be null.
1097 @param pInternalCapacity
1098 This output parameter receives a pointer to the internal capacity.
1099 pInternalCapacity itself must not be null.
1101 inline void accessInternals(rtl_uString
*** pInternalData
,
1102 sal_Int32
** pInternalCapacity
)
1104 *pInternalData
= &pData
;
1105 *pInternalCapacity
= &nCapacity
;
1110 Returns the index within this string of the first occurrence of the
1111 specified character, starting the search at the specified index.
1113 @since LibreOffice 4.0
1115 @param ch character to be located.
1116 @param fromIndex the index to start the search from.
1117 The index must be greater or equal than 0
1118 and less or equal as the string length.
1119 @return the index of the first occurrence of the character in the
1120 character sequence represented by this string that is
1121 greater than or equal to fromIndex, or
1122 -1 if the character does not occur.
1124 sal_Int32
indexOf( sal_Unicode ch
, sal_Int32 fromIndex
= 0 ) const
1126 assert( fromIndex
>= 0 && fromIndex
<= pData
->length
);
1127 sal_Int32 ret
= rtl_ustr_indexOfChar_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
, ch
);
1128 return (ret
< 0 ? ret
: ret
+fromIndex
);
1132 Returns the index within this string of the last occurrence of the
1133 specified character, searching backward starting at the end.
1135 @since LibreOffice 4.0
1137 @param ch character to be located.
1138 @return the index of the last occurrence of the character in the
1139 character sequence represented by this string, or
1140 -1 if the character does not occur.
1142 sal_Int32
lastIndexOf( sal_Unicode ch
) const
1144 return rtl_ustr_lastIndexOfChar_WithLength( pData
->buffer
, pData
->length
, ch
);
1148 Returns the index within this string of the last occurrence of the
1149 specified character, searching backward starting before the specified
1152 @since LibreOffice 4.0
1154 @param ch character to be located.
1155 @param fromIndex the index before which to start the search.
1156 @return the index of the last occurrence of the character in the
1157 character sequence represented by this string that
1158 is less than fromIndex, or -1
1159 if the character does not occur before that point.
1161 sal_Int32
lastIndexOf( sal_Unicode ch
, sal_Int32 fromIndex
) const
1163 assert( fromIndex
>= 0 && fromIndex
<= pData
->length
);
1164 return rtl_ustr_lastIndexOfChar_WithLength( pData
->buffer
, fromIndex
, ch
);
1168 Returns the index within this string of the first occurrence of the
1169 specified substring, starting at the specified index.
1171 If str doesn't include any character, always -1 is
1172 returned. This is also the case, if both strings are empty.
1174 @since LibreOffice 4.0
1176 @param str the substring to search for.
1177 @param fromIndex the index to start the search from.
1178 @return If the string argument occurs one or more times as a substring
1179 within this string at the starting index, then the index
1180 of the first character of the first such substring is
1181 returned. If it does not occur as a substring starting
1182 at fromIndex or beyond, -1 is returned.
1184 sal_Int32
indexOf( const OUString
& str
, sal_Int32 fromIndex
= 0 ) const
1186 assert( fromIndex
>= 0 && fromIndex
<= pData
->length
);
1187 sal_Int32 ret
= rtl_ustr_indexOfStr_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
1188 str
.pData
->buffer
, str
.pData
->length
);
1189 return (ret
< 0 ? ret
: ret
+fromIndex
);
1194 This function accepts an ASCII string literal as its argument.
1196 @since LibreOffice 4.0
1198 template< typename T
>
1199 typename
libreoffice_internal::ConstCharArrayDetector
< T
, sal_Int32
>::Type
indexOf( T
& literal
, sal_Int32 fromIndex
= 0 ) const
1201 assert( strlen( literal
) == libreoffice_internal::ConstCharArrayDetector
< T
>::size
- 1 );
1202 sal_Int32 ret
= rtl_ustr_indexOfAscii_WithLength(
1203 pData
->buffer
+ fromIndex
, pData
->length
- fromIndex
, literal
,
1204 libreoffice_internal::ConstCharArrayDetector
< T
, void >::size
- 1);
1205 return ret
< 0 ? ret
: ret
+ fromIndex
;
1209 Returns the index within this string of the last occurrence of
1210 the specified substring, searching backward starting at the end.
1212 The returned index indicates the starting index of the substring
1214 If str doesn't include any character, always -1 is
1215 returned. This is also the case, if both strings are empty.
1217 @since LibreOffice 4.0
1219 @param str the substring to search for.
1220 @return If the string argument occurs one or more times as a substring
1221 within this string, then the index of the first character of
1222 the last such substring is returned. If it does not occur as
1223 a substring, -1 is returned.
1225 sal_Int32
lastIndexOf( const OUString
& str
) const
1227 return rtl_ustr_lastIndexOfStr_WithLength( pData
->buffer
, pData
->length
,
1228 str
.pData
->buffer
, str
.pData
->length
);
1232 Returns the index within this string of the last occurrence of
1233 the specified substring, searching backward starting before the specified
1236 The returned index indicates the starting index of the substring
1238 If str doesn't include any character, always -1 is
1239 returned. This is also the case, if both strings are empty.
1241 @since LibreOffice 4.0
1243 @param str the substring to search for.
1244 @param fromIndex the index before which to start the search.
1245 @return If the string argument occurs one or more times as a substring
1246 within this string before the starting index, then the index
1247 of the first character of the last such substring is
1248 returned. Otherwise, -1 is returned.
1250 sal_Int32
lastIndexOf( const OUString
& str
, sal_Int32 fromIndex
) const
1252 assert( fromIndex
>= 0 && fromIndex
<= pData
->length
);
1253 return rtl_ustr_lastIndexOfStr_WithLength( pData
->buffer
, fromIndex
,
1254 str
.pData
->buffer
, str
.pData
->length
);
1259 This function accepts an ASCII string literal as its argument.
1260 @since LibreOffice 4.0
1262 template< typename T
>
1263 typename
libreoffice_internal::ConstCharArrayDetector
< T
, sal_Int32
>::Type
lastIndexOf( T
& literal
) const
1265 assert( strlen( literal
) == libreoffice_internal::ConstCharArrayDetector
< T
>::size
- 1 );
1266 return rtl_ustr_lastIndexOfAscii_WithLength(
1267 pData
->buffer
, pData
->length
, literal
, libreoffice_internal::ConstCharArrayDetector
< T
, void >::size
- 1);
1271 Strip the given character from the start of the buffer.
1273 @since LibreOffice 4.0
1275 @param c the character to strip
1276 @return The number of characters stripped
1279 sal_Int32
stripStart(sal_Unicode c
= (sal_Unicode
)' ')
1282 for(index
= 0; index
< getLength() ; index
++)
1284 if(pData
->buffer
[ index
] != c
)
1297 Strip the given character from the end of the buffer.
1299 @since LibreOffice 4.0
1301 @param c the character to strip
1302 @return The number of characters stripped
1305 sal_Int32
stripEnd(sal_Unicode c
= (sal_Unicode
)' ')
1307 sal_Int32 result
= getLength();
1309 for(index
= getLength(); index
> 0 ; index
--)
1311 if(pData
->buffer
[ index
- 1 ] != c
)
1316 if(index
< getLength())
1320 return result
- getLength();
1323 Strip the given character from the both end of the buffer.
1325 @since LibreOffice 4.0
1327 @param c the character to strip
1328 @return The number of characters stripped
1331 sal_Int32
strip(sal_Unicode c
= (sal_Unicode
)' ')
1333 return stripStart(c
) + stripEnd(c
);
1336 Returns a new string buffer that is a substring of this string.
1338 The substring begins at the specified beginIndex. If
1339 beginIndex is negative or be greater than the length of
1340 this string, behaviour is undefined.
1342 @param beginIndex the beginning index, inclusive.
1343 @return the specified substring.
1344 @since LibreOffice 4.1
1346 OUStringBuffer
copy( sal_Int32 beginIndex
) const
1348 return copy( beginIndex
, getLength() - beginIndex
);
1352 Returns a new string buffer that is a substring of this string.
1354 The substring begins at the specified beginIndex and contains count
1355 characters. If either beginIndex or count are negative,
1356 or beginIndex + count are greater than the length of this string
1357 then behaviour is undefined.
1359 @param beginIndex the beginning index, inclusive.
1360 @param count the number of characters.
1361 @return the specified substring.
1362 @since LibreOffice 4.1
1364 OUStringBuffer
copy( sal_Int32 beginIndex
, sal_Int32 count
) const
1366 assert(beginIndex
>= 0 && beginIndex
<= getLength());
1367 assert(count
>= 0 && count
<= getLength() - beginIndex
);
1368 rtl_uString
*pNew
= 0;
1369 rtl_uStringbuffer_newFromStr_WithLength( &pNew
, getStr() + beginIndex
, count
);
1370 return OUStringBuffer( pNew
, count
+ 16 );
1374 OUStringBuffer( rtl_uString
* value
, const sal_Int32 capacity
)
1377 nCapacity
= capacity
;
1381 A pointer to the data structure which contains the data.
1383 rtl_uString
* pData
;
1386 The len of the pData->buffer.
1388 sal_Int32 nCapacity
;
1391 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
1396 struct ToStringHelper
< OUStringBuffer
>
1398 static int length( const OUStringBuffer
& s
) { return s
.getLength(); }
1399 static sal_Unicode
* addData( sal_Unicode
* buffer
, const OUStringBuffer
& s
) { return addDataHelper( buffer
, s
.getStr(), s
.getLength()); }
1400 static const bool allowOStringConcat
= false;
1401 static const bool allowOUStringConcat
= true;
1407 #ifdef RTL_STRING_UNITTEST
1410 typedef rtlunittest::OUStringBuffer OUStringBuffer
;
1414 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
1415 using ::rtl::OUStringBuffer
;
1418 #endif // INCLUDED_RTL_USTRBUF_HXX
1420 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */