nss: upgrade to release 3.73
[LibreOffice.git] / include / rtl / ustring.hxx
blob48aca3243383c515a209da3cf6e184cd97483b67
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 <cstddef>
27 #include <cstdlib>
28 #include <limits>
29 #include <new>
30 #include <ostream>
31 #include <utility>
33 #if defined LIBO_INTERNAL_ONLY
34 #include <string_view>
35 #include <type_traits>
36 #endif
38 #include "rtl/ustring.h"
39 #include "rtl/string.hxx"
40 #include "rtl/stringutils.hxx"
41 #include "rtl/textenc.h"
43 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
44 #include "config_global.h"
45 #include "rtl/stringconcat.hxx"
46 #endif
48 #ifdef RTL_STRING_UNITTEST
49 extern bool rtl_string_unittest_invalid_conversion;
50 #endif
52 // The unittest uses slightly different code to help check that the proper
53 // calls are made. The class is put into a different namespace to make
54 // sure the compiler generates a different (if generating also non-inline)
55 // copy of the function and does not merge them together. The class
56 // is "brought" into the proper rtl namespace by a typedef below.
57 #ifdef RTL_STRING_UNITTEST
58 #define rtl rtlunittest
59 #endif
61 namespace rtl
64 class OUStringBuffer;
66 #ifdef RTL_STRING_UNITTEST
67 #undef rtl
68 #endif
70 #if defined LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
71 /// @cond INTERNAL
73 /**
74 A wrapper dressing a string literal as a static-refcount rtl_uString.
76 This class is not part of public API and is meant to be used only in LibreOffice code.
77 @since LibreOffice 4.0
79 template<std::size_t N> class SAL_WARN_UNUSED OUStringLiteral {
80 static_assert(N != 0);
81 static_assert(N - 1 <= std::numeric_limits<sal_Int32>::max(), "literal too long");
83 public:
84 #if HAVE_CPP_CONSTEVAL
85 consteval
86 #else
87 constexpr
88 #endif
89 OUStringLiteral(char16_t const (&literal)[N]) {
90 assertLayout();
91 assert(literal[N - 1] == '\0');
92 //TODO: Use C++20 constexpr std::copy_n (P0202R3):
93 for (std::size_t i = 0; i != N; ++i) {
94 buffer[i] = literal[i];
98 constexpr sal_Int32 getLength() const { return length; }
100 constexpr sal_Unicode const * getStr() const SAL_RETURNS_NONNULL { return buffer; }
102 constexpr operator std::u16string_view() const { return {buffer, sal_uInt32(length)}; }
104 private:
105 static constexpr void assertLayout() {
106 // These static_asserts verifying the layout compatibility with rtl_uString cannot be class
107 // member declarations, as offsetof requires a complete type, so defer them to here:
108 static_assert(offsetof(OUStringLiteral, refCount) == offsetof(rtl_uString, refCount));
109 static_assert(std::is_same_v<decltype(refCount), decltype(rtl_uString::refCount)>);
110 static_assert(offsetof(OUStringLiteral, length) == offsetof(rtl_uString, length));
111 static_assert(std::is_same_v<decltype(length), decltype(rtl_uString::length)>);
112 static_assert(offsetof(OUStringLiteral, buffer) == offsetof(rtl_uString, buffer));
113 static_assert(
114 std::is_same_v<
115 std::remove_extent_t<decltype(buffer)>,
116 std::remove_extent_t<decltype(rtl_uString::buffer)>>);
119 // Same layout as rtl_uString (include/rtl/ustring.h):
120 oslInterlockedCount refCount = 0x40000000; // SAL_STRING_STATIC_FLAG (sal/rtl/strimp.hxx)
121 sal_Int32 length = N - 1;
122 sal_Unicode buffer[N] = {}; //TODO: drop initialization for C++20 (P1331R2)
125 #if defined RTL_STRING_UNITTEST
126 namespace libreoffice_internal {
127 template<std::size_t N> struct ExceptConstCharArrayDetector<OUStringLiteral<N>> {};
128 template<std::size_t N> struct ExceptCharArrayDetector<OUStringLiteral<N>> {};
130 #endif
132 /// @endcond
133 #endif
135 /* ======================================================================= */
138 This String class provides base functionality for C++ like Unicode
139 character array handling. The advantage of this class is that it
140 handles all the memory management for you - and it does it
141 more efficiently. If you assign a string to another string, the
142 data of both strings are shared (without any copy operation or
143 memory allocation) as long as you do not change the string. This class
144 also stores the length of the string, so that many operations are
145 faster than the C-str-functions.
147 This class provides only readonly string handling. So you could create
148 a string and you could only query the content from this string.
149 It provides also functionality to change the string, but this results
150 in every case in a new string instance (in the most cases with a
151 memory allocation). You don't have functionality to change the
152 content of the string. If you want to change the string content, then
153 you should use the OStringBuffer class, which provides these
154 functionalities and avoids too much memory allocation.
156 The design of this class is similar to the string classes in Java so
157 less people should have understanding problems when they use this class.
160 class SAL_WARN_UNUSED SAL_DLLPUBLIC_RTTI OUString
162 public:
163 /// @cond INTERNAL
164 rtl_uString * pData;
165 /// @endcond
168 New string containing no characters.
170 OUString()
172 pData = NULL;
173 rtl_uString_new( &pData );
177 New string from OUString.
179 @param str an OUString.
181 OUString( const OUString & str )
183 pData = str.pData;
184 rtl_uString_acquire( pData );
187 #if defined LIBO_INTERNAL_ONLY
189 Move constructor.
191 @param str an OUString.
192 @since LibreOffice 5.2
194 OUString( OUString && str ) noexcept
196 pData = str.pData;
197 str.pData = nullptr;
198 rtl_uString_new( &str.pData );
200 #endif
203 New string from OUString data.
205 @param str an OUString data.
207 OUString( rtl_uString * str )
209 pData = str;
210 rtl_uString_acquire( pData );
213 /** New OUString from OUString data without acquiring it. Takeover of ownership.
215 The SAL_NO_ACQUIRE dummy parameter is only there to distinguish this
216 from other constructors.
218 @param str
219 OUString data
221 OUString( rtl_uString * str, __sal_NoAcquire )
222 { pData = str; }
225 New string from a single Unicode character.
227 @param value a Unicode character.
229 explicit OUString( sal_Unicode value )
230 : pData (NULL)
232 rtl_uString_newFromStr_WithLength( &pData, &value, 1 );
235 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST_CONCAT
236 /// @cond INTERNAL
237 // Catch inadvertent conversions to the above ctor (but still allow
238 // construction from char literals):
239 OUString(int) = delete;
240 explicit OUString(char c):
241 OUString(sal_Unicode(static_cast<unsigned char>(c)))
243 /// @endcond
244 #endif
246 #if defined LIBO_INTERNAL_ONLY
248 template<typename T> explicit OUString(
249 T const & value,
250 typename libreoffice_internal::CharPtrDetector<T, libreoffice_internal::Dummy>::TypeUtf16
251 = libreoffice_internal::Dummy()):
252 pData(nullptr)
253 { rtl_uString_newFromStr(&pData, value); }
255 template<typename T> explicit OUString(
256 T & value,
257 typename
258 libreoffice_internal::NonConstCharArrayDetector<T, libreoffice_internal::Dummy>::TypeUtf16
259 = libreoffice_internal::Dummy()):
260 pData(nullptr)
261 { rtl_uString_newFromStr(&pData, value); }
263 #else
266 New string from a Unicode character buffer array.
268 @param value a NULL-terminated Unicode character array.
270 OUString( const sal_Unicode * value )
272 pData = NULL;
273 rtl_uString_newFromStr( &pData, value );
276 #endif
279 New string from a Unicode character buffer array.
281 @param value a Unicode character array.
282 @param length the number of character which should be copied.
283 The character array length must be greater than
284 or equal to this value.
286 OUString( const sal_Unicode * value, sal_Int32 length )
288 pData = NULL;
289 rtl_uString_newFromStr_WithLength( &pData, value, length );
293 New string from an 8-Bit string literal that is expected to contain only
294 characters in the ASCII set (i.e. first 128 characters). This constructor
295 allows an efficient and convenient way to create OUString
296 instances from ASCII literals. When creating strings from data that
297 is not pure ASCII, it needs to be converted to OUString by explicitly
298 providing the encoding to use for the conversion.
300 If there are any embedded \0's in the string literal, the result is undefined.
301 Use the overload that explicitly accepts length.
303 @param literal the 8-bit ASCII string literal
305 @since LibreOffice 3.6
307 template< typename T >
308 OUString( T& literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
310 assert(
311 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
312 pData = NULL;
313 if (libreoffice_internal::ConstCharArrayDetector<T>::length == 0) {
314 rtl_uString_new(&pData);
315 } else {
316 rtl_uString_newFromLiteral(
317 &pData,
318 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
319 literal),
320 libreoffice_internal::ConstCharArrayDetector<T>::length, 0);
322 #ifdef RTL_STRING_UNITTEST
323 rtl_string_unittest_const_literal = true;
324 #endif
327 #if defined LIBO_INTERNAL_ONLY
328 /** @overload @since LibreOffice 5.3 */
329 template<typename T> OUString(
330 T & literal,
331 typename libreoffice_internal::ConstCharArrayDetector<
332 T, libreoffice_internal::Dummy>::TypeUtf16
333 = libreoffice_internal::Dummy()):
334 pData(nullptr)
336 assert(
337 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
338 if (libreoffice_internal::ConstCharArrayDetector<T>::length == 0) {
339 rtl_uString_new(&pData);
340 } else {
341 rtl_uString_newFromStr_WithLength(
342 &pData,
343 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
344 literal),
345 libreoffice_internal::ConstCharArrayDetector<T>::length);
348 #endif
350 #if defined LIBO_INTERNAL_ONLY && defined RTL_STRING_UNITTEST
351 /// @cond INTERNAL
353 * Only used by unittests to detect incorrect conversions.
354 * @internal
356 template< typename T >
357 OUString( T&, typename libreoffice_internal::ExceptConstCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
359 pData = NULL;
360 rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
361 rtl_string_unittest_invalid_conversion = true;
364 * Only used by unittests to detect incorrect conversions.
365 * @internal
367 template< typename T >
368 OUString( const T&, typename libreoffice_internal::ExceptCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
370 pData = NULL;
371 rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
372 rtl_string_unittest_invalid_conversion = true;
374 /// @endcond
375 #endif
377 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
378 /// @cond INTERNAL
380 New string from a string literal.
382 @since LibreOffice 5.0
384 template<std::size_t N> OUString(OUStringLiteral<N> const & literal):
385 pData(const_cast<rtl_uString *>(reinterpret_cast<rtl_uString const *>(&literal))) {}
386 template<std::size_t N> OUString(OUStringLiteral<N> &&) = delete;
387 /// @endcond
388 #endif
391 New string from an 8-Bit character buffer array.
393 @param value An 8-Bit character array.
394 @param length The number of character which should be converted.
395 The 8-Bit character array length must be
396 greater than or equal to this value.
397 @param encoding The text encoding from which the 8-Bit character
398 sequence should be converted.
399 @param convertFlags Flags which control the conversion.
400 see RTL_TEXTTOUNICODE_FLAGS_...
402 @exception std::bad_alloc is thrown if an out-of-memory condition occurs
404 OUString( const char * value, sal_Int32 length,
405 rtl_TextEncoding encoding,
406 sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS )
408 pData = NULL;
409 rtl_string2UString( &pData, value, length, encoding, convertFlags );
410 if (pData == NULL) {
411 throw std::bad_alloc();
415 /** Create a new string from an array of Unicode code points.
417 @param codePoints
418 an array of at least codePointCount code points, which each must be in
419 the range from 0 to 0x10FFFF, inclusive. May be null if codePointCount
420 is zero.
422 @param codePointCount
423 the non-negative number of code points.
425 @exception std::bad_alloc
426 is thrown if either an out-of-memory condition occurs or the resulting
427 number of UTF-16 code units would have been larger than SAL_MAX_INT32.
429 @since UDK 3.2.7
431 explicit OUString(
432 sal_uInt32 const * codePoints, sal_Int32 codePointCount):
433 pData(NULL)
435 rtl_uString_newFromCodePoints(&pData, codePoints, codePointCount);
436 if (pData == NULL) {
437 throw std::bad_alloc();
441 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
443 @overload
444 @internal
446 template< typename T1, typename T2 >
447 OUString( OUStringConcat< T1, T2 >&& c )
449 const sal_Int32 l = c.length();
450 pData = rtl_uString_alloc( l );
451 if (l != 0)
453 sal_Unicode* end = c.addData( pData->buffer );
454 pData->length = l;
455 *end = '\0';
456 // TODO realloc in case pData->length is noticeably smaller than l?
461 @overload
462 @internal
464 template< typename T >
465 OUString( OUStringNumber< T >&& n )
466 : OUString( n.buf, n.length )
468 #endif
470 #if defined LIBO_INTERNAL_ONLY
471 OUString(std::u16string_view sv) {
472 if (sv.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max())) {
473 throw std::bad_alloc();
475 pData = nullptr;
476 rtl_uString_newFromStr_WithLength(&pData, sv.data(), sv.size());
478 #endif
481 Release the string data.
483 ~OUString()
485 rtl_uString_release( pData );
488 /** Provides an OUString const & passing a storage pointer of an
489 rtl_uString * handle.
490 It is more convenient to use C++ OUString member functions when dealing
491 with rtl_uString * handles. Using this function avoids unnecessary
492 acquire()/release() calls for a temporary OUString object.
494 @param ppHandle
495 pointer to storage
496 @return
497 OUString const & based on given storage
499 static OUString const & unacquired( rtl_uString * const * ppHandle )
500 { return * reinterpret_cast< OUString const * >( ppHandle ); }
503 Assign a new string.
505 @param str an OUString.
507 OUString & operator=( const OUString & str )
509 rtl_uString_assign( &pData, str.pData );
510 return *this;
513 #if defined LIBO_INTERNAL_ONLY
515 Move assign a new string.
517 @param str an OUString.
518 @since LibreOffice 5.2
520 OUString & operator=( OUString && str ) noexcept
522 rtl_uString_release( pData );
523 pData = str.pData;
524 str.pData = nullptr;
525 rtl_uString_new( &str.pData );
526 return *this;
528 #endif
531 Assign a new string from an 8-Bit string literal that is expected to contain only
532 characters in the ASCII set (i.e. first 128 characters). This operator
533 allows an efficient and convenient way to assign OUString
534 instances from ASCII literals. When assigning strings from data that
535 is not pure ASCII, it needs to be converted to OUString by explicitly
536 providing the encoding to use for the conversion.
538 @param literal the 8-bit ASCII string literal
540 @since LibreOffice 3.6
542 template< typename T >
543 typename libreoffice_internal::ConstCharArrayDetector< T, OUString& >::Type operator=( T& literal )
545 assert(
546 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
547 if (libreoffice_internal::ConstCharArrayDetector<T>::length == 0) {
548 rtl_uString_new(&pData);
549 } else {
550 rtl_uString_newFromLiteral(
551 &pData,
552 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
553 literal),
554 libreoffice_internal::ConstCharArrayDetector<T>::length, 0);
556 return *this;
559 #if defined LIBO_INTERNAL_ONLY
560 /** @overload @since LibreOffice 5.3 */
561 template<typename T>
562 typename
563 libreoffice_internal::ConstCharArrayDetector<T, OUString &>::TypeUtf16
564 operator =(T & literal) {
565 if (libreoffice_internal::ConstCharArrayDetector<T>::length == 0) {
566 rtl_uString_new(&pData);
567 } else {
568 rtl_uString_newFromStr_WithLength(
569 &pData,
570 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
571 literal),
572 libreoffice_internal::ConstCharArrayDetector<T>::length);
574 return *this;
577 /** @overload @since LibreOffice 5.4 */
578 template<std::size_t N> OUString & operator =(OUStringLiteral<N> const & literal) {
579 if (literal.getLength() == 0) {
580 rtl_uString_new(&pData);
581 } else {
582 rtl_uString_newFromStr_WithLength(&pData, literal.getStr(), literal.getLength());
584 return *this;
587 template<typename T>
588 OUString & operator =(OUStringNumber<T> && n) {
589 // n.length should never be zero, so no need to add an optimization for that case
590 rtl_uString_newFromStr_WithLength(&pData, n.buf, n.length);
591 return *this;
594 OUString & operator =(std::u16string_view sv) {
595 if (sv.empty()) {
596 rtl_uString_new(&pData);
597 } else {
598 rtl_uString_newFromStr_WithLength(&pData, sv.data(), sv.size());
600 return *this;
602 #endif
604 #if defined LIBO_INTERNAL_ONLY
606 Append the contents of an OUStringBuffer to this string.
608 @param str an OUStringBuffer.
610 @exception std::bad_alloc is thrown if an out-of-memory condition occurs
611 @since LibreOffice 6.2
613 inline OUString & operator+=( const OUStringBuffer & str ) &;
614 #endif
617 Append a string to this string.
619 @param str an OUString.
621 @exception std::bad_alloc is thrown if an out-of-memory condition occurs
623 OUString & operator+=( const OUString & str )
624 #if defined LIBO_INTERNAL_ONLY
626 #endif
628 return internalAppend(str.pData);
630 #if defined LIBO_INTERNAL_ONLY
631 void operator+=(OUString const &) && = delete;
632 #endif
634 /** Append an ASCII string literal to this string.
636 @param literal an 8-bit ASCII-only string literal
638 @since LibreOffice 5.1
640 template<typename T>
641 typename libreoffice_internal::ConstCharArrayDetector<T, OUString &>::Type
642 operator +=(T & literal)
643 #if defined LIBO_INTERNAL_ONLY
645 #endif
647 assert(
648 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
649 rtl_uString_newConcatAsciiL(
650 &pData, pData,
651 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
652 libreoffice_internal::ConstCharArrayDetector<T>::length);
653 return *this;
655 #if defined LIBO_INTERNAL_ONLY
656 template<typename T>
657 typename libreoffice_internal::ConstCharArrayDetector<T, OUString &>::Type
658 operator +=(T &) && = delete;
659 #endif
661 #if defined LIBO_INTERNAL_ONLY
662 /** @overload @since LibreOffice 5.3 */
663 template<typename T>
664 typename
665 libreoffice_internal::ConstCharArrayDetector<T, OUString &>::TypeUtf16
666 operator +=(T & literal) & {
667 rtl_uString_newConcatUtf16L(
668 &pData, pData,
669 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
670 libreoffice_internal::ConstCharArrayDetector<T>::length);
671 return *this;
673 template<typename T>
674 typename
675 libreoffice_internal::ConstCharArrayDetector<T, OUString &>::TypeUtf16
676 operator +=(T &) && = delete;
678 /** @overload @since LibreOffice 5.4 */
679 template<std::size_t N> OUString & operator +=(OUStringLiteral<N> const & literal) & {
680 rtl_uString_newConcatUtf16L(&pData, pData, literal.getStr(), literal.getLength());
681 return *this;
683 template<std::size_t N> void operator +=(OUStringLiteral<N> const &) && = delete;
685 OUString & operator +=(std::u16string_view sv) & {
686 if (sv.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max())) {
687 throw std::bad_alloc();
689 rtl_uString_newConcatUtf16L(&pData, pData, sv.data(), sv.size());
690 return *this;
692 void operator +=(std::u16string_view) && = delete;
693 #endif
695 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
697 @overload
698 @internal
700 template< typename T1, typename T2 >
701 OUString& operator+=( OUStringConcat< T1, T2 >&& c ) & {
702 sal_Int32 l = c.length();
703 if( l == 0 )
704 return *this;
705 l += pData->length;
706 rtl_uString_ensureCapacity( &pData, l );
707 sal_Unicode* end = c.addData( pData->buffer + pData->length );
708 *end = '\0';
709 pData->length = l;
710 return *this;
712 template<typename T1, typename T2> void operator +=(
713 OUStringConcat<T1, T2> &&) && = delete;
716 @overload
717 @internal
719 template< typename T >
720 OUString& operator+=( OUStringNumber< T >&& n ) & {
721 sal_Int32 l = n.length;
722 if( l == 0 )
723 return *this;
724 l += pData->length;
725 rtl_uString_ensureCapacity( &pData, l );
726 sal_Unicode* end = addDataHelper( pData->buffer + pData->length, n.buf, n.length );
727 *end = '\0';
728 pData->length = l;
729 return *this;
731 template<typename T> void operator +=(
732 OUStringNumber<T> &&) && = delete;
733 #endif
736 Clears the string, i.e, makes a zero-character string
737 @since LibreOffice 4.4
739 void clear()
741 rtl_uString_new( &pData );
745 Returns the length of this string.
747 The length is equal to the number of Unicode characters in this string.
749 @return the length of the sequence of characters represented by this
750 object.
752 sal_Int32 getLength() const { return pData->length; }
755 Checks if a string is empty.
757 @return true if the string is empty;
758 false, otherwise.
760 @since LibreOffice 3.4
762 bool isEmpty() const
764 return pData->length == 0;
768 Returns a pointer to the Unicode character buffer for this string.
770 It isn't necessarily NULL terminated.
772 @return a pointer to the Unicode characters buffer for this object.
774 const sal_Unicode * getStr() const SAL_RETURNS_NONNULL { return pData->buffer; }
777 Access to individual characters.
779 @param index must be non-negative and less than length.
781 @return the character at the given index.
783 @since LibreOffice 3.5
785 sal_Unicode operator [](sal_Int32 index) const {
786 // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
787 assert(index >= 0 && static_cast<sal_uInt32>(index) < static_cast<sal_uInt32>(getLength()));
788 return getStr()[index];
792 Compares two strings.
794 The comparison is based on the numeric value of each character in
795 the strings and return a value indicating their relationship.
796 This function can't be used for language specific sorting.
798 @param str the object to be compared.
799 @return 0 - if both strings are equal
800 < 0 - if this string is less than the string argument
801 > 0 - if this string is greater than the string argument
803 sal_Int32 compareTo( const OUString & str ) const
805 return rtl_ustr_compare_WithLength( pData->buffer, pData->length,
806 str.pData->buffer, str.pData->length );
810 Compares two strings with a maximum count of characters.
812 The comparison is based on the numeric value of each character in
813 the strings and return a value indicating their relationship.
814 This function can't be used for language specific sorting.
816 @param str the object to be compared.
817 @param maxLength the maximum count of characters to be compared.
818 @return 0 - if both strings are equal
819 < 0 - if this string is less than the string argument
820 > 0 - if this string is greater than the string argument
822 @since UDK 3.2.7
824 sal_Int32 compareTo( const OUString & str, sal_Int32 maxLength ) const
826 return rtl_ustr_shortenedCompare_WithLength( pData->buffer, pData->length,
827 str.pData->buffer, str.pData->length, maxLength );
831 Compares two strings in reverse order.
833 The comparison is based on the numeric value of each character in
834 the strings and return a value indicating their relationship.
835 This function can't be used for language specific sorting.
837 @param str the object to be compared.
838 @return 0 - if both strings are equal
839 < 0 - if this string is less than the string argument
840 > 0 - if this string is greater than the string argument
842 #if defined LIBO_INTERNAL_ONLY
843 sal_Int32 reverseCompareTo(std::u16string_view sv) const {
844 return rtl_ustr_reverseCompare_WithLength(
845 pData->buffer, pData->length, sv.data(), sv.size());
847 #else
848 sal_Int32 reverseCompareTo( const OUString & str ) const
850 return rtl_ustr_reverseCompare_WithLength( pData->buffer, pData->length,
851 str.pData->buffer, str.pData->length );
853 #endif
856 @overload
857 This function accepts an ASCII string literal as its argument.
858 @since LibreOffice 4.1
860 template< typename T >
861 typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type reverseCompareTo( T& literal ) const
863 assert(
864 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
865 return rtl_ustr_asciil_reverseCompare_WithLength(
866 pData->buffer, pData->length,
867 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
868 libreoffice_internal::ConstCharArrayDetector<T>::length);
872 Perform a comparison of two strings.
874 The result is true if and only if second string
875 represents the same sequence of characters as the first string.
876 This function can't be used for language specific comparison.
878 @param str the object to be compared.
879 @return true if the strings are equal;
880 false, otherwise.
882 bool equals( const OUString & str ) const
884 if ( pData->length != str.pData->length )
885 return false;
886 if ( pData == str.pData )
887 return true;
888 return rtl_ustr_reverseCompare_WithLength( pData->buffer, pData->length,
889 str.pData->buffer, str.pData->length ) == 0;
893 Perform an ASCII lowercase comparison of two strings.
895 The result is true if and only if second string
896 represents the same sequence of characters as the first string,
897 ignoring the case.
898 Character values between 65 and 90 (ASCII A-Z) are interpreted as
899 values between 97 and 122 (ASCII a-z).
900 This function can't be used for language specific comparison.
902 @param str the object to be compared.
903 @return true if the strings are equal;
904 false, otherwise.
906 #if defined LIBO_INTERNAL_ONLY
907 bool equalsIgnoreAsciiCase(std::u16string_view sv) const {
908 return
909 rtl_ustr_compareIgnoreAsciiCase_WithLength(
910 pData->buffer, pData->length, sv.data(), sv.size())
911 == 0;
913 #else
914 bool equalsIgnoreAsciiCase( const OUString & str ) const
916 if ( pData->length != str.pData->length )
917 return false;
918 if ( pData == str.pData )
919 return true;
920 return rtl_ustr_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
921 str.pData->buffer, str.pData->length ) == 0;
923 #endif
926 Perform an ASCII lowercase comparison of two strings.
928 Compare the two strings with uppercase ASCII
929 character values between 65 and 90 (ASCII A-Z) interpreted as
930 values between 97 and 122 (ASCII a-z).
931 This function can't be used for language specific comparison.
933 @param str the object to be compared.
934 @return 0 - if both strings are equal
935 < 0 - if this string is less than the string argument
936 > 0 - if this string is greater than the string argument
938 @since LibreOffice 4.0
940 #if defined LIBO_INTERNAL_ONLY
941 sal_Int32 compareToIgnoreAsciiCase(std::u16string_view sv) const {
942 return rtl_ustr_compareIgnoreAsciiCase_WithLength(
943 pData->buffer, pData->length, sv.data(), sv.size());
945 #else
946 sal_Int32 compareToIgnoreAsciiCase( const OUString & str ) const
948 return rtl_ustr_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
949 str.pData->buffer, str.pData->length );
951 #endif
954 @overload
955 This function accepts an ASCII string literal as its argument.
956 @since LibreOffice 3.6
958 template< typename T >
959 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type equalsIgnoreAsciiCase( T& literal ) const
961 assert(
962 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
963 return
964 (pData->length
965 == libreoffice_internal::ConstCharArrayDetector<T>::length)
966 && (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength(
967 pData->buffer, pData->length,
968 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
969 literal))
970 == 0);
974 Match against a substring appearing in this string.
976 The result is true if and only if the second string appears as a substring
977 of this string, at the given position.
978 This function can't be used for language specific comparison.
980 @param str the object (substring) to be compared.
981 @param fromIndex the index to start the comparison from.
982 The index must be greater than or equal to 0
983 and less or equal as the string length.
984 @return true if str match with the characters in the string
985 at the given position;
986 false, otherwise.
988 #if defined LIBO_INTERNAL_ONLY
989 bool match(std::u16string_view sv, sal_Int32 fromIndex = 0) const {
990 return
991 rtl_ustr_shortenedCompare_WithLength(
992 pData->buffer + fromIndex, pData->length - fromIndex, sv.data(), sv.size(),
993 sv.size())
994 == 0;
996 #else
997 bool match( const OUString & str, sal_Int32 fromIndex = 0 ) const
999 return rtl_ustr_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1000 str.pData->buffer, str.pData->length, str.pData->length ) == 0;
1002 #endif
1005 @overload
1006 This function accepts an ASCII string literal as its argument.
1007 @since LibreOffice 3.6
1009 template< typename T >
1010 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type match( T& literal, sal_Int32 fromIndex = 0 ) const
1012 assert(
1013 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1014 return
1015 rtl_ustr_ascii_shortenedCompare_WithLength(
1016 pData->buffer+fromIndex, pData->length-fromIndex,
1017 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1018 literal),
1019 libreoffice_internal::ConstCharArrayDetector<T>::length)
1020 == 0;
1024 Match against a substring appearing in this string, ignoring the case of
1025 ASCII letters.
1027 The result is true if and only if the second string appears as a substring
1028 of this string, at the given position.
1029 Character values between 65 and 90 (ASCII A-Z) are interpreted as
1030 values between 97 and 122 (ASCII a-z).
1031 This function can't be used for language specific comparison.
1033 @param str the object (substring) to be compared.
1034 @param fromIndex the index to start the comparison from.
1035 The index must be greater than or equal to 0
1036 and less than or equal to the string length.
1037 @return true if str match with the characters in the string
1038 at the given position;
1039 false, otherwise.
1041 #if defined LIBO_INTERNAL_ONLY
1042 bool matchIgnoreAsciiCase(std::u16string_view sv, sal_Int32 fromIndex = 0) const {
1043 return
1044 rtl_ustr_shortenedCompareIgnoreAsciiCase_WithLength(
1045 pData->buffer + fromIndex, pData->length - fromIndex, sv.data(), sv.size(),
1046 sv.size())
1047 == 0;
1049 #else
1050 bool matchIgnoreAsciiCase( const OUString & str, sal_Int32 fromIndex = 0 ) const
1052 return rtl_ustr_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1053 str.pData->buffer, str.pData->length,
1054 str.pData->length ) == 0;
1056 #endif
1059 @overload
1060 This function accepts an ASCII string literal as its argument.
1061 @since LibreOffice 3.6
1063 template< typename T >
1064 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type matchIgnoreAsciiCase( T& literal, sal_Int32 fromIndex = 0 ) const
1066 assert(
1067 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1068 return
1069 rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength(
1070 pData->buffer+fromIndex, pData->length-fromIndex,
1071 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1072 literal),
1073 libreoffice_internal::ConstCharArrayDetector<T>::length)
1074 == 0;
1078 Compares two strings.
1080 The comparison is based on the numeric value of each character in
1081 the strings and return a value indicating their relationship.
1082 Since this method is optimized for performance, the ASCII character
1083 values are not converted in any way. The caller has to make sure that
1084 all ASCII characters are in the allowed range between 0 and 127.
1085 The ASCII string must be NULL-terminated.
1086 This function can't be used for language specific sorting.
1088 @param asciiStr the 8-Bit ASCII character string to be compared.
1089 @return 0 - if both strings are equal
1090 < 0 - if this string is less than the string argument
1091 > 0 - if this string is greater than the string argument
1093 sal_Int32 compareToAscii( const char* asciiStr ) const
1095 return rtl_ustr_ascii_compare_WithLength( pData->buffer, pData->length, asciiStr );
1099 Compares two strings with a maximum count of characters.
1101 The comparison is based on the numeric value of each character in
1102 the strings and return a value indicating their relationship.
1103 Since this method is optimized for performance, the ASCII character
1104 values are not converted in any way. The caller has to make sure that
1105 all ASCII characters are in the allowed range between 0 and 127.
1106 The ASCII string must be NULL-terminated.
1107 This function can't be used for language specific sorting.
1109 @deprecated This is a confusing overload with unexpectedly different
1110 semantics from the one-parameter form, so it is marked as deprecated.
1111 Practically all uses compare the return value against zero and can thus
1112 be replaced with uses of startsWith.
1114 @param asciiStr the 8-Bit ASCII character string to be compared.
1115 @param maxLength the maximum count of characters to be compared.
1116 @return 0 - if both strings are equal
1117 < 0 - if this string is less than the string argument
1118 > 0 - if this string is greater than the string argument
1120 SAL_DEPRECATED(
1121 "replace s1.compareToAscii(s2, strlen(s2)) == 0 with s1.startsWith(s2)")
1122 sal_Int32 compareToAscii( const char * asciiStr, sal_Int32 maxLength ) const
1124 return rtl_ustr_ascii_shortenedCompare_WithLength( pData->buffer, pData->length,
1125 asciiStr, maxLength );
1129 Compares two strings in reverse order.
1131 This could be useful, if normally both strings start with the same
1132 content. The comparison is based on the numeric value of each character
1133 in the strings and return a value indicating their relationship.
1134 Since this method is optimized for performance, the ASCII character
1135 values are not converted in any way. The caller has to make sure that
1136 all ASCII characters are in the allowed range between 0 and 127.
1137 The ASCII string must be NULL-terminated and must be greater than
1138 or equal to asciiStrLength.
1139 This function can't be used for language specific sorting.
1141 @param asciiStr the 8-Bit ASCII character string to be compared.
1142 @param asciiStrLength the length of the ascii string
1143 @return 0 - if both strings are equal
1144 < 0 - if this string is less than the string argument
1145 > 0 - if this string is greater than the string argument
1147 sal_Int32 reverseCompareToAsciiL( const char * asciiStr, sal_Int32 asciiStrLength ) const
1149 return rtl_ustr_asciil_reverseCompare_WithLength( pData->buffer, pData->length,
1150 asciiStr, asciiStrLength );
1154 Perform a comparison of two strings.
1156 The result is true if and only if second string
1157 represents the same sequence of characters as the first string.
1158 Since this method is optimized for performance, the ASCII character
1159 values are not converted in any way. The caller has to make sure that
1160 all ASCII characters are in the allowed range between 0 and 127.
1161 The ASCII string must be NULL-terminated.
1162 This function can't be used for language specific comparison.
1164 @param asciiStr the 8-Bit ASCII character string to be compared.
1165 @return true if the strings are equal;
1166 false, otherwise.
1168 bool equalsAscii( const char* asciiStr ) const
1170 return rtl_ustr_ascii_compare_WithLength( pData->buffer, pData->length,
1171 asciiStr ) == 0;
1175 Perform a comparison of two strings.
1177 The result is true if and only if second string
1178 represents the same sequence of characters as the first string.
1179 Since this method is optimized for performance, the ASCII character
1180 values are not converted in any way. The caller has to make sure that
1181 all ASCII characters are in the allowed range between 0 and 127.
1182 The ASCII string must be NULL-terminated and must be greater than
1183 or equal to asciiStrLength.
1184 This function can't be used for language specific comparison.
1186 @param asciiStr the 8-Bit ASCII character string to be compared.
1187 @param asciiStrLength the length of the ascii string
1188 @return true if the strings are equal;
1189 false, otherwise.
1191 bool equalsAsciiL( const char* asciiStr, sal_Int32 asciiStrLength ) const
1193 if ( pData->length != asciiStrLength )
1194 return false;
1196 return rtl_ustr_asciil_reverseEquals_WithLength(
1197 pData->buffer, asciiStr, asciiStrLength );
1201 Perform an ASCII lowercase comparison of two strings.
1203 The result is true if and only if second string
1204 represents the same sequence of characters as the first string,
1205 ignoring the case.
1206 Character values between 65 and 90 (ASCII A-Z) are interpreted as
1207 values between 97 and 122 (ASCII a-z).
1208 Since this method is optimized for performance, the ASCII character
1209 values are not converted in any way. The caller has to make sure that
1210 all ASCII characters are in the allowed range between 0 and 127.
1211 The ASCII string must be NULL-terminated.
1212 This function can't be used for language specific comparison.
1214 @param asciiStr the 8-Bit ASCII character string to be compared.
1215 @return true if the strings are equal;
1216 false, otherwise.
1218 bool equalsIgnoreAsciiCaseAscii( const char * asciiStr ) const
1220 return rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, asciiStr ) == 0;
1224 Compares two ASCII strings ignoring case
1226 The comparison is based on the numeric value of each character in
1227 the strings and return a value indicating their relationship.
1228 Since this method is optimized for performance, the ASCII character
1229 values are not converted in any way. The caller has to make sure that
1230 all ASCII characters are in the allowed range between 0 and 127.
1231 The ASCII string must be NULL-terminated.
1232 This function can't be used for language specific sorting.
1234 @param asciiStr the 8-Bit ASCII character string to be compared.
1235 @return 0 - if both strings are equal
1236 < 0 - if this string is less than the string argument
1237 > 0 - if this string is greater than the string argument
1239 @since LibreOffice 3.5
1241 sal_Int32 compareToIgnoreAsciiCaseAscii( const char * asciiStr ) const
1243 return rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, asciiStr );
1247 Perform an ASCII lowercase comparison of two strings.
1249 The result is true if and only if second string
1250 represents the same sequence of characters as the first string,
1251 ignoring the case.
1252 Character values between 65 and 90 (ASCII A-Z) are interpreted as
1253 values between 97 and 122 (ASCII a-z).
1254 Since this method is optimized for performance, the ASCII character
1255 values are not converted in any way. The caller has to make sure that
1256 all ASCII characters are in the allowed range between 0 and 127.
1257 The ASCII string must be NULL-terminated and must be greater than
1258 or equal to asciiStrLength.
1259 This function can't be used for language specific comparison.
1261 @param asciiStr the 8-Bit ASCII character string to be compared.
1262 @param asciiStrLength the length of the ascii string
1263 @return true if the strings are equal;
1264 false, otherwise.
1266 bool equalsIgnoreAsciiCaseAsciiL( const char * asciiStr, sal_Int32 asciiStrLength ) const
1268 if ( pData->length != asciiStrLength )
1269 return false;
1271 return rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, asciiStr ) == 0;
1275 Match against a substring appearing in this string.
1277 The result is true if and only if the second string appears as a substring
1278 of this string, at the given position.
1279 Since this method is optimized for performance, the ASCII character
1280 values are not converted in any way. The caller has to make sure that
1281 all ASCII characters are in the allowed range between 0 and 127.
1282 The ASCII string must be NULL-terminated and must be greater than or
1283 equal to asciiStrLength.
1284 This function can't be used for language specific comparison.
1286 @param asciiStr the object (substring) to be compared.
1287 @param asciiStrLength the length of asciiStr.
1288 @param fromIndex the index to start the comparison from.
1289 The index must be greater than or equal to 0
1290 and less than or equal to the string length.
1291 @return true if str match with the characters in the string
1292 at the given position;
1293 false, otherwise.
1295 bool matchAsciiL( const char* asciiStr, sal_Int32 asciiStrLength, sal_Int32 fromIndex = 0 ) const
1297 return rtl_ustr_ascii_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1298 asciiStr, asciiStrLength ) == 0;
1301 // This overload is left undefined, to detect calls of matchAsciiL that
1302 // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
1303 // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
1304 // platforms):
1305 #if SAL_TYPES_SIZEOFLONG == 8
1306 void matchAsciiL(char const *, sal_Int32, rtl_TextEncoding) const;
1307 #endif
1310 Match against a substring appearing in this string, ignoring the case of
1311 ASCII letters.
1313 The result is true if and only if the second string appears as a substring
1314 of this string, at the given position.
1315 Character values between 65 and 90 (ASCII A-Z) are interpreted as
1316 values between 97 and 122 (ASCII a-z).
1317 Since this method is optimized for performance, the ASCII character
1318 values are not converted in any way. The caller has to make sure that
1319 all ASCII characters are in the allowed range between 0 and 127.
1320 The ASCII string must be NULL-terminated and must be greater than or
1321 equal to asciiStrLength.
1322 This function can't be used for language specific comparison.
1324 @param asciiStr the 8-Bit ASCII character string to be compared.
1325 @param asciiStrLength the length of the ascii string
1326 @param fromIndex the index to start the comparison from.
1327 The index must be greater than or equal to 0
1328 and less than or equal to the string length.
1329 @return true if str match with the characters in the string
1330 at the given position;
1331 false, otherwise.
1333 bool matchIgnoreAsciiCaseAsciiL( const char* asciiStr, sal_Int32 asciiStrLength, sal_Int32 fromIndex = 0 ) const
1335 return rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1336 asciiStr, asciiStrLength ) == 0;
1339 // This overload is left undefined, to detect calls of
1340 // matchIgnoreAsciiCaseAsciiL that erroneously use
1341 // RTL_CONSTASCII_USTRINGPARAM instead of RTL_CONSTASCII_STRINGPARAM (but
1342 // would lead to ambiguities on 32 bit platforms):
1343 #if SAL_TYPES_SIZEOFLONG == 8
1344 void matchIgnoreAsciiCaseAsciiL(char const *, sal_Int32, rtl_TextEncoding)
1345 const;
1346 #endif
1349 Check whether this string starts with a given substring.
1351 @param str the substring to be compared
1353 @param rest if non-null, and this function returns true, then assign a
1354 copy of the remainder of this string to *rest. Available since
1355 LibreOffice 4.2
1357 @return true if and only if the given str appears as a substring at the
1358 start of this string
1360 @since LibreOffice 4.0
1362 #if defined LIBO_INTERNAL_ONLY
1363 bool startsWith(std::u16string_view sv, OUString * rest = nullptr) const {
1364 auto const b = match(sv);
1365 if (b && rest != nullptr) {
1366 *rest = copy(sv.size());
1368 return b;
1370 #else
1371 bool startsWith(OUString const & str, OUString * rest = NULL) const {
1372 bool b = match(str);
1373 if (b && rest != NULL) {
1374 *rest = copy(str.getLength());
1376 return b;
1378 #endif
1381 @overload
1382 This function accepts an ASCII string literal as its argument.
1383 @since LibreOffice 4.0
1385 template< typename T >
1386 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type startsWith(
1387 T & literal, OUString * rest = NULL) const
1389 assert(
1390 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1391 bool b
1392 = (libreoffice_internal::ConstCharArrayDetector<T>::length
1393 <= sal_uInt32(pData->length))
1394 && rtl_ustr_asciil_reverseEquals_WithLength(
1395 pData->buffer,
1396 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1397 literal),
1398 libreoffice_internal::ConstCharArrayDetector<T>::length);
1399 if (b && rest != NULL) {
1400 *rest = copy(
1401 libreoffice_internal::ConstCharArrayDetector<T>::length);
1403 return b;
1407 Check whether this string starts with a given string, ignoring the case of
1408 ASCII letters.
1410 Character values between 65 and 90 (ASCII A-Z) are interpreted as
1411 values between 97 and 122 (ASCII a-z).
1412 This function can't be used for language specific comparison.
1414 @param str the substring to be compared
1416 @param rest if non-null, and this function returns true, then assign a
1417 copy of the remainder of this string to *rest. Available since
1418 LibreOffice 4.2
1420 @return true if and only if the given str appears as a substring at the
1421 start of this string, ignoring the case of ASCII letters ("A"--"Z" and
1422 "a"--"z")
1424 @since LibreOffice 4.0
1426 #if defined LIBO_INTERNAL_ONLY
1427 bool startsWithIgnoreAsciiCase(std::u16string_view sv, OUString * rest = nullptr) const {
1428 auto const b = matchIgnoreAsciiCase(sv);
1429 if (b && rest != nullptr) {
1430 *rest = copy(sv.size());
1432 return b;
1434 #else
1435 bool startsWithIgnoreAsciiCase(OUString const & str, OUString * rest = NULL)
1436 const
1438 bool b = matchIgnoreAsciiCase(str);
1439 if (b && rest != NULL) {
1440 *rest = copy(str.getLength());
1442 return b;
1444 #endif
1447 @overload
1448 This function accepts an ASCII string literal as its argument.
1449 @since LibreOffice 4.0
1451 template< typename T >
1452 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type
1453 startsWithIgnoreAsciiCase(T & literal, OUString * rest = NULL) const
1455 assert(
1456 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1457 bool b
1458 = (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths(
1459 pData->buffer,
1460 libreoffice_internal::ConstCharArrayDetector<T>::length,
1461 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1462 literal),
1463 libreoffice_internal::ConstCharArrayDetector<T>::length)
1464 == 0);
1465 if (b && rest != NULL) {
1466 *rest = copy(
1467 libreoffice_internal::ConstCharArrayDetector<T>::length);
1469 return b;
1473 Check whether this string ends with a given substring.
1475 @param str the substring to be compared
1477 @param rest if non-null, and this function returns true, then assign a
1478 copy of the remainder of this string to *rest. Available since
1479 LibreOffice 4.2
1481 @return true if and only if the given str appears as a substring at the
1482 end of this string
1484 @since LibreOffice 3.6
1486 #if defined LIBO_INTERNAL_ONLY
1487 bool endsWith(std::u16string_view sv, OUString * rest = nullptr) const {
1488 auto const b = sv.size() <= sal_uInt32(pData->length)
1489 && match(sv, pData->length - sv.size());
1490 if (b && rest != nullptr) {
1491 *rest = copy(0, (pData->length - sv.size()));
1493 return b;
1495 #else
1496 bool endsWith(OUString const & str, OUString * rest = NULL) const {
1497 bool b = str.getLength() <= getLength()
1498 && match(str, getLength() - str.getLength());
1499 if (b && rest != NULL) {
1500 *rest = copy(0, getLength() - str.getLength());
1502 return b;
1504 #endif
1507 @overload
1508 This function accepts an ASCII string literal as its argument.
1509 @since LibreOffice 3.6
1511 template< typename T >
1512 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type
1513 endsWith(T & literal, OUString * rest = NULL) const
1515 assert(
1516 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1517 bool b
1518 = (libreoffice_internal::ConstCharArrayDetector<T>::length
1519 <= sal_uInt32(pData->length))
1520 && rtl_ustr_asciil_reverseEquals_WithLength(
1521 (pData->buffer + pData->length
1522 - libreoffice_internal::ConstCharArrayDetector<T>::length),
1523 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1524 literal),
1525 libreoffice_internal::ConstCharArrayDetector<T>::length);
1526 if (b && rest != NULL) {
1527 *rest = copy(
1529 (getLength()
1530 - libreoffice_internal::ConstCharArrayDetector<T>::length));
1532 return b;
1536 Check whether this string ends with a given ASCII string.
1538 @param asciiStr a sequence of at least asciiStrLength ASCII characters
1539 (bytes in the range 0x00--0x7F)
1540 @param asciiStrLength the length of asciiStr; must be non-negative
1541 @return true if this string ends with asciiStr; otherwise, false is
1542 returned
1544 @since UDK 3.2.7
1546 bool endsWithAsciiL(char const * asciiStr, sal_Int32 asciiStrLength)
1547 const
1549 return asciiStrLength <= pData->length
1550 && rtl_ustr_asciil_reverseEquals_WithLength(
1551 pData->buffer + pData->length - asciiStrLength, asciiStr,
1552 asciiStrLength);
1556 Check whether this string ends with a given string, ignoring the case of
1557 ASCII letters.
1559 Character values between 65 and 90 (ASCII A-Z) are interpreted as
1560 values between 97 and 122 (ASCII a-z).
1561 This function can't be used for language specific comparison.
1563 @param str the substring to be compared
1565 @param rest if non-null, and this function returns true, then assign a
1566 copy of the remainder of this string to *rest. Available since
1567 LibreOffice 4.2
1569 @return true if and only if the given str appears as a substring at the
1570 end of this string, ignoring the case of ASCII letters ("A"--"Z" and
1571 "a"--"z")
1573 @since LibreOffice 3.6
1575 #if defined LIBO_INTERNAL_ONLY
1576 bool endsWithIgnoreAsciiCase(std::u16string_view sv, OUString * rest = nullptr) const {
1577 auto const b = sv.size() <= sal_uInt32(pData->length)
1578 && matchIgnoreAsciiCase(sv, pData->length - sv.size());
1579 if (b && rest != nullptr) {
1580 *rest = copy(0, pData->length - sv.size());
1582 return b;
1584 #else
1585 bool endsWithIgnoreAsciiCase(OUString const & str, OUString * rest = NULL) const
1587 bool b = str.getLength() <= getLength()
1588 && matchIgnoreAsciiCase(str, getLength() - str.getLength());
1589 if (b && rest != NULL) {
1590 *rest = copy(0, getLength() - str.getLength());
1592 return b;
1594 #endif
1597 @overload
1598 This function accepts an ASCII string literal as its argument.
1599 @since LibreOffice 3.6
1601 template< typename T >
1602 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type
1603 endsWithIgnoreAsciiCase(T & literal, OUString * rest = NULL) const
1605 assert(
1606 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1607 bool b
1608 = (libreoffice_internal::ConstCharArrayDetector<T>::length
1609 <= sal_uInt32(pData->length))
1610 && (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths(
1611 (pData->buffer + pData->length
1612 - libreoffice_internal::ConstCharArrayDetector<T>::length),
1613 libreoffice_internal::ConstCharArrayDetector<T>::length,
1614 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1615 literal),
1616 libreoffice_internal::ConstCharArrayDetector<T>::length)
1617 == 0);
1618 if (b && rest != NULL) {
1619 *rest = copy(
1621 (getLength()
1622 - libreoffice_internal::ConstCharArrayDetector<T>::length));
1624 return b;
1628 Check whether this string ends with a given ASCII string, ignoring the
1629 case of ASCII letters.
1631 @param asciiStr a sequence of at least asciiStrLength ASCII characters
1632 (bytes in the range 0x00--0x7F)
1633 @param asciiStrLength the length of asciiStr; must be non-negative
1634 @return true if this string ends with asciiStr, ignoring the case of ASCII
1635 letters ("A"--"Z" and "a"--"z"); otherwise, false is returned
1637 bool endsWithIgnoreAsciiCaseAsciiL(
1638 char const * asciiStr, sal_Int32 asciiStrLength) const
1640 return asciiStrLength <= pData->length
1641 && (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths(
1642 pData->buffer + pData->length - asciiStrLength,
1643 asciiStrLength, asciiStr, asciiStrLength)
1644 == 0);
1647 friend bool operator == ( const OUString& rStr1, const OUString& rStr2 )
1648 { return rStr1.equals(rStr2); }
1650 friend bool operator != ( const OUString& rStr1, const OUString& rStr2 )
1651 { return !(operator == ( rStr1, rStr2 )); }
1653 friend bool operator < ( const OUString& rStr1, const OUString& rStr2 )
1654 { return rStr1.compareTo( rStr2 ) < 0; }
1655 friend bool operator > ( const OUString& rStr1, const OUString& rStr2 )
1656 { return rStr1.compareTo( rStr2 ) > 0; }
1657 friend bool operator <= ( const OUString& rStr1, const OUString& rStr2 )
1658 { return rStr1.compareTo( rStr2 ) <= 0; }
1659 friend bool operator >= ( const OUString& rStr1, const OUString& rStr2 )
1660 { return rStr1.compareTo( rStr2 ) >= 0; }
1662 #if defined LIBO_INTERNAL_ONLY
1664 template<typename T> friend typename libreoffice_internal::CharPtrDetector<T, bool>::TypeUtf16
1665 operator ==(OUString const & s1, T const & s2) {
1666 return rtl_ustr_compare_WithLength(s1.getStr(), s1.getLength(), s2, rtl_ustr_getLength(s2))
1667 == 0;
1670 template<typename T>
1671 friend typename libreoffice_internal::NonConstCharArrayDetector<T, bool>::TypeUtf16
1672 operator ==(OUString const & s1, T & s2) {
1673 return rtl_ustr_compare_WithLength(s1.getStr(), s1.getLength(), s2, rtl_ustr_getLength(s2))
1674 == 0;
1677 template<typename T> friend typename libreoffice_internal::CharPtrDetector<T, bool>::TypeUtf16
1678 operator ==(T const & s1, OUString const & s2) {
1679 return rtl_ustr_compare_WithLength(s1, rtl_ustr_getLength(s1), s2.getStr(), s2.getLength())
1680 == 0;
1683 template<typename T>
1684 friend typename libreoffice_internal::NonConstCharArrayDetector<T, bool>::TypeUtf16
1685 operator ==(T & s1, OUString const & s2) {
1686 return rtl_ustr_compare_WithLength(s1, rtl_ustr_getLength(s1), s2.getStr(), s2.getLength())
1687 == 0;
1690 template<typename T> friend typename libreoffice_internal::CharPtrDetector<T, bool>::TypeUtf16
1691 operator !=(OUString const & s1, T const & s2) { return !(s1 == s2); }
1693 template<typename T>
1694 friend typename libreoffice_internal::NonConstCharArrayDetector<T, bool>::TypeUtf16
1695 operator !=(OUString const & s1, T & s2) { return !(s1 == s2); }
1697 template<typename T> friend typename libreoffice_internal::CharPtrDetector<T, bool>::TypeUtf16
1698 operator !=(T const & s1, OUString const & s2) { return !(s1 == s2); }
1700 template<typename T>
1701 friend typename libreoffice_internal::NonConstCharArrayDetector<T, bool>::TypeUtf16
1702 operator !=(T & s1, OUString const & s2) { return !(s1 == s2); }
1704 #else
1706 friend bool operator == ( const OUString& rStr1, const sal_Unicode * pStr2 )
1707 { return rStr1.compareTo( pStr2 ) == 0; }
1708 friend bool operator == ( const sal_Unicode * pStr1, const OUString& rStr2 )
1709 { return OUString( pStr1 ).compareTo( rStr2 ) == 0; }
1711 friend bool operator != ( const OUString& rStr1, const sal_Unicode * pStr2 )
1712 { return !(operator == ( rStr1, pStr2 )); }
1713 friend bool operator != ( const sal_Unicode * pStr1, const OUString& rStr2 )
1714 { return !(operator == ( pStr1, rStr2 )); }
1716 #endif
1719 * Compare string to an ASCII string literal.
1721 * This operator is equal to calling equalsAsciiL().
1723 * @since LibreOffice 3.6
1725 template< typename T >
1726 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator==( const OUString& rString, T& literal )
1728 assert(
1729 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1730 return rString.equalsAsciiL(
1731 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1732 libreoffice_internal::ConstCharArrayDetector<T>::length);
1735 * Compare string to an ASCII string literal.
1737 * This operator is equal to calling equalsAsciiL().
1739 * @since LibreOffice 3.6
1741 template< typename T >
1742 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator==( T& literal, const OUString& rString )
1744 assert(
1745 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1746 return rString.equalsAsciiL(
1747 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1748 libreoffice_internal::ConstCharArrayDetector<T>::length);
1751 * Compare string to an ASCII string literal.
1753 * This operator is equal to calling !equalsAsciiL().
1755 * @since LibreOffice 3.6
1757 template< typename T >
1758 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator!=( const OUString& rString, T& literal )
1760 assert(
1761 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1762 return !rString.equalsAsciiL(
1763 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1764 libreoffice_internal::ConstCharArrayDetector<T>::length);
1767 * Compare string to an ASCII string literal.
1769 * This operator is equal to calling !equalsAsciiL().
1771 * @since LibreOffice 3.6
1773 template< typename T >
1774 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator!=( T& literal, const OUString& rString )
1776 assert(
1777 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1778 return !rString.equalsAsciiL(
1779 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1780 libreoffice_internal::ConstCharArrayDetector<T>::length);
1783 #if defined LIBO_INTERNAL_ONLY
1784 /** @overload @since LibreOffice 5.3 */
1785 template<typename T> friend typename libreoffice_internal::ConstCharArrayDetector<T, bool>::TypeUtf16
1786 operator ==(OUString const & string, T & literal) {
1787 return
1788 rtl_ustr_reverseCompare_WithLength(
1789 string.pData->buffer, string.pData->length,
1790 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1791 literal),
1792 libreoffice_internal::ConstCharArrayDetector<T>::length)
1793 == 0;
1795 /** @overload @since LibreOffice 5.3 */
1796 template<typename T> friend typename libreoffice_internal::ConstCharArrayDetector<T, bool>::TypeUtf16
1797 operator ==(T & literal, OUString const & string) {
1798 return
1799 rtl_ustr_reverseCompare_WithLength(
1800 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1801 literal),
1802 libreoffice_internal::ConstCharArrayDetector<T>::length,
1803 string.pData->buffer, string.pData->length)
1804 == 0;
1806 /** @overload @since LibreOffice 5.3 */
1807 template<typename T> friend typename libreoffice_internal::ConstCharArrayDetector<T, bool>::TypeUtf16
1808 operator !=(OUString const & string, T & literal) {
1809 return
1810 rtl_ustr_reverseCompare_WithLength(
1811 string.pData->buffer, string.pData->length,
1812 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1813 literal),
1814 libreoffice_internal::ConstCharArrayDetector<T>::length)
1815 != 0;
1817 /** @overload @since LibreOffice 5.3 */
1818 template<typename T> friend typename libreoffice_internal::ConstCharArrayDetector<T, bool>::TypeUtf16
1819 operator !=(T & literal, OUString const & string) {
1820 return
1821 rtl_ustr_reverseCompare_WithLength(
1822 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1823 literal),
1824 libreoffice_internal::ConstCharArrayDetector<T>::length,
1825 string.pData->buffer, string.pData->length)
1826 != 0;
1828 #endif
1830 #if defined LIBO_INTERNAL_ONLY
1831 /// @cond INTERNAL
1833 /* Comparison between OUString and OUStringLiteral.
1835 @since LibreOffice 5.0
1838 template<std::size_t N>
1839 friend bool operator ==(OUString const & lhs, OUStringLiteral<N> const & rhs) {
1840 return
1841 rtl_ustr_reverseCompare_WithLength(
1842 lhs.pData->buffer, lhs.pData->length, rhs.getStr(), rhs.getLength())
1843 == 0;
1846 template<std::size_t N>
1847 friend bool operator !=(OUString const & lhs, OUStringLiteral<N> const & rhs) {
1848 return
1849 rtl_ustr_reverseCompare_WithLength(
1850 lhs.pData->buffer, lhs.pData->length, rhs.getStr(), rhs.getLength())
1851 != 0;
1854 template<std::size_t N>
1855 friend bool operator <(OUString const & lhs, OUStringLiteral<N> const & rhs) {
1856 return
1857 (rtl_ustr_compare_WithLength(
1858 lhs.pData->buffer, lhs.pData->length, rhs.getStr(), rhs.getLength()))
1859 < 0;
1862 template<std::size_t N>
1863 friend bool operator <=(OUString const & lhs, OUStringLiteral<N> const & rhs) {
1864 return
1865 (rtl_ustr_compare_WithLength(
1866 lhs.pData->buffer, lhs.pData->length, rhs.getStr(), rhs.getLength()))
1867 <= 0;
1870 template<std::size_t N>
1871 friend bool operator >(OUString const & lhs, OUStringLiteral<N> const & rhs) {
1872 return
1873 (rtl_ustr_compare_WithLength(
1874 lhs.pData->buffer, lhs.pData->length, rhs.getStr(), rhs.getLength()))
1875 > 0;
1878 template<std::size_t N>
1879 friend bool operator >=(OUString const & lhs, OUStringLiteral<N> const & rhs) {
1880 return
1881 (rtl_ustr_compare_WithLength(
1882 lhs.pData->buffer, lhs.pData->length, rhs.getStr(), rhs.getLength()))
1883 >= 0;
1886 template<std::size_t N>
1887 friend bool operator ==(OUStringLiteral<N> const & lhs, OUString const & rhs) {
1888 return
1889 rtl_ustr_reverseCompare_WithLength(
1890 lhs.getStr(), lhs.getLength(), rhs.pData->buffer, rhs.pData->length)
1891 == 0;
1894 template<std::size_t N>
1895 friend bool operator !=(OUStringLiteral<N> const & lhs, OUString const & rhs) {
1896 return
1897 rtl_ustr_reverseCompare_WithLength(
1898 lhs.getStr(), lhs.getLength(), rhs.pData->buffer, rhs.pData->length)
1899 != 0;
1902 template<std::size_t N>
1903 friend bool operator <(OUStringLiteral<N> const & lhs, OUString const & rhs) {
1904 return
1905 (rtl_ustr_compare_WithLength(
1906 lhs.getStr(), lhs.getLength(), rhs.pData->buffer, rhs.pData->length))
1907 < 0;
1910 template<std::size_t N>
1911 friend bool operator <=(OUStringLiteral<N> const & lhs, OUString const & rhs) {
1912 return
1913 (rtl_ustr_compare_WithLength(
1914 lhs.getStr(), lhs.getLength(), rhs.pData->buffer, rhs.pData->length))
1915 <= 0;
1918 template<std::size_t N>
1919 friend bool operator >(OUStringLiteral<N> const & lhs, OUString const & rhs) {
1920 return
1921 (rtl_ustr_compare_WithLength(
1922 lhs.getStr(), lhs.getLength(), rhs.pData->buffer, rhs.pData->length))
1923 > 0;
1926 template<std::size_t N>
1927 friend bool operator >=(OUStringLiteral<N> const & lhs, OUString const & rhs) {
1928 return
1929 (rtl_ustr_compare_WithLength(
1930 lhs.getStr(), lhs.getLength(), rhs.pData->buffer, rhs.pData->length))
1931 >= 0;
1934 /// @endcond
1935 #endif
1937 #if defined LIBO_INTERNAL_ONLY
1938 friend bool operator ==(OUString const & lhs, std::u16string_view rhs) {
1939 return
1940 rtl_ustr_reverseCompare_WithLength(
1941 lhs.pData->buffer, lhs.pData->length, rhs.data(), rhs.size())
1942 == 0;
1945 friend bool operator !=(OUString const & lhs, std::u16string_view rhs) {
1946 return
1947 rtl_ustr_reverseCompare_WithLength(
1948 lhs.pData->buffer, lhs.pData->length, rhs.data(), rhs.size())
1949 != 0;
1952 friend bool operator <(OUString const & lhs, std::u16string_view rhs) {
1953 return
1954 (rtl_ustr_compare_WithLength(
1955 lhs.pData->buffer, lhs.pData->length, rhs.data(), rhs.size()))
1956 < 0;
1959 friend bool operator <=(OUString const & lhs, std::u16string_view rhs) {
1960 return
1961 (rtl_ustr_compare_WithLength(
1962 lhs.pData->buffer, lhs.pData->length, rhs.data(), rhs.size()))
1963 <= 0;
1966 friend bool operator >(OUString const & lhs, std::u16string_view rhs) {
1967 return
1968 (rtl_ustr_compare_WithLength(
1969 lhs.pData->buffer, lhs.pData->length, rhs.data(), rhs.size()))
1970 > 0;
1973 friend bool operator >=(OUString const & lhs, std::u16string_view rhs) {
1974 return
1975 (rtl_ustr_compare_WithLength(
1976 lhs.pData->buffer, lhs.pData->length, rhs.data(), rhs.size()))
1977 >= 0;
1980 friend bool operator ==(std::u16string_view lhs, OUString const & rhs) {
1981 return
1982 rtl_ustr_reverseCompare_WithLength(
1983 lhs.data(), lhs.size(), rhs.pData->buffer, rhs.pData->length)
1984 == 0;
1987 friend bool operator !=(std::u16string_view lhs, OUString const & rhs) {
1988 return
1989 rtl_ustr_reverseCompare_WithLength(
1990 lhs.data(), lhs.size(), rhs.pData->buffer, rhs.pData->length)
1991 != 0;
1994 friend bool operator <(std::u16string_view lhs, OUString const & rhs) {
1995 return
1996 (rtl_ustr_compare_WithLength(
1997 lhs.data(), lhs.size(), rhs.pData->buffer, rhs.pData->length))
1998 < 0;
2001 friend bool operator <=(std::u16string_view lhs, OUString const & rhs) {
2002 return
2003 (rtl_ustr_compare_WithLength(
2004 lhs.data(), lhs.size(), rhs.pData->buffer, rhs.pData->length))
2005 <= 0;
2008 friend bool operator >(std::u16string_view lhs, OUString const & rhs) {
2009 return
2010 (rtl_ustr_compare_WithLength(
2011 lhs.data(), lhs.size(), rhs.pData->buffer, rhs.pData->length))
2012 > 0;
2015 friend bool operator >=(std::u16string_view lhs, OUString const & rhs) {
2016 return
2017 (rtl_ustr_compare_WithLength(
2018 lhs.data(), lhs.size(), rhs.pData->buffer, rhs.pData->length))
2019 >= 0;
2021 #endif
2024 Returns a hashcode for this string.
2026 @return a hash code value for this object.
2028 @see rtl::OUStringHash for convenient use of std::unordered_map
2030 sal_Int32 hashCode() const
2032 return rtl_ustr_hashCode_WithLength( pData->buffer, pData->length );
2036 Returns the index within this string of the first occurrence of the
2037 specified character, starting the search at the specified index.
2039 @param ch character to be located.
2040 @param fromIndex the index to start the search from.
2041 The index must be greater than or equal to 0
2042 and less than or equal to the string length.
2043 @return the index of the first occurrence of the character in the
2044 character sequence represented by this string that is
2045 greater than or equal to fromIndex, or
2046 -1 if the character does not occur.
2048 sal_Int32 indexOf( sal_Unicode ch, sal_Int32 fromIndex = 0 ) const
2050 sal_Int32 ret = rtl_ustr_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch );
2051 return (ret < 0 ? ret : ret+fromIndex);
2055 Returns the index within this string of the last occurrence of the
2056 specified character, searching backward starting at the end.
2058 @param ch character to be located.
2059 @return the index of the last occurrence of the character in the
2060 character sequence represented by this string, or
2061 -1 if the character does not occur.
2063 sal_Int32 lastIndexOf( sal_Unicode ch ) const
2065 return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch );
2069 Returns the index within this string of the last occurrence of the
2070 specified character, searching backward starting before the specified
2071 index.
2073 @param ch character to be located.
2074 @param fromIndex the index before which to start the search.
2075 @return the index of the last occurrence of the character in the
2076 character sequence represented by this string that
2077 is less than fromIndex, or -1
2078 if the character does not occur before that point.
2080 sal_Int32 lastIndexOf( sal_Unicode ch, sal_Int32 fromIndex ) const
2082 return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch );
2086 Returns the index within this string of the first occurrence of the
2087 specified substring, starting at the specified index.
2089 If str doesn't include any character, always -1 is
2090 returned. This is also the case, if both strings are empty.
2092 @param str the substring to search for.
2093 @param fromIndex the index to start the search from.
2094 @return If the string argument occurs one or more times as a substring
2095 within this string at the starting index, then the index
2096 of the first character of the first such substring is
2097 returned. If it does not occur as a substring starting
2098 at fromIndex or beyond, -1 is returned.
2100 #if defined LIBO_INTERNAL_ONLY
2101 sal_Int32 indexOf(std::u16string_view sv, sal_Int32 fromIndex = 0) const {
2102 auto const n = rtl_ustr_indexOfStr_WithLength(
2103 pData->buffer + fromIndex, pData->length - fromIndex, sv.data(), sv.size());
2104 return n < 0 ? n : n + fromIndex;
2106 #else
2107 sal_Int32 indexOf( const OUString & str, sal_Int32 fromIndex = 0 ) const
2109 sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
2110 str.pData->buffer, str.pData->length );
2111 return (ret < 0 ? ret : ret+fromIndex);
2113 #endif
2116 @overload
2117 This function accepts an ASCII string literal as its argument.
2118 @since LibreOffice 3.6
2120 template< typename T >
2121 typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const
2123 assert(
2124 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
2125 sal_Int32 n = rtl_ustr_indexOfAscii_WithLength(
2126 pData->buffer + fromIndex, pData->length - fromIndex,
2127 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
2128 libreoffice_internal::ConstCharArrayDetector<T>::length);
2129 return n < 0 ? n : n + fromIndex;
2133 Returns the index within this string of the first occurrence of the
2134 specified ASCII substring, starting at the specified index.
2136 @param str
2137 the substring to be searched for. Need not be null-terminated, but must
2138 be at least as long as the specified len. Must only contain characters
2139 in the ASCII range 0x00--7F.
2141 @param len
2142 the length of the substring; must be non-negative.
2144 @param fromIndex
2145 the index to start the search from. Must be in the range from zero to
2146 the length of this string, inclusive.
2148 @return
2149 the index (starting at 0) of the first character of the first occurrence
2150 of the substring within this string starting at the given fromIndex, or
2151 -1 if the substring does not occur. If len is zero, -1 is returned.
2153 @since UDK 3.2.7
2155 sal_Int32 indexOfAsciiL(
2156 char const * str, sal_Int32 len, sal_Int32 fromIndex = 0) const
2158 sal_Int32 ret = rtl_ustr_indexOfAscii_WithLength(
2159 pData->buffer + fromIndex, pData->length - fromIndex, str, len);
2160 return ret < 0 ? ret : ret + fromIndex;
2163 // This overload is left undefined, to detect calls of indexOfAsciiL that
2164 // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
2165 // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
2166 // platforms):
2167 #if SAL_TYPES_SIZEOFLONG == 8
2168 void indexOfAsciiL(char const *, sal_Int32 len, rtl_TextEncoding) const;
2169 #endif
2172 Returns the index within this string of the last occurrence of
2173 the specified substring, searching backward starting at the end.
2175 The returned index indicates the starting index of the substring
2176 in this string.
2177 If str doesn't include any character, always -1 is
2178 returned. This is also the case, if both strings are empty.
2180 @param str the substring to search for.
2181 @return If the string argument occurs one or more times as a substring
2182 within this string, then the index of the first character of
2183 the last such substring is returned. If it does not occur as
2184 a substring, -1 is returned.
2186 #if defined LIBO_INTERNAL_ONLY
2187 sal_Int32 lastIndexOf(std::u16string_view sv) const {
2188 return rtl_ustr_lastIndexOfStr_WithLength(
2189 pData->buffer, pData->length, sv.data(), sv.size());
2191 #else
2192 sal_Int32 lastIndexOf( const OUString & str ) const
2194 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
2195 str.pData->buffer, str.pData->length );
2197 #endif
2200 Returns the index within this string of the last occurrence of
2201 the specified substring, searching backward starting before the specified
2202 index.
2204 The returned index indicates the starting index of the substring
2205 in this string.
2206 If str doesn't include any character, always -1 is
2207 returned. This is also the case, if both strings are empty.
2209 @param str the substring to search for.
2210 @param fromIndex the index before which to start the search.
2211 @return If the string argument occurs one or more times as a substring
2212 within this string before the starting index, then the index
2213 of the first character of the last such substring is
2214 returned. Otherwise, -1 is returned.
2216 #if defined LIBO_INTERNAL_ONLY
2217 sal_Int32 lastIndexOf(std::u16string_view sv, sal_Int32 fromIndex) const {
2218 return rtl_ustr_lastIndexOfStr_WithLength(pData->buffer, fromIndex, sv.data(), sv.size());
2220 #else
2221 sal_Int32 lastIndexOf( const OUString & str, sal_Int32 fromIndex ) const
2223 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
2224 str.pData->buffer, str.pData->length );
2226 #endif
2229 @overload
2230 This function accepts an ASCII string literal as its argument.
2231 @since LibreOffice 3.6
2233 template< typename T >
2234 typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type lastIndexOf( T& literal ) const
2236 assert(
2237 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
2238 return rtl_ustr_lastIndexOfAscii_WithLength(
2239 pData->buffer, pData->length,
2240 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
2241 libreoffice_internal::ConstCharArrayDetector<T>::length);
2245 Returns the index within this string of the last occurrence of the
2246 specified ASCII substring.
2248 @param str
2249 the substring to be searched for. Need not be null-terminated, but must
2250 be at least as long as the specified len. Must only contain characters
2251 in the ASCII range 0x00--7F.
2253 @param len
2254 the length of the substring; must be non-negative.
2256 @return
2257 the index (starting at 0) of the first character of the last occurrence
2258 of the substring within this string, or -1 if the substring does not
2259 occur. If len is zero, -1 is returned.
2261 @since UDK 3.2.7
2263 sal_Int32 lastIndexOfAsciiL(char const * str, sal_Int32 len) const
2265 return rtl_ustr_lastIndexOfAscii_WithLength(
2266 pData->buffer, pData->length, str, len);
2270 Returns a new string that is a substring of this string.
2272 The substring begins at the specified beginIndex. If
2273 beginIndex is negative or be greater than the length of
2274 this string, behaviour is undefined.
2276 @param beginIndex the beginning index, inclusive.
2277 @return the specified substring.
2279 SAL_WARN_UNUSED_RESULT OUString copy( sal_Int32 beginIndex ) const
2281 return copy(beginIndex, getLength() - beginIndex);
2285 Returns a new string that is a substring of this string.
2287 The substring begins at the specified beginIndex and contains count
2288 characters. If either beginIndex or count are negative,
2289 or beginIndex + count are greater than the length of this string
2290 then behaviour is undefined.
2292 @param beginIndex the beginning index, inclusive.
2293 @param count the number of characters.
2294 @return the specified substring.
2296 SAL_WARN_UNUSED_RESULT OUString copy( sal_Int32 beginIndex, sal_Int32 count ) const
2298 rtl_uString *pNew = NULL;
2299 rtl_uString_newFromSubString( &pNew, pData, beginIndex, count );
2300 return OUString( pNew, SAL_NO_ACQUIRE );
2303 #if defined LIBO_INTERNAL_ONLY
2305 Returns a std::u16string_view that is a view of a substring of this string.
2307 The substring begins at the specified beginIndex. If
2308 beginIndex is negative or be greater than the length of
2309 this string, behaviour is undefined.
2311 @param beginIndex the beginning index, inclusive.
2312 @return the specified substring.
2314 SAL_WARN_UNUSED_RESULT std::u16string_view subView( sal_Int32 beginIndex ) const
2316 assert(beginIndex >= 0);
2317 assert(beginIndex <= getLength());
2318 return subView(beginIndex, getLength() - beginIndex);
2322 Returns a std::u16string_view that is a view of a substring of this string.
2324 The substring begins at the specified beginIndex and contains count
2325 characters. If either beginIndex or count are negative,
2326 or beginIndex + count are greater than the length of this string
2327 then behaviour is undefined.
2329 @param beginIndex the beginning index, inclusive.
2330 @param count the number of characters.
2331 @return the specified substring.
2333 SAL_WARN_UNUSED_RESULT std::u16string_view subView( sal_Int32 beginIndex, sal_Int32 count ) const
2335 assert(beginIndex >= 0);
2336 assert(count >= 0);
2337 assert(beginIndex <= getLength());
2338 assert(count <= getLength() - beginIndex);
2339 return std::u16string_view(*this).substr(beginIndex, count);
2341 #endif
2343 #ifndef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
2345 Concatenates the specified string to the end of this string.
2347 @param str the string that is concatenated to the end
2348 of this string.
2349 @return a string that represents the concatenation of this string
2350 followed by the string argument.
2352 SAL_WARN_UNUSED_RESULT OUString concat( const OUString & str ) const
2354 rtl_uString* pNew = NULL;
2355 rtl_uString_newConcat( &pNew, pData, str.pData );
2356 return OUString( pNew, SAL_NO_ACQUIRE );
2358 #endif
2360 #ifndef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
2361 friend OUString operator+( const OUString& rStr1, const OUString& rStr2 )
2363 return rStr1.concat( rStr2 );
2365 #endif
2368 Returns a new string resulting from replacing n = count characters
2369 from position index in this string with newStr.
2371 @param index the replacing index in str.
2372 The index must be greater than or equal to 0 and
2373 less than or equal to the length of the string.
2374 @param count the count of characters that will be replaced
2375 The count must be greater than or equal to 0 and
2376 less than or equal to the length of the string minus index.
2377 @param newStr the new substring.
2378 @return the new string.
2380 SAL_WARN_UNUSED_RESULT OUString replaceAt( sal_Int32 index, sal_Int32 count, const OUString& newStr ) const
2382 rtl_uString* pNew = NULL;
2383 rtl_uString_newReplaceStrAt( &pNew, pData, index, count, newStr.pData );
2384 return OUString( pNew, SAL_NO_ACQUIRE );
2388 Returns a new string resulting from replacing all occurrences of
2389 oldChar in this string with newChar.
2391 If the character oldChar does not occur in the character sequence
2392 represented by this object, then the string is assigned with
2393 str.
2395 @param oldChar the old character.
2396 @param newChar the new character.
2397 @return a string derived from this string by replacing every
2398 occurrence of oldChar with newChar.
2400 SAL_WARN_UNUSED_RESULT OUString replace( sal_Unicode oldChar, sal_Unicode newChar ) const
2402 rtl_uString* pNew = NULL;
2403 rtl_uString_newReplace( &pNew, pData, oldChar, newChar );
2404 return OUString( pNew, SAL_NO_ACQUIRE );
2408 Returns a new string resulting from replacing the first occurrence of a
2409 given substring with another substring.
2411 @param from the substring to be replaced
2413 @param to the replacing substring
2415 @param[in,out] index pointer to a start index; if the pointer is
2416 non-null: upon entry to the function, its value is the index into this
2417 string at which to start searching for the \p from substring, the value
2418 must be non-negative and not greater than this string's length; upon exiting
2419 the function its value is the index into this string at which the
2420 replacement took place or -1 if no replacement took place; if the pointer
2421 is null, searching always starts at index 0
2423 @since LibreOffice 3.6
2425 #if defined LIBO_INTERNAL_ONLY
2426 [[nodiscard]] OUString replaceFirst(
2427 std::u16string_view from, std::u16string_view to, sal_Int32 * index = nullptr) const
2429 rtl_uString * s = nullptr;
2430 sal_Int32 i = 0;
2431 rtl_uString_newReplaceFirstUtf16LUtf16L(
2432 &s, pData, from.data(), from.size(), to.data(), to.size(),
2433 index == nullptr ? &i : index);
2434 return OUString(s, SAL_NO_ACQUIRE);
2436 #else
2437 SAL_WARN_UNUSED_RESULT OUString replaceFirst(
2438 OUString const & from, OUString const & to, sal_Int32 * index = NULL) const
2440 rtl_uString * s = NULL;
2441 sal_Int32 i = 0;
2442 rtl_uString_newReplaceFirst(
2443 &s, pData, from.pData, to.pData, index == NULL ? &i : index);
2444 return OUString(s, SAL_NO_ACQUIRE);
2446 #endif
2449 Returns a new string resulting from replacing the first occurrence of a
2450 given substring with another substring.
2452 @param from ASCII string literal, the substring to be replaced
2454 @param to the replacing substring
2456 @param[in,out] index pointer to a start index; if the pointer is
2457 non-null: upon entry to the function, its value is the index into the this
2458 string at which to start searching for the \p from substring, the value
2459 must be non-negative and not greater than this string's length; upon exiting
2460 the function its value is the index into this string at which the
2461 replacement took place or -1 if no replacement took place; if the pointer
2462 is null, searching always starts at index 0
2464 @since LibreOffice 3.6
2466 #if defined LIBO_INTERNAL_ONLY
2467 template<typename T> [[nodiscard]]
2468 typename libreoffice_internal::ConstCharArrayDetector<T, OUString >::Type replaceFirst(
2469 T & from, std::u16string_view to, sal_Int32 * index = nullptr) const
2471 assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(from));
2472 rtl_uString * s = nullptr;
2473 sal_Int32 i = 0;
2474 rtl_uString_newReplaceFirstAsciiLUtf16L(
2475 &s, pData, libreoffice_internal::ConstCharArrayDetector<T>::toPointer(from),
2476 libreoffice_internal::ConstCharArrayDetector<T>::length, to.data(), to.size(),
2477 index == nullptr ? &i : index);
2478 return OUString(s, SAL_NO_ACQUIRE);
2480 #else
2481 template< typename T >
2482 SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T, OUString >::Type replaceFirst( T& from, OUString const & to,
2483 sal_Int32 * index = NULL) const
2485 assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(from));
2486 rtl_uString * s = NULL;
2487 sal_Int32 i = 0;
2488 rtl_uString_newReplaceFirstAsciiL(
2489 &s, pData,
2490 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(from),
2491 libreoffice_internal::ConstCharArrayDetector<T>::length, to.pData,
2492 index == NULL ? &i : index);
2493 return OUString(s, SAL_NO_ACQUIRE);
2495 #endif
2498 Returns a new string resulting from replacing the first occurrence of a
2499 given substring with another substring.
2501 @param from the substring to be replaced
2503 @param to ASCII string literal, the replacing substring
2505 @param[in,out] index pointer to a start index; if the pointer is
2506 non-null: upon entry to the function, its value is the index into the this
2507 string at which to start searching for the \p from substring, the value
2508 must be non-negative and not greater than this string's length; upon exiting
2509 the function its value is the index into this string at which the
2510 replacement took place or -1 if no replacement took place; if the pointer
2511 is null, searching always starts at index 0
2513 @since LibreOffice 5.1
2515 #if defined LIBO_INTERNAL_ONLY
2516 template<typename T> [[nodiscard]]
2517 typename libreoffice_internal::ConstCharArrayDetector<T, OUString >::Type replaceFirst(
2518 std::u16string_view from, T & to, sal_Int32 * index = nullptr) const
2520 assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(to));
2521 rtl_uString * s = nullptr;
2522 sal_Int32 i = 0;
2523 rtl_uString_newReplaceFirstUtf16LAsciiL(
2524 &s, pData, from.data(), from.size(),
2525 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(to),
2526 libreoffice_internal::ConstCharArrayDetector<T>::length, index == nullptr ? &i : index);
2527 return OUString(s, SAL_NO_ACQUIRE);
2529 #else
2530 template< typename T >
2531 SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T, OUString >::Type replaceFirst( OUString const & from, T& to,
2532 sal_Int32 * index = NULL) const
2534 assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(to));
2535 rtl_uString * s = NULL;
2536 sal_Int32 i = 0;
2537 rtl_uString_newReplaceFirstToAsciiL(
2538 &s, pData, from.pData,
2539 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(to),
2540 libreoffice_internal::ConstCharArrayDetector<T>::length,
2541 index == NULL ? &i : index);
2542 return OUString(s, SAL_NO_ACQUIRE);
2544 #endif
2547 Returns a new string resulting from replacing the first occurrence of a
2548 given substring with another substring.
2550 @param from ASCII string literal, the substring to be replaced
2552 @param to ASCII string literal, the substring to be replaced
2554 @param[in,out] index pointer to a start index; if the pointer is
2555 non-null: upon entry to the function, its value is the index into the this
2556 string at which to start searching for the \p from substring, the value
2557 must be non-negative and not greater than this string's length; upon exiting
2558 the function its value is the index into this string at which the
2559 replacement took place or -1 if no replacement took place; if the pointer
2560 is null, searching always starts at index 0
2562 @since LibreOffice 3.6
2564 template< typename T1, typename T2 >
2565 SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T1, typename libreoffice_internal::ConstCharArrayDetector< T2, OUString >::Type >::Type
2566 replaceFirst( T1& from, T2& to, sal_Int32 * index = NULL) const
2568 assert(libreoffice_internal::ConstCharArrayDetector<T1>::isValid(from));
2569 assert(libreoffice_internal::ConstCharArrayDetector<T2>::isValid(to));
2570 rtl_uString * s = NULL;
2571 sal_Int32 i = 0;
2572 rtl_uString_newReplaceFirstAsciiLAsciiL(
2573 &s, pData,
2574 libreoffice_internal::ConstCharArrayDetector<T1>::toPointer(from),
2575 libreoffice_internal::ConstCharArrayDetector<T1>::length,
2576 libreoffice_internal::ConstCharArrayDetector<T2>::toPointer(to),
2577 libreoffice_internal::ConstCharArrayDetector<T2>::length,
2578 index == NULL ? &i : index);
2579 return OUString(s, SAL_NO_ACQUIRE);
2583 Returns a new string resulting from replacing all occurrences of a given
2584 substring with another substring.
2586 Replacing subsequent occurrences picks up only after a given replacement.
2587 That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
2589 @param from the substring to be replaced
2591 @param to the replacing substring
2593 @param fromIndex the position in the string where we will begin searching
2595 @since LibreOffice 4.0
2597 #if defined LIBO_INTERNAL_ONLY
2598 [[nodiscard]] OUString replaceAll(
2599 std::u16string_view from, std::u16string_view to, sal_Int32 fromIndex = 0) const
2601 rtl_uString * s = nullptr;
2602 rtl_uString_newReplaceAllFromIndexUtf16LUtf16L(
2603 &s, pData, from.data(), from.size(), to.data(), to.size(), fromIndex);
2604 return OUString(s, SAL_NO_ACQUIRE);
2606 #else
2607 SAL_WARN_UNUSED_RESULT OUString replaceAll(
2608 OUString const & from, OUString const & to, sal_Int32 fromIndex = 0) const
2610 rtl_uString * s = NULL;
2611 rtl_uString_newReplaceAllFromIndex(&s, pData, from.pData, to.pData, fromIndex);
2612 return OUString(s, SAL_NO_ACQUIRE);
2614 #endif
2617 Returns a new string resulting from replacing all occurrences of a given
2618 substring with another substring.
2620 Replacing subsequent occurrences picks up only after a given replacement.
2621 That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
2623 @param from ASCII string literal, the substring to be replaced
2625 @param to the replacing substring
2627 @since LibreOffice 3.6
2629 #if defined LIBO_INTERNAL_ONLY
2630 template<typename T> [[nodiscard]]
2631 typename libreoffice_internal::ConstCharArrayDetector<T, OUString >::Type replaceAll(
2632 T & from, std::u16string_view to) const
2634 assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(from));
2635 rtl_uString * s = nullptr;
2636 rtl_uString_newReplaceAllAsciiLUtf16L(
2637 &s, pData, libreoffice_internal::ConstCharArrayDetector<T>::toPointer(from),
2638 libreoffice_internal::ConstCharArrayDetector<T>::length, to.data(), to.size());
2639 return OUString(s, SAL_NO_ACQUIRE);
2641 #else
2642 template< typename T >
2643 SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T, OUString >::Type replaceAll( T& from, OUString const & to) const
2645 assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(from));
2646 rtl_uString * s = NULL;
2647 rtl_uString_newReplaceAllAsciiL(
2648 &s, pData,
2649 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(from),
2650 libreoffice_internal::ConstCharArrayDetector<T>::length, to.pData);
2651 return OUString(s, SAL_NO_ACQUIRE);
2653 #endif
2656 Returns a new string resulting from replacing all occurrences of a given
2657 substring with another substring.
2659 Replacing subsequent occurrences picks up only after a given replacement.
2660 That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
2662 @param from the substring to be replaced
2664 @param to ASCII string literal, the replacing substring
2666 @since LibreOffice 5.1
2668 #if defined LIBO_INTERNAL_ONLY
2669 template<typename T> [[nodiscard]]
2670 typename libreoffice_internal::ConstCharArrayDetector<T, OUString >::Type replaceAll(
2671 std::u16string_view from, T & to) const
2673 assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(to));
2674 rtl_uString * s = nullptr;
2675 rtl_uString_newReplaceAllUtf16LAsciiL(
2676 &s, pData, from.data(), from.size(),
2677 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(to),
2678 libreoffice_internal::ConstCharArrayDetector<T>::length);
2679 return OUString(s, SAL_NO_ACQUIRE);
2681 #else
2682 template< typename T >
2683 SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T, OUString >::Type replaceAll( OUString const & from, T& to) const
2685 assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(to));
2686 rtl_uString * s = NULL;
2687 rtl_uString_newReplaceAllToAsciiL(
2688 &s, pData, from.pData,
2689 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(to),
2690 libreoffice_internal::ConstCharArrayDetector<T>::length);
2691 return OUString(s, SAL_NO_ACQUIRE);
2693 #endif
2696 Returns a new string resulting from replacing all occurrences of a given
2697 substring with another substring.
2699 Replacing subsequent occurrences picks up only after a given replacement.
2700 That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
2702 @param from ASCII string literal, the substring to be replaced
2704 @param to ASCII string literal, the substring to be replaced
2706 @since LibreOffice 3.6
2708 template< typename T1, typename T2 >
2709 SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T1, typename libreoffice_internal::ConstCharArrayDetector< T2, OUString >::Type >::Type
2710 replaceAll( T1& from, T2& to ) const
2712 assert(libreoffice_internal::ConstCharArrayDetector<T1>::isValid(from));
2713 assert(libreoffice_internal::ConstCharArrayDetector<T2>::isValid(to));
2714 rtl_uString * s = NULL;
2715 rtl_uString_newReplaceAllAsciiLAsciiL(
2716 &s, pData,
2717 libreoffice_internal::ConstCharArrayDetector<T1>::toPointer(from),
2718 libreoffice_internal::ConstCharArrayDetector<T1>::length,
2719 libreoffice_internal::ConstCharArrayDetector<T2>::toPointer(to),
2720 libreoffice_internal::ConstCharArrayDetector<T2>::length);
2721 return OUString(s, SAL_NO_ACQUIRE);
2725 Converts from this string all ASCII uppercase characters (65-90)
2726 to ASCII lowercase characters (97-122).
2728 This function can't be used for language specific conversion.
2729 If the string doesn't contain characters which must be converted,
2730 then the new string is assigned with str.
2732 @return the string, converted to ASCII lowercase.
2734 SAL_WARN_UNUSED_RESULT OUString toAsciiLowerCase() const
2736 rtl_uString* pNew = NULL;
2737 rtl_uString_newToAsciiLowerCase( &pNew, pData );
2738 return OUString( pNew, SAL_NO_ACQUIRE );
2742 Converts from this string all ASCII lowercase characters (97-122)
2743 to ASCII uppercase characters (65-90).
2745 This function can't be used for language specific conversion.
2746 If the string doesn't contain characters which must be converted,
2747 then the new string is assigned with str.
2749 @return the string, converted to ASCII uppercase.
2751 SAL_WARN_UNUSED_RESULT OUString toAsciiUpperCase() const
2753 rtl_uString* pNew = NULL;
2754 rtl_uString_newToAsciiUpperCase( &pNew, pData );
2755 return OUString( pNew, SAL_NO_ACQUIRE );
2759 Returns a new string resulting from removing white space from both ends
2760 of the string.
2762 All characters that have codes less than or equal to
2763 32 (the space character), and Unicode General Punctuation area Space
2764 and some Control characters are considered to be white space (see
2765 rtl_ImplIsWhitespace).
2766 If the string doesn't contain white spaces at both ends,
2767 then the new string is assigned with str.
2769 @return the string, with white space removed from the front and end.
2771 SAL_WARN_UNUSED_RESULT OUString trim() const
2773 rtl_uString* pNew = NULL;
2774 rtl_uString_newTrim( &pNew, pData );
2775 return OUString( pNew, SAL_NO_ACQUIRE );
2779 Returns a token in the string.
2781 Example:
2782 sal_Int32 nIndex = 0;
2786 OUString aToken = aStr.getToken( 0, ';', nIndex );
2789 while ( nIndex >= 0 );
2791 @param token the number of the token to return
2792 @param cTok the character which separate the tokens.
2793 @param index the position at which the token is searched in the
2794 string.
2795 The index must not be greater than the length of the
2796 string.
2797 This param is set to the position of the
2798 next token or to -1, if it is the last token.
2799 @return the token; if either token or index is negative, an empty token
2800 is returned (and index is set to -1)
2802 OUString getToken( sal_Int32 token, sal_Unicode cTok, sal_Int32& index ) const
2804 rtl_uString * pNew = NULL;
2805 index = rtl_uString_getToken( &pNew, pData, token, cTok, index );
2806 return OUString( pNew, SAL_NO_ACQUIRE );
2810 Returns a token from the string.
2812 The same as getToken(sal_Int32, sal_Unicode, sal_Int32 &), but always
2813 passing in 0 as the start index in the third argument.
2815 @param count the number of the token to return, starting with 0
2816 @param separator the character which separates the tokens
2818 @return the given token, or an empty string
2820 @since LibreOffice 3.6
2822 OUString getToken(sal_Int32 count, sal_Unicode separator) const {
2823 sal_Int32 n = 0;
2824 return getToken(count, separator, n);
2828 Returns the Boolean value from this string.
2830 This function can't be used for language specific conversion.
2832 @return true, if the string is 1 or "True" in any ASCII case.
2833 false in any other case.
2835 bool toBoolean() const
2837 return rtl_ustr_toBoolean( pData->buffer );
2841 Returns the first character from this string.
2843 @return the first character from this string or 0, if this string
2844 is empty.
2846 sal_Unicode toChar() const
2848 return pData->buffer[0];
2852 Returns the int32 value from this string.
2854 This function can't be used for language specific conversion.
2856 @param radix the radix (between 2 and 36)
2857 @return the int32 represented from this string.
2858 0 if this string represents no number or one of too large
2859 magnitude.
2861 sal_Int32 toInt32( sal_Int16 radix = 10 ) const
2863 return rtl_ustr_toInt32( pData->buffer, radix );
2867 Returns the uint32 value from this string.
2869 This function can't be used for language specific conversion.
2871 @param radix the radix (between 2 and 36)
2872 @return the uint32 represented from this string.
2873 0 if this string represents no number or one of too large
2874 magnitude.
2876 @since LibreOffice 4.2
2878 sal_uInt32 toUInt32( sal_Int16 radix = 10 ) const
2880 return rtl_ustr_toUInt32( pData->buffer, radix );
2884 Returns the int64 value from this string.
2886 This function can't be used for language specific conversion.
2888 @param radix the radix (between 2 and 36)
2889 @return the int64 represented from this string.
2890 0 if this string represents no number or one of too large
2891 magnitude.
2893 sal_Int64 toInt64( sal_Int16 radix = 10 ) const
2895 return rtl_ustr_toInt64( pData->buffer, radix );
2899 Returns the uint64 value from this string.
2901 This function can't be used for language specific conversion.
2903 @param radix the radix (between 2 and 36)
2904 @return the uint64 represented from this string.
2905 0 if this string represents no number or one of too large
2906 magnitude.
2908 @since LibreOffice 4.1
2910 sal_uInt64 toUInt64( sal_Int16 radix = 10 ) const
2912 return rtl_ustr_toUInt64( pData->buffer, radix );
2916 Returns the float value from this string.
2918 This function can't be used for language specific conversion.
2920 @return the float represented from this string.
2921 0.0 if this string represents no number.
2923 float toFloat() const
2925 return rtl_ustr_toFloat( pData->buffer );
2929 Returns the double value from this string.
2931 This function can't be used for language specific conversion.
2933 @return the double represented from this string.
2934 0.0 if this string represents no number.
2936 double toDouble() const
2938 return rtl_ustr_toDouble( pData->buffer );
2943 Return a canonical representation for a string.
2945 A pool of strings, initially empty is maintained privately
2946 by the string class. On invocation, if present in the pool
2947 the original string will be returned. Otherwise this string,
2948 or a copy thereof will be added to the pool and returned.
2950 @return
2951 a version of the string from the pool.
2953 @exception std::bad_alloc is thrown if an out-of-memory condition occurs
2955 @since UDK 3.2.7
2957 OUString intern() const
2959 rtl_uString * pNew = NULL;
2960 rtl_uString_intern( &pNew, pData );
2961 if (pNew == NULL) {
2962 throw std::bad_alloc();
2964 return OUString( pNew, SAL_NO_ACQUIRE );
2968 Return a canonical representation for a converted string.
2970 A pool of strings, initially empty is maintained privately
2971 by the string class. On invocation, if present in the pool
2972 the original string will be returned. Otherwise this string,
2973 or a copy thereof will be added to the pool and returned.
2975 @param value a 8-Bit character array.
2976 @param length the number of character which should be converted.
2977 The 8-Bit character array length must be
2978 greater than or equal to this value.
2979 @param encoding the text encoding from which the 8-Bit character
2980 sequence should be converted.
2981 @param convertFlags flags which controls the conversion.
2982 see RTL_TEXTTOUNICODE_FLAGS_...
2983 @param pInfo pointer to return conversion status or NULL.
2985 @return
2986 a version of the converted string from the pool.
2988 @exception std::bad_alloc is thrown if an out-of-memory condition occurs
2990 @since UDK 3.2.7
2992 static OUString intern( const char * value, sal_Int32 length,
2993 rtl_TextEncoding encoding,
2994 sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS,
2995 sal_uInt32 *pInfo = NULL )
2997 rtl_uString * pNew = NULL;
2998 rtl_uString_internConvert( &pNew, value, length, encoding,
2999 convertFlags, pInfo );
3000 if (pNew == NULL) {
3001 throw std::bad_alloc();
3003 return OUString( pNew, SAL_NO_ACQUIRE );
3007 Converts to an OString, signalling failure.
3009 @param pTarget
3010 An out parameter receiving the converted OString. Must not be null; the
3011 contents are not modified if conversion fails (convertToOString returns
3012 false).
3014 @param nEncoding
3015 The text encoding to convert into. Must be an octet encoding (i.e.,
3016 rtl_isOctetTextEncoding(nEncoding) must return true).
3018 @param nFlags
3019 A combination of RTL_UNICODETOTEXT_FLAGS that detail how to do the
3020 conversion (see rtl_convertUnicodeToText). RTL_UNICODETOTEXT_FLAGS_FLUSH
3021 need not be included, it is implicitly assumed. Typical uses are either
3022 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
3023 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR (fail if a Unicode character cannot
3024 be converted to the target nEncoding) or OUSTRING_TO_OSTRING_CVTFLAGS
3025 (make a best efforts conversion).
3027 @return
3028 True if the conversion succeeded, false otherwise.
3030 bool convertToString(OString * pTarget, rtl_TextEncoding nEncoding,
3031 sal_uInt32 nFlags) const
3033 return rtl_convertUStringToString(&pTarget->pData, pData->buffer,
3034 pData->length, nEncoding, nFlags);
3037 /** Iterate through this string based on code points instead of UTF-16 code
3038 units.
3040 See Chapter 3 of The Unicode Standard 5.0 (Addison--Wesley, 2006) for
3041 definitions of the various terms used in this description.
3043 This string is interpreted as a sequence of zero or more UTF-16 code
3044 units. For each index into this sequence (from zero to one less than
3045 the length of the sequence, inclusive), a code point represented
3046 starting at the given index is computed as follows:
3048 - If the UTF-16 code unit addressed by the index constitutes a
3049 well-formed UTF-16 code unit sequence, the computed code point is the
3050 scalar value encoded by that UTF-16 code unit sequence.
3052 - Otherwise, if the index is at least two UTF-16 code units away from
3053 the end of the sequence, and the sequence of two UTF-16 code units
3054 addressed by the index constitutes a well-formed UTF-16 code unit
3055 sequence, the computed code point is the scalar value encoded by that
3056 UTF-16 code unit sequence.
3058 - Otherwise, the computed code point is the UTF-16 code unit addressed
3059 by the index. (This last case catches unmatched surrogates as well as
3060 indices pointing into the middle of surrogate pairs.)
3062 @param indexUtf16
3063 pointer to a UTF-16 based index into this string; must not be null. On
3064 entry, the index must be in the range from zero to the length of this
3065 string (in UTF-16 code units), inclusive. Upon successful return, the
3066 index will be updated to address the UTF-16 code unit that is the given
3067 incrementCodePoints away from the initial index.
3069 @param incrementCodePoints
3070 the number of code points to move the given *indexUtf16. If
3071 non-negative, moving is done after determining the code point at the
3072 index. If negative, moving is done before determining the code point
3073 at the (then updated) index. The value must be such that the resulting
3074 UTF-16 based index is in the range from zero to the length of this
3075 string (in UTF-16 code units), inclusive.
3077 @return
3078 the code point (an integer in the range from 0 to 0x10FFFF, inclusive)
3079 that is represented within this string starting at the index computed as
3080 follows: If incrementCodePoints is non-negative, the index is the
3081 initial value of *indexUtf16; if incrementCodePoints is negative, the
3082 index is the updated value of *indexUtf16. In either case, the computed
3083 index must be in the range from zero to one less than the length of this
3084 string (in UTF-16 code units), inclusive.
3086 @since UDK 3.2.7
3088 sal_uInt32 iterateCodePoints(
3089 sal_Int32 * indexUtf16, sal_Int32 incrementCodePoints = 1) const
3091 return rtl_uString_iterateCodePoints(
3092 pData, indexUtf16, incrementCodePoints);
3096 * Convert an OString to an OUString, assuming that the OString is
3097 * UTF-8-encoded.
3099 * @param rSource
3100 * an OString to convert
3102 * @since LibreOffice 4.4
3104 static OUString fromUtf8(const OString& rSource)
3106 OUString aTarget;
3107 bool bSuccess = rtl_convertStringToUString(&aTarget.pData,
3108 rSource.getStr(),
3109 rSource.getLength(),
3110 RTL_TEXTENCODING_UTF8,
3111 RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR|RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR|RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR);
3112 (void) bSuccess;
3113 assert(bSuccess);
3114 return aTarget;
3118 * Convert this string to an OString, assuming that the string can be
3119 * UTF-8-encoded successfully.
3121 * In other words, you must not use this method on a random sequence of
3122 * UTF-16 code units, but only at places where it is assumed that the
3123 * content is a proper string.
3125 * @since LibreOffice 4.4
3127 OString toUtf8() const
3129 OString aTarget;
3130 bool bSuccess = rtl_convertUStringToString(&aTarget.pData,
3131 getStr(),
3132 getLength(),
3133 RTL_TEXTENCODING_UTF8,
3134 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR|RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR);
3135 (void) bSuccess;
3136 assert(bSuccess);
3137 return aTarget;
3140 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
3142 static OUStringNumber< int > number( int i, sal_Int16 radix = 10 )
3144 return OUStringNumber< int >( i, radix );
3146 static OUStringNumber< long long > number( long long ll, sal_Int16 radix = 10 )
3148 return OUStringNumber< long long >( ll, radix );
3150 static OUStringNumber< unsigned long long > number( unsigned long long ll, sal_Int16 radix = 10 )
3152 return OUStringNumber< unsigned long long >( ll, radix );
3154 static OUStringNumber< unsigned long long > number( unsigned int i, sal_Int16 radix = 10 )
3156 return number( static_cast< unsigned long long >( i ), radix );
3158 static OUStringNumber< long long > number( long i, sal_Int16 radix = 10)
3160 return number( static_cast< long long >( i ), radix );
3162 static OUStringNumber< unsigned long long > number( unsigned long i, sal_Int16 radix = 10 )
3164 return number( static_cast< unsigned long long >( i ), radix );
3166 static OUStringNumber< float > number( float f )
3168 return OUStringNumber< float >( f );
3170 static OUStringNumber< double > number( double d )
3172 return OUStringNumber< double >( d );
3174 #else
3176 Returns the string representation of the integer argument.
3178 This function can't be used for language specific conversion.
3180 @param i an integer value
3181 @param radix the radix (between 2 and 36)
3182 @return a string with the string representation of the argument.
3183 @since LibreOffice 4.1
3185 static OUString number( int i, sal_Int16 radix = 10 )
3187 sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFINT32];
3188 return OUString(aBuf, rtl_ustr_valueOfInt32(aBuf, i, radix));
3190 /// @overload
3191 /// @since LibreOffice 4.1
3192 static OUString number( unsigned int i, sal_Int16 radix = 10 )
3194 return number( static_cast< unsigned long long >( i ), radix );
3196 /// @overload
3197 /// @since LibreOffice 4.1
3198 static OUString number( long i, sal_Int16 radix = 10)
3200 return number( static_cast< long long >( i ), radix );
3202 /// @overload
3203 /// @since LibreOffice 4.1
3204 static OUString number( unsigned long i, sal_Int16 radix = 10 )
3206 return number( static_cast< unsigned long long >( i ), radix );
3208 /// @overload
3209 /// @since LibreOffice 4.1
3210 static OUString number( long long ll, sal_Int16 radix = 10 )
3212 sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFINT64];
3213 return OUString(aBuf, rtl_ustr_valueOfInt64(aBuf, ll, radix));
3215 /// @overload
3216 /// @since LibreOffice 4.1
3217 static OUString number( unsigned long long ll, sal_Int16 radix = 10 )
3219 sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFUINT64];
3220 return OUString(aBuf, rtl_ustr_valueOfUInt64(aBuf, ll, radix));
3224 Returns the string representation of the float argument.
3226 This function can't be used for language specific conversion.
3228 @param f a float.
3229 @return a string with the decimal representation of the argument.
3230 @since LibreOffice 4.1
3232 static OUString number( float f )
3234 sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFFLOAT];
3235 return OUString(aBuf, rtl_ustr_valueOfFloat(aBuf, f));
3239 Returns the string representation of the double argument.
3241 This function can't be used for language specific conversion.
3243 @param d a double.
3244 @return a string with the decimal representation of the argument.
3245 @since LibreOffice 4.1
3247 static OUString number( double d )
3249 sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFDOUBLE];
3250 return OUString(aBuf, rtl_ustr_valueOfDouble(aBuf, d));
3252 #endif
3255 Returns the string representation of the sal_Bool argument.
3257 If the sal_Bool is true, the string "true" is returned.
3258 If the sal_Bool is false, the string "false" is returned.
3259 This function can't be used for language specific conversion.
3261 @param b a sal_Bool.
3262 @return a string with the string representation of the argument.
3263 @deprecated use boolean()
3265 SAL_DEPRECATED("use boolean()") static OUString valueOf( sal_Bool b )
3267 return boolean(b);
3271 Returns the string representation of the boolean argument.
3273 If the argument is true, the string "true" is returned.
3274 If the argument is false, the string "false" is returned.
3275 This function can't be used for language specific conversion.
3277 @param b a bool.
3278 @return a string with the string representation of the argument.
3279 @since LibreOffice 4.1
3281 static OUString boolean( bool b )
3283 sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFBOOLEAN];
3284 return OUString(aBuf, rtl_ustr_valueOfBoolean(aBuf, b));
3288 Returns the string representation of the char argument.
3290 @param c a character.
3291 @return a string with the string representation of the argument.
3292 @deprecated use operator, function or constructor taking char or sal_Unicode argument
3294 SAL_DEPRECATED("convert to OUString or use directly") static OUString valueOf( sal_Unicode c )
3296 return OUString( &c, 1 );
3300 Returns the string representation of the int argument.
3302 This function can't be used for language specific conversion.
3304 @param i a int32.
3305 @param radix the radix (between 2 and 36)
3306 @return a string with the string representation of the argument.
3307 @deprecated use number()
3309 SAL_DEPRECATED("use number()") static OUString valueOf( sal_Int32 i, sal_Int16 radix = 10 )
3311 return number( i, radix );
3315 Returns the string representation of the long argument.
3317 This function can't be used for language specific conversion.
3319 @param ll a int64.
3320 @param radix the radix (between 2 and 36)
3321 @return a string with the string representation of the argument.
3322 @deprecated use number()
3324 SAL_DEPRECATED("use number()") static OUString valueOf( sal_Int64 ll, sal_Int16 radix = 10 )
3326 return number( ll, radix );
3330 Returns the string representation of the float argument.
3332 This function can't be used for language specific conversion.
3334 @param f a float.
3335 @return a string with the string representation of the argument.
3336 @deprecated use number()
3338 SAL_DEPRECATED("use number()") static OUString valueOf( float f )
3340 return number(f);
3344 Returns the string representation of the double argument.
3346 This function can't be used for language specific conversion.
3348 @param d a double.
3349 @return a string with the string representation of the argument.
3350 @deprecated use number()
3352 SAL_DEPRECATED("use number()") static OUString valueOf( double d )
3354 return number(d);
3358 Returns an OUString copied without conversion from an ASCII
3359 character string.
3361 Since this method is optimized for performance, the ASCII character
3362 values are not converted in any way. The caller has to make sure that
3363 all ASCII characters are in the allowed range between 0 and 127.
3364 The ASCII string must be NULL-terminated.
3366 Note that for string literals it is simpler and more efficient
3367 to directly use the OUString constructor.
3369 @param value the 8-Bit ASCII character string
3370 @return a string with the string representation of the argument.
3372 static OUString createFromAscii( const char * value )
3374 rtl_uString* pNew = NULL;
3375 rtl_uString_newFromAscii( &pNew, value );
3376 return OUString( pNew, SAL_NO_ACQUIRE );
3379 #if defined LIBO_INTERNAL_ONLY
3380 static OUString createFromAscii(std::string_view value) {
3381 rtl_uString * p = nullptr;
3382 rtl_uString_newFromLiteral(&p, value.data(), value.size(), 0); //TODO: check for overflow
3383 return OUString(p, SAL_NO_ACQUIRE);
3385 #endif
3387 #if defined LIBO_INTERNAL_ONLY
3388 operator std::u16string_view() const { return {getStr(), sal_uInt32(getLength())}; }
3389 #endif
3391 #if defined LIBO_INTERNAL_ONLY
3392 // A wrapper for the first expression in an
3394 // OUString::Concat(e1) + e2 + ...
3396 // concatenation chain, when neither of the first two e1, e2 is one of our rtl string-related
3397 // classes (so something like
3399 // OUString s = "a" + (b ? std::u16string_view(u"c") : std::u16string_view(u"dd"));
3401 // would not compile):
3402 template<typename T> [[nodiscard]] static
3403 typename std::enable_if_t<
3404 ToStringHelper<T>::allowOUStringConcat, OUStringConcat<OUStringConcatMarker, T>>
3405 Concat(T const & value) { return OUStringConcat<OUStringConcatMarker, T>({}, value); }
3407 // This overload is needed so that an argument of type 'char const[N]' ends up as
3408 // 'OUStringConcat<rtl::OUStringConcatMarker, char const[N]>' rather than as
3409 // 'OUStringConcat<rtl::OUStringConcatMarker, char[N]>':
3410 template<typename T, std::size_t N> [[nodiscard]] static
3411 typename std::enable_if_t<
3412 ToStringHelper<T[N]>::allowOUStringConcat, OUStringConcat<OUStringConcatMarker, T[N]>>
3413 Concat(T (& value)[N]) { return OUStringConcat<OUStringConcatMarker, T[N]>({}, value); }
3414 #endif
3416 private:
3417 OUString & internalAppend( rtl_uString* pOtherData )
3419 rtl_uString* pNewData = NULL;
3420 rtl_uString_newConcat( &pNewData, pData, pOtherData );
3421 if (pNewData == NULL) {
3422 throw std::bad_alloc();
3424 rtl_uString_assign(&pData, pNewData);
3425 rtl_uString_release(pNewData);
3426 return *this;
3431 #if defined LIBO_INTERNAL_ONLY
3432 // Prevent the operator ==/!= overloads with 'sal_Unicode const *' parameter from
3433 // being selected for nonsensical code like
3435 // if (ouIdAttr == nullptr)
3437 void operator ==(OUString const &, std::nullptr_t) = delete;
3438 void operator ==(std::nullptr_t, OUString const &) = delete;
3439 void operator !=(OUString const &, std::nullptr_t) = delete;
3440 void operator !=(std::nullptr_t, OUString const &) = delete;
3441 #endif
3443 #if defined LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
3444 /// @cond INTERNAL
3447 @internal
3449 template<>
3450 struct ToStringHelper< OUString >
3452 static std::size_t length( const OUString& s ) { return s.getLength(); }
3453 static sal_Unicode* addData( sal_Unicode* buffer, const OUString& s ) { return addDataHelper( buffer, s.getStr(), s.getLength()); }
3454 static const bool allowOStringConcat = false;
3455 static const bool allowOUStringConcat = true;
3459 @internal
3461 template<std::size_t N>
3462 struct ToStringHelper< OUStringLiteral<N> >
3464 static std::size_t length( const OUStringLiteral<N>& str ) { return str.getLength(); }
3465 static sal_Unicode* addData( sal_Unicode* buffer, const OUStringLiteral<N>& str ) { return addDataHelper( buffer, str.getStr(), str.getLength() ); }
3466 static const bool allowOStringConcat = false;
3467 static const bool allowOUStringConcat = true;
3471 @internal
3473 template< typename charT, typename traits, typename T1, typename T2 >
3474 inline std::basic_ostream<charT, traits> & operator <<(
3475 std::basic_ostream<charT, traits> & stream, OUStringConcat< T1, T2 >&& concat)
3477 return stream << OUString( std::move(concat) );
3480 /// @endcond
3481 #endif
3483 /** A helper to use OUStrings with hash maps.
3485 Instances of this class are unary function objects that can be used as
3486 hash function arguments to std::unordered_map and similar constructs.
3488 struct OUStringHash
3490 /** Compute a hash code for a string.
3492 @param rString
3493 a string.
3495 @return
3496 a hash code for the string. This hash code should not be stored
3497 persistently, as its computation may change in later revisions.
3499 size_t operator()(const OUString& rString) const
3500 { return static_cast<size_t>(rString.hashCode()); }
3503 /* ======================================================================= */
3505 /** Convert an OString to an OUString, using a specific text encoding.
3507 The lengths of the two strings may differ (e.g., for double-byte
3508 encodings, UTF-7, UTF-8).
3510 @param rStr
3511 an OString to convert.
3513 @param encoding
3514 the text encoding to use for conversion.
3516 @param convertFlags
3517 flags which control the conversion. Either use
3518 OSTRING_TO_OUSTRING_CVTFLAGS, or see
3519 <http://udk.openoffice.org/cpp/man/spec/textconversion.html> for more
3520 details.
3522 inline OUString OStringToOUString( const OString & rStr,
3523 rtl_TextEncoding encoding,
3524 sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS )
3526 return OUString( rStr.getStr(), rStr.getLength(), encoding, convertFlags );
3529 /** Convert an OUString to an OString, using a specific text encoding.
3531 The lengths of the two strings may differ (e.g., for double-byte
3532 encodings, UTF-7, UTF-8).
3534 @param rUnicode
3535 an OUString to convert.
3537 @param encoding
3538 the text encoding to use for conversion.
3540 @param convertFlags
3541 flags which control the conversion. Either use
3542 OUSTRING_TO_OSTRING_CVTFLAGS, or see
3543 <http://udk.openoffice.org/cpp/man/spec/textconversion.html> for more
3544 details.
3546 inline OString OUStringToOString( const OUString & rUnicode,
3547 rtl_TextEncoding encoding,
3548 sal_uInt32 convertFlags = OUSTRING_TO_OSTRING_CVTFLAGS )
3550 return OString( rUnicode.getStr(), rUnicode.getLength(), encoding, convertFlags );
3553 /* ======================================================================= */
3556 Support for rtl::OUString in std::ostream (and thus in
3557 CPPUNIT_ASSERT or SAL_INFO macros, for example).
3559 The rtl::OUString is converted to UTF-8.
3561 @since LibreOffice 3.5.
3563 template< typename charT, typename traits >
3564 inline std::basic_ostream<charT, traits> & operator <<(
3565 std::basic_ostream<charT, traits> & stream, OUString const & rString)
3567 return stream <<
3568 OUStringToOString(rString, RTL_TEXTENCODING_UTF8);
3569 // best effort; potentially loses data due to conversion failures
3570 // (stray surrogate halves) and embedded null characters
3573 } // namespace
3575 #ifdef RTL_STRING_UNITTEST
3576 namespace rtl
3578 typedef rtlunittest::OUString OUString;
3580 #endif
3582 // In internal code, allow to use classes like OUString without having to
3583 // explicitly refer to the rtl namespace, which is kind of superfluous given
3584 // that OUString itself is namespaced by its OU prefix:
3585 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
3586 using ::rtl::OUString;
3587 using ::rtl::OUStringHash;
3588 using ::rtl::OStringToOUString;
3589 using ::rtl::OUStringToOString;
3590 using ::rtl::OUStringLiteral;
3591 using ::rtl::OUStringChar;
3592 #endif
3594 /// @cond INTERNAL
3596 Make OUString hashable by default for use in STL containers.
3598 @since LibreOffice 6.0
3600 #if defined LIBO_INTERNAL_ONLY
3601 namespace std {
3603 template<>
3604 struct hash<::rtl::OUString>
3606 std::size_t operator()(::rtl::OUString const & s) const
3607 { return std::size_t(s.hashCode()); }
3612 #endif
3613 /// @endcond
3615 #endif /* _RTL_USTRING_HXX */
3617 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */