Kaydet (Commit) 8007417c authored tarafından Stephan Bergmann's avatar Stephan Bergmann

Revert "Revert "Generalize OUStringLiteral1""

This reverts commit 5cba714b, now including a
workaround for <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53658> "internal
compiler error -- segmentation fault."

Change-Id: I31f6d9ddcb0b884134703df2b9dc1800ba0a84be
üst 707ef317
...@@ -170,10 +170,14 @@ public: ...@@ -170,10 +170,14 @@ public:
template< typename T > template< typename T >
OStringBuffer( T& literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy()) OStringBuffer( T& literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy())
: pData(NULL) : pData(NULL)
, nCapacity( libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 + 16 ) , nCapacity( libreoffice_internal::ConstCharArrayDetector<T>::length + 16 )
{ {
assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); assert(
rtl_string_newFromLiteral( &pData, literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, 16 ); 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 #ifdef RTL_STRING_UNITTEST
rtl_string_unittest_const_literal = true; rtl_string_unittest_const_literal = true;
#endif #endif
...@@ -453,8 +457,12 @@ public: ...@@ -453,8 +457,12 @@ public:
typename libreoffice_internal::ConstCharArrayDetector< T, OStringBuffer& >::Type append( T& literal ) typename libreoffice_internal::ConstCharArrayDetector< T, OStringBuffer& >::Type append( T& literal )
{ {
RTL_STRING_CONST_FUNCTION RTL_STRING_CONST_FUNCTION
assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); assert(
rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ); 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; return *this;
} }
...@@ -708,8 +716,12 @@ public: ...@@ -708,8 +716,12 @@ public:
typename libreoffice_internal::ConstCharArrayDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, T& literal ) typename libreoffice_internal::ConstCharArrayDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, T& literal )
{ {
RTL_STRING_CONST_FUNCTION RTL_STRING_CONST_FUNCTION
assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); assert(
rtl_stringbuffer_insert( &pData, &nCapacity, offset, literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ); 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; return *this;
} }
......
This diff is collapsed.
...@@ -141,6 +141,19 @@ struct ToStringHelper< const char[ N ] > ...@@ -141,6 +141,19 @@ struct ToStringHelper< const char[ N ] >
static const bool allowOUStringConcat = true; 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 @internal
......
...@@ -11,6 +11,10 @@ ...@@ -11,6 +11,10 @@
#define INCLUDED_RTL_STRINGUTILS_HXX #define INCLUDED_RTL_STRINGUTILS_HXX
#include <sal/config.h> #include <sal/config.h>
#include <cstddef>
#include <cstring>
#include <sal/types.h> #include <sal/types.h>
// The unittest uses slightly different code to help check that the proper // The unittest uses slightly different code to help check that the proper
...@@ -29,6 +33,60 @@ namespace rtl ...@@ -29,6 +33,60 @@ namespace rtl
#undef rtl #undef rtl
#endif #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 namespace libreoffice_internal
{ {
/* /*
...@@ -102,13 +160,34 @@ struct ConstCharArrayDetector ...@@ -102,13 +160,34 @@ struct ConstCharArrayDetector
{ {
static const bool ok = false; static const bool ok = false;
}; };
template< int N, typename T > template< std::size_t N, typename T >
struct ConstCharArrayDetector< const char[ N ], T > struct ConstCharArrayDetector< const char[ N ], T >
{ {
typedef T Type; typedef T Type;
static const int size = N; static const std::size_t length = N - 1;
static const bool ok = true; 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<
#if defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ <= 8 \
&& !defined __clang__
OUStringLiteral1_<C> const,
#else
OUStringLiteral1<C>,
#endif
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> const & literal)
{ return &literal.c; }
};
#endif
// this one is used to rule out only const char[N] // this one is used to rule out only const char[N]
template< typename T > template< typename T >
...@@ -137,6 +216,10 @@ template< int N > ...@@ -137,6 +216,10 @@ template< int N >
struct ExceptCharArrayDetector< const char[ 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 > template< typename T1, typename T2 = void >
struct SalUnicodePtrDetector struct SalUnicodePtrDetector
......
...@@ -132,10 +132,14 @@ public: ...@@ -132,10 +132,14 @@ public:
template< typename T > template< typename T >
OUStringBuffer( T& literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() ) OUStringBuffer( T& literal, typename libreoffice_internal::ConstCharArrayDetector< T, libreoffice_internal::Dummy >::Type = libreoffice_internal::Dummy() )
: pData(NULL) : pData(NULL)
, nCapacity( libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 + 16 ) , nCapacity( libreoffice_internal::ConstCharArrayDetector<T>::length + 16 )
{ {
assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); assert(
rtl_uString_newFromLiteral( &pData, literal, libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1, 16 ); 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 #ifdef RTL_STRING_UNITTEST
rtl_string_unittest_const_literal = true; rtl_string_unittest_const_literal = true;
#endif #endif
...@@ -470,9 +474,12 @@ public: ...@@ -470,9 +474,12 @@ public:
template< typename T > template< typename T >
typename libreoffice_internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type append( T& literal ) typename libreoffice_internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type append( T& literal )
{ {
assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); assert(
rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), literal, libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ); rtl_uStringbuffer_insert_ascii(
&pData, &nCapacity, getLength(),
libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
libreoffice_internal::ConstCharArrayDetector<T>::length);
return *this; return *this;
} }
...@@ -813,9 +820,12 @@ public: ...@@ -813,9 +820,12 @@ public:
template< typename T > template< typename T >
typename libreoffice_internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type insert( sal_Int32 offset, T& literal ) typename libreoffice_internal::ConstCharArrayDetector< T, OUStringBuffer& >::Type insert( sal_Int32 offset, T& literal )
{ {
assert( strlen( literal ) == libreoffice_internal::ConstCharArrayDetector< T >::size - 1 ); assert(
rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, offset, literal, libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1 ); rtl_uStringbuffer_insert_ascii(
&pData, &nCapacity, offset,
libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
libreoffice_internal::ConstCharArrayDetector<T>::length);
return *this; return *this;
} }
...@@ -1199,11 +1209,13 @@ public: ...@@ -1199,11 +1209,13 @@ public:
template< typename T > template< typename T >
typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type indexOf( T& literal, sal_Int32 fromIndex = 0 ) const 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 ); assert(
sal_Int32 ret = rtl_ustr_indexOfAscii_WithLength( libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
pData->buffer + fromIndex, pData->length - fromIndex, literal, sal_Int32 n = rtl_ustr_indexOfAscii_WithLength(
libreoffice_internal::ConstCharArrayDetector< T, void >::size - 1); pData->buffer + fromIndex, pData->length - fromIndex,
return ret < 0 ? ret : ret + fromIndex; libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
libreoffice_internal::ConstCharArrayDetector<T>::length);
return n < 0 ? n : n + fromIndex;
} }
/** /**
...@@ -1263,9 +1275,12 @@ public: ...@@ -1263,9 +1275,12 @@ public:
template< typename T > template< typename T >
typename libreoffice_internal::ConstCharArrayDetector< T, sal_Int32 >::Type lastIndexOf( T& literal ) const 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( 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);
} }
/** /**
......
This diff is collapsed.
...@@ -192,6 +192,20 @@ void test::oustring::StringLiterals::checkOUStringLiteral() ...@@ -192,6 +192,20 @@ void test::oustring::StringLiterals::checkOUStringLiteral()
void test::oustring::StringLiterals::checkOUStringLiteral1() void test::oustring::StringLiterals::checkOUStringLiteral1()
{ {
auto l1 = rtlunittest::OUStringLiteral1<'A'>();
CPPUNIT_ASSERT_EQUAL('A', l1.c);
char const c2 = 'A';
auto l2 = rtlunittest::OUStringLiteral1<c2>();
CPPUNIT_ASSERT_EQUAL('A', l2.c);
// char c3 = 'A'; auto l3 = rtlunittest::OUStringLiteral1<c3>();
auto l4 = rtlunittest::OUStringLiteral1<sal_Unicode('A')>();
CPPUNIT_ASSERT_EQUAL('A', l4.c);
// auto l5 = rtlunittest::OUStringLiteral1<sal_Unicode(0x100)>();
rtl::OUString s1; rtl::OUString s1;
s1 = rtlunittest::OUStringLiteral1<'A'>(); s1 = rtlunittest::OUStringLiteral1<'A'>();
CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s1.getLength()); CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s1.getLength());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment