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