LibreOffice
LibreOffice 4.4 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ustring.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_USTRING_HXX
21 #define INCLUDED_RTL_USTRING_HXX
22 
23 #include <sal/config.h>
24 
25 #include <cassert>
26 #include <new>
27 #include <ostream>
28 #include <string.h>
29 
30 #include <rtl/ustring.h>
31 #include <rtl/string.hxx>
32 #include <rtl/stringutils.hxx>
33 #include <rtl/textenc.h>
34 #include <sal/log.hxx>
35 
36 #ifdef RTL_FAST_STRING
37 #include <rtl/stringconcat.hxx>
38 #endif
39 
40 // The unittest uses slightly different code to help check that the proper
41 // calls are made. The class is put into a different namespace to make
42 // sure the compiler generates a different (if generating also non-inline)
43 // copy of the function and does not merge them together. The class
44 // is "brought" into the proper rtl namespace by a typedef below.
45 #ifdef RTL_STRING_UNITTEST
46 #define rtl rtlunittest
47 #endif
48 
49 namespace rtl
50 {
51 
52 #ifdef RTL_STRING_UNITTEST
53 #undef rtl
54 #endif
55 
56 /* ======================================================================= */
57 
82 {
83 public:
85  rtl_uString * pData;
87 
92  {
93  pData = 0;
94  rtl_uString_new( &pData );
95  }
96 
102  OUString( const OUString & str )
103  {
104  pData = str.pData;
105  rtl_uString_acquire( pData );
106  }
107 
113  OUString( rtl_uString * str )
114  {
115  pData = str;
116  rtl_uString_acquire( pData );
117  }
118 
127  inline OUString( rtl_uString * str, __sal_NoAcquire )
128  { pData = str; }
129 
135  explicit OUString( sal_Unicode value )
136  : pData (0)
137  {
138  rtl_uString_newFromStr_WithLength( &pData, &value, 1 );
139  }
140 
146  OUString( const sal_Unicode * value )
147  {
148  pData = 0;
149  rtl_uString_newFromStr( &pData, value );
150  }
151 
160  OUString( const sal_Unicode * value, sal_Int32 length )
161  {
162  pData = 0;
163  rtl_uString_newFromStr_WithLength( &pData, value, length );
164  }
165 
181  template< typename T >
183  {
184  assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
185  pData = 0;
187  rtl_uString_new( &pData );
188  else
190 #ifdef RTL_STRING_UNITTEST
191  rtl_string_unittest_const_literal = true;
192 #endif
193  }
194 
195 #ifdef RTL_STRING_UNITTEST
196 
200  template< typename T >
202  {
203  pData = 0;
204  rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
205  rtl_string_unittest_invalid_conversion = true;
206  }
211  template< typename T >
212  OUString( const T&, typename libreoffice_internal::ExceptCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
213  {
214  pData = 0;
215  rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
216  rtl_string_unittest_invalid_conversion = true;
217  }
218 #endif
219 
234  OUString( const sal_Char * value, sal_Int32 length,
235  rtl_TextEncoding encoding,
236  sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS )
237  {
238  pData = 0;
239  rtl_string2UString( &pData, value, length, encoding, convertFlags );
240  if (pData == 0) {
241  throw std::bad_alloc();
242  }
243  }
244 
261  inline explicit OUString(
262  sal_uInt32 const * codePoints, sal_Int32 codePointCount):
263  pData(NULL)
264  {
265  rtl_uString_newFromCodePoints(&pData, codePoints, codePointCount);
266  if (pData == NULL) {
267  throw std::bad_alloc();
268  }
269  }
270 
271 #ifdef RTL_FAST_STRING
272 
276  template< typename T1, typename T2 >
277  OUString( const OUStringConcat< T1, T2 >& c )
278  {
279  const sal_Int32 l = c.length();
280  pData = rtl_uString_alloc( l );
281  if (l != 0)
282  {
283  sal_Unicode* end = c.addData( pData->buffer );
284  pData->length = end - pData->buffer;
285  *end = '\0';
286  // TODO realloc in case pData->length is noticeably smaller than l?
287  }
288  }
289 #endif
290 
295  {
296  rtl_uString_release( pData );
297  }
298 
310  static inline OUString const & unacquired( rtl_uString * const * ppHandle )
311  { return * reinterpret_cast< OUString const * >( ppHandle ); }
312 
318  OUString & operator=( const OUString & str )
319  {
320  rtl_uString_assign( &pData, str.pData );
321  return *this;
322  }
323 
336  template< typename T >
338  {
339  assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
341  rtl_uString_new( &pData );
342  else
344  return *this;
345  }
346 
352  OUString & operator+=( const OUString & str )
353  {
354  rtl_uString_newConcat( &pData, pData, str.pData );
355  return *this;
356  }
357 
358 #ifdef RTL_FAST_STRING
359 
363  template< typename T1, typename T2 >
364  OUString& operator+=( const OUStringConcat< T1, T2 >& c )
365  {
366  const int l = c.length();
367  if( l == 0 )
368  return *this;
369  rtl_uString_ensureCapacity( &pData, pData->length + l );
370  sal_Unicode* end = c.addData( pData->buffer + pData->length );
371  *end = '\0';
372  pData->length = end - pData->buffer;
373  return *this;
374  }
375 #endif
376 
381  void clear()
382  {
383  rtl_uString_new( &pData );
384  }
385 
394  sal_Int32 getLength() const { return pData->length; }
395 
404  bool isEmpty() const
405  {
406  return pData->length == 0;
407  }
408 
416  const sal_Unicode * getStr() const { return pData->buffer; }
417 
427  sal_Unicode operator [](sal_Int32 index) const {
428  // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
429  assert(index >= 0 && static_cast<sal_uInt32>(index) < static_cast<sal_uInt32>(getLength()));
430  return getStr()[index];
431  }
432 
445  sal_Int32 compareTo( const OUString & str ) const
446  {
447  return rtl_ustr_compare_WithLength( pData->buffer, pData->length,
448  str.pData->buffer, str.pData->length );
449  }
450 
466  sal_Int32 compareTo( const OUString & str, sal_Int32 maxLength ) const
467  {
468  return rtl_ustr_shortenedCompare_WithLength( pData->buffer, pData->length,
469  str.pData->buffer, str.pData->length, maxLength );
470  }
471 
484  sal_Int32 reverseCompareTo( const OUString & str ) const
485  {
486  return rtl_ustr_reverseCompare_WithLength( pData->buffer, pData->length,
487  str.pData->buffer, str.pData->length );
488  }
489 
495  template< typename T >
497  {
498  assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
499  return rtl_ustr_asciil_reverseCompare_WithLength( pData->buffer, pData->length,
501  }
502 
514  bool equals( const OUString & str ) const
515  {
516  if ( pData->length != str.pData->length )
517  return false;
518  if ( pData == str.pData )
519  return true;
520  return rtl_ustr_reverseCompare_WithLength( pData->buffer, pData->length,
521  str.pData->buffer, str.pData->length ) == 0;
522  }
523 
538  bool equalsIgnoreAsciiCase( const OUString & str ) const
539  {
540  if ( pData->length != str.pData->length )
541  return false;
542  if ( pData == str.pData )
543  return true;
544  return rtl_ustr_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
545  str.pData->buffer, str.pData->length ) == 0;
546  }
547 
563  sal_Int32 compareToIgnoreAsciiCase( const OUString & str ) const
564  {
565  return rtl_ustr_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
566  str.pData->buffer, str.pData->length );
567  }
568 
569 
575  template< typename T >
577  {
578  assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
580  return false;
581 
582  return rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, literal ) == 0;
583  }
584 
600  bool match( const OUString & str, sal_Int32 fromIndex = 0 ) const
601  {
602  return rtl_ustr_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
603  str.pData->buffer, str.pData->length, str.pData->length ) == 0;
604  }
605 
611  template< typename T >
612  typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type match( T& literal, sal_Int32 fromIndex = 0 ) const
613  {
614  assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
615  return rtl_ustr_ascii_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
617  }
618 
637  bool matchIgnoreAsciiCase( const OUString & str, sal_Int32 fromIndex = 0 ) const
638  {
639  return rtl_ustr_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
640  str.pData->buffer, str.pData->length,
641  str.pData->length ) == 0;
642  }
643 
649  template< typename T >
650  typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type matchIgnoreAsciiCase( T& literal, sal_Int32 fromIndex = 0 ) const
651  {
652  assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
653  return rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
655  }
656 
673  sal_Int32 compareToAscii( const sal_Char* asciiStr ) const
674  {
675  return rtl_ustr_ascii_compare_WithLength( pData->buffer, pData->length, asciiStr );
676  }
677 
701  "replace s1.compareToAscii(s2, strlen(s2)) == 0 with s1.startsWith(s2)")
702  sal_Int32 compareToAscii( const sal_Char * asciiStr, sal_Int32 maxLength ) const
703  {
704  return rtl_ustr_ascii_shortenedCompare_WithLength( pData->buffer, pData->length,
705  asciiStr, maxLength );
706  }
707 
727  sal_Int32 reverseCompareToAsciiL( const sal_Char * asciiStr, sal_Int32 asciiStrLength ) const
728  {
729  return rtl_ustr_asciil_reverseCompare_WithLength( pData->buffer, pData->length,
730  asciiStr, asciiStrLength );
731  }
732 
748  bool equalsAscii( const sal_Char* asciiStr ) const
749  {
750  return rtl_ustr_ascii_compare_WithLength( pData->buffer, pData->length,
751  asciiStr ) == 0;
752  }
753 
771  bool equalsAsciiL( const sal_Char* asciiStr, sal_Int32 asciiStrLength ) const
772  {
773  if ( pData->length != asciiStrLength )
774  return false;
775 
777  pData->buffer, asciiStr, asciiStrLength );
778  }
779 
798  bool equalsIgnoreAsciiCaseAscii( const sal_Char * asciiStr ) const
799  {
800  return rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, asciiStr ) == 0;
801  }
802 
821  sal_Int32 compareToIgnoreAsciiCaseAscii( const sal_Char * asciiStr ) const
822  {
823  return rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, asciiStr );
824  }
825 
846  bool equalsIgnoreAsciiCaseAsciiL( const sal_Char * asciiStr, sal_Int32 asciiStrLength ) const
847  {
848  if ( pData->length != asciiStrLength )
849  return false;
850 
851  return rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, asciiStr ) == 0;
852  }
853 
875  bool matchAsciiL( const sal_Char* asciiStr, sal_Int32 asciiStrLength, sal_Int32 fromIndex = 0 ) const
876  {
877  return rtl_ustr_ascii_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
878  asciiStr, asciiStrLength ) == 0;
879  }
880 
881  // This overload is left undefined, to detect calls of matchAsciiL that
882  // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
883  // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
884  // platforms):
885 #if SAL_TYPES_SIZEOFLONG == 8
886  void matchAsciiL(char const *, sal_Int32, rtl_TextEncoding) const;
887 #endif
888 
913  bool matchIgnoreAsciiCaseAsciiL( const sal_Char* asciiStr, sal_Int32 asciiStrLength, sal_Int32 fromIndex = 0 ) const
914  {
915  return rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
916  asciiStr, asciiStrLength ) == 0;
917  }
918 
919  // This overload is left undefined, to detect calls of
920  // matchIgnoreAsciiCaseAsciiL that erroneously use
921  // RTL_CONSTASCII_USTRINGPARAM instead of RTL_CONSTASCII_STRINGPARAM (but
922  // would lead to ambiguities on 32 bit platforms):
923 #if SAL_TYPES_SIZEOFLONG == 8
924  void matchIgnoreAsciiCaseAsciiL(char const *, sal_Int32, rtl_TextEncoding)
925  const;
926 #endif
927 
942  bool startsWith(OUString const & str, OUString * rest = 0) const {
943  bool b = match(str, 0);
944  if (b && rest != 0) {
945  *rest = copy(str.getLength());
946  }
947  return b;
948  }
949 
955  template< typename T >
957  T & literal, OUString * rest = 0) const
958  {
959  assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
961  && rtl_ustr_asciil_reverseEquals_WithLength( pData->buffer, literal,
963  if (b && rest != 0) {
965  }
966  return b;
967  }
968 
989  bool startsWithIgnoreAsciiCase(OUString const & str, OUString * rest = 0)
990  const
991  {
992  bool b = matchIgnoreAsciiCase(str, 0);
993  if (b && rest != 0) {
994  *rest = copy(str.getLength());
995  }
996  return b;
997  }
998 
1004  template< typename T >
1006  startsWithIgnoreAsciiCase(T & literal, OUString * rest = 0) const
1007  {
1008  assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1010  pData->buffer,
1013  == 0);
1014  if (b && rest != 0) {
1016  }
1017  return b;
1018  }
1019 
1034  bool endsWith(OUString const & str, OUString * rest = 0) const {
1035  bool b = str.getLength() <= getLength()
1036  && match(str, getLength() - str.getLength());
1037  if (b && rest != 0) {
1038  *rest = copy(0, getLength() - str.getLength());
1039  }
1040  return b;
1041  }
1042 
1048  template< typename T >
1050  endsWith(T & literal, OUString * rest = 0) const
1051  {
1052  assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1055  pData->buffer + pData->length - ( libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ), literal,
1057  if (b && rest != 0) {
1058  *rest = copy(
1059  0,
1060  (getLength()
1062  }
1063  return b;
1064  }
1065 
1077  inline bool endsWithAsciiL(char const * asciiStr, sal_Int32 asciiStrLength)
1078  const
1079  {
1080  return asciiStrLength <= pData->length
1082  pData->buffer + pData->length - asciiStrLength, asciiStr,
1083  asciiStrLength);
1084  }
1085 
1106  bool endsWithIgnoreAsciiCase(OUString const & str, OUString * rest = 0) const
1107  {
1108  bool b = str.getLength() <= getLength()
1109  && matchIgnoreAsciiCase(str, getLength() - str.getLength());
1110  if (b && rest != 0) {
1111  *rest = copy(0, getLength() - str.getLength());
1112  }
1113  return b;
1114  }
1115 
1121  template< typename T >
1123  endsWithIgnoreAsciiCase(T & literal, OUString * rest = 0) const
1124  {
1125  assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1128  pData->buffer + pData->length - ( libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ),
1131  == 0);
1132  if (b && rest != 0) {
1133  *rest = copy(
1134  0,
1135  (getLength()
1137  }
1138  return b;
1139  }
1140 
1152  char const * asciiStr, sal_Int32 asciiStrLength) const
1153  {
1154  return asciiStrLength <= pData->length
1156  pData->buffer + pData->length - asciiStrLength,
1157  asciiStrLength, asciiStr, asciiStrLength)
1158  == 0);
1159  }
1160 
1161  friend bool operator == ( const OUString& rStr1, const OUString& rStr2 )
1162  { return rStr1.equals(rStr2); }
1163  friend bool operator == ( const OUString& rStr1, const sal_Unicode * pStr2 )
1164  { return rStr1.compareTo( pStr2 ) == 0; }
1165  friend bool operator == ( const sal_Unicode * pStr1, const OUString& rStr2 )
1166  { return OUString( pStr1 ).compareTo( rStr2 ) == 0; }
1167 
1168  friend bool operator != ( const OUString& rStr1, const OUString& rStr2 )
1169  { return !(operator == ( rStr1, rStr2 )); }
1170  friend bool operator != ( const OUString& rStr1, const sal_Unicode * pStr2 )
1171  { return !(operator == ( rStr1, pStr2 )); }
1172  friend bool operator != ( const sal_Unicode * pStr1, const OUString& rStr2 )
1173  { return !(operator == ( pStr1, rStr2 )); }
1174 
1175  friend bool operator < ( const OUString& rStr1, const OUString& rStr2 )
1176  { return rStr1.compareTo( rStr2 ) < 0; }
1177  friend bool operator > ( const OUString& rStr1, const OUString& rStr2 )
1178  { return rStr1.compareTo( rStr2 ) > 0; }
1179  friend bool operator <= ( const OUString& rStr1, const OUString& rStr2 )
1180  { return rStr1.compareTo( rStr2 ) <= 0; }
1181  friend bool operator >= ( const OUString& rStr1, const OUString& rStr2 )
1182  { return rStr1.compareTo( rStr2 ) >= 0; }
1183 
1191  template< typename T >
1192  friend inline typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator==( const OUString& string, T& literal )
1193  {
1194  assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1195  return string.equalsAsciiL( literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 );
1196  }
1204  template< typename T >
1205  friend inline typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator==( T& literal, const OUString& string )
1206  {
1207  assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1208  return string.equalsAsciiL( literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 );
1209  }
1217  template< typename T >
1218  friend inline typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator!=( const OUString& string, T& literal )
1219  {
1220  assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1221  return !string.equalsAsciiL( literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 );
1222  }
1230  template< typename T >
1231  friend inline typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator!=( T& literal, const OUString& string )
1232  {
1233  assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1234  return !string.equalsAsciiL( literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 );
1235  }
1236 
1244  sal_Int32 hashCode() const
1245  {
1246  return rtl_ustr_hashCode_WithLength( pData->buffer, pData->length );
1247  }
1248 
1262  sal_Int32 indexOf( sal_Unicode ch, sal_Int32 fromIndex = 0 ) const
1263  {
1264  sal_Int32 ret = rtl_ustr_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch );
1265  return (ret < 0 ? ret : ret+fromIndex);
1266  }
1267 
1277  sal_Int32 lastIndexOf( sal_Unicode ch ) const
1278  {
1279  return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch );
1280  }
1281 
1294  sal_Int32 lastIndexOf( sal_Unicode ch, sal_Int32 fromIndex ) const
1295  {
1296  return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch );
1297  }
1298 
1314  sal_Int32 indexOf( const OUString & str, sal_Int32 fromIndex = 0 ) const
1315  {
1316  sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1317  str.pData->buffer, str.pData->length );
1318  return (ret < 0 ? ret : ret+fromIndex);
1319  }
1320 
1326  template< typename T >
1327  typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const
1328  {
1329  assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1330  sal_Int32 ret = rtl_ustr_indexOfAscii_WithLength(
1331  pData->buffer + fromIndex, pData->length - fromIndex, literal,
1333  return ret < 0 ? ret : ret + fromIndex;
1334  }
1335 
1359  sal_Int32 indexOfAsciiL(
1360  char const * str, sal_Int32 len, sal_Int32 fromIndex = 0) const
1361  {
1362  sal_Int32 ret = rtl_ustr_indexOfAscii_WithLength(
1363  pData->buffer + fromIndex, pData->length - fromIndex, str, len);
1364  return ret < 0 ? ret : ret + fromIndex;
1365  }
1366 
1367  // This overload is left undefined, to detect calls of indexOfAsciiL that
1368  // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
1369  // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
1370  // platforms):
1371 #if SAL_TYPES_SIZEOFLONG == 8
1372  void indexOfAsciiL(char const *, sal_Int32 len, rtl_TextEncoding) const;
1373 #endif
1374 
1390  sal_Int32 lastIndexOf( const OUString & str ) const
1391  {
1392  return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1393  str.pData->buffer, str.pData->length );
1394  }
1395 
1413  sal_Int32 lastIndexOf( const OUString & str, sal_Int32 fromIndex ) const
1414  {
1415  return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1416  str.pData->buffer, str.pData->length );
1417  }
1418 
1424  template< typename T >
1426  {
1427  assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1429  pData->buffer, pData->length, literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1);
1430  }
1431 
1451  sal_Int32 lastIndexOfAsciiL(char const * str, sal_Int32 len) const
1452  {
1454  pData->buffer, pData->length, str, len);
1455  }
1456 
1467  SAL_WARN_UNUSED_RESULT OUString copy( sal_Int32 beginIndex ) const
1468  {
1469  rtl_uString *pNew = 0;
1470  rtl_uString_newFromSubString( &pNew, pData, beginIndex, getLength() - beginIndex );
1471  return OUString( pNew, SAL_NO_ACQUIRE );
1472  }
1473 
1486  SAL_WARN_UNUSED_RESULT OUString copy( sal_Int32 beginIndex, sal_Int32 count ) const
1487  {
1488  rtl_uString *pNew = 0;
1489  rtl_uString_newFromSubString( &pNew, pData, beginIndex, count );
1490  return OUString( pNew, SAL_NO_ACQUIRE );
1491  }
1492 
1502  {
1503  rtl_uString* pNew = 0;
1504  rtl_uString_newConcat( &pNew, pData, str.pData );
1505  return OUString( pNew, SAL_NO_ACQUIRE );
1506  }
1507 
1508 #ifndef RTL_FAST_STRING
1509  friend OUString operator+( const OUString& rStr1, const OUString& rStr2 )
1510  {
1511  return rStr1.concat( rStr2 );
1512  }
1513 #endif
1514 
1528  SAL_WARN_UNUSED_RESULT OUString replaceAt( sal_Int32 index, sal_Int32 count, const OUString& newStr ) const
1529  {
1530  rtl_uString* pNew = 0;
1531  rtl_uString_newReplaceStrAt( &pNew, pData, index, count, newStr.pData );
1532  return OUString( pNew, SAL_NO_ACQUIRE );
1533  }
1534 
1549  {
1550  rtl_uString* pNew = 0;
1551  rtl_uString_newReplace( &pNew, pData, oldChar, newChar );
1552  return OUString( pNew, SAL_NO_ACQUIRE );
1553  }
1554 
1574  OUString const & from, OUString const & to, sal_Int32 * index = 0) const
1575  {
1576  rtl_uString * s = 0;
1577  sal_Int32 i = 0;
1579  &s, pData, from.pData, to.pData, index == 0 ? &i : index);
1580  return OUString(s, SAL_NO_ACQUIRE);
1581  }
1582 
1601  template< typename T >
1603  sal_Int32 * index = 0) const
1604  {
1605  rtl_uString * s = 0;
1606  sal_Int32 i = 0;
1607  assert( strlen( from ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1609  &s, pData, from, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, to.pData, index == 0 ? &i : index);
1610  return OUString(s, SAL_NO_ACQUIRE);
1611  }
1612 
1631  template< typename T1, typename T2 >
1633  replaceFirst( T1& from, T2& to, sal_Int32 * index = 0) const
1634  {
1635  rtl_uString * s = 0;
1636  sal_Int32 i = 0;
1637  assert( strlen( from ) == libreoffice_internal::ConstCharArrayDetector< T1 >::size - 1 );
1638  assert( strlen( to ) == libreoffice_internal::ConstCharArrayDetector< T2 >::size - 1 );
1642  return OUString(s, SAL_NO_ACQUIRE);
1643  }
1644 
1661  OUString const & from, OUString const & to, sal_Int32 fromIndex = 0) const
1662  {
1663  rtl_uString * s = 0;
1664  rtl_uString_newReplaceAllFromIndex(&s, pData, from.pData, to.pData, fromIndex);
1665  return OUString(s, SAL_NO_ACQUIRE);
1666  }
1667 
1681  template< typename T >
1683  {
1684  rtl_uString * s = 0;
1685  assert( strlen( from ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1687  return OUString(s, SAL_NO_ACQUIRE);
1688  }
1689 
1703  template< typename T1, typename T2 >
1705  replaceAll( T1& from, T2& to ) const
1706  {
1707  rtl_uString * s = 0;
1708  assert( strlen( from ) == libreoffice_internal::ConstCharArrayDetector< T1 >::size - 1 );
1709  assert( strlen( to ) == libreoffice_internal::ConstCharArrayDetector< T2 >::size - 1 );
1713  return OUString(s, SAL_NO_ACQUIRE);
1714  }
1715 
1727  {
1728  rtl_uString* pNew = 0;
1729  rtl_uString_newToAsciiLowerCase( &pNew, pData );
1730  return OUString( pNew, SAL_NO_ACQUIRE );
1731  }
1732 
1744  {
1745  rtl_uString* pNew = 0;
1746  rtl_uString_newToAsciiUpperCase( &pNew, pData );
1747  return OUString( pNew, SAL_NO_ACQUIRE );
1748  }
1749 
1762  {
1763  rtl_uString* pNew = 0;
1764  rtl_uString_newTrim( &pNew, pData );
1765  return OUString( pNew, SAL_NO_ACQUIRE );
1766  }
1767 
1792  OUString getToken( sal_Int32 token, sal_Unicode cTok, sal_Int32& index ) const
1793  {
1794  rtl_uString * pNew = 0;
1795  index = rtl_uString_getToken( &pNew, pData, token, cTok, index );
1796  return OUString( pNew, SAL_NO_ACQUIRE );
1797  }
1798 
1812  OUString getToken(sal_Int32 count, sal_Unicode separator) const {
1813  sal_Int32 n = 0;
1814  return getToken(count, separator, n);
1815  }
1816 
1825  bool toBoolean() const
1826  {
1827  return rtl_ustr_toBoolean( pData->buffer );
1828  }
1829 
1837  {
1838  return pData->buffer[0];
1839  }
1840 
1851  sal_Int32 toInt32( sal_Int16 radix = 10 ) const
1852  {
1853  return rtl_ustr_toInt32( pData->buffer, radix );
1854  }
1855 
1868  sal_uInt32 toUInt32( sal_Int16 radix = 10 ) const
1869  {
1870  return rtl_ustr_toUInt32( pData->buffer, radix );
1871  }
1872 
1883  sal_Int64 toInt64( sal_Int16 radix = 10 ) const
1884  {
1885  return rtl_ustr_toInt64( pData->buffer, radix );
1886  }
1887 
1900  sal_uInt64 toUInt64( sal_Int16 radix = 10 ) const
1901  {
1902  return rtl_ustr_toUInt64( pData->buffer, radix );
1903  }
1904 
1913  float toFloat() const
1914  {
1915  return rtl_ustr_toFloat( pData->buffer );
1916  }
1917 
1926  double toDouble() const
1927  {
1928  return rtl_ustr_toDouble( pData->buffer );
1929  }
1930 
1931 
1948  {
1949  rtl_uString * pNew = 0;
1950  rtl_uString_intern( &pNew, pData );
1951  if (pNew == 0) {
1952  throw std::bad_alloc();
1953  }
1954  return OUString( pNew, SAL_NO_ACQUIRE );
1955  }
1956 
1982  static OUString intern( const sal_Char * value, sal_Int32 length,
1983  rtl_TextEncoding encoding,
1984  sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS,
1985  sal_uInt32 *pInfo = NULL )
1986  {
1987  rtl_uString * pNew = 0;
1988  rtl_uString_internConvert( &pNew, value, length, encoding,
1989  convertFlags, pInfo );
1990  if (pNew == 0) {
1991  throw std::bad_alloc();
1992  }
1993  return OUString( pNew, SAL_NO_ACQUIRE );
1994  }
1995 
2020  inline bool convertToString(OString * pTarget, rtl_TextEncoding nEncoding,
2021  sal_uInt32 nFlags) const
2022  {
2023  return rtl_convertUStringToString(&pTarget->pData, pData->buffer,
2024  pData->length, nEncoding, nFlags);
2025  }
2026 
2078  inline sal_uInt32 iterateCodePoints(
2079  sal_Int32 * indexUtf16, sal_Int32 incrementCodePoints = 1) const
2080  {
2082  pData, indexUtf16, incrementCodePoints);
2083  }
2084 
2094  static inline OUString fromUtf8(const OString& rSource)
2095  {
2096  OUString aTarget;
2097  bool bSuccess = rtl_convertStringToUString(&aTarget.pData,
2098  rSource.getStr(),
2099  rSource.getLength(),
2102  (void) bSuccess;
2103  assert(bSuccess);
2104  return aTarget;
2105  }
2106 
2117  inline OString toUtf8() const
2118  {
2119  OString aTarget;
2120  bool bSuccess = rtl_convertUStringToString(&aTarget.pData,
2121  getStr(),
2122  getLength(),
2125  (void) bSuccess;
2126  assert(bSuccess);
2127  return aTarget;
2128  }
2129 
2140  static OUString number( int i, sal_Int16 radix = 10 )
2141  {
2143  rtl_uString* pNewData = 0;
2144  rtl_uString_newFromStr_WithLength( &pNewData, aBuf, rtl_ustr_valueOfInt32( aBuf, i, radix ) );
2145  return OUString( pNewData, SAL_NO_ACQUIRE );
2146  }
2149  static OUString number( unsigned int i, sal_Int16 radix = 10 )
2150  {
2151  return number( static_cast< unsigned long long >( i ), radix );
2152  }
2155  static OUString number( long i, sal_Int16 radix = 10)
2156  {
2157  return number( static_cast< long long >( i ), radix );
2158  }
2161  static OUString number( unsigned long i, sal_Int16 radix = 10 )
2162  {
2163  return number( static_cast< unsigned long long >( i ), radix );
2164  }
2167  static OUString number( long long ll, sal_Int16 radix = 10 )
2168  {
2170  rtl_uString* pNewData = 0;
2171  rtl_uString_newFromStr_WithLength( &pNewData, aBuf, rtl_ustr_valueOfInt64( aBuf, ll, radix ) );
2172  return OUString( pNewData, SAL_NO_ACQUIRE );
2173  }
2176  static OUString number( unsigned long long ll, sal_Int16 radix = 10 )
2177  {
2179  rtl_uString* pNewData = 0;
2180  rtl_uString_newFromStr_WithLength( &pNewData, aBuf, rtl_ustr_valueOfUInt64( aBuf, ll, radix ) );
2181  return OUString( pNewData, SAL_NO_ACQUIRE );
2182  }
2183 
2193  static OUString number( float f )
2194  {
2196  rtl_uString* pNewData = 0;
2197  rtl_uString_newFromStr_WithLength( &pNewData, aBuf, rtl_ustr_valueOfFloat( aBuf, f ) );
2198  return OUString( pNewData, SAL_NO_ACQUIRE );
2199  }
2200 
2210  static OUString number( double d )
2211  {
2213  rtl_uString* pNewData = 0;
2214  rtl_uString_newFromStr_WithLength( &pNewData, aBuf, rtl_ustr_valueOfDouble( aBuf, d ) );
2215  return OUString( pNewData, SAL_NO_ACQUIRE );
2216  }
2217 
2229  SAL_DEPRECATED("use boolean()") static OUString valueOf( sal_Bool b )
2230  {
2231  return boolean(b);
2232  }
2233 
2245  static OUString boolean( bool b )
2246  {
2248  rtl_uString* pNewData = 0;
2249  rtl_uString_newFromStr_WithLength( &pNewData, aBuf, rtl_ustr_valueOfBoolean( aBuf, b ) );
2250  return OUString( pNewData, SAL_NO_ACQUIRE );
2251  }
2252 
2260  SAL_DEPRECATED("convert to OUString or use directly") static OUString valueOf( sal_Unicode c )
2261  {
2262  return OUString( &c, 1 );
2263  }
2264 
2275  SAL_DEPRECATED("use number()") static OUString valueOf( sal_Int32 i, sal_Int16 radix = 10 )
2276  {
2277  return number( i, radix );
2278  }
2279 
2290  SAL_DEPRECATED("use number()") static OUString valueOf( sal_Int64 ll, sal_Int16 radix = 10 )
2291  {
2292  return number( ll, radix );
2293  }
2294 
2304  SAL_DEPRECATED("use number()") static OUString valueOf( float f )
2305  {
2306  return number(f);
2307  }
2308 
2318  SAL_DEPRECATED("use number()") static OUString valueOf( double d )
2319  {
2320  return number(d);
2321  }
2322 
2338  static OUString createFromAscii( const sal_Char * value )
2339  {
2340  rtl_uString* pNew = 0;
2341  rtl_uString_newFromAscii( &pNew, value );
2342  return OUString( pNew, SAL_NO_ACQUIRE );
2343  }
2344 };
2345 
2346 /* ======================================================================= */
2347 
2348 #ifdef RTL_FAST_STRING
2349 
2357 struct SAL_WARN_UNUSED OUStringLiteral
2358 {
2359  template< int N >
2360  OUStringLiteral( const char (&str)[ N ] ) : size( N - 1 ), data( str ) { assert( strlen( str ) == N - 1 ); }
2361  int size;
2362  const char* data;
2363 };
2364 
2368 template<>
2369 struct ToStringHelper< OUString >
2370  {
2371  static int length( const OUString& s ) { return s.getLength(); }
2372  static sal_Unicode* addData( sal_Unicode* buffer, const OUString& s ) { return addDataHelper( buffer, s.getStr(), s.getLength()); }
2373  static const bool allowOStringConcat = false;
2374  static const bool allowOUStringConcat = true;
2375  };
2376 
2380 template<>
2381 struct ToStringHelper< OUStringLiteral >
2382  {
2383  static int length( const OUStringLiteral& str ) { return str.size; }
2384  static sal_Unicode* addData( sal_Unicode* buffer, const OUStringLiteral& str ) { return addDataLiteral( buffer, str.data, str.size ); }
2385  static const bool allowOStringConcat = false;
2386  static const bool allowOUStringConcat = true;
2387  };
2388 
2392 template< typename charT, typename traits, typename T1, typename T2 >
2393 inline std::basic_ostream<charT, traits> & operator <<(
2394  std::basic_ostream<charT, traits> & stream, const OUStringConcat< T1, T2 >& concat)
2395 {
2396  return stream << OUString( concat );
2397 }
2398 #else
2399 // non-RTL_FAST_STRING needs this to compile
2401 typedef OUString OUStringLiteral;
2403 #endif
2404 
2411 {
2421  size_t operator()(const OUString& rString) const
2422  { return (size_t)rString.hashCode(); }
2423 };
2424 
2425 /* ======================================================================= */
2426 
2444 inline OUString OStringToOUString( const OString & rStr,
2445  rtl_TextEncoding encoding,
2446  sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS )
2447 {
2448  return OUString( rStr.getStr(), rStr.getLength(), encoding, convertFlags );
2449 }
2450 
2468 inline OString OUStringToOString( const OUString & rUnicode,
2469  rtl_TextEncoding encoding,
2470  sal_uInt32 convertFlags = OUSTRING_TO_OSTRING_CVTFLAGS )
2471 {
2472  return OString( rUnicode.getStr(), rUnicode.getLength(), encoding, convertFlags );
2473 }
2474 
2475 /* ======================================================================= */
2476 
2485 template< typename charT, typename traits >
2486 inline std::basic_ostream<charT, traits> & operator <<(
2487  std::basic_ostream<charT, traits> & stream, OUString const & string)
2488 {
2489  return stream <<
2491  // best effort; potentially loses data due to conversion failures
2492  // (stray surrogate halves) and embedded null characters
2493 }
2494 
2495 } // namespace
2496 
2497 #ifdef RTL_STRING_UNITTEST
2498 namespace rtl
2499 {
2500 typedef rtlunittest::OUString OUString;
2501 }
2502 #endif
2503 
2504 // RTL_USING is defined by gbuild for all modules except those with stable public API
2505 // (as listed in ure/source/README). It allows to use classes like OUString without
2506 // having to explicitly refer to the rtl namespace, which is kind of superfluous
2507 // given that OUString itself is namespaced by its OU prefix.
2508 #ifdef RTL_USING
2509 using ::rtl::OUString;
2510 using ::rtl::OUStringHash;
2513 using ::rtl::OUStringLiteral;
2514 #endif
2515 
2516 #endif /* _RTL_USTRING_HXX */
2517 
2518 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */