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