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 1269765 : OStringBuffer()
106 : : pData(NULL)
107 1269765 : , nCapacity( 16 )
108 : {
109 1269765 : rtl_string_new_WithLength( &pData, nCapacity );
110 1269765 : }
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 15422 : OStringBuffer( const OStringBuffer & value )
119 : : pData(NULL)
120 15422 : , nCapacity( value.nCapacity )
121 : {
122 15422 : rtl_stringbuffer_newFromStringBuffer( &pData, value.nCapacity, value.pData );
123 15422 : }
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 1337418 : explicit OStringBuffer(int length)
132 : : pData(NULL)
133 1337418 : , nCapacity( length )
134 : {
135 1337418 : rtl_string_new_WithLength( &pData, length );
136 1337418 : }
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 78871 : OStringBuffer(const OString& value)
149 : : pData(NULL)
150 78871 : , nCapacity( value.getLength() + 16 )
151 : {
152 78871 : rtl_stringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
153 78871 : }
154 :
155 : /**
156 : @overload
157 : @since LibreOffice 3.6
158 : */
159 : template< typename T >
160 16 : OStringBuffer( const T& value, typename libreoffice_internal::CharPtrDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy())
161 16 : : pData(NULL)
162 : {
163 16 : sal_Int32 length = rtl_str_getLength( value );
164 16 : nCapacity = length + 16;
165 16 : rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
166 16 : }
167 :
168 : template< typename T >
169 6 : OStringBuffer( T& value, typename libreoffice_internal::NonConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy())
170 6 : : pData(NULL)
171 : {
172 6 : sal_Int32 length = rtl_str_getLength( value );
173 6 : nCapacity = length + 16;
174 6 : rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
175 6 : }
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 45556 : OStringBuffer( T& literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy())
190 : : pData(NULL)
191 45556 : , nCapacity( libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 + 16 )
192 : {
193 : assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
194 45556 : rtl_string_newFromLiteral( &pData, literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, 16 );
195 : #ifdef RTL_STRING_UNITTEST
196 6 : rtl_string_unittest_const_literal = true;
197 : #endif
198 45556 : }
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 2 : OStringBuffer(const sal_Char * value, sal_Int32 length)
213 : : pData(NULL)
214 2 : , nCapacity( length + 16 )
215 : {
216 2 : rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
217 2 : }
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 278 : OStringBuffer& operator = ( const OStringBuffer& value )
239 : {
240 278 : if (this != &value)
241 : {
242 : rtl_stringbuffer_newFromStringBuffer(&pData,
243 : value.nCapacity,
244 278 : value.pData);
245 278 : nCapacity = value.nCapacity;
246 : }
247 278 : return *this;
248 : }
249 :
250 : /**
251 : Release the string data.
252 : */
253 2747056 : ~OStringBuffer()
254 : {
255 2747056 : rtl_string_release( pData );
256 2747056 : }
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 2195700 : OString makeStringAndClear()
267 : {
268 2195700 : OString aRet( pData );
269 2195700 : rtl_string_new(&pData);
270 2195700 : nCapacity = 0;
271 2195700 : 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 38201410 : sal_Int32 getLength() const
280 : {
281 38201410 : 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 38272708 : bool isEmpty() const
293 : {
294 38272708 : 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 132 : sal_Int32 getCapacity() const
308 : {
309 132 : 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 1642 : void ensureCapacity(sal_Int32 minimumCapacity)
324 : {
325 1642 : rtl_stringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
326 1642 : }
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 37421656 : void setLength(sal_Int32 newLength)
347 : {
348 : assert(newLength >= 0);
349 : // Avoid modifications if pData points to const empty string:
350 37421656 : if( newLength != pData->length )
351 : {
352 86807 : if( newLength > nCapacity )
353 46508 : rtl_stringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
354 : else
355 40299 : pData->buffer[newLength] = '\0';
356 86807 : pData->length = newLength;
357 : }
358 37421656 : }
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 868712 : 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 6497732 : sal_Char & operator [](sal_Int32 index)
413 : {
414 : assert(index >= 0 && index < pData->length);
415 6497732 : return pData->buffer[index];
416 : }
417 :
418 : /**
419 : Return a OString instance reflecting the current content
420 : of this OStringBuffer.
421 : */
422 1282 : const OString toString() const
423 : {
424 1282 : 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 916402 : OStringBuffer & append(const OString &str)
438 : {
439 916402 : 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 342198 : typename libreoffice_internal::CharPtrDetector< T, OStringBuffer& >::Type append( const T& str )
455 : {
456 342198 : return append( str, rtl_str_getLength( str ) );
457 : }
458 :
459 : template< typename T >
460 57656 : typename libreoffice_internal::NonConstCharArrayDetector< T, OStringBuffer& >::Type append( T& str )
461 : {
462 57656 : 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 213053 : typename libreoffice_internal::ConstCharArrayDetector< T, OStringBuffer& >::Type append( T& literal )
472 : {
473 4 : RTL_STRING_CONST_FUNCTION
474 : assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
475 213053 : rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 );
476 213053 : 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 32604420 : OStringBuffer & append( const sal_Char * str, sal_Int32 len)
493 : {
494 : assert( len >= 0 );
495 32604420 : rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
496 32604420 : return *this;
497 : }
498 :
499 : #ifdef RTL_FAST_STRING
500 : /**
501 : @overload
502 : @internal
503 : */
504 : template< typename T1, typename T2 >
505 371015 : OStringBuffer& append( const OStringConcat< T1, T2 >& c )
506 : {
507 371015 : const int l = c.length();
508 371015 : if( l == 0 )
509 0 : return *this;
510 371015 : rtl_stringbuffer_ensureCapacity( &pData, &nCapacity, pData->length + l );
511 371015 : char* end = c.addData( pData->buffer + pData->length );
512 371015 : *end = '\0';
513 371015 : pData->length = end - pData->buffer;
514 371015 : 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 38 : OStringBuffer & append(bool b)
549 : {
550 : sal_Char sz[RTL_STR_MAX_VALUEOFBOOLEAN];
551 38 : 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 libreoffice_internal::Enable< void,
560 : !libreoffice_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 29541053 : OStringBuffer & append(sal_Char c)
575 : {
576 29541053 : 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 423348 : OStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
592 : {
593 : sal_Char sz[RTL_STR_MAX_VALUEOFINT32];
594 423348 : 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 5848 : OStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
610 : {
611 : sal_Char sz[RTL_STR_MAX_VALUEOFINT64];
612 5848 : 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 100 : OStringBuffer & append(float f)
627 : {
628 : sal_Char sz[RTL_STR_MAX_VALUEOFFLOAT];
629 100 : 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 5526 : OStringBuffer & append(double d)
644 : {
645 : sal_Char sz[RTL_STR_MAX_VALUEOFDOUBLE];
646 5526 : 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 848 : OStringBuffer & insert(sal_Int32 offset, const OString & str)
665 : {
666 848 : 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 libreoffice_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 2 : typename libreoffice_internal::NonConstCharArrayDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, T& str )
694 : {
695 2 : 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 2 : typename libreoffice_internal::ConstCharArrayDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, T& literal )
705 : {
706 2 : RTL_STRING_CONST_FUNCTION
707 : assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
708 2 : rtl_stringbuffer_insert( &pData, &nCapacity, offset, literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 );
709 2 : 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 117624 : OStringBuffer & insert( sal_Int32 offset, const sal_Char * str, sal_Int32 len)
731 : {
732 : assert( offset >= 0 && offset <= pData->length );
733 : assert( len >= 0 );
734 117624 : rtl_stringbuffer_insert( &pData, &nCapacity, offset, str, len );
735 117624 : return *this;
736 : }
737 :
738 : /**
739 : Inserts the string representation of the <code>sal_Bool</code>
740 : argument into this string buffer.
741 :
742 : The second argument is converted to a string as if by the method
743 : <code>String.valueOf</code>, and the characters of that
744 : string are then inserted into this string buffer at the indicated
745 : offset.
746 : <p>
747 : The offset argument must be greater than or equal to
748 : <code>0</code>, and less than or equal to the length of this
749 : string buffer.
750 :
751 : @param offset the offset.
752 : @param b a <code>sal_Bool</code>.
753 : @return this string buffer.
754 : */
755 : OStringBuffer & insert(sal_Int32 offset, sal_Bool b)
756 : {
757 : sal_Char sz[RTL_STR_MAX_VALUEOFBOOLEAN];
758 : return insert( offset, sz, rtl_str_valueOfBoolean( sz, b ) );
759 : }
760 :
761 : /**
762 : Inserts the string representation of the <code>bool</code>
763 : argument into this string buffer.
764 :
765 : The second argument is converted to a string as if by the method
766 : <code>OString::boolean</code>, and the characters of that
767 : string are then inserted into this string buffer at the indicated
768 : offset.
769 : <p>
770 : The offset argument must be greater than or equal to
771 : <code>0</code>, and less than or equal to the length of this
772 : string buffer.
773 :
774 : @param offset the offset.
775 : @param b a <code>bool</code>.
776 : @return this string buffer.
777 :
778 : @since LibreOffice 4.3
779 : */
780 : OStringBuffer & insert(sal_Int32 offset, bool b)
781 : {
782 : sal_Char sz[RTL_STR_MAX_VALUEOFBOOLEAN];
783 : return insert( offset, sz, rtl_str_valueOfBoolean( sz, b ) );
784 : }
785 :
786 : /**
787 : Inserts the string representation of the <code>char</code>
788 : argument into this string buffer.
789 :
790 : The second argument is inserted into the contents of this string
791 : buffer at the position indicated by <code>offset</code>. The length
792 : of this string buffer increases by one.
793 : <p>
794 : The offset argument must be greater than or equal to
795 : <code>0</code>, and less than or equal to the length of this
796 : string buffer.
797 :
798 : @param offset the offset.
799 : @param c a <code>char</code>.
800 : @return this string buffer.
801 : */
802 0 : OStringBuffer & insert(sal_Int32 offset, sal_Char c)
803 : {
804 0 : return insert( offset, &c, 1 );
805 : }
806 :
807 : /**
808 : Inserts the string representation of the second <code>sal_Int32</code>
809 : argument into this string buffer.
810 :
811 : The second argument is converted to a string as if by the method
812 : <code>String.valueOf</code>, and the characters of that
813 : string are then inserted into this string buffer at the indicated
814 : offset.
815 : <p>
816 : The offset argument must be greater than or equal to
817 : <code>0</code>, and less than or equal to the length of this
818 : string buffer.
819 :
820 : @param offset the offset.
821 : @param i an <code>sal_Int32</code>.
822 : @param radix the radix
823 : @return this string buffer.
824 : */
825 : OStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
826 : {
827 : sal_Char sz[RTL_STR_MAX_VALUEOFINT32];
828 : return insert( offset, sz, rtl_str_valueOfInt32( sz, i, radix ) );
829 : }
830 :
831 : /**
832 : Inserts the string representation of the <code>long</code>
833 : argument into this string buffer.
834 :
835 : The second argument is converted to a string as if by the method
836 : <code>String.valueOf</code>, and the characters of that
837 : string are then inserted into this string buffer at the indicated
838 : offset.
839 : <p>
840 : The offset argument must be greater than or equal to
841 : <code>0</code>, and less than or equal to the length of this
842 : string buffer.
843 :
844 : @param offset the offset.
845 : @param l a <code>long</code>.
846 : @param radix the radix
847 : @return this string buffer.
848 : */
849 : OStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
850 : {
851 : sal_Char sz[RTL_STR_MAX_VALUEOFINT64];
852 : return insert( offset, sz, rtl_str_valueOfInt64( sz, l, radix ) );
853 : }
854 :
855 : /**
856 : Inserts the string representation of the <code>float</code>
857 : argument into this string buffer.
858 :
859 : The second argument is converted to a string as if by the method
860 : <code>String.valueOf</code>, and the characters of that
861 : string are then inserted into this string buffer at the indicated
862 : offset.
863 : <p>
864 : The offset argument must be greater than or equal to
865 : <code>0</code>, and less than or equal to the length of this
866 : string buffer.
867 :
868 : @param offset the offset.
869 : @param f a <code>float</code>.
870 : @return this string buffer.
871 : */
872 : OStringBuffer insert(sal_Int32 offset, float f)
873 : {
874 : sal_Char sz[RTL_STR_MAX_VALUEOFFLOAT];
875 : return insert( offset, sz, rtl_str_valueOfFloat( sz, f ) );
876 : }
877 :
878 : /**
879 : Inserts the string representation of the <code>double</code>
880 : argument into this string buffer.
881 :
882 : The second argument is converted to a string as if by the method
883 : <code>String.valueOf</code>, and the characters of that
884 : string are then inserted into this string buffer at the indicated
885 : offset.
886 : <p>
887 : The offset argument must be greater than or equal to
888 : <code>0</code>, and less than or equal to the length of this
889 : string buffer.
890 :
891 : @param offset the offset.
892 : @param d a <code>double</code>.
893 : @return this string buffer.
894 : */
895 : OStringBuffer & insert(sal_Int32 offset, double d)
896 : {
897 : sal_Char sz[RTL_STR_MAX_VALUEOFDOUBLE];
898 : return insert( offset, sz, rtl_str_valueOfDouble( sz, d ) );
899 : }
900 :
901 : /**
902 : Removes the characters in a substring of this sequence.
903 :
904 : The substring begins at the specified <code>start</code> and
905 : is <code>len</code> characters long.
906 :
907 : start must be >= 0 && <= getLength() && <= end
908 :
909 : @param start The beginning index, inclusive
910 : @param len The substring length
911 : @return this string buffer.
912 : */
913 1850 : OStringBuffer & remove( sal_Int32 start, sal_Int32 len )
914 : {
915 : assert( start >= 0 && start <= pData->length );
916 : assert( len >= 0 );
917 1850 : rtl_stringbuffer_remove( &pData, start, len );
918 1850 : return *this;
919 : }
920 :
921 : #ifdef LIBO_INTERNAL_ONLY
922 : // This is to complement the RTL_FAST_STRING operator+, which allows any combination of valid operands,
923 : // even two buffers. It's intentional it returns OString, just like the operator+ would in the fast variant.
924 : #ifndef RTL_FAST_STRING
925 : /**
926 : @internal
927 : @since LibreOffice 4.1
928 : */
929 : friend OString operator+( const OStringBuffer& str1, const OStringBuffer& str2 )
930 : {
931 : return OString( str1.pData ).concat( str2.pData );
932 : }
933 : #endif
934 : #endif
935 :
936 : private:
937 : /**
938 : A pointer to the data structure which contains the data.
939 : */
940 : rtl_String * pData;
941 :
942 : /**
943 : The len of the pData->buffer.
944 : */
945 : sal_Int32 nCapacity;
946 : };
947 :
948 : #ifdef RTL_FAST_STRING
949 : /**
950 : @internal
951 : */
952 : template<>
953 : struct ToStringHelper< OStringBuffer >
954 : {
955 42 : static int length( const OStringBuffer& s ) { return s.getLength(); }
956 42 : static char* addData( char* buffer, const OStringBuffer& s ) { return addDataHelper( buffer, s.getStr(), s.getLength()); }
957 : static const bool allowOStringConcat = true;
958 : static const bool allowOUStringConcat = false;
959 : };
960 : #endif
961 :
962 :
963 : }
964 :
965 : #ifdef RTL_STRING_UNITTEST
966 : namespace rtl
967 : {
968 : typedef rtlunittest::OStringBuffer OStringBuffer;
969 : }
970 : #undef RTL_STRING_CONST_FUNCTION
971 : #endif
972 :
973 : #ifdef RTL_USING
974 669 : using ::rtl::OStringBuffer;
975 : #endif
976 :
977 : #endif /* __cplusplus */
978 : #endif // INCLUDED_RTL_STRBUF_HXX
979 :
980 :
981 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|