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 private: 223 /** 224 Returns a pointer to the characters of this string. 225 226 NOTE: the implicit cast to a char pointer is obsolete 227 because it is too dangerous #i123068# 228 229 <p>The returned pointer is not guaranteed to point to a null-terminated 230 byte string. Note that this string object may contain embedded null 231 characters, which will thus also be embedded in the returned byte 232 string.</p> 233 234 @return a pointer to a (not necessarily null-terminated) byte string 235 representing the characters of this string object. 236 */ 237 238 public: 239 operator const sal_Char *() const SAL_THROW(()) { return pData->buffer; } 240 /** Returns a reference to a character of this string. */ 241 sal_Char& operator[]( int n ) { return pData->buffer[n]; } 242 /** Returns a const reference to a character of this string. */ 243 const sal_Char& operator[]( int n ) const { return pData->buffer[n]; } 244 /** Returns a bool indicating whether this string is empty. */ 245 bool isEmpty() const { return (pData->length == 0); } 246 247 /** 248 Returns a pointer to the characters of this string. 249 250 <p>The returned pointer is guaranteed to point to a null-terminated byte 251 string. But note that this string object may contain embedded null 252 characters, which will thus also be embedded in the returned 253 null-terminated byte string.</p> 254 255 @return a pointer to a null-terminated byte string representing the 256 characters of this string object. 257 */ 258 const sal_Char * getStr() const SAL_THROW(()) { return pData->buffer; } 259 260 /** 261 Compares two strings. 262 263 The comparison is based on the numeric value of each character in 264 the strings and return a value indicating their relationship. 265 This function can't be used for language specific sorting. 266 267 @param str the object to be compared. 268 @return 0 - if both strings are equal 269 < 0 - if this string is less than the string argument 270 > 0 - if this string is greater than the string argument 271 */ 272 sal_Int32 compareTo( const OString & str ) const SAL_THROW(()) 273 { 274 return rtl_str_compare_WithLength( pData->buffer, pData->length, 275 str.pData->buffer, str.pData->length ); 276 } 277 278 /** 279 Compares two strings with an maximum count of characters. 280 281 The comparison is based on the numeric value of each character in 282 the strings and return a value indicating their relationship. 283 This function can't be used for language specific sorting. 284 285 @param str the object to be compared. 286 @param maxLength the maximum count of characters to be compared. 287 @return 0 - if both strings are equal 288 < 0 - if this string is less than the string argument 289 > 0 - if this string is greater than the string argument 290 */ 291 sal_Int32 compareTo( const OString & rObj, sal_Int32 maxLength ) const SAL_THROW(()) 292 { 293 return rtl_str_shortenedCompare_WithLength( pData->buffer, pData->length, 294 rObj.pData->buffer, rObj.pData->length, maxLength ); 295 } 296 297 /** 298 Compares two strings in reverse order. 299 300 The comparison is based on the numeric value of each character in 301 the strings and return a value indicating their relationship. 302 This function can't be used for language specific sorting. 303 304 @param str the object to be compared. 305 @return 0 - if both strings are equal 306 < 0 - if this string is less than the string argument 307 > 0 - if this string is greater than the string argument 308 */ 309 sal_Int32 reverseCompareTo( const OString & str ) const SAL_THROW(()) 310 { 311 return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length, 312 str.pData->buffer, str.pData->length ); 313 } 314 315 /** 316 Perform a comparison of two strings. 317 318 The result is true if and only if second string 319 represents the same sequence of characters as the first string. 320 This function can't be used for language specific comparison. 321 322 @param str the object to be compared. 323 @return sal_True if the strings are equal; 324 sal_False, otherwise. 325 */ 326 sal_Bool equals( const OString & str ) const SAL_THROW(()) 327 { 328 if ( pData->length != str.pData->length ) 329 return sal_False; 330 if ( pData == str.pData ) 331 return sal_True; 332 return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length, 333 str.pData->buffer, str.pData->length ) == 0; 334 } 335 336 /** 337 Perform a ASCII lowercase comparison of two strings. 338 339 The result is true if and only if second string 340 represents the same sequence of characters as the first string, 341 ignoring the case. 342 Character values between 65 and 90 (ASCII A-Z) are interpreted as 343 values between 97 and 122 (ASCII a-z). 344 This function can't be used for language specific comparison. 345 346 @param str the object to be compared. 347 @return sal_True if the strings are equal; 348 sal_False, otherwise. 349 */ 350 sal_Bool equalsIgnoreAsciiCase( const OString & str ) const SAL_THROW(()) 351 { 352 if ( pData->length != str.pData->length ) 353 return sal_False; 354 if ( pData == str.pData ) 355 return sal_True; 356 return rtl_str_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, 357 str.pData->buffer, str.pData->length ) == 0; 358 } 359 360 /** 361 Match against a substring appearing in this string. 362 363 The result is true if and only if the second string appears as a substring 364 of this string, at the given position. 365 This function can't be used for language specific comparison. 366 367 @param str the object (substring) to be compared. 368 @param fromIndex the index to start the comparion from. 369 The index must be greater or equal than 0 370 and less or equal as the string length. 371 @return sal_True if str match with the characters in the string 372 at the given position; 373 sal_False, otherwise. 374 */ 375 sal_Bool match( const OString & str, sal_Int32 fromIndex = 0 ) const SAL_THROW(()) 376 { 377 return rtl_str_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, 378 str.pData->buffer, str.pData->length, str.pData->length ) == 0; 379 } 380 381 /** 382 Match against a substring appearing in this string, ignoring the case of 383 ASCII letters. 384 385 The result is true if and only if the second string appears as a substring 386 of this string, at the given position. 387 Character values between 65 and 90 (ASCII A-Z) are interpreted as 388 values between 97 and 122 (ASCII a-z). 389 This function can't be used for language specific comparison. 390 391 @param str the object (substring) to be compared. 392 @param fromIndex the index to start the comparion from. 393 The index must be greater or equal than 0 394 and less or equal as the string length. 395 @return sal_True if str match with the characters in the string 396 at the given position; 397 sal_False, otherwise. 398 */ 399 sal_Bool matchIgnoreAsciiCase( const OString & str, sal_Int32 fromIndex = 0 ) const SAL_THROW(()) 400 { 401 return rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, 402 str.pData->buffer, str.pData->length, 403 str.pData->length ) == 0; 404 } 405 406 friend sal_Bool operator == ( const OString& rStr1, const OString& rStr2 ) SAL_THROW(()) 407 { return rStr1.getLength() == rStr2.getLength() && rStr1.compareTo( rStr2 ) == 0; } 408 friend sal_Bool operator == ( const OString& rStr1, const sal_Char * pStr2 ) SAL_THROW(()) 409 { return rStr1.compareTo( pStr2 ) == 0; } 410 friend sal_Bool operator == ( const sal_Char * pStr1, const OString& rStr2 ) SAL_THROW(()) 411 { return OString( pStr1 ).compareTo( rStr2 ) == 0; } 412 413 friend sal_Bool operator != ( const OString& rStr1, const OString& rStr2 ) SAL_THROW(()) 414 { return !(operator == ( rStr1, rStr2 )); } 415 friend sal_Bool operator != ( const OString& rStr1, const sal_Char * pStr2 ) SAL_THROW(()) 416 { return !(operator == ( rStr1, pStr2 )); } 417 friend sal_Bool operator != ( const sal_Char * pStr1, const OString& rStr2 ) SAL_THROW(()) 418 { return !(operator == ( pStr1, rStr2 )); } 419 420 friend sal_Bool operator < ( const OString& rStr1, const OString& rStr2 ) SAL_THROW(()) 421 { return rStr1.compareTo( rStr2 ) < 0; } 422 friend sal_Bool operator > ( const OString& rStr1, const OString& rStr2 ) SAL_THROW(()) 423 { return rStr1.compareTo( rStr2 ) > 0; } 424 friend sal_Bool operator <= ( const OString& rStr1, const OString& rStr2 ) SAL_THROW(()) 425 { return rStr1.compareTo( rStr2 ) <= 0; } 426 friend sal_Bool operator >= ( const OString& rStr1, const OString& rStr2 ) SAL_THROW(()) 427 { return rStr1.compareTo( rStr2 ) >= 0; } 428 429 /** 430 Returns a hashcode for this string. 431 432 @return a hash code value for this object. 433 434 @see rtl::OStringHash for convenient use of hash_map / unordered_map 435 */ 436 sal_Int32 hashCode() const SAL_THROW(()) 437 { 438 return rtl_str_hashCode_WithLength( pData->buffer, pData->length ); 439 } 440 441 /** 442 Returns the index within this string of the first occurrence of the 443 specified character, starting the search at the specified index. 444 445 @param ch character to be located. 446 @param fromIndex the index to start the search from. 447 The index must be greater or equal than 0 448 and less or equal as the string length. 449 @return the index of the first occurrence of the character in the 450 character sequence represented by this string that is 451 greater than or equal to fromIndex, or 452 -1 if the character does not occur. 453 */ 454 sal_Int32 indexOf( sal_Char ch, sal_Int32 fromIndex = 0 ) const SAL_THROW(()) 455 { 456 sal_Int32 ret = rtl_str_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch ); 457 return (ret < 0 ? ret : ret+fromIndex); 458 } 459 460 /** 461 Returns the index within this string of the last occurrence of the 462 specified character, searching backward starting at the end. 463 464 @param ch character to be located. 465 @return the index of the last occurrence of the character in the 466 character sequence represented by this string, or 467 -1 if the character does not occur. 468 */ 469 sal_Int32 lastIndexOf( sal_Char ch ) const SAL_THROW(()) 470 { 471 return rtl_str_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch ); 472 } 473 474 /** 475 Returns the index within this string of the last occurrence of the 476 specified character, searching backward starting before the specified 477 index. 478 479 @param ch character to be located. 480 @param fromIndex the index before which to start the search. 481 @return the index of the last occurrence of the character in the 482 character sequence represented by this string that 483 is less than fromIndex, or -1 484 if the character does not occur before that point. 485 */ 486 sal_Int32 lastIndexOf( sal_Char ch, sal_Int32 fromIndex ) const SAL_THROW(()) 487 { 488 return rtl_str_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch ); 489 } 490 491 /** 492 Returns the index within this string of the first occurrence of the 493 specified substring, starting at the specified index. 494 495 If str doesn't include any character, always -1 is 496 returned. This is also the case, if both strings are empty. 497 498 @param str the substring to search for. 499 @param fromIndex the index to start the search from. 500 @return If the string argument occurs one or more times as a substring 501 within this string at the starting index, then the index 502 of the first character of the first such substring is 503 returned. If it does not occur as a substring starting 504 at fromIndex or beyond, -1 is returned. 505 */ 506 sal_Int32 indexOf( const OString & str, sal_Int32 fromIndex = 0 ) const SAL_THROW(()) 507 { 508 sal_Int32 ret = rtl_str_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, 509 str.pData->buffer, str.pData->length ); 510 return (ret < 0 ? ret : ret+fromIndex); 511 } 512 513 /** 514 Returns the index within this string of the last occurrence of 515 the specified substring, searching backward starting at the end. 516 517 The returned index indicates the starting index of the substring 518 in this string. 519 If str doesn't include any character, always -1 is 520 returned. This is also the case, if both strings are empty. 521 522 @param str the substring to search for. 523 @return If the string argument occurs one or more times as a substring 524 within this string, then the index of the first character of 525 the last such substring is returned. If it does not occur as 526 a substring, -1 is returned. 527 */ 528 sal_Int32 lastIndexOf( const OString & str ) const SAL_THROW(()) 529 { 530 return rtl_str_lastIndexOfStr_WithLength( pData->buffer, pData->length, 531 str.pData->buffer, str.pData->length ); 532 } 533 534 /** 535 Returns the index within this string of the last occurrence of 536 the specified substring, searching backward starting before the specified 537 index. 538 539 The returned index indicates the starting index of the substring 540 in this string. 541 If str doesn't include any character, always -1 is 542 returned. This is also the case, if both strings are empty. 543 544 @param str the substring to search for. 545 @param fromIndex the index before which to start the search. 546 @return If the string argument occurs one or more times as a substring 547 within this string before the starting index, then the index 548 of the first character of the last such substring is 549 returned. Otherwise, -1 is returned. 550 */ 551 sal_Int32 lastIndexOf( const OString & str, sal_Int32 fromIndex ) const SAL_THROW(()) 552 { 553 return rtl_str_lastIndexOfStr_WithLength( pData->buffer, fromIndex, 554 str.pData->buffer, str.pData->length ); 555 } 556 557 /** 558 Returns a new string that is a substring of this string. 559 560 The substring begins at the specified beginIndex. It is an error for 561 beginIndex to be negative or to be greater than the length of this string. 562 563 @param beginIndex the beginning index, inclusive. 564 @return the specified substring. 565 */ 566 OString copy( sal_Int32 beginIndex ) const SAL_THROW(()) 567 { 568 OSL_ASSERT(beginIndex >= 0 && beginIndex <= getLength()); 569 if ( beginIndex == 0 ) 570 return *this; 571 else 572 { 573 rtl_String* pNew = 0; 574 rtl_string_newFromStr_WithLength( &pNew, pData->buffer+beginIndex, getLength()-beginIndex ); 575 return OString( pNew, (DO_NOT_ACQUIRE*)0 ); 576 } 577 } 578 579 /** 580 Returns a new string that is a substring of this string. 581 582 The substring begins at the specified beginIndex and contains count 583 characters. It is an error for either beginIndex or count to be negative, 584 or for beginIndex + count to be greater than the length of this string. 585 586 @param beginIndex the beginning index, inclusive. 587 @param count the number of characters. 588 @return the specified substring. 589 */ 590 OString copy( sal_Int32 beginIndex, sal_Int32 count ) const SAL_THROW(()) 591 { 592 OSL_ASSERT(beginIndex >= 0 && beginIndex <= getLength() 593 && count >= 0 && count <= getLength() - beginIndex); 594 if ( (beginIndex == 0) && (count == getLength()) ) 595 return *this; 596 else 597 { 598 rtl_String* pNew = 0; 599 rtl_string_newFromStr_WithLength( &pNew, pData->buffer+beginIndex, count ); 600 return OString( pNew, (DO_NOT_ACQUIRE*)0 ); 601 } 602 } 603 604 /** 605 Concatenates the specified string to the end of this string. 606 607 @param str the string that is concatenated to the end 608 of this string. 609 @return a string that represents the concatenation of this string 610 followed by the string argument. 611 */ 612 OString concat( const OString & str ) const SAL_THROW(()) 613 { 614 rtl_String* pNew = 0; 615 rtl_string_newConcat( &pNew, pData, str.pData ); 616 return OString( pNew, (DO_NOT_ACQUIRE*)0 ); 617 } 618 619 friend OString operator+( const OString & str1, const OString & str2 ) SAL_THROW(()) 620 { 621 return str1.concat( str2 ); 622 } 623 624 /** 625 Returns a new string resulting from replacing n = count characters 626 from position index in this string with newStr. 627 628 @param index the replacing index in str. 629 The index must be greater or equal as 0 and 630 less or equal as the length of the string. 631 @param count the count of charcters that will replaced 632 The count must be greater or equal as 0 and 633 less or equal as the length of the string minus index. 634 @param newStr the new substring. 635 @return the new string. 636 */ 637 OString replaceAt( sal_Int32 index, sal_Int32 count, const OString& newStr ) const SAL_THROW(()) 638 { 639 rtl_String* pNew = 0; 640 rtl_string_newReplaceStrAt( &pNew, pData, index, count, newStr.pData ); 641 return OString( pNew, (DO_NOT_ACQUIRE*)0 ); 642 } 643 644 /** 645 Returns a new string resulting from replacing all occurrences of 646 oldChar in this string with newChar. 647 648 If the character oldChar does not occur in the character sequence 649 represented by this object, then the string is assigned with 650 str. 651 652 @param oldChar the old character. 653 @param newChar the new character. 654 @return a string derived from this string by replacing every 655 occurrence of oldChar with newChar. 656 */ 657 OString replace( sal_Char oldChar, sal_Char newChar ) const SAL_THROW(()) 658 { 659 rtl_String* pNew = 0; 660 rtl_string_newReplace( &pNew, pData, oldChar, newChar ); 661 return OString( pNew, (DO_NOT_ACQUIRE*)0 ); 662 } 663 664 /** 665 Converts from this string all ASCII uppercase characters (65-90) 666 to ASCII lowercase characters (97-122). 667 668 This function can't be used for language specific conversion. 669 If the string doesn't contain characters which must be converted, 670 then the new string is assigned with str. 671 672 @return the string, converted to ASCII lowercase. 673 */ 674 OString toAsciiLowerCase() const SAL_THROW(()) 675 { 676 rtl_String* pNew = 0; 677 rtl_string_newToAsciiLowerCase( &pNew, pData ); 678 return OString( pNew, (DO_NOT_ACQUIRE*)0 ); 679 } 680 681 /** 682 Converts from this string all ASCII lowercase characters (97-122) 683 to ASCII uppercase characters (65-90). 684 685 This function can't be used for language specific conversion. 686 If the string doesn't contain characters which must be converted, 687 then the new string is assigned with str. 688 689 @return the string, converted to ASCII uppercase. 690 */ 691 OString toAsciiUpperCase() const SAL_THROW(()) 692 { 693 rtl_String* pNew = 0; 694 rtl_string_newToAsciiUpperCase( &pNew, pData ); 695 return OString( pNew, (DO_NOT_ACQUIRE*)0 ); 696 } 697 698 /** 699 Returns a new string resulting from removing white space from both ends 700 of the string. 701 702 All characters that have codes less than or equal to 703 32 (the space character) are considered to be white space. 704 If the string doesn't contain white spaces at both ends, 705 then the new string is assigned with str. 706 707 @return the string, with white space removed from the front and end. 708 */ 709 OString trim() const SAL_THROW(()) 710 { 711 rtl_String* pNew = 0; 712 rtl_string_newTrim( &pNew, pData ); 713 return OString( pNew, (DO_NOT_ACQUIRE*)0 ); 714 } 715 716 /** 717 Returns a token in the string. 718 719 Example: 720 sal_Int32 nIndex = 0; 721 do 722 { 723 ... 724 OString aToken = aStr.getToken( 0, ';', nIndex ); 725 ... 726 } 727 while ( nIndex >= 0 ); 728 729 @param token the number of the token to return. 730 @param cTok the character which separate the tokens. 731 @param index the position at which the token is searched in the 732 string. 733 The index must not be greater thanthe length of the 734 string. 735 This param is set to the position of the 736 next token or to -1, if it is the last token. 737 @return the token; if either token or index is negative, an empty token 738 is returned (and index is set to -1) 739 */ 740 OString getToken( sal_Int32 token, sal_Char cTok, sal_Int32& index ) const SAL_THROW(()) 741 { 742 rtl_String * pNew = 0; 743 index = rtl_string_getToken( &pNew, pData, token, cTok, index ); 744 return OString( pNew, (DO_NOT_ACQUIRE *)0 ); 745 } 746 747 /** 748 Returns the Boolean value from this string. 749 750 This function can't be used for language specific conversion. 751 752 @return sal_True, if the string is 1 or "True" in any ASCII case. 753 sal_False in any other case. 754 */ 755 sal_Bool toBoolean() const SAL_THROW(()) 756 { 757 return rtl_str_toBoolean( pData->buffer ); 758 } 759 760 /** 761 Returns the first character from this string. 762 763 @return the first character from this string or 0, if this string 764 is emptry. 765 */ 766 sal_Char toChar() const SAL_THROW(()) 767 { 768 return pData->buffer[0]; 769 } 770 771 /** 772 Returns the int32 value from this string. 773 774 This function can't be used for language specific conversion. 775 776 @param radix the radix (between 2 and 36) 777 @return the int32 represented from this string. 778 0 if this string represents no number. 779 */ 780 sal_Int32 toInt32( sal_Int16 radix = 10 ) const SAL_THROW(()) 781 { 782 return rtl_str_toInt32( pData->buffer, radix ); 783 } 784 785 /** 786 Returns the int64 value from this string. 787 788 This function can't be used for language specific conversion. 789 790 @param radix the radix (between 2 and 36) 791 @return the int64 represented from this string. 792 0 if this string represents no number. 793 */ 794 sal_Int64 toInt64( sal_Int16 radix = 10 ) const SAL_THROW(()) 795 { 796 return rtl_str_toInt64( pData->buffer, radix ); 797 } 798 799 /** 800 Returns the float value from this string. 801 802 This function can't be used for language specific conversion. 803 804 @return the float represented from this string. 805 0.0 if this string represents no number. 806 */ 807 float toFloat() const SAL_THROW(()) 808 { 809 return rtl_str_toFloat( pData->buffer ); 810 } 811 812 /** 813 Returns the double value from this string. 814 815 This function can't be used for language specific conversion. 816 817 @return the double represented from this string. 818 0.0 if this string represents no number. 819 */ 820 double toDouble() const SAL_THROW(()) 821 { 822 return rtl_str_toDouble( pData->buffer ); 823 } 824 825 /** 826 Returns the string representation of the sal_Bool argument. 827 828 If the sal_Bool is true, the string "true" is returned. 829 If the sal_Bool is false, the string "false" is returned. 830 This function can't be used for language specific conversion. 831 832 @param b a sal_Bool. 833 @return a string with the string representation of the argument. 834 */ 835 static OString valueOf( sal_Bool b ) SAL_THROW(()) 836 { 837 sal_Char aBuf[RTL_STR_MAX_VALUEOFBOOLEAN]; 838 rtl_String* pNewData = 0; 839 rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfBoolean( aBuf, b ) ); 840 return OString( pNewData, (DO_NOT_ACQUIRE*)0 ); 841 } 842 843 /** 844 Returns the string representation of the char argument. 845 846 @param c a character. 847 @return a string with the string representation of the argument. 848 */ 849 static OString valueOf( sal_Char c ) SAL_THROW(()) 850 { 851 return OString( &c, 1 ); 852 } 853 854 /** 855 Returns the string representation of the int argument. 856 857 This function can't be used for language specific conversion. 858 859 @param i a int32. 860 @param radix the radix (between 2 and 36) 861 @return a string with the string representation of the argument. 862 */ 863 static OString valueOf( sal_Int32 i, sal_Int16 radix = 10 ) SAL_THROW(()) 864 { 865 sal_Char aBuf[RTL_STR_MAX_VALUEOFINT32]; 866 rtl_String* pNewData = 0; 867 rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfInt32( aBuf, i, radix ) ); 868 return OString( pNewData, (DO_NOT_ACQUIRE*)0 ); 869 } 870 871 /** 872 Returns the string representation of the long argument. 873 874 This function can't be used for language specific conversion. 875 876 @param ll a int64. 877 @param radix the radix (between 2 and 36) 878 @return a string with the string representation of the argument. 879 */ 880 static OString valueOf( sal_Int64 ll, sal_Int16 radix = 10 ) SAL_THROW(()) 881 { 882 sal_Char aBuf[RTL_STR_MAX_VALUEOFINT64]; 883 rtl_String* pNewData = 0; 884 rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfInt64( aBuf, ll, radix ) ); 885 return OString( pNewData, (DO_NOT_ACQUIRE*)0 ); 886 } 887 888 /** 889 Returns the string representation of the float argument. 890 891 This function can't be used for language specific conversion. 892 893 @param f a float. 894 @return a string with the string representation of the argument. 895 */ 896 static OString valueOf( float f ) SAL_THROW(()) 897 { 898 sal_Char aBuf[RTL_STR_MAX_VALUEOFFLOAT]; 899 rtl_String* pNewData = 0; 900 rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfFloat( aBuf, f ) ); 901 return OString( pNewData, (DO_NOT_ACQUIRE*)0 ); 902 } 903 904 /** 905 Returns the string representation of the double argument. 906 907 This function can't be used for language specific conversion. 908 909 @param d a double. 910 @return a string with the string representation of the argument. 911 */ 912 static OString valueOf( double d ) SAL_THROW(()) 913 { 914 sal_Char aBuf[RTL_STR_MAX_VALUEOFDOUBLE]; 915 rtl_String* pNewData = 0; 916 rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfDouble( aBuf, d ) ); 917 return OString( pNewData, (DO_NOT_ACQUIRE*)0 ); 918 } 919 }; 920 921 /* ======================================================================= */ 922 923 /** A helper to use OStrings with hash maps. 924 925 Instances of this class are unary function objects that can be used as 926 hash function arguments to unordered_map, hash_map and similar constructs. 927 */ 928 struct OStringHash 929 { 930 /** Compute a hash code for a string. 931 932 @param rString 933 a string. 934 935 @return 936 a hash code for the string. This hash code should not be stored 937 persistently, as its computation may change in later revisions. 938 */ 939 size_t operator()( const rtl::OString& rString ) const 940 { return (size_t)rString.hashCode(); } 941 }; 942 943 /* ======================================================================= */ 944 945 /** Equality functor for classic c-strings (i.e. null-terminated char* strings) */ 946 struct CStringEqual 947 { 948 bool operator()( const char* p1, const char* p2) const { 949 while( *p1 != '\0') 950 if( *(p1++) != *(p2++)) 951 return false; 952 return (*p2 == '\0'); 953 } 954 }; 955 956 /** Hashing functor for classic c-strings (i.e. null-terminated char* strings) */ 957 struct CStringHash 958 { 959 size_t operator()( const char* p) const { 960 size_t n = 0; 961 while( *p) 962 n += 4*n + *reinterpret_cast<const unsigned char*>(p++); 963 return n; 964 } 965 }; 966 967 } /* Namespace */ 968 969 /* Helper methods to support OString messages in OSL_ENSURE, DBG_ERROR, DBG_WARN, DBG_TRACE, etc. */ 970 inline sal_Bool SAL_CALL osl_assertFailedLine( const sal_Char* pszFileName, sal_Int32 nLine, const ::rtl::OString& rMessage) 971 { return osl_assertFailedLine( pszFileName, nLine, rMessage.getStr()); } 972 inline void DbgOut( const rtl::OString& rMessage, sal_uInt16 nOutType, const sal_Char* pFileName, sal_uInt16 nLineNum ) 973 { DbgOut( rMessage.getStr(), nOutType, pFileName, nLineNum); } 974 975 #endif /* __cplusplus */ 976 977 #endif /* _RTL_STRING_HXX_ */ 978 979