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