UDK 3.2.7 C/C++ API Reference
rtl/ustrbuf.hxx
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /*************************************************************************
00003  *
00004  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
00005  *
00006  * Copyright 2000, 2010 Oracle and/or its affiliates.
00007  *
00008  * OpenOffice.org - a multi-platform office productivity suite
00009  *
00010  * This file is part of OpenOffice.org.
00011  *
00012  * OpenOffice.org is free software: you can redistribute it and/or modify
00013  * it under the terms of the GNU Lesser General Public License version 3
00014  * only, as published by the Free Software Foundation.
00015  *
00016  * OpenOffice.org is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Lesser General Public License version 3 for more details
00020  * (a copy is included in the LICENSE file that accompanied this code).
00021  *
00022  * You should have received a copy of the GNU Lesser General Public License
00023  * version 3 along with OpenOffice.org.  If not, see
00024  * <http://www.openoffice.org/license.html>
00025  * for a copy of the LGPLv3 License.
00026  *
00027  ************************************************************************/
00028 
00029 #ifndef _RTL_USTRBUF_HXX_
00030 #define _RTL_USTRBUF_HXX_
00031 
00032 #include "sal/config.h"
00033 
00034 #include <cassert>
00035 
00036 #include <osl/diagnose.h>
00037 #include <rtl/ustrbuf.h>
00038 #include <rtl/ustring.hxx>
00039 #include <rtl/stringutils.hxx>
00040 
00041 // The unittest uses slightly different code to help check that the proper
00042 // calls are made. The class is put into a different namespace to make
00043 // sure the compiler generates a different (if generating also non-inline)
00044 // copy of the function and does not merge them together. The class
00045 // is "brought" into the proper rtl namespace by a typedef below.
00046 #ifdef RTL_STRING_UNITTEST
00047 #define rtl rtlunittest
00048 #endif
00049 
00050 namespace rtl
00051 {
00052 
00053 #ifdef RTL_STRING_UNITTEST
00054 #undef rtl
00055 #endif
00056 
00095 class OUStringBuffer
00096 {
00097 public:
00102     OUStringBuffer()
00103         : pData(NULL)
00104         , nCapacity( 16 )
00105     {
00106         rtl_uString_new_WithLength( &pData, nCapacity );
00107     }
00108 
00115     OUStringBuffer( const OUStringBuffer & value )
00116         : pData(NULL)
00117         , nCapacity( value.nCapacity )
00118     {
00119         rtl_uStringbuffer_newFromStringBuffer( &pData, value.nCapacity, value.pData );
00120     }
00121 
00128     explicit OUStringBuffer(int length)
00129         : pData(NULL)
00130         , nCapacity( length )
00131     {
00132         rtl_uString_new_WithLength( &pData, length );
00133     }
00134 
00145     OUStringBuffer(OUString value)
00146         : pData(NULL)
00147         , nCapacity( value.getLength() + 16 )
00148     {
00149         rtl_uStringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
00150     }
00151 
00152 #ifdef HAVE_SFINAE_ANONYMOUS_BROKEN // see OUString ctors
00153     template< int N >
00154     OUStringBuffer( const char (&literal)[ N ] )
00155         : pData(NULL)
00156         , nCapacity( N - 1 + 16 )
00157     {
00158         rtl_uString_newFromLiteral( &pData, literal, N - 1, 16 );
00159 #ifdef RTL_STRING_UNITTEST
00160         rtl_string_unittest_const_literal = true;
00161 #endif
00162     }
00163 
00168     template< int N >
00169     OUStringBuffer( char (&value)[ N ] )
00170 #ifndef RTL_STRING_UNITTEST
00171         ; // intentionally not implemented
00172 #else
00173     {
00174         (void) value; // unused
00175         pData = 0;
00176         nCapacity = 10;
00177         rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
00178         rtl_string_unittest_invalid_conversion = true;
00179     }
00180 #endif
00181 #else // HAVE_SFINAE_ANONYMOUS_BROKEN
00182     template< typename T >
00183     OUStringBuffer( T& literal, typename internal::ConstCharArrayDetector< T, internal::Dummy >::Type = internal::Dummy() )
00184         : pData(NULL)
00185         , nCapacity( internal::ConstCharArrayDetector< T, void >::size - 1 + 16 )
00186     {
00187         rtl_uString_newFromLiteral( &pData, literal, internal::ConstCharArrayDetector< T, void >::size - 1, 16 );
00188 #ifdef RTL_STRING_UNITTEST
00189         rtl_string_unittest_const_literal = true;
00190 #endif
00191     }
00192 #endif // HAVE_SFINAE_ANONYMOUS_BROKEN
00193 
00194 #ifdef RTL_STRING_UNITTEST
00195 
00199     template< typename T >
00200     OUStringBuffer( T&, typename internal::ExceptConstCharArrayDetector< T >::Type = internal::Dummy() )
00201     {
00202         pData = 0;
00203         nCapacity = 10;
00204         rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
00205         rtl_string_unittest_invalid_conversion = true;
00206     }
00211     template< typename T >
00212     OUStringBuffer( const T&, typename internal::ExceptCharArrayDetector< T >::Type = internal::Dummy() )
00213     {
00214         pData = 0;
00215         nCapacity = 10;
00216         rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
00217         rtl_string_unittest_invalid_conversion = true;
00218     }
00219 #endif
00220 
00223     OUStringBuffer& operator = ( const OUStringBuffer& value )
00224     {
00225         if (this != &value)
00226         {
00227             rtl_uStringbuffer_newFromStringBuffer(&pData,
00228                                                   value.nCapacity,
00229                                                   value.pData);
00230             nCapacity = value.nCapacity;
00231         }
00232         return *this;
00233     }
00234 
00238     ~OUStringBuffer()
00239     {
00240         rtl_uString_release( pData );
00241     }
00242 
00251     OUString makeStringAndClear()
00252     {
00253         return OUString(
00254                   rtl_uStringBuffer_makeStringAndClear( &pData, &nCapacity ),
00255                   SAL_NO_ACQUIRE );
00256     }
00257 
00263     sal_Int32 getLength() const
00264     {
00265         return pData->length;
00266     }
00267 
00278     sal_Int32 getCapacity() const
00279     {
00280         return nCapacity;
00281     }
00282 
00294     void ensureCapacity(sal_Int32 minimumCapacity)
00295     {
00296         rtl_uStringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
00297     }
00298 
00317     void setLength(sal_Int32 newLength)
00318     {
00319         assert(newLength >= 0);
00320         // Avoid modifications if pData points to const empty string:
00321         if( newLength != pData->length )
00322         {
00323             if( newLength > nCapacity )
00324                 rtl_uStringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
00325             else
00326                 pData->buffer[newLength] = 0;
00327             pData->length = newLength;
00328         }
00329     }
00330 
00344     SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
00345     sal_Unicode charAt( sal_Int32 index ) const
00346     {
00347         assert(index >= 0 && index < pData->length);
00348         return pData->buffer[ index ];
00349     }
00350 
00361     SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
00362     OUStringBuffer & setCharAt(sal_Int32 index, sal_Unicode ch)
00363     {
00364         assert(index >= 0 && index < pData->length);
00365         pData->buffer[ index ] = ch;
00366         return *this;
00367     }
00368 
00372     const sal_Unicode*  getStr() const { return pData->buffer; }
00373 
00383     sal_Unicode & operator [](sal_Int32 index) { return pData->buffer[index]; }
00384 
00389     const OUString toString() const
00390     {
00391         return OUString(pData->buffer, pData->length);
00392     }
00393 
00404     OUStringBuffer & append(const OUString &str)
00405     {
00406         return append( str.getStr(), str.getLength() );
00407     }
00408 
00420     OUStringBuffer & append( const sal_Unicode * str )
00421     {
00422         return append( str, rtl_ustr_getLength( str ) );
00423     }
00424 
00438     OUStringBuffer & append( const sal_Unicode * str, sal_Int32 len)
00439     {
00440         // insert behind the last character
00441         rtl_uStringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
00442         return *this;
00443     }
00444 
00450     template< typename T >
00451     typename internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type append( T& literal )
00452     {
00453         rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), literal,
00454             internal::ConstCharArrayDetector< T, void >::size - 1 );
00455         return *this;
00456     }
00457 
00474     OUStringBuffer & appendAscii( const sal_Char * str )
00475     {
00476         return appendAscii( str, rtl_str_getLength( str ) );
00477     }
00478 
00497     OUStringBuffer & appendAscii( const sal_Char * str, sal_Int32 len)
00498     {
00499         rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), str, len );
00500         return *this;
00501     }
00502 
00514     OUStringBuffer & append(sal_Bool b)
00515     {
00516         sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
00517         return append( sz, rtl_ustr_valueOfBoolean( sz, b ) );
00518     }
00519 
00532     OUStringBuffer & append(char c)
00533     {
00534         assert(static_cast< unsigned char >(c) <= 0x7F);
00535         return append(sal_Unicode(c));
00536     }
00537 
00548     OUStringBuffer & append(sal_Unicode c)
00549     {
00550         return append( &c, 1 );
00551     }
00552 
00564     OUStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
00565     {
00566         sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT32];
00567         return append( sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
00568     }
00569 
00581     OUStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
00582     {
00583         sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT64];
00584         return append( sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
00585     }
00586 
00598     OUStringBuffer & append(float f)
00599     {
00600         sal_Unicode sz[RTL_USTR_MAX_VALUEOFFLOAT];
00601         return append( sz, rtl_ustr_valueOfFloat( sz, f ) );
00602     }
00603 
00615     OUStringBuffer & append(double d)
00616     {
00617         sal_Unicode sz[RTL_USTR_MAX_VALUEOFDOUBLE];
00618         return append( sz, rtl_ustr_valueOfDouble( sz, d ) );
00619     }
00620 
00634     OUStringBuffer & appendUtf32(sal_uInt32 c) {
00635         return insertUtf32(getLength(), c);
00636     }
00637 
00653     OUStringBuffer & insert(sal_Int32 offset, const OUString & str)
00654     {
00655         return insert( offset, str.getStr(), str.getLength() );
00656     }
00657 
00675     OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str )
00676     {
00677         return insert( offset, str, rtl_ustr_getLength( str ) );
00678     }
00679 
00698     OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str, sal_Int32 len)
00699     {
00700         // insert behind the last character
00701         rtl_uStringbuffer_insert( &pData, &nCapacity, offset, str, len );
00702         return *this;
00703     }
00704 
00710     template< typename T >
00711     typename internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type insert( sal_Int32 offset, T& literal )
00712     {
00713         rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, offset, literal,
00714             internal::ConstCharArrayDetector< T, void >::size - 1 );
00715         return *this;
00716     }
00717 
00735     OUStringBuffer & insert(sal_Int32 offset, sal_Bool b)
00736     {
00737         sal_Unicode sz[RTL_USTR_MAX_VALUEOFBOOLEAN];
00738         return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
00739     }
00740 
00759     OUStringBuffer & insert(sal_Int32 offset, char c)
00760     {
00761         sal_Unicode u = c;
00762         return insert( offset, &u, 1 );
00763     }
00764 
00781     OUStringBuffer & insert(sal_Int32 offset, sal_Unicode c)
00782     {
00783         return insert( offset, &c, 1 );
00784     }
00785 
00805     OUStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
00806     {
00807         sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT32];
00808         return insert( offset, sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
00809     }
00810 
00830     OUStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
00831     {
00832         sal_Unicode sz[RTL_USTR_MAX_VALUEOFINT64];
00833         return insert( offset, sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
00834     }
00835 
00854     OUStringBuffer insert(sal_Int32 offset, float f)
00855     {
00856         sal_Unicode sz[RTL_USTR_MAX_VALUEOFFLOAT];
00857         return insert( offset, sz, rtl_ustr_valueOfFloat( sz, f ) );
00858     }
00859 
00878     OUStringBuffer & insert(sal_Int32 offset, double d)
00879     {
00880         sal_Unicode sz[RTL_USTR_MAX_VALUEOFDOUBLE];
00881         return insert( offset, sz, rtl_ustr_valueOfDouble( sz, d ) );
00882     }
00883 
00899     OUStringBuffer & insertUtf32(sal_Int32 offset, sal_uInt32 c) {
00900         rtl_uStringbuffer_insertUtf32(&pData, &nCapacity, offset, c);
00901         return *this;
00902     }
00903 
00916     OUStringBuffer & remove( sal_Int32 start, sal_Int32 len )
00917     {
00918         rtl_uStringbuffer_remove( &pData, start, len );
00919         return *this;
00920     }
00921 
00937     inline void accessInternals(rtl_uString *** pInternalData,
00938                                 sal_Int32 ** pInternalCapacity)
00939     {
00940         *pInternalData = &pData;
00941         *pInternalCapacity = &nCapacity;
00942     }
00943 
00944 private:
00948     rtl_uString * pData;
00949 
00953     sal_Int32       nCapacity;
00954 };
00955 
00956 }
00957 
00958 #ifdef RTL_STRING_UNITTEST
00959 namespace rtl
00960 {
00961 typedef rtlunittest::OUStringBuffer OUStringBuffer;
00962 }
00963 #endif
00964 
00965 #endif  /* _RTL_USTRBUF_HXX_ */
00966 
00967 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines