My Project
UDK 3.2.7 C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
strbuf.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef _RTL_STRBUF_HXX_
21 #define _RTL_STRBUF_HXX_
22 
23 #include "sal/config.h"
24 
25 #include <cassert>
26 #include <string.h>
27 
28 #include <rtl/strbuf.h>
29 #include <rtl/string.hxx>
30 #include <rtl/stringutils.hxx>
31 
32 #ifdef RTL_FAST_STRING
33 #include <rtl/stringconcat.hxx>
34 #endif
35 
36 #ifdef __cplusplus
37 
38 // The unittest uses slightly different code to help check that the proper
39 // calls are made. The class is put into a different namespace to make
40 // sure the compiler generates a different (if generating also non-inline)
41 // copy of the function and does not merge them together. The class
42 // is "brought" into the proper rtl namespace by a typedef below.
43 #ifdef RTL_STRING_UNITTEST
44 #define rtl rtlunittest
45 #endif
46 
47 namespace rtl
48 {
49 
50 #ifdef RTL_STRING_UNITTEST
51 #undef rtl
52 // helper macro to make functions appear more readable
53 #define RTL_STRING_CONST_FUNCTION rtl_string_unittest_const_literal_function = true;
54 #else
55 #define RTL_STRING_CONST_FUNCTION
56 #endif
57 
97 {
98 public:
104  : pData(NULL)
105  , nCapacity( 16 )
106  {
107  rtl_string_new_WithLength( &pData, nCapacity );
108  }
109 
116  OStringBuffer( const OStringBuffer & value )
117  : pData(NULL)
118  , nCapacity( value.nCapacity )
119  {
120  rtl_stringbuffer_newFromStringBuffer( &pData, value.nCapacity, value.pData );
121  }
122 
129  explicit OStringBuffer(int length)
130  : pData(NULL)
131  , nCapacity( length )
132  {
133  rtl_string_new_WithLength( &pData, length );
134  }
135 
147  : pData(NULL)
148  , nCapacity( value.getLength() + 16 )
149  {
150  rtl_stringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
151  }
152 
157 #ifdef HAVE_SFINAE_ANONYMOUS_BROKEN // see the OString ctors
158  OStringBuffer( const char* value )
159  : pData(NULL)
160  {
161  sal_Int32 length = rtl_str_getLength( value );
162  nCapacity = length + 16;
163  rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
164  }
165 #else
166  template< typename T >
168  : pData(NULL)
169  {
170  sal_Int32 length = rtl_str_getLength( value );
171  nCapacity = length + 16;
172  rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
173  }
174 
175  template< typename T >
177  : pData(NULL)
178  {
179  sal_Int32 length = rtl_str_getLength( value );
180  nCapacity = length + 16;
181  rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
182  }
183 
195  template< typename T >
197  : pData(NULL)
198  , nCapacity( internal::ConstCharArrayDetector< T, void >::size - 1 + 16 )
199  {
200  assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
202 #ifdef RTL_STRING_UNITTEST
203  rtl_string_unittest_const_literal = true;
204 #endif
205  }
206 #endif // HAVE_SFINAE_ANONYMOUS_BROKEN
207 
220  OStringBuffer(const sal_Char * value, sal_Int32 length)
221  : pData(NULL)
222  , nCapacity( length + 16 )
223  {
224  rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
225  }
226 
227 #ifdef RTL_FAST_STRING
228  template< typename T1, typename T2 >
229  OStringBuffer( const OStringConcat< T1, T2 >& c )
230  {
231  const sal_Int32 l = c.length();
232  rtl_String* buffer = NULL;
233  nCapacity = l + 16;
234  rtl_string_new_WithLength( &buffer, nCapacity );
235  char* end = c.addData( buffer->buffer );
236  *end = '\0';
237  buffer->length = end - buffer->buffer;
238  pData = buffer;
239  }
240 #endif
241 
244  OStringBuffer& operator = ( const OStringBuffer& value )
245  {
246  if (this != &value)
247  {
249  value.nCapacity,
250  value.pData);
251  nCapacity = value.nCapacity;
252  }
253  return *this;
254  }
255 
260  {
261  rtl_string_release( pData );
262  }
263 
272  OString makeStringAndClear()
273  {
274  OString aRet( pData );
275  rtl_string_new(&pData);
276  nCapacity = 0;
277  return aRet;
278  }
279 
285  sal_Int32 getLength() const
286  {
287  return pData->length;
288  }
289 
300  sal_Int32 getCapacity() const
301  {
302  return nCapacity;
303  }
304 
316  void ensureCapacity(sal_Int32 minimumCapacity)
317  {
318  rtl_stringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
319  }
320 
339  void setLength(sal_Int32 newLength)
340  {
341  assert(newLength >= 0);
342  // Avoid modifications if pData points to const empty string:
343  if( newLength != pData->length )
344  {
345  if( newLength > nCapacity )
346  rtl_stringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
347  else
348  pData->buffer[newLength] = '\0';
349  pData->length = newLength;
350  }
351  }
352 
366  SAL_DEPRECATED("use rtl::OStringBuffer::operator [] instead")
367  sal_Char charAt( sal_Int32 index )
368  {
369  assert(index >= 0 && index < pData->length);
370  return pData->buffer[ index ];
371  }
372 
383  SAL_DEPRECATED("use rtl::OStringBuffer::operator [] instead")
384  OStringBuffer & setCharAt(sal_Int32 index, sal_Char ch)
385  {
386  assert(index >= 0 && index < pData->length);
387  pData->buffer[ index ] = ch;
388  return *this;
389  }
390 
394  const sal_Char* getStr() const { return pData->buffer; }
395 
405  sal_Char & operator [](sal_Int32 index) { return pData->buffer[index]; }
406 
411  const OString toString() const
412  {
413  return OString(pData->buffer, pData->length);
414  }
415 
426  OStringBuffer & append(const OString &str)
427  {
428  return append( str.getStr(), str.getLength() );
429  }
430 
442 #ifdef HAVE_SFINAE_ANONYMOUS_BROKEN
443  OStringBuffer & append( const sal_Char * str )
444  {
445  return append( str, rtl_str_getLength( str ) );
446  }
447 #else
448  template< typename T >
450  {
451  return append( str, rtl_str_getLength( str ) );
452  }
453 
454  template< typename T >
456  {
457  return append( str, rtl_str_getLength( str ) );
458  }
459 
465  template< typename T >
467  {
469  assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
470  rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), literal, internal::ConstCharArrayDetector< T, void >::size - 1 );
471  return *this;
472  }
473 #endif
474 
488  OStringBuffer & append( const sal_Char * str, sal_Int32 len)
489  {
490  // insert behind the last character
491  rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
492  return *this;
493  }
494 
507  {
509  return append( sz, rtl_str_valueOfBoolean( sz, b ) );
510  }
511 
523  {
524  return append( &c, 1 );
525  }
526 
538  OStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
539  {
541  return append( sz, rtl_str_valueOfInt32( sz, i, radix ) );
542  }
543 
555  OStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
556  {
558  return append( sz, rtl_str_valueOfInt64( sz, l, radix ) );
559  }
560 
572  OStringBuffer & append(float f)
573  {
575  return append( sz, rtl_str_valueOfFloat( sz, f ) );
576  }
577 
589  OStringBuffer & append(double d)
590  {
592  return append( sz, rtl_str_valueOfDouble( sz, d ) );
593  }
594 
610  OStringBuffer & insert(sal_Int32 offset, const OString & str)
611  {
612  return insert( offset, str.getStr(), str.getLength() );
613  }
614 
632 #ifdef HAVE_SFINAE_ANONYMOUS_BROKEN
633  OStringBuffer & insert( sal_Int32 offset, const sal_Char * str )
634  {
635  return insert( offset, str, rtl_str_getLength( str ) );
636  }
637 #else
638  template< typename T >
639  typename internal::CharPtrDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, const T& str )
640  {
641  return insert( offset, str, rtl_str_getLength( str ) );
642  }
643 
644  template< typename T >
645  typename internal::NonConstCharArrayDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, T& str )
646  {
647  return insert( offset, str, rtl_str_getLength( str ) );
648  }
649 
655  template< typename T >
656  typename internal::ConstCharArrayDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, T& literal )
657  {
659  assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
660  rtl_stringbuffer_insert( &pData, &nCapacity, offset, literal, internal::ConstCharArrayDetector< T, void >::size - 1 );
661  return *this;
662  }
663 #endif
664 
683  OStringBuffer & insert( sal_Int32 offset, const sal_Char * str, sal_Int32 len)
684  {
685  // insert behind the last character
686  rtl_stringbuffer_insert( &pData, &nCapacity, offset, str, len );
687  return *this;
688  }
689 
707  OStringBuffer & insert(sal_Int32 offset, sal_Bool b)
708  {
710  return insert( offset, sz, rtl_str_valueOfBoolean( sz, b ) );
711  }
712 
729  OStringBuffer & insert(sal_Int32 offset, sal_Char c)
730  {
731  return insert( offset, &c, 1 );
732  }
733 
751  OStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
752  {
754  return insert( offset, sz, rtl_str_valueOfInt32( sz, i, radix ) );
755  }
756 
774  OStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
775  {
777  return insert( offset, sz, rtl_str_valueOfInt64( sz, l, radix ) );
778  }
779 
797  OStringBuffer insert(sal_Int32 offset, float f)
798  {
800  return insert( offset, sz, rtl_str_valueOfFloat( sz, f ) );
801  }
802 
820  OStringBuffer & insert(sal_Int32 offset, double d)
821  {
823  return insert( offset, sz, rtl_str_valueOfDouble( sz, d ) );
824  }
825 
838  OStringBuffer & remove( sal_Int32 start, sal_Int32 len )
839  {
840  rtl_stringbuffer_remove( &pData, start, len );
841  return *this;
842  }
843 
844 private:
848  rtl_String * pData;
849 
853  sal_Int32 nCapacity;
854 };
855 
856 #ifdef RTL_FAST_STRING
857 template<>
858 struct ToStringHelper< OStringBuffer >
859  {
860  static int length( const OStringBuffer& s ) { return s.getLength(); }
861  static char* addData( char* buffer, const OStringBuffer& s ) { return addDataHelper( buffer, s.getStr(), s.getLength()); }
862  static const bool allowOStringConcat = true;
863  static const bool allowOUStringConcat = false;
864  };
865 #endif
866 
867 
868 }
869 
870 #ifdef RTL_STRING_UNITTEST
871 namespace rtl
872 {
873 typedef rtlunittest::OStringBuffer OStringBuffer;
874 }
875 #undef RTL_STRING_CONST_FUNCTION
876 #endif
877 
878 #ifdef RTL_USING
879 using ::rtl::OStringBuffer;
880 #endif
881 
882 #endif /* __cplusplus */
883 #endif /* _RTL_STRBUF_HXX_ */
884 
885 
886 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */