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 INCLUDED_RTL_STRBUF_HXX
21 : #define INCLUDED_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 19 : OStringBuffer()
106 : : pData(NULL)
107 19 : , nCapacity( 16 )
108 : {
109 19 : rtl_string_new_WithLength( &pData, nCapacity );
110 19 : }
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 0 : OStringBuffer( const OStringBuffer & value )
119 : : pData(NULL)
120 0 : , nCapacity( value.nCapacity )
121 : {
122 0 : rtl_stringbuffer_newFromStringBuffer( &pData, value.nCapacity, value.pData );
123 0 : }
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 0 : explicit OStringBuffer(int length)
132 : : pData(NULL)
133 0 : , nCapacity( length )
134 : {
135 0 : rtl_string_new_WithLength( &pData, length );
136 0 : }
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 0 : OStringBuffer(const OString& value)
149 : : pData(NULL)
150 0 : , nCapacity( value.getLength() + 16 )
151 : {
152 0 : rtl_stringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
153 0 : }
154 :
155 : /**
156 : @overload
157 : @since LibreOffice 3.6
158 : */
159 : template< typename T >
160 : OStringBuffer( const T& value, typename internal::CharPtrDetector< T, internal::Dummy >::Type = internal::Dummy())
161 : : pData(NULL)
162 : {
163 : sal_Int32 length = rtl_str_getLength( value );
164 : nCapacity = length + 16;
165 : rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
166 : }
167 :
168 : template< typename T >
169 : OStringBuffer( T& value, typename internal::NonConstCharArrayDetector< T, internal::Dummy >::Type = internal::Dummy())
170 : : pData(NULL)
171 : {
172 : sal_Int32 length = rtl_str_getLength( value );
173 : nCapacity = length + 16;
174 : rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
175 : }
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 1 : OStringBuffer( T& literal, typename internal::ConstCharArrayDetector< T, internal::Dummy >::Type = internal::Dummy())
190 : : pData(NULL)
191 1 : , nCapacity( internal::ConstCharArrayDetector< T, void >::size - 1 + 16 )
192 : {
193 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
194 1 : rtl_string_newFromLiteral( &pData, literal, internal::ConstCharArrayDetector< T, void >::size - 1, 16 );
195 : #ifdef RTL_STRING_UNITTEST
196 : rtl_string_unittest_const_literal = true;
197 : #endif
198 1 : }
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 : OStringBuffer(const sal_Char * value, sal_Int32 length)
213 : : pData(NULL)
214 : , nCapacity( length + 16 )
215 : {
216 : rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
217 : }
218 :
219 : #ifdef RTL_FAST_STRING
220 : /**
221 : @overload
222 : @internal
223 : */
224 : template< typename T1, typename T2 >
225 : OStringBuffer( const OStringConcat< T1, T2 >& c )
226 : {
227 : const sal_Int32 l = c.length();
228 : nCapacity = l + 16;
229 : pData = rtl_string_alloc( nCapacity );
230 : char* end = c.addData( pData->buffer );
231 : *end = '\0';
232 : pData->length = end - pData->buffer;
233 : }
234 : #endif
235 :
236 : /** Assign to this a copy of value.
237 : */
238 0 : OStringBuffer& operator = ( const OStringBuffer& value )
239 : {
240 0 : if (this != &value)
241 : {
242 : rtl_stringbuffer_newFromStringBuffer(&pData,
243 : value.nCapacity,
244 0 : value.pData);
245 0 : nCapacity = value.nCapacity;
246 : }
247 0 : return *this;
248 : }
249 :
250 : /**
251 : Release the string data.
252 : */
253 20 : ~OStringBuffer()
254 : {
255 20 : rtl_string_release( pData );
256 20 : }
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 2 : OString makeStringAndClear()
267 : {
268 2 : OString aRet( pData );
269 2 : rtl_string_new(&pData);
270 2 : nCapacity = 0;
271 2 : 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 605 : sal_Int32 getLength() const
280 : {
281 605 : 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 91632 : bool isEmpty() const SAL_THROW(())
293 : {
294 91632 : 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 : sal_Int32 getCapacity() const
308 : {
309 : 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 0 : void ensureCapacity(sal_Int32 minimumCapacity)
324 : {
325 0 : rtl_stringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
326 0 : }
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 91373 : void setLength(sal_Int32 newLength)
347 : {
348 : assert(newLength >= 0);
349 : // Avoid modifications if pData points to const empty string:
350 91373 : if( newLength != pData->length )
351 : {
352 85 : if( newLength > nCapacity )
353 0 : rtl_stringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
354 : else
355 85 : pData->buffer[newLength] = '\0';
356 85 : pData->length = newLength;
357 : }
358 91373 : }
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 86 : 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 0 : sal_Char & operator [](sal_Int32 index)
413 : {
414 : assert(index >= 0 && index < pData->length);
415 0 : return pData->buffer[index];
416 : }
417 :
418 : /**
419 : Return a OString instance reflecting the current content
420 : of this OStringBuffer.
421 : */
422 0 : const OString toString() const
423 : {
424 0 : 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 0 : OStringBuffer & append(const OString &str)
438 : {
439 0 : 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 0 : typename internal::CharPtrDetector< T, OStringBuffer& >::Type append( const T& str )
455 : {
456 0 : return append( str, rtl_str_getLength( str ) );
457 : }
458 :
459 : template< typename T >
460 0 : typename internal::NonConstCharArrayDetector< T, OStringBuffer& >::Type append( T& str )
461 : {
462 0 : 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 0 : typename internal::ConstCharArrayDetector< T, OStringBuffer& >::Type append( T& literal )
472 : {
473 : RTL_STRING_CONST_FUNCTION
474 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
475 0 : rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), literal, internal::ConstCharArrayDetector< T, void >::size - 1 );
476 0 : 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 518 : OStringBuffer & append( const sal_Char * str, sal_Int32 len)
493 : {
494 : // insert behind the last character
495 518 : rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
496 518 : return *this;
497 : }
498 :
499 : #ifdef RTL_FAST_STRING
500 : /**
501 : @overload
502 : @internal
503 : */
504 : template< typename T1, typename T2 >
505 0 : OStringBuffer& append( const OStringConcat< T1, T2 >& c )
506 : {
507 0 : const int l = c.length();
508 0 : if( l == 0 )
509 0 : return *this;
510 0 : rtl_stringbuffer_ensureCapacity( &pData, &nCapacity, pData->length + l );
511 0 : char* end = c.addData( pData->buffer + pData->length );
512 0 : *end = '\0';
513 0 : pData->length = end - pData->buffer;
514 0 : 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 0 : OStringBuffer & append(sal_Bool b)
530 : {
531 : sal_Char sz[RTL_STR_MAX_VALUEOFBOOLEAN];
532 0 : return append( sz, rtl_str_valueOfBoolean( sz, b ) );
533 : }
534 :
535 : /**
536 : Appends the string representation of the <code>bool</code>
537 : argument to the string buffer.
538 :
539 : The argument is converted to a string as if by the method
540 : <code>String.valueOf</code>, and the characters of that
541 : string are then appended to this string buffer.
542 :
543 : @param b a <code>bool</code>.
544 : @return this string buffer.
545 :
546 : @since LibreOffice 4.3
547 : */
548 0 : OStringBuffer & append(bool b)
549 : {
550 : sal_Char sz[RTL_STR_MAX_VALUEOFBOOLEAN];
551 0 : return append( sz, rtl_str_valueOfBoolean( sz, b ) );
552 : }
553 :
554 : /// @cond INTERNAL
555 : // Pointer can be automatically converted to bool, which is unwanted here.
556 : // Explicitly delete all pointer append() overloads to prevent this
557 : // (except for char* overload, which is handled elsewhere).
558 : template< typename T >
559 : typename internal::Enable< void,
560 : !internal::CharPtrDetector< T* >::ok >::Type
561 : append( T* ) SAL_DELETED_FUNCTION;
562 : /// @endcond
563 :
564 : /**
565 : Appends the string representation of the <code>char</code>
566 : argument to this string buffer.
567 :
568 : The argument is appended to the contents of this string buffer.
569 : The length of this string buffer increases by <code>1</code>.
570 :
571 : @param c a <code>char</code>.
572 : @return this string buffer.
573 : */
574 90 : OStringBuffer & append(sal_Char c)
575 : {
576 90 : return append( &c, 1 );
577 : }
578 :
579 : /**
580 : Appends the string representation of the <code>sal_Int32</code>
581 : argument to this string buffer.
582 :
583 : The argument is converted to a string as if by the method
584 : <code>String.valueOf</code>, and the characters of that
585 : string are then appended to this string buffer.
586 :
587 : @param i an <code>sal_Int32</code>.
588 : @param radix the radix
589 : @return this string buffer.
590 : */
591 0 : OStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
592 : {
593 : sal_Char sz[RTL_STR_MAX_VALUEOFINT32];
594 0 : return append( sz, rtl_str_valueOfInt32( sz, i, radix ) );
595 : }
596 :
597 : /**
598 : Appends the string representation of the <code>long</code>
599 : argument to this string buffer.
600 :
601 : The argument is converted to a string as if by the method
602 : <code>String.valueOf</code>, and the characters of that
603 : string are then appended to this string buffer.
604 :
605 : @param l a <code>long</code>.
606 : @param radix the radix
607 : @return this string buffer.
608 : */
609 0 : OStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
610 : {
611 : sal_Char sz[RTL_STR_MAX_VALUEOFINT64];
612 0 : return append( sz, rtl_str_valueOfInt64( sz, l, radix ) );
613 : }
614 :
615 : /**
616 : Appends the string representation of the <code>float</code>
617 : argument to this string buffer.
618 :
619 : The argument is converted to a string as if by the method
620 : <code>String.valueOf</code>, and the characters of that
621 : string are then appended to this string buffer.
622 :
623 : @param f a <code>float</code>.
624 : @return this string buffer.
625 : */
626 0 : OStringBuffer & append(float f)
627 : {
628 : sal_Char sz[RTL_STR_MAX_VALUEOFFLOAT];
629 0 : return append( sz, rtl_str_valueOfFloat( sz, f ) );
630 : }
631 :
632 : /**
633 : Appends the string representation of the <code>double</code>
634 : argument to this string buffer.
635 :
636 : The argument is converted to a string as if by the method
637 : <code>String.valueOf</code>, and the characters of that
638 : string are then appended to this string buffer.
639 :
640 : @param d a <code>double</code>.
641 : @return this string buffer.
642 : */
643 0 : OStringBuffer & append(double d)
644 : {
645 : sal_Char sz[RTL_STR_MAX_VALUEOFDOUBLE];
646 0 : return append( sz, rtl_str_valueOfDouble( sz, d ) );
647 : }
648 :
649 : /**
650 : Inserts the string into this string buffer.
651 :
652 : The characters of the <code>String</code> argument are inserted, in
653 : order, into this string buffer at the indicated offset. The length
654 : of this string buffer is increased by the length of the argument.
655 : <p>
656 : The offset argument must be greater than or equal to
657 : <code>0</code>, and less than or equal to the length of this
658 : string buffer.
659 :
660 : @param offset the offset.
661 : @param str a string.
662 : @return this string buffer.
663 : */
664 0 : OStringBuffer & insert(sal_Int32 offset, const OString & str)
665 : {
666 0 : return insert( offset, str.getStr(), str.getLength() );
667 : }
668 :
669 : /**
670 : Inserts the string representation of the <code>char</code> array
671 : argument into this string buffer.
672 :
673 : The characters of the array argument are inserted into the
674 : contents of this string buffer at the position indicated by
675 : <code>offset</code>. The length of this string buffer increases by
676 : the length of the argument.
677 : <p>
678 : The offset argument must be greater than or equal to
679 : <code>0</code>, and less than or equal to the length of this
680 : string buffer.
681 :
682 : @param offset the offset.
683 : @param str a character array.
684 : @return this string buffer.
685 : */
686 : template< typename T >
687 : typename internal::CharPtrDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, const T& str )
688 : {
689 : return insert( offset, str, rtl_str_getLength( str ) );
690 : }
691 :
692 : template< typename T >
693 : typename internal::NonConstCharArrayDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, T& str )
694 : {
695 : return insert( offset, str, rtl_str_getLength( str ) );
696 : }
697 :
698 : /**
699 : @overload
700 : This function accepts an ASCII string literal as its argument.
701 : @since LibreOffice 3.6
702 : */
703 : template< typename T >
704 0 : typename internal::ConstCharArrayDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, T& literal )
705 : {
706 : RTL_STRING_CONST_FUNCTION
707 : assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
708 0 : rtl_stringbuffer_insert( &pData, &nCapacity, offset, literal, internal::ConstCharArrayDetector< T, void >::size - 1 );
709 0 : return *this;
710 : }
711 :
712 : /**
713 : Inserts the string representation of the <code>char</code> array
714 : argument into this string buffer.
715 :
716 : The characters of the array argument are inserted into the
717 : contents of this string buffer at the position indicated by
718 : <code>offset</code>. The length of this string buffer increases by
719 : the length of the argument.
720 : <p>
721 : The offset argument must be greater than or equal to
722 : <code>0</code>, and less than or equal to the length of this
723 : string buffer.
724 :
725 : @param offset the offset.
726 : @param str a character array.
727 : @param len the number of characters to append.
728 : @return this string buffer.
729 : */
730 0 : OStringBuffer & insert( sal_Int32 offset, const sal_Char * str, sal_Int32 len)
731 : {
732 : // insert behind the last character
733 0 : rtl_stringbuffer_insert( &pData, &nCapacity, offset, str, len );
734 0 : return *this;
735 : }
736 :
737 : /**
738 : Inserts the string representation of the <code>sal_Bool</code>
739 : argument into this string buffer.
740 :
741 : The second argument is converted to a string as if by the method
742 : <code>String.valueOf</code>, and the characters of that
743 : string are then inserted into this string buffer at the indicated
744 : offset.
745 : <p>
746 : The offset argument must be greater than or equal to
747 : <code>0</code>, and less than or equal to the length of this
748 : string buffer.
749 :
750 : @param offset the offset.
751 : @param b a <code>sal_Bool</code>.
752 : @return this string buffer.
753 : */
754 : OStringBuffer & insert(sal_Int32 offset, sal_Bool b)
755 : {
756 : sal_Char sz[RTL_STR_MAX_VALUEOFBOOLEAN];
757 : return insert( offset, sz, rtl_str_valueOfBoolean( sz, b ) );
758 : }
759 :
760 : /**
761 : Inserts the string representation of the <code>bool</code>
762 : argument into this string buffer.
763 :
764 : The second argument is converted to a string as if by the method
765 : <code>OString::boolean</code>, and the characters of that
766 : string are then inserted into this string buffer at the indicated
767 : offset.
768 : <p>
769 : The offset argument must be greater than or equal to
770 : <code>0</code>, and less than or equal to the length of this
771 : string buffer.
772 :
773 : @param offset the offset.
774 : @param b a <code>bool</code>.
775 : @return this string buffer.
776 :
777 : @since LibreOffice 4.3
778 : */
779 : OStringBuffer & insert(sal_Int32 offset, bool b)
780 : {
781 : sal_Char sz[RTL_STR_MAX_VALUEOFBOOLEAN];
782 : return insert( offset, sz, rtl_str_valueOfBoolean( sz, b ) );
783 : }
784 :
785 : /**
786 : Inserts the string representation of the <code>char</code>
787 : argument into this string buffer.
788 :
789 : The second argument is inserted into the contents of this string
790 : buffer at the position indicated by <code>offset</code>. The length
791 : of this string buffer increases by one.
792 : <p>
793 : The offset argument must be greater than or equal to
794 : <code>0</code>, and less than or equal to the length of this
795 : string buffer.
796 :
797 : @param offset the offset.
798 : @param c a <code>char</code>.
799 : @return this string buffer.
800 : */
801 0 : OStringBuffer & insert(sal_Int32 offset, sal_Char c)
802 : {
803 0 : return insert( offset, &c, 1 );
804 : }
805 :
806 : /**
807 : Inserts the string representation of the second <code>sal_Int32</code>
808 : argument into this string buffer.
809 :
810 : The second argument is converted to a string as if by the method
811 : <code>String.valueOf</code>, and the characters of that
812 : string are then inserted into this string buffer at the indicated
813 : offset.
814 : <p>
815 : The offset argument must be greater than or equal to
816 : <code>0</code>, and less than or equal to the length of this
817 : string buffer.
818 :
819 : @param offset the offset.
820 : @param i an <code>sal_Int32</code>.
821 : @param radix the radix
822 : @return this string buffer.
823 : */
824 : OStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
825 : {
826 : sal_Char sz[RTL_STR_MAX_VALUEOFINT32];
827 : return insert( offset, sz, rtl_str_valueOfInt32( sz, i, radix ) );
828 : }
829 :
830 : /**
831 : Inserts the string representation of the <code>long</code>
832 : argument into this string buffer.
833 :
834 : The second argument is converted to a string as if by the method
835 : <code>String.valueOf</code>, and the characters of that
836 : string are then inserted into this string buffer at the indicated
837 : offset.
838 : <p>
839 : The offset argument must be greater than or equal to
840 : <code>0</code>, and less than or equal to the length of this
841 : string buffer.
842 :
843 : @param offset the offset.
844 : @param l a <code>long</code>.
845 : @param radix the radix
846 : @return this string buffer.
847 : */
848 : OStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
849 : {
850 : sal_Char sz[RTL_STR_MAX_VALUEOFINT64];
851 : return insert( offset, sz, rtl_str_valueOfInt64( sz, l, radix ) );
852 : }
853 :
854 : /**
855 : Inserts the string representation of the <code>float</code>
856 : argument into this string buffer.
857 :
858 : The second argument is converted to a string as if by the method
859 : <code>String.valueOf</code>, and the characters of that
860 : string are then inserted into this string buffer at the indicated
861 : offset.
862 : <p>
863 : The offset argument must be greater than or equal to
864 : <code>0</code>, and less than or equal to the length of this
865 : string buffer.
866 :
867 : @param offset the offset.
868 : @param f a <code>float</code>.
869 : @return this string buffer.
870 : */
871 : OStringBuffer insert(sal_Int32 offset, float f)
872 : {
873 : sal_Char sz[RTL_STR_MAX_VALUEOFFLOAT];
874 : return insert( offset, sz, rtl_str_valueOfFloat( sz, f ) );
875 : }
876 :
877 : /**
878 : Inserts the string representation of the <code>double</code>
879 : argument into this string buffer.
880 :
881 : The second argument is converted to a string as if by the method
882 : <code>String.valueOf</code>, and the characters of that
883 : string are then inserted into this string buffer at the indicated
884 : offset.
885 : <p>
886 : The offset argument must be greater than or equal to
887 : <code>0</code>, and less than or equal to the length of this
888 : string buffer.
889 :
890 : @param offset the offset.
891 : @param d a <code>double</code>.
892 : @return this string buffer.
893 : */
894 : OStringBuffer & insert(sal_Int32 offset, double d)
895 : {
896 : sal_Char sz[RTL_STR_MAX_VALUEOFDOUBLE];
897 : return insert( offset, sz, rtl_str_valueOfDouble( sz, d ) );
898 : }
899 :
900 : /**
901 : Removes the characters in a substring of this sequence.
902 :
903 : The substring begins at the specified <code>start</code> and
904 : is <code>len</code> characters long.
905 :
906 : start must be >= 0 && <= getLength() && <= end
907 :
908 : @param start The beginning index, inclusive
909 : @param len The substring length
910 : @return this string buffer.
911 : */
912 0 : OStringBuffer & remove( sal_Int32 start, sal_Int32 len )
913 : {
914 0 : rtl_stringbuffer_remove( &pData, start, len );
915 0 : return *this;
916 : }
917 :
918 : #ifdef LIBO_INTERNAL_ONLY
919 : // This is to complement the RTL_FAST_STRING operator+, which allows any combination of valid operands,
920 : // even two buffers. It's intentional it returns OString, just like the operator+ would in the fast variant.
921 : #ifndef RTL_FAST_STRING
922 : /**
923 : @internal
924 : @since LibreOffice 4.1
925 : */
926 : friend OString operator+( const OStringBuffer& str1, const OStringBuffer& str2 ) SAL_THROW(())
927 : {
928 : return OString( str1.pData ).concat( str2.pData );
929 : }
930 : #endif
931 : #endif
932 :
933 : private:
934 : /**
935 : A pointer to the data structur which contains the data.
936 : */
937 : rtl_String * pData;
938 :
939 : /**
940 : The len of the pData->buffer.
941 : */
942 : sal_Int32 nCapacity;
943 : };
944 :
945 : #ifdef RTL_FAST_STRING
946 : /**
947 : @internal
948 : */
949 : template<>
950 : struct ToStringHelper< OStringBuffer >
951 : {
952 0 : static int length( const OStringBuffer& s ) { return s.getLength(); }
953 0 : static char* addData( char* buffer, const OStringBuffer& s ) { return addDataHelper( buffer, s.getStr(), s.getLength()); }
954 : static const bool allowOStringConcat = true;
955 : static const bool allowOUStringConcat = false;
956 : };
957 : #endif
958 :
959 :
960 : }
961 :
962 : #ifdef RTL_STRING_UNITTEST
963 : namespace rtl
964 : {
965 : typedef rtlunittest::OStringBuffer OStringBuffer;
966 : }
967 : #undef RTL_STRING_CONST_FUNCTION
968 : #endif
969 :
970 : #ifdef RTL_USING
971 : using ::rtl::OStringBuffer;
972 : #endif
973 :
974 : #endif /* __cplusplus */
975 : #endif // INCLUDED_RTL_STRBUF_HXX
976 :
977 :
978 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|