LibreOffice
LibreOffice 4.4 SDK 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 INCLUDED_RTL_STRBUF_HXX
21 #define INCLUDED_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 // The unittest uses slightly different code to help check that the proper
37 // calls are made. The class is put into a different namespace to make
38 // sure the compiler generates a different (if generating also non-inline)
39 // copy of the function and does not merge them together. The class
40 // is "brought" into the proper rtl namespace by a typedef below.
41 #ifdef RTL_STRING_UNITTEST
42 #define rtl rtlunittest
43 #endif
44 
45 namespace rtl
46 {
47 
49 #ifdef RTL_STRING_UNITTEST
50 #undef rtl
51 // helper macro to make functions appear more readable
52 #define RTL_STRING_CONST_FUNCTION rtl_string_unittest_const_literal_function = true;
53 #else
54 #define RTL_STRING_CONST_FUNCTION
55 #endif
56 
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 
146  OStringBuffer(const OString& value)
147  : pData(NULL)
148  , nCapacity( value.getLength() + 16 )
149  {
150  rtl_stringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
151  }
152 
157  template< typename T >
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 
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 
186  template< typename T >
188  : pData(NULL)
189  , nCapacity( libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 + 16 )
190  {
191  assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
193 #ifdef RTL_STRING_UNITTEST
194  rtl_string_unittest_const_literal = true;
195 #endif
196  }
197 
210  OStringBuffer(const sal_Char * value, sal_Int32 length)
211  : pData(NULL)
212  , nCapacity( length + 16 )
213  {
214  rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
215  }
216 
217 #ifdef RTL_FAST_STRING
218 
222  template< typename T1, typename T2 >
223  OStringBuffer( const OStringConcat< T1, T2 >& c )
224  {
225  const sal_Int32 l = c.length();
226  nCapacity = l + 16;
227  pData = rtl_string_alloc( nCapacity );
228  char* end = c.addData( pData->buffer );
229  *end = '\0';
230  pData->length = end - pData->buffer;
231  }
232 #endif
233 
236  OStringBuffer& operator = ( const OStringBuffer& value )
237  {
238  if (this != &value)
239  {
241  value.nCapacity,
242  value.pData);
243  nCapacity = value.nCapacity;
244  }
245  return *this;
246  }
247 
252  {
253  rtl_string_release( pData );
254  }
255 
265  {
266  OString aRet( pData );
267  rtl_string_new(&pData);
268  nCapacity = 0;
269  return aRet;
270  }
271 
277  sal_Int32 getLength() const
278  {
279  return pData->length;
280  }
281 
290  bool isEmpty() const
291  {
292  return pData->length == 0;
293  }
294 
305  sal_Int32 getCapacity() const
306  {
307  return nCapacity;
308  }
309 
321  void ensureCapacity(sal_Int32 minimumCapacity)
322  {
323  rtl_stringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
324  }
325 
344  void setLength(sal_Int32 newLength)
345  {
346  assert(newLength >= 0);
347  // Avoid modifications if pData points to const empty string:
348  if( newLength != pData->length )
349  {
350  if( newLength > nCapacity )
351  rtl_stringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
352  else
353  pData->buffer[newLength] = '\0';
354  pData->length = newLength;
355  }
356  }
357 
371  SAL_DEPRECATED("use rtl::OStringBuffer::operator [] instead")
372  sal_Char charAt( sal_Int32 index )
373  {
374  assert(index >= 0 && index < pData->length);
375  return pData->buffer[ index ];
376  }
377 
388  SAL_DEPRECATED("use rtl::OStringBuffer::operator [] instead")
389  OStringBuffer & setCharAt(sal_Int32 index, sal_Char ch)
390  {
391  assert(index >= 0 && index < pData->length);
392  pData->buffer[ index ] = ch;
393  return *this;
394  }
395 
399  const sal_Char* getStr() const { return pData->buffer; }
400 
410  sal_Char & operator [](sal_Int32 index)
411  {
412  assert(index >= 0 && index < pData->length);
413  return pData->buffer[index];
414  }
415 
420  const OString toString() const
421  {
422  return OString(pData->buffer, pData->length);
423  }
424 
436  {
437  return append( str.getStr(), str.getLength() );
438  }
439 
451  template< typename T >
453  {
454  return append( str, rtl_str_getLength( str ) );
455  }
456 
457  template< typename T >
459  {
460  return append( str, rtl_str_getLength( str ) );
461  }
462 
468  template< typename T >
470  {
471  RTL_STRING_CONST_FUNCTION
472  assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
473  rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 );
474  return *this;
475  }
476 
490  OStringBuffer & append( const sal_Char * str, sal_Int32 len)
491  {
492  assert( len >= 0 );
493  assert( len == 0 || str != 0 );
494  rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
495  return *this;
496  }
497 
498 #ifdef RTL_FAST_STRING
499 
503  template< typename T1, typename T2 >
504  OStringBuffer& append( const OStringConcat< T1, T2 >& c )
505  {
506  const int l = c.length();
507  if( l == 0 )
508  return *this;
509  rtl_stringbuffer_ensureCapacity( &pData, &nCapacity, pData->length + l );
510  char* end = c.addData( pData->buffer + pData->length );
511  *end = '\0';
512  pData->length = end - pData->buffer;
513  return *this;
514  }
515 #endif
516 
529  {
531  return append( sz, rtl_str_valueOfBoolean( sz, b ) );
532  }
533 
548  {
550  return append( sz, rtl_str_valueOfBoolean( sz, b ) );
551  }
552 
554  // Pointer can be automatically converted to bool, which is unwanted here.
555  // Explicitly delete all pointer append() overloads to prevent this
556  // (except for char* overload, which is handled elsewhere).
557  template< typename T >
558  typename libreoffice_internal::Enable< void,
560  append( T* ) SAL_DELETED_FUNCTION;
562 
574  {
575  return append( &c, 1 );
576  }
577 
590  OStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
591  {
593  return append( sz, rtl_str_valueOfInt32( sz, i, radix ) );
594  }
595 
608  OStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
609  {
611  return append( sz, rtl_str_valueOfInt64( sz, l, radix ) );
612  }
613 
626  {
628  return append( sz, rtl_str_valueOfFloat( sz, f ) );
629  }
630 
642  OStringBuffer & append(double d)
643  {
645  return append( sz, rtl_str_valueOfDouble( sz, d ) );
646  }
647 
663  char * appendUninitialized(sal_Int32 length) {
664  assert(length >= 0);
665  sal_Int32 n = getLength();
666  rtl_stringbuffer_insert(&pData, &nCapacity, n, 0, length);
667  return pData->buffer + n;
668  }
669 
685  OStringBuffer & insert(sal_Int32 offset, const OString & str)
686  {
687  return insert( offset, str.getStr(), str.getLength() );
688  }
689 
707  template< typename T >
709  {
710  return insert( offset, str, rtl_str_getLength( str ) );
711  }
712 
713  template< typename T >
715  {
716  return insert( offset, str, rtl_str_getLength( str ) );
717  }
718 
724  template< typename T >
726  {
727  RTL_STRING_CONST_FUNCTION
728  assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
730  return *this;
731  }
732 
751  OStringBuffer & insert( sal_Int32 offset, const sal_Char * str, sal_Int32 len)
752  {
753  assert( offset >= 0 && offset <= pData->length );
754  assert( len >= 0 );
755  assert( len == 0 || str != 0 );
756  rtl_stringbuffer_insert( &pData, &nCapacity, offset, str, len );
757  return *this;
758  }
759 
777  OStringBuffer & insert(sal_Int32 offset, sal_Bool b)
778  {
780  return insert( offset, sz, rtl_str_valueOfBoolean( sz, b ) );
781  }
782 
802  OStringBuffer & insert(sal_Int32 offset, bool b)
803  {
805  return insert( offset, sz, rtl_str_valueOfBoolean( sz, b ) );
806  }
807 
824  OStringBuffer & insert(sal_Int32 offset, sal_Char c)
825  {
826  return insert( offset, &c, 1 );
827  }
828 
847  OStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
848  {
850  return insert( offset, sz, rtl_str_valueOfInt32( sz, i, radix ) );
851  }
852 
871  OStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
872  {
874  return insert( offset, sz, rtl_str_valueOfInt64( sz, l, radix ) );
875  }
876 
894  OStringBuffer insert(sal_Int32 offset, float f)
895  {
897  return insert( offset, sz, rtl_str_valueOfFloat( sz, f ) );
898  }
899 
917  OStringBuffer & insert(sal_Int32 offset, double d)
918  {
920  return insert( offset, sz, rtl_str_valueOfDouble( sz, d ) );
921  }
922 
935  OStringBuffer & remove( sal_Int32 start, sal_Int32 len )
936  {
937  assert( start >= 0 && start <= pData->length );
938  assert( len >= 0 );
939  rtl_stringbuffer_remove( &pData, start, len );
940  return *this;
941  }
942 
943 #ifdef LIBO_INTERNAL_ONLY
944  // This is to complement the RTL_FAST_STRING operator+, which allows any combination of valid operands,
945  // even two buffers. It's intentional it returns OString, just like the operator+ would in the fast variant.
946 #ifndef RTL_FAST_STRING
947 
951  friend OString operator+( const OStringBuffer& str1, const OStringBuffer& str2 )
952  {
953  return OString( str1.pData ).concat( str2.pData );
954  }
955 #endif
956 #endif
957 
958 private:
962  rtl_String * pData;
963 
967  sal_Int32 nCapacity;
968 };
969 
970 #ifdef RTL_FAST_STRING
971 
974 template<>
975 struct ToStringHelper< OStringBuffer >
976  {
977  static int length( const OStringBuffer& s ) { return s.getLength(); }
978  static char* addData( char* buffer, const OStringBuffer& s ) { return addDataHelper( buffer, s.getStr(), s.getLength()); }
979  static const bool allowOStringConcat = true;
980  static const bool allowOUStringConcat = false;
981  };
982 #endif
983 
984 
985 }
986 
987 #ifdef RTL_STRING_UNITTEST
988 namespace rtl
989 {
990 typedef rtlunittest::OStringBuffer OStringBuffer;
991 }
992 #undef RTL_STRING_CONST_FUNCTION
993 #endif
994 
995 #ifdef RTL_USING
996 using ::rtl::OStringBuffer;
997 #endif
998 
999 #endif // INCLUDED_RTL_STRBUF_HXX
1000 
1001 
1002 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */