LibreOffice
LibreOffice 4.2 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 #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 
546  {
547  return append( &c, 1 );
548  }
549 
562  OStringBuffer & append(sal_Int32 i, sal_Int16 radix = 10 )
563  {
565  return append( sz, rtl_str_valueOfInt32( sz, i, radix ) );
566  }
567 
580  OStringBuffer & append(sal_Int64 l, sal_Int16 radix = 10 )
581  {
583  return append( sz, rtl_str_valueOfInt64( sz, l, radix ) );
584  }
585 
598  {
600  return append( sz, rtl_str_valueOfFloat( sz, f ) );
601  }
602 
614  OStringBuffer & append(double d)
615  {
617  return append( sz, rtl_str_valueOfDouble( sz, d ) );
618  }
619 
635  OStringBuffer & insert(sal_Int32 offset, const OString & str)
636  {
637  return insert( offset, str.getStr(), str.getLength() );
638  }
639 
657  template< typename T >
658  typename internal::CharPtrDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, const T& str )
659  {
660  return insert( offset, str, rtl_str_getLength( str ) );
661  }
662 
663  template< typename T >
665  {
666  return insert( offset, str, rtl_str_getLength( str ) );
667  }
668 
674  template< typename T >
676  {
677  RTL_STRING_CONST_FUNCTION
678  assert( strlen( literal ) == internal::ConstCharArrayDetector< T >::size - 1 );
679  rtl_stringbuffer_insert( &pData, &nCapacity, offset, literal, internal::ConstCharArrayDetector< T, void >::size - 1 );
680  return *this;
681  }
682 
701  OStringBuffer & insert( sal_Int32 offset, const sal_Char * str, sal_Int32 len)
702  {
703  // insert behind the last character
704  rtl_stringbuffer_insert( &pData, &nCapacity, offset, str, len );
705  return *this;
706  }
707 
725  OStringBuffer & insert(sal_Int32 offset, sal_Bool b)
726  {
728  return insert( offset, sz, rtl_str_valueOfBoolean( sz, b ) );
729  }
730 
747  OStringBuffer & insert(sal_Int32 offset, sal_Char c)
748  {
749  return insert( offset, &c, 1 );
750  }
751 
770  OStringBuffer & insert(sal_Int32 offset, sal_Int32 i, sal_Int16 radix = 10 )
771  {
773  return insert( offset, sz, rtl_str_valueOfInt32( sz, i, radix ) );
774  }
775 
794  OStringBuffer & insert(sal_Int32 offset, sal_Int64 l, sal_Int16 radix = 10 )
795  {
797  return insert( offset, sz, rtl_str_valueOfInt64( sz, l, radix ) );
798  }
799 
817  OStringBuffer insert(sal_Int32 offset, float f)
818  {
820  return insert( offset, sz, rtl_str_valueOfFloat( sz, f ) );
821  }
822 
840  OStringBuffer & insert(sal_Int32 offset, double d)
841  {
843  return insert( offset, sz, rtl_str_valueOfDouble( sz, d ) );
844  }
845 
858  OStringBuffer & remove( sal_Int32 start, sal_Int32 len )
859  {
860  rtl_stringbuffer_remove( &pData, start, len );
861  return *this;
862  }
863 
864 #ifdef LIBO_INTERNAL_ONLY
865  // This is to complement the RTL_FAST_STRING operator+, which allows any combination of valid operands,
866  // even two buffers. It's intentional it returns OString, just like the operator+ would in the fast variant.
867 #ifndef RTL_FAST_STRING
868 
872  friend OString operator+( const OStringBuffer& str1, const OStringBuffer& str2 ) SAL_THROW(())
873  {
874  return OString( str1.pData ).concat( str2.pData );
875  }
876 #endif
877 #endif
878 
879 private:
883  rtl_String * pData;
884 
888  sal_Int32 nCapacity;
889 };
890 
891 #ifdef RTL_FAST_STRING
892 
895 template<>
896 struct ToStringHelper< OStringBuffer >
897  {
898  static int length( const OStringBuffer& s ) { return s.getLength(); }
899  static char* addData( char* buffer, const OStringBuffer& s ) { return addDataHelper( buffer, s.getStr(), s.getLength()); }
900  static const bool allowOStringConcat = true;
901  static const bool allowOUStringConcat = false;
902  };
903 #endif
904 
905 
906 }
907 
908 #ifdef RTL_STRING_UNITTEST
909 namespace rtl
910 {
911 typedef rtlunittest::OStringBuffer OStringBuffer;
912 }
913 #undef RTL_STRING_CONST_FUNCTION
914 #endif
915 
916 #ifdef RTL_USING
917 using ::rtl::OStringBuffer;
918 #endif
919 
920 #endif /* __cplusplus */
921 #endif // INCLUDED_RTL_STRBUF_HXX
922 
923 
924 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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 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.
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.
const sal_Char * getStr() const
Return a null terminated character array.
Definition: strbuf.hxx:401
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
#define RTL_STR_MAX_VALUEOFINT64
Definition: string.h:650
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:794
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, float f)
Inserts the string representation of the float argument into this string buffer.
Definition: strbuf.hxx:817
Definition: stringutils.hxx:87
sal_Int32 getLength() const
Returns the length (character count) of this string buffer.
Definition: strbuf.hxx:279
OStringBuffer & insert(sal_Int32 offset, const OString &str)
Inserts the string into this string buffer.
Definition: strbuf.hxx:635
Definition: stringutils.hxx:67
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
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:701
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.
OStringBuffer & append(double d)
Appends the string representation of the double argument to this string buffer.
Definition: strbuf.hxx:614
OStringBuffer & append(const OString &str)
Appends the string to this string buffer.
Definition: strbuf.hxx:437
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
sal_Int32 getCapacity() const
Returns the current capacity of the String buffer.
Definition: strbuf.hxx:307
OStringBuffer()
Constructs a string buffer with no characters in it and an initial capacity of 16 characters...
Definition: strbuf.hxx:105
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_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 sal_Int32 rtl_str_valueOfDouble(sal_Char *str, double d) SAL_THROW_EXTERN_C()
Create the string representation of a double.
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.
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
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:591
internal::NonConstCharArrayDetector< T, OStringBuffer & >::Type append(T &str)
Definition: strbuf.hxx:460
#define SAL_DEPRECATED(message)
Use as follows: SAL_DEPRECATED("Dont use, its evil.") void doit(int nPara);.
Definition: types.h:479
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:675
#define RTL_STR_MAX_VALUEOFINT32
Definition: string.h:627
OStringBuffer & insert(sal_Int32 offset, double d)
Inserts the string representation of the double argument into this string buffer. ...
Definition: strbuf.hxx:840
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:658
SAL_DLLPUBLIC void rtl_string_newFromLiteral(rtl_String **newStr, const sal_Char *value, sal_Int32 len, sal_Int32 allocExtra) SAL_THROW_EXTERN_C()
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
OStringBuffer & append(sal_Int64 l, sal_Int16 radix=10)
Appends the string representation of the long argument to this string buffer.
Definition: strbuf.hxx:580
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:562
SAL_DLLPUBLIC void rtl_string_release(rtl_String *str) SAL_THROW_EXTERN_C()
Decrement the reference count of a string.
#define RTL_STR_MAX_VALUEOFDOUBLE
Definition: string.h:711
OStringBuffer & append(float f)
Appends the string representation of the float argument to this string buffer.
Definition: strbuf.hxx:597
This String class provide base functionality for C++ like 8-Bit character array handling.
Definition: string.hxx:89
~OStringBuffer()
Release the string data.
Definition: strbuf.hxx:253
internal::NonConstCharArrayDetector< T, OStringBuffer & >::Type insert(sal_Int32 offset, T &str)
Definition: strbuf.hxx:664
Definition: stringutils.hxx:110
const OString toString() const
Return a OString instance reflecting the current content of this OStringBuffer.
Definition: strbuf.hxx:422
unsigned char sal_Bool
Definition: types.h:46
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, sal_Bool b)
Inserts the string representation of the sal_Bool argument into this string buffer.
Definition: strbuf.hxx:725
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
OStringBuffer & insert(sal_Int32 offset, sal_Char c)
Inserts the string representation of the char argument into this string buffer.
Definition: strbuf.hxx:747
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_VALUEOFBOOLEAN
Definition: string.h:585
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.
void setLength(sal_Int32 newLength)
Sets the length of this String buffer.
Definition: strbuf.hxx:346
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 & 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:770
sal_Int32 getLength() const
Returns the length of this string.
Definition: string.hxx:327
char sal_Char
Definition: types.h:124
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
A string buffer implements a mutable sequence of characters.
Definition: strbuf.hxx:98
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
SAL_DLLPUBLIC sal_Int32 rtl_str_getLength(const sal_Char *str) SAL_THROW_EXTERN_C()
Return the length of a string.
const sal_Char * getStr() const
Returns a pointer to the characters of this string.
Definition: string.hxx:353
OStringBuffer & append(sal_Char c)
Appends the string representation of the char argument to this string buffer.
Definition: strbuf.hxx:545
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.
bool isEmpty() const
Checks if a string buffer is empty.
Definition: strbuf.hxx:292
SAL_DLLPUBLIC void rtl_string_new(rtl_String **newStr) SAL_THROW_EXTERN_C()
Allocate a new string containing no characters.
#define SAL_THROW(exc)
Definition of function throw clause macros.
Definition: types.h:358
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.
SAL_WARN_UNUSED_RESULT OString concat(const OString &str) const
Concatenates the specified string to the end of this string.
Definition: string.hxx:1115
#define RTL_STR_MAX_VALUEOFFLOAT
Definition: string.h:692