UDK 3.2.7 C/C++ API Reference

rtl/string.hxx

Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /*
00003  * This file is part of the LibreOffice project.
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this
00007  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
00008  *
00009  * This file incorporates work covered by the following license notice:
00010  *
00011  *   Licensed to the Apache Software Foundation (ASF) under one or more
00012  *   contributor license agreements. See the NOTICE file distributed
00013  *   with this work for additional information regarding copyright
00014  *   ownership. The ASF licenses this file to you under the Apache
00015  *   License, Version 2.0 (the "License"); you may not use this file
00016  *   except in compliance with the License. You may obtain a copy of
00017  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
00018  */
00019 
00020 #ifndef _RTL_STRING_HXX_
00021 #define _RTL_STRING_HXX_
00022 
00023 #include "sal/config.h"
00024 
00025 #include <cassert>
00026 #include <ostream>
00027 #include <string.h>
00028 
00029 #include <osl/diagnose.h>
00030 #include <rtl/textenc.h>
00031 #include <rtl/string.h>
00032 #include <rtl/stringutils.hxx>
00033 
00034 #ifdef RTL_FAST_STRING
00035 #include <rtl/stringconcat.hxx>
00036 #endif
00037 
00038 #include "sal/log.hxx"
00039 
00040 #if !defined EXCEPTIONS_OFF
00041 #include <new>
00042 #endif
00043 
00044 // The unittest uses slightly different code to help check that the proper
00045 // calls are made. The class is put into a different namespace to make
00046 // sure the compiler generates a different (if generating also non-inline)
00047 // copy of the function and does not merge them together. The class
00048 // is "brought" into the proper rtl namespace by a typedef below.
00049 #ifdef RTL_STRING_UNITTEST
00050 #define rtl rtlunittest
00051 #endif
00052 
00053 namespace rtl
00054 {
00055 
00056 #ifdef RTL_STRING_UNITTEST
00057 #undef rtl
00058 // helper macro to make functions appear more readable
00059 #define RTL_STRING_CONST_FUNCTION rtl_string_unittest_const_literal_function = true;
00060 #else
00061 #define RTL_STRING_CONST_FUNCTION
00062 #endif
00063 
00064 /* ======================================================================= */
00065 
00090 class SAL_WARN_UNUSED OString
00091 {
00092 public:
00094     rtl_String * pData;
00096 
00097 private:
00098     class DO_NOT_ACQUIRE;
00099 
00100     OString( rtl_String * value, SAL_UNUSED_PARAMETER DO_NOT_ACQUIRE * )
00101     {
00102         pData = value;
00103     }
00104 
00105 public:
00109     OString() SAL_THROW(())
00110     {
00111         pData = 0;
00112         rtl_string_new( &pData );
00113     }
00114 
00120     OString( const OString & str ) SAL_THROW(())
00121     {
00122         pData = str.pData;
00123         rtl_string_acquire( pData );
00124     }
00125 
00131     OString( rtl_String * str ) SAL_THROW(())
00132     {
00133         pData = str;
00134         rtl_string_acquire( pData );
00135     }
00136 
00144     inline OString( rtl_String * str, __sal_NoAcquire ) SAL_THROW(())
00145     {
00146         pData = str;
00147     }
00148 
00154     explicit OString( sal_Char value ) SAL_THROW(())
00155         : pData (0)
00156     {
00157         rtl_string_newFromStr_WithLength( &pData, &value, 1 );
00158     }
00159 
00168 #ifdef HAVE_SFINAE_ANONYMOUS_BROKEN
00169     // Old gcc can try to convert anonymous enums to OString and give compile error.
00170     // So there's no special-cased handling of string literals.
00171     // These are inline functions and technically both variants should work
00172     // the same in practice, so there should be no compatibility problem.
00173     OString( const sal_Char * value ) SAL_THROW(())
00174      {
00175          pData = 0;
00176          rtl_string_newFromStr( &pData, value );
00177      }
00178 #else
00179     template< typename T >
00180     OString( const T& value, typename internal::CharPtrDetector< T, internal::Dummy >::Type = internal::Dummy() ) SAL_THROW(())
00181     {
00182         pData = 0;
00183         rtl_string_newFromStr( &pData, value );
00184     }
00185 
00186     template< typename T >
00187     OString( T& value, typename internal::NonConstCharArrayDetector< T, internal::Dummy >::Type = internal::Dummy() ) SAL_THROW(())
00188     {
00189         pData = 0;
00190         rtl_string_newFromStr( &pData, value );
00191     }
00192 
00203     template< typename T >
00204     OString( T& literal, typename internal::ConstCharArrayDetector< T, internal::Dummy >::Type = internal::Dummy() ) SAL_THROW(())
00205     {
00206         assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
00207         pData = 0;
00208         rtl_string_newFromLiteral( &pData, literal, internal::ConstCharArrayDetector< T, void >::size - 1, 0 );
00209 #ifdef RTL_STRING_UNITTEST
00210         rtl_string_unittest_const_literal = true;
00211 #endif
00212     }
00213 
00214 #endif // HAVE_SFINAE_ANONYMOUS_BROKEN
00215 
00224     OString( const sal_Char * value, sal_Int32 length ) SAL_THROW(())
00225     {
00226         pData = 0;
00227         rtl_string_newFromStr_WithLength( &pData, value, length );
00228     }
00229 
00244     OString( const sal_Unicode * value, sal_Int32 length,
00245              rtl_TextEncoding encoding,
00246              sal_uInt32 convertFlags = OUSTRING_TO_OSTRING_CVTFLAGS )
00247     {
00248         pData = 0;
00249         rtl_uString2String( &pData, value, length, encoding, convertFlags );
00250         if (pData == 0) {
00251 #if defined EXCEPTIONS_OFF
00252             abort();
00253 #else
00254             throw std::bad_alloc();
00255 #endif
00256         }
00257     }
00258 
00259 #ifdef RTL_FAST_STRING
00260     template< typename T1, typename T2 >
00261     OString( const OStringConcat< T1, T2 >& c )
00262     {
00263         const sal_Int32 l = c.length();
00264         rtl_String* buffer = NULL;
00265         rtl_string_new_WithLength( &buffer, l );
00266         if (l != 0)
00267         {
00268             char* end = c.addData( buffer->buffer );
00269             buffer->length = end - buffer->buffer;
00270         }
00271         pData = buffer;
00272     }
00273 #endif
00274 
00278     ~OString() SAL_THROW(())
00279     {
00280         rtl_string_release( pData );
00281     }
00282 
00288     OString & operator=( const OString & str ) SAL_THROW(())
00289     {
00290         rtl_string_assign( &pData, str.pData );
00291         return *this;
00292     }
00293 
00299     template< typename T >
00300     typename internal::ConstCharArrayDetector< T, OString& >::Type operator=( T& literal ) SAL_THROW(())
00301     {
00302         RTL_STRING_CONST_FUNCTION
00303         assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
00304         rtl_string_newFromLiteral( &pData, literal, internal::ConstCharArrayDetector< T, void >::size - 1, 0 );
00305         return *this;
00306     }
00307 
00313     OString & operator+=( const OString & str ) SAL_THROW(())
00314     {
00315         rtl_string_newConcat( &pData, pData, str.pData );
00316         return *this;
00317     }
00318 
00327     sal_Int32 getLength() const SAL_THROW(()) { return pData->length; }
00328 
00337     bool isEmpty() const SAL_THROW(())
00338     {
00339         return pData->length == 0;
00340     }
00341 
00353     const sal_Char * getStr() const SAL_THROW(()) { return pData->buffer; }
00354 
00364     sal_Char operator [](sal_Int32 index) const { return getStr()[index]; }
00365 
00378     sal_Int32 compareTo( const OString & str ) const SAL_THROW(())
00379     {
00380         return rtl_str_compare_WithLength( pData->buffer, pData->length,
00381                                            str.pData->buffer, str.pData->length );
00382     }
00383 
00397     sal_Int32 compareTo( const OString & rObj, sal_Int32 maxLength ) const SAL_THROW(())
00398     {
00399         return rtl_str_shortenedCompare_WithLength( pData->buffer, pData->length,
00400                                                     rObj.pData->buffer, rObj.pData->length, maxLength );
00401     }
00402 
00415     sal_Int32 reverseCompareTo( const OString & str ) const SAL_THROW(())
00416     {
00417         return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length,
00418                                                   str.pData->buffer, str.pData->length );
00419     }
00420 
00432     sal_Bool equals( const OString & str ) const SAL_THROW(())
00433     {
00434         if ( pData->length != str.pData->length )
00435             return sal_False;
00436         if ( pData == str.pData )
00437             return sal_True;
00438         return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length,
00439                                                   str.pData->buffer, str.pData->length ) == 0;
00440     }
00441 
00457     sal_Bool equalsL( const sal_Char* value, sal_Int32 length ) const SAL_THROW(())
00458     {
00459         if ( pData->length != length )
00460             return sal_False;
00461 
00462         return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length,
00463                                                   value, length ) == 0;
00464     }
00465 
00480     sal_Bool equalsIgnoreAsciiCase( const OString & str ) const SAL_THROW(())
00481     {
00482         if ( pData->length != str.pData->length )
00483             return sal_False;
00484         if ( pData == str.pData )
00485             return sal_True;
00486         return rtl_str_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
00487                                                           str.pData->buffer, str.pData->length ) == 0;
00488     }
00489 
00511 #ifdef HAVE_SFINAE_ANONYMOUS_BROKEN
00512     sal_Bool equalsIgnoreAsciiCase( const sal_Char * asciiStr ) const SAL_THROW(())
00513     {
00514         return rtl_str_compareIgnoreAsciiCase( pData->buffer, asciiStr ) == 0;
00515     }
00516 #else
00517     template< typename T >
00518     typename internal::CharPtrDetector< T, bool >::Type equalsIgnoreAsciiCase( const T& asciiStr ) const SAL_THROW(())
00519     {
00520         return rtl_str_compareIgnoreAsciiCase( pData->buffer, asciiStr ) == 0;
00521     }
00522 
00523     template< typename T >
00524     typename internal::NonConstCharArrayDetector< T, bool >::Type equalsIgnoreAsciiCase( T& asciiStr ) const SAL_THROW(())
00525     {
00526         return rtl_str_compareIgnoreAsciiCase( pData->buffer, asciiStr ) == 0;
00527     }
00528 
00534     template< typename T >
00535     typename internal::ConstCharArrayDetector< T, bool >::Type  equalsIgnoreAsciiCase( T& literal ) const SAL_THROW(())
00536     {
00537         RTL_STRING_CONST_FUNCTION
00538         assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
00539         if ( pData->length != internal::ConstCharArrayDetector< T, void >::size - 1 )
00540             return false;
00541         return rtl_str_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
00542                                                           literal, internal::ConstCharArrayDetector< T, void >::size - 1 ) == 0;
00543     }
00544 #endif
00545 
00565     sal_Bool equalsIgnoreAsciiCaseL( const sal_Char * asciiStr, sal_Int32 asciiStrLength ) const SAL_THROW(())
00566     {
00567         if ( pData->length != asciiStrLength )
00568             return sal_False;
00569 
00570         return rtl_str_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
00571                                                           asciiStr, asciiStrLength ) == 0;
00572     }
00573 
00589     sal_Bool match( const OString & str, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
00590     {
00591         return rtl_str_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
00592                                                     str.pData->buffer, str.pData->length, str.pData->length ) == 0;
00593     }
00594 
00600     template< typename T >
00601     typename internal::ConstCharArrayDetector< T, bool >::Type  match( T& literal, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
00602     {
00603         RTL_STRING_CONST_FUNCTION
00604         assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
00605         return rtl_str_shortenedCompare_WithLength(
00606             pData->buffer + fromIndex, pData->length - fromIndex,
00607             literal, internal::ConstCharArrayDetector< T, void >::size - 1, internal::ConstCharArrayDetector< T, void >::size - 1) == 0;
00608     }
00609 
00626     bool matchL(
00627         char const * str, sal_Int32 strLength, sal_Int32 fromIndex = 0)
00628         const
00629     {
00630         return rtl_str_shortenedCompare_WithLength(
00631             pData->buffer + fromIndex, pData->length - fromIndex,
00632             str, strLength, strLength) == 0;
00633     }
00634 
00635     // This overload is left undefined, to detect calls of matchL that
00636     // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
00637     // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
00638     // platforms):
00639 #if SAL_TYPES_SIZEOFLONG == 8
00640     void matchL(char const *, sal_Int32, rtl_TextEncoding) const;
00641 #endif
00642 
00661     sal_Bool matchIgnoreAsciiCase( const OString & str, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
00662     {
00663         return rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
00664                                                                    str.pData->buffer, str.pData->length,
00665                                                                    str.pData->length ) == 0;
00666     }
00667 
00673     template< typename T >
00674     typename internal::ConstCharArrayDetector< T, bool >::Type matchIgnoreAsciiCase( T& literal, sal_Int32 fromIndex = 0 ) const
00675     {
00676         RTL_STRING_CONST_FUNCTION
00677         assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
00678         return rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
00679             literal, internal::ConstCharArrayDetector< T, void >::size - 1, internal::ConstCharArrayDetector< T, void >::size - 1 ) == 0;
00680     }
00681 
00692     bool startsWith(OString const & str) const {
00693         return match(str, 0);
00694     }
00695 
00701     template< typename T >
00702     typename internal::ConstCharArrayDetector< T, bool >::Type startsWith( T& literal ) const
00703     {
00704         RTL_STRING_CONST_FUNCTION
00705         return match(literal, 0);
00706     }
00707 
00718     bool endsWith(OString const & str) const {
00719         return str.getLength() <= getLength()
00720             && match(str, getLength() - str.getLength());
00721     }
00722 
00728     template< typename T >
00729     typename internal::ConstCharArrayDetector< T, bool >::Type endsWith( T& literal ) const
00730     {
00731         RTL_STRING_CONST_FUNCTION
00732         assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
00733         return internal::ConstCharArrayDetector< T, void >::size - 1 <= getLength()
00734             && match(literal, getLength() - ( internal::ConstCharArrayDetector< T, void >::size - 1 ));
00735     }
00736 
00750     bool endsWithL(char const * str, sal_Int32 strLength) const {
00751         return strLength <= getLength()
00752             && matchL(str, strLength, getLength() - strLength);
00753     }
00754 
00755     friend sal_Bool     operator == ( const OString& rStr1, const OString& rStr2 ) SAL_THROW(())
00756                         { return rStr1.equals(rStr2); }
00757     friend sal_Bool     operator != ( const OString& rStr1,     const OString& rStr2 ) SAL_THROW(())
00758                         { return !(operator == ( rStr1, rStr2 )); }
00759     friend sal_Bool     operator <  ( const OString& rStr1,    const OString& rStr2 ) SAL_THROW(())
00760                         { return rStr1.compareTo( rStr2 ) < 0; }
00761     friend sal_Bool     operator >  ( const OString& rStr1,    const OString& rStr2 ) SAL_THROW(())
00762                         { return rStr1.compareTo( rStr2 ) > 0; }
00763     friend sal_Bool     operator <= ( const OString& rStr1,    const OString& rStr2 ) SAL_THROW(())
00764                         { return rStr1.compareTo( rStr2 ) <= 0; }
00765     friend sal_Bool     operator >= ( const OString& rStr1,    const OString& rStr2 ) SAL_THROW(())
00766                         { return rStr1.compareTo( rStr2 ) >= 0; }
00767 
00768     template< typename T >
00769     friend typename internal::CharPtrDetector< T, bool >::Type operator==( const OString& rStr1, const T& value ) SAL_THROW(())
00770     {
00771         return rStr1.compareTo( value ) == 0;
00772     }
00773 
00774     template< typename T >
00775     friend typename internal::NonConstCharArrayDetector< T, bool >::Type operator==( const OString& rStr1, T& value ) SAL_THROW(())
00776     {
00777         return rStr1.compareTo( value ) == 0;
00778     }
00779 
00780     template< typename T >
00781     friend typename internal::CharPtrDetector< T, bool >::Type operator==( const T& value, const OString& rStr2 ) SAL_THROW(())
00782     {
00783         return rStr2.compareTo( value ) == 0;
00784     }
00785 
00786     template< typename T >
00787     friend typename internal::NonConstCharArrayDetector< T, bool >::Type operator==( T& value, const OString& rStr2 ) SAL_THROW(())
00788     {
00789         return rStr2.compareTo( value ) == 0;
00790     }
00791 
00797     template< typename T >
00798     friend typename internal::ConstCharArrayDetector< T, bool >::Type operator==( const OString& rStr, T& literal ) SAL_THROW(())
00799     {
00800         RTL_STRING_CONST_FUNCTION
00801         assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
00802         return rStr.getLength() == internal::ConstCharArrayDetector< T, void >::size - 1
00803             && rtl_str_compare_WithLength( rStr.pData->buffer, rStr.pData->length, literal,
00804                 internal::ConstCharArrayDetector< T, void >::size - 1 ) == 0;
00805     }
00806 
00812     template< typename T >
00813     friend typename internal::ConstCharArrayDetector< T, bool >::Type operator==( T& literal, const OString& rStr ) SAL_THROW(())
00814     {
00815         RTL_STRING_CONST_FUNCTION
00816         assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
00817         return rStr.getLength() == internal::ConstCharArrayDetector< T, void >::size - 1
00818             && rtl_str_compare_WithLength( rStr.pData->buffer, rStr.pData->length, literal,
00819                 internal::ConstCharArrayDetector< T, void >::size - 1 ) == 0;
00820     }
00821 
00822     template< typename T >
00823     friend typename internal::CharPtrDetector< T, bool >::Type operator!=( const OString& rStr1, const T& value ) SAL_THROW(())
00824     {
00825         return !(operator == ( rStr1, value ));
00826     }
00827 
00828     template< typename T >
00829     friend typename internal::NonConstCharArrayDetector< T, bool >::Type operator!=( const OString& rStr1, T& value ) SAL_THROW(())
00830     {
00831         return !(operator == ( rStr1, value ));
00832     }
00833 
00834     template< typename T >
00835     friend typename internal::CharPtrDetector< T, bool >::Type operator!=( const T& value,   const OString& rStr2 ) SAL_THROW(())
00836     {
00837         return !(operator == ( value, rStr2 ));
00838     }
00839 
00840     template< typename T >
00841     friend typename internal::NonConstCharArrayDetector< T, bool >::Type operator!=( T& value,   const OString& rStr2 ) SAL_THROW(())
00842     {
00843         return !(operator == ( value, rStr2 ));
00844     }
00845 
00851     template< typename T >
00852     friend typename internal::ConstCharArrayDetector< T, bool >::Type operator!=( const OString& rStr, T& literal ) SAL_THROW(())
00853     {
00854         return !( rStr == literal );
00855     }
00856 
00862     template< typename T >
00863     friend typename internal::ConstCharArrayDetector< T, bool >::Type operator!=( T& literal, const OString& rStr ) SAL_THROW(())
00864     {
00865         return !( literal == rStr );
00866     }
00867 
00875     sal_Int32 hashCode() const SAL_THROW(())
00876     {
00877         return rtl_str_hashCode_WithLength( pData->buffer, pData->length );
00878     }
00879 
00893     sal_Int32 indexOf( sal_Char ch, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
00894     {
00895         sal_Int32 ret = rtl_str_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch );
00896         return (ret < 0 ? ret : ret+fromIndex);
00897     }
00898 
00908     sal_Int32 lastIndexOf( sal_Char ch ) const SAL_THROW(())
00909     {
00910         return rtl_str_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch );
00911     }
00912 
00925     sal_Int32 lastIndexOf( sal_Char ch, sal_Int32 fromIndex ) const SAL_THROW(())
00926     {
00927         return rtl_str_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch );
00928     }
00929 
00945     sal_Int32 indexOf( const OString & str, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
00946     {
00947         sal_Int32 ret = rtl_str_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
00948                                                        str.pData->buffer, str.pData->length );
00949         return (ret < 0 ? ret : ret+fromIndex);
00950     }
00951 
00957     template< typename T >
00958     typename internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const SAL_THROW(())
00959     {
00960         RTL_STRING_CONST_FUNCTION
00961         assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
00962         sal_Int32 n = rtl_str_indexOfStr_WithLength(
00963             pData->buffer + fromIndex, pData->length - fromIndex, literal, internal::ConstCharArrayDetector< T, void >::size - 1);
00964         return n < 0 ? n : n + fromIndex;
00965     }
00966 
00985     sal_Int32 indexOfL(char const * str, sal_Int32 len, sal_Int32 fromIndex = 0)
00986         const SAL_THROW(())
00987     {
00988         sal_Int32 n = rtl_str_indexOfStr_WithLength(
00989             pData->buffer + fromIndex, pData->length - fromIndex, str, len);
00990         return n < 0 ? n : n + fromIndex;
00991     }
00992 
00993     // This overload is left undefined, to detect calls of indexOfL that
00994     // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
00995     // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
00996     // platforms):
00997 #if SAL_TYPES_SIZEOFLONG == 8
00998     void indexOfL(char const *, sal_Int32, rtl_TextEncoding) const;
00999 #endif
01000 
01016     sal_Int32 lastIndexOf( const OString & str ) const SAL_THROW(())
01017     {
01018         return rtl_str_lastIndexOfStr_WithLength( pData->buffer, pData->length,
01019                                                   str.pData->buffer, str.pData->length );
01020     }
01021 
01039     sal_Int32 lastIndexOf( const OString & str, sal_Int32 fromIndex ) const SAL_THROW(())
01040     {
01041         return rtl_str_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
01042                                                   str.pData->buffer, str.pData->length );
01043     }
01044 
01055     OString copy( sal_Int32 beginIndex ) const SAL_THROW(())
01056     {
01057         rtl_String *pNew = 0;
01058         rtl_string_newFromSubString( &pNew, pData, beginIndex, getLength() - beginIndex );
01059         return OString( pNew, (DO_NOT_ACQUIRE*)0 );
01060     }
01061 
01074     OString copy( sal_Int32 beginIndex, sal_Int32 count ) const SAL_THROW(())
01075     {
01076         rtl_String *pNew = 0;
01077         rtl_string_newFromSubString( &pNew, pData, beginIndex, count );
01078         return OString( pNew, (DO_NOT_ACQUIRE*)0 );
01079     }
01080 
01089     SAL_WARN_UNUSED_RESULT OString concat( const OString & str ) const SAL_THROW(())
01090     {
01091         rtl_String* pNew = 0;
01092         rtl_string_newConcat( &pNew, pData, str.pData );
01093         return OString( pNew, (DO_NOT_ACQUIRE*)0 );
01094     }
01095 
01096 #ifndef RTL_FAST_STRING
01097     friend OString operator+( const OString & str1, const OString & str2  ) SAL_THROW(())
01098     {
01099         return str1.concat( str2 );
01100     }
01101 #endif
01102 
01116     SAL_WARN_UNUSED_RESULT OString replaceAt( sal_Int32 index, sal_Int32 count, const OString& newStr ) const SAL_THROW(())
01117     {
01118         rtl_String* pNew = 0;
01119         rtl_string_newReplaceStrAt( &pNew, pData, index, count, newStr.pData );
01120         return OString( pNew, (DO_NOT_ACQUIRE*)0 );
01121     }
01122 
01136     SAL_WARN_UNUSED_RESULT OString replace( sal_Char oldChar, sal_Char newChar ) const SAL_THROW(())
01137     {
01138         rtl_String* pNew = 0;
01139         rtl_string_newReplace( &pNew, pData, oldChar, newChar );
01140         return OString( pNew, (DO_NOT_ACQUIRE*)0 );
01141     }
01142 
01161     SAL_WARN_UNUSED_RESULT OString replaceFirst(
01162         OString const & from, OString const & to, sal_Int32 * index = 0) const
01163     {
01164         rtl_String * s = 0;
01165         sal_Int32 i = 0;
01166         rtl_string_newReplaceFirst(
01167             &s, pData, from.pData->buffer, from.pData->length,
01168             to.pData->buffer, to.pData->length, index == 0 ? &i : index);
01169         return OString(s, SAL_NO_ACQUIRE);
01170     }
01171 
01185     SAL_WARN_UNUSED_RESULT OString replaceAll(OString const & from, OString const & to) const {
01186         rtl_String * s = 0;
01187         rtl_string_newReplaceAll(
01188             &s, pData, from.pData->buffer, from.pData->length,
01189             to.pData->buffer, to.pData->length);
01190         return OString(s, SAL_NO_ACQUIRE);
01191     }
01192 
01203     SAL_WARN_UNUSED_RESULT OString toAsciiLowerCase() const SAL_THROW(())
01204     {
01205         rtl_String* pNew = 0;
01206         rtl_string_newToAsciiLowerCase( &pNew, pData );
01207         return OString( pNew, (DO_NOT_ACQUIRE*)0 );
01208     }
01209 
01220     SAL_WARN_UNUSED_RESULT OString toAsciiUpperCase() const SAL_THROW(())
01221     {
01222         rtl_String* pNew = 0;
01223         rtl_string_newToAsciiUpperCase( &pNew, pData );
01224         return OString( pNew, (DO_NOT_ACQUIRE*)0 );
01225     }
01226 
01238     SAL_WARN_UNUSED_RESULT OString trim() const SAL_THROW(())
01239     {
01240         rtl_String* pNew = 0;
01241         rtl_string_newTrim( &pNew, pData );
01242         return OString( pNew, (DO_NOT_ACQUIRE*)0 );
01243     }
01244 
01269     OString getToken( sal_Int32 token, sal_Char cTok, sal_Int32& index ) const SAL_THROW(())
01270     {
01271         rtl_String * pNew = 0;
01272         index = rtl_string_getToken( &pNew, pData, token, cTok, index );
01273         return OString( pNew, (DO_NOT_ACQUIRE *)0 );
01274     }
01275 
01289     OString getToken(sal_Int32 count, char separator) const {
01290         sal_Int32 n = 0;
01291         return getToken(count, separator, n);
01292     }
01293 
01302     sal_Bool toBoolean() const SAL_THROW(())
01303     {
01304         return rtl_str_toBoolean( pData->buffer );
01305     }
01306 
01313     sal_Char toChar() const SAL_THROW(())
01314     {
01315         return pData->buffer[0];
01316     }
01317 
01327     sal_Int32 toInt32( sal_Int16 radix = 10 ) const SAL_THROW(())
01328     {
01329         return rtl_str_toInt32( pData->buffer, radix );
01330     }
01331 
01341     sal_Int64 toInt64( sal_Int16 radix = 10 ) const SAL_THROW(())
01342     {
01343         return rtl_str_toInt64( pData->buffer, radix );
01344     }
01345 
01354     float toFloat() const SAL_THROW(())
01355     {
01356         return rtl_str_toFloat( pData->buffer );
01357     }
01358 
01367     double toDouble() const SAL_THROW(())
01368     {
01369         return rtl_str_toDouble( pData->buffer );
01370     }
01371 
01382     static OString valueOf( sal_Bool b ) SAL_THROW(())
01383     {
01384         sal_Char aBuf[RTL_STR_MAX_VALUEOFBOOLEAN];
01385         rtl_String* pNewData = 0;
01386         rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfBoolean( aBuf, b ) );
01387         return OString( pNewData, (DO_NOT_ACQUIRE*)0 );
01388     }
01389 
01396     static OString valueOf( sal_Char c ) SAL_THROW(())
01397     {
01398         return OString( &c, 1 );
01399     }
01400 
01410     static OString valueOf( sal_Int32 i, sal_Int16 radix = 10 ) SAL_THROW(())
01411     {
01412         sal_Char aBuf[RTL_STR_MAX_VALUEOFINT32];
01413         rtl_String* pNewData = 0;
01414         rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfInt32( aBuf, i, radix ) );
01415         return OString( pNewData, (DO_NOT_ACQUIRE*)0 );
01416     }
01417 
01427     static OString valueOf( sal_Int64 ll, sal_Int16 radix = 10 ) SAL_THROW(())
01428     {
01429         sal_Char aBuf[RTL_STR_MAX_VALUEOFINT64];
01430         rtl_String* pNewData = 0;
01431         rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfInt64( aBuf, ll, radix ) );
01432         return OString( pNewData, (DO_NOT_ACQUIRE*)0 );
01433     }
01434 
01443     static OString valueOf( float f ) SAL_THROW(())
01444     {
01445         sal_Char aBuf[RTL_STR_MAX_VALUEOFFLOAT];
01446         rtl_String* pNewData = 0;
01447         rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfFloat( aBuf, f ) );
01448         return OString( pNewData, (DO_NOT_ACQUIRE*)0 );
01449     }
01450 
01459     static OString valueOf( double d ) SAL_THROW(())
01460     {
01461         sal_Char aBuf[RTL_STR_MAX_VALUEOFDOUBLE];
01462         rtl_String* pNewData = 0;
01463         rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfDouble( aBuf, d ) );
01464         return OString( pNewData, (DO_NOT_ACQUIRE*)0 );
01465     }
01466 };
01467 
01468 /* ======================================================================= */
01469 
01470 #ifdef RTL_FAST_STRING
01471 
01479 struct SAL_WARN_UNUSED OStringLiteral
01480 {
01481     template< int N >
01482     OStringLiteral( const char (&str)[ N ] ) : size( N - 1 ), data( str ) { assert( strlen( str ) == N - 1 ); }
01483     int size;
01484     const char* data;
01485 };
01486 
01487 template<>
01488 struct ToStringHelper< OString >
01489     {
01490     static int length( const OString& s ) { return s.getLength(); }
01491     static char* addData( char* buffer, const OString& s ) { return addDataHelper( buffer, s.getStr(), s.getLength()); }
01492     static const bool allowOStringConcat = true;
01493     static const bool allowOUStringConcat = false;
01494     };
01495 
01496 template<>
01497 struct ToStringHelper< OStringLiteral >
01498     {
01499     static int length( const OStringLiteral& str ) { return str.size; }
01500     static char* addData( char* buffer, const OStringLiteral& str ) { return addDataHelper( buffer, str.data, str.size ); }
01501     static const bool allowOStringConcat = true;
01502     static const bool allowOUStringConcat = false;
01503     };
01504 
01505 template< typename charT, typename traits, typename T1, typename T2 >
01506 inline std::basic_ostream<charT, traits> & operator <<(
01507     std::basic_ostream<charT, traits> & stream, const OStringConcat< T1, T2 >& concat)
01508 {
01509     return stream << OString( concat );
01510 }
01511 #else
01512 // non-RTL_FAST_CODE needs this to compile
01513 typedef OString OStringLiteral;
01514 #endif
01515 
01516 } /* Namespace */
01517 
01518 #ifdef RTL_STRING_UNITTEST
01519 namespace rtl
01520 {
01521 typedef rtlunittest::OString OString;
01522 }
01523 #undef RTL_STRING_CONST_FUNCTION
01524 #endif
01525 
01526 namespace rtl
01527 {
01528 
01534 struct OStringHash
01535 {
01545     size_t operator()( const OString& rString ) const
01546         { return (size_t)rString.hashCode(); }
01547 };
01548 
01549 /* ======================================================================= */
01550 
01557 template< typename charT, typename traits > std::basic_ostream<charT, traits> &
01558 operator <<(
01559     std::basic_ostream<charT, traits> & stream, rtl::OString const & string)
01560 {
01561     return stream << string.getStr();
01562         // best effort; potentially loses data due to embedded null characters
01563 }
01564 
01565 } /* Namespace */
01566 
01567 #ifdef RTL_USING
01568 using ::rtl::OString;
01569 using ::rtl::OStringHash;
01570 using ::rtl::OStringLiteral;
01571 #endif
01572 
01573 #endif /* _RTL_STRING_HXX_ */
01574 
01575 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines