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 .
20 #ifndef INCLUDED_RTL_STRING_HXX
21 #define INCLUDED_RTL_STRING_HXX
23 #include "sal/config.h"
34 #if defined LIBO_INTERNAL_ONLY
35 #include <string_view>
36 #include <type_traits>
39 #include "rtl/textenc.h"
40 #include "rtl/string.h"
41 #include "rtl/stringutils.hxx"
43 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
44 #include "config_global.h"
45 #include "rtl/stringconcat.hxx"
48 #ifdef RTL_STRING_UNITTEST
49 extern bool rtl_string_unittest_const_literal
;
50 extern bool rtl_string_unittest_const_literal_function
;
53 // The unittest uses slightly different code to help check that the proper
54 // calls are made. The class is put into a different namespace to make
55 // sure the compiler generates a different (if generating also non-inline)
56 // copy of the function and does not merge them together. The class
57 // is "brought" into the proper rtl namespace by a typedef below.
58 #ifdef RTL_STRING_UNITTEST
59 #define rtl rtlunittest
66 #ifdef RTL_STRING_UNITTEST
68 // helper macro to make functions appear more readable
69 #define RTL_STRING_CONST_FUNCTION rtl_string_unittest_const_literal_function = true;
71 #define RTL_STRING_CONST_FUNCTION
75 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
77 A wrapper dressing a string literal as a static-refcount rtl_String.
79 This class is not part of public API and is meant to be used only in LibreOffice code.
80 @since LibreOffice 4.0
82 template<std::size_t N
> class SAL_WARN_UNUSED OStringLiteral
{
83 static_assert(N
!= 0);
84 static_assert(N
- 1 <= std::numeric_limits
<sal_Int32
>::max(), "literal too long");
87 #if HAVE_CPP_CONSTEVAL
92 explicit OStringLiteral(char const (&literal
)[N
]) {
94 assert(literal
[N
- 1] == '\0');
95 //TODO: Use C++20 constexpr std::copy_n (P0202R3):
96 for (std::size_t i
= 0; i
!= N
; ++i
) {
97 buffer
[i
] = literal
[i
];
101 #if defined __cpp_char8_t
102 #if HAVE_CPP_CONSTEVAL
107 explicit OStringLiteral(char8_t
const (&literal
)[N
]) {
109 assert(literal
[N
- 1] == '\0');
110 //TODO: Use C++20 constexpr std::copy_n (P0202R3):
111 for (std::size_t i
= 0; i
!= N
; ++i
) {
112 buffer
[i
] = literal
[i
];
117 constexpr sal_Int32
getLength() const { return length
; }
119 constexpr char const * getStr() const SAL_RETURNS_NONNULL
{ return buffer
; }
122 static constexpr void assertLayout() {
123 // These static_asserts verifying the layout compatibility with rtl_String cannot be class
124 // member declarations, as offsetof requires a complete type, so defer them to here:
125 static_assert(offsetof(OStringLiteral
, refCount
) == offsetof(rtl_String
, refCount
));
127 std::is_same_v
<decltype(refCount
), decltype(rtl_String::refCount
)>);
128 static_assert(offsetof(OStringLiteral
, length
) == offsetof(rtl_String
, length
));
129 static_assert(std::is_same_v
<decltype(length
), decltype(rtl_String::length
)>);
130 static_assert(offsetof(OStringLiteral
, buffer
) == offsetof(rtl_String
, buffer
));
133 std::remove_extent_t
<decltype(buffer
)>,
134 std::remove_extent_t
<decltype(rtl_String::buffer
)>>);
137 // Same layout as rtl_String (include/rtl/string.h):
138 oslInterlockedCount refCount
= 0x40000000; // SAL_STRING_STATIC_FLAG (sal/rtl/strimp.hxx)
139 sal_Int32 length
= N
- 1;
140 char buffer
[N
] = {}; //TODO: drop initialization for C++20 (P1331R2)
144 /* ======================================================================= */
147 This String class provide base functionality for C++ like 8-Bit
148 character array handling. The advantage of this class is, that it
149 handle all the memory management for you - and it do it
150 more efficient. If you assign a string to another string, the
151 data of both strings are shared (without any copy operation or
152 memory allocation) as long as you do not change the string. This class
153 stores also the length of the string, so that many operations are
154 faster as the C-str-functions.
156 This class provides only readonly string handling. So you could create
157 a string and you could only query the content from this string.
158 It provides also functionality to change the string, but this results
159 in every case in a new string instance (in the most cases with an
160 memory allocation). You don't have functionality to change the
161 content of the string. If you want to change the string content, then
162 you should use the OStringBuffer class, which provides these
163 functionalities and avoid too much memory allocation.
165 The design of this class is similar to the string classes in Java
166 and so more people should have fewer understanding problems when they
170 class SAL_WARN_UNUSED SAL_DLLPUBLIC_RTTI OString
178 New string containing no characters.
183 rtl_string_new( &pData
);
187 New string from OString.
189 @param str an OString.
191 OString( const OString
& str
)
194 rtl_string_acquire( pData
);
197 #if defined LIBO_INTERNAL_ONLY
201 @param str an OString.
202 @since LibreOffice 5.2
204 OString( OString
&& str
) noexcept
208 rtl_string_new( &str
.pData
);
213 New string from OString data.
215 @param str an OString data.
217 OString( rtl_String
* str
)
220 rtl_string_acquire( pData
);
223 /** New string from OString data without acquiring it. Takeover of ownership.
225 The SAL_NO_ACQUIRE dummy parameter is only there to distinguish this
226 from other constructors.
228 @param str an OString data.
230 OString( rtl_String
* str
, __sal_NoAcquire
)
236 New string from a single character.
238 @param value a character.
240 explicit OString( char value
)
243 rtl_string_newFromStr_WithLength( &pData
, &value
, 1 );
247 New string from a character buffer array.
249 Note: The argument type is always either char* or const char*. The template is
250 used only for technical reasons, as is the second argument.
252 @param value a NULL-terminated character array.
254 template< typename T
>
255 OString( const T
& value
, typename
libreoffice_internal::CharPtrDetector
< T
, libreoffice_internal::Dummy
>::Type
= libreoffice_internal::Dummy() )
258 rtl_string_newFromStr( &pData
, value
);
261 template< typename T
>
262 OString( T
& value
, typename
libreoffice_internal::NonConstCharArrayDetector
< T
, libreoffice_internal::Dummy
>::Type
= libreoffice_internal::Dummy() )
265 rtl_string_newFromStr( &pData
, value
);
269 New string from a string literal.
271 If there are any embedded \0's in the string literal, the result is undefined.
272 Use the overload that explicitly accepts length.
274 @since LibreOffice 3.6
276 @param literal a string literal
278 template< typename T
>
279 OString( T
& literal
, typename
libreoffice_internal::ConstCharArrayDetector
< T
, libreoffice_internal::Dummy
>::Type
= libreoffice_internal::Dummy() )
282 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
284 if (libreoffice_internal::ConstCharArrayDetector
<T
>::length
== 0) {
285 rtl_string_new(&pData
);
287 rtl_string_newFromLiteral(
289 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
291 libreoffice_internal::ConstCharArrayDetector
<T
>::length
, 0);
293 #ifdef RTL_STRING_UNITTEST
294 rtl_string_unittest_const_literal
= true;
299 New string from a character buffer array.
301 @param value a character array.
302 @param length the number of character which should be copied.
303 The character array length must be greater or
304 equal than this value.
306 OString( const char * value
, sal_Int32 length
)
309 rtl_string_newFromStr_WithLength( &pData
, value
, length
);
312 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
315 New string from an 8-Bit string literal.
317 @since LibreOffice 7.1
319 template<std::size_t N
> OString(OStringLiteral
<N
> const & literal
):
320 pData(const_cast<rtl_String
*>(reinterpret_cast<rtl_String
const *>(&literal
))) {}
321 template<std::size_t N
> OString(OStringLiteral
<N
> &&) = delete;
325 #if defined LIBO_INTERNAL_ONLY
326 OString(std::string_view sv
) {
327 if (sv
.size() > sal_uInt32(std::numeric_limits
<sal_Int32
>::max())) {
328 throw std::bad_alloc();
331 rtl_string_newFromStr_WithLength(&pData
, sv
.data(), sv
.size());
336 New string from a Unicode character buffer array.
338 @param value a Unicode character array.
339 @param length the number of character which should be converted.
340 The Unicode character array length must be
341 greater or equal than this value.
342 @param encoding the text encoding in which the Unicode character
343 sequence should be converted.
344 @param convertFlags flags which controls the conversion.
345 see RTL_UNICODETOTEXT_FLAGS_...
347 @exception std::bad_alloc is thrown if an out-of-memory condition occurs
349 OString( const sal_Unicode
* value
, sal_Int32 length
,
350 rtl_TextEncoding encoding
,
351 sal_uInt32 convertFlags
= OUSTRING_TO_OSTRING_CVTFLAGS
)
354 rtl_uString2String( &pData
, value
, length
, encoding
, convertFlags
);
356 throw std::bad_alloc();
360 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
365 template< typename T1
, typename T2
>
366 OString( OStringConcat
< T1
, T2
>&& c
)
368 const sal_Int32 l
= c
.length();
369 pData
= rtl_string_alloc( l
);
372 char* end
= c
.addData( pData
->buffer
);
382 template< typename T
>
383 OString( OStringNumber
< T
>&& n
)
384 : OString( n
.buf
, n
.length
)
388 #ifdef LIBO_INTERNAL_ONLY
389 OString(std::nullptr_t
) = delete;
393 Release the string data.
397 rtl_string_release( pData
);
403 @param str an OString.
405 OString
& operator=( const OString
& str
)
407 rtl_string_assign( &pData
, str
.pData
);
411 #if defined LIBO_INTERNAL_ONLY
413 Move assign a new string.
415 @param str an OString.
416 @since LibreOffice 5.2
418 OString
& operator=( OString
&& str
) noexcept
420 rtl_string_release( pData
);
423 rtl_string_new( &str
.pData
);
430 This function accepts an ASCII string literal as its argument.
431 @since LibreOffice 3.6
433 template< typename T
>
434 typename
libreoffice_internal::ConstCharArrayDetector
< T
, OString
& >::Type
operator=( T
& literal
)
436 RTL_STRING_CONST_FUNCTION
438 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
439 if (libreoffice_internal::ConstCharArrayDetector
<T
>::length
== 0) {
440 rtl_string_new(&pData
);
442 rtl_string_newFromLiteral(
444 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
446 libreoffice_internal::ConstCharArrayDetector
<T
>::length
, 0);
452 Append a string to this string.
454 @param str an OString.
456 OString
& operator+=( const OString
& str
)
457 #if defined LIBO_INTERNAL_ONLY
461 rtl_string_newConcat( &pData
, pData
, str
.pData
);
464 #if defined LIBO_INTERNAL_ONLY
465 void operator+=(OString
const &) && = delete;
468 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
473 template< typename T1
, typename T2
>
474 OString
& operator+=( OStringConcat
< T1
, T2
>&& c
) & {
475 sal_Int32 l
= c
.length();
479 rtl_string_ensureCapacity( &pData
, l
);
480 char* end
= c
.addData( pData
->buffer
+ pData
->length
);
485 template<typename T1
, typename T2
> void operator +=(
486 OStringConcat
<T1
, T2
> &&) && = delete;
492 template< typename T
>
493 OString
& operator+=( OStringNumber
< T
>&& n
) & {
494 sal_Int32 l
= n
.length
;
498 rtl_string_ensureCapacity( &pData
, l
);
499 char* end
= addDataHelper( pData
->buffer
+ pData
->length
, n
.buf
, n
.length
);
504 template<typename T
> void operator +=(
505 OStringNumber
<T
> &&) && = delete;
509 Clears the string, i.e, makes a zero-character string
510 @since LibreOffice 4.4
514 rtl_string_new( &pData
);
518 Returns the length of this string.
520 The length is equal to the number of characters in this string.
522 @return the length of the sequence of characters represented by this
525 sal_Int32
getLength() const { return pData
->length
; }
528 Checks if a string is empty.
530 @return true if the string is empty;
533 @since LibreOffice 3.4
537 return pData
->length
== 0;
541 Returns a pointer to the characters of this string.
543 <p>The returned pointer is guaranteed to point to a null-terminated byte
544 string. But note that this string object may contain embedded null
545 characters, which will thus also be embedded in the returned
546 null-terminated byte string.</p>
548 @return a pointer to a null-terminated byte string representing the
549 characters of this string object.
551 const char * getStr() const SAL_RETURNS_NONNULL
{ return pData
->buffer
; }
554 Access to individual characters.
556 @param index must be non-negative and less than length.
558 @return the character at the given index.
560 @since LibreOffice 3.5
562 char operator [](sal_Int32 index
) const {
563 // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
564 assert(index
>= 0 && static_cast<sal_uInt32
>(index
) < static_cast<sal_uInt32
>(getLength()));
565 return getStr()[index
];
569 Compares two strings.
571 The comparison is based on the numeric value of each character in
572 the strings and return a value indicating their relationship.
573 This function can't be used for language specific sorting.
575 @param str the object to be compared.
576 @return 0 - if both strings are equal
577 < 0 - if this string is less than the string argument
578 > 0 - if this string is greater than the string argument
580 sal_Int32
compareTo( const OString
& str
) const
582 return rtl_str_compare_WithLength( pData
->buffer
, pData
->length
,
583 str
.pData
->buffer
, str
.pData
->length
);
587 Compares two strings with an maximum count of characters.
589 The comparison is based on the numeric value of each character in
590 the strings and return a value indicating their relationship.
591 This function can't be used for language specific sorting.
593 @param rObj the object to be compared.
594 @param maxLength the maximum count of characters to be compared.
595 @return 0 - if both strings are equal
596 < 0 - if this string is less than the string argument
597 > 0 - if this string is greater than the string argument
599 sal_Int32
compareTo( const OString
& rObj
, sal_Int32 maxLength
) const
601 return rtl_str_shortenedCompare_WithLength( pData
->buffer
, pData
->length
,
602 rObj
.pData
->buffer
, rObj
.pData
->length
, maxLength
);
606 Compares two strings in reverse order.
608 The comparison is based on the numeric value of each character in
609 the strings and return a value indicating their relationship.
610 This function can't be used for language specific sorting.
612 @param str the object to be compared.
613 @return 0 - if both strings are equal
614 < 0 - if this string is less than the string argument
615 > 0 - if this string is greater than the string argument
617 sal_Int32
reverseCompareTo( const OString
& str
) const
619 return rtl_str_reverseCompare_WithLength( pData
->buffer
, pData
->length
,
620 str
.pData
->buffer
, str
.pData
->length
);
624 Perform a comparison of two strings.
626 The result is true if and only if second string
627 represents the same sequence of characters as the first string.
628 This function can't be used for language specific comparison.
630 @param str the object to be compared.
631 @return true if the strings are equal;
634 bool equals( const OString
& str
) const
636 if ( pData
->length
!= str
.pData
->length
)
638 if ( pData
== str
.pData
)
640 return rtl_str_reverseCompare_WithLength( pData
->buffer
, pData
->length
,
641 str
.pData
->buffer
, str
.pData
->length
) == 0;
645 Perform a comparison of two strings.
647 The result is true if and only if second string
648 represents the same sequence of characters as the first string.
649 The ASCII string must be NULL-terminated and must be greater or
651 This function can't be used for language specific comparison.
654 @param value a character array.
655 @param length the length of the character array.
656 @return true if the strings are equal;
659 bool equalsL( const char* value
, sal_Int32 length
) const
661 if ( pData
->length
!= length
)
664 return rtl_str_reverseCompare_WithLength( pData
->buffer
, pData
->length
,
665 value
, length
) == 0;
669 Perform an ASCII lowercase comparison of two strings.
671 The result is true if and only if second string
672 represents the same sequence of characters as the first string,
674 Character values between 65 and 90 (ASCII A-Z) are interpreted as
675 values between 97 and 122 (ASCII a-z).
676 This function can't be used for language specific comparison.
678 @param str the object to be compared.
679 @return true if the strings are equal;
682 bool equalsIgnoreAsciiCase( const OString
& str
) const
684 if ( pData
->length
!= str
.pData
->length
)
686 if ( pData
== str
.pData
)
688 return rtl_str_compareIgnoreAsciiCase_WithLength( pData
->buffer
, pData
->length
,
689 str
.pData
->buffer
, str
.pData
->length
) == 0;
693 Perform an ASCII lowercase comparison of two strings.
695 The result is true if and only if second string
696 represents the same sequence of characters as the first string,
698 Character values between 65 and 90 (ASCII A-Z) are interpreted as
699 values between 97 and 122 (ASCII a-z).
700 Since this method is optimized for performance, the ASCII character
701 values are not converted in any way. The caller has to make sure that
702 all ASCII characters are in the allowed range between 0 and
703 127. The ASCII string must be NULL-terminated.
704 This function can't be used for language specific comparison.
706 Note: The argument type is always either char* or const char*, the return type is bool.
707 The template is used only for technical reasons.
709 @param asciiStr the 8-Bit ASCII character string to be compared.
710 @return true if the strings are equal;
713 template< typename T
>
714 typename
libreoffice_internal::CharPtrDetector
< T
, bool >::Type
equalsIgnoreAsciiCase( const T
& asciiStr
) const
716 return rtl_str_compareIgnoreAsciiCase( pData
->buffer
, asciiStr
) == 0;
719 template< typename T
>
720 typename
libreoffice_internal::NonConstCharArrayDetector
< T
, bool >::Type
equalsIgnoreAsciiCase( T
& asciiStr
) const
722 return rtl_str_compareIgnoreAsciiCase( pData
->buffer
, asciiStr
) == 0;
727 This function accepts an ASCII string literal as its argument.
728 @since LibreOffice 3.6
730 template< typename T
>
731 typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
equalsIgnoreAsciiCase( T
& literal
) const
733 RTL_STRING_CONST_FUNCTION
735 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
738 == libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
739 && (rtl_str_compareIgnoreAsciiCase_WithLength(
740 pData
->buffer
, pData
->length
,
741 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
743 libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
748 Perform an ASCII lowercase comparison of two strings.
750 The result is true if and only if second string
751 represents the same sequence of characters as the first string,
753 Character values between 65 and 90 (ASCII A-Z) are interpreted as
754 values between 97 and 122 (ASCII a-z).
755 Since this method is optimized for performance, the ASCII character
756 values are not converted in any way. The caller has to make sure that
757 all ASCII characters are in the allowed range between 0 and
758 127. The ASCII string must be greater or equal in length as asciiStrLength.
759 This function can't be used for language specific comparison.
761 @param asciiStr the 8-Bit ASCII character string to be compared.
762 @param asciiStrLength the length of the ascii string
763 @return true if the strings are equal;
766 bool equalsIgnoreAsciiCaseL( const char * asciiStr
, sal_Int32 asciiStrLength
) const
768 if ( pData
->length
!= asciiStrLength
)
771 return rtl_str_compareIgnoreAsciiCase_WithLength( pData
->buffer
, pData
->length
,
772 asciiStr
, asciiStrLength
) == 0;
776 Match against a substring appearing in this string.
778 The result is true if and only if the second string appears as a substring
779 of this string, at the given position.
780 This function can't be used for language specific comparison.
782 @param str the object (substring) to be compared.
783 @param fromIndex the index to start the comparison from.
784 The index must be greater or equal than 0
785 and less or equal as the string length.
786 @return true if str match with the characters in the string
787 at the given position;
790 bool match( const OString
& str
, sal_Int32 fromIndex
= 0 ) const
792 return rtl_str_shortenedCompare_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
793 str
.pData
->buffer
, str
.pData
->length
, str
.pData
->length
) == 0;
798 This function accepts an ASCII string literal as its argument.
799 @since LibreOffice 3.6
801 template< typename T
>
802 typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
match( T
& literal
, sal_Int32 fromIndex
= 0 ) const
804 RTL_STRING_CONST_FUNCTION
806 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
808 rtl_str_shortenedCompare_WithLength(
809 pData
->buffer
+ fromIndex
, pData
->length
- fromIndex
,
810 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
812 libreoffice_internal::ConstCharArrayDetector
<T
>::length
,
813 libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
818 Match against a substring appearing in this string.
820 @param str the substring to be compared; must not be null and must point
821 to memory of at least strLength bytes
823 @param strLength the length of the substring; must be non-negative
825 @param fromIndex the index into this string to start the comparison at;
826 must be non-negative and not greater than this string's length
828 @return true if and only if the given str is contained as a substring of
829 this string at the given fromIndex
831 @since LibreOffice 3.6
834 char const * str
, sal_Int32 strLength
, sal_Int32 fromIndex
= 0)
837 return rtl_str_shortenedCompare_WithLength(
838 pData
->buffer
+ fromIndex
, pData
->length
- fromIndex
,
839 str
, strLength
, strLength
) == 0;
842 // This overload is left undefined, to detect calls of matchL that
843 // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
844 // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
846 #if SAL_TYPES_SIZEOFLONG == 8
847 void matchL(char const *, sal_Int32
, rtl_TextEncoding
) const;
851 Match against a substring appearing in this string, ignoring the case of
854 The result is true if and only if the second string appears as a substring
855 of this string, at the given position.
856 Character values between 65 and 90 (ASCII A-Z) are interpreted as
857 values between 97 and 122 (ASCII a-z).
858 This function can't be used for language specific comparison.
860 @param str the object (substring) to be compared.
861 @param fromIndex the index to start the comparison from.
862 The index must be greater or equal than 0
863 and less or equal as the string length.
864 @return true if str match with the characters in the string
865 at the given position;
868 bool matchIgnoreAsciiCase( const OString
& str
, sal_Int32 fromIndex
= 0 ) const
870 return rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
871 str
.pData
->buffer
, str
.pData
->length
,
872 str
.pData
->length
) == 0;
877 This function accepts an ASCII string literal as its argument.
878 @since LibreOffice 3.6
880 template< typename T
>
881 typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
matchIgnoreAsciiCase( T
& literal
, sal_Int32 fromIndex
= 0 ) const
883 RTL_STRING_CONST_FUNCTION
885 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
887 rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
888 pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
889 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
891 libreoffice_internal::ConstCharArrayDetector
<T
>::length
,
892 libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
897 Check whether this string starts with a given substring.
899 @param str the substring to be compared
901 @param rest if non-null, and this function returns true, then assign a
902 copy of the remainder of this string to *rest. Available since
905 @return true if and only if the given str appears as a substring at the
908 @since LibreOffice 4.0
910 bool startsWith(OString
const & str
, OString
* rest
= NULL
) const {
912 if (b
&& rest
!= NULL
) {
913 *rest
= copy(str
.getLength());
920 This function accepts an ASCII string literal as its argument.
921 @since LibreOffice 4.0
923 template< typename T
>
924 typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
startsWith(
925 T
& literal
, OString
* rest
= NULL
) const
927 RTL_STRING_CONST_FUNCTION
928 bool b
= match(literal
, 0);
929 if (b
&& rest
!= NULL
) {
931 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
937 Check whether this string starts with a given string, ignoring the case of
940 Character values between 65 and 90 (ASCII A-Z) are interpreted as
941 values between 97 and 122 (ASCII a-z).
942 This function can't be used for language specific comparison.
944 @param str the substring to be compared
946 @param rest if non-null, and this function returns true, then assign a
947 copy of the remainder of this string to *rest.
949 @return true if and only if the given str appears as a substring at the
950 start of this string, ignoring the case of ASCII letters ("A"--"Z" and
953 @since LibreOffice 5.1
955 bool startsWithIgnoreAsciiCase(OString
const & str
, OString
* rest
= NULL
)
958 bool b
= matchIgnoreAsciiCase(str
);
959 if (b
&& rest
!= NULL
) {
960 *rest
= copy(str
.getLength());
967 This function accepts an ASCII string literal as its argument.
968 @since LibreOffice 5.1
970 template< typename T
>
971 typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
972 startsWithIgnoreAsciiCase(T
& literal
, OString
* rest
= NULL
) const
974 RTL_STRING_CONST_FUNCTION
976 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
977 bool b
= matchIgnoreAsciiCase(literal
);
978 if (b
&& rest
!= NULL
) {
980 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
986 Check whether this string ends with a given substring.
988 @param str the substring to be compared
990 @param rest if non-null, and this function returns true, then assign a
991 copy of the remainder of this string to *rest. Available since
994 @return true if and only if the given str appears as a substring at the
997 @since LibreOffice 3.6
999 bool endsWith(OString
const & str
, OString
* rest
= NULL
) const {
1000 bool b
= str
.getLength() <= getLength()
1001 && match(str
, getLength() - str
.getLength());
1002 if (b
&& rest
!= NULL
) {
1003 *rest
= copy(0, getLength() - str
.getLength());
1010 This function accepts an ASCII string literal as its argument.
1011 @since LibreOffice 3.6
1013 template< typename T
>
1014 typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
endsWith(
1015 T
& literal
, OString
* rest
= NULL
) const
1017 RTL_STRING_CONST_FUNCTION
1019 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
1021 = (libreoffice_internal::ConstCharArrayDetector
<T
>::length
1022 <= sal_uInt32(getLength()))
1024 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
1027 - libreoffice_internal::ConstCharArrayDetector
<T
>::length
));
1028 if (b
&& rest
!= NULL
) {
1032 - libreoffice_internal::ConstCharArrayDetector
<T
>::length
));
1038 Check whether this string ends with a given substring.
1040 @param str the substring to be compared; must not be null and must point
1041 to memory of at least strLength bytes
1043 @param strLength the length of the substring; must be non-negative
1045 @return true if and only if the given str appears as a substring at the
1048 @since LibreOffice 3.6
1050 bool endsWithL(char const * str
, sal_Int32 strLength
) const {
1051 return strLength
<= getLength()
1052 && matchL(str
, strLength
, getLength() - strLength
);
1055 friend bool operator == ( const OString
& rStr1
, const OString
& rStr2
)
1056 { return rStr1
.equals(rStr2
); }
1057 friend bool operator != ( const OString
& rStr1
, const OString
& rStr2
)
1058 { return !(operator == ( rStr1
, rStr2
)); }
1059 friend bool operator < ( const OString
& rStr1
, const OString
& rStr2
)
1060 { return rStr1
.compareTo( rStr2
) < 0; }
1061 friend bool operator > ( const OString
& rStr1
, const OString
& rStr2
)
1062 { return rStr1
.compareTo( rStr2
) > 0; }
1063 friend bool operator <= ( const OString
& rStr1
, const OString
& rStr2
)
1064 { return rStr1
.compareTo( rStr2
) <= 0; }
1065 friend bool operator >= ( const OString
& rStr1
, const OString
& rStr2
)
1066 { return rStr1
.compareTo( rStr2
) >= 0; }
1068 template< typename T
>
1069 friend typename
libreoffice_internal::CharPtrDetector
< T
, bool >::Type
operator==( const OString
& rStr1
, const T
& value
)
1071 return rStr1
.compareTo( value
) == 0;
1074 template< typename T
>
1075 friend typename
libreoffice_internal::NonConstCharArrayDetector
< T
, bool >::Type
operator==( const OString
& rStr1
, T
& value
)
1077 return rStr1
.compareTo( value
) == 0;
1080 template< typename T
>
1081 friend typename
libreoffice_internal::CharPtrDetector
< T
, bool >::Type
operator==( const T
& value
, const OString
& rStr2
)
1083 return rStr2
.compareTo( value
) == 0;
1086 template< typename T
>
1087 friend typename
libreoffice_internal::NonConstCharArrayDetector
< T
, bool >::Type
operator==( T
& value
, const OString
& rStr2
)
1089 return rStr2
.compareTo( value
) == 0;
1094 This function accepts an ASCII string literal as its argument.
1095 @since LibreOffice 3.6
1097 template< typename T
>
1098 friend typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
operator==( const OString
& rStr
, T
& literal
)
1100 RTL_STRING_CONST_FUNCTION
1102 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
1105 == libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
1106 && (rtl_str_compare_WithLength(
1107 rStr
.pData
->buffer
, rStr
.pData
->length
,
1108 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
1110 libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
1116 This function accepts an ASCII string literal as its argument.
1117 @since LibreOffice 3.6
1119 template< typename T
>
1120 friend typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
operator==( T
& literal
, const OString
& rStr
)
1122 RTL_STRING_CONST_FUNCTION
1124 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
1127 == libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
1128 && (rtl_str_compare_WithLength(
1129 rStr
.pData
->buffer
, rStr
.pData
->length
,
1130 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(
1132 libreoffice_internal::ConstCharArrayDetector
<T
>::length
)
1136 template< typename T
>
1137 friend typename
libreoffice_internal::CharPtrDetector
< T
, bool >::Type
operator!=( const OString
& rStr1
, const T
& value
)
1139 return !(operator == ( rStr1
, value
));
1142 template< typename T
>
1143 friend typename
libreoffice_internal::NonConstCharArrayDetector
< T
, bool >::Type
operator!=( const OString
& rStr1
, T
& value
)
1145 return !(operator == ( rStr1
, value
));
1148 template< typename T
>
1149 friend typename
libreoffice_internal::CharPtrDetector
< T
, bool >::Type
operator!=( const T
& value
, const OString
& rStr2
)
1151 return !(operator == ( value
, rStr2
));
1154 template< typename T
>
1155 friend typename
libreoffice_internal::NonConstCharArrayDetector
< T
, bool >::Type
operator!=( T
& value
, const OString
& rStr2
)
1157 return !(operator == ( value
, rStr2
));
1162 This function accepts an ASCII string literal as its argument.
1163 @since LibreOffice 3.6
1165 template< typename T
>
1166 friend typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
operator!=( const OString
& rStr
, T
& literal
)
1168 return !( rStr
== literal
);
1173 This function accepts an ASCII string literal as its argument.
1174 @since LibreOffice 3.6
1176 template< typename T
>
1177 friend typename
libreoffice_internal::ConstCharArrayDetector
< T
, bool >::Type
operator!=( T
& literal
, const OString
& rStr
)
1179 return !( literal
== rStr
);
1183 Returns a hashcode for this string.
1185 @return a hash code value for this object.
1187 @see rtl::OStringHash for convenient use of std::unordered_map
1189 sal_Int32
hashCode() const
1191 return rtl_str_hashCode_WithLength( pData
->buffer
, pData
->length
);
1195 Returns the index within this string of the first occurrence of the
1196 specified character, starting the search at the specified index.
1198 @param ch character to be located.
1199 @param fromIndex the index to start the search from.
1200 The index must be greater or equal than 0
1201 and less or equal as the string length.
1202 @return the index of the first occurrence of the character in the
1203 character sequence represented by this string that is
1204 greater than or equal to fromIndex, or
1205 -1 if the character does not occur.
1207 sal_Int32
indexOf( char ch
, sal_Int32 fromIndex
= 0 ) const
1209 sal_Int32 ret
= rtl_str_indexOfChar_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
, ch
);
1210 return (ret
< 0 ? ret
: ret
+fromIndex
);
1214 Returns the index within this string of the last occurrence of the
1215 specified character, searching backward starting at the end.
1217 @param ch character to be located.
1218 @return the index of the last occurrence of the character in the
1219 character sequence represented by this string, or
1220 -1 if the character does not occur.
1222 sal_Int32
lastIndexOf( char ch
) const
1224 return rtl_str_lastIndexOfChar_WithLength( pData
->buffer
, pData
->length
, ch
);
1228 Returns the index within this string of the last occurrence of the
1229 specified character, searching backward starting before the specified
1232 @param ch character to be located.
1233 @param fromIndex the index before which to start the search.
1234 @return the index of the last occurrence of the character in the
1235 character sequence represented by this string that
1236 is less than fromIndex, or -1
1237 if the character does not occur before that point.
1239 sal_Int32
lastIndexOf( char ch
, sal_Int32 fromIndex
) const
1241 return rtl_str_lastIndexOfChar_WithLength( pData
->buffer
, fromIndex
, ch
);
1245 Returns the index within this string of the first occurrence of the
1246 specified substring, starting at the specified index.
1248 If str doesn't include any character, always -1 is
1249 returned. This is also the case, if both strings are empty.
1251 @param str the substring to search for.
1252 @param fromIndex the index to start the search from.
1253 @return If the string argument occurs one or more times as a substring
1254 within this string at the starting index, then the index
1255 of the first character of the first such substring is
1256 returned. If it does not occur as a substring starting
1257 at fromIndex or beyond, -1 is returned.
1259 sal_Int32
indexOf( const OString
& str
, sal_Int32 fromIndex
= 0 ) const
1261 sal_Int32 ret
= rtl_str_indexOfStr_WithLength( pData
->buffer
+fromIndex
, pData
->length
-fromIndex
,
1262 str
.pData
->buffer
, str
.pData
->length
);
1263 return (ret
< 0 ? ret
: ret
+fromIndex
);
1268 This function accepts an ASCII string literal as its argument.
1269 @since LibreOffice 3.6
1271 template< typename T
>
1272 typename
libreoffice_internal::ConstCharArrayDetector
< T
, sal_Int32
>::Type
indexOf( T
& literal
, sal_Int32 fromIndex
= 0 ) const
1274 RTL_STRING_CONST_FUNCTION
1276 libreoffice_internal::ConstCharArrayDetector
<T
>::isValid(literal
));
1277 sal_Int32 n
= rtl_str_indexOfStr_WithLength(
1278 pData
->buffer
+ fromIndex
, pData
->length
- fromIndex
,
1279 libreoffice_internal::ConstCharArrayDetector
<T
>::toPointer(literal
),
1280 libreoffice_internal::ConstCharArrayDetector
<T
>::length
);
1281 return n
< 0 ? n
: n
+ fromIndex
;
1285 Returns the index within this string of the first occurrence of the
1286 specified substring, starting at the specified index.
1288 If str doesn't include any character, always -1 is
1289 returned. This is also the case, if both strings are empty.
1291 @param str the substring to search for.
1292 @param len the length of the substring.
1293 @param fromIndex the index to start the search from.
1294 @return If the string argument occurs one or more times as a substring
1295 within this string at the starting index, then the index
1296 of the first character of the first such substring is
1297 returned. If it does not occur as a substring starting
1298 at fromIndex or beyond, -1 is returned.
1300 @since LibreOffice 3.6
1302 sal_Int32
indexOfL(char const * str
, sal_Int32 len
, sal_Int32 fromIndex
= 0)
1305 sal_Int32 n
= rtl_str_indexOfStr_WithLength(
1306 pData
->buffer
+ fromIndex
, pData
->length
- fromIndex
, str
, len
);
1307 return n
< 0 ? n
: n
+ fromIndex
;
1310 // This overload is left undefined, to detect calls of indexOfL that
1311 // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
1312 // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
1314 #if SAL_TYPES_SIZEOFLONG == 8
1315 void indexOfL(char const *, sal_Int32
, rtl_TextEncoding
) const;
1319 Returns the index within this string of the last occurrence of
1320 the specified substring, searching backward starting at the end.
1322 The returned index indicates the starting index of the substring
1324 If str doesn't include any character, always -1 is
1325 returned. This is also the case, if both strings are empty.
1327 @param str the substring to search for.
1328 @return If the string argument occurs one or more times as a substring
1329 within this string, then the index of the first character of
1330 the last such substring is returned. If it does not occur as
1331 a substring, -1 is returned.
1333 sal_Int32
lastIndexOf( const OString
& str
) const
1335 return rtl_str_lastIndexOfStr_WithLength( pData
->buffer
, pData
->length
,
1336 str
.pData
->buffer
, str
.pData
->length
);
1340 Returns the index within this string of the last occurrence of
1341 the specified substring, searching backward starting before the specified
1344 The returned index indicates the starting index of the substring
1346 If str doesn't include any character, always -1 is
1347 returned. This is also the case, if both strings are empty.
1349 @param str the substring to search for.
1350 @param fromIndex the index before which to start the search.
1351 @return If the string argument occurs one or more times as a substring
1352 within this string before the starting index, then the index
1353 of the first character of the last such substring is
1354 returned. Otherwise, -1 is returned.
1356 sal_Int32
lastIndexOf( const OString
& str
, sal_Int32 fromIndex
) const
1358 return rtl_str_lastIndexOfStr_WithLength( pData
->buffer
, fromIndex
,
1359 str
.pData
->buffer
, str
.pData
->length
);
1363 Returns a new string that is a substring of this string.
1365 The substring begins at the specified beginIndex. If
1366 beginIndex is negative or be greater than the length of
1367 this string, behaviour is undefined.
1369 @param beginIndex the beginning index, inclusive.
1370 @return the specified substring.
1372 SAL_WARN_UNUSED_RESULT OString
copy( sal_Int32 beginIndex
) const
1374 return copy(beginIndex
, getLength() - beginIndex
);
1378 Returns a new string that is a substring of this string.
1380 The substring begins at the specified beginIndex and contains count
1381 characters. If either beginIndex or count are negative,
1382 or beginIndex + count are greater than the length of this string
1383 then behaviour is undefined.
1385 @param beginIndex the beginning index, inclusive.
1386 @param count the number of characters.
1387 @return the specified substring.
1389 SAL_WARN_UNUSED_RESULT OString
copy( sal_Int32 beginIndex
, sal_Int32 count
) const
1391 rtl_String
*pNew
= NULL
;
1392 rtl_string_newFromSubString( &pNew
, pData
, beginIndex
, count
);
1393 return OString( pNew
, SAL_NO_ACQUIRE
);
1396 #if defined LIBO_INTERNAL_ONLY
1398 Returns a std::string_view that is a view of a substring of this string.
1400 The substring begins at the specified beginIndex. If
1401 beginIndex is negative or be greater than the length of
1402 this string, behaviour is undefined.
1404 @param beginIndex the beginning index, inclusive.
1405 @return the specified substring.
1407 SAL_WARN_UNUSED_RESULT
std::string_view
subView( sal_Int32 beginIndex
) const
1409 assert(beginIndex
>= 0);
1410 assert(beginIndex
<= getLength());
1411 return subView(beginIndex
, getLength() - beginIndex
);
1415 Returns a std::string_view that is a view of a substring of this string.
1417 The substring begins at the specified beginIndex and contains count
1418 characters. If either beginIndex or count are negative,
1419 or beginIndex + count are greater than the length of this string
1420 then behaviour is undefined.
1422 @param beginIndex the beginning index, inclusive.
1423 @param count the number of characters.
1424 @return the specified substring.
1426 SAL_WARN_UNUSED_RESULT
std::string_view
subView( sal_Int32 beginIndex
, sal_Int32 count
) const
1428 assert(beginIndex
>= 0);
1430 assert(beginIndex
<= getLength());
1431 assert(count
<= getLength() - beginIndex
);
1432 return std::string_view(*this).substr(beginIndex
, count
);
1436 #ifndef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
1438 Concatenates the specified string to the end of this string.
1440 @param str the string that is concatenated to the end
1442 @return a string that represents the concatenation of this string
1443 followed by the string argument.
1445 SAL_WARN_UNUSED_RESULT OString
concat( const OString
& str
) const
1447 rtl_String
* pNew
= NULL
;
1448 rtl_string_newConcat( &pNew
, pData
, str
.pData
);
1449 return OString( pNew
, SAL_NO_ACQUIRE
);
1453 #ifndef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
1454 friend OString
operator+( const OString
& str1
, const OString
& str2
)
1456 return str1
.concat( str2
);
1461 Returns a new string resulting from replacing n = count characters
1462 from position index in this string with newStr.
1464 @param index the replacing index in str.
1465 The index must be greater or equal as 0 and
1466 less or equal as the length of the string.
1467 @param count the count of characters that will replaced
1468 The count must be greater or equal as 0 and
1469 less or equal as the length of the string minus index.
1470 @param newStr the new substring.
1471 @return the new string.
1473 SAL_WARN_UNUSED_RESULT OString
replaceAt( sal_Int32 index
, sal_Int32 count
, const OString
& newStr
) const
1475 rtl_String
* pNew
= NULL
;
1476 rtl_string_newReplaceStrAt( &pNew
, pData
, index
, count
, newStr
.pData
);
1477 return OString( pNew
, SAL_NO_ACQUIRE
);
1481 Returns a new string resulting from replacing all occurrences of
1482 oldChar in this string with newChar.
1484 If the character oldChar does not occur in the character sequence
1485 represented by this object, then the string is assigned with
1488 @param oldChar the old character.
1489 @param newChar the new character.
1490 @return a string derived from this string by replacing every
1491 occurrence of oldChar with newChar.
1493 SAL_WARN_UNUSED_RESULT OString
replace( char oldChar
, char newChar
) const
1495 rtl_String
* pNew
= NULL
;
1496 rtl_string_newReplace( &pNew
, pData
, oldChar
, newChar
);
1497 return OString( pNew
, SAL_NO_ACQUIRE
);
1501 Returns a new string resulting from replacing the first occurrence of a
1502 given substring with another substring.
1504 @param from the substring to be replaced
1506 @param to the replacing substring
1508 @param[in,out] index pointer to a start index; if the pointer is
1509 non-null: upon entry to the function, its value is the index into the this
1510 string at which to start searching for the \p from substring, the value
1511 must be non-negative and not greater than this string's length; upon exit
1512 from the function its value is the index into this string at which the
1513 replacement took place or -1 if no replacement took place; if the pointer
1514 is null, searching always starts at index 0
1516 @since LibreOffice 3.6
1518 SAL_WARN_UNUSED_RESULT OString
replaceFirst(
1519 OString
const & from
, OString
const & to
, sal_Int32
* index
= NULL
) const
1521 rtl_String
* s
= NULL
;
1523 rtl_string_newReplaceFirst(
1524 &s
, pData
, from
.pData
->buffer
, from
.pData
->length
,
1525 to
.pData
->buffer
, to
.pData
->length
, index
== NULL
? &i
: index
);
1526 return OString(s
, SAL_NO_ACQUIRE
);
1530 Returns a new string resulting from replacing all occurrences of a given
1531 substring with another substring.
1533 Replacing subsequent occurrences picks up only after a given replacement.
1534 That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
1536 @param from the substring to be replaced
1538 @param to the replacing substring
1540 @since LibreOffice 3.6
1542 SAL_WARN_UNUSED_RESULT OString
replaceAll(OString
const & from
, OString
const & to
) const {
1543 rtl_String
* s
= NULL
;
1544 rtl_string_newReplaceAll(
1545 &s
, pData
, from
.pData
->buffer
, from
.pData
->length
,
1546 to
.pData
->buffer
, to
.pData
->length
);
1547 return OString(s
, SAL_NO_ACQUIRE
);
1551 Converts from this string all ASCII uppercase characters (65-90)
1552 to ASCII lowercase characters (97-122).
1554 This function can't be used for language specific conversion.
1555 If the string doesn't contain characters which must be converted,
1556 then the new string is assigned with str.
1558 @return the string, converted to ASCII lowercase.
1560 SAL_WARN_UNUSED_RESULT OString
toAsciiLowerCase() const
1562 rtl_String
* pNew
= NULL
;
1563 rtl_string_newToAsciiLowerCase( &pNew
, pData
);
1564 return OString( pNew
, SAL_NO_ACQUIRE
);
1568 Converts from this string all ASCII lowercase characters (97-122)
1569 to ASCII uppercase characters (65-90).
1571 This function can't be used for language specific conversion.
1572 If the string doesn't contain characters which must be converted,
1573 then the new string is assigned with str.
1575 @return the string, converted to ASCII uppercase.
1577 SAL_WARN_UNUSED_RESULT OString
toAsciiUpperCase() const
1579 rtl_String
* pNew
= NULL
;
1580 rtl_string_newToAsciiUpperCase( &pNew
, pData
);
1581 return OString( pNew
, SAL_NO_ACQUIRE
);
1585 Returns a new string resulting from removing white space from both ends
1588 All characters that have codes less than or equal to
1589 32 (the space character) are considered to be white space.
1590 If the string doesn't contain white spaces at both ends,
1591 then the new string is assigned with str.
1593 @return the string, with white space removed from the front and end.
1595 SAL_WARN_UNUSED_RESULT OString
trim() const
1597 rtl_String
* pNew
= NULL
;
1598 rtl_string_newTrim( &pNew
, pData
);
1599 return OString( pNew
, SAL_NO_ACQUIRE
);
1603 Returns a token in the string.
1606 sal_Int32 nIndex = 0;
1610 OString aToken = aStr.getToken( 0, ';', nIndex );
1613 while ( nIndex >= 0 );
1615 @param token the number of the token to return.
1616 @param cTok the character which separate the tokens.
1617 @param index the position at which the token is searched in the
1619 The index must not be greater than the length of the
1621 This param is set to the position of the
1622 next token or to -1, if it is the last token.
1623 @return the token; if either token or index is negative, an empty token
1624 is returned (and index is set to -1)
1626 OString
getToken( sal_Int32 token
, char cTok
, sal_Int32
& index
) const
1628 rtl_String
* pNew
= NULL
;
1629 index
= rtl_string_getToken( &pNew
, pData
, token
, cTok
, index
);
1630 return OString( pNew
, SAL_NO_ACQUIRE
);
1634 Returns a token from the string.
1636 The same as getToken(sal_Int32, char, sal_Int32 &), but always passing
1637 in 0 as the start index in the third argument.
1639 @param count the number of the token to return, starting with 0
1640 @param separator the character which separates the tokens
1642 @return the given token, or an empty string
1644 @since LibreOffice 3.6
1646 OString
getToken(sal_Int32 count
, char separator
) const {
1648 return getToken(count
, separator
, n
);
1652 Returns the Boolean value from this string.
1654 This function can't be used for language specific conversion.
1656 @return true, if the string is 1 or "True" in any ASCII case.
1657 false in any other case.
1659 bool toBoolean() const
1661 return rtl_str_toBoolean( pData
->buffer
);
1665 Returns the first character from this string.
1667 @return the first character from this string or 0, if this string
1672 return pData
->buffer
[0];
1676 Returns the int32 value from this string.
1678 This function can't be used for language specific conversion.
1680 @param radix the radix (between 2 and 36)
1681 @return the int32 represented from this string.
1682 0 if this string represents no number or one of too large
1685 sal_Int32
toInt32( sal_Int16 radix
= 10 ) const
1687 return rtl_str_toInt32( pData
->buffer
, radix
);
1691 Returns the uint32 value from this string.
1693 This function can't be used for language specific conversion.
1695 @param radix the radix (between 2 and 36)
1696 @return the uint32 represented from this string.
1697 0 if this string represents no number or one of too large
1700 @since LibreOffice 4.2
1702 sal_uInt32
toUInt32( sal_Int16 radix
= 10 ) const
1704 return rtl_str_toUInt32( pData
->buffer
, radix
);
1708 Returns the int64 value from this string.
1710 This function can't be used for language specific conversion.
1712 @param radix the radix (between 2 and 36)
1713 @return the int64 represented from this string.
1714 0 if this string represents no number or one of too large
1717 sal_Int64
toInt64( sal_Int16 radix
= 10 ) const
1719 return rtl_str_toInt64( pData
->buffer
, radix
);
1723 Returns the uint64 value from this string.
1725 This function can't be used for language specific conversion.
1727 @param radix the radix (between 2 and 36)
1728 @return the uint64 represented from this string.
1729 0 if this string represents no number or one of too large
1732 @since LibreOffice 4.1
1734 sal_uInt64
toUInt64( sal_Int16 radix
= 10 ) const
1736 return rtl_str_toUInt64( pData
->buffer
, radix
);
1740 Returns the float value from this string.
1742 This function can't be used for language specific conversion.
1744 @return the float represented from this string.
1745 0.0 if this string represents no number.
1747 float toFloat() const
1749 return rtl_str_toFloat( pData
->buffer
);
1753 Returns the double value from this string.
1755 This function can't be used for language specific conversion.
1757 @return the double represented from this string.
1758 0.0 if this string represents no number.
1760 double toDouble() const
1762 return rtl_str_toDouble( pData
->buffer
);
1765 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
1767 static OStringNumber
< int > number( int i
, sal_Int16 radix
= 10 )
1769 return OStringNumber
< int >( i
, radix
);
1771 static OStringNumber
< long long > number( long long ll
, sal_Int16 radix
= 10 )
1773 return OStringNumber
< long long >( ll
, radix
);
1775 static OStringNumber
< unsigned long long > number( unsigned long long ll
, sal_Int16 radix
= 10 )
1777 return OStringNumber
< unsigned long long >( ll
, radix
);
1779 static OStringNumber
< unsigned long long > number( unsigned int i
, sal_Int16 radix
= 10 )
1781 return number( static_cast< unsigned long long >( i
), radix
);
1783 static OStringNumber
< long long > number( long i
, sal_Int16 radix
= 10)
1785 return number( static_cast< long long >( i
), radix
);
1787 static OStringNumber
< unsigned long long > number( unsigned long i
, sal_Int16 radix
= 10 )
1789 return number( static_cast< unsigned long long >( i
), radix
);
1791 static OStringNumber
< float > number( float f
)
1793 return OStringNumber
< float >( f
);
1795 static OStringNumber
< double > number( double d
)
1797 return OStringNumber
< double >( d
);
1801 Returns the string representation of the integer argument.
1803 This function can't be used for language specific conversion.
1805 @param i an integer value
1806 @param radix the radix (between 2 and 36)
1807 @return a string with the string representation of the argument.
1808 @since LibreOffice 4.1
1810 static OString
number( int i
, sal_Int16 radix
= 10 )
1812 char aBuf
[RTL_STR_MAX_VALUEOFINT32
];
1813 return OString(aBuf
, rtl_str_valueOfInt32(aBuf
, i
, radix
));
1816 /// @since LibreOffice 4.1
1817 static OString
number( unsigned int i
, sal_Int16 radix
= 10 )
1819 return number( static_cast< unsigned long long >( i
), radix
);
1822 /// @since LibreOffice 4.1
1823 static OString
number( long i
, sal_Int16 radix
= 10 )
1825 return number( static_cast< long long >( i
), radix
);
1828 /// @since LibreOffice 4.1
1829 static OString
number( unsigned long i
, sal_Int16 radix
= 10 )
1831 return number( static_cast< unsigned long long >( i
), radix
);
1834 /// @since LibreOffice 4.1
1835 static OString
number( long long ll
, sal_Int16 radix
= 10 )
1837 char aBuf
[RTL_STR_MAX_VALUEOFINT64
];
1838 return OString(aBuf
, rtl_str_valueOfInt64(aBuf
, ll
, radix
));
1841 /// @since LibreOffice 4.1
1842 static OString
number( unsigned long long ll
, sal_Int16 radix
= 10 )
1844 char aBuf
[RTL_STR_MAX_VALUEOFUINT64
];
1845 return OString(aBuf
, rtl_str_valueOfUInt64(aBuf
, ll
, radix
));
1849 Returns the string representation of the float argument.
1851 This function can't be used for language specific conversion.
1854 @return a string with the decimal representation of the argument.
1855 @since LibreOffice 4.1
1857 static OString
number( float f
)
1859 char aBuf
[RTL_STR_MAX_VALUEOFFLOAT
];
1860 return OString(aBuf
, rtl_str_valueOfFloat(aBuf
, f
));
1864 Returns the string representation of the double argument.
1866 This function can't be used for language specific conversion.
1869 @return a string with the decimal representation of the argument.
1870 @since LibreOffice 4.1
1872 static OString
number( double d
)
1874 char aBuf
[RTL_STR_MAX_VALUEOFDOUBLE
];
1875 return OString(aBuf
, rtl_str_valueOfDouble(aBuf
, d
));
1880 Returns the string representation of the sal_Bool argument.
1882 If the sal_Bool is true, the string "true" is returned.
1883 If the sal_Bool is false, the string "false" is returned.
1884 This function can't be used for language specific conversion.
1886 @param b a sal_Bool.
1887 @return a string with the string representation of the argument.
1888 @deprecated use boolean()
1890 SAL_DEPRECATED("use boolean()") static OString
valueOf( sal_Bool b
)
1896 Returns the string representation of the boolean argument.
1898 If the argument is true, the string "true" is returned.
1899 If the argument is false, the string "false" is returned.
1900 This function can't be used for language specific conversion.
1903 @return a string with the string representation of the argument.
1904 @since LibreOffice 4.1
1906 static OString
boolean( bool b
)
1908 char aBuf
[RTL_STR_MAX_VALUEOFBOOLEAN
];
1909 return OString(aBuf
, rtl_str_valueOfBoolean(aBuf
, b
));
1913 Returns the string representation of the char argument.
1915 @param c a character.
1916 @return a string with the string representation of the argument.
1917 @deprecated use operator, function or constructor taking char or sal_Unicode argument
1919 SAL_DEPRECATED("convert to OString or use directly") static OString
valueOf( char c
)
1921 return OString( &c
, 1 );
1925 Returns the string representation of the int argument.
1927 This function can't be used for language specific conversion.
1930 @param radix the radix (between 2 and 36)
1931 @return a string with the string representation of the argument.
1932 @deprecated use number()
1934 SAL_DEPRECATED("use number()") static OString
valueOf( sal_Int32 i
, sal_Int16 radix
= 10 )
1936 return number( i
, radix
);
1940 Returns the string representation of the long argument.
1942 This function can't be used for language specific conversion.
1945 @param radix the radix (between 2 and 36)
1946 @return a string with the string representation of the argument.
1947 @deprecated use number()
1949 SAL_DEPRECATED("use number()") static OString
valueOf( sal_Int64 ll
, sal_Int16 radix
= 10 )
1951 return number( ll
, radix
);
1955 Returns the string representation of the float argument.
1957 This function can't be used for language specific conversion.
1960 @return a string with the string representation of the argument.
1961 @deprecated use number()
1963 SAL_DEPRECATED("use number()") static OString
valueOf( float f
)
1969 Returns the string representation of the double argument.
1971 This function can't be used for language specific conversion.
1974 @return a string with the string representation of the argument.
1975 @deprecated use number()
1977 SAL_DEPRECATED("use number()") static OString
valueOf( double d
)
1982 #if defined LIBO_INTERNAL_ONLY
1983 operator std::string_view() const { return {getStr(), sal_uInt32(getLength())}; }
1986 #if defined LIBO_INTERNAL_ONLY
1987 // A wrapper for the first expression in an
1989 // OString::Concat(e1) + e2 + ...
1991 // concatenation chain, when neither of the first two e1, e2 is one of our rtl string-related
1992 // classes (so something like
1994 // OString s = "a" + (b ? std::string_view("c") : std::string_view("dd"));
1996 // would not compile):
1997 template<typename T
> [[nodiscard
]] static
1998 typename
std::enable_if_t
<
1999 ToStringHelper
<T
>::allowOStringConcat
, OStringConcat
<OStringConcatMarker
, T
>>
2000 Concat(T
const & value
) { return OStringConcat
<OStringConcatMarker
, T
>({}, value
); }
2002 // This overload is needed so that an argument of type 'char const[N]' ends up as
2003 // 'OStringConcat<rtl::OStringConcatMarker, char const[N]>' rather than as
2004 // 'OStringConcat<rtl::OStringConcatMarker, char[N]>':
2005 template<typename T
, std::size_t N
> [[nodiscard
]] static
2006 typename
std::enable_if_t
<
2007 ToStringHelper
<T
[N
]>::allowOStringConcat
, OStringConcat
<OStringConcatMarker
, T
[N
]>>
2008 Concat(T (& value
)[N
]) { return OStringConcat
<OStringConcatMarker
, T
[N
]>({}, value
); }
2012 /* ======================================================================= */
2014 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
2020 struct ToStringHelper
< OString
>
2022 static std::size_t length( const OString
& s
) { return s
.getLength(); }
2023 static char* addData( char* buffer
, const OString
& s
) { return addDataHelper( buffer
, s
.getStr(), s
.getLength()); }
2024 static const bool allowOStringConcat
= true;
2025 static const bool allowOUStringConcat
= false;
2031 template<std::size_t N
>
2032 struct ToStringHelper
< OStringLiteral
<N
> >
2034 static constexpr std::size_t length( const OStringLiteral
<N
>& str
) { return str
.getLength(); }
2035 static char* addData( char* buffer
, const OStringLiteral
<N
>& str
) { return addDataHelper( buffer
, str
.getStr(), str
.getLength() ); }
2036 static const bool allowOStringConcat
= true;
2037 static const bool allowOUStringConcat
= false;
2043 template< typename charT
, typename traits
, typename T1
, typename T2
>
2044 inline std::basic_ostream
<charT
, traits
> & operator <<(
2045 std::basic_ostream
<charT
, traits
> & stream
, OStringConcat
< T1
, T2
>&& concat
)
2047 return stream
<< OString( std::move(concat
) );
2052 /** A helper to use OStrings with hash maps.
2054 Instances of this class are unary function objects that can be used as
2055 hash function arguments to std::unordered_map and similar constructs.
2059 /** Compute a hash code for a string.
2065 a hash code for the string. This hash code should not be stored
2066 persistently, as its computation may change in later revisions.
2068 size_t operator()( const OString
& rString
) const
2069 { return static_cast<size_t>(rString
.hashCode()); }
2072 /** Equality functor for classic c-strings (i.e., null-terminated char* strings). */
2075 bool operator()( const char* p1
, const char* p2
) const
2076 { return rtl_str_compare(p1
, p2
) == 0; }
2079 /** Hashing functor for classic c-strings (i.e., null-terminated char* strings). */
2082 size_t operator()(const char* p
) const
2083 { return rtl_str_hashCode(p
); }
2086 /* ======================================================================= */
2089 Support for rtl::OString in std::ostream (and thus in
2090 CPPUNIT_ASSERT or SAL_INFO macros, for example).
2092 @since LibreOffice 4.0
2094 template< typename charT
, typename traits
> std::basic_ostream
<charT
, traits
> &
2096 std::basic_ostream
<charT
, traits
> & stream
, OString
const & rString
)
2098 return stream
<< rString
.getStr();
2099 // best effort; potentially loses data due to embedded null characters
2104 #ifdef RTL_STRING_UNITTEST
2107 typedef rtlunittest::OString OString
;
2109 #undef RTL_STRING_CONST_FUNCTION
2112 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
2113 using ::rtl::OString
;
2114 using ::rtl::OStringChar
;
2115 using ::rtl::OStringHash
;
2116 using ::rtl::OStringLiteral
;
2121 Make OString hashable by default for use in STL containers.
2123 @since LibreOffice 6.0
2125 #if defined LIBO_INTERNAL_ONLY
2129 struct hash
<::rtl::OString
>
2131 std::size_t operator()(::rtl::OString
const & s
) const
2132 { return std::size_t(s
.hashCode()); }
2140 #endif // INCLUDED_RTL_STRING_HXX
2142 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */