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