Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / rtl / ustring.hxx
blobc4869f43a8f0d1a37e293606d0bf1422e7aed673
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 .
21 * This file is part of LibreOffice published API.
24 #ifndef INCLUDED_RTL_USTRING_HXX
25 #define INCLUDED_RTL_USTRING_HXX
27 #include "sal/config.h"
29 #include <cassert>
30 #include <cstddef>
31 #include <cstdlib>
32 #include <limits>
33 #include <new>
34 #include <ostream>
35 #include <utility>
37 #if defined LIBO_INTERNAL_ONLY
38 #include <algorithm>
39 #include <string_view>
40 #include <type_traits>
41 #endif
43 #include "rtl/math.h"
44 #include "rtl/ustring.h"
45 #include "rtl/string.hxx"
46 #include "rtl/stringutils.hxx"
47 #include "rtl/textenc.h"
49 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
50 #include "config_global.h"
51 #include "o3tl/safeint.hxx"
52 #include "rtl/stringconcat.hxx"
53 #endif
55 #ifdef RTL_STRING_UNITTEST
56 extern bool rtl_string_unittest_invalid_conversion;
57 #endif
59 // The unittest uses slightly different code to help check that the proper
60 // calls are made. The class is put into a different namespace to make
61 // sure the compiler generates a different (if generating also non-inline)
62 // copy of the function and does not merge them together. The class
63 // is "brought" into the proper rtl namespace by a typedef below.
64 #ifdef RTL_STRING_UNITTEST
65 #define rtl rtlunittest
66 #endif
68 namespace rtl
71 class OUStringBuffer;
73 #ifdef RTL_STRING_UNITTEST
74 #undef rtl
75 #endif
77 #if defined LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
78 /// @cond INTERNAL
80 /**
81 A wrapper dressing a string literal as a static-refcount rtl_uString.
83 This class is not part of public API and is meant to be used only in LibreOffice code.
84 @since LibreOffice 4.0
86 template<std::size_t N> class SAL_WARN_UNUSED OUStringLiteral {
87 static_assert(N != 0);
88 static_assert(N - 1 <= std::numeric_limits<sal_Int32>::max(), "literal too long");
89 friend class OUString;
90 friend class OUStringConstExpr;
92 public:
93 #if HAVE_CPP_CONSTEVAL
94 consteval
95 #else
96 constexpr
97 #endif
98 OUStringLiteral(char16_t const (&literal)[N]) {
99 assertLayout();
100 assert(literal[N - 1] == '\0');
101 //TODO: Use C++20 constexpr std::copy_n (P0202R3):
102 for (std::size_t i = 0; i != N; ++i) {
103 more.buffer[i] = literal[i];
107 constexpr sal_Int32 getLength() const { return more.length; }
109 constexpr sal_Unicode const * getStr() const SAL_RETURNS_NONNULL { return more.buffer; }
111 constexpr operator std::u16string_view() const { return {more.buffer, sal_uInt32(more.length)}; }
113 private:
114 static constexpr void assertLayout() {
115 // These static_asserts verifying the layout compatibility with rtl_uString cannot be class
116 // member declarations, as offsetof requires a complete type, so defer them to here:
117 static_assert(std::is_standard_layout_v<OUStringLiteral>);
118 static_assert(offsetof(OUStringLiteral, str.refCount) == offsetof(OUStringLiteral, more.refCount));
119 static_assert(offsetof(OUStringLiteral, str.length) == offsetof(OUStringLiteral, more.length));
120 static_assert(offsetof(OUStringLiteral, str.buffer) == offsetof(OUStringLiteral, more.buffer));
123 struct Data {
124 Data() = default;
126 oslInterlockedCount refCount = 0x40000000; // SAL_STRING_STATIC_FLAG (sal/rtl/strimp.hxx)
127 sal_Int32 length = N - 1;
128 sal_Unicode buffer[N] = {}; //TODO: drop initialization for C++20 (P1331R2)
131 union {
132 rtl_uString str;
133 Data more = {};
137 #if defined RTL_STRING_UNITTEST
138 namespace libreoffice_internal {
139 template<std::size_t N> struct ExceptConstCharArrayDetector<OUStringLiteral<N>> {};
140 template<std::size_t N> struct ExceptCharArrayDetector<OUStringLiteral<N>> {};
142 #endif
145 This is intended to be used when declaring compile-time-constant structs or arrays
146 that can be initialised from named OUStringLiteral e.g.
148 constexpr OUStringLiteral AAA = u"aaa";
149 constexpr OUStringLiteral BBB = u"bbb";
150 constexpr OUStringConstExpr FOO[] { AAA, BBB };
152 class OUString;
153 class OUStringConstExpr
155 public:
156 template<std::size_t N> constexpr OUStringConstExpr(OUStringLiteral<N> const & literal):
157 pData(const_cast<rtl_uString *>(&literal.str)) {}
159 // prevent mis-use
160 template<std::size_t N> constexpr OUStringConstExpr(OUStringLiteral<N> && literal)
161 = delete;
163 // no destructor necessary because we know we are pointing at a compile-time
164 // constant OUStringLiteral, which bypasses ref-counting.
167 make it easier to pass to OUStringBuffer and similar without casting/converting
169 constexpr std::u16string_view asView() const { return std::u16string_view(pData->buffer, pData->length); }
171 inline operator const OUString&() const;
173 private:
174 rtl_uString* pData;
177 /// @endcond
178 #endif
180 /* ======================================================================= */
183 This String class provides base functionality for C++ like Unicode
184 character array handling. The advantage of this class is that it
185 handles all the memory management for you - and it does it
186 more efficiently. If you assign a string to another string, the
187 data of both strings are shared (without any copy operation or
188 memory allocation) as long as you do not change the string. This class
189 also stores the length of the string, so that many operations are
190 faster than the C-str-functions.
192 This class provides only readonly string handling. So you could create
193 a string and you could only query the content from this string.
194 It provides also functionality to change the string, but this results
195 in every case in a new string instance (in the most cases with a
196 memory allocation). You don't have functionality to change the
197 content of the string. If you want to change the string content, then
198 you should use the OStringBuffer class, which provides these
199 functionalities and avoids too much memory allocation.
201 The design of this class is similar to the string classes in Java so
202 less people should have understanding problems when they use this class.
205 class SAL_WARN_UNUSED SAL_DLLPUBLIC_RTTI OUString
207 public:
208 /// @cond INTERNAL
209 rtl_uString * pData;
210 /// @endcond
213 New string containing no characters.
215 OUString()
217 pData = NULL;
218 rtl_uString_new( &pData );
222 New string from OUString.
224 @param str an OUString.
226 OUString( const OUString & str )
228 pData = str.pData;
229 rtl_uString_acquire( pData );
232 #if defined LIBO_INTERNAL_ONLY
234 Move constructor.
236 @param str an OUString.
237 @since LibreOffice 5.2
239 OUString( OUString && str ) noexcept
241 pData = str.pData;
242 str.pData = nullptr;
243 rtl_uString_new( &str.pData );
245 #endif
248 New string from OUString data.
250 @param str an OUString data.
252 OUString( rtl_uString * str )
254 pData = str;
255 rtl_uString_acquire( pData );
258 #if defined LIBO_INTERNAL_ONLY
259 /// @cond INTERNAL
260 // Catch inadvertent conversions to the above ctor:
261 OUString(std::nullptr_t) = delete;
262 /// @endcond
263 #endif
265 /** New OUString from OUString data without acquiring it. Takeover of ownership.
267 The SAL_NO_ACQUIRE dummy parameter is only there to distinguish this
268 from other constructors.
270 @param str
271 OUString data
273 OUString( rtl_uString * str, __sal_NoAcquire )
274 { pData = str; }
277 New string from a single Unicode character.
279 @param value a Unicode character.
281 explicit OUString( sal_Unicode value )
282 : pData (NULL)
284 rtl_uString_newFromStr_WithLength( &pData, &value, 1 );
287 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST_CONCAT
288 /// @cond INTERNAL
289 // Catch inadvertent conversions to the above ctor (but still allow
290 // construction from char literals):
291 OUString(int) = delete;
292 explicit OUString(char c):
293 OUString(sal_Unicode(static_cast<unsigned char>(c)))
295 /// @endcond
296 #endif
298 #if defined LIBO_INTERNAL_ONLY
300 template<typename T> explicit OUString(
301 T const & value,
302 typename libreoffice_internal::CharPtrDetector<T, libreoffice_internal::Dummy>::TypeUtf16
303 = libreoffice_internal::Dummy()):
304 pData(nullptr)
305 { rtl_uString_newFromStr(&pData, value); }
307 template<typename T> explicit OUString(
308 T & value,
309 typename
310 libreoffice_internal::NonConstCharArrayDetector<T, libreoffice_internal::Dummy>::TypeUtf16
311 = libreoffice_internal::Dummy()):
312 pData(nullptr)
313 { rtl_uString_newFromStr(&pData, value); }
315 #else
318 New string from a Unicode character buffer array.
320 @param value a NULL-terminated Unicode character array.
322 OUString( const sal_Unicode * value )
324 pData = NULL;
325 rtl_uString_newFromStr( &pData, value );
328 #endif
331 New string from a Unicode character buffer array.
333 @param value a Unicode character array.
334 @param length the number of character which should be copied.
335 The character array length must be greater than
336 or equal to this value.
338 OUString( const sal_Unicode * value, sal_Int32 length )
340 pData = NULL;
341 rtl_uString_newFromStr_WithLength( &pData, value, length );
345 New string from an 8-Bit string literal that is expected to contain only
346 characters in the ASCII set (i.e. first 128 characters). This constructor
347 allows an efficient and convenient way to create OUString
348 instances from ASCII literals. When creating strings from data that
349 is not pure ASCII, it needs to be converted to OUString by explicitly
350 providing the encoding to use for the conversion.
352 If there are any embedded \0's in the string literal, the result is undefined.
353 Use the overload that explicitly accepts length.
355 @param literal the 8-bit ASCII string literal
357 @since LibreOffice 3.6
359 template< typename T >
360 OUString( T& literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
362 assert(
363 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
364 pData = NULL;
365 if (libreoffice_internal::ConstCharArrayDetector<T>::length == 0) {
366 rtl_uString_new(&pData);
367 } else {
368 rtl_uString_newFromLiteral(
369 &pData,
370 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
371 literal),
372 libreoffice_internal::ConstCharArrayDetector<T>::length, 0);
374 #ifdef RTL_STRING_UNITTEST
375 rtl_string_unittest_const_literal = true;
376 #endif
379 #if defined LIBO_INTERNAL_ONLY
380 /** @overload @since LibreOffice 5.3 */
381 template<typename T> OUString(
382 T & literal,
383 typename libreoffice_internal::ConstCharArrayDetector<
384 T, libreoffice_internal::Dummy>::TypeUtf16
385 = libreoffice_internal::Dummy()):
386 pData(nullptr)
388 assert(
389 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
390 if (libreoffice_internal::ConstCharArrayDetector<T>::length == 0) {
391 rtl_uString_new(&pData);
392 } else {
393 rtl_uString_newFromStr_WithLength(
394 &pData,
395 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
396 literal),
397 libreoffice_internal::ConstCharArrayDetector<T>::length);
400 #endif
402 #if defined LIBO_INTERNAL_ONLY && defined RTL_STRING_UNITTEST
403 /// @cond INTERNAL
405 * Only used by unittests to detect incorrect conversions.
406 * @internal
408 template< typename T >
409 OUString( T&, typename libreoffice_internal::ExceptConstCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
411 pData = NULL;
412 rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
413 rtl_string_unittest_invalid_conversion = true;
416 * Only used by unittests to detect incorrect conversions.
417 * @internal
419 template< typename T >
420 OUString( const T&, typename libreoffice_internal::ExceptCharArrayDetector< T >::Type = libreoffice_internal::Dummy() )
422 pData = NULL;
423 rtl_uString_newFromLiteral( &pData, "!!br0ken!!", 10, 0 ); // set to garbage
424 rtl_string_unittest_invalid_conversion = true;
426 /// @endcond
427 #endif
429 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
430 /// @cond INTERNAL
432 New string from a string literal.
434 @since LibreOffice 5.0
436 template<std::size_t N> constexpr OUString(OUStringLiteral<N> const & literal):
437 pData(const_cast<rtl_uString *>(&literal.str)) {}
438 template<std::size_t N> OUString(OUStringLiteral<N> &&) = delete;
439 /// @endcond
440 #endif
443 New string from an 8-Bit character buffer array.
445 @param value An 8-Bit character array.
446 @param length The number of character which should be converted.
447 The 8-Bit character array length must be
448 greater than or equal to this value.
449 @param encoding The text encoding from which the 8-Bit character
450 sequence should be converted.
451 @param convertFlags Flags which control the conversion.
452 see RTL_TEXTTOUNICODE_FLAGS_...
454 @exception std::bad_alloc is thrown if an out-of-memory condition occurs
456 OUString( const char * value, sal_Int32 length,
457 rtl_TextEncoding encoding,
458 sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS )
460 pData = NULL;
461 rtl_string2UString( &pData, value, length, encoding, convertFlags );
462 if (pData == NULL) {
463 throw std::bad_alloc();
467 /** Create a new string from an array of Unicode code points.
469 @param codePoints
470 an array of at least codePointCount code points, which each must be in
471 the range from 0 to 0x10FFFF, inclusive. May be null if codePointCount
472 is zero.
474 @param codePointCount
475 the non-negative number of code points.
477 @exception std::bad_alloc
478 is thrown if either an out-of-memory condition occurs or the resulting
479 number of UTF-16 code units would have been larger than SAL_MAX_INT32.
481 @since UDK 3.2.7
483 explicit OUString(
484 sal_uInt32 const * codePoints, sal_Int32 codePointCount):
485 pData(NULL)
487 rtl_uString_newFromCodePoints(&pData, codePoints, codePointCount);
488 if (pData == NULL) {
489 throw std::bad_alloc();
493 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
495 @overload
496 @internal
498 template< typename T1, typename T2 >
499 OUString( OUStringConcat< T1, T2 >&& c )
501 const sal_Int32 l = c.length();
502 pData = rtl_uString_alloc( l );
503 if (l != 0)
505 sal_Unicode* end = c.addData( pData->buffer );
506 pData->length = l;
507 *end = '\0';
512 @overload
513 @internal
515 template< std::size_t N >
516 OUString( OUStringNumber< N >&& n )
517 : OUString( n.buf, n.length )
519 #endif
521 #if defined LIBO_INTERNAL_ONLY
522 explicit OUString(std::u16string_view sv) {
523 if (sv.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max())) {
524 throw std::bad_alloc();
526 pData = nullptr;
527 rtl_uString_newFromStr_WithLength(&pData, sv.data(), sv.size());
529 #endif
532 Release the string data.
534 #if defined LIBO_INTERNAL_ONLY && __cplusplus >= 202002L
535 constexpr
536 #endif
537 ~OUString()
539 #if defined LIBO_INTERNAL_ONLY && __cplusplus >= 202002L
540 if (std::is_constant_evaluated()) {
541 //TODO: We would want to
543 // assert(SAL_STRING_IS_STATIC(pData));
545 // here, but that wouldn't work because read of member `str` of OUStringLiteral's
546 // anonymous union with active member `more` is not allowed in a constant expression.
547 } else
548 #endif
549 rtl_uString_release( pData );
552 /** Provides an OUString const & passing a storage pointer of an
553 rtl_uString * handle.
554 It is more convenient to use C++ OUString member functions when dealing
555 with rtl_uString * handles. Using this function avoids unnecessary
556 acquire()/release() calls for a temporary OUString object.
558 @param ppHandle
559 pointer to storage
560 @return
561 OUString const & based on given storage
563 static OUString const & unacquired( rtl_uString * const * ppHandle )
564 { return * reinterpret_cast< OUString const * >( ppHandle ); }
566 #if defined LIBO_INTERNAL_ONLY
567 /** Provides an OUString const & passing an OUStringBuffer const reference.
568 It is more convenient to use C++ OUString member functions when checking
569 current buffer content. Use this function instead of toString (that
570 allocates a new OUString from buffer data) when the result is used in
571 comparisons.
573 @param str
574 an OUStringBuffer
575 @return
576 OUString const & based on given storage
577 @since LibreOffice 7.4
579 static OUString const& unacquired(const OUStringBuffer& str);
580 #endif
583 Assign a new string.
585 @param str an OUString.
587 OUString & operator=( const OUString & str )
589 rtl_uString_assign( &pData, str.pData );
590 return *this;
593 #if defined LIBO_INTERNAL_ONLY
595 Move assign a new string.
597 @param str an OUString.
598 @since LibreOffice 5.2
600 OUString & operator=( OUString && str ) noexcept
602 std::swap(pData, str.pData);
603 return *this;
605 #endif
608 Assign a new string from an 8-Bit string literal that is expected to contain only
609 characters in the ASCII set (i.e. first 128 characters). This operator
610 allows an efficient and convenient way to assign OUString
611 instances from ASCII literals. When assigning strings from data that
612 is not pure ASCII, it needs to be converted to OUString by explicitly
613 providing the encoding to use for the conversion.
615 @param literal the 8-bit ASCII string literal
617 @since LibreOffice 3.6
619 template< typename T >
620 typename libreoffice_internal::ConstCharArrayDetector< T, OUString& >::Type operator=( T& literal )
622 assert(
623 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
624 if (libreoffice_internal::ConstCharArrayDetector<T>::length == 0) {
625 rtl_uString_new(&pData);
626 } else {
627 rtl_uString_newFromLiteral(
628 &pData,
629 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
630 literal),
631 libreoffice_internal::ConstCharArrayDetector<T>::length, 0);
633 return *this;
636 #if defined LIBO_INTERNAL_ONLY
637 /** @overload @since LibreOffice 5.3 */
638 template<typename T>
639 typename
640 libreoffice_internal::ConstCharArrayDetector<T, OUString &>::TypeUtf16
641 operator =(T & literal) {
642 if (libreoffice_internal::ConstCharArrayDetector<T>::length == 0) {
643 rtl_uString_new(&pData);
644 } else {
645 rtl_uString_newFromStr_WithLength(
646 &pData,
647 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
648 literal),
649 libreoffice_internal::ConstCharArrayDetector<T>::length);
651 return *this;
654 /** @overload @since LibreOffice 5.4 */
655 template<std::size_t N> OUString & operator =(OUStringLiteral<N> const & literal) {
656 rtl_uString_release(pData);
657 pData = const_cast<rtl_uString *>(&literal.str);
658 return *this;
660 template<std::size_t N> OUString & operator =(OUStringLiteral<N> &&) = delete;
662 template <std::size_t N>
663 OUString & operator =(OUStringNumber<N> && n) {
664 // n.length should never be zero, so no need to add an optimization for that case
665 rtl_uString_newFromStr_WithLength(&pData, n.buf, n.length);
666 return *this;
669 OUString & operator =(std::u16string_view sv) {
670 if (sv.empty()) {
671 rtl_uString_new(&pData);
672 } else {
673 rtl_uString_newFromStr_WithLength(&pData, sv.data(), sv.size());
675 return *this;
677 #endif
679 #if defined LIBO_INTERNAL_ONLY
681 Append the contents of an OUStringBuffer to this string.
683 @param str an OUStringBuffer.
685 @exception std::bad_alloc is thrown if an out-of-memory condition occurs
686 @since LibreOffice 6.2
688 inline OUString & operator+=( const OUStringBuffer & str ) &;
689 #endif
692 Append a string to this string.
694 @param str an OUString.
696 @exception std::bad_alloc is thrown if an out-of-memory condition occurs
698 OUString & operator+=( const OUString & str )
699 #if defined LIBO_INTERNAL_ONLY
701 #endif
703 return internalAppend(str.pData);
705 #if defined LIBO_INTERNAL_ONLY
706 void operator+=(OUString const &) && = delete;
707 #endif
709 /** Append an ASCII string literal to this string.
711 @param literal an 8-bit ASCII-only string literal
713 @since LibreOffice 5.1
715 template<typename T>
716 typename libreoffice_internal::ConstCharArrayDetector<T, OUString &>::Type
717 operator +=(T & literal)
718 #if defined LIBO_INTERNAL_ONLY
720 #endif
722 assert(
723 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
724 rtl_uString_newConcatAsciiL(
725 &pData, pData,
726 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
727 libreoffice_internal::ConstCharArrayDetector<T>::length);
728 return *this;
730 #if defined LIBO_INTERNAL_ONLY
731 template<typename T>
732 typename libreoffice_internal::ConstCharArrayDetector<T, OUString &>::Type
733 operator +=(T &) && = delete;
734 #endif
736 #if defined LIBO_INTERNAL_ONLY
737 /** @overload @since LibreOffice 5.3 */
738 template<typename T>
739 typename
740 libreoffice_internal::ConstCharArrayDetector<T, OUString &>::TypeUtf16
741 operator +=(T & literal) & {
742 rtl_uString_newConcatUtf16L(
743 &pData, pData,
744 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
745 libreoffice_internal::ConstCharArrayDetector<T>::length);
746 return *this;
748 template<typename T>
749 typename
750 libreoffice_internal::ConstCharArrayDetector<T, OUString &>::TypeUtf16
751 operator +=(T &) && = delete;
753 /** @overload @since LibreOffice 5.4 */
754 template<std::size_t N> OUString & operator +=(OUStringLiteral<N> const & literal) & {
755 rtl_uString_newConcatUtf16L(&pData, pData, literal.getStr(), literal.getLength());
756 return *this;
758 template<std::size_t N> void operator +=(OUStringLiteral<N> const &) && = delete;
760 OUString & operator +=(std::u16string_view sv) & {
761 if (sv.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max())) {
762 throw std::bad_alloc();
764 rtl_uString_newConcatUtf16L(&pData, pData, sv.data(), sv.size());
765 return *this;
767 void operator +=(std::u16string_view) && = delete;
768 #endif
770 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
772 @overload
773 @internal
775 template< typename T1, typename T2 >
776 OUString& operator+=( OUStringConcat< T1, T2 >&& c ) & {
777 sal_Int32 l = c.length();
778 if( l == 0 )
779 return *this;
780 l += pData->length;
781 rtl_uString_ensureCapacity( &pData, l );
782 sal_Unicode* end = c.addData( pData->buffer + pData->length );
783 *end = '\0';
784 pData->length = l;
785 return *this;
787 template<typename T1, typename T2> void operator +=(
788 OUStringConcat<T1, T2> &&) && = delete;
791 @overload
792 @internal
794 template< std::size_t N >
795 OUString& operator+=( OUStringNumber< N >&& n ) & {
796 sal_Int32 l = n.length;
797 if( l == 0 )
798 return *this;
799 l += pData->length;
800 rtl_uString_ensureCapacity( &pData, l );
801 sal_Unicode* end = addDataHelper( pData->buffer + pData->length, n.buf, n.length );
802 *end = '\0';
803 pData->length = l;
804 return *this;
806 template<std::size_t N> void operator +=(
807 OUStringNumber<N> &&) && = delete;
808 #endif
811 Clears the string, i.e, makes a zero-character string
812 @since LibreOffice 4.4
814 void clear()
816 rtl_uString_new( &pData );
820 Returns the length of this string.
822 The length is equal to the number of Unicode characters in this string.
824 @return the length of the sequence of characters represented by this
825 object.
827 sal_Int32 getLength() const { return pData->length; }
830 Checks if a string is empty.
832 @return true if the string is empty;
833 false, otherwise.
835 @since LibreOffice 3.4
837 bool isEmpty() const
839 return pData->length == 0;
843 Returns a pointer to the Unicode character buffer for this string.
845 It isn't necessarily NULL terminated.
847 @return a pointer to the Unicode characters buffer for this object.
849 const sal_Unicode * getStr() const SAL_RETURNS_NONNULL { return pData->buffer; }
852 Access to individual characters.
854 @param index must be non-negative and less than length.
856 @return the character at the given index.
858 @since LibreOffice 3.5
860 sal_Unicode operator [](sal_Int32 index) const {
861 // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
862 assert(index >= 0 && static_cast<sal_uInt32>(index) < static_cast<sal_uInt32>(getLength()));
863 return getStr()[index];
867 Compares two strings.
869 The comparison is based on the numeric value of each character in
870 the strings and return a value indicating their relationship.
871 This function can't be used for language specific sorting.
873 @param str the object to be compared.
874 @return 0 - if both strings are equal
875 < 0 - if this string is less than the string argument
876 > 0 - if this string is greater than the string argument
878 #if defined LIBO_INTERNAL_ONLY
879 sal_Int32 compareTo( std::u16string_view str ) const
881 return rtl_ustr_compare_WithLength( pData->buffer, pData->length,
882 str.data(), str.length() );
884 #else
885 sal_Int32 compareTo( const OUString & str ) const
887 return rtl_ustr_compare_WithLength( pData->buffer, pData->length,
888 str.pData->buffer, str.pData->length );
890 #endif
893 Compares two strings with a maximum count of characters.
895 The comparison is based on the numeric value of each character in
896 the strings and return a value indicating their relationship.
897 This function can't be used for language specific sorting.
899 @param str the object to be compared.
900 @param maxLength the maximum count of characters to be compared.
901 @return 0 - if both strings are equal
902 < 0 - if this string is less than the string argument
903 > 0 - if this string is greater than the string argument
905 @since UDK 3.2.7
907 #if defined LIBO_INTERNAL_ONLY
908 sal_Int32 compareTo( std::u16string_view str, sal_Int32 maxLength ) const
910 return rtl_ustr_shortenedCompare_WithLength( pData->buffer, pData->length,
911 str.data(), str.length(), maxLength );
913 #else
914 sal_Int32 compareTo( const OUString & str, sal_Int32 maxLength ) const
916 return rtl_ustr_shortenedCompare_WithLength( pData->buffer, pData->length,
917 str.pData->buffer, str.pData->length, maxLength );
919 #endif
922 Compares two strings in reverse order.
924 The comparison is based on the numeric value of each character in
925 the strings and return a value indicating their relationship.
926 This function can't be used for language specific sorting.
928 @param str the object to be compared.
929 @return 0 - if both strings are equal
930 < 0 - if this string is less than the string argument
931 > 0 - if this string is greater than the string argument
933 #if defined LIBO_INTERNAL_ONLY
934 sal_Int32 reverseCompareTo(std::u16string_view sv) const {
935 return rtl_ustr_reverseCompare_WithLength(
936 pData->buffer, pData->length, sv.data(), sv.size());
938 #else
939 sal_Int32 reverseCompareTo( const OUString & str ) const
941 return rtl_ustr_reverseCompare_WithLength( pData->buffer, pData->length,
942 str.pData->buffer, str.pData->length );
944 #endif
947 @overload
948 This function accepts an ASCII string literal as its argument.
949 @since LibreOffice 4.1
951 template< typename T >
952 typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type reverseCompareTo( T& literal ) const
954 assert(
955 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
956 return rtl_ustr_asciil_reverseCompare_WithLength(
957 pData->buffer, pData->length,
958 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
959 libreoffice_internal::ConstCharArrayDetector<T>::length);
963 Perform a comparison of two strings.
965 The result is true if and only if second string
966 represents the same sequence of characters as the first string.
967 This function can't be used for language specific comparison.
969 @param str the object to be compared.
970 @return true if the strings are equal;
971 false, otherwise.
973 bool equals( const OUString & str ) const
975 if ( pData->length != str.pData->length )
976 return false;
977 if ( pData == str.pData )
978 return true;
979 return rtl_ustr_reverseCompare_WithLength( pData->buffer, pData->length,
980 str.pData->buffer, str.pData->length ) == 0;
984 Perform an ASCII lowercase comparison of two strings.
986 The result is true if and only if second string
987 represents the same sequence of characters as the first string,
988 ignoring the case.
989 Character values between 65 and 90 (ASCII A-Z) are interpreted as
990 values between 97 and 122 (ASCII a-z).
991 This function can't be used for language specific comparison.
993 @param str the object to be compared.
994 @return true if the strings are equal;
995 false, otherwise.
997 #if defined LIBO_INTERNAL_ONLY
998 bool equalsIgnoreAsciiCase(std::u16string_view sv) const {
999 if ( sal_uInt32(pData->length) != sv.size() )
1000 return false;
1001 if ( pData->buffer == sv.data() )
1002 return true;
1003 return
1004 rtl_ustr_compareIgnoreAsciiCase_WithLength(
1005 pData->buffer, pData->length, sv.data(), sv.size())
1006 == 0;
1008 #else
1009 bool equalsIgnoreAsciiCase( const OUString & str ) const
1011 if ( pData->length != str.pData->length )
1012 return false;
1013 if ( pData == str.pData )
1014 return true;
1015 return rtl_ustr_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
1016 str.pData->buffer, str.pData->length ) == 0;
1018 #endif
1021 Perform an ASCII lowercase comparison of two strings.
1023 Compare the two strings with uppercase ASCII
1024 character values between 65 and 90 (ASCII A-Z) interpreted as
1025 values between 97 and 122 (ASCII a-z).
1026 This function can't be used for language specific comparison.
1028 @param str the object to be compared.
1029 @return 0 - if both strings are equal
1030 < 0 - if this string is less than the string argument
1031 > 0 - if this string is greater than the string argument
1033 @since LibreOffice 4.0
1035 #if defined LIBO_INTERNAL_ONLY
1036 sal_Int32 compareToIgnoreAsciiCase(std::u16string_view sv) const {
1037 return rtl_ustr_compareIgnoreAsciiCase_WithLength(
1038 pData->buffer, pData->length, sv.data(), sv.size());
1040 #else
1041 sal_Int32 compareToIgnoreAsciiCase( const OUString & str ) const
1043 return rtl_ustr_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
1044 str.pData->buffer, str.pData->length );
1046 #endif
1049 @overload
1050 This function accepts an ASCII string literal as its argument.
1051 @since LibreOffice 3.6
1053 template< typename T >
1054 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type equalsIgnoreAsciiCase( T& literal ) const
1056 assert(
1057 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1058 return
1059 (pData->length
1060 == libreoffice_internal::ConstCharArrayDetector<T>::length)
1061 && (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength(
1062 pData->buffer, pData->length,
1063 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1064 literal))
1065 == 0);
1069 Match against a substring appearing in this string.
1071 The result is true if and only if the second string appears as a substring
1072 of this string, at the given position.
1073 This function can't be used for language specific comparison.
1075 @param str the object (substring) to be compared.
1076 @param fromIndex the index to start the comparison from.
1077 The index must be greater than or equal to 0
1078 and less or equal as the string length.
1079 @return true if str match with the characters in the string
1080 at the given position;
1081 false, otherwise.
1083 #if defined LIBO_INTERNAL_ONLY
1084 bool match(std::u16string_view sv, sal_Int32 fromIndex = 0) const {
1085 return
1086 rtl_ustr_shortenedCompare_WithLength(
1087 pData->buffer + fromIndex, pData->length - fromIndex, sv.data(), sv.size(),
1088 sv.size())
1089 == 0;
1091 #else
1092 bool match( const OUString & str, sal_Int32 fromIndex = 0 ) const
1094 return rtl_ustr_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1095 str.pData->buffer, str.pData->length, str.pData->length ) == 0;
1097 #endif
1100 @overload
1101 This function accepts an ASCII string literal as its argument.
1102 @since LibreOffice 3.6
1104 template< typename T >
1105 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type match( T& literal, sal_Int32 fromIndex = 0 ) const
1107 assert(
1108 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1109 return
1110 rtl_ustr_ascii_shortenedCompare_WithLength(
1111 pData->buffer+fromIndex, pData->length-fromIndex,
1112 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1113 literal),
1114 libreoffice_internal::ConstCharArrayDetector<T>::length)
1115 == 0;
1119 Match against a substring appearing in this string, ignoring the case of
1120 ASCII letters.
1122 The result is true if and only if the second string appears as a substring
1123 of this string, at the given position.
1124 Character values between 65 and 90 (ASCII A-Z) are interpreted as
1125 values between 97 and 122 (ASCII a-z).
1126 This function can't be used for language specific comparison.
1128 @param str the object (substring) to be compared.
1129 @param fromIndex the index to start the comparison from.
1130 The index must be greater than or equal to 0
1131 and less than or equal to the string length.
1132 @return true if str match with the characters in the string
1133 at the given position;
1134 false, otherwise.
1136 #if defined LIBO_INTERNAL_ONLY
1137 bool matchIgnoreAsciiCase(std::u16string_view sv, sal_Int32 fromIndex = 0) const {
1138 return
1139 rtl_ustr_shortenedCompareIgnoreAsciiCase_WithLength(
1140 pData->buffer + fromIndex, pData->length - fromIndex, sv.data(), sv.size(),
1141 sv.size())
1142 == 0;
1144 #else
1145 bool matchIgnoreAsciiCase( const OUString & str, sal_Int32 fromIndex = 0 ) const
1147 return rtl_ustr_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1148 str.pData->buffer, str.pData->length,
1149 str.pData->length ) == 0;
1151 #endif
1154 @overload
1155 This function accepts an ASCII string literal as its argument.
1156 @since LibreOffice 3.6
1158 template< typename T >
1159 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type matchIgnoreAsciiCase( T& literal, sal_Int32 fromIndex = 0 ) const
1161 assert(
1162 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1163 return matchIgnoreAsciiCaseAsciiL(
1164 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1165 libreoffice_internal::ConstCharArrayDetector<T>::length, fromIndex);
1169 Compares two strings.
1171 The comparison is based on the numeric value of each character in
1172 the strings and return a value indicating their relationship.
1173 Since this method is optimized for performance, the ASCII character
1174 values are not converted in any way. The caller has to make sure that
1175 all ASCII characters are in the allowed range between 0 and 127.
1176 The ASCII string must be NULL-terminated.
1177 This function can't be used for language specific sorting.
1179 @param asciiStr the 8-Bit ASCII character string to be compared.
1180 @return 0 - if both strings are equal
1181 < 0 - if this string is less than the string argument
1182 > 0 - if this string is greater than the string argument
1184 sal_Int32 compareToAscii( const char* asciiStr ) const
1186 return rtl_ustr_ascii_compare_WithLength( pData->buffer, pData->length, asciiStr );
1190 Compares two strings with a maximum count of characters.
1192 The comparison is based on the numeric value of each character in
1193 the strings and return a value indicating their relationship.
1194 Since this method is optimized for performance, the ASCII character
1195 values are not converted in any way. The caller has to make sure that
1196 all ASCII characters are in the allowed range between 0 and 127.
1197 The ASCII string must be NULL-terminated.
1198 This function can't be used for language specific sorting.
1200 @deprecated This is a confusing overload with unexpectedly different
1201 semantics from the one-parameter form, so it is marked as deprecated.
1202 Practically all uses compare the return value against zero and can thus
1203 be replaced with uses of startsWith.
1205 @param asciiStr the 8-Bit ASCII character string to be compared.
1206 @param maxLength the maximum count of characters to be compared.
1207 @return 0 - if both strings are equal
1208 < 0 - if this string is less than the string argument
1209 > 0 - if this string is greater than the string argument
1211 SAL_DEPRECATED(
1212 "replace s1.compareToAscii(s2, strlen(s2)) == 0 with s1.startsWith(s2)")
1213 sal_Int32 compareToAscii( const char * asciiStr, sal_Int32 maxLength ) const
1215 return rtl_ustr_ascii_shortenedCompare_WithLength( pData->buffer, pData->length,
1216 asciiStr, maxLength );
1220 Compares two strings in reverse order.
1222 This could be useful, if normally both strings start with the same
1223 content. The comparison is based on the numeric value of each character
1224 in the strings and return a value indicating their relationship.
1225 Since this method is optimized for performance, the ASCII character
1226 values are not converted in any way. The caller has to make sure that
1227 all ASCII characters are in the allowed range between 0 and 127.
1228 The ASCII string must be greater than or equal to asciiStrLength.
1229 This function can't be used for language specific sorting.
1231 @param asciiStr the 8-Bit ASCII character string to be compared.
1232 @param asciiStrLength the length of the ascii string
1233 @return 0 - if both strings are equal
1234 < 0 - if this string is less than the string argument
1235 > 0 - if this string is greater than the string argument
1237 sal_Int32 reverseCompareToAsciiL( const char * asciiStr, sal_Int32 asciiStrLength ) const
1239 return rtl_ustr_asciil_reverseCompare_WithLength( pData->buffer, pData->length,
1240 asciiStr, asciiStrLength );
1244 Perform a comparison of two strings.
1246 The result is true if and only if second string
1247 represents the same sequence of characters as the first string.
1248 Since this method is optimized for performance, the ASCII character
1249 values are not converted in any way. The caller has to make sure that
1250 all ASCII characters are in the allowed range between 0 and 127.
1251 The ASCII string must be NULL-terminated.
1252 This function can't be used for language specific comparison.
1254 @param asciiStr the 8-Bit ASCII character string to be compared.
1255 @return true if the strings are equal;
1256 false, otherwise.
1258 bool equalsAscii( const char* asciiStr ) const
1260 return rtl_ustr_ascii_compare_WithLength( pData->buffer, pData->length,
1261 asciiStr ) == 0;
1265 Perform a comparison of two strings.
1267 The result is true if and only if second string
1268 represents the same sequence of characters as the first string.
1269 Since this method is optimized for performance, the ASCII character
1270 values are not converted in any way. The caller has to make sure that
1271 all ASCII characters are in the allowed range between 0 and 127.
1272 The ASCII string must be greater than or equal to asciiStrLength.
1273 This function can't be used for language specific comparison.
1275 @param asciiStr the 8-Bit ASCII character string to be compared.
1276 @param asciiStrLength the length of the ascii string
1277 @return true if the strings are equal;
1278 false, otherwise.
1280 bool equalsAsciiL( const char* asciiStr, sal_Int32 asciiStrLength ) const
1282 if ( pData->length != asciiStrLength )
1283 return false;
1285 return rtl_ustr_asciil_reverseEquals_WithLength(
1286 pData->buffer, asciiStr, asciiStrLength );
1290 Perform an ASCII lowercase comparison of two strings.
1292 The result is true if and only if second string
1293 represents the same sequence of characters as the first string,
1294 ignoring the case.
1295 Character values between 65 and 90 (ASCII A-Z) are interpreted as
1296 values between 97 and 122 (ASCII a-z).
1297 Since this method is optimized for performance, the ASCII character
1298 values are not converted in any way. The caller has to make sure that
1299 all ASCII characters are in the allowed range between 0 and 127.
1300 The ASCII string must be NULL-terminated.
1301 This function can't be used for language specific comparison.
1303 @param asciiStr the 8-Bit ASCII character string to be compared.
1304 @return true if the strings are equal;
1305 false, otherwise.
1307 bool equalsIgnoreAsciiCaseAscii( const char * asciiStr ) const
1309 return rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, asciiStr ) == 0;
1312 #if defined LIBO_INTERNAL_ONLY
1313 bool equalsIgnoreAsciiCaseAscii( std::string_view asciiStr ) const
1315 return o3tl::make_unsigned(pData->length) == asciiStr.length()
1316 && rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths(
1317 pData->buffer, pData->length, asciiStr.data(), asciiStr.length()) == 0;
1319 #endif
1322 Compares two ASCII strings ignoring case
1324 The comparison is based on the numeric value of each character in
1325 the strings and return a value indicating their relationship.
1326 Since this method is optimized for performance, the ASCII character
1327 values are not converted in any way. The caller has to make sure that
1328 all ASCII characters are in the allowed range between 0 and 127.
1329 The ASCII string must be NULL-terminated.
1330 This function can't be used for language specific sorting.
1332 @param asciiStr the 8-Bit ASCII character string to be compared.
1333 @return 0 - if both strings are equal
1334 < 0 - if this string is less than the string argument
1335 > 0 - if this string is greater than the string argument
1337 @since LibreOffice 3.5
1339 sal_Int32 compareToIgnoreAsciiCaseAscii( const char * asciiStr ) const
1341 return rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, asciiStr );
1344 #if defined LIBO_INTERNAL_ONLY
1345 sal_Int32 compareToIgnoreAsciiCaseAscii( std::string_view asciiStr ) const
1347 sal_Int32 nMax = std::min<size_t>(asciiStr.length(), std::numeric_limits<sal_Int32>::max());
1348 sal_Int32 result = rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths(
1349 pData->buffer, pData->length, asciiStr.data(), nMax);
1350 if (result == 0 && o3tl::make_unsigned(pData->length) < asciiStr.length())
1351 result = -1;
1352 return result;
1354 #endif
1357 Perform an ASCII lowercase comparison of two strings.
1359 The result is true if and only if second string
1360 represents the same sequence of characters as the first string,
1361 ignoring the case.
1362 Character values between 65 and 90 (ASCII A-Z) are interpreted as
1363 values between 97 and 122 (ASCII a-z).
1364 Since this method is optimized for performance, the ASCII character
1365 values are not converted in any way. The caller has to make sure that
1366 all ASCII characters are in the allowed range between 0 and 127.
1367 The ASCII string must be greater than or equal to asciiStrLength.
1368 This function can't be used for language specific comparison.
1370 @param asciiStr the 8-Bit ASCII character string to be compared.
1371 @param asciiStrLength the length of the ascii string
1372 @return true if the strings are equal;
1373 false, otherwise.
1375 bool equalsIgnoreAsciiCaseAsciiL( const char * asciiStr, sal_Int32 asciiStrLength ) const
1377 if ( pData->length != asciiStrLength )
1378 return false;
1380 return rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, asciiStr ) == 0;
1384 Match against a substring appearing in this string.
1386 The result is true if and only if the second string appears as a substring
1387 of this string, at the given position.
1388 Since this method is optimized for performance, the ASCII character
1389 values are not converted in any way. The caller has to make sure that
1390 all ASCII characters are in the allowed range between 0 and 127.
1391 The ASCII string must be greater than or equal to asciiStrLength.
1392 This function can't be used for language specific comparison.
1394 @param asciiStr the object (substring) to be compared.
1395 @param asciiStrLength the length of asciiStr.
1396 @param fromIndex the index to start the comparison from.
1397 The index must be greater than or equal to 0
1398 and less than or equal to the string length.
1399 @return true if str match with the characters in the string
1400 at the given position;
1401 false, otherwise.
1403 bool matchAsciiL( const char* asciiStr, sal_Int32 asciiStrLength, sal_Int32 fromIndex = 0 ) const
1405 return rtl_ustr_ascii_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1406 asciiStr, asciiStrLength ) == 0;
1409 // This overload is left undefined, to detect calls of matchAsciiL that
1410 // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
1411 // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
1412 // platforms):
1413 #if SAL_TYPES_SIZEOFLONG == 8
1414 void matchAsciiL(char const *, sal_Int32, rtl_TextEncoding) const;
1415 #endif
1418 Match against a substring appearing in this string, ignoring the case of
1419 ASCII letters.
1421 The result is true if and only if the second string appears as a substring
1422 of this string, at the given position.
1423 Character values between 65 and 90 (ASCII A-Z) are interpreted as
1424 values between 97 and 122 (ASCII a-z).
1425 Since this method is optimized for performance, the ASCII character
1426 values are not converted in any way. The caller has to make sure that
1427 all ASCII characters are in the allowed range between 0 and 127.
1428 The ASCII string must be greater than or equal to asciiStrLength.
1429 This function can't be used for language specific comparison.
1431 @param asciiStr the 8-Bit ASCII character string to be compared.
1432 @param asciiStrLength the length of the ascii string
1433 @param fromIndex the index to start the comparison from.
1434 The index must be greater than or equal to 0
1435 and less than or equal to the string length.
1436 @return true if str match with the characters in the string
1437 at the given position;
1438 false, otherwise.
1440 bool matchIgnoreAsciiCaseAsciiL( const char* asciiStr, sal_Int32 asciiStrLength, sal_Int32 fromIndex = 0 ) const
1442 return rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1443 asciiStr, asciiStrLength ) == 0;
1446 // This overload is left undefined, to detect calls of
1447 // matchIgnoreAsciiCaseAsciiL that erroneously use
1448 // RTL_CONSTASCII_USTRINGPARAM instead of RTL_CONSTASCII_STRINGPARAM (but
1449 // would lead to ambiguities on 32 bit platforms):
1450 #if SAL_TYPES_SIZEOFLONG == 8
1451 void matchIgnoreAsciiCaseAsciiL(char const *, sal_Int32, rtl_TextEncoding)
1452 const;
1453 #endif
1456 Check whether this string starts with a given substring.
1458 @param str the substring to be compared
1460 @param rest if non-null, and this function returns true, then assign a
1461 copy of the remainder of this string to *rest. Available since
1462 LibreOffice 4.2
1464 @return true if and only if the given str appears as a substring at the
1465 start of this string
1467 @since LibreOffice 4.0
1469 #if defined LIBO_INTERNAL_ONLY
1470 bool startsWith(std::u16string_view sv, OUString * rest = nullptr) const {
1471 auto const b = match(sv);
1472 if (b && rest != nullptr) {
1473 *rest = copy(sv.size());
1475 return b;
1477 #else
1478 bool startsWith(OUString const & str, OUString * rest = NULL) const {
1479 bool b = match(str);
1480 if (b && rest != NULL) {
1481 *rest = copy(str.getLength());
1483 return b;
1485 #endif
1488 @overload
1489 This function accepts an ASCII string literal as its argument.
1490 @since LibreOffice 4.0
1492 template< typename T >
1493 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type startsWith(
1494 T & literal, OUString * rest = NULL) const
1496 assert(
1497 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1498 bool b
1499 = (libreoffice_internal::ConstCharArrayDetector<T>::length
1500 <= sal_uInt32(pData->length))
1501 && rtl_ustr_asciil_reverseEquals_WithLength(
1502 pData->buffer,
1503 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1504 literal),
1505 libreoffice_internal::ConstCharArrayDetector<T>::length);
1506 if (b && rest != NULL) {
1507 *rest = copy(
1508 libreoffice_internal::ConstCharArrayDetector<T>::length);
1510 return b;
1514 Check whether this string starts with a given string, ignoring the case of
1515 ASCII letters.
1517 Character values between 65 and 90 (ASCII A-Z) are interpreted as
1518 values between 97 and 122 (ASCII a-z).
1519 This function can't be used for language specific comparison.
1521 @param str the substring to be compared
1523 @param rest if non-null, and this function returns true, then assign a
1524 copy of the remainder of this string to *rest. Available since
1525 LibreOffice 4.2
1527 @return true if and only if the given str appears as a substring at the
1528 start of this string, ignoring the case of ASCII letters ("A"--"Z" and
1529 "a"--"z")
1531 @since LibreOffice 4.0
1533 #if defined LIBO_INTERNAL_ONLY
1534 bool startsWithIgnoreAsciiCase(std::u16string_view sv, OUString * rest = nullptr) const {
1535 auto const b = matchIgnoreAsciiCase(sv);
1536 if (b && rest != nullptr) {
1537 *rest = copy(sv.size());
1539 return b;
1541 #else
1542 bool startsWithIgnoreAsciiCase(OUString const & str, OUString * rest = NULL)
1543 const
1545 bool b = matchIgnoreAsciiCase(str);
1546 if (b && rest != NULL) {
1547 *rest = copy(str.getLength());
1549 return b;
1551 #endif
1554 @overload
1555 This function accepts an ASCII string literal as its argument.
1556 @since LibreOffice 4.0
1558 template< typename T >
1559 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type
1560 startsWithIgnoreAsciiCase(T & literal, OUString * rest = NULL) const
1562 assert(
1563 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1564 bool b
1565 = (libreoffice_internal::ConstCharArrayDetector<T>::length
1566 <= sal_uInt32(pData->length))
1567 && (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths(
1568 pData->buffer,
1569 libreoffice_internal::ConstCharArrayDetector<T>::length,
1570 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1571 literal),
1572 libreoffice_internal::ConstCharArrayDetector<T>::length)
1573 == 0);
1574 if (b && rest != NULL) {
1575 *rest = copy(
1576 libreoffice_internal::ConstCharArrayDetector<T>::length);
1578 return b;
1582 Check whether this string ends with a given substring.
1584 @param str the substring to be compared
1586 @param rest if non-null, and this function returns true, then assign a
1587 copy of the remainder of this string to *rest. Available since
1588 LibreOffice 4.2
1590 @return true if and only if the given str appears as a substring at the
1591 end of this string
1593 @since LibreOffice 3.6
1595 #if defined LIBO_INTERNAL_ONLY
1596 bool endsWith(std::u16string_view sv, OUString * rest = nullptr) const {
1597 auto const b = sv.size() <= sal_uInt32(pData->length)
1598 && match(sv, pData->length - sv.size());
1599 if (b && rest != nullptr) {
1600 *rest = copy(0, (pData->length - sv.size()));
1602 return b;
1604 #else
1605 bool endsWith(OUString const & str, OUString * rest = NULL) const {
1606 bool b = str.getLength() <= getLength()
1607 && match(str, getLength() - str.getLength());
1608 if (b && rest != NULL) {
1609 *rest = copy(0, getLength() - str.getLength());
1611 return b;
1613 #endif
1616 @overload
1617 This function accepts an ASCII string literal as its argument.
1618 @since LibreOffice 3.6
1620 template< typename T >
1621 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type
1622 endsWith(T & literal, OUString * rest = NULL) const
1624 assert(
1625 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1626 bool b
1627 = (libreoffice_internal::ConstCharArrayDetector<T>::length
1628 <= sal_uInt32(pData->length))
1629 && rtl_ustr_asciil_reverseEquals_WithLength(
1630 (pData->buffer + pData->length
1631 - libreoffice_internal::ConstCharArrayDetector<T>::length),
1632 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1633 literal),
1634 libreoffice_internal::ConstCharArrayDetector<T>::length);
1635 if (b && rest != NULL) {
1636 *rest = copy(
1638 (getLength()
1639 - libreoffice_internal::ConstCharArrayDetector<T>::length));
1641 return b;
1645 Check whether this string ends with a given ASCII string.
1647 @param asciiStr a sequence of at least asciiStrLength ASCII characters
1648 (bytes in the range 0x00--0x7F)
1649 @param asciiStrLength the length of asciiStr; must be non-negative
1650 @return true if this string ends with asciiStr; otherwise, false is
1651 returned
1653 @since UDK 3.2.7
1655 bool endsWithAsciiL(char const * asciiStr, sal_Int32 asciiStrLength)
1656 const
1658 return asciiStrLength <= pData->length
1659 && rtl_ustr_asciil_reverseEquals_WithLength(
1660 pData->buffer + pData->length - asciiStrLength, asciiStr,
1661 asciiStrLength);
1665 Check whether this string ends with a given string, ignoring the case of
1666 ASCII letters.
1668 Character values between 65 and 90 (ASCII A-Z) are interpreted as
1669 values between 97 and 122 (ASCII a-z).
1670 This function can't be used for language specific comparison.
1672 @param str the substring to be compared
1674 @param rest if non-null, and this function returns true, then assign a
1675 copy of the remainder of this string to *rest. Available since
1676 LibreOffice 4.2
1678 @return true if and only if the given str appears as a substring at the
1679 end of this string, ignoring the case of ASCII letters ("A"--"Z" and
1680 "a"--"z")
1682 @since LibreOffice 3.6
1684 #if defined LIBO_INTERNAL_ONLY
1685 bool endsWithIgnoreAsciiCase(std::u16string_view sv, OUString * rest = nullptr) const {
1686 auto const b = sv.size() <= sal_uInt32(pData->length)
1687 && matchIgnoreAsciiCase(sv, pData->length - sv.size());
1688 if (b && rest != nullptr) {
1689 *rest = copy(0, pData->length - sv.size());
1691 return b;
1693 #else
1694 bool endsWithIgnoreAsciiCase(OUString const & str, OUString * rest = NULL) const
1696 bool b = str.getLength() <= getLength()
1697 && matchIgnoreAsciiCase(str, getLength() - str.getLength());
1698 if (b && rest != NULL) {
1699 *rest = copy(0, getLength() - str.getLength());
1701 return b;
1703 #endif
1706 @overload
1707 This function accepts an ASCII string literal as its argument.
1708 @since LibreOffice 3.6
1710 template< typename T >
1711 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type
1712 endsWithIgnoreAsciiCase(T & literal, OUString * rest = NULL) const
1714 assert(
1715 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1716 bool b
1717 = (libreoffice_internal::ConstCharArrayDetector<T>::length
1718 <= sal_uInt32(pData->length))
1719 && (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths(
1720 (pData->buffer + pData->length
1721 - libreoffice_internal::ConstCharArrayDetector<T>::length),
1722 libreoffice_internal::ConstCharArrayDetector<T>::length,
1723 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1724 literal),
1725 libreoffice_internal::ConstCharArrayDetector<T>::length)
1726 == 0);
1727 if (b && rest != NULL) {
1728 *rest = copy(
1730 (getLength()
1731 - libreoffice_internal::ConstCharArrayDetector<T>::length));
1733 return b;
1737 Check whether this string ends with a given ASCII string, ignoring the
1738 case of ASCII letters.
1740 @param asciiStr a sequence of at least asciiStrLength ASCII characters
1741 (bytes in the range 0x00--0x7F)
1742 @param asciiStrLength the length of asciiStr; must be non-negative
1743 @return true if this string ends with asciiStr, ignoring the case of ASCII
1744 letters ("A"--"Z" and "a"--"z"); otherwise, false is returned
1746 bool endsWithIgnoreAsciiCaseAsciiL(
1747 char const * asciiStr, sal_Int32 asciiStrLength) const
1749 return asciiStrLength <= pData->length
1750 && (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths(
1751 pData->buffer + pData->length - asciiStrLength,
1752 asciiStrLength, asciiStr, asciiStrLength)
1753 == 0);
1756 friend bool operator == ( const OUString& rStr1, const OUString& rStr2 )
1757 { return rStr1.equals(rStr2); }
1759 friend bool operator != ( const OUString& rStr1, const OUString& rStr2 )
1760 { return !(operator == ( rStr1, rStr2 )); }
1762 friend bool operator < ( const OUString& rStr1, const OUString& rStr2 )
1763 { return rStr1.compareTo( rStr2 ) < 0; }
1764 friend bool operator > ( const OUString& rStr1, const OUString& rStr2 )
1765 { return rStr1.compareTo( rStr2 ) > 0; }
1766 friend bool operator <= ( const OUString& rStr1, const OUString& rStr2 )
1767 { return rStr1.compareTo( rStr2 ) <= 0; }
1768 friend bool operator >= ( const OUString& rStr1, const OUString& rStr2 )
1769 { return rStr1.compareTo( rStr2 ) >= 0; }
1771 #if defined LIBO_INTERNAL_ONLY
1773 template<typename T> friend typename libreoffice_internal::CharPtrDetector<T, bool>::TypeUtf16
1774 operator ==(OUString const & s1, T const & s2) {
1775 return rtl_ustr_compare_WithLength(s1.getStr(), s1.getLength(), s2, rtl_ustr_getLength(s2))
1776 == 0;
1779 template<typename T>
1780 friend typename libreoffice_internal::NonConstCharArrayDetector<T, bool>::TypeUtf16
1781 operator ==(OUString const & s1, T & s2) {
1782 return rtl_ustr_compare_WithLength(s1.getStr(), s1.getLength(), s2, rtl_ustr_getLength(s2))
1783 == 0;
1786 template<typename T> friend typename libreoffice_internal::CharPtrDetector<T, bool>::TypeUtf16
1787 operator ==(T const & s1, OUString const & s2) {
1788 return rtl_ustr_compare_WithLength(s1, rtl_ustr_getLength(s1), s2.getStr(), s2.getLength())
1789 == 0;
1792 template<typename T>
1793 friend typename libreoffice_internal::NonConstCharArrayDetector<T, bool>::TypeUtf16
1794 operator ==(T & s1, OUString const & s2) {
1795 return rtl_ustr_compare_WithLength(s1, rtl_ustr_getLength(s1), s2.getStr(), s2.getLength())
1796 == 0;
1799 template<typename T> friend typename libreoffice_internal::CharPtrDetector<T, bool>::TypeUtf16
1800 operator !=(OUString const & s1, T const & s2) { return !(s1 == s2); }
1802 template<typename T>
1803 friend typename libreoffice_internal::NonConstCharArrayDetector<T, bool>::TypeUtf16
1804 operator !=(OUString const & s1, T & s2) { return !(s1 == s2); }
1806 template<typename T> friend typename libreoffice_internal::CharPtrDetector<T, bool>::TypeUtf16
1807 operator !=(T const & s1, OUString const & s2) { return !(s1 == s2); }
1809 template<typename T>
1810 friend typename libreoffice_internal::NonConstCharArrayDetector<T, bool>::TypeUtf16
1811 operator !=(T & s1, OUString const & s2) { return !(s1 == s2); }
1813 #else
1815 friend bool operator == ( const OUString& rStr1, const sal_Unicode * pStr2 )
1816 { return rStr1.compareTo( pStr2 ) == 0; }
1817 friend bool operator == ( const sal_Unicode * pStr1, const OUString& rStr2 )
1818 { return OUString( pStr1 ).compareTo( rStr2 ) == 0; }
1820 friend bool operator != ( const OUString& rStr1, const sal_Unicode * pStr2 )
1821 { return !(operator == ( rStr1, pStr2 )); }
1822 friend bool operator != ( const sal_Unicode * pStr1, const OUString& rStr2 )
1823 { return !(operator == ( pStr1, rStr2 )); }
1825 #endif
1828 * Compare string to an ASCII string literal.
1830 * This operator is equal to calling equalsAsciiL().
1832 * @since LibreOffice 3.6
1834 template< typename T >
1835 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator==( const OUString& rString, T& literal )
1837 assert(
1838 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1839 return rString.equalsAsciiL(
1840 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1841 libreoffice_internal::ConstCharArrayDetector<T>::length);
1844 * Compare string to an ASCII string literal.
1846 * This operator is equal to calling equalsAsciiL().
1848 * @since LibreOffice 3.6
1850 template< typename T >
1851 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator==( T& literal, const OUString& rString )
1853 assert(
1854 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1855 return rString.equalsAsciiL(
1856 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1857 libreoffice_internal::ConstCharArrayDetector<T>::length);
1860 * Compare string to an ASCII string literal.
1862 * This operator is equal to calling !equalsAsciiL().
1864 * @since LibreOffice 3.6
1866 template< typename T >
1867 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator!=( const OUString& rString, T& literal )
1869 assert(
1870 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1871 return !rString.equalsAsciiL(
1872 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1873 libreoffice_internal::ConstCharArrayDetector<T>::length);
1876 * Compare string to an ASCII string literal.
1878 * This operator is equal to calling !equalsAsciiL().
1880 * @since LibreOffice 3.6
1882 template< typename T >
1883 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator!=( T& literal, const OUString& rString )
1885 assert(
1886 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1887 return !rString.equalsAsciiL(
1888 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1889 libreoffice_internal::ConstCharArrayDetector<T>::length);
1892 #if defined LIBO_INTERNAL_ONLY
1893 /** @overload @since LibreOffice 5.3 */
1894 template<typename T> friend typename libreoffice_internal::ConstCharArrayDetector<T, bool>::TypeUtf16
1895 operator ==(OUString const & string, T & literal) {
1896 return
1897 rtl_ustr_reverseCompare_WithLength(
1898 string.pData->buffer, string.pData->length,
1899 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1900 literal),
1901 libreoffice_internal::ConstCharArrayDetector<T>::length)
1902 == 0;
1904 /** @overload @since LibreOffice 5.3 */
1905 template<typename T> friend typename libreoffice_internal::ConstCharArrayDetector<T, bool>::TypeUtf16
1906 operator ==(T & literal, OUString const & string) {
1907 return
1908 rtl_ustr_reverseCompare_WithLength(
1909 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1910 literal),
1911 libreoffice_internal::ConstCharArrayDetector<T>::length,
1912 string.pData->buffer, string.pData->length)
1913 == 0;
1915 /** @overload @since LibreOffice 5.3 */
1916 template<typename T> friend typename libreoffice_internal::ConstCharArrayDetector<T, bool>::TypeUtf16
1917 operator !=(OUString const & string, T & literal) {
1918 return
1919 rtl_ustr_reverseCompare_WithLength(
1920 string.pData->buffer, string.pData->length,
1921 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1922 literal),
1923 libreoffice_internal::ConstCharArrayDetector<T>::length)
1924 != 0;
1926 /** @overload @since LibreOffice 5.3 */
1927 template<typename T> friend typename libreoffice_internal::ConstCharArrayDetector<T, bool>::TypeUtf16
1928 operator !=(T & literal, OUString const & string) {
1929 return
1930 rtl_ustr_reverseCompare_WithLength(
1931 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1932 literal),
1933 libreoffice_internal::ConstCharArrayDetector<T>::length,
1934 string.pData->buffer, string.pData->length)
1935 != 0;
1937 #endif
1940 Returns a hashcode for this string.
1942 @return a hash code value for this object.
1944 @see rtl::OUStringHash for convenient use of std::unordered_map
1946 sal_Int32 hashCode() const
1948 return rtl_ustr_hashCode_WithLength( pData->buffer, pData->length );
1952 Returns the index within this string of the first occurrence of the
1953 specified character, starting the search at the specified index.
1955 @param ch character to be located.
1956 @param fromIndex the index to start the search from.
1957 The index must be greater than or equal to 0
1958 and less than or equal to the string length.
1959 @return the index of the first occurrence of the character in the
1960 character sequence represented by this string that is
1961 greater than or equal to fromIndex, or
1962 -1 if the character does not occur.
1964 sal_Int32 indexOf( sal_Unicode ch, sal_Int32 fromIndex = 0 ) const
1966 sal_Int32 ret = rtl_ustr_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch );
1967 return (ret < 0 ? ret : ret+fromIndex);
1971 Returns the index within this string of the last occurrence of the
1972 specified character, searching backward starting at the end.
1974 @param ch character to be located.
1975 @return the index of the last occurrence of the character in the
1976 character sequence represented by this string, or
1977 -1 if the character does not occur.
1979 sal_Int32 lastIndexOf( sal_Unicode ch ) const
1981 return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch );
1985 Returns the index within this string of the last occurrence of the
1986 specified character, searching backward starting before the specified
1987 index.
1989 @param ch character to be located.
1990 @param fromIndex the index before which to start the search.
1991 @return the index of the last occurrence of the character in the
1992 character sequence represented by this string that
1993 is less than fromIndex, or -1
1994 if the character does not occur before that point.
1996 sal_Int32 lastIndexOf( sal_Unicode ch, sal_Int32 fromIndex ) const
1998 return rtl_ustr_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch );
2002 Returns the index within this string of the first occurrence of the
2003 specified substring, starting at the specified index.
2005 If str doesn't include any character, always -1 is
2006 returned. This is also the case, if both strings are empty.
2008 @param str the substring to search for.
2009 @param fromIndex the index to start the search from.
2010 @return If the string argument occurs one or more times as a substring
2011 within this string at the starting index, then the index
2012 of the first character of the first such substring is
2013 returned. If it does not occur as a substring starting
2014 at fromIndex or beyond, -1 is returned.
2016 #if defined LIBO_INTERNAL_ONLY
2017 sal_Int32 indexOf(std::u16string_view sv, sal_Int32 fromIndex = 0) const {
2018 auto const n = rtl_ustr_indexOfStr_WithLength(
2019 pData->buffer + fromIndex, pData->length - fromIndex, sv.data(), sv.size());
2020 return n < 0 ? n : n + fromIndex;
2022 #else
2023 sal_Int32 indexOf( const OUString & str, sal_Int32 fromIndex = 0 ) const
2025 sal_Int32 ret = rtl_ustr_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
2026 str.pData->buffer, str.pData->length );
2027 return (ret < 0 ? ret : ret+fromIndex);
2029 #endif
2032 @overload
2033 This function accepts an ASCII string literal as its argument.
2034 @since LibreOffice 3.6
2036 template< typename T >
2037 typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const
2039 assert(
2040 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
2041 sal_Int32 n = rtl_ustr_indexOfAscii_WithLength(
2042 pData->buffer + fromIndex, pData->length - fromIndex,
2043 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
2044 libreoffice_internal::ConstCharArrayDetector<T>::length);
2045 return n < 0 ? n : n + fromIndex;
2049 Returns the index within this string of the first occurrence of the
2050 specified ASCII substring, starting at the specified index.
2052 @param str
2053 the substring to be searched for. Need not be null-terminated, but must
2054 be at least as long as the specified len. Must only contain characters
2055 in the ASCII range 0x00--7F.
2057 @param len
2058 the length of the substring; must be non-negative.
2060 @param fromIndex
2061 the index to start the search from. Must be in the range from zero to
2062 the length of this string, inclusive.
2064 @return
2065 the index (starting at 0) of the first character of the first occurrence
2066 of the substring within this string starting at the given fromIndex, or
2067 -1 if the substring does not occur. If len is zero, -1 is returned.
2069 @since UDK 3.2.7
2071 sal_Int32 indexOfAsciiL(
2072 char const * str, sal_Int32 len, sal_Int32 fromIndex = 0) const
2074 sal_Int32 ret = rtl_ustr_indexOfAscii_WithLength(
2075 pData->buffer + fromIndex, pData->length - fromIndex, str, len);
2076 return ret < 0 ? ret : ret + fromIndex;
2079 // This overload is left undefined, to detect calls of indexOfAsciiL that
2080 // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
2081 // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
2082 // platforms):
2083 #if SAL_TYPES_SIZEOFLONG == 8
2084 void indexOfAsciiL(char const *, sal_Int32 len, rtl_TextEncoding) const;
2085 #endif
2088 Returns the index within this string of the last occurrence of
2089 the specified substring, searching backward starting at the end.
2091 The returned index indicates the starting index of the substring
2092 in this string.
2093 If str doesn't include any character, always -1 is
2094 returned. This is also the case, if both strings are empty.
2096 @param str the substring to search for.
2097 @return If the string argument occurs one or more times as a substring
2098 within this string, then the index of the first character of
2099 the last such substring is returned. If it does not occur as
2100 a substring, -1 is returned.
2102 #if defined LIBO_INTERNAL_ONLY
2103 sal_Int32 lastIndexOf(std::u16string_view sv) const {
2104 return rtl_ustr_lastIndexOfStr_WithLength(
2105 pData->buffer, pData->length, sv.data(), sv.size());
2107 #else
2108 sal_Int32 lastIndexOf( const OUString & str ) const
2110 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length,
2111 str.pData->buffer, str.pData->length );
2113 #endif
2116 Returns the index within this string of the last occurrence of
2117 the specified substring, searching backward starting before the specified
2118 index.
2120 The returned index indicates the starting index of the substring
2121 in this string.
2122 If str doesn't include any character, always -1 is
2123 returned. This is also the case, if both strings are empty.
2125 @param str the substring to search for.
2126 @param fromIndex the index before which to start the search.
2127 @return If the string argument occurs one or more times as a substring
2128 within this string before the starting index, then the index
2129 of the first character of the last such substring is
2130 returned. Otherwise, -1 is returned.
2132 #if defined LIBO_INTERNAL_ONLY
2133 sal_Int32 lastIndexOf(std::u16string_view sv, sal_Int32 fromIndex) const {
2134 return rtl_ustr_lastIndexOfStr_WithLength(pData->buffer, fromIndex, sv.data(), sv.size());
2136 #else
2137 sal_Int32 lastIndexOf( const OUString & str, sal_Int32 fromIndex ) const
2139 return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
2140 str.pData->buffer, str.pData->length );
2142 #endif
2145 @overload
2146 This function accepts an ASCII string literal as its argument.
2147 @since LibreOffice 3.6
2149 template< typename T >
2150 typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type lastIndexOf( T& literal ) const
2152 assert(
2153 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
2154 return rtl_ustr_lastIndexOfAscii_WithLength(
2155 pData->buffer, pData->length,
2156 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
2157 libreoffice_internal::ConstCharArrayDetector<T>::length);
2161 Returns the index within this string of the last occurrence of the
2162 specified ASCII substring.
2164 @param str
2165 the substring to be searched for. Need not be null-terminated, but must
2166 be at least as long as the specified len. Must only contain characters
2167 in the ASCII range 0x00--7F.
2169 @param len
2170 the length of the substring; must be non-negative.
2172 @return
2173 the index (starting at 0) of the first character of the last occurrence
2174 of the substring within this string, or -1 if the substring does not
2175 occur. If len is zero, -1 is returned.
2177 @since UDK 3.2.7
2179 sal_Int32 lastIndexOfAsciiL(char const * str, sal_Int32 len) const
2181 return rtl_ustr_lastIndexOfAscii_WithLength(
2182 pData->buffer, pData->length, str, len);
2186 Returns a new string that is a substring of this string.
2188 The substring begins at the specified beginIndex. If
2189 beginIndex is negative or be greater than the length of
2190 this string, behaviour is undefined.
2192 @param beginIndex the beginning index, inclusive.
2193 @return the specified substring.
2195 SAL_WARN_UNUSED_RESULT OUString copy( sal_Int32 beginIndex ) const
2197 return copy(beginIndex, getLength() - beginIndex);
2201 Returns a new string that is a substring of this string.
2203 The substring begins at the specified beginIndex and contains count
2204 characters. If either beginIndex or count are negative,
2205 or beginIndex + count are greater than the length of this string
2206 then behaviour is undefined.
2208 @param beginIndex the beginning index, inclusive.
2209 @param count the number of characters.
2210 @return the specified substring.
2212 SAL_WARN_UNUSED_RESULT OUString copy( sal_Int32 beginIndex, sal_Int32 count ) const
2214 rtl_uString *pNew = NULL;
2215 rtl_uString_newFromSubString( &pNew, pData, beginIndex, count );
2216 return OUString( pNew, SAL_NO_ACQUIRE );
2219 #if defined LIBO_INTERNAL_ONLY
2221 Returns a std::u16string_view that is a view of a substring of this string.
2223 The substring begins at the specified beginIndex. If
2224 beginIndex is negative or be greater than the length of
2225 this string, behaviour is undefined.
2227 @param beginIndex the beginning index, inclusive.
2228 @return the specified substring.
2230 SAL_WARN_UNUSED_RESULT std::u16string_view subView( sal_Int32 beginIndex ) const
2232 assert(beginIndex >= 0);
2233 assert(beginIndex <= getLength());
2234 return subView(beginIndex, getLength() - beginIndex);
2238 Returns a std::u16string_view that is a view of a substring of this string.
2240 The substring begins at the specified beginIndex and contains count
2241 characters. If either beginIndex or count are negative,
2242 or beginIndex + count are greater than the length of this string
2243 then behaviour is undefined.
2245 @param beginIndex the beginning index, inclusive.
2246 @param count the number of characters.
2247 @return the specified substring.
2249 SAL_WARN_UNUSED_RESULT std::u16string_view subView( sal_Int32 beginIndex, sal_Int32 count ) const
2251 assert(beginIndex >= 0);
2252 assert(count >= 0);
2253 assert(beginIndex <= getLength());
2254 assert(count <= getLength() - beginIndex);
2255 return std::u16string_view(*this).substr(beginIndex, count);
2257 #endif
2259 #ifndef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
2261 Concatenates the specified string to the end of this string.
2263 @param str the string that is concatenated to the end
2264 of this string.
2265 @return a string that represents the concatenation of this string
2266 followed by the string argument.
2268 SAL_WARN_UNUSED_RESULT OUString concat( const OUString & str ) const
2270 rtl_uString* pNew = NULL;
2271 rtl_uString_newConcat( &pNew, pData, str.pData );
2272 return OUString( pNew, SAL_NO_ACQUIRE );
2274 #endif
2276 #ifndef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
2277 friend OUString operator+( const OUString& rStr1, const OUString& rStr2 )
2279 return rStr1.concat( rStr2 );
2281 #endif
2283 // hide this from internal code to avoid ambiguous lookup error
2284 #ifndef LIBO_INTERNAL_ONLY
2286 Returns a new string resulting from replacing n = count characters
2287 from position index in this string with newStr.
2289 @param index the replacing index in str.
2290 The index must be greater than or equal to 0 and
2291 less than or equal to the length of the string.
2292 @param count the count of characters that will be replaced
2293 The count must be greater than or equal to 0 and
2294 less than or equal to the length of the string minus index.
2295 @param newStr the new substring.
2296 @return the new string.
2298 SAL_WARN_UNUSED_RESULT OUString replaceAt( sal_Int32 index, sal_Int32 count, const OUString& newStr ) const
2300 rtl_uString* pNew = NULL;
2301 rtl_uString_newReplaceStrAt( &pNew, pData, index, count, newStr.pData );
2302 return OUString( pNew, SAL_NO_ACQUIRE );
2304 #endif
2306 #ifdef LIBO_INTERNAL_ONLY
2307 SAL_WARN_UNUSED_RESULT OUString replaceAt( sal_Int32 index, sal_Int32 count, std::u16string_view newStr ) const
2309 rtl_uString* pNew = NULL;
2310 rtl_uString_newReplaceStrAtUtf16L( &pNew, pData, index, count, newStr.data(), newStr.size() );
2311 return OUString( pNew, SAL_NO_ACQUIRE );
2313 #endif
2316 Returns a new string resulting from replacing all occurrences of
2317 oldChar in this string with newChar.
2319 If the character oldChar does not occur in the character sequence
2320 represented by this object, then the string is assigned with
2321 str.
2323 @param oldChar the old character.
2324 @param newChar the new character.
2325 @return a string derived from this string by replacing every
2326 occurrence of oldChar with newChar.
2328 SAL_WARN_UNUSED_RESULT OUString replace( sal_Unicode oldChar, sal_Unicode newChar ) const
2330 rtl_uString* pNew = NULL;
2331 rtl_uString_newReplace( &pNew, pData, oldChar, newChar );
2332 return OUString( pNew, SAL_NO_ACQUIRE );
2336 Returns a new string resulting from replacing the first occurrence of a
2337 given substring with another substring.
2339 @param from the substring to be replaced
2341 @param to the replacing substring
2343 @param[in,out] index pointer to a start index; if the pointer is
2344 non-null: upon entry to the function, its value is the index into this
2345 string at which to start searching for the \p from substring, the value
2346 must be non-negative and not greater than this string's length; upon exiting
2347 the function its value is the index into this string at which the
2348 replacement took place or -1 if no replacement took place; if the pointer
2349 is null, searching always starts at index 0
2351 @since LibreOffice 3.6
2353 #if defined LIBO_INTERNAL_ONLY
2354 [[nodiscard]] OUString replaceFirst(
2355 std::u16string_view from, std::u16string_view to, sal_Int32 * index = nullptr) const
2357 rtl_uString * s = nullptr;
2358 sal_Int32 i = 0;
2359 rtl_uString_newReplaceFirstUtf16LUtf16L(
2360 &s, pData, from.data(), from.size(), to.data(), to.size(),
2361 index == nullptr ? &i : index);
2362 return OUString(s, SAL_NO_ACQUIRE);
2364 #else
2365 SAL_WARN_UNUSED_RESULT OUString replaceFirst(
2366 OUString const & from, OUString const & to, sal_Int32 * index = NULL) const
2368 rtl_uString * s = NULL;
2369 sal_Int32 i = 0;
2370 rtl_uString_newReplaceFirst(
2371 &s, pData, from.pData, to.pData, index == NULL ? &i : index);
2372 return OUString(s, SAL_NO_ACQUIRE);
2374 #endif
2377 Returns a new string resulting from replacing the first occurrence of a
2378 given substring with another substring.
2380 @param from ASCII string literal, the substring to be replaced
2382 @param to the replacing substring
2384 @param[in,out] index pointer to a start index; if the pointer is
2385 non-null: upon entry to the function, its value is the index into the this
2386 string at which to start searching for the \p from substring, the value
2387 must be non-negative and not greater than this string's length; upon exiting
2388 the function its value is the index into this string at which the
2389 replacement took place or -1 if no replacement took place; if the pointer
2390 is null, searching always starts at index 0
2392 @since LibreOffice 3.6
2394 #if defined LIBO_INTERNAL_ONLY
2395 template<typename T> [[nodiscard]]
2396 typename libreoffice_internal::ConstCharArrayDetector<T, OUString >::Type replaceFirst(
2397 T & from, std::u16string_view to, sal_Int32 * index = nullptr) const
2399 assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(from));
2400 rtl_uString * s = nullptr;
2401 sal_Int32 i = 0;
2402 rtl_uString_newReplaceFirstAsciiLUtf16L(
2403 &s, pData, libreoffice_internal::ConstCharArrayDetector<T>::toPointer(from),
2404 libreoffice_internal::ConstCharArrayDetector<T>::length, to.data(), to.size(),
2405 index == nullptr ? &i : index);
2406 return OUString(s, SAL_NO_ACQUIRE);
2408 #else
2409 template< typename T >
2410 SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T, OUString >::Type replaceFirst( T& from, OUString const & to,
2411 sal_Int32 * index = NULL) const
2413 assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(from));
2414 rtl_uString * s = NULL;
2415 sal_Int32 i = 0;
2416 rtl_uString_newReplaceFirstAsciiL(
2417 &s, pData,
2418 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(from),
2419 libreoffice_internal::ConstCharArrayDetector<T>::length, to.pData,
2420 index == NULL ? &i : index);
2421 return OUString(s, SAL_NO_ACQUIRE);
2423 #endif
2426 Returns a new string resulting from replacing the first occurrence of a
2427 given substring with another substring.
2429 @param from the substring to be replaced
2431 @param to ASCII string literal, the replacing substring
2433 @param[in,out] index pointer to a start index; if the pointer is
2434 non-null: upon entry to the function, its value is the index into the this
2435 string at which to start searching for the \p from substring, the value
2436 must be non-negative and not greater than this string's length; upon exiting
2437 the function its value is the index into this string at which the
2438 replacement took place or -1 if no replacement took place; if the pointer
2439 is null, searching always starts at index 0
2441 @since LibreOffice 5.1
2443 #if defined LIBO_INTERNAL_ONLY
2444 template<typename T> [[nodiscard]]
2445 typename libreoffice_internal::ConstCharArrayDetector<T, OUString >::Type replaceFirst(
2446 std::u16string_view from, T & to, sal_Int32 * index = nullptr) const
2448 assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(to));
2449 rtl_uString * s = nullptr;
2450 sal_Int32 i = 0;
2451 rtl_uString_newReplaceFirstUtf16LAsciiL(
2452 &s, pData, from.data(), from.size(),
2453 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(to),
2454 libreoffice_internal::ConstCharArrayDetector<T>::length, index == nullptr ? &i : index);
2455 return OUString(s, SAL_NO_ACQUIRE);
2457 #else
2458 template< typename T >
2459 SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T, OUString >::Type replaceFirst( OUString const & from, T& to,
2460 sal_Int32 * index = NULL) const
2462 assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(to));
2463 rtl_uString * s = NULL;
2464 sal_Int32 i = 0;
2465 rtl_uString_newReplaceFirstToAsciiL(
2466 &s, pData, from.pData,
2467 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(to),
2468 libreoffice_internal::ConstCharArrayDetector<T>::length,
2469 index == NULL ? &i : index);
2470 return OUString(s, SAL_NO_ACQUIRE);
2472 #endif
2475 Returns a new string resulting from replacing the first occurrence of a
2476 given substring with another substring.
2478 @param from ASCII string literal, the substring to be replaced
2480 @param to ASCII string literal, the substring to be replaced
2482 @param[in,out] index pointer to a start index; if the pointer is
2483 non-null: upon entry to the function, its value is the index into the this
2484 string at which to start searching for the \p from substring, the value
2485 must be non-negative and not greater than this string's length; upon exiting
2486 the function its value is the index into this string at which the
2487 replacement took place or -1 if no replacement took place; if the pointer
2488 is null, searching always starts at index 0
2490 @since LibreOffice 3.6
2492 template< typename T1, typename T2 >
2493 SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T1, typename libreoffice_internal::ConstCharArrayDetector< T2, OUString >::Type >::Type
2494 replaceFirst( T1& from, T2& to, sal_Int32 * index = NULL) const
2496 assert(libreoffice_internal::ConstCharArrayDetector<T1>::isValid(from));
2497 assert(libreoffice_internal::ConstCharArrayDetector<T2>::isValid(to));
2498 rtl_uString * s = NULL;
2499 sal_Int32 i = 0;
2500 rtl_uString_newReplaceFirstAsciiLAsciiL(
2501 &s, pData,
2502 libreoffice_internal::ConstCharArrayDetector<T1>::toPointer(from),
2503 libreoffice_internal::ConstCharArrayDetector<T1>::length,
2504 libreoffice_internal::ConstCharArrayDetector<T2>::toPointer(to),
2505 libreoffice_internal::ConstCharArrayDetector<T2>::length,
2506 index == NULL ? &i : index);
2507 return OUString(s, SAL_NO_ACQUIRE);
2511 Returns a new string resulting from replacing all occurrences of a given
2512 substring with another substring.
2514 Replacing subsequent occurrences picks up only after a given replacement.
2515 That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
2517 @param from the substring to be replaced
2519 @param to the replacing substring
2521 @param fromIndex the position in the string where we will begin searching
2523 @since LibreOffice 4.0
2525 #if defined LIBO_INTERNAL_ONLY
2526 [[nodiscard]] OUString replaceAll(
2527 std::u16string_view from, std::u16string_view to, sal_Int32 fromIndex = 0) const
2529 rtl_uString * s = nullptr;
2530 rtl_uString_newReplaceAllFromIndexUtf16LUtf16L(
2531 &s, pData, from.data(), from.size(), to.data(), to.size(), fromIndex);
2532 return OUString(s, SAL_NO_ACQUIRE);
2534 #else
2535 SAL_WARN_UNUSED_RESULT OUString replaceAll(
2536 OUString const & from, OUString const & to, sal_Int32 fromIndex = 0) const
2538 rtl_uString * s = NULL;
2539 rtl_uString_newReplaceAllFromIndex(&s, pData, from.pData, to.pData, fromIndex);
2540 return OUString(s, SAL_NO_ACQUIRE);
2542 #endif
2545 Returns a new string resulting from replacing all occurrences of a given
2546 substring with another substring.
2548 Replacing subsequent occurrences picks up only after a given replacement.
2549 That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
2551 @param from ASCII string literal, the substring to be replaced
2553 @param to the replacing substring
2555 @since LibreOffice 3.6
2557 #if defined LIBO_INTERNAL_ONLY
2558 template<typename T> [[nodiscard]]
2559 typename libreoffice_internal::ConstCharArrayDetector<T, OUString >::Type replaceAll(
2560 T & from, std::u16string_view to) const
2562 assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(from));
2563 rtl_uString * s = nullptr;
2564 rtl_uString_newReplaceAllAsciiLUtf16L(
2565 &s, pData, libreoffice_internal::ConstCharArrayDetector<T>::toPointer(from),
2566 libreoffice_internal::ConstCharArrayDetector<T>::length, to.data(), to.size());
2567 return OUString(s, SAL_NO_ACQUIRE);
2569 #else
2570 template< typename T >
2571 SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T, OUString >::Type replaceAll( T& from, OUString const & to) const
2573 assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(from));
2574 rtl_uString * s = NULL;
2575 rtl_uString_newReplaceAllAsciiL(
2576 &s, pData,
2577 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(from),
2578 libreoffice_internal::ConstCharArrayDetector<T>::length, to.pData);
2579 return OUString(s, SAL_NO_ACQUIRE);
2581 #endif
2584 Returns a new string resulting from replacing all occurrences of a given
2585 substring with another substring.
2587 Replacing subsequent occurrences picks up only after a given replacement.
2588 That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
2590 @param from the substring to be replaced
2592 @param to ASCII string literal, the replacing substring
2594 @since LibreOffice 5.1
2596 #if defined LIBO_INTERNAL_ONLY
2597 template<typename T> [[nodiscard]]
2598 typename libreoffice_internal::ConstCharArrayDetector<T, OUString >::Type replaceAll(
2599 std::u16string_view from, T & to) const
2601 assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(to));
2602 rtl_uString * s = nullptr;
2603 rtl_uString_newReplaceAllUtf16LAsciiL(
2604 &s, pData, from.data(), from.size(),
2605 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(to),
2606 libreoffice_internal::ConstCharArrayDetector<T>::length);
2607 return OUString(s, SAL_NO_ACQUIRE);
2609 #else
2610 template< typename T >
2611 SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T, OUString >::Type replaceAll( OUString const & from, T& to) const
2613 assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(to));
2614 rtl_uString * s = NULL;
2615 rtl_uString_newReplaceAllToAsciiL(
2616 &s, pData, from.pData,
2617 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(to),
2618 libreoffice_internal::ConstCharArrayDetector<T>::length);
2619 return OUString(s, SAL_NO_ACQUIRE);
2621 #endif
2624 Returns a new string resulting from replacing all occurrences of a given
2625 substring with another substring.
2627 Replacing subsequent occurrences picks up only after a given replacement.
2628 That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
2630 @param from ASCII string literal, the substring to be replaced
2632 @param to ASCII string literal, the substring to be replaced
2634 @since LibreOffice 3.6
2636 template< typename T1, typename T2 >
2637 SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T1, typename libreoffice_internal::ConstCharArrayDetector< T2, OUString >::Type >::Type
2638 replaceAll( T1& from, T2& to ) const
2640 assert(libreoffice_internal::ConstCharArrayDetector<T1>::isValid(from));
2641 assert(libreoffice_internal::ConstCharArrayDetector<T2>::isValid(to));
2642 rtl_uString * s = NULL;
2643 rtl_uString_newReplaceAllAsciiLAsciiL(
2644 &s, pData,
2645 libreoffice_internal::ConstCharArrayDetector<T1>::toPointer(from),
2646 libreoffice_internal::ConstCharArrayDetector<T1>::length,
2647 libreoffice_internal::ConstCharArrayDetector<T2>::toPointer(to),
2648 libreoffice_internal::ConstCharArrayDetector<T2>::length);
2649 return OUString(s, SAL_NO_ACQUIRE);
2653 Converts from this string all ASCII uppercase characters (65-90)
2654 to ASCII lowercase characters (97-122).
2656 This function can't be used for language specific conversion.
2657 If the string doesn't contain characters which must be converted,
2658 then the new string is assigned with str.
2660 @return the string, converted to ASCII lowercase.
2662 SAL_WARN_UNUSED_RESULT OUString toAsciiLowerCase() const
2664 rtl_uString* pNew = NULL;
2665 rtl_uString_newToAsciiLowerCase( &pNew, pData );
2666 return OUString( pNew, SAL_NO_ACQUIRE );
2670 Converts from this string all ASCII lowercase characters (97-122)
2671 to ASCII uppercase characters (65-90).
2673 This function can't be used for language specific conversion.
2674 If the string doesn't contain characters which must be converted,
2675 then the new string is assigned with str.
2677 @return the string, converted to ASCII uppercase.
2679 SAL_WARN_UNUSED_RESULT OUString toAsciiUpperCase() const
2681 rtl_uString* pNew = NULL;
2682 rtl_uString_newToAsciiUpperCase( &pNew, pData );
2683 return OUString( pNew, SAL_NO_ACQUIRE );
2687 Returns a new string resulting from removing white space from both ends
2688 of the string.
2690 All characters that have codes less than or equal to
2691 32 (the space character), and Unicode General Punctuation area Space
2692 and some Control characters are considered to be white space (see
2693 implIsWhitespace).
2694 If the string doesn't contain white spaces at both ends,
2695 then the new string is assigned with str.
2697 @return the string, with white space removed from the front and end.
2699 SAL_WARN_UNUSED_RESULT OUString trim() const
2701 rtl_uString* pNew = NULL;
2702 rtl_uString_newTrim( &pNew, pData );
2703 return OUString( pNew, SAL_NO_ACQUIRE );
2707 Returns a token in the string.
2709 Example:
2710 sal_Int32 nIndex = 0;
2714 OUString aToken = aStr.getToken( 0, ';', nIndex );
2717 while ( nIndex >= 0 );
2719 @param token the number of the token to return
2720 @param cTok the character which separate the tokens.
2721 @param index the position at which the token is searched in the
2722 string.
2723 The index must not be greater than the length of the
2724 string.
2725 This param is set to the position of the
2726 next token or to -1, if it is the last token.
2727 @return the token; if either token or index is negative, an empty token
2728 is returned (and index is set to -1)
2730 OUString getToken( sal_Int32 token, sal_Unicode cTok, sal_Int32& index ) const
2732 rtl_uString * pNew = NULL;
2733 index = rtl_uString_getToken( &pNew, pData, token, cTok, index );
2734 return OUString( pNew, SAL_NO_ACQUIRE );
2738 Returns a token from the string.
2740 The same as getToken(sal_Int32, sal_Unicode, sal_Int32 &), but always
2741 passing in 0 as the start index in the third argument.
2743 @param count the number of the token to return, starting with 0
2744 @param separator the character which separates the tokens
2746 @return the given token, or an empty string
2748 @since LibreOffice 3.6
2750 OUString getToken(sal_Int32 count, sal_Unicode separator) const {
2751 sal_Int32 n = 0;
2752 return getToken(count, separator, n);
2756 Returns the Boolean value from this string.
2758 This function can't be used for language specific conversion.
2760 @return true, if the string is 1 or "True" in any ASCII case.
2761 false in any other case.
2763 bool toBoolean() const
2765 return rtl_ustr_toBoolean( pData->buffer );
2769 Returns the first character from this string.
2771 @return the first character from this string or 0, if this string
2772 is empty.
2774 sal_Unicode toChar() const
2776 return pData->buffer[0];
2780 Returns the int32 value from this string.
2782 This function can't be used for language specific conversion.
2784 @param radix the radix (between 2 and 36)
2785 @return the int32 represented from this string.
2786 0 if this string represents no number or one of too large
2787 magnitude.
2789 sal_Int32 toInt32( sal_Int16 radix = 10 ) const
2791 return rtl_ustr_toInt32( pData->buffer, radix );
2795 Returns the uint32 value from this string.
2797 This function can't be used for language specific conversion.
2799 @param radix the radix (between 2 and 36)
2800 @return the uint32 represented from this string.
2801 0 if this string represents no number or one of too large
2802 magnitude.
2804 @since LibreOffice 4.2
2806 sal_uInt32 toUInt32( sal_Int16 radix = 10 ) const
2808 return rtl_ustr_toUInt32( pData->buffer, radix );
2812 Returns the int64 value from this string.
2814 This function can't be used for language specific conversion.
2816 @param radix the radix (between 2 and 36)
2817 @return the int64 represented from this string.
2818 0 if this string represents no number or one of too large
2819 magnitude.
2821 sal_Int64 toInt64( sal_Int16 radix = 10 ) const
2823 return rtl_ustr_toInt64( pData->buffer, radix );
2827 Returns the uint64 value from this string.
2829 This function can't be used for language specific conversion.
2831 @param radix the radix (between 2 and 36)
2832 @return the uint64 represented from this string.
2833 0 if this string represents no number or one of too large
2834 magnitude.
2836 @since LibreOffice 4.1
2838 sal_uInt64 toUInt64( sal_Int16 radix = 10 ) const
2840 return rtl_ustr_toUInt64( pData->buffer, radix );
2844 Returns the float value from this string.
2846 This function can't be used for language specific conversion.
2848 @return the float represented from this string.
2849 0.0 if this string represents no number.
2851 float toFloat() const
2853 return rtl_ustr_toFloat( pData->buffer );
2857 Returns the double value from this string.
2859 This function can't be used for language specific conversion.
2861 @return the double represented from this string.
2862 0.0 if this string represents no number.
2864 double toDouble() const
2866 return rtl_ustr_toDouble( pData->buffer );
2871 Return a canonical representation for a string.
2873 A pool of strings, initially empty is maintained privately
2874 by the string class. On invocation, if present in the pool
2875 the original string will be returned. Otherwise this string,
2876 or a copy thereof will be added to the pool and returned.
2878 @return
2879 a version of the string from the pool.
2881 @exception std::bad_alloc is thrown if an out-of-memory condition occurs
2883 @since UDK 3.2.7
2885 OUString intern() const
2887 rtl_uString * pNew = NULL;
2888 rtl_uString_intern( &pNew, pData );
2889 if (pNew == NULL) {
2890 throw std::bad_alloc();
2892 return OUString( pNew, SAL_NO_ACQUIRE );
2896 Return a canonical representation for a converted string.
2898 A pool of strings, initially empty is maintained privately
2899 by the string class. On invocation, if present in the pool
2900 the original string will be returned. Otherwise this string,
2901 or a copy thereof will be added to the pool and returned.
2903 @param value a 8-Bit character array.
2904 @param length the number of character which should be converted.
2905 The 8-Bit character array length must be
2906 greater than or equal to this value.
2907 @param encoding the text encoding from which the 8-Bit character
2908 sequence should be converted.
2909 @param convertFlags flags which controls the conversion.
2910 see RTL_TEXTTOUNICODE_FLAGS_...
2911 @param pInfo pointer to return conversion status or NULL.
2913 @return
2914 a version of the converted string from the pool.
2916 @exception std::bad_alloc is thrown if an out-of-memory condition occurs
2918 @since UDK 3.2.7
2920 static OUString intern( const char * value, sal_Int32 length,
2921 rtl_TextEncoding encoding,
2922 sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS,
2923 sal_uInt32 *pInfo = NULL )
2925 rtl_uString * pNew = NULL;
2926 rtl_uString_internConvert( &pNew, value, length, encoding,
2927 convertFlags, pInfo );
2928 if (pNew == NULL) {
2929 throw std::bad_alloc();
2931 return OUString( pNew, SAL_NO_ACQUIRE );
2935 Converts to an OString, signalling failure.
2937 @param pTarget
2938 An out parameter receiving the converted OString. Must not be null; the
2939 contents are not modified if conversion fails (convertToOString returns
2940 false).
2942 @param nEncoding
2943 The text encoding to convert into. Must be an octet encoding (i.e.,
2944 rtl_isOctetTextEncoding(nEncoding) must return true).
2946 @param nFlags
2947 A combination of RTL_UNICODETOTEXT_FLAGS that detail how to do the
2948 conversion (see rtl_convertUnicodeToText). RTL_UNICODETOTEXT_FLAGS_FLUSH
2949 need not be included, it is implicitly assumed. Typical uses are either
2950 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
2951 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR (fail if a Unicode character cannot
2952 be converted to the target nEncoding) or OUSTRING_TO_OSTRING_CVTFLAGS
2953 (make a best efforts conversion).
2955 @return
2956 True if the conversion succeeded, false otherwise.
2958 bool convertToString(OString * pTarget, rtl_TextEncoding nEncoding,
2959 sal_uInt32 nFlags) const
2961 return rtl_convertUStringToString(&pTarget->pData, pData->buffer,
2962 pData->length, nEncoding, nFlags);
2965 /** Iterate through this string based on code points instead of UTF-16 code
2966 units.
2968 See Chapter 3 of The Unicode Standard 5.0 (Addison--Wesley, 2006) for
2969 definitions of the various terms used in this description.
2971 This string is interpreted as a sequence of zero or more UTF-16 code
2972 units. For each index into this sequence (from zero to one less than
2973 the length of the sequence, inclusive), a code point represented
2974 starting at the given index is computed as follows:
2976 - If the UTF-16 code unit addressed by the index constitutes a
2977 well-formed UTF-16 code unit sequence, the computed code point is the
2978 scalar value encoded by that UTF-16 code unit sequence.
2980 - Otherwise, if the index is at least two UTF-16 code units away from
2981 the end of the sequence, and the sequence of two UTF-16 code units
2982 addressed by the index constitutes a well-formed UTF-16 code unit
2983 sequence, the computed code point is the scalar value encoded by that
2984 UTF-16 code unit sequence.
2986 - Otherwise, the computed code point is the UTF-16 code unit addressed
2987 by the index. (This last case catches unmatched surrogates as well as
2988 indices pointing into the middle of surrogate pairs.)
2990 @param indexUtf16
2991 pointer to a UTF-16 based index into this string; must not be null. On
2992 entry, the index must be in the range from zero to the length of this
2993 string (in UTF-16 code units), inclusive. Upon successful return, the
2994 index will be updated to address the UTF-16 code unit that is the given
2995 incrementCodePoints away from the initial index.
2997 @param incrementCodePoints
2998 the number of code points to move the given *indexUtf16. If
2999 non-negative, moving is done after determining the code point at the
3000 index. If negative, moving is done before determining the code point
3001 at the (then updated) index. The value must be such that the resulting
3002 UTF-16 based index is in the range from zero to the length of this
3003 string (in UTF-16 code units), inclusive.
3005 @return
3006 the code point (an integer in the range from 0 to 0x10FFFF, inclusive)
3007 that is represented within this string starting at the index computed as
3008 follows: If incrementCodePoints is non-negative, the index is the
3009 initial value of *indexUtf16; if incrementCodePoints is negative, the
3010 index is the updated value of *indexUtf16. In either case, the computed
3011 index must be in the range from zero to one less than the length of this
3012 string (in UTF-16 code units), inclusive.
3014 @since UDK 3.2.7
3016 sal_uInt32 iterateCodePoints(
3017 sal_Int32 * indexUtf16, sal_Int32 incrementCodePoints = 1) const
3019 return rtl_uString_iterateCodePoints(
3020 pData, indexUtf16, incrementCodePoints);
3024 * Convert an OString to an OUString, assuming that the OString is
3025 * UTF-8-encoded.
3027 * @param rSource
3028 * an OString to convert
3030 * @since LibreOffice 4.4
3032 #if defined LIBO_INTERNAL_ONLY
3033 static OUString fromUtf8(std::string_view rSource)
3035 OUString aTarget;
3036 bool bSuccess = rtl_convertStringToUString(&aTarget.pData,
3037 rSource.data(),
3038 rSource.length(),
3039 RTL_TEXTENCODING_UTF8,
3040 RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR|RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR|RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR);
3041 (void) bSuccess;
3042 assert(bSuccess);
3043 return aTarget;
3045 #else
3046 static OUString fromUtf8(const OString& rSource)
3048 OUString aTarget;
3049 bool bSuccess = rtl_convertStringToUString(&aTarget.pData,
3050 rSource.getStr(),
3051 rSource.getLength(),
3052 RTL_TEXTENCODING_UTF8,
3053 RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR|RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR|RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR);
3054 (void) bSuccess;
3055 assert(bSuccess);
3056 return aTarget;
3058 #endif
3061 * Convert this string to an OString, assuming that the string can be
3062 * UTF-8-encoded successfully.
3064 * In other words, you must not use this method on a random sequence of
3065 * UTF-16 code units, but only at places where it is assumed that the
3066 * content is a proper string.
3068 * @since LibreOffice 4.4
3070 OString toUtf8() const
3072 OString aTarget;
3073 bool bSuccess = rtl_convertUStringToString(&aTarget.pData,
3074 getStr(),
3075 getLength(),
3076 RTL_TEXTENCODING_UTF8,
3077 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR|RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR);
3078 (void) bSuccess;
3079 assert(bSuccess);
3080 return aTarget;
3083 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
3085 static auto number( int i, sal_Int16 radix = 10 )
3087 return OUStringNumber<RTL_USTR_MAX_VALUEOFINT32>(rtl_ustr_valueOfInt32, i, radix);
3089 static auto number( long long ll, sal_Int16 radix = 10 )
3091 return OUStringNumber<RTL_USTR_MAX_VALUEOFINT64>(rtl_ustr_valueOfInt64, ll, radix);
3093 static auto number( unsigned long long ll, sal_Int16 radix = 10 )
3095 return OUStringNumber<RTL_USTR_MAX_VALUEOFUINT64>(rtl_ustr_valueOfUInt64, ll, radix);
3097 static auto number( unsigned int i, sal_Int16 radix = 10 )
3099 return number( static_cast< unsigned long long >( i ), radix );
3101 static auto number( long i, sal_Int16 radix = 10)
3103 return number( static_cast< long long >( i ), radix );
3105 static auto number( unsigned long i, sal_Int16 radix = 10 )
3107 return number( static_cast< unsigned long long >( i ), radix );
3109 #else
3111 Returns the string representation of the integer argument.
3113 This function can't be used for language specific conversion.
3115 @param i an integer value
3116 @param radix the radix (between 2 and 36)
3117 @return a string with the string representation of the argument.
3118 @since LibreOffice 4.1
3120 static OUString number( int i, sal_Int16 radix = 10 )
3122 sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFINT32];
3123 return OUString(aBuf, rtl_ustr_valueOfInt32(aBuf, i, radix));
3125 /// @overload
3126 /// @since LibreOffice 4.1
3127 static OUString number( unsigned int i, sal_Int16 radix = 10 )
3129 return number( static_cast< unsigned long long >( i ), radix );
3131 /// @overload
3132 /// @since LibreOffice 4.1
3133 static OUString number( long i, sal_Int16 radix = 10)
3135 return number( static_cast< long long >( i ), radix );
3137 /// @overload
3138 /// @since LibreOffice 4.1
3139 static OUString number( unsigned long i, sal_Int16 radix = 10 )
3141 return number( static_cast< unsigned long long >( i ), radix );
3143 /// @overload
3144 /// @since LibreOffice 4.1
3145 static OUString number( long long ll, sal_Int16 radix = 10 )
3147 sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFINT64];
3148 return OUString(aBuf, rtl_ustr_valueOfInt64(aBuf, ll, radix));
3150 /// @overload
3151 /// @since LibreOffice 4.1
3152 static OUString number( unsigned long long ll, sal_Int16 radix = 10 )
3154 sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFUINT64];
3155 return OUString(aBuf, rtl_ustr_valueOfUInt64(aBuf, ll, radix));
3157 #endif
3160 Returns the string representation of the float argument.
3162 This function can't be used for language specific conversion.
3164 @param f a float.
3165 @return a string with the decimal representation of the argument.
3166 @since LibreOffice 4.1
3168 static OUString number( float f )
3170 rtl_uString* pNew = NULL;
3171 // Same as rtl::str::valueOfFP, used for rtl_ustr_valueOfFloat
3172 rtl_math_doubleToUString(&pNew, NULL, 0, f, rtl_math_StringFormat_G,
3173 RTL_USTR_MAX_VALUEOFFLOAT - SAL_N_ELEMENTS("-x.E-xxx") + 1, '.',
3174 NULL, 0, true);
3175 if (pNew == NULL)
3176 throw std::bad_alloc();
3178 return OUString(pNew, SAL_NO_ACQUIRE);
3182 Returns the string representation of the double argument.
3184 This function can't be used for language specific conversion.
3186 @param d a double.
3187 @return a string with the decimal representation of the argument.
3188 @since LibreOffice 4.1
3190 static OUString number( double d )
3192 rtl_uString* pNew = NULL;
3193 // Same as rtl::str::valueOfFP, used for rtl_ustr_valueOfDouble
3194 rtl_math_doubleToUString(&pNew, NULL, 0, d, rtl_math_StringFormat_G,
3195 RTL_USTR_MAX_VALUEOFDOUBLE - SAL_N_ELEMENTS("-x.E-xxx") + 1, '.',
3196 NULL, 0, true);
3197 if (pNew == NULL)
3198 throw std::bad_alloc();
3200 return OUString(pNew, SAL_NO_ACQUIRE);
3203 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
3204 static auto boolean(bool b)
3206 return OUStringNumber<RTL_USTR_MAX_VALUEOFBOOLEAN>(rtl_ustr_valueOfBoolean, b);
3208 #else
3210 Returns the string representation of the sal_Bool argument.
3212 If the sal_Bool is true, the string "true" is returned.
3213 If the sal_Bool is false, the string "false" is returned.
3214 This function can't be used for language specific conversion.
3216 @param b a sal_Bool.
3217 @return a string with the string representation of the argument.
3218 @deprecated use boolean()
3220 SAL_DEPRECATED("use boolean()") static OUString valueOf( sal_Bool b )
3222 return boolean(b);
3226 Returns the string representation of the boolean argument.
3228 If the argument is true, the string "true" is returned.
3229 If the argument is false, the string "false" is returned.
3230 This function can't be used for language specific conversion.
3232 @param b a bool.
3233 @return a string with the string representation of the argument.
3234 @since LibreOffice 4.1
3236 static OUString boolean( bool b )
3238 sal_Unicode aBuf[RTL_USTR_MAX_VALUEOFBOOLEAN];
3239 return OUString(aBuf, rtl_ustr_valueOfBoolean(aBuf, b));
3241 #endif
3244 Returns the string representation of the char argument.
3246 @param c a character.
3247 @return a string with the string representation of the argument.
3248 @deprecated use operator, function or constructor taking char or sal_Unicode argument
3250 SAL_DEPRECATED("convert to OUString or use directly") static OUString valueOf( sal_Unicode c )
3252 return OUString( &c, 1 );
3256 Returns the string representation of the int argument.
3258 This function can't be used for language specific conversion.
3260 @param i a int32.
3261 @param radix the radix (between 2 and 36)
3262 @return a string with the string representation of the argument.
3263 @deprecated use number()
3265 SAL_DEPRECATED("use number()") static OUString valueOf( sal_Int32 i, sal_Int16 radix = 10 )
3267 return number( i, radix );
3271 Returns the string representation of the long argument.
3273 This function can't be used for language specific conversion.
3275 @param ll a int64.
3276 @param radix the radix (between 2 and 36)
3277 @return a string with the string representation of the argument.
3278 @deprecated use number()
3280 SAL_DEPRECATED("use number()") static OUString valueOf( sal_Int64 ll, sal_Int16 radix = 10 )
3282 return number( ll, radix );
3286 Returns the string representation of the float argument.
3288 This function can't be used for language specific conversion.
3290 @param f a float.
3291 @return a string with the string representation of the argument.
3292 @deprecated use number()
3294 SAL_DEPRECATED("use number()") static OUString valueOf( float f )
3296 return number(f);
3300 Returns the string representation of the double argument.
3302 This function can't be used for language specific conversion.
3304 @param d a double.
3305 @return a string with the string representation of the argument.
3306 @deprecated use number()
3308 SAL_DEPRECATED("use number()") static OUString valueOf( double d )
3310 return number(d);
3314 Returns an OUString copied without conversion from an ASCII
3315 character string.
3317 Since this method is optimized for performance, the ASCII character
3318 values are not converted in any way. The caller has to make sure that
3319 all ASCII characters are in the allowed range between 0 and 127.
3320 The ASCII string must be NULL-terminated.
3322 Note that for string literals it is simpler and more efficient
3323 to directly use the OUString constructor.
3325 @param value the 8-Bit ASCII character string
3326 @return a string with the string representation of the argument.
3328 static OUString createFromAscii( const char * value )
3330 rtl_uString* pNew = NULL;
3331 rtl_uString_newFromAscii( &pNew, value );
3332 return OUString( pNew, SAL_NO_ACQUIRE );
3335 #if defined LIBO_INTERNAL_ONLY
3336 static OUString createFromAscii(std::string_view value) {
3337 rtl_uString * p = nullptr;
3338 rtl_uString_newFromLiteral(&p, value.data(), value.size(), 0); //TODO: check for overflow
3339 return OUString(p, SAL_NO_ACQUIRE);
3341 #endif
3343 #if defined LIBO_INTERNAL_ONLY
3344 operator std::u16string_view() const { return {getStr(), sal_uInt32(getLength())}; }
3345 #endif
3347 #if defined LIBO_INTERNAL_ONLY
3348 // A wrapper for the first expression in an
3350 // OUString::Concat(e1) + e2 + ...
3352 // concatenation chain, when neither of the first two e1, e2 is one of our rtl string-related
3353 // classes (so something like
3355 // OUString s = "a" + (b ? std::u16string_view(u"c") : std::u16string_view(u"dd"));
3357 // would not compile):
3358 template<typename T> [[nodiscard]] static
3359 OUStringConcat<OUStringConcatMarker, T>
3360 Concat(T const & value) { return OUStringConcat<OUStringConcatMarker, T>(value); }
3362 // This overload is needed so that an argument of type 'char const[N]' ends up as
3363 // 'OUStringConcat<rtl::OUStringConcatMarker, char const[N]>' rather than as
3364 // 'OUStringConcat<rtl::OUStringConcatMarker, char[N]>':
3365 template<typename T, std::size_t N> [[nodiscard]] static
3366 OUStringConcat<OUStringConcatMarker, T[N]>
3367 Concat(T (& value)[N]) { return OUStringConcat<OUStringConcatMarker, T[N]>(value); }
3368 #endif
3370 private:
3371 OUString & internalAppend( rtl_uString* pOtherData )
3373 rtl_uString* pNewData = NULL;
3374 rtl_uString_newConcat( &pNewData, pData, pOtherData );
3375 if (pNewData == NULL) {
3376 throw std::bad_alloc();
3378 rtl_uString_assign(&pData, pNewData);
3379 rtl_uString_release(pNewData);
3380 return *this;
3385 #if defined LIBO_INTERNAL_ONLY
3386 // Can only define this after we define OUString
3387 inline OUStringConstExpr::operator const OUString &() const { return OUString::unacquired(&pData); }
3388 #endif
3390 #if defined LIBO_INTERNAL_ONLY
3391 // Prevent the operator ==/!= overloads with 'sal_Unicode const *' parameter from
3392 // being selected for nonsensical code like
3394 // if (ouIdAttr == nullptr)
3396 void operator ==(OUString const &, std::nullptr_t) = delete;
3397 void operator ==(std::nullptr_t, OUString const &) = delete;
3398 void operator !=(OUString const &, std::nullptr_t) = delete;
3399 void operator !=(std::nullptr_t, OUString const &) = delete;
3400 #endif
3402 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
3403 inline bool operator ==(OUString const & lhs, StringConcatenation<char16_t> const & rhs)
3404 { return lhs == std::u16string_view(rhs); }
3405 inline bool operator !=(OUString const & lhs, StringConcatenation<char16_t> const & rhs)
3406 { return lhs != std::u16string_view(rhs); }
3407 inline bool operator ==(StringConcatenation<char16_t> const & lhs, OUString const & rhs)
3408 { return std::u16string_view(lhs) == rhs; }
3409 inline bool operator !=(StringConcatenation<char16_t> const & lhs, OUString const & rhs)
3410 { return std::u16string_view(lhs) != rhs; }
3411 #endif
3413 #if defined LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
3414 /// @cond INTERNAL
3417 @internal
3419 template<>
3420 struct ToStringHelper< OUString >
3422 static std::size_t length( const OUString& s ) { return s.getLength(); }
3423 sal_Unicode* operator() ( sal_Unicode* buffer, const OUString& s ) const { return addDataHelper( buffer, s.getStr(), s.getLength()); }
3427 @internal
3429 template<std::size_t N>
3430 struct ToStringHelper< OUStringLiteral<N> >
3432 static std::size_t length( const OUStringLiteral<N>& str ) { return str.getLength(); }
3433 sal_Unicode* operator()( sal_Unicode* buffer, const OUStringLiteral<N>& str ) const { return addDataHelper( buffer, str.getStr(), str.getLength() ); }
3437 @internal
3439 template< typename charT, typename traits, typename T1, typename T2 >
3440 inline std::basic_ostream<charT, traits> & operator <<(
3441 std::basic_ostream<charT, traits> & stream, OUStringConcat< T1, T2 >&& concat)
3443 return stream << OUString( std::move(concat) );
3446 /// @endcond
3447 #endif
3449 /** A helper to use OUStrings with hash maps.
3451 Instances of this class are unary function objects that can be used as
3452 hash function arguments to std::unordered_map and similar constructs.
3454 struct OUStringHash
3456 /** Compute a hash code for a string.
3458 @param rString
3459 a string.
3461 @return
3462 a hash code for the string. This hash code should not be stored
3463 persistently, as its computation may change in later revisions.
3465 size_t operator()(const OUString& rString) const
3466 { return static_cast<size_t>(rString.hashCode()); }
3469 /* ======================================================================= */
3471 /** Convert an OString to an OUString, using a specific text encoding.
3473 The lengths of the two strings may differ (e.g., for double-byte
3474 encodings, UTF-7, UTF-8).
3476 @param rStr
3477 an OString to convert.
3479 @param encoding
3480 the text encoding to use for conversion.
3482 @param convertFlags
3483 flags which control the conversion. Either use
3484 OSTRING_TO_OUSTRING_CVTFLAGS, or see
3485 <http://udk.openoffice.org/cpp/man/spec/textconversion.html> for more
3486 details.
3488 #if defined LIBO_INTERNAL_ONLY
3489 inline OUString OStringToOUString( std::string_view rStr,
3490 rtl_TextEncoding encoding,
3491 sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS )
3493 return OUString( rStr.data(), rStr.length(), encoding, convertFlags );
3495 #else
3496 inline OUString OStringToOUString( const OString & rStr,
3497 rtl_TextEncoding encoding,
3498 sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS )
3500 return OUString( rStr.getStr(), rStr.getLength(), encoding, convertFlags );
3502 #endif
3504 /** Convert an OUString to an OString, using a specific text encoding.
3506 The lengths of the two strings may differ (e.g., for double-byte
3507 encodings, UTF-7, UTF-8).
3509 @param rUnicode
3510 an OUString to convert.
3512 @param encoding
3513 the text encoding to use for conversion.
3515 @param convertFlags
3516 flags which control the conversion. Either use
3517 OUSTRING_TO_OSTRING_CVTFLAGS, or see
3518 <http://udk.openoffice.org/cpp/man/spec/textconversion.html> for more
3519 details.
3521 #if defined LIBO_INTERNAL_ONLY
3522 inline OString OUStringToOString( std::u16string_view rUnicode,
3523 rtl_TextEncoding encoding,
3524 sal_uInt32 convertFlags = OUSTRING_TO_OSTRING_CVTFLAGS )
3526 return OString( rUnicode.data(), rUnicode.length(), encoding, convertFlags );
3528 #else
3529 inline OString OUStringToOString( const OUString & rUnicode,
3530 rtl_TextEncoding encoding,
3531 sal_uInt32 convertFlags = OUSTRING_TO_OSTRING_CVTFLAGS )
3533 return OString( rUnicode.getStr(), rUnicode.getLength(), encoding, convertFlags );
3535 #endif
3537 /* ======================================================================= */
3540 Support for rtl::OUString in std::ostream (and thus in
3541 CPPUNIT_ASSERT or SAL_INFO macros, for example).
3543 The rtl::OUString is converted to UTF-8.
3545 @since LibreOffice 3.5.
3547 template< typename charT, typename traits >
3548 inline std::basic_ostream<charT, traits> & operator <<(
3549 std::basic_ostream<charT, traits> & stream, OUString const & rString)
3551 return stream <<
3552 OUStringToOString(rString, RTL_TEXTENCODING_UTF8);
3553 // best effort; potentially loses data due to conversion failures
3554 // (stray surrogate halves) and embedded null characters
3557 } // namespace
3559 #ifdef RTL_STRING_UNITTEST
3560 namespace rtl
3562 typedef rtlunittest::OUString OUString;
3564 #endif
3566 // In internal code, allow to use classes like OUString without having to
3567 // explicitly refer to the rtl namespace, which is kind of superfluous given
3568 // that OUString itself is namespaced by its OU prefix:
3569 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
3570 using ::rtl::OUString;
3571 using ::rtl::OUStringHash;
3572 using ::rtl::OStringToOUString;
3573 using ::rtl::OUStringToOString;
3574 using ::rtl::OUStringLiteral;
3575 using ::rtl::OUStringChar;
3576 using ::rtl::Concat2View;
3577 #endif
3579 /// @cond INTERNAL
3581 Make OUString hashable by default for use in STL containers.
3583 @since LibreOffice 6.0
3585 #if defined LIBO_INTERNAL_ONLY
3586 namespace std {
3588 template<>
3589 struct hash<::rtl::OUString>
3591 std::size_t operator()(::rtl::OUString const & s) const
3593 if constexpr (sizeof(std::size_t) == 8)
3595 // return a hash that uses the full 64-bit range instead of a 32-bit value
3596 size_t n = 0;
3597 for (sal_Int32 i = 0, len = s.getLength(); i < len; ++i)
3598 n = 31 * n + s[i];
3599 return n;
3601 else
3602 return std::size_t(s.hashCode());
3608 #endif
3609 /// @endcond
3611 #endif /* _RTL_USTRING_HXX */
3613 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */