Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef _RTL_USTRBUF_HXX_
30 : : #define _RTL_USTRBUF_HXX_
31 : :
32 : : #include "sal/config.h"
33 : :
34 : : #include <cassert>
35 : :
36 : : #include <osl/diagnose.h>
37 : : #include <rtl/ustrbuf.h>
38 : : #include <rtl/ustring.hxx>
39 : : #include <rtl/stringutils.hxx>
40 : :
41 : : // The unittest uses slightly different code to help check that the proper
42 : : // calls are made. The class is put into a different namespace to make
43 : : // sure the compiler generates a different (if generating also non-inline)
44 : : // copy of the function and does not merge them together. The class
45 : : // is "brought" into the proper rtl namespace by a typedef below.
46 : : #ifdef RTL_STRING_UNITTEST
47 : : #define rtl rtlunittest
48 : : #endif
49 : :
50 : : namespace rtl
51 : : {
52 : :
53 : : #ifdef RTL_STRING_UNITTEST
54 : : #undef rtl
55 : : #endif
56 : :
57 : : /** A string buffer implements a mutable sequence of characters.
58 : : <p>
59 : : String buffers are safe for use by multiple threads. The methods
60 : : are synchronized where necessary so that all the operations on any
61 : : particular instance behave as if they occur in some serial order.
62 : : <p>
63 : : String buffers are used by the compiler to implement the binary
64 : : string concatenation operator <code>+</code>. For example, the code:
65 : : <p><blockquote><pre>
66 : : x = "a" + 4 + "c"
67 : : </pre></blockquote><p>
68 : : is compiled to the equivalent of:
69 : : <p><blockquote><pre>
70 : : x = new OUStringBuffer().append("a").append(4).append("c")
71 : : .makeStringAndClear()
72 : : </pre></blockquote><p>
73 : : The principal operations on a <code>OUStringBuffer</code> are the
74 : : <code>append</code> and <code>insert</code> methods, which are
75 : : overloaded so as to accept data of any type. Each effectively
76 : : converts a given datum to a string and then appends or inserts the
77 : : characters of that string to the string buffer. The
78 : : <code>append</code> method always adds these characters at the end
79 : : of the buffer; the <code>insert</code> method adds the characters at
80 : : a specified point.
81 : : <p>
82 : : For example, if <code>z</code> refers to a string buffer object
83 : : whose current contents are "<code>start</code>", then
84 : : the method call <code>z.append("le")</code> would cause the string
85 : : buffer to contain "<code>startle</code>", whereas
86 : : <code>z.insert(4, "le")</code> would alter the string buffer to
87 : : contain "<code>starlet</code>".
88 : : <p>
89 : : Every string buffer has a capacity. As long as the length of the
90 : : character sequence contained in the string buffer does not exceed
91 : : the capacity, it is not necessary to allocate a new internal
92 : : buffer array. If the internal buffer overflows, it is
93 : : automatically made larger.
94 : : */
95 : : class OUStringBuffer
96 : : {
97 : : public:
98 : : /**
99 : : Constructs a string buffer with no characters in it and an
100 : : initial capacity of 16 characters.
101 : : */
102 : 5039364 : OUStringBuffer()
103 : : : pData(NULL)
104 : 5039364 : , nCapacity( 16 )
105 : : {
106 : 5039364 : rtl_uString_new_WithLength( &pData, nCapacity );
107 : 5039364 : }
108 : :
109 : : /**
110 : : Allocates a new string buffer that contains the same sequence of
111 : : characters as the string buffer argument.
112 : :
113 : : @param value a <code>OUStringBuffer</code>.
114 : : */
115 : 43583 : OUStringBuffer( const OUStringBuffer & value )
116 : : : pData(NULL)
117 : 43583 : , nCapacity( value.nCapacity )
118 : : {
119 : 43583 : rtl_uStringbuffer_newFromStringBuffer( &pData, value.nCapacity, value.pData );
120 : 43583 : }
121 : :
122 : : /**
123 : : Constructs a string buffer with no characters in it and an
124 : : initial capacity specified by the <code>length</code> argument.
125 : :
126 : : @param length the initial capacity.
127 : : */
128 : 1859326 : explicit OUStringBuffer(int length)
129 : : : pData(NULL)
130 : 1859326 : , nCapacity( length )
131 : : {
132 : 1859326 : rtl_uString_new_WithLength( &pData, length );
133 : 1859326 : }
134 : :
135 : : /**
136 : : Constructs a string buffer so that it represents the same
137 : : sequence of characters as the string argument.
138 : :
139 : : The initial
140 : : capacity of the string buffer is <code>16</code> plus the length
141 : : of the string argument.
142 : :
143 : : @param value the initial contents of the buffer.
144 : : */
145 : 3210791 : OUStringBuffer(OUString value)
146 : : : pData(NULL)
147 : 3210791 : , nCapacity( value.getLength() + 16 )
148 : : {
149 : 3210791 : rtl_uStringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
150 : 3210791 : }
151 : :
152 : : #ifdef HAVE_SFINAE_ANONYMOUS_BROKEN // see OUString ctors
153 : : template< int N >
154 : : OUStringBuffer( const char (&literal)[ N ] )
155 : : : pData(NULL)
156 : : , nCapacity( N - 1 + 16 )
157 : : {
158 : : rtl_uString_newFromLiteral( &pData, literal, N - 1, 16 );
159 : : #ifdef RTL_STRING_UNITTEST
160 : : rtl_string_unittest_const_literal = true;
161 : : #endif
162 : : }
163 : :
164 : : /**
165 : : * It is an error to call this overload. Strings cannot directly use non-const char[].
166 : : * @internal
167 : : */
168 : : template< int N >
169 : : OUStringBuffer( char (&value)[ N ] )
170 : : #ifndef RTL_STRING_UNITTEST
171 : : ; // intentionally not implemented
172 : : #else
173 : : {
174 : : (void) value; // unused
175 : : pData = 0;
176 : : nCapacity = 10;
177 : : rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
178 : : rtl_string_unittest_invalid_conversion = true;
179 : : }
180 : : #endif
181 : : #else // HAVE_SFINAE_ANONYMOUS_BROKEN
182 : : template< typename T >
183 : 955 : OUStringBuffer( T& literal, typename internal::ConstCharArrayDetector< T, internal::Dummy >::Type = internal::Dummy() )
184 : : : pData(NULL)
185 : 955 : , nCapacity( internal::ConstCharArrayDetector< T, void >::size - 1 + 16 )
186 : : {
187 : 955 : rtl_uString_newFromLiteral( &pData, literal, internal::ConstCharArrayDetector< T, void >::size - 1, 16 );
188 : : #ifdef RTL_STRING_UNITTEST
189 : 15 : rtl_string_unittest_const_literal = true;
190 : : #endif
191 : 955 : }
192 : : #endif // HAVE_SFINAE_ANONYMOUS_BROKEN
193 : :
194 : : #ifdef RTL_STRING_UNITTEST
195 : : /**
196 : : * Only used by unittests to detect incorrect conversions.
197 : : * @internal
198 : : */
199 : : template< typename T >
200 : 30 : OUStringBuffer( T&, typename internal::ExceptConstCharArrayDetector< T >::Type = internal::Dummy() )
201 : : {
202 : 30 : pData = 0;
203 : 30 : nCapacity = 10;
204 : 30 : rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
205 : 30 : rtl_string_unittest_invalid_conversion = true;
206 : 30 : }
207 : : /**
208 : : * Only used by unittests to detect incorrect conversions.
209 : : * @internal
210 : : */
211 : : template< typename T >
212 : 5 : OUStringBuffer( const T&, typename internal::ExceptCharArrayDetector< T >::Type = internal::Dummy() )
213 : : {
214 : 5 : pData = 0;
215 : 5 : nCapacity = 10;
216 : 5 : rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
217 : 5 : rtl_string_unittest_invalid_conversion = true;
218 : 5 : }
219 : : #endif
220 : :
221 : : /** Assign to this a copy of value.
222 : : */
223 : 1814312 : OUStringBuffer& operator = ( const OUStringBuffer& value )
224 : : {
225 [ + - ]: 1814312 : if (this != &value)
226 : : {
227 : : rtl_uStringbuffer_newFromStringBuffer(&pData,
228 : : value.nCapacity,
229 : 1814312 : value.pData);
230 : 1814312 : nCapacity = value.nCapacity;
231 : : }
232 : 1814312 : return *this;
233 : : }
234 : :
235 : : /**
236 : : Release the string data.
237 : : */
238 : 10121510 : ~OUStringBuffer()
239 : : {
240 : 10121510 : rtl_uString_release( pData );
241 : 10121510 : }
242 : :
243 : : /**
244 : : Fill the string data in the new string and clear the buffer.
245 : :
246 : : This method is more efficient than the contructor of the string. It does
247 : : not copy the buffer.
248 : :
249 : : @return the string previously contained in the buffer.
250 : : */
251 : 8185916 : OUString makeStringAndClear()
252 : : {
253 : : return OUString(
254 : : rtl_uStringBuffer_makeStringAndClear( &pData, &nCapacity ),
255 : 8185916 : SAL_NO_ACQUIRE );
256 : : }
257 : :
258 : : /**
259 : : Returns the length (character count) of this string buffer.
260 : :
261 : : @return the number of characters in this string buffer.
262 : : */
263 : 55908006 : sal_Int32 getLength() const
264 : : {
265 : 55908006 : return pData->length;
266 : : }
267 : :
268 : : /**
269 : : Returns the current capacity of the String buffer.
270 : :
271 : : The capacity
272 : : is the amount of storage available for newly inserted
273 : : characters. The real buffer size is 2 bytes longer, because
274 : : all strings are 0 terminated.
275 : :
276 : : @return the current capacity of this string buffer.
277 : : */
278 : : sal_Int32 getCapacity() const
279 : : {
280 : : return nCapacity;
281 : : }
282 : :
283 : : /**
284 : : Ensures that the capacity of the buffer is at least equal to the
285 : : specified minimum.
286 : :
287 : : The new capacity will be at least as large as the maximum of the current
288 : : length (so that no contents of the buffer is destroyed) and the given
289 : : minimumCapacity. If the given minimumCapacity is negative, nothing is
290 : : changed.
291 : :
292 : : @param minimumCapacity the minimum desired capacity.
293 : : */
294 : 520305 : void ensureCapacity(sal_Int32 minimumCapacity)
295 : : {
296 : 520305 : rtl_uStringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
297 : 520305 : }
298 : :
299 : : /**
300 : : Sets the length of this String buffer.
301 : :
302 : : If the <code>newLength</code> argument is less than the current
303 : : length of the string buffer, the string buffer is truncated to
304 : : contain exactly the number of characters given by the
305 : : <code>newLength</code> argument.
306 : : <p>
307 : : If the <code>newLength</code> argument is greater than or equal
308 : : to the current length, sufficient null characters
309 : : (<code>'\u0000'</code>) are appended to the string buffer so that
310 : : length becomes the <code>newLength</code> argument.
311 : : <p>
312 : : The <code>newLength</code> argument must be greater than or equal
313 : : to <code>0</code>.
314 : :
315 : : @param newLength the new length of the buffer.
316 : : */
317 : 873943 : void setLength(sal_Int32 newLength)
318 : : {
319 : : assert(newLength >= 0);
320 : : // Avoid modifications if pData points to const empty string:
321 [ + + ]: 873943 : if( newLength != pData->length )
322 : : {
323 [ + + ]: 1269 : if( newLength > nCapacity )
324 : 28 : rtl_uStringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
325 : : else
326 : 1241 : pData->buffer[newLength] = 0;
327 : 1269 : pData->length = newLength;
328 : : }
329 : 873943 : }
330 : :
331 : : /**
332 : : Returns the character at a specific index in this string buffer.
333 : :
334 : : The first character of a string buffer is at index
335 : : <code>0</code>, the next at index <code>1</code>, and so on, for
336 : : array indexing.
337 : : <p>
338 : : The index argument must be greater than or equal to
339 : : <code>0</code>, and less than the length of this string buffer.
340 : :
341 : : @param index the index of the desired character.
342 : : @return the character at the specified index of this string buffer.
343 : : */
344 : : SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
345 : : sal_Unicode charAt( sal_Int32 index ) const
346 : : {
347 : : assert(index >= 0 && index < pData->length);
348 : : return pData->buffer[ index ];
349 : : }
350 : :
351 : : /**
352 : : The character at the specified index of this string buffer is set
353 : : to <code>ch</code>.
354 : :
355 : : The index argument must be greater than or equal to
356 : : <code>0</code>, and less than the length of this string buffer.
357 : :
358 : : @param index the index of the character to modify.
359 : : @param ch the new character.
360 : : */
361 : : SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
362 : : OUStringBuffer & setCharAt(sal_Int32 index, sal_Unicode ch)
363 : : {
364 : : assert(index >= 0 && index < pData->length);
365 : : pData->buffer[ index ] = ch;
366 : : return *this;
367 : : }
368 : :
369 : : /**
370 : : Return a null terminated unicode character array.
371 : : */
372 : 4014694 : const sal_Unicode* getStr() const { return pData->buffer; }
373 : :
374 : : /**
375 : : Access to individual characters.
376 : :
377 : : @param index must be non-negative and less than length.
378 : :
379 : : @return a reference to the character at the given index.
380 : :
381 : : @since LibreOffice 3.5
382 : : */
383 : 1682374 : sal_Unicode & operator [](sal_Int32 index) { return pData->buffer[index]; }
384 : :
385 : : /**
386 : : Return a OUString instance reflecting the current content
387 : : of this OUStringBuffer.
388 : : */
389 : 261 : const OUString toString() const
390 : : {
391 : 261 : return OUString(pData->buffer, pData->length);
392 : : }
393 : :
394 : : /**
395 : : Appends the string to this string buffer.
396 : :
397 : : The characters of the <code>String</code> argument are appended, in
398 : : order, to the contents of this string buffer, increasing the
399 : : length of this string buffer by the length of the argument.
400 : :
401 : : @param str a string.
402 : : @return this string buffer.
403 : : */
404 : 7007130 : OUStringBuffer & append(const OUString &str)
405 : : {
406 : 7007130 : return append( str.getStr(), str.getLength() );
407 : : }
408 : :
409 : : /**
410 : : Appends the string representation of the <code>char</code> array
411 : : argument to this string buffer.
412 : :
413 : : The characters of the array argument are appended, in order, to
414 : : the contents of this string buffer. The length of this string
415 : : buffer increases by the length of the argument.
416 : :
417 : : @param str the characters to be appended.
418 : : @return this string buffer.
419 : : */
420 : 13861 : OUStringBuffer & append( const sal_Unicode * str )
421 : : {
422 : 13861 : return append( str, rtl_ustr_getLength( str ) );
423 : : }
424 : :
425 : : /**
426 : : Appends the string representation of the <code>char</code> array
427 : : argument to this string buffer.
428 : :
429 : : Characters of the character array <code>str</code> are appended,
430 : : in order, to the contents of this string buffer. The length of this
431 : : string buffer increases by the value of <code>len</code>.
432 : :
433 : : @param str the characters to be appended; must be non-null, and must
434 : : point to at least len characters
435 : : @param len the number of characters to append; must be non-negative
436 : : @return this string buffer.
437 : : */
438 : 47183071 : OUStringBuffer & append( const sal_Unicode * str, sal_Int32 len)
439 : : {
440 : : // insert behind the last character
441 : 47183071 : rtl_uStringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
442 : 47183071 : return *this;
443 : : }
444 : :
445 : : /**
446 : : @overload
447 : : This function accepts an ASCII string literal as its argument.
448 : : @since LibreOffice 3.6
449 : : */
450 : : template< typename T >
451 : 691 : typename internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type append( T& literal )
452 : : {
453 : 691 : rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), literal,
454 : : internal::ConstCharArrayDetector< T, void >::size - 1 );
455 : 691 : return *this;
456 : : }
457 : :
458 : : /**
459 : : Appends a 8-Bit ASCII character string to this string buffer.
460 : :
461 : : Since this method is optimized for performance. the ASCII
462 : : character values are not converted in any way. The caller
463 : : has to make sure that all ASCII characters are in the
464 : : allowed range between 0 and 127. The ASCII string must be
465 : : NULL-terminated.
466 : : <p>
467 : : The characters of the array argument are appended, in order, to
468 : : the contents of this string buffer. The length of this string
469 : : buffer increases by the length of the argument.
470 : :
471 : : @param str the 8-Bit ASCII characters to be appended.
472 : : @return this string buffer.
473 : : */
474 : 2104383 : OUStringBuffer & appendAscii( const sal_Char * str )
475 : : {
476 : 2104383 : return appendAscii( str, rtl_str_getLength( str ) );
477 : : }
478 : :
479 : : /**
480 : : Appends a 8-Bit ASCII character string to this string buffer.
481 : :
482 : : Since this method is optimized for performance. the ASCII
483 : : character values are not converted in any way. The caller
484 : : has to make sure that all ASCII characters are in the
485 : : allowed range between 0 and 127. The ASCII string must be
486 : : NULL-terminated.
487 : : <p>
488 : : Characters of the character array <code>str</code> are appended,
489 : : in order, to the contents of this string buffer. The length of this
490 : : string buffer increases by the value of <code>len</code>.
491 : :
492 : : @param str the 8-Bit ASCII characters to be appended; must be non-null,
493 : : and must point to at least len characters
494 : : @param len the number of characters to append; must be non-negative
495 : : @return this string buffer.
496 : : */
497 : 3368645 : OUStringBuffer & appendAscii( const sal_Char * str, sal_Int32 len)
498 : : {
499 : 3368645 : rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), str, len );
500 : 3368645 : return *this;
501 : : }
502 : :
503 : : /**
504 : : Appends the string representation of the <code>sal_Bool</code>
505 : : argument to the string buffer.
506 : :
507 : : The argument is converted to a string as if by the method
508 : : <code>String.valueOf</code>, and the characters of that
509 : : string are then appended to this string buffer.
510 : :
511 : : @param b a <code>sal_Bool</code>.
512 : : @return this string buffer.
513 : : */
514 : : OUStringBuffer & append(sal_Bool b)
515 : : {
516 : : sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
517 : : return append( sz, rtl_ustr_valueOfBoolean( sz, b ) );
518 : : }
519 : :
520 : : /**
521 : : Appends the string representation of the ASCII <code>char</code>
522 : : argument to this string buffer.
523 : :
524 : : The argument is appended to the contents of this string buffer.
525 : : The length of this string buffer increases by <code>1</code>.
526 : :
527 : : @param c an ASCII <code>char</code>.
528 : : @return this string buffer.
529 : :
530 : : @since LibreOffice 3.5
531 : : */
532 : 37745 : OUStringBuffer & append(char c)
533 : : {
534 : : assert(static_cast< unsigned char >(c) <= 0x7F);
535 : 37745 : return append(sal_Unicode(c));
536 : : }
537 : :
538 : : /**
539 : : Appends the string representation of the <code>char</code>
540 : : argument to this string buffer.
541 : :
542 : : The argument is appended to the contents of this string buffer.
543 : : The length of this string buffer increases by <code>1</code>.
544 : :
545 : : @param c a <code>char</code>.
546 : : @return this string buffer.
547 : : */
548 : 36929516 : OUStringBuffer & append(sal_Unicode c)
549 : : {
550 : 36929516 : return append( &c, 1 );
551 : : }
552 : :
553 : : /**
554 : : Appends the string representation of the <code>sal_Int32</code>
555 : : argument to this string buffer.
556 : :
557 : : The argument is converted to a string as if by the method
558 : : <code>String.valueOf</code>, and the characters of that
559 : : string are then appended to this string buffer.
560 : :
561 : : @param i an <code>sal_Int32</code>.
562 : : @return this string buffer.
563 : : */
564 : 1859209 : OUStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
565 : : {
566 : : sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT32];
567 [ + - ]: 1859209 : return append( sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
568 : : }
569 : :
570 : : /**
571 : : Appends the string representation of the <code>long</code>
572 : : argument to this string buffer.
573 : :
574 : : The argument is converted to a string as if by the method
575 : : <code>String.valueOf</code>, and the characters of that
576 : : string are then appended to this string buffer.
577 : :
578 : : @param l a <code>long</code>.
579 : : @return this string buffer.
580 : : */
581 : 40449 : OUStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
582 : : {
583 : : sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT64];
584 [ + - ]: 40449 : return append( sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
585 : : }
586 : :
587 : : /**
588 : : Appends the string representation of the <code>float</code>
589 : : argument to this string buffer.
590 : :
591 : : The argument is converted to a string as if by the method
592 : : <code>String.valueOf</code>, and the characters of that
593 : : string are then appended to this string buffer.
594 : :
595 : : @param f a <code>float</code>.
596 : : @return this string buffer.
597 : : */
598 : 0 : OUStringBuffer & append(float f)
599 : : {
600 : : sal_Unicode sz[RTL_USTR_MAX_VALUEOFFLOAT];
601 [ # # ]: 0 : return append( sz, rtl_ustr_valueOfFloat( sz, f ) );
602 : : }
603 : :
604 : : /**
605 : : Appends the string representation of the <code>double</code>
606 : : argument to this string buffer.
607 : :
608 : : The argument is converted to a string as if by the method
609 : : <code>String.valueOf</code>, and the characters of that
610 : : string are then appended to this string buffer.
611 : :
612 : : @param d a <code>double</code>.
613 : : @return this string buffer.
614 : : */
615 : 15235 : OUStringBuffer & append(double d)
616 : : {
617 : : sal_Unicode sz[RTL_USTR_MAX_VALUEOFDOUBLE];
618 [ + - ]: 15235 : return append( sz, rtl_ustr_valueOfDouble( sz, d ) );
619 : : }
620 : :
621 : : /**
622 : : Appends a single UTF-32 character to this string buffer.
623 : :
624 : : <p>The single UTF-32 character will be represented within the string
625 : : buffer as either one or two UTF-16 code units.</p>
626 : :
627 : : @param c a well-formed UTF-32 code unit (that is, a value in the range
628 : : <code>0</code>–<code>0x10FFFF</code>, but excluding
629 : : <code>0xD800</code>–<code>0xDFFF</code>)
630 : :
631 : : @return
632 : : this string buffer
633 : : */
634 : 25 : OUStringBuffer & appendUtf32(sal_uInt32 c) {
635 : 25 : return insertUtf32(getLength(), c);
636 : : }
637 : :
638 : : /**
639 : : Inserts the string into this string buffer.
640 : :
641 : : The characters of the <code>String</code> argument are inserted, in
642 : : order, into this string buffer at the indicated offset. The length
643 : : of this string buffer is increased by the length of the argument.
644 : : <p>
645 : : The offset argument must be greater than or equal to
646 : : <code>0</code>, and less than or equal to the length of this
647 : : string buffer.
648 : :
649 : : @param offset the offset.
650 : : @param str a string.
651 : : @return this string buffer.
652 : : */
653 : 945 : OUStringBuffer & insert(sal_Int32 offset, const OUString & str)
654 : : {
655 : 945 : return insert( offset, str.getStr(), str.getLength() );
656 : : }
657 : :
658 : : /**
659 : : Inserts the string representation of the <code>char</code> array
660 : : argument into this string buffer.
661 : :
662 : : The characters of the array argument are inserted into the
663 : : contents of this string buffer at the position indicated by
664 : : <code>offset</code>. The length of this string buffer increases by
665 : : the length of the argument.
666 : : <p>
667 : : The offset argument must be greater than or equal to
668 : : <code>0</code>, and less than or equal to the length of this
669 : : string buffer.
670 : :
671 : : @param offset the offset.
672 : : @param str a character array.
673 : : @return this string buffer.
674 : : */
675 : 0 : OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str )
676 : : {
677 : 0 : return insert( offset, str, rtl_ustr_getLength( str ) );
678 : : }
679 : :
680 : : /**
681 : : Inserts the string representation of the <code>char</code> array
682 : : argument into this string buffer.
683 : :
684 : : The characters of the array argument are inserted into the
685 : : contents of this string buffer at the position indicated by
686 : : <code>offset</code>. The length of this string buffer increases by
687 : : the length of the argument.
688 : : <p>
689 : : The offset argument must be greater than or equal to
690 : : <code>0</code>, and less than or equal to the length of this
691 : : string buffer.
692 : :
693 : : @param offset the offset.
694 : : @param str a character array.
695 : : @param len the number of characters to append.
696 : : @return this string buffer.
697 : : */
698 : 3924 : OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str, sal_Int32 len)
699 : : {
700 : : // insert behind the last character
701 : 3924 : rtl_uStringbuffer_insert( &pData, &nCapacity, offset, str, len );
702 : 3924 : return *this;
703 : : }
704 : :
705 : : /**
706 : : @overload
707 : : This function accepts an ASCII string literal as its argument.
708 : : @since LibreOffice 3.6
709 : : */
710 : : template< typename T >
711 : 5 : typename internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type insert( sal_Int32 offset, T& literal )
712 : : {
713 : 5 : rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, offset, literal,
714 : : internal::ConstCharArrayDetector< T, void >::size - 1 );
715 : 5 : return *this;
716 : : }
717 : :
718 : : /**
719 : : Inserts the string representation of the <code>sal_Bool</code>
720 : : argument into this string buffer.
721 : :
722 : : The second argument is converted to a string as if by the method
723 : : <code>String.valueOf</code>, and the characters of that
724 : : string are then inserted into this string buffer at the indicated
725 : : offset.
726 : : <p>
727 : : The offset argument must be greater than or equal to
728 : : <code>0</code>, and less than or equal to the length of this
729 : : string buffer.
730 : :
731 : : @param offset the offset.
732 : : @param b a <code>sal_Bool</code>.
733 : : @return this string buffer.
734 : : */
735 : : OUStringBuffer & insert(sal_Int32 offset, sal_Bool b)
736 : : {
737 : : sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
738 : : return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
739 : : }
740 : :
741 : : /**
742 : : Inserts the string representation of the <code>char</code>
743 : : argument into this string buffer.
744 : :
745 : : The second argument is inserted into the contents of this string
746 : : buffer at the position indicated by <code>offset</code>. The length
747 : : of this string buffer increases by one.
748 : : <p>
749 : : The offset argument must be greater than or equal to
750 : : <code>0</code>, and less than or equal to the length of this
751 : : string buffer.
752 : :
753 : : @param offset the offset.
754 : : @param c a <code>char</code>.
755 : : @return this string buffer.
756 : :
757 : : @since LibreOffice 3.6
758 : : */
759 : : OUStringBuffer & insert(sal_Int32 offset, char c)
760 : : {
761 : : sal_Unicode u = c;
762 : : return insert( offset, &u, 1 );
763 : : }
764 : :
765 : : /**
766 : : Inserts the string representation of the <code>char</code>
767 : : argument into this string buffer.
768 : :
769 : : The second argument is inserted into the contents of this string
770 : : buffer at the position indicated by <code>offset</code>. The length
771 : : of this string buffer increases by one.
772 : : <p>
773 : : The offset argument must be greater than or equal to
774 : : <code>0</code>, and less than or equal to the length of this
775 : : string buffer.
776 : :
777 : : @param offset the offset.
778 : : @param c a <code>char</code>.
779 : : @return this string buffer.
780 : : */
781 : 2879 : OUStringBuffer & insert(sal_Int32 offset, sal_Unicode c)
782 : : {
783 : 2879 : return insert( offset, &c, 1 );
784 : : }
785 : :
786 : : /**
787 : : Inserts the string representation of the second <code>sal_Int32</code>
788 : : argument into this string buffer.
789 : :
790 : : The second argument is converted to a string as if by the method
791 : : <code>String.valueOf</code>, and the characters of that
792 : : string are then inserted into this string buffer at the indicated
793 : : offset.
794 : : <p>
795 : : The offset argument must be greater than or equal to
796 : : <code>0</code>, and less than or equal to the length of this
797 : : string buffer.
798 : :
799 : : @param offset the offset.
800 : : @param i an <code>sal_Int32</code>.
801 : : @param radix the radix.
802 : : @return this string buffer.
803 : : @exception StringIndexOutOfBoundsException if the offset is invalid.
804 : : */
805 : 0 : OUStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
806 : : {
807 : : sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT32];
808 [ # # ]: 0 : return insert( offset, sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
809 : : }
810 : :
811 : : /**
812 : : Inserts the string representation of the <code>long</code>
813 : : argument into this string buffer.
814 : :
815 : : The second argument is converted to a string as if by the method
816 : : <code>String.valueOf</code>, and the characters of that
817 : : string are then inserted into this string buffer at the indicated
818 : : offset.
819 : : <p>
820 : : The offset argument must be greater than or equal to
821 : : <code>0</code>, and less than or equal to the length of this
822 : : string buffer.
823 : :
824 : : @param offset the offset.
825 : : @param l a <code>long</code>.
826 : : @param radix the radix.
827 : : @return this string buffer.
828 : : @exception StringIndexOutOfBoundsException if the offset is invalid.
829 : : */
830 : : OUStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
831 : : {
832 : : sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT64];
833 : : return insert( offset, sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
834 : : }
835 : :
836 : : /**
837 : : Inserts the string representation of the <code>float</code>
838 : : argument into this string buffer.
839 : :
840 : : The second argument is converted to a string as if by the method
841 : : <code>String.valueOf</code>, and the characters of that
842 : : string are then inserted into this string buffer at the indicated
843 : : offset.
844 : : <p>
845 : : The offset argument must be greater than or equal to
846 : : <code>0</code>, and less than or equal to the length of this
847 : : string buffer.
848 : :
849 : : @param offset the offset.
850 : : @param f a <code>float</code>.
851 : : @return this string buffer.
852 : : @exception StringIndexOutOfBoundsException if the offset is invalid.
853 : : */
854 : : OUStringBuffer insert(sal_Int32 offset, float f)
855 : : {
856 : : sal_Unicode sz[RTL_USTR_MAX_VALUEOFFLOAT];
857 : : return insert( offset, sz, rtl_ustr_valueOfFloat( sz, f ) );
858 : : }
859 : :
860 : : /**
861 : : Inserts the string representation of the <code>double</code>
862 : : argument into this string buffer.
863 : :
864 : : The second argument is converted to a string as if by the method
865 : : <code>String.valueOf</code>, and the characters of that
866 : : string are then inserted into this string buffer at the indicated
867 : : offset.
868 : : <p>
869 : : The offset argument must be greater than or equal to
870 : : <code>0</code>, and less than or equal to the length of this
871 : : string buffer.
872 : :
873 : : @param offset the offset.
874 : : @param d a <code>double</code>.
875 : : @return this string buffer.
876 : : @exception StringIndexOutOfBoundsException if the offset is invalid.
877 : : */
878 : : OUStringBuffer & insert(sal_Int32 offset, double d)
879 : : {
880 : : sal_Unicode sz[RTL_USTR_MAX_VALUEOFDOUBLE];
881 : : return insert( offset, sz, rtl_ustr_valueOfDouble( sz, d ) );
882 : : }
883 : :
884 : : /**
885 : : Inserts a single UTF-32 character into this string buffer.
886 : :
887 : : <p>The single UTF-32 character will be represented within the string
888 : : buffer as either one or two UTF-16 code units.</p>
889 : :
890 : : @param offset the offset into this string buffer (from zero to the length
891 : : of this string buffer, inclusive)
892 : :
893 : : @param c a well-formed UTF-32 code unit (that is, a value in the range
894 : : <code>0</code>–<code>0x10FFFF</code>, but excluding
895 : : <code>0xD800</code>–<code>0xDFFF</code>)
896 : :
897 : : @return this string buffer
898 : : */
899 : 35 : OUStringBuffer & insertUtf32(sal_Int32 offset, sal_uInt32 c) {
900 : 35 : rtl_uStringbuffer_insertUtf32(&pData, &nCapacity, offset, c);
901 : 35 : return *this;
902 : : }
903 : :
904 : : /**
905 : : Removes the characters in a substring of this sequence.
906 : :
907 : : The substring begins at the specified <code>start</code> and
908 : : is <code>len</code> characters long.
909 : :
910 : : start must be >= 0 && <= This->length
911 : :
912 : : @param start The beginning index, inclusive
913 : : @param len The substring length
914 : : @return this string buffer.
915 : : */
916 : 42 : OUStringBuffer & remove( sal_Int32 start, sal_Int32 len )
917 : : {
918 : 42 : rtl_uStringbuffer_remove( &pData, start, len );
919 : 42 : return *this;
920 : : }
921 : :
922 : : /** Allows access to the internal data of this OUStringBuffer, for effective
923 : : manipulation.
924 : :
925 : : This method should be used with care. After you have called this
926 : : method, you may use the returned pInternalData or pInternalCapacity only
927 : : as long as you make no other method call on this OUStringBuffer.
928 : :
929 : : @param pInternalData
930 : : This output parameter receives a pointer to the internal data
931 : : (rtl_uString pointer). pInternalData itself must not be null.
932 : :
933 : : @param pInternalCapacity
934 : : This output parameter receives a pointer to the internal capacity.
935 : : pInternalCapacity itself must not be null.
936 : : */
937 : 1283 : inline void accessInternals(rtl_uString *** pInternalData,
938 : : sal_Int32 ** pInternalCapacity)
939 : : {
940 : 1283 : *pInternalData = &pData;
941 : 1283 : *pInternalCapacity = &nCapacity;
942 : 1283 : }
943 : :
944 : : private:
945 : : /**
946 : : A pointer to the data structur which contains the data.
947 : : */
948 : : rtl_uString * pData;
949 : :
950 : : /**
951 : : The len of the pData->buffer.
952 : : */
953 : : sal_Int32 nCapacity;
954 : : };
955 : :
956 : : }
957 : :
958 : : #ifdef RTL_STRING_UNITTEST
959 : : namespace rtl
960 : : {
961 : : typedef rtlunittest::OUStringBuffer OUStringBuffer;
962 : : }
963 : : #endif
964 : :
965 : : #ifdef RTL_USING
966 : : using ::rtl::OUStringBuffer;
967 : : #endif
968 : :
969 : : #endif /* _RTL_USTRBUF_HXX_ */
970 : :
971 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|