merge the formfield patch from ooo-build
[ooovba.git] / sal / inc / rtl / ustrbuf.hxx
blob3ba702a79ee9535002a5f61058e8b2d9f46b4679
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ustrbuf.hxx,v $
10 * $Revision: 1.13 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef _RTL_USTRBUF_HXX_
32 #define _RTL_USTRBUF_HXX_
34 #include <osl/diagnose.h>
35 #include <rtl/ustrbuf.h>
36 #ifndef _RTL_USTRING_HXX
37 #include <rtl/ustring.hxx>
38 #endif
40 #ifdef __cplusplus
42 namespace rtl
45 /** @HTML
46 A string buffer implements a mutable sequence of characters.
47 <p>
48 String buffers are safe for use by multiple threads. The methods
49 are synchronized where necessary so that all the operations on any
50 particular instance behave as if they occur in some serial order.
51 <p>
52 String buffers are used by the compiler to implement the binary
53 string concatenation operator <code>+</code>. For example, the code:
54 <p><blockquote><pre>
55 x = "a" + 4 + "c"
56 </pre></blockquote><p>
57 is compiled to the equivalent of:
58 <p><blockquote><pre>
59 x = new OUStringBuffer().append("a").append(4).append("c")
60 .toString()
61 </pre></blockquote><p>
62 The principal operations on a <code>OUStringBuffer</code> are the
63 <code>append</code> and <code>insert</code> methods, which are
64 overloaded so as to accept data of any type. Each effectively
65 converts a given datum to a string and then appends or inserts the
66 characters of that string to the string buffer. The
67 <code>append</code> method always adds these characters at the end
68 of the buffer; the <code>insert</code> method adds the characters at
69 a specified point.
70 <p>
71 For example, if <code>z</code> refers to a string buffer object
72 whose current contents are "<code>start</code>", then
73 the method call <code>z.append("le")</code> would cause the string
74 buffer to contain "<code>startle</code>", whereas
75 <code>z.insert(4, "le")</code> would alter the string buffer to
76 contain "<code>starlet</code>".
77 <p>
78 Every string buffer has a capacity. As long as the length of the
79 character sequence contained in the string buffer does not exceed
80 the capacity, it is not necessary to allocate a new internal
81 buffer array. If the internal buffer overflows, it is
82 automatically made larger.
84 class OUStringBuffer
86 public:
87 /**
88 Constructs a string buffer with no characters in it and an
89 initial capacity of 16 characters.
91 OUStringBuffer()
92 : pData(NULL)
93 , nCapacity( 16 )
95 rtl_uString_new_WithLength( &pData, nCapacity );
98 /**
99 Allocates a new string buffer that contains the same sequence of
100 characters as the string buffer argument.
102 @param value a <code>OStringBuffer</code>.
104 OUStringBuffer( const OUStringBuffer & value )
105 : pData(NULL)
106 , nCapacity( value.nCapacity )
108 rtl_uStringbuffer_newFromStringBuffer( &pData, value.nCapacity, value.pData );
112 Constructs a string buffer with no characters in it and an
113 initial capacity specified by the <code>length</code> argument.
115 @param length the initial capacity.
117 OUStringBuffer(sal_Int32 length)
118 : pData(NULL)
119 , nCapacity( length )
121 rtl_uString_new_WithLength( &pData, length );
125 Constructs a string buffer so that it represents the same
126 sequence of characters as the string argument.
128 The initial
129 capacity of the string buffer is <code>16</code> plus the length
130 of the string argument.
132 @param str the initial contents of the buffer.
134 OUStringBuffer(OUString value)
135 : pData(NULL)
136 , nCapacity( value.getLength() + 16 )
138 rtl_uStringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
141 /** Assign to this a copy of value.
143 OUStringBuffer& operator = ( const OUStringBuffer& value )
145 if (this != &value)
147 rtl_uStringbuffer_newFromStringBuffer(&pData,
148 value.nCapacity,
149 value.pData);
150 nCapacity = value.nCapacity;
152 return *this;
156 Release the string data.
158 ~OUStringBuffer()
160 rtl_uString_release( pData );
164 Fill the string data in the new string and clear the buffer.
166 This method is more efficient than the contructor of the string. It does
167 not copy the buffer.
169 @return the string previously contained in the buffer.
171 OUString makeStringAndClear()
173 OUString aRet( pData );
174 rtl_uString_new(&pData);
175 nCapacity = 0;
176 return aRet;
180 Returns the length (character count) of this string buffer.
182 @return the number of characters in this string buffer.
184 sal_Int32 getLength() const
186 return pData->length;
190 Returns the current capacity of the String buffer.
192 The capacity
193 is the amount of storage available for newly inserted
194 characters. The real buffer size is 2 bytes longer, because
195 all strings are 0 terminated.
197 @return the current capacity of this string buffer.
199 sal_Int32 getCapacity() const
201 return nCapacity;
205 Ensures that the capacity of the buffer is at least equal to the
206 specified minimum.
208 The new capacity will be at least as large as the maximum of the current
209 length (so that no contents of the buffer is destroyed) and the given
210 minimumCapacity. If the given minimumCapacity is negative, nothing is
211 changed.
213 @param minimumCapacity the minimum desired capacity.
215 void ensureCapacity(sal_Int32 minimumCapacity)
217 rtl_uStringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
221 Sets the length of this String buffer.
223 If the <code>newLength</code> argument is less than the current
224 length of the string buffer, the string buffer is truncated to
225 contain exactly the number of characters given by the
226 <code>newLength</code> argument.
228 If the <code>newLength</code> argument is greater than or equal
229 to the current length, sufficient null characters
230 (<code>'&#92;u0000'</code>) are appended to the string buffer so that
231 length becomes the <code>newLength</code> argument.
233 The <code>newLength</code> argument must be greater than or equal
234 to <code>0</code>.
236 @param newLength the new length of the buffer.
238 void setLength(sal_Int32 newLength)
240 OSL_ASSERT(newLength >= 0);
241 // Avoid modifications if pData points to const empty string:
242 if( newLength != pData->length )
244 if( newLength > nCapacity )
245 rtl_uStringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
246 else
247 pData->buffer[newLength] = 0;
248 pData->length = newLength;
253 Returns the character at a specific index in this string buffer.
255 The first character of a string buffer is at index
256 <code>0</code>, the next at index <code>1</code>, and so on, for
257 array indexing.
259 The index argument must be greater than or equal to
260 <code>0</code>, and less than the length of this string buffer.
262 @param index the index of the desired character.
263 @return the character at the specified index of this string buffer.
265 sal_Unicode charAt( sal_Int32 index ) const
267 OSL_ASSERT(index >= 0 && index < pData->length);
268 return pData->buffer[ index ];
272 Return a null terminated unicode character array.
274 operator const sal_Unicode *() const { return pData->buffer; }
277 Return a null terminated unicode character array.
279 const sal_Unicode* getStr() const { return pData->buffer; }
283 The character at the specified index of this string buffer is set
284 to <code>ch</code>.
286 The index argument must be greater than or equal to
287 <code>0</code>, and less than the length of this string buffer.
289 @param index the index of the character to modify.
290 @param ch the new character.
292 OUStringBuffer & setCharAt(sal_Int32 index, sal_Unicode ch)
294 OSL_ASSERT(index >= 0 && index < pData->length);
295 pData->buffer[ index ] = ch;
296 return *this;
300 Appends the string to this string buffer.
302 The characters of the <code>String</code> argument are appended, in
303 order, to the contents of this string buffer, increasing the
304 length of this string buffer by the length of the argument.
306 @param str a string.
307 @return this string buffer.
309 OUStringBuffer & append(const OUString &str)
311 return append( str.getStr(), str.getLength() );
315 Appends the string representation of the <code>char</code> array
316 argument to this string buffer.
318 The characters of the array argument are appended, in order, to
319 the contents of this string buffer. The length of this string
320 buffer increases by the length of the argument.
322 @param str the characters to be appended.
323 @return this string buffer.
325 OUStringBuffer & append( const sal_Unicode * str )
327 return append( str, rtl_ustr_getLength( str ) );
331 Appends the string representation of the <code>char</code> array
332 argument to this string buffer.
334 Characters of the character array <code>str</code> are appended,
335 in order, to the contents of this string buffer. The length of this
336 string buffer increases by the value of <code>len</code>.
338 @param str the characters to be appended; must be non-null, and must
339 point to at least len characters
340 @param len the number of characters to append; must be non-negative
341 @return this string buffer.
343 OUStringBuffer & append( const sal_Unicode * str, sal_Int32 len)
345 // insert behind the last character
346 rtl_uStringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
347 return *this;
351 Appends a 8-Bit ASCII character string to this string buffer.
353 Since this method is optimized for performance. the ASCII
354 character values are not converted in any way. The caller
355 has to make sure that all ASCII characters are in the
356 allowed range between 0 and 127. The ASCII string must be
357 NULL-terminated.
359 The characters of the array argument are appended, in order, to
360 the contents of this string buffer. The length of this string
361 buffer increases by the length of the argument.
363 @param str the 8-Bit ASCII characters to be appended.
364 @return this string buffer.
366 OUStringBuffer & appendAscii( const sal_Char * str )
368 return appendAscii( str, rtl_str_getLength( str ) );
372 Appends a 8-Bit ASCII character string to this string buffer.
374 Since this method is optimized for performance. the ASCII
375 character values are not converted in any way. The caller
376 has to make sure that all ASCII characters are in the
377 allowed range between 0 and 127. The ASCII string must be
378 NULL-terminated.
380 Characters of the character array <code>str</code> are appended,
381 in order, to the contents of this string buffer. The length of this
382 string buffer increases by the value of <code>len</code>.
384 @param str the 8-Bit ASCII characters to be appended; must be non-null,
385 and must point to at least len characters
386 @param len the number of characters to append; must be non-negative
387 @return this string buffer.
389 OUStringBuffer & appendAscii( const sal_Char * str, sal_Int32 len)
391 rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), str, len );
392 return *this;
397 Appends the string representation of the <code>sal_Bool</code>
398 argument to the string buffer.
400 The argument is converted to a string as if by the method
401 <code>String.valueOf</code>, and the characters of that
402 string are then appended to this string buffer.
404 @param b a <code>sal_Bool</code>.
405 @return this string buffer.
407 OUStringBuffer & append(sal_Bool b)
409 sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
410 return append( sz, rtl_ustr_valueOfBoolean( sz, b ) );
414 Appends the string representation of the <code>char</code>
415 argument to this string buffer.
417 The argument is appended to the contents of this string buffer.
418 The length of this string buffer increases by <code>1</code>.
420 @param ch a <code>char</code>.
421 @return this string buffer.
423 OUStringBuffer & append(sal_Unicode c)
425 return append( &c, 1 );
429 Appends the string representation of the <code>sal_Int32</code>
430 argument to this string buffer.
432 The argument is converted to a string as if by the method
433 <code>String.valueOf</code>, and the characters of that
434 string are then appended to this string buffer.
436 @param i an <code>sal_Int32</code>.
437 @return this string buffer.
439 OUStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
441 sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT32];
442 return append( sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
446 Appends the string representation of the <code>long</code>
447 argument to this string buffer.
449 The argument is converted to a string as if by the method
450 <code>String.valueOf</code>, and the characters of that
451 string are then appended to this string buffer.
453 @param l a <code>long</code>.
454 @return this string buffer.
456 OUStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
458 sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT64];
459 return append( sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
463 Appends the string representation of the <code>float</code>
464 argument to this string buffer.
466 The argument is converted to a string as if by the method
467 <code>String.valueOf</code>, and the characters of that
468 string are then appended to this string buffer.
470 @param f a <code>float</code>.
471 @return this string buffer.
473 OUStringBuffer & append(float f)
475 sal_Unicode sz[RTL_USTR_MAX_VALUEOFFLOAT];
476 return append( sz, rtl_ustr_valueOfFloat( sz, f ) );
480 Appends the string representation of the <code>double</code>
481 argument to this string buffer.
483 The argument is converted to a string as if by the method
484 <code>String.valueOf</code>, and the characters of that
485 string are then appended to this string buffer.
487 @param d a <code>double</code>.
488 @return this string buffer.
490 OUStringBuffer & append(double d)
492 sal_Unicode sz[RTL_USTR_MAX_VALUEOFDOUBLE];
493 return append( sz, rtl_ustr_valueOfDouble( sz, d ) );
497 Appends a single UTF-32 character to this string buffer.
499 <p>The single UTF-32 character will be represented within the string
500 buffer as either one or two UTF-16 code units.</p>
502 @param c a well-formed UTF-32 code unit (that is, a value in the range
503 <code>0</code>&ndash;<code>0x10FFFF</code>, but excluding
504 <code>0xD800</code>&ndash;<code>0xDFFF</code>)
506 @return
507 this string buffer
509 OUStringBuffer & appendUtf32(sal_uInt32 c) {
510 return insertUtf32(getLength(), c);
514 Inserts the string into this string buffer.
516 The characters of the <code>String</code> argument are inserted, in
517 order, into this string buffer at the indicated offset. The length
518 of this string buffer is increased by the length of the argument.
520 The offset argument must be greater than or equal to
521 <code>0</code>, and less than or equal to the length of this
522 string buffer.
524 @param offset the offset.
525 @param str a string.
526 @return this string buffer.
528 OUStringBuffer & insert(sal_Int32 offset, const OUString & str)
530 return insert( offset, str.getStr(), str.getLength() );
534 Inserts the string representation of the <code>char</code> array
535 argument into this string buffer.
537 The characters of the array argument are inserted into the
538 contents of this string buffer at the position indicated by
539 <code>offset</code>. The length of this string buffer increases by
540 the length of the argument.
542 The offset argument must be greater than or equal to
543 <code>0</code>, and less than or equal to the length of this
544 string buffer.
546 @param offset the offset.
547 @param ch a character array.
548 @return this string buffer.
550 OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str )
552 return insert( offset, str, rtl_ustr_getLength( str ) );
556 Inserts the string representation of the <code>char</code> array
557 argument into this string buffer.
559 The characters of the array argument are inserted into the
560 contents of this string buffer at the position indicated by
561 <code>offset</code>. The length of this string buffer increases by
562 the length of the argument.
564 The offset argument must be greater than or equal to
565 <code>0</code>, and less than or equal to the length of this
566 string buffer.
568 @param offset the offset.
569 @param ch a character array.
570 @param len the number of characters to append.
571 @return this string buffer.
573 OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str, sal_Int32 len)
575 // insert behind the last character
576 rtl_uStringbuffer_insert( &pData, &nCapacity, offset, str, len );
577 return *this;
581 Inserts the string representation of the <code>sal_Bool</code>
582 argument into this string buffer.
584 The second argument is converted to a string as if by the method
585 <code>String.valueOf</code>, and the characters of that
586 string are then inserted into this string buffer at the indicated
587 offset.
589 The offset argument must be greater than or equal to
590 <code>0</code>, and less than or equal to the length of this
591 string buffer.
593 @param offset the offset.
594 @param b a <code>sal_Bool</code>.
595 @return this string buffer.
597 OUStringBuffer & insert(sal_Int32 offset, sal_Bool b)
599 sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
600 return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
604 Inserts the string representation of the <code>char</code>
605 argument into this string buffer.
607 The second argument is inserted into the contents of this string
608 buffer at the position indicated by <code>offset</code>. The length
609 of this string buffer increases by one.
611 The offset argument must be greater than or equal to
612 <code>0</code>, and less than or equal to the length of this
613 string buffer.
615 @param offset the offset.
616 @param ch a <code>char</code>.
617 @return this string buffer.
619 OUStringBuffer & insert(sal_Int32 offset, sal_Unicode c)
621 return insert( offset, &c, 1 );
625 Inserts the string representation of the second <code>sal_Int32</code>
626 argument into this string buffer.
628 The second argument is converted to a string as if by the method
629 <code>String.valueOf</code>, and the characters of that
630 string are then inserted into this string buffer at the indicated
631 offset.
633 The offset argument must be greater than or equal to
634 <code>0</code>, and less than or equal to the length of this
635 string buffer.
637 @param offset the offset.
638 @param b an <code>sal_Int32</code>.
639 @return this string buffer.
640 @exception StringIndexOutOfBoundsException if the offset is invalid.
642 OUStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
644 sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT32];
645 return insert( offset, sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
649 Inserts the string representation of the <code>long</code>
650 argument into this string buffer.
652 The second argument is converted to a string as if by the method
653 <code>String.valueOf</code>, and the characters of that
654 string are then inserted into this string buffer at the indicated
655 offset.
657 The offset argument must be greater than or equal to
658 <code>0</code>, and less than or equal to the length of this
659 string buffer.
661 @param offset the offset.
662 @param b a <code>long</code>.
663 @return this string buffer.
664 @exception StringIndexOutOfBoundsException if the offset is invalid.
666 OUStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
668 sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT64];
669 return insert( offset, sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
673 Inserts the string representation of the <code>float</code>
674 argument into this string buffer.
676 The second argument is converted to a string as if by the method
677 <code>String.valueOf</code>, and the characters of that
678 string are then inserted into this string buffer at the indicated
679 offset.
681 The offset argument must be greater than or equal to
682 <code>0</code>, and less than or equal to the length of this
683 string buffer.
685 @param offset the offset.
686 @param b a <code>float</code>.
687 @return this string buffer.
688 @exception StringIndexOutOfBoundsException if the offset is invalid.
690 OUStringBuffer insert(sal_Int32 offset, float f)
692 sal_Unicode sz[RTL_USTR_MAX_VALUEOFFLOAT];
693 return insert( offset, sz, rtl_ustr_valueOfFloat( sz, f ) );
697 Inserts the string representation of the <code>double</code>
698 argument into this string buffer.
700 The second argument is converted to a string as if by the method
701 <code>String.valueOf</code>, and the characters of that
702 string are then inserted into this string buffer at the indicated
703 offset.
705 The offset argument must be greater than or equal to
706 <code>0</code>, and less than or equal to the length of this
707 string buffer.
709 @param offset the offset.
710 @param b a <code>double</code>.
711 @return this string buffer.
712 @exception StringIndexOutOfBoundsException if the offset is invalid.
714 OUStringBuffer & insert(sal_Int32 offset, double d)
716 sal_Unicode sz[RTL_USTR_MAX_VALUEOFDOUBLE];
717 return insert( offset, sz, rtl_ustr_valueOfDouble( sz, d ) );
721 Inserts a single UTF-32 character into this string buffer.
723 <p>The single UTF-32 character will be represented within the string
724 buffer as either one or two UTF-16 code units.</p>
726 @param offset the offset into this string buffer (from zero to the length
727 of this string buffer, inclusive)
729 @param c a well-formed UTF-32 code unit (that is, a value in the range
730 <code>0</code>&ndash;<code>0x10FFFF</code>, but excluding
731 <code>0xD800</code>&ndash;<code>0xDFFF</code>)
733 @return this string buffer
735 OUStringBuffer & insertUtf32(sal_Int32 offset, sal_uInt32 c) {
736 rtl_uStringbuffer_insertUtf32(&pData, &nCapacity, offset, c);
737 return *this;
740 /** Allows access to the internal data of this OUStringBuffer, for effective
741 manipulation.
743 This method should be used with care. After you have called this
744 method, you may use the returned pInternalData or pInternalCapacity only
745 as long as you make no other method call on this OUStringBuffer.
747 @param pInternalData
748 This output parameter receives a pointer to the internal data
749 (rtl_uString pointer). pInternalData itself must not be null.
751 @param pInternalCapacity
752 This output parameter receives a pointer to the internal capacity.
753 pInternalCapacity itself must not be null.
755 inline void accessInternals(rtl_uString *** pInternalData,
756 sal_Int32 ** pInternalCapacity)
758 *pInternalData = &pData;
759 *pInternalCapacity = &nCapacity;
762 private:
764 A pointer to the data structur which contains the data.
766 rtl_uString * pData;
769 The len of the pData->buffer.
771 sal_Int32 nCapacity;
776 #endif /* __cplusplus */
777 #endif /* _RTL_USTRBUF_HXX_ */