Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / include / rtl / ustrbuf.hxx
blob1b1067977a3808511d6781eab283c62296ade27a
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 INCLUDED_RTL_USTRBUF_HXX
21 #define INCLUDED_RTL_USTRBUF_HXX
23 #include <sal/config.h>
25 #include <cassert>
26 #include <cstddef>
27 #include <string.h>
29 #include <rtl/ustrbuf.h>
30 #include <rtl/ustring.hxx>
31 #include <rtl/stringutils.hxx>
32 #include <sal/types.h>
34 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
35 #include <rtl/stringconcat.hxx>
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.
56 class SAL_WARN_UNUSED OUStringBuffer
58 public:
59 /**
60 Constructs a string buffer with no characters in it and an
61 initial capacity of 16 characters.
63 OUStringBuffer()
64 : pData(NULL)
65 , nCapacity( 16 )
67 rtl_uString_new_WithLength( &pData, nCapacity );
70 /**
71 Allocates a new string buffer that contains the same sequence of
72 characters as the string buffer argument.
74 @param value a <code>OUStringBuffer</code>.
76 OUStringBuffer( const OUStringBuffer & value )
77 : pData(NULL)
78 , nCapacity( value.nCapacity )
80 rtl_uStringbuffer_newFromStringBuffer( &pData, value.nCapacity, value.pData );
83 /**
84 Constructs a string buffer with no characters in it and an
85 initial capacity specified by the <code>length</code> argument.
87 @param length the initial capacity.
89 explicit OUStringBuffer(int length)
90 : pData(NULL)
91 , nCapacity( length )
93 rtl_uString_new_WithLength( &pData, length );
95 #if __cplusplus >= 201103L
96 explicit OUStringBuffer(unsigned int length)
97 : OUStringBuffer(static_cast<int>(length))
100 #if SAL_TYPES_SIZEOFLONG == 4
101 // additional overloads for sal_Int32 sal_uInt32
102 explicit OUStringBuffer(long length)
103 : OUStringBuffer(static_cast<int>(length))
106 explicit OUStringBuffer(unsigned long length)
107 : OUStringBuffer(static_cast<int>(length))
110 #endif
111 // avoid obvious bugs
112 explicit OUStringBuffer(char) = delete;
113 explicit OUStringBuffer(sal_Unicode) = delete;
114 #endif
117 Constructs a string buffer so that it represents the same
118 sequence of characters as the string argument.
120 The initial
121 capacity of the string buffer is <code>16</code> plus the length
122 of the string argument.
124 @param value the initial contents of the buffer.
126 OUStringBuffer(const OUString& value)
127 : pData(NULL)
128 , nCapacity( value.getLength() + 16 )
130 rtl_uStringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
133 template< typename T >
134 OUStringBuffer( T& literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
135 : pData(NULL)
136 , nCapacity( libreoffice_internal::ConstCharArrayDetector<T>::length + 16 )
138 assert(
139 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
140 rtl_uString_newFromLiteral(
141 &pData,
142 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
143 libreoffice_internal::ConstCharArrayDetector<T>::length, 16);
144 #ifdef RTL_STRING_UNITTEST
145 rtl_string_unittest_const_literal = true;
146 #endif
149 #ifdef RTL_STRING_UNITTEST
151 * Only used by unittests to detect incorrect conversions.
152 * @internal
154 template< typename T >
155 OUStringBuffer( T&, typename libreoffice_internal::ExceptConstCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
157 pData = NULL;
158 nCapacity = 10;
159 rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
160 rtl_string_unittest_invalid_conversion = true;
163 * Only used by unittests to detect incorrect conversions.
164 * @internal
166 template< typename T >
167 OUStringBuffer( const T&, typename libreoffice_internal::ExceptCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
169 pData = NULL;
170 nCapacity = 10;
171 rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
172 rtl_string_unittest_invalid_conversion = true;
174 #endif
176 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
178 @overload
179 @internal
181 template< typename T1, typename T2 >
182 OUStringBuffer( const OUStringConcat< T1, T2 >& c )
184 const sal_Int32 l = c.length();
185 nCapacity = l + 16;
186 pData = rtl_uString_alloc( nCapacity );
187 sal_Unicode* end = c.addData( pData->buffer );
188 *end = '\0';
189 pData->length = l;
190 // TODO realloc in case pData->>length is noticeably smaller than l ?
192 #endif
193 /** Assign to this a copy of value.
195 OUStringBuffer& operator = ( const OUStringBuffer& value )
197 if (this != &value)
199 rtl_uStringbuffer_newFromStringBuffer(&pData,
200 value.nCapacity,
201 value.pData);
202 nCapacity = value.nCapacity;
204 return *this;
208 Release the string data.
210 ~OUStringBuffer()
212 rtl_uString_release( pData );
216 Fill the string data in the new string and clear the buffer.
218 This method is more efficient than the constructor of the string. It does
219 not copy the buffer.
221 @return the string previously contained in the buffer.
223 OUString makeStringAndClear()
225 return OUString(
226 rtl_uStringBuffer_makeStringAndClear( &pData, &nCapacity ),
227 SAL_NO_ACQUIRE );
231 Returns the length (character count) of this string buffer.
233 @return the number of characters in this string buffer.
235 sal_Int32 getLength() const
237 return pData->length;
241 Checks if a string buffer is empty.
243 @return true if the string buffer is empty;
244 false, otherwise.
246 @since LibreOffice 4.1
248 bool isEmpty() const
250 return pData->length == 0;
254 Returns the current capacity of the String buffer.
256 The capacity
257 is the amount of storage available for newly inserted
258 characters. The real buffer size is 2 bytes longer, because
259 all strings are 0 terminated.
261 @return the current capacity of this string buffer.
263 sal_Int32 getCapacity() const
265 return nCapacity;
269 Ensures that the capacity of the buffer is at least equal to the
270 specified minimum.
272 The new capacity will be at least as large as the maximum of the current
273 length (so that no contents of the buffer is destroyed) and the given
274 minimumCapacity. If the given minimumCapacity is negative, nothing is
275 changed.
277 @param minimumCapacity the minimum desired capacity.
279 void ensureCapacity(sal_Int32 minimumCapacity)
281 rtl_uStringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
285 Sets the length of this String buffer.
287 If the <code>newLength</code> argument is less than the current
288 length of the string buffer, the string buffer is truncated to
289 contain exactly the number of characters given by the
290 <code>newLength</code> argument.
292 If the <code>newLength</code> argument is greater than or equal
293 to the current length, sufficient null characters
294 (<code>'&#92;u0000'</code>) are appended to the string buffer so that
295 length becomes the <code>newLength</code> argument.
297 The <code>newLength</code> argument must be greater than or equal
298 to <code>0</code>.
300 @param newLength the new length of the buffer.
302 void setLength(sal_Int32 newLength)
304 assert(newLength >= 0);
305 // Avoid modifications if pData points to const empty string:
306 if( newLength != pData->length )
308 if( newLength > nCapacity )
309 rtl_uStringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
310 else
311 pData->buffer[newLength] = 0;
312 pData->length = newLength;
317 Returns the character at a specific index in this string buffer.
319 The first character of a string buffer is at index
320 <code>0</code>, the next at index <code>1</code>, and so on, for
321 array indexing.
323 The index argument must be greater than or equal to
324 <code>0</code>, and less than the length of this string buffer.
326 @param index the index of the desired character.
327 @return the character at the specified index of this string buffer.
329 SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
330 sal_Unicode charAt( sal_Int32 index ) const
332 assert(index >= 0 && index < pData->length);
333 return pData->buffer[ index ];
337 The character at the specified index of this string buffer is set
338 to <code>ch</code>.
340 The index argument must be greater than or equal to
341 <code>0</code>, and less than the length of this string buffer.
343 @param index the index of the character to modify.
344 @param ch the new character.
346 SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
347 OUStringBuffer & setCharAt(sal_Int32 index, sal_Unicode ch)
349 assert(index >= 0 && index < pData->length);
350 pData->buffer[ index ] = ch;
351 return *this;
355 Return a null terminated unicode character array.
357 const sal_Unicode* getStr() const { return pData->buffer; }
360 Access to individual characters.
362 @param index must be non-negative and less than length.
364 @return a reference to the character at the given index.
366 @since LibreOffice 3.5
368 sal_Unicode & operator [](sal_Int32 index)
370 assert(index >= 0 && index < pData->length);
371 return pData->buffer[index];
375 Access to individual characters.
377 @param index must be non-negative and less than length.
379 @return a reference to the character at the given index.
381 @since LibreOffice 4.2
383 const sal_Unicode & operator [](sal_Int32 index) const
385 assert(index >= 0 && index < pData->length);
386 return pData->buffer[index];
390 Return a OUString instance reflecting the current content
391 of this OUStringBuffer.
393 const OUString toString() const
395 return OUString(pData->buffer, pData->length);
399 Appends the string to this string buffer.
401 The characters of the <code>OUString</code> argument are appended, in
402 order, to the contents of this string buffer, increasing the
403 length of this string buffer by the length of the argument.
405 @param str a string.
406 @return this string buffer.
408 OUStringBuffer & append(const OUString &str)
410 return append( str.getStr(), str.getLength() );
414 Appends the content of a stringbuffer to this string buffer.
416 The characters of the <code>OUStringBuffer</code> argument are appended, in
417 order, to the contents of this string buffer, increasing the
418 length of this string buffer by the length of the argument.
420 @param str a string.
421 @return this string buffer.
423 @since LibreOffice 4.0
425 OUStringBuffer & append(const OUStringBuffer &str)
427 if(!str.isEmpty())
429 append( str.getStr(), str.getLength() );
431 return *this;
435 Appends the string representation of the <code>char</code> array
436 argument to this string buffer.
438 The characters of the array argument are appended, in order, to
439 the contents of this string buffer. The length of this string
440 buffer increases by the length of the argument.
442 @param str the characters to be appended.
443 @return this string buffer.
445 OUStringBuffer & append( const sal_Unicode * str )
447 return append( str, rtl_ustr_getLength( str ) );
451 Appends the string representation of the <code>char</code> array
452 argument to this string buffer.
454 Characters of the character array <code>str</code> are appended,
455 in order, to the contents of this string buffer. The length of this
456 string buffer increases by the value of <code>len</code>.
458 @param str the characters to be appended; must be non-null, and must
459 point to at least len characters
460 @param len the number of characters to append; must be non-negative
461 @return this string buffer.
463 OUStringBuffer & append( const sal_Unicode * str, sal_Int32 len)
465 assert( len == 0 || str != NULL ); // cannot assert that in rtl_uStringbuffer_insert
466 rtl_uStringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
467 return *this;
471 @overload
472 This function accepts an ASCII string literal as its argument.
473 @since LibreOffice 3.6
475 template< typename T >
476 typename libreoffice_internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type append( T& literal )
478 assert(
479 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
480 rtl_uStringbuffer_insert_ascii(
481 &pData, &nCapacity, getLength(),
482 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
483 libreoffice_internal::ConstCharArrayDetector<T>::length);
484 return *this;
487 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
489 @overload
490 @internal
492 template< typename T1, typename T2 >
493 OUStringBuffer& append( const OUStringConcat< T1, T2 >& c )
495 sal_Int32 l = c.length();
496 if( l == 0 )
497 return *this;
498 l += pData->length;
499 rtl_uStringbuffer_ensureCapacity( &pData, &nCapacity, l );
500 sal_Unicode* end = c.addData( pData->buffer + pData->length );
501 *end = '\0';
502 pData->length = l;
503 return *this;
505 #endif
508 Appends a 8-Bit ASCII character string to this string buffer.
510 Since this method is optimized for performance. the ASCII
511 character values are not converted in any way. The caller
512 has to make sure that all ASCII characters are in the
513 allowed range between 0 and 127. The ASCII string must be
514 NULL-terminated.
516 The characters of the array argument are appended, in order, to
517 the contents of this string buffer. The length of this string
518 buffer increases by the length of the argument.
520 @param str the 8-Bit ASCII characters to be appended.
521 @return this string buffer.
523 OUStringBuffer & appendAscii( const sal_Char * str )
525 return appendAscii( str, rtl_str_getLength( str ) );
529 Appends a 8-Bit ASCII character string to this string buffer.
531 Since this method is optimized for performance. the ASCII
532 character values are not converted in any way. The caller
533 has to make sure that all ASCII characters are in the
534 allowed range between 0 and 127. The ASCII string must be
535 NULL-terminated.
537 Characters of the character array <code>str</code> are appended,
538 in order, to the contents of this string buffer. The length of this
539 string buffer increases by the value of <code>len</code>.
541 @param str the 8-Bit ASCII characters to be appended; must be non-null,
542 and must point to at least len characters
543 @param len the number of characters to append; must be non-negative
544 @return this string buffer.
546 OUStringBuffer & appendAscii( const sal_Char * str, sal_Int32 len)
548 rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), str, len );
549 return *this;
553 Appends the string representation of the <code>bool</code>
554 argument to the string buffer.
556 The argument is converted to a string as if by the method
557 <code>String.valueOf</code>, and the characters of that
558 string are then appended to this string buffer.
560 @param b a <code>bool</code>.
561 @return this string buffer.
563 @since LibreOffice 4.1
565 OUStringBuffer & append(bool b)
567 sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
568 return append( sz, rtl_ustr_valueOfBoolean( sz, b ) );
571 /// @cond INTERNAL
572 // Pointer can be automatically converted to bool, which is unwanted here.
573 // Explicitly delete all pointer append() overloads to prevent this
574 // (except for char* and sal_Unicode* overloads, which are handled elsewhere).
575 template< typename T >
576 typename libreoffice_internal::Enable< void,
577 !libreoffice_internal::CharPtrDetector< T* >::ok && !libreoffice_internal::SalUnicodePtrDetector< T* >::ok >::Type
578 append( T* ) SAL_DELETED_FUNCTION;
579 /// @endcond
581 // This overload is needed because OUString has a ctor from rtl_uString*, but
582 // the bool overload above would be preferred to the conversion.
584 @internal
586 OUStringBuffer & append(rtl_uString* str)
588 return append( OUString( str ));
592 Appends the string representation of the <code>sal_Bool</code>
593 argument to the string buffer.
595 The argument is converted to a string as if by the method
596 <code>String.valueOf</code>, and the characters of that
597 string are then appended to this string buffer.
599 @param b a <code>sal_Bool</code>.
600 @return this string buffer.
602 OUStringBuffer & append(sal_Bool b)
604 sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
605 return append( sz, rtl_ustr_valueOfBoolean( sz, b ) );
609 Appends the string representation of the ASCII <code>char</code>
610 argument to this string buffer.
612 The argument is appended to the contents of this string buffer.
613 The length of this string buffer increases by <code>1</code>.
615 @param c an ASCII <code>char</code>.
616 @return this string buffer.
618 @since LibreOffice 3.5
620 OUStringBuffer & append(char c)
622 assert(static_cast< unsigned char >(c) <= 0x7F);
623 return append(sal_Unicode(c));
627 Appends the string representation of the <code>char</code>
628 argument to this string buffer.
630 The argument is appended to the contents of this string buffer.
631 The length of this string buffer increases by <code>1</code>.
633 @param c a <code>char</code>.
634 @return this string buffer.
636 OUStringBuffer & append(sal_Unicode c)
638 return append( &c, 1 );
641 #if LIBO_INTERNAL_ONLY && \
642 (!defined SAL_W32 || defined __MINGW32__ || defined __clang__)
643 // cf. sal/types.h sal_Unicode
644 void append(sal_uInt16) = delete;
645 #endif
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>&ndash;<code>0x10FFFF</code>, but excluding
725 <code>0xD800</code>&ndash;<code>0xDFFF</code>)
727 @return
728 this string buffer
730 OUStringBuffer & appendUtf32(sal_uInt32 c) {
731 return insertUtf32(getLength(), c);
735 Unsafe way to make space for a fixed amount of characters to be appended
736 into this OUStringBuffer.
738 A call to this function must immediately be followed by code that
739 completely fills the uninitialized block pointed to by the return value.
741 @param length the length of the uninitialized block of sal_Unicode
742 entities; must be non-negative
744 @return a pointer to the start of the uninitialized block; only valid
745 until this OUStringBuffer's capacity changes
747 @since LibreOffice 4.4
749 sal_Unicode * appendUninitialized(sal_Int32 length) {
750 sal_Int32 n = getLength();
751 rtl_uStringbuffer_insert(&pData, &nCapacity, n, NULL, length);
752 return pData->buffer + n;
756 Inserts the string into this string buffer.
758 The characters of the <code>String</code> argument are inserted, in
759 order, into this string buffer at the indicated offset. The length
760 of this string buffer is increased by the length of the argument.
762 The offset argument must be greater than or equal to
763 <code>0</code>, and less than or equal to the length of this
764 string buffer.
766 @param offset the offset.
767 @param str a string.
768 @return this string buffer.
770 OUStringBuffer & insert(sal_Int32 offset, const OUString & str)
772 return insert( offset, str.getStr(), str.getLength() );
776 Inserts the string representation of the <code>char</code> array
777 argument into this string buffer.
779 The characters of the array argument are inserted into the
780 contents of this string buffer at the position indicated by
781 <code>offset</code>. The length of this string buffer increases by
782 the length of the argument.
784 The offset argument must be greater than or equal to
785 <code>0</code>, and less than or equal to the length of this
786 string buffer.
788 @param offset the offset.
789 @param str a character array.
790 @return this string buffer.
792 OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str )
794 return insert( offset, str, rtl_ustr_getLength( str ) );
798 Inserts the string representation of the <code>char</code> array
799 argument into this string buffer.
801 The characters of the array argument are inserted into the
802 contents of this string buffer at the position indicated by
803 <code>offset</code>. The length of this string buffer increases by
804 the length of the argument.
806 The offset argument must be greater than or equal to
807 <code>0</code>, and less than or equal to the length of this
808 string buffer.
810 @param offset the offset.
811 @param str a character array.
812 @param len the number of characters to append.
813 @return this string buffer.
815 OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str, sal_Int32 len)
817 assert( len == 0 || str != NULL ); // cannot assert that in rtl_uStringbuffer_insert
818 rtl_uStringbuffer_insert( &pData, &nCapacity, offset, str, len );
819 return *this;
823 @overload
824 This function accepts an ASCII string literal as its argument.
825 @since LibreOffice 3.6
827 template< typename T >
828 typename libreoffice_internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type insert( sal_Int32 offset, T& literal )
830 assert(
831 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
832 rtl_uStringbuffer_insert_ascii(
833 &pData, &nCapacity, offset,
834 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
835 libreoffice_internal::ConstCharArrayDetector<T>::length);
836 return *this;
840 Inserts the string representation of the <code>sal_Bool</code>
841 argument into this string buffer.
843 The second argument is converted to a string as if by the method
844 <code>String.valueOf</code>, and the characters of that
845 string are then inserted into this string buffer at the indicated
846 offset.
848 The offset argument must be greater than or equal to
849 <code>0</code>, and less than or equal to the length of this
850 string buffer.
852 @param offset the offset.
853 @param b a <code>sal_Bool</code>.
854 @return this string buffer.
856 OUStringBuffer & insert(sal_Int32 offset, sal_Bool b)
858 sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
859 return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
863 Inserts the string representation of the <code>bool</code>
864 argument into this string buffer.
866 The second argument is converted to a string as if by the method
867 <code>OUString::boolean</code>, and the characters of that
868 string are then inserted into this string buffer at the indicated
869 offset.
871 The offset argument must be greater than or equal to
872 <code>0</code>, and less than or equal to the length of this
873 string buffer.
875 @param offset the offset.
876 @param b a <code>bool</code>.
877 @return this string buffer.
879 @since LibreOffice 4.3
881 OUStringBuffer & insert(sal_Int32 offset, bool b)
883 sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
884 return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
888 Inserts the string representation of the <code>char</code>
889 argument into this string buffer.
891 The second argument is inserted into the contents of this string
892 buffer at the position indicated by <code>offset</code>. The length
893 of this string buffer increases by one.
895 The offset argument must be greater than or equal to
896 <code>0</code>, and less than or equal to the length of this
897 string buffer.
899 @param offset the offset.
900 @param c a <code>char</code>.
901 @return this string buffer.
903 @since LibreOffice 3.6
905 OUStringBuffer & insert(sal_Int32 offset, char c)
907 sal_Unicode u = c;
908 return insert( offset, &u, 1 );
912 Inserts the string representation of the <code>char</code>
913 argument into this string buffer.
915 The second argument is inserted into the contents of this string
916 buffer at the position indicated by <code>offset</code>. The length
917 of this string buffer increases by one.
919 The offset argument must be greater than or equal to
920 <code>0</code>, and less than or equal to the length of this
921 string buffer.
923 @param offset the offset.
924 @param c a <code>char</code>.
925 @return this string buffer.
927 OUStringBuffer & insert(sal_Int32 offset, sal_Unicode c)
929 return insert( offset, &c, 1 );
933 Inserts the string representation of the second <code>sal_Int32</code>
934 argument into this string buffer.
936 The second argument is converted to a string as if by the method
937 <code>String.valueOf</code>, and the characters of that
938 string are then inserted into this string buffer at the indicated
939 offset.
941 The offset argument must be greater than or equal to
942 <code>0</code>, and less than or equal to the length of this
943 string buffer.
945 @param offset the offset.
946 @param i an <code>sal_Int32</code>.
947 @param radix the radix.
948 @return this string buffer.
949 @exception StringIndexOutOfBoundsException if the offset is invalid.
951 OUStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
953 sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT32];
954 return insert( offset, sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
958 Inserts the string representation of the <code>long</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
964 offset.
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
968 string buffer.
970 @param offset the offset.
971 @param l a <code>long</code>.
972 @param radix the radix.
973 @return this string buffer.
974 @exception StringIndexOutOfBoundsException if the offset is invalid.
976 OUStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
978 sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT64];
979 return insert( offset, sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
983 Inserts the string representation of the <code>float</code>
984 argument into this string buffer.
986 The second argument is converted to a string as if by the method
987 <code>String.valueOf</code>, and the characters of that
988 string are then inserted into this string buffer at the indicated
989 offset.
991 The offset argument must be greater than or equal to
992 <code>0</code>, and less than or equal to the length of this
993 string buffer.
995 @param offset the offset.
996 @param f a <code>float</code>.
997 @return this string buffer.
998 @exception StringIndexOutOfBoundsException if the offset is invalid.
1000 OUStringBuffer insert(sal_Int32 offset, float f)
1002 sal_Unicode sz[RTL_USTR_MAX_VALUEOFFLOAT];
1003 return insert( offset, sz, rtl_ustr_valueOfFloat( sz, f ) );
1007 Inserts the string representation of the <code>double</code>
1008 argument into this string buffer.
1010 The second argument is converted to a string as if by the method
1011 <code>String.valueOf</code>, and the characters of that
1012 string are then inserted into this string buffer at the indicated
1013 offset.
1015 The offset argument must be greater than or equal to
1016 <code>0</code>, and less than or equal to the length of this
1017 string buffer.
1019 @param offset the offset.
1020 @param d a <code>double</code>.
1021 @return this string buffer.
1022 @exception StringIndexOutOfBoundsException if the offset is invalid.
1024 OUStringBuffer & insert(sal_Int32 offset, double d)
1026 sal_Unicode sz[RTL_USTR_MAX_VALUEOFDOUBLE];
1027 return insert( offset, sz, rtl_ustr_valueOfDouble( sz, d ) );
1031 Inserts a single UTF-32 character into this string buffer.
1033 <p>The single UTF-32 character will be represented within the string
1034 buffer as either one or two UTF-16 code units.</p>
1036 @param offset the offset into this string buffer (from zero to the length
1037 of this string buffer, inclusive)
1039 @param c a well-formed UTF-32 code unit (that is, a value in the range
1040 <code>0</code>&ndash;<code>0x10FFFF</code>, but excluding
1041 <code>0xD800</code>&ndash;<code>0xDFFF</code>)
1043 @return this string buffer
1045 OUStringBuffer & insertUtf32(sal_Int32 offset, sal_uInt32 c) {
1046 rtl_uStringbuffer_insertUtf32(&pData, &nCapacity, offset, c);
1047 return *this;
1051 Removes the characters in a substring of this sequence.
1053 The substring begins at the specified <code>start</code> and
1054 is <code>len</code> characters long.
1056 start must be >= 0 && <= This->length
1058 @param start The beginning index, inclusive
1059 @param len The substring length
1060 @return this string buffer.
1062 OUStringBuffer & remove( sal_Int32 start, sal_Int32 len )
1064 rtl_uStringbuffer_remove( &pData, start, len );
1065 return *this;
1069 Removes the tail of a string buffer start at the indicate position
1071 start must be >= 0 && <= This->length
1073 @param start The beginning index, inclusive. default to 0
1074 @return this string buffer.
1076 @since LibreOffice 4.0
1078 OUStringBuffer & truncate( sal_Int32 start = 0 )
1080 rtl_uStringbuffer_remove( &pData, start, getLength() - start );
1081 return *this;
1085 Replace all occurrences of
1086 oldChar in this string buffer with newChar.
1088 @since LibreOffice 4.0
1090 @param oldChar the old character.
1091 @param newChar the new character.
1092 @return this string buffer
1094 OUStringBuffer& replace( sal_Unicode oldChar, sal_Unicode newChar )
1096 sal_Int32 index = 0;
1097 while((index = indexOf(oldChar, index)) >= 0)
1099 pData->buffer[ index ] = newChar;
1101 return *this;
1104 /** Allows access to the internal data of this OUStringBuffer, for effective
1105 manipulation.
1107 This method should be used with care. After you have called this
1108 method, you may use the returned pInternalData or pInternalCapacity only
1109 as long as you make no other method call on this OUStringBuffer.
1111 @param pInternalData
1112 This output parameter receives a pointer to the internal data
1113 (rtl_uString pointer). pInternalData itself must not be null.
1115 @param pInternalCapacity
1116 This output parameter receives a pointer to the internal capacity.
1117 pInternalCapacity itself must not be null.
1119 inline void accessInternals(rtl_uString *** pInternalData,
1120 sal_Int32 ** pInternalCapacity)
1122 *pInternalData = &pData;
1123 *pInternalCapacity = &nCapacity;
1128 Returns the index within this string of the first occurrence of the
1129 specified character, starting the search at the specified index.
1131 @since LibreOffice 4.0
1133 @param ch character to be located.
1134 @param fromIndex the index to start the search from.
1135 The index must be greater or equal than 0
1136 and less or equal as the string length.
1137 @return the index of the first occurrence of the character in the
1138 character sequence represented by this string that is
1139 greater than or equal to fromIndex, or
1140 -1 if the character does not occur.
1142 sal_Int32 indexOf( sal_Unicode ch, sal_Int32 fromIndex = 0 ) const
1144 assert( fromIndex >= 0 && fromIndex <= pData->length );
1145 sal_Int32 ret = rtl_ustr_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch );
1146 return (ret < 0 ? ret : ret+fromIndex);
1150 Returns the index within this string of the last occurrence of the
1151 specified character, searching backward starting at the end.
1153 @since LibreOffice 4.0
1155 @param ch character to be located.
1156 @return the index of the last occurrence of the character in the
1157 character sequence represented by this string, or
1158 -1 if the character does not occur.
1160 sal_Int32 lastIndexOf( sal_Unicode ch ) const
1162 return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch );
1166 Returns the index within this string of the last occurrence of the
1167 specified character, searching backward starting before the specified
1168 index.
1170 @since LibreOffice 4.0
1172 @param ch character to be located.
1173 @param fromIndex the index before which to start the search.
1174 @return the index of the last occurrence of the character in the
1175 character sequence represented by this string that
1176 is less than fromIndex, or -1
1177 if the character does not occur before that point.
1179 sal_Int32 lastIndexOf( sal_Unicode ch, sal_Int32 fromIndex ) const
1181 assert( fromIndex >= 0 && fromIndex <= pData->length );
1182 return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch );
1186 Returns the index within this string of the first occurrence of the
1187 specified substring, starting at the specified index.
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 to start the search from.
1196 @return If the string argument occurs one or more times as a substring
1197 within this string at the starting index, then the index
1198 of the first character of the first such substring is
1199 returned. If it does not occur as a substring starting
1200 at fromIndex or beyond, -1 is returned.
1202 sal_Int32 indexOf( const OUString & str, sal_Int32 fromIndex = 0 ) const
1204 assert( fromIndex >= 0 && fromIndex <= pData->length );
1205 sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1206 str.pData->buffer, str.pData->length );
1207 return (ret < 0 ? ret : ret+fromIndex);
1211 @overload
1212 This function accepts an ASCII string literal as its argument.
1214 @since LibreOffice 4.0
1216 template< typename T >
1217 typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const
1219 assert(
1220 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1221 sal_Int32 n = rtl_ustr_indexOfAscii_WithLength(
1222 pData->buffer + fromIndex, pData->length - fromIndex,
1223 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1224 libreoffice_internal::ConstCharArrayDetector<T>::length);
1225 return n < 0 ? n : n + fromIndex;
1229 Returns the index within this string of the last occurrence of
1230 the specified substring, searching backward starting at the end.
1232 The returned index indicates the starting index of the substring
1233 in this string.
1234 If str doesn't include any character, always -1 is
1235 returned. This is also the case, if both strings are empty.
1237 @since LibreOffice 4.0
1239 @param str the substring to search for.
1240 @return If the string argument occurs one or more times as a substring
1241 within this string, then the index of the first character of
1242 the last such substring is returned. If it does not occur as
1243 a substring, -1 is returned.
1245 sal_Int32 lastIndexOf( const OUString & str ) const
1247 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1248 str.pData->buffer, str.pData->length );
1252 Returns the index within this string of the last occurrence of
1253 the specified substring, searching backward starting before the specified
1254 index.
1256 The returned index indicates the starting index of the substring
1257 in this string.
1258 If str doesn't include any character, always -1 is
1259 returned. This is also the case, if both strings are empty.
1261 @since LibreOffice 4.0
1263 @param str the substring to search for.
1264 @param fromIndex the index before which to start the search.
1265 @return If the string argument occurs one or more times as a substring
1266 within this string before the starting index, then the index
1267 of the first character of the last such substring is
1268 returned. Otherwise, -1 is returned.
1270 sal_Int32 lastIndexOf( const OUString & str, sal_Int32 fromIndex ) const
1272 assert( fromIndex >= 0 && fromIndex <= pData->length );
1273 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1274 str.pData->buffer, str.pData->length );
1278 @overload
1279 This function accepts an ASCII string literal as its argument.
1280 @since LibreOffice 4.0
1282 template< typename T >
1283 typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type lastIndexOf( T& literal ) const
1285 assert(
1286 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1287 return rtl_ustr_lastIndexOfAscii_WithLength(
1288 pData->buffer, pData->length,
1289 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1290 libreoffice_internal::ConstCharArrayDetector<T>::length);
1294 Strip the given character from the start of the buffer.
1296 @since LibreOffice 4.0
1298 @param c the character to strip
1299 @return The number of characters stripped
1302 sal_Int32 stripStart(sal_Unicode c = (sal_Unicode)' ')
1304 sal_Int32 index;
1305 for(index = 0; index < getLength() ; index++)
1307 if(pData->buffer[ index ] != c)
1309 break;
1312 if(index)
1314 remove(0, index);
1316 return index;
1320 Strip the given character from the end of the buffer.
1322 @since LibreOffice 4.0
1324 @param c the character to strip
1325 @return The number of characters stripped
1328 sal_Int32 stripEnd(sal_Unicode c = (sal_Unicode)' ')
1330 sal_Int32 result = getLength();
1331 sal_Int32 index;
1332 for(index = getLength(); index > 0 ; index--)
1334 if(pData->buffer[ index - 1 ] != c)
1336 break;
1339 if(index < getLength())
1341 truncate(index);
1343 return result - getLength();
1346 Strip the given character from the both end of the buffer.
1348 @since LibreOffice 4.0
1350 @param c the character to strip
1351 @return The number of characters stripped
1354 sal_Int32 strip(sal_Unicode c = (sal_Unicode)' ')
1356 return stripStart(c) + stripEnd(c);
1359 Returns a new string buffer that is a substring of this string.
1361 The substring begins at the specified beginIndex. If
1362 beginIndex is negative or be greater than the length of
1363 this string, behaviour is undefined.
1365 @param beginIndex the beginning index, inclusive.
1366 @return the specified substring.
1367 @since LibreOffice 4.1
1369 OUStringBuffer copy( sal_Int32 beginIndex ) const
1371 return copy( beginIndex, getLength() - beginIndex );
1375 Returns a new string buffer that is a substring of this string.
1377 The substring begins at the specified beginIndex and contains count
1378 characters. If either beginIndex or count are negative,
1379 or beginIndex + count are greater than the length of this string
1380 then behaviour is undefined.
1382 @param beginIndex the beginning index, inclusive.
1383 @param count the number of characters.
1384 @return the specified substring.
1385 @since LibreOffice 4.1
1387 OUStringBuffer copy( sal_Int32 beginIndex, sal_Int32 count ) const
1389 assert(beginIndex >= 0 && beginIndex <= getLength());
1390 assert(count >= 0 && count <= getLength() - beginIndex);
1391 rtl_uString *pNew = NULL;
1392 rtl_uStringbuffer_newFromStr_WithLength( &pNew, getStr() + beginIndex, count );
1393 return OUStringBuffer( pNew, count + 16 );
1396 private:
1397 OUStringBuffer( rtl_uString * value, const sal_Int32 capacity )
1399 pData = value;
1400 nCapacity = capacity;
1404 A pointer to the data structure which contains the data.
1406 rtl_uString * pData;
1409 The len of the pData->buffer.
1411 sal_Int32 nCapacity;
1414 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
1416 @internal
1418 template<>
1419 struct ToStringHelper< OUStringBuffer >
1421 static int length( const OUStringBuffer& s ) { return s.getLength(); }
1422 static sal_Unicode* addData( sal_Unicode* buffer, const OUStringBuffer& s ) { return addDataHelper( buffer, s.getStr(), s.getLength()); }
1423 static const bool allowOStringConcat = false;
1424 static const bool allowOUStringConcat = true;
1426 #endif
1430 #ifdef RTL_STRING_UNITTEST
1431 namespace rtl
1433 typedef rtlunittest::OUStringBuffer OUStringBuffer;
1435 #endif
1437 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
1438 using ::rtl::OUStringBuffer;
1439 #endif
1441 #endif // INCLUDED_RTL_USTRBUF_HXX
1443 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */