sal/inc/rtl/string.hxx | 35 ++++++++++++++ sal/inc/rtl/ustrbuf.hxx | 18 +++++++ sal/qa/rtl/strings/test_ostring_stringliterals.cxx | 50 ++++++++++++++++++++ sal/qa/rtl/strings/test_oustring_stringliterals.cxx | 22 +++++++- 4 files changed, 121 insertions(+), 4 deletions(-)
New commits: commit 09517a98fe44eafcd19a8eecb0c14384799e2684 Author: LuboÅ¡ LuÅák <l.lu...@suse.cz> Date: Mon Mar 26 18:51:59 2012 +0200 string literal overload for OString::operator=() diff --git a/sal/inc/rtl/string.hxx b/sal/inc/rtl/string.hxx index 79b9a15..d899067 100644 --- a/sal/inc/rtl/string.hxx +++ b/sal/inc/rtl/string.hxx @@ -57,6 +57,12 @@ namespace rtl #ifdef RTL_STRING_UNITTEST #undef rtl +// helper macros to make functions appear more readable +#define RTL_STRING_CONST_FUNCTION rtl_string_unittest_const_literal_function = true; +#define RTL_STRING_NON_CONST_FUNCTION rtl_string_unittest_non_const_literal_function = true; +#else +#define RTL_STRING_CONST_FUNCTION +#define RTL_STRING_NON_CONST_FUNCTION #endif /* ======================================================================= */ @@ -209,6 +215,7 @@ public: If there are any embedded \0's in the string literal, the result is undefined. Use the overload that explicitly accepts length. + @since LibreOffice 3.6 @param literal a string literal */ @@ -225,6 +232,7 @@ public: /** @overload New string from a non-const char array. + @since LibreOffice 3.6 @param value non-const char array */ @@ -299,6 +307,33 @@ public: } /** + @overload + This function accepts an ASCII string literal as its argument. + @since LibreOffice 3.6 + */ + template< int N > + OString& operator=( const char (&literal)[ N ] ) SAL_THROW(()) + { + rtl_string_newFromLiteral( &pData, literal, N - 1 ); + RTL_STRING_CONST_FUNCTION + return *this; + } + /** + @overload + This function accepts a non-const char array as its argument. + @since LibreOffice 3.6 + + @param value non-const char array + */ + template< int N > + OString& operator=( char (&value)[ N ] ) SAL_THROW(()) + { + rtl_string_newFromStr( &pData, value ); + RTL_STRING_NON_CONST_FUNCTION + return *this; + } + + /** Append a string to this string. @param str a OString. diff --git a/sal/qa/rtl/strings/test_ostring_stringliterals.cxx b/sal/qa/rtl/strings/test_ostring_stringliterals.cxx index 8398ae9..2176a50 100644 --- a/sal/qa/rtl/strings/test_ostring_stringliterals.cxx +++ b/sal/qa/rtl/strings/test_ostring_stringliterals.cxx @@ -29,6 +29,8 @@ // activate the extra needed ctor #define RTL_STRING_UNITTEST bool rtl_string_unittest_const_literal; +bool rtl_string_unittest_const_literal_function; +bool rtl_string_unittest_non_const_literal_function; #include "sal/config.h" #include "sal/precppunit.hxx" @@ -38,17 +40,33 @@ bool rtl_string_unittest_const_literal; #include "rtl/string.h" #include "rtl/string.hxx" +namespace rtlunittest { + +template< typename charT, typename traits > std::basic_ostream<charT, traits> & +operator <<( + std::basic_ostream<charT, traits> & stream, rtl::OString const & string) +{ + return stream << string.getStr(); + // best effort; potentially loses data due to embedded null characters +} + +} + namespace test { namespace ostring { class StringLiterals: public CppUnit::TestFixture { private: void checkCtors(); + void checkUsage(); + void checkNonConstUsage(); void testcall( const char str[] ); CPPUNIT_TEST_SUITE(StringLiterals); CPPUNIT_TEST(checkCtors); +CPPUNIT_TEST(checkUsage); +CPPUNIT_TEST(checkNonConstUsage); CPPUNIT_TEST_SUITE_END(); }; @@ -116,6 +134,38 @@ void test::ostring::StringLiterals::testcall( const char str[] ) #endif } +void test::ostring::StringLiterals::checkUsage() +{ +// simply check that all string literal based calls work as expected +// also check that they really use string literal overload and do not convert to OString + rtl::OString foo( "foo" ); + + rtl_string_unittest_const_literal = false; // start checking for OString conversions + rtl_string_unittest_non_const_literal_function = false; // and check for non-const variants + CPPUNIT_ASSERT_EQUAL( foo, rtl::OString() = "foo" ); + // if this is not true, some of the calls above converted to OString + CPPUNIT_ASSERT( rtl_string_unittest_const_literal == false ); + // if this is not true, some of the calls above used non-const variants + CPPUNIT_ASSERT( rtl_string_unittest_non_const_literal_function == false ); +} + +void test::ostring::StringLiterals::checkNonConstUsage() +{ +// check that (non-const) char[] overloads work and do not use const char[] overloads + rtl::OString foo( "foo" ); + char foo_c[] = "foo"; + + rtl_string_unittest_const_literal = false; // start checking for OString conversions + rtl_string_unittest_const_literal_function = false; // and check for const variants + sleep(10); + rtl::OString() = (const char*)"foo"; + CPPUNIT_ASSERT_EQUAL( foo, rtl::OString() = foo_c ); + // if this is not true, some of the calls above converted to OString + CPPUNIT_ASSERT( rtl_string_unittest_const_literal == false ); + // if this is not true, some of the calls above used const variants + CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function == false ); +} + #undef CONST_CTOR_USED }} // namespace diff --git a/sal/qa/rtl/strings/test_oustring_stringliterals.cxx b/sal/qa/rtl/strings/test_oustring_stringliterals.cxx index c90fe5e..fb0e21e 100644 --- a/sal/qa/rtl/strings/test_oustring_stringliterals.cxx +++ b/sal/qa/rtl/strings/test_oustring_stringliterals.cxx @@ -29,6 +29,8 @@ // activate the extra needed ctor #define RTL_STRING_UNITTEST extern bool rtl_string_unittest_const_literal; +extern bool rtl_string_unittest_const_literal_function; +extern bool rtl_string_unittest_non_const_literal_function; #include "sal/config.h" #include "sal/precppunit.hxx" commit 62983d5ae66d510c3a6a3167eb41207a9d2777ec Author: LuboÅ¡ LuÅák <l.lu...@suse.cz> Date: Mon Mar 26 18:06:51 2012 +0200 CPPUNIT_ASSERT( == ) -> CPPUNIT_ASSERT_EQUAL() diff --git a/sal/qa/rtl/strings/test_oustring_stringliterals.cxx b/sal/qa/rtl/strings/test_oustring_stringliterals.cxx index 9c8c934..c90fe5e 100644 --- a/sal/qa/rtl/strings/test_oustring_stringliterals.cxx +++ b/sal/qa/rtl/strings/test_oustring_stringliterals.cxx @@ -91,10 +91,10 @@ void test::oustring::StringLiterals::checkCtors() // Check that contents are correct and equal to the case when RTL_CONSTASCII_USTRINGPARAM is used. // Also check that embedded \0 is included. - CPPUNIT_ASSERT( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "" )) == rtl::OUString( "" )); - CPPUNIT_ASSERT( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "\0" )) == rtl::OUString( "\0" )); - CPPUNIT_ASSERT( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ab" )) == rtl::OUString( "ab" )); - CPPUNIT_ASSERT( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "a\0b" )) == rtl::OUString( "a\0b" )); + CPPUNIT_ASSERT_EQUAL( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "" )), rtl::OUString( "" )); + CPPUNIT_ASSERT_EQUAL( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "\0" )), rtl::OUString( "\0" )); + CPPUNIT_ASSERT_EQUAL( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ab" )), rtl::OUString( "ab" )); + CPPUNIT_ASSERT_EQUAL( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "a\0b" )), rtl::OUString( "a\0b" )); } void test::oustring::StringLiterals::testcall( const char str[] ) @@ -167,9 +167,9 @@ void test::oustring::StringLiterals::checkBuffer() { rtl::OUStringBuffer buf; buf.append( "foo" ); - CPPUNIT_ASSERT( buf.toString() == "foo" ); + CPPUNIT_ASSERT_EQUAL( buf.toString(), rtl::OUString( "foo" )); buf.append( "bar" ); - CPPUNIT_ASSERT( buf.toString() == "foobar" ); + CPPUNIT_ASSERT_EQUAL( buf.toString(), rtl::OUString( "foobar" )); } }} // namespace commit 92fec304fb2820b5719080c0889c7432f819af17 Author: LuboÅ¡ LuÅák <l.lu...@suse.cz> Date: Mon Mar 26 18:02:45 2012 +0200 string literal overloads for OUStringBuffer diff --git a/sal/inc/rtl/ustrbuf.hxx b/sal/inc/rtl/ustrbuf.hxx index af008bf..adb1761 100644 --- a/sal/inc/rtl/ustrbuf.hxx +++ b/sal/inc/rtl/ustrbuf.hxx @@ -405,6 +405,24 @@ public: return *this; } + /** + @overload + This function accepts an ASCII string literal as its argument. + @since LibreOffice 3.6 + */ + template< int N > + OUStringBuffer& append( const char (&literal)[ N ] ) + { + rtl_uStringbuffer_insert_ascii( &pData, &nCapacity, getLength(), literal, N - 1 ); + return *this; + } + + /** + It is an error to call this overload. Strings cannot directly use non-const char[]. + @internal + */ + template< int N > + OUStringBuffer& append( char (&literal)[ N ] ); /** Appends the string representation of the <code>sal_Bool</code> diff --git a/sal/qa/rtl/strings/test_oustring_stringliterals.cxx b/sal/qa/rtl/strings/test_oustring_stringliterals.cxx index 59e1300..9c8c934 100644 --- a/sal/qa/rtl/strings/test_oustring_stringliterals.cxx +++ b/sal/qa/rtl/strings/test_oustring_stringliterals.cxx @@ -37,6 +37,7 @@ extern bool rtl_string_unittest_const_literal; #include <cppunit/extensions/HelperMacros.h> #include "rtl/string.h" #include "rtl/ustring.hxx" +#include "rtl/ustrbuf.hxx" #include "rtl/oustringostreaminserter.hxx" namespace test { namespace oustring { @@ -48,6 +49,7 @@ private: void checkUsage(); void checkExtraIntArgument(); void checkNonconstChar(); + void checkBuffer(); void testcall( const char str[] ); // invalid conversions will trigger templated OUString ctor that creates an empty string @@ -59,6 +61,7 @@ CPPUNIT_TEST(checkCtors); CPPUNIT_TEST(checkUsage); CPPUNIT_TEST(checkExtraIntArgument); CPPUNIT_TEST(checkNonconstChar); +CPPUNIT_TEST(checkBuffer); CPPUNIT_TEST_SUITE_END(); }; @@ -160,6 +163,15 @@ void test::oustring::StringLiterals::checkNonconstChar() CPPUNIT_ASSERT( rtl::OUString( "foobar" ) == rtl::OUString( "footest" ).replaceAll( consttest, constbar )); } +void test::oustring::StringLiterals::checkBuffer() +{ + rtl::OUStringBuffer buf; + buf.append( "foo" ); + CPPUNIT_ASSERT( buf.toString() == "foo" ); + buf.append( "bar" ); + CPPUNIT_ASSERT( buf.toString() == "foobar" ); +} + }} // namespace CPPUNIT_TEST_SUITE_REGISTRATION(test::oustring::StringLiterals);
_______________________________________________ Libreoffice-commits mailing list Libreoffice-commits@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/libreoffice-commits