Branch libreoffice-5-0-4
[LibreOffice.git] / include / rtl / ustring.hxx
blob80d67e8436777b3bcb13af49140c9c876a7161b9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_RTL_USTRING_HXX
21 #define INCLUDED_RTL_USTRING_HXX
23 #include <sal/config.h>
25 #include <cassert>
26 #include <new>
27 #include <ostream>
28 #include <string.h>
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>
36 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
37 #include <rtl/stringconcat.hxx>
38 #endif
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
49 namespace rtl
52 #ifdef RTL_STRING_UNITTEST
53 #undef rtl
54 #endif
56 #if defined LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
57 /// @cond INTERNAL
59 /**
60 A simple wrapper around string literal. It is usually not necessary to use, can
61 be mostly used to force OUString operator+ working with operands that otherwise would
62 not trigger it.
64 This class is not part of public API and is meant to be used only in LibreOffice code.
65 @since LibreOffice 4.0
67 struct SAL_WARN_UNUSED OUStringLiteral
69 template< int N >
70 explicit SAL_CONSTEXPR OUStringLiteral( const char (&str)[ N ] ) : size( N - 1 ), data( str )
71 { /* only C++14 constexpr: assert( strlen( str ) == N - 1 ); */ }
72 int size;
73 const char* data;
76 /** A simple wrapper around an ASCII character literal, for use in certain
77 OUString functions designed for efficient processing of string literals.
79 @since LibreOffice 5.0
81 template<char C> struct SAL_WARN_UNUSED OUStringLiteral1 {
82 static_assert(
83 static_cast<unsigned char>(C) < 0x80,
84 "non-ASCII character in OUStringLiteral1");
87 /// @endcond
88 #endif
90 /* ======================================================================= */
92 /**
93 This String class provides base functionality for C++ like Unicode
94 character array handling. The advantage of this class is that it
95 handles all the memory management for you - and it does it
96 more efficiently. If you assign a string to another string, the
97 data of both strings are shared (without any copy operation or
98 memory allocation) as long as you do not change the string. This class
99 also stores the length of the string, so that many operations are
100 faster than the C-str-functions.
102 This class provides only readonly string handling. So you could create
103 a string and you could only query the content from this string.
104 It provides also functionality to change the string, but this results
105 in every case in a new string instance (in the most cases with a
106 memory allocation). You don't have functionality to change the
107 content of the string. If you want to change the string content, then
108 you should use the OStringBuffer class, which provides these
109 functionalities and avoids too much memory allocation.
111 The design of this class is similar to the string classes in Java so
112 less people should have understanding problems when they use this class.
115 class SAL_WARN_UNUSED SAL_DLLPUBLIC_RTTI OUString
117 public:
118 /// @cond INTERNAL
119 rtl_uString * pData;
120 /// @endcond
123 New string containing no characters.
125 OUString()
127 pData = 0;
128 rtl_uString_new( &pData );
132 New string from OUString.
134 @param str a OUString.
136 OUString( const OUString & str )
138 pData = str.pData;
139 rtl_uString_acquire( pData );
143 New string from OUString data.
145 @param str a OUString data.
147 OUString( rtl_uString * str )
149 pData = str;
150 rtl_uString_acquire( pData );
153 /** New OUString from OUString data without acquiring it. Takeover of ownership.
155 The SAL_NO_ACQUIRE dummy parameter is only there to distinguish this
156 from other constructors.
158 @param str
159 OUString data
161 inline OUString( rtl_uString * str, __sal_NoAcquire )
162 { pData = str; }
165 New string from a single Unicode character.
167 @param value a Unicode character.
169 explicit OUString( sal_Unicode value )
170 : pData (0)
172 rtl_uString_newFromStr_WithLength( &pData, &value, 1 );
176 New string from a Unicode character buffer array.
178 @param value a NULL-terminated Unicode character array.
180 OUString( const sal_Unicode * value )
182 pData = 0;
183 rtl_uString_newFromStr( &pData, value );
187 New string from a Unicode character buffer array.
189 @param value a Unicode character array.
190 @param length the number of character which should be copied.
191 The character array length must be greater than
192 or equal to this value.
194 OUString( const sal_Unicode * value, sal_Int32 length )
196 pData = 0;
197 rtl_uString_newFromStr_WithLength( &pData, value, length );
201 New string from an 8-Bit string literal that is expected to contain only
202 characters in the ASCII set (i.e. first 128 characters). This constructor
203 allows an efficient and convenient way to create OUString
204 instances from ASCII literals. When creating strings from data that
205 is not pure ASCII, it needs to be converted to OUString by explicitly
206 providing the encoding to use for the conversion.
208 If there are any embedded \0's in the string literal, the result is undefined.
209 Use the overload that explicitly accepts length.
211 @param literal the 8-bit ASCII string literal
213 @since LibreOffice 3.6
215 template< typename T >
216 OUString( T& literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
218 assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
219 pData = 0;
220 if( libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 == 0 ) // empty string
221 rtl_uString_new( &pData );
222 else
223 rtl_uString_newFromLiteral( &pData, literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, 0 );
224 #ifdef RTL_STRING_UNITTEST
225 rtl_string_unittest_const_literal = true;
226 #endif
229 #ifdef RTL_STRING_UNITTEST
231 * Only used by unittests to detect incorrect conversions.
232 * @internal
234 template< typename T >
235 OUString( T&, typename libreoffice_internal::ExceptConstCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
237 pData = 0;
238 rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
239 rtl_string_unittest_invalid_conversion = true;
242 * Only used by unittests to detect incorrect conversions.
243 * @internal
245 template< typename T >
246 OUString( const T&, typename libreoffice_internal::ExceptCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
248 pData = 0;
249 rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
250 rtl_string_unittest_invalid_conversion = true;
252 #endif
254 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
255 /// @cond INTERNAL
257 New string from an 8-Bit string literal that is expected to contain only
258 characters in the ASCII set (i.e. first 128 characters).
260 This constructor is similar to the "direct template" one, but can be
261 useful in cases where the latter does not work, like in
263 OUString(flag ? "a" : "bb")
265 written as
267 OUString(flag ? OUStringLiteral("a") : OUStringLiteral("bb"))
269 @since LibreOffice 5.0
271 OUString(OUStringLiteral literal): pData(0) {
272 rtl_uString_newFromLiteral(&pData, literal.data, literal.size, 0);
274 /// @endcond
275 #endif
278 New string from an 8-Bit character buffer array.
280 @param value An 8-Bit character array.
281 @param length The number of character which should be converted.
282 The 8-Bit character array length must be
283 greater than or equal to this value.
284 @param encoding The text encoding from which the 8-Bit character
285 sequence should be converted.
286 @param convertFlags Flags which control the conversion.
287 see RTL_TEXTTOUNICODE_FLAGS_...
289 @exception std::bad_alloc is thrown if an out-of-memory condition occurs
291 OUString( const sal_Char * value, sal_Int32 length,
292 rtl_TextEncoding encoding,
293 sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS )
295 pData = 0;
296 rtl_string2UString( &pData, value, length, encoding, convertFlags );
297 if (pData == 0) {
298 throw std::bad_alloc();
302 /** Create a new string from an array of Unicode code points.
304 @param codePoints
305 an array of at least codePointCount code points, which each must be in
306 the range from 0 to 0x10FFFF, inclusive. May be null if codePointCount
307 is zero.
309 @param codePointCount
310 the non-negative number of code points.
312 @exception std::bad_alloc
313 is thrown if either an out-of-memory condition occurs or the resulting
314 number of UTF-16 code units would have been larger than SAL_MAX_INT32.
316 @since UDK 3.2.7
318 inline explicit OUString(
319 sal_uInt32 const * codePoints, sal_Int32 codePointCount):
320 pData(NULL)
322 rtl_uString_newFromCodePoints(&pData, codePoints, codePointCount);
323 if (pData == NULL) {
324 throw std::bad_alloc();
328 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
330 @overload
331 @internal
333 template< typename T1, typename T2 >
334 OUString( const OUStringConcat< T1, T2 >& c )
336 const sal_Int32 l = c.length();
337 pData = rtl_uString_alloc( l );
338 if (l != 0)
340 sal_Unicode* end = c.addData( pData->buffer );
341 pData->length = end - pData->buffer;
342 *end = '\0';
343 // TODO realloc in case pData->length is noticeably smaller than l?
346 #endif
349 Release the string data.
351 ~OUString()
353 rtl_uString_release( pData );
356 /** Provides an OUString const & passing a storage pointer of an
357 rtl_uString * handle.
358 It is more convenient to use C++ OUString member functions when dealing
359 with rtl_uString * handles. Using this function avoids unnecessary
360 acquire()/release() calls for a temporary OUString object.
362 @param ppHandle
363 pointer to storage
364 @return
365 OUString const & based on given storage
367 static inline OUString const & unacquired( rtl_uString * const * ppHandle )
368 { return * reinterpret_cast< OUString const * >( ppHandle ); }
371 Assign a new string.
373 @param str a OUString.
375 OUString & operator=( const OUString & str )
377 rtl_uString_assign( &pData, str.pData );
378 return *this;
382 Assign a new string from an 8-Bit string literal that is expected to contain only
383 characters in the ASCII set (i.e. first 128 characters). This operator
384 allows an efficient and convenient way to assign OUString
385 instances from ASCII literals. When assigning strings from data that
386 is not pure ASCII, it needs to be converted to OUString by explicitly
387 providing the encoding to use for the conversion.
389 @param literal the 8-bit ASCII string literal
391 @since LibreOffice 3.6
393 template< typename T >
394 typename libreoffice_internal::ConstCharArrayDetector< T, OUString& >::Type operator=( T& literal )
396 assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
397 if( libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 == 0 ) // empty string
398 rtl_uString_new( &pData );
399 else
400 rtl_uString_newFromLiteral( &pData, literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, 0 );
401 return *this;
404 #if defined LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
405 /// @cond INTERNAL
406 /** Assign a new string from a single ASCII character literal.
408 @since LibreOffice 5.0
410 template<char C> OUString & operator =(OUStringLiteral1<C>) {
411 sal_Unicode const c = C;
412 rtl_uString_newFromStr_WithLength(&pData, &c, 1);
413 return *this;
415 /// @endcond
416 #endif
419 Append a string to this string.
421 @param str a OUString.
423 OUString & operator+=( const OUString & str )
425 rtl_uString_newConcat( &pData, pData, str.pData );
426 return *this;
429 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
431 @overload
432 @internal
434 template< typename T1, typename T2 >
435 OUString& operator+=( const OUStringConcat< T1, T2 >& c )
437 const int l = c.length();
438 if( l == 0 )
439 return *this;
440 rtl_uString_ensureCapacity( &pData, pData->length + l );
441 sal_Unicode* end = c.addData( pData->buffer + pData->length );
442 *end = '\0';
443 pData->length = end - pData->buffer;
444 return *this;
446 #endif
449 Clears the string, i.e, makes a zero-character string
450 @since LibreOffice 4.4
452 void clear()
454 rtl_uString_new( &pData );
458 Returns the length of this string.
460 The length is equal to the number of Unicode characters in this string.
462 @return the length of the sequence of characters represented by this
463 object.
465 sal_Int32 getLength() const { return pData->length; }
468 Checks if a string is empty.
470 @return true if the string is empty;
471 false, otherwise.
473 @since LibreOffice 3.4
475 bool isEmpty() const
477 return pData->length == 0;
481 Returns a pointer to the Unicode character buffer for this string.
483 It isn't necessarily NULL terminated.
485 @return a pointer to the Unicode characters buffer for this object.
487 const sal_Unicode * getStr() const { return pData->buffer; }
490 Access to individual characters.
492 @param index must be non-negative and less than length.
494 @return the character at the given index.
496 @since LibreOffice 3.5
498 sal_Unicode operator [](sal_Int32 index) const {
499 // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
500 assert(index >= 0 && static_cast<sal_uInt32>(index) < static_cast<sal_uInt32>(getLength()));
501 return getStr()[index];
505 Compares two strings.
507 The comparison is based on the numeric value of each character in
508 the strings and return a value indicating their relationship.
509 This function can't be used for language specific sorting.
511 @param str the object to be compared.
512 @return 0 - if both strings are equal
513 < 0 - if this string is less than the string argument
514 > 0 - if this string is greater than the string argument
516 sal_Int32 compareTo( const OUString & str ) const
518 return rtl_ustr_compare_WithLength( pData->buffer, pData->length,
519 str.pData->buffer, str.pData->length );
523 Compares two strings with a maximum count of characters.
525 The comparison is based on the numeric value of each character in
526 the strings and return a value indicating their relationship.
527 This function can't be used for language specific sorting.
529 @param str the object to be compared.
530 @param maxLength the maximum count of characters to be compared.
531 @return 0 - if both strings are equal
532 < 0 - if this string is less than the string argument
533 > 0 - if this string is greater than the string argument
535 @since UDK 3.2.7
537 sal_Int32 compareTo( const OUString & str, sal_Int32 maxLength ) const
539 return rtl_ustr_shortenedCompare_WithLength( pData->buffer, pData->length,
540 str.pData->buffer, str.pData->length, maxLength );
544 Compares two strings in reverse order.
546 The comparison is based on the numeric value of each character in
547 the strings and return a value indicating their relationship.
548 This function can't be used for language specific sorting.
550 @param str the object to be compared.
551 @return 0 - if both strings are equal
552 < 0 - if this string is less than the string argument
553 > 0 - if this string is greater than the string argument
555 sal_Int32 reverseCompareTo( const OUString & str ) const
557 return rtl_ustr_reverseCompare_WithLength( pData->buffer, pData->length,
558 str.pData->buffer, str.pData->length );
562 @overload
563 This function accepts an ASCII string literal as its argument.
564 @since LibreOffice 4.1
566 template< typename T >
567 typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type reverseCompareTo( T& literal ) const
569 assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
570 return rtl_ustr_asciil_reverseCompare_WithLength( pData->buffer, pData->length,
571 literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 );
575 Perform a comparison of two strings.
577 The result is true if and only if second string
578 represents the same sequence of characters as the first string.
579 This function can't be used for language specific comparison.
581 @param str the object to be compared.
582 @return true if the strings are equal;
583 false, otherwise.
585 bool equals( const OUString & str ) const
587 if ( pData->length != str.pData->length )
588 return false;
589 if ( pData == str.pData )
590 return true;
591 return rtl_ustr_reverseCompare_WithLength( pData->buffer, pData->length,
592 str.pData->buffer, str.pData->length ) == 0;
596 Perform a ASCII lowercase comparison of two strings.
598 The result is true if and only if second string
599 represents the same sequence of characters as the first string,
600 ignoring the case.
601 Character values between 65 and 90 (ASCII A-Z) are interpreted as
602 values between 97 and 122 (ASCII a-z).
603 This function can't be used for language specific comparison.
605 @param str the object to be compared.
606 @return true if the strings are equal;
607 false, otherwise.
609 bool equalsIgnoreAsciiCase( const OUString & str ) const
611 if ( pData->length != str.pData->length )
612 return false;
613 if ( pData == str.pData )
614 return true;
615 return rtl_ustr_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
616 str.pData->buffer, str.pData->length ) == 0;
620 Perform a ASCII lowercase comparison of two strings.
622 Compare the two strings with uppercase ASCII
623 character values between 65 and 90 (ASCII A-Z) interpreted as
624 values between 97 and 122 (ASCII a-z).
625 This function can't be used for language specific comparison.
627 @param str the object to be compared.
628 @return 0 - if both strings are equal
629 < 0 - if this string is less than the string argument
630 > 0 - if this string is greater than the string argument
632 @since LibreOffice 4.0
634 sal_Int32 compareToIgnoreAsciiCase( const OUString & str ) const
636 return rtl_ustr_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
637 str.pData->buffer, str.pData->length );
642 @overload
643 This function accepts an ASCII string literal as its argument.
644 @since LibreOffice 3.6
646 template< typename T >
647 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type equalsIgnoreAsciiCase( T& literal ) const
649 assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
650 if ( pData->length != libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 )
651 return false;
653 return rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, literal ) == 0;
657 Match against a substring appearing in this string.
659 The result is true if and only if the second string appears as a substring
660 of this string, at the given position.
661 This function can't be used for language specific comparison.
663 @param str the object (substring) to be compared.
664 @param fromIndex the index to start the comparion from.
665 The index must be greater than or equal to 0
666 and less or equal as the string length.
667 @return true if str match with the characters in the string
668 at the given position;
669 false, otherwise.
671 bool match( const OUString & str, sal_Int32 fromIndex = 0 ) const
673 return rtl_ustr_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
674 str.pData->buffer, str.pData->length, str.pData->length ) == 0;
678 @overload
679 This function accepts an ASCII string literal as its argument.
680 @since LibreOffice 3.6
682 template< typename T >
683 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type match( T& literal, sal_Int32 fromIndex = 0 ) const
685 assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
686 return rtl_ustr_ascii_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
687 literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ) == 0;
691 Match against a substring appearing in this string, ignoring the case of
692 ASCII letters.
694 The result is true if and only if the second string appears as a substring
695 of this string, at the given position.
696 Character values between 65 and 90 (ASCII A-Z) are interpreted as
697 values between 97 and 122 (ASCII a-z).
698 This function can't be used for language specific comparison.
700 @param str the object (substring) to be compared.
701 @param fromIndex the index to start the comparion from.
702 The index must be greater than or equal to 0
703 and less than or equal to the string length.
704 @return true if str match with the characters in the string
705 at the given position;
706 false, otherwise.
708 bool matchIgnoreAsciiCase( const OUString & str, sal_Int32 fromIndex = 0 ) const
710 return rtl_ustr_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
711 str.pData->buffer, str.pData->length,
712 str.pData->length ) == 0;
716 @overload
717 This function accepts an ASCII string literal as its argument.
718 @since LibreOffice 3.6
720 template< typename T >
721 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type matchIgnoreAsciiCase( T& literal, sal_Int32 fromIndex = 0 ) const
723 assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
724 return rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
725 literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ) == 0;
729 Compares two strings.
731 The comparison is based on the numeric value of each character in
732 the strings and return a value indicating their relationship.
733 Since this method is optimized for performance, the ASCII character
734 values are not converted in any way. The caller has to make sure that
735 all ASCII characters are in the allowed range between 0 and
736 127. The ASCII string must be NULL-terminated.
737 This function can't be used for language specific sorting.
739 @param asciiStr the 8-Bit ASCII character string to be compared.
740 @return 0 - if both strings are equal
741 < 0 - if this string is less than the string argument
742 > 0 - if this string is greater than the string argument
744 sal_Int32 compareToAscii( const sal_Char* asciiStr ) const
746 return rtl_ustr_ascii_compare_WithLength( pData->buffer, pData->length, asciiStr );
750 Compares two strings with a maximum count of characters.
752 The comparison is based on the numeric value of each character in
753 the strings and return a value indicating their relationship.
754 Since this method is optimized for performance, the ASCII character
755 values are not converted in any way. The caller has to make sure that
756 all ASCII characters are in the allowed range between 0 and
757 127. The ASCII string must be NULL-terminated.
758 This function can't be used for language specific sorting.
760 @deprecated This is a confusing overload with unexpectedly different
761 semantics from the one-parameter form, so it is marked as deprecated.
762 Practically all uses compare the return value against zero and can thus
763 be replaced with uses of startsWith.
765 @param asciiStr the 8-Bit ASCII character string to be compared.
766 @param maxLength the maximum count of characters to be compared.
767 @return 0 - if both strings are equal
768 < 0 - if this string is less than the string argument
769 > 0 - if this string is greater than the string argument
771 SAL_DEPRECATED(
772 "replace s1.compareToAscii(s2, strlen(s2)) == 0 with s1.startsWith(s2)")
773 sal_Int32 compareToAscii( const sal_Char * asciiStr, sal_Int32 maxLength ) const
775 return rtl_ustr_ascii_shortenedCompare_WithLength( pData->buffer, pData->length,
776 asciiStr, maxLength );
780 Compares two strings in reverse order.
782 This could be useful, if normally both strings start with the same
783 content. The comparison is based on the numeric value of each character
784 in the strings and return a value indicating their relationship.
785 Since this method is optimized for performance, the ASCII character
786 values are not converted in any way. The caller has to make sure that
787 all ASCII characters are in the allowed range between 0 and 127.
788 The ASCII string must be NULL-terminated and must be greater than
789 or equal to asciiStrLength.
790 This function can't be used for language specific sorting.
792 @param asciiStr the 8-Bit ASCII character string to be compared.
793 @param asciiStrLength the length of the ascii string
794 @return 0 - if both strings are equal
795 < 0 - if this string is less than the string argument
796 > 0 - if this string is greater than the string argument
798 sal_Int32 reverseCompareToAsciiL( const sal_Char * asciiStr, sal_Int32 asciiStrLength ) const
800 return rtl_ustr_asciil_reverseCompare_WithLength( pData->buffer, pData->length,
801 asciiStr, asciiStrLength );
805 Perform a comparison of two strings.
807 The result is true if and only if second string
808 represents the same sequence of characters as the first string.
809 Since this method is optimized for performance, the ASCII character
810 values are not converted in any way. The caller has to make sure that
811 all ASCII characters are in the allowed range between 0 and
812 127. The ASCII string must be NULL-terminated.
813 This function can't be used for language specific comparison.
815 @param asciiStr the 8-Bit ASCII character string to be compared.
816 @return true if the strings are equal;
817 false, otherwise.
819 bool equalsAscii( const sal_Char* asciiStr ) const
821 return rtl_ustr_ascii_compare_WithLength( pData->buffer, pData->length,
822 asciiStr ) == 0;
826 Perform a comparison of two strings.
828 The result is true if and only if second string
829 represents the same sequence of characters as the first string.
830 Since this method is optimized for performance, the ASCII character
831 values are not converted in any way. The caller has to make sure that
832 all ASCII characters are in the allowed range between 0 and
833 127. The ASCII string must be NULL-terminated and must be greater than
834 or equal to asciiStrLength.
835 This function can't be used for language specific comparison.
837 @param asciiStr the 8-Bit ASCII character string to be compared.
838 @param asciiStrLength the length of the ascii string
839 @return true if the strings are equal;
840 false, otherwise.
842 bool equalsAsciiL( const sal_Char* asciiStr, sal_Int32 asciiStrLength ) const
844 if ( pData->length != asciiStrLength )
845 return false;
847 return rtl_ustr_asciil_reverseEquals_WithLength(
848 pData->buffer, asciiStr, asciiStrLength );
852 Perform a ASCII lowercase comparison of two strings.
854 The result is true if and only if second string
855 represents the same sequence of characters as the first string,
856 ignoring the case.
857 Character values between 65 and 90 (ASCII A-Z) are interpreted as
858 values between 97 and 122 (ASCII a-z).
859 Since this method is optimized for performance, the ASCII character
860 values are not converted in any way. The caller has to make sure that
861 all ASCII characters are in the allowed range between 0 and
862 127. The ASCII string must be NULL-terminated.
863 This function can't be used for language specific comparison.
865 @param asciiStr the 8-Bit ASCII character string to be compared.
866 @return true if the strings are equal;
867 false, otherwise.
869 bool equalsIgnoreAsciiCaseAscii( const sal_Char * asciiStr ) const
871 return rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, asciiStr ) == 0;
875 Compares two ASCII strings ignoring case
877 The comparison is based on the numeric value of each character in
878 the strings and return a value indicating their relationship.
879 Since this method is optimized for performance, the ASCII character
880 values are not converted in any way. The caller has to make sure that
881 all ASCII characters are in the allowed range between 0 and
882 127. The ASCII string must be NULL-terminated.
883 This function can't be used for language specific sorting.
885 @param asciiStr the 8-Bit ASCII character string to be compared.
886 @return 0 - if both strings are equal
887 < 0 - if this string is less than the string argument
888 > 0 - if this string is greater than the string argument
890 @since LibreOffice 3.5
892 sal_Int32 compareToIgnoreAsciiCaseAscii( const sal_Char * asciiStr ) const
894 return rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, asciiStr );
898 Perform an ASCII lowercase comparison of two strings.
900 The result is true if and only if second string
901 represents the same sequence of characters as the first string,
902 ignoring the case.
903 Character values between 65 and 90 (ASCII A-Z) are interpreted as
904 values between 97 and 122 (ASCII a-z).
905 Since this method is optimized for performance, the ASCII character
906 values are not converted in any way. The caller has to make sure that
907 all ASCII characters are in the allowed range between 0 and 127.
908 The ASCII string must be NULL-terminated and must be greater than
909 or equal to asciiStrLength.
910 This function can't be used for language specific comparison.
912 @param asciiStr the 8-Bit ASCII character string to be compared.
913 @param asciiStrLength the length of the ascii string
914 @return true if the strings are equal;
915 false, otherwise.
917 bool equalsIgnoreAsciiCaseAsciiL( const sal_Char * asciiStr, sal_Int32 asciiStrLength ) const
919 if ( pData->length != asciiStrLength )
920 return false;
922 return rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, asciiStr ) == 0;
926 Match against a substring appearing in this string.
928 The result is true if and only if the second string appears as a substring
929 of this string, at the given position.
930 Since this method is optimized for performance, the ASCII character
931 values are not converted in any way. The caller has to make sure that
932 all ASCII characters are in the allowed range between 0 and
933 127. The ASCII string must be NULL-terminated and must be greater than or
934 equal to asciiStrLength.
935 This function can't be used for language specific comparison.
937 @param asciiStr the object (substring) to be compared.
938 @param asciiStrLength the length of asciiStr.
939 @param fromIndex the index to start the comparion from.
940 The index must be greater than or equal to 0
941 and less than or equal to the string length.
942 @return true if str match with the characters in the string
943 at the given position;
944 false, otherwise.
946 bool matchAsciiL( const sal_Char* asciiStr, sal_Int32 asciiStrLength, sal_Int32 fromIndex = 0 ) const
948 return rtl_ustr_ascii_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
949 asciiStr, asciiStrLength ) == 0;
952 // This overload is left undefined, to detect calls of matchAsciiL that
953 // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
954 // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
955 // platforms):
956 #if SAL_TYPES_SIZEOFLONG == 8
957 void matchAsciiL(char const *, sal_Int32, rtl_TextEncoding) const;
958 #endif
961 Match against a substring appearing in this string, ignoring the case of
962 ASCII letters.
964 The result is true if and only if the second string appears as a substring
965 of this string, at the given position.
966 Character values between 65 and 90 (ASCII A-Z) are interpreted as
967 values between 97 and 122 (ASCII a-z).
968 Since this method is optimized for performance, the ASCII character
969 values are not converted in any way. The caller has to make sure that
970 all ASCII characters are in the allowed range between 0 and
971 127. The ASCII string must be NULL-terminated and must be greater than or
972 equal to asciiStrLength.
973 This function can't be used for language specific comparison.
975 @param asciiStr the 8-Bit ASCII character string to be compared.
976 @param asciiStrLength the length of the ascii string
977 @param fromIndex the index to start the comparion from.
978 The index must be greater than or equal to 0
979 and less than or equal to the string length.
980 @return true if str match with the characters in the string
981 at the given position;
982 false, otherwise.
984 bool matchIgnoreAsciiCaseAsciiL( const sal_Char* asciiStr, sal_Int32 asciiStrLength, sal_Int32 fromIndex = 0 ) const
986 return rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
987 asciiStr, asciiStrLength ) == 0;
990 // This overload is left undefined, to detect calls of
991 // matchIgnoreAsciiCaseAsciiL that erroneously use
992 // RTL_CONSTASCII_USTRINGPARAM instead of RTL_CONSTASCII_STRINGPARAM (but
993 // would lead to ambiguities on 32 bit platforms):
994 #if SAL_TYPES_SIZEOFLONG == 8
995 void matchIgnoreAsciiCaseAsciiL(char const *, sal_Int32, rtl_TextEncoding)
996 const;
997 #endif
1000 Check whether this string starts with a given substring.
1002 @param str the substring to be compared
1004 @param rest if non-null, and this function returns true, then assign a
1005 copy of the remainder of this string to *rest. Available since
1006 LibreOffice 4.2
1008 @return true if and only if the given str appears as a substring at the
1009 start of this string
1011 @since LibreOffice 4.0
1013 bool startsWith(OUString const & str, OUString * rest = 0) const {
1014 bool b = match(str, 0);
1015 if (b && rest != 0) {
1016 *rest = copy(str.getLength());
1018 return b;
1022 @overload
1023 This function accepts an ASCII string literal as its argument.
1024 @since LibreOffice 4.0
1026 template< typename T >
1027 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type startsWith(
1028 T & literal, OUString * rest = 0) const
1030 assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1031 bool b = libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 <= pData->length
1032 && rtl_ustr_asciil_reverseEquals_WithLength( pData->buffer, literal,
1033 libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1);
1034 if (b && rest != 0) {
1035 *rest = copy(libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1);
1037 return b;
1041 Check whether this string starts with a given string, ignoring the case of
1042 ASCII letters.
1044 Character values between 65 and 90 (ASCII A-Z) are interpreted as
1045 values between 97 and 122 (ASCII a-z).
1046 This function can't be used for language specific comparison.
1048 @param str the substring to be compared
1050 @param rest if non-null, and this function returns true, then assign a
1051 copy of the remainder of this string to *rest. Available since
1052 LibreOffice 4.2
1054 @return true if and only if the given str appears as a substring at the
1055 start of this string, ignoring the case of ASCII letters ("A"--"Z" and
1056 "a"--"z")
1058 @since LibreOffice 4.0
1060 bool startsWithIgnoreAsciiCase(OUString const & str, OUString * rest = 0)
1061 const
1063 bool b = matchIgnoreAsciiCase(str, 0);
1064 if (b && rest != 0) {
1065 *rest = copy(str.getLength());
1067 return b;
1071 @overload
1072 This function accepts an ASCII string literal as its argument.
1073 @since LibreOffice 4.0
1075 template< typename T >
1076 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type
1077 startsWithIgnoreAsciiCase(T & literal, OUString * rest = 0) const
1079 assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1080 bool b = (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths(
1081 pData->buffer,
1082 libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, literal,
1083 libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1)
1084 == 0);
1085 if (b && rest != 0) {
1086 *rest = copy(libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1);
1088 return b;
1092 Check whether this string ends with a given substring.
1094 @param str the substring to be compared
1096 @param rest if non-null, and this function returns true, then assign a
1097 copy of the remainder of this string to *rest. Available since
1098 LibreOffice 4.2
1100 @return true if and only if the given str appears as a substring at the
1101 end of this string
1103 @since LibreOffice 3.6
1105 bool endsWith(OUString const & str, OUString * rest = 0) const {
1106 bool b = str.getLength() <= getLength()
1107 && match(str, getLength() - str.getLength());
1108 if (b && rest != 0) {
1109 *rest = copy(0, getLength() - str.getLength());
1111 return b;
1115 @overload
1116 This function accepts an ASCII string literal as its argument.
1117 @since LibreOffice 3.6
1119 template< typename T >
1120 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type
1121 endsWith(T & literal, OUString * rest = 0) const
1123 assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1124 bool b = libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 <= pData->length
1125 && rtl_ustr_asciil_reverseEquals_WithLength(
1126 pData->buffer + pData->length - ( libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ), literal,
1127 libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1);
1128 if (b && rest != 0) {
1129 *rest = copy(
1131 (getLength()
1132 - (libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1)));
1134 return b;
1138 Check whether this string ends with a given ASCII string.
1140 @param asciiStr a sequence of at least asciiStrLength ASCII characters
1141 (bytes in the range 0x00--0x7F)
1142 @param asciiStrLength the length of asciiStr; must be non-negative
1143 @return true if this string ends with asciiStr; otherwise, false is
1144 returned
1146 @since UDK 3.2.7
1148 inline bool endsWithAsciiL(char const * asciiStr, sal_Int32 asciiStrLength)
1149 const
1151 return asciiStrLength <= pData->length
1152 && rtl_ustr_asciil_reverseEquals_WithLength(
1153 pData->buffer + pData->length - asciiStrLength, asciiStr,
1154 asciiStrLength);
1158 Check whether this string ends with a given string, ignoring the case of
1159 ASCII letters.
1161 Character values between 65 and 90 (ASCII A-Z) are interpreted as
1162 values between 97 and 122 (ASCII a-z).
1163 This function can't be used for language specific comparison.
1165 @param str the substring to be compared
1167 @param rest if non-null, and this function returns true, then assign a
1168 copy of the remainder of this string to *rest. Available since
1169 LibreOffice 4.2
1171 @return true if and only if the given str appears as a substring at the
1172 end of this string, ignoring the case of ASCII letters ("A"--"Z" and
1173 "a"--"z")
1175 @since LibreOffice 3.6
1177 bool endsWithIgnoreAsciiCase(OUString const & str, OUString * rest = 0) const
1179 bool b = str.getLength() <= getLength()
1180 && matchIgnoreAsciiCase(str, getLength() - str.getLength());
1181 if (b && rest != 0) {
1182 *rest = copy(0, getLength() - str.getLength());
1184 return b;
1188 @overload
1189 This function accepts an ASCII string literal as its argument.
1190 @since LibreOffice 3.6
1192 template< typename T >
1193 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type
1194 endsWithIgnoreAsciiCase(T & literal, OUString * rest = 0) const
1196 assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1197 bool b = libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 <= pData->length
1198 && (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths(
1199 pData->buffer + pData->length - ( libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ),
1200 libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, literal,
1201 libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1)
1202 == 0);
1203 if (b && rest != 0) {
1204 *rest = copy(
1206 (getLength()
1207 - (libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1)));
1209 return b;
1213 Check whether this string ends with a given ASCII string, ignoring the
1214 case of ASCII letters.
1216 @param asciiStr a sequence of at least asciiStrLength ASCII characters
1217 (bytes in the range 0x00--0x7F)
1218 @param asciiStrLength the length of asciiStr; must be non-negative
1219 @return true if this string ends with asciiStr, ignoring the case of ASCII
1220 letters ("A"--"Z" and "a"--"z"); otherwise, false is returned
1222 inline bool endsWithIgnoreAsciiCaseAsciiL(
1223 char const * asciiStr, sal_Int32 asciiStrLength) const
1225 return asciiStrLength <= pData->length
1226 && (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths(
1227 pData->buffer + pData->length - asciiStrLength,
1228 asciiStrLength, asciiStr, asciiStrLength)
1229 == 0);
1232 friend bool operator == ( const OUString& rStr1, const OUString& rStr2 )
1233 { return rStr1.equals(rStr2); }
1234 friend bool operator == ( const OUString& rStr1, const sal_Unicode * pStr2 )
1235 { return rStr1.compareTo( pStr2 ) == 0; }
1236 friend bool operator == ( const sal_Unicode * pStr1, const OUString& rStr2 )
1237 { return OUString( pStr1 ).compareTo( rStr2 ) == 0; }
1239 friend bool operator != ( const OUString& rStr1, const OUString& rStr2 )
1240 { return !(operator == ( rStr1, rStr2 )); }
1241 friend bool operator != ( const OUString& rStr1, const sal_Unicode * pStr2 )
1242 { return !(operator == ( rStr1, pStr2 )); }
1243 friend bool operator != ( const sal_Unicode * pStr1, const OUString& rStr2 )
1244 { return !(operator == ( pStr1, rStr2 )); }
1246 friend bool operator < ( const OUString& rStr1, const OUString& rStr2 )
1247 { return rStr1.compareTo( rStr2 ) < 0; }
1248 friend bool operator > ( const OUString& rStr1, const OUString& rStr2 )
1249 { return rStr1.compareTo( rStr2 ) > 0; }
1250 friend bool operator <= ( const OUString& rStr1, const OUString& rStr2 )
1251 { return rStr1.compareTo( rStr2 ) <= 0; }
1252 friend bool operator >= ( const OUString& rStr1, const OUString& rStr2 )
1253 { return rStr1.compareTo( rStr2 ) >= 0; }
1256 * Compare string to an ASCII string literal.
1258 * This operator is equal to calling equalsAsciiL().
1260 * @since LibreOffice 3.6
1262 template< typename T >
1263 friend inline typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator==( const OUString& string, T& literal )
1265 assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1266 return string.equalsAsciiL( literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 );
1269 * Compare string to an ASCII string literal.
1271 * This operator is equal to calling equalsAsciiL().
1273 * @since LibreOffice 3.6
1275 template< typename T >
1276 friend inline typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator==( T& literal, const OUString& string )
1278 assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1279 return string.equalsAsciiL( literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 );
1282 * Compare string to an ASCII string literal.
1284 * This operator is equal to calling !equalsAsciiL().
1286 * @since LibreOffice 3.6
1288 template< typename T >
1289 friend inline typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator!=( const OUString& string, T& literal )
1291 assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1292 return !string.equalsAsciiL( literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 );
1295 * Compare string to an ASCII string literal.
1297 * This operator is equal to calling !equalsAsciiL().
1299 * @since LibreOffice 3.6
1301 template< typename T >
1302 friend inline typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator!=( T& literal, const OUString& string )
1304 assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1305 return !string.equalsAsciiL( literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 );
1308 #if defined LIBO_INTERNAL_ONLY
1309 /// @cond INTERNAL
1311 /* Comparison between OUString and OUStringLiteral.
1313 @since LibreOffice 5.0
1316 friend bool operator ==(OUString const & lhs, OUStringLiteral const & rhs) {
1317 return lhs.equalsAsciiL(rhs.data, rhs.size);
1320 friend bool operator !=(OUString const & lhs, OUStringLiteral const & rhs) {
1321 return !lhs.equalsAsciiL(rhs.data, rhs.size);
1324 friend bool operator <(OUString const & lhs, OUStringLiteral const & rhs) {
1325 return
1326 (rtl_ustr_ascii_compare_WithLength(
1327 lhs.pData->buffer, lhs.pData->length, rhs.data))
1328 < 0;
1331 friend bool operator <=(OUString const & lhs, OUStringLiteral const & rhs) {
1332 return
1333 (rtl_ustr_ascii_compare_WithLength(
1334 lhs.pData->buffer, lhs.pData->length, rhs.data))
1335 <= 0;
1338 friend bool operator >(OUString const & lhs, OUStringLiteral const & rhs) {
1339 return
1340 (rtl_ustr_ascii_compare_WithLength(
1341 lhs.pData->buffer, lhs.pData->length, rhs.data))
1342 > 0;
1345 friend bool operator >=(OUString const & lhs, OUStringLiteral const & rhs) {
1346 return
1347 (rtl_ustr_ascii_compare_WithLength(
1348 lhs.pData->buffer, lhs.pData->length, rhs.data))
1349 >= 0;
1352 friend bool operator ==(OUStringLiteral const & lhs, OUString const & rhs) {
1353 return rhs.equalsAsciiL(lhs.data, lhs.size);
1356 friend bool operator !=(OUStringLiteral const & lhs, OUString const & rhs) {
1357 return !rhs.equalsAsciiL(lhs.data, lhs.size);
1360 friend bool operator <(OUStringLiteral const & lhs, OUString const & rhs) {
1361 return
1362 (rtl_ustr_ascii_compare_WithLength(
1363 rhs.pData->buffer, rhs.pData->length, lhs.data))
1364 >= 0;
1367 friend bool operator <=(OUStringLiteral const & lhs, OUString const & rhs) {
1368 return
1369 (rtl_ustr_ascii_compare_WithLength(
1370 rhs.pData->buffer, rhs.pData->length, lhs.data))
1371 > 0;
1374 friend bool operator >(OUStringLiteral const & lhs, OUString const & rhs) {
1375 return
1376 (rtl_ustr_ascii_compare_WithLength(
1377 rhs.pData->buffer, rhs.pData->length, lhs.data))
1378 <= 0;
1381 friend bool operator >=(OUStringLiteral const & lhs, OUString const & rhs) {
1382 return
1383 (rtl_ustr_ascii_compare_WithLength(
1384 rhs.pData->buffer, rhs.pData->length, lhs.data))
1385 < 0;
1388 /// @endcond
1389 #endif
1392 Returns a hashcode for this string.
1394 @return a hash code value for this object.
1396 @see rtl::OUStringHash for convenient use of std::unordered_map
1398 sal_Int32 hashCode() const
1400 return rtl_ustr_hashCode_WithLength( pData->buffer, pData->length );
1404 Returns the index within this string of the first occurrence of the
1405 specified character, starting the search at the specified index.
1407 @param ch character to be located.
1408 @param fromIndex the index to start the search from.
1409 The index must be greater than or equal to 0
1410 and less than or equal to the string length.
1411 @return the index of the first occurrence of the character in the
1412 character sequence represented by this string that is
1413 greater than or equal to fromIndex, or
1414 -1 if the character does not occur.
1416 sal_Int32 indexOf( sal_Unicode ch, sal_Int32 fromIndex = 0 ) const
1418 sal_Int32 ret = rtl_ustr_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch );
1419 return (ret < 0 ? ret : ret+fromIndex);
1423 Returns the index within this string of the last occurrence of the
1424 specified character, searching backward starting at the end.
1426 @param ch character to be located.
1427 @return the index of the last occurrence of the character in the
1428 character sequence represented by this string, or
1429 -1 if the character does not occur.
1431 sal_Int32 lastIndexOf( sal_Unicode ch ) const
1433 return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch );
1437 Returns the index within this string of the last occurrence of the
1438 specified character, searching backward starting before the specified
1439 index.
1441 @param ch character to be located.
1442 @param fromIndex the index before which to start the search.
1443 @return the index of the last occurrence of the character in the
1444 character sequence represented by this string that
1445 is less than fromIndex, or -1
1446 if the character does not occur before that point.
1448 sal_Int32 lastIndexOf( sal_Unicode ch, sal_Int32 fromIndex ) const
1450 return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch );
1454 Returns the index within this string of the first occurrence of the
1455 specified substring, starting at the specified index.
1457 If str doesn't include any character, always -1 is
1458 returned. This is also the case, if both strings are empty.
1460 @param str the substring to search for.
1461 @param fromIndex the index to start the search from.
1462 @return If the string argument occurs one or more times as a substring
1463 within this string at the starting index, then the index
1464 of the first character of the first such substring is
1465 returned. If it does not occur as a substring starting
1466 at fromIndex or beyond, -1 is returned.
1468 sal_Int32 indexOf( const OUString & str, sal_Int32 fromIndex = 0 ) const
1470 sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1471 str.pData->buffer, str.pData->length );
1472 return (ret < 0 ? ret : ret+fromIndex);
1476 @overload
1477 This function accepts an ASCII string literal as its argument.
1478 @since LibreOffice 3.6
1480 template< typename T >
1481 typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const
1483 assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1484 sal_Int32 ret = rtl_ustr_indexOfAscii_WithLength(
1485 pData->buffer + fromIndex, pData->length - fromIndex, literal,
1486 libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1);
1487 return ret < 0 ? ret : ret + fromIndex;
1491 Returns the index within this string of the first occurrence of the
1492 specified ASCII substring, starting at the specified index.
1494 @param str
1495 the substring to be searched for. Need not be null-terminated, but must
1496 be at least as long as the specified len. Must only contain characters
1497 in the ASCII range 0x00--7F.
1499 @param len
1500 the length of the substring; must be non-negative.
1502 @param fromIndex
1503 the index to start the search from. Must be in the range from zero to
1504 the length of this string, inclusive.
1506 @return
1507 the index (starting at 0) of the first character of the first occurrence
1508 of the substring within this string starting at the given fromIndex, or
1509 -1 if the substring does not occur. If len is zero, -1 is returned.
1511 @since UDK 3.2.7
1513 sal_Int32 indexOfAsciiL(
1514 char const * str, sal_Int32 len, sal_Int32 fromIndex = 0) const
1516 sal_Int32 ret = rtl_ustr_indexOfAscii_WithLength(
1517 pData->buffer + fromIndex, pData->length - fromIndex, str, len);
1518 return ret < 0 ? ret : ret + fromIndex;
1521 // This overload is left undefined, to detect calls of indexOfAsciiL that
1522 // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
1523 // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
1524 // platforms):
1525 #if SAL_TYPES_SIZEOFLONG == 8
1526 void indexOfAsciiL(char const *, sal_Int32 len, rtl_TextEncoding) const;
1527 #endif
1530 Returns the index within this string of the last occurrence of
1531 the specified substring, searching backward starting at the end.
1533 The returned index indicates the starting index of the substring
1534 in this string.
1535 If str doesn't include any character, always -1 is
1536 returned. This is also the case, if both strings are empty.
1538 @param str the substring to search for.
1539 @return If the string argument occurs one or more times as a substring
1540 within this string, then the index of the first character of
1541 the last such substring is returned. If it does not occur as
1542 a substring, -1 is returned.
1544 sal_Int32 lastIndexOf( const OUString & str ) const
1546 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1547 str.pData->buffer, str.pData->length );
1551 Returns the index within this string of the last occurrence of
1552 the specified substring, searching backward starting before the specified
1553 index.
1555 The returned index indicates the starting index of the substring
1556 in this string.
1557 If str doesn't include any character, always -1 is
1558 returned. This is also the case, if both strings are empty.
1560 @param str the substring to search for.
1561 @param fromIndex the index before which to start the search.
1562 @return If the string argument occurs one or more times as a substring
1563 within this string before the starting index, then the index
1564 of the first character of the last such substring is
1565 returned. Otherwise, -1 is returned.
1567 sal_Int32 lastIndexOf( const OUString & str, sal_Int32 fromIndex ) const
1569 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1570 str.pData->buffer, str.pData->length );
1574 @overload
1575 This function accepts an ASCII string literal as its argument.
1576 @since LibreOffice 3.6
1578 template< typename T >
1579 typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type lastIndexOf( T& literal ) const
1581 assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1582 return rtl_ustr_lastIndexOfAscii_WithLength(
1583 pData->buffer, pData->length, literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1);
1587 Returns the index within this string of the last occurrence of the
1588 specified ASCII substring.
1590 @param str
1591 the substring to be searched for. Need not be null-terminated, but must
1592 be at least as long as the specified len. Must only contain characters
1593 in the ASCII range 0x00--7F.
1595 @param len
1596 the length of the substring; must be non-negative.
1598 @return
1599 the index (starting at 0) of the first character of the last occurrence
1600 of the substring within this string, or -1 if the substring does not
1601 occur. If len is zero, -1 is returned.
1603 @since UDK 3.2.7
1605 sal_Int32 lastIndexOfAsciiL(char const * str, sal_Int32 len) const
1607 return rtl_ustr_lastIndexOfAscii_WithLength(
1608 pData->buffer, pData->length, str, len);
1612 Returns a new string that is a substring of this string.
1614 The substring begins at the specified beginIndex. If
1615 beginIndex is negative or be greater than the length of
1616 this string, behaviour is undefined.
1618 @param beginIndex the beginning index, inclusive.
1619 @return the specified substring.
1621 SAL_WARN_UNUSED_RESULT OUString copy( sal_Int32 beginIndex ) const
1623 rtl_uString *pNew = 0;
1624 rtl_uString_newFromSubString( &pNew, pData, beginIndex, getLength() - beginIndex );
1625 return OUString( pNew, SAL_NO_ACQUIRE );
1629 Returns a new string that is a substring of this string.
1631 The substring begins at the specified beginIndex and contains count
1632 characters. If either beginIndex or count are negative,
1633 or beginIndex + count are greater than the length of this string
1634 then behaviour is undefined.
1636 @param beginIndex the beginning index, inclusive.
1637 @param count the number of characters.
1638 @return the specified substring.
1640 SAL_WARN_UNUSED_RESULT OUString copy( sal_Int32 beginIndex, sal_Int32 count ) const
1642 rtl_uString *pNew = 0;
1643 rtl_uString_newFromSubString( &pNew, pData, beginIndex, count );
1644 return OUString( pNew, SAL_NO_ACQUIRE );
1648 Concatenates the specified string to the end of this string.
1650 @param str the string that is concatenated to the end
1651 of this string.
1652 @return a string that represents the concatenation of this string
1653 followed by the string argument.
1655 SAL_WARN_UNUSED_RESULT OUString concat( const OUString & str ) const
1657 rtl_uString* pNew = 0;
1658 rtl_uString_newConcat( &pNew, pData, str.pData );
1659 return OUString( pNew, SAL_NO_ACQUIRE );
1662 #ifndef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
1663 friend OUString operator+( const OUString& rStr1, const OUString& rStr2 )
1665 return rStr1.concat( rStr2 );
1667 #endif
1670 Returns a new string resulting from replacing n = count characters
1671 from position index in this string with newStr.
1673 @param index the replacing index in str.
1674 The index must be greater than or equal to 0 and
1675 less than or equal to the length of the string.
1676 @param count the count of characters that will be replaced
1677 The count must be greater than or equal to 0 and
1678 less than or equal to the length of the string minus index.
1679 @param newStr the new substring.
1680 @return the new string.
1682 SAL_WARN_UNUSED_RESULT OUString replaceAt( sal_Int32 index, sal_Int32 count, const OUString& newStr ) const
1684 rtl_uString* pNew = 0;
1685 rtl_uString_newReplaceStrAt( &pNew, pData, index, count, newStr.pData );
1686 return OUString( pNew, SAL_NO_ACQUIRE );
1690 Returns a new string resulting from replacing all occurrences of
1691 oldChar in this string with newChar.
1693 If the character oldChar does not occur in the character sequence
1694 represented by this object, then the string is assigned with
1695 str.
1697 @param oldChar the old character.
1698 @param newChar the new character.
1699 @return a string derived from this string by replacing every
1700 occurrence of oldChar with newChar.
1702 SAL_WARN_UNUSED_RESULT OUString replace( sal_Unicode oldChar, sal_Unicode newChar ) const
1704 rtl_uString* pNew = 0;
1705 rtl_uString_newReplace( &pNew, pData, oldChar, newChar );
1706 return OUString( pNew, SAL_NO_ACQUIRE );
1710 Returns a new string resulting from replacing the first occurrence of a
1711 given substring with another substring.
1713 @param from the substring to be replaced
1715 @param to the replacing substring
1717 @param[in,out] index pointer to a start index; if the pointer is
1718 non-null: upon entry to the function, its value is the index into this
1719 string at which to start searching for the \p from substring, the value
1720 must be non-negative and not greater than this string's length; upon exiting
1721 the function its value is the index into this string at which the
1722 replacement took place or -1 if no replacement took place; if the pointer
1723 is null, searching always starts at index 0
1725 @since LibreOffice 3.6
1727 SAL_WARN_UNUSED_RESULT OUString replaceFirst(
1728 OUString const & from, OUString const & to, sal_Int32 * index = 0) const
1730 rtl_uString * s = 0;
1731 sal_Int32 i = 0;
1732 rtl_uString_newReplaceFirst(
1733 &s, pData, from.pData, to.pData, index == 0 ? &i : index);
1734 return OUString(s, SAL_NO_ACQUIRE);
1738 Returns a new string resulting from replacing the first occurrence of a
1739 given substring with another substring.
1741 @param from ASCII string literal, the substring to be replaced
1743 @param to the replacing substring
1745 @param[in,out] index pointer to a start index; if the pointer is
1746 non-null: upon entry to the function, its value is the index into the this
1747 string at which to start searching for the \p from substring, the value
1748 must be non-negative and not greater than this string's length; upon exiting
1749 the function its value is the index into this string at which the
1750 replacement took place or -1 if no replacement took place; if the pointer
1751 is null, searching always starts at index 0
1753 @since LibreOffice 3.6
1755 template< typename T >
1756 SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T, OUString >::Type replaceFirst( T& from, OUString const & to,
1757 sal_Int32 * index = 0) const
1759 rtl_uString * s = 0;
1760 sal_Int32 i = 0;
1761 assert( strlen( from ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1762 rtl_uString_newReplaceFirstAsciiL(
1763 &s, pData, from, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, to.pData, index == 0 ? &i : index);
1764 return OUString(s, SAL_NO_ACQUIRE);
1768 Returns a new string resulting from replacing the first occurrence of a
1769 given substring with another substring.
1771 @param from ASCII string literal, the substring to be replaced
1773 @param to ASCII string literal, the substring to be replaced
1775 @param[in,out] index pointer to a start index; if the pointer is
1776 non-null: upon entry to the function, its value is the index into the this
1777 string at which to start searching for the \p from substring, the value
1778 must be non-negative and not greater than this string's length; upon exiting
1779 the function its value is the index into this string at which the
1780 replacement took place or -1 if no replacement took place; if the pointer
1781 is null, searching always starts at index 0
1783 @since LibreOffice 3.6
1785 template< typename T1, typename T2 >
1786 SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T1, typename libreoffice_internal::ConstCharArrayDetector< T2, OUString >::Type >::Type
1787 replaceFirst( T1& from, T2& to, sal_Int32 * index = 0) const
1789 rtl_uString * s = 0;
1790 sal_Int32 i = 0;
1791 assert( strlen( from ) == libreoffice_internal::ConstCharArrayDetector< T1 >::size - 1 );
1792 assert( strlen( to ) == libreoffice_internal::ConstCharArrayDetector< T2 >::size - 1 );
1793 rtl_uString_newReplaceFirstAsciiLAsciiL(
1794 &s, pData, from, libreoffice_internal::ConstCharArrayDetector< T1, void >::size - 1, to,
1795 libreoffice_internal::ConstCharArrayDetector< T2, void >::size - 1, index == 0 ? &i : index);
1796 return OUString(s, SAL_NO_ACQUIRE);
1800 Returns a new string resulting from replacing all occurrences of a given
1801 substring with another substring.
1803 Replacing subsequent occurrences picks up only after a given replacement.
1804 That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
1806 @param from the substring to be replaced
1808 @param to the replacing substring
1810 @param fromIndex the position in the string where we will begin searching
1812 @since LibreOffice 4.0
1814 SAL_WARN_UNUSED_RESULT OUString replaceAll(
1815 OUString const & from, OUString const & to, sal_Int32 fromIndex = 0) const
1817 rtl_uString * s = 0;
1818 rtl_uString_newReplaceAllFromIndex(&s, pData, from.pData, to.pData, fromIndex);
1819 return OUString(s, SAL_NO_ACQUIRE);
1823 Returns a new string resulting from replacing all occurrences of a given
1824 substring with another substring.
1826 Replacing subsequent occurrences picks up only after a given replacement.
1827 That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
1829 @param from ASCII string literal, the substring to be replaced
1831 @param to the replacing substring
1833 @since LibreOffice 3.6
1835 template< typename T >
1836 SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T, OUString >::Type replaceAll( T& from, OUString const & to) const
1838 rtl_uString * s = 0;
1839 assert( strlen( from ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 );
1840 rtl_uString_newReplaceAllAsciiL(&s, pData, from, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, to.pData);
1841 return OUString(s, SAL_NO_ACQUIRE);
1845 Returns a new string resulting from replacing all occurrences of a given
1846 substring with another substring.
1848 Replacing subsequent occurrences picks up only after a given replacement.
1849 That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
1851 @param from ASCII string literal, the substring to be replaced
1853 @param to ASCII string literal, the substring to be replaced
1855 @since LibreOffice 3.6
1857 template< typename T1, typename T2 >
1858 SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T1, typename libreoffice_internal::ConstCharArrayDetector< T2, OUString >::Type >::Type
1859 replaceAll( T1& from, T2& to ) const
1861 rtl_uString * s = 0;
1862 assert( strlen( from ) == libreoffice_internal::ConstCharArrayDetector< T1 >::size - 1 );
1863 assert( strlen( to ) == libreoffice_internal::ConstCharArrayDetector< T2 >::size - 1 );
1864 rtl_uString_newReplaceAllAsciiLAsciiL(
1865 &s, pData, from, libreoffice_internal::ConstCharArrayDetector< T1, void >::size - 1,
1866 to, libreoffice_internal::ConstCharArrayDetector< T2, void >::size - 1);
1867 return OUString(s, SAL_NO_ACQUIRE);
1871 Converts from this string all ASCII uppercase characters (65-90)
1872 to ASCII lowercase characters (97-122).
1874 This function can't be used for language specific conversion.
1875 If the string doesn't contain characters which must be converted,
1876 then the new string is assigned with str.
1878 @return the string, converted to ASCII lowercase.
1880 SAL_WARN_UNUSED_RESULT OUString toAsciiLowerCase() const
1882 rtl_uString* pNew = 0;
1883 rtl_uString_newToAsciiLowerCase( &pNew, pData );
1884 return OUString( pNew, SAL_NO_ACQUIRE );
1888 Converts from this string all ASCII lowercase characters (97-122)
1889 to ASCII uppercase characters (65-90).
1891 This function can't be used for language specific conversion.
1892 If the string doesn't contain characters which must be converted,
1893 then the new string is assigned with str.
1895 @return the string, converted to ASCII uppercase.
1897 SAL_WARN_UNUSED_RESULT OUString toAsciiUpperCase() const
1899 rtl_uString* pNew = 0;
1900 rtl_uString_newToAsciiUpperCase( &pNew, pData );
1901 return OUString( pNew, SAL_NO_ACQUIRE );
1905 Returns a new string resulting from removing white space from both ends
1906 of the string.
1908 All characters that have codes less than or equal to
1909 32 (the space character) are considered to be white space.
1910 If the string doesn't contain white spaces at both ends,
1911 then the new string is assigned with str.
1913 @return the string, with white space removed from the front and end.
1915 SAL_WARN_UNUSED_RESULT OUString trim() const
1917 rtl_uString* pNew = 0;
1918 rtl_uString_newTrim( &pNew, pData );
1919 return OUString( pNew, SAL_NO_ACQUIRE );
1923 Returns a token in the string.
1925 Example:
1926 sal_Int32 nIndex = 0;
1930 OUString aToken = aStr.getToken( 0, ';', nIndex );
1933 while ( nIndex >= 0 );
1935 @param token the number of the token to return
1936 @param cTok the character which separate the tokens.
1937 @param index the position at which the token is searched in the
1938 string.
1939 The index must not be greater than the length of the
1940 string.
1941 This param is set to the position of the
1942 next token or to -1, if it is the last token.
1943 @return the token; if either token or index is negative, an empty token
1944 is returned (and index is set to -1)
1946 OUString getToken( sal_Int32 token, sal_Unicode cTok, sal_Int32& index ) const
1948 rtl_uString * pNew = 0;
1949 index = rtl_uString_getToken( &pNew, pData, token, cTok, index );
1950 return OUString( pNew, SAL_NO_ACQUIRE );
1954 Returns a token from the string.
1956 The same as getToken(sal_Int32, sal_Unicode, sal_Int32 &), but always
1957 passing in 0 as the start index in the third argument.
1959 @param count the number of the token to return, starting with 0
1960 @param separator the character which separates the tokens
1962 @return the given token, or an empty string
1964 @since LibreOffice 3.6
1966 OUString getToken(sal_Int32 count, sal_Unicode separator) const {
1967 sal_Int32 n = 0;
1968 return getToken(count, separator, n);
1972 Returns the Boolean value from this string.
1974 This function can't be used for language specific conversion.
1976 @return true, if the string is 1 or "True" in any ASCII case.
1977 false in any other case.
1979 bool toBoolean() const
1981 return rtl_ustr_toBoolean( pData->buffer );
1985 Returns the first character from this string.
1987 @return the first character from this string or 0, if this string
1988 is emptry.
1990 sal_Unicode toChar() const
1992 return pData->buffer[0];
1996 Returns the int32 value from this string.
1998 This function can't be used for language specific conversion.
2000 @param radix the radix (between 2 and 36)
2001 @return the int32 represented from this string.
2002 0 if this string represents no number or one of too large
2003 magnitude.
2005 sal_Int32 toInt32( sal_Int16 radix = 10 ) const
2007 return rtl_ustr_toInt32( pData->buffer, radix );
2011 Returns the uint32 value from this string.
2013 This function can't be used for language specific conversion.
2015 @param radix the radix (between 2 and 36)
2016 @return the uint32 represented from this string.
2017 0 if this string represents no number or one of too large
2018 magnitude.
2020 @since LibreOffice 4.2
2022 sal_uInt32 toUInt32( sal_Int16 radix = 10 ) const
2024 return rtl_ustr_toUInt32( pData->buffer, radix );
2028 Returns the int64 value from this string.
2030 This function can't be used for language specific conversion.
2032 @param radix the radix (between 2 and 36)
2033 @return the int64 represented from this string.
2034 0 if this string represents no number or one of too large
2035 magnitude.
2037 sal_Int64 toInt64( sal_Int16 radix = 10 ) const
2039 return rtl_ustr_toInt64( pData->buffer, radix );
2043 Returns the uint64 value from this string.
2045 This function can't be used for language specific conversion.
2047 @param radix the radix (between 2 and 36)
2048 @return the uint64 represented from this string.
2049 0 if this string represents no number or one of too large
2050 magnitude.
2052 @since LibreOffice 4.1
2054 sal_uInt64 toUInt64( sal_Int16 radix = 10 ) const
2056 return rtl_ustr_toUInt64( pData->buffer, radix );
2060 Returns the float value from this string.
2062 This function can't be used for language specific conversion.
2064 @return the float represented from this string.
2065 0.0 if this string represents no number.
2067 float toFloat() const
2069 return rtl_ustr_toFloat( pData->buffer );
2073 Returns the double value from this string.
2075 This function can't be used for language specific conversion.
2077 @return the double represented from this string.
2078 0.0 if this string represents no number.
2080 double toDouble() const
2082 return rtl_ustr_toDouble( pData->buffer );
2087 Return a canonical representation for a string.
2089 A pool of strings, initially empty is maintained privately
2090 by the string class. On invocation, if present in the pool
2091 the original string will be returned. Otherwise this string,
2092 or a copy thereof will be added to the pool and returned.
2094 @return
2095 a version of the string from the pool.
2097 @exception std::bad_alloc is thrown if an out-of-memory condition occurs
2099 @since UDK 3.2.7
2101 OUString intern() const
2103 rtl_uString * pNew = 0;
2104 rtl_uString_intern( &pNew, pData );
2105 if (pNew == 0) {
2106 throw std::bad_alloc();
2108 return OUString( pNew, SAL_NO_ACQUIRE );
2112 Return a canonical representation for a converted string.
2114 A pool of strings, initially empty is maintained privately
2115 by the string class. On invocation, if present in the pool
2116 the original string will be returned. Otherwise this string,
2117 or a copy thereof will be added to the pool and returned.
2119 @param value a 8-Bit character array.
2120 @param length the number of character which should be converted.
2121 The 8-Bit character array length must be
2122 greater than or equal to this value.
2123 @param encoding the text encoding from which the 8-Bit character
2124 sequence should be converted.
2125 @param convertFlags flags which controls the conversion.
2126 see RTL_TEXTTOUNICODE_FLAGS_...
2127 @param pInfo pointer to return conversion status or NULL.
2129 @return
2130 a version of the converted string from the pool.
2132 @exception std::bad_alloc is thrown if an out-of-memory condition occurs
2134 @since UDK 3.2.7
2136 static OUString intern( const sal_Char * value, sal_Int32 length,
2137 rtl_TextEncoding encoding,
2138 sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS,
2139 sal_uInt32 *pInfo = NULL )
2141 rtl_uString * pNew = 0;
2142 rtl_uString_internConvert( &pNew, value, length, encoding,
2143 convertFlags, pInfo );
2144 if (pNew == 0) {
2145 throw std::bad_alloc();
2147 return OUString( pNew, SAL_NO_ACQUIRE );
2151 Converts to an OString, signalling failure.
2153 @param pTarget
2154 An out parameter receiving the converted OString. Must not be null; the
2155 contents are not modified if conversion fails (convertToOString returns
2156 false).
2158 @param nEncoding
2159 The text encoding to convert into. Must be an octet encoding (i.e.,
2160 rtl_isOctetTextEncoding(nEncoding) must return true).
2162 @param nFlags
2163 A combination of RTL_UNICODETOTEXT_FLAGS that detail how to do the
2164 conversion (see rtl_convertUnicodeToText). RTL_UNICODETOTEXT_FLAGS_FLUSH
2165 need not be included, it is implicitly assumed. Typical uses are either
2166 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
2167 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR (fail if a Unicode character cannot
2168 be converted to the target nEncoding) or OUSTRING_TO_OSTRING_CVTFLAGS
2169 (make a best efforts conversion).
2171 @return
2172 True if the conversion succeeded, false otherwise.
2174 inline bool convertToString(OString * pTarget, rtl_TextEncoding nEncoding,
2175 sal_uInt32 nFlags) const
2177 return rtl_convertUStringToString(&pTarget->pData, pData->buffer,
2178 pData->length, nEncoding, nFlags);
2181 /** Iterate through this string based on code points instead of UTF-16 code
2182 units.
2184 See Chapter 3 of The Unicode Standard 5.0 (Addison--Wesley, 2006) for
2185 definitions of the various terms used in this description.
2187 This string is interpreted as a sequence of zero or more UTF-16 code
2188 units. For each index into this sequence (from zero to one less than
2189 the length of the sequence, inclusive), a code point represented
2190 starting at the given index is computed as follows:
2192 - If the UTF-16 code unit addressed by the index constitutes a
2193 well-formed UTF-16 code unit sequence, the computed code point is the
2194 scalar value encoded by that UTF-16 code unit sequence.
2196 - Otherwise, if the index is at least two UTF-16 code units away from
2197 the end of the sequence, and the sequence of two UTF-16 code units
2198 addressed by the index constitutes a well-formed UTF-16 code unit
2199 sequence, the computed code point is the scalar value encoded by that
2200 UTF-16 code unit sequence.
2202 - Otherwise, the computed code point is the UTF-16 code unit addressed
2203 by the index. (This last case catches unmatched surrogates as well as
2204 indices pointing into the middle of surrogate pairs.)
2206 @param indexUtf16
2207 pointer to a UTF-16 based index into this string; must not be null. On
2208 entry, the index must be in the range from zero to the length of this
2209 string (in UTF-16 code units), inclusive. Upon successful return, the
2210 index will be updated to address the UTF-16 code unit that is the given
2211 incrementCodePoints away from the initial index.
2213 @param incrementCodePoints
2214 the number of code points to move the given *indexUtf16. If
2215 non-negative, moving is done after determining the code point at the
2216 index. If negative, moving is done before determining the code point
2217 at the (then updated) index. The value must be such that the resulting
2218 UTF-16 based index is in the range from zero to the length of this
2219 string (in UTF-16 code units), inclusive.
2221 @return
2222 the code point (an integer in the range from 0 to 0x10FFFF, inclusive)
2223 that is represented within this string starting at the index computed as
2224 follows: If incrementCodePoints is non-negative, the index is the
2225 initial value of *indexUtf16; if incrementCodePoints is negative, the
2226 index is the updated value of *indexUtf16. In either case, the computed
2227 index must be in the range from zero to one less than the length of this
2228 string (in UTF-16 code units), inclusive.
2230 @since UDK 3.2.7
2232 inline sal_uInt32 iterateCodePoints(
2233 sal_Int32 * indexUtf16, sal_Int32 incrementCodePoints = 1) const
2235 return rtl_uString_iterateCodePoints(
2236 pData, indexUtf16, incrementCodePoints);
2240 * Convert an OString to an OUString, assuming that the OString is
2241 * UTF-8-encoded.
2243 * @param rSource
2244 * an OString to convert
2246 * @since LibreOffice 4.4
2248 static inline OUString fromUtf8(const OString& rSource)
2250 OUString aTarget;
2251 bool bSuccess = rtl_convertStringToUString(&aTarget.pData,
2252 rSource.getStr(),
2253 rSource.getLength(),
2254 RTL_TEXTENCODING_UTF8,
2255 RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR|RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR|RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR);
2256 (void) bSuccess;
2257 assert(bSuccess);
2258 return aTarget;
2262 * Convert this string to an OString, assuming that the string can be
2263 * UTF-8-encoded successfully.
2265 * In other words, you must not use this method on a random sequence of
2266 * UTF-16 code units, but only at places where it is assumed that the
2267 * content is a proper string.
2269 * @since LibreOffice 4.4
2271 inline OString toUtf8() const
2273 OString aTarget;
2274 bool bSuccess = rtl_convertUStringToString(&aTarget.pData,
2275 getStr(),
2276 getLength(),
2277 RTL_TEXTENCODING_UTF8,
2278 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR|RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR);
2279 (void) bSuccess;
2280 assert(bSuccess);
2281 return aTarget;
2285 Returns the string representation of the integer argument.
2287 This function can't be used for language specific conversion.
2289 @param i an integer value
2290 @param radix the radix (between 2 and 36)
2291 @return a string with the string representation of the argument.
2292 @since LibreOffice 4.1
2294 static OUString number( int i, sal_Int16 radix = 10 )
2296 sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFINT32];
2297 rtl_uString* pNewData = 0;
2298 rtl_uString_newFromStr_WithLength( &pNewData, aBuf, rtl_ustr_valueOfInt32( aBuf, i, radix ) );
2299 return OUString( pNewData, SAL_NO_ACQUIRE );
2301 /// @overload
2302 /// @since LibreOffice 4.1
2303 static OUString number( unsigned int i, sal_Int16 radix = 10 )
2305 return number( static_cast< unsigned long long >( i ), radix );
2307 /// @overload
2308 /// @since LibreOffice 4.1
2309 static OUString number( long i, sal_Int16 radix = 10)
2311 return number( static_cast< long long >( i ), radix );
2313 /// @overload
2314 /// @since LibreOffice 4.1
2315 static OUString number( unsigned long i, sal_Int16 radix = 10 )
2317 return number( static_cast< unsigned long long >( i ), radix );
2319 /// @overload
2320 /// @since LibreOffice 4.1
2321 static OUString number( long long ll, sal_Int16 radix = 10 )
2323 sal_Unicode aBuf[RTL_STR_MAX_VALUEOFINT64];
2324 rtl_uString* pNewData = 0;
2325 rtl_uString_newFromStr_WithLength( &pNewData, aBuf, rtl_ustr_valueOfInt64( aBuf, ll, radix ) );
2326 return OUString( pNewData, SAL_NO_ACQUIRE );
2328 /// @overload
2329 /// @since LibreOffice 4.1
2330 static OUString number( unsigned long long ll, sal_Int16 radix = 10 )
2332 sal_Unicode aBuf[RTL_STR_MAX_VALUEOFUINT64];
2333 rtl_uString* pNewData = 0;
2334 rtl_uString_newFromStr_WithLength( &pNewData, aBuf, rtl_ustr_valueOfUInt64( aBuf, ll, radix ) );
2335 return OUString( pNewData, SAL_NO_ACQUIRE );
2339 Returns the string representation of the float argument.
2341 This function can't be used for language specific conversion.
2343 @param f a float.
2344 @return a string with the string representation of the argument.
2345 @since LibreOffice 4.1
2347 static OUString number( float f )
2349 sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFFLOAT];
2350 rtl_uString* pNewData = 0;
2351 rtl_uString_newFromStr_WithLength( &pNewData, aBuf, rtl_ustr_valueOfFloat( aBuf, f ) );
2352 return OUString( pNewData, SAL_NO_ACQUIRE );
2356 Returns the string representation of the double argument.
2358 This function can't be used for language specific conversion.
2360 @param d a double.
2361 @return a string with the string representation of the argument.
2362 @since LibreOffice 4.1
2364 static OUString number( double d )
2366 sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFDOUBLE];
2367 rtl_uString* pNewData = 0;
2368 rtl_uString_newFromStr_WithLength( &pNewData, aBuf, rtl_ustr_valueOfDouble( aBuf, d ) );
2369 return OUString( pNewData, SAL_NO_ACQUIRE );
2373 Returns the string representation of the sal_Bool argument.
2375 If the sal_Bool is true, the string "true" is returned.
2376 If the sal_Bool is false, the string "false" is returned.
2377 This function can't be used for language specific conversion.
2379 @param b a sal_Bool.
2380 @return a string with the string representation of the argument.
2381 @deprecated use boolean()
2383 SAL_DEPRECATED("use boolean()") static OUString valueOf( sal_Bool b )
2385 return boolean(b);
2389 Returns the string representation of the boolean argument.
2391 If the argument is true, the string "true" is returned.
2392 If the argument is false, the string "false" is returned.
2393 This function can't be used for language specific conversion.
2395 @param b a bool.
2396 @return a string with the string representation of the argument.
2397 @since LibreOffice 4.1
2399 static OUString boolean( bool b )
2401 sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFBOOLEAN];
2402 rtl_uString* pNewData = 0;
2403 rtl_uString_newFromStr_WithLength( &pNewData, aBuf, rtl_ustr_valueOfBoolean( aBuf, b ) );
2404 return OUString( pNewData, SAL_NO_ACQUIRE );
2408 Returns the string representation of the char argument.
2410 @param c a character.
2411 @return a string with the string representation of the argument.
2412 @deprecated use operator, function or constructor taking char or sal_Unicode argument
2414 SAL_DEPRECATED("convert to OUString or use directly") static OUString valueOf( sal_Unicode c )
2416 return OUString( &c, 1 );
2420 Returns the string representation of the int argument.
2422 This function can't be used for language specific conversion.
2424 @param i a int32.
2425 @param radix the radix (between 2 and 36)
2426 @return a string with the string representation of the argument.
2427 @deprecated use number()
2429 SAL_DEPRECATED("use number()") static OUString valueOf( sal_Int32 i, sal_Int16 radix = 10 )
2431 return number( i, radix );
2435 Returns the string representation of the long argument.
2437 This function can't be used for language specific conversion.
2439 @param ll a int64.
2440 @param radix the radix (between 2 and 36)
2441 @return a string with the string representation of the argument.
2442 @deprecated use number()
2444 SAL_DEPRECATED("use number()") static OUString valueOf( sal_Int64 ll, sal_Int16 radix = 10 )
2446 return number( ll, radix );
2450 Returns the string representation of the float argument.
2452 This function can't be used for language specific conversion.
2454 @param f a float.
2455 @return a string with the string representation of the argument.
2456 @deprecated use number()
2458 SAL_DEPRECATED("use number()") static OUString valueOf( float f )
2460 return number(f);
2464 Returns the string representation of the double argument.
2466 This function can't be used for language specific conversion.
2468 @param d a double.
2469 @return a string with the string representation of the argument.
2470 @deprecated use number()
2472 SAL_DEPRECATED("use number()") static OUString valueOf( double d )
2474 return number(d);
2478 Returns a OUString copied without conversion from an ASCII
2479 character string.
2481 Since this method is optimized for performance, the ASCII character
2482 values are not converted in any way. The caller has to make sure that
2483 all ASCII characters are in the allowed range between 0 and
2484 127. The ASCII string must be NULL-terminated.
2486 Note that for string literals it is simpler and more efficient
2487 to directly use the OUString constructor.
2489 @param value the 8-Bit ASCII character string
2490 @return a string with the string representation of the argument.
2492 static OUString createFromAscii( const sal_Char * value )
2494 rtl_uString* pNew = 0;
2495 rtl_uString_newFromAscii( &pNew, value );
2496 return OUString( pNew, SAL_NO_ACQUIRE );
2500 #if defined LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
2501 /// @cond INTERNAL
2503 /** Compare a string and an ASCII character literal for equality.
2505 @since LibreOffice 5.0
2507 template<char C> bool operator ==(OUString const & string, OUStringLiteral1<C>)
2509 char const c = C;
2510 return string.equalsAsciiL(&c, 1);
2513 /** Compare a string and an ASCII character literal for inequality.
2515 @since LibreOffice 5.0
2517 template<char C> bool operator !=(
2518 OUString const & string, OUStringLiteral1<C> literal)
2520 return !(string == literal);
2524 @internal
2526 template<>
2527 struct ToStringHelper< OUString >
2529 static int length( const OUString& s ) { return s.getLength(); }
2530 static sal_Unicode* addData( sal_Unicode* buffer, const OUString& s ) { return addDataHelper( buffer, s.getStr(), s.getLength()); }
2531 static const bool allowOStringConcat = false;
2532 static const bool allowOUStringConcat = true;
2536 @internal
2538 template<>
2539 struct ToStringHelper< OUStringLiteral >
2541 static int length( const OUStringLiteral& str ) { return str.size; }
2542 static sal_Unicode* addData( sal_Unicode* buffer, const OUStringLiteral& str ) { return addDataLiteral( buffer, str.data, str.size ); }
2543 static const bool allowOStringConcat = false;
2544 static const bool allowOUStringConcat = true;
2548 @internal
2550 template<char C> struct ToStringHelper<OUStringLiteral1<C>>
2552 static int length(OUStringLiteral1<C>) { return 1; }
2553 static sal_Unicode * addData(sal_Unicode * buffer, OUStringLiteral1<C>)
2554 { *buffer++ = C; return buffer; }
2555 static const bool allowOStringConcat = false;
2556 static const bool allowOUStringConcat = true;
2560 @internal
2562 template< typename charT, typename traits, typename T1, typename T2 >
2563 inline std::basic_ostream<charT, traits> & operator <<(
2564 std::basic_ostream<charT, traits> & stream, const OUStringConcat< T1, T2 >& concat)
2566 return stream << OUString( concat );
2569 /// @endcond
2570 #endif
2572 /** A helper to use OUStrings with hash maps.
2574 Instances of this class are unary function objects that can be used as
2575 hash function arguments to std::unordered_map and similar constructs.
2577 struct OUStringHash
2579 /** Compute a hash code for a string.
2581 @param rString
2582 a string.
2584 @return
2585 a hash code for the string. This hash code should not be stored
2586 persistently, as its computation may change in later revisions.
2588 size_t operator()(const OUString& rString) const
2589 { return (size_t)rString.hashCode(); }
2592 /* ======================================================================= */
2594 /** Convert an OString to an OUString, using a specific text encoding.
2596 The lengths of the two strings may differ (e.g., for double-byte
2597 encodings, UTF-7, UTF-8).
2599 @param rStr
2600 an OString to convert.
2602 @param encoding
2603 the text encoding to use for conversion.
2605 @param convertFlags
2606 flags which control the conversion. Either use
2607 OSTRING_TO_OUSTRING_CVTFLAGS, or see
2608 <http://udk.openoffice.org/cpp/man/spec/textconversion.html> for more
2609 details.
2611 inline OUString OStringToOUString( const OString & rStr,
2612 rtl_TextEncoding encoding,
2613 sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS )
2615 return OUString( rStr.getStr(), rStr.getLength(), encoding, convertFlags );
2618 /** Convert an OUString to an OString, using a specific text encoding.
2620 The lengths of the two strings may differ (e.g., for double-byte
2621 encodings, UTF-7, UTF-8).
2623 @param rUnicode
2624 an OUString to convert.
2626 @param encoding
2627 the text encoding to use for conversion.
2629 @param convertFlags
2630 flags which control the conversion. Either use
2631 OUSTRING_TO_OSTRING_CVTFLAGS, or see
2632 <http://udk.openoffice.org/cpp/man/spec/textconversion.html> for more
2633 details.
2635 inline OString OUStringToOString( const OUString & rUnicode,
2636 rtl_TextEncoding encoding,
2637 sal_uInt32 convertFlags = OUSTRING_TO_OSTRING_CVTFLAGS )
2639 return OString( rUnicode.getStr(), rUnicode.getLength(), encoding, convertFlags );
2642 /* ======================================================================= */
2645 Support for rtl::OUString in std::ostream (and thus in
2646 CPPUNIT_ASSERT or SAL_INFO macros, for example).
2648 The rtl::OUString is converted to UTF-8.
2650 @since LibreOffice 3.5.
2652 template< typename charT, typename traits >
2653 inline std::basic_ostream<charT, traits> & operator <<(
2654 std::basic_ostream<charT, traits> & stream, OUString const & string)
2656 return stream <<
2657 OUStringToOString(string, RTL_TEXTENCODING_UTF8).getStr();
2658 // best effort; potentially loses data due to conversion failures
2659 // (stray surrogate halves) and embedded null characters
2662 } // namespace
2664 #ifdef RTL_STRING_UNITTEST
2665 namespace rtl
2667 typedef rtlunittest::OUString OUString;
2669 #endif
2671 // In internal code, allow to use classes like OUString without having to
2672 // explicitly refer to the rtl namespace, which is kind of superfluous given
2673 // that OUString itself is namespaced by its OU prefix:
2674 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
2675 using ::rtl::OUString;
2676 using ::rtl::OUStringHash;
2677 using ::rtl::OStringToOUString;
2678 using ::rtl::OUStringToOString;
2679 using ::rtl::OUStringLiteral;
2680 using ::rtl::OUStringLiteral1;
2681 #endif
2683 #endif /* _RTL_USTRING_HXX */
2685 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */