diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2015-06-24 15:40:44 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2015-06-24 15:48:21 +0200 |
commit | 4d4f3512db0cf0bf47c2ba1b39c3813842903ef7 (patch) | |
tree | d684cbc20bcff5071c0de27ec523b81f991e1823 /include/rtl | |
parent | 892455e72a57d890918f37bd2b32d6c5000ba6ff (diff) |
Generalize OUStringLiteral1
...by making all places that accept a string literal via ConstCharArrayDetector
also accept an OUStringLiteral1 via ConstCharArrayDetector (which required some
tweaking of the ConstCharArrayDetector internals). That removes the need for
special-purpose OUStringLiteral1 overloads, and will allow OUStringLiteral1 to
be used in more places.
Change-Id: I370de8480e02f8423cde5677dd38479b81bccdb2
Diffstat (limited to 'include/rtl')
-rw-r--r-- | include/rtl/strbuf.hxx | 28 | ||||
-rw-r--r-- | include/rtl/string.hxx | 130 | ||||
-rw-r--r-- | include/rtl/stringconcat.hxx | 13 | ||||
-rw-r--r-- | include/rtl/stringutils.hxx | 80 | ||||
-rw-r--r-- | include/rtl/ustrbuf.hxx | 49 | ||||
-rw-r--r-- | include/rtl/ustring.hxx | 305 |
6 files changed, 406 insertions, 199 deletions
diff --git a/include/rtl/strbuf.hxx b/include/rtl/strbuf.hxx index 81773b48a28a..307110367a1e 100644 --- a/include/rtl/strbuf.hxx +++ b/include/rtl/strbuf.hxx @@ -170,10 +170,14 @@ public: template< typename T > OStringBuffer( T& literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy()) : pData(NULL) - , nCapacity( libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 + 16 ) - { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - rtl_string_newFromLiteral( &pData, literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, 16 ); + , nCapacity( libreoffice_internal::ConstCharArrayDetector<T>::length + 16 ) + { + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + rtl_string_newFromLiteral( + &pData, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal), + libreoffice_internal::ConstCharArrayDetector<T>::length, 16); #ifdef RTL_STRING_UNITTEST rtl_string_unittest_const_literal = true; #endif @@ -453,8 +457,12 @@ public: typename libreoffice_internal::ConstCharArrayDetector< T, OStringBuffer& >::Type append( T& literal ) { RTL_STRING_CONST_FUNCTION - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ); + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + rtl_stringbuffer_insert( + &pData, &nCapacity, getLength(), + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal), + libreoffice_internal::ConstCharArrayDetector<T>::length); return *this; } @@ -708,8 +716,12 @@ public: typename libreoffice_internal::ConstCharArrayDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, T& literal ) { RTL_STRING_CONST_FUNCTION - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - rtl_stringbuffer_insert( &pData, &nCapacity, offset, literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ); + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + rtl_stringbuffer_insert( + &pData, &nCapacity, offset, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal), + libreoffice_internal::ConstCharArrayDetector<T>::length); return *this; } diff --git a/include/rtl/string.hxx b/include/rtl/string.hxx index 56cbf932cea1..e3237ff29afd 100644 --- a/include/rtl/string.hxx +++ b/include/rtl/string.hxx @@ -181,12 +181,18 @@ public: template< typename T > OString( T& literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() ) { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); pData = 0; - if( libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 == 0 ) // empty string - rtl_string_new( &pData ); - else - rtl_string_newFromLiteral( &pData, literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, 0 ); + if (libreoffice_internal::ConstCharArrayDetector<T>::length == 0) { + rtl_string_new(&pData); + } else { + rtl_string_newFromLiteral( + &pData, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer( + literal), + libreoffice_internal::ConstCharArrayDetector<T>::length, 0); + } #ifdef RTL_STRING_UNITTEST rtl_string_unittest_const_literal = true; #endif @@ -278,11 +284,17 @@ public: typename libreoffice_internal::ConstCharArrayDetector< T, OString& >::Type operator=( T& literal ) { RTL_STRING_CONST_FUNCTION - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - if( libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 == 0 ) // empty string - rtl_string_new( &pData ); - else - rtl_string_newFromLiteral( &pData, literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, 0 ); + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + if (libreoffice_internal::ConstCharArrayDetector<T>::length == 0) { + rtl_string_new(&pData); + } else { + rtl_string_newFromLiteral( + &pData, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer( + literal), + libreoffice_internal::ConstCharArrayDetector<T>::length, 0); + } return *this; } @@ -543,11 +555,17 @@ public: typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type equalsIgnoreAsciiCase( T& literal ) const { RTL_STRING_CONST_FUNCTION - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - if ( pData->length != libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ) - return false; - return rtl_str_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, - literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ) == 0; + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + return + (pData->length + == libreoffice_internal::ConstCharArrayDetector<T>::length) + && (rtl_str_compareIgnoreAsciiCase_WithLength( + pData->buffer, pData->length, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer( + literal), + libreoffice_internal::ConstCharArrayDetector<T>::length) + == 0); } /** @@ -608,10 +626,16 @@ public: typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type match( T& literal, sal_Int32 fromIndex = 0 ) const { RTL_STRING_CONST_FUNCTION - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - return rtl_str_shortenedCompare_WithLength( - pData->buffer + fromIndex, pData->length - fromIndex, - literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1) == 0; + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + return + rtl_str_shortenedCompare_WithLength( + pData->buffer + fromIndex, pData->length - fromIndex, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer( + literal), + libreoffice_internal::ConstCharArrayDetector<T>::length, + libreoffice_internal::ConstCharArrayDetector<T>::length) + == 0; } /** @@ -681,9 +705,16 @@ public: typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type matchIgnoreAsciiCase( T& literal, sal_Int32 fromIndex = 0 ) const { RTL_STRING_CONST_FUNCTION - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - return rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, - literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ) == 0; + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + return + rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( + pData->buffer+fromIndex, pData->length-fromIndex, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer( + literal), + libreoffice_internal::ConstCharArrayDetector<T>::length, + libreoffice_internal::ConstCharArrayDetector<T>::length) + == 0; } /** @@ -720,7 +751,8 @@ public: RTL_STRING_CONST_FUNCTION bool b = match(literal, 0); if (b && rest != 0) { - *rest = copy(libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1); + *rest = copy( + libreoffice_internal::ConstCharArrayDetector<T>::length); } return b; } @@ -758,14 +790,21 @@ public: T & literal, OString * rest = 0) const { RTL_STRING_CONST_FUNCTION - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - bool b = libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 <= getLength() - && match(literal, getLength() - ( libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 )); + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + bool b + = (libreoffice_internal::ConstCharArrayDetector<T>::length + <= sal_uInt32(getLength())) + && match( + libreoffice_internal::ConstCharArrayDetector<T>::toPointer( + literal), + (getLength() + - libreoffice_internal::ConstCharArrayDetector<T>::length)); if (b && rest != 0) { *rest = copy( 0, (getLength() - - (libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1))); + - libreoffice_internal::ConstCharArrayDetector<T>::length)); } return b; } @@ -834,10 +873,17 @@ public: friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator==( const OString& rStr, T& literal ) { RTL_STRING_CONST_FUNCTION - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - return rStr.getLength() == libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 - && rtl_str_compare_WithLength( rStr.pData->buffer, rStr.pData->length, literal, - libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ) == 0; + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + return + (rStr.getLength() + == libreoffice_internal::ConstCharArrayDetector<T>::length) + && (rtl_str_compare_WithLength( + rStr.pData->buffer, rStr.pData->length, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer( + literal), + libreoffice_internal::ConstCharArrayDetector<T>::length) + == 0); } /** @@ -849,10 +895,17 @@ public: friend typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator==( T& literal, const OString& rStr ) { RTL_STRING_CONST_FUNCTION - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - return rStr.getLength() == libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 - && rtl_str_compare_WithLength( rStr.pData->buffer, rStr.pData->length, literal, - libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ) == 0; + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + return + (rStr.getLength() + == libreoffice_internal::ConstCharArrayDetector<T>::length) + && (rtl_str_compare_WithLength( + rStr.pData->buffer, rStr.pData->length, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer( + literal), + libreoffice_internal::ConstCharArrayDetector<T>::length) + == 0); } template< typename T > @@ -994,9 +1047,12 @@ public: typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const { RTL_STRING_CONST_FUNCTION - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); sal_Int32 n = rtl_str_indexOfStr_WithLength( - pData->buffer + fromIndex, pData->length - fromIndex, literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1); + pData->buffer + fromIndex, pData->length - fromIndex, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal), + libreoffice_internal::ConstCharArrayDetector<T>::length); return n < 0 ? n : n + fromIndex; } diff --git a/include/rtl/stringconcat.hxx b/include/rtl/stringconcat.hxx index a74dd77ad78c..7ef9e1104094 100644 --- a/include/rtl/stringconcat.hxx +++ b/include/rtl/stringconcat.hxx @@ -141,6 +141,19 @@ struct ToStringHelper< const char[ N ] > static const bool allowOUStringConcat = true; }; +#if defined LIBO_INTERNAL_ONLY +template<char C> struct ToStringHelper<OUStringLiteral1_<C>> { + static int length(OUStringLiteral1_<C>) { return 1; } + static char * addData(char * buffer, OUStringLiteral1_<C> literal) + { return addDataHelper(buffer, &literal.c, 1); } + static sal_Unicode * addData( + sal_Unicode * buffer, OUStringLiteral1_<C> literal) + { return addDataLiteral(buffer, &literal.c, 1); } + static bool const allowOStringConcat = false; + static bool const allowOUStringConcat = true; +}; +#endif + /** @internal diff --git a/include/rtl/stringutils.hxx b/include/rtl/stringutils.hxx index fb11545569e4..32a57f399d24 100644 --- a/include/rtl/stringutils.hxx +++ b/include/rtl/stringutils.hxx @@ -11,6 +11,10 @@ #define INCLUDED_RTL_STRINGUTILS_HXX #include <sal/config.h> + +#include <cstddef> +#include <cstring> + #include <sal/types.h> // The unittest uses slightly different code to help check that the proper @@ -29,6 +33,60 @@ namespace rtl #undef rtl #endif +#if defined LIBO_INTERNAL_ONLY +/// @cond INTERNAL + +/** A simple wrapper around an ASCII character literal. + + Can be useful to pass a char constant with ASCII value into a + OUString-related function that is optimized for ASCII string literal + arguments. That is, instead of + + char const WILDCARD = '%'; + ... + if (s[i] == WILDCARD) ... + ... + if (s.endsWith(OUString(WILDCARD))) ... + + use + + char const WILDCARD = '%'; + ... + if (s[i] == WILDCARD) ... + ... + if (s.endsWith(OUStringLiteral1<WILDCARD>())) ... + + to avoid creating a temporary OUString instance, and instead pick the + endsWith overload actually designed to take an argument of type + char const[N]. + + Instances of OUStringLiteral1 need to be const, as those literal-optimized + functions take the literal argument by non-const lvalue reference, for + technical reasons. Except with MSVC, at least up to Visual Studio 2013: + For one, it fails to take that const-ness into account when trying to match + "OUStringLiteral1_<C> const" against T in a "T & literal" parameter of a + function template. But for another, as a language extension, it allows to + bind non-const temporary OUStringLiteral1_ instances to non-const lvalue + references, but also with a warning that thus needs to be disabled. + + @since LibreOffice 5.0 +*/ +template<char C> struct SAL_WARN_UNUSED OUStringLiteral1_ { + static_assert( + static_cast<unsigned char>(C) < 0x80, + "non-ASCII character in OUStringLiteral1"); + char const c = C; +}; +#if defined _MSC_VER && _MSC_VER <= 1800 // Visual Studio 2013 +template<char C> using OUStringLiteral1 = OUStringLiteral1_<C>; +#pragma warning(disable: 4239) +#else +template<char C> using OUStringLiteral1 = OUStringLiteral1_<C> const; +#endif + +/// @endcond +#endif + namespace libreoffice_internal { /* @@ -102,13 +160,27 @@ struct ConstCharArrayDetector { static const bool ok = false; }; -template< int N, typename T > +template< std::size_t N, typename T > struct ConstCharArrayDetector< const char[ N ], T > { typedef T Type; - static const int size = N; + static const std::size_t length = N - 1; static const bool ok = true; + static bool isValid(char const (& literal)[N]) + { return std::strlen(literal) == length; } + static char const * toPointer(char const (& literal)[N]) { return literal; } }; +#if defined LIBO_INTERNAL_ONLY +template<char C, typename T> +struct ConstCharArrayDetector<OUStringLiteral1<C>, T> { + typedef T Type; + static const std::size_t length = 1; + static const bool ok = true; + static bool isValid(OUStringLiteral1<C>) { return true; } + static char const * toPointer(OUStringLiteral1<C> & literal) + { return &literal.c; } +}; +#endif // this one is used to rule out only const char[N] template< typename T > @@ -137,6 +209,10 @@ template< int N > struct ExceptCharArrayDetector< const char[ N ] > { }; +#if defined LIBO_INTERNAL_ONLY && defined _MSC_VER && _MSC_VER <= 1800 + // Visual Studio 2013 +template<char C> struct ExceptCharArrayDetector<OUStringLiteral1<C>> {}; +#endif template< typename T1, typename T2 = void > struct SalUnicodePtrDetector diff --git a/include/rtl/ustrbuf.hxx b/include/rtl/ustrbuf.hxx index 04ac8a6c00f4..fe64ebe48280 100644 --- a/include/rtl/ustrbuf.hxx +++ b/include/rtl/ustrbuf.hxx @@ -132,10 +132,14 @@ public: template< typename T > OUStringBuffer( T& literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() ) : pData(NULL) - , nCapacity( libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 + 16 ) - { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - rtl_uString_newFromLiteral( &pData, literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, 16 ); + , nCapacity( libreoffice_internal::ConstCharArrayDetector<T>::length + 16 ) + { + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + rtl_uString_newFromLiteral( + &pData, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal), + libreoffice_internal::ConstCharArrayDetector<T>::length, 16); #ifdef RTL_STRING_UNITTEST rtl_string_unittest_const_literal = true; #endif @@ -470,9 +474,12 @@ public: template< typename T > typename libreoffice_internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type append( T& literal ) { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), literal, - libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ); + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + rtl_uStringbuffer_insert_ascii( + &pData, &nCapacity, getLength(), + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal), + libreoffice_internal::ConstCharArrayDetector<T>::length); return *this; } @@ -813,9 +820,12 @@ public: template< typename T > typename libreoffice_internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type insert( sal_Int32 offset, T& literal ) { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, offset, literal, - libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ); + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + rtl_uStringbuffer_insert_ascii( + &pData, &nCapacity, offset, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal), + libreoffice_internal::ConstCharArrayDetector<T>::length); return *this; } @@ -1199,11 +1209,13 @@ public: template< typename T > typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - sal_Int32 ret = rtl_ustr_indexOfAscii_WithLength( - pData->buffer + fromIndex, pData->length - fromIndex, literal, - libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1); - return ret < 0 ? ret : ret + fromIndex; + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + sal_Int32 n = rtl_ustr_indexOfAscii_WithLength( + pData->buffer + fromIndex, pData->length - fromIndex, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal), + libreoffice_internal::ConstCharArrayDetector<T>::length); + return n < 0 ? n : n + fromIndex; } /** @@ -1263,9 +1275,12 @@ public: template< typename T > typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type lastIndexOf( T& literal ) const { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); return rtl_ustr_lastIndexOfAscii_WithLength( - pData->buffer, pData->length, literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1); + pData->buffer, pData->length, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal), + libreoffice_internal::ConstCharArrayDetector<T>::length); } /** diff --git a/include/rtl/ustring.hxx b/include/rtl/ustring.hxx index 1e9bfa8fbd5e..387a6e8099eb 100644 --- a/include/rtl/ustring.hxx +++ b/include/rtl/ustring.hxx @@ -73,17 +73,6 @@ struct SAL_WARN_UNUSED OUStringLiteral const char* data; }; -/** A simple wrapper around an ASCII character literal, for use in certain - OUString functions designed for efficient processing of string literals. - - @since LibreOffice 5.0 -*/ -template<char C> struct SAL_WARN_UNUSED OUStringLiteral1 { - static_assert( - static_cast<unsigned char>(C) < 0x80, - "non-ASCII character in OUStringLiteral1"); -}; - /// @endcond #endif @@ -226,12 +215,18 @@ public: template< typename T > OUString( T& literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() ) { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); pData = 0; - if( libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 == 0 ) // empty string - rtl_uString_new( &pData ); - else - rtl_uString_newFromLiteral( &pData, literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, 0 ); + if (libreoffice_internal::ConstCharArrayDetector<T>::length == 0) { + rtl_uString_new(&pData); + } else { + rtl_uString_newFromLiteral( + &pData, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer( + literal), + libreoffice_internal::ConstCharArrayDetector<T>::length, 0); + } #ifdef RTL_STRING_UNITTEST rtl_string_unittest_const_literal = true; #endif @@ -404,27 +399,19 @@ public: template< typename T > typename libreoffice_internal::ConstCharArrayDetector< T, OUString& >::Type operator=( T& literal ) { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - if( libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 == 0 ) // empty string - rtl_uString_new( &pData ); - else - rtl_uString_newFromLiteral( &pData, literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, 0 ); - return *this; - } - -#if defined LIBO_INTERNAL_ONLY // "RTL_FAST_STRING" - /// @cond INTERNAL - /** Assign a new string from a single ASCII character literal. - - @since LibreOffice 5.0 - */ - template<char C> OUString & operator =(OUStringLiteral1<C>) { - sal_Unicode const c = C; - rtl_uString_newFromStr_WithLength(&pData, &c, 1); + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + if (libreoffice_internal::ConstCharArrayDetector<T>::length == 0) { + rtl_uString_new(&pData); + } else { + rtl_uString_newFromLiteral( + &pData, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer( + literal), + libreoffice_internal::ConstCharArrayDetector<T>::length, 0); + } return *this; } - /// @endcond -#endif /** Append a string to this string. @@ -578,9 +565,12 @@ public: template< typename T > typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type reverseCompareTo( T& literal ) const { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - return rtl_ustr_asciil_reverseCompare_WithLength( pData->buffer, pData->length, - literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ); + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + return rtl_ustr_asciil_reverseCompare_WithLength( + pData->buffer, pData->length, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal), + libreoffice_internal::ConstCharArrayDetector<T>::length); } /** @@ -658,11 +648,16 @@ public: template< typename T > typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type equalsIgnoreAsciiCase( T& literal ) const { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - if ( pData->length != libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ) - return false; - - return rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( pData->buffer, pData->length, literal ) == 0; + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + return + (pData->length + == libreoffice_internal::ConstCharArrayDetector<T>::length) + && (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( + pData->buffer, pData->length, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer( + literal)) + == 0); } /** @@ -694,9 +689,15 @@ public: template< typename T > typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type match( T& literal, sal_Int32 fromIndex = 0 ) const { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - return rtl_ustr_ascii_shortenedCompare_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, - literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ) == 0; + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + return + rtl_ustr_ascii_shortenedCompare_WithLength( + pData->buffer+fromIndex, pData->length-fromIndex, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer( + literal), + libreoffice_internal::ConstCharArrayDetector<T>::length) + == 0; } /** @@ -732,9 +733,15 @@ public: template< typename T > typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type matchIgnoreAsciiCase( T& literal, sal_Int32 fromIndex = 0 ) const { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - return rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, - literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ) == 0; + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + return + rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength( + pData->buffer+fromIndex, pData->length-fromIndex, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer( + literal), + libreoffice_internal::ConstCharArrayDetector<T>::length) + == 0; } /** @@ -1039,12 +1046,19 @@ public: typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type startsWith( T & literal, OUString * rest = 0) const { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - bool b = libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 <= pData->length - && rtl_ustr_asciil_reverseEquals_WithLength( pData->buffer, literal, - libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1); + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + bool b + = (libreoffice_internal::ConstCharArrayDetector<T>::length + <= sal_uInt32(pData->length)) + && rtl_ustr_asciil_reverseEquals_WithLength( + pData->buffer, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer( + literal), + libreoffice_internal::ConstCharArrayDetector<T>::length); if (b && rest != 0) { - *rest = copy(libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1); + *rest = copy( + libreoffice_internal::ConstCharArrayDetector<T>::length); } return b; } @@ -1088,14 +1102,19 @@ public: typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type startsWithIgnoreAsciiCase(T & literal, OUString * rest = 0) const { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - bool b = (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths( - pData->buffer, - libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, literal, - libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1) - == 0); + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + bool b + = (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths( + pData->buffer, + libreoffice_internal::ConstCharArrayDetector<T>::length, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer( + literal), + libreoffice_internal::ConstCharArrayDetector<T>::length) + == 0); if (b && rest != 0) { - *rest = copy(libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1); + *rest = copy( + libreoffice_internal::ConstCharArrayDetector<T>::length); } return b; } @@ -1132,16 +1151,22 @@ public: typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type endsWith(T & literal, OUString * rest = 0) const { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - bool b = libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 <= pData->length + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + bool b + = (libreoffice_internal::ConstCharArrayDetector<T>::length + <= sal_uInt32(pData->length)) && rtl_ustr_asciil_reverseEquals_WithLength( - pData->buffer + pData->length - ( libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ), literal, - libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1); + (pData->buffer + pData->length + - libreoffice_internal::ConstCharArrayDetector<T>::length), + libreoffice_internal::ConstCharArrayDetector<T>::toPointer( + literal), + libreoffice_internal::ConstCharArrayDetector<T>::length); if (b && rest != 0) { *rest = copy( 0, (getLength() - - (libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1))); + - libreoffice_internal::ConstCharArrayDetector<T>::length)); } return b; } @@ -1205,18 +1230,24 @@ public: typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type endsWithIgnoreAsciiCase(T & literal, OUString * rest = 0) const { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - bool b = libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 <= pData->length + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + bool b + = (libreoffice_internal::ConstCharArrayDetector<T>::length + <= sal_uInt32(pData->length)) && (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths( - pData->buffer + pData->length - ( libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ), - libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, literal, - libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1) + (pData->buffer + pData->length + - libreoffice_internal::ConstCharArrayDetector<T>::length), + libreoffice_internal::ConstCharArrayDetector<T>::length, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer( + literal), + libreoffice_internal::ConstCharArrayDetector<T>::length) == 0); if (b && rest != 0) { *rest = copy( 0, (getLength() - - (libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1))); + - libreoffice_internal::ConstCharArrayDetector<T>::length)); } return b; } @@ -1274,8 +1305,11 @@ public: template< typename T > friend inline typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator==( const OUString& string, T& literal ) { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - return string.equalsAsciiL( literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ); + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + return string.equalsAsciiL( + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal), + libreoffice_internal::ConstCharArrayDetector<T>::length); } /** * Compare string to an ASCII string literal. @@ -1287,8 +1321,11 @@ public: template< typename T > friend inline typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator==( T& literal, const OUString& string ) { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - return string.equalsAsciiL( literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ); + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + return string.equalsAsciiL( + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal), + libreoffice_internal::ConstCharArrayDetector<T>::length); } /** * Compare string to an ASCII string literal. @@ -1300,8 +1337,11 @@ public: template< typename T > friend inline typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator!=( const OUString& string, T& literal ) { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - return !string.equalsAsciiL( literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ); + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + return !string.equalsAsciiL( + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal), + libreoffice_internal::ConstCharArrayDetector<T>::length); } /** * Compare string to an ASCII string literal. @@ -1313,8 +1353,11 @@ public: template< typename T > friend inline typename libreoffice_internal::ConstCharArrayDetector< T, bool >::Type operator!=( T& literal, const OUString& string ) { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - return !string.equalsAsciiL( literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ); + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + return !string.equalsAsciiL( + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal), + libreoffice_internal::ConstCharArrayDetector<T>::length); } #if defined LIBO_INTERNAL_ONLY @@ -1492,11 +1535,13 @@ public: template< typename T > typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - sal_Int32 ret = rtl_ustr_indexOfAscii_WithLength( - pData->buffer + fromIndex, pData->length - fromIndex, literal, - libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1); - return ret < 0 ? ret : ret + fromIndex; + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + sal_Int32 n = rtl_ustr_indexOfAscii_WithLength( + pData->buffer + fromIndex, pData->length - fromIndex, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal), + libreoffice_internal::ConstCharArrayDetector<T>::length); + return n < 0 ? n : n + fromIndex; } /** @@ -1590,9 +1635,12 @@ public: template< typename T > typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type lastIndexOf( T& literal ) const { - assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); return rtl_ustr_lastIndexOfAscii_WithLength( - pData->buffer, pData->length, literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1); + pData->buffer, pData->length, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal), + libreoffice_internal::ConstCharArrayDetector<T>::length); } /** @@ -1768,11 +1816,14 @@ public: SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T, OUString >::Type replaceFirst( T& from, OUString const & to, sal_Int32 * index = 0) const { + assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(from)); rtl_uString * s = 0; sal_Int32 i = 0; - assert( strlen( from ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); rtl_uString_newReplaceFirstAsciiL( - &s, pData, from, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, to.pData, index == 0 ? &i : index); + &s, pData, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(from), + libreoffice_internal::ConstCharArrayDetector<T>::length, to.pData, + index == 0 ? &i : index); return OUString(s, SAL_NO_ACQUIRE); } @@ -1798,11 +1849,14 @@ public: SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T, OUString >::Type replaceFirst( OUString const & from, T& to, sal_Int32 * index = 0) const { + assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(to)); rtl_uString * s = 0; sal_Int32 i = 0; - assert( strlen( to ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); rtl_uString_newReplaceFirstToAsciiL( - &s, pData, from.pData, to, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, index == 0 ? &i : index); + &s, pData, from.pData, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(to), + libreoffice_internal::ConstCharArrayDetector<T>::length, + index == 0 ? &i : index); return OUString(s, SAL_NO_ACQUIRE); } @@ -1828,13 +1882,17 @@ public: SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T1, typename libreoffice_internal::ConstCharArrayDetector< T2, OUString >::Type >::Type replaceFirst( T1& from, T2& to, sal_Int32 * index = 0) const { + assert(libreoffice_internal::ConstCharArrayDetector<T1>::isValid(from)); + assert(libreoffice_internal::ConstCharArrayDetector<T2>::isValid(to)); rtl_uString * s = 0; sal_Int32 i = 0; - assert( strlen( from ) == libreoffice_internal::ConstCharArrayDetector< T1 >::size - 1 ); - assert( strlen( to ) == libreoffice_internal::ConstCharArrayDetector< T2 >::size - 1 ); rtl_uString_newReplaceFirstAsciiLAsciiL( - &s, pData, from, libreoffice_internal::ConstCharArrayDetector< T1, void >::size - 1, to, - libreoffice_internal::ConstCharArrayDetector< T2, void >::size - 1, index == 0 ? &i : index); + &s, pData, + libreoffice_internal::ConstCharArrayDetector<T1>::toPointer(from), + libreoffice_internal::ConstCharArrayDetector<T1>::length, + libreoffice_internal::ConstCharArrayDetector<T2>::toPointer(to), + libreoffice_internal::ConstCharArrayDetector<T2>::length, + index == 0 ? &i : index); return OUString(s, SAL_NO_ACQUIRE); } @@ -1877,9 +1935,12 @@ public: template< typename T > SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T, OUString >::Type replaceAll( T& from, OUString const & to) const { + assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(from)); rtl_uString * s = 0; - assert( strlen( from ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - rtl_uString_newReplaceAllAsciiL(&s, pData, from, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, to.pData); + rtl_uString_newReplaceAllAsciiL( + &s, pData, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(from), + libreoffice_internal::ConstCharArrayDetector<T>::length, to.pData); return OUString(s, SAL_NO_ACQUIRE); } @@ -1899,9 +1960,12 @@ public: template< typename T > SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T, OUString >::Type replaceAll( OUString const & from, T& to) const { + assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(to)); rtl_uString * s = 0; - assert( strlen( to ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); - rtl_uString_newReplaceAllToAsciiL(&s, pData, from.pData, to, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1); + rtl_uString_newReplaceAllToAsciiL( + &s, pData, from.pData, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(to), + libreoffice_internal::ConstCharArrayDetector<T>::length); return OUString(s, SAL_NO_ACQUIRE); } @@ -1922,12 +1986,15 @@ public: SAL_WARN_UNUSED_RESULT typename libreoffice_internal::ConstCharArrayDetector< T1, typename libreoffice_internal::ConstCharArrayDetector< T2, OUString >::Type >::Type replaceAll( T1& from, T2& to ) const { + assert(libreoffice_internal::ConstCharArrayDetector<T1>::isValid(from)); + assert(libreoffice_internal::ConstCharArrayDetector<T2>::isValid(to)); rtl_uString * s = 0; - assert( strlen( from ) == libreoffice_internal::ConstCharArrayDetector< T1 >::size - 1 ); - assert( strlen( to ) == libreoffice_internal::ConstCharArrayDetector< T2 >::size - 1 ); rtl_uString_newReplaceAllAsciiLAsciiL( - &s, pData, from, libreoffice_internal::ConstCharArrayDetector< T1, void >::size - 1, - to, libreoffice_internal::ConstCharArrayDetector< T2, void >::size - 1); + &s, pData, + libreoffice_internal::ConstCharArrayDetector<T1>::toPointer(from), + libreoffice_internal::ConstCharArrayDetector<T1>::length, + libreoffice_internal::ConstCharArrayDetector<T2>::toPointer(to), + libreoffice_internal::ConstCharArrayDetector<T2>::length); return OUString(s, SAL_NO_ACQUIRE); } @@ -2564,26 +2631,6 @@ public: #if defined LIBO_INTERNAL_ONLY // "RTL_FAST_STRING" /// @cond INTERNAL -/** Compare a string and an ASCII character literal for equality. - - @since LibreOffice 5.0 -*/ -template<char C> bool operator ==(OUString const & string, OUStringLiteral1<C>) -{ - char const c = C; - return string.equalsAsciiL(&c, 1); -} - -/** Compare a string and an ASCII character literal for inequality. - - @since LibreOffice 5.0 -*/ -template<char C> bool operator !=( - OUString const & string, OUStringLiteral1<C> literal) -{ - return !(string == literal); -} - /** @internal */ @@ -2611,18 +2658,6 @@ struct ToStringHelper< OUStringLiteral > /** @internal */ -template<char C> struct ToStringHelper<OUStringLiteral1<C>> -{ - static int length(OUStringLiteral1<C>) { return 1; } - static sal_Unicode * addData(sal_Unicode * buffer, OUStringLiteral1<C>) - { *buffer++ = C; return buffer; } - static const bool allowOStringConcat = false; - static const bool allowOUStringConcat = true; -}; - -/** - @internal -*/ template< typename charT, typename traits, typename T1, typename T2 > inline std::basic_ostream<charT, traits> & operator <<( std::basic_ostream<charT, traits> & stream, const OUStringConcat< T1, T2 >& concat) |