android: Update app-specific/MIME type icons
[LibreOffice.git] / include / rtl / string.hxx
blob091f224b25d64db48a1c45235252cf3506e57392
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_STRING_HXX
25 #define INCLUDED_RTL_STRING_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>
36 #include <string.h>
38 #if defined LIBO_INTERNAL_ONLY
39 #include <string_view>
40 #include <type_traits>
41 #endif
43 #include "rtl/math.h"
44 #include "rtl/textenc.h"
45 #include "rtl/string.h"
46 #include "rtl/stringutils.hxx"
48 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
49 #include "config_global.h"
50 #include "rtl/stringconcat.hxx"
51 #endif
53 #ifdef RTL_STRING_UNITTEST
54 extern bool rtl_string_unittest_const_literal;
55 extern bool rtl_string_unittest_const_literal_function;
56 #endif
58 // The unittest uses slightly different code to help check that the proper
59 // calls are made. The class is put into a different namespace to make
60 // sure the compiler generates a different (if generating also non-inline)
61 // copy of the function and does not merge them together. The class
62 // is "brought" into the proper rtl namespace by a typedef below.
63 #ifdef RTL_STRING_UNITTEST
64 #define rtl rtlunittest
65 #endif
67 namespace rtl
70 /// @cond INTERNAL
71 #ifdef RTL_STRING_UNITTEST
72 #undef rtl
73 // helper macro to make functions appear more readable
74 #define RTL_STRING_CONST_FUNCTION rtl_string_unittest_const_literal_function = true;
75 #else
76 #define RTL_STRING_CONST_FUNCTION
77 #endif
78 /// @endcond
80 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
82 /**
83 A wrapper dressing a string literal as a static-refcount rtl_String.
85 This class is not part of public API and is meant to be used only in LibreOffice code.
86 @since LibreOffice 4.0
88 template<std::size_t N> class SAL_WARN_UNUSED OStringLiteral {
89 static_assert(N != 0);
90 static_assert(N - 1 <= std::numeric_limits<sal_Int32>::max(), "literal too long");
91 friend class OString;
92 friend class OStringConstExpr;
94 public:
95 #if HAVE_CPP_CONSTEVAL
96 consteval
97 #else
98 constexpr
99 #endif
100 OStringLiteral(char const (&literal)[N]) {
101 assertLayout();
102 assert(literal[N - 1] == '\0');
103 //TODO: Use C++20 constexpr std::copy_n (P0202R3):
104 for (std::size_t i = 0; i != N; ++i) {
105 more.buffer[i] = literal[i];
109 #if defined __cpp_char8_t
110 #if HAVE_CPP_CONSTEVAL
111 consteval
112 #else
113 constexpr
114 #endif
115 explicit OStringLiteral(char8_t const (&literal)[N]) {
116 assertLayout();
117 assert(literal[N - 1] == '\0');
118 //TODO: Use C++20 constexpr std::copy_n (P0202R3):
119 for (std::size_t i = 0; i != N; ++i) {
120 more.buffer[i] = literal[i];
123 #endif
125 constexpr sal_Int32 getLength() const { return more.length; }
127 constexpr char const * getStr() const SAL_RETURNS_NONNULL { return more.buffer; }
129 constexpr operator std::string_view() const { return {more.buffer, sal_uInt32(more.length)}; }
131 private:
132 static constexpr void assertLayout() {
133 // These static_asserts verifying the layout compatibility with rtl_String cannot be class
134 // member declarations, as offsetof requires a complete type, so defer them to here:
135 static_assert(std::is_standard_layout_v<OStringLiteral>);
136 static_assert(offsetof(OStringLiteral, str.refCount) == offsetof(OStringLiteral, more.refCount));
137 static_assert(offsetof(OStringLiteral, str.length) == offsetof(OStringLiteral, more.length));
138 static_assert(offsetof(OStringLiteral, str.buffer) == offsetof(OStringLiteral, more.buffer));
141 struct Data {
142 Data() = default;
144 oslInterlockedCount refCount = 0x40000000; // SAL_STRING_STATIC_FLAG (sal/rtl/strimp.hxx)
145 sal_Int32 length = N - 1;
146 char buffer[N] = {}; //TODO: drop initialization for C++20 (P1331R2)
149 union {
150 rtl_String str;
151 Data more = {};
156 This is intended to be used when declaring compile-time-constant structs or arrays
157 that can be initialised from named OStringLiteral e.g.
159 constexpr OStringLiteral AAA = u"aaa";
160 constexpr OStringLiteral BBB = u"bbb";
161 constexpr OStringConstExpr FOO[] { AAA, BBB };
163 class OString;
164 class OStringConstExpr
166 public:
167 template<std::size_t N> constexpr OStringConstExpr(OStringLiteral<N> const & literal):
168 pData(const_cast<rtl_String *>(&literal.str)) {}
170 // prevent mis-use
171 template<std::size_t N> constexpr OStringConstExpr(OStringLiteral<N> && literal)
172 = delete;
174 // no destructor necessary because we know we are pointing at a compile-time
175 // constant OStringLiteral, which bypasses ref-counting.
178 make it easier to pass to OStringBuffer and similar without casting/converting
180 constexpr std::string_view asView() const { return std::string_view(pData->buffer, pData->length); }
182 inline operator const OString&() const;
184 private:
185 rtl_String* pData;
188 #endif
190 /* ======================================================================= */
193 This String class provide base functionality for C++ like 8-Bit
194 character array handling. The advantage of this class is, that it
195 handle all the memory management for you - and it do it
196 more efficient. If you assign a string to another string, the
197 data of both strings are shared (without any copy operation or
198 memory allocation) as long as you do not change the string. This class
199 stores also the length of the string, so that many operations are
200 faster as the C-str-functions.
202 This class provides only readonly string handling. So you could create
203 a string and you could only query the content from this string.
204 It provides also functionality to change the string, but this results
205 in every case in a new string instance (in the most cases with an
206 memory allocation). You don't have functionality to change the
207 content of the string. If you want to change the string content, then
208 you should use the OStringBuffer class, which provides these
209 functionalities and avoid too much memory allocation.
211 The design of this class is similar to the string classes in Java
212 and so more people should have fewer understanding problems when they
213 use this class.
216 class SAL_WARN_UNUSED SAL_DLLPUBLIC_RTTI OString
218 public:
219 /// @cond INTERNAL
220 rtl_String * pData;
221 /// @endcond
224 New string containing no characters.
226 OString()
228 pData = NULL;
229 rtl_string_new( &pData );
233 New string from OString.
235 @param str an OString.
237 OString( const OString & str )
239 pData = str.pData;
240 rtl_string_acquire( pData );
243 #if defined LIBO_INTERNAL_ONLY
245 Move constructor.
247 @param str an OString.
248 @since LibreOffice 5.2
250 OString( OString && str ) noexcept
252 pData = str.pData;
253 str.pData = nullptr;
254 rtl_string_new( &str.pData );
256 #endif
259 New string from OString data.
261 @param str an OString data.
263 OString( rtl_String * str )
265 pData = str;
266 rtl_string_acquire( pData );
269 /** New string from OString data without acquiring it. Takeover of ownership.
271 The SAL_NO_ACQUIRE dummy parameter is only there to distinguish this
272 from other constructors.
274 @param str an OString data.
276 OString( rtl_String * str, __sal_NoAcquire )
278 pData = str;
282 New string from a single character.
284 @param value a character.
286 explicit OString( char value )
287 : pData (NULL)
289 rtl_string_newFromStr_WithLength( &pData, &value, 1 );
292 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST_CONCAT
293 // Catch inadvertent conversions to the above ctor (e.g., from sal_[u]Int8, aka [un]signed
294 // char):
295 OString(int) = delete;
296 #endif
299 New string from a character buffer array.
301 Note: The argument type is always either char* or const char*. The template is
302 used only for technical reasons, as is the second argument.
304 @param value a NULL-terminated character array.
306 template< typename T >
307 OString( const T& value, typename libreoffice_internal::CharPtrDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
309 pData = NULL;
310 rtl_string_newFromStr( &pData, value );
313 template< typename T >
314 OString( T& value, typename libreoffice_internal::NonConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
316 pData = NULL;
317 rtl_string_newFromStr( &pData, value );
320 #if __cplusplus > 202002L // C++23 P2266R3 "Simpler implicit move"
321 template< typename T >
322 OString( T&& value, typename libreoffice_internal::NonConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
324 pData = NULL;
325 rtl_string_newFromStr( &pData, value );
327 #endif
330 New string from a string literal.
332 If there are any embedded \0's in the string literal, the result is undefined.
333 Use the overload that explicitly accepts length.
335 @since LibreOffice 3.6
337 @param literal a string literal
339 template< typename T >
340 OString( T& literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
342 assert(
343 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
344 pData = NULL;
345 if (libreoffice_internal::ConstCharArrayDetector<T>::length == 0) {
346 rtl_string_new(&pData);
347 } else {
348 rtl_string_newFromLiteral(
349 &pData,
350 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
351 literal),
352 libreoffice_internal::ConstCharArrayDetector<T>::length, 0);
354 #ifdef RTL_STRING_UNITTEST
355 rtl_string_unittest_const_literal = true;
356 #endif
360 New string from a character buffer array.
362 @param value a character array.
363 @param length the number of character which should be copied.
364 The character array length must be greater or
365 equal than this value.
367 OString( const char * value, sal_Int32 length )
369 pData = NULL;
370 rtl_string_newFromStr_WithLength( &pData, value, length );
373 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
374 /// @cond INTERNAL
376 New string from an 8-Bit string literal.
378 @since LibreOffice 7.1
380 template<std::size_t N> constexpr OString(OStringLiteral<N> const & literal):
381 pData(const_cast<rtl_String *>(&literal.str)) {}
382 template<std::size_t N> OString(OStringLiteral<N> &&) = delete;
383 /// @endcond
384 #endif
386 #if defined LIBO_INTERNAL_ONLY
387 explicit OString(std::string_view sv) {
388 if (sv.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max())) {
389 throw std::bad_alloc();
391 pData = nullptr;
392 rtl_string_newFromStr_WithLength(&pData, sv.data(), sv.size());
394 #endif
397 New string from a Unicode character buffer array.
399 @param value a Unicode character array.
400 @param length the number of character which should be converted.
401 The Unicode character array length must be
402 greater or equal than this value.
403 @param encoding the text encoding in which the Unicode character
404 sequence should be converted.
405 @param convertFlags flags which controls the conversion.
406 see RTL_UNICODETOTEXT_FLAGS_...
408 @exception std::bad_alloc is thrown if an out-of-memory condition occurs
410 OString( const sal_Unicode * value, sal_Int32 length,
411 rtl_TextEncoding encoding,
412 sal_uInt32 convertFlags = OUSTRING_TO_OSTRING_CVTFLAGS )
414 pData = NULL;
415 rtl_uString2String( &pData, value, length, encoding, convertFlags );
416 if (pData == NULL) {
417 throw std::bad_alloc();
421 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
423 @overload
424 @internal
426 template< typename T1, typename T2 >
427 OString( OStringConcat< T1, T2 >&& c )
429 const sal_Int32 l = c.length();
430 pData = rtl_string_alloc( l );
431 if (l != 0)
433 char* end = c.addData( pData->buffer );
434 pData->length = l;
435 *end = '\0';
440 @overload
441 @internal
443 template< std::size_t N >
444 OString( OStringNumber< N >&& n )
445 : OString( n.buf, n.length )
447 #endif
449 #ifdef LIBO_INTERNAL_ONLY
450 OString(std::nullptr_t) = delete;
451 #endif
454 Release the string data.
456 #if defined LIBO_INTERNAL_ONLY && __cplusplus >= 202002L
457 constexpr
458 #endif
459 ~OString()
461 #if defined LIBO_INTERNAL_ONLY && __cplusplus >= 202002L
462 if (std::is_constant_evaluated()) {
463 //TODO: We would want to
465 // assert(SAL_STRING_IS_STATIC(pData));
467 // here, but that wouldn't work because read of member `str` of OUStringLiteral's
468 // anonymous union with active member `more` is not allowed in a constant expression.
469 } else
470 #endif
471 rtl_string_release( pData );
474 #if defined LIBO_INTERNAL_ONLY
475 /** Provides an OString const & passing a storage pointer of an
476 rtl_String * handle.
477 It is more convenient to use C++ OString member functions when dealing
478 with rtl_String * handles. Using this function avoids unnecessary
479 acquire()/release() calls for a temporary OString object.
481 @param ppHandle
482 pointer to storage
483 @return
484 OString const & based on given storage
486 static OString const & unacquired( rtl_String * const * ppHandle )
487 { return * reinterpret_cast< OString const * >( ppHandle ); }
488 #endif
491 Assign a new string.
493 @param str an OString.
495 OString & operator=( const OString & str )
497 rtl_string_assign( &pData, str.pData );
498 return *this;
501 #if defined LIBO_INTERNAL_ONLY
503 Move assign a new string.
505 @param str an OString.
506 @since LibreOffice 5.2
508 OString & operator=( OString && str ) noexcept
510 rtl_string_release( pData );
511 pData = str.pData;
512 str.pData = nullptr;
513 rtl_string_new( &str.pData );
514 return *this;
516 #endif
519 @overload
520 This function accepts an ASCII string literal as its argument.
521 @since LibreOffice 3.6
523 template< typename T >
524 typename libreoffice_internal::ConstCharArrayDetector< T, OString& >::Type operator=( T& literal )
526 RTL_STRING_CONST_FUNCTION
527 assert(
528 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
529 if (libreoffice_internal::ConstCharArrayDetector<T>::length == 0) {
530 rtl_string_new(&pData);
531 } else {
532 rtl_string_newFromLiteral(
533 &pData,
534 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
535 literal),
536 libreoffice_internal::ConstCharArrayDetector<T>::length, 0);
538 return *this;
542 Append a string to this string.
544 @param str an OString.
546 OString & operator+=( const OString & str )
547 #if defined LIBO_INTERNAL_ONLY
549 #endif
551 rtl_string_newConcat( &pData, pData, str.pData );
552 return *this;
554 #if defined LIBO_INTERNAL_ONLY
555 void operator+=(OString const &) && = delete;
556 #endif
558 #if defined LIBO_INTERNAL_ONLY
559 template<typename T> typename libreoffice_internal::CharPtrDetector<T, OString &>::Type
560 operator +=(T const & value) & { return operator +=(std::string_view(value)); }
561 template<typename T> typename libreoffice_internal::CharPtrDetector<T, OString &>::Type
562 operator +=(T const &) && = delete;
564 template<typename T>
565 typename libreoffice_internal::NonConstCharArrayDetector<T, OString &>::Type
566 operator +=(T & value) & { return operator +=(std::string_view(value)); }
567 template<typename T>
568 typename libreoffice_internal::NonConstCharArrayDetector<T, OString &>::Type operator +=(T &) &&
569 = delete;
571 template<typename T> typename libreoffice_internal::ConstCharArrayDetector<T, OString &>::Type
572 operator +=(T & literal) & {
573 assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
574 return operator +=(
575 std::string_view(
576 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
577 libreoffice_internal::ConstCharArrayDetector<T>::length));
579 template<typename T> typename libreoffice_internal::ConstCharArrayDetector<T, OString &>::Type
580 operator +=(T &) && = delete;
582 template<std::size_t N> OString & operator +=(OStringLiteral<N> const & literal) &
583 { return operator +=(std::string_view(literal.getStr(), literal.getLength())); }
584 template<std::size_t N> void operator +=(OStringLiteral<N> const &) && = delete;
586 OString & operator +=(std::string_view sv) & {
587 if (sv.empty()) {
588 return *this;
590 if (sv.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max() - pData->length)) {
591 throw std::bad_alloc();
593 auto const l = pData->length + sv.size();
594 rtl_string_ensureCapacity(&pData, l);
595 *addDataHelper(pData->buffer + pData->length, sv.data(), sv.size()) = '\0';
596 pData->length = l;
597 return *this;
599 void operator +=(std::string_view) && = delete;
600 #endif
602 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
604 @overload
605 @internal
607 template< typename T1, typename T2 >
608 OString& operator+=( OStringConcat< T1, T2 >&& c ) & {
609 sal_Int32 l = c.length();
610 if( l == 0 )
611 return *this;
612 l += pData->length;
613 rtl_string_ensureCapacity( &pData, l );
614 char* end = c.addData( pData->buffer + pData->length );
615 *end = '\0';
616 pData->length = l;
617 return *this;
619 template<typename T1, typename T2> void operator +=(
620 OStringConcat<T1, T2> &&) && = delete;
623 @overload
624 @internal
626 template< std::size_t N >
627 OString& operator+=( OStringNumber< N >&& n ) & {
628 return operator +=(std::string_view(n.buf, n.length));
630 template<std::size_t N> void operator +=(
631 OStringNumber<N> &&) && = delete;
632 #endif
635 Clears the string, i.e, makes a zero-character string
636 @since LibreOffice 4.4
638 void clear()
640 rtl_string_new( &pData );
644 Returns the length of this string.
646 The length is equal to the number of characters in this string.
648 @return the length of the sequence of characters represented by this
649 object.
651 sal_Int32 getLength() const { return pData->length; }
654 Checks if a string is empty.
656 @return true if the string is empty;
657 false, otherwise.
659 @since LibreOffice 3.4
661 bool isEmpty() const
663 return pData->length == 0;
667 Returns a pointer to the characters of this string.
669 <p>The returned pointer is guaranteed to point to a null-terminated byte
670 string. But note that this string object may contain embedded null
671 characters, which will thus also be embedded in the returned
672 null-terminated byte string.</p>
674 @return a pointer to a null-terminated byte string representing the
675 characters of this string object.
677 const char * getStr() const SAL_RETURNS_NONNULL { return pData->buffer; }
680 Access to individual characters.
682 @param index must be non-negative and less than length.
684 @return the character at the given index.
686 @since LibreOffice 3.5
688 char operator [](sal_Int32 index) const {
689 // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
690 assert(index >= 0 && static_cast<sal_uInt32>(index) < static_cast<sal_uInt32>(getLength()));
691 return getStr()[index];
695 Compares two strings.
697 The comparison is based on the numeric value of each character in
698 the strings and return a value indicating their relationship.
699 This function can't be used for language specific sorting.
701 @param str the object to be compared.
702 @return 0 - if both strings are equal
703 < 0 - if this string is less than the string argument
704 > 0 - if this string is greater than the string argument
706 sal_Int32 compareTo( const OString & str ) const
708 return rtl_str_compare_WithLength( pData->buffer, pData->length,
709 str.pData->buffer, str.pData->length );
713 Compares two strings with an maximum count of characters.
715 The comparison is based on the numeric value of each character in
716 the strings and return a value indicating their relationship.
717 This function can't be used for language specific sorting.
719 @param rObj the object to be compared.
720 @param maxLength the maximum count of characters to be compared.
721 @return 0 - if both strings are equal
722 < 0 - if this string is less than the string argument
723 > 0 - if this string is greater than the string argument
725 sal_Int32 compareTo( const OString & rObj, sal_Int32 maxLength ) const
727 return rtl_str_shortenedCompare_WithLength( pData->buffer, pData->length,
728 rObj.pData->buffer, rObj.pData->length, maxLength );
732 Compares two strings in reverse order.
734 The comparison is based on the numeric value of each character in
735 the strings and return a value indicating their relationship.
736 This function can't be used for language specific sorting.
738 @param str the object to be compared.
739 @return 0 - if both strings are equal
740 < 0 - if this string is less than the string argument
741 > 0 - if this string is greater than the string argument
743 sal_Int32 reverseCompareTo( const OString & str ) const
745 return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length,
746 str.pData->buffer, str.pData->length );
750 Perform a comparison of two strings.
752 The result is true if and only if second string
753 represents the same sequence of characters as the first string.
754 This function can't be used for language specific comparison.
756 @param str the object to be compared.
757 @return true if the strings are equal;
758 false, otherwise.
760 bool equals( const OString & str ) const
762 if ( pData->length != str.pData->length )
763 return false;
764 if ( pData == str.pData )
765 return true;
766 return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length,
767 str.pData->buffer, str.pData->length ) == 0;
771 Perform a comparison of two strings.
773 The result is true if and only if second string
774 represents the same sequence of characters as the first string.
775 The ASCII string must be greater or equal as length.
776 This function can't be used for language specific comparison.
779 @param value a character array.
780 @param length the length of the character array.
781 @return true if the strings are equal;
782 false, otherwise.
784 bool equalsL( const char* value, sal_Int32 length ) const
786 if ( pData->length != length )
787 return false;
789 return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length,
790 value, length ) == 0;
794 Perform an ASCII lowercase comparison of two strings.
796 The result is true if and only if second string
797 represents the same sequence of characters as the first string,
798 ignoring the case.
799 Character values between 65 and 90 (ASCII A-Z) are interpreted as
800 values between 97 and 122 (ASCII a-z).
801 This function can't be used for language specific comparison.
803 @param str the object to be compared.
804 @return true if the strings are equal;
805 false, otherwise.
807 #if defined LIBO_INTERNAL_ONLY
808 bool equalsIgnoreAsciiCase( std::string_view str ) const
810 if ( sal_uInt32(pData->length) != str.size() )
811 return false;
812 if ( pData->buffer == str.data() )
813 return true;
814 return rtl_str_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
815 str.data(), str.size() ) == 0;
817 #else
818 bool equalsIgnoreAsciiCase( const OString & str ) const
820 if ( pData->length != str.pData->length )
821 return false;
822 if ( pData == str.pData )
823 return true;
824 return rtl_str_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
825 str.pData->buffer, str.pData->length ) == 0;
827 #endif
830 Perform an ASCII lowercase comparison of two strings.
832 The result is true if and only if second string
833 represents the same sequence of characters as the first string,
834 ignoring the case.
835 Character values between 65 and 90 (ASCII A-Z) are interpreted as
836 values between 97 and 122 (ASCII a-z).
837 Since this method is optimized for performance, the ASCII character
838 values are not converted in any way. The caller has to make sure that
839 all ASCII characters are in the allowed range between 0 and
840 127. The ASCII string must be NULL-terminated.
841 This function can't be used for language specific comparison.
843 Note: The argument type is always either char* or const char*, the return type is bool.
844 The template is used only for technical reasons.
846 @param asciiStr the 8-Bit ASCII character string to be compared.
847 @return true if the strings are equal;
848 false, otherwise.
850 template< typename T >
851 typename libreoffice_internal::CharPtrDetector< T, bool >::Type equalsIgnoreAsciiCase( const T& asciiStr ) const
853 return rtl_str_compareIgnoreAsciiCase( pData->buffer, asciiStr ) == 0;
856 template< typename T >
857 typename libreoffice_internal::NonConstCharArrayDetector< T, bool >::Type equalsIgnoreAsciiCase( T& asciiStr ) const
859 return rtl_str_compareIgnoreAsciiCase( pData->buffer, asciiStr ) == 0;
863 @overload
864 This function accepts an ASCII string literal as its argument.
865 @since LibreOffice 3.6
867 template< typename T >
868 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type equalsIgnoreAsciiCase( T& literal ) const
870 RTL_STRING_CONST_FUNCTION
871 assert(
872 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
873 return
874 (pData->length
875 == libreoffice_internal::ConstCharArrayDetector<T>::length)
876 && (rtl_str_compareIgnoreAsciiCase_WithLength(
877 pData->buffer, pData->length,
878 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
879 literal),
880 libreoffice_internal::ConstCharArrayDetector<T>::length)
881 == 0);
885 Perform an ASCII lowercase comparison of two strings.
887 The result is true if and only if second string
888 represents the same sequence of characters as the first string,
889 ignoring the case.
890 Character values between 65 and 90 (ASCII A-Z) are interpreted as
891 values between 97 and 122 (ASCII a-z).
892 Since this method is optimized for performance, the ASCII character
893 values are not converted in any way. The caller has to make sure that
894 all ASCII characters are in the allowed range between 0 and
895 127. The ASCII string must be greater or equal in length as asciiStrLength.
896 This function can't be used for language specific comparison.
898 @param asciiStr the 8-Bit ASCII character string to be compared.
899 @param asciiStrLength the length of the ascii string
900 @return true if the strings are equal;
901 false, otherwise.
903 bool equalsIgnoreAsciiCaseL( const char * asciiStr, sal_Int32 asciiStrLength ) const
905 if ( pData->length != asciiStrLength )
906 return false;
908 return rtl_str_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
909 asciiStr, asciiStrLength ) == 0;
913 Match against a substring appearing in this string.
915 The result is true if and only if the second string appears as a substring
916 of this string, at the given position.
917 This function can't be used for language specific comparison.
919 @param str the object (substring) to be compared.
920 @param fromIndex the index to start the comparison from.
921 The index must be greater or equal than 0
922 and less or equal as the string length.
923 @return true if str match with the characters in the string
924 at the given position;
925 false, otherwise.
927 #if defined LIBO_INTERNAL_ONLY
928 bool match( std::string_view str, sal_Int32 fromIndex = 0 ) const
930 return rtl_str_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
931 str.data(), str.size(), str.size() ) == 0;
933 #else
934 bool match( const OString & str, sal_Int32 fromIndex = 0 ) const
936 return rtl_str_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
937 str.pData->buffer, str.pData->length, str.pData->length ) == 0;
939 #endif
942 @overload
943 This function accepts an ASCII string literal as its argument.
944 @since LibreOffice 3.6
946 template< typename T >
947 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type match( T& literal, sal_Int32 fromIndex = 0 ) const
949 RTL_STRING_CONST_FUNCTION
950 assert(
951 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
952 return
953 rtl_str_shortenedCompare_WithLength(
954 pData->buffer + fromIndex, pData->length - fromIndex,
955 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
956 literal),
957 libreoffice_internal::ConstCharArrayDetector<T>::length,
958 libreoffice_internal::ConstCharArrayDetector<T>::length)
959 == 0;
963 Match against a substring appearing in this string.
965 @param str the substring to be compared; must not be null and must point
966 to memory of at least strLength bytes
968 @param strLength the length of the substring; must be non-negative
970 @param fromIndex the index into this string to start the comparison at;
971 must be non-negative and not greater than this string's length
973 @return true if and only if the given str is contained as a substring of
974 this string at the given fromIndex
976 @since LibreOffice 3.6
978 bool matchL(
979 char const * str, sal_Int32 strLength, sal_Int32 fromIndex = 0)
980 const
982 return rtl_str_shortenedCompare_WithLength(
983 pData->buffer + fromIndex, pData->length - fromIndex,
984 str, strLength, strLength) == 0;
987 // This overload is left undefined, to detect calls of matchL that
988 // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
989 // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
990 // platforms):
991 #if SAL_TYPES_SIZEOFLONG == 8
992 void matchL(char const *, sal_Int32, rtl_TextEncoding) const;
993 #endif
996 Match against a substring appearing in this string, ignoring the case of
997 ASCII letters.
999 The result is true if and only if the second string appears as a substring
1000 of this string, at the given position.
1001 Character values between 65 and 90 (ASCII A-Z) are interpreted as
1002 values between 97 and 122 (ASCII a-z).
1003 This function can't be used for language specific comparison.
1005 @param str the object (substring) to be compared.
1006 @param fromIndex the index to start the comparison from.
1007 The index must be greater or equal than 0
1008 and less or equal as the string length.
1009 @return true if str match with the characters in the string
1010 at the given position;
1011 false, otherwise.
1013 #if defined LIBO_INTERNAL_ONLY
1014 bool matchIgnoreAsciiCase( std::string_view str, sal_Int32 fromIndex = 0 ) const
1016 return rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1017 str.data(), str.size(),
1018 str.size() ) == 0;
1020 #else
1021 bool matchIgnoreAsciiCase( const OString & str, sal_Int32 fromIndex = 0 ) const
1023 return rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1024 str.pData->buffer, str.pData->length,
1025 str.pData->length ) == 0;
1027 #endif
1029 @overload
1030 This function accepts an ASCII string literal as its argument.
1031 @since LibreOffice 3.6
1033 template< typename T >
1034 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type matchIgnoreAsciiCase( T& literal, sal_Int32 fromIndex = 0 ) const
1036 RTL_STRING_CONST_FUNCTION
1037 assert(
1038 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1039 return
1040 rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
1041 pData->buffer+fromIndex, pData->length-fromIndex,
1042 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1043 literal),
1044 libreoffice_internal::ConstCharArrayDetector<T>::length,
1045 libreoffice_internal::ConstCharArrayDetector<T>::length)
1046 == 0;
1050 Check whether this string starts with a given substring.
1052 @param str the substring to be compared
1054 @param rest if non-null, and this function returns true, then assign a
1055 copy of the remainder of this string to *rest. Available since
1056 LibreOffice 4.2
1058 @return true if and only if the given str appears as a substring at the
1059 start of this string
1061 @since LibreOffice 4.0
1063 #if defined LIBO_INTERNAL_ONLY
1064 bool startsWith(std::string_view str, OString * rest = NULL) const {
1065 bool b = match(str);
1066 if (b && rest != NULL) {
1067 *rest = copy(str.size());
1069 return b;
1071 #else
1072 bool startsWith(OString const & str, OString * rest = NULL) const {
1073 bool b = match(str);
1074 if (b && rest != NULL) {
1075 *rest = copy(str.getLength());
1077 return b;
1079 #endif
1082 @overload
1083 This function accepts an ASCII string literal as its argument.
1084 @since LibreOffice 4.0
1086 template< typename T >
1087 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type startsWith(
1088 T & literal, OString * rest = NULL) const
1090 RTL_STRING_CONST_FUNCTION
1091 bool b = match(literal, 0);
1092 if (b && rest != NULL) {
1093 *rest = copy(
1094 libreoffice_internal::ConstCharArrayDetector<T>::length);
1096 return b;
1100 Check whether this string starts with a given string, ignoring the case of
1101 ASCII letters.
1103 Character values between 65 and 90 (ASCII A-Z) are interpreted as
1104 values between 97 and 122 (ASCII a-z).
1105 This function can't be used for language specific comparison.
1107 @param str the substring to be compared
1109 @param rest if non-null, and this function returns true, then assign a
1110 copy of the remainder of this string to *rest.
1112 @return true if and only if the given str appears as a substring at the
1113 start of this string, ignoring the case of ASCII letters ("A"--"Z" and
1114 "a"--"z")
1116 @since LibreOffice 5.1
1118 #if defined LIBO_INTERNAL_ONLY
1119 bool startsWithIgnoreAsciiCase(std::string_view str, OString * rest = NULL)
1120 const
1122 bool b = matchIgnoreAsciiCase(str);
1123 if (b && rest != NULL) {
1124 *rest = copy(str.size());
1126 return b;
1128 #else
1129 bool startsWithIgnoreAsciiCase(OString const & str, OString * rest = NULL)
1130 const
1132 bool b = matchIgnoreAsciiCase(str);
1133 if (b && rest != NULL) {
1134 *rest = copy(str.getLength());
1136 return b;
1138 #endif
1141 @overload
1142 This function accepts an ASCII string literal as its argument.
1143 @since LibreOffice 5.1
1145 template< typename T >
1146 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type
1147 startsWithIgnoreAsciiCase(T & literal, OString * rest = NULL) const
1149 RTL_STRING_CONST_FUNCTION
1150 assert(
1151 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1152 bool b = matchIgnoreAsciiCase(literal);
1153 if (b && rest != NULL) {
1154 *rest = copy(
1155 libreoffice_internal::ConstCharArrayDetector<T>::length);
1157 return b;
1161 Check whether this string ends with a given substring.
1163 @param str the substring to be compared
1165 @param rest if non-null, and this function returns true, then assign a
1166 copy of the remainder of this string to *rest. Available since
1167 LibreOffice 4.2
1169 @return true if and only if the given str appears as a substring at the
1170 end of this string
1172 @since LibreOffice 3.6
1174 #if defined LIBO_INTERNAL_ONLY
1175 bool endsWith(std::string_view str, OString * rest = NULL) const {
1176 bool b = str.size() <= sal_uInt32(getLength())
1177 && match(str, getLength() - str.size());
1178 if (b && rest != NULL) {
1179 *rest = copy(0, getLength() - str.size());
1181 return b;
1183 #else
1184 bool endsWith(OString const & str, OString * rest = NULL) const {
1185 bool b = str.getLength() <= getLength()
1186 && match(str, getLength() - str.getLength());
1187 if (b && rest != NULL) {
1188 *rest = copy(0, getLength() - str.getLength());
1190 return b;
1192 #endif
1195 @overload
1196 This function accepts an ASCII string literal as its argument.
1197 @since LibreOffice 3.6
1199 template< typename T >
1200 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type endsWith(
1201 T & literal, OString * rest = NULL) const
1203 RTL_STRING_CONST_FUNCTION
1204 assert(
1205 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1206 bool b
1207 = (libreoffice_internal::ConstCharArrayDetector<T>::length
1208 <= sal_uInt32(getLength()))
1209 && match(
1210 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1211 literal),
1212 (getLength()
1213 - libreoffice_internal::ConstCharArrayDetector<T>::length));
1214 if (b && rest != NULL) {
1215 *rest = copy(
1217 (getLength()
1218 - libreoffice_internal::ConstCharArrayDetector<T>::length));
1220 return b;
1224 Check whether this string ends with a given substring.
1226 @param str the substring to be compared; must not be null and must point
1227 to memory of at least strLength bytes
1229 @param strLength the length of the substring; must be non-negative
1231 @return true if and only if the given str appears as a substring at the
1232 end of this string
1234 @since LibreOffice 3.6
1236 bool endsWithL(char const * str, sal_Int32 strLength) const {
1237 return strLength <= getLength()
1238 && matchL(str, strLength, getLength() - strLength);
1241 friend bool operator == ( const OString& rStr1, const OString& rStr2 )
1242 { return rStr1.equals(rStr2); }
1243 friend bool operator != ( const OString& rStr1, const OString& rStr2 )
1244 { return !(operator == ( rStr1, rStr2 )); }
1245 friend bool operator < ( const OString& rStr1, const OString& rStr2 )
1246 { return rStr1.compareTo( rStr2 ) < 0; }
1247 friend bool operator > ( const OString& rStr1, const OString& rStr2 )
1248 { return rStr1.compareTo( rStr2 ) > 0; }
1249 friend bool operator <= ( const OString& rStr1, const OString& rStr2 )
1250 { return rStr1.compareTo( rStr2 ) <= 0; }
1251 friend bool operator >= ( const OString& rStr1, const OString& rStr2 )
1252 { return rStr1.compareTo( rStr2 ) >= 0; }
1254 template< typename T >
1255 friend typename libreoffice_internal::CharPtrDetector< T, bool >::Type operator==( const OString& rStr1, const T& value )
1257 return
1258 rtl_str_compare_WithLength(
1259 rStr1.getStr(), rStr1.getLength(), value, rtl_str_getLength(value))
1260 == 0;
1263 template< typename T >
1264 friend typename libreoffice_internal::NonConstCharArrayDetector< T, bool >::Type operator==( const OString& rStr1, T& value )
1266 return
1267 rtl_str_compare_WithLength(
1268 rStr1.getStr(), rStr1.getLength(), value, rtl_str_getLength(value))
1269 == 0;
1272 template< typename T >
1273 friend typename libreoffice_internal::CharPtrDetector< T, bool >::Type operator==( const T& value, const OString& rStr2 )
1275 return
1276 rtl_str_compare_WithLength(
1277 value, rtl_str_getLength(value), rStr2.getStr(), rStr2.getLength())
1278 == 0;
1281 template< typename T >
1282 friend typename libreoffice_internal::NonConstCharArrayDetector< T, bool >::Type operator==( T& value, const OString& rStr2 )
1284 return
1285 rtl_str_compare_WithLength(
1286 value, rtl_str_getLength(value), rStr2.getStr(), rStr2.getLength())
1287 == 0;
1291 @overload
1292 This function accepts an ASCII string literal as its argument.
1293 @since LibreOffice 3.6
1295 template< typename T >
1296 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator==( const OString& rStr, T& literal )
1298 RTL_STRING_CONST_FUNCTION
1299 assert(
1300 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1301 return
1302 (rStr.getLength()
1303 == libreoffice_internal::ConstCharArrayDetector<T>::length)
1304 && (rtl_str_compare_WithLength(
1305 rStr.pData->buffer, rStr.pData->length,
1306 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1307 literal),
1308 libreoffice_internal::ConstCharArrayDetector<T>::length)
1309 == 0);
1313 @overload
1314 This function accepts an ASCII string literal as its argument.
1315 @since LibreOffice 3.6
1317 template< typename T >
1318 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator==( T& literal, const OString& rStr )
1320 RTL_STRING_CONST_FUNCTION
1321 assert(
1322 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1323 return
1324 (rStr.getLength()
1325 == libreoffice_internal::ConstCharArrayDetector<T>::length)
1326 && (rtl_str_compare_WithLength(
1327 rStr.pData->buffer, rStr.pData->length,
1328 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1329 literal),
1330 libreoffice_internal::ConstCharArrayDetector<T>::length)
1331 == 0);
1334 template< typename T >
1335 friend typename libreoffice_internal::CharPtrDetector< T, bool >::Type operator!=( const OString& rStr1, const T& value )
1337 return !(operator == ( rStr1, value ));
1340 template< typename T >
1341 friend typename libreoffice_internal::NonConstCharArrayDetector< T, bool >::Type operator!=( const OString& rStr1, T& value )
1343 return !(operator == ( rStr1, value ));
1346 template< typename T >
1347 friend typename libreoffice_internal::CharPtrDetector< T, bool >::Type operator!=( const T& value, const OString& rStr2 )
1349 return !(operator == ( value, rStr2 ));
1352 template< typename T >
1353 friend typename libreoffice_internal::NonConstCharArrayDetector< T, bool >::Type operator!=( T& value, const OString& rStr2 )
1355 return !(operator == ( value, rStr2 ));
1359 @overload
1360 This function accepts an ASCII string literal as its argument.
1361 @since LibreOffice 3.6
1363 template< typename T >
1364 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator!=( const OString& rStr, T& literal )
1366 return !( rStr == literal );
1370 @overload
1371 This function accepts an ASCII string literal as its argument.
1372 @since LibreOffice 3.6
1374 template< typename T >
1375 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator!=( T& literal, const OString& rStr )
1377 return !( literal == rStr );
1381 Returns a hashcode for this string.
1383 @return a hash code value for this object.
1385 @see rtl::OStringHash for convenient use of std::unordered_map
1387 sal_Int32 hashCode() const
1389 return rtl_str_hashCode_WithLength( pData->buffer, pData->length );
1393 Returns the index within this string of the first occurrence of the
1394 specified character, starting the search at the specified index.
1396 @param ch character to be located.
1397 @param fromIndex the index to start the search from.
1398 The index must be greater or equal than 0
1399 and less or equal as the string length.
1400 @return the index of the first occurrence of the character in the
1401 character sequence represented by this string that is
1402 greater than or equal to fromIndex, or
1403 -1 if the character does not occur.
1405 sal_Int32 indexOf( char ch, sal_Int32 fromIndex = 0 ) const
1407 sal_Int32 ret = rtl_str_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch );
1408 return (ret < 0 ? ret : ret+fromIndex);
1412 Returns the index within this string of the last occurrence of the
1413 specified character, searching backward starting at the end.
1415 @param ch character to be located.
1416 @return the index of the last occurrence of the character in the
1417 character sequence represented by this string, or
1418 -1 if the character does not occur.
1420 sal_Int32 lastIndexOf( char ch ) const
1422 return rtl_str_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch );
1426 Returns the index within this string of the last occurrence of the
1427 specified character, searching backward starting before the specified
1428 index.
1430 @param ch character to be located.
1431 @param fromIndex the index before which to start the search.
1432 @return the index of the last occurrence of the character in the
1433 character sequence represented by this string that
1434 is less than fromIndex, or -1
1435 if the character does not occur before that point.
1437 sal_Int32 lastIndexOf( char ch, sal_Int32 fromIndex ) const
1439 return rtl_str_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch );
1443 Returns the index within this string of the first occurrence of the
1444 specified substring, starting at the specified index.
1446 If str doesn't include any character, always -1 is
1447 returned. This is also the case, if both strings are empty.
1449 @param str the substring to search for.
1450 @param fromIndex the index to start the search from.
1451 @return If the string argument occurs one or more times as a substring
1452 within this string at the starting index, then the index
1453 of the first character of the first such substring is
1454 returned. If it does not occur as a substring starting
1455 at fromIndex or beyond, -1 is returned.
1457 #if defined LIBO_INTERNAL_ONLY
1458 sal_Int32 indexOf( std::string_view str, sal_Int32 fromIndex = 0 ) const
1460 sal_Int32 ret = rtl_str_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1461 str.data(), str.size() );
1462 return (ret < 0 ? ret : ret+fromIndex);
1464 #else
1465 sal_Int32 indexOf( const OString & str, sal_Int32 fromIndex = 0 ) const
1467 sal_Int32 ret = rtl_str_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1468 str.pData->buffer, str.pData->length );
1469 return (ret < 0 ? ret : ret+fromIndex);
1471 #endif
1473 @overload
1474 This function accepts an ASCII string literal as its argument.
1475 @since LibreOffice 3.6
1477 template< typename T >
1478 typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const
1480 RTL_STRING_CONST_FUNCTION
1481 assert(
1482 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1483 sal_Int32 n = rtl_str_indexOfStr_WithLength(
1484 pData->buffer + fromIndex, pData->length - fromIndex,
1485 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1486 libreoffice_internal::ConstCharArrayDetector<T>::length);
1487 return n < 0 ? n : n + fromIndex;
1491 Returns the index within this string of the first occurrence of the
1492 specified substring, starting at the specified index.
1494 If str doesn't include any character, always -1 is
1495 returned. This is also the case, if both strings are empty.
1497 @param str the substring to search for.
1498 @param len the length of the substring.
1499 @param fromIndex the index to start the search from.
1500 @return If the string argument occurs one or more times as a substring
1501 within this string at the starting index, then the index
1502 of the first character of the first such substring is
1503 returned. If it does not occur as a substring starting
1504 at fromIndex or beyond, -1 is returned.
1506 @since LibreOffice 3.6
1508 sal_Int32 indexOfL(char const * str, sal_Int32 len, sal_Int32 fromIndex = 0)
1509 const
1511 sal_Int32 n = rtl_str_indexOfStr_WithLength(
1512 pData->buffer + fromIndex, pData->length - fromIndex, str, len);
1513 return n < 0 ? n : n + fromIndex;
1516 // This overload is left undefined, to detect calls of indexOfL that
1517 // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
1518 // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
1519 // platforms):
1520 #if SAL_TYPES_SIZEOFLONG == 8
1521 void indexOfL(char const *, sal_Int32, rtl_TextEncoding) const;
1522 #endif
1525 Returns the index within this string of the last occurrence of
1526 the specified substring, searching backward starting at the end.
1528 The returned index indicates the starting index of the substring
1529 in this string.
1530 If str doesn't include any character, always -1 is
1531 returned. This is also the case, if both strings are empty.
1533 @param str the substring to search for.
1534 @return If the string argument occurs one or more times as a substring
1535 within this string, then the index of the first character of
1536 the last such substring is returned. If it does not occur as
1537 a substring, -1 is returned.
1539 #if defined LIBO_INTERNAL_ONLY
1540 sal_Int32 lastIndexOf( std::string_view str ) const
1542 return rtl_str_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1543 str.data(), str.size() );
1545 #else
1546 sal_Int32 lastIndexOf( const OString & str ) const
1548 return rtl_str_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1549 str.pData->buffer, str.pData->length );
1551 #endif
1554 Returns the index within this string of the last occurrence of
1555 the specified substring, searching backward starting before the specified
1556 index.
1558 The returned index indicates the starting index of the substring
1559 in this string.
1560 If str doesn't include any character, always -1 is
1561 returned. This is also the case, if both strings are empty.
1563 @param str the substring to search for.
1564 @param fromIndex the index before which to start the search.
1565 @return If the string argument occurs one or more times as a substring
1566 within this string before the starting index, then the index
1567 of the first character of the last such substring is
1568 returned. Otherwise, -1 is returned.
1570 #if defined LIBO_INTERNAL_ONLY
1571 sal_Int32 lastIndexOf( std::string_view str, sal_Int32 fromIndex ) const
1573 return rtl_str_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1574 str.data(), str.size() );
1576 #else
1577 sal_Int32 lastIndexOf( const OString & str, sal_Int32 fromIndex ) const
1579 return rtl_str_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1580 str.pData->buffer, str.pData->length );
1582 #endif
1585 Returns a new string that is a substring of this string.
1587 The substring begins at the specified beginIndex. If
1588 beginIndex is negative or be greater than the length of
1589 this string, behaviour is undefined.
1591 @param beginIndex the beginning index, inclusive.
1592 @return the specified substring.
1594 SAL_WARN_UNUSED_RESULT OString copy( sal_Int32 beginIndex ) const
1596 return copy(beginIndex, getLength() - beginIndex);
1600 Returns a new string that is a substring of this string.
1602 The substring begins at the specified beginIndex and contains count
1603 characters. If either beginIndex or count are negative,
1604 or beginIndex + count are greater than the length of this string
1605 then behaviour is undefined.
1607 @param beginIndex the beginning index, inclusive.
1608 @param count the number of characters.
1609 @return the specified substring.
1611 SAL_WARN_UNUSED_RESULT OString copy( sal_Int32 beginIndex, sal_Int32 count ) const
1613 rtl_String *pNew = NULL;
1614 rtl_string_newFromSubString( &pNew, pData, beginIndex, count );
1615 return OString( pNew, SAL_NO_ACQUIRE );
1618 #if defined LIBO_INTERNAL_ONLY
1620 Returns a std::string_view that is a view of a substring of this string.
1622 The substring begins at the specified beginIndex. If
1623 beginIndex is negative or be greater than the length of
1624 this string, behaviour is undefined.
1626 @param beginIndex the beginning index, inclusive.
1627 @return the specified substring.
1629 SAL_WARN_UNUSED_RESULT std::string_view subView( sal_Int32 beginIndex ) const
1631 assert(beginIndex >= 0);
1632 assert(beginIndex <= getLength());
1633 return subView(beginIndex, getLength() - beginIndex);
1637 Returns a std::string_view that is a view of a substring of this string.
1639 The substring begins at the specified beginIndex and contains count
1640 characters. If either beginIndex or count are negative,
1641 or beginIndex + count are greater than the length of this string
1642 then behaviour is undefined.
1644 @param beginIndex the beginning index, inclusive.
1645 @param count the number of characters.
1646 @return the specified substring.
1648 SAL_WARN_UNUSED_RESULT std::string_view subView( sal_Int32 beginIndex, sal_Int32 count ) const
1650 assert(beginIndex >= 0);
1651 assert(count >= 0);
1652 assert(beginIndex <= getLength());
1653 assert(count <= getLength() - beginIndex);
1654 return std::string_view(*this).substr(beginIndex, count);
1656 #endif
1658 #ifndef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
1660 Concatenates the specified string to the end of this string.
1662 @param str the string that is concatenated to the end
1663 of this string.
1664 @return a string that represents the concatenation of this string
1665 followed by the string argument.
1667 SAL_WARN_UNUSED_RESULT OString concat( const OString & str ) const
1669 rtl_String* pNew = NULL;
1670 rtl_string_newConcat( &pNew, pData, str.pData );
1671 return OString( pNew, SAL_NO_ACQUIRE );
1673 #endif
1675 #ifndef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
1676 friend OString operator+( const OString & str1, const OString & str2 )
1678 return str1.concat( str2 );
1680 #endif
1682 // hide this from internal code to avoid ambiguous lookup error
1683 #ifndef LIBO_INTERNAL_ONLY
1685 Returns a new string resulting from replacing n = count characters
1686 from position index in this string with newStr.
1688 @param index the replacing index in str.
1689 The index must be greater or equal as 0 and
1690 less or equal as the length of the string.
1691 @param count the count of characters that will replaced
1692 The count must be greater or equal as 0 and
1693 less or equal as the length of the string minus index.
1694 @param newStr the new substring.
1695 @return the new string.
1697 SAL_WARN_UNUSED_RESULT OString replaceAt( sal_Int32 index, sal_Int32 count, const OString& newStr ) const
1699 rtl_String* pNew = NULL;
1700 rtl_string_newReplaceStrAt( &pNew, pData, index, count, newStr.pData );
1701 return OString( pNew, SAL_NO_ACQUIRE );
1703 #endif
1705 #ifdef LIBO_INTERNAL_ONLY
1706 SAL_WARN_UNUSED_RESULT OString replaceAt( sal_Int32 index, sal_Int32 count, std::string_view newStr ) const
1708 rtl_String* pNew = NULL;
1709 rtl_string_newReplaceStrAt_WithLength ( &pNew, pData, index, count, newStr.data(), newStr.size() );
1710 return OString( pNew, SAL_NO_ACQUIRE );
1712 #endif
1715 Returns a new string resulting from replacing all occurrences of
1716 oldChar in this string with newChar.
1718 If the character oldChar does not occur in the character sequence
1719 represented by this object, then the string is assigned with
1720 str.
1722 @param oldChar the old character.
1723 @param newChar the new character.
1724 @return a string derived from this string by replacing every
1725 occurrence of oldChar with newChar.
1727 SAL_WARN_UNUSED_RESULT OString replace( char oldChar, char newChar ) const
1729 rtl_String* pNew = NULL;
1730 rtl_string_newReplace( &pNew, pData, oldChar, newChar );
1731 return OString( pNew, SAL_NO_ACQUIRE );
1735 Returns a new string resulting from replacing the first occurrence of a
1736 given substring with another substring.
1738 @param from the substring to be replaced
1740 @param to the replacing substring
1742 @param[in,out] index pointer to a start index; if the pointer is
1743 non-null: upon entry to the function, its value is the index into the this
1744 string at which to start searching for the \p from substring, the value
1745 must be non-negative and not greater than this string's length; upon exit
1746 from the function its value is the index into this string at which the
1747 replacement took place or -1 if no replacement took place; if the pointer
1748 is null, searching always starts at index 0
1750 @since LibreOffice 3.6
1752 SAL_WARN_UNUSED_RESULT OString replaceFirst(
1753 OString const & from, OString const & to, sal_Int32 * index = NULL) const
1755 rtl_String * s = NULL;
1756 sal_Int32 i = 0;
1757 rtl_string_newReplaceFirst(
1758 &s, pData, from.pData->buffer, from.pData->length,
1759 to.pData->buffer, to.pData->length, index == NULL ? &i : index);
1760 return OString(s, SAL_NO_ACQUIRE);
1764 Returns a new string resulting from replacing all occurrences of a given
1765 substring with another substring.
1767 Replacing subsequent occurrences picks up only after a given replacement.
1768 That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
1770 @param from the substring to be replaced
1772 @param to the replacing substring
1774 @since LibreOffice 3.6
1776 SAL_WARN_UNUSED_RESULT OString replaceAll(OString const & from, OString const & to) const {
1777 rtl_String * s = NULL;
1778 rtl_string_newReplaceAll(
1779 &s, pData, from.pData->buffer, from.pData->length,
1780 to.pData->buffer, to.pData->length);
1781 return OString(s, SAL_NO_ACQUIRE);
1785 Converts from this string all ASCII uppercase characters (65-90)
1786 to ASCII lowercase characters (97-122).
1788 This function can't be used for language specific conversion.
1789 If the string doesn't contain characters which must be converted,
1790 then the new string is assigned with str.
1792 @return the string, converted to ASCII lowercase.
1794 SAL_WARN_UNUSED_RESULT OString toAsciiLowerCase() const
1796 rtl_String* pNew = NULL;
1797 rtl_string_newToAsciiLowerCase( &pNew, pData );
1798 return OString( pNew, SAL_NO_ACQUIRE );
1802 Converts from this string all ASCII lowercase characters (97-122)
1803 to ASCII uppercase characters (65-90).
1805 This function can't be used for language specific conversion.
1806 If the string doesn't contain characters which must be converted,
1807 then the new string is assigned with str.
1809 @return the string, converted to ASCII uppercase.
1811 SAL_WARN_UNUSED_RESULT OString toAsciiUpperCase() const
1813 rtl_String* pNew = NULL;
1814 rtl_string_newToAsciiUpperCase( &pNew, pData );
1815 return OString( pNew, SAL_NO_ACQUIRE );
1819 Returns a new string resulting from removing white space from both ends
1820 of the string.
1822 All characters that have codes less than or equal to
1823 32 (the space character) are considered to be white space.
1824 If the string doesn't contain white spaces at both ends,
1825 then the new string is assigned with str.
1827 @return the string, with white space removed from the front and end.
1829 SAL_WARN_UNUSED_RESULT OString trim() const
1831 rtl_String* pNew = NULL;
1832 rtl_string_newTrim( &pNew, pData );
1833 return OString( pNew, SAL_NO_ACQUIRE );
1837 Returns a token in the string.
1839 Example:
1840 sal_Int32 nIndex = 0;
1844 OString aToken = aStr.getToken( 0, ';', nIndex );
1847 while ( nIndex >= 0 );
1849 @param token the number of the token to return.
1850 @param cTok the character which separate the tokens.
1851 @param index the position at which the token is searched in the
1852 string.
1853 The index must not be greater than the length of the
1854 string.
1855 This param is set to the position of the
1856 next token or to -1, if it is the last token.
1857 @return the token; if either token or index is negative, an empty token
1858 is returned (and index is set to -1)
1860 OString getToken( sal_Int32 token, char cTok, sal_Int32& index ) const
1862 rtl_String * pNew = NULL;
1863 index = rtl_string_getToken( &pNew, pData, token, cTok, index );
1864 return OString( pNew, SAL_NO_ACQUIRE );
1868 Returns a token from the string.
1870 The same as getToken(sal_Int32, char, sal_Int32 &), but always passing
1871 in 0 as the start index in the third argument.
1873 @param count the number of the token to return, starting with 0
1874 @param separator the character which separates the tokens
1876 @return the given token, or an empty string
1878 @since LibreOffice 3.6
1880 OString getToken(sal_Int32 count, char separator) const {
1881 sal_Int32 n = 0;
1882 return getToken(count, separator, n);
1886 Returns the Boolean value from this string.
1888 This function can't be used for language specific conversion.
1890 @return true, if the string is 1 or "True" in any ASCII case.
1891 false in any other case.
1893 bool toBoolean() const
1895 return rtl_str_toBoolean( pData->buffer );
1899 Returns the first character from this string.
1901 @return the first character from this string or 0, if this string
1902 is empty.
1904 char toChar() const
1906 return pData->buffer[0];
1910 Returns the int32 value from this string.
1912 This function can't be used for language specific conversion.
1914 @param radix the radix (between 2 and 36)
1915 @return the int32 represented from this string.
1916 0 if this string represents no number or one of too large
1917 magnitude.
1919 sal_Int32 toInt32( sal_Int16 radix = 10 ) const
1921 return rtl_str_toInt32( pData->buffer, radix );
1925 Returns the uint32 value from this string.
1927 This function can't be used for language specific conversion.
1929 @param radix the radix (between 2 and 36)
1930 @return the uint32 represented from this string.
1931 0 if this string represents no number or one of too large
1932 magnitude.
1934 @since LibreOffice 4.2
1936 sal_uInt32 toUInt32( sal_Int16 radix = 10 ) const
1938 return rtl_str_toUInt32( pData->buffer, radix );
1942 Returns the int64 value from this string.
1944 This function can't be used for language specific conversion.
1946 @param radix the radix (between 2 and 36)
1947 @return the int64 represented from this string.
1948 0 if this string represents no number or one of too large
1949 magnitude.
1951 sal_Int64 toInt64( sal_Int16 radix = 10 ) const
1953 return rtl_str_toInt64( pData->buffer, radix );
1957 Returns the uint64 value from this string.
1959 This function can't be used for language specific conversion.
1961 @param radix the radix (between 2 and 36)
1962 @return the uint64 represented from this string.
1963 0 if this string represents no number or one of too large
1964 magnitude.
1966 @since LibreOffice 4.1
1968 sal_uInt64 toUInt64( sal_Int16 radix = 10 ) const
1970 return rtl_str_toUInt64( pData->buffer, radix );
1974 Returns the float value from this string.
1976 This function can't be used for language specific conversion.
1978 @return the float represented from this string.
1979 0.0 if this string represents no number.
1981 float toFloat() const
1983 return rtl_str_toFloat( pData->buffer );
1987 Returns the double value from this string.
1989 This function can't be used for language specific conversion.
1991 @return the double represented from this string.
1992 0.0 if this string represents no number.
1994 double toDouble() const
1996 return rtl_str_toDouble( pData->buffer );
1999 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
2001 static auto number( int i, sal_Int16 radix = 10 )
2003 return OStringNumber<RTL_STR_MAX_VALUEOFINT32>(rtl_str_valueOfInt32, i, radix);
2005 static auto number( long long ll, sal_Int16 radix = 10 )
2007 return OStringNumber<RTL_STR_MAX_VALUEOFINT64>(rtl_str_valueOfInt64, ll, radix);
2009 static auto number( unsigned long long ll, sal_Int16 radix = 10 )
2011 return OStringNumber<RTL_STR_MAX_VALUEOFUINT64>(rtl_str_valueOfUInt64, ll, radix);
2013 static auto number( unsigned int i, sal_Int16 radix = 10 )
2015 return number( static_cast< unsigned long long >( i ), radix );
2017 static auto number( long i, sal_Int16 radix = 10)
2019 return number( static_cast< long long >( i ), radix );
2021 static auto number( unsigned long i, sal_Int16 radix = 10 )
2023 return number( static_cast< unsigned long long >( i ), radix );
2025 #else
2027 Returns the string representation of the integer argument.
2029 This function can't be used for language specific conversion.
2031 @param i an integer value
2032 @param radix the radix (between 2 and 36)
2033 @return a string with the string representation of the argument.
2034 @since LibreOffice 4.1
2036 static OString number( int i, sal_Int16 radix = 10 )
2038 char aBuf[RTL_STR_MAX_VALUEOFINT32];
2039 return OString(aBuf, rtl_str_valueOfInt32(aBuf, i, radix));
2041 /// @overload
2042 /// @since LibreOffice 4.1
2043 static OString number( unsigned int i, sal_Int16 radix = 10 )
2045 return number( static_cast< unsigned long long >( i ), radix );
2047 /// @overload
2048 /// @since LibreOffice 4.1
2049 static OString number( long i, sal_Int16 radix = 10 )
2051 return number( static_cast< long long >( i ), radix );
2053 /// @overload
2054 /// @since LibreOffice 4.1
2055 static OString number( unsigned long i, sal_Int16 radix = 10 )
2057 return number( static_cast< unsigned long long >( i ), radix );
2059 /// @overload
2060 /// @since LibreOffice 4.1
2061 static OString number( long long ll, sal_Int16 radix = 10 )
2063 char aBuf[RTL_STR_MAX_VALUEOFINT64];
2064 return OString(aBuf, rtl_str_valueOfInt64(aBuf, ll, radix));
2066 /// @overload
2067 /// @since LibreOffice 4.1
2068 static OString number( unsigned long long ll, sal_Int16 radix = 10 )
2070 char aBuf[RTL_STR_MAX_VALUEOFUINT64];
2071 return OString(aBuf, rtl_str_valueOfUInt64(aBuf, ll, radix));
2073 #endif
2076 Returns the string representation of the float argument.
2078 This function can't be used for language specific conversion.
2080 @param f a float.
2081 @return a string with the decimal representation of the argument.
2082 @since LibreOffice 4.1
2084 static OString number( float f )
2086 rtl_String* pNew = NULL;
2087 // Same as rtl::str::valueOfFP, used for rtl_str_valueOfFloat
2088 rtl_math_doubleToString(&pNew, NULL, 0, f, rtl_math_StringFormat_G,
2089 RTL_STR_MAX_VALUEOFFLOAT - SAL_N_ELEMENTS("-x.E-xxx") + 1, '.',
2090 NULL, 0, true);
2091 if (pNew == NULL)
2092 throw std::bad_alloc();
2094 return OString(pNew, SAL_NO_ACQUIRE);
2098 Returns the string representation of the double argument.
2100 This function can't be used for language specific conversion.
2102 @param d a double.
2103 @return a string with the decimal representation of the argument.
2104 @since LibreOffice 4.1
2106 static OString number( double d )
2108 rtl_String* pNew = NULL;
2109 // Same as rtl::str::valueOfFP, used for rtl_str_valueOfDouble
2110 rtl_math_doubleToString(&pNew, NULL, 0, d, rtl_math_StringFormat_G,
2111 RTL_STR_MAX_VALUEOFDOUBLE - SAL_N_ELEMENTS("-x.E-xxx") + 1, '.',
2112 NULL, 0, true);
2113 if (pNew == NULL)
2114 throw std::bad_alloc();
2116 return OString(pNew, SAL_NO_ACQUIRE);
2119 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
2120 static auto boolean(bool b)
2122 return OStringNumber<RTL_STR_MAX_VALUEOFBOOLEAN>(rtl_str_valueOfBoolean, b);
2124 #else
2126 Returns the string representation of the sal_Bool argument.
2128 If the sal_Bool is true, the string "true" is returned.
2129 If the sal_Bool is false, the string "false" is returned.
2130 This function can't be used for language specific conversion.
2132 @param b a sal_Bool.
2133 @return a string with the string representation of the argument.
2134 @deprecated use boolean()
2136 SAL_DEPRECATED("use boolean()") static OString valueOf( sal_Bool b )
2138 return boolean(b);
2142 Returns the string representation of the boolean argument.
2144 If the argument is true, the string "true" is returned.
2145 If the argument is false, the string "false" is returned.
2146 This function can't be used for language specific conversion.
2148 @param b a bool.
2149 @return a string with the string representation of the argument.
2150 @since LibreOffice 4.1
2152 static OString boolean( bool b )
2154 char aBuf[RTL_STR_MAX_VALUEOFBOOLEAN];
2155 return OString(aBuf, rtl_str_valueOfBoolean(aBuf, b));
2157 #endif
2160 Returns the string representation of the char argument.
2162 @param c a character.
2163 @return a string with the string representation of the argument.
2164 @deprecated use operator, function or constructor taking char or sal_Unicode argument
2166 SAL_DEPRECATED("convert to OString or use directly") static OString valueOf( char c )
2168 return OString( &c, 1 );
2172 Returns the string representation of the int argument.
2174 This function can't be used for language specific conversion.
2176 @param i a int32.
2177 @param radix the radix (between 2 and 36)
2178 @return a string with the string representation of the argument.
2179 @deprecated use number()
2181 SAL_DEPRECATED("use number()") static OString valueOf( sal_Int32 i, sal_Int16 radix = 10 )
2183 return number( i, radix );
2187 Returns the string representation of the long argument.
2189 This function can't be used for language specific conversion.
2191 @param ll a int64.
2192 @param radix the radix (between 2 and 36)
2193 @return a string with the string representation of the argument.
2194 @deprecated use number()
2196 SAL_DEPRECATED("use number()") static OString valueOf( sal_Int64 ll, sal_Int16 radix = 10 )
2198 return number( ll, radix );
2202 Returns the string representation of the float argument.
2204 This function can't be used for language specific conversion.
2206 @param f a float.
2207 @return a string with the string representation of the argument.
2208 @deprecated use number()
2210 SAL_DEPRECATED("use number()") static OString valueOf( float f )
2212 return number(f);
2216 Returns the string representation of the double argument.
2218 This function can't be used for language specific conversion.
2220 @param d a double.
2221 @return a string with the string representation of the argument.
2222 @deprecated use number()
2224 SAL_DEPRECATED("use number()") static OString valueOf( double d )
2226 return number(d);
2229 #if defined LIBO_INTERNAL_ONLY
2230 operator std::string_view() const { return {getStr(), sal_uInt32(getLength())}; }
2231 #endif
2233 #if defined LIBO_INTERNAL_ONLY
2234 // A wrapper for the first expression in an
2236 // OString::Concat(e1) + e2 + ...
2238 // concatenation chain, when neither of the first two e1, e2 is one of our rtl string-related
2239 // classes (so something like
2241 // OString s = "a" + (b ? std::string_view("c") : std::string_view("dd"));
2243 // would not compile):
2244 template<typename T> [[nodiscard]] static
2245 OStringConcat<OStringConcatMarker, T>
2246 Concat(T const & value) { return OStringConcat<OStringConcatMarker, T>(value); }
2248 // This overload is needed so that an argument of type 'char const[N]' ends up as
2249 // 'OStringConcat<rtl::OStringConcatMarker, char const[N]>' rather than as
2250 // 'OStringConcat<rtl::OStringConcatMarker, char[N]>':
2251 template<typename T, std::size_t N> [[nodiscard]] static
2252 OStringConcat<OStringConcatMarker, T[N]>
2253 Concat(T (& value)[N]) { return OStringConcat<OStringConcatMarker, T[N]>(value); }
2254 #endif
2257 #if defined LIBO_INTERNAL_ONLY
2258 // Can only define this after we define OString
2259 inline OStringConstExpr::operator const OString &() const { return OString::unacquired(&pData); }
2260 #endif
2262 #if defined LIBO_INTERNAL_ONLY
2263 inline bool operator ==(OString const & lhs, StringConcatenation<char> const & rhs)
2264 { return lhs == std::string_view(rhs); }
2265 inline bool operator !=(OString const & lhs, StringConcatenation<char> const & rhs)
2266 { return lhs != std::string_view(rhs); }
2267 inline bool operator ==(StringConcatenation<char> const & lhs, OString const & rhs)
2268 { return std::string_view(lhs) == rhs; }
2269 inline bool operator !=(StringConcatenation<char> const & lhs, OString const & rhs)
2270 { return std::string_view(lhs) != rhs; }
2271 #endif
2273 /* ======================================================================= */
2275 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
2278 @internal
2280 template<>
2281 struct ToStringHelper< OString >
2283 static std::size_t length( const OString& s ) { return s.getLength(); }
2284 char* operator()( char* buffer, const OString& s ) const { return addDataHelper( buffer, s.getStr(), s.getLength()); }
2288 @internal
2290 template<std::size_t N>
2291 struct ToStringHelper< OStringLiteral<N> >
2293 static constexpr std::size_t length( const OStringLiteral<N>& str ) { return str.getLength(); }
2294 char* operator()( char* buffer, const OStringLiteral<N>& str ) const { return addDataHelper( buffer, str.getStr(), str.getLength() ); }
2298 @internal
2300 template< typename charT, typename traits, typename T1, typename T2 >
2301 inline std::basic_ostream<charT, traits> & operator <<(
2302 std::basic_ostream<charT, traits> & stream, OStringConcat< T1, T2 >&& concat)
2304 return stream << OString( std::move(concat) );
2306 #endif
2309 /** A helper to use OStrings with hash maps.
2311 Instances of this class are unary function objects that can be used as
2312 hash function arguments to std::unordered_map and similar constructs.
2314 struct OStringHash
2316 /** Compute a hash code for a string.
2318 @param rString
2319 a string.
2321 @return
2322 a hash code for the string. This hash code should not be stored
2323 persistently, as its computation may change in later revisions.
2325 size_t operator()( const OString& rString ) const
2326 { return static_cast<size_t>(rString.hashCode()); }
2329 /** Equality functor for classic c-strings (i.e., null-terminated char* strings). */
2330 struct CStringEqual
2332 bool operator()( const char* p1, const char* p2) const
2333 { return rtl_str_compare(p1, p2) == 0; }
2336 /** Hashing functor for classic c-strings (i.e., null-terminated char* strings). */
2337 struct CStringHash
2339 size_t operator()(const char* p) const
2340 { return rtl_str_hashCode(p); }
2343 /* ======================================================================= */
2346 Support for rtl::OString in std::ostream (and thus in
2347 CPPUNIT_ASSERT or SAL_INFO macros, for example).
2349 @since LibreOffice 4.0
2351 template< typename charT, typename traits > std::basic_ostream<charT, traits> &
2352 operator <<(
2353 std::basic_ostream<charT, traits> & stream, OString const & rString)
2355 return stream << rString.getStr();
2356 // best effort; potentially loses data due to embedded null characters
2359 } /* Namespace */
2361 #ifdef RTL_STRING_UNITTEST
2362 namespace rtl
2364 typedef rtlunittest::OString OString;
2366 #undef RTL_STRING_CONST_FUNCTION
2367 #endif
2369 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
2370 using ::rtl::OString;
2371 using ::rtl::OStringChar;
2372 using ::rtl::Concat2View;
2373 using ::rtl::OStringHash;
2374 using ::rtl::OStringLiteral;
2375 #endif
2377 /// @cond INTERNAL
2379 Make OString hashable by default for use in STL containers.
2381 @since LibreOffice 6.0
2383 #if defined LIBO_INTERNAL_ONLY
2384 namespace std {
2386 template<>
2387 struct hash<::rtl::OString>
2389 std::size_t operator()(::rtl::OString const & s) const
2391 if constexpr (sizeof(std::size_t) == 8)
2393 // return a hash that uses the full 64-bit range instead of a 32-bit value
2394 size_t n = 0;
2395 for (sal_Int32 i = 0, len = s.getLength(); i < len; ++i)
2396 n = 31 * n + s[i];
2397 return n;
2399 else
2400 return std::size_t(s.hashCode());
2406 #endif
2407 /// @endcond
2409 #endif // INCLUDED_RTL_STRING_HXX
2411 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */