1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #ifndef _RTL_STRING_HXX_ 25 #define _RTL_STRING_HXX_ 26 27 #ifdef __cplusplus 28 29 #ifndef _RTL_DIAGNOSE_H_ 30 #include <osl/diagnose.h> 31 #endif 32 #include <rtl/memory.h> 33 #include <rtl/textenc.h> 34 #include <rtl/string.h> 35 36 #if !defined EXCEPTIONS_OFF 37 #include <new> 38 #endif 39 40 namespace rtl 41 { 42 43 /* ======================================================================= */ 44 45 /** 46 This String class provide base functionality for C++ like 8-Bit 47 character array handling. The advantage of this class is, that it 48 handle all the memory managament for you - and it do it 49 more efficient. If you assign a string to another string, the 50 data of both strings are shared (without any copy operation or 51 memory allocation) as long as you do not change the string. This class 52 stores also the length of the string, so that many operations are 53 faster as the C-str-functions. 54 55 This class provide only readonly string handling. So you could create 56 a string and you could only query the content from this string. 57 It provide also functionality to change the string, but this results 58 in every case in a new string instance (in the most cases with an 59 memory allocation). You don't have functionality to change the 60 content of the string. If you want change the string content, than 61 you should us the OStringBuffer class, which provide these 62 functionality and avoid to much memory allocation. 63 64 The design of this class is similar to the string classes in Java 65 and so more people should have fewer understanding problems when they 66 use this class. 67 */ 68 69 class OString 70 { 71 public: 72 /** @internal */ 73 rtl_String * pData; 74 75 private: 76 /** @internal */ 77 class DO_NOT_ACQUIRE; 78 79 /** @internal */ 80 OString( rtl_String * value, DO_NOT_ACQUIRE * ) 81 { 82 pData = value; 83 } 84 85 public: 86 /** 87 New string containing no characters. 88 */ 89 OString() SAL_THROW(()) 90 { 91 pData = 0; 92 rtl_string_new( &pData ); 93 } 94 95 /** 96 New string from OString. 97 98 @param str a OString. 99 */ 100 OString( const OString & str ) SAL_THROW(()) 101 { 102 pData = str.pData; 103 rtl_string_acquire( pData ); 104 } 105 106 /** 107 New string from OString data. 108 109 @param str a OString data. 110 */ 111 OString( rtl_String * str ) SAL_THROW(()) 112 { 113 pData = str; 114 rtl_string_acquire( pData ); 115 } 116 117 /** 118 New string from a single character. 119 120 @param value a character. 121 */ 122 explicit OString( sal_Char value ) SAL_THROW(()) 123 : pData (0) 124 { 125 rtl_string_newFromStr_WithLength( &pData, &value, 1 ); 126 } 127 128 /** 129 New string from a character buffer array. 130 131 @param value a NULL-terminated character array. 132 */ 133 OString( const sal_Char * value ) SAL_THROW(()) 134 { 135 pData = 0; 136 rtl_string_newFromStr( &pData, value ); 137 } 138 139 /** 140 New string from a character buffer array. 141 142 @param value a character array. 143 @param length the number of character which should be copied. 144 The character array length must be greater or 145 equal than this value. 146 */ 147 OString( const sal_Char * value, sal_Int32 length ) SAL_THROW(()) 148 { 149 pData = 0; 150 rtl_string_newFromStr_WithLength( &pData, value, length ); 151 } 152 153 /** 154 New string from a Unicode character buffer array. 155 156 @param value a Unicode character array. 157 @param length the number of character which should be converted. 158 The Unicode character array length must be 159 greater or equal than this value. 160 @param encoding the text encoding in which the Unicode character 161 sequence should be converted. 162 @param convertFlags flags which controls the conversion. 163 see RTL_UNICODETOTEXT_FLAGS_... 164 165 @exception std::bad_alloc is thrown if an out-of-memory condition occurs 166 */ 167 OString( const sal_Unicode * value, sal_Int32 length, 168 rtl_TextEncoding encoding, 169 sal_uInt32 convertFlags = OUSTRING_TO_OSTRING_CVTFLAGS ) 170 { 171 pData = 0; 172 rtl_uString2String( &pData, value, length, encoding, convertFlags ); 173 #if defined EXCEPTIONS_OFF 174 OSL_ASSERT(pData != NULL); 175 #else 176 if (pData == 0) { 177 throw std::bad_alloc(); 178 } 179 #endif 180 } 181 182 /** 183 Release the string data. 184 */ 185 ~OString() SAL_THROW(()) 186 { 187 rtl_string_release( pData ); 188 } 189 190 /** 191 Assign a new string. 192 193 @param str a OString. 194 */ 195 OString & operator=( const OString & str ) SAL_THROW(()) 196 { 197 rtl_string_assign( &pData, str.pData ); 198 return *this; 199 } 200 201 /** 202 Append a string to this string. 203 204 @param str a OString. 205 */ 206 OString & operator+=( const OString & str ) SAL_THROW(()) 207 { 208 rtl_string_newConcat( &pData, pData, str.pData ); 209 return *this; 210 } 211 212 /** 213 Returns the length of this string. 214 215 The length is equal to the number of characters in this string. 216 217 @return the length of the sequence of characters represented by this 218 object. 219 */ 220 sal_Int32 getLength() const SAL_THROW(()) { return pData->length; } 221 222 /** 223 Returns a pointer to the characters of this string. 224 225 <p>The returned pointer is not guaranteed to point to a null-terminated 226 byte string. Note that this string object may contain embedded null 227 characters, which will thus also be embedded in the returned byte 228 string.</p> 229 230 @return a pointer to a (not necessarily null-terminated) byte string 231 representing the characters of this string object. 232 */ 233 operator const sal_Char *() const SAL_THROW(()) { return pData->buffer; } 234 235 /** 236 Returns a pointer to the characters of this string. 237 238 <p>The returned pointer is guaranteed to point to a null-terminated byte 239 string. But note that this string object may contain embedded null 240 characters, which will thus also be embedded in the returned 241 null-terminated byte string.</p> 242 243 @return a pointer to a null-terminated byte string representing the 244 characters of this string object. 245 */ 246 const sal_Char * getStr() const SAL_THROW(()) { return pData->buffer; } 247 248 /** 249 Compares two strings. 250 251 The comparison is based on the numeric value of each character in 252 the strings and return a value indicating their relationship. 253 This function can't be used for language specific sorting. 254 255 @param str the object to be compared. 256 @return 0 - if both strings are equal 257 < 0 - if this string is less than the string argument 258 > 0 - if this string is greater than the string argument 259 */ 260 sal_Int32 compareTo( const OString & str ) const SAL_THROW(()) 261 { 262 return rtl_str_compare_WithLength( pData->buffer, pData->length, 263 str.pData->buffer, str.pData->length ); 264 } 265 266 /** 267 Compares two strings with an maximum count of characters. 268 269 The comparison is based on the numeric value of each character in 270 the strings and return a value indicating their relationship. 271 This function can't be used for language specific sorting. 272 273 @param str the object to be compared. 274 @param maxLength the maximum count of characters to be compared. 275 @return 0 - if both strings are equal 276 < 0 - if this string is less than the string argument 277 > 0 - if this string is greater than the string argument 278 */ 279 sal_Int32 compareTo( const OString & rObj, sal_Int32 maxLength ) const SAL_THROW(()) 280 { 281 return rtl_str_shortenedCompare_WithLength( pData->buffer, pData->length, 282 rObj.pData->buffer, rObj.pData->length, maxLength ); 283 } 284 285 /** 286 Compares two strings in reverse order. 287 288 The comparison is based on the numeric value of each character in 289 the strings and return a value indicating their relationship. 290 This function can't be used for language specific sorting. 291 292 @param str the object to be compared. 293 @return 0 - if both strings are equal 294 < 0 - if this string is less than the string argument 295 > 0 - if this string is greater than the string argument 296 */ 297 sal_Int32 reverseCompareTo( const OString & str ) const SAL_THROW(()) 298 { 299 return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length, 300 str.pData->buffer, str.pData->length ); 301 } 302 303 /** 304 Perform a comparison of two strings. 305 306 The result is true if and only if second string 307 represents the same sequence of characters as the first string. 308 This function can't be used for language specific comparison. 309 310 @param str the object to be compared. 311 @return sal_True if the strings are equal; 312 sal_False, otherwise. 313 */ 314 sal_Bool equals( const OString & str ) const SAL_THROW(()) 315 { 316 if ( pData->length != str.pData->length ) 317 return sal_False; 318 if ( pData == str.pData ) 319 return sal_True; 320 return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length, 321 str.pData->buffer, str.pData->length ) == 0; 322 } 323 324 /** 325 Perform a ASCII lowercase comparison of two strings. 326 327 The result is true if and only if second string 328 represents the same sequence of characters as the first string, 329 ignoring the case. 330 Character values between 65 and 90 (ASCII A-Z) are interpreted as 331 values between 97 and 122 (ASCII a-z). 332 This function can't be used for language specific comparison. 333 334 @param str the object to be compared. 335 @return sal_True if the strings are equal; 336 sal_False, otherwise. 337 */ 338 sal_Bool equalsIgnoreAsciiCase( const OString & str ) const SAL_THROW(()) 339 { 340 if ( pData->length != str.pData->length ) 341 return sal_False; 342 if ( pData == str.pData ) 343 return sal_True; 344 return rtl_str_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, 345 str.pData->buffer, str.pData->length ) == 0; 346 } 347 348 /** 349 Match against a substring appearing in this string. 350 351 The result is true if and only if the second string appears as a substring 352 of this string, at the given position. 353 This function can't be used for language specific comparison. 354 355 @param str the object (substring) to be compared. 356 @param fromIndex the index to start the comparion from. 357 The index must be greater or equal than 0 358 and less or equal as the string length. 359 @return sal_True if str match with the characters in the string 360 at the given position; 361 sal_False, otherwise. 362 */ 363 sal_Bool match( const OString & str, sal_Int32 fromIndex = 0 ) const SAL_THROW(()) 364 { 365 return rtl_str_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, 366 str.pData->buffer, str.pData->length, str.pData->length ) == 0; 367 } 368 369 /** 370 Match against a substring appearing in this string, ignoring the case of 371 ASCII letters. 372 373 The result is true if and only if the second string appears as a substring 374 of this string, at the given position. 375 Character values between 65 and 90 (ASCII A-Z) are interpreted as 376 values between 97 and 122 (ASCII a-z). 377 This function can't be used for language specific comparison. 378 379 @param str the object (substring) to be compared. 380 @param fromIndex the index to start the comparion from. 381 The index must be greater or equal than 0 382 and less or equal as the string length. 383 @return sal_True if str match with the characters in the string 384 at the given position; 385 sal_False, otherwise. 386 */ 387 sal_Bool matchIgnoreAsciiCase( const OString & str, sal_Int32 fromIndex = 0 ) const SAL_THROW(()) 388 { 389 return rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, 390 str.pData->buffer, str.pData->length, 391 str.pData->length ) == 0; 392 } 393 394 friend sal_Bool operator == ( const OString& rStr1, const OString& rStr2 ) SAL_THROW(()) 395 { return rStr1.getLength() == rStr2.getLength() && rStr1.compareTo( rStr2 ) == 0; } 396 friend sal_Bool operator == ( const OString& rStr1, const sal_Char * pStr2 ) SAL_THROW(()) 397 { return rStr1.compareTo( pStr2 ) == 0; } 398 friend sal_Bool operator == ( const sal_Char * pStr1, const OString& rStr2 ) SAL_THROW(()) 399 { return OString( pStr1 ).compareTo( rStr2 ) == 0; } 400 401 friend sal_Bool operator != ( const OString& rStr1, const OString& rStr2 ) SAL_THROW(()) 402 { return !(operator == ( rStr1, rStr2 )); } 403 friend sal_Bool operator != ( const OString& rStr1, const sal_Char * pStr2 ) SAL_THROW(()) 404 { return !(operator == ( rStr1, pStr2 )); } 405 friend sal_Bool operator != ( const sal_Char * pStr1, const OString& rStr2 ) SAL_THROW(()) 406 { return !(operator == ( pStr1, rStr2 )); } 407 408 friend sal_Bool operator < ( const OString& rStr1, const OString& rStr2 ) SAL_THROW(()) 409 { return rStr1.compareTo( rStr2 ) < 0; } 410 friend sal_Bool operator > ( const OString& rStr1, const OString& rStr2 ) SAL_THROW(()) 411 { return rStr1.compareTo( rStr2 ) > 0; } 412 friend sal_Bool operator <= ( const OString& rStr1, const OString& rStr2 ) SAL_THROW(()) 413 { return rStr1.compareTo( rStr2 ) <= 0; } 414 friend sal_Bool operator >= ( const OString& rStr1, const OString& rStr2 ) SAL_THROW(()) 415 { return rStr1.compareTo( rStr2 ) >= 0; } 416 417 /** 418 Returns a hashcode for this string. 419 420 @return a hash code value for this object. 421 422 @see rtl::OStringHash for convenient use of STLPort's hash_map 423 */ 424 sal_Int32 hashCode() const SAL_THROW(()) 425 { 426 return rtl_str_hashCode_WithLength( pData->buffer, pData->length ); 427 } 428 429 /** 430 Returns the index within this string of the first occurrence of the 431 specified character, starting the search at the specified index. 432 433 @param ch character to be located. 434 @param fromIndex the index to start the search from. 435 The index must be greater or equal than 0 436 and less or equal as the string length. 437 @return the index of the first occurrence of the character in the 438 character sequence represented by this string that is 439 greater than or equal to fromIndex, or 440 -1 if the character does not occur. 441 */ 442 sal_Int32 indexOf( sal_Char ch, sal_Int32 fromIndex = 0 ) const SAL_THROW(()) 443 { 444 sal_Int32 ret = rtl_str_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch ); 445 return (ret < 0 ? ret : ret+fromIndex); 446 } 447 448 /** 449 Returns the index within this string of the last occurrence of the 450 specified character, searching backward starting at the end. 451 452 @param ch character to be located. 453 @return the index of the last occurrence of the character in the 454 character sequence represented by this string, or 455 -1 if the character does not occur. 456 */ 457 sal_Int32 lastIndexOf( sal_Char ch ) const SAL_THROW(()) 458 { 459 return rtl_str_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch ); 460 } 461 462 /** 463 Returns the index within this string of the last occurrence of the 464 specified character, searching backward starting before the specified 465 index. 466 467 @param ch character to be located. 468 @param fromIndex the index before which to start the search. 469 @return the index of the last occurrence of the character in the 470 character sequence represented by this string that 471 is less than fromIndex, or -1 472 if the character does not occur before that point. 473 */ 474 sal_Int32 lastIndexOf( sal_Char ch, sal_Int32 fromIndex ) const SAL_THROW(()) 475 { 476 return rtl_str_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch ); 477 } 478 479 /** 480 Returns the index within this string of the first occurrence of the 481 specified substring, starting at the specified index. 482 483 If str doesn't include any character, always -1 is 484 returned. This is also the case, if both strings are empty. 485 486 @param str the substring to search for. 487 @param fromIndex the index to start the search from. 488 @return If the string argument occurs one or more times as a substring 489 within this string at the starting index, then the index 490 of the first character of the first such substring is 491 returned. If it does not occur as a substring starting 492 at fromIndex or beyond, -1 is returned. 493 */ 494 sal_Int32 indexOf( const OString & str, sal_Int32 fromIndex = 0 ) const SAL_THROW(()) 495 { 496 sal_Int32 ret = rtl_str_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, 497 str.pData->buffer, str.pData->length ); 498 return (ret < 0 ? ret : ret+fromIndex); 499 } 500 501 /** 502 Returns the index within this string of the last occurrence of 503 the specified substring, searching backward starting at the end. 504 505 The returned index indicates the starting index of the substring 506 in this string. 507 If str doesn't include any character, always -1 is 508 returned. This is also the case, if both strings are empty. 509 510 @param str the substring to search for. 511 @return If the string argument occurs one or more times as a substring 512 within this string, then the index of the first character of 513 the last such substring is returned. If it does not occur as 514 a substring, -1 is returned. 515 */ 516 sal_Int32 lastIndexOf( const OString & str ) const SAL_THROW(()) 517 { 518 return rtl_str_lastIndexOfStr_WithLength( pData->buffer, pData->length, 519 str.pData->buffer, str.pData->length ); 520 } 521 522 /** 523 Returns the index within this string of the last occurrence of 524 the specified substring, searching backward starting before the specified 525 index. 526 527 The returned index indicates the starting index of the substring 528 in this string. 529 If str doesn't include any character, always -1 is 530 returned. This is also the case, if both strings are empty. 531 532 @param str the substring to search for. 533 @param fromIndex the index before which to start the search. 534 @return If the string argument occurs one or more times as a substring 535 within this string before the starting index, then the index 536 of the first character of the last such substring is 537 returned. Otherwise, -1 is returned. 538 */ 539 sal_Int32 lastIndexOf( const OString & str, sal_Int32 fromIndex ) const SAL_THROW(()) 540 { 541 return rtl_str_lastIndexOfStr_WithLength( pData->buffer, fromIndex, 542 str.pData->buffer, str.pData->length ); 543 } 544 545 /** 546 Returns a new string that is a substring of this string. 547 548 The substring begins at the specified beginIndex. It is an error for 549 beginIndex to be negative or to be greater than the length of this string. 550 551 @param beginIndex the beginning index, inclusive. 552 @return the specified substring. 553 */ 554 OString copy( sal_Int32 beginIndex ) const SAL_THROW(()) 555 { 556 OSL_ASSERT(beginIndex >= 0 && beginIndex <= getLength()); 557 if ( beginIndex == 0 ) 558 return *this; 559 else 560 { 561 rtl_String* pNew = 0; 562 rtl_string_newFromStr_WithLength( &pNew, pData->buffer+beginIndex, getLength()-beginIndex ); 563 return OString( pNew, (DO_NOT_ACQUIRE*)0 ); 564 } 565 } 566 567 /** 568 Returns a new string that is a substring of this string. 569 570 The substring begins at the specified beginIndex and contains count 571 characters. It is an error for either beginIndex or count to be negative, 572 or for beginIndex + count to be greater than the length of this string. 573 574 @param beginIndex the beginning index, inclusive. 575 @param count the number of characters. 576 @return the specified substring. 577 */ 578 OString copy( sal_Int32 beginIndex, sal_Int32 count ) const SAL_THROW(()) 579 { 580 OSL_ASSERT(beginIndex >= 0 && beginIndex <= getLength() 581 && count >= 0 && count <= getLength() - beginIndex); 582 if ( (beginIndex == 0) && (count == getLength()) ) 583 return *this; 584 else 585 { 586 rtl_String* pNew = 0; 587 rtl_string_newFromStr_WithLength( &pNew, pData->buffer+beginIndex, count ); 588 return OString( pNew, (DO_NOT_ACQUIRE*)0 ); 589 } 590 } 591 592 /** 593 Concatenates the specified string to the end of this string. 594 595 @param str the string that is concatenated to the end 596 of this string. 597 @return a string that represents the concatenation of this string 598 followed by the string argument. 599 */ 600 OString concat( const OString & str ) const SAL_THROW(()) 601 { 602 rtl_String* pNew = 0; 603 rtl_string_newConcat( &pNew, pData, str.pData ); 604 return OString( pNew, (DO_NOT_ACQUIRE*)0 ); 605 } 606 607 friend OString operator+( const OString & str1, const OString & str2 ) SAL_THROW(()) 608 { 609 return str1.concat( str2 ); 610 } 611 612 /** 613 Returns a new string resulting from replacing n = count characters 614 from position index in this string with newStr. 615 616 @param index the replacing index in str. 617 The index must be greater or equal as 0 and 618 less or equal as the length of the string. 619 @param count the count of charcters that will replaced 620 The count must be greater or equal as 0 and 621 less or equal as the length of the string minus index. 622 @param newStr the new substring. 623 @return the new string. 624 */ 625 OString replaceAt( sal_Int32 index, sal_Int32 count, const OString& newStr ) const SAL_THROW(()) 626 { 627 rtl_String* pNew = 0; 628 rtl_string_newReplaceStrAt( &pNew, pData, index, count, newStr.pData ); 629 return OString( pNew, (DO_NOT_ACQUIRE*)0 ); 630 } 631 632 /** 633 Returns a new string resulting from replacing all occurrences of 634 oldChar in this string with newChar. 635 636 If the character oldChar does not occur in the character sequence 637 represented by this object, then the string is assigned with 638 str. 639 640 @param oldChar the old character. 641 @param newChar the new character. 642 @return a string derived from this string by replacing every 643 occurrence of oldChar with newChar. 644 */ 645 OString replace( sal_Char oldChar, sal_Char newChar ) const SAL_THROW(()) 646 { 647 rtl_String* pNew = 0; 648 rtl_string_newReplace( &pNew, pData, oldChar, newChar ); 649 return OString( pNew, (DO_NOT_ACQUIRE*)0 ); 650 } 651 652 /** 653 Converts from this string all ASCII uppercase characters (65-90) 654 to ASCII lowercase characters (97-122). 655 656 This function can't be used for language specific conversion. 657 If the string doesn't contain characters which must be converted, 658 then the new string is assigned with str. 659 660 @return the string, converted to ASCII lowercase. 661 */ 662 OString toAsciiLowerCase() const SAL_THROW(()) 663 { 664 rtl_String* pNew = 0; 665 rtl_string_newToAsciiLowerCase( &pNew, pData ); 666 return OString( pNew, (DO_NOT_ACQUIRE*)0 ); 667 } 668 669 /** 670 Converts from this string all ASCII lowercase characters (97-122) 671 to ASCII uppercase characters (65-90). 672 673 This function can't be used for language specific conversion. 674 If the string doesn't contain characters which must be converted, 675 then the new string is assigned with str. 676 677 @return the string, converted to ASCII uppercase. 678 */ 679 OString toAsciiUpperCase() const SAL_THROW(()) 680 { 681 rtl_String* pNew = 0; 682 rtl_string_newToAsciiUpperCase( &pNew, pData ); 683 return OString( pNew, (DO_NOT_ACQUIRE*)0 ); 684 } 685 686 /** 687 Returns a new string resulting from removing white space from both ends 688 of the string. 689 690 All characters that have codes less than or equal to 691 32 (the space character) are considered to be white space. 692 If the string doesn't contain white spaces at both ends, 693 then the new string is assigned with str. 694 695 @return the string, with white space removed from the front and end. 696 */ 697 OString trim() const SAL_THROW(()) 698 { 699 rtl_String* pNew = 0; 700 rtl_string_newTrim( &pNew, pData ); 701 return OString( pNew, (DO_NOT_ACQUIRE*)0 ); 702 } 703 704 /** 705 Returns a token in the string. 706 707 Example: 708 sal_Int32 nIndex = 0; 709 do 710 { 711 ... 712 OString aToken = aStr.getToken( 0, ';', nIndex ); 713 ... 714 } 715 while ( nIndex >= 0 ); 716 717 @param token the number of the token to return. 718 @param cTok the character which seperate the tokens. 719 @param index the position at which the token is searched in the 720 string. 721 The index must not be greater thanthe length of the 722 string. 723 This param is set to the position of the 724 next token or to -1, if it is the last token. 725 @return the token; if either token or index is negative, an empty token 726 is returned (and index is set to -1) 727 */ 728 OString getToken( sal_Int32 token, sal_Char cTok, sal_Int32& index ) const SAL_THROW(()) 729 { 730 rtl_String * pNew = 0; 731 index = rtl_string_getToken( &pNew, pData, token, cTok, index ); 732 return OString( pNew, (DO_NOT_ACQUIRE *)0 ); 733 } 734 735 /** 736 Returns the Boolean value from this string. 737 738 This function can't be used for language specific conversion. 739 740 @return sal_True, if the string is 1 or "True" in any ASCII case. 741 sal_False in any other case. 742 */ 743 sal_Bool toBoolean() const SAL_THROW(()) 744 { 745 return rtl_str_toBoolean( pData->buffer ); 746 } 747 748 /** 749 Returns the first character from this string. 750 751 @return the first character from this string or 0, if this string 752 is emptry. 753 */ 754 sal_Char toChar() const SAL_THROW(()) 755 { 756 return pData->buffer[0]; 757 } 758 759 /** 760 Returns the int32 value from this string. 761 762 This function can't be used for language specific conversion. 763 764 @param radix the radix (between 2 and 36) 765 @return the int32 represented from this string. 766 0 if this string represents no number. 767 */ 768 sal_Int32 toInt32( sal_Int16 radix = 10 ) const SAL_THROW(()) 769 { 770 return rtl_str_toInt32( pData->buffer, radix ); 771 } 772 773 /** 774 Returns the int64 value from this string. 775 776 This function can't be used for language specific conversion. 777 778 @param radix the radix (between 2 and 36) 779 @return the int64 represented from this string. 780 0 if this string represents no number. 781 */ 782 sal_Int64 toInt64( sal_Int16 radix = 10 ) const SAL_THROW(()) 783 { 784 return rtl_str_toInt64( pData->buffer, radix ); 785 } 786 787 /** 788 Returns the float value from this string. 789 790 This function can't be used for language specific conversion. 791 792 @return the float represented from this string. 793 0.0 if this string represents no number. 794 */ 795 float toFloat() const SAL_THROW(()) 796 { 797 return rtl_str_toFloat( pData->buffer ); 798 } 799 800 /** 801 Returns the double value from this string. 802 803 This function can't be used for language specific conversion. 804 805 @return the double represented from this string. 806 0.0 if this string represents no number. 807 */ 808 double toDouble() const SAL_THROW(()) 809 { 810 return rtl_str_toDouble( pData->buffer ); 811 } 812 813 /** 814 Returns the string representation of the sal_Bool argument. 815 816 If the sal_Bool is true, the string "true" is returned. 817 If the sal_Bool is false, the string "false" is returned. 818 This function can't be used for language specific conversion. 819 820 @param b a sal_Bool. 821 @return a string with the string representation of the argument. 822 */ 823 static OString valueOf( sal_Bool b ) SAL_THROW(()) 824 { 825 sal_Char aBuf[RTL_STR_MAX_VALUEOFBOOLEAN]; 826 rtl_String* pNewData = 0; 827 rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfBoolean( aBuf, b ) ); 828 return OString( pNewData, (DO_NOT_ACQUIRE*)0 ); 829 } 830 831 /** 832 Returns the string representation of the char argument. 833 834 @param c a character. 835 @return a string with the string representation of the argument. 836 */ 837 static OString valueOf( sal_Char c ) SAL_THROW(()) 838 { 839 return OString( &c, 1 ); 840 } 841 842 /** 843 Returns the string representation of the int argument. 844 845 This function can't be used for language specific conversion. 846 847 @param i a int32. 848 @param radix the radix (between 2 and 36) 849 @return a string with the string representation of the argument. 850 */ 851 static OString valueOf( sal_Int32 i, sal_Int16 radix = 10 ) SAL_THROW(()) 852 { 853 sal_Char aBuf[RTL_STR_MAX_VALUEOFINT32]; 854 rtl_String* pNewData = 0; 855 rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfInt32( aBuf, i, radix ) ); 856 return OString( pNewData, (DO_NOT_ACQUIRE*)0 ); 857 } 858 859 /** 860 Returns the string representation of the long argument. 861 862 This function can't be used for language specific conversion. 863 864 @param ll a int64. 865 @param radix the radix (between 2 and 36) 866 @return a string with the string representation of the argument. 867 */ 868 static OString valueOf( sal_Int64 ll, sal_Int16 radix = 10 ) SAL_THROW(()) 869 { 870 sal_Char aBuf[RTL_STR_MAX_VALUEOFINT64]; 871 rtl_String* pNewData = 0; 872 rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfInt64( aBuf, ll, radix ) ); 873 return OString( pNewData, (DO_NOT_ACQUIRE*)0 ); 874 } 875 876 /** 877 Returns the string representation of the float argument. 878 879 This function can't be used for language specific conversion. 880 881 @param f a float. 882 @return a string with the string representation of the argument. 883 */ 884 static OString valueOf( float f ) SAL_THROW(()) 885 { 886 sal_Char aBuf[RTL_STR_MAX_VALUEOFFLOAT]; 887 rtl_String* pNewData = 0; 888 rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfFloat( aBuf, f ) ); 889 return OString( pNewData, (DO_NOT_ACQUIRE*)0 ); 890 } 891 892 /** 893 Returns the string representation of the double argument. 894 895 This function can't be used for language specific conversion. 896 897 @param d a double. 898 @return a string with the string representation of the argument. 899 */ 900 static OString valueOf( double d ) SAL_THROW(()) 901 { 902 sal_Char aBuf[RTL_STR_MAX_VALUEOFDOUBLE]; 903 rtl_String* pNewData = 0; 904 rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfDouble( aBuf, d ) ); 905 return OString( pNewData, (DO_NOT_ACQUIRE*)0 ); 906 } 907 }; 908 909 /* ======================================================================= */ 910 911 /** A helper to use OStrings with hash maps. 912 913 Instances of this class are unary function objects that can be used as 914 hash function arguments to STLPort's hash_map and similar constructs. 915 */ 916 struct OStringHash 917 { 918 /** Compute a hash code for a string. 919 920 @param rString 921 a string. 922 923 @return 924 a hash code for the string. This hash code should not be stored 925 persistently, as its computation may change in later revisions. 926 */ 927 size_t operator()( const rtl::OString& rString ) const 928 { return (size_t)rString.hashCode(); } 929 }; 930 931 /* ======================================================================= */ 932 933 /** Equality functor for classic c-strings (i.e. null-terminated char* strings) */ 934 struct CStringEqual 935 { 936 bool operator()( const char* p1, const char* p2) const { 937 while( *p1) 938 if( *(p1++) != *(p2++)) 939 return false; 940 return true; 941 } 942 }; 943 944 /** Hashing functor for classic c-strings (i.e. null-terminated char* strings) */ 945 struct CStringHash 946 { 947 size_t operator()( const char* p) const { 948 size_t n = 0; 949 while( *p) 950 n += 4*n + *reinterpret_cast<const unsigned char*>(p++); 951 return n; 952 } 953 }; 954 955 } /* Namespace */ 956 957 #endif /* __cplusplus */ 958 959 #endif /* _RTL_STRING_HXX_ */ 960 961