build fix
[LibreOffice.git] / include / rtl / string.hxx
blob827ab8e3fbd5259cff626210e1003b7da01d88ff
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 <string.h>
31 #include <rtl/textenc.h>
32 #include <rtl/string.h>
33 #include <rtl/stringutils.hxx>
35 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
36 #include <config_global.h>
37 #include <rtl/stringconcat.hxx>
38 #endif
40 #include <sal/log.hxx>
42 // The unittest uses slightly different code to help check that the proper
43 // calls are made. The class is put into a different namespace to make
44 // sure the compiler generates a different (if generating also non-inline)
45 // copy of the function and does not merge them together. The class
46 // is "brought" into the proper rtl namespace by a typedef below.
47 #ifdef RTL_STRING_UNITTEST
48 #define rtl rtlunittest
49 #endif
51 namespace rtl
54 /// @cond INTERNAL
55 #ifdef RTL_STRING_UNITTEST
56 #undef rtl
57 // helper macro to make functions appear more readable
58 #define RTL_STRING_CONST_FUNCTION rtl_string_unittest_const_literal_function = true;
59 #else
60 #define RTL_STRING_CONST_FUNCTION
61 #endif
62 /// @endcond
64 /* ======================================================================= */
66 /**
67 This String class provide base functionality for C++ like 8-Bit
68 character array handling. The advantage of this class is, that it
69 handle all the memory managament for you - and it do it
70 more efficient. If you assign a string to another string, the
71 data of both strings are shared (without any copy operation or
72 memory allocation) as long as you do not change the string. This class
73 stores also the length of the string, so that many operations are
74 faster as the C-str-functions.
76 This class provide only readonly string handling. So you could create
77 a string and you could only query the content from this string.
78 It provide also functionality to change the string, but this results
79 in every case in a new string instance (in the most cases with an
80 memory allocation). You don't have functionality to change the
81 content of the string. If you want change the string content, than
82 you should us the OStringBuffer class, which provide these
83 functionality and avoid to much memory allocation.
85 The design of this class is similar to the string classes in Java
86 and so more people should have fewer understanding problems when they
87 use this class.
90 class SAL_WARN_UNUSED SAL_DLLPUBLIC_RTTI OString
92 public:
93 /// @cond INTERNAL
94 rtl_String * pData;
95 /// @endcond
97 /**
98 New string containing no characters.
100 OString()
102 pData = NULL;
103 rtl_string_new( &pData );
107 New string from OString.
109 @param str a OString.
111 OString( const OString & str )
113 pData = str.pData;
114 rtl_string_acquire( pData );
117 #ifndef _MSC_VER // TODO?
118 #if defined LIBO_INTERNAL_ONLY
120 Move constructor.
122 @param str a OString.
123 @since LibreOffice 5.2
125 OString( OString && str )
127 pData = str.pData;
128 str.pData = nullptr;
129 rtl_string_new( &str.pData );
131 #endif
132 #endif
135 New string from OString data.
137 @param str a OString data.
139 OString( rtl_String * str )
141 pData = str;
142 rtl_string_acquire( pData );
145 /** New string from OString data without acquiring it. Takeover of ownership.
147 The SAL_NO_ACQUIRE dummy parameter is only there to distinguish this
148 from other constructors.
150 @param str a OString data.
152 inline OString( rtl_String * str, __sal_NoAcquire )
154 pData = str;
158 New string from a single character.
160 @param value a character.
162 explicit OString( sal_Char value )
163 : pData (NULL)
165 rtl_string_newFromStr_WithLength( &pData, &value, 1 );
169 New string from a character buffer array.
171 Note: The argument type is always either char* or const char*. The template is
172 used only for technical reasons, as is the second argument.
174 @param value a NULL-terminated character array.
176 template< typename T >
177 OString( const T& value, typename libreoffice_internal::CharPtrDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
179 pData = NULL;
180 rtl_string_newFromStr( &pData, value );
183 template< typename T >
184 OString( T& value, typename libreoffice_internal::NonConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
186 pData = NULL;
187 rtl_string_newFromStr( &pData, value );
191 New string from a string literal.
193 If there are any embedded \0's in the string literal, the result is undefined.
194 Use the overload that explicitly accepts length.
196 @since LibreOffice 3.6
198 @param literal a string literal
200 template< typename T >
201 OString( T& literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
203 assert(
204 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
205 pData = NULL;
206 if (libreoffice_internal::ConstCharArrayDetector<T>::length == 0) {
207 rtl_string_new(&pData);
208 } else {
209 rtl_string_newFromLiteral(
210 &pData,
211 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
212 literal),
213 libreoffice_internal::ConstCharArrayDetector<T>::length, 0);
215 #ifdef RTL_STRING_UNITTEST
216 rtl_string_unittest_const_literal = true;
217 #endif
221 New string from a character buffer array.
223 @param value a character array.
224 @param length the number of character which should be copied.
225 The character array length must be greater or
226 equal than this value.
228 OString( const sal_Char * value, sal_Int32 length )
230 pData = NULL;
231 rtl_string_newFromStr_WithLength( &pData, value, length );
235 New string from a Unicode character buffer array.
237 @param value a Unicode character array.
238 @param length the number of character which should be converted.
239 The Unicode character array length must be
240 greater or equal than this value.
241 @param encoding the text encoding in which the Unicode character
242 sequence should be converted.
243 @param convertFlags flags which controls the conversion.
244 see RTL_UNICODETOTEXT_FLAGS_...
246 @exception std::bad_alloc is thrown if an out-of-memory condition occurs
248 OString( const sal_Unicode * value, sal_Int32 length,
249 rtl_TextEncoding encoding,
250 sal_uInt32 convertFlags = OUSTRING_TO_OSTRING_CVTFLAGS )
252 pData = NULL;
253 rtl_uString2String( &pData, value, length, encoding, convertFlags );
254 if (pData == NULL) {
255 throw std::bad_alloc();
259 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
261 @overload
262 @internal
264 template< typename T1, typename T2 >
265 OString( const OStringConcat< T1, T2 >& c )
267 const sal_Int32 l = c.length();
268 pData = rtl_string_alloc( l );
269 if (l != 0)
271 char* end = c.addData( pData->buffer );
272 pData->length = l;
273 *end = '\0';
276 #endif
278 #ifdef LIBO_INTERNAL_ONLY
279 OString(std::nullptr_t) = delete;
280 #endif
283 Release the string data.
285 ~OString()
287 rtl_string_release( pData );
291 Assign a new string.
293 @param str a OString.
295 OString & operator=( const OString & str )
297 rtl_string_assign( &pData, str.pData );
298 return *this;
301 #ifndef _MSC_VER // TODO?
302 #if defined LIBO_INTERNAL_ONLY
304 Move assign a new string.
306 @param str a OString.
307 @since LibreOffice 5.2
309 OString & operator=( OString && str )
311 rtl_string_release( pData );
312 pData = str.pData;
313 str.pData = nullptr;
314 rtl_string_new( &str.pData );
315 return *this;
317 #endif
318 #endif
321 @overload
322 This function accepts an ASCII string literal as its argument.
323 @since LibreOffice 3.6
325 template< typename T >
326 typename libreoffice_internal::ConstCharArrayDetector< T, OString& >::Type operator=( T& literal )
328 RTL_STRING_CONST_FUNCTION
329 assert(
330 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
331 if (libreoffice_internal::ConstCharArrayDetector<T>::length == 0) {
332 rtl_string_new(&pData);
333 } else {
334 rtl_string_newFromLiteral(
335 &pData,
336 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
337 literal),
338 libreoffice_internal::ConstCharArrayDetector<T>::length, 0);
340 return *this;
344 Append a string to this string.
346 @param str a OString.
348 OString & operator+=( const OString & str )
349 #if defined LIBO_INTERNAL_ONLY && HAVE_CXX11_REF_QUALIFIER
351 #endif
353 rtl_string_newConcat( &pData, pData, str.pData );
354 return *this;
356 #if defined LIBO_INTERNAL_ONLY && HAVE_CXX11_REF_QUALIFIER
357 void operator+=(OString const &) && = delete;
358 #endif
360 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
362 @overload
363 @internal
365 template< typename T1, typename T2 >
366 OString& operator+=( const OStringConcat< T1, T2 >& c )
367 #if HAVE_CXX11_REF_QUALIFIER
369 #endif
371 sal_Int32 l = c.length();
372 if( l == 0 )
373 return *this;
374 l += pData->length;
375 rtl_string_ensureCapacity( &pData, l );
376 char* end = c.addData( pData->buffer + pData->length );
377 *end = '\0';
378 pData->length = l;
379 return *this;
381 #if HAVE_CXX11_REF_QUALIFIER
382 template<typename T1, typename T2> void operator +=(
383 OStringConcat<T1, T2> const &) && = delete;
384 #endif
385 #endif
388 Clears the string, i.e, makes a zero-character string
389 @since LibreOffice 4.4
391 void clear()
393 rtl_string_new( &pData );
397 Returns the length of this string.
399 The length is equal to the number of characters in this string.
401 @return the length of the sequence of characters represented by this
402 object.
404 sal_Int32 getLength() const { return pData->length; }
407 Checks if a string is empty.
409 @return true if the string is empty;
410 false, otherwise.
412 @since LibreOffice 3.4
414 bool isEmpty() const
416 return pData->length == 0;
420 Returns a pointer to the characters of this string.
422 <p>The returned pointer is guaranteed to point to a null-terminated byte
423 string. But note that this string object may contain embedded null
424 characters, which will thus also be embedded in the returned
425 null-terminated byte string.</p>
427 @return a pointer to a null-terminated byte string representing the
428 characters of this string object.
430 const sal_Char * getStr() const { return pData->buffer; }
433 Access to individual characters.
435 @param index must be non-negative and less than length.
437 @return the character at the given index.
439 @since LibreOffice 3.5
441 sal_Char operator [](sal_Int32 index) const {
442 // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
443 assert(index >= 0 && static_cast<sal_uInt32>(index) < static_cast<sal_uInt32>(getLength()));
444 return getStr()[index];
448 Compares two strings.
450 The comparison is based on the numeric value of each character in
451 the strings and return a value indicating their relationship.
452 This function can't be used for language specific sorting.
454 @param str the object to be compared.
455 @return 0 - if both strings are equal
456 < 0 - if this string is less than the string argument
457 > 0 - if this string is greater than the string argument
459 sal_Int32 compareTo( const OString & str ) const
461 return rtl_str_compare_WithLength( pData->buffer, pData->length,
462 str.pData->buffer, str.pData->length );
466 Compares two strings with an maximum count of characters.
468 The comparison is based on the numeric value of each character in
469 the strings and return a value indicating their relationship.
470 This function can't be used for language specific sorting.
472 @param rObj the object to be compared.
473 @param maxLength the maximum count of characters to be compared.
474 @return 0 - if both strings are equal
475 < 0 - if this string is less than the string argument
476 > 0 - if this string is greater than the string argument
478 sal_Int32 compareTo( const OString & rObj, sal_Int32 maxLength ) const
480 return rtl_str_shortenedCompare_WithLength( pData->buffer, pData->length,
481 rObj.pData->buffer, rObj.pData->length, maxLength );
485 Compares two strings in reverse order.
487 The comparison is based on the numeric value of each character in
488 the strings and return a value indicating their relationship.
489 This function can't be used for language specific sorting.
491 @param str the object to be compared.
492 @return 0 - if both strings are equal
493 < 0 - if this string is less than the string argument
494 > 0 - if this string is greater than the string argument
496 sal_Int32 reverseCompareTo( const OString & str ) const
498 return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length,
499 str.pData->buffer, str.pData->length );
503 Perform a comparison of two strings.
505 The result is true if and only if second string
506 represents the same sequence of characters as the first string.
507 This function can't be used for language specific comparison.
509 @param str the object to be compared.
510 @return true if the strings are equal;
511 false, otherwise.
513 bool equals( const OString & str ) const
515 if ( pData->length != str.pData->length )
516 return false;
517 if ( pData == str.pData )
518 return true;
519 return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length,
520 str.pData->buffer, str.pData->length ) == 0;
524 Perform a comparison of two strings.
526 The result is true if and only if second string
527 represents the same sequence of characters as the first string.
528 The ASCII string must be NULL-terminated and must be greater or
529 equal as length.
530 This function can't be used for language specific comparison.
533 @param value a character array.
534 @param length the length of the character array.
535 @return true if the strings are equal;
536 false, otherwise.
538 bool equalsL( const sal_Char* value, sal_Int32 length ) const
540 if ( pData->length != length )
541 return false;
543 return rtl_str_reverseCompare_WithLength( pData->buffer, pData->length,
544 value, length ) == 0;
548 Perform a ASCII lowercase comparison of two strings.
550 The result is true if and only if second string
551 represents the same sequence of characters as the first string,
552 ignoring the case.
553 Character values between 65 and 90 (ASCII A-Z) are interpreted as
554 values between 97 and 122 (ASCII a-z).
555 This function can't be used for language specific comparison.
557 @param str the object to be compared.
558 @return true if the strings are equal;
559 false, otherwise.
561 bool equalsIgnoreAsciiCase( const OString & str ) const
563 if ( pData->length != str.pData->length )
564 return false;
565 if ( pData == str.pData )
566 return true;
567 return rtl_str_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
568 str.pData->buffer, str.pData->length ) == 0;
572 Perform a ASCII lowercase comparison of two strings.
574 The result is true if and only if second string
575 represents the same sequence of characters as the first string,
576 ignoring the case.
577 Character values between 65 and 90 (ASCII A-Z) are interpreted as
578 values between 97 and 122 (ASCII a-z).
579 Since this method is optimized for performance, the ASCII character
580 values are not converted in any way. The caller has to make sure that
581 all ASCII characters are in the allowed range between 0 and
582 127. The ASCII string must be NULL-terminated.
583 This function can't be used for language specific comparison.
585 Note: The argument type is always either char* or const char*, the return type is bool.
586 The template is used only for technical reasons.
588 @param asciiStr the 8-Bit ASCII character string to be compared.
589 @return true if the strings are equal;
590 false, otherwise.
592 template< typename T >
593 typename libreoffice_internal::CharPtrDetector< T, bool >::Type equalsIgnoreAsciiCase( const T& asciiStr ) const
595 return rtl_str_compareIgnoreAsciiCase( pData->buffer, asciiStr ) == 0;
598 template< typename T >
599 typename libreoffice_internal::NonConstCharArrayDetector< T, bool >::Type equalsIgnoreAsciiCase( T& asciiStr ) const
601 return rtl_str_compareIgnoreAsciiCase( pData->buffer, asciiStr ) == 0;
605 @overload
606 This function accepts an ASCII string literal as its argument.
607 @since LibreOffice 3.6
609 template< typename T >
610 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type equalsIgnoreAsciiCase( T& literal ) const
612 RTL_STRING_CONST_FUNCTION
613 assert(
614 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
615 return
616 (pData->length
617 == libreoffice_internal::ConstCharArrayDetector<T>::length)
618 && (rtl_str_compareIgnoreAsciiCase_WithLength(
619 pData->buffer, pData->length,
620 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
621 literal),
622 libreoffice_internal::ConstCharArrayDetector<T>::length)
623 == 0);
627 Perform a ASCII lowercase comparison of two strings.
629 The result is true if and only if second string
630 represents the same sequence of characters as the first string,
631 ignoring the case.
632 Character values between 65 and 90 (ASCII A-Z) are interpreted as
633 values between 97 and 122 (ASCII a-z).
634 Since this method is optimized for performance, the ASCII character
635 values are not converted in any way. The caller has to make sure that
636 all ASCII characters are in the allowed range between 0 and
637 127. The ASCII string must be greater or equal in length as asciiStrLength.
638 This function can't be used for language specific comparison.
640 @param asciiStr the 8-Bit ASCII character string to be compared.
641 @param asciiStrLength the length of the ascii string
642 @return true if the strings are equal;
643 false, otherwise.
645 bool equalsIgnoreAsciiCaseL( const sal_Char * asciiStr, sal_Int32 asciiStrLength ) const
647 if ( pData->length != asciiStrLength )
648 return false;
650 return rtl_str_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length,
651 asciiStr, asciiStrLength ) == 0;
655 Match against a substring appearing in this string.
657 The result is true if and only if the second string appears as a substring
658 of this string, at the given position.
659 This function can't be used for language specific comparison.
661 @param str the object (substring) to be compared.
662 @param fromIndex the index to start the comparion from.
663 The index must be greater or equal than 0
664 and less or equal as the string length.
665 @return true if str match with the characters in the string
666 at the given position;
667 false, otherwise.
669 bool match( const OString & str, sal_Int32 fromIndex = 0 ) const
671 return rtl_str_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
672 str.pData->buffer, str.pData->length, str.pData->length ) == 0;
676 @overload
677 This function accepts an ASCII string literal as its argument.
678 @since LibreOffice 3.6
680 template< typename T >
681 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type match( T& literal, sal_Int32 fromIndex = 0 ) const
683 RTL_STRING_CONST_FUNCTION
684 assert(
685 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
686 return
687 rtl_str_shortenedCompare_WithLength(
688 pData->buffer + fromIndex, pData->length - fromIndex,
689 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
690 literal),
691 libreoffice_internal::ConstCharArrayDetector<T>::length,
692 libreoffice_internal::ConstCharArrayDetector<T>::length)
693 == 0;
697 Match against a substring appearing in this string.
699 @param str the substring to be compared; must not be null and must point
700 to memory of at least strLength bytes
702 @param strLength the length of the substring; must be non-negative
704 @param fromIndex the index into this string to start the comparison at;
705 must be non-negative and not greater than this string's length
707 @return true if and only if the given str is contained as a substring of
708 this string at the given fromIndex
710 @since LibreOffice 3.6
712 bool matchL(
713 char const * str, sal_Int32 strLength, sal_Int32 fromIndex = 0)
714 const
716 return rtl_str_shortenedCompare_WithLength(
717 pData->buffer + fromIndex, pData->length - fromIndex,
718 str, strLength, strLength) == 0;
721 // This overload is left undefined, to detect calls of matchL that
722 // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
723 // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
724 // platforms):
725 #if SAL_TYPES_SIZEOFLONG == 8
726 void matchL(char const *, sal_Int32, rtl_TextEncoding) const;
727 #endif
730 Match against a substring appearing in this string, ignoring the case of
731 ASCII letters.
733 The result is true if and only if the second string appears as a substring
734 of this string, at the given position.
735 Character values between 65 and 90 (ASCII A-Z) are interpreted as
736 values between 97 and 122 (ASCII a-z).
737 This function can't be used for language specific comparison.
739 @param str the object (substring) to be compared.
740 @param fromIndex the index to start the comparion from.
741 The index must be greater or equal than 0
742 and less or equal as the string length.
743 @return true if str match with the characters in the string
744 at the given position;
745 false, otherwise.
747 bool matchIgnoreAsciiCase( const OString & str, sal_Int32 fromIndex = 0 ) const
749 return rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
750 str.pData->buffer, str.pData->length,
751 str.pData->length ) == 0;
755 @overload
756 This function accepts an ASCII string literal as its argument.
757 @since LibreOffice 3.6
759 template< typename T >
760 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type matchIgnoreAsciiCase( T& literal, sal_Int32 fromIndex = 0 ) const
762 RTL_STRING_CONST_FUNCTION
763 assert(
764 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
765 return
766 rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
767 pData->buffer+fromIndex, pData->length-fromIndex,
768 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
769 literal),
770 libreoffice_internal::ConstCharArrayDetector<T>::length,
771 libreoffice_internal::ConstCharArrayDetector<T>::length)
772 == 0;
776 Check whether this string starts with a given substring.
778 @param str the substring to be compared
780 @param rest if non-null, and this function returns true, then assign a
781 copy of the remainder of this string to *rest. Available since
782 LibreOffice 4.2
784 @return true if and only if the given str appears as a substring at the
785 start of this string
787 @since LibreOffice 4.0
789 bool startsWith(OString const & str, OString * rest = NULL) const {
790 bool b = match(str);
791 if (b && rest != NULL) {
792 *rest = copy(str.getLength());
794 return b;
798 @overload
799 This function accepts an ASCII string literal as its argument.
800 @since LibreOffice 4.0
802 template< typename T >
803 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type startsWith(
804 T & literal, OString * rest = NULL) const
806 RTL_STRING_CONST_FUNCTION
807 bool b = match(literal, 0);
808 if (b && rest != NULL) {
809 *rest = copy(
810 libreoffice_internal::ConstCharArrayDetector<T>::length);
812 return b;
816 Check whether this string starts with a given string, ignoring the case of
817 ASCII letters.
819 Character values between 65 and 90 (ASCII A-Z) are interpreted as
820 values between 97 and 122 (ASCII a-z).
821 This function can't be used for language specific comparison.
823 @param str the substring to be compared
825 @param rest if non-null, and this function returns true, then assign a
826 copy of the remainder of this string to *rest.
828 @return true if and only if the given str appears as a substring at the
829 start of this string, ignoring the case of ASCII letters ("A"--"Z" and
830 "a"--"z")
832 @since LibreOffice 5.1
834 bool startsWithIgnoreAsciiCase(OString const & str, OString * rest = NULL)
835 const
837 bool b = matchIgnoreAsciiCase(str);
838 if (b && rest != NULL) {
839 *rest = copy(str.getLength());
841 return b;
845 @overload
846 This function accepts an ASCII string literal as its argument.
847 @since LibreOffice 5.1
849 template< typename T >
850 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type
851 startsWithIgnoreAsciiCase(T & literal, OString * rest = NULL) const
853 RTL_STRING_CONST_FUNCTION
854 assert(
855 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
856 bool b = matchIgnoreAsciiCase(literal);
857 if (b && rest != NULL) {
858 *rest = copy(
859 libreoffice_internal::ConstCharArrayDetector<T>::length);
861 return b;
865 Check whether this string ends with a given substring.
867 @param str the substring to be compared
869 @param rest if non-null, and this function returns true, then assign a
870 copy of the remainder of this string to *rest. Available since
871 LibreOffice 4.2
873 @return true if and only if the given str appears as a substring at the
874 end of this string
876 @since LibreOffice 3.6
878 bool endsWith(OString const & str, OString * rest = NULL) const {
879 bool b = str.getLength() <= getLength()
880 && match(str, getLength() - str.getLength());
881 if (b && rest != NULL) {
882 *rest = copy(0, getLength() - str.getLength());
884 return b;
888 @overload
889 This function accepts an ASCII string literal as its argument.
890 @since LibreOffice 3.6
892 template< typename T >
893 typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type endsWith(
894 T & literal, OString * rest = NULL) const
896 RTL_STRING_CONST_FUNCTION
897 assert(
898 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
899 bool b
900 = (libreoffice_internal::ConstCharArrayDetector<T>::length
901 <= sal_uInt32(getLength()))
902 && match(
903 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
904 literal),
905 (getLength()
906 - libreoffice_internal::ConstCharArrayDetector<T>::length));
907 if (b && rest != NULL) {
908 *rest = copy(
910 (getLength()
911 - libreoffice_internal::ConstCharArrayDetector<T>::length));
913 return b;
917 Check whether this string ends with a given substring.
919 @param str the substring to be compared; must not be null and must point
920 to memory of at least strLength bytes
922 @param strLength the length of the substring; must be non-negative
924 @return true if and only if the given str appears as a substring at the
925 end of this string
927 @since LibreOffice 3.6
929 bool endsWithL(char const * str, sal_Int32 strLength) const {
930 return strLength <= getLength()
931 && matchL(str, strLength, getLength() - strLength);
934 friend bool operator == ( const OString& rStr1, const OString& rStr2 )
935 { return rStr1.equals(rStr2); }
936 friend bool operator != ( const OString& rStr1, const OString& rStr2 )
937 { return !(operator == ( rStr1, rStr2 )); }
938 friend bool operator < ( const OString& rStr1, const OString& rStr2 )
939 { return rStr1.compareTo( rStr2 ) < 0; }
940 friend bool operator > ( const OString& rStr1, const OString& rStr2 )
941 { return rStr1.compareTo( rStr2 ) > 0; }
942 friend bool operator <= ( const OString& rStr1, const OString& rStr2 )
943 { return rStr1.compareTo( rStr2 ) <= 0; }
944 friend bool operator >= ( const OString& rStr1, const OString& rStr2 )
945 { return rStr1.compareTo( rStr2 ) >= 0; }
947 template< typename T >
948 friend typename libreoffice_internal::CharPtrDetector< T, bool >::Type operator==( const OString& rStr1, const T& value )
950 return rStr1.compareTo( value ) == 0;
953 template< typename T >
954 friend typename libreoffice_internal::NonConstCharArrayDetector< T, bool >::Type operator==( const OString& rStr1, T& value )
956 return rStr1.compareTo( value ) == 0;
959 template< typename T >
960 friend typename libreoffice_internal::CharPtrDetector< T, bool >::Type operator==( const T& value, const OString& rStr2 )
962 return rStr2.compareTo( value ) == 0;
965 template< typename T >
966 friend typename libreoffice_internal::NonConstCharArrayDetector< T, bool >::Type operator==( T& value, const OString& rStr2 )
968 return rStr2.compareTo( value ) == 0;
972 @overload
973 This function accepts an ASCII string literal as its argument.
974 @since LibreOffice 3.6
976 template< typename T >
977 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator==( const OString& rStr, T& literal )
979 RTL_STRING_CONST_FUNCTION
980 assert(
981 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
982 return
983 (rStr.getLength()
984 == libreoffice_internal::ConstCharArrayDetector<T>::length)
985 && (rtl_str_compare_WithLength(
986 rStr.pData->buffer, rStr.pData->length,
987 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
988 literal),
989 libreoffice_internal::ConstCharArrayDetector<T>::length)
990 == 0);
994 @overload
995 This function accepts an ASCII string literal as its argument.
996 @since LibreOffice 3.6
998 template< typename T >
999 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator==( T& literal, const OString& rStr )
1001 RTL_STRING_CONST_FUNCTION
1002 assert(
1003 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1004 return
1005 (rStr.getLength()
1006 == libreoffice_internal::ConstCharArrayDetector<T>::length)
1007 && (rtl_str_compare_WithLength(
1008 rStr.pData->buffer, rStr.pData->length,
1009 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(
1010 literal),
1011 libreoffice_internal::ConstCharArrayDetector<T>::length)
1012 == 0);
1015 template< typename T >
1016 friend typename libreoffice_internal::CharPtrDetector< T, bool >::Type operator!=( const OString& rStr1, const T& value )
1018 return !(operator == ( rStr1, value ));
1021 template< typename T >
1022 friend typename libreoffice_internal::NonConstCharArrayDetector< T, bool >::Type operator!=( const OString& rStr1, T& value )
1024 return !(operator == ( rStr1, value ));
1027 template< typename T >
1028 friend typename libreoffice_internal::CharPtrDetector< T, bool >::Type operator!=( const T& value, const OString& rStr2 )
1030 return !(operator == ( value, rStr2 ));
1033 template< typename T >
1034 friend typename libreoffice_internal::NonConstCharArrayDetector< T, bool >::Type operator!=( T& value, const OString& rStr2 )
1036 return !(operator == ( value, rStr2 ));
1040 @overload
1041 This function accepts an ASCII string literal as its argument.
1042 @since LibreOffice 3.6
1044 template< typename T >
1045 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator!=( const OString& rStr, T& literal )
1047 return !( rStr == literal );
1051 @overload
1052 This function accepts an ASCII string literal as its argument.
1053 @since LibreOffice 3.6
1055 template< typename T >
1056 friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator!=( T& literal, const OString& rStr )
1058 return !( literal == rStr );
1062 Returns a hashcode for this string.
1064 @return a hash code value for this object.
1066 @see rtl::OStringHash for convenient use of std::unordered_map
1068 sal_Int32 hashCode() const
1070 return rtl_str_hashCode_WithLength( pData->buffer, pData->length );
1074 Returns the index within this string of the first occurrence of the
1075 specified character, starting the search at the specified index.
1077 @param ch character to be located.
1078 @param fromIndex the index to start the search from.
1079 The index must be greater or equal than 0
1080 and less or equal as the string length.
1081 @return the index of the first occurrence of the character in the
1082 character sequence represented by this string that is
1083 greater than or equal to fromIndex, or
1084 -1 if the character does not occur.
1086 sal_Int32 indexOf( sal_Char ch, sal_Int32 fromIndex = 0 ) const
1088 sal_Int32 ret = rtl_str_indexOfChar_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, ch );
1089 return (ret < 0 ? ret : ret+fromIndex);
1093 Returns the index within this string of the last occurrence of the
1094 specified character, searching backward starting at the end.
1096 @param ch character to be located.
1097 @return the index of the last occurrence of the character in the
1098 character sequence represented by this string, or
1099 -1 if the character does not occur.
1101 sal_Int32 lastIndexOf( sal_Char ch ) const
1103 return rtl_str_lastIndexOfChar_WithLength( pData->buffer, pData->length, ch );
1107 Returns the index within this string of the last occurrence of the
1108 specified character, searching backward starting before the specified
1109 index.
1111 @param ch character to be located.
1112 @param fromIndex the index before which to start the search.
1113 @return the index of the last occurrence of the character in the
1114 character sequence represented by this string that
1115 is less than fromIndex, or -1
1116 if the character does not occur before that point.
1118 sal_Int32 lastIndexOf( sal_Char ch, sal_Int32 fromIndex ) const
1120 return rtl_str_lastIndexOfChar_WithLength( pData->buffer, fromIndex, ch );
1124 Returns the index within this string of the first occurrence of the
1125 specified substring, starting at the specified index.
1127 If str doesn't include any character, always -1 is
1128 returned. This is also the case, if both strings are empty.
1130 @param str the substring to search for.
1131 @param fromIndex the index to start the search from.
1132 @return If the string argument occurs one or more times as a substring
1133 within this string at the starting index, then the index
1134 of the first character of the first such substring is
1135 returned. If it does not occur as a substring starting
1136 at fromIndex or beyond, -1 is returned.
1138 sal_Int32 indexOf( const OString & str, sal_Int32 fromIndex = 0 ) const
1140 sal_Int32 ret = rtl_str_indexOfStr_WithLength( pData->buffer+fromIndex, pData->length-fromIndex,
1141 str.pData->buffer, str.pData->length );
1142 return (ret < 0 ? ret : ret+fromIndex);
1146 @overload
1147 This function accepts an ASCII string literal as its argument.
1148 @since LibreOffice 3.6
1150 template< typename T >
1151 typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const
1153 RTL_STRING_CONST_FUNCTION
1154 assert(
1155 libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
1156 sal_Int32 n = rtl_str_indexOfStr_WithLength(
1157 pData->buffer + fromIndex, pData->length - fromIndex,
1158 libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
1159 libreoffice_internal::ConstCharArrayDetector<T>::length);
1160 return n < 0 ? n : n + fromIndex;
1164 Returns the index within this string of the first occurrence of the
1165 specified substring, starting at the specified index.
1167 If str doesn't include any character, always -1 is
1168 returned. This is also the case, if both strings are empty.
1170 @param str the substring to search for.
1171 @param len the length of the substring.
1172 @param fromIndex the index to start the search from.
1173 @return If the string argument occurs one or more times as a substring
1174 within this string at the starting index, then the index
1175 of the first character of the first such substring is
1176 returned. If it does not occur as a substring starting
1177 at fromIndex or beyond, -1 is returned.
1179 @since LibreOffice 3.6
1181 sal_Int32 indexOfL(char const * str, sal_Int32 len, sal_Int32 fromIndex = 0)
1182 const
1184 sal_Int32 n = rtl_str_indexOfStr_WithLength(
1185 pData->buffer + fromIndex, pData->length - fromIndex, str, len);
1186 return n < 0 ? n : n + fromIndex;
1189 // This overload is left undefined, to detect calls of indexOfL that
1190 // erroneously use RTL_CONSTASCII_USTRINGPARAM instead of
1191 // RTL_CONSTASCII_STRINGPARAM (but would lead to ambiguities on 32 bit
1192 // platforms):
1193 #if SAL_TYPES_SIZEOFLONG == 8
1194 void indexOfL(char const *, sal_Int32, rtl_TextEncoding) const;
1195 #endif
1198 Returns the index within this string of the last occurrence of
1199 the specified substring, searching backward starting at the end.
1201 The returned index indicates the starting index of the substring
1202 in this string.
1203 If str doesn't include any character, always -1 is
1204 returned. This is also the case, if both strings are empty.
1206 @param str the substring to search for.
1207 @return If the string argument occurs one or more times as a substring
1208 within this string, then the index of the first character of
1209 the last such substring is returned. If it does not occur as
1210 a substring, -1 is returned.
1212 sal_Int32 lastIndexOf( const OString & str ) const
1214 return rtl_str_lastIndexOfStr_WithLength( pData->buffer, pData->length,
1215 str.pData->buffer, str.pData->length );
1219 Returns the index within this string of the last occurrence of
1220 the specified substring, searching backward starting before the specified
1221 index.
1223 The returned index indicates the starting index of the substring
1224 in this string.
1225 If str doesn't include any character, always -1 is
1226 returned. This is also the case, if both strings are empty.
1228 @param str the substring to search for.
1229 @param fromIndex the index before which to start the search.
1230 @return If the string argument occurs one or more times as a substring
1231 within this string before the starting index, then the index
1232 of the first character of the last such substring is
1233 returned. Otherwise, -1 is returned.
1235 sal_Int32 lastIndexOf( const OString & str, sal_Int32 fromIndex ) const
1237 return rtl_str_lastIndexOfStr_WithLength( pData->buffer, fromIndex,
1238 str.pData->buffer, str.pData->length );
1242 Returns a new string that is a substring of this string.
1244 The substring begins at the specified beginIndex. If
1245 beginIndex is negative or be greater than the length of
1246 this string, behaviour is undefined.
1248 @param beginIndex the beginning index, inclusive.
1249 @return the specified substring.
1251 SAL_WARN_UNUSED_RESULT OString copy( sal_Int32 beginIndex ) const
1253 rtl_String *pNew = NULL;
1254 rtl_string_newFromSubString( &pNew, pData, beginIndex, getLength() - beginIndex );
1255 return OString( pNew, SAL_NO_ACQUIRE );
1259 Returns a new string that is a substring of this string.
1261 The substring begins at the specified beginIndex and contains count
1262 characters. If either beginIndex or count are negative,
1263 or beginIndex + count are greater than the length of this string
1264 then behaviour is undefined.
1266 @param beginIndex the beginning index, inclusive.
1267 @param count the number of characters.
1268 @return the specified substring.
1270 SAL_WARN_UNUSED_RESULT OString copy( sal_Int32 beginIndex, sal_Int32 count ) const
1272 rtl_String *pNew = NULL;
1273 rtl_string_newFromSubString( &pNew, pData, beginIndex, count );
1274 return OString( pNew, SAL_NO_ACQUIRE );
1278 Concatenates the specified string to the end of this string.
1280 @param str the string that is concatenated to the end
1281 of this string.
1282 @return a string that represents the concatenation of this string
1283 followed by the string argument.
1285 SAL_WARN_UNUSED_RESULT OString concat( const OString & str ) const
1287 rtl_String* pNew = NULL;
1288 rtl_string_newConcat( &pNew, pData, str.pData );
1289 return OString( pNew, SAL_NO_ACQUIRE );
1292 #ifndef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
1293 friend OString operator+( const OString & str1, const OString & str2 )
1295 return str1.concat( str2 );
1297 #endif
1300 Returns a new string resulting from replacing n = count characters
1301 from position index in this string with newStr.
1303 @param index the replacing index in str.
1304 The index must be greater or equal as 0 and
1305 less or equal as the length of the string.
1306 @param count the count of characters that will replaced
1307 The count must be greater or equal as 0 and
1308 less or equal as the length of the string minus index.
1309 @param newStr the new substring.
1310 @return the new string.
1312 SAL_WARN_UNUSED_RESULT OString replaceAt( sal_Int32 index, sal_Int32 count, const OString& newStr ) const
1314 rtl_String* pNew = NULL;
1315 rtl_string_newReplaceStrAt( &pNew, pData, index, count, newStr.pData );
1316 return OString( pNew, SAL_NO_ACQUIRE );
1320 Returns a new string resulting from replacing all occurrences of
1321 oldChar in this string with newChar.
1323 If the character oldChar does not occur in the character sequence
1324 represented by this object, then the string is assigned with
1325 str.
1327 @param oldChar the old character.
1328 @param newChar the new character.
1329 @return a string derived from this string by replacing every
1330 occurrence of oldChar with newChar.
1332 SAL_WARN_UNUSED_RESULT OString replace( sal_Char oldChar, sal_Char newChar ) const
1334 rtl_String* pNew = NULL;
1335 rtl_string_newReplace( &pNew, pData, oldChar, newChar );
1336 return OString( pNew, SAL_NO_ACQUIRE );
1340 Returns a new string resulting from replacing the first occurrence of a
1341 given substring with another substring.
1343 @param from the substring to be replaced
1345 @param to the replacing substring
1347 @param[in,out] index pointer to a start index; if the pointer is
1348 non-null: upon entry to the function, its value is the index into the this
1349 string at which to start searching for the \p from substring, the value
1350 must be non-negative and not greater than this string's length; upon exit
1351 from the function its value is the index into this string at which the
1352 replacement took place or -1 if no replacement took place; if the pointer
1353 is null, searching always starts at index 0
1355 @since LibreOffice 3.6
1357 SAL_WARN_UNUSED_RESULT OString replaceFirst(
1358 OString const & from, OString const & to, sal_Int32 * index = NULL) const
1360 rtl_String * s = NULL;
1361 sal_Int32 i = 0;
1362 rtl_string_newReplaceFirst(
1363 &s, pData, from.pData->buffer, from.pData->length,
1364 to.pData->buffer, to.pData->length, index == NULL ? &i : index);
1365 return OString(s, SAL_NO_ACQUIRE);
1369 Returns a new string resulting from replacing all occurrences of a given
1370 substring with another substring.
1372 Replacing subsequent occurrences picks up only after a given replacement.
1373 That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
1375 @param from the substring to be replaced
1377 @param to the replacing substring
1379 @since LibreOffice 3.6
1381 SAL_WARN_UNUSED_RESULT OString replaceAll(OString const & from, OString const & to) const {
1382 rtl_String * s = NULL;
1383 rtl_string_newReplaceAll(
1384 &s, pData, from.pData->buffer, from.pData->length,
1385 to.pData->buffer, to.pData->length);
1386 return OString(s, SAL_NO_ACQUIRE);
1390 Converts from this string all ASCII uppercase characters (65-90)
1391 to ASCII lowercase characters (97-122).
1393 This function can't be used for language specific conversion.
1394 If the string doesn't contain characters which must be converted,
1395 then the new string is assigned with str.
1397 @return the string, converted to ASCII lowercase.
1399 SAL_WARN_UNUSED_RESULT OString toAsciiLowerCase() const
1401 rtl_String* pNew = NULL;
1402 rtl_string_newToAsciiLowerCase( &pNew, pData );
1403 return OString( pNew, SAL_NO_ACQUIRE );
1407 Converts from this string all ASCII lowercase characters (97-122)
1408 to ASCII uppercase characters (65-90).
1410 This function can't be used for language specific conversion.
1411 If the string doesn't contain characters which must be converted,
1412 then the new string is assigned with str.
1414 @return the string, converted to ASCII uppercase.
1416 SAL_WARN_UNUSED_RESULT OString toAsciiUpperCase() const
1418 rtl_String* pNew = NULL;
1419 rtl_string_newToAsciiUpperCase( &pNew, pData );
1420 return OString( pNew, SAL_NO_ACQUIRE );
1424 Returns a new string resulting from removing white space from both ends
1425 of the string.
1427 All characters that have codes less than or equal to
1428 32 (the space character) are considered to be white space.
1429 If the string doesn't contain white spaces at both ends,
1430 then the new string is assigned with str.
1432 @return the string, with white space removed from the front and end.
1434 SAL_WARN_UNUSED_RESULT OString trim() const
1436 rtl_String* pNew = NULL;
1437 rtl_string_newTrim( &pNew, pData );
1438 return OString( pNew, SAL_NO_ACQUIRE );
1442 Returns a token in the string.
1444 Example:
1445 sal_Int32 nIndex = 0;
1449 OString aToken = aStr.getToken( 0, ';', nIndex );
1452 while ( nIndex >= 0 );
1454 @param token the number of the token to return.
1455 @param cTok the character which separate the tokens.
1456 @param index the position at which the token is searched in the
1457 string.
1458 The index must not be greater than the length of the
1459 string.
1460 This param is set to the position of the
1461 next token or to -1, if it is the last token.
1462 @return the token; if either token or index is negative, an empty token
1463 is returned (and index is set to -1)
1465 OString getToken( sal_Int32 token, sal_Char cTok, sal_Int32& index ) const
1467 rtl_String * pNew = NULL;
1468 index = rtl_string_getToken( &pNew, pData, token, cTok, index );
1469 return OString( pNew, SAL_NO_ACQUIRE );
1473 Returns a token from the string.
1475 The same as getToken(sal_Int32, sal_Char, sal_Int32 &), but always passing
1476 in 0 as the start index in the third argument.
1478 @param count the number of the token to return, starting with 0
1479 @param separator the character which separates the tokens
1481 @return the given token, or an empty string
1483 @since LibreOffice 3.6
1485 OString getToken(sal_Int32 count, char separator) const {
1486 sal_Int32 n = 0;
1487 return getToken(count, separator, n);
1491 Returns the Boolean value from this string.
1493 This function can't be used for language specific conversion.
1495 @return true, if the string is 1 or "True" in any ASCII case.
1496 false in any other case.
1498 bool toBoolean() const
1500 return rtl_str_toBoolean( pData->buffer );
1504 Returns the first character from this string.
1506 @return the first character from this string or 0, if this string
1507 is empty.
1509 sal_Char toChar() const
1511 return pData->buffer[0];
1515 Returns the int32 value from this string.
1517 This function can't be used for language specific conversion.
1519 @param radix the radix (between 2 and 36)
1520 @return the int32 represented from this string.
1521 0 if this string represents no number or one of too large
1522 magnitude.
1524 sal_Int32 toInt32( sal_Int16 radix = 10 ) const
1526 return rtl_str_toInt32( pData->buffer, radix );
1530 Returns the uint32 value from this string.
1532 This function can't be used for language specific conversion.
1534 @param radix the radix (between 2 and 36)
1535 @return the uint32 represented from this string.
1536 0 if this string represents no number or one of too large
1537 magnitude.
1539 @since LibreOffice 4.2
1541 sal_uInt32 toUInt32( sal_Int16 radix = 10 ) const
1543 return rtl_str_toUInt32( pData->buffer, radix );
1547 Returns the int64 value from this string.
1549 This function can't be used for language specific conversion.
1551 @param radix the radix (between 2 and 36)
1552 @return the int64 represented from this string.
1553 0 if this string represents no number or one of too large
1554 magnitude.
1556 sal_Int64 toInt64( sal_Int16 radix = 10 ) const
1558 return rtl_str_toInt64( pData->buffer, radix );
1562 Returns the uint64 value from this string.
1564 This function can't be used for language specific conversion.
1566 @param radix the radix (between 2 and 36)
1567 @return the uint64 represented from this string.
1568 0 if this string represents no number or one of too large
1569 magnitude.
1571 @since LibreOffice 4.1
1573 sal_uInt64 toUInt64( sal_Int16 radix = 10 ) const
1575 return rtl_str_toUInt64( pData->buffer, radix );
1579 Returns the float value from this string.
1581 This function can't be used for language specific conversion.
1583 @return the float represented from this string.
1584 0.0 if this string represents no number.
1586 float toFloat() const
1588 return rtl_str_toFloat( pData->buffer );
1592 Returns the double value from this string.
1594 This function can't be used for language specific conversion.
1596 @return the double represented from this string.
1597 0.0 if this string represents no number.
1599 double toDouble() const
1601 return rtl_str_toDouble( pData->buffer );
1605 Returns the string representation of the integer argument.
1607 This function can't be used for language specific conversion.
1609 @param i an integer value
1610 @param radix the radix (between 2 and 36)
1611 @return a string with the string representation of the argument.
1612 @since LibreOffice 4.1
1614 static OString number( int i, sal_Int16 radix = 10 )
1616 return number( static_cast< long long >( i ), radix );
1618 /// @overload
1619 /// @since LibreOffice 4.1
1620 static OString number( unsigned int i, sal_Int16 radix = 10 )
1622 return number( static_cast< unsigned long long >( i ), radix );
1624 /// @overload
1625 /// @since LibreOffice 4.1
1626 static OString number( long i, sal_Int16 radix = 10 )
1628 return number( static_cast< long long >( i ), radix );
1630 /// @overload
1631 /// @since LibreOffice 4.1
1632 static OString number( unsigned long i, sal_Int16 radix = 10 )
1634 return number( static_cast< unsigned long long >( i ), radix );
1636 /// @overload
1637 /// @since LibreOffice 4.1
1638 static OString number( long long ll, sal_Int16 radix = 10 )
1640 sal_Char aBuf[RTL_STR_MAX_VALUEOFINT64];
1641 rtl_String* pNewData = NULL;
1642 rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfInt64( aBuf, ll, radix ) );
1643 return OString( pNewData, SAL_NO_ACQUIRE );
1645 /// @overload
1646 /// @since LibreOffice 4.1
1647 static OString number( unsigned long long ll, sal_Int16 radix = 10 )
1649 sal_Char aBuf[RTL_STR_MAX_VALUEOFUINT64];
1650 rtl_String* pNewData = NULL;
1651 rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfUInt64( aBuf, ll, radix ) );
1652 return OString( pNewData, SAL_NO_ACQUIRE );
1656 Returns the string representation of the float argument.
1658 This function can't be used for language specific conversion.
1660 @param f a float.
1661 @return a string with the string representation of the argument.
1662 @since LibreOffice 4.1
1664 static OString number( float f )
1666 sal_Char aBuf[RTL_STR_MAX_VALUEOFFLOAT];
1667 rtl_String* pNewData = NULL;
1668 rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfFloat( aBuf, f ) );
1669 return OString( pNewData, SAL_NO_ACQUIRE );
1673 Returns the string representation of the double argument.
1675 This function can't be used for language specific conversion.
1677 @param d a double.
1678 @return a string with the string representation of the argument.
1679 @since LibreOffice 4.1
1681 static OString number( double d )
1683 sal_Char aBuf[RTL_STR_MAX_VALUEOFDOUBLE];
1684 rtl_String* pNewData = NULL;
1685 rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfDouble( aBuf, d ) );
1686 return OString( pNewData, SAL_NO_ACQUIRE );
1690 Returns the string representation of the sal_Bool argument.
1692 If the sal_Bool is true, the string "true" is returned.
1693 If the sal_Bool is false, the string "false" is returned.
1694 This function can't be used for language specific conversion.
1696 @param b a sal_Bool.
1697 @return a string with the string representation of the argument.
1698 @deprecated use boolean()
1700 SAL_DEPRECATED("use boolean()") static OString valueOf( sal_Bool b )
1702 return boolean(b);
1706 Returns the string representation of the boolean argument.
1708 If the argument is true, the string "true" is returned.
1709 If the argument is false, the string "false" is returned.
1710 This function can't be used for language specific conversion.
1712 @param b a bool.
1713 @return a string with the string representation of the argument.
1714 @since LibreOffice 4.1
1716 static OString boolean( bool b )
1718 sal_Char aBuf[RTL_STR_MAX_VALUEOFBOOLEAN];
1719 rtl_String* pNewData = NULL;
1720 rtl_string_newFromStr_WithLength( &pNewData, aBuf, rtl_str_valueOfBoolean( aBuf, b ) );
1721 return OString( pNewData, SAL_NO_ACQUIRE );
1725 Returns the string representation of the char argument.
1727 @param c a character.
1728 @return a string with the string representation of the argument.
1729 @deprecated use operator, function or constructor taking char or sal_Unicode argument
1731 SAL_DEPRECATED("convert to OString or use directly") static OString valueOf( sal_Char c )
1733 return OString( &c, 1 );
1737 Returns the string representation of the int argument.
1739 This function can't be used for language specific conversion.
1741 @param i a int32.
1742 @param radix the radix (between 2 and 36)
1743 @return a string with the string representation of the argument.
1744 @deprecated use number()
1746 SAL_DEPRECATED("use number()") static OString valueOf( sal_Int32 i, sal_Int16 radix = 10 )
1748 return number( i, radix );
1752 Returns the string representation of the long argument.
1754 This function can't be used for language specific conversion.
1756 @param ll a int64.
1757 @param radix the radix (between 2 and 36)
1758 @return a string with the string representation of the argument.
1759 @deprecated use number()
1761 SAL_DEPRECATED("use number()") static OString valueOf( sal_Int64 ll, sal_Int16 radix = 10 )
1763 return number( ll, radix );
1767 Returns the string representation of the float argument.
1769 This function can't be used for language specific conversion.
1771 @param f a float.
1772 @return a string with the string representation of the argument.
1773 @deprecated use number()
1775 SAL_DEPRECATED("use number()") static OString valueOf( float f )
1777 return number(f);
1781 Returns the string representation of the double argument.
1783 This function can't be used for language specific conversion.
1785 @param d a double.
1786 @return a string with the string representation of the argument.
1787 @deprecated use number()
1789 SAL_DEPRECATED("use number()") static OString valueOf( double d )
1791 return number(d);
1796 /* ======================================================================= */
1798 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
1800 A simple wrapper around string literal. It is usually not necessary to use, can
1801 be mostly used to force OString operator+ working with operands that otherwise would
1802 not trigger it.
1804 This class is not part of public API and is meant to be used only in LibreOffice code.
1805 @since LibreOffice 4.0
1807 struct SAL_WARN_UNUSED OStringLiteral
1809 template< int N >
1810 explicit OStringLiteral( const char (&str)[ N ] ) : size( N - 1 ), data( str ) { assert( strlen( str ) == N - 1 ); }
1811 int size;
1812 const char* data;
1816 @internal
1818 template<>
1819 struct ToStringHelper< OString >
1821 static int length( const OString& s ) { return s.getLength(); }
1822 static char* addData( char* buffer, const OString& s ) { return addDataHelper( buffer, s.getStr(), s.getLength()); }
1823 static const bool allowOStringConcat = true;
1824 static const bool allowOUStringConcat = false;
1828 @internal
1830 template<>
1831 struct ToStringHelper< OStringLiteral >
1833 static int length( const OStringLiteral& str ) { return str.size; }
1834 static char* addData( char* buffer, const OStringLiteral& str ) { return addDataHelper( buffer, str.data, str.size ); }
1835 static const bool allowOStringConcat = true;
1836 static const bool allowOUStringConcat = false;
1840 @internal
1842 template< typename charT, typename traits, typename T1, typename T2 >
1843 inline std::basic_ostream<charT, traits> & operator <<(
1844 std::basic_ostream<charT, traits> & stream, const OStringConcat< T1, T2 >& concat)
1846 return stream << OString( concat );
1848 #endif
1851 /** A helper to use OStrings with hash maps.
1853 Instances of this class are unary function objects that can be used as
1854 hash function arguments to std::unordered_map and similar constructs.
1856 struct OStringHash
1858 /** Compute a hash code for a string.
1860 @param rString
1861 a string.
1863 @return
1864 a hash code for the string. This hash code should not be stored
1865 persistently, as its computation may change in later revisions.
1867 size_t operator()( const OString& rString ) const
1868 { return (size_t)rString.hashCode(); }
1871 /** Equality functor for classic c-strings (i.e., null-terminated char* strings). */
1872 struct CStringEqual
1874 bool operator()( const char* p1, const char* p2) const
1875 { return rtl_str_compare(p1, p2) == 0; }
1878 /** Hashing functor for classic c-strings (i.e., null-terminated char* strings). */
1879 struct CStringHash
1881 size_t operator()(const char* p) const
1882 { return rtl_str_hashCode(p); }
1885 /* ======================================================================= */
1888 Support for rtl::OString in std::ostream (and thus in
1889 CPPUNIT_ASSERT or SAL_INFO macros, for example).
1891 @since LibreOffice 4.0
1893 template< typename charT, typename traits > std::basic_ostream<charT, traits> &
1894 operator <<(
1895 std::basic_ostream<charT, traits> & stream, OString const & rString)
1897 return stream << rString.getStr();
1898 // best effort; potentially loses data due to embedded null characters
1901 } /* Namespace */
1903 #ifdef RTL_STRING_UNITTEST
1904 namespace rtl
1906 typedef rtlunittest::OString OString;
1908 #undef RTL_STRING_CONST_FUNCTION
1909 #endif
1911 #if defined LIBO_INTERNAL_ONLY && !defined RTL_STRING_UNITTEST
1912 using ::rtl::OString;
1913 using ::rtl::OStringHash;
1914 using ::rtl::OStringLiteral;
1915 #endif
1917 #endif // INCLUDED_RTL_STRING_HXX
1919 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */