sd: sidebars are now visible in LOOL
[LibreOffice.git] / include / rtl / string.hxx
blob3b33ee4f1ae53253681c17cf89889c8e1bb8daa3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_RTL_STRING_HXX
21 #define INCLUDED_RTL_STRING_HXX
23 #include "sal/config.h"
25 #include <cassert>
26 #include <cstddef>
27 #include <new>
28 #include <ostream>
29 #include <utility>
30 #include <string.h>
32 #if defined LIBO_INTERNAL_ONLY
33 #include <string_view>
34 #endif
36 #include "rtl/textenc.h"
37 #include "rtl/string.h"
38 #include "rtl/stringutils.hxx"
40 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
41 #include "rtl/stringconcat.hxx"
42 #endif
44 #ifdef RTL_STRING_UNITTEST
45 extern bool rtl_string_unittest_const_literal;
46 extern bool rtl_string_unittest_const_literal_function;
47 #endif
49 // The unittest uses slightly different code to help check that the proper
50 // calls are made. The class is put into a different namespace to make
51 // sure the compiler generates a different (if generating also non-inline)
52 // copy of the function and does not merge them together. The class
53 // is "brought" into the proper rtl namespace by a typedef below.
54 #ifdef RTL_STRING_UNITTEST
55 #define rtl rtlunittest
56 #endif
58 namespace rtl
61 /// @cond INTERNAL
62 #ifdef RTL_STRING_UNITTEST
63 #undef rtl
64 // helper macro to make functions appear more readable
65 #define RTL_STRING_CONST_FUNCTION rtl_string_unittest_const_literal_function = true;
66 #else
67 #define RTL_STRING_CONST_FUNCTION
68 #endif
69 /// @endcond
71 /* ======================================================================= */
73 /**
74 This String class provide base functionality for C++ like 8-Bit
75 character array handling. The advantage of this class is, that it
76 handle all the memory management for you - and it do it
77 more efficient. If you assign a string to another string, the
78 data of both strings are shared (without any copy operation or
79 memory allocation) as long as you do not change the string. This class
80 stores also the length of the string, so that many operations are
81 faster as the C-str-functions.
83 This class provides only readonly string handling. So you could create
84 a string and you could only query the content from this string.
85 It provides also functionality to change the string, but this results
86 in every case in a new string instance (in the most cases with an
87 memory allocation). You don't have functionality to change the
88 content of the string. If you want to change the string content, then
89 you should use the OStringBuffer class, which provides these
90 functionalities and avoid too much memory allocation.
92 The design of this class is similar to the string classes in Java
93 and so more people should have fewer understanding problems when they
94 use this class.
97 class SAL_WARN_UNUSED SAL_DLLPUBLIC_RTTI OString
99 public:
100 /// @cond INTERNAL
101 rtl_String * pData;
102 /// @endcond
105 New string containing no characters.
107 OString()
109 pData = NULL;
110 rtl_string_new( &pData );
114 New string from OString.
116 @param str an OString.
118 OString( const OString & str )
120 pData = str.pData;
121 rtl_string_acquire( pData );
124 #ifndef _MSC_VER // TODO?
125 #if defined LIBO_INTERNAL_ONLY
127 Move constructor.
129 @param str an OString.
130 @since LibreOffice 5.2
132 OString( OString && str )
134 pData = str.pData;
135 str.pData = nullptr;
136 rtl_string_new( &str.pData );
138 #endif
139 #endif
142 New string from OString data.
144 @param str an OString data.
146 OString( rtl_String * str )
148 pData = str;
149 rtl_string_acquire( pData );
152 /** New string from OString data without acquiring it. Takeover of ownership.
154 The SAL_NO_ACQUIRE dummy parameter is only there to distinguish this
155 from other constructors.
157 @param str an OString data.
159 OString( rtl_String * str, __sal_NoAcquire )
161 pData = str;
165 New string from a single character.
167 @param value a character.
169 explicit OString( sal_Char value )
170 : pData (NULL)
172 rtl_string_newFromStr_WithLength( &pData, &value, 1 );
176 New string from a character buffer array.
178 Note: The argument type is always either char* or const char*. The template is
179 used only for technical reasons, as is the second argument.
181 @param value a NULL-terminated character array.
183 template< typename T >
184 OString( const T& value, typename libreoffice_internal::CharPtrDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
186 pData = NULL;
187 rtl_string_newFromStr( &pData, value );
190 template< typename T >
191 OString( T& value, typename libreoffice_internal::NonConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
193 pData = NULL;
194 rtl_string_newFromStr( &pData, value );
198 New string from a string literal.
200 If there are any embedded \0's in the string literal, the result is undefined.
201 Use the overload that explicitly accepts length.
203 @since LibreOffice 3.6
205 @param literal a string literal
207 template< typename T >
208 OString( T& literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
210 assert(
211 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
212 pData = NULL;
213 if (libreoffice_internal::ConstCharArrayDetector<T>::length == 0) {
214 rtl_string_new(&pData);
215 } else {
216 rtl_string_newFromLiteral(
217 &pData,
218 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
219 literal),
220 libreoffice_internal::ConstCharArrayDetector<T>::length, 0);
222 #ifdef RTL_STRING_UNITTEST
223 rtl_string_unittest_const_literal = true;
224 #endif
228 New string from a character buffer array.
230 @param value a character array.
231 @param length the number of character which should be copied.
232 The character array length must be greater or
233 equal than this value.
235 OString( const sal_Char * value, sal_Int32 length )
237 pData = NULL;
238 rtl_string_newFromStr_WithLength( &pData, value, length );
242 New string from a Unicode character buffer array.
244 @param value a Unicode character array.
245 @param length the number of character which should be converted.
246 The Unicode character array length must be
247 greater or equal than this value.
248 @param encoding the text encoding in which the Unicode character
249 sequence should be converted.
250 @param convertFlags flags which controls the conversion.
251 see RTL_UNICODETOTEXT_FLAGS_...
253 @exception std::bad_alloc is thrown if an out-of-memory condition occurs
255 OString( const sal_Unicode * value, sal_Int32 length,
256 rtl_TextEncoding encoding,
257 sal_uInt32 convertFlags = OUSTRING_TO_OSTRING_CVTFLAGS )
259 pData = NULL;
260 rtl_uString2String( &pData, value, length, encoding, convertFlags );
261 if (pData == NULL) {
262 throw std::bad_alloc();
266 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
268 @overload
269 @internal
271 template< typename T1, typename T2 >
272 OString( OStringConcat< T1, T2 >&& c )
274 const sal_Int32 l = c.length();
275 pData = rtl_string_alloc( l );
276 if (l != 0)
278 char* end = c.addData( pData->buffer );
279 pData->length = l;
280 *end = '\0';
283 #endif
285 #ifdef LIBO_INTERNAL_ONLY
286 OString(std::nullptr_t) = delete;
287 #endif
290 Release the string data.
292 ~OString()
294 rtl_string_release( pData );
298 Assign a new string.
300 @param str an OString.
302 OString & operator=( const OString & str )
304 rtl_string_assign( &pData, str.pData );
305 return *this;
308 #ifndef _MSC_VER // TODO?
309 #if defined LIBO_INTERNAL_ONLY
311 Move assign a new string.
313 @param str an OString.
314 @since LibreOffice 5.2
316 OString & operator=( OString && str )
318 rtl_string_release( pData );
319 pData = str.pData;
320 str.pData = nullptr;
321 rtl_string_new( &str.pData );
322 return *this;
324 #endif
325 #endif
328 @overload
329 This function accepts an ASCII string literal as its argument.
330 @since LibreOffice 3.6
332 template< typename T >
333 typename libreoffice_internal::ConstCharArrayDetector< T, OString& >::Type operator=( T& literal )
335 RTL_STRING_CONST_FUNCTION
336 assert(
337 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
338 if (libreoffice_internal::ConstCharArrayDetector<T>::length == 0) {
339 rtl_string_new(&pData);
340 } else {
341 rtl_string_newFromLiteral(
342 &pData,
343 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
344 literal),
345 libreoffice_internal::ConstCharArrayDetector<T>::length, 0);
347 return *this;
351 Append a string to this string.
353 @param str an OString.
355 OString & operator+=( const OString & str )
356 #if defined LIBO_INTERNAL_ONLY
358 #endif
360 rtl_string_newConcat( &pData, pData, str.pData );
361 return *this;
363 #if defined LIBO_INTERNAL_ONLY
364 void operator+=(OString const &) && = delete;
365 #endif
367 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
369 @overload
370 @internal
372 template< typename T1, typename T2 >
373 OString& operator+=( OStringConcat< T1, T2 >&& c ) & {
374 sal_Int32 l = c.length();
375 if( l == 0 )
376 return *this;
377 l += pData->length;
378 rtl_string_ensureCapacity( &pData, l );
379 char* end = c.addData( pData->buffer + pData->length );
380 *end = '\0';
381 pData->length = l;
382 return *this;
384 template<typename T1, typename T2> void operator +=(
385 OStringConcat<T1, T2> &&) && = delete;
386 #endif
389 Clears the string, i.e, makes a zero-character string
390 @since LibreOffice 4.4
392 void clear()
394 rtl_string_new( &pData );
398 Returns the length of this string.
400 The length is equal to the number of characters in this string.
402 @return the length of the sequence of characters represented by this
403 object.
405 sal_Int32 getLength() const { return pData->length; }
408 Checks if a string is empty.
410 @return true if the string is empty;
411 false, otherwise.
413 @since LibreOffice 3.4
415 bool isEmpty() const
417 return pData->length == 0;
421 Returns a pointer to the characters of this string.
423 <p>The returned pointer is guaranteed to point to a null-terminated byte
424 string. But note that this string object may contain embedded null
425 characters, which will thus also be embedded in the returned
426 null-terminated byte string.</p>
428 @return a pointer to a null-terminated byte string representing the
429 characters of this string object.
431 const sal_Char * getStr() const SAL_RETURNS_NONNULL { return pData->buffer; }
434 Access to individual characters.
436 @param index must be non-negative and less than length.
438 @return the character at the given index.
440 @since LibreOffice 3.5
442 sal_Char operator [](sal_Int32 index) const {
443 // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
444 assert(index >= 0 && static_cast<sal_uInt32>(index) < static_cast<sal_uInt32>(getLength()));
445 return getStr()[index];
449 Compares two strings.
451 The comparison is based on the numeric value of each character in
452 the strings and return a value indicating their relationship.
453 This function can't be used for language specific sorting.
455 @param str the object to be compared.
456 @return 0 - if both strings are equal
457 < 0 - if this string is less than the string argument
458 > 0 - if this string is greater than the string argument
460 sal_Int32 compareTo( const OString & str ) const
462 return rtl_str_compare_WithLength( pData->buffer, pData->length,
463 str.pData->buffer, str.pData->length );
467 Compares two strings with an maximum count of characters.
469 The comparison is based on the numeric value of each character in
470 the strings and return a value indicating their relationship.
471 This function can't be used for language specific sorting.
473 @param rObj the object to be compared.
474 @param maxLength the maximum count of characters to be compared.
475 @return 0 - if both strings are equal
476 < 0 - if this string is less than the string argument
477 > 0 - if this string is greater than the string argument
479 sal_Int32 compareTo( const OString & rObj, sal_Int32 maxLength ) const
481 return rtl_str_shortenedCompare_WithLength( pData->buffer, pData->length,
482 rObj.pData->buffer, rObj.pData->length, maxLength );
486 Compares two strings in reverse order.
488 The comparison is based on the numeric value of each character in
489 the strings and return a value indicating their relationship.
490 This function can't be used for language specific sorting.
492 @param str the object to be compared.
493 @return 0 - if both strings are equal
494 < 0 - if this string is less than the string argument
495 > 0 - if this string is greater than the string argument
497 sal_Int32 reverseCompareTo( const OString & str ) const
499 return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length,
500 str.pData->buffer, str.pData->length );
504 Perform a comparison of two strings.
506 The result is true if and only if second string
507 represents the same sequence of characters as the first string.
508 This function can't be used for language specific comparison.
510 @param str the object to be compared.
511 @return true if the strings are equal;
512 false, otherwise.
514 bool equals( const OString & str ) const
516 if ( pData->length != str.pData->length )
517 return false;
518 if ( pData == str.pData )
519 return true;
520 return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length,
521 str.pData->buffer, str.pData->length ) == 0;
525 Perform a comparison of two strings.
527 The result is true if and only if second string
528 represents the same sequence of characters as the first string.
529 The ASCII string must be NULL-terminated and must be greater or
530 equal as length.
531 This function can't be used for language specific comparison.
534 @param value a character array.
535 @param length the length of the character array.
536 @return true if the strings are equal;
537 false, otherwise.
539 bool equalsL( const sal_Char* value, sal_Int32 length ) const
541 if ( pData->length != length )
542 return false;
544 return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length,
545 value, length ) == 0;
549 Perform an ASCII lowercase comparison of two strings.
551 The result is true if and only if second string
552 represents the same sequence of characters as the first string,
553 ignoring the case.
554 Character values between 65 and 90 (ASCII A-Z) are interpreted as
555 values between 97 and 122 (ASCII a-z).
556 This function can't be used for language specific comparison.
558 @param str the object to be compared.
559 @return true if the strings are equal;
560 false, otherwise.
562 bool equalsIgnoreAsciiCase( const OString & str ) const
564 if ( pData->length != str.pData->length )
565 return false;
566 if ( pData == str.pData )
567 return true;
568 return rtl_str_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
569 str.pData->buffer, str.pData->length ) == 0;
573 Perform an ASCII lowercase comparison of two strings.
575 The result is true if and only if second string
576 represents the same sequence of characters as the first string,
577 ignoring the case.
578 Character values between 65 and 90 (ASCII A-Z) are interpreted as
579 values between 97 and 122 (ASCII a-z).
580 Since this method is optimized for performance, the ASCII character
581 values are not converted in any way. The caller has to make sure that
582 all ASCII characters are in the allowed range between 0 and
583 127. The ASCII string must be NULL-terminated.
584 This function can't be used for language specific comparison.
586 Note: The argument type is always either char* or const char*, the return type is bool.
587 The template is used only for technical reasons.
589 @param asciiStr the 8-Bit ASCII character string to be compared.
590 @return true if the strings are equal;
591 false, otherwise.
593 template< typename T >
594 typename libreoffice_internal::CharPtrDetector< T, bool >::Type equalsIgnoreAsciiCase( const T& asciiStr ) const
596 return rtl_str_compareIgnoreAsciiCase( pData->buffer, asciiStr ) == 0;
599 template< typename T >
600 typename libreoffice_internal::NonConstCharArrayDetector< T, bool >::Type equalsIgnoreAsciiCase( T& asciiStr ) const
602 return rtl_str_compareIgnoreAsciiCase( pData->buffer, asciiStr ) == 0;
606 @overload
607 This function accepts an ASCII string literal as its argument.
608 @since LibreOffice 3.6
610 template< typename T >
611 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type equalsIgnoreAsciiCase( T& literal ) const
613 RTL_STRING_CONST_FUNCTION
614 assert(
615 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
616 return
617 (pData->length
618 == libreoffice_internal::ConstCharArrayDetector<T>::length)
619 && (rtl_str_compareIgnoreAsciiCase_WithLength(
620 pData->buffer, pData->length,
621 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
622 literal),
623 libreoffice_internal::ConstCharArrayDetector<T>::length)
624 == 0);
628 Perform an ASCII lowercase comparison of two strings.
630 The result is true if and only if second string
631 represents the same sequence of characters as the first string,
632 ignoring the case.
633 Character values between 65 and 90 (ASCII A-Z) are interpreted as
634 values between 97 and 122 (ASCII a-z).
635 Since this method is optimized for performance, the ASCII character
636 values are not converted in any way. The caller has to make sure that
637 all ASCII characters are in the allowed range between 0 and
638 127. The ASCII string must be greater or equal in length as asciiStrLength.
639 This function can't be used for language specific comparison.
641 @param asciiStr the 8-Bit ASCII character string to be compared.
642 @param asciiStrLength the length of the ascii string
643 @return true if the strings are equal;
644 false, otherwise.
646 bool equalsIgnoreAsciiCaseL( const sal_Char * asciiStr, sal_Int32 asciiStrLength ) const
648 if ( pData->length != asciiStrLength )
649 return false;
651 return rtl_str_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
652 asciiStr, asciiStrLength ) == 0;
656 Match against a substring appearing in this string.
658 The result is true if and only if the second string appears as a substring
659 of this string, at the given position.
660 This function can't be used for language specific comparison.
662 @param str the object (substring) to be compared.
663 @param fromIndex the index to start the comparion from.
664 The index must be greater or equal than 0
665 and less or equal as the string length.
666 @return true if str match with the characters in the string
667 at the given position;
668 false, otherwise.
670 bool match( const OString & str, sal_Int32 fromIndex = 0 ) const
672 return rtl_str_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
673 str.pData->buffer, str.pData->length, str.pData->length ) == 0;
677 @overload
678 This function accepts an ASCII string literal as its argument.
679 @since LibreOffice 3.6
681 template< typename T >
682 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type match( T& literal, sal_Int32 fromIndex = 0 ) const
684 RTL_STRING_CONST_FUNCTION
685 assert(
686 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
687 return
688 rtl_str_shortenedCompare_WithLength(
689 pData->buffer + fromIndex, pData->length - fromIndex,
690 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
691 literal),
692 libreoffice_internal::ConstCharArrayDetector<T>::length,
693 libreoffice_internal::ConstCharArrayDetector<T>::length)
694 == 0;
698 Match against a substring appearing in this string.
700 @param str the substring to be compared; must not be null and must point
701 to memory of at least strLength bytes
703 @param strLength the length of the substring; must be non-negative
705 @param fromIndex the index into this string to start the comparison at;
706 must be non-negative and not greater than this string's length
708 @return true if and only if the given str is contained as a substring of
709 this string at the given fromIndex
711 @since LibreOffice 3.6
713 bool matchL(
714 char const * str, sal_Int32 strLength, sal_Int32 fromIndex = 0)
715 const
717 return rtl_str_shortenedCompare_WithLength(
718 pData->buffer + fromIndex, pData->length - fromIndex,
719 str, strLength, strLength) == 0;
722 // This overload is left undefined, to detect calls of matchL that
723 // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
724 // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
725 // platforms):
726 #if SAL_TYPES_SIZEOFLONG == 8
727 void matchL(char const *, sal_Int32, rtl_TextEncoding) const;
728 #endif
731 Match against a substring appearing in this string, ignoring the case of
732 ASCII letters.
734 The result is true if and only if the second string appears as a substring
735 of this string, at the given position.
736 Character values between 65 and 90 (ASCII A-Z) are interpreted as
737 values between 97 and 122 (ASCII a-z).
738 This function can't be used for language specific comparison.
740 @param str the object (substring) to be compared.
741 @param fromIndex the index to start the comparion from.
742 The index must be greater or equal than 0
743 and less or equal as the string length.
744 @return true if str match with the characters in the string
745 at the given position;
746 false, otherwise.
748 bool matchIgnoreAsciiCase( const OString & str, sal_Int32 fromIndex = 0 ) const
750 return rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
751 str.pData->buffer, str.pData->length,
752 str.pData->length ) == 0;
756 @overload
757 This function accepts an ASCII string literal as its argument.
758 @since LibreOffice 3.6
760 template< typename T >
761 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type matchIgnoreAsciiCase( T& literal, sal_Int32 fromIndex = 0 ) const
763 RTL_STRING_CONST_FUNCTION
764 assert(
765 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
766 return
767 rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
768 pData->buffer+fromIndex, pData->length-fromIndex,
769 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
770 literal),
771 libreoffice_internal::ConstCharArrayDetector<T>::length,
772 libreoffice_internal::ConstCharArrayDetector<T>::length)
773 == 0;
777 Check whether this string starts with a given substring.
779 @param str the substring to be compared
781 @param rest if non-null, and this function returns true, then assign a
782 copy of the remainder of this string to *rest. Available since
783 LibreOffice 4.2
785 @return true if and only if the given str appears as a substring at the
786 start of this string
788 @since LibreOffice 4.0
790 bool startsWith(OString const & str, OString * rest = NULL) const {
791 bool b = match(str);
792 if (b && rest != NULL) {
793 *rest = copy(str.getLength());
795 return b;
799 @overload
800 This function accepts an ASCII string literal as its argument.
801 @since LibreOffice 4.0
803 template< typename T >
804 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type startsWith(
805 T & literal, OString * rest = NULL) const
807 RTL_STRING_CONST_FUNCTION
808 bool b = match(literal, 0);
809 if (b && rest != NULL) {
810 *rest = copy(
811 libreoffice_internal::ConstCharArrayDetector<T>::length);
813 return b;
817 Check whether this string starts with a given string, ignoring the case of
818 ASCII letters.
820 Character values between 65 and 90 (ASCII A-Z) are interpreted as
821 values between 97 and 122 (ASCII a-z).
822 This function can't be used for language specific comparison.
824 @param str the substring to be compared
826 @param rest if non-null, and this function returns true, then assign a
827 copy of the remainder of this string to *rest.
829 @return true if and only if the given str appears as a substring at the
830 start of this string, ignoring the case of ASCII letters ("A"--"Z" and
831 "a"--"z")
833 @since LibreOffice 5.1
835 bool startsWithIgnoreAsciiCase(OString const & str, OString * rest = NULL)
836 const
838 bool b = matchIgnoreAsciiCase(str);
839 if (b && rest != NULL) {
840 *rest = copy(str.getLength());
842 return b;
846 @overload
847 This function accepts an ASCII string literal as its argument.
848 @since LibreOffice 5.1
850 template< typename T >
851 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type
852 startsWithIgnoreAsciiCase(T & literal, OString * rest = NULL) const
854 RTL_STRING_CONST_FUNCTION
855 assert(
856 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
857 bool b = matchIgnoreAsciiCase(literal);
858 if (b && rest != NULL) {
859 *rest = copy(
860 libreoffice_internal::ConstCharArrayDetector<T>::length);
862 return b;
866 Check whether this string ends with a given substring.
868 @param str the substring to be compared
870 @param rest if non-null, and this function returns true, then assign a
871 copy of the remainder of this string to *rest. Available since
872 LibreOffice 4.2
874 @return true if and only if the given str appears as a substring at the
875 end of this string
877 @since LibreOffice 3.6
879 bool endsWith(OString const & str, OString * rest = NULL) const {
880 bool b = str.getLength() <= getLength()
881 && match(str, getLength() - str.getLength());
882 if (b && rest != NULL) {
883 *rest = copy(0, getLength() - str.getLength());
885 return b;
889 @overload
890 This function accepts an ASCII string literal as its argument.
891 @since LibreOffice 3.6
893 template< typename T >
894 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type endsWith(
895 T & literal, OString * rest = NULL) const
897 RTL_STRING_CONST_FUNCTION
898 assert(
899 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
900 bool b
901 = (libreoffice_internal::ConstCharArrayDetector<T>::length
902 <= sal_uInt32(getLength()))
903 && match(
904 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
905 literal),
906 (getLength()
907 - libreoffice_internal::ConstCharArrayDetector<T>::length));
908 if (b && rest != NULL) {
909 *rest = copy(
911 (getLength()
912 - libreoffice_internal::ConstCharArrayDetector<T>::length));
914 return b;
918 Check whether this string ends with a given substring.
920 @param str the substring to be compared; must not be null and must point
921 to memory of at least strLength bytes
923 @param strLength the length of the substring; must be non-negative
925 @return true if and only if the given str appears as a substring at the
926 end of this string
928 @since LibreOffice 3.6
930 bool endsWithL(char const * str, sal_Int32 strLength) const {
931 return strLength <= getLength()
932 && matchL(str, strLength, getLength() - strLength);
935 friend bool operator == ( const OString& rStr1, const OString& rStr2 )
936 { return rStr1.equals(rStr2); }
937 friend bool operator != ( const OString& rStr1, const OString& rStr2 )
938 { return !(operator == ( rStr1, rStr2 )); }
939 friend bool operator < ( const OString& rStr1, const OString& rStr2 )
940 { return rStr1.compareTo( rStr2 ) < 0; }
941 friend bool operator > ( const OString& rStr1, const OString& rStr2 )
942 { return rStr1.compareTo( rStr2 ) > 0; }
943 friend bool operator <= ( const OString& rStr1, const OString& rStr2 )
944 { return rStr1.compareTo( rStr2 ) <= 0; }
945 friend bool operator >= ( const OString& rStr1, const OString& rStr2 )
946 { return rStr1.compareTo( rStr2 ) >= 0; }
948 template< typename T >
949 friend typename libreoffice_internal::CharPtrDetector< T, bool >::Type operator==( const OString& rStr1, const T& value )
951 return rStr1.compareTo( value ) == 0;
954 template< typename T >
955 friend typename libreoffice_internal::NonConstCharArrayDetector< T, bool >::Type operator==( const OString& rStr1, T& value )
957 return rStr1.compareTo( value ) == 0;
960 template< typename T >
961 friend typename libreoffice_internal::CharPtrDetector< T, bool >::Type operator==( const T& value, const OString& rStr2 )
963 return rStr2.compareTo( value ) == 0;
966 template< typename T >
967 friend typename libreoffice_internal::NonConstCharArrayDetector< T, bool >::Type operator==( T& value, const OString& rStr2 )
969 return rStr2.compareTo( value ) == 0;
973 @overload
974 This function accepts an ASCII string literal as its argument.
975 @since LibreOffice 3.6
977 template< typename T >
978 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator==( const OString& rStr, T& literal )
980 RTL_STRING_CONST_FUNCTION
981 assert(
982 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
983 return
984 (rStr.getLength()
985 == libreoffice_internal::ConstCharArrayDetector<T>::length)
986 && (rtl_str_compare_WithLength(
987 rStr.pData->buffer, rStr.pData->length,
988 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
989 literal),
990 libreoffice_internal::ConstCharArrayDetector<T>::length)
991 == 0);
995 @overload
996 This function accepts an ASCII string literal as its argument.
997 @since LibreOffice 3.6
999 template< typename T >
1000 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator==( T& literal, const OString& rStr )
1002 RTL_STRING_CONST_FUNCTION
1003 assert(
1004 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1005 return
1006 (rStr.getLength()
1007 == libreoffice_internal::ConstCharArrayDetector<T>::length)
1008 && (rtl_str_compare_WithLength(
1009 rStr.pData->buffer, rStr.pData->length,
1010 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1011 literal),
1012 libreoffice_internal::ConstCharArrayDetector<T>::length)
1013 == 0);
1016 template< typename T >
1017 friend typename libreoffice_internal::CharPtrDetector< T, bool >::Type operator!=( const OString& rStr1, const T& value )
1019 return !(operator == ( rStr1, value ));
1022 template< typename T >
1023 friend typename libreoffice_internal::NonConstCharArrayDetector< T, bool >::Type operator!=( const OString& rStr1, T& value )
1025 return !(operator == ( rStr1, value ));
1028 template< typename T >
1029 friend typename libreoffice_internal::CharPtrDetector< T, bool >::Type operator!=( const T& value, const OString& rStr2 )
1031 return !(operator == ( value, rStr2 ));
1034 template< typename T >
1035 friend typename libreoffice_internal::NonConstCharArrayDetector< T, bool >::Type operator!=( T& value, const OString& rStr2 )
1037 return !(operator == ( value, rStr2 ));
1041 @overload
1042 This function accepts an ASCII string literal as its argument.
1043 @since LibreOffice 3.6
1045 template< typename T >
1046 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator!=( const OString& rStr, T& literal )
1048 return !( rStr == literal );
1052 @overload
1053 This function accepts an ASCII string literal as its argument.
1054 @since LibreOffice 3.6
1056 template< typename T >
1057 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator!=( T& literal, const OString& rStr )
1059 return !( literal == rStr );
1063 Returns a hashcode for this string.
1065 @return a hash code value for this object.
1067 @see rtl::OStringHash for convenient use of std::unordered_map
1069 sal_Int32 hashCode() const
1071 return rtl_str_hashCode_WithLength( pData->buffer, pData->length );
1075 Returns the index within this string of the first occurrence of the
1076 specified character, starting the search at the specified index.
1078 @param ch character to be located.
1079 @param fromIndex the index to start the search from.
1080 The index must be greater or equal than 0
1081 and less or equal as the string length.
1082 @return the index of the first occurrence of the character in the
1083 character sequence represented by this string that is
1084 greater than or equal to fromIndex, or
1085 -1 if the character does not occur.
1087 sal_Int32 indexOf( sal_Char ch, sal_Int32 fromIndex = 0 ) const
1089 sal_Int32 ret = rtl_str_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch );
1090 return (ret < 0 ? ret : ret+fromIndex);
1094 Returns the index within this string of the last occurrence of the
1095 specified character, searching backward starting at the end.
1097 @param ch character to be located.
1098 @return the index of the last occurrence of the character in the
1099 character sequence represented by this string, or
1100 -1 if the character does not occur.
1102 sal_Int32 lastIndexOf( sal_Char ch ) const
1104 return rtl_str_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch );
1108 Returns the index within this string of the last occurrence of the
1109 specified character, searching backward starting before the specified
1110 index.
1112 @param ch character to be located.
1113 @param fromIndex the index before which to start the search.
1114 @return the index of the last occurrence of the character in the
1115 character sequence represented by this string that
1116 is less than fromIndex, or -1
1117 if the character does not occur before that point.
1119 sal_Int32 lastIndexOf( sal_Char ch, sal_Int32 fromIndex ) const
1121 return rtl_str_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch );
1125 Returns the index within this string of the first occurrence of the
1126 specified substring, starting at the specified index.
1128 If str doesn't include any character, always -1 is
1129 returned. This is also the case, if both strings are empty.
1131 @param str the substring to search for.
1132 @param fromIndex the index to start the search from.
1133 @return If the string argument occurs one or more times as a substring
1134 within this string at the starting index, then the index
1135 of the first character of the first such substring is
1136 returned. If it does not occur as a substring starting
1137 at fromIndex or beyond, -1 is returned.
1139 sal_Int32 indexOf( const OString & str, sal_Int32 fromIndex = 0 ) const
1141 sal_Int32 ret = rtl_str_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1142 str.pData->buffer, str.pData->length );
1143 return (ret < 0 ? ret : ret+fromIndex);
1147 @overload
1148 This function accepts an ASCII string literal as its argument.
1149 @since LibreOffice 3.6
1151 template< typename T >
1152 typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const
1154 RTL_STRING_CONST_FUNCTION
1155 assert(
1156 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1157 sal_Int32 n = rtl_str_indexOfStr_WithLength(
1158 pData->buffer + fromIndex, pData->length - fromIndex,
1159 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1160 libreoffice_internal::ConstCharArrayDetector<T>::length);
1161 return n < 0 ? n : n + fromIndex;
1165 Returns the index within this string of the first occurrence of the
1166 specified substring, starting at the specified index.
1168 If str doesn't include any character, always -1 is
1169 returned. This is also the case, if both strings are empty.
1171 @param str the substring to search for.
1172 @param len the length of the substring.
1173 @param fromIndex the index to start the search from.
1174 @return If the string argument occurs one or more times as a substring
1175 within this string at the starting index, then the index
1176 of the first character of the first such substring is
1177 returned. If it does not occur as a substring starting
1178 at fromIndex or beyond, -1 is returned.
1180 @since LibreOffice 3.6
1182 sal_Int32 indexOfL(char const * str, sal_Int32 len, sal_Int32 fromIndex = 0)
1183 const
1185 sal_Int32 n = rtl_str_indexOfStr_WithLength(
1186 pData->buffer + fromIndex, pData->length - fromIndex, str, len);
1187 return n < 0 ? n : n + fromIndex;
1190 // This overload is left undefined, to detect calls of indexOfL that
1191 // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
1192 // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
1193 // platforms):
1194 #if SAL_TYPES_SIZEOFLONG == 8
1195 void indexOfL(char const *, sal_Int32, rtl_TextEncoding) const;
1196 #endif
1199 Returns the index within this string of the last occurrence of
1200 the specified substring, searching backward starting at the end.
1202 The returned index indicates the starting index of the substring
1203 in this string.
1204 If str doesn't include any character, always -1 is
1205 returned. This is also the case, if both strings are empty.
1207 @param str the substring to search for.
1208 @return If the string argument occurs one or more times as a substring
1209 within this string, then the index of the first character of
1210 the last such substring is returned. If it does not occur as
1211 a substring, -1 is returned.
1213 sal_Int32 lastIndexOf( const OString & str ) const
1215 return rtl_str_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1216 str.pData->buffer, str.pData->length );
1220 Returns the index within this string of the last occurrence of
1221 the specified substring, searching backward starting before the specified
1222 index.
1224 The returned index indicates the starting index of the substring
1225 in this string.
1226 If str doesn't include any character, always -1 is
1227 returned. This is also the case, if both strings are empty.
1229 @param str the substring to search for.
1230 @param fromIndex the index before which to start the search.
1231 @return If the string argument occurs one or more times as a substring
1232 within this string before the starting index, then the index
1233 of the first character of the last such substring is
1234 returned. Otherwise, -1 is returned.
1236 sal_Int32 lastIndexOf( const OString & str, sal_Int32 fromIndex ) const
1238 return rtl_str_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1239 str.pData->buffer, str.pData->length );
1243 Returns a new string that is a substring of this string.
1245 The substring begins at the specified beginIndex. If
1246 beginIndex is negative or be greater than the length of
1247 this string, behaviour is undefined.
1249 @param beginIndex the beginning index, inclusive.
1250 @return the specified substring.
1252 SAL_WARN_UNUSED_RESULT OString copy( sal_Int32 beginIndex ) const
1254 rtl_String *pNew = NULL;
1255 rtl_string_newFromSubString( &pNew, pData, beginIndex, getLength() - beginIndex );
1256 return OString( pNew, SAL_NO_ACQUIRE );
1260 Returns a new string that is a substring of this string.
1262 The substring begins at the specified beginIndex and contains count
1263 characters. If either beginIndex or count are negative,
1264 or beginIndex + count are greater than the length of this string
1265 then behaviour is undefined.
1267 @param beginIndex the beginning index, inclusive.
1268 @param count the number of characters.
1269 @return the specified substring.
1271 SAL_WARN_UNUSED_RESULT OString copy( sal_Int32 beginIndex, sal_Int32 count ) const
1273 rtl_String *pNew = NULL;
1274 rtl_string_newFromSubString( &pNew, pData, beginIndex, count );
1275 return OString( pNew, SAL_NO_ACQUIRE );
1279 Concatenates the specified string to the end of this string.
1281 @param str the string that is concatenated to the end
1282 of this string.
1283 @return a string that represents the concatenation of this string
1284 followed by the string argument.
1286 SAL_WARN_UNUSED_RESULT OString concat( const OString & str ) const
1288 rtl_String* pNew = NULL;
1289 rtl_string_newConcat( &pNew, pData, str.pData );
1290 return OString( pNew, SAL_NO_ACQUIRE );
1293 #ifndef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
1294 friend OString operator+( const OString & str1, const OString & str2 )
1296 return str1.concat( str2 );
1298 #endif
1301 Returns a new string resulting from replacing n = count characters
1302 from position index in this string with newStr.
1304 @param index the replacing index in str.
1305 The index must be greater or equal as 0 and
1306 less or equal as the length of the string.
1307 @param count the count of characters that will replaced
1308 The count must be greater or equal as 0 and
1309 less or equal as the length of the string minus index.
1310 @param newStr the new substring.
1311 @return the new string.
1313 SAL_WARN_UNUSED_RESULT OString replaceAt( sal_Int32 index, sal_Int32 count, const OString& newStr ) const
1315 rtl_String* pNew = NULL;
1316 rtl_string_newReplaceStrAt( &pNew, pData, index, count, newStr.pData );
1317 return OString( pNew, SAL_NO_ACQUIRE );
1321 Returns a new string resulting from replacing all occurrences of
1322 oldChar in this string with newChar.
1324 If the character oldChar does not occur in the character sequence
1325 represented by this object, then the string is assigned with
1326 str.
1328 @param oldChar the old character.
1329 @param newChar the new character.
1330 @return a string derived from this string by replacing every
1331 occurrence of oldChar with newChar.
1333 SAL_WARN_UNUSED_RESULT OString replace( sal_Char oldChar, sal_Char newChar ) const
1335 rtl_String* pNew = NULL;
1336 rtl_string_newReplace( &pNew, pData, oldChar, newChar );
1337 return OString( pNew, SAL_NO_ACQUIRE );
1341 Returns a new string resulting from replacing the first occurrence of a
1342 given substring with another substring.
1344 @param from the substring to be replaced
1346 @param to the replacing substring
1348 @param[in,out] index pointer to a start index; if the pointer is
1349 non-null: upon entry to the function, its value is the index into the this
1350 string at which to start searching for the \p from substring, the value
1351 must be non-negative and not greater than this string's length; upon exit
1352 from the function its value is the index into this string at which the
1353 replacement took place or -1 if no replacement took place; if the pointer
1354 is null, searching always starts at index 0
1356 @since LibreOffice 3.6
1358 SAL_WARN_UNUSED_RESULT OString replaceFirst(
1359 OString const & from, OString const & to, sal_Int32 * index = NULL) const
1361 rtl_String * s = NULL;
1362 sal_Int32 i = 0;
1363 rtl_string_newReplaceFirst(
1364 &s, pData, from.pData->buffer, from.pData->length,
1365 to.pData->buffer, to.pData->length, index == NULL ? &i : index);
1366 return OString(s, SAL_NO_ACQUIRE);
1370 Returns a new string resulting from replacing all occurrences of a given
1371 substring with another substring.
1373 Replacing subsequent occurrences picks up only after a given replacement.
1374 That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
1376 @param from the substring to be replaced
1378 @param to the replacing substring
1380 @since LibreOffice 3.6
1382 SAL_WARN_UNUSED_RESULT OString replaceAll(OString const & from, OString const & to) const {
1383 rtl_String * s = NULL;
1384 rtl_string_newReplaceAll(
1385 &s, pData, from.pData->buffer, from.pData->length,
1386 to.pData->buffer, to.pData->length);
1387 return OString(s, SAL_NO_ACQUIRE);
1391 Converts from this string all ASCII uppercase characters (65-90)
1392 to ASCII lowercase characters (97-122).
1394 This function can't be used for language specific conversion.
1395 If the string doesn't contain characters which must be converted,
1396 then the new string is assigned with str.
1398 @return the string, converted to ASCII lowercase.
1400 SAL_WARN_UNUSED_RESULT OString toAsciiLowerCase() const
1402 rtl_String* pNew = NULL;
1403 rtl_string_newToAsciiLowerCase( &pNew, pData );
1404 return OString( pNew, SAL_NO_ACQUIRE );
1408 Converts from this string all ASCII lowercase characters (97-122)
1409 to ASCII uppercase characters (65-90).
1411 This function can't be used for language specific conversion.
1412 If the string doesn't contain characters which must be converted,
1413 then the new string is assigned with str.
1415 @return the string, converted to ASCII uppercase.
1417 SAL_WARN_UNUSED_RESULT OString toAsciiUpperCase() const
1419 rtl_String* pNew = NULL;
1420 rtl_string_newToAsciiUpperCase( &pNew, pData );
1421 return OString( pNew, SAL_NO_ACQUIRE );
1425 Returns a new string resulting from removing white space from both ends
1426 of the string.
1428 All characters that have codes less than or equal to
1429 32 (the space character) are considered to be white space.
1430 If the string doesn't contain white spaces at both ends,
1431 then the new string is assigned with str.
1433 @return the string, with white space removed from the front and end.
1435 SAL_WARN_UNUSED_RESULT OString trim() const
1437 rtl_String* pNew = NULL;
1438 rtl_string_newTrim( &pNew, pData );
1439 return OString( pNew, SAL_NO_ACQUIRE );
1443 Returns a token in the string.
1445 Example:
1446 sal_Int32 nIndex = 0;
1450 OString aToken = aStr.getToken( 0, ';', nIndex );
1453 while ( nIndex >= 0 );
1455 @param token the number of the token to return.
1456 @param cTok the character which separate the tokens.
1457 @param index the position at which the token is searched in the
1458 string.
1459 The index must not be greater than the length of the
1460 string.
1461 This param is set to the position of the
1462 next token or to -1, if it is the last token.
1463 @return the token; if either token or index is negative, an empty token
1464 is returned (and index is set to -1)
1466 OString getToken( sal_Int32 token, sal_Char cTok, sal_Int32& index ) const
1468 rtl_String * pNew = NULL;
1469 index = rtl_string_getToken( &pNew, pData, token, cTok, index );
1470 return OString( pNew, SAL_NO_ACQUIRE );
1474 Returns a token from the string.
1476 The same as getToken(sal_Int32, sal_Char, sal_Int32 &), but always passing
1477 in 0 as the start index in the third argument.
1479 @param count the number of the token to return, starting with 0
1480 @param separator the character which separates the tokens
1482 @return the given token, or an empty string
1484 @since LibreOffice 3.6
1486 OString getToken(sal_Int32 count, char separator) const {
1487 sal_Int32 n = 0;
1488 return getToken(count, separator, n);
1492 Returns the Boolean value from this string.
1494 This function can't be used for language specific conversion.
1496 @return true, if the string is 1 or "True" in any ASCII case.
1497 false in any other case.
1499 bool toBoolean() const
1501 return rtl_str_toBoolean( pData->buffer );
1505 Returns the first character from this string.
1507 @return the first character from this string or 0, if this string
1508 is empty.
1510 sal_Char toChar() const
1512 return pData->buffer[0];
1516 Returns the int32 value from this string.
1518 This function can't be used for language specific conversion.
1520 @param radix the radix (between 2 and 36)
1521 @return the int32 represented from this string.
1522 0 if this string represents no number or one of too large
1523 magnitude.
1525 sal_Int32 toInt32( sal_Int16 radix = 10 ) const
1527 return rtl_str_toInt32( pData->buffer, radix );
1531 Returns the uint32 value from this string.
1533 This function can't be used for language specific conversion.
1535 @param radix the radix (between 2 and 36)
1536 @return the uint32 represented from this string.
1537 0 if this string represents no number or one of too large
1538 magnitude.
1540 @since LibreOffice 4.2
1542 sal_uInt32 toUInt32( sal_Int16 radix = 10 ) const
1544 return rtl_str_toUInt32( pData->buffer, radix );
1548 Returns the int64 value from this string.
1550 This function can't be used for language specific conversion.
1552 @param radix the radix (between 2 and 36)
1553 @return the int64 represented from this string.
1554 0 if this string represents no number or one of too large
1555 magnitude.
1557 sal_Int64 toInt64( sal_Int16 radix = 10 ) const
1559 return rtl_str_toInt64( pData->buffer, radix );
1563 Returns the uint64 value from this string.
1565 This function can't be used for language specific conversion.
1567 @param radix the radix (between 2 and 36)
1568 @return the uint64 represented from this string.
1569 0 if this string represents no number or one of too large
1570 magnitude.
1572 @since LibreOffice 4.1
1574 sal_uInt64 toUInt64( sal_Int16 radix = 10 ) const
1576 return rtl_str_toUInt64( pData->buffer, radix );
1580 Returns the float value from this string.
1582 This function can't be used for language specific conversion.
1584 @return the float represented from this string.
1585 0.0 if this string represents no number.
1587 float toFloat() const
1589 return rtl_str_toFloat( pData->buffer );
1593 Returns the double value from this string.
1595 This function can't be used for language specific conversion.
1597 @return the double represented from this string.
1598 0.0 if this string represents no number.
1600 double toDouble() const
1602 return rtl_str_toDouble( pData->buffer );
1606 Returns the string representation of the integer argument.
1608 This function can't be used for language specific conversion.
1610 @param i an integer value
1611 @param radix the radix (between 2 and 36)
1612 @return a string with the string representation of the argument.
1613 @since LibreOffice 4.1
1615 static OString number( int i, sal_Int16 radix = 10 )
1617 return number( static_cast< long long >( i ), radix );
1619 /// @overload
1620 /// @since LibreOffice 4.1
1621 static OString number( unsigned int i, sal_Int16 radix = 10 )
1623 return number( static_cast< unsigned long long >( i ), radix );
1625 /// @overload
1626 /// @since LibreOffice 4.1
1627 static OString number( long i, sal_Int16 radix = 10 )
1629 return number( static_cast< long long >( i ), radix );
1631 /// @overload
1632 /// @since LibreOffice 4.1
1633 static OString number( unsigned long i, sal_Int16 radix = 10 )
1635 return number( static_cast< unsigned long long >( i ), radix );
1637 /// @overload
1638 /// @since LibreOffice 4.1
1639 static OString number( long long ll, sal_Int16 radix = 10 )
1641 sal_Char aBuf[RTL_STR_MAX_VALUEOFINT64];
1642 rtl_String* pNewData = NULL;
1643 rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfInt64( aBuf, ll, radix ) );
1644 return OString( pNewData, SAL_NO_ACQUIRE );
1646 /// @overload
1647 /// @since LibreOffice 4.1
1648 static OString number( unsigned long long ll, sal_Int16 radix = 10 )
1650 sal_Char aBuf[RTL_STR_MAX_VALUEOFUINT64];
1651 rtl_String* pNewData = NULL;
1652 rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfUInt64( aBuf, ll, radix ) );
1653 return OString( pNewData, SAL_NO_ACQUIRE );
1657 Returns the string representation of the float argument.
1659 This function can't be used for language specific conversion.
1661 @param f a float.
1662 @return a string with the string representation of the argument.
1663 @since LibreOffice 4.1
1665 static OString number( float f )
1667 sal_Char aBuf[RTL_STR_MAX_VALUEOFFLOAT];
1668 rtl_String* pNewData = NULL;
1669 rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfFloat( aBuf, f ) );
1670 return OString( pNewData, SAL_NO_ACQUIRE );
1674 Returns the string representation of the double argument.
1676 This function can't be used for language specific conversion.
1678 @param d a double.
1679 @return a string with the string representation of the argument.
1680 @since LibreOffice 4.1
1682 static OString number( double d )
1684 sal_Char aBuf[RTL_STR_MAX_VALUEOFDOUBLE];
1685 rtl_String* pNewData = NULL;
1686 rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfDouble( aBuf, d ) );
1687 return OString( pNewData, SAL_NO_ACQUIRE );
1691 Returns the string representation of the sal_Bool argument.
1693 If the sal_Bool is true, the string "true" is returned.
1694 If the sal_Bool is false, the string "false" is returned.
1695 This function can't be used for language specific conversion.
1697 @param b a sal_Bool.
1698 @return a string with the string representation of the argument.
1699 @deprecated use boolean()
1701 SAL_DEPRECATED("use boolean()") static OString valueOf( sal_Bool b )
1703 return boolean(b);
1707 Returns the string representation of the boolean argument.
1709 If the argument is true, the string "true" is returned.
1710 If the argument is false, the string "false" is returned.
1711 This function can't be used for language specific conversion.
1713 @param b a bool.
1714 @return a string with the string representation of the argument.
1715 @since LibreOffice 4.1
1717 static OString boolean( bool b )
1719 sal_Char aBuf[RTL_STR_MAX_VALUEOFBOOLEAN];
1720 rtl_String* pNewData = NULL;
1721 rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfBoolean( aBuf, b ) );
1722 return OString( pNewData, SAL_NO_ACQUIRE );
1726 Returns the string representation of the char argument.
1728 @param c a character.
1729 @return a string with the string representation of the argument.
1730 @deprecated use operator, function or constructor taking char or sal_Unicode argument
1732 SAL_DEPRECATED("convert to OString or use directly") static OString valueOf( sal_Char c )
1734 return OString( &c, 1 );
1738 Returns the string representation of the int argument.
1740 This function can't be used for language specific conversion.
1742 @param i a int32.
1743 @param radix the radix (between 2 and 36)
1744 @return a string with the string representation of the argument.
1745 @deprecated use number()
1747 SAL_DEPRECATED("use number()") static OString valueOf( sal_Int32 i, sal_Int16 radix = 10 )
1749 return number( i, radix );
1753 Returns the string representation of the long argument.
1755 This function can't be used for language specific conversion.
1757 @param ll a int64.
1758 @param radix the radix (between 2 and 36)
1759 @return a string with the string representation of the argument.
1760 @deprecated use number()
1762 SAL_DEPRECATED("use number()") static OString valueOf( sal_Int64 ll, sal_Int16 radix = 10 )
1764 return number( ll, radix );
1768 Returns the string representation of the float argument.
1770 This function can't be used for language specific conversion.
1772 @param f a float.
1773 @return a string with the string representation of the argument.
1774 @deprecated use number()
1776 SAL_DEPRECATED("use number()") static OString valueOf( float f )
1778 return number(f);
1782 Returns the string representation of the double argument.
1784 This function can't be used for language specific conversion.
1786 @param d a double.
1787 @return a string with the string representation of the argument.
1788 @deprecated use number()
1790 SAL_DEPRECATED("use number()") static OString valueOf( double d )
1792 return number(d);
1795 #if defined LIBO_INTERNAL_ONLY
1796 operator std::string_view() const { return {getStr(), sal_uInt32(getLength())}; }
1797 #endif
1800 /* ======================================================================= */
1802 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
1804 A simple wrapper around string literal. It is usually not necessary to use, can
1805 be mostly used to force OString operator+ working with operands that otherwise would
1806 not trigger it.
1808 This class is not part of public API and is meant to be used only in LibreOffice code.
1809 @since LibreOffice 4.0
1811 struct SAL_WARN_UNUSED OStringLiteral
1813 template< int N >
1814 explicit OStringLiteral( const char (&str)[ N ] ) : size( N - 1 ), data( str ) { assert( strlen( str ) == N - 1 ); }
1815 #if defined __cpp_char8_t
1816 template< int N >
1817 explicit OStringLiteral( const char8_t (&str)[ N ] ) : size( N - 1 ), data( reinterpret_cast<char const *>(str) ) { assert( strlen( data ) == N - 1 ); }
1818 #endif
1819 int size;
1820 const char* data;
1824 @internal
1826 template<>
1827 struct ToStringHelper< OString >
1829 static int length( const OString& s ) { return s.getLength(); }
1830 static char* addData( char* buffer, const OString& s ) { return addDataHelper( buffer, s.getStr(), s.getLength()); }
1831 static const bool allowOStringConcat = true;
1832 static const bool allowOUStringConcat = false;
1836 @internal
1838 template<>
1839 struct ToStringHelper< OStringLiteral >
1841 static int length( const OStringLiteral& str ) { return str.size; }
1842 static char* addData( char* buffer, const OStringLiteral& str ) { return addDataHelper( buffer, str.data, str.size ); }
1843 static const bool allowOStringConcat = true;
1844 static const bool allowOUStringConcat = false;
1848 @internal
1850 template< typename charT, typename traits, typename T1, typename T2 >
1851 inline std::basic_ostream<charT, traits> & operator <<(
1852 std::basic_ostream<charT, traits> & stream, OStringConcat< T1, T2 >&& concat)
1854 return stream << OString( std::move(concat) );
1856 #endif
1859 /** A helper to use OStrings with hash maps.
1861 Instances of this class are unary function objects that can be used as
1862 hash function arguments to std::unordered_map and similar constructs.
1864 struct OStringHash
1866 /** Compute a hash code for a string.
1868 @param rString
1869 a string.
1871 @return
1872 a hash code for the string. This hash code should not be stored
1873 persistently, as its computation may change in later revisions.
1875 size_t operator()( const OString& rString ) const
1876 { return static_cast<size_t>(rString.hashCode()); }
1879 /** Equality functor for classic c-strings (i.e., null-terminated char* strings). */
1880 struct CStringEqual
1882 bool operator()( const char* p1, const char* p2) const
1883 { return rtl_str_compare(p1, p2) == 0; }
1886 /** Hashing functor for classic c-strings (i.e., null-terminated char* strings). */
1887 struct CStringHash
1889 size_t operator()(const char* p) const
1890 { return rtl_str_hashCode(p); }
1893 /* ======================================================================= */
1896 Support for rtl::OString in std::ostream (and thus in
1897 CPPUNIT_ASSERT or SAL_INFO macros, for example).
1899 @since LibreOffice 4.0
1901 template< typename charT, typename traits > std::basic_ostream<charT, traits> &
1902 operator <<(
1903 std::basic_ostream<charT, traits> & stream, OString const & rString)
1905 return stream << rString.getStr();
1906 // best effort; potentially loses data due to embedded null characters
1909 } /* Namespace */
1911 #ifdef RTL_STRING_UNITTEST
1912 namespace rtl
1914 typedef rtlunittest::OString OString;
1916 #undef RTL_STRING_CONST_FUNCTION
1917 #endif
1919 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
1920 using ::rtl::OString;
1921 using ::rtl::OStringHash;
1922 using ::rtl::OStringLiteral;
1923 #endif
1925 /// @cond INTERNAL
1927 Make OString hashable by default for use in STL containers.
1929 @since LibreOffice 6.0
1931 #if defined LIBO_INTERNAL_ONLY
1932 namespace std {
1934 template<>
1935 struct hash<::rtl::OString>
1937 std::size_t operator()(::rtl::OString const & s) const
1938 { return std::size_t(s.hashCode()); }
1943 #endif
1944 /// @endcond
1946 #endif // INCLUDED_RTL_STRING_HXX
1948 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */