1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
38 #if defined LIBO_INTERNAL_ONLY
39 #include <string_view>
40 #include <type_traits>
43 #include "rtl/textenc.h"
44 #include "rtl/string.h"
45 #include "rtl/stringutils.hxx"
47 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
48 #include "config_global.h"
49 #include "rtl/stringconcat.hxx"
52 #ifdef RTL_STRING_UNITTEST
53 extern bool rtl_string_unittest_const_literal
;
54 extern bool rtl_string_unittest_const_literal_function
;
57 // The unittest uses slightly different code to help check that the proper
58 // calls are made. The class is put into a different namespace to make
59 // sure the compiler generates a different (if generating also non-inline)
60 // copy of the function and does not merge them together. The class
61 // is "brought" into the proper rtl namespace by a typedef below.
62 #ifdef RTL_STRING_UNITTEST
63 #define rtl rtlunittest
70 #ifdef RTL_STRING_UNITTEST
72 // helper macro to make functions appear more readable
73 #define RTL_STRING_CONST_FUNCTION rtl_string_unittest_const_literal_function = true;
75 #define RTL_STRING_CONST_FUNCTION
79 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
82 A wrapper dressing a string literal as a static-refcount rtl_String.
84 This class is not part of public API and is meant to be used only in LibreOffice code.
85 @since LibreOffice 4.0
87 template<std::size_t N
> class SAL_WARN_UNUSED OStringLiteral
{
88 static_assert(N
!= 0);
89 static_assert(N
- 1 <= std::numeric_limits
<sal_Int32
>::max(), "literal too long");
91 friend class OStringConstExpr
;
94 #if HAVE_CPP_CONSTEVAL
99 OStringLiteral(char const (&literal
)[N
]) {
101 assert(literal
[N
- 1] == '\0');
102 //TODO: Use C++20 constexpr std::copy_n (P0202R3):
103 for (std::size_t i
= 0; i
!= N
; ++i
) {
104 more
.buffer
[i
] = literal
[i
];
108 #if defined __cpp_char8_t
109 #if HAVE_CPP_CONSTEVAL
114 explicit OStringLiteral(char8_t
const (&literal
)[N
]) {
116 assert(literal
[N
- 1] == '\0');
117 //TODO: Use C++20 constexpr std::copy_n (P0202R3):
118 for (std::size_t i
= 0; i
!= N
; ++i
) {
119 more
.buffer
[i
] = literal
[i
];
124 constexpr sal_Int32
getLength() const { return more
.length
; }
126 constexpr char const * getStr() const SAL_RETURNS_NONNULL
{ return more
.buffer
; }
128 constexpr operator std::string_view() const { return {more
.buffer
, sal_uInt32(more
.length
)}; }
131 static constexpr void assertLayout() {
132 // These static_asserts verifying the layout compatibility with rtl_String cannot be class
133 // member declarations, as offsetof requires a complete type, so defer them to here:
134 static_assert(std::is_standard_layout_v
<OStringLiteral
>);
135 static_assert(offsetof(OStringLiteral
, str
.refCount
) == offsetof(OStringLiteral
, more
.refCount
));
136 static_assert(offsetof(OStringLiteral
, str
.length
) == offsetof(OStringLiteral
, more
.length
));
137 static_assert(offsetof(OStringLiteral
, str
.buffer
) == offsetof(OStringLiteral
, more
.buffer
));
143 oslInterlockedCount refCount
= 0x40000000; // SAL_STRING_STATIC_FLAG (sal/rtl/strimp.hxx)
144 sal_Int32 length
= N
- 1;
145 char buffer
[N
] = {}; //TODO: drop initialization for C++20 (P1331R2)
155 This is intended to be used when declaring compile-time-constant structs or arrays
156 that can be initialised from named OStringLiteral e.g.
158 constexpr OStringLiteral AAA = u"aaa";
159 constexpr OStringLiteral BBB = u"bbb";
160 constexpr OStringConstExpr FOO[] { AAA, BBB };
163 class OStringConstExpr
166 template<std::size_t N
> constexpr OStringConstExpr(OStringLiteral
<N
> const & literal
):
167 pData(const_cast<rtl_String
*>(&literal
.str
)) {}
170 template<std::size_t N
> constexpr OStringConstExpr(OStringLiteral
<N
> && literal
)
173 // no destructor necessary because we know we are pointing at a compile-time
174 // constant OStringLiteral, which bypasses ref-counting.
177 make it easier to pass to OStringBuffer and similar without casting/converting
179 constexpr std::string_view
asView() const { return std::string_view(pData
->buffer
, pData
->length
); }
181 inline operator const OString
&() const;
189 /* ======================================================================= */
192 This String class provide base functionality for C++ like 8-Bit
193 character array handling. The advantage of this class is, that it
194 handle all the memory management for you - and it do it
195 more efficient. If you assign a string to another string, the
196 data of both strings are shared (without any copy operation or
197 memory allocation) as long as you do not change the string. This class
198 stores also the length of the string, so that many operations are
199 faster as the C-str-functions.
201 This class provides only readonly string handling. So you could create
202 a string and you could only query the content from this string.
203 It provides also functionality to change the string, but this results
204 in every case in a new string instance (in the most cases with an
205 memory allocation). You don't have functionality to change the
206 content of the string. If you want to change the string content, then
207 you should use the OStringBuffer class, which provides these
208 functionalities and avoid too much memory allocation.
210 The design of this class is similar to the string classes in Java
211 and so more people should have fewer understanding problems when they
215 class SAL_WARN_UNUSED SAL_DLLPUBLIC_RTTI OString
223 New string containing no characters.
228 rtl_string_new( &pData
);
232 New string from OString.
234 @param str an OString.
236 OString( const OString
& str
)
239 rtl_string_acquire( pData
);
242 #if defined LIBO_INTERNAL_ONLY
246 @param str an OString.
247 @since LibreOffice 5.2
249 OString( OString
&& str
) noexcept
253 rtl_string_new( &str
.pData
);
258 New string from OString data.
260 @param str an OString data.
262 OString( rtl_String
* str
)
265 rtl_string_acquire( pData
);
268 /** New string from OString data without acquiring it. Takeover of ownership.
270 The SAL_NO_ACQUIRE dummy parameter is only there to distinguish this
271 from other constructors.
273 @param str an OString data.
275 OString( rtl_String
* str
, __sal_NoAcquire
)
281 New string from a single character.
283 @param value a character.
285 explicit OString( char value
)
288 rtl_string_newFromStr_WithLength( &pData
, &value
, 1 );
291 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST_CONCAT
292 // Catch inadvertent conversions to the above ctor (e.g., from sal_[u]Int8, aka [un]signed
294 OString(int) = delete;
298 New string from a character buffer array.
300 Note: The argument type is always either char* or const char*. The template is
301 used only for technical reasons, as is the second argument.
303 @param value a NULL-terminated character array.
305 template< typename T
>
306 OString( const T
& value
, typename
libreoffice_internal::CharPtrDetector
< T
, libreoffice_internal::Dummy
>::Type
= libreoffice_internal::Dummy() )
309 rtl_string_newFromStr( &pData
, value
);
312 template< typename T
>
313 OString( T
& value
, typename
libreoffice_internal::NonConstCharArrayDetector
< T
, libreoffice_internal::Dummy
>::Type
= libreoffice_internal::Dummy() )
316 rtl_string_newFromStr( &pData
, value
);
319 #if __cplusplus > 202002L // C++23 P2266R3 "Simpler implicit move"
320 template< typename T
>
321 OString( T
&& value
, typename
libreoffice_internal::NonConstCharArrayDetector
< T
, libreoffice_internal::Dummy
>::Type
= libreoffice_internal::Dummy() )
324 rtl_string_newFromStr( &pData
, value
);
329 New string from a string literal.
331 If there are any embedded \0's in the string literal, the result is undefined.
332 Use the overload that explicitly accepts length.
334 @since LibreOffice 3.6
336 @param literal a string literal
338 template< typename T
>
339 OString( T
& literal
, typename
libreoffice_internal::ConstCharArrayDetector
< T
, libreoffice_internal::Dummy
>::Type
= libreoffice_internal::Dummy() )
342 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
344 if (libreoffice_internal::ConstCharArrayDetector
<T
>::length
== 0) {
345 rtl_string_new(&pData
);
347 rtl_string_newFromLiteral(
349 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
351 libreoffice_internal::ConstCharArrayDetector
<T
>::length
, 0);
353 #ifdef RTL_STRING_UNITTEST
354 rtl_string_unittest_const_literal
= true;
359 New string from a character buffer array.
361 @param value a character array.
362 @param length the number of character which should be copied.
363 The character array length must be greater or
364 equal than this value.
366 OString( const char * value
, sal_Int32 length
)
369 rtl_string_newFromStr_WithLength( &pData
, value
, length
);
372 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
375 New string from an 8-Bit string literal.
377 @since LibreOffice 7.1
379 template<std::size_t N
> constexpr OString(OStringLiteral
<N
> const & literal
):
380 pData(const_cast<rtl_String
*>(&literal
.str
)) {}
381 template<std::size_t N
> OString(OStringLiteral
<N
> &&) = delete;
385 #if defined LIBO_INTERNAL_ONLY
386 explicit OString(std::string_view sv
) {
387 if (sv
.size() > sal_uInt32(std::numeric_limits
<sal_Int32
>::max())) {
388 throw std::bad_alloc();
391 rtl_string_newFromStr_WithLength(&pData
, sv
.data(), sv
.size());
396 New string from a Unicode character buffer array.
398 @param value a Unicode character array.
399 @param length the number of character which should be converted.
400 The Unicode character array length must be
401 greater or equal than this value.
402 @param encoding the text encoding in which the Unicode character
403 sequence should be converted.
404 @param convertFlags flags which controls the conversion.
405 see RTL_UNICODETOTEXT_FLAGS_...
407 @exception std::bad_alloc is thrown if an out-of-memory condition occurs
409 OString( const sal_Unicode
* value
, sal_Int32 length
,
410 rtl_TextEncoding encoding
,
411 sal_uInt32 convertFlags
= OUSTRING_TO_OSTRING_CVTFLAGS
)
414 rtl_uString2String( &pData
, value
, length
, encoding
, convertFlags
);
416 throw std::bad_alloc();
420 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
425 template< typename T1
, typename T2
>
426 OString( OStringConcat
< T1
, T2
>&& c
)
428 const sal_Int32 l
= c
.length();
429 pData
= rtl_string_alloc( l
);
432 char* end
= c
.addData( pData
->buffer
);
442 template< typename T
, std::size_t N
>
443 OString( StringNumberBase
< char, T
, N
>&& n
)
444 : OString( n
.buf
, n
.length
)
448 #ifdef LIBO_INTERNAL_ONLY
449 OString(std::nullptr_t
) = delete;
453 Release the string data.
457 rtl_string_release( pData
);
460 #if defined LIBO_INTERNAL_ONLY
461 /** Provides an OString const & passing a storage pointer of an
463 It is more convenient to use C++ OString member functions when dealing
464 with rtl_String * handles. Using this function avoids unnecessary
465 acquire()/release() calls for a temporary OString object.
470 OString const & based on given storage
472 static OString
const & unacquired( rtl_String
* const * ppHandle
)
473 { return * reinterpret_cast< OString
const * >( ppHandle
); }
479 @param str an OString.
481 OString
& operator=( const OString
& str
)
483 rtl_string_assign( &pData
, str
.pData
);
487 #if defined LIBO_INTERNAL_ONLY
489 Move assign a new string.
491 @param str an OString.
492 @since LibreOffice 5.2
494 OString
& operator=( OString
&& str
) noexcept
496 rtl_string_release( pData
);
499 rtl_string_new( &str
.pData
);
506 This function accepts an ASCII string literal as its argument.
507 @since LibreOffice 3.6
509 template< typename T
>
510 typename
libreoffice_internal::ConstCharArrayDetector
< T
, OString
& >::Type
operator=( T
& literal
)
512 RTL_STRING_CONST_FUNCTION
514 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
515 if (libreoffice_internal::ConstCharArrayDetector
<T
>::length
== 0) {
516 rtl_string_new(&pData
);
518 rtl_string_newFromLiteral(
520 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
522 libreoffice_internal::ConstCharArrayDetector
<T
>::length
, 0);
528 Append a string to this string.
530 @param str an OString.
532 OString
& operator+=( const OString
& str
)
533 #if defined LIBO_INTERNAL_ONLY
537 rtl_string_newConcat( &pData
, pData
, str
.pData
);
540 #if defined LIBO_INTERNAL_ONLY
541 void operator+=(OString
const &) && = delete;
544 #if defined LIBO_INTERNAL_ONLY
545 template<typename T
> typename
libreoffice_internal::CharPtrDetector
<T
, OString
&>::Type
546 operator +=(T
const & value
) & { return operator +=(std::string_view(value
)); }
547 template<typename T
> typename
libreoffice_internal::CharPtrDetector
<T
, OString
&>::Type
548 operator +=(T
const &) && = delete;
551 typename
libreoffice_internal::NonConstCharArrayDetector
<T
, OString
&>::Type
552 operator +=(T
& value
) & { return operator +=(std::string_view(value
)); }
554 typename
libreoffice_internal::NonConstCharArrayDetector
<T
, OString
&>::Type
operator +=(T
&) &&
557 template<typename T
> typename
libreoffice_internal::ConstCharArrayDetector
<T
, OString
&>::Type
558 operator +=(T
& literal
) & {
559 assert(libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
562 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
563 libreoffice_internal::ConstCharArrayDetector
<T
>::length
));
565 template<typename T
> typename
libreoffice_internal::ConstCharArrayDetector
<T
, OString
&>::Type
566 operator +=(T
&) && = delete;
568 template<std::size_t N
> OString
& operator +=(OStringLiteral
<N
> const & literal
) &
569 { return operator +=(std::string_view(literal
.getStr(), literal
.getLength())); }
570 template<std::size_t N
> void operator +=(OStringLiteral
<N
> const &) && = delete;
572 OString
& operator +=(std::string_view sv
) & {
576 if (sv
.size() > sal_uInt32(std::numeric_limits
<sal_Int32
>::max() - pData
->length
)) {
577 throw std::bad_alloc();
579 auto const l
= pData
->length
+ sv
.size();
580 rtl_string_ensureCapacity(&pData
, l
);
581 *addDataHelper(pData
->buffer
+ pData
->length
, sv
.data(), sv
.size()) = '\0';
585 void operator +=(std::string_view
) && = delete;
588 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
593 template< typename T1
, typename T2
>
594 OString
& operator+=( OStringConcat
< T1
, T2
>&& c
) & {
595 sal_Int32 l
= c
.length();
599 rtl_string_ensureCapacity( &pData
, l
);
600 char* end
= c
.addData( pData
->buffer
+ pData
->length
);
605 template<typename T1
, typename T2
> void operator +=(
606 OStringConcat
<T1
, T2
> &&) && = delete;
612 template< typename T
, std::size_t N
>
613 OString
& operator+=( StringNumberBase
< char, T
, N
>&& n
) & {
614 return operator +=(std::string_view(n
.buf
, n
.length
));
616 template<typename T
, std::size_t N
> void operator +=(
617 StringNumberBase
<char, T
, N
> &&) && = delete;
621 Clears the string, i.e, makes a zero-character string
622 @since LibreOffice 4.4
626 rtl_string_new( &pData
);
630 Returns the length of this string.
632 The length is equal to the number of characters in this string.
634 @return the length of the sequence of characters represented by this
637 sal_Int32
getLength() const { return pData
->length
; }
640 Checks if a string is empty.
642 @return true if the string is empty;
645 @since LibreOffice 3.4
649 return pData
->length
== 0;
653 Returns a pointer to the characters of this string.
655 <p>The returned pointer is guaranteed to point to a null-terminated byte
656 string. But note that this string object may contain embedded null
657 characters, which will thus also be embedded in the returned
658 null-terminated byte string.</p>
660 @return a pointer to a null-terminated byte string representing the
661 characters of this string object.
663 const char * getStr() const SAL_RETURNS_NONNULL
{ return pData
->buffer
; }
666 Access to individual characters.
668 @param index must be non-negative and less than length.
670 @return the character at the given index.
672 @since LibreOffice 3.5
674 char operator [](sal_Int32 index
) const {
675 // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
676 assert(index
>= 0 && static_cast<sal_uInt32
>(index
) < static_cast<sal_uInt32
>(getLength()));
677 return getStr()[index
];
681 Compares two strings.
683 The comparison is based on the numeric value of each character in
684 the strings and return a value indicating their relationship.
685 This function can't be used for language specific sorting.
687 @param str the object to be compared.
688 @return 0 - if both strings are equal
689 < 0 - if this string is less than the string argument
690 > 0 - if this string is greater than the string argument
692 sal_Int32
compareTo( const OString
& str
) const
694 return rtl_str_compare_WithLength( pData
->buffer
, pData
->length
,
695 str
.pData
->buffer
, str
.pData
->length
);
699 Compares two strings with an maximum count of characters.
701 The comparison is based on the numeric value of each character in
702 the strings and return a value indicating their relationship.
703 This function can't be used for language specific sorting.
705 @param rObj the object to be compared.
706 @param maxLength the maximum count of characters to be compared.
707 @return 0 - if both strings are equal
708 < 0 - if this string is less than the string argument
709 > 0 - if this string is greater than the string argument
711 sal_Int32
compareTo( const OString
& rObj
, sal_Int32 maxLength
) const
713 return rtl_str_shortenedCompare_WithLength( pData
->buffer
, pData
->length
,
714 rObj
.pData
->buffer
, rObj
.pData
->length
, maxLength
);
718 Compares two strings in reverse order.
720 The comparison is based on the numeric value of each character in
721 the strings and return a value indicating their relationship.
722 This function can't be used for language specific sorting.
724 @param str the object to be compared.
725 @return 0 - if both strings are equal
726 < 0 - if this string is less than the string argument
727 > 0 - if this string is greater than the string argument
729 sal_Int32
reverseCompareTo( const OString
& str
) const
731 return rtl_str_reverseCompare_WithLength( pData
->buffer
, pData
->length
,
732 str
.pData
->buffer
, str
.pData
->length
);
736 Perform a comparison of two strings.
738 The result is true if and only if second string
739 represents the same sequence of characters as the first string.
740 This function can't be used for language specific comparison.
742 @param str the object to be compared.
743 @return true if the strings are equal;
746 bool equals( const OString
& str
) const
748 if ( pData
->length
!= str
.pData
->length
)
750 if ( pData
== str
.pData
)
752 return rtl_str_reverseCompare_WithLength( pData
->buffer
, pData
->length
,
753 str
.pData
->buffer
, str
.pData
->length
) == 0;
757 Perform a comparison of two strings.
759 The result is true if and only if second string
760 represents the same sequence of characters as the first string.
761 The ASCII string must be greater or equal as length.
762 This function can't be used for language specific comparison.
765 @param value a character array.
766 @param length the length of the character array.
767 @return true if the strings are equal;
770 bool equalsL( const char* value
, sal_Int32 length
) const
772 if ( pData
->length
!= length
)
775 return rtl_str_reverseCompare_WithLength( pData
->buffer
, pData
->length
,
776 value
, length
) == 0;
780 Perform an ASCII lowercase comparison of two strings.
782 The result is true if and only if second string
783 represents the same sequence of characters as the first string,
785 Character values between 65 and 90 (ASCII A-Z) are interpreted as
786 values between 97 and 122 (ASCII a-z).
787 This function can't be used for language specific comparison.
789 @param str the object to be compared.
790 @return true if the strings are equal;
793 #if defined LIBO_INTERNAL_ONLY
794 bool equalsIgnoreAsciiCase( std::string_view str
) const
796 if ( sal_uInt32(pData
->length
) != str
.size() )
798 if ( pData
->buffer
== str
.data() )
800 return rtl_str_compareIgnoreAsciiCase_WithLength( pData
->buffer
, pData
->length
,
801 str
.data(), str
.size() ) == 0;
804 bool equalsIgnoreAsciiCase( const OString
& str
) const
806 if ( pData
->length
!= str
.pData
->length
)
808 if ( pData
== str
.pData
)
810 return rtl_str_compareIgnoreAsciiCase_WithLength( pData
->buffer
, pData
->length
,
811 str
.pData
->buffer
, str
.pData
->length
) == 0;
816 Perform an ASCII lowercase comparison of two strings.
818 The result is true if and only if second string
819 represents the same sequence of characters as the first string,
821 Character values between 65 and 90 (ASCII A-Z) are interpreted as
822 values between 97 and 122 (ASCII a-z).
823 Since this method is optimized for performance, the ASCII character
824 values are not converted in any way. The caller has to make sure that
825 all ASCII characters are in the allowed range between 0 and
826 127. The ASCII string must be NULL-terminated.
827 This function can't be used for language specific comparison.
829 Note: The argument type is always either char* or const char*, the return type is bool.
830 The template is used only for technical reasons.
832 @param asciiStr the 8-Bit ASCII character string to be compared.
833 @return true if the strings are equal;
836 template< typename T
>
837 typename
libreoffice_internal::CharPtrDetector
< T
, bool >::Type
equalsIgnoreAsciiCase( const T
& asciiStr
) const
839 return rtl_str_compareIgnoreAsciiCase( pData
->buffer
, asciiStr
) == 0;
842 template< typename T
>
843 typename
libreoffice_internal::NonConstCharArrayDetector
< T
, bool >::Type
equalsIgnoreAsciiCase( T
& asciiStr
) const
845 return rtl_str_compareIgnoreAsciiCase( pData
->buffer
, asciiStr
) == 0;
850 This function accepts an ASCII string literal as its argument.
851 @since LibreOffice 3.6
853 template< typename T
>
854 typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
equalsIgnoreAsciiCase( T
& literal
) const
856 RTL_STRING_CONST_FUNCTION
858 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
861 == libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
862 && (rtl_str_compareIgnoreAsciiCase_WithLength(
863 pData
->buffer
, pData
->length
,
864 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
866 libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
871 Perform an ASCII lowercase comparison of two strings.
873 The result is true if and only if second string
874 represents the same sequence of characters as the first string,
876 Character values between 65 and 90 (ASCII A-Z) are interpreted as
877 values between 97 and 122 (ASCII a-z).
878 Since this method is optimized for performance, the ASCII character
879 values are not converted in any way. The caller has to make sure that
880 all ASCII characters are in the allowed range between 0 and
881 127. The ASCII string must be greater or equal in length as asciiStrLength.
882 This function can't be used for language specific comparison.
884 @param asciiStr the 8-Bit ASCII character string to be compared.
885 @param asciiStrLength the length of the ascii string
886 @return true if the strings are equal;
889 bool equalsIgnoreAsciiCaseL( const char * asciiStr
, sal_Int32 asciiStrLength
) const
891 if ( pData
->length
!= asciiStrLength
)
894 return rtl_str_compareIgnoreAsciiCase_WithLength( pData
->buffer
, pData
->length
,
895 asciiStr
, asciiStrLength
) == 0;
899 Match against a substring appearing in this string.
901 The result is true if and only if the second string appears as a substring
902 of this string, at the given position.
903 This function can't be used for language specific comparison.
905 @param str the object (substring) to be compared.
906 @param fromIndex the index to start the comparison from.
907 The index must be greater or equal than 0
908 and less or equal as the string length.
909 @return true if str match with the characters in the string
910 at the given position;
913 #if defined LIBO_INTERNAL_ONLY
914 bool match( std::string_view str
, sal_Int32 fromIndex
= 0 ) const
916 return rtl_str_shortenedCompare_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
917 str
.data(), str
.size(), str
.size() ) == 0;
920 bool match( const OString
& str
, sal_Int32 fromIndex
= 0 ) const
922 return rtl_str_shortenedCompare_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
923 str
.pData
->buffer
, str
.pData
->length
, str
.pData
->length
) == 0;
929 This function accepts an ASCII string literal as its argument.
930 @since LibreOffice 3.6
932 template< typename T
>
933 typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
match( T
& literal
, sal_Int32 fromIndex
= 0 ) const
935 RTL_STRING_CONST_FUNCTION
937 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
939 rtl_str_shortenedCompare_WithLength(
940 pData
->buffer
+ fromIndex
, pData
->length
- fromIndex
,
941 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
943 libreoffice_internal::ConstCharArrayDetector
<T
>::length
,
944 libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
949 Match against a substring appearing in this string.
951 @param str the substring to be compared; must not be null and must point
952 to memory of at least strLength bytes
954 @param strLength the length of the substring; must be non-negative
956 @param fromIndex the index into this string to start the comparison at;
957 must be non-negative and not greater than this string's length
959 @return true if and only if the given str is contained as a substring of
960 this string at the given fromIndex
962 @since LibreOffice 3.6
965 char const * str
, sal_Int32 strLength
, sal_Int32 fromIndex
= 0)
968 return rtl_str_shortenedCompare_WithLength(
969 pData
->buffer
+ fromIndex
, pData
->length
- fromIndex
,
970 str
, strLength
, strLength
) == 0;
973 // This overload is left undefined, to detect calls of matchL that
974 // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
975 // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
977 #if SAL_TYPES_SIZEOFLONG == 8
978 void matchL(char const *, sal_Int32
, rtl_TextEncoding
) const;
982 Match against a substring appearing in this string, ignoring the case of
985 The result is true if and only if the second string appears as a substring
986 of this string, at the given position.
987 Character values between 65 and 90 (ASCII A-Z) are interpreted as
988 values between 97 and 122 (ASCII a-z).
989 This function can't be used for language specific comparison.
991 @param str the object (substring) to be compared.
992 @param fromIndex the index to start the comparison from.
993 The index must be greater or equal than 0
994 and less or equal as the string length.
995 @return true if str match with the characters in the string
996 at the given position;
999 #if defined LIBO_INTERNAL_ONLY
1000 bool matchIgnoreAsciiCase( std::string_view str
, sal_Int32 fromIndex
= 0 ) const
1002 return rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
1003 str
.data(), str
.size(),
1007 bool matchIgnoreAsciiCase( const OString
& str
, sal_Int32 fromIndex
= 0 ) const
1009 return rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
1010 str
.pData
->buffer
, str
.pData
->length
,
1011 str
.pData
->length
) == 0;
1016 This function accepts an ASCII string literal as its argument.
1017 @since LibreOffice 3.6
1019 template< typename T
>
1020 typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
matchIgnoreAsciiCase( T
& literal
, sal_Int32 fromIndex
= 0 ) const
1022 RTL_STRING_CONST_FUNCTION
1024 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
1026 rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
1027 pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
1028 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
1030 libreoffice_internal::ConstCharArrayDetector
<T
>::length
,
1031 libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
1036 Check whether this string starts with a given substring.
1038 @param str the substring to be compared
1040 @param rest if non-null, and this function returns true, then assign a
1041 copy of the remainder of this string to *rest. Available since
1044 @return true if and only if the given str appears as a substring at the
1045 start of this string
1047 @since LibreOffice 4.0
1049 #if defined LIBO_INTERNAL_ONLY
1050 bool startsWith(std::string_view str
, OString
* rest
= NULL
) const {
1051 bool b
= match(str
);
1052 if (b
&& rest
!= NULL
) {
1053 *rest
= copy(str
.size());
1058 bool startsWith(OString
const & str
, OString
* rest
= NULL
) const {
1059 bool b
= match(str
);
1060 if (b
&& rest
!= NULL
) {
1061 *rest
= copy(str
.getLength());
1069 This function accepts an ASCII string literal as its argument.
1070 @since LibreOffice 4.0
1072 template< typename T
>
1073 typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
startsWith(
1074 T
& literal
, OString
* rest
= NULL
) const
1076 RTL_STRING_CONST_FUNCTION
1077 bool b
= match(literal
, 0);
1078 if (b
&& rest
!= NULL
) {
1080 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
1086 Check whether this string starts with a given string, ignoring the case of
1089 Character values between 65 and 90 (ASCII A-Z) are interpreted as
1090 values between 97 and 122 (ASCII a-z).
1091 This function can't be used for language specific comparison.
1093 @param str the substring to be compared
1095 @param rest if non-null, and this function returns true, then assign a
1096 copy of the remainder of this string to *rest.
1098 @return true if and only if the given str appears as a substring at the
1099 start of this string, ignoring the case of ASCII letters ("A"--"Z" and
1102 @since LibreOffice 5.1
1104 #if defined LIBO_INTERNAL_ONLY
1105 bool startsWithIgnoreAsciiCase(std::string_view str
, OString
* rest
= NULL
)
1108 bool b
= matchIgnoreAsciiCase(str
);
1109 if (b
&& rest
!= NULL
) {
1110 *rest
= copy(str
.size());
1115 bool startsWithIgnoreAsciiCase(OString
const & str
, OString
* rest
= NULL
)
1118 bool b
= matchIgnoreAsciiCase(str
);
1119 if (b
&& rest
!= NULL
) {
1120 *rest
= copy(str
.getLength());
1128 This function accepts an ASCII string literal as its argument.
1129 @since LibreOffice 5.1
1131 template< typename T
>
1132 typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
1133 startsWithIgnoreAsciiCase(T
& literal
, OString
* rest
= NULL
) const
1135 RTL_STRING_CONST_FUNCTION
1137 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
1138 bool b
= matchIgnoreAsciiCase(literal
);
1139 if (b
&& rest
!= NULL
) {
1141 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
1147 Check whether this string ends with a given substring.
1149 @param str the substring to be compared
1151 @param rest if non-null, and this function returns true, then assign a
1152 copy of the remainder of this string to *rest. Available since
1155 @return true if and only if the given str appears as a substring at the
1158 @since LibreOffice 3.6
1160 #if defined LIBO_INTERNAL_ONLY
1161 bool endsWith(std::string_view str
, OString
* rest
= NULL
) const {
1162 bool b
= str
.size() <= sal_uInt32(getLength())
1163 && match(str
, getLength() - str
.size());
1164 if (b
&& rest
!= NULL
) {
1165 *rest
= copy(0, getLength() - str
.size());
1170 bool endsWith(OString
const & str
, OString
* rest
= NULL
) const {
1171 bool b
= str
.getLength() <= getLength()
1172 && match(str
, getLength() - str
.getLength());
1173 if (b
&& rest
!= NULL
) {
1174 *rest
= copy(0, getLength() - str
.getLength());
1182 This function accepts an ASCII string literal as its argument.
1183 @since LibreOffice 3.6
1185 template< typename T
>
1186 typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
endsWith(
1187 T
& literal
, OString
* rest
= NULL
) const
1189 RTL_STRING_CONST_FUNCTION
1191 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
1193 = (libreoffice_internal::ConstCharArrayDetector
<T
>::length
1194 <= sal_uInt32(getLength()))
1196 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
1199 - libreoffice_internal::ConstCharArrayDetector
<T
>::length
));
1200 if (b
&& rest
!= NULL
) {
1204 - libreoffice_internal::ConstCharArrayDetector
<T
>::length
));
1210 Check whether this string ends with a given substring.
1212 @param str the substring to be compared; must not be null and must point
1213 to memory of at least strLength bytes
1215 @param strLength the length of the substring; must be non-negative
1217 @return true if and only if the given str appears as a substring at the
1220 @since LibreOffice 3.6
1222 bool endsWithL(char const * str
, sal_Int32 strLength
) const {
1223 return strLength
<= getLength()
1224 && matchL(str
, strLength
, getLength() - strLength
);
1227 friend bool operator == ( const OString
& rStr1
, const OString
& rStr2
)
1228 { return rStr1
.equals(rStr2
); }
1229 friend bool operator != ( const OString
& rStr1
, const OString
& rStr2
)
1230 { return !(operator == ( rStr1
, rStr2
)); }
1231 friend bool operator < ( const OString
& rStr1
, const OString
& rStr2
)
1232 { return rStr1
.compareTo( rStr2
) < 0; }
1233 friend bool operator > ( const OString
& rStr1
, const OString
& rStr2
)
1234 { return rStr1
.compareTo( rStr2
) > 0; }
1235 friend bool operator <= ( const OString
& rStr1
, const OString
& rStr2
)
1236 { return rStr1
.compareTo( rStr2
) <= 0; }
1237 friend bool operator >= ( const OString
& rStr1
, const OString
& rStr2
)
1238 { return rStr1
.compareTo( rStr2
) >= 0; }
1240 template< typename T
>
1241 friend typename
libreoffice_internal::CharPtrDetector
< T
, bool >::Type
operator==( const OString
& rStr1
, const T
& value
)
1244 rtl_str_compare_WithLength(
1245 rStr1
.getStr(), rStr1
.getLength(), value
, rtl_str_getLength(value
))
1249 template< typename T
>
1250 friend typename
libreoffice_internal::NonConstCharArrayDetector
< T
, bool >::Type
operator==( const OString
& rStr1
, T
& value
)
1253 rtl_str_compare_WithLength(
1254 rStr1
.getStr(), rStr1
.getLength(), value
, rtl_str_getLength(value
))
1258 template< typename T
>
1259 friend typename
libreoffice_internal::CharPtrDetector
< T
, bool >::Type
operator==( const T
& value
, const OString
& rStr2
)
1262 rtl_str_compare_WithLength(
1263 value
, rtl_str_getLength(value
), rStr2
.getStr(), rStr2
.getLength())
1267 template< typename T
>
1268 friend typename
libreoffice_internal::NonConstCharArrayDetector
< T
, bool >::Type
operator==( T
& value
, const OString
& rStr2
)
1271 rtl_str_compare_WithLength(
1272 value
, rtl_str_getLength(value
), rStr2
.getStr(), rStr2
.getLength())
1278 This function accepts an ASCII string literal as its argument.
1279 @since LibreOffice 3.6
1281 template< typename T
>
1282 friend typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
operator==( const OString
& rStr
, T
& literal
)
1284 RTL_STRING_CONST_FUNCTION
1286 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
1289 == libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
1290 && (rtl_str_compare_WithLength(
1291 rStr
.pData
->buffer
, rStr
.pData
->length
,
1292 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
1294 libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
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==( T
& literal
, const OString
& rStr
)
1306 RTL_STRING_CONST_FUNCTION
1308 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
1311 == libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
1312 && (rtl_str_compare_WithLength(
1313 rStr
.pData
->buffer
, rStr
.pData
->length
,
1314 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
1316 libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
1320 template< typename T
>
1321 friend typename
libreoffice_internal::CharPtrDetector
< T
, bool >::Type
operator!=( const OString
& rStr1
, const T
& value
)
1323 return !(operator == ( rStr1
, value
));
1326 template< typename T
>
1327 friend typename
libreoffice_internal::NonConstCharArrayDetector
< T
, bool >::Type
operator!=( const OString
& rStr1
, T
& value
)
1329 return !(operator == ( rStr1
, value
));
1332 template< typename T
>
1333 friend typename
libreoffice_internal::CharPtrDetector
< T
, bool >::Type
operator!=( const T
& value
, const OString
& rStr2
)
1335 return !(operator == ( value
, rStr2
));
1338 template< typename T
>
1339 friend typename
libreoffice_internal::NonConstCharArrayDetector
< T
, bool >::Type
operator!=( T
& value
, const OString
& rStr2
)
1341 return !(operator == ( value
, rStr2
));
1346 This function accepts an ASCII string literal as its argument.
1347 @since LibreOffice 3.6
1349 template< typename T
>
1350 friend typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
operator!=( const OString
& rStr
, T
& literal
)
1352 return !( rStr
== literal
);
1357 This function accepts an ASCII string literal as its argument.
1358 @since LibreOffice 3.6
1360 template< typename T
>
1361 friend typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
operator!=( T
& literal
, const OString
& rStr
)
1363 return !( literal
== rStr
);
1367 Returns a hashcode for this string.
1369 @return a hash code value for this object.
1371 @see rtl::OStringHash for convenient use of std::unordered_map
1373 sal_Int32
hashCode() const
1375 return rtl_str_hashCode_WithLength( pData
->buffer
, pData
->length
);
1379 Returns the index within this string of the first occurrence of the
1380 specified character, starting the search at the specified index.
1382 @param ch character to be located.
1383 @param fromIndex the index to start the search from.
1384 The index must be greater or equal than 0
1385 and less or equal as the string length.
1386 @return the index of the first occurrence of the character in the
1387 character sequence represented by this string that is
1388 greater than or equal to fromIndex, or
1389 -1 if the character does not occur.
1391 sal_Int32
indexOf( char ch
, sal_Int32 fromIndex
= 0 ) const
1393 sal_Int32 ret
= rtl_str_indexOfChar_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
, ch
);
1394 return (ret
< 0 ? ret
: ret
+fromIndex
);
1398 Returns the index within this string of the last occurrence of the
1399 specified character, searching backward starting at the end.
1401 @param ch character to be located.
1402 @return the index of the last occurrence of the character in the
1403 character sequence represented by this string, or
1404 -1 if the character does not occur.
1406 sal_Int32
lastIndexOf( char ch
) const
1408 return rtl_str_lastIndexOfChar_WithLength( pData
->buffer
, pData
->length
, ch
);
1412 Returns the index within this string of the last occurrence of the
1413 specified character, searching backward starting before the specified
1416 @param ch character to be located.
1417 @param fromIndex the index before which to start the search.
1418 @return the index of the last occurrence of the character in the
1419 character sequence represented by this string that
1420 is less than fromIndex, or -1
1421 if the character does not occur before that point.
1423 sal_Int32
lastIndexOf( char ch
, sal_Int32 fromIndex
) const
1425 return rtl_str_lastIndexOfChar_WithLength( pData
->buffer
, fromIndex
, ch
);
1429 Returns the index within this string of the first occurrence of the
1430 specified substring, starting at the specified index.
1432 If str doesn't include any character, always -1 is
1433 returned. This is also the case, if both strings are empty.
1435 @param str the substring to search for.
1436 @param fromIndex the index to start the search from.
1437 @return If the string argument occurs one or more times as a substring
1438 within this string at the starting index, then the index
1439 of the first character of the first such substring is
1440 returned. If it does not occur as a substring starting
1441 at fromIndex or beyond, -1 is returned.
1443 #if defined LIBO_INTERNAL_ONLY
1444 sal_Int32
indexOf( std::string_view str
, sal_Int32 fromIndex
= 0 ) const
1446 sal_Int32 ret
= rtl_str_indexOfStr_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
1447 str
.data(), str
.size() );
1448 return (ret
< 0 ? ret
: ret
+fromIndex
);
1451 sal_Int32
indexOf( const OString
& str
, sal_Int32 fromIndex
= 0 ) const
1453 sal_Int32 ret
= rtl_str_indexOfStr_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
1454 str
.pData
->buffer
, str
.pData
->length
);
1455 return (ret
< 0 ? ret
: ret
+fromIndex
);
1460 This function accepts an ASCII string literal as its argument.
1461 @since LibreOffice 3.6
1463 template< typename T
>
1464 typename
libreoffice_internal::ConstCharArrayDetector
< T
, sal_Int32
>::Type
indexOf( T
& literal
, sal_Int32 fromIndex
= 0 ) const
1466 RTL_STRING_CONST_FUNCTION
1468 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
1469 sal_Int32 n
= rtl_str_indexOfStr_WithLength(
1470 pData
->buffer
+ fromIndex
, pData
->length
- fromIndex
,
1471 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
1472 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
1473 return n
< 0 ? n
: n
+ fromIndex
;
1477 Returns the index within this string of the first occurrence of the
1478 specified substring, starting at the specified index.
1480 If str doesn't include any character, always -1 is
1481 returned. This is also the case, if both strings are empty.
1483 @param str the substring to search for.
1484 @param len the length of the substring.
1485 @param fromIndex the index to start the search from.
1486 @return If the string argument occurs one or more times as a substring
1487 within this string at the starting index, then the index
1488 of the first character of the first such substring is
1489 returned. If it does not occur as a substring starting
1490 at fromIndex or beyond, -1 is returned.
1492 @since LibreOffice 3.6
1494 sal_Int32
indexOfL(char const * str
, sal_Int32 len
, sal_Int32 fromIndex
= 0)
1497 sal_Int32 n
= rtl_str_indexOfStr_WithLength(
1498 pData
->buffer
+ fromIndex
, pData
->length
- fromIndex
, str
, len
);
1499 return n
< 0 ? n
: n
+ fromIndex
;
1502 // This overload is left undefined, to detect calls of indexOfL that
1503 // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
1504 // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
1506 #if SAL_TYPES_SIZEOFLONG == 8
1507 void indexOfL(char const *, sal_Int32
, rtl_TextEncoding
) const;
1511 Returns the index within this string of the last occurrence of
1512 the specified substring, searching backward starting at the end.
1514 The returned index indicates the starting index of the substring
1516 If str doesn't include any character, always -1 is
1517 returned. This is also the case, if both strings are empty.
1519 @param str the substring to search for.
1520 @return If the string argument occurs one or more times as a substring
1521 within this string, then the index of the first character of
1522 the last such substring is returned. If it does not occur as
1523 a substring, -1 is returned.
1525 #if defined LIBO_INTERNAL_ONLY
1526 sal_Int32
lastIndexOf( std::string_view str
) const
1528 return rtl_str_lastIndexOfStr_WithLength( pData
->buffer
, pData
->length
,
1529 str
.data(), str
.size() );
1532 sal_Int32
lastIndexOf( const OString
& str
) const
1534 return rtl_str_lastIndexOfStr_WithLength( pData
->buffer
, pData
->length
,
1535 str
.pData
->buffer
, str
.pData
->length
);
1540 Returns the index within this string of the last occurrence of
1541 the specified substring, searching backward starting before the specified
1544 The returned index indicates the starting index of the substring
1546 If str doesn't include any character, always -1 is
1547 returned. This is also the case, if both strings are empty.
1549 @param str the substring to search for.
1550 @param fromIndex the index before which to start the search.
1551 @return If the string argument occurs one or more times as a substring
1552 within this string before the starting index, then the index
1553 of the first character of the last such substring is
1554 returned. Otherwise, -1 is returned.
1556 #if defined LIBO_INTERNAL_ONLY
1557 sal_Int32
lastIndexOf( std::string_view str
, sal_Int32 fromIndex
) const
1559 return rtl_str_lastIndexOfStr_WithLength( pData
->buffer
, fromIndex
,
1560 str
.data(), str
.size() );
1563 sal_Int32
lastIndexOf( const OString
& str
, sal_Int32 fromIndex
) const
1565 return rtl_str_lastIndexOfStr_WithLength( pData
->buffer
, fromIndex
,
1566 str
.pData
->buffer
, str
.pData
->length
);
1571 Returns a new string that is a substring of this string.
1573 The substring begins at the specified beginIndex. If
1574 beginIndex is negative or be greater than the length of
1575 this string, behaviour is undefined.
1577 @param beginIndex the beginning index, inclusive.
1578 @return the specified substring.
1580 SAL_WARN_UNUSED_RESULT OString
copy( sal_Int32 beginIndex
) const
1582 return copy(beginIndex
, getLength() - beginIndex
);
1586 Returns a new string that is a substring of this string.
1588 The substring begins at the specified beginIndex and contains count
1589 characters. If either beginIndex or count are negative,
1590 or beginIndex + count are greater than the length of this string
1591 then behaviour is undefined.
1593 @param beginIndex the beginning index, inclusive.
1594 @param count the number of characters.
1595 @return the specified substring.
1597 SAL_WARN_UNUSED_RESULT OString
copy( sal_Int32 beginIndex
, sal_Int32 count
) const
1599 rtl_String
*pNew
= NULL
;
1600 rtl_string_newFromSubString( &pNew
, pData
, beginIndex
, count
);
1601 return OString( pNew
, SAL_NO_ACQUIRE
);
1604 #if defined LIBO_INTERNAL_ONLY
1606 Returns a std::string_view that is a view of a substring of this string.
1608 The substring begins at the specified beginIndex. If
1609 beginIndex is negative or be greater than the length of
1610 this string, behaviour is undefined.
1612 @param beginIndex the beginning index, inclusive.
1613 @return the specified substring.
1615 SAL_WARN_UNUSED_RESULT
std::string_view
subView( sal_Int32 beginIndex
) const
1617 assert(beginIndex
>= 0);
1618 assert(beginIndex
<= getLength());
1619 return subView(beginIndex
, getLength() - beginIndex
);
1623 Returns a std::string_view that is a view of a substring of this string.
1625 The substring begins at the specified beginIndex and contains count
1626 characters. If either beginIndex or count are negative,
1627 or beginIndex + count are greater than the length of this string
1628 then behaviour is undefined.
1630 @param beginIndex the beginning index, inclusive.
1631 @param count the number of characters.
1632 @return the specified substring.
1634 SAL_WARN_UNUSED_RESULT
std::string_view
subView( sal_Int32 beginIndex
, sal_Int32 count
) const
1636 assert(beginIndex
>= 0);
1638 assert(beginIndex
<= getLength());
1639 assert(count
<= getLength() - beginIndex
);
1640 return std::string_view(*this).substr(beginIndex
, count
);
1644 #ifndef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
1646 Concatenates the specified string to the end of this string.
1648 @param str the string that is concatenated to the end
1650 @return a string that represents the concatenation of this string
1651 followed by the string argument.
1653 SAL_WARN_UNUSED_RESULT OString
concat( const OString
& str
) const
1655 rtl_String
* pNew
= NULL
;
1656 rtl_string_newConcat( &pNew
, pData
, str
.pData
);
1657 return OString( pNew
, SAL_NO_ACQUIRE
);
1661 #ifndef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
1662 friend OString
operator+( const OString
& str1
, const OString
& str2
)
1664 return str1
.concat( str2
);
1668 // hide this from internal code to avoid ambiguous lookup error
1669 #ifndef LIBO_INTERNAL_ONLY
1671 Returns a new string resulting from replacing n = count characters
1672 from position index in this string with newStr.
1674 @param index the replacing index in str.
1675 The index must be greater or equal as 0 and
1676 less or equal as the length of the string.
1677 @param count the count of characters that will replaced
1678 The count must be greater or equal as 0 and
1679 less or equal as the length of the string minus index.
1680 @param newStr the new substring.
1681 @return the new string.
1683 SAL_WARN_UNUSED_RESULT OString
replaceAt( sal_Int32 index
, sal_Int32 count
, const OString
& newStr
) const
1685 rtl_String
* pNew
= NULL
;
1686 rtl_string_newReplaceStrAt( &pNew
, pData
, index
, count
, newStr
.pData
);
1687 return OString( pNew
, SAL_NO_ACQUIRE
);
1691 #ifdef LIBO_INTERNAL_ONLY
1692 SAL_WARN_UNUSED_RESULT OString
replaceAt( sal_Int32 index
, sal_Int32 count
, std::string_view newStr
) const
1694 rtl_String
* pNew
= NULL
;
1695 rtl_string_newReplaceStrAt_WithLength ( &pNew
, pData
, index
, count
, newStr
.data(), newStr
.size() );
1696 return OString( pNew
, SAL_NO_ACQUIRE
);
1701 Returns a new string resulting from replacing all occurrences of
1702 oldChar in this string with newChar.
1704 If the character oldChar does not occur in the character sequence
1705 represented by this object, then the string is assigned with
1708 @param oldChar the old character.
1709 @param newChar the new character.
1710 @return a string derived from this string by replacing every
1711 occurrence of oldChar with newChar.
1713 SAL_WARN_UNUSED_RESULT OString
replace( char oldChar
, char newChar
) const
1715 rtl_String
* pNew
= NULL
;
1716 rtl_string_newReplace( &pNew
, pData
, oldChar
, newChar
);
1717 return OString( pNew
, SAL_NO_ACQUIRE
);
1721 Returns a new string resulting from replacing the first occurrence of a
1722 given substring with another substring.
1724 @param from the substring to be replaced
1726 @param to the replacing substring
1728 @param[in,out] index pointer to a start index; if the pointer is
1729 non-null: upon entry to the function, its value is the index into the this
1730 string at which to start searching for the \p from substring, the value
1731 must be non-negative and not greater than this string's length; upon exit
1732 from the function its value is the index into this string at which the
1733 replacement took place or -1 if no replacement took place; if the pointer
1734 is null, searching always starts at index 0
1736 @since LibreOffice 3.6
1738 SAL_WARN_UNUSED_RESULT OString
replaceFirst(
1739 OString
const & from
, OString
const & to
, sal_Int32
* index
= NULL
) const
1741 rtl_String
* s
= NULL
;
1743 rtl_string_newReplaceFirst(
1744 &s
, pData
, from
.pData
->buffer
, from
.pData
->length
,
1745 to
.pData
->buffer
, to
.pData
->length
, index
== NULL
? &i
: index
);
1746 return OString(s
, SAL_NO_ACQUIRE
);
1750 Returns a new string resulting from replacing all occurrences of a given
1751 substring with another substring.
1753 Replacing subsequent occurrences picks up only after a given replacement.
1754 That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
1756 @param from the substring to be replaced
1758 @param to the replacing substring
1760 @since LibreOffice 3.6
1762 SAL_WARN_UNUSED_RESULT OString
replaceAll(OString
const & from
, OString
const & to
) const {
1763 rtl_String
* s
= NULL
;
1764 rtl_string_newReplaceAll(
1765 &s
, pData
, from
.pData
->buffer
, from
.pData
->length
,
1766 to
.pData
->buffer
, to
.pData
->length
);
1767 return OString(s
, SAL_NO_ACQUIRE
);
1771 Converts from this string all ASCII uppercase characters (65-90)
1772 to ASCII lowercase characters (97-122).
1774 This function can't be used for language specific conversion.
1775 If the string doesn't contain characters which must be converted,
1776 then the new string is assigned with str.
1778 @return the string, converted to ASCII lowercase.
1780 SAL_WARN_UNUSED_RESULT OString
toAsciiLowerCase() const
1782 rtl_String
* pNew
= NULL
;
1783 rtl_string_newToAsciiLowerCase( &pNew
, pData
);
1784 return OString( pNew
, SAL_NO_ACQUIRE
);
1788 Converts from this string all ASCII lowercase characters (97-122)
1789 to ASCII uppercase characters (65-90).
1791 This function can't be used for language specific conversion.
1792 If the string doesn't contain characters which must be converted,
1793 then the new string is assigned with str.
1795 @return the string, converted to ASCII uppercase.
1797 SAL_WARN_UNUSED_RESULT OString
toAsciiUpperCase() const
1799 rtl_String
* pNew
= NULL
;
1800 rtl_string_newToAsciiUpperCase( &pNew
, pData
);
1801 return OString( pNew
, SAL_NO_ACQUIRE
);
1805 Returns a new string resulting from removing white space from both ends
1808 All characters that have codes less than or equal to
1809 32 (the space character) are considered to be white space.
1810 If the string doesn't contain white spaces at both ends,
1811 then the new string is assigned with str.
1813 @return the string, with white space removed from the front and end.
1815 SAL_WARN_UNUSED_RESULT OString
trim() const
1817 rtl_String
* pNew
= NULL
;
1818 rtl_string_newTrim( &pNew
, pData
);
1819 return OString( pNew
, SAL_NO_ACQUIRE
);
1823 Returns a token in the string.
1826 sal_Int32 nIndex = 0;
1830 OString aToken = aStr.getToken( 0, ';', nIndex );
1833 while ( nIndex >= 0 );
1835 @param token the number of the token to return.
1836 @param cTok the character which separate the tokens.
1837 @param index the position at which the token is searched in the
1839 The index must not be greater than the length of the
1841 This param is set to the position of the
1842 next token or to -1, if it is the last token.
1843 @return the token; if either token or index is negative, an empty token
1844 is returned (and index is set to -1)
1846 OString
getToken( sal_Int32 token
, char cTok
, sal_Int32
& index
) const
1848 rtl_String
* pNew
= NULL
;
1849 index
= rtl_string_getToken( &pNew
, pData
, token
, cTok
, index
);
1850 return OString( pNew
, SAL_NO_ACQUIRE
);
1854 Returns a token from the string.
1856 The same as getToken(sal_Int32, char, sal_Int32 &), but always passing
1857 in 0 as the start index in the third argument.
1859 @param count the number of the token to return, starting with 0
1860 @param separator the character which separates the tokens
1862 @return the given token, or an empty string
1864 @since LibreOffice 3.6
1866 OString
getToken(sal_Int32 count
, char separator
) const {
1868 return getToken(count
, separator
, n
);
1872 Returns the Boolean value from this string.
1874 This function can't be used for language specific conversion.
1876 @return true, if the string is 1 or "True" in any ASCII case.
1877 false in any other case.
1879 bool toBoolean() const
1881 return rtl_str_toBoolean( pData
->buffer
);
1885 Returns the first character from this string.
1887 @return the first character from this string or 0, if this string
1892 return pData
->buffer
[0];
1896 Returns the int32 value from this string.
1898 This function can't be used for language specific conversion.
1900 @param radix the radix (between 2 and 36)
1901 @return the int32 represented from this string.
1902 0 if this string represents no number or one of too large
1905 sal_Int32
toInt32( sal_Int16 radix
= 10 ) const
1907 return rtl_str_toInt32( pData
->buffer
, radix
);
1911 Returns the uint32 value from this string.
1913 This function can't be used for language specific conversion.
1915 @param radix the radix (between 2 and 36)
1916 @return the uint32 represented from this string.
1917 0 if this string represents no number or one of too large
1920 @since LibreOffice 4.2
1922 sal_uInt32
toUInt32( sal_Int16 radix
= 10 ) const
1924 return rtl_str_toUInt32( pData
->buffer
, radix
);
1928 Returns the int64 value from this string.
1930 This function can't be used for language specific conversion.
1932 @param radix the radix (between 2 and 36)
1933 @return the int64 represented from this string.
1934 0 if this string represents no number or one of too large
1937 sal_Int64
toInt64( sal_Int16 radix
= 10 ) const
1939 return rtl_str_toInt64( pData
->buffer
, radix
);
1943 Returns the uint64 value from this string.
1945 This function can't be used for language specific conversion.
1947 @param radix the radix (between 2 and 36)
1948 @return the uint64 represented from this string.
1949 0 if this string represents no number or one of too large
1952 @since LibreOffice 4.1
1954 sal_uInt64
toUInt64( sal_Int16 radix
= 10 ) const
1956 return rtl_str_toUInt64( pData
->buffer
, radix
);
1960 Returns the float value from this string.
1962 This function can't be used for language specific conversion.
1964 @return the float represented from this string.
1965 0.0 if this string represents no number.
1967 float toFloat() const
1969 return rtl_str_toFloat( pData
->buffer
);
1973 Returns the double value from this string.
1975 This function can't be used for language specific conversion.
1977 @return the double represented from this string.
1978 0.0 if this string represents no number.
1980 double toDouble() const
1982 return rtl_str_toDouble( pData
->buffer
);
1985 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
1987 static OStringNumber
< int > number( int i
, sal_Int16 radix
= 10 )
1989 return OStringNumber
< int >( i
, radix
);
1991 static OStringNumber
< long long > number( long long ll
, sal_Int16 radix
= 10 )
1993 return OStringNumber
< long long >( ll
, radix
);
1995 static OStringNumber
< unsigned long long > number( unsigned long long ll
, sal_Int16 radix
= 10 )
1997 return OStringNumber
< unsigned long long >( ll
, radix
);
1999 static OStringNumber
< unsigned long long > number( unsigned int i
, sal_Int16 radix
= 10 )
2001 return number( static_cast< unsigned long long >( i
), radix
);
2003 static OStringNumber
< long long > number( long i
, sal_Int16 radix
= 10)
2005 return number( static_cast< long long >( i
), radix
);
2007 static OStringNumber
< unsigned long long > number( unsigned long i
, sal_Int16 radix
= 10 )
2009 return number( static_cast< unsigned long long >( i
), radix
);
2011 static OStringNumber
< float > number( float f
)
2013 return OStringNumber
< float >( f
);
2015 static OStringNumber
< double > number( double d
)
2017 return OStringNumber
< double >( d
);
2021 Returns the string representation of the integer argument.
2023 This function can't be used for language specific conversion.
2025 @param i an integer value
2026 @param radix the radix (between 2 and 36)
2027 @return a string with the string representation of the argument.
2028 @since LibreOffice 4.1
2030 static OString
number( int i
, sal_Int16 radix
= 10 )
2032 char aBuf
[RTL_STR_MAX_VALUEOFINT32
];
2033 return OString(aBuf
, rtl_str_valueOfInt32(aBuf
, i
, radix
));
2036 /// @since LibreOffice 4.1
2037 static OString
number( unsigned int i
, sal_Int16 radix
= 10 )
2039 return number( static_cast< unsigned long long >( i
), radix
);
2042 /// @since LibreOffice 4.1
2043 static OString
number( long i
, sal_Int16 radix
= 10 )
2045 return number( static_cast< long long >( i
), radix
);
2048 /// @since LibreOffice 4.1
2049 static OString
number( unsigned long i
, sal_Int16 radix
= 10 )
2051 return number( static_cast< unsigned long long >( i
), radix
);
2054 /// @since LibreOffice 4.1
2055 static OString
number( long long ll
, sal_Int16 radix
= 10 )
2057 char aBuf
[RTL_STR_MAX_VALUEOFINT64
];
2058 return OString(aBuf
, rtl_str_valueOfInt64(aBuf
, ll
, radix
));
2061 /// @since LibreOffice 4.1
2062 static OString
number( unsigned long long ll
, sal_Int16 radix
= 10 )
2064 char aBuf
[RTL_STR_MAX_VALUEOFUINT64
];
2065 return OString(aBuf
, rtl_str_valueOfUInt64(aBuf
, ll
, radix
));
2069 Returns the string representation of the float argument.
2071 This function can't be used for language specific conversion.
2074 @return a string with the decimal representation of the argument.
2075 @since LibreOffice 4.1
2077 static OString
number( float f
)
2079 char aBuf
[RTL_STR_MAX_VALUEOFFLOAT
];
2080 return OString(aBuf
, rtl_str_valueOfFloat(aBuf
, f
));
2084 Returns the string representation of the double argument.
2086 This function can't be used for language specific conversion.
2089 @return a string with the decimal representation of the argument.
2090 @since LibreOffice 4.1
2092 static OString
number( double d
)
2094 char aBuf
[RTL_STR_MAX_VALUEOFDOUBLE
];
2095 return OString(aBuf
, rtl_str_valueOfDouble(aBuf
, d
));
2100 Returns the string representation of the sal_Bool argument.
2102 If the sal_Bool is true, the string "true" is returned.
2103 If the sal_Bool is false, the string "false" is returned.
2104 This function can't be used for language specific conversion.
2106 @param b a sal_Bool.
2107 @return a string with the string representation of the argument.
2108 @deprecated use boolean()
2110 SAL_DEPRECATED("use boolean()") static OString
valueOf( sal_Bool b
)
2116 Returns the string representation of the boolean argument.
2118 If the argument is true, the string "true" is returned.
2119 If the argument is false, the string "false" is returned.
2120 This function can't be used for language specific conversion.
2123 @return a string with the string representation of the argument.
2124 @since LibreOffice 4.1
2126 static OString
boolean( bool b
)
2128 char aBuf
[RTL_STR_MAX_VALUEOFBOOLEAN
];
2129 return OString(aBuf
, rtl_str_valueOfBoolean(aBuf
, b
));
2133 Returns the string representation of the char argument.
2135 @param c a character.
2136 @return a string with the string representation of the argument.
2137 @deprecated use operator, function or constructor taking char or sal_Unicode argument
2139 SAL_DEPRECATED("convert to OString or use directly") static OString
valueOf( char c
)
2141 return OString( &c
, 1 );
2145 Returns the string representation of the int argument.
2147 This function can't be used for language specific conversion.
2150 @param radix the radix (between 2 and 36)
2151 @return a string with the string representation of the argument.
2152 @deprecated use number()
2154 SAL_DEPRECATED("use number()") static OString
valueOf( sal_Int32 i
, sal_Int16 radix
= 10 )
2156 return number( i
, radix
);
2160 Returns the string representation of the long argument.
2162 This function can't be used for language specific conversion.
2165 @param radix the radix (between 2 and 36)
2166 @return a string with the string representation of the argument.
2167 @deprecated use number()
2169 SAL_DEPRECATED("use number()") static OString
valueOf( sal_Int64 ll
, sal_Int16 radix
= 10 )
2171 return number( ll
, radix
);
2175 Returns the string representation of the float argument.
2177 This function can't be used for language specific conversion.
2180 @return a string with the string representation of the argument.
2181 @deprecated use number()
2183 SAL_DEPRECATED("use number()") static OString
valueOf( float f
)
2189 Returns the string representation of the double argument.
2191 This function can't be used for language specific conversion.
2194 @return a string with the string representation of the argument.
2195 @deprecated use number()
2197 SAL_DEPRECATED("use number()") static OString
valueOf( double d
)
2202 #if defined LIBO_INTERNAL_ONLY
2203 operator std::string_view() const { return {getStr(), sal_uInt32(getLength())}; }
2206 #if defined LIBO_INTERNAL_ONLY
2207 // A wrapper for the first expression in an
2209 // OString::Concat(e1) + e2 + ...
2211 // concatenation chain, when neither of the first two e1, e2 is one of our rtl string-related
2212 // classes (so something like
2214 // OString s = "a" + (b ? std::string_view("c") : std::string_view("dd"));
2216 // would not compile):
2217 template<typename T
> [[nodiscard
]] static
2218 OStringConcat
<OStringConcatMarker
, T
>
2219 Concat(T
const & value
) { return OStringConcat
<OStringConcatMarker
, T
>({}, value
); }
2221 // This overload is needed so that an argument of type 'char const[N]' ends up as
2222 // 'OStringConcat<rtl::OStringConcatMarker, char const[N]>' rather than as
2223 // 'OStringConcat<rtl::OStringConcatMarker, char[N]>':
2224 template<typename T
, std::size_t N
> [[nodiscard
]] static
2225 OStringConcat
<OStringConcatMarker
, T
[N
]>
2226 Concat(T (& value
)[N
]) { return OStringConcat
<OStringConcatMarker
, T
[N
]>({}, value
); }
2230 #if defined LIBO_INTERNAL_ONLY
2231 // Can only define this after we define OString
2232 inline OStringConstExpr::operator const OString
&() const { return OString::unacquired(&pData
); }
2235 #if defined LIBO_INTERNAL_ONLY
2236 inline bool operator ==(OString
const & lhs
, StringConcatenation
<char> const & rhs
)
2237 { return lhs
== std::string_view(rhs
); }
2238 inline bool operator !=(OString
const & lhs
, StringConcatenation
<char> const & rhs
)
2239 { return lhs
!= std::string_view(rhs
); }
2240 inline bool operator ==(StringConcatenation
<char> const & lhs
, OString
const & rhs
)
2241 { return std::string_view(lhs
) == rhs
; }
2242 inline bool operator !=(StringConcatenation
<char> const & lhs
, OString
const & rhs
)
2243 { return std::string_view(lhs
) != rhs
; }
2246 /* ======================================================================= */
2248 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
2254 struct ToStringHelper
< OString
>
2256 static std::size_t length( const OString
& s
) { return s
.getLength(); }
2257 char* operator()( char* buffer
, const OString
& s
) const { return addDataHelper( buffer
, s
.getStr(), s
.getLength()); }
2263 template<std::size_t N
>
2264 struct ToStringHelper
< OStringLiteral
<N
> >
2266 static constexpr std::size_t length( const OStringLiteral
<N
>& str
) { return str
.getLength(); }
2267 char* operator()( char* buffer
, const OStringLiteral
<N
>& str
) const { return addDataHelper( buffer
, str
.getStr(), str
.getLength() ); }
2273 template< typename charT
, typename traits
, typename T1
, typename T2
>
2274 inline std::basic_ostream
<charT
, traits
> & operator <<(
2275 std::basic_ostream
<charT
, traits
> & stream
, OStringConcat
< T1
, T2
>&& concat
)
2277 return stream
<< OString( std::move(concat
) );
2282 /** A helper to use OStrings with hash maps.
2284 Instances of this class are unary function objects that can be used as
2285 hash function arguments to std::unordered_map and similar constructs.
2289 /** Compute a hash code for a string.
2295 a hash code for the string. This hash code should not be stored
2296 persistently, as its computation may change in later revisions.
2298 size_t operator()( const OString
& rString
) const
2299 { return static_cast<size_t>(rString
.hashCode()); }
2302 /** Equality functor for classic c-strings (i.e., null-terminated char* strings). */
2305 bool operator()( const char* p1
, const char* p2
) const
2306 { return rtl_str_compare(p1
, p2
) == 0; }
2309 /** Hashing functor for classic c-strings (i.e., null-terminated char* strings). */
2312 size_t operator()(const char* p
) const
2313 { return rtl_str_hashCode(p
); }
2316 /* ======================================================================= */
2319 Support for rtl::OString in std::ostream (and thus in
2320 CPPUNIT_ASSERT or SAL_INFO macros, for example).
2322 @since LibreOffice 4.0
2324 template< typename charT
, typename traits
> std::basic_ostream
<charT
, traits
> &
2326 std::basic_ostream
<charT
, traits
> & stream
, OString
const & rString
)
2328 return stream
<< rString
.getStr();
2329 // best effort; potentially loses data due to embedded null characters
2334 #ifdef RTL_STRING_UNITTEST
2337 typedef rtlunittest::OString OString
;
2339 #undef RTL_STRING_CONST_FUNCTION
2342 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
2343 using ::rtl::OString
;
2344 using ::rtl::OStringChar
;
2345 using ::rtl::Concat2View
;
2346 using ::rtl::OStringHash
;
2347 using ::rtl::OStringLiteral
;
2352 Make OString hashable by default for use in STL containers.
2354 @since LibreOffice 6.0
2356 #if defined LIBO_INTERNAL_ONLY
2360 struct hash
<::rtl::OString
>
2362 std::size_t operator()(::rtl::OString
const & s
) const
2364 if constexpr (sizeof(std::size_t) == 8)
2366 // return a hash that uses the full 64-bit range instead of a 32-bit value
2368 for (sal_Int32 i
= 0, len
= s
.getLength(); i
< len
; ++i
)
2373 return std::size_t(s
.hashCode());
2382 #endif // INCLUDED_RTL_STRING_HXX
2384 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */