update credits
[LibreOffice.git] / include / rtl / ustrbuf.hxx
blobb648714aad970205969e84dfc0551887719442df
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 _RTL_USTRBUF_HXX_
21 #define _RTL_USTRBUF_HXX_
23 #include "sal/config.h"
25 #include <cassert>
26 #include <string.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>
36 #endif
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
45 #endif
47 namespace rtl
50 #ifdef RTL_STRING_UNITTEST
51 #undef rtl
52 #endif
54 /** A string buffer implements a mutable sequence of characters.
55 <p>
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.
59 <p>
60 String buffers are used by the compiler to implement the binary
61 string concatenation operator <code>+</code>. For example, the code:
62 <p><blockquote><pre>
63 x = "a" + 4 + "c"
64 </pre></blockquote><p>
65 is compiled to the equivalent of:
66 <p><blockquote><pre>
67 x = new OUStringBuffer().append("a").append(4).append("c")
68 .makeStringAndClear()
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
77 a specified point.
78 <p>
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>".
85 <p>
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
94 public:
95 /**
96 Constructs a string buffer with no characters in it and an
97 initial capacity of 16 characters.
99 OUStringBuffer()
100 : pData(NULL)
101 , nCapacity( 16 )
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 )
113 : pData(NULL)
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)
126 : pData(NULL)
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.
136 The initial
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)
143 : pData(NULL)
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() )
151 : pData(NULL)
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;
158 #endif
161 #ifdef RTL_STRING_UNITTEST
163 * Only used by unittests to detect incorrect conversions.
164 * @internal
166 template< typename T >
167 OUStringBuffer( T&, typename internal::ExceptConstCharArrayDetector< T >::Type = internal::Dummy() )
169 pData = 0;
170 nCapacity = 10;
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.
176 * @internal
178 template< typename T >
179 OUStringBuffer( const T&, typename internal::ExceptCharArrayDetector< T >::Type = internal::Dummy() )
181 pData = 0;
182 nCapacity = 10;
183 rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
184 rtl_string_unittest_invalid_conversion = true;
186 #endif
188 #ifdef RTL_FAST_STRING
190 @overload
191 @internal
193 template< typename T1, typename T2 >
194 OUStringBuffer( const OUStringConcat< T1, T2 >& c )
196 const sal_Int32 l = c.length();
197 nCapacity = l + 16;
198 pData = rtl_uString_alloc( nCapacity );
199 sal_Unicode* end = c.addData( pData->buffer );
200 *end = '\0';
201 pData->length = end - pData->buffer;
202 // TODO realloc in case pData->>length is noticeably smaller than l ?
204 #endif
205 /** Assign to this a copy of value.
207 OUStringBuffer& operator = ( const OUStringBuffer& value )
209 if (this != &value)
211 rtl_uStringbuffer_newFromStringBuffer(&pData,
212 value.nCapacity,
213 value.pData);
214 nCapacity = value.nCapacity;
216 return *this;
220 Release the string data.
222 ~OUStringBuffer()
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
231 not copy the buffer.
233 @return the string previously contained in the buffer.
235 OUString makeStringAndClear()
237 return OUString(
238 rtl_uStringBuffer_makeStringAndClear( &pData, &nCapacity ),
239 SAL_NO_ACQUIRE );
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;
256 false, otherwise.
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.
268 The capacity
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
277 return nCapacity;
281 Ensures that the capacity of the buffer is at least equal to the
282 specified minimum.
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
287 changed.
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>'&#92;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
310 to <code>0</code>.
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);
322 else
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
333 array indexing.
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
350 to <code>ch</code>.
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;
363 return *this;
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 Return a OUString instance reflecting the current content
388 of this OUStringBuffer.
390 const OUString toString() const
392 return OUString(pData->buffer, pData->length);
396 Appends the string to this string buffer.
398 The characters of the <code>OUString</code> argument are appended, in
399 order, to the contents of this string buffer, increasing the
400 length of this string buffer by the length of the argument.
402 @param str a string.
403 @return this string buffer.
405 OUStringBuffer & append(const OUString &str)
407 return append( str.getStr(), str.getLength() );
411 Appends the content of a stringbuffer to this string buffer.
413 The characters of the <code>OUStringBuffer</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.
417 @param str a string.
418 @return this string buffer.
420 @since LibreOffice 4.0
422 OUStringBuffer & append(const OUStringBuffer &str)
424 if(str.getLength() > 0)
426 append( str.getStr(), str.getLength() );
428 return *this;
432 Appends the string representation of the <code>char</code> array
433 argument to this string buffer.
435 The characters of the array argument are appended, in order, to
436 the contents of this string buffer. The length of this string
437 buffer increases by the length of the argument.
439 @param str the characters to be appended.
440 @return this string buffer.
442 OUStringBuffer & append( const sal_Unicode * str )
444 return append( str, rtl_ustr_getLength( str ) );
448 Appends the string representation of the <code>char</code> array
449 argument to this string buffer.
451 Characters of the character array <code>str</code> are appended,
452 in order, to the contents of this string buffer. The length of this
453 string buffer increases by the value of <code>len</code>.
455 @param str the characters to be appended; must be non-null, and must
456 point to at least len characters
457 @param len the number of characters to append; must be non-negative
458 @return this string buffer.
460 OUStringBuffer & append( const sal_Unicode * str, sal_Int32 len)
462 // insert behind the last character
463 rtl_uStringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
464 return *this;
468 @overload
469 This function accepts an ASCII string literal as its argument.
470 @since LibreOffice 3.6
472 template< typename T >
473 typename internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type append( T& literal )
475 assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
476 rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), literal,
477 internal::ConstCharArrayDetector< T, void >::size - 1 );
478 return *this;
481 #ifdef RTL_FAST_STRING
483 @overload
484 @internal
486 template< typename T1, typename T2 >
487 OUStringBuffer& append( const OUStringConcat< T1, T2 >& c )
489 const int l = c.length();
490 if( l == 0 )
491 return *this;
492 rtl_uStringbuffer_ensureCapacity( &pData, &nCapacity, pData->length + l );
493 sal_Unicode* end = c.addData( pData->buffer + pData->length );
494 *end = '\0';
495 pData->length = end - pData->buffer;
496 return *this;
498 #endif
501 Appends a 8-Bit ASCII character string to this string buffer.
503 Since this method is optimized for performance. the ASCII
504 character values are not converted in any way. The caller
505 has to make sure that all ASCII characters are in the
506 allowed range between 0 and 127. The ASCII string must be
507 NULL-terminated.
509 The characters of the array argument are appended, in order, to
510 the contents of this string buffer. The length of this string
511 buffer increases by the length of the argument.
513 @param str the 8-Bit ASCII characters to be appended.
514 @return this string buffer.
516 OUStringBuffer & appendAscii( const sal_Char * str )
518 return appendAscii( str, rtl_str_getLength( str ) );
522 Appends a 8-Bit ASCII character string to this string buffer.
524 Since this method is optimized for performance. the ASCII
525 character values are not converted in any way. The caller
526 has to make sure that all ASCII characters are in the
527 allowed range between 0 and 127. The ASCII string must be
528 NULL-terminated.
530 Characters of the character array <code>str</code> are appended,
531 in order, to the contents of this string buffer. The length of this
532 string buffer increases by the value of <code>len</code>.
534 @param str the 8-Bit ASCII characters to be appended; must be non-null,
535 and must point to at least len characters
536 @param len the number of characters to append; must be non-negative
537 @return this string buffer.
539 OUStringBuffer & appendAscii( const sal_Char * str, sal_Int32 len)
541 rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), str, len );
542 return *this;
546 Appends the string representation of the <code>bool</code>
547 argument to the string buffer.
549 The argument is converted to a string as if by the method
550 <code>String.valueOf</code>, and the characters of that
551 string are then appended to this string buffer.
553 @param b a <code>bool</code>.
554 @return this string buffer.
556 @since LibreOffice 4.1
558 OUStringBuffer & append(bool b)
560 sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
561 return append( sz, rtl_ustr_valueOfBoolean( sz, b ) );
564 // Pointer can be automatically converted to bool, which is unwanted here.
565 // Explicitly delete all pointer append() overloads to prevent this
566 // (except for char* and sal_Unicode* overloads, which are handled elsewhere).
567 template< typename T >
568 typename internal::Enable< void,
569 !internal::CharPtrDetector< T* >::ok && !internal::SalUnicodePtrDetector< T* >::ok >::Type
570 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 prefered to the conversion.
575 @internal
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>&ndash;<code>0x10FFFF</code>, but excluding
710 <code>0xD800</code>&ndash;<code>0xDFFF</code>)
712 @return
713 this string buffer
715 OUStringBuffer & appendUtf32(sal_uInt32 c) {
716 return insertUtf32(getLength(), c);
720 Inserts the string into this string buffer.
722 The characters of the <code>String</code> argument are inserted, in
723 order, into this string buffer at the indicated offset. The length
724 of this string buffer is increased by the length of the argument.
726 The offset argument must be greater than or equal to
727 <code>0</code>, and less than or equal to the length of this
728 string buffer.
730 @param offset the offset.
731 @param str a string.
732 @return this string buffer.
734 OUStringBuffer & insert(sal_Int32 offset, const OUString & str)
736 return insert( offset, str.getStr(), str.getLength() );
740 Inserts the string representation of the <code>char</code> array
741 argument into this string buffer.
743 The characters of the array argument are inserted into the
744 contents of this string buffer at the position indicated by
745 <code>offset</code>. The length of this string buffer increases by
746 the length of the argument.
748 The offset argument must be greater than or equal to
749 <code>0</code>, and less than or equal to the length of this
750 string buffer.
752 @param offset the offset.
753 @param str a character array.
754 @return this string buffer.
756 OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str )
758 return insert( offset, str, rtl_ustr_getLength( str ) );
762 Inserts the string representation of the <code>char</code> array
763 argument into this string buffer.
765 The characters of the array argument are inserted into the
766 contents of this string buffer at the position indicated by
767 <code>offset</code>. The length of this string buffer increases by
768 the length of the argument.
770 The offset argument must be greater than or equal to
771 <code>0</code>, and less than or equal to the length of this
772 string buffer.
774 @param offset the offset.
775 @param str a character array.
776 @param len the number of characters to append.
777 @return this string buffer.
779 OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str, sal_Int32 len)
781 // insert behind the last character
782 rtl_uStringbuffer_insert( &pData, &nCapacity, offset, str, len );
783 return *this;
787 @overload
788 This function accepts an ASCII string literal as its argument.
789 @since LibreOffice 3.6
791 template< typename T >
792 typename internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type insert( sal_Int32 offset, T& literal )
794 assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
795 rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, offset, literal,
796 internal::ConstCharArrayDetector< T, void >::size - 1 );
797 return *this;
801 Inserts the string representation of the <code>sal_Bool</code>
802 argument into this string buffer.
804 The second argument is converted to a string as if by the method
805 <code>String.valueOf</code>, and the characters of that
806 string are then inserted into this string buffer at the indicated
807 offset.
809 The offset argument must be greater than or equal to
810 <code>0</code>, and less than or equal to the length of this
811 string buffer.
813 @param offset the offset.
814 @param b a <code>sal_Bool</code>.
815 @return this string buffer.
817 OUStringBuffer & insert(sal_Int32 offset, sal_Bool b)
819 sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
820 return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
824 Inserts the string representation of the <code>char</code>
825 argument into this string buffer.
827 The second argument is inserted into the contents of this string
828 buffer at the position indicated by <code>offset</code>. The length
829 of this string buffer increases by one.
831 The offset argument must be greater than or equal to
832 <code>0</code>, and less than or equal to the length of this
833 string buffer.
835 @param offset the offset.
836 @param c a <code>char</code>.
837 @return this string buffer.
839 @since LibreOffice 3.6
841 OUStringBuffer & insert(sal_Int32 offset, char c)
843 sal_Unicode u = c;
844 return insert( offset, &u, 1 );
848 Inserts the string representation of the <code>char</code>
849 argument into this string buffer.
851 The second argument is inserted into the contents of this string
852 buffer at the position indicated by <code>offset</code>. The length
853 of this string buffer increases by one.
855 The offset argument must be greater than or equal to
856 <code>0</code>, and less than or equal to the length of this
857 string buffer.
859 @param offset the offset.
860 @param c a <code>char</code>.
861 @return this string buffer.
863 OUStringBuffer & insert(sal_Int32 offset, sal_Unicode c)
865 return insert( offset, &c, 1 );
869 Inserts the string representation of the second <code>sal_Int32</code>
870 argument into this string buffer.
872 The second argument is converted to a string as if by the method
873 <code>String.valueOf</code>, and the characters of that
874 string are then inserted into this string buffer at the indicated
875 offset.
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
879 string buffer.
881 @param offset the offset.
882 @param i an <code>sal_Int32</code>.
883 @param radix the radix.
884 @return this string buffer.
885 @exception StringIndexOutOfBoundsException if the offset is invalid.
887 OUStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
889 sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT32];
890 return insert( offset, sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
894 Inserts the string representation of the <code>long</code>
895 argument into this string buffer.
897 The second argument is converted to a string as if by the method
898 <code>String.valueOf</code>, and the characters of that
899 string are then inserted into this string buffer at the indicated
900 offset.
902 The offset argument must be greater than or equal to
903 <code>0</code>, and less than or equal to the length of this
904 string buffer.
906 @param offset the offset.
907 @param l a <code>long</code>.
908 @param radix the radix.
909 @return this string buffer.
910 @exception StringIndexOutOfBoundsException if the offset is invalid.
912 OUStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
914 sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT64];
915 return insert( offset, sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
919 Inserts the string representation of the <code>float</code>
920 argument into this string buffer.
922 The second argument is converted to a string as if by the method
923 <code>String.valueOf</code>, and the characters of that
924 string are then inserted into this string buffer at the indicated
925 offset.
927 The offset argument must be greater than or equal to
928 <code>0</code>, and less than or equal to the length of this
929 string buffer.
931 @param offset the offset.
932 @param f a <code>float</code>.
933 @return this string buffer.
934 @exception StringIndexOutOfBoundsException if the offset is invalid.
936 OUStringBuffer insert(sal_Int32 offset, float f)
938 sal_Unicode sz[RTL_USTR_MAX_VALUEOFFLOAT];
939 return insert( offset, sz, rtl_ustr_valueOfFloat( sz, f ) );
943 Inserts the string representation of the <code>double</code>
944 argument into this string buffer.
946 The second argument is converted to a string as if by the method
947 <code>String.valueOf</code>, and the characters of that
948 string are then inserted into this string buffer at the indicated
949 offset.
951 The offset argument must be greater than or equal to
952 <code>0</code>, and less than or equal to the length of this
953 string buffer.
955 @param offset the offset.
956 @param d a <code>double</code>.
957 @return this string buffer.
958 @exception StringIndexOutOfBoundsException if the offset is invalid.
960 OUStringBuffer & insert(sal_Int32 offset, double d)
962 sal_Unicode sz[RTL_USTR_MAX_VALUEOFDOUBLE];
963 return insert( offset, sz, rtl_ustr_valueOfDouble( sz, d ) );
967 Inserts a single UTF-32 character into this string buffer.
969 <p>The single UTF-32 character will be represented within the string
970 buffer as either one or two UTF-16 code units.</p>
972 @param offset the offset into this string buffer (from zero to the length
973 of this string buffer, inclusive)
975 @param c a well-formed UTF-32 code unit (that is, a value in the range
976 <code>0</code>&ndash;<code>0x10FFFF</code>, but excluding
977 <code>0xD800</code>&ndash;<code>0xDFFF</code>)
979 @return this string buffer
981 OUStringBuffer & insertUtf32(sal_Int32 offset, sal_uInt32 c) {
982 rtl_uStringbuffer_insertUtf32(&pData, &nCapacity, offset, c);
983 return *this;
987 Removes the characters in a substring of this sequence.
989 The substring begins at the specified <code>start</code> and
990 is <code>len</code> characters long.
992 start must be >= 0 && <= This->length
994 @param start The beginning index, inclusive
995 @param len The substring length
996 @return this string buffer.
998 OUStringBuffer & remove( sal_Int32 start, sal_Int32 len )
1000 rtl_uStringbuffer_remove( &pData, start, len );
1001 return *this;
1005 Removes the tail of a string buffer start at the indicate position
1007 start must be >= 0 && <= This->length
1009 @param start The beginning index, inclusive. default to 0
1010 @return this string buffer.
1012 @since LibreOffice 4.0
1014 OUStringBuffer & truncate( sal_Int32 start = 0 )
1016 rtl_uStringbuffer_remove( &pData, start, getLength() - start );
1017 return *this;
1021 Replace all occurrences of
1022 oldChar in this string buffer with newChar.
1024 @since LibreOffice 4.0
1026 @param oldChar the old character.
1027 @param newChar the new character.
1028 @return this string buffer
1030 OUStringBuffer& replace( sal_Unicode oldChar, sal_Unicode newChar )
1032 sal_Int32 index = 0;
1033 while((index = indexOf(oldChar, index)) >= 0)
1035 pData->buffer[ index ] = newChar;
1037 return *this;
1040 /** Allows access to the internal data of this OUStringBuffer, for effective
1041 manipulation.
1043 This method should be used with care. After you have called this
1044 method, you may use the returned pInternalData or pInternalCapacity only
1045 as long as you make no other method call on this OUStringBuffer.
1047 @param pInternalData
1048 This output parameter receives a pointer to the internal data
1049 (rtl_uString pointer). pInternalData itself must not be null.
1051 @param pInternalCapacity
1052 This output parameter receives a pointer to the internal capacity.
1053 pInternalCapacity itself must not be null.
1055 inline void accessInternals(rtl_uString *** pInternalData,
1056 sal_Int32 ** pInternalCapacity)
1058 *pInternalData = &pData;
1059 *pInternalCapacity = &nCapacity;
1064 Returns the index within this string of the first occurrence of the
1065 specified character, starting the search at the specified index.
1067 @since LibreOffice 4.0
1069 @param ch character to be located.
1070 @param fromIndex the index to start the search from.
1071 The index must be greater or equal than 0
1072 and less or equal as the string length.
1073 @return the index of the first occurrence of the character in the
1074 character sequence represented by this string that is
1075 greater than or equal to fromIndex, or
1076 -1 if the character does not occur.
1078 sal_Int32 indexOf( sal_Unicode ch, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
1080 sal_Int32 ret = rtl_ustr_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch );
1081 return (ret < 0 ? ret : ret+fromIndex);
1085 Returns the index within this string of the last occurrence of the
1086 specified character, searching backward starting at the end.
1088 @since LibreOffice 4.0
1090 @param ch character to be located.
1091 @return the index of the last occurrence of the character in the
1092 character sequence represented by this string, or
1093 -1 if the character does not occur.
1095 sal_Int32 lastIndexOf( sal_Unicode ch ) const SAL_THROW(())
1097 return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch );
1101 Returns the index within this string of the last occurrence of the
1102 specified character, searching backward starting before the specified
1103 index.
1105 @since LibreOffice 4.0
1107 @param ch character to be located.
1108 @param fromIndex the index before which to start the search.
1109 @return the index of the last occurrence of the character in the
1110 character sequence represented by this string that
1111 is less than fromIndex, or -1
1112 if the character does not occur before that point.
1114 sal_Int32 lastIndexOf( sal_Unicode ch, sal_Int32 fromIndex ) const SAL_THROW(())
1116 return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch );
1120 Returns the index within this string of the first occurrence of the
1121 specified substring, starting at the specified index.
1123 If str doesn't include any character, always -1 is
1124 returned. This is also the case, if both strings are empty.
1126 @since LibreOffice 4.0
1128 @param str the substring to search for.
1129 @param fromIndex the index to start the search from.
1130 @return If the string argument occurs one or more times as a substring
1131 within this string at the starting index, then the index
1132 of the first character of the first such substring is
1133 returned. If it does not occur as a substring starting
1134 at fromIndex or beyond, -1 is returned.
1136 sal_Int32 indexOf( const OUString & str, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
1138 sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1139 str.pData->buffer, str.pData->length );
1140 return (ret < 0 ? ret : ret+fromIndex);
1144 @overload
1145 This function accepts an ASCII string literal as its argument.
1147 @since LibreOffice 4.0
1149 template< typename T >
1150 typename internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
1152 assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
1153 sal_Int32 ret = rtl_ustr_indexOfAscii_WithLength(
1154 pData->buffer + fromIndex, pData->length - fromIndex, literal,
1155 internal::ConstCharArrayDetector< T, void >::size - 1);
1156 return ret < 0 ? ret : ret + fromIndex;
1160 Returns the index within this string of the last occurrence of
1161 the specified substring, searching backward starting at the end.
1163 The returned index indicates the starting index of the substring
1164 in this string.
1165 If str doesn't include any character, always -1 is
1166 returned. This is also the case, if both strings are empty.
1168 @since LibreOffice 4.0
1170 @param str the substring to search for.
1171 @return If the string argument occurs one or more times as a substring
1172 within this string, then the index of the first character of
1173 the last such substring is returned. If it does not occur as
1174 a substring, -1 is returned.
1176 sal_Int32 lastIndexOf( const OUString & str ) const SAL_THROW(())
1178 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1179 str.pData->buffer, str.pData->length );
1183 Returns the index within this string of the last occurrence of
1184 the specified substring, searching backward starting before the specified
1185 index.
1187 The returned index indicates the starting index of the substring
1188 in this string.
1189 If str doesn't include any character, always -1 is
1190 returned. This is also the case, if both strings are empty.
1192 @since LibreOffice 4.0
1194 @param str the substring to search for.
1195 @param fromIndex the index before which to start the search.
1196 @return If the string argument occurs one or more times as a substring
1197 within this string before the starting index, then the index
1198 of the first character of the last such substring is
1199 returned. Otherwise, -1 is returned.
1201 sal_Int32 lastIndexOf( const OUString & str, sal_Int32 fromIndex ) const SAL_THROW(())
1203 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1204 str.pData->buffer, str.pData->length );
1208 @overload
1209 This function accepts an ASCII string literal as its argument.
1210 @since LibreOffice 4.0
1212 template< typename T >
1213 typename internal::ConstCharArrayDetector< T, sal_Int32 >::Type lastIndexOf( T& literal ) const SAL_THROW(())
1215 assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
1216 return rtl_ustr_lastIndexOfAscii_WithLength(
1217 pData->buffer, pData->length, literal, internal::ConstCharArrayDetector< T, void >::size - 1);
1221 Strip the given character from the start of the buffer.
1223 @since LibreOffice 4.0
1225 @param c the character to strip
1226 @return The number of characters stripped
1229 sal_Int32 stripStart(sal_Unicode c = (sal_Unicode)' ')
1231 sal_Int32 index;
1232 for(index = 0; index < getLength() ; index++)
1234 if(pData->buffer[ index ] != c)
1236 break;
1239 if(index)
1241 remove(0, index);
1243 return index;
1247 Strip the given character from the end of the buffer.
1249 @since LibreOffice 4.0
1251 @param c the character to strip
1252 @return The number of characters stripped
1255 sal_Int32 stripEnd(sal_Unicode c = (sal_Unicode)' ')
1257 sal_Int32 result = getLength();
1258 sal_Int32 index;
1259 for(index = getLength(); index > 0 ; index--)
1261 if(pData->buffer[ index - 1 ] != c)
1263 break;
1266 if(index < getLength())
1268 truncate(index);
1270 return result - getLength();
1273 Strip the given character from the both end of the buffer.
1275 @since LibreOffice 4.0
1277 @param c the character to strip
1278 @return The number of characters stripped
1281 sal_Int32 strip(sal_Unicode c = (sal_Unicode)' ')
1283 return stripStart(c) + stripEnd(c);
1286 Returns a new string buffer that is a substring of this string.
1288 The substring begins at the specified beginIndex. If
1289 beginIndex is negative or be greater than the length of
1290 this string, behaviour is undefined.
1292 @param beginIndex the beginning index, inclusive.
1293 @return the specified substring.
1294 @since LibreOffice 4.1
1296 OUStringBuffer copy( sal_Int32 beginIndex ) const SAL_THROW(())
1298 assert(beginIndex >= 0 && beginIndex <= getLength());
1299 return copy( beginIndex, getLength() - beginIndex );
1303 Returns a new string buffer that is a substring of this string.
1305 The substring begins at the specified beginIndex and contains count
1306 characters. If either beginIndex or count are negative,
1307 or beginIndex + count are greater than the length of this string
1308 then behaviour is undefined.
1310 @param beginIndex the beginning index, inclusive.
1311 @param count the number of characters.
1312 @return the specified substring.
1313 @since LibreOffice 4.1
1315 OUStringBuffer copy( sal_Int32 beginIndex, sal_Int32 count ) const SAL_THROW(())
1317 assert(beginIndex >= 0 && beginIndex <= getLength());
1318 assert(count >= 0 && count <= getLength() - beginIndex);
1319 rtl_uString *pNew = 0;
1320 rtl_uStringbuffer_newFromStr_WithLength( &pNew, getStr() + beginIndex, count );
1321 return OUStringBuffer( pNew, count + 16 );
1324 #ifdef LIBO_INTERNAL_ONLY
1325 // This is to complement the RTL_FAST_STRING operator+, which allows any combination of valid operands,
1326 // even two buffers. It's intentional it returns OUString, just like the operator+ would in the fast variant.
1327 #ifndef RTL_FAST_STRING
1329 @internal
1330 @since LibreOffice 4.1
1332 friend OUString operator+( const OUStringBuffer& str1, const OUStringBuffer& str2 ) SAL_THROW(())
1334 return OUString( str1.pData ).concat( str2.pData );
1336 #endif
1337 #endif
1339 private:
1340 OUStringBuffer( rtl_uString * value, const sal_Int32 capacity )
1342 pData = value;
1343 nCapacity = capacity;
1347 A pointer to the data structur which contains the data.
1349 rtl_uString * pData;
1352 The len of the pData->buffer.
1354 sal_Int32 nCapacity;
1357 #ifdef RTL_FAST_STRING
1359 @internal
1361 template<>
1362 struct ToStringHelper< OUStringBuffer >
1364 static int length( const OUStringBuffer& s ) { return s.getLength(); }
1365 static sal_Unicode* addData( sal_Unicode* buffer, const OUStringBuffer& s ) { return addDataHelper( buffer, s.getStr(), s.getLength()); }
1366 static const bool allowOStringConcat = false;
1367 static const bool allowOUStringConcat = true;
1369 #endif
1373 #ifdef RTL_STRING_UNITTEST
1374 namespace rtl
1376 typedef rtlunittest::OUStringBuffer OUStringBuffer;
1378 #endif
1380 #ifdef RTL_USING
1381 using ::rtl::OUStringBuffer;
1382 #endif
1384 #endif /* _RTL_USTRBUF_HXX_ */
1386 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */