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_USTRBUF_HXX
21 : #define INCLUDED_RTL_USTRBUF_HXX
22 :
23 : #include <sal/config.h>
24 :
25 : #include <cassert>
26 : #include <string.h>
27 :
28 : #include <rtl/ustrbuf.h>
29 : #include <rtl/ustring.hxx>
30 : #include <rtl/stringutils.hxx>
31 : #include <sal/types.h>
32 :
33 : #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
34 : #include <rtl/stringconcat.hxx>
35 : #endif
36 :
37 : // The unittest uses slightly different code to help check that the proper
38 : // calls are made. The class is put into a different namespace to make
39 : // sure the compiler generates a different (if generating also non-inline)
40 : // copy of the function and does not merge them together. The class
41 : // is "brought" into the proper rtl namespace by a typedef below.
42 : #ifdef RTL_STRING_UNITTEST
43 : #define rtl rtlunittest
44 : #endif
45 :
46 : namespace rtl
47 : {
48 :
49 : #ifdef RTL_STRING_UNITTEST
50 : #undef rtl
51 : #endif
52 :
53 : /** A string buffer implements a mutable sequence of characters.
54 : */
55 : class SAL_WARN_UNUSED OUStringBuffer
56 : {
57 : public:
58 : /**
59 : Constructs a string buffer with no characters in it and an
60 : initial capacity of 16 characters.
61 : */
62 35102788 : OUStringBuffer()
63 : : pData(NULL)
64 35102788 : , nCapacity( 16 )
65 : {
66 35102788 : rtl_uString_new_WithLength( &pData, nCapacity );
67 35102788 : }
68 :
69 : /**
70 : Allocates a new string buffer that contains the same sequence of
71 : characters as the string buffer argument.
72 :
73 : @param value a <code>OUStringBuffer</code>.
74 : */
75 133562 : OUStringBuffer( const OUStringBuffer & value )
76 : : pData(NULL)
77 133562 : , nCapacity( value.nCapacity )
78 : {
79 133562 : rtl_uStringbuffer_newFromStringBuffer( &pData, value.nCapacity, value.pData );
80 133562 : }
81 :
82 : /**
83 : Constructs a string buffer with no characters in it and an
84 : initial capacity specified by the <code>length</code> argument.
85 :
86 : @param length the initial capacity.
87 : */
88 87709748 : explicit OUStringBuffer(int length)
89 : : pData(NULL)
90 87709748 : , nCapacity( length )
91 : {
92 87709748 : rtl_uString_new_WithLength( &pData, length );
93 87709747 : }
94 : #if __cplusplus >= 201103L
95 1422 : explicit OUStringBuffer(unsigned int length)
96 1422 : : OUStringBuffer(static_cast<int>(length))
97 : {
98 1422 : }
99 : #if SAL_TYPES_SIZEOFLONG == 4
100 : // additional overloads for sal_Int32 sal_uInt32
101 : explicit OUStringBuffer(long length)
102 : : OUStringBuffer(static_cast<int>(length))
103 : {
104 : }
105 : explicit OUStringBuffer(unsigned long length)
106 : : OUStringBuffer(static_cast<int>(length))
107 : {
108 : }
109 : #endif
110 : // avoid obvious bugs
111 : explicit OUStringBuffer(char) = delete;
112 : explicit OUStringBuffer(sal_Unicode) = delete;
113 : #endif
114 :
115 : /**
116 : Constructs a string buffer so that it represents the same
117 : sequence of characters as the string argument.
118 :
119 : The initial
120 : capacity of the string buffer is <code>16</code> plus the length
121 : of the string argument.
122 :
123 : @param value the initial contents of the buffer.
124 : */
125 124033744 : OUStringBuffer(const OUString& value)
126 : : pData(NULL)
127 124033744 : , nCapacity( value.getLength() + 16 )
128 : {
129 124033744 : rtl_uStringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
130 124033744 : }
131 :
132 : template< typename T >
133 62039 : OUStringBuffer( T& literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
134 : : pData(NULL)
135 62039 : , nCapacity( libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 + 16 )
136 : {
137 : assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
138 62039 : rtl_uString_newFromLiteral( &pData, literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, 16 );
139 : #ifdef RTL_STRING_UNITTEST
140 3 : rtl_string_unittest_const_literal = true;
141 : #endif
142 62039 : }
143 :
144 : #ifdef RTL_STRING_UNITTEST
145 : /**
146 : * Only used by unittests to detect incorrect conversions.
147 : * @internal
148 : */
149 : template< typename T >
150 8 : OUStringBuffer( T&, typename libreoffice_internal::ExceptConstCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
151 : {
152 8 : pData = 0;
153 8 : nCapacity = 10;
154 8 : rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
155 8 : rtl_string_unittest_invalid_conversion = true;
156 8 : }
157 : /**
158 : * Only used by unittests to detect incorrect conversions.
159 : * @internal
160 : */
161 : template< typename T >
162 1 : OUStringBuffer( const T&, typename libreoffice_internal::ExceptCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
163 : {
164 1 : pData = 0;
165 1 : nCapacity = 10;
166 1 : rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
167 1 : rtl_string_unittest_invalid_conversion = true;
168 1 : }
169 : #endif
170 :
171 : #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
172 : /**
173 : @overload
174 : @internal
175 : */
176 : template< typename T1, typename T2 >
177 81 : OUStringBuffer( const OUStringConcat< T1, T2 >& c )
178 : {
179 81 : const sal_Int32 l = c.length();
180 81 : nCapacity = l + 16;
181 81 : pData = rtl_uString_alloc( nCapacity );
182 81 : sal_Unicode* end = c.addData( pData->buffer );
183 81 : *end = '\0';
184 81 : pData->length = end - pData->buffer;
185 : // TODO realloc in case pData->>length is noticeably smaller than l ?
186 81 : }
187 : #endif
188 : /** Assign to this a copy of value.
189 : */
190 2764273 : OUStringBuffer& operator = ( const OUStringBuffer& value )
191 : {
192 2764273 : if (this != &value)
193 : {
194 : rtl_uStringbuffer_newFromStringBuffer(&pData,
195 : value.nCapacity,
196 2764227 : value.pData);
197 2764227 : nCapacity = value.nCapacity;
198 : }
199 2764273 : return *this;
200 : }
201 :
202 : /**
203 : Release the string data.
204 : */
205 247038898 : ~OUStringBuffer()
206 : {
207 247038898 : rtl_uString_release( pData );
208 247038898 : }
209 :
210 : /**
211 : Fill the string data in the new string and clear the buffer.
212 :
213 : This method is more efficient than the constructor of the string. It does
214 : not copy the buffer.
215 :
216 : @return the string previously contained in the buffer.
217 : */
218 195356753 : OUString makeStringAndClear()
219 : {
220 : return OUString(
221 : rtl_uStringBuffer_makeStringAndClear( &pData, &nCapacity ),
222 195356753 : SAL_NO_ACQUIRE );
223 : }
224 :
225 : /**
226 : Returns the length (character count) of this string buffer.
227 :
228 : @return the number of characters in this string buffer.
229 : */
230 477676294 : sal_Int32 getLength() const
231 : {
232 477676294 : return pData->length;
233 : }
234 :
235 : /**
236 : Checks if a string buffer is empty.
237 :
238 : @return true if the string buffer is empty;
239 : false, otherwise.
240 :
241 : @since LibreOffice 4.1
242 : */
243 2348346 : bool isEmpty() const
244 : {
245 2348346 : return pData->length == 0;
246 : }
247 :
248 : /**
249 : Returns the current capacity of the String buffer.
250 :
251 : The capacity
252 : is the amount of storage available for newly inserted
253 : characters. The real buffer size is 2 bytes longer, because
254 : all strings are 0 terminated.
255 :
256 : @return the current capacity of this string buffer.
257 : */
258 : sal_Int32 getCapacity() const
259 : {
260 : return nCapacity;
261 : }
262 :
263 : /**
264 : Ensures that the capacity of the buffer is at least equal to the
265 : specified minimum.
266 :
267 : The new capacity will be at least as large as the maximum of the current
268 : length (so that no contents of the buffer is destroyed) and the given
269 : minimumCapacity. If the given minimumCapacity is negative, nothing is
270 : changed.
271 :
272 : @param minimumCapacity the minimum desired capacity.
273 : */
274 85484248 : void ensureCapacity(sal_Int32 minimumCapacity)
275 : {
276 85484248 : rtl_uStringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
277 85484248 : }
278 :
279 : /**
280 : Sets the length of this String buffer.
281 :
282 : If the <code>newLength</code> argument is less than the current
283 : length of the string buffer, the string buffer is truncated to
284 : contain exactly the number of characters given by the
285 : <code>newLength</code> argument.
286 : <p>
287 : If the <code>newLength</code> argument is greater than or equal
288 : to the current length, sufficient null characters
289 : (<code>'\u0000'</code>) are appended to the string buffer so that
290 : length becomes the <code>newLength</code> argument.
291 : <p>
292 : The <code>newLength</code> argument must be greater than or equal
293 : to <code>0</code>.
294 :
295 : @param newLength the new length of the buffer.
296 : */
297 1289554 : void setLength(sal_Int32 newLength)
298 : {
299 : assert(newLength >= 0);
300 : // Avoid modifications if pData points to const empty string:
301 1289554 : if( newLength != pData->length )
302 : {
303 89684 : if( newLength > nCapacity )
304 9563 : rtl_uStringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
305 : else
306 80121 : pData->buffer[newLength] = 0;
307 89684 : pData->length = newLength;
308 : }
309 1289554 : }
310 :
311 : /**
312 : Returns the character at a specific index in this string buffer.
313 :
314 : The first character of a string buffer is at index
315 : <code>0</code>, the next at index <code>1</code>, and so on, for
316 : array indexing.
317 : <p>
318 : The index argument must be greater than or equal to
319 : <code>0</code>, and less than the length of this string buffer.
320 :
321 : @param index the index of the desired character.
322 : @return the character at the specified index of this string buffer.
323 : */
324 : SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
325 : sal_Unicode charAt( sal_Int32 index ) const
326 : {
327 : assert(index >= 0 && index < pData->length);
328 : return pData->buffer[ index ];
329 : }
330 :
331 : /**
332 : The character at the specified index of this string buffer is set
333 : to <code>ch</code>.
334 :
335 : The index argument must be greater than or equal to
336 : <code>0</code>, and less than the length of this string buffer.
337 :
338 : @param index the index of the character to modify.
339 : @param ch the new character.
340 : */
341 : SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
342 : OUStringBuffer & setCharAt(sal_Int32 index, sal_Unicode ch)
343 : {
344 : assert(index >= 0 && index < pData->length);
345 : pData->buffer[ index ] = ch;
346 : return *this;
347 : }
348 :
349 : /**
350 : Return a null terminated unicode character array.
351 : */
352 4184697 : const sal_Unicode* getStr() const { return pData->buffer; }
353 :
354 : /**
355 : Access to individual characters.
356 :
357 : @param index must be non-negative and less than length.
358 :
359 : @return a reference to the character at the given index.
360 :
361 : @since LibreOffice 3.5
362 : */
363 726896079 : sal_Unicode & operator [](sal_Int32 index)
364 : {
365 : assert(index >= 0 && index < pData->length);
366 726896079 : return pData->buffer[index];
367 : }
368 :
369 : /**
370 : Access to individual characters.
371 :
372 : @param index must be non-negative and less than length.
373 :
374 : @return a reference to the character at the given index.
375 :
376 : @since LibreOffice 4.2
377 : */
378 576 : const sal_Unicode & operator [](sal_Int32 index) const
379 : {
380 : assert(index >= 0 && index < pData->length);
381 576 : return pData->buffer[index];
382 : }
383 :
384 : /**
385 : Return a OUString instance reflecting the current content
386 : of this OUStringBuffer.
387 : */
388 181771 : const OUString toString() const
389 : {
390 181771 : return OUString(pData->buffer, pData->length);
391 : }
392 :
393 : /**
394 : Appends the string to this string buffer.
395 :
396 : The characters of the <code>OUString</code> argument are appended, in
397 : order, to the contents of this string buffer, increasing the
398 : length of this string buffer by the length of the argument.
399 :
400 : @param str a string.
401 : @return this string buffer.
402 : */
403 34507245 : OUStringBuffer & append(const OUString &str)
404 : {
405 34507245 : return append( str.getStr(), str.getLength() );
406 : }
407 :
408 : /**
409 : Appends the content of a stringbuffer to this string buffer.
410 :
411 : The characters of the <code>OUStringBuffer</code> argument are appended, in
412 : order, to the contents of this string buffer, increasing the
413 : length of this string buffer by the length of the argument.
414 :
415 : @param str a string.
416 : @return this string buffer.
417 :
418 : @since LibreOffice 4.0
419 : */
420 205 : OUStringBuffer & append(const OUStringBuffer &str)
421 : {
422 205 : if(!str.isEmpty())
423 : {
424 204 : append( str.getStr(), str.getLength() );
425 : }
426 205 : return *this;
427 : }
428 :
429 : /**
430 : Appends the string representation of the <code>char</code> array
431 : argument to this string buffer.
432 :
433 : The characters of the array argument are appended, in order, to
434 : the contents of this string buffer. The length of this string
435 : buffer increases by the length of the argument.
436 :
437 : @param str the characters to be appended.
438 : @return this string buffer.
439 : */
440 47236 : OUStringBuffer & append( const sal_Unicode * str )
441 : {
442 47236 : return append( str, rtl_ustr_getLength( str ) );
443 : }
444 :
445 : /**
446 : Appends the string representation of the <code>char</code> array
447 : argument to this string buffer.
448 :
449 : Characters of the character array <code>str</code> are appended,
450 : in order, to the contents of this string buffer. The length of this
451 : string buffer increases by the value of <code>len</code>.
452 :
453 : @param str the characters to be appended; must be non-null, and must
454 : point to at least len characters
455 : @param len the number of characters to append; must be non-negative
456 : @return this string buffer.
457 : */
458 219878895 : OUStringBuffer & append( const sal_Unicode * str, sal_Int32 len)
459 : {
460 : assert( len == 0 || str != 0 ); // cannot assert that in rtl_uStringbuffer_insert
461 219878895 : rtl_uStringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
462 219878896 : return *this;
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 1740730 : typename libreoffice_internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type append( T& literal )
472 : {
473 : assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
474 1740730 : rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), literal,
475 1740730 : libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 );
476 1740730 : return *this;
477 : }
478 :
479 : #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
480 : /**
481 : @overload
482 : @internal
483 : */
484 : template< typename T1, typename T2 >
485 221511 : OUStringBuffer& append( const OUStringConcat< T1, T2 >& c )
486 : {
487 221511 : const int l = c.length();
488 221511 : if( l == 0 )
489 0 : return *this;
490 221511 : rtl_uStringbuffer_ensureCapacity( &pData, &nCapacity, pData->length + l );
491 221511 : sal_Unicode* end = c.addData( pData->buffer + pData->length );
492 221511 : *end = '\0';
493 221511 : pData->length = end - pData->buffer;
494 221511 : return *this;
495 : }
496 : #endif
497 :
498 : /**
499 : Appends a 8-Bit ASCII character string to this string buffer.
500 :
501 : Since this method is optimized for performance. the ASCII
502 : character values are not converted in any way. The caller
503 : has to make sure that all ASCII characters are in the
504 : allowed range between 0 and 127. The ASCII string must be
505 : NULL-terminated.
506 : <p>
507 : The characters of the array argument are appended, in order, to
508 : the contents of this string buffer. The length of this string
509 : buffer increases by the length of the argument.
510 :
511 : @param str the 8-Bit ASCII characters to be appended.
512 : @return this string buffer.
513 : */
514 172310196 : OUStringBuffer & appendAscii( const sal_Char * str )
515 : {
516 172310196 : return appendAscii( str, rtl_str_getLength( str ) );
517 : }
518 :
519 : /**
520 : Appends a 8-Bit ASCII character string to this string buffer.
521 :
522 : Since this method is optimized for performance. the ASCII
523 : character values are not converted in any way. The caller
524 : has to make sure that all ASCII characters are in the
525 : allowed range between 0 and 127. The ASCII string must be
526 : NULL-terminated.
527 : <p>
528 : Characters of the character array <code>str</code> are appended,
529 : in order, to the contents of this string buffer. The length of this
530 : string buffer increases by the value of <code>len</code>.
531 :
532 : @param str the 8-Bit ASCII characters to be appended; must be non-null,
533 : and must point to at least len characters
534 : @param len the number of characters to append; must be non-negative
535 : @return this string buffer.
536 : */
537 172451959 : OUStringBuffer & appendAscii( const sal_Char * str, sal_Int32 len)
538 : {
539 172451959 : rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), str, len );
540 172451959 : return *this;
541 : }
542 :
543 : /**
544 : Appends the string representation of the <code>bool</code>
545 : argument to the string buffer.
546 :
547 : The argument is converted to a string as if by the method
548 : <code>String.valueOf</code>, and the characters of that
549 : string are then appended to this string buffer.
550 :
551 : @param b a <code>bool</code>.
552 : @return this string buffer.
553 :
554 : @since LibreOffice 4.1
555 : */
556 0 : OUStringBuffer & append(bool b)
557 : {
558 : sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
559 0 : return append( sz, rtl_ustr_valueOfBoolean( sz, b ) );
560 : }
561 :
562 : /// @cond INTERNAL
563 : // Pointer can be automatically converted to bool, which is unwanted here.
564 : // Explicitly delete all pointer append() overloads to prevent this
565 : // (except for char* and sal_Unicode* overloads, which are handled elsewhere).
566 : template< typename T >
567 : typename libreoffice_internal::Enable< void,
568 : !libreoffice_internal::CharPtrDetector< T* >::ok && !libreoffice_internal::SalUnicodePtrDetector< T* >::ok >::Type
569 : append( T* ) SAL_DELETED_FUNCTION;
570 : /// @endcond
571 :
572 : // This overload is needed because OUString has a ctor from rtl_uString*, but
573 : // the bool overload above would be preferred to the conversion.
574 : /**
575 : @internal
576 : */
577 597209 : OUStringBuffer & append(rtl_uString* str)
578 : {
579 597209 : return append( OUString( str ));
580 : }
581 :
582 : /**
583 : Appends the string representation of the <code>sal_Bool</code>
584 : argument to the string buffer.
585 :
586 : The argument is converted to a string as if by the method
587 : <code>String.valueOf</code>, and the characters of that
588 : string are then appended to this string buffer.
589 :
590 : @param b a <code>sal_Bool</code>.
591 : @return this string buffer.
592 : */
593 : OUStringBuffer & append(sal_Bool b)
594 : {
595 : sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
596 : return append( sz, rtl_ustr_valueOfBoolean( sz, b ) );
597 : }
598 :
599 : /**
600 : Appends the string representation of the ASCII <code>char</code>
601 : argument to this string buffer.
602 :
603 : The argument is appended to the contents of this string buffer.
604 : The length of this string buffer increases by <code>1</code>.
605 :
606 : @param c an ASCII <code>char</code>.
607 : @return this string buffer.
608 :
609 : @since LibreOffice 3.5
610 : */
611 38981014 : OUStringBuffer & append(char c)
612 : {
613 : assert(static_cast< unsigned char >(c) <= 0x7F);
614 38981014 : return append(sal_Unicode(c));
615 : }
616 :
617 : /**
618 : Appends the string representation of the <code>char</code>
619 : argument to this string buffer.
620 :
621 : The argument is appended to the contents of this string buffer.
622 : The length of this string buffer increases by <code>1</code>.
623 :
624 : @param c a <code>char</code>.
625 : @return this string buffer.
626 : */
627 182318941 : OUStringBuffer & append(sal_Unicode c)
628 : {
629 182318941 : return append( &c, 1 );
630 : }
631 :
632 : /**
633 : Appends the string representation of the <code>sal_Int32</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 i an <code>sal_Int32</code>.
641 : @param radix the radix
642 : @return this string buffer.
643 : */
644 1479156 : OUStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
645 : {
646 : sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT32];
647 1479156 : return append( sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
648 : }
649 :
650 : /**
651 : Appends the string representation of the <code>long</code>
652 : argument to this string buffer.
653 :
654 : The argument is converted to a string as if by the method
655 : <code>String.valueOf</code>, and the characters of that
656 : string are then appended to this string buffer.
657 :
658 : @param l a <code>long</code>.
659 : @param radix the radix
660 : @return this string buffer.
661 : */
662 741965 : OUStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
663 : {
664 : sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT64];
665 741965 : return append( sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
666 : }
667 :
668 : /**
669 : Appends the string representation of the <code>float</code>
670 : argument to this string buffer.
671 :
672 : The argument is converted to a string as if by the method
673 : <code>String.valueOf</code>, and the characters of that
674 : string are then appended to this string buffer.
675 :
676 : @param f a <code>float</code>.
677 : @return this string buffer.
678 : */
679 0 : OUStringBuffer & append(float f)
680 : {
681 : sal_Unicode sz[RTL_USTR_MAX_VALUEOFFLOAT];
682 0 : return append( sz, rtl_ustr_valueOfFloat( sz, f ) );
683 : }
684 :
685 : /**
686 : Appends the string representation of the <code>double</code>
687 : argument to this string buffer.
688 :
689 : The argument is converted to a string as if by the method
690 : <code>String.valueOf</code>, and the characters of that
691 : string are then appended to this string buffer.
692 :
693 : @param d a <code>double</code>.
694 : @return this string buffer.
695 : */
696 4863 : OUStringBuffer & append(double d)
697 : {
698 : sal_Unicode sz[RTL_USTR_MAX_VALUEOFDOUBLE];
699 4863 : return append( sz, rtl_ustr_valueOfDouble( sz, d ) );
700 : }
701 :
702 : /**
703 : Appends a single UTF-32 character to this string buffer.
704 :
705 : <p>The single UTF-32 character will be represented within the string
706 : buffer as either one or two UTF-16 code units.</p>
707 :
708 : @param c a well-formed UTF-32 code unit (that is, a value in the range
709 : <code>0</code>–<code>0x10FFFF</code>, but excluding
710 : <code>0xD800</code>–<code>0xDFFF</code>)
711 :
712 : @return
713 : this string buffer
714 : */
715 4583464 : OUStringBuffer & appendUtf32(sal_uInt32 c) {
716 4583464 : return insertUtf32(getLength(), c);
717 : }
718 :
719 : /**
720 : Unsafe way to make space for a fixed amount of characters to be appended
721 : into this OUStringBuffer.
722 :
723 : A call to this function must immediately be followed by code that
724 : completely fills the uninitialized block pointed to by the return value.
725 :
726 : @param length the length of the uninitialized block of sal_Unicode
727 : entities; must be non-negative
728 :
729 : @return a pointer to the start of the uninitialized block; only valid
730 : until this OUStringBuffer's capacity changes
731 :
732 : @since LibreOffice 4.4
733 : */
734 3 : sal_Unicode * appendUninitialized(sal_Int32 length) {
735 3 : sal_Int32 n = getLength();
736 3 : rtl_uStringbuffer_insert(&pData, &nCapacity, n, 0, length);
737 3 : return pData->buffer + n;
738 : }
739 :
740 : /**
741 : Inserts the string into this string buffer.
742 :
743 : The characters of the <code>String</code> argument are inserted, in
744 : order, into this string buffer at the indicated offset. The length
745 : of this string buffer is increased by the length of the argument.
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 str a string.
753 : @return this string buffer.
754 : */
755 458213 : OUStringBuffer & insert(sal_Int32 offset, const OUString & str)
756 : {
757 458213 : return insert( offset, str.getStr(), str.getLength() );
758 : }
759 :
760 : /**
761 : Inserts the string representation of the <code>char</code> array
762 : argument into this string buffer.
763 :
764 : The characters of the array argument are inserted into the
765 : contents of this string buffer at the position indicated by
766 : <code>offset</code>. The length of this string buffer increases by
767 : the length of the argument.
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 str a character array.
775 : @return this string buffer.
776 : */
777 0 : OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str )
778 : {
779 0 : return insert( offset, str, rtl_ustr_getLength( str ) );
780 : }
781 :
782 : /**
783 : Inserts the string representation of the <code>char</code> array
784 : argument into this string buffer.
785 :
786 : The characters of the array argument are inserted into the
787 : contents of this string buffer at the position indicated by
788 : <code>offset</code>. The length of this string buffer increases by
789 : the length of the argument.
790 : <p>
791 : The offset argument must be greater than or equal to
792 : <code>0</code>, and less than or equal to the length of this
793 : string buffer.
794 :
795 : @param offset the offset.
796 : @param str a character array.
797 : @param len the number of characters to append.
798 : @return this string buffer.
799 : */
800 480892 : OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str, sal_Int32 len)
801 : {
802 : assert( len == 0 || str != 0 ); // cannot assert that in rtl_uStringbuffer_insert
803 480892 : rtl_uStringbuffer_insert( &pData, &nCapacity, offset, str, len );
804 480892 : return *this;
805 : }
806 :
807 : /**
808 : @overload
809 : This function accepts an ASCII string literal as its argument.
810 : @since LibreOffice 3.6
811 : */
812 : template< typename T >
813 75530 : typename libreoffice_internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type insert( sal_Int32 offset, T& literal )
814 : {
815 : assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
816 75530 : rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, offset, literal,
817 75530 : libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 );
818 75530 : return *this;
819 : }
820 :
821 : /**
822 : Inserts the string representation of the <code>sal_Bool</code>
823 : argument into this string buffer.
824 :
825 : The second argument is converted to a string as if by the method
826 : <code>String.valueOf</code>, and the characters of that
827 : string are then inserted into this string buffer at the indicated
828 : offset.
829 : <p>
830 : The offset argument must be greater than or equal to
831 : <code>0</code>, and less than or equal to the length of this
832 : string buffer.
833 :
834 : @param offset the offset.
835 : @param b a <code>sal_Bool</code>.
836 : @return this string buffer.
837 : */
838 : OUStringBuffer & insert(sal_Int32 offset, sal_Bool b)
839 : {
840 : sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
841 : return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
842 : }
843 :
844 : /**
845 : Inserts the string representation of the <code>bool</code>
846 : argument into this string buffer.
847 :
848 : The second argument is converted to a string as if by the method
849 : <code>OUString::boolean</code>, and the characters of that
850 : string are then inserted into this string buffer at the indicated
851 : offset.
852 : <p>
853 : The offset argument must be greater than or equal to
854 : <code>0</code>, and less than or equal to the length of this
855 : string buffer.
856 :
857 : @param offset the offset.
858 : @param b a <code>bool</code>.
859 : @return this string buffer.
860 :
861 : @since LibreOffice 4.3
862 : */
863 : OUStringBuffer & insert(sal_Int32 offset, bool b)
864 : {
865 : sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
866 : return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
867 : }
868 :
869 : /**
870 : Inserts the string representation of the <code>char</code>
871 : argument into this string buffer.
872 :
873 : The second argument is inserted into the contents of this string
874 : buffer at the position indicated by <code>offset</code>. The length
875 : of this string buffer increases by one.
876 : <p>
877 : The offset argument must be greater than or equal to
878 : <code>0</code>, and less than or equal to the length of this
879 : string buffer.
880 :
881 : @param offset the offset.
882 : @param c a <code>char</code>.
883 : @return this string buffer.
884 :
885 : @since LibreOffice 3.6
886 : */
887 10756 : OUStringBuffer & insert(sal_Int32 offset, char c)
888 : {
889 10756 : sal_Unicode u = c;
890 10756 : return insert( offset, &u, 1 );
891 : }
892 :
893 : /**
894 : Inserts the string representation of the <code>char</code>
895 : argument into this string buffer.
896 :
897 : The second argument is inserted into the contents of this string
898 : buffer at the position indicated by <code>offset</code>. The length
899 : of this string buffer increases by one.
900 : <p>
901 : The offset argument must be greater than or equal to
902 : <code>0</code>, and less than or equal to the length of this
903 : string buffer.
904 :
905 : @param offset the offset.
906 : @param c a <code>char</code>.
907 : @return this string buffer.
908 : */
909 11923 : OUStringBuffer & insert(sal_Int32 offset, sal_Unicode c)
910 : {
911 11923 : return insert( offset, &c, 1 );
912 : }
913 :
914 : /**
915 : Inserts the string representation of the second <code>sal_Int32</code>
916 : argument into this string buffer.
917 :
918 : The second argument is converted to a string as if by the method
919 : <code>String.valueOf</code>, and the characters of that
920 : string are then inserted into this string buffer at the indicated
921 : offset.
922 : <p>
923 : The offset argument must be greater than or equal to
924 : <code>0</code>, and less than or equal to the length of this
925 : string buffer.
926 :
927 : @param offset the offset.
928 : @param i an <code>sal_Int32</code>.
929 : @param radix the radix.
930 : @return this string buffer.
931 : @exception StringIndexOutOfBoundsException if the offset is invalid.
932 : */
933 0 : OUStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
934 : {
935 : sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT32];
936 0 : return insert( offset, sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
937 : }
938 :
939 : /**
940 : Inserts the string representation of the <code>long</code>
941 : argument into this string buffer.
942 :
943 : The second argument is converted to a string as if by the method
944 : <code>String.valueOf</code>, and the characters of that
945 : string are then inserted into this string buffer at the indicated
946 : offset.
947 : <p>
948 : The offset argument must be greater than or equal to
949 : <code>0</code>, and less than or equal to the length of this
950 : string buffer.
951 :
952 : @param offset the offset.
953 : @param l a <code>long</code>.
954 : @param radix the radix.
955 : @return this string buffer.
956 : @exception StringIndexOutOfBoundsException if the offset is invalid.
957 : */
958 : OUStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
959 : {
960 : sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT64];
961 : return insert( offset, sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
962 : }
963 :
964 : /**
965 : Inserts the string representation of the <code>float</code>
966 : argument into this string buffer.
967 :
968 : The second argument is converted to a string as if by the method
969 : <code>String.valueOf</code>, and the characters of that
970 : string are then inserted into this string buffer at the indicated
971 : offset.
972 : <p>
973 : The offset argument must be greater than or equal to
974 : <code>0</code>, and less than or equal to the length of this
975 : string buffer.
976 :
977 : @param offset the offset.
978 : @param f a <code>float</code>.
979 : @return this string buffer.
980 : @exception StringIndexOutOfBoundsException if the offset is invalid.
981 : */
982 : OUStringBuffer insert(sal_Int32 offset, float f)
983 : {
984 : sal_Unicode sz[RTL_USTR_MAX_VALUEOFFLOAT];
985 : return insert( offset, sz, rtl_ustr_valueOfFloat( sz, f ) );
986 : }
987 :
988 : /**
989 : Inserts the string representation of the <code>double</code>
990 : argument into this string buffer.
991 :
992 : The second argument is converted to a string as if by the method
993 : <code>String.valueOf</code>, and the characters of that
994 : string are then inserted into this string buffer at the indicated
995 : offset.
996 : <p>
997 : The offset argument must be greater than or equal to
998 : <code>0</code>, and less than or equal to the length of this
999 : string buffer.
1000 :
1001 : @param offset the offset.
1002 : @param d a <code>double</code>.
1003 : @return this string buffer.
1004 : @exception StringIndexOutOfBoundsException if the offset is invalid.
1005 : */
1006 : OUStringBuffer & insert(sal_Int32 offset, double d)
1007 : {
1008 : sal_Unicode sz[RTL_USTR_MAX_VALUEOFDOUBLE];
1009 : return insert( offset, sz, rtl_ustr_valueOfDouble( sz, d ) );
1010 : }
1011 :
1012 : /**
1013 : Inserts a single UTF-32 character into this string buffer.
1014 :
1015 : <p>The single UTF-32 character will be represented within the string
1016 : buffer as either one or two UTF-16 code units.</p>
1017 :
1018 : @param offset the offset into this string buffer (from zero to the length
1019 : of this string buffer, inclusive)
1020 :
1021 : @param c a well-formed UTF-32 code unit (that is, a value in the range
1022 : <code>0</code>–<code>0x10FFFF</code>, but excluding
1023 : <code>0xD800</code>–<code>0xDFFF</code>)
1024 :
1025 : @return this string buffer
1026 : */
1027 4583466 : OUStringBuffer & insertUtf32(sal_Int32 offset, sal_uInt32 c) {
1028 4583466 : rtl_uStringbuffer_insertUtf32(&pData, &nCapacity, offset, c);
1029 4583466 : return *this;
1030 : }
1031 :
1032 : /**
1033 : Removes the characters in a substring of this sequence.
1034 :
1035 : The substring begins at the specified <code>start</code> and
1036 : is <code>len</code> characters long.
1037 :
1038 : start must be >= 0 && <= This->length
1039 :
1040 : @param start The beginning index, inclusive
1041 : @param len The substring length
1042 : @return this string buffer.
1043 : */
1044 2171041 : OUStringBuffer & remove( sal_Int32 start, sal_Int32 len )
1045 : {
1046 2171041 : rtl_uStringbuffer_remove( &pData, start, len );
1047 2171041 : return *this;
1048 : }
1049 :
1050 : /**
1051 : Removes the tail of a string buffer start at the indicate position
1052 :
1053 : start must be >= 0 && <= This->length
1054 :
1055 : @param start The beginning index, inclusive. default to 0
1056 : @return this string buffer.
1057 :
1058 : @since LibreOffice 4.0
1059 : */
1060 364 : OUStringBuffer & truncate( sal_Int32 start = 0 )
1061 : {
1062 364 : rtl_uStringbuffer_remove( &pData, start, getLength() - start );
1063 364 : return *this;
1064 : }
1065 :
1066 : /**
1067 : Replace all occurrences of
1068 : oldChar in this string buffer with newChar.
1069 :
1070 : @since LibreOffice 4.0
1071 :
1072 : @param oldChar the old character.
1073 : @param newChar the new character.
1074 : @return this string buffer
1075 : */
1076 3558 : OUStringBuffer& replace( sal_Unicode oldChar, sal_Unicode newChar )
1077 : {
1078 3558 : sal_Int32 index = 0;
1079 8603 : while((index = indexOf(oldChar, index)) >= 0)
1080 : {
1081 1487 : pData->buffer[ index ] = newChar;
1082 : }
1083 3558 : return *this;
1084 : }
1085 :
1086 : /** Allows access to the internal data of this OUStringBuffer, for effective
1087 : manipulation.
1088 :
1089 : This method should be used with care. After you have called this
1090 : method, you may use the returned pInternalData or pInternalCapacity only
1091 : as long as you make no other method call on this OUStringBuffer.
1092 :
1093 : @param pInternalData
1094 : This output parameter receives a pointer to the internal data
1095 : (rtl_uString pointer). pInternalData itself must not be null.
1096 :
1097 : @param pInternalCapacity
1098 : This output parameter receives a pointer to the internal capacity.
1099 : pInternalCapacity itself must not be null.
1100 : */
1101 20415 : inline void accessInternals(rtl_uString *** pInternalData,
1102 : sal_Int32 ** pInternalCapacity)
1103 : {
1104 20415 : *pInternalData = &pData;
1105 20415 : *pInternalCapacity = &nCapacity;
1106 20415 : }
1107 :
1108 :
1109 : /**
1110 : Returns the index within this string of the first occurrence of the
1111 : specified character, starting the search at the specified index.
1112 :
1113 : @since LibreOffice 4.0
1114 :
1115 : @param ch character to be located.
1116 : @param fromIndex the index to start the search from.
1117 : The index must be greater or equal than 0
1118 : and less or equal as the string length.
1119 : @return the index of the first occurrence of the character in the
1120 : character sequence represented by this string that is
1121 : greater than or equal to fromIndex, or
1122 : -1 if the character does not occur.
1123 : */
1124 16667 : sal_Int32 indexOf( sal_Unicode ch, sal_Int32 fromIndex = 0 ) const
1125 : {
1126 : assert( fromIndex >= 0 && fromIndex <= pData->length );
1127 16667 : sal_Int32 ret = rtl_ustr_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch );
1128 16667 : return (ret < 0 ? ret : ret+fromIndex);
1129 : }
1130 :
1131 : /**
1132 : Returns the index within this string of the last occurrence of the
1133 : specified character, searching backward starting at the end.
1134 :
1135 : @since LibreOffice 4.0
1136 :
1137 : @param ch character to be located.
1138 : @return the index of the last occurrence of the character in the
1139 : character sequence represented by this string, or
1140 : -1 if the character does not occur.
1141 : */
1142 1550 : sal_Int32 lastIndexOf( sal_Unicode ch ) const
1143 : {
1144 1550 : return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch );
1145 : }
1146 :
1147 : /**
1148 : Returns the index within this string of the last occurrence of the
1149 : specified character, searching backward starting before the specified
1150 : index.
1151 :
1152 : @since LibreOffice 4.0
1153 :
1154 : @param ch character to be located.
1155 : @param fromIndex the index before which to start the search.
1156 : @return the index of the last occurrence of the character in the
1157 : character sequence represented by this string that
1158 : is less than fromIndex, or -1
1159 : if the character does not occur before that point.
1160 : */
1161 : sal_Int32 lastIndexOf( sal_Unicode ch, sal_Int32 fromIndex ) const
1162 : {
1163 : assert( fromIndex >= 0 && fromIndex <= pData->length );
1164 : return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch );
1165 : }
1166 :
1167 : /**
1168 : Returns the index within this string of the first occurrence of the
1169 : specified substring, starting at the specified index.
1170 :
1171 : If str doesn't include any character, always -1 is
1172 : returned. This is also the case, if both strings are empty.
1173 :
1174 : @since LibreOffice 4.0
1175 :
1176 : @param str the substring to search for.
1177 : @param fromIndex the index to start the search from.
1178 : @return If the string argument occurs one or more times as a substring
1179 : within this string at the starting index, then the index
1180 : of the first character of the first such substring is
1181 : returned. If it does not occur as a substring starting
1182 : at fromIndex or beyond, -1 is returned.
1183 : */
1184 4037 : sal_Int32 indexOf( const OUString & str, sal_Int32 fromIndex = 0 ) const
1185 : {
1186 : assert( fromIndex >= 0 && fromIndex <= pData->length );
1187 4037 : sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1188 8074 : str.pData->buffer, str.pData->length );
1189 4037 : return (ret < 0 ? ret : ret+fromIndex);
1190 : }
1191 :
1192 : /**
1193 : @overload
1194 : This function accepts an ASCII string literal as its argument.
1195 :
1196 : @since LibreOffice 4.0
1197 : */
1198 : template< typename T >
1199 : typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const
1200 : {
1201 : assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1202 : sal_Int32 ret = rtl_ustr_indexOfAscii_WithLength(
1203 : pData->buffer + fromIndex, pData->length - fromIndex, literal,
1204 : libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1);
1205 : return ret < 0 ? ret : ret + fromIndex;
1206 : }
1207 :
1208 : /**
1209 : Returns the index within this string of the last occurrence of
1210 : the specified substring, searching backward starting at the end.
1211 :
1212 : The returned index indicates the starting index of the substring
1213 : in this string.
1214 : If str doesn't include any character, always -1 is
1215 : returned. This is also the case, if both strings are empty.
1216 :
1217 : @since LibreOffice 4.0
1218 :
1219 : @param str the substring to search for.
1220 : @return If the string argument occurs one or more times as a substring
1221 : within this string, then the index of the first character of
1222 : the last such substring is returned. If it does not occur as
1223 : a substring, -1 is returned.
1224 : */
1225 : sal_Int32 lastIndexOf( const OUString & str ) const
1226 : {
1227 : return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1228 : str.pData->buffer, str.pData->length );
1229 : }
1230 :
1231 : /**
1232 : Returns the index within this string of the last occurrence of
1233 : the specified substring, searching backward starting before the specified
1234 : index.
1235 :
1236 : The returned index indicates the starting index of the substring
1237 : in this string.
1238 : If str doesn't include any character, always -1 is
1239 : returned. This is also the case, if both strings are empty.
1240 :
1241 : @since LibreOffice 4.0
1242 :
1243 : @param str the substring to search for.
1244 : @param fromIndex the index before which to start the search.
1245 : @return If the string argument occurs one or more times as a substring
1246 : within this string before the starting index, then the index
1247 : of the first character of the last such substring is
1248 : returned. Otherwise, -1 is returned.
1249 : */
1250 : sal_Int32 lastIndexOf( const OUString & str, sal_Int32 fromIndex ) const
1251 : {
1252 : assert( fromIndex >= 0 && fromIndex <= pData->length );
1253 : return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1254 : str.pData->buffer, str.pData->length );
1255 : }
1256 :
1257 : /**
1258 : @overload
1259 : This function accepts an ASCII string literal as its argument.
1260 : @since LibreOffice 4.0
1261 : */
1262 : template< typename T >
1263 : typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type lastIndexOf( T& literal ) const
1264 : {
1265 : assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1266 : return rtl_ustr_lastIndexOfAscii_WithLength(
1267 : pData->buffer, pData->length, literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1);
1268 : }
1269 :
1270 : /**
1271 : Strip the given character from the start of the buffer.
1272 :
1273 : @since LibreOffice 4.0
1274 :
1275 : @param c the character to strip
1276 : @return The number of characters stripped
1277 :
1278 : */
1279 36379 : sal_Int32 stripStart(sal_Unicode c = (sal_Unicode)' ')
1280 : {
1281 : sal_Int32 index;
1282 68288 : for(index = 0; index < getLength() ; index++)
1283 : {
1284 36554 : if(pData->buffer[ index ] != c)
1285 : {
1286 4645 : break;
1287 : }
1288 : }
1289 36379 : if(index)
1290 : {
1291 31909 : remove(0, index);
1292 : }
1293 36379 : return index;
1294 : }
1295 :
1296 : /**
1297 : Strip the given character from the end of the buffer.
1298 :
1299 : @since LibreOffice 4.0
1300 :
1301 : @param c the character to strip
1302 : @return The number of characters stripped
1303 :
1304 : */
1305 220 : sal_Int32 stripEnd(sal_Unicode c = (sal_Unicode)' ')
1306 : {
1307 220 : sal_Int32 result = getLength();
1308 : sal_Int32 index;
1309 440 : for(index = getLength(); index > 0 ; index--)
1310 : {
1311 440 : if(pData->buffer[ index - 1 ] != c)
1312 : {
1313 220 : break;
1314 : }
1315 : }
1316 220 : if(index < getLength())
1317 : {
1318 220 : truncate(index);
1319 : }
1320 220 : return result - getLength();
1321 : }
1322 : /**
1323 : Strip the given character from the both end of the buffer.
1324 :
1325 : @since LibreOffice 4.0
1326 :
1327 : @param c the character to strip
1328 : @return The number of characters stripped
1329 :
1330 : */
1331 : sal_Int32 strip(sal_Unicode c = (sal_Unicode)' ')
1332 : {
1333 : return stripStart(c) + stripEnd(c);
1334 : }
1335 : /**
1336 : Returns a new string buffer that is a substring of this string.
1337 :
1338 : The substring begins at the specified beginIndex. If
1339 : beginIndex is negative or be greater than the length of
1340 : this string, behaviour is undefined.
1341 :
1342 : @param beginIndex the beginning index, inclusive.
1343 : @return the specified substring.
1344 : @since LibreOffice 4.1
1345 : */
1346 424 : OUStringBuffer copy( sal_Int32 beginIndex ) const
1347 : {
1348 424 : return copy( beginIndex, getLength() - beginIndex );
1349 : }
1350 :
1351 : /**
1352 : Returns a new string buffer that is a substring of this string.
1353 :
1354 : The substring begins at the specified beginIndex and contains count
1355 : characters. If either beginIndex or count are negative,
1356 : or beginIndex + count are greater than the length of this string
1357 : then behaviour is undefined.
1358 :
1359 : @param beginIndex the beginning index, inclusive.
1360 : @param count the number of characters.
1361 : @return the specified substring.
1362 : @since LibreOffice 4.1
1363 : */
1364 424 : OUStringBuffer copy( sal_Int32 beginIndex, sal_Int32 count ) const
1365 : {
1366 : assert(beginIndex >= 0 && beginIndex <= getLength());
1367 : assert(count >= 0 && count <= getLength() - beginIndex);
1368 424 : rtl_uString *pNew = 0;
1369 424 : rtl_uStringbuffer_newFromStr_WithLength( &pNew, getStr() + beginIndex, count );
1370 424 : return OUStringBuffer( pNew, count + 16 );
1371 : }
1372 :
1373 : private:
1374 424 : OUStringBuffer( rtl_uString * value, const sal_Int32 capacity )
1375 : {
1376 424 : pData = value;
1377 424 : nCapacity = capacity;
1378 424 : }
1379 :
1380 : /**
1381 : A pointer to the data structure which contains the data.
1382 : */
1383 : rtl_uString * pData;
1384 :
1385 : /**
1386 : The len of the pData->buffer.
1387 : */
1388 : sal_Int32 nCapacity;
1389 : };
1390 :
1391 : #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
1392 : /**
1393 : @internal
1394 : */
1395 : template<>
1396 : struct ToStringHelper< OUStringBuffer >
1397 : {
1398 1 : static int length( const OUStringBuffer& s ) { return s.getLength(); }
1399 1 : static sal_Unicode* addData( sal_Unicode* buffer, const OUStringBuffer& s ) { return addDataHelper( buffer, s.getStr(), s.getLength()); }
1400 : static const bool allowOStringConcat = false;
1401 : static const bool allowOUStringConcat = true;
1402 : };
1403 : #endif
1404 :
1405 : }
1406 :
1407 : #ifdef RTL_STRING_UNITTEST
1408 : namespace rtl
1409 : {
1410 : typedef rtlunittest::OUStringBuffer OUStringBuffer;
1411 : }
1412 : #endif
1413 :
1414 : #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
1415 : using ::rtl::OUStringBuffer;
1416 : #endif
1417 :
1418 : #endif // INCLUDED_RTL_USTRBUF_HXX
1419 :
1420 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|