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"
81 A wrapper dressing a string literal as a static-refcount rtl_String.
83 This class is not part of public API and is meant to be used only in LibreOffice code.
84 @since LibreOffice 4.0
86 template<std::size_t N
> class SAL_WARN_UNUSED OStringLiteral
{
87 static_assert(N
!= 0);
88 static_assert(N
- 1 <= std::numeric_limits
<sal_Int32
>::max(), "literal too long");
92 #if HAVE_CPP_CONSTEVAL
97 OStringLiteral(char const (&literal
)[N
]) {
99 assert(literal
[N
- 1] == '\0');
100 //TODO: Use C++20 constexpr std::copy_n (P0202R3):
101 for (std::size_t i
= 0; i
!= N
; ++i
) {
102 more
.buffer
[i
] = literal
[i
];
106 #if defined __cpp_char8_t
107 #if HAVE_CPP_CONSTEVAL
112 explicit OStringLiteral(char8_t
const (&literal
)[N
]) {
114 assert(literal
[N
- 1] == '\0');
115 //TODO: Use C++20 constexpr std::copy_n (P0202R3):
116 for (std::size_t i
= 0; i
!= N
; ++i
) {
117 more
.buffer
[i
] = literal
[i
];
122 constexpr sal_Int32
getLength() const { return more
.length
; }
124 constexpr char const * getStr() const SAL_RETURNS_NONNULL
{ return more
.buffer
; }
126 constexpr operator std::string_view() const { return {more
.buffer
, sal_uInt32(more
.length
)}; }
129 static constexpr void assertLayout() {
130 // These static_asserts verifying the layout compatibility with rtl_String cannot be class
131 // member declarations, as offsetof requires a complete type, so defer them to here:
132 static_assert(std::is_standard_layout_v
<OStringLiteral
>);
133 static_assert(offsetof(OStringLiteral
, str
.refCount
) == offsetof(OStringLiteral
, more
.refCount
));
134 static_assert(offsetof(OStringLiteral
, str
.length
) == offsetof(OStringLiteral
, more
.length
));
135 static_assert(offsetof(OStringLiteral
, str
.buffer
) == offsetof(OStringLiteral
, more
.buffer
));
141 oslInterlockedCount refCount
;
146 0x40000000, // SAL_STRING_STATIC_FLAG (sal/rtl/strimp.hxx)
148 {} //TODO: drop initialization for C++20 (P1331R2)
154 /* ======================================================================= */
157 This String class provide base functionality for C++ like 8-Bit
158 character array handling. The advantage of this class is, that it
159 handle all the memory management for you - and it do it
160 more efficient. If you assign a string to another string, the
161 data of both strings are shared (without any copy operation or
162 memory allocation) as long as you do not change the string. This class
163 stores also the length of the string, so that many operations are
164 faster as the C-str-functions.
166 This class provides only readonly string handling. So you could create
167 a string and you could only query the content from this string.
168 It provides also functionality to change the string, but this results
169 in every case in a new string instance (in the most cases with an
170 memory allocation). You don't have functionality to change the
171 content of the string. If you want to change the string content, then
172 you should use the OStringBuffer class, which provides these
173 functionalities and avoid too much memory allocation.
175 The design of this class is similar to the string classes in Java
176 and so more people should have fewer understanding problems when they
180 class SAL_WARN_UNUSED SAL_DLLPUBLIC_RTTI OString
188 New string containing no characters.
193 rtl_string_new( &pData
);
197 New string from OString.
199 @param str an OString.
201 OString( const OString
& str
)
204 rtl_string_acquire( pData
);
207 #if defined LIBO_INTERNAL_ONLY
211 @param str an OString.
212 @since LibreOffice 5.2
214 OString( OString
&& str
) noexcept
218 rtl_string_new( &str
.pData
);
223 New string from OString data.
225 @param str an OString data.
227 OString( rtl_String
* str
)
230 rtl_string_acquire( pData
);
233 /** New string from OString data without acquiring it. Takeover of ownership.
235 The SAL_NO_ACQUIRE dummy parameter is only there to distinguish this
236 from other constructors.
238 @param str an OString data.
240 OString( rtl_String
* str
, __sal_NoAcquire
)
246 New string from a single character.
248 @param value a character.
250 explicit OString( char value
)
253 rtl_string_newFromStr_WithLength( &pData
, &value
, 1 );
256 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST_CONCAT
257 // Catch inadvertent conversions to the above ctor (e.g., from sal_[u]Int8, aka [un]signed
259 OString(int) = delete;
263 New string from a character buffer array.
265 Note: The argument type is always either char* or const char*. The template is
266 used only for technical reasons, as is the second argument.
268 @param value a NULL-terminated character array.
270 template< typename T
>
271 OString( const T
& value
, typename
libreoffice_internal::CharPtrDetector
< T
, libreoffice_internal::Dummy
>::Type
= libreoffice_internal::Dummy() )
274 rtl_string_newFromStr( &pData
, value
);
277 template< typename T
>
278 OString( T
& value
, typename
libreoffice_internal::NonConstCharArrayDetector
< T
, libreoffice_internal::Dummy
>::Type
= libreoffice_internal::Dummy() )
281 rtl_string_newFromStr( &pData
, value
);
285 New string from a string literal.
287 If there are any embedded \0's in the string literal, the result is undefined.
288 Use the overload that explicitly accepts length.
290 @since LibreOffice 3.6
292 @param literal a string literal
294 template< typename T
>
295 OString( T
& literal
, typename
libreoffice_internal::ConstCharArrayDetector
< T
, libreoffice_internal::Dummy
>::Type
= libreoffice_internal::Dummy() )
298 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
300 if (libreoffice_internal::ConstCharArrayDetector
<T
>::length
== 0) {
301 rtl_string_new(&pData
);
303 rtl_string_newFromLiteral(
305 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
307 libreoffice_internal::ConstCharArrayDetector
<T
>::length
, 0);
309 #ifdef RTL_STRING_UNITTEST
310 rtl_string_unittest_const_literal
= true;
315 New string from a character buffer array.
317 @param value a character array.
318 @param length the number of character which should be copied.
319 The character array length must be greater or
320 equal than this value.
322 OString( const char * value
, sal_Int32 length
)
325 rtl_string_newFromStr_WithLength( &pData
, value
, length
);
328 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
331 New string from an 8-Bit string literal.
333 @since LibreOffice 7.1
335 template<std::size_t N
> constexpr OString(OStringLiteral
<N
> const & literal
):
336 pData(const_cast<rtl_String
*>(&literal
.str
)) {}
337 template<std::size_t N
> OString(OStringLiteral
<N
> &&) = delete;
341 #if defined LIBO_INTERNAL_ONLY
342 explicit OString(std::string_view sv
) {
343 if (sv
.size() > sal_uInt32(std::numeric_limits
<sal_Int32
>::max())) {
344 throw std::bad_alloc();
347 rtl_string_newFromStr_WithLength(&pData
, sv
.data(), sv
.size());
352 New string from a Unicode character buffer array.
354 @param value a Unicode character array.
355 @param length the number of character which should be converted.
356 The Unicode character array length must be
357 greater or equal than this value.
358 @param encoding the text encoding in which the Unicode character
359 sequence should be converted.
360 @param convertFlags flags which controls the conversion.
361 see RTL_UNICODETOTEXT_FLAGS_...
363 @exception std::bad_alloc is thrown if an out-of-memory condition occurs
365 OString( const sal_Unicode
* value
, sal_Int32 length
,
366 rtl_TextEncoding encoding
,
367 sal_uInt32 convertFlags
= OUSTRING_TO_OSTRING_CVTFLAGS
)
370 rtl_uString2String( &pData
, value
, length
, encoding
, convertFlags
);
372 throw std::bad_alloc();
376 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
381 template< typename T1
, typename T2
>
382 OString( OStringConcat
< T1
, T2
>&& c
)
384 const sal_Int32 l
= c
.length();
385 pData
= rtl_string_alloc( l
);
388 char* end
= c
.addData( pData
->buffer
);
398 template< typename T
>
399 OString( OStringNumber
< T
>&& n
)
400 : OString( n
.buf
, n
.length
)
404 #ifdef LIBO_INTERNAL_ONLY
405 OString(std::nullptr_t
) = delete;
409 Release the string data.
413 rtl_string_release( pData
);
419 @param str an OString.
421 OString
& operator=( const OString
& str
)
423 rtl_string_assign( &pData
, str
.pData
);
427 #if defined LIBO_INTERNAL_ONLY
429 Move assign a new string.
431 @param str an OString.
432 @since LibreOffice 5.2
434 OString
& operator=( OString
&& str
) noexcept
436 rtl_string_release( pData
);
439 rtl_string_new( &str
.pData
);
446 This function accepts an ASCII string literal as its argument.
447 @since LibreOffice 3.6
449 template< typename T
>
450 typename
libreoffice_internal::ConstCharArrayDetector
< T
, OString
& >::Type
operator=( T
& literal
)
452 RTL_STRING_CONST_FUNCTION
454 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
455 if (libreoffice_internal::ConstCharArrayDetector
<T
>::length
== 0) {
456 rtl_string_new(&pData
);
458 rtl_string_newFromLiteral(
460 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
462 libreoffice_internal::ConstCharArrayDetector
<T
>::length
, 0);
468 Append a string to this string.
470 @param str an OString.
472 OString
& operator+=( const OString
& str
)
473 #if defined LIBO_INTERNAL_ONLY
477 rtl_string_newConcat( &pData
, pData
, str
.pData
);
480 #if defined LIBO_INTERNAL_ONLY
481 void operator+=(OString
const &) && = delete;
484 #if defined LIBO_INTERNAL_ONLY
485 template<typename T
> typename
libreoffice_internal::CharPtrDetector
<T
, OString
&>::Type
486 operator +=(T
const & value
) & { return operator +=(std::string_view(value
)); }
487 template<typename T
> typename
libreoffice_internal::CharPtrDetector
<T
, OString
&>::Type
488 operator +=(T
const &) && = delete;
491 typename
libreoffice_internal::NonConstCharArrayDetector
<T
, OString
&>::Type
492 operator +=(T
& value
) & { return operator +=(std::string_view(value
)); }
494 typename
libreoffice_internal::NonConstCharArrayDetector
<T
, OString
&>::Type
operator +=(T
&) &&
497 template<typename T
> typename
libreoffice_internal::ConstCharArrayDetector
<T
, OString
&>::Type
498 operator +=(T
& literal
) & {
499 assert(libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
502 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
503 libreoffice_internal::ConstCharArrayDetector
<T
>::length
));
505 template<typename T
> typename
libreoffice_internal::ConstCharArrayDetector
<T
, OString
&>::Type
506 operator +=(T
&) && = delete;
508 template<std::size_t N
> OString
& operator +=(OStringLiteral
<N
> const & literal
) &
509 { return operator +=(std::string_view(literal
.getStr(), literal
.getLength())); }
510 template<std::size_t N
> void operator +=(OStringLiteral
<N
> const &) && = delete;
512 OString
& operator +=(std::string_view sv
) & {
516 if (sv
.size() > sal_uInt32(std::numeric_limits
<sal_Int32
>::max() - pData
->length
)) {
517 throw std::bad_alloc();
519 auto const l
= pData
->length
+ sv
.size();
520 rtl_string_ensureCapacity(&pData
, l
);
521 *addDataHelper(pData
->buffer
+ pData
->length
, sv
.data(), sv
.size()) = '\0';
525 void operator +=(std::string_view
) && = delete;
528 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
533 template< typename T1
, typename T2
>
534 OString
& operator+=( OStringConcat
< T1
, T2
>&& c
) & {
535 sal_Int32 l
= c
.length();
539 rtl_string_ensureCapacity( &pData
, l
);
540 char* end
= c
.addData( pData
->buffer
+ pData
->length
);
545 template<typename T1
, typename T2
> void operator +=(
546 OStringConcat
<T1
, T2
> &&) && = delete;
552 template< typename T
>
553 OString
& operator+=( OStringNumber
< T
>&& n
) & {
554 return operator +=(std::string_view(n
.buf
, n
.length
));
556 template<typename T
> void operator +=(
557 OStringNumber
<T
> &&) && = delete;
561 Clears the string, i.e, makes a zero-character string
562 @since LibreOffice 4.4
566 rtl_string_new( &pData
);
570 Returns the length of this string.
572 The length is equal to the number of characters in this string.
574 @return the length of the sequence of characters represented by this
577 sal_Int32
getLength() const { return pData
->length
; }
580 Checks if a string is empty.
582 @return true if the string is empty;
585 @since LibreOffice 3.4
589 return pData
->length
== 0;
593 Returns a pointer to the characters of this string.
595 <p>The returned pointer is guaranteed to point to a null-terminated byte
596 string. But note that this string object may contain embedded null
597 characters, which will thus also be embedded in the returned
598 null-terminated byte string.</p>
600 @return a pointer to a null-terminated byte string representing the
601 characters of this string object.
603 const char * getStr() const SAL_RETURNS_NONNULL
{ return pData
->buffer
; }
606 Access to individual characters.
608 @param index must be non-negative and less than length.
610 @return the character at the given index.
612 @since LibreOffice 3.5
614 char operator [](sal_Int32 index
) const {
615 // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
616 assert(index
>= 0 && static_cast<sal_uInt32
>(index
) < static_cast<sal_uInt32
>(getLength()));
617 return getStr()[index
];
621 Compares two strings.
623 The comparison is based on the numeric value of each character in
624 the strings and return a value indicating their relationship.
625 This function can't be used for language specific sorting.
627 @param str the object to be compared.
628 @return 0 - if both strings are equal
629 < 0 - if this string is less than the string argument
630 > 0 - if this string is greater than the string argument
632 sal_Int32
compareTo( const OString
& str
) const
634 return rtl_str_compare_WithLength( pData
->buffer
, pData
->length
,
635 str
.pData
->buffer
, str
.pData
->length
);
639 Compares two strings with an maximum count of characters.
641 The comparison is based on the numeric value of each character in
642 the strings and return a value indicating their relationship.
643 This function can't be used for language specific sorting.
645 @param rObj the object to be compared.
646 @param maxLength the maximum count of characters to be compared.
647 @return 0 - if both strings are equal
648 < 0 - if this string is less than the string argument
649 > 0 - if this string is greater than the string argument
651 sal_Int32
compareTo( const OString
& rObj
, sal_Int32 maxLength
) const
653 return rtl_str_shortenedCompare_WithLength( pData
->buffer
, pData
->length
,
654 rObj
.pData
->buffer
, rObj
.pData
->length
, maxLength
);
658 Compares two strings in reverse order.
660 The comparison is based on the numeric value of each character in
661 the strings and return a value indicating their relationship.
662 This function can't be used for language specific sorting.
664 @param str the object to be compared.
665 @return 0 - if both strings are equal
666 < 0 - if this string is less than the string argument
667 > 0 - if this string is greater than the string argument
669 sal_Int32
reverseCompareTo( const OString
& str
) const
671 return rtl_str_reverseCompare_WithLength( pData
->buffer
, pData
->length
,
672 str
.pData
->buffer
, str
.pData
->length
);
676 Perform a comparison of two strings.
678 The result is true if and only if second string
679 represents the same sequence of characters as the first string.
680 This function can't be used for language specific comparison.
682 @param str the object to be compared.
683 @return true if the strings are equal;
686 bool equals( const OString
& str
) const
688 if ( pData
->length
!= str
.pData
->length
)
690 if ( pData
== str
.pData
)
692 return rtl_str_reverseCompare_WithLength( pData
->buffer
, pData
->length
,
693 str
.pData
->buffer
, str
.pData
->length
) == 0;
697 Perform a comparison of two strings.
699 The result is true if and only if second string
700 represents the same sequence of characters as the first string.
701 The ASCII string must be NULL-terminated and must be greater or
703 This function can't be used for language specific comparison.
706 @param value a character array.
707 @param length the length of the character array.
708 @return true if the strings are equal;
711 bool equalsL( const char* value
, sal_Int32 length
) const
713 if ( pData
->length
!= length
)
716 return rtl_str_reverseCompare_WithLength( pData
->buffer
, pData
->length
,
717 value
, length
) == 0;
721 Perform an ASCII lowercase comparison of two strings.
723 The result is true if and only if second string
724 represents the same sequence of characters as the first string,
726 Character values between 65 and 90 (ASCII A-Z) are interpreted as
727 values between 97 and 122 (ASCII a-z).
728 This function can't be used for language specific comparison.
730 @param str the object to be compared.
731 @return true if the strings are equal;
734 #if defined LIBO_INTERNAL_ONLY
735 bool equalsIgnoreAsciiCase( std::string_view str
) const
737 if ( sal_uInt32(pData
->length
) != str
.size() )
739 if ( pData
->buffer
== str
.data() )
741 return rtl_str_compareIgnoreAsciiCase_WithLength( pData
->buffer
, pData
->length
,
742 str
.data(), str
.size() ) == 0;
745 bool equalsIgnoreAsciiCase( const OString
& str
) const
747 if ( pData
->length
!= str
.pData
->length
)
749 if ( pData
== str
.pData
)
751 return rtl_str_compareIgnoreAsciiCase_WithLength( pData
->buffer
, pData
->length
,
752 str
.pData
->buffer
, str
.pData
->length
) == 0;
757 Perform an ASCII lowercase 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,
762 Character values between 65 and 90 (ASCII A-Z) are interpreted as
763 values between 97 and 122 (ASCII a-z).
764 Since this method is optimized for performance, the ASCII character
765 values are not converted in any way. The caller has to make sure that
766 all ASCII characters are in the allowed range between 0 and
767 127. The ASCII string must be NULL-terminated.
768 This function can't be used for language specific comparison.
770 Note: The argument type is always either char* or const char*, the return type is bool.
771 The template is used only for technical reasons.
773 @param asciiStr the 8-Bit ASCII character string to be compared.
774 @return true if the strings are equal;
777 template< typename T
>
778 typename
libreoffice_internal::CharPtrDetector
< T
, bool >::Type
equalsIgnoreAsciiCase( const T
& asciiStr
) const
780 return rtl_str_compareIgnoreAsciiCase( pData
->buffer
, asciiStr
) == 0;
783 template< typename T
>
784 typename
libreoffice_internal::NonConstCharArrayDetector
< T
, bool >::Type
equalsIgnoreAsciiCase( T
& asciiStr
) const
786 return rtl_str_compareIgnoreAsciiCase( pData
->buffer
, asciiStr
) == 0;
791 This function accepts an ASCII string literal as its argument.
792 @since LibreOffice 3.6
794 template< typename T
>
795 typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
equalsIgnoreAsciiCase( T
& literal
) const
797 RTL_STRING_CONST_FUNCTION
799 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
802 == libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
803 && (rtl_str_compareIgnoreAsciiCase_WithLength(
804 pData
->buffer
, pData
->length
,
805 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
807 libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
812 Perform an ASCII lowercase comparison of two strings.
814 The result is true if and only if second string
815 represents the same sequence of characters as the first string,
817 Character values between 65 and 90 (ASCII A-Z) are interpreted as
818 values between 97 and 122 (ASCII a-z).
819 Since this method is optimized for performance, the ASCII character
820 values are not converted in any way. The caller has to make sure that
821 all ASCII characters are in the allowed range between 0 and
822 127. The ASCII string must be greater or equal in length as asciiStrLength.
823 This function can't be used for language specific comparison.
825 @param asciiStr the 8-Bit ASCII character string to be compared.
826 @param asciiStrLength the length of the ascii string
827 @return true if the strings are equal;
830 bool equalsIgnoreAsciiCaseL( const char * asciiStr
, sal_Int32 asciiStrLength
) const
832 if ( pData
->length
!= asciiStrLength
)
835 return rtl_str_compareIgnoreAsciiCase_WithLength( pData
->buffer
, pData
->length
,
836 asciiStr
, asciiStrLength
) == 0;
840 Match against a substring appearing in this string.
842 The result is true if and only if the second string appears as a substring
843 of this string, at the given position.
844 This function can't be used for language specific comparison.
846 @param str the object (substring) to be compared.
847 @param fromIndex the index to start the comparison from.
848 The index must be greater or equal than 0
849 and less or equal as the string length.
850 @return true if str match with the characters in the string
851 at the given position;
854 #if defined LIBO_INTERNAL_ONLY
855 bool match( std::string_view str
, sal_Int32 fromIndex
= 0 ) const
857 return rtl_str_shortenedCompare_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
858 str
.data(), str
.size(), str
.size() ) == 0;
861 bool match( const OString
& str
, sal_Int32 fromIndex
= 0 ) const
863 return rtl_str_shortenedCompare_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
864 str
.pData
->buffer
, str
.pData
->length
, str
.pData
->length
) == 0;
870 This function accepts an ASCII string literal as its argument.
871 @since LibreOffice 3.6
873 template< typename T
>
874 typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
match( T
& literal
, sal_Int32 fromIndex
= 0 ) const
876 RTL_STRING_CONST_FUNCTION
878 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
880 rtl_str_shortenedCompare_WithLength(
881 pData
->buffer
+ fromIndex
, pData
->length
- fromIndex
,
882 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
884 libreoffice_internal::ConstCharArrayDetector
<T
>::length
,
885 libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
890 Match against a substring appearing in this string.
892 @param str the substring to be compared; must not be null and must point
893 to memory of at least strLength bytes
895 @param strLength the length of the substring; must be non-negative
897 @param fromIndex the index into this string to start the comparison at;
898 must be non-negative and not greater than this string's length
900 @return true if and only if the given str is contained as a substring of
901 this string at the given fromIndex
903 @since LibreOffice 3.6
906 char const * str
, sal_Int32 strLength
, sal_Int32 fromIndex
= 0)
909 return rtl_str_shortenedCompare_WithLength(
910 pData
->buffer
+ fromIndex
, pData
->length
- fromIndex
,
911 str
, strLength
, strLength
) == 0;
914 // This overload is left undefined, to detect calls of matchL that
915 // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
916 // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
918 #if SAL_TYPES_SIZEOFLONG == 8
919 void matchL(char const *, sal_Int32
, rtl_TextEncoding
) const;
923 Match against a substring appearing in this string, ignoring the case of
926 The result is true if and only if the second string appears as a substring
927 of this string, at the given position.
928 Character values between 65 and 90 (ASCII A-Z) are interpreted as
929 values between 97 and 122 (ASCII a-z).
930 This function can't be used for language specific comparison.
932 @param str the object (substring) to be compared.
933 @param fromIndex the index to start the comparison from.
934 The index must be greater or equal than 0
935 and less or equal as the string length.
936 @return true if str match with the characters in the string
937 at the given position;
940 #if defined LIBO_INTERNAL_ONLY
941 bool matchIgnoreAsciiCase( std::string_view str
, sal_Int32 fromIndex
= 0 ) const
943 return rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
944 str
.data(), str
.size(),
948 bool matchIgnoreAsciiCase( const OString
& str
, sal_Int32 fromIndex
= 0 ) const
950 return rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
951 str
.pData
->buffer
, str
.pData
->length
,
952 str
.pData
->length
) == 0;
957 This function accepts an ASCII string literal as its argument.
958 @since LibreOffice 3.6
960 template< typename T
>
961 typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
matchIgnoreAsciiCase( T
& literal
, sal_Int32 fromIndex
= 0 ) const
963 RTL_STRING_CONST_FUNCTION
965 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
967 rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
968 pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
969 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
971 libreoffice_internal::ConstCharArrayDetector
<T
>::length
,
972 libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
977 Check whether this string starts with a given substring.
979 @param str the substring to be compared
981 @param rest if non-null, and this function returns true, then assign a
982 copy of the remainder of this string to *rest. Available since
985 @return true if and only if the given str appears as a substring at the
988 @since LibreOffice 4.0
990 #if defined LIBO_INTERNAL_ONLY
991 bool startsWith(std::string_view str
, OString
* rest
= NULL
) const {
993 if (b
&& rest
!= NULL
) {
994 *rest
= copy(str
.size());
999 bool startsWith(OString
const & str
, OString
* rest
= NULL
) const {
1000 bool b
= match(str
);
1001 if (b
&& rest
!= NULL
) {
1002 *rest
= copy(str
.getLength());
1010 This function accepts an ASCII string literal as its argument.
1011 @since LibreOffice 4.0
1013 template< typename T
>
1014 typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
startsWith(
1015 T
& literal
, OString
* rest
= NULL
) const
1017 RTL_STRING_CONST_FUNCTION
1018 bool b
= match(literal
, 0);
1019 if (b
&& rest
!= NULL
) {
1021 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
1027 Check whether this string starts with a given string, ignoring the case of
1030 Character values between 65 and 90 (ASCII A-Z) are interpreted as
1031 values between 97 and 122 (ASCII a-z).
1032 This function can't be used for language specific comparison.
1034 @param str the substring to be compared
1036 @param rest if non-null, and this function returns true, then assign a
1037 copy of the remainder of this string to *rest.
1039 @return true if and only if the given str appears as a substring at the
1040 start of this string, ignoring the case of ASCII letters ("A"--"Z" and
1043 @since LibreOffice 5.1
1045 #if defined LIBO_INTERNAL_ONLY
1046 bool startsWithIgnoreAsciiCase(std::string_view str
, OString
* rest
= NULL
)
1049 bool b
= matchIgnoreAsciiCase(str
);
1050 if (b
&& rest
!= NULL
) {
1051 *rest
= copy(str
.size());
1056 bool startsWithIgnoreAsciiCase(OString
const & str
, OString
* rest
= NULL
)
1059 bool b
= matchIgnoreAsciiCase(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 5.1
1072 template< typename T
>
1073 typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
1074 startsWithIgnoreAsciiCase(T
& literal
, OString
* rest
= NULL
) const
1076 RTL_STRING_CONST_FUNCTION
1078 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
1079 bool b
= matchIgnoreAsciiCase(literal
);
1080 if (b
&& rest
!= NULL
) {
1082 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
1088 Check whether this string ends with a given substring.
1090 @param str the substring to be compared
1092 @param rest if non-null, and this function returns true, then assign a
1093 copy of the remainder of this string to *rest. Available since
1096 @return true if and only if the given str appears as a substring at the
1099 @since LibreOffice 3.6
1101 #if defined LIBO_INTERNAL_ONLY
1102 bool endsWith(std::string_view str
, OString
* rest
= NULL
) const {
1103 bool b
= str
.size() <= sal_uInt32(getLength())
1104 && match(str
, getLength() - str
.size());
1105 if (b
&& rest
!= NULL
) {
1106 *rest
= copy(0, getLength() - str
.size());
1111 bool endsWith(OString
const & str
, OString
* rest
= NULL
) const {
1112 bool b
= str
.getLength() <= getLength()
1113 && match(str
, getLength() - str
.getLength());
1114 if (b
&& rest
!= NULL
) {
1115 *rest
= copy(0, getLength() - str
.getLength());
1123 This function accepts an ASCII string literal as its argument.
1124 @since LibreOffice 3.6
1126 template< typename T
>
1127 typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
endsWith(
1128 T
& literal
, OString
* rest
= NULL
) const
1130 RTL_STRING_CONST_FUNCTION
1132 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
1134 = (libreoffice_internal::ConstCharArrayDetector
<T
>::length
1135 <= sal_uInt32(getLength()))
1137 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
1140 - libreoffice_internal::ConstCharArrayDetector
<T
>::length
));
1141 if (b
&& rest
!= NULL
) {
1145 - libreoffice_internal::ConstCharArrayDetector
<T
>::length
));
1151 Check whether this string ends with a given substring.
1153 @param str the substring to be compared; must not be null and must point
1154 to memory of at least strLength bytes
1156 @param strLength the length of the substring; must be non-negative
1158 @return true if and only if the given str appears as a substring at the
1161 @since LibreOffice 3.6
1163 bool endsWithL(char const * str
, sal_Int32 strLength
) const {
1164 return strLength
<= getLength()
1165 && matchL(str
, strLength
, getLength() - strLength
);
1168 friend bool operator == ( const OString
& rStr1
, const OString
& rStr2
)
1169 { return rStr1
.equals(rStr2
); }
1170 friend bool operator != ( const OString
& rStr1
, const OString
& rStr2
)
1171 { return !(operator == ( rStr1
, rStr2
)); }
1172 friend bool operator < ( const OString
& rStr1
, const OString
& rStr2
)
1173 { return rStr1
.compareTo( rStr2
) < 0; }
1174 friend bool operator > ( const OString
& rStr1
, const OString
& rStr2
)
1175 { return rStr1
.compareTo( rStr2
) > 0; }
1176 friend bool operator <= ( const OString
& rStr1
, const OString
& rStr2
)
1177 { return rStr1
.compareTo( rStr2
) <= 0; }
1178 friend bool operator >= ( const OString
& rStr1
, const OString
& rStr2
)
1179 { return rStr1
.compareTo( rStr2
) >= 0; }
1181 template< typename T
>
1182 friend typename
libreoffice_internal::CharPtrDetector
< T
, bool >::Type
operator==( const OString
& rStr1
, const T
& value
)
1184 return rStr1
.compareTo( value
) == 0;
1187 template< typename T
>
1188 friend typename
libreoffice_internal::NonConstCharArrayDetector
< T
, bool >::Type
operator==( const OString
& rStr1
, T
& value
)
1190 return rStr1
.compareTo( value
) == 0;
1193 template< typename T
>
1194 friend typename
libreoffice_internal::CharPtrDetector
< T
, bool >::Type
operator==( const T
& value
, const OString
& rStr2
)
1196 return rStr2
.compareTo( value
) == 0;
1199 template< typename T
>
1200 friend typename
libreoffice_internal::NonConstCharArrayDetector
< T
, bool >::Type
operator==( T
& value
, const OString
& rStr2
)
1202 return rStr2
.compareTo( value
) == 0;
1207 This function accepts an ASCII string literal as its argument.
1208 @since LibreOffice 3.6
1210 template< typename T
>
1211 friend typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
operator==( const OString
& rStr
, T
& literal
)
1213 RTL_STRING_CONST_FUNCTION
1215 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
1218 == libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
1219 && (rtl_str_compare_WithLength(
1220 rStr
.pData
->buffer
, rStr
.pData
->length
,
1221 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
1223 libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
1229 This function accepts an ASCII string literal as its argument.
1230 @since LibreOffice 3.6
1232 template< typename T
>
1233 friend typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
operator==( T
& literal
, const OString
& rStr
)
1235 RTL_STRING_CONST_FUNCTION
1237 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
1240 == libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
1241 && (rtl_str_compare_WithLength(
1242 rStr
.pData
->buffer
, rStr
.pData
->length
,
1243 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
1245 libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
1249 template< typename T
>
1250 friend typename
libreoffice_internal::CharPtrDetector
< T
, bool >::Type
operator!=( const OString
& rStr1
, const T
& value
)
1252 return !(operator == ( rStr1
, value
));
1255 template< typename T
>
1256 friend typename
libreoffice_internal::NonConstCharArrayDetector
< T
, bool >::Type
operator!=( const OString
& rStr1
, T
& value
)
1258 return !(operator == ( rStr1
, value
));
1261 template< typename T
>
1262 friend typename
libreoffice_internal::CharPtrDetector
< T
, bool >::Type
operator!=( const T
& value
, const OString
& rStr2
)
1264 return !(operator == ( value
, rStr2
));
1267 template< typename T
>
1268 friend typename
libreoffice_internal::NonConstCharArrayDetector
< T
, bool >::Type
operator!=( T
& value
, const OString
& rStr2
)
1270 return !(operator == ( value
, rStr2
));
1275 This function accepts an ASCII string literal as its argument.
1276 @since LibreOffice 3.6
1278 template< typename T
>
1279 friend typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
operator!=( const OString
& rStr
, T
& literal
)
1281 return !( rStr
== literal
);
1286 This function accepts an ASCII string literal as its argument.
1287 @since LibreOffice 3.6
1289 template< typename T
>
1290 friend typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
operator!=( T
& literal
, const OString
& rStr
)
1292 return !( literal
== rStr
);
1296 Returns a hashcode for this string.
1298 @return a hash code value for this object.
1300 @see rtl::OStringHash for convenient use of std::unordered_map
1302 sal_Int32
hashCode() const
1304 return rtl_str_hashCode_WithLength( pData
->buffer
, pData
->length
);
1308 Returns the index within this string of the first occurrence of the
1309 specified character, starting the search at the specified index.
1311 @param ch character to be located.
1312 @param fromIndex the index to start the search from.
1313 The index must be greater or equal than 0
1314 and less or equal as the string length.
1315 @return the index of the first occurrence of the character in the
1316 character sequence represented by this string that is
1317 greater than or equal to fromIndex, or
1318 -1 if the character does not occur.
1320 sal_Int32
indexOf( char ch
, sal_Int32 fromIndex
= 0 ) const
1322 sal_Int32 ret
= rtl_str_indexOfChar_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
, ch
);
1323 return (ret
< 0 ? ret
: ret
+fromIndex
);
1327 Returns the index within this string of the last occurrence of the
1328 specified character, searching backward starting at the end.
1330 @param ch character to be located.
1331 @return the index of the last occurrence of the character in the
1332 character sequence represented by this string, or
1333 -1 if the character does not occur.
1335 sal_Int32
lastIndexOf( char ch
) const
1337 return rtl_str_lastIndexOfChar_WithLength( pData
->buffer
, pData
->length
, ch
);
1341 Returns the index within this string of the last occurrence of the
1342 specified character, searching backward starting before the specified
1345 @param ch character to be located.
1346 @param fromIndex the index before which to start the search.
1347 @return the index of the last occurrence of the character in the
1348 character sequence represented by this string that
1349 is less than fromIndex, or -1
1350 if the character does not occur before that point.
1352 sal_Int32
lastIndexOf( char ch
, sal_Int32 fromIndex
) const
1354 return rtl_str_lastIndexOfChar_WithLength( pData
->buffer
, fromIndex
, ch
);
1358 Returns the index within this string of the first occurrence of the
1359 specified substring, starting at the specified index.
1361 If str doesn't include any character, always -1 is
1362 returned. This is also the case, if both strings are empty.
1364 @param str the substring to search for.
1365 @param fromIndex the index to start the search from.
1366 @return If the string argument occurs one or more times as a substring
1367 within this string at the starting index, then the index
1368 of the first character of the first such substring is
1369 returned. If it does not occur as a substring starting
1370 at fromIndex or beyond, -1 is returned.
1372 #if defined LIBO_INTERNAL_ONLY
1373 sal_Int32
indexOf( std::string_view str
, sal_Int32 fromIndex
= 0 ) const
1375 sal_Int32 ret
= rtl_str_indexOfStr_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
1376 str
.data(), str
.size() );
1377 return (ret
< 0 ? ret
: ret
+fromIndex
);
1380 sal_Int32
indexOf( const OString
& str
, sal_Int32 fromIndex
= 0 ) const
1382 sal_Int32 ret
= rtl_str_indexOfStr_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
1383 str
.pData
->buffer
, str
.pData
->length
);
1384 return (ret
< 0 ? ret
: ret
+fromIndex
);
1389 This function accepts an ASCII string literal as its argument.
1390 @since LibreOffice 3.6
1392 template< typename T
>
1393 typename
libreoffice_internal::ConstCharArrayDetector
< T
, sal_Int32
>::Type
indexOf( T
& literal
, sal_Int32 fromIndex
= 0 ) const
1395 RTL_STRING_CONST_FUNCTION
1397 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
1398 sal_Int32 n
= rtl_str_indexOfStr_WithLength(
1399 pData
->buffer
+ fromIndex
, pData
->length
- fromIndex
,
1400 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
1401 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
1402 return n
< 0 ? n
: n
+ fromIndex
;
1406 Returns the index within this string of the first occurrence of the
1407 specified substring, starting at the specified index.
1409 If str doesn't include any character, always -1 is
1410 returned. This is also the case, if both strings are empty.
1412 @param str the substring to search for.
1413 @param len the length of the substring.
1414 @param fromIndex the index to start the search from.
1415 @return If the string argument occurs one or more times as a substring
1416 within this string at the starting index, then the index
1417 of the first character of the first such substring is
1418 returned. If it does not occur as a substring starting
1419 at fromIndex or beyond, -1 is returned.
1421 @since LibreOffice 3.6
1423 sal_Int32
indexOfL(char const * str
, sal_Int32 len
, sal_Int32 fromIndex
= 0)
1426 sal_Int32 n
= rtl_str_indexOfStr_WithLength(
1427 pData
->buffer
+ fromIndex
, pData
->length
- fromIndex
, str
, len
);
1428 return n
< 0 ? n
: n
+ fromIndex
;
1431 // This overload is left undefined, to detect calls of indexOfL that
1432 // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
1433 // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
1435 #if SAL_TYPES_SIZEOFLONG == 8
1436 void indexOfL(char const *, sal_Int32
, rtl_TextEncoding
) const;
1440 Returns the index within this string of the last occurrence of
1441 the specified substring, searching backward starting at the end.
1443 The returned index indicates the starting index of the substring
1445 If str doesn't include any character, always -1 is
1446 returned. This is also the case, if both strings are empty.
1448 @param str the substring to search for.
1449 @return If the string argument occurs one or more times as a substring
1450 within this string, then the index of the first character of
1451 the last such substring is returned. If it does not occur as
1452 a substring, -1 is returned.
1454 #if defined LIBO_INTERNAL_ONLY
1455 sal_Int32
lastIndexOf( std::string_view str
) const
1457 return rtl_str_lastIndexOfStr_WithLength( pData
->buffer
, pData
->length
,
1458 str
.data(), str
.size() );
1461 sal_Int32
lastIndexOf( const OString
& str
) const
1463 return rtl_str_lastIndexOfStr_WithLength( pData
->buffer
, pData
->length
,
1464 str
.pData
->buffer
, str
.pData
->length
);
1469 Returns the index within this string of the last occurrence of
1470 the specified substring, searching backward starting before the specified
1473 The returned index indicates the starting index of the substring
1475 If str doesn't include any character, always -1 is
1476 returned. This is also the case, if both strings are empty.
1478 @param str the substring to search for.
1479 @param fromIndex the index before which to start the search.
1480 @return If the string argument occurs one or more times as a substring
1481 within this string before the starting index, then the index
1482 of the first character of the last such substring is
1483 returned. Otherwise, -1 is returned.
1485 #if defined LIBO_INTERNAL_ONLY
1486 sal_Int32
lastIndexOf( std::string_view str
, sal_Int32 fromIndex
) const
1488 return rtl_str_lastIndexOfStr_WithLength( pData
->buffer
, fromIndex
,
1489 str
.data(), str
.size() );
1492 sal_Int32
lastIndexOf( const OString
& str
, sal_Int32 fromIndex
) const
1494 return rtl_str_lastIndexOfStr_WithLength( pData
->buffer
, fromIndex
,
1495 str
.pData
->buffer
, str
.pData
->length
);
1500 Returns a new string that is a substring of this string.
1502 The substring begins at the specified beginIndex. If
1503 beginIndex is negative or be greater than the length of
1504 this string, behaviour is undefined.
1506 @param beginIndex the beginning index, inclusive.
1507 @return the specified substring.
1509 SAL_WARN_UNUSED_RESULT OString
copy( sal_Int32 beginIndex
) const
1511 return copy(beginIndex
, getLength() - beginIndex
);
1515 Returns a new string that is a substring of this string.
1517 The substring begins at the specified beginIndex and contains count
1518 characters. If either beginIndex or count are negative,
1519 or beginIndex + count are greater than the length of this string
1520 then behaviour is undefined.
1522 @param beginIndex the beginning index, inclusive.
1523 @param count the number of characters.
1524 @return the specified substring.
1526 SAL_WARN_UNUSED_RESULT OString
copy( sal_Int32 beginIndex
, sal_Int32 count
) const
1528 rtl_String
*pNew
= NULL
;
1529 rtl_string_newFromSubString( &pNew
, pData
, beginIndex
, count
);
1530 return OString( pNew
, SAL_NO_ACQUIRE
);
1533 #if defined LIBO_INTERNAL_ONLY
1535 Returns a std::string_view that is a view of a substring of this string.
1537 The substring begins at the specified beginIndex. If
1538 beginIndex is negative or be greater than the length of
1539 this string, behaviour is undefined.
1541 @param beginIndex the beginning index, inclusive.
1542 @return the specified substring.
1544 SAL_WARN_UNUSED_RESULT
std::string_view
subView( sal_Int32 beginIndex
) const
1546 assert(beginIndex
>= 0);
1547 assert(beginIndex
<= getLength());
1548 return subView(beginIndex
, getLength() - beginIndex
);
1552 Returns a std::string_view that is a view of a substring of this string.
1554 The substring begins at the specified beginIndex and contains count
1555 characters. If either beginIndex or count are negative,
1556 or beginIndex + count are greater than the length of this string
1557 then behaviour is undefined.
1559 @param beginIndex the beginning index, inclusive.
1560 @param count the number of characters.
1561 @return the specified substring.
1563 SAL_WARN_UNUSED_RESULT
std::string_view
subView( sal_Int32 beginIndex
, sal_Int32 count
) const
1565 assert(beginIndex
>= 0);
1567 assert(beginIndex
<= getLength());
1568 assert(count
<= getLength() - beginIndex
);
1569 return std::string_view(*this).substr(beginIndex
, count
);
1573 #ifndef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
1575 Concatenates the specified string to the end of this string.
1577 @param str the string that is concatenated to the end
1579 @return a string that represents the concatenation of this string
1580 followed by the string argument.
1582 SAL_WARN_UNUSED_RESULT OString
concat( const OString
& str
) const
1584 rtl_String
* pNew
= NULL
;
1585 rtl_string_newConcat( &pNew
, pData
, str
.pData
);
1586 return OString( pNew
, SAL_NO_ACQUIRE
);
1590 #ifndef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
1591 friend OString
operator+( const OString
& str1
, const OString
& str2
)
1593 return str1
.concat( str2
);
1597 // hide this from internal code to avoid ambiguous lookup error
1598 #ifndef LIBO_INTERNAL_ONLY
1600 Returns a new string resulting from replacing n = count characters
1601 from position index in this string with newStr.
1603 @param index the replacing index in str.
1604 The index must be greater or equal as 0 and
1605 less or equal as the length of the string.
1606 @param count the count of characters that will replaced
1607 The count must be greater or equal as 0 and
1608 less or equal as the length of the string minus index.
1609 @param newStr the new substring.
1610 @return the new string.
1612 SAL_WARN_UNUSED_RESULT OString
replaceAt( sal_Int32 index
, sal_Int32 count
, const OString
& newStr
) const
1614 rtl_String
* pNew
= NULL
;
1615 rtl_string_newReplaceStrAt( &pNew
, pData
, index
, count
, newStr
.pData
);
1616 return OString( pNew
, SAL_NO_ACQUIRE
);
1620 #ifdef LIBO_INTERNAL_ONLY
1621 SAL_WARN_UNUSED_RESULT OString
replaceAt( sal_Int32 index
, sal_Int32 count
, std::string_view newStr
) const
1623 rtl_String
* pNew
= NULL
;
1624 rtl_string_newReplaceStrAt_WithLength ( &pNew
, pData
, index
, count
, newStr
.data(), newStr
.size() );
1625 return OString( pNew
, SAL_NO_ACQUIRE
);
1630 Returns a new string resulting from replacing all occurrences of
1631 oldChar in this string with newChar.
1633 If the character oldChar does not occur in the character sequence
1634 represented by this object, then the string is assigned with
1637 @param oldChar the old character.
1638 @param newChar the new character.
1639 @return a string derived from this string by replacing every
1640 occurrence of oldChar with newChar.
1642 SAL_WARN_UNUSED_RESULT OString
replace( char oldChar
, char newChar
) const
1644 rtl_String
* pNew
= NULL
;
1645 rtl_string_newReplace( &pNew
, pData
, oldChar
, newChar
);
1646 return OString( pNew
, SAL_NO_ACQUIRE
);
1650 Returns a new string resulting from replacing the first occurrence of a
1651 given substring with another substring.
1653 @param from the substring to be replaced
1655 @param to the replacing substring
1657 @param[in,out] index pointer to a start index; if the pointer is
1658 non-null: upon entry to the function, its value is the index into the this
1659 string at which to start searching for the \p from substring, the value
1660 must be non-negative and not greater than this string's length; upon exit
1661 from the function its value is the index into this string at which the
1662 replacement took place or -1 if no replacement took place; if the pointer
1663 is null, searching always starts at index 0
1665 @since LibreOffice 3.6
1667 SAL_WARN_UNUSED_RESULT OString
replaceFirst(
1668 OString
const & from
, OString
const & to
, sal_Int32
* index
= NULL
) const
1670 rtl_String
* s
= NULL
;
1672 rtl_string_newReplaceFirst(
1673 &s
, pData
, from
.pData
->buffer
, from
.pData
->length
,
1674 to
.pData
->buffer
, to
.pData
->length
, index
== NULL
? &i
: index
);
1675 return OString(s
, SAL_NO_ACQUIRE
);
1679 Returns a new string resulting from replacing all occurrences of a given
1680 substring with another substring.
1682 Replacing subsequent occurrences picks up only after a given replacement.
1683 That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
1685 @param from the substring to be replaced
1687 @param to the replacing substring
1689 @since LibreOffice 3.6
1691 SAL_WARN_UNUSED_RESULT OString
replaceAll(OString
const & from
, OString
const & to
) const {
1692 rtl_String
* s
= NULL
;
1693 rtl_string_newReplaceAll(
1694 &s
, pData
, from
.pData
->buffer
, from
.pData
->length
,
1695 to
.pData
->buffer
, to
.pData
->length
);
1696 return OString(s
, SAL_NO_ACQUIRE
);
1700 Converts from this string all ASCII uppercase characters (65-90)
1701 to ASCII lowercase characters (97-122).
1703 This function can't be used for language specific conversion.
1704 If the string doesn't contain characters which must be converted,
1705 then the new string is assigned with str.
1707 @return the string, converted to ASCII lowercase.
1709 SAL_WARN_UNUSED_RESULT OString
toAsciiLowerCase() const
1711 rtl_String
* pNew
= NULL
;
1712 rtl_string_newToAsciiLowerCase( &pNew
, pData
);
1713 return OString( pNew
, SAL_NO_ACQUIRE
);
1717 Converts from this string all ASCII lowercase characters (97-122)
1718 to ASCII uppercase characters (65-90).
1720 This function can't be used for language specific conversion.
1721 If the string doesn't contain characters which must be converted,
1722 then the new string is assigned with str.
1724 @return the string, converted to ASCII uppercase.
1726 SAL_WARN_UNUSED_RESULT OString
toAsciiUpperCase() const
1728 rtl_String
* pNew
= NULL
;
1729 rtl_string_newToAsciiUpperCase( &pNew
, pData
);
1730 return OString( pNew
, SAL_NO_ACQUIRE
);
1734 Returns a new string resulting from removing white space from both ends
1737 All characters that have codes less than or equal to
1738 32 (the space character) are considered to be white space.
1739 If the string doesn't contain white spaces at both ends,
1740 then the new string is assigned with str.
1742 @return the string, with white space removed from the front and end.
1744 SAL_WARN_UNUSED_RESULT OString
trim() const
1746 rtl_String
* pNew
= NULL
;
1747 rtl_string_newTrim( &pNew
, pData
);
1748 return OString( pNew
, SAL_NO_ACQUIRE
);
1752 Returns a token in the string.
1755 sal_Int32 nIndex = 0;
1759 OString aToken = aStr.getToken( 0, ';', nIndex );
1762 while ( nIndex >= 0 );
1764 @param token the number of the token to return.
1765 @param cTok the character which separate the tokens.
1766 @param index the position at which the token is searched in the
1768 The index must not be greater than the length of the
1770 This param is set to the position of the
1771 next token or to -1, if it is the last token.
1772 @return the token; if either token or index is negative, an empty token
1773 is returned (and index is set to -1)
1775 OString
getToken( sal_Int32 token
, char cTok
, sal_Int32
& index
) const
1777 rtl_String
* pNew
= NULL
;
1778 index
= rtl_string_getToken( &pNew
, pData
, token
, cTok
, index
);
1779 return OString( pNew
, SAL_NO_ACQUIRE
);
1783 Returns a token from the string.
1785 The same as getToken(sal_Int32, char, sal_Int32 &), but always passing
1786 in 0 as the start index in the third argument.
1788 @param count the number of the token to return, starting with 0
1789 @param separator the character which separates the tokens
1791 @return the given token, or an empty string
1793 @since LibreOffice 3.6
1795 OString
getToken(sal_Int32 count
, char separator
) const {
1797 return getToken(count
, separator
, n
);
1801 Returns the Boolean value from this string.
1803 This function can't be used for language specific conversion.
1805 @return true, if the string is 1 or "True" in any ASCII case.
1806 false in any other case.
1808 bool toBoolean() const
1810 return rtl_str_toBoolean( pData
->buffer
);
1814 Returns the first character from this string.
1816 @return the first character from this string or 0, if this string
1821 return pData
->buffer
[0];
1825 Returns the int32 value from this string.
1827 This function can't be used for language specific conversion.
1829 @param radix the radix (between 2 and 36)
1830 @return the int32 represented from this string.
1831 0 if this string represents no number or one of too large
1834 sal_Int32
toInt32( sal_Int16 radix
= 10 ) const
1836 return rtl_str_toInt32( pData
->buffer
, radix
);
1840 Returns the uint32 value from this string.
1842 This function can't be used for language specific conversion.
1844 @param radix the radix (between 2 and 36)
1845 @return the uint32 represented from this string.
1846 0 if this string represents no number or one of too large
1849 @since LibreOffice 4.2
1851 sal_uInt32
toUInt32( sal_Int16 radix
= 10 ) const
1853 return rtl_str_toUInt32( pData
->buffer
, radix
);
1857 Returns the int64 value from this string.
1859 This function can't be used for language specific conversion.
1861 @param radix the radix (between 2 and 36)
1862 @return the int64 represented from this string.
1863 0 if this string represents no number or one of too large
1866 sal_Int64
toInt64( sal_Int16 radix
= 10 ) const
1868 return rtl_str_toInt64( pData
->buffer
, radix
);
1872 Returns the uint64 value from this string.
1874 This function can't be used for language specific conversion.
1876 @param radix the radix (between 2 and 36)
1877 @return the uint64 represented from this string.
1878 0 if this string represents no number or one of too large
1881 @since LibreOffice 4.1
1883 sal_uInt64
toUInt64( sal_Int16 radix
= 10 ) const
1885 return rtl_str_toUInt64( pData
->buffer
, radix
);
1889 Returns the float value from this string.
1891 This function can't be used for language specific conversion.
1893 @return the float represented from this string.
1894 0.0 if this string represents no number.
1896 float toFloat() const
1898 return rtl_str_toFloat( pData
->buffer
);
1902 Returns the double value from this string.
1904 This function can't be used for language specific conversion.
1906 @return the double represented from this string.
1907 0.0 if this string represents no number.
1909 double toDouble() const
1911 return rtl_str_toDouble( pData
->buffer
);
1914 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
1916 static OStringNumber
< int > number( int i
, sal_Int16 radix
= 10 )
1918 return OStringNumber
< int >( i
, radix
);
1920 static OStringNumber
< long long > number( long long ll
, sal_Int16 radix
= 10 )
1922 return OStringNumber
< long long >( ll
, radix
);
1924 static OStringNumber
< unsigned long long > number( unsigned long long ll
, sal_Int16 radix
= 10 )
1926 return OStringNumber
< unsigned long long >( ll
, radix
);
1928 static OStringNumber
< unsigned long long > number( unsigned int i
, sal_Int16 radix
= 10 )
1930 return number( static_cast< unsigned long long >( i
), radix
);
1932 static OStringNumber
< long long > number( long i
, sal_Int16 radix
= 10)
1934 return number( static_cast< long long >( i
), radix
);
1936 static OStringNumber
< unsigned long long > number( unsigned long i
, sal_Int16 radix
= 10 )
1938 return number( static_cast< unsigned long long >( i
), radix
);
1940 static OStringNumber
< float > number( float f
)
1942 return OStringNumber
< float >( f
);
1944 static OStringNumber
< double > number( double d
)
1946 return OStringNumber
< double >( d
);
1950 Returns the string representation of the integer argument.
1952 This function can't be used for language specific conversion.
1954 @param i an integer value
1955 @param radix the radix (between 2 and 36)
1956 @return a string with the string representation of the argument.
1957 @since LibreOffice 4.1
1959 static OString
number( int i
, sal_Int16 radix
= 10 )
1961 char aBuf
[RTL_STR_MAX_VALUEOFINT32
];
1962 return OString(aBuf
, rtl_str_valueOfInt32(aBuf
, i
, radix
));
1965 /// @since LibreOffice 4.1
1966 static OString
number( unsigned int i
, sal_Int16 radix
= 10 )
1968 return number( static_cast< unsigned long long >( i
), radix
);
1971 /// @since LibreOffice 4.1
1972 static OString
number( long i
, sal_Int16 radix
= 10 )
1974 return number( static_cast< long long >( i
), radix
);
1977 /// @since LibreOffice 4.1
1978 static OString
number( unsigned long i
, sal_Int16 radix
= 10 )
1980 return number( static_cast< unsigned long long >( i
), radix
);
1983 /// @since LibreOffice 4.1
1984 static OString
number( long long ll
, sal_Int16 radix
= 10 )
1986 char aBuf
[RTL_STR_MAX_VALUEOFINT64
];
1987 return OString(aBuf
, rtl_str_valueOfInt64(aBuf
, ll
, radix
));
1990 /// @since LibreOffice 4.1
1991 static OString
number( unsigned long long ll
, sal_Int16 radix
= 10 )
1993 char aBuf
[RTL_STR_MAX_VALUEOFUINT64
];
1994 return OString(aBuf
, rtl_str_valueOfUInt64(aBuf
, ll
, radix
));
1998 Returns the string representation of the float argument.
2000 This function can't be used for language specific conversion.
2003 @return a string with the decimal representation of the argument.
2004 @since LibreOffice 4.1
2006 static OString
number( float f
)
2008 char aBuf
[RTL_STR_MAX_VALUEOFFLOAT
];
2009 return OString(aBuf
, rtl_str_valueOfFloat(aBuf
, f
));
2013 Returns the string representation of the double argument.
2015 This function can't be used for language specific conversion.
2018 @return a string with the decimal representation of the argument.
2019 @since LibreOffice 4.1
2021 static OString
number( double d
)
2023 char aBuf
[RTL_STR_MAX_VALUEOFDOUBLE
];
2024 return OString(aBuf
, rtl_str_valueOfDouble(aBuf
, d
));
2029 Returns the string representation of the sal_Bool argument.
2031 If the sal_Bool is true, the string "true" is returned.
2032 If the sal_Bool is false, the string "false" is returned.
2033 This function can't be used for language specific conversion.
2035 @param b a sal_Bool.
2036 @return a string with the string representation of the argument.
2037 @deprecated use boolean()
2039 SAL_DEPRECATED("use boolean()") static OString
valueOf( sal_Bool b
)
2045 Returns the string representation of the boolean argument.
2047 If the argument is true, the string "true" is returned.
2048 If the argument is false, the string "false" is returned.
2049 This function can't be used for language specific conversion.
2052 @return a string with the string representation of the argument.
2053 @since LibreOffice 4.1
2055 static OString
boolean( bool b
)
2057 char aBuf
[RTL_STR_MAX_VALUEOFBOOLEAN
];
2058 return OString(aBuf
, rtl_str_valueOfBoolean(aBuf
, b
));
2062 Returns the string representation of the char argument.
2064 @param c a character.
2065 @return a string with the string representation of the argument.
2066 @deprecated use operator, function or constructor taking char or sal_Unicode argument
2068 SAL_DEPRECATED("convert to OString or use directly") static OString
valueOf( char c
)
2070 return OString( &c
, 1 );
2074 Returns the string representation of the int argument.
2076 This function can't be used for language specific conversion.
2079 @param radix the radix (between 2 and 36)
2080 @return a string with the string representation of the argument.
2081 @deprecated use number()
2083 SAL_DEPRECATED("use number()") static OString
valueOf( sal_Int32 i
, sal_Int16 radix
= 10 )
2085 return number( i
, radix
);
2089 Returns the string representation of the long argument.
2091 This function can't be used for language specific conversion.
2094 @param radix the radix (between 2 and 36)
2095 @return a string with the string representation of the argument.
2096 @deprecated use number()
2098 SAL_DEPRECATED("use number()") static OString
valueOf( sal_Int64 ll
, sal_Int16 radix
= 10 )
2100 return number( ll
, radix
);
2104 Returns the string representation of the float argument.
2106 This function can't be used for language specific conversion.
2109 @return a string with the string representation of the argument.
2110 @deprecated use number()
2112 SAL_DEPRECATED("use number()") static OString
valueOf( float f
)
2118 Returns the string representation of the double argument.
2120 This function can't be used for language specific conversion.
2123 @return a string with the string representation of the argument.
2124 @deprecated use number()
2126 SAL_DEPRECATED("use number()") static OString
valueOf( double d
)
2131 #if defined LIBO_INTERNAL_ONLY
2132 operator std::string_view() const { return {getStr(), sal_uInt32(getLength())}; }
2135 #if defined LIBO_INTERNAL_ONLY
2136 // A wrapper for the first expression in an
2138 // OString::Concat(e1) + e2 + ...
2140 // concatenation chain, when neither of the first two e1, e2 is one of our rtl string-related
2141 // classes (so something like
2143 // OString s = "a" + (b ? std::string_view("c") : std::string_view("dd"));
2145 // would not compile):
2146 template<typename T
> [[nodiscard
]] static
2147 typename
std::enable_if_t
<
2148 ToStringHelper
<T
>::allowOStringConcat
, OStringConcat
<OStringConcatMarker
, T
>>
2149 Concat(T
const & value
) { return OStringConcat
<OStringConcatMarker
, T
>({}, value
); }
2151 // This overload is needed so that an argument of type 'char const[N]' ends up as
2152 // 'OStringConcat<rtl::OStringConcatMarker, char const[N]>' rather than as
2153 // 'OStringConcat<rtl::OStringConcatMarker, char[N]>':
2154 template<typename T
, std::size_t N
> [[nodiscard
]] static
2155 typename
std::enable_if_t
<
2156 ToStringHelper
<T
[N
]>::allowOStringConcat
, OStringConcat
<OStringConcatMarker
, T
[N
]>>
2157 Concat(T (& value
)[N
]) { return OStringConcat
<OStringConcatMarker
, T
[N
]>({}, value
); }
2161 #if defined LIBO_INTERNAL_ONLY
2162 inline bool operator ==(OString
const & lhs
, OStringConcatenation
const & rhs
)
2163 { return lhs
== std::string_view(rhs
); }
2164 inline bool operator !=(OString
const & lhs
, OStringConcatenation
const & rhs
)
2165 { return lhs
!= std::string_view(rhs
); }
2166 inline bool operator ==(OStringConcatenation
const & lhs
, OString
const & rhs
)
2167 { return std::string_view(lhs
) == rhs
; }
2168 inline bool operator !=(OStringConcatenation
const & lhs
, OString
const & rhs
)
2169 { return std::string_view(lhs
) != rhs
; }
2172 /* ======================================================================= */
2174 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
2180 struct ToStringHelper
< OString
>
2182 static std::size_t length( const OString
& s
) { return s
.getLength(); }
2183 static char* addData( char* buffer
, const OString
& s
) { return addDataHelper( buffer
, s
.getStr(), s
.getLength()); }
2184 static const bool allowOStringConcat
= true;
2185 static const bool allowOUStringConcat
= false;
2191 template<std::size_t N
>
2192 struct ToStringHelper
< OStringLiteral
<N
> >
2194 static constexpr std::size_t length( const OStringLiteral
<N
>& str
) { return str
.getLength(); }
2195 static char* addData( char* buffer
, const OStringLiteral
<N
>& str
) { return addDataHelper( buffer
, str
.getStr(), str
.getLength() ); }
2196 static const bool allowOStringConcat
= true;
2197 static const bool allowOUStringConcat
= false;
2203 template< typename charT
, typename traits
, typename T1
, typename T2
>
2204 inline std::basic_ostream
<charT
, traits
> & operator <<(
2205 std::basic_ostream
<charT
, traits
> & stream
, OStringConcat
< T1
, T2
>&& concat
)
2207 return stream
<< OString( std::move(concat
) );
2212 /** A helper to use OStrings with hash maps.
2214 Instances of this class are unary function objects that can be used as
2215 hash function arguments to std::unordered_map and similar constructs.
2219 /** Compute a hash code for a string.
2225 a hash code for the string. This hash code should not be stored
2226 persistently, as its computation may change in later revisions.
2228 size_t operator()( const OString
& rString
) const
2229 { return static_cast<size_t>(rString
.hashCode()); }
2232 /** Equality functor for classic c-strings (i.e., null-terminated char* strings). */
2235 bool operator()( const char* p1
, const char* p2
) const
2236 { return rtl_str_compare(p1
, p2
) == 0; }
2239 /** Hashing functor for classic c-strings (i.e., null-terminated char* strings). */
2242 size_t operator()(const char* p
) const
2243 { return rtl_str_hashCode(p
); }
2246 /* ======================================================================= */
2249 Support for rtl::OString in std::ostream (and thus in
2250 CPPUNIT_ASSERT or SAL_INFO macros, for example).
2252 @since LibreOffice 4.0
2254 template< typename charT
, typename traits
> std::basic_ostream
<charT
, traits
> &
2256 std::basic_ostream
<charT
, traits
> & stream
, OString
const & rString
)
2258 return stream
<< rString
.getStr();
2259 // best effort; potentially loses data due to embedded null characters
2264 #ifdef RTL_STRING_UNITTEST
2267 typedef rtlunittest::OString OString
;
2269 #undef RTL_STRING_CONST_FUNCTION
2272 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
2273 using ::rtl::OString
;
2274 using ::rtl::OStringChar
;
2275 using ::rtl::OStringConcatenation
;
2276 using ::rtl::OStringHash
;
2277 using ::rtl::OStringLiteral
;
2282 Make OString hashable by default for use in STL containers.
2284 @since LibreOffice 6.0
2286 #if defined LIBO_INTERNAL_ONLY
2290 struct hash
<::rtl::OString
>
2292 std::size_t operator()(::rtl::OString
const & s
) const
2293 { return std::size_t(s
.hashCode()); }
2301 #endif // INCLUDED_RTL_STRING_HXX
2303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */