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_STRBUF_HXX_
30 : : #define _RTL_STRBUF_HXX_
31 : :
32 : : #include "sal/config.h"
33 : :
34 : : #include <cassert>
35 : :
36 : : #include <rtl/strbuf.h>
37 : : #include <rtl/string.hxx>
38 : : #include <rtl/stringutils.hxx>
39 : :
40 : : #ifdef __cplusplus
41 : :
42 : : // The unittest uses slightly different code to help check that the proper
43 : : // calls are made. The class is put into a different namespace to make
44 : : // sure the compiler generates a different (if generating also non-inline)
45 : : // copy of the function and does not merge them together. The class
46 : : // is "brought" into the proper rtl namespace by a typedef below.
47 : : #ifdef RTL_STRING_UNITTEST
48 : : #define rtl rtlunittest
49 : : #endif
50 : :
51 : : namespace rtl
52 : : {
53 : :
54 : : #ifdef RTL_STRING_UNITTEST
55 : : #undef rtl
56 : : // helper macro to make functions appear more readable
57 : : #define RTL_STRING_CONST_FUNCTION rtl_string_unittest_const_literal_function = true;
58 : : #else
59 : : #define RTL_STRING_CONST_FUNCTION
60 : : #endif
61 : :
62 : : /** A string buffer implements a mutable sequence of characters.
63 : : <p>
64 : : String buffers are safe for use by multiple threads. The methods
65 : : are synchronized where necessary so that all the operations on any
66 : : particular instance behave as if they occur in some serial order.
67 : : <p>
68 : : String buffers are used by the compiler to implement the binary
69 : : string concatenation operator <code>+</code>. For example, the code:
70 : : <p><blockquote><pre>
71 : : x = "a" + 4 + "c"
72 : : </pre></blockquote><p>
73 : : is compiled to the equivalent of:
74 : : <p><blockquote><pre>
75 : : x = new OStringBuffer().append("a").append(4).append("c")
76 : : .makeStringAndClear()
77 : : </pre></blockquote><p>
78 : : The principal operations on a <code>OStringBuffer</code> are the
79 : : <code>append</code> and <code>insert</code> methods, which are
80 : : overloaded so as to accept data of any type. Each effectively
81 : : converts a given datum to a string and then appends or inserts the
82 : : characters of that string to the string buffer. The
83 : : <code>append</code> method always adds these characters at the end
84 : : of the buffer; the <code>insert</code> method adds the characters at
85 : : a specified point.
86 : : <p>
87 : : For example, if <code>z</code> refers to a string buffer object
88 : : whose current contents are "<code>start</code>", then
89 : : the method call <code>z.append("le")</code> would cause the string
90 : : buffer to contain "<code>startle</code>", whereas
91 : : <code>z.insert(4, "le")</code> would alter the string buffer to
92 : : contain "<code>starlet</code>".
93 : : <p>
94 : : Every string buffer has a capacity. As long as the length of the
95 : : character sequence contained in the string buffer does not exceed
96 : : the capacity, it is not necessary to allocate a new internal
97 : : buffer array. If the internal buffer overflows, it is
98 : : automatically made larger.
99 : : */
100 : : class OStringBuffer
101 : : {
102 : : public:
103 : : /**
104 : : Constructs a string buffer with no characters in it and an
105 : : initial capacity of 16 characters.
106 : : */
107 : 2259931 : OStringBuffer()
108 : : : pData(NULL)
109 : 2259931 : , nCapacity( 16 )
110 : : {
111 : 2259931 : rtl_string_new_WithLength( &pData, nCapacity );
112 : 2259931 : }
113 : :
114 : : /**
115 : : Allocates a new string buffer that contains the same sequence of
116 : : characters as the string buffer argument.
117 : :
118 : : @param value a <code>OStringBuffer</code>.
119 : : */
120 : 17519 : OStringBuffer( const OStringBuffer & value )
121 : : : pData(NULL)
122 : 17519 : , nCapacity( value.nCapacity )
123 : : {
124 : 17519 : rtl_stringbuffer_newFromStringBuffer( &pData, value.nCapacity, value.pData );
125 : 17519 : }
126 : :
127 : : /**
128 : : Constructs a string buffer with no characters in it and an
129 : : initial capacity specified by the <code>length</code> argument.
130 : :
131 : : @param length the initial capacity.
132 : : */
133 : 4323425 : explicit OStringBuffer(int length)
134 : : : pData(NULL)
135 : 4323425 : , nCapacity( length )
136 : : {
137 : 4323425 : rtl_string_new_WithLength( &pData, length );
138 : 4323425 : }
139 : :
140 : : /**
141 : : Constructs a string buffer so that it represents the same
142 : : sequence of characters as the string argument.
143 : :
144 : : The initial
145 : : capacity of the string buffer is <code>16</code> plus the length
146 : : of the string argument.
147 : :
148 : : @param value the initial string value.
149 : : */
150 : 121338 : OStringBuffer(OString value)
151 : : : pData(NULL)
152 : 121338 : , nCapacity( value.getLength() + 16 )
153 : : {
154 : 121338 : rtl_stringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
155 : 121338 : }
156 : :
157 : : /**
158 : : @overload
159 : : @since LibreOffice 3.6
160 : : */
161 : : #ifdef HAVE_SFINAE_ANONYMOUS_BROKEN // see the OString ctors
162 : : OStringBuffer( const char* value )
163 : : : pData(NULL)
164 : : {
165 : : sal_Int32 length = rtl_str_getLength( value );
166 : : nCapacity = length + 16;
167 : : rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
168 : : }
169 : : #else
170 : : template< typename T >
171 : 40 : OStringBuffer( const T& value, typename internal::CharPtrDetector< T, internal::Dummy >::Type = internal::Dummy())
172 : 40 : : pData(NULL)
173 : : {
174 : 40 : sal_Int32 length = rtl_str_getLength( value );
175 : 40 : nCapacity = length + 16;
176 : 40 : rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
177 : 40 : }
178 : :
179 : : template< typename T >
180 : 15 : OStringBuffer( T& value, typename internal::NonConstCharArrayDetector< T, internal::Dummy >::Type = internal::Dummy())
181 : 15 : : pData(NULL)
182 : : {
183 : 15 : sal_Int32 length = rtl_str_getLength( value );
184 : 15 : nCapacity = length + 16;
185 : 15 : rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
186 : 15 : }
187 : :
188 : : /**
189 : : Constructs a string buffer so that it represents the same
190 : : sequence of characters as the string literal.
191 : :
192 : : If there are any embedded \0's in the string literal, the result is undefined.
193 : : Use the overload that explicitly accepts length.
194 : :
195 : : @since LibreOffice 3.6
196 : :
197 : : @param literal a string literal
198 : : */
199 : : template< typename T >
200 : 10909 : OStringBuffer( T& literal, typename internal::ConstCharArrayDetector< T, internal::Dummy >::Type = internal::Dummy())
201 : : : pData(NULL)
202 : 10909 : , nCapacity( internal::ConstCharArrayDetector< T, void >::size - 1 + 16 )
203 : : {
204 : 10909 : rtl_string_newFromLiteral( &pData, literal, internal::ConstCharArrayDetector< T, void >::size - 1, 16 );
205 : : #ifdef RTL_STRING_UNITTEST
206 : 20 : rtl_string_unittest_const_literal = true;
207 : : #endif
208 : 10909 : }
209 : : #endif // HAVE_SFINAE_ANONYMOUS_BROKEN
210 : :
211 : : /**
212 : : Constructs a string buffer so that it represents the same
213 : : sequence of characters as the string argument.
214 : :
215 : : The initial
216 : : capacity of the string buffer is <code>16</code> plus length
217 : :
218 : : @param value a character array.
219 : : @param length the number of character which should be copied.
220 : : The character array length must be greater or
221 : : equal than this value.
222 : : */
223 : 71406 : OStringBuffer(const sal_Char * value, sal_Int32 length)
224 : : : pData(NULL)
225 : 71406 : , nCapacity( length + 16 )
226 : : {
227 : 71406 : rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
228 : 71406 : }
229 : :
230 : : /** Assign to this a copy of value.
231 : : */
232 : 0 : OStringBuffer& operator = ( const OStringBuffer& value )
233 : : {
234 [ # # ]: 0 : if (this != &value)
235 : : {
236 : : rtl_stringbuffer_newFromStringBuffer(&pData,
237 : : value.nCapacity,
238 : 0 : value.pData);
239 : 0 : nCapacity = value.nCapacity;
240 : : }
241 : 0 : return *this;
242 : : }
243 : :
244 : : /**
245 : : Release the string data.
246 : : */
247 : 6273685 : ~OStringBuffer()
248 : : {
249 : 6273685 : rtl_string_release( pData );
250 : 6273685 : }
251 : :
252 : : /**
253 : : Fill the string data in the new string and clear the buffer.
254 : :
255 : : This method is more efficient than the contructor of the string. It does
256 : : not copy the buffer.
257 : :
258 : : @return the string previously contained in the buffer.
259 : : */
260 : 4716380 : OString makeStringAndClear()
261 : : {
262 : 4716380 : OString aRet( pData );
263 : 4716380 : rtl_string_new(&pData);
264 : 4716380 : nCapacity = 0;
265 : 4716380 : return aRet;
266 : : }
267 : :
268 : : /**
269 : : Returns the length (character count) of this string buffer.
270 : :
271 : : @return the number of characters in this string buffer.
272 : : */
273 : 78834207 : sal_Int32 getLength() const
274 : : {
275 : 78834207 : return pData->length;
276 : : }
277 : :
278 : : /**
279 : : Returns the current capacity of the String buffer.
280 : :
281 : : The capacity
282 : : is the amount of storage available for newly inserted
283 : : characters. The real buffer size is 2 bytes longer, because
284 : : all strings are 0 terminated.
285 : :
286 : : @return the current capacity of this string buffer.
287 : : */
288 : 330 : sal_Int32 getCapacity() const
289 : : {
290 : 330 : return nCapacity;
291 : : }
292 : :
293 : : /**
294 : : Ensures that the capacity of the buffer is at least equal to the
295 : : specified minimum.
296 : :
297 : : The new capacity will be at least as large as the maximum of the current
298 : : length (so that no contents of the buffer is destroyed) and the given
299 : : minimumCapacity. If the given minimumCapacity is negative, nothing is
300 : : changed.
301 : :
302 : : @param minimumCapacity the minimum desired capacity.
303 : : */
304 : 83 : void ensureCapacity(sal_Int32 minimumCapacity)
305 : : {
306 : 83 : rtl_stringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
307 : 83 : }
308 : :
309 : : /**
310 : : Sets the length of this String buffer.
311 : :
312 : : If the <code>newLength</code> argument is less than the current
313 : : length of the string buffer, the string buffer is truncated to
314 : : contain exactly the number of characters given by the
315 : : <code>newLength</code> argument.
316 : : <p>
317 : : If the <code>newLength</code> argument is greater than or equal
318 : : to the current length, sufficient null characters
319 : : (<code>'\u0000'</code>) are appended to the string buffer so that
320 : : length becomes the <code>newLength</code> argument.
321 : : <p>
322 : : The <code>newLength</code> argument must be greater than or equal
323 : : to <code>0</code>.
324 : :
325 : : @param newLength the new length of the buffer.
326 : : */
327 : 32441807 : void setLength(sal_Int32 newLength)
328 : : {
329 : : assert(newLength >= 0);
330 : : // Avoid modifications if pData points to const empty string:
331 [ + + ]: 32441807 : if( newLength != pData->length )
332 : : {
333 [ + + ]: 35062 : if( newLength > nCapacity )
334 : 13351 : rtl_stringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
335 : : else
336 : 21711 : pData->buffer[newLength] = '\0';
337 : 35062 : pData->length = newLength;
338 : : }
339 : 32441807 : }
340 : :
341 : : /**
342 : : Returns the character at a specific index in this string buffer.
343 : :
344 : : The first character of a string buffer is at index
345 : : <code>0</code>, the next at index <code>1</code>, and so on, for
346 : : array indexing.
347 : : <p>
348 : : The index argument must be greater than or equal to
349 : : <code>0</code>, and less than the length of this string buffer.
350 : :
351 : : @param index the index of the desired character.
352 : : @return the character at the specified index of this string buffer.
353 : : */
354 : : SAL_DEPRECATED("use rtl::OStringBuffer::operator [] instead")
355 : : sal_Char charAt( sal_Int32 index )
356 : : {
357 : : assert(index >= 0 && index < pData->length);
358 : : return pData->buffer[ index ];
359 : : }
360 : :
361 : : /**
362 : : The character at the specified index of this string buffer is set
363 : : to <code>ch</code>.
364 : :
365 : : The index argument must be greater than or equal to
366 : : <code>0</code>, and less than the length of this string buffer.
367 : :
368 : : @param index the index of the character to modify.
369 : : @param ch the new character.
370 : : */
371 : : SAL_DEPRECATED("use rtl::OStringBuffer::operator [] instead")
372 : : OStringBuffer & setCharAt(sal_Int32 index, sal_Char ch)
373 : : {
374 : : assert(index >= 0 && index < pData->length);
375 : : pData->buffer[ index ] = ch;
376 : : return *this;
377 : : }
378 : :
379 : : /**
380 : : Return a null terminated character array.
381 : : */
382 : 2438165 : const sal_Char* getStr() const { return pData->buffer; }
383 : :
384 : : /**
385 : : Access to individual characters.
386 : :
387 : : @param index must be non-negative and less than length.
388 : :
389 : : @return a reference to the character at the given index.
390 : :
391 : : @since LibreOffice 3.5
392 : : */
393 : 3008582 : sal_Char & operator [](sal_Int32 index) { return pData->buffer[index]; }
394 : :
395 : : /**
396 : : Return a OString instance reflecting the current content
397 : : of this OStringBuffer.
398 : : */
399 : 683 : const OString toString() const
400 : : {
401 : 683 : return OString(pData->buffer, pData->length);
402 : : }
403 : :
404 : : /**
405 : : Appends the string to this string buffer.
406 : :
407 : : The characters of the <code>String</code> argument are appended, in
408 : : order, to the contents of this string buffer, increasing the
409 : : length of this string buffer by the length of the argument.
410 : :
411 : : @param str a string.
412 : : @return this string buffer.
413 : : */
414 : 7268653 : OStringBuffer & append(const OString &str)
415 : : {
416 : 7268653 : return append( str.getStr(), str.getLength() );
417 : : }
418 : :
419 : : /**
420 : : Appends the string representation of the <code>char</code> array
421 : : argument to this string buffer.
422 : :
423 : : The characters of the array argument are appended, in order, to
424 : : the contents of this string buffer. The length of this string
425 : : buffer increases by the length of the argument.
426 : :
427 : : @param str the characters to be appended.
428 : : @return this string buffer.
429 : : */
430 : : #ifdef HAVE_SFINAE_ANONYMOUS_BROKEN
431 : : OStringBuffer & append( const sal_Char * str )
432 : : {
433 : : return append( str, rtl_str_getLength( str ) );
434 : : }
435 : : #else
436 : : template< typename T >
437 : 924292 : typename internal::CharPtrDetector< T, OStringBuffer& >::Type append( const T& str )
438 : : {
439 : 924292 : return append( str, rtl_str_getLength( str ) );
440 : : }
441 : :
442 : : template< typename T >
443 : 28192 : typename internal::NonConstCharArrayDetector< T, OStringBuffer& >::Type append( T& str )
444 : : {
445 : 28192 : return append( str, rtl_str_getLength( str ) );
446 : : }
447 : :
448 : : /**
449 : : @overload
450 : : This function accepts an ASCII string literal as its argument.
451 : : @since LibreOffice 3.6
452 : : */
453 : : template< typename T >
454 : 816077 : typename internal::ConstCharArrayDetector< T, OStringBuffer& >::Type append( T& literal )
455 : : {
456 : 10 : RTL_STRING_CONST_FUNCTION
457 : 816077 : rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), literal, internal::ConstCharArrayDetector< T, void >::size - 1 );
458 : 816077 : return *this;
459 : : }
460 : : #endif
461 : :
462 : : /**
463 : : Appends the string representation of the <code>char</code> array
464 : : argument to this string buffer.
465 : :
466 : : Characters of the character array <code>str</code> are appended,
467 : : in order, to the contents of this string buffer. The length of this
468 : : string buffer increases by the value of <code>len</code>.
469 : :
470 : : @param str the characters to be appended; must be non-null, and must
471 : : point to at least len characters
472 : : @param len the number of characters to append; must be non-negative
473 : : @return this string buffer.
474 : : */
475 : 41880272 : OStringBuffer & append( const sal_Char * str, sal_Int32 len)
476 : : {
477 : : // insert behind the last character
478 : 41880272 : rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
479 : 41880272 : return *this;
480 : : }
481 : :
482 : : /**
483 : : Appends the string representation of the <code>sal_Bool</code>
484 : : argument to the string buffer.
485 : :
486 : : The argument is converted to a string as if by the method
487 : : <code>String.valueOf</code>, and the characters of that
488 : : string are then appended to this string buffer.
489 : :
490 : : @param b a <code>sal_Bool</code>.
491 : : @return this string buffer.
492 : : */
493 : 95 : OStringBuffer & append(sal_Bool b)
494 : : {
495 : : sal_Char sz[RTL_STR_MAX_VALUEOFBOOLEAN];
496 [ + - ]: 95 : return append( sz, rtl_str_valueOfBoolean( sz, b ) );
497 : : }
498 : :
499 : : /**
500 : : Appends the string representation of the <code>char</code>
501 : : argument to this string buffer.
502 : :
503 : : The argument is appended to the contents of this string buffer.
504 : : The length of this string buffer increases by <code>1</code>.
505 : :
506 : : @param c a <code>char</code>.
507 : : @return this string buffer.
508 : : */
509 : 32318808 : OStringBuffer & append(sal_Char c)
510 : : {
511 : 32318808 : return append( &c, 1 );
512 : : }
513 : :
514 : : /**
515 : : Appends the string representation of the <code>sal_Int32</code>
516 : : argument to this string buffer.
517 : :
518 : : The argument is converted to a string as if by the method
519 : : <code>String.valueOf</code>, and the characters of that
520 : : string are then appended to this string buffer.
521 : :
522 : : @param i an <code>sal_Int32</code>.
523 : : @return this string buffer.
524 : : */
525 : 315814 : OStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
526 : : {
527 : : sal_Char sz[RTL_STR_MAX_VALUEOFINT32];
528 [ + - ]: 315814 : return append( sz, rtl_str_valueOfInt32( sz, i, radix ) );
529 : : }
530 : :
531 : : /**
532 : : Appends the string representation of the <code>long</code>
533 : : argument to this string buffer.
534 : :
535 : : The argument is converted to a string as if by the method
536 : : <code>String.valueOf</code>, and the characters of that
537 : : string are then appended to this string buffer.
538 : :
539 : : @param l a <code>long</code>.
540 : : @return this string buffer.
541 : : */
542 : 3260 : OStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
543 : : {
544 : : sal_Char sz[RTL_STR_MAX_VALUEOFINT64];
545 [ + - ]: 3260 : return append( sz, rtl_str_valueOfInt64( sz, l, radix ) );
546 : : }
547 : :
548 : : /**
549 : : Appends the string representation of the <code>float</code>
550 : : argument to this string buffer.
551 : :
552 : : The argument is converted to a string as if by the method
553 : : <code>String.valueOf</code>, and the characters of that
554 : : string are then appended to this string buffer.
555 : :
556 : : @param f a <code>float</code>.
557 : : @return this string buffer.
558 : : */
559 : 250 : OStringBuffer & append(float f)
560 : : {
561 : : sal_Char sz[RTL_STR_MAX_VALUEOFFLOAT];
562 [ + - ]: 250 : return append( sz, rtl_str_valueOfFloat( sz, f ) );
563 : : }
564 : :
565 : : /**
566 : : Appends the string representation of the <code>double</code>
567 : : argument to this string buffer.
568 : :
569 : : The argument is converted to a string as if by the method
570 : : <code>String.valueOf</code>, and the characters of that
571 : : string are then appended to this string buffer.
572 : :
573 : : @param d a <code>double</code>.
574 : : @return this string buffer.
575 : : */
576 : 2534 : OStringBuffer & append(double d)
577 : : {
578 : : sal_Char sz[RTL_STR_MAX_VALUEOFDOUBLE];
579 [ + - ]: 2534 : return append( sz, rtl_str_valueOfDouble( sz, d ) );
580 : : }
581 : :
582 : : /**
583 : : Inserts the string into this string buffer.
584 : :
585 : : The characters of the <code>String</code> argument are inserted, in
586 : : order, into this string buffer at the indicated offset. The length
587 : : of this string buffer is increased by the length of the argument.
588 : : <p>
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.
592 : :
593 : : @param offset the offset.
594 : : @param str a string.
595 : : @return this string buffer.
596 : : */
597 : 0 : OStringBuffer & insert(sal_Int32 offset, const OString & str)
598 : : {
599 : 0 : return insert( offset, str.getStr(), str.getLength() );
600 : : }
601 : :
602 : : /**
603 : : Inserts the string representation of the <code>char</code> array
604 : : argument into this string buffer.
605 : :
606 : : The characters of the array argument are inserted into the
607 : : contents of this string buffer at the position indicated by
608 : : <code>offset</code>. The length of this string buffer increases by
609 : : the length of the argument.
610 : : <p>
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.
614 : :
615 : : @param offset the offset.
616 : : @param str a character array.
617 : : @return this string buffer.
618 : : */
619 : : #ifdef HAVE_SFINAE_ANONYMOUS_BROKEN
620 : : OStringBuffer & insert( sal_Int32 offset, const sal_Char * str )
621 : : {
622 : : return insert( offset, str, rtl_str_getLength( str ) );
623 : : }
624 : : #else
625 : : template< typename T >
626 : : typename internal::CharPtrDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, const T& str )
627 : : {
628 : : return insert( offset, str, rtl_str_getLength( str ) );
629 : : }
630 : :
631 : : template< typename T >
632 : 5 : typename internal::NonConstCharArrayDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, T& str )
633 : : {
634 : 5 : return insert( offset, str, rtl_str_getLength( str ) );
635 : : }
636 : :
637 : : /**
638 : : @overload
639 : : This function accepts an ASCII string literal as its argument.
640 : : @since LibreOffice 3.6
641 : : */
642 : : template< typename T >
643 : 5 : typename internal::ConstCharArrayDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, T& literal )
644 : : {
645 : 5 : RTL_STRING_CONST_FUNCTION
646 : 5 : rtl_stringbuffer_insert( &pData, &nCapacity, offset, literal, internal::ConstCharArrayDetector< T, void >::size - 1 );
647 : 5 : return *this;
648 : : }
649 : : #endif
650 : :
651 : : /**
652 : : Inserts the string representation of the <code>char</code> array
653 : : argument into this string buffer.
654 : :
655 : : The characters of the array argument are inserted into the
656 : : contents of this string buffer at the position indicated by
657 : : <code>offset</code>. The length of this string buffer increases by
658 : : the length of the argument.
659 : : <p>
660 : : The offset argument must be greater than or equal to
661 : : <code>0</code>, and less than or equal to the length of this
662 : : string buffer.
663 : :
664 : : @param offset the offset.
665 : : @param str a character array.
666 : : @param len the number of characters to append.
667 : : @return this string buffer.
668 : : */
669 : 81932 : OStringBuffer & insert( sal_Int32 offset, const sal_Char * str, sal_Int32 len)
670 : : {
671 : : // insert behind the last character
672 : 81932 : rtl_stringbuffer_insert( &pData, &nCapacity, offset, str, len );
673 : 81932 : return *this;
674 : : }
675 : :
676 : : /**
677 : : Inserts the string representation of the <code>sal_Bool</code>
678 : : argument into this string buffer.
679 : :
680 : : The second argument is converted to a string as if by the method
681 : : <code>String.valueOf</code>, and the characters of that
682 : : string are then inserted into this string buffer at the indicated
683 : : offset.
684 : : <p>
685 : : The offset argument must be greater than or equal to
686 : : <code>0</code>, and less than or equal to the length of this
687 : : string buffer.
688 : :
689 : : @param offset the offset.
690 : : @param b a <code>sal_Bool</code>.
691 : : @return this string buffer.
692 : : */
693 : : OStringBuffer & insert(sal_Int32 offset, sal_Bool b)
694 : : {
695 : : sal_Char sz[RTL_STR_MAX_VALUEOFBOOLEAN];
696 : : return insert( offset, sz, rtl_str_valueOfBoolean( sz, b ) );
697 : : }
698 : :
699 : : /**
700 : : Inserts the string representation of the <code>char</code>
701 : : argument into this string buffer.
702 : :
703 : : The second argument is inserted into the contents of this string
704 : : buffer at the position indicated by <code>offset</code>. The length
705 : : of this string buffer increases by one.
706 : : <p>
707 : : The offset argument must be greater than or equal to
708 : : <code>0</code>, and less than or equal to the length of this
709 : : string buffer.
710 : :
711 : : @param offset the offset.
712 : : @param c a <code>char</code>.
713 : : @return this string buffer.
714 : : */
715 : 0 : OStringBuffer & insert(sal_Int32 offset, sal_Char c)
716 : : {
717 : 0 : return insert( offset, &c, 1 );
718 : : }
719 : :
720 : : /**
721 : : Inserts the string representation of the second <code>sal_Int32</code>
722 : : argument into this string buffer.
723 : :
724 : : The second argument is converted to a string as if by the method
725 : : <code>String.valueOf</code>, and the characters of that
726 : : string are then inserted into this string buffer at the indicated
727 : : offset.
728 : : <p>
729 : : The offset argument must be greater than or equal to
730 : : <code>0</code>, and less than or equal to the length of this
731 : : string buffer.
732 : :
733 : : @param offset the offset.
734 : : @param i an <code>sal_Int32</code>.
735 : : @return this string buffer.
736 : : */
737 : : OStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
738 : : {
739 : : sal_Char sz[RTL_STR_MAX_VALUEOFINT32];
740 : : return insert( offset, sz, rtl_str_valueOfInt32( sz, i, radix ) );
741 : : }
742 : :
743 : : /**
744 : : Inserts the string representation of the <code>long</code>
745 : : argument into this string buffer.
746 : :
747 : : The second argument is converted to a string as if by the method
748 : : <code>String.valueOf</code>, and the characters of that
749 : : string are then inserted into this string buffer at the indicated
750 : : offset.
751 : : <p>
752 : : The offset argument must be greater than or equal to
753 : : <code>0</code>, and less than or equal to the length of this
754 : : string buffer.
755 : :
756 : : @param offset the offset.
757 : : @param l a <code>long</code>.
758 : : @return this string buffer.
759 : : */
760 : : OStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
761 : : {
762 : : sal_Char sz[RTL_STR_MAX_VALUEOFINT64];
763 : : return insert( offset, sz, rtl_str_valueOfInt64( sz, l, radix ) );
764 : : }
765 : :
766 : : /**
767 : : Inserts the string representation of the <code>float</code>
768 : : argument into this string buffer.
769 : :
770 : : The second argument is converted to a string as if by the method
771 : : <code>String.valueOf</code>, and the characters of that
772 : : string are then inserted into this string buffer at the indicated
773 : : offset.
774 : : <p>
775 : : The offset argument must be greater than or equal to
776 : : <code>0</code>, and less than or equal to the length of this
777 : : string buffer.
778 : :
779 : : @param offset the offset.
780 : : @param f a <code>float</code>.
781 : : @return this string buffer.
782 : : */
783 : : OStringBuffer insert(sal_Int32 offset, float f)
784 : : {
785 : : sal_Char sz[RTL_STR_MAX_VALUEOFFLOAT];
786 : : return insert( offset, sz, rtl_str_valueOfFloat( sz, f ) );
787 : : }
788 : :
789 : : /**
790 : : Inserts the string representation of the <code>double</code>
791 : : argument into this string buffer.
792 : :
793 : : The second argument is converted to a string as if by the method
794 : : <code>String.valueOf</code>, and the characters of that
795 : : string are then inserted into this string buffer at the indicated
796 : : offset.
797 : : <p>
798 : : The offset argument must be greater than or equal to
799 : : <code>0</code>, and less than or equal to the length of this
800 : : string buffer.
801 : :
802 : : @param offset the offset.
803 : : @param d a <code>double</code>.
804 : : @return this string buffer.
805 : : */
806 : : OStringBuffer & insert(sal_Int32 offset, double d)
807 : : {
808 : : sal_Char sz[RTL_STR_MAX_VALUEOFDOUBLE];
809 : : return insert( offset, sz, rtl_str_valueOfDouble( sz, d ) );
810 : : }
811 : :
812 : : /**
813 : : Removes the characters in a substring of this sequence.
814 : :
815 : : The substring begins at the specified <code>start</code> and
816 : : is <code>len</code> characters long.
817 : :
818 : : start must be >= 0 && <= getLength() && <= end
819 : :
820 : : @param start The beginning index, inclusive
821 : : @param len The substring length
822 : : @return this string buffer.
823 : : */
824 : 1026 : OStringBuffer & remove( sal_Int32 start, sal_Int32 len )
825 : : {
826 : 1026 : rtl_stringbuffer_remove( &pData, start, len );
827 : 1026 : return *this;
828 : : }
829 : :
830 : : private:
831 : : /**
832 : : A pointer to the data structur which contains the data.
833 : : */
834 : : rtl_String * pData;
835 : :
836 : : /**
837 : : The len of the pData->buffer.
838 : : */
839 : : sal_Int32 nCapacity;
840 : : };
841 : :
842 : : }
843 : :
844 : : #ifdef RTL_STRING_UNITTEST
845 : : namespace rtl
846 : : {
847 : : typedef rtlunittest::OStringBuffer OStringBuffer;
848 : : }
849 : : #undef RTL_STRING_CONST_FUNCTION
850 : : #endif
851 : :
852 : : #ifdef RTL_USING
853 : : using ::rtl::OStringBuffer;
854 : : #endif
855 : :
856 : : #endif /* __cplusplus */
857 : : #endif /* _RTL_STRBUF_HXX_ */
858 : :
859 : :
860 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|