Avoid potential negative array index access to cached text.
[LibreOffice.git] / include / rtl / string.hxx
blob91fb155a5e9d6f5701fce5363d97d012a5ce4cd0
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 <algorithm>
40 #include <string_view>
41 #include <type_traits>
42 #endif
44 #include "rtl/math.h"
45 #include "rtl/textenc.h"
46 #include "rtl/string.h"
47 #include "rtl/stringutils.hxx"
49 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
50 #include "config_global.h"
51 #include "rtl/stringconcat.hxx"
52 #endif
54 #ifdef RTL_STRING_UNITTEST
55 extern bool rtl_string_unittest_const_literal;
56 extern bool rtl_string_unittest_const_literal_function;
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 /// @cond INTERNAL
72 #ifdef RTL_STRING_UNITTEST
73 #undef rtl
74 // helper macro to make functions appear more readable
75 #define RTL_STRING_CONST_FUNCTION rtl_string_unittest_const_literal_function = true;
76 #else
77 #define RTL_STRING_CONST_FUNCTION
78 #endif
79 /// @endcond
81 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
83 /**
84 A wrapper dressing a string literal as a static-refcount rtl_String.
86 This class is not part of public API and is meant to be used only in LibreOffice code.
87 @since LibreOffice 4.0
89 template<std::size_t N> class SAL_WARN_UNUSED OStringLiteral {
90 static_assert(N != 0);
91 static_assert(N - 1 <= std::numeric_limits<sal_Int32>::max(), "literal too long");
93 public:
94 #if HAVE_CPP_CONSTEVAL
95 consteval
96 #else
97 constexpr
98 #endif
99 OStringLiteral(char const (&literal)[N]) {
100 assertLayout();
101 assert(literal[N - 1] == '\0');
102 std::copy_n(literal, N, more.buffer);
105 #if !(defined _MSC_VER && _MSC_VER >= 1930 && _MSC_VER <= 1939 && defined _MANAGED)
106 #if HAVE_CPP_CONSTEVAL
107 consteval
108 #else
109 constexpr
110 #endif
111 OStringLiteral(char8_t const (&literal)[N]) {
112 assertLayout();
113 assert(literal[N - 1] == '\0');
114 std::copy_n(literal, N, more.buffer);
116 #endif
118 constexpr sal_Int32 getLength() const { return more.length; }
120 constexpr char const * getStr() const SAL_RETURNS_NONNULL { return more.buffer; }
122 constexpr operator std::string_view() const { return {more.buffer, sal_uInt32(more.length)}; }
124 private:
125 static constexpr void assertLayout() {
126 // These static_asserts verifying the layout compatibility with rtl_String cannot be class
127 // member declarations, as offsetof requires a complete type, so defer them to here:
128 static_assert(std::is_standard_layout_v<OStringLiteral>);
129 static_assert(offsetof(OStringLiteral, str.refCount) == offsetof(OStringLiteral, more.refCount));
130 static_assert(offsetof(OStringLiteral, str.length) == offsetof(OStringLiteral, more.length));
131 static_assert(offsetof(OStringLiteral, str.buffer) == offsetof(OStringLiteral, more.buffer));
134 struct Data {
135 Data() = default;
137 oslInterlockedCount refCount = 0x40000000; // SAL_STRING_STATIC_FLAG (sal/rtl/strimp.hxx)
138 sal_Int32 length = N - 1;
139 char buffer[N];
142 public:
143 // (Data members must be public so that OStringLiteral is a structural type that can be used as
144 // a non-type template parameter type for operator ""_ostr and rtl::detail::OStringHolder:)
145 union {
146 rtl_String str;
147 Data more = {};
151 #if !(defined _MSC_VER && _MSC_VER <= 1929 && defined _MANAGED)
153 namespace detail {
155 template<OStringLiteral L> struct OStringHolder {
156 static constexpr auto & literal = L;
161 #endif
163 #endif
165 /* ======================================================================= */
168 This String class provide base functionality for C++ like 8-Bit
169 character array handling. The advantage of this class is, that it
170 handle all the memory management for you - and it do it
171 more efficient. If you assign a string to another string, the
172 data of both strings are shared (without any copy operation or
173 memory allocation) as long as you do not change the string. This class
174 stores also the length of the string, so that many operations are
175 faster as the C-str-functions.
177 This class provides only readonly string handling. So you could create
178 a string and you could only query the content from this string.
179 It provides also functionality to change the string, but this results
180 in every case in a new string instance (in the most cases with an
181 memory allocation). You don't have functionality to change the
182 content of the string. If you want to change the string content, then
183 you should use the OStringBuffer class, which provides these
184 functionalities and avoid too much memory allocation.
186 The design of this class is similar to the string classes in Java
187 and so more people should have fewer understanding problems when they
188 use this class.
191 class SAL_WARN_UNUSED SAL_DLLPUBLIC_RTTI OString
193 public:
194 /// @cond INTERNAL
195 rtl_String * pData;
196 /// @endcond
199 New string containing no characters.
201 OString()
203 pData = NULL;
204 rtl_string_new( &pData );
208 New string from OString.
210 @param str an OString.
212 #if defined LIBO_INTERNAL_ONLY && !(defined _MSC_VER && _MSC_VER <= 1929 && defined _MANAGED)
213 constexpr
214 #endif
215 OString( const OString & str )
217 pData = str.pData;
218 #if defined LIBO_INTERNAL_ONLY && !(defined _MSC_VER && _MSC_VER <= 1929 && defined _MANAGED)
219 if (std::is_constant_evaluated()) {
220 //TODO: We would want to
222 // assert(SAL_STRING_IS_STATIC(pData));
224 // here, but that wouldn't work because read of member `str` of OUStringLiteral's
225 // anonymous union with active member `more` is not allowed in a constant expression.
226 } else
227 #endif
228 rtl_string_acquire( pData );
231 #if defined LIBO_INTERNAL_ONLY
233 Move constructor.
235 @param str an OString.
236 @since LibreOffice 5.2
238 #if !(defined _MSC_VER && _MSC_VER <= 1929 && defined _MANAGED)
239 constexpr
240 #endif
241 OString( OString && str ) noexcept
243 pData = str.pData;
244 #if !(defined _MSC_VER && _MSC_VER <= 1929 && defined _MANAGED)
245 if (std::is_constant_evaluated()) {
246 //TODO: We would want to
248 // assert(SAL_STRING_IS_STATIC(pData));
250 // here, but that wouldn't work because read of member `str` of OUStringLiteral's
251 // anonymous union with active member `more` is not allowed in a constant expression.
252 return;
254 #endif
255 str.pData = nullptr;
256 rtl_string_new( &str.pData );
258 #endif
261 New string from OString data.
263 @param str an OString data.
265 OString( rtl_String * str )
267 pData = str;
268 rtl_string_acquire( pData );
271 /** New string from OString data without acquiring it. Takeover of ownership.
273 The SAL_NO_ACQUIRE dummy parameter is only there to distinguish this
274 from other constructors.
276 @param str an OString data.
278 OString( rtl_String * str, __sal_NoAcquire )
280 pData = str;
284 New string from a single character.
286 @param value a character.
288 explicit OString( char value )
289 : pData (NULL)
291 rtl_string_newFromStr_WithLength( &pData, &value, 1 );
294 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST_CONCAT
295 // Catch inadvertent conversions to the above ctor (e.g., from sal_[u]Int8, aka [un]signed
296 // char):
297 OString(int) = delete;
298 #endif
301 New string from a character buffer array.
303 Note: The argument type is always either char* or const char*. The template is
304 used only for technical reasons, as is the second argument.
306 @param value a NULL-terminated character array.
308 template< typename T >
309 OString( const T& value, typename libreoffice_internal::CharPtrDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
311 pData = NULL;
312 rtl_string_newFromStr( &pData, value );
315 template< typename T >
316 OString( T& value, typename libreoffice_internal::NonConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
318 pData = NULL;
319 rtl_string_newFromStr( &pData, value );
322 #if __cplusplus > 202002L // C++23 P2266R3 "Simpler implicit move"
323 template< typename T >
324 OString( T&& value, typename libreoffice_internal::NonConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
326 pData = NULL;
327 rtl_string_newFromStr( &pData, value );
329 #endif
332 New string from a string literal.
334 If there are any embedded \0's in the string literal, the result is undefined.
335 Use the overload that explicitly accepts length.
337 @since LibreOffice 3.6
339 @param literal a string literal
341 template< typename T >
342 OString( T& literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
344 assert(
345 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
346 pData = NULL;
347 if (libreoffice_internal::ConstCharArrayDetector<T>::length == 0) {
348 rtl_string_new(&pData);
349 } else {
350 rtl_string_newFromLiteral(
351 &pData,
352 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
353 literal),
354 libreoffice_internal::ConstCharArrayDetector<T>::length, 0);
356 #ifdef RTL_STRING_UNITTEST
357 rtl_string_unittest_const_literal = true;
358 #endif
362 New string from a character buffer array.
364 @param value a character array.
365 @param length the number of character which should be copied.
366 The character array length must be greater or
367 equal than this value.
369 OString( const char * value, sal_Int32 length )
371 pData = NULL;
372 rtl_string_newFromStr_WithLength( &pData, value, length );
375 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
376 /// @cond INTERNAL
378 New string from an 8-Bit string literal.
380 @since LibreOffice 7.1
382 template<std::size_t N> constexpr OString(OStringLiteral<N> const & literal):
383 pData(const_cast<rtl_String *>(&literal.str)) {}
384 template<std::size_t N> OString(OStringLiteral<N> &&) = delete;
385 /// @endcond
386 #endif
388 #if defined LIBO_INTERNAL_ONLY && !(defined _MSC_VER && _MSC_VER <= 1929 && defined _MANAGED)
389 // For operator ""_tstr:
390 template<OStringLiteral L> constexpr OString(detail::OStringHolder<L> const & holder):
391 pData(const_cast<rtl_String *>(&holder.literal.str)) {}
392 #endif
394 #if defined LIBO_INTERNAL_ONLY
395 explicit OString(std::string_view sv) {
396 if (sv.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max())) {
397 throw std::bad_alloc();
399 pData = nullptr;
400 rtl_string_newFromStr_WithLength(&pData, sv.data(), sv.size());
402 #endif
405 New string from a Unicode character buffer array.
407 @param value a Unicode character array.
408 @param length the number of character which should be converted.
409 The Unicode character array length must be
410 greater or equal than this value.
411 @param encoding the text encoding in which the Unicode character
412 sequence should be converted.
413 @param convertFlags flags which controls the conversion.
414 see RTL_UNICODETOTEXT_FLAGS_...
416 @exception std::bad_alloc is thrown if an out-of-memory condition occurs
418 OString( const sal_Unicode * value, sal_Int32 length,
419 rtl_TextEncoding encoding,
420 sal_uInt32 convertFlags = OUSTRING_TO_OSTRING_CVTFLAGS )
422 pData = NULL;
423 rtl_uString2String( &pData, value, length, encoding, convertFlags );
424 if (pData == NULL) {
425 throw std::bad_alloc();
429 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
431 @overload
432 @internal
434 template< typename T1, typename T2 >
435 OString( OStringConcat< T1, T2 >&& c )
437 const sal_Int32 l = c.length();
438 pData = rtl_string_alloc( l );
439 if (l != 0)
441 char* end = c.addData( pData->buffer );
442 pData->length = l;
443 *end = '\0';
448 @overload
449 @internal
451 template< std::size_t N >
452 OString( OStringNumber< N >&& n )
453 : OString( n.buf, n.length )
455 #endif
457 #ifdef LIBO_INTERNAL_ONLY
458 OString(std::nullptr_t) = delete;
459 #endif
462 Release the string data.
464 #if defined LIBO_INTERNAL_ONLY && !(defined _MSC_VER && _MSC_VER <= 1929 && defined _MANAGED)
465 constexpr
466 #endif
467 ~OString()
469 #if defined LIBO_INTERNAL_ONLY && !(defined _MSC_VER && _MSC_VER <= 1929 && defined _MANAGED)
470 if (std::is_constant_evaluated()) {
471 //TODO: We would want to
473 // assert(SAL_STRING_IS_STATIC(pData));
475 // here, but that wouldn't work because read of member `str` of OUStringLiteral's
476 // anonymous union with active member `more` is not allowed in a constant expression.
477 } else
478 #endif
479 rtl_string_release( pData );
482 #if defined LIBO_INTERNAL_ONLY
483 /** Provides an OString const & passing a storage pointer of an
484 rtl_String * handle.
485 It is more convenient to use C++ OString member functions when dealing
486 with rtl_String * handles. Using this function avoids unnecessary
487 acquire()/release() calls for a temporary OString object.
489 @param ppHandle
490 pointer to storage
491 @return
492 OString const & based on given storage
494 static OString const & unacquired( rtl_String * const * ppHandle )
495 { return * reinterpret_cast< OString const * >( ppHandle ); }
496 #endif
499 Assign a new string.
501 @param str an OString.
503 OString & operator=( const OString & str )
505 rtl_string_assign( &pData, str.pData );
506 return *this;
509 #if defined LIBO_INTERNAL_ONLY
511 Move assign a new string.
513 @param str an OString.
514 @since LibreOffice 5.2
516 OString & operator=( OString && str ) noexcept
518 rtl_string_release( pData );
519 pData = str.pData;
520 str.pData = nullptr;
521 rtl_string_new( &str.pData );
522 return *this;
524 #endif
527 @overload
528 This function accepts an ASCII string literal as its argument.
529 @since LibreOffice 3.6
531 template< typename T >
532 typename libreoffice_internal::ConstCharArrayDetector< T, OString& >::Type operator=( T& literal )
534 RTL_STRING_CONST_FUNCTION
535 assert(
536 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
537 if (libreoffice_internal::ConstCharArrayDetector<T>::length == 0) {
538 rtl_string_new(&pData);
539 } else {
540 rtl_string_newFromLiteral(
541 &pData,
542 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
543 literal),
544 libreoffice_internal::ConstCharArrayDetector<T>::length, 0);
546 return *this;
550 Append a string to this string.
552 @param str an OString.
554 OString & operator+=( const OString & str )
555 #if defined LIBO_INTERNAL_ONLY
557 #endif
559 rtl_string_newConcat( &pData, pData, str.pData );
560 return *this;
562 #if defined LIBO_INTERNAL_ONLY
563 void operator+=(OString const &) && = delete;
564 #endif
566 #if defined LIBO_INTERNAL_ONLY
567 template<typename T> typename libreoffice_internal::CharPtrDetector<T, OString &>::Type
568 operator +=(T const & value) & { return operator +=(std::string_view(value)); }
569 template<typename T> typename libreoffice_internal::CharPtrDetector<T, OString &>::Type
570 operator +=(T const &) && = delete;
572 template<typename T>
573 typename libreoffice_internal::NonConstCharArrayDetector<T, OString &>::Type
574 operator +=(T & value) & { return operator +=(std::string_view(value)); }
575 template<typename T>
576 typename libreoffice_internal::NonConstCharArrayDetector<T, OString &>::Type operator +=(T &) &&
577 = delete;
579 template<typename T> typename libreoffice_internal::ConstCharArrayDetector<T, OString &>::Type
580 operator +=(T & literal) & {
581 assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
582 return operator +=(
583 std::string_view(
584 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
585 libreoffice_internal::ConstCharArrayDetector<T>::length));
587 template<typename T> typename libreoffice_internal::ConstCharArrayDetector<T, OString &>::Type
588 operator +=(T &) && = delete;
590 template<std::size_t N> OString & operator +=(OStringLiteral<N> const & literal) &
591 { return operator +=(std::string_view(literal.getStr(), literal.getLength())); }
592 template<std::size_t N> void operator +=(OStringLiteral<N> const &) && = delete;
594 OString & operator +=(std::string_view sv) & {
595 if (sv.empty()) {
596 return *this;
598 if (sv.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max() - pData->length)) {
599 throw std::bad_alloc();
601 auto const l = pData->length + sv.size();
602 rtl_string_ensureCapacity(&pData, l);
603 *addDataHelper(pData->buffer + pData->length, sv.data(), sv.size()) = '\0';
604 pData->length = l;
605 return *this;
607 void operator +=(std::string_view) && = delete;
608 #endif
610 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
612 @overload
613 @internal
615 template< typename T1, typename T2 >
616 OString& operator+=( OStringConcat< T1, T2 >&& c ) & {
617 sal_Int32 l = c.length();
618 if( l == 0 )
619 return *this;
620 l += pData->length;
621 rtl_string_ensureCapacity( &pData, l );
622 char* end = c.addData( pData->buffer + pData->length );
623 *end = '\0';
624 pData->length = l;
625 return *this;
627 template<typename T1, typename T2> void operator +=(
628 OStringConcat<T1, T2> &&) && = delete;
631 @overload
632 @internal
634 template< std::size_t N >
635 OString& operator+=( OStringNumber< N >&& n ) & {
636 return operator +=(std::string_view(n.buf, n.length));
638 template<std::size_t N> void operator +=(
639 OStringNumber<N> &&) && = delete;
640 #endif
643 Clears the string, i.e, makes a zero-character string
644 @since LibreOffice 4.4
646 void clear()
648 rtl_string_new( &pData );
652 Returns the length of this string.
654 The length is equal to the number of characters in this string.
656 @return the length of the sequence of characters represented by this
657 object.
659 sal_Int32 getLength() const { return pData->length; }
662 Checks if a string is empty.
664 @return true if the string is empty;
665 false, otherwise.
667 @since LibreOffice 3.4
669 bool isEmpty() const
671 return pData->length == 0;
675 Returns a pointer to the characters of this string.
677 <p>The returned pointer is guaranteed to point to a null-terminated byte
678 string. But note that this string object may contain embedded null
679 characters, which will thus also be embedded in the returned
680 null-terminated byte string.</p>
682 @return a pointer to a null-terminated byte string representing the
683 characters of this string object.
685 const char * getStr() const SAL_RETURNS_NONNULL { return pData->buffer; }
688 Access to individual characters.
690 @param index must be non-negative and less than length.
692 @return the character at the given index.
694 @since LibreOffice 3.5
696 char operator [](sal_Int32 index) const {
697 // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
698 assert(index >= 0 && static_cast<sal_uInt32>(index) < static_cast<sal_uInt32>(getLength()));
699 return getStr()[index];
703 Compares two strings.
705 The comparison is based on the numeric value of each character in
706 the strings and return a value indicating their relationship.
707 This function can't be used for language specific sorting.
709 @param str the object to be compared.
710 @return 0 - if both strings are equal
711 < 0 - if this string is less than the string argument
712 > 0 - if this string is greater than the string argument
714 sal_Int32 compareTo( const OString & str ) const
716 return rtl_str_compare_WithLength( pData->buffer, pData->length,
717 str.pData->buffer, str.pData->length );
721 Compares two strings with an maximum count of characters.
723 The comparison is based on the numeric value of each character in
724 the strings and return a value indicating their relationship.
725 This function can't be used for language specific sorting.
727 @param rObj the object to be compared.
728 @param maxLength the maximum count of characters to be compared.
729 @return 0 - if both strings are equal
730 < 0 - if this string is less than the string argument
731 > 0 - if this string is greater than the string argument
733 sal_Int32 compareTo( const OString & rObj, sal_Int32 maxLength ) const
735 return rtl_str_shortenedCompare_WithLength( pData->buffer, pData->length,
736 rObj.pData->buffer, rObj.pData->length, maxLength );
740 Compares two strings in reverse order.
742 The comparison is based on the numeric value of each character in
743 the strings and return a value indicating their relationship.
744 This function can't be used for language specific sorting.
746 @param str the object to be compared.
747 @return 0 - if both strings are equal
748 < 0 - if this string is less than the string argument
749 > 0 - if this string is greater than the string argument
751 sal_Int32 reverseCompareTo( const OString & str ) const
753 return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length,
754 str.pData->buffer, str.pData->length );
758 Perform a comparison of two strings.
760 The result is true if and only if second string
761 represents the same sequence of characters as the first string.
762 This function can't be used for language specific comparison.
764 @param str the object to be compared.
765 @return true if the strings are equal;
766 false, otherwise.
768 bool equals( const OString & str ) const
770 if ( pData->length != str.pData->length )
771 return false;
772 if ( pData == str.pData )
773 return true;
774 return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length,
775 str.pData->buffer, str.pData->length ) == 0;
779 Perform a comparison of two strings.
781 The result is true if and only if second string
782 represents the same sequence of characters as the first string.
783 The ASCII string must be greater or equal as length.
784 This function can't be used for language specific comparison.
787 @param value a character array.
788 @param length the length of the character array.
789 @return true if the strings are equal;
790 false, otherwise.
792 bool equalsL( const char* value, sal_Int32 length ) const
794 if ( pData->length != length )
795 return false;
797 return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length,
798 value, length ) == 0;
802 Perform an ASCII lowercase comparison of two strings.
804 The result is true if and only if second string
805 represents the same sequence of characters as the first string,
806 ignoring the case.
807 Character values between 65 and 90 (ASCII A-Z) are interpreted as
808 values between 97 and 122 (ASCII a-z).
809 This function can't be used for language specific comparison.
811 @param str the object to be compared.
812 @return true if the strings are equal;
813 false, otherwise.
815 #if defined LIBO_INTERNAL_ONLY
816 bool equalsIgnoreAsciiCase( std::string_view str ) const
818 if ( sal_uInt32(pData->length) != str.size() )
819 return false;
820 if ( pData->buffer == str.data() )
821 return true;
822 return rtl_str_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
823 str.data(), str.size() ) == 0;
825 #else
826 bool equalsIgnoreAsciiCase( const OString & str ) const
828 if ( pData->length != str.pData->length )
829 return false;
830 if ( pData == str.pData )
831 return true;
832 return rtl_str_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
833 str.pData->buffer, str.pData->length ) == 0;
835 #endif
838 Perform an ASCII lowercase comparison of two strings.
840 The result is true if and only if second string
841 represents the same sequence of characters as the first string,
842 ignoring the case.
843 Character values between 65 and 90 (ASCII A-Z) are interpreted as
844 values between 97 and 122 (ASCII a-z).
845 Since this method is optimized for performance, the ASCII character
846 values are not converted in any way. The caller has to make sure that
847 all ASCII characters are in the allowed range between 0 and
848 127. The ASCII string must be NULL-terminated.
849 This function can't be used for language specific comparison.
851 Note: The argument type is always either char* or const char*, the return type is bool.
852 The template is used only for technical reasons.
854 @param asciiStr the 8-Bit ASCII character string to be compared.
855 @return true if the strings are equal;
856 false, otherwise.
858 template< typename T >
859 typename libreoffice_internal::CharPtrDetector< T, bool >::Type equalsIgnoreAsciiCase( const T& asciiStr ) const
861 return rtl_str_compareIgnoreAsciiCase( pData->buffer, asciiStr ) == 0;
864 template< typename T >
865 typename libreoffice_internal::NonConstCharArrayDetector< T, bool >::Type equalsIgnoreAsciiCase( T& asciiStr ) const
867 return rtl_str_compareIgnoreAsciiCase( pData->buffer, asciiStr ) == 0;
871 @overload
872 This function accepts an ASCII string literal as its argument.
873 @since LibreOffice 3.6
875 template< typename T >
876 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type equalsIgnoreAsciiCase( T& literal ) const
878 RTL_STRING_CONST_FUNCTION
879 assert(
880 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
881 return
882 (pData->length
883 == libreoffice_internal::ConstCharArrayDetector<T>::length)
884 && (rtl_str_compareIgnoreAsciiCase_WithLength(
885 pData->buffer, pData->length,
886 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
887 literal),
888 libreoffice_internal::ConstCharArrayDetector<T>::length)
889 == 0);
893 Perform an ASCII lowercase comparison of two strings.
895 The result is true if and only if second string
896 represents the same sequence of characters as the first string,
897 ignoring the case.
898 Character values between 65 and 90 (ASCII A-Z) are interpreted as
899 values between 97 and 122 (ASCII a-z).
900 Since this method is optimized for performance, the ASCII character
901 values are not converted in any way. The caller has to make sure that
902 all ASCII characters are in the allowed range between 0 and
903 127. The ASCII string must be greater or equal in length as asciiStrLength.
904 This function can't be used for language specific comparison.
906 @param asciiStr the 8-Bit ASCII character string to be compared.
907 @param asciiStrLength the length of the ascii string
908 @return true if the strings are equal;
909 false, otherwise.
911 bool equalsIgnoreAsciiCaseL( const char * asciiStr, sal_Int32 asciiStrLength ) const
913 if ( pData->length != asciiStrLength )
914 return false;
916 return rtl_str_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
917 asciiStr, asciiStrLength ) == 0;
921 Match against a substring appearing in this string.
923 The result is true if and only if the second string appears as a substring
924 of this string, at the given position.
925 This function can't be used for language specific comparison.
927 @param str the object (substring) to be compared.
928 @param fromIndex the index to start the comparison from.
929 The index must be greater or equal than 0
930 and less or equal as the string length.
931 @return true if str match with the characters in the string
932 at the given position;
933 false, otherwise.
935 #if defined LIBO_INTERNAL_ONLY
936 bool match( std::string_view str, sal_Int32 fromIndex = 0 ) const
938 return rtl_str_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
939 str.data(), str.size(), str.size() ) == 0;
941 #else
942 bool match( const OString & str, sal_Int32 fromIndex = 0 ) const
944 return rtl_str_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
945 str.pData->buffer, str.pData->length, str.pData->length ) == 0;
947 #endif
950 @overload
951 This function accepts an ASCII string literal as its argument.
952 @since LibreOffice 3.6
954 template< typename T >
955 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type match( T& literal, sal_Int32 fromIndex = 0 ) const
957 RTL_STRING_CONST_FUNCTION
958 assert(
959 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
960 return
961 rtl_str_shortenedCompare_WithLength(
962 pData->buffer + fromIndex, pData->length - fromIndex,
963 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
964 literal),
965 libreoffice_internal::ConstCharArrayDetector<T>::length,
966 libreoffice_internal::ConstCharArrayDetector<T>::length)
967 == 0;
971 Match against a substring appearing in this string.
973 @param str the substring to be compared; must not be null and must point
974 to memory of at least strLength bytes
976 @param strLength the length of the substring; must be non-negative
978 @param fromIndex the index into this string to start the comparison at;
979 must be non-negative and not greater than this string's length
981 @return true if and only if the given str is contained as a substring of
982 this string at the given fromIndex
984 @since LibreOffice 3.6
986 bool matchL(
987 char const * str, sal_Int32 strLength, sal_Int32 fromIndex = 0)
988 const
990 return rtl_str_shortenedCompare_WithLength(
991 pData->buffer + fromIndex, pData->length - fromIndex,
992 str, strLength, strLength) == 0;
995 // This overload is left undefined, to detect calls of matchL that
996 // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
997 // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
998 // platforms):
999 #if SAL_TYPES_SIZEOFLONG == 8
1000 void matchL(char const *, sal_Int32, rtl_TextEncoding) const;
1001 #endif
1004 Match against a substring appearing in this string, ignoring the case of
1005 ASCII letters.
1007 The result is true if and only if the second string appears as a substring
1008 of this string, at the given position.
1009 Character values between 65 and 90 (ASCII A-Z) are interpreted as
1010 values between 97 and 122 (ASCII a-z).
1011 This function can't be used for language specific comparison.
1013 @param str the object (substring) to be compared.
1014 @param fromIndex the index to start the comparison from.
1015 The index must be greater or equal than 0
1016 and less or equal as the string length.
1017 @return true if str match with the characters in the string
1018 at the given position;
1019 false, otherwise.
1021 #if defined LIBO_INTERNAL_ONLY
1022 bool matchIgnoreAsciiCase( std::string_view str, sal_Int32 fromIndex = 0 ) const
1024 return rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1025 str.data(), str.size(),
1026 str.size() ) == 0;
1028 #else
1029 bool matchIgnoreAsciiCase( const OString & str, sal_Int32 fromIndex = 0 ) const
1031 return rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1032 str.pData->buffer, str.pData->length,
1033 str.pData->length ) == 0;
1035 #endif
1037 @overload
1038 This function accepts an ASCII string literal as its argument.
1039 @since LibreOffice 3.6
1041 template< typename T >
1042 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type matchIgnoreAsciiCase( T& literal, sal_Int32 fromIndex = 0 ) const
1044 RTL_STRING_CONST_FUNCTION
1045 assert(
1046 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1047 return
1048 rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
1049 pData->buffer+fromIndex, pData->length-fromIndex,
1050 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1051 literal),
1052 libreoffice_internal::ConstCharArrayDetector<T>::length,
1053 libreoffice_internal::ConstCharArrayDetector<T>::length)
1054 == 0;
1058 Check whether this string starts with a given substring.
1060 @param str the substring to be compared
1062 @param rest if non-null, and this function returns true, then assign a
1063 copy of the remainder of this string to *rest. Available since
1064 LibreOffice 4.2
1066 @return true if and only if the given str appears as a substring at the
1067 start of this string
1069 @since LibreOffice 4.0
1071 #if defined LIBO_INTERNAL_ONLY
1072 bool startsWith(std::string_view str, OString * rest = NULL) const {
1073 bool b = match(str);
1074 if (b && rest != NULL) {
1075 *rest = copy(str.size());
1077 return b;
1079 #else
1080 bool startsWith(OString const & str, OString * rest = NULL) const {
1081 bool b = match(str);
1082 if (b && rest != NULL) {
1083 *rest = copy(str.getLength());
1085 return b;
1087 #endif
1090 @overload
1091 This function accepts an ASCII string literal as its argument.
1092 @since LibreOffice 4.0
1094 template< typename T >
1095 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type startsWith(
1096 T & literal, OString * rest = NULL) const
1098 RTL_STRING_CONST_FUNCTION
1099 bool b = match(literal, 0);
1100 if (b && rest != NULL) {
1101 *rest = copy(
1102 libreoffice_internal::ConstCharArrayDetector<T>::length);
1104 return b;
1108 Check whether this string starts with a given string, ignoring the case of
1109 ASCII letters.
1111 Character values between 65 and 90 (ASCII A-Z) are interpreted as
1112 values between 97 and 122 (ASCII a-z).
1113 This function can't be used for language specific comparison.
1115 @param str the substring to be compared
1117 @param rest if non-null, and this function returns true, then assign a
1118 copy of the remainder of this string to *rest.
1120 @return true if and only if the given str appears as a substring at the
1121 start of this string, ignoring the case of ASCII letters ("A"--"Z" and
1122 "a"--"z")
1124 @since LibreOffice 5.1
1126 #if defined LIBO_INTERNAL_ONLY
1127 bool startsWithIgnoreAsciiCase(std::string_view str, OString * rest = NULL)
1128 const
1130 bool b = matchIgnoreAsciiCase(str);
1131 if (b && rest != NULL) {
1132 *rest = copy(str.size());
1134 return b;
1136 #else
1137 bool startsWithIgnoreAsciiCase(OString const & str, OString * rest = NULL)
1138 const
1140 bool b = matchIgnoreAsciiCase(str);
1141 if (b && rest != NULL) {
1142 *rest = copy(str.getLength());
1144 return b;
1146 #endif
1149 @overload
1150 This function accepts an ASCII string literal as its argument.
1151 @since LibreOffice 5.1
1153 template< typename T >
1154 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type
1155 startsWithIgnoreAsciiCase(T & literal, OString * rest = NULL) const
1157 RTL_STRING_CONST_FUNCTION
1158 assert(
1159 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1160 bool b = matchIgnoreAsciiCase(literal);
1161 if (b && rest != NULL) {
1162 *rest = copy(
1163 libreoffice_internal::ConstCharArrayDetector<T>::length);
1165 return b;
1169 Check whether this string ends with a given substring.
1171 @param str the substring to be compared
1173 @param rest if non-null, and this function returns true, then assign a
1174 copy of the remainder of this string to *rest. Available since
1175 LibreOffice 4.2
1177 @return true if and only if the given str appears as a substring at the
1178 end of this string
1180 @since LibreOffice 3.6
1182 #if defined LIBO_INTERNAL_ONLY
1183 bool endsWith(std::string_view str, OString * rest = NULL) const {
1184 bool b = str.size() <= sal_uInt32(getLength())
1185 && match(str, getLength() - str.size());
1186 if (b && rest != NULL) {
1187 *rest = copy(0, getLength() - str.size());
1189 return b;
1191 #else
1192 bool endsWith(OString const & str, OString * rest = NULL) const {
1193 bool b = str.getLength() <= getLength()
1194 && match(str, getLength() - str.getLength());
1195 if (b && rest != NULL) {
1196 *rest = copy(0, getLength() - str.getLength());
1198 return b;
1200 #endif
1203 @overload
1204 This function accepts an ASCII string literal as its argument.
1205 @since LibreOffice 3.6
1207 template< typename T >
1208 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type endsWith(
1209 T & literal, OString * rest = NULL) const
1211 RTL_STRING_CONST_FUNCTION
1212 assert(
1213 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1214 bool b
1215 = (libreoffice_internal::ConstCharArrayDetector<T>::length
1216 <= sal_uInt32(getLength()))
1217 && match(
1218 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1219 literal),
1220 (getLength()
1221 - libreoffice_internal::ConstCharArrayDetector<T>::length));
1222 if (b && rest != NULL) {
1223 *rest = copy(
1225 (getLength()
1226 - libreoffice_internal::ConstCharArrayDetector<T>::length));
1228 return b;
1232 Check whether this string ends with a given substring.
1234 @param str the substring to be compared; must not be null and must point
1235 to memory of at least strLength bytes
1237 @param strLength the length of the substring; must be non-negative
1239 @return true if and only if the given str appears as a substring at the
1240 end of this string
1242 @since LibreOffice 3.6
1244 bool endsWithL(char const * str, sal_Int32 strLength) const {
1245 return strLength <= getLength()
1246 && matchL(str, strLength, getLength() - strLength);
1249 friend bool operator == ( const OString& rStr1, const OString& rStr2 )
1250 { return rStr1.equals(rStr2); }
1251 friend bool operator != ( const OString& rStr1, const OString& rStr2 )
1252 { return !(operator == ( rStr1, rStr2 )); }
1253 friend bool operator < ( const OString& rStr1, const OString& rStr2 )
1254 { return rStr1.compareTo( rStr2 ) < 0; }
1255 friend bool operator > ( const OString& rStr1, const OString& rStr2 )
1256 { return rStr1.compareTo( rStr2 ) > 0; }
1257 friend bool operator <= ( const OString& rStr1, const OString& rStr2 )
1258 { return rStr1.compareTo( rStr2 ) <= 0; }
1259 friend bool operator >= ( const OString& rStr1, const OString& rStr2 )
1260 { return rStr1.compareTo( rStr2 ) >= 0; }
1262 template< typename T >
1263 friend typename libreoffice_internal::CharPtrDetector< T, bool >::Type operator==( const OString& rStr1, const T& value )
1265 return
1266 rtl_str_compare_WithLength(
1267 rStr1.getStr(), rStr1.getLength(), value, rtl_str_getLength(value))
1268 == 0;
1271 template< typename T >
1272 friend typename libreoffice_internal::NonConstCharArrayDetector< T, bool >::Type operator==( const OString& rStr1, T& value )
1274 return
1275 rtl_str_compare_WithLength(
1276 rStr1.getStr(), rStr1.getLength(), value, rtl_str_getLength(value))
1277 == 0;
1280 template< typename T >
1281 friend typename libreoffice_internal::CharPtrDetector< T, bool >::Type operator==( const T& value, const OString& rStr2 )
1283 return
1284 rtl_str_compare_WithLength(
1285 value, rtl_str_getLength(value), rStr2.getStr(), rStr2.getLength())
1286 == 0;
1289 template< typename T >
1290 friend typename libreoffice_internal::NonConstCharArrayDetector< T, bool >::Type operator==( T& value, const OString& rStr2 )
1292 return
1293 rtl_str_compare_WithLength(
1294 value, rtl_str_getLength(value), rStr2.getStr(), rStr2.getLength())
1295 == 0;
1299 @overload
1300 This function accepts an ASCII string literal as its argument.
1301 @since LibreOffice 3.6
1303 template< typename T >
1304 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator==( const OString& rStr, T& literal )
1306 RTL_STRING_CONST_FUNCTION
1307 assert(
1308 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1309 return
1310 (rStr.getLength()
1311 == libreoffice_internal::ConstCharArrayDetector<T>::length)
1312 && (rtl_str_compare_WithLength(
1313 rStr.pData->buffer, rStr.pData->length,
1314 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1315 literal),
1316 libreoffice_internal::ConstCharArrayDetector<T>::length)
1317 == 0);
1321 @overload
1322 This function accepts an ASCII string literal as its argument.
1323 @since LibreOffice 3.6
1325 template< typename T >
1326 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator==( T& literal, const OString& rStr )
1328 RTL_STRING_CONST_FUNCTION
1329 assert(
1330 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1331 return
1332 (rStr.getLength()
1333 == libreoffice_internal::ConstCharArrayDetector<T>::length)
1334 && (rtl_str_compare_WithLength(
1335 rStr.pData->buffer, rStr.pData->length,
1336 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1337 literal),
1338 libreoffice_internal::ConstCharArrayDetector<T>::length)
1339 == 0);
1342 template< typename T >
1343 friend typename libreoffice_internal::CharPtrDetector< T, bool >::Type operator!=( const OString& rStr1, const T& value )
1345 return !(operator == ( rStr1, value ));
1348 template< typename T >
1349 friend typename libreoffice_internal::NonConstCharArrayDetector< T, bool >::Type operator!=( const OString& rStr1, T& value )
1351 return !(operator == ( rStr1, value ));
1354 template< typename T >
1355 friend typename libreoffice_internal::CharPtrDetector< T, bool >::Type operator!=( const T& value, const OString& rStr2 )
1357 return !(operator == ( value, rStr2 ));
1360 template< typename T >
1361 friend typename libreoffice_internal::NonConstCharArrayDetector< T, bool >::Type operator!=( T& value, const OString& rStr2 )
1363 return !(operator == ( value, rStr2 ));
1367 @overload
1368 This function accepts an ASCII string literal as its argument.
1369 @since LibreOffice 3.6
1371 template< typename T >
1372 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator!=( const OString& rStr, T& literal )
1374 return !( rStr == literal );
1378 @overload
1379 This function accepts an ASCII string literal as its argument.
1380 @since LibreOffice 3.6
1382 template< typename T >
1383 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator!=( T& literal, const OString& rStr )
1385 return !( literal == rStr );
1389 Returns a hashcode for this string.
1391 @return a hash code value for this object.
1393 @see rtl::OStringHash for convenient use of std::unordered_map
1395 sal_Int32 hashCode() const
1397 return rtl_str_hashCode_WithLength( pData->buffer, pData->length );
1401 Returns the index within this string of the first occurrence of the
1402 specified character, starting the search at the specified index.
1404 @param ch character to be located.
1405 @param fromIndex the index to start the search from.
1406 The index must be greater or equal than 0
1407 and less or equal as the string length.
1408 @return the index of the first occurrence of the character in the
1409 character sequence represented by this string that is
1410 greater than or equal to fromIndex, or
1411 -1 if the character does not occur.
1413 sal_Int32 indexOf( char ch, sal_Int32 fromIndex = 0 ) const
1415 sal_Int32 ret = rtl_str_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch );
1416 return (ret < 0 ? ret : ret+fromIndex);
1420 Returns the index within this string of the last occurrence of the
1421 specified character, searching backward starting at the end.
1423 @param ch character to be located.
1424 @return the index of the last occurrence of the character in the
1425 character sequence represented by this string, or
1426 -1 if the character does not occur.
1428 sal_Int32 lastIndexOf( char ch ) const
1430 return rtl_str_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch );
1434 Returns the index within this string of the last occurrence of the
1435 specified character, searching backward starting before the specified
1436 index.
1438 @param ch character to be located.
1439 @param fromIndex the index before which to start the search.
1440 @return the index of the last occurrence of the character in the
1441 character sequence represented by this string that
1442 is less than fromIndex, or -1
1443 if the character does not occur before that point.
1445 sal_Int32 lastIndexOf( char ch, sal_Int32 fromIndex ) const
1447 return rtl_str_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch );
1451 Returns the index within this string of the first occurrence of the
1452 specified substring, starting at the specified index.
1454 If str doesn't include any character, always -1 is
1455 returned. This is also the case, if both strings are empty.
1457 @param str the substring to search for.
1458 @param fromIndex the index to start the search from.
1459 @return If the string argument occurs one or more times as a substring
1460 within this string at the starting index, then the index
1461 of the first character of the first such substring is
1462 returned. If it does not occur as a substring starting
1463 at fromIndex or beyond, -1 is returned.
1465 #if defined LIBO_INTERNAL_ONLY
1466 sal_Int32 indexOf( std::string_view str, sal_Int32 fromIndex = 0 ) const
1468 sal_Int32 ret = rtl_str_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1469 str.data(), str.size() );
1470 return (ret < 0 ? ret : ret+fromIndex);
1472 #else
1473 sal_Int32 indexOf( const OString & str, sal_Int32 fromIndex = 0 ) const
1475 sal_Int32 ret = rtl_str_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1476 str.pData->buffer, str.pData->length );
1477 return (ret < 0 ? ret : ret+fromIndex);
1479 #endif
1481 @overload
1482 This function accepts an ASCII string literal as its argument.
1483 @since LibreOffice 3.6
1485 template< typename T >
1486 typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const
1488 RTL_STRING_CONST_FUNCTION
1489 assert(
1490 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1491 sal_Int32 n = rtl_str_indexOfStr_WithLength(
1492 pData->buffer + fromIndex, pData->length - fromIndex,
1493 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1494 libreoffice_internal::ConstCharArrayDetector<T>::length);
1495 return n < 0 ? n : n + fromIndex;
1499 Returns the index within this string of the first occurrence of the
1500 specified substring, starting at the specified index.
1502 If str doesn't include any character, always -1 is
1503 returned. This is also the case, if both strings are empty.
1505 @param str the substring to search for.
1506 @param len the length of the substring.
1507 @param fromIndex the index to start the search from.
1508 @return If the string argument occurs one or more times as a substring
1509 within this string at the starting index, then the index
1510 of the first character of the first such substring is
1511 returned. If it does not occur as a substring starting
1512 at fromIndex or beyond, -1 is returned.
1514 @since LibreOffice 3.6
1516 sal_Int32 indexOfL(char const * str, sal_Int32 len, sal_Int32 fromIndex = 0)
1517 const
1519 sal_Int32 n = rtl_str_indexOfStr_WithLength(
1520 pData->buffer + fromIndex, pData->length - fromIndex, str, len);
1521 return n < 0 ? n : n + fromIndex;
1524 // This overload is left undefined, to detect calls of indexOfL that
1525 // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
1526 // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
1527 // platforms):
1528 #if SAL_TYPES_SIZEOFLONG == 8
1529 void indexOfL(char const *, sal_Int32, rtl_TextEncoding) const;
1530 #endif
1533 Returns the index within this string of the last occurrence of
1534 the specified substring, searching backward starting at the end.
1536 The returned index indicates the starting index of the substring
1537 in this string.
1538 If str doesn't include any character, always -1 is
1539 returned. This is also the case, if both strings are empty.
1541 @param str the substring to search for.
1542 @return If the string argument occurs one or more times as a substring
1543 within this string, then the index of the first character of
1544 the last such substring is returned. If it does not occur as
1545 a substring, -1 is returned.
1547 #if defined LIBO_INTERNAL_ONLY
1548 sal_Int32 lastIndexOf( std::string_view str ) const
1550 return rtl_str_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1551 str.data(), str.size() );
1553 #else
1554 sal_Int32 lastIndexOf( const OString & str ) const
1556 return rtl_str_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1557 str.pData->buffer, str.pData->length );
1559 #endif
1562 Returns the index within this string of the last occurrence of
1563 the specified substring, searching backward starting before the specified
1564 index.
1566 The returned index indicates the starting index of the substring
1567 in this string.
1568 If str doesn't include any character, always -1 is
1569 returned. This is also the case, if both strings are empty.
1571 @param str the substring to search for.
1572 @param fromIndex the index before which to start the search.
1573 @return If the string argument occurs one or more times as a substring
1574 within this string before the starting index, then the index
1575 of the first character of the last such substring is
1576 returned. Otherwise, -1 is returned.
1578 #if defined LIBO_INTERNAL_ONLY
1579 sal_Int32 lastIndexOf( std::string_view str, sal_Int32 fromIndex ) const
1581 return rtl_str_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1582 str.data(), str.size() );
1584 #else
1585 sal_Int32 lastIndexOf( const OString & str, sal_Int32 fromIndex ) const
1587 return rtl_str_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1588 str.pData->buffer, str.pData->length );
1590 #endif
1593 Returns a new string that is a substring of this string.
1595 The substring begins at the specified beginIndex. If
1596 beginIndex is negative or be greater than the length of
1597 this string, behaviour is undefined.
1599 @param beginIndex the beginning index, inclusive.
1600 @return the specified substring.
1602 SAL_WARN_UNUSED_RESULT OString copy( sal_Int32 beginIndex ) const
1604 return copy(beginIndex, getLength() - beginIndex);
1608 Returns a new string that is a substring of this string.
1610 The substring begins at the specified beginIndex and contains count
1611 characters. If either beginIndex or count are negative,
1612 or beginIndex + count are greater than the length of this string
1613 then behaviour is undefined.
1615 @param beginIndex the beginning index, inclusive.
1616 @param count the number of characters.
1617 @return the specified substring.
1619 SAL_WARN_UNUSED_RESULT OString copy( sal_Int32 beginIndex, sal_Int32 count ) const
1621 rtl_String *pNew = NULL;
1622 rtl_string_newFromSubString( &pNew, pData, beginIndex, count );
1623 return OString( pNew, SAL_NO_ACQUIRE );
1626 #if defined LIBO_INTERNAL_ONLY
1628 Returns a std::string_view that is a view of a substring of this string.
1630 The substring begins at the specified beginIndex. If
1631 beginIndex is negative or be greater than the length of
1632 this string, behaviour is undefined.
1634 @param beginIndex the beginning index, inclusive.
1635 @return the specified substring.
1637 SAL_WARN_UNUSED_RESULT std::string_view subView( sal_Int32 beginIndex ) const
1639 assert(beginIndex >= 0);
1640 assert(beginIndex <= getLength());
1641 return subView(beginIndex, getLength() - beginIndex);
1645 Returns a std::string_view that is a view of a substring of this string.
1647 The substring begins at the specified beginIndex and contains count
1648 characters. If either beginIndex or count are negative,
1649 or beginIndex + count are greater than the length of this string
1650 then behaviour is undefined.
1652 @param beginIndex the beginning index, inclusive.
1653 @param count the number of characters.
1654 @return the specified substring.
1656 SAL_WARN_UNUSED_RESULT std::string_view subView( sal_Int32 beginIndex, sal_Int32 count ) const
1658 assert(beginIndex >= 0);
1659 assert(count >= 0);
1660 assert(beginIndex <= getLength());
1661 assert(count <= getLength() - beginIndex);
1662 return std::string_view(*this).substr(beginIndex, count);
1664 #endif
1666 #ifndef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
1668 Concatenates the specified string to the end of this string.
1670 @param str the string that is concatenated to the end
1671 of this string.
1672 @return a string that represents the concatenation of this string
1673 followed by the string argument.
1675 SAL_WARN_UNUSED_RESULT OString concat( const OString & str ) const
1677 rtl_String* pNew = NULL;
1678 rtl_string_newConcat( &pNew, pData, str.pData );
1679 return OString( pNew, SAL_NO_ACQUIRE );
1681 #endif
1683 #ifndef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
1684 friend OString operator+( const OString & str1, const OString & str2 )
1686 return str1.concat( str2 );
1688 #endif
1690 // hide this from internal code to avoid ambiguous lookup error
1691 #ifndef LIBO_INTERNAL_ONLY
1693 Returns a new string resulting from replacing n = count characters
1694 from position index in this string with newStr.
1696 @param index the replacing index in str.
1697 The index must be greater or equal as 0 and
1698 less or equal as the length of the string.
1699 @param count the count of characters that will replaced
1700 The count must be greater or equal as 0 and
1701 less or equal as the length of the string minus index.
1702 @param newStr the new substring.
1703 @return the new string.
1705 SAL_WARN_UNUSED_RESULT OString replaceAt( sal_Int32 index, sal_Int32 count, const OString& newStr ) const
1707 rtl_String* pNew = NULL;
1708 rtl_string_newReplaceStrAt( &pNew, pData, index, count, newStr.pData );
1709 return OString( pNew, SAL_NO_ACQUIRE );
1711 #endif
1713 #ifdef LIBO_INTERNAL_ONLY
1714 SAL_WARN_UNUSED_RESULT OString replaceAt( sal_Int32 index, sal_Int32 count, std::string_view newStr ) const
1716 rtl_String* pNew = NULL;
1717 rtl_string_newReplaceStrAt_WithLength ( &pNew, pData, index, count, newStr.data(), newStr.size() );
1718 return OString( pNew, SAL_NO_ACQUIRE );
1720 #endif
1723 Returns a new string resulting from replacing all occurrences of
1724 oldChar in this string with newChar.
1726 If the character oldChar does not occur in the character sequence
1727 represented by this object, then the string is assigned with
1728 str.
1730 @param oldChar the old character.
1731 @param newChar the new character.
1732 @return a string derived from this string by replacing every
1733 occurrence of oldChar with newChar.
1735 SAL_WARN_UNUSED_RESULT OString replace( char oldChar, char newChar ) const
1737 rtl_String* pNew = NULL;
1738 rtl_string_newReplace( &pNew, pData, oldChar, newChar );
1739 return OString( pNew, SAL_NO_ACQUIRE );
1743 Returns a new string resulting from replacing the first occurrence of a
1744 given substring with another substring.
1746 @param from the substring to be replaced
1748 @param to the replacing substring
1750 @param[in,out] index pointer to a start index; if the pointer is
1751 non-null: upon entry to the function, its value is the index into the this
1752 string at which to start searching for the \p from substring, the value
1753 must be non-negative and not greater than this string's length; upon exit
1754 from the function its value is the index into this string at which the
1755 replacement took place or -1 if no replacement took place; if the pointer
1756 is null, searching always starts at index 0
1758 @since LibreOffice 3.6
1760 SAL_WARN_UNUSED_RESULT OString replaceFirst(
1761 OString const & from, OString const & to, sal_Int32 * index = NULL) const
1763 rtl_String * s = NULL;
1764 sal_Int32 i = 0;
1765 rtl_string_newReplaceFirst(
1766 &s, pData, from.pData->buffer, from.pData->length,
1767 to.pData->buffer, to.pData->length, index == NULL ? &i : index);
1768 return OString(s, SAL_NO_ACQUIRE);
1772 Returns a new string resulting from replacing all occurrences of a given
1773 substring with another substring.
1775 Replacing subsequent occurrences picks up only after a given replacement.
1776 That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
1778 @param from the substring to be replaced
1780 @param to the replacing substring
1782 @since LibreOffice 3.6
1784 SAL_WARN_UNUSED_RESULT OString replaceAll(OString const & from, OString const & to) const {
1785 rtl_String * s = NULL;
1786 rtl_string_newReplaceAll(
1787 &s, pData, from.pData->buffer, from.pData->length,
1788 to.pData->buffer, to.pData->length);
1789 return OString(s, SAL_NO_ACQUIRE);
1793 Converts from this string all ASCII uppercase characters (65-90)
1794 to ASCII lowercase characters (97-122).
1796 This function can't be used for language specific conversion.
1797 If the string doesn't contain characters which must be converted,
1798 then the new string is assigned with str.
1800 @return the string, converted to ASCII lowercase.
1802 SAL_WARN_UNUSED_RESULT OString toAsciiLowerCase() const
1804 rtl_String* pNew = NULL;
1805 rtl_string_newToAsciiLowerCase( &pNew, pData );
1806 return OString( pNew, SAL_NO_ACQUIRE );
1810 Converts from this string all ASCII lowercase characters (97-122)
1811 to ASCII uppercase characters (65-90).
1813 This function can't be used for language specific conversion.
1814 If the string doesn't contain characters which must be converted,
1815 then the new string is assigned with str.
1817 @return the string, converted to ASCII uppercase.
1819 SAL_WARN_UNUSED_RESULT OString toAsciiUpperCase() const
1821 rtl_String* pNew = NULL;
1822 rtl_string_newToAsciiUpperCase( &pNew, pData );
1823 return OString( pNew, SAL_NO_ACQUIRE );
1827 Returns a new string resulting from removing white space from both ends
1828 of the string.
1830 All characters that have codes less than or equal to
1831 32 (the space character) are considered to be white space.
1832 If the string doesn't contain white spaces at both ends,
1833 then the new string is assigned with str.
1835 @return the string, with white space removed from the front and end.
1837 SAL_WARN_UNUSED_RESULT OString trim() const
1839 rtl_String* pNew = NULL;
1840 rtl_string_newTrim( &pNew, pData );
1841 return OString( pNew, SAL_NO_ACQUIRE );
1845 Returns a token in the string.
1847 Example:
1848 sal_Int32 nIndex = 0;
1852 OString aToken = aStr.getToken( 0, ';', nIndex );
1855 while ( nIndex >= 0 );
1857 @param token the number of the token to return.
1858 @param cTok the character which separate the tokens.
1859 @param index the position at which the token is searched in the
1860 string.
1861 The index must not be greater than the length of the
1862 string.
1863 This param is set to the position of the
1864 next token or to -1, if it is the last token.
1865 @return the token; if either token or index is negative, an empty token
1866 is returned (and index is set to -1)
1868 OString getToken( sal_Int32 token, char cTok, sal_Int32& index ) const
1870 rtl_String * pNew = NULL;
1871 index = rtl_string_getToken( &pNew, pData, token, cTok, index );
1872 return OString( pNew, SAL_NO_ACQUIRE );
1876 Returns a token from the string.
1878 The same as getToken(sal_Int32, char, sal_Int32 &), but always passing
1879 in 0 as the start index in the third argument.
1881 @param count the number of the token to return, starting with 0
1882 @param separator the character which separates the tokens
1884 @return the given token, or an empty string
1886 @since LibreOffice 3.6
1888 OString getToken(sal_Int32 count, char separator) const {
1889 sal_Int32 n = 0;
1890 return getToken(count, separator, n);
1894 Returns the Boolean value from this string.
1896 This function can't be used for language specific conversion.
1898 @return true, if the string is 1 or "True" in any ASCII case.
1899 false in any other case.
1901 bool toBoolean() const
1903 return rtl_str_toBoolean( pData->buffer );
1907 Returns the first character from this string.
1909 @return the first character from this string or 0, if this string
1910 is empty.
1912 char toChar() const
1914 return pData->buffer[0];
1918 Returns the int32 value from this string.
1920 This function can't be used for language specific conversion.
1922 @param radix the radix (between 2 and 36)
1923 @return the int32 represented from this string.
1924 0 if this string represents no number or one of too large
1925 magnitude.
1927 sal_Int32 toInt32( sal_Int16 radix = 10 ) const
1929 return rtl_str_toInt32( pData->buffer, radix );
1933 Returns the uint32 value from this string.
1935 This function can't be used for language specific conversion.
1937 @param radix the radix (between 2 and 36)
1938 @return the uint32 represented from this string.
1939 0 if this string represents no number or one of too large
1940 magnitude.
1942 @since LibreOffice 4.2
1944 sal_uInt32 toUInt32( sal_Int16 radix = 10 ) const
1946 return rtl_str_toUInt32( pData->buffer, radix );
1950 Returns the int64 value from this string.
1952 This function can't be used for language specific conversion.
1954 @param radix the radix (between 2 and 36)
1955 @return the int64 represented from this string.
1956 0 if this string represents no number or one of too large
1957 magnitude.
1959 sal_Int64 toInt64( sal_Int16 radix = 10 ) const
1961 return rtl_str_toInt64( pData->buffer, radix );
1965 Returns the uint64 value from this string.
1967 This function can't be used for language specific conversion.
1969 @param radix the radix (between 2 and 36)
1970 @return the uint64 represented from this string.
1971 0 if this string represents no number or one of too large
1972 magnitude.
1974 @since LibreOffice 4.1
1976 sal_uInt64 toUInt64( sal_Int16 radix = 10 ) const
1978 return rtl_str_toUInt64( pData->buffer, radix );
1982 Returns the float value from this string.
1984 This function can't be used for language specific conversion.
1986 @return the float represented from this string.
1987 0.0 if this string represents no number.
1989 float toFloat() const
1991 return rtl_str_toFloat( pData->buffer );
1995 Returns the double value from this string.
1997 This function can't be used for language specific conversion.
1999 @return the double represented from this string.
2000 0.0 if this string represents no number.
2002 double toDouble() const
2004 return rtl_str_toDouble( pData->buffer );
2007 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
2009 static auto number( int i, sal_Int16 radix = 10 )
2011 return OStringNumber<RTL_STR_MAX_VALUEOFINT32>(rtl_str_valueOfInt32, i, radix);
2013 static auto number( long long ll, sal_Int16 radix = 10 )
2015 return OStringNumber<RTL_STR_MAX_VALUEOFINT64>(rtl_str_valueOfInt64, ll, radix);
2017 static auto number( unsigned long long ll, sal_Int16 radix = 10 )
2019 return OStringNumber<RTL_STR_MAX_VALUEOFUINT64>(rtl_str_valueOfUInt64, ll, radix);
2021 static auto number( unsigned int i, sal_Int16 radix = 10 )
2023 return number( static_cast< unsigned long long >( i ), radix );
2025 static auto number( long i, sal_Int16 radix = 10)
2027 return number( static_cast< long long >( i ), radix );
2029 static auto number( unsigned long i, sal_Int16 radix = 10 )
2031 return number( static_cast< unsigned long long >( i ), radix );
2033 #else
2035 Returns the string representation of the integer argument.
2037 This function can't be used for language specific conversion.
2039 @param i an integer value
2040 @param radix the radix (between 2 and 36)
2041 @return a string with the string representation of the argument.
2042 @since LibreOffice 4.1
2044 static OString number( int i, sal_Int16 radix = 10 )
2046 char aBuf[RTL_STR_MAX_VALUEOFINT32];
2047 return OString(aBuf, rtl_str_valueOfInt32(aBuf, i, radix));
2049 /// @overload
2050 /// @since LibreOffice 4.1
2051 static OString number( unsigned int i, sal_Int16 radix = 10 )
2053 return number( static_cast< unsigned long long >( i ), radix );
2055 /// @overload
2056 /// @since LibreOffice 4.1
2057 static OString number( long i, sal_Int16 radix = 10 )
2059 return number( static_cast< long long >( i ), radix );
2061 /// @overload
2062 /// @since LibreOffice 4.1
2063 static OString number( unsigned long i, sal_Int16 radix = 10 )
2065 return number( static_cast< unsigned long long >( i ), radix );
2067 /// @overload
2068 /// @since LibreOffice 4.1
2069 static OString number( long long ll, sal_Int16 radix = 10 )
2071 char aBuf[RTL_STR_MAX_VALUEOFINT64];
2072 return OString(aBuf, rtl_str_valueOfInt64(aBuf, ll, radix));
2074 /// @overload
2075 /// @since LibreOffice 4.1
2076 static OString number( unsigned long long ll, sal_Int16 radix = 10 )
2078 char aBuf[RTL_STR_MAX_VALUEOFUINT64];
2079 return OString(aBuf, rtl_str_valueOfUInt64(aBuf, ll, radix));
2081 #endif
2084 Returns the string representation of the float argument.
2086 This function can't be used for language specific conversion.
2088 @param f a float.
2089 @return a string with the decimal representation of the argument.
2090 @since LibreOffice 4.1
2092 static OString number( float f )
2094 rtl_String* pNew = NULL;
2095 // Same as rtl::str::valueOfFP, used for rtl_str_valueOfFloat
2096 rtl_math_doubleToString(&pNew, NULL, 0, f, rtl_math_StringFormat_G,
2097 RTL_STR_MAX_VALUEOFFLOAT - SAL_N_ELEMENTS("-x.E-xxx") + 1, '.',
2098 NULL, 0, true);
2099 if (pNew == NULL)
2100 throw std::bad_alloc();
2102 return OString(pNew, SAL_NO_ACQUIRE);
2106 Returns the string representation of the double argument.
2108 This function can't be used for language specific conversion.
2110 @param d a double.
2111 @return a string with the decimal representation of the argument.
2112 @since LibreOffice 4.1
2114 static OString number( double d )
2116 rtl_String* pNew = NULL;
2117 // Same as rtl::str::valueOfFP, used for rtl_str_valueOfDouble
2118 rtl_math_doubleToString(&pNew, NULL, 0, d, rtl_math_StringFormat_G,
2119 RTL_STR_MAX_VALUEOFDOUBLE - SAL_N_ELEMENTS("-x.E-xxx") + 1, '.',
2120 NULL, 0, true);
2121 if (pNew == NULL)
2122 throw std::bad_alloc();
2124 return OString(pNew, SAL_NO_ACQUIRE);
2127 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
2128 static auto boolean(bool b)
2130 return OStringNumber<RTL_STR_MAX_VALUEOFBOOLEAN>(rtl_str_valueOfBoolean, b);
2132 #else
2134 Returns the string representation of the sal_Bool argument.
2136 If the sal_Bool is true, the string "true" is returned.
2137 If the sal_Bool is false, the string "false" is returned.
2138 This function can't be used for language specific conversion.
2140 @param b a sal_Bool.
2141 @return a string with the string representation of the argument.
2142 @deprecated use boolean()
2144 SAL_DEPRECATED("use boolean()") static OString valueOf( sal_Bool b )
2146 return boolean(b);
2150 Returns the string representation of the boolean argument.
2152 If the argument is true, the string "true" is returned.
2153 If the argument is false, the string "false" is returned.
2154 This function can't be used for language specific conversion.
2156 @param b a bool.
2157 @return a string with the string representation of the argument.
2158 @since LibreOffice 4.1
2160 static OString boolean( bool b )
2162 char aBuf[RTL_STR_MAX_VALUEOFBOOLEAN];
2163 return OString(aBuf, rtl_str_valueOfBoolean(aBuf, b));
2165 #endif
2168 Returns the string representation of the char argument.
2170 @param c a character.
2171 @return a string with the string representation of the argument.
2172 @deprecated use operator, function or constructor taking char or sal_Unicode argument
2174 SAL_DEPRECATED("convert to OString or use directly") static OString valueOf( char c )
2176 return OString( &c, 1 );
2180 Returns the string representation of the int argument.
2182 This function can't be used for language specific conversion.
2184 @param i a int32.
2185 @param radix the radix (between 2 and 36)
2186 @return a string with the string representation of the argument.
2187 @deprecated use number()
2189 SAL_DEPRECATED("use number()") static OString valueOf( sal_Int32 i, sal_Int16 radix = 10 )
2191 return number( i, radix );
2195 Returns the string representation of the long argument.
2197 This function can't be used for language specific conversion.
2199 @param ll a int64.
2200 @param radix the radix (between 2 and 36)
2201 @return a string with the string representation of the argument.
2202 @deprecated use number()
2204 SAL_DEPRECATED("use number()") static OString valueOf( sal_Int64 ll, sal_Int16 radix = 10 )
2206 return number( ll, radix );
2210 Returns the string representation of the float argument.
2212 This function can't be used for language specific conversion.
2214 @param f a float.
2215 @return a string with the string representation of the argument.
2216 @deprecated use number()
2218 SAL_DEPRECATED("use number()") static OString valueOf( float f )
2220 return number(f);
2224 Returns the string representation of the double argument.
2226 This function can't be used for language specific conversion.
2228 @param d a double.
2229 @return a string with the string representation of the argument.
2230 @deprecated use number()
2232 SAL_DEPRECATED("use number()") static OString valueOf( double d )
2234 return number(d);
2237 #if defined LIBO_INTERNAL_ONLY
2238 operator std::string_view() const { return {getStr(), sal_uInt32(getLength())}; }
2239 #endif
2241 #if defined LIBO_INTERNAL_ONLY
2242 // A wrapper for the first expression in an
2244 // OString::Concat(e1) + e2 + ...
2246 // concatenation chain, when neither of the first two e1, e2 is one of our rtl string-related
2247 // classes (so something like
2249 // OString s = "a" + (b ? std::string_view("c") : std::string_view("dd"));
2251 // would not compile):
2252 template<typename T> [[nodiscard]] static
2253 OStringConcat<OStringConcatMarker, T>
2254 Concat(T const & value) { return OStringConcat<OStringConcatMarker, T>(value); }
2256 // This overload is needed so that an argument of type 'char const[N]' ends up as
2257 // 'OStringConcat<rtl::OStringConcatMarker, char const[N]>' rather than as
2258 // 'OStringConcat<rtl::OStringConcatMarker, char[N]>':
2259 template<typename T, std::size_t N> [[nodiscard]] static
2260 OStringConcat<OStringConcatMarker, T[N]>
2261 Concat(T (& value)[N]) { return OStringConcat<OStringConcatMarker, T[N]>(value); }
2262 #endif
2265 #if defined LIBO_INTERNAL_ONLY
2266 inline bool operator ==(OString const & lhs, StringConcatenation<char> const & rhs)
2267 { return lhs == std::string_view(rhs); }
2268 inline bool operator !=(OString const & lhs, StringConcatenation<char> const & rhs)
2269 { return lhs != std::string_view(rhs); }
2270 inline bool operator ==(StringConcatenation<char> const & lhs, OString const & rhs)
2271 { return std::string_view(lhs) == rhs; }
2272 inline bool operator !=(StringConcatenation<char> const & lhs, OString const & rhs)
2273 { return std::string_view(lhs) != rhs; }
2274 #endif
2276 /* ======================================================================= */
2278 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
2281 @internal
2283 template<>
2284 struct ToStringHelper< OString >
2286 static std::size_t length( const OString& s ) { return s.getLength(); }
2287 char* operator()( char* buffer, const OString& s ) const { return addDataHelper( buffer, s.getStr(), s.getLength()); }
2291 @internal
2293 template<std::size_t N>
2294 struct ToStringHelper< OStringLiteral<N> >
2296 static constexpr std::size_t length( const OStringLiteral<N>& str ) { return str.getLength(); }
2297 char* operator()( char* buffer, const OStringLiteral<N>& str ) const { return addDataHelper( buffer, str.getStr(), str.getLength() ); }
2301 @internal
2303 template< typename charT, typename traits, typename T1, typename T2 >
2304 inline std::basic_ostream<charT, traits> & operator <<(
2305 std::basic_ostream<charT, traits> & stream, OStringConcat< T1, T2 >&& concat)
2307 return stream << OString( std::move(concat) );
2309 #endif
2312 /** A helper to use OStrings with hash maps.
2314 Instances of this class are unary function objects that can be used as
2315 hash function arguments to std::unordered_map and similar constructs.
2317 struct OStringHash
2319 /** Compute a hash code for a string.
2321 @param rString
2322 a string.
2324 @return
2325 a hash code for the string. This hash code should not be stored
2326 persistently, as its computation may change in later revisions.
2328 size_t operator()( const OString& rString ) const
2329 { return static_cast<size_t>(rString.hashCode()); }
2332 /** Equality functor for classic c-strings (i.e., null-terminated char* strings). */
2333 struct CStringEqual
2335 bool operator()( const char* p1, const char* p2) const
2336 { return rtl_str_compare(p1, p2) == 0; }
2339 /** Hashing functor for classic c-strings (i.e., null-terminated char* strings). */
2340 struct CStringHash
2342 size_t operator()(const char* p) const
2343 { return rtl_str_hashCode(p); }
2346 /* ======================================================================= */
2349 Support for rtl::OString in std::ostream (and thus in
2350 CPPUNIT_ASSERT or SAL_INFO macros, for example).
2352 @since LibreOffice 4.0
2354 template< typename charT, typename traits > std::basic_ostream<charT, traits> &
2355 operator <<(
2356 std::basic_ostream<charT, traits> & stream, OString const & rString)
2358 return stream << rString.getStr();
2359 // best effort; potentially loses data due to embedded null characters
2362 } /* Namespace */
2364 #ifdef RTL_STRING_UNITTEST
2365 namespace rtl
2367 typedef rtlunittest::OString OString;
2369 #undef RTL_STRING_CONST_FUNCTION
2370 #endif
2372 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
2373 using ::rtl::OString;
2374 using ::rtl::OStringChar;
2375 using ::rtl::Concat2View;
2376 using ::rtl::OStringHash;
2377 using ::rtl::OStringLiteral;
2378 #endif
2380 #if defined LIBO_INTERNAL_ONLY && !(defined _MSC_VER && _MSC_VER <= 1929 && defined _MANAGED)
2382 template<
2383 #if defined RTL_STRING_UNITTEST
2384 rtlunittest::
2385 #endif
2386 OStringLiteral L>
2387 constexpr
2388 #if defined RTL_STRING_UNITTEST
2389 rtlunittest::
2390 #endif
2391 OString
2392 operator ""_ostr() { return L; }
2394 template<
2395 #if defined RTL_STRING_UNITTEST
2396 rtlunittest::
2397 #endif
2398 OStringLiteral L>
2399 constexpr
2400 #if defined RTL_STRING_UNITTEST
2401 rtlunittest
2402 #else
2404 #endif
2405 ::detail::OStringHolder<L> operator ""_tstr() {
2406 return
2407 #if defined RTL_STRING_UNITTEST
2408 rtlunittest
2409 #else
2411 #endif
2412 ::detail::OStringHolder<L>();
2415 #endif
2417 /// @cond INTERNAL
2419 Make OString hashable by default for use in STL containers.
2421 @since LibreOffice 6.0
2423 #if defined LIBO_INTERNAL_ONLY
2424 namespace std {
2426 template<>
2427 struct hash<::rtl::OString>
2429 std::size_t operator()(::rtl::OString const & s) const
2431 if constexpr (sizeof(std::size_t) == 8)
2433 // return a hash that uses the full 64-bit range instead of a 32-bit value
2434 size_t n = s.getLength();
2435 for (sal_Int32 i = 0, len = s.getLength(); i < len; ++i)
2436 n = 37 * n + s[i];
2437 return n;
2439 else
2440 return std::size_t(s.hashCode());
2446 #endif
2447 /// @endcond
2449 #endif // INCLUDED_RTL_STRING_HXX
2451 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */