My Project
UDK 3.2.7 C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ustrbuf.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
12  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org. If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  ************************************************************************/
28 
29 #ifndef _RTL_USTRBUF_HXX_
30 #define _RTL_USTRBUF_HXX_
31 
32 #include "sal/config.h"
33 
34 #include <cassert>
35 
36 #include <osl/diagnose.h>
37 #include <rtl/ustrbuf.h>
38 #include <rtl/ustring.hxx>
39 #include <rtl/stringutils.hxx>
40 
41 // The unittest uses slightly different code to help check that the proper
42 // calls are made. The class is put into a different namespace to make
43 // sure the compiler generates a different (if generating also non-inline)
44 // copy of the function and does not merge them together. The class
45 // is "brought" into the proper rtl namespace by a typedef below.
46 #ifdef RTL_STRING_UNITTEST
47 #define rtl rtlunittest
48 #endif
49 
50 namespace rtl
51 {
52 
53 #ifdef RTL_STRING_UNITTEST
54 #undef rtl
55 #endif
56 
96 {
97 public:
103  : pData(NULL)
104  , nCapacity( 16 )
105  {
106  rtl_uString_new_WithLength( &pData, nCapacity );
107  }
108 
116  : pData(NULL)
117  , nCapacity( value.nCapacity )
118  {
119  rtl_uStringbuffer_newFromStringBuffer( &pData, value.nCapacity, value.pData );
120  }
121 
128  explicit OUStringBuffer(int length)
129  : pData(NULL)
130  , nCapacity( length )
131  {
132  rtl_uString_new_WithLength( &pData, length );
133  }
134 
146  : pData(NULL)
147  , nCapacity( value.getLength() + 16 )
148  {
149  rtl_uStringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
150  }
151 
152 #ifdef HAVE_SFINAE_ANONYMOUS_BROKEN // see OUString ctors
153  template< int N >
154  OUStringBuffer( const char (&literal)[ N ] )
155  : pData(NULL)
156  , nCapacity( N - 1 + 16 )
157  {
158  rtl_uString_newFromLiteral( &pData, literal, N - 1, 16 );
159 #ifdef RTL_STRING_UNITTEST
160  rtl_string_unittest_const_literal = true;
161 #endif
162  }
163 
168  template< int N >
169  OUStringBuffer( char (&value)[ N ] )
170 #ifndef RTL_STRING_UNITTEST
171  ; // intentionally not implemented
172 #else
173  {
174  (void) value; // unused
175  pData = 0;
176  nCapacity = 10;
177  rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
178  rtl_string_unittest_invalid_conversion = true;
179  }
180 #endif
181 #else // HAVE_SFINAE_ANONYMOUS_BROKEN
182  template< typename T >
184  : pData(NULL)
185  , nCapacity( internal::ConstCharArrayDetector< T, void >::size - 1 + 16 )
186  {
188 #ifdef RTL_STRING_UNITTEST
189  rtl_string_unittest_const_literal = true;
190 #endif
191  }
192 #endif // HAVE_SFINAE_ANONYMOUS_BROKEN
193 
194 #ifdef RTL_STRING_UNITTEST
195 
199  template< typename T >
201  {
202  pData = 0;
203  nCapacity = 10;
204  rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
205  rtl_string_unittest_invalid_conversion = true;
206  }
211  template< typename T >
212  OUStringBuffer( const T&, typename internal::ExceptCharArrayDetector< T >::Type = internal::Dummy() )
213  {
214  pData = 0;
215  nCapacity = 10;
216  rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
217  rtl_string_unittest_invalid_conversion = true;
218  }
219 #endif
220 
224  {
225  if (this != &value)
226  {
228  value.nCapacity,
229  value.pData);
230  nCapacity = value.nCapacity;
231  }
232  return *this;
233  }
234 
239  {
240  rtl_uString_release( pData );
241  }
242 
252  {
253  return OUString(
254  rtl_uStringBuffer_makeStringAndClear( &pData, &nCapacity ),
255  SAL_NO_ACQUIRE );
256  }
257 
263  sal_Int32 getLength() const
264  {
265  return pData->length;
266  }
267 
278  sal_Int32 getCapacity() const
279  {
280  return nCapacity;
281  }
282 
294  void ensureCapacity(sal_Int32 minimumCapacity)
295  {
296  rtl_uStringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
297  }
298 
317  void setLength(sal_Int32 newLength)
318  {
319  assert(newLength >= 0);
320  // Avoid modifications if pData points to const empty string:
321  if( newLength != pData->length )
322  {
323  if( newLength > nCapacity )
324  rtl_uStringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
325  else
326  pData->buffer[newLength] = 0;
327  pData->length = newLength;
328  }
329  }
330 
344  SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
345  sal_Unicode charAt( sal_Int32 index ) const
346  {
347  assert(index >= 0 && index < pData->length);
348  return pData->buffer[ index ];
349  }
350 
361  SAL_DEPRECATED("use rtl::OUStringBuffer::operator [] instead")
362  OUStringBuffer & setCharAt(sal_Int32 index, sal_Unicode ch)
363  {
364  assert(index >= 0 && index < pData->length);
365  pData->buffer[ index ] = ch;
366  return *this;
367  }
368 
372  const sal_Unicode* getStr() const { return pData->buffer; }
373 
383  sal_Unicode & operator [](sal_Int32 index) { return pData->buffer[index]; }
384 
389  const OUString toString() const
390  {
391  return OUString(pData->buffer, pData->length);
392  }
393 
405  {
406  return append( str.getStr(), str.getLength() );
407  }
408 
421  {
422  return append( str, rtl_ustr_getLength( str ) );
423  }
424 
438  OUStringBuffer & append( const sal_Unicode * str, sal_Int32 len)
439  {
440  // insert behind the last character
441  rtl_uStringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
442  return *this;
443  }
444 
450  template< typename T >
452  {
453  rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), literal,
455  return *this;
456  }
457 
475  {
476  return appendAscii( str, rtl_str_getLength( str ) );
477  }
478 
497  OUStringBuffer & appendAscii( const sal_Char * str, sal_Int32 len)
498  {
499  rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), str, len );
500  return *this;
501  }
502 
515  {
517  return append( sz, rtl_ustr_valueOfBoolean( sz, b ) );
518  }
519 
533  {
534  assert(static_cast< unsigned char >(c) <= 0x7F);
535  return append(sal_Unicode(c));
536  }
537 
549  {
550  return append( &c, 1 );
551  }
552 
564  OUStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
565  {
567  return append( sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
568  }
569 
581  OUStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
582  {
584  return append( sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
585  }
586 
599  {
601  return append( sz, rtl_ustr_valueOfFloat( sz, f ) );
602  }
603 
615  OUStringBuffer & append(double d)
616  {
618  return append( sz, rtl_ustr_valueOfDouble( sz, d ) );
619  }
620 
634  OUStringBuffer & appendUtf32(sal_uInt32 c) {
635  return insertUtf32(getLength(), c);
636  }
637 
653  OUStringBuffer & insert(sal_Int32 offset, const OUString & str)
654  {
655  return insert( offset, str.getStr(), str.getLength() );
656  }
657 
675  OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str )
676  {
677  return insert( offset, str, rtl_ustr_getLength( str ) );
678  }
679 
698  OUStringBuffer & insert( sal_Int32 offset, const sal_Unicode * str, sal_Int32 len)
699  {
700  // insert behind the last character
701  rtl_uStringbuffer_insert( &pData, &nCapacity, offset, str, len );
702  return *this;
703  }
704 
710  template< typename T >
712  {
713  rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, offset, literal,
715  return *this;
716  }
717 
735  OUStringBuffer & insert(sal_Int32 offset, sal_Bool b)
736  {
738  return insert( offset, sz, rtl_ustr_valueOfBoolean( sz, b ) );
739  }
740 
759  OUStringBuffer & insert(sal_Int32 offset, char c)
760  {
761  sal_Unicode u = c;
762  return insert( offset, &u, 1 );
763  }
764 
781  OUStringBuffer & insert(sal_Int32 offset, sal_Unicode c)
782  {
783  return insert( offset, &c, 1 );
784  }
785 
805  OUStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
806  {
808  return insert( offset, sz, rtl_ustr_valueOfInt32( sz, i, radix ) );
809  }
810 
830  OUStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
831  {
833  return insert( offset, sz, rtl_ustr_valueOfInt64( sz, l, radix ) );
834  }
835 
854  OUStringBuffer insert(sal_Int32 offset, float f)
855  {
857  return insert( offset, sz, rtl_ustr_valueOfFloat( sz, f ) );
858  }
859 
878  OUStringBuffer & insert(sal_Int32 offset, double d)
879  {
881  return insert( offset, sz, rtl_ustr_valueOfDouble( sz, d ) );
882  }
883 
899  OUStringBuffer & insertUtf32(sal_Int32 offset, sal_uInt32 c) {
900  rtl_uStringbuffer_insertUtf32(&pData, &nCapacity, offset, c);
901  return *this;
902  }
903 
916  OUStringBuffer & remove( sal_Int32 start, sal_Int32 len )
917  {
918  rtl_uStringbuffer_remove( &pData, start, len );
919  return *this;
920  }
921 
937  inline void accessInternals(rtl_uString *** pInternalData,
938  sal_Int32 ** pInternalCapacity)
939  {
940  *pInternalData = &pData;
941  *pInternalCapacity = &nCapacity;
942  }
943 
944 private:
948  rtl_uString * pData;
949 
953  sal_Int32 nCapacity;
954 };
955 
956 }
957 
958 #ifdef RTL_STRING_UNITTEST
959 namespace rtl
960 {
961 typedef rtlunittest::OUStringBuffer OUStringBuffer;
962 }
963 #endif
964 
965 #endif /* _RTL_USTRBUF_HXX_ */
966 
967 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */