1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #ifndef _RTL_STRBUF_HXX_ 29 #define _RTL_STRBUF_HXX_ 30 31 #include "osl/diagnose.h" 32 #include <rtl/strbuf.h> 33 #include <rtl/string.hxx> 34 35 #ifdef __cplusplus 36 37 namespace rtl 38 { 39 40 /** @HTML 41 42 A string buffer implements a mutable sequence of characters. 43 <p> 44 String buffers are safe for use by multiple threads. The methods 45 are synchronized where necessary so that all the operations on any 46 particular instance behave as if they occur in some serial order. 47 <p> 48 String buffers are used by the compiler to implement the binary 49 string concatenation operator <code>+</code>. For example, the code: 50 <p><blockquote><pre> 51 x = "a" + 4 + "c" 52 </pre></blockquote><p> 53 is compiled to the equivalent of: 54 <p><blockquote><pre> 55 x = new OStringBuffer().append("a").append(4).append("c") 56 .toString() 57 </pre></blockquote><p> 58 The principal operations on a <code>OStringBuffer</code> are the 59 <code>append</code> and <code>insert</code> methods, which are 60 overloaded so as to accept data of any type. Each effectively 61 converts a given datum to a string and then appends or inserts the 62 characters of that string to the string buffer. The 63 <code>append</code> method always adds these characters at the end 64 of the buffer; the <code>insert</code> method adds the characters at 65 a specified point. 66 <p> 67 For example, if <code>z</code> refers to a string buffer object 68 whose current contents are "<code>start</code>", then 69 the method call <code>z.append("le")</code> would cause the string 70 buffer to contain "<code>startle</code>", whereas 71 <code>z.insert(4, "le")</code> would alter the string buffer to 72 contain "<code>starlet</code>". 73 <p> 74 Every string buffer has a capacity. As long as the length of the 75 character sequence contained in the string buffer does not exceed 76 the capacity, it is not necessary to allocate a new internal 77 buffer array. If the internal buffer overflows, it is 78 automatically made larger. 79 */ 80 class OStringBuffer 81 { 82 public: 83 /** 84 Constructs a string buffer with no characters in it and an 85 initial capacity of 16 characters. 86 */ 87 OStringBuffer() 88 : pData(NULL) 89 , nCapacity( 16 ) 90 { 91 rtl_string_new_WithLength( &pData, nCapacity ); 92 } 93 94 /** 95 Allocates a new string buffer that contains the same sequence of 96 characters as the string buffer argument. 97 98 @param value a <code>OStringBuffer</code>. 99 */ 100 OStringBuffer( const OStringBuffer & value ) 101 : pData(NULL) 102 , nCapacity( value.nCapacity ) 103 { 104 rtl_stringbuffer_newFromStringBuffer( &pData, value.nCapacity, value.pData ); 105 } 106 107 /** 108 Constructs a string buffer with no characters in it and an 109 initial capacity specified by the <code>length</code> argument. 110 111 @param length the initial capacity. 112 */ 113 OStringBuffer(sal_Int32 length) 114 : pData(NULL) 115 , nCapacity( length ) 116 { 117 rtl_string_new_WithLength( &pData, length ); 118 } 119 120 /** 121 Constructs a string buffer so that it represents the same 122 sequence of characters as the string argument. 123 124 The initial 125 capacity of the string buffer is <code>16</code> plus the length 126 of the string argument. 127 128 @param value the initial string value. 129 */ 130 OStringBuffer(OString value) 131 : pData(NULL) 132 , nCapacity( value.getLength() + 16 ) 133 { 134 rtl_stringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() ); 135 } 136 137 /** Assign to this a copy of value. 138 */ 139 OStringBuffer& operator = ( const OStringBuffer& value ) 140 { 141 if (this != &value) 142 { 143 rtl_stringbuffer_newFromStringBuffer(&pData, 144 value.nCapacity, 145 value.pData); 146 nCapacity = value.nCapacity; 147 } 148 return *this; 149 } 150 151 /** 152 Release the string data. 153 */ 154 ~OStringBuffer() 155 { 156 rtl_string_release( pData ); 157 } 158 159 /** 160 Fill the string data in the new string and clear the buffer. 161 162 This method is more efficient than the contructor of the string. It does 163 not copy the buffer. 164 165 @return the string previously contained in the buffer. 166 */ 167 OString makeStringAndClear() 168 { 169 OString aRet( pData ); 170 rtl_string_new(&pData); 171 nCapacity = 0; 172 return aRet; 173 } 174 175 /** 176 Returns the length (character count) of this string buffer. 177 178 @return the number of characters in this string buffer. 179 */ 180 sal_Int32 getLength() const 181 { 182 return pData->length; 183 } 184 185 /** 186 Returns the current capacity of the String buffer. 187 188 The capacity 189 is the amount of storage available for newly inserted 190 characters. The real buffer size is 2 bytes longer, because 191 all strings are 0 terminated. 192 193 @return the current capacity of this string buffer. 194 */ 195 sal_Int32 getCapacity() const 196 { 197 return nCapacity; 198 } 199 200 /** 201 Ensures that the capacity of the buffer is at least equal to the 202 specified minimum. 203 204 The new capacity will be at least as large as the maximum of the current 205 length (so that no contents of the buffer is destroyed) and the given 206 minimumCapacity. If the given minimumCapacity is negative, nothing is 207 changed. 208 209 @param minimumCapacity the minimum desired capacity. 210 */ 211 void ensureCapacity(sal_Int32 minimumCapacity) 212 { 213 rtl_stringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity ); 214 } 215 216 /** 217 Sets the length of this String buffer. 218 219 If the <code>newLength</code> argument is less than the current 220 length of the string buffer, the string buffer is truncated to 221 contain exactly the number of characters given by the 222 <code>newLength</code> argument. 223 <p> 224 If the <code>newLength</code> argument is greater than or equal 225 to the current length, sufficient null characters 226 (<code>'\u0000'</code>) are appended to the string buffer so that 227 length becomes the <code>newLength</code> argument. 228 <p> 229 The <code>newLength</code> argument must be greater than or equal 230 to <code>0</code>. 231 232 @param newLength the new length of the buffer. 233 */ 234 void setLength(sal_Int32 newLength) 235 { 236 OSL_ASSERT(newLength >= 0); 237 // Avoid modifications if pData points to const empty string: 238 if( newLength != pData->length ) 239 { 240 if( newLength > nCapacity ) 241 rtl_stringbuffer_ensureCapacity(&pData, &nCapacity, newLength); 242 else 243 pData->buffer[newLength] = '\0'; 244 pData->length = newLength; 245 } 246 } 247 248 /** 249 Returns the character at a specific index in this string buffer. 250 251 The first character of a string buffer is at index 252 <code>0</code>, the next at index <code>1</code>, and so on, for 253 array indexing. 254 <p> 255 The index argument must be greater than or equal to 256 <code>0</code>, and less than the length of this string buffer. 257 258 @param index the index of the desired character. 259 @return the character at the specified index of this string buffer. 260 */ 261 sal_Char charAt( sal_Int32 index ) 262 { 263 OSL_ASSERT(index >= 0 && index < pData->length); 264 return pData->buffer[ index ]; 265 } 266 267 /** 268 Return a null terminated character array. 269 */ 270 operator const sal_Char *() const { return pData->buffer; } 271 272 /** 273 Return a null terminated character array. 274 */ 275 const sal_Char* getStr() const { return pData->buffer; } 276 277 278 /** 279 The character at the specified index of this string buffer is set 280 to <code>ch</code>. 281 282 The index argument must be greater than or equal to 283 <code>0</code>, and less than the length of this string buffer. 284 285 @param index the index of the character to modify. 286 @param ch the new character. 287 */ 288 OStringBuffer & setCharAt(sal_Int32 index, sal_Char ch) 289 { 290 OSL_ASSERT(index >= 0 && index < pData->length); 291 pData->buffer[ index ] = ch; 292 return *this; 293 } 294 295 /** 296 Appends the string to this string buffer. 297 298 The characters of the <code>String</code> argument are appended, in 299 order, to the contents of this string buffer, increasing the 300 length of this string buffer by the length of the argument. 301 302 @param str a string. 303 @return this string buffer. 304 */ 305 OStringBuffer & append(const OString &str) 306 { 307 return append( str.getStr(), str.getLength() ); 308 } 309 310 /** 311 Appends the string representation of the <code>char</code> array 312 argument to this string buffer. 313 314 The characters of the array argument are appended, in order, to 315 the contents of this string buffer. The length of this string 316 buffer increases by the length of the argument. 317 318 @param str the characters to be appended. 319 @return this string buffer. 320 */ 321 OStringBuffer & append( const sal_Char * str ) 322 { 323 return append( str, rtl_str_getLength( str ) ); 324 } 325 326 /** 327 Appends the string representation of the <code>char</code> array 328 argument to this string buffer. 329 330 Characters of the character array <code>str</code> are appended, 331 in order, to the contents of this string buffer. The length of this 332 string buffer increases by the value of <code>len</code>. 333 334 @param str the characters to be appended; must be non-null, and must 335 point to at least len characters 336 @param len the number of characters to append; must be non-negative 337 @return this string buffer. 338 */ 339 OStringBuffer & append( const sal_Char * str, sal_Int32 len) 340 { 341 // insert behind the last character 342 rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), str, len ); 343 return *this; 344 } 345 346 /** 347 Appends the string representation of the <code>sal_Bool</code> 348 argument to the string buffer. 349 350 The argument is converted to a string as if by the method 351 <code>String.valueOf</code>, and the characters of that 352 string are then appended to this string buffer. 353 354 @param b a <code>sal_Bool</code>. 355 @return this string buffer. 356 */ 357 OStringBuffer & append(sal_Bool b) 358 { 359 sal_Char sz[RTL_STR_MAX_VALUEOFBOOLEAN]; 360 return append( sz, rtl_str_valueOfBoolean( sz, b ) ); 361 } 362 363 /** 364 Appends the string representation of the <code>char</code> 365 argument to this string buffer. 366 367 The argument is appended to the contents of this string buffer. 368 The length of this string buffer increases by <code>1</code>. 369 370 @param ch a <code>char</code>. 371 @return this string buffer. 372 */ 373 OStringBuffer & append(sal_Char c) 374 { 375 return append( &c, 1 ); 376 } 377 378 /** 379 Appends the string representation of the <code>sal_Int32</code> 380 argument to this string buffer. 381 382 The argument is converted to a string as if by the method 383 <code>String.valueOf</code>, and the characters of that 384 string are then appended to this string buffer. 385 386 @param i an <code>sal_Int32</code>. 387 @return this string buffer. 388 */ 389 OStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 ) 390 { 391 sal_Char sz[RTL_STR_MAX_VALUEOFINT32]; 392 return append( sz, rtl_str_valueOfInt32( sz, i, radix ) ); 393 } 394 395 /** 396 Appends the string representation of the <code>long</code> 397 argument to this string buffer. 398 399 The argument is converted to a string as if by the method 400 <code>String.valueOf</code>, and the characters of that 401 string are then appended to this string buffer. 402 403 @param l a <code>long</code>. 404 @return this string buffer. 405 */ 406 OStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 ) 407 { 408 sal_Char sz[RTL_STR_MAX_VALUEOFINT64]; 409 return append( sz, rtl_str_valueOfInt64( sz, l, radix ) ); 410 } 411 412 /** 413 Appends the string representation of the <code>float</code> 414 argument to this string buffer. 415 416 The argument is converted to a string as if by the method 417 <code>String.valueOf</code>, and the characters of that 418 string are then appended to this string buffer. 419 420 @param f a <code>float</code>. 421 @return this string buffer. 422 */ 423 OStringBuffer & append(float f) 424 { 425 sal_Char sz[RTL_STR_MAX_VALUEOFFLOAT]; 426 return append( sz, rtl_str_valueOfFloat( sz, f ) ); 427 } 428 429 /** 430 Appends the string representation of the <code>double</code> 431 argument to this string buffer. 432 433 The argument is converted to a string as if by the method 434 <code>String.valueOf</code>, and the characters of that 435 string are then appended to this string buffer. 436 437 @param d a <code>double</code>. 438 @return this string buffer. 439 */ 440 OStringBuffer & append(double d) 441 { 442 sal_Char sz[RTL_STR_MAX_VALUEOFDOUBLE]; 443 return append( sz, rtl_str_valueOfDouble( sz, d ) ); 444 } 445 446 /** 447 Inserts the string into this string buffer. 448 449 The characters of the <code>String</code> argument are inserted, in 450 order, into this string buffer at the indicated offset. The length 451 of this string buffer is increased by the length of the argument. 452 <p> 453 The offset argument must be greater than or equal to 454 <code>0</code>, and less than or equal to the length of this 455 string buffer. 456 457 @param offset the offset. 458 @param str a string. 459 @return this string buffer. 460 */ 461 OStringBuffer & insert(sal_Int32 offset, const OString & str) 462 { 463 return insert( offset, str.getStr(), str.getLength() ); 464 } 465 466 /** 467 Inserts the string representation of the <code>char</code> array 468 argument into this string buffer. 469 470 The characters of the array argument are inserted into the 471 contents of this string buffer at the position indicated by 472 <code>offset</code>. The length of this string buffer increases by 473 the length of the argument. 474 <p> 475 The offset argument must be greater than or equal to 476 <code>0</code>, and less than or equal to the length of this 477 string buffer. 478 479 @param offset the offset. 480 @param ch a character array. 481 @return this string buffer. 482 */ 483 OStringBuffer & insert( sal_Int32 offset, const sal_Char * str ) 484 { 485 return insert( offset, str, rtl_str_getLength( str ) ); 486 } 487 488 /** 489 Inserts the string representation of the <code>char</code> array 490 argument into this string buffer. 491 492 The characters of the array argument are inserted into the 493 contents of this string buffer at the position indicated by 494 <code>offset</code>. The length of this string buffer increases by 495 the length of the argument. 496 <p> 497 The offset argument must be greater than or equal to 498 <code>0</code>, and less than or equal to the length of this 499 string buffer. 500 501 @param offset the offset. 502 @param ch a character array. 503 @param len the number of characters to append. 504 @return this string buffer. 505 */ 506 OStringBuffer & insert( sal_Int32 offset, const sal_Char * str, sal_Int32 len) 507 { 508 // insert behind the last character 509 rtl_stringbuffer_insert( &pData, &nCapacity, offset, str, len ); 510 return *this; 511 } 512 513 /** 514 Inserts the string representation of the <code>sal_Bool</code> 515 argument into this string buffer. 516 517 The second argument is converted to a string as if by the method 518 <code>String.valueOf</code>, and the characters of that 519 string are then inserted into this string buffer at the indicated 520 offset. 521 <p> 522 The offset argument must be greater than or equal to 523 <code>0</code>, and less than or equal to the length of this 524 string buffer. 525 526 @param offset the offset. 527 @param b a <code>sal_Bool</code>. 528 @return this string buffer. 529 */ 530 OStringBuffer & insert(sal_Int32 offset, sal_Bool b) 531 { 532 sal_Char sz[RTL_STR_MAX_VALUEOFBOOLEAN]; 533 return insert( offset, sz, rtl_str_valueOfBoolean( sz, b ) ); 534 } 535 536 /** 537 Inserts the string representation of the <code>char</code> 538 argument into this string buffer. 539 540 The second argument is inserted into the contents of this string 541 buffer at the position indicated by <code>offset</code>. The length 542 of this string buffer increases by one. 543 <p> 544 The offset argument must be greater than or equal to 545 <code>0</code>, and less than or equal to the length of this 546 string buffer. 547 548 @param offset the offset. 549 @param ch a <code>char</code>. 550 @return this string buffer. 551 */ 552 OStringBuffer & insert(sal_Int32 offset, sal_Char c) 553 { 554 return insert( offset, &c, 1 ); 555 } 556 557 /** 558 Inserts the string representation of the second <code>sal_Int32</code> 559 argument into this string buffer. 560 561 The second argument is converted to a string as if by the method 562 <code>String.valueOf</code>, and the characters of that 563 string are then inserted into this string buffer at the indicated 564 offset. 565 <p> 566 The offset argument must be greater than or equal to 567 <code>0</code>, and less than or equal to the length of this 568 string buffer. 569 570 @param offset the offset. 571 @param b an <code>sal_Int32</code>. 572 @return this string buffer. 573 */ 574 OStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 ) 575 { 576 sal_Char sz[RTL_STR_MAX_VALUEOFINT32]; 577 return insert( offset, sz, rtl_str_valueOfInt32( sz, i, radix ) ); 578 } 579 580 /** 581 Inserts the string representation of the <code>long</code> 582 argument into this string buffer. 583 584 The second argument is converted to a string as if by the method 585 <code>String.valueOf</code>, and the characters of that 586 string are then inserted into this string buffer at the indicated 587 offset. 588 <p> 589 The offset argument must be greater than or equal to 590 <code>0</code>, and less than or equal to the length of this 591 string buffer. 592 593 @param offset the offset. 594 @param b a <code>long</code>. 595 @return this string buffer. 596 */ 597 OStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 ) 598 { 599 sal_Char sz[RTL_STR_MAX_VALUEOFINT64]; 600 return insert( offset, sz, rtl_str_valueOfInt64( sz, l, radix ) ); 601 } 602 603 /** 604 Inserts the string representation of the <code>float</code> 605 argument into this string buffer. 606 607 The second argument is converted to a string as if by the method 608 <code>String.valueOf</code>, and the characters of that 609 string are then inserted into this string buffer at the indicated 610 offset. 611 <p> 612 The offset argument must be greater than or equal to 613 <code>0</code>, and less than or equal to the length of this 614 string buffer. 615 616 @param offset the offset. 617 @param b a <code>float</code>. 618 @return this string buffer. 619 */ 620 OStringBuffer insert(sal_Int32 offset, float f) 621 { 622 sal_Char sz[RTL_STR_MAX_VALUEOFFLOAT]; 623 return insert( offset, sz, rtl_str_valueOfFloat( sz, f ) ); 624 } 625 626 /** 627 Inserts the string representation of the <code>double</code> 628 argument into this string buffer. 629 630 The second argument is converted to a string as if by the method 631 <code>String.valueOf</code>, and the characters of that 632 string are then inserted into this string buffer at the indicated 633 offset. 634 <p> 635 The offset argument must be greater than or equal to 636 <code>0</code>, and less than or equal to the length of this 637 string buffer. 638 639 @param offset the offset. 640 @param b a <code>double</code>. 641 @return this string buffer. 642 */ 643 OStringBuffer & insert(sal_Int32 offset, double d) 644 { 645 sal_Char sz[RTL_STR_MAX_VALUEOFDOUBLE]; 646 return insert( offset, sz, rtl_str_valueOfDouble( sz, d ) ); 647 } 648 private: 649 /** 650 A pointer to the data structur which contains the data. 651 */ 652 rtl_String * pData; 653 654 /** 655 The len of the pData->buffer. 656 */ 657 sal_Int32 nCapacity; 658 }; 659 660 } 661 662 #endif /* __cplusplus */ 663 #endif /* _RTL_STRBUF_HXX_ */ 664 665 666