rtl::OUStringLiteral to the rescue

...for cases where ? "a" : "bb" does not work, as well as to work around the
MSVC bug for cases like ? "a" : "b".

Change-Id: Id404716047aca5cc81440f291616d92365379b8f
This commit is contained in:
Stephan Bergmann
2014-12-17 16:37:41 +01:00
parent 0f5e917024
commit d0ec8c4901
3 changed files with 61 additions and 18 deletions

View File

@@ -55,6 +55,23 @@ namespace rtl
#if defined RTL_FAST_STRING
/// @cond INTERNAL
/**
A simple wrapper around string literal. It is usually not necessary to use, can
be mostly used to force OUString operator+ working with operands that otherwise would
not trigger it.
This class is not part of public API and is meant to be used only in LibreOffice code.
@since LibreOffice 4.0
*/
struct SAL_WARN_UNUSED OUStringLiteral
{
template< int N >
OUStringLiteral( const char (&str)[ N ] ) : size( N - 1 ), data( str ) { assert( strlen( str ) == N - 1 ); }
int size;
const char* data;
};
/** A simple wrapper around an ASCII character literal, for use in certain
OUString functions designed for efficient processing of string literals.
@@ -65,6 +82,7 @@ template<char C> struct SAL_WARN_UNUSED OUStringLiteral1 {
static_cast<unsigned char>(C) < 0x80,
"non-ASCII character in OUStringLiteral1");
};
/// @endcond
#endif
@@ -232,6 +250,29 @@ public:
}
#endif
#ifdef RTL_FAST_STRING
/// @cond INTERNAL
/**
New string from an 8-Bit string literal that is expected to contain only
characters in the ASCII set (i.e. first 128 characters).
This constructor is similar to the "direct template" one, but can be
useful in cases where the latter does not work, like in
OUString(flag ? "a" : "bb")
written as
OUString(flag ? OUStringLiteral("a") : OUStringLiteral("bb"))
@since LibreOffice 4.5
*/
OUString(OUStringLiteral literal): pData(0) {
rtl_uString_newFromLiteral(&pData, literal.data, literal.size, 0);
}
/// @endcond
#endif
/**
New string from an 8-Bit character buffer array.
@@ -2401,22 +2442,6 @@ template<char C> bool operator !=(
/* ======================================================================= */
#ifdef RTL_FAST_STRING
/**
A simple wrapper around string literal. It is usually not necessary to use, can
be mostly used to force OUString operator+ working with operands that otherwise would
not trigger it.
This class is not part of public API and is meant to be used only in LibreOffice code.
@since LibreOffice 4.0
*/
struct SAL_WARN_UNUSED OUStringLiteral
{
template< int N >
OUStringLiteral( const char (&str)[ N ] ) : size( N - 1 ), data( str ) { assert( strlen( str ) == N - 1 ); }
int size;
const char* data;
};
/**
@internal
*/

View File

@@ -31,6 +31,7 @@ private:
void checkExtraIntArgument();
void checkNonconstChar();
void checkBuffer();
void checkOUStringLiteral();
void checkOUStringLiteral1();
void testcall( const char str[] );
@@ -41,6 +42,7 @@ CPPUNIT_TEST(checkUsage);
CPPUNIT_TEST(checkExtraIntArgument);
CPPUNIT_TEST(checkNonconstChar);
CPPUNIT_TEST(checkBuffer);
CPPUNIT_TEST(checkOUStringLiteral);
CPPUNIT_TEST(checkOUStringLiteral1);
CPPUNIT_TEST_SUITE_END();
};
@@ -172,6 +174,22 @@ void test::oustring::StringLiterals::checkBuffer()
CPPUNIT_ASSERT( !VALID_CONVERSION( buf.append( rtl::OUStringBuffer( d ))));
}
namespace {
rtl::OUString conditional(bool flag) {
return flag
? rtlunittest::OUStringLiteral("a")
: rtlunittest::OUStringLiteral("bb");
}
}
void test::oustring::StringLiterals::checkOUStringLiteral()
{
CPPUNIT_ASSERT(conditional(true) == "a");
CPPUNIT_ASSERT(conditional(false) == "bb");
}
void test::oustring::StringLiterals::checkOUStringLiteral1()
{
rtl::OUString s1;

View File

@@ -5202,8 +5202,8 @@ int RTFDocumentImpl::popState()
break; // not for nested group
OUString str(m_aStates.top().pDestinationText->makeStringAndClear());
// dmapper expects this as a field, so let's fake something...
OUString const field = OUString::createFromAscii(
(DESTINATION_INDEXENTRY == aState.nDestinationState) ? "XE" : "TC");
OUString const field(
(DESTINATION_INDEXENTRY == aState.nDestinationState) ? OUStringLiteral("XE") : OUStringLiteral("TC"));
str = field + " \"" + str.replaceAll("\"", "\\\"") + "\"";
singleChar(0x13);
Mapper().utext(reinterpret_cast<sal_uInt8 const*>(str.getStr()), str.getLength());