LibreOffice
LibreOffice 4.3 SDK C/C++ API Reference
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 #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 
51 #ifdef RTL_STRING_UNITTEST
52 #undef rtl
53 // helper macro to make functions appear more readable
54 #define RTL_STRING_CONST_FUNCTION rtl_string_unittest_const_literal_function = true;
55 #else
56 #define RTL_STRING_CONST_FUNCTION
57 #endif
58 
99 {
100 public:
106  : pData(NULL)
107  , nCapacity( 16 )
108  {
109  rtl_string_new_WithLength( &pData, nCapacity );
110  }
111 
118  OStringBuffer( const OStringBuffer & value )
119  : pData(NULL)
120  , nCapacity( value.nCapacity )
121  {
122  rtl_stringbuffer_newFromStringBuffer( &pData, value.nCapacity, value.pData );
123  }
124 
131  explicit OStringBuffer(int length)
132  : pData(NULL)
133  , nCapacity( length )
134  {
135  rtl_string_new_WithLength( &pData, length );
136  }
137 
148  OStringBuffer(const OString& value)
149  : pData(NULL)
150  , nCapacity( value.getLength() + 16 )
151  {
152  rtl_stringbuffer_newFromStr_WithLength( &pData, value.getStr(), value.getLength() );
153  }
154 
159  template< typename T >
161  : pData(NULL)
162  {
163  sal_Int32 length = rtl_str_getLength( value );
164  nCapacity = length + 16;
165  rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
166  }
167 
168  template< typename T >
170  : pData(NULL)
171  {
172  sal_Int32 length = rtl_str_getLength( value );
173  nCapacity = length + 16;
174  rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
175  }
176 
188  template< typename T >
190  : pData(NULL)
191  , nCapacity( internal::ConstCharArrayDetector< T, void >::size - 1 + 16 )
192  {
193  assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
195 #ifdef RTL_STRING_UNITTEST
196  rtl_string_unittest_const_literal = true;
197 #endif
198  }
199 
212  OStringBuffer(const sal_Char * value, sal_Int32 length)
213  : pData(NULL)
214  , nCapacity( length + 16 )
215  {
216  rtl_stringbuffer_newFromStr_WithLength( &pData, value, length );
217  }
218 
219 #ifdef RTL_FAST_STRING
220 
224  template< typename T1, typename T2 >
225  OStringBuffer( const OStringConcat< T1, T2 >& c )
226  {
227  const sal_Int32 l = c.length();
228  nCapacity = l + 16;
229  pData = rtl_string_alloc( nCapacity );
230  char* end = c.addData( pData->buffer );
231  *end = '\0';
232  pData->length = end - pData->buffer;
233  }
234 #endif
235 
238  OStringBuffer& operator = ( const OStringBuffer& value )
239  {
240  if (this != &value)
241  {
243  value.nCapacity,
244  value.pData);
245  nCapacity = value.nCapacity;
246  }
247  return *this;
248  }
249 
254  {
255  rtl_string_release( pData );
256  }
257 
267  {
268  OString aRet( pData );
269  rtl_string_new(&pData);
270  nCapacity = 0;
271  return aRet;
272  }
273 
279  sal_Int32 getLength() const
280  {
281  return pData->length;
282  }
283 
292  bool isEmpty() const SAL_THROW(())
293  {
294  return pData->length == 0;
295  }
296 
307  sal_Int32 getCapacity() const
308  {
309  return nCapacity;
310  }
311 
323  void ensureCapacity(sal_Int32 minimumCapacity)
324  {
325  rtl_stringbuffer_ensureCapacity( &pData, &nCapacity, minimumCapacity );
326  }
327 
346  void setLength(sal_Int32 newLength)
347  {
348  assert(newLength >= 0);
349  // Avoid modifications if pData points to const empty string:
350  if( newLength != pData->length )
351  {
352  if( newLength > nCapacity )
353  rtl_stringbuffer_ensureCapacity(&pData, &nCapacity, newLength);
354  else
355  pData->buffer[newLength] = '\0';
356  pData->length = newLength;
357  }
358  }
359 
373  SAL_DEPRECATED("use rtl::OStringBuffer::operator [] instead")
374  sal_Char charAt( sal_Int32 index )
375  {
376  assert(index >= 0 && index < pData->length);
377  return pData->buffer[ index ];
378  }
379 
390  SAL_DEPRECATED("use rtl::OStringBuffer::operator [] instead")
391  OStringBuffer & setCharAt(sal_Int32 index, sal_Char ch)
392  {
393  assert(index >= 0 && index < pData->length);
394  pData->buffer[ index ] = ch;
395  return *this;
396  }
397 
401  const sal_Char* getStr() const { return pData->buffer; }
402 
412  sal_Char & operator [](sal_Int32 index)
413  {
414  assert(index >= 0 && index < pData->length);
415  return pData->buffer[index];
416  }
417 
422  const OString toString() const
423  {
424  return OString(pData->buffer, pData->length);
425  }
426 
438  {
439  return append( str.getStr(), str.getLength() );
440  }
441 
453  template< typename T >
455  {
456  return append( str, rtl_str_getLength( str ) );
457  }
458 
459  template< typename T >
461  {
462  return append( str, rtl_str_getLength( str ) );
463  }
464 
470  template< typename T >
472  {
473  RTL_STRING_CONST_FUNCTION
474  assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
475  rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), literal, internal::ConstCharArrayDetector< T, void >::size - 1 );
476  return *this;
477  }
478 
492  OStringBuffer & append( const sal_Char * str, sal_Int32 len)
493  {
494  // insert behind the last character
495  rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
496  return *this;
497  }
498 
499 #ifdef RTL_FAST_STRING
500 
504  template< typename T1, typename T2 >
505  OStringBuffer& append( const OStringConcat< T1, T2 >& c )
506  {
507  const int l = c.length();
508  if( l == 0 )
509  return *this;
510  rtl_stringbuffer_ensureCapacity( &pData, &nCapacity, pData->length + l );
511  char* end = c.addData( pData->buffer + pData->length );
512  *end = '\0';
513  pData->length = end - pData->buffer;
514  return *this;
515  }
516 #endif
517 
530  {
532  return append( sz, rtl_str_valueOfBoolean( sz, b ) );
533  }
534 
549  {
551  return append( sz, rtl_str_valueOfBoolean( sz, b ) );
552  }
553 
555  // Pointer can be automatically converted to bool, which is unwanted here.
556  // Explicitly delete all pointer append() overloads to prevent this
557  // (except for char* overload, which is handled elsewhere).
558  template< typename T >
559  typename internal::Enable< void,
561  append( T* ) SAL_DELETED_FUNCTION;
563 
575  {
576  return append( &c, 1 );
577  }
578 
591  OStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
592  {
594  return append( sz, rtl_str_valueOfInt32( sz, i, radix ) );
595  }
596 
609  OStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
610  {
612  return append( sz, rtl_str_valueOfInt64( sz, l, radix ) );
613  }
614 
627  {
629  return append( sz, rtl_str_valueOfFloat( sz, f ) );
630  }
631 
643  OStringBuffer & append(double d)
644  {
646  return append( sz, rtl_str_valueOfDouble( sz, d ) );
647  }
648 
664  OStringBuffer & insert(sal_Int32 offset, const OString & str)
665  {
666  return insert( offset, str.getStr(), str.getLength() );
667  }
668 
686  template< typename T >
687  typename internal::CharPtrDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, const T& str )
688  {
689  return insert( offset, str, rtl_str_getLength( str ) );
690  }
691 
692  template< typename T >
694  {
695  return insert( offset, str, rtl_str_getLength( str ) );
696  }
697 
703  template< typename T >
705  {
706  RTL_STRING_CONST_FUNCTION
707  assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
708  rtl_stringbuffer_insert( &pData, &nCapacity, offset, literal, internal::ConstCharArrayDetector< T, void >::size - 1 );
709  return *this;
710  }
711 
730  OStringBuffer & insert( sal_Int32 offset, const sal_Char * str, sal_Int32 len)
731  {
732  // insert behind the last character
733  rtl_stringbuffer_insert( &pData, &nCapacity, offset, str, len );
734  return *this;
735  }
736 
754  OStringBuffer & insert(sal_Int32 offset, sal_Bool b)
755  {
757  return insert( offset, sz, rtl_str_valueOfBoolean( sz, b ) );
758  }
759 
779  OStringBuffer & insert(sal_Int32 offset, bool b)
780  {
782  return insert( offset, sz, rtl_str_valueOfBoolean( sz, b ) );
783  }
784 
801  OStringBuffer & insert(sal_Int32 offset, sal_Char c)
802  {
803  return insert( offset, &c, 1 );
804  }
805 
824  OStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
825  {
827  return insert( offset, sz, rtl_str_valueOfInt32( sz, i, radix ) );
828  }
829 
848  OStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
849  {
851  return insert( offset, sz, rtl_str_valueOfInt64( sz, l, radix ) );
852  }
853 
871  OStringBuffer insert(sal_Int32 offset, float f)
872  {
874  return insert( offset, sz, rtl_str_valueOfFloat( sz, f ) );
875  }
876 
894  OStringBuffer & insert(sal_Int32 offset, double d)
895  {
897  return insert( offset, sz, rtl_str_valueOfDouble( sz, d ) );
898  }
899 
912  OStringBuffer & remove( sal_Int32 start, sal_Int32 len )
913  {
914  rtl_stringbuffer_remove( &pData, start, len );
915  return *this;
916  }
917 
918 #ifdef LIBO_INTERNAL_ONLY
919  // This is to complement the RTL_FAST_STRING operator+, which allows any combination of valid operands,
920  // even two buffers. It's intentional it returns OString, just like the operator+ would in the fast variant.
921 #ifndef RTL_FAST_STRING
922 
926  friend OString operator+( const OStringBuffer& str1, const OStringBuffer& str2 ) SAL_THROW(())
927  {
928  return OString( str1.pData ).concat( str2.pData );
929  }
930 #endif
931 #endif
932 
933 private:
937  rtl_String * pData;
938 
942  sal_Int32 nCapacity;
943 };
944 
945 #ifdef RTL_FAST_STRING
946 
949 template<>
950 struct ToStringHelper< OStringBuffer >
951  {
952  static int length( const OStringBuffer& s ) { return s.getLength(); }
953  static char* addData( char* buffer, const OStringBuffer& s ) { return addDataHelper( buffer, s.getStr(), s.getLength()); }
954  static const bool allowOStringConcat = true;
955  static const bool allowOUStringConcat = false;
956  };
957 #endif
958 
959 
960 }
961 
962 #ifdef RTL_STRING_UNITTEST
963 namespace rtl
964 {
965 typedef rtlunittest::OStringBuffer OStringBuffer;
966 }
967 #undef RTL_STRING_CONST_FUNCTION
968 #endif
969 
970 #ifdef RTL_USING
971 using ::rtl::OStringBuffer;
972 #endif
973 
974 #endif /* __cplusplus */
975 #endif // INCLUDED_RTL_STRBUF_HXX
976 
977 
978 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OStringBuffer & append(double d)
Appends the string representation of the double argument to this string buffer.
Definition: strbuf.hxx:643
#define SAL_DEPRECATED(message)
Use as follows: SAL_DEPRECATED("Dont use, its evil.") void doit(int nPara);.
Definition: types.h:491
SAL_DLLPUBLIC void rtl_string_new_WithLength(rtl_String **newStr, sal_Int32 len) SAL_THROW_EXTERN_C()
Allocate a new string containing space for a given number of characters.
SAL_DLLPUBLIC sal_Int32 rtl_str_valueOfInt64(sal_Char *str, sal_Int64 l, sal_Int16 radix) SAL_THROW_EXTERN_C()
Create the string representation of a long integer.
const sal_Char * getStr() const SAL_THROW(())
Returns a pointer to the characters of this string.
Definition: string.hxx:353
OStringBuffer & append(bool b)
Appends the string representation of the bool argument to the string buffer.
Definition: strbuf.hxx:548
OStringBuffer & append(sal_Int64 l, sal_Int16 radix=10)
Appends the string representation of the long argument to this string buffer.
Definition: strbuf.hxx:609
#define RTL_STR_MAX_VALUEOFFLOAT
Definition: string.h:692
unsigned char sal_Bool
Definition: types.h:46
internal::ConstCharArrayDetector< T, OStringBuffer & >::Type insert(sal_Int32 offset, T &literal)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: strbuf.hxx:704
OStringBuffer(const OString &value)
Constructs a string buffer so that it represents the same sequence of characters as the string argume...
Definition: strbuf.hxx:148
Definition: bootstrap.hxx:24
Definition: stringutils.hxx:67
SAL_DLLPUBLIC void rtl_stringbuffer_newFromStr_WithLength(rtl_String **newStr, const sal_Char *value, sal_Int32 count)
Allocates a new String that contains characters from the character array argument.
SAL_DLLPUBLIC rtl_String * rtl_string_alloc(sal_Int32 nLen) SAL_THROW_EXTERN_C()
Allocate a new string containing space for a given number of characters.
OStringBuffer & insert(sal_Int32 offset, const OString &str)
Inserts the string into this string buffer.
Definition: strbuf.hxx:664
OStringBuffer(const T &value, typename internal::CharPtrDetector< T, internal::Dummy >::Type=internal::Dummy())
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: strbuf.hxx:160
const OString toString() const
Return a OString instance reflecting the current content of this OStringBuffer.
Definition: strbuf.hxx:422
OStringBuffer(const OStringBuffer &value)
Allocates a new string buffer that contains the same sequence of characters as the string buffer argu...
Definition: strbuf.hxx:118
A string buffer implements a mutable sequence of characters.
Definition: strbuf.hxx:98
OStringBuffer & insert(sal_Int32 offset, sal_Char c)
Inserts the string representation of the char argument into this string buffer.
Definition: strbuf.hxx:801
SAL_DLLPUBLIC sal_Int32 rtl_str_valueOfBoolean(sal_Char *str, sal_Bool b) SAL_THROW_EXTERN_C()
Create the string representation of a boolean.
SAL_DLLPUBLIC sal_Int32 rtl_str_valueOfDouble(sal_Char *str, double d) SAL_THROW_EXTERN_C()
Create the string representation of a double.
OStringBuffer(T &value, typename internal::NonConstCharArrayDetector< T, internal::Dummy >::Type=internal::Dummy())
Definition: strbuf.hxx:169
OStringBuffer & append(const sal_Char *str, sal_Int32 len)
Appends the string representation of the char array argument to this string buffer.
Definition: strbuf.hxx:492
OStringBuffer()
Constructs a string buffer with no characters in it and an initial capacity of 16 characters...
Definition: strbuf.hxx:105
~OStringBuffer()
Release the string data.
Definition: strbuf.hxx:253
internal::ConstCharArrayDetector< T, OStringBuffer & >::Type append(T &literal)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: strbuf.hxx:471
SAL_DLLPUBLIC sal_Int32 rtl_str_valueOfInt32(sal_Char *str, sal_Int32 i, sal_Int16 radix) SAL_THROW_EXTERN_C()
Create the string representation of an integer.
sal_Int32 getCapacity() const
Returns the current capacity of the String buffer.
Definition: strbuf.hxx:307
OStringBuffer(T &literal, typename internal::ConstCharArrayDetector< T, internal::Dummy >::Type=internal::Dummy())
Constructs a string buffer so that it represents the same sequence of characters as the string litera...
Definition: strbuf.hxx:189
char sal_Char
A legacy synonym for char.
Definition: types.h:128
OStringBuffer & insert(sal_Int32 offset, const sal_Char *str, sal_Int32 len)
Inserts the string representation of the char array argument into this string buffer.
Definition: strbuf.hxx:730
OStringBuffer & append(sal_Int32 i, sal_Int16 radix=10)
Appends the string representation of the sal_Int32 argument to this string buffer.
Definition: strbuf.hxx:591
void ensureCapacity(sal_Int32 minimumCapacity)
Ensures that the capacity of the buffer is at least equal to the specified minimum.
Definition: strbuf.hxx:323
SAL_DLLPUBLIC void rtl_stringbuffer_remove(rtl_String **This, sal_Int32 start, sal_Int32 len)
Removes the characters in a substring of this sequence.
#define RTL_STR_MAX_VALUEOFDOUBLE
Definition: string.h:711
SAL_WARN_UNUSED_RESULT OString concat(const OString &str) const SAL_THROW(())
Concatenates the specified string to the end of this string.
Definition: string.hxx:1115
const sal_Char * getStr() const
Return a null terminated character array.
Definition: strbuf.hxx:401
internal::CharPtrDetector< T, OStringBuffer & >::Type insert(sal_Int32 offset, const T &str)
Inserts the string representation of the char array argument into this string buffer.
Definition: strbuf.hxx:687
sal_Int32 getLength() const SAL_THROW(())
Returns the length of this string.
Definition: string.hxx:327
OStringBuffer(int length)
Constructs a string buffer with no characters in it and an initial capacity specified by the length a...
Definition: strbuf.hxx:131
Definition: stringutils.hxx:87
OStringBuffer & insert(sal_Int32 offset, sal_Bool b)
Inserts the string representation of the sal_Bool argument into this string buffer.
Definition: strbuf.hxx:754
Definition: stringutils.hxx:170
#define RTL_STR_MAX_VALUEOFINT32
Definition: string.h:627
OStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix=10)
Inserts the string representation of the long argument into this string buffer.
Definition: strbuf.hxx:848
SAL_DLLPUBLIC void rtl_string_release(rtl_String *str) SAL_THROW_EXTERN_C()
Decrement the reference count of a string.
This String class provide base functionality for C++ like 8-Bit character array handling.
Definition: string.hxx:89
Definition: stringutils.hxx:110
#define SAL_THROW(x)
Exception specification documentation.
Definition: types.h:361
internal::NonConstCharArrayDetector< T, OStringBuffer & >::Type insert(sal_Int32 offset, T &str)
Definition: strbuf.hxx:693
SAL_DLLPUBLIC void rtl_stringbuffer_insert(rtl_String **This, sal_Int32 *capacity, sal_Int32 offset, const sal_Char *str, sal_Int32 len)
Inserts the string representation of the char array argument into this string buffer.
OStringBuffer & append(sal_Bool b)
Appends the string representation of the sal_Bool argument to the string buffer.
Definition: strbuf.hxx:529
OStringBuffer & insert(sal_Int32 offset, bool b)
Inserts the string representation of the bool argument into this string buffer.
Definition: strbuf.hxx:779
OStringBuffer insert(sal_Int32 offset, float f)
Inserts the string representation of the float argument into this string buffer.
Definition: strbuf.hxx:871
sal_Int32 getLength() const
Returns the length (character count) of this string buffer.
Definition: strbuf.hxx:279
OStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix=10)
Inserts the string representation of the second sal_Int32 argument into this string buffer...
Definition: strbuf.hxx:824
SAL_DLLPUBLIC sal_Int32 rtl_stringbuffer_newFromStringBuffer(rtl_String **newStr, sal_Int32 capacity, rtl_String *oldStr)
Allocates a new String that contains the same sequence of characters as the string argument...
Definition: stringutils.hxx:69
SAL_DLLPUBLIC sal_Int32 rtl_str_valueOfFloat(sal_Char *str, float f) SAL_THROW_EXTERN_C()
Create the string representation of a float.
SAL_DLLPUBLIC sal_Int32 rtl_str_getLength(const sal_Char *str) SAL_THROW_EXTERN_C()
Return the length of a string.
SAL_DLLPUBLIC void rtl_string_newFromLiteral(rtl_String **newStr, const sal_Char *value, sal_Int32 len, sal_Int32 allocExtra) SAL_THROW_EXTERN_C()
OStringBuffer & insert(sal_Int32 offset, double d)
Inserts the string representation of the double argument into this string buffer. ...
Definition: strbuf.hxx:894
bool isEmpty() const SAL_THROW(())
Checks if a string buffer is empty.
Definition: strbuf.hxx:292
OStringBuffer & append(float f)
Appends the string representation of the float argument to this string buffer.
Definition: strbuf.hxx:626
SAL_DLLPUBLIC void rtl_string_new(rtl_String **newStr) SAL_THROW_EXTERN_C()
Allocate a new string containing no characters.
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:410
#define RTL_STR_MAX_VALUEOFBOOLEAN
Definition: string.h:585
OStringBuffer & append(const OString &str)
Appends the string to this string buffer.
Definition: strbuf.hxx:437
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:603
void setLength(sal_Int32 newLength)
Sets the length of this String buffer.
Definition: strbuf.hxx:346
SAL_DLLPUBLIC void rtl_stringbuffer_ensureCapacity(rtl_String **This, sal_Int32 *capacity, sal_Int32 minimumCapacity)
Ensures that the capacity of the buffer is at least equal to the specified minimum.
internal::CharPtrDetector< T, OStringBuffer & >::Type append(const T &str)
Appends the string representation of the char array argument to this string buffer.
Definition: strbuf.hxx:454
OString makeStringAndClear()
Fill the string data in the new string and clear the buffer.
Definition: strbuf.hxx:266
OStringBuffer(const sal_Char *value, sal_Int32 length)
Constructs a string buffer so that it represents the same sequence of characters as the string argume...
Definition: strbuf.hxx:212
internal::NonConstCharArrayDetector< T, OStringBuffer & >::Type append(T &str)
Definition: strbuf.hxx:460
#define RTL_STR_MAX_VALUEOFINT64
Definition: string.h:650