LibreOffice
LibreOffice 4.1 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 _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 
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( internal::ConstCharArrayDetector< T, void >::size - 1 + 16 )
190  {
191  assert( strlen( literal ) == 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 SAL_THROW(())
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  {
472  assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
473  rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), literal, internal::ConstCharArrayDetector< T, void >::size - 1 );
474  return *this;
475  }
476 
490  OStringBuffer & append( const sal_Char * str, sal_Int32 len)
491  {
492  // insert behind the last character
493  rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
494  return *this;
495  }
496 
497 #ifdef RTL_FAST_STRING
498 
502  template< typename T1, typename T2 >
503  OStringBuffer& append( const OStringConcat< T1, T2 >& c )
504  {
505  const int l = c.length();
506  if( l == 0 )
507  return *this;
508  rtl_stringbuffer_ensureCapacity( &pData, &nCapacity, pData->length + l );
509  char* end = c.addData( pData->buffer + pData->length );
510  *end = '\0';
511  pData->length = end - pData->buffer;
512  return *this;
513  }
514 #endif
515 
528  {
530  return append( sz, rtl_str_valueOfBoolean( sz, b ) );
531  }
532 
544  {
545  return append( &c, 1 );
546  }
547 
560  OStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
561  {
563  return append( sz, rtl_str_valueOfInt32( sz, i, radix ) );
564  }
565 
578  OStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
579  {
581  return append( sz, rtl_str_valueOfInt64( sz, l, radix ) );
582  }
583 
596  {
598  return append( sz, rtl_str_valueOfFloat( sz, f ) );
599  }
600 
612  OStringBuffer & append(double d)
613  {
615  return append( sz, rtl_str_valueOfDouble( sz, d ) );
616  }
617 
633  OStringBuffer & insert(sal_Int32 offset, const OString & str)
634  {
635  return insert( offset, str.getStr(), str.getLength() );
636  }
637 
655  template< typename T >
656  typename internal::CharPtrDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, const T& str )
657  {
658  return insert( offset, str, rtl_str_getLength( str ) );
659  }
660 
661  template< typename T >
663  {
664  return insert( offset, str, rtl_str_getLength( str ) );
665  }
666 
672  template< typename T >
674  {
676  assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
677  rtl_stringbuffer_insert( &pData, &nCapacity, offset, literal, internal::ConstCharArrayDetector< T, void >::size - 1 );
678  return *this;
679  }
680 
699  OStringBuffer & insert( sal_Int32 offset, const sal_Char * str, sal_Int32 len)
700  {
701  // insert behind the last character
702  rtl_stringbuffer_insert( &pData, &nCapacity, offset, str, len );
703  return *this;
704  }
705 
723  OStringBuffer & insert(sal_Int32 offset, sal_Bool b)
724  {
726  return insert( offset, sz, rtl_str_valueOfBoolean( sz, b ) );
727  }
728 
745  OStringBuffer & insert(sal_Int32 offset, sal_Char c)
746  {
747  return insert( offset, &c, 1 );
748  }
749 
768  OStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
769  {
771  return insert( offset, sz, rtl_str_valueOfInt32( sz, i, radix ) );
772  }
773 
792  OStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
793  {
795  return insert( offset, sz, rtl_str_valueOfInt64( sz, l, radix ) );
796  }
797 
815  OStringBuffer insert(sal_Int32 offset, float f)
816  {
818  return insert( offset, sz, rtl_str_valueOfFloat( sz, f ) );
819  }
820 
838  OStringBuffer & insert(sal_Int32 offset, double d)
839  {
841  return insert( offset, sz, rtl_str_valueOfDouble( sz, d ) );
842  }
843 
856  OStringBuffer & remove( sal_Int32 start, sal_Int32 len )
857  {
858  rtl_stringbuffer_remove( &pData, start, len );
859  return *this;
860  }
861 
862 #ifdef LIBO_INTERNAL_ONLY
863  // This is to complement the RTL_FAST_STRING operator+, which allows any combination of valid operands,
864  // even two buffers. It's intentional it returns OString, just like the operator+ would in the fast variant.
865 #ifndef RTL_FAST_STRING
866 
870  friend OString operator+( const OStringBuffer& str1, const OStringBuffer& str2 ) SAL_THROW(())
871  {
872  return OString( str1.pData ).concat( str2.pData );
873  }
874 #endif
875 #endif
876 
877 private:
881  rtl_String * pData;
882 
886  sal_Int32 nCapacity;
887 };
888 
889 #ifdef RTL_FAST_STRING
890 
893 template<>
894 struct ToStringHelper< OStringBuffer >
895  {
896  static int length( const OStringBuffer& s ) { return s.getLength(); }
897  static char* addData( char* buffer, const OStringBuffer& s ) { return addDataHelper( buffer, s.getStr(), s.getLength()); }
898  static const bool allowOStringConcat = true;
899  static const bool allowOUStringConcat = false;
900  };
901 #endif
902 
903 
904 }
905 
906 #ifdef RTL_STRING_UNITTEST
907 namespace rtl
908 {
909 typedef rtlunittest::OStringBuffer OStringBuffer;
910 }
911 #undef RTL_STRING_CONST_FUNCTION
912 #endif
913 
914 #ifdef RTL_USING
915 using ::rtl::OStringBuffer;
916 #endif
917 
918 #endif /* __cplusplus */
919 #endif /* _RTL_STRBUF_HXX_ */
920 
921 
922 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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.
OStringBuffer & append(double d)
Appends the string representation of the double argument to this string buffer.
Definition: strbuf.hxx:612
#define RTL_STR_MAX_VALUEOFINT64
Definition: string.h:650
OStringBuffer & append(const OString &str)
Appends the string to this string buffer.
Definition: strbuf.hxx:435
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:490
SAL_DLLPUBLIC sal_Int32 rtl_str_valueOfFloat(sal_Char *str, float f) SAL_THROW_EXTERN_C()
Create the string representation of a float.
OStringBuffer & insert(sal_Int32 offset, sal_Char c)
Inserts the string representation of the char argument into this string buffer.
Definition: strbuf.hxx:745
SAL_DLLPUBLIC void rtl_string_newFromLiteral(rtl_String **newStr, const sal_Char *value, sal_Int32 len, sal_Int32 allocExtra) SAL_THROW_EXTERN_C()
OString makeStringAndClear()
Fill the string data in the new string and clear the buffer.
Definition: strbuf.hxx:264
const OString toString() const
Return a OString instance reflecting the current content of this OStringBuffer.
Definition: strbuf.hxx:420
SAL_DLLPUBLIC void rtl_stringbuffer_remove(rtl_String **This, sal_Int32 start, sal_Int32 len)
Removes the characters in a substring of this sequence.
OStringBuffer(const OString &value)
Constructs a string buffer so that it represents the same sequence of characters as the string argume...
Definition: strbuf.hxx:146
OStringBuffer()
Constructs a string buffer with no characters in it and an initial capacity of 16 characters...
Definition: strbuf.hxx:103
OStringBuffer & insert(sal_Int32 offset, const OString &str)
Inserts the string into this string buffer.
Definition: strbuf.hxx:633
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:469
OStringBuffer(const OStringBuffer &value)
Allocates a new string buffer that contains the same sequence of characters as the string buffer argu...
Definition: strbuf.hxx:116
const sal_Char * getStr() const
Returns a pointer to the characters of this string.
Definition: string.hxx:367
SAL_DLLPUBLIC void rtl_string_new(rtl_String **newStr) SAL_THROW_EXTERN_C()
Allocate a new string containing no characters.
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.
#define SAL_DEPRECATED(message)
Use as follows: SAL_DEPRECATED(&quot;Dont use, its evil.&quot;) void doit(int nPara);.
Definition: types.h:477
SAL_WARN_UNUSED_RESULT OString concat(const OString &str) const
Concatenates the specified string to the end of this string.
Definition: string.hxx:1103
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.
Definition: stringutils.hxx:110
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:673
#define RTL_STR_MAX_VALUEOFBOOLEAN
Definition: string.h:585
SAL_DLLPUBLIC sal_Int32 rtl_str_valueOfDouble(sal_Char *str, double d) SAL_THROW_EXTERN_C()
Create the string representation of a double.
nCapacity
Definition: strbuf.hxx:162
sal_Int32 getLength() const
Returns the length of this string.
Definition: string.hxx:341
~OStringBuffer()
Release the string data.
Definition: strbuf.hxx:251
OStringBuffer & append(sal_Int64 l, sal_Int16 radix=10)
Appends the string representation of the long argument to this string buffer.
Definition: strbuf.hxx:578
sal_Int32 getCapacity() const
Returns the current capacity of the String buffer.
Definition: strbuf.hxx:305
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:560
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:768
Definition: stringutils.hxx:69
#define RTL_STR_MAX_VALUEOFFLOAT
Definition: string.h:692
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.
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:656
SAL_DLLPUBLIC void rtl_string_release(rtl_String *str) SAL_THROW_EXTERN_C()
Decrement the reference count of a string.
OStringBuffer(int length)
Constructs a string buffer with no characters in it and an initial capacity specified by the length a...
Definition: strbuf.hxx:129
OStringBuffer & append(sal_Char c)
Appends the string representation of the char argument to this string buffer.
Definition: strbuf.hxx:543
char sal_Char
Definition: types.h:122
void ensureCapacity(sal_Int32 minimumCapacity)
Ensures that the capacity of the buffer is at least equal to the specified minimum.
Definition: strbuf.hxx:321
sal_Int32 getLength() const
Returns the length (character count) of this string buffer.
Definition: strbuf.hxx:277
unsigned char sal_Bool
Definition: types.h:37
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.
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:561
OStringBuffer & insert(sal_Int32 offset, double d)
Inserts the string representation of the double argument into this string buffer. ...
Definition: strbuf.hxx:838
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:792
internal::NonConstCharArrayDetector< T, OStringBuffer & >::Type insert(sal_Int32 offset, T &str)
Definition: strbuf.hxx:662
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:210
A string buffer implements a mutable sequence of characters.
Definition: strbuf.hxx:96
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.
OStringBuffer & insert(sal_Int32 offset, sal_Bool b)
Inserts the string representation of the sal_Bool argument into this string buffer.
Definition: strbuf.hxx:723
bool isEmpty() const
Checks if a string buffer is empty.
Definition: strbuf.hxx:290
OStringBuffer & append(sal_Bool b)
Appends the string representation of the sal_Bool argument to the string buffer.
Definition: strbuf.hxx:527
OStringBuffer insert(sal_Int32 offset, float f)
Inserts the string representation of the float argument into this string buffer.
Definition: strbuf.hxx:815
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:699
const sal_Char * getStr() const
Return a null terminated character array.
Definition: strbuf.hxx:399
#define RTL_STR_MAX_VALUEOFINT32
Definition: string.h:627
SAL_DLLPUBLIC sal_Int32 rtl_str_valueOfBoolean(sal_Char *str, sal_Bool b) SAL_THROW_EXTERN_C()
Create the string representation of a boolean.
rtl_stringbuffer_newFromStr_WithLength & pData
Definition: strbuf.hxx:163
OStringBuffer & append(float f)
Appends the string representation of the float argument to this string buffer.
Definition: strbuf.hxx:595
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:452
#define RTL_STR_MAX_VALUEOFDOUBLE
Definition: string.h:711
internal::NonConstCharArrayDetector< T, OStringBuffer & >::Type append(T &str)
Definition: strbuf.hxx:458
void setLength(sal_Int32 newLength)
Sets the length of this String buffer.
Definition: strbuf.hxx:344
This String class provide base functionality for C++ like 8-Bit character array handling.
Definition: string.hxx:90
Definition: stringutils.hxx:67
SAL_DLLPUBLIC sal_Int32 rtl_str_getLength(const sal_Char *str) SAL_THROW_EXTERN_C()
Return the length of a string.
#define SAL_THROW(exc)
Definition of function throw clause macros.
Definition: types.h:356
#define RTL_STRING_CONST_FUNCTION
Definition: strbuf.hxx:55
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...
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.
Definition: stringutils.hxx:87