diff options
author | Luboš Luňák <l.lunak@suse.cz> | 2012-03-28 22:58:11 +0200 |
---|---|---|
committer | Luboš Luňák <l.lunak@suse.cz> | 2012-03-28 23:00:55 +0200 |
commit | ef87e804ec80451ff1517482c1b70e7dccb961ce (patch) | |
tree | 0f677f23738d6b94a1daa502a3a95e474dca9d60 /sal | |
parent | b741f7fb1ea7b62c9cf2988a64e07cbbb8db904a (diff) |
string literal overloads for OStringBuffer
Diffstat (limited to 'sal')
-rw-r--r-- | sal/inc/rtl/strbuf.hxx | 44 | ||||
-rw-r--r-- | sal/qa/rtl/strings/test_ostring_stringliterals.cxx | 31 |
2 files changed, 73 insertions, 2 deletions
diff --git a/sal/inc/rtl/strbuf.hxx b/sal/inc/rtl/strbuf.hxx index 62250f3faaa0..54428d340a10 100644 --- a/sal/inc/rtl/strbuf.hxx +++ b/sal/inc/rtl/strbuf.hxx @@ -373,11 +373,31 @@ public: @param str the characters to be appended. @return this string buffer. */ - OStringBuffer & append( const sal_Char * str ) + template< typename T > + typename internal::CharPtrDetector< T, OStringBuffer& >::Type append( const T& str ) { return append( str, rtl_str_getLength( str ) ); } + template< typename T > + typename internal::NonConstCharArrayDetector< T, OStringBuffer& >::Type append( T& str ) + { + return append( str, rtl_str_getLength( str ) ); + } + + /** + @overload + This function accepts an ASCII string literal as its argument. + @since LibreOffice 3.6 + */ + template< typename T > + typename internal::ConstCharArrayDetector< T, OStringBuffer& >::Type append( T& literal ) + { + RTL_STRING_CONST_FUNCTION + rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), literal, internal::ConstCharArrayDetector< T, void >::size - 1 ); + return *this; + } + /** Appends the string representation of the <code>char</code> array argument to this string buffer. @@ -535,11 +555,31 @@ public: @param str a character array. @return this string buffer. */ - OStringBuffer & insert( sal_Int32 offset, const sal_Char * str ) + template< typename T > + typename internal::CharPtrDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, const T& str ) { return insert( offset, str, rtl_str_getLength( str ) ); } + template< typename T > + typename internal::NonConstCharArrayDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, T& str ) + { + return insert( offset, str, rtl_str_getLength( str ) ); + } + + /** + @overload + This function accepts an ASCII string literal as its argument. + @since LibreOffice 3.6 + */ + template< typename T > + typename internal::ConstCharArrayDetector< T, OStringBuffer& >::Type insert( sal_Int32 offset, T& literal ) + { + RTL_STRING_CONST_FUNCTION + rtl_stringbuffer_insert( &pData, &nCapacity, offset, literal, internal::ConstCharArrayDetector< T, void >::size - 1 ); + return *this; + } + /** Inserts the string representation of the <code>char</code> array argument into this string buffer. diff --git a/sal/qa/rtl/strings/test_ostring_stringliterals.cxx b/sal/qa/rtl/strings/test_ostring_stringliterals.cxx index 462385ea23e5..3f2ed8465815 100644 --- a/sal/qa/rtl/strings/test_ostring_stringliterals.cxx +++ b/sal/qa/rtl/strings/test_ostring_stringliterals.cxx @@ -40,6 +40,7 @@ bool rtl_string_unittest_non_const_literal_function; #include <cppunit/extensions/HelperMacros.h> #include "rtl/string.h" #include "rtl/string.hxx" +#include "rtl/strbuf.hxx" namespace rtlunittest { @@ -61,6 +62,7 @@ private: void checkCtors(); void checkUsage(); void checkNonConstUsage(); + void checkBuffer(); void testcall( const char str[] ); @@ -68,6 +70,7 @@ CPPUNIT_TEST_SUITE(StringLiterals); CPPUNIT_TEST(checkCtors); CPPUNIT_TEST(checkUsage); CPPUNIT_TEST(checkNonConstUsage); +CPPUNIT_TEST(checkBuffer); CPPUNIT_TEST_SUITE_END(); }; @@ -247,6 +250,34 @@ void test::ostring::StringLiterals::checkNonConstUsage() // CPPUNIT_ASSERT( foobarfoo.lastIndexOf( (const char*)foo_c ) == 6 ); // CPPUNIT_ASSERT( foobarfoo.lastIndexOf( foo_c ) == 6 ); // if this is not true, some of the calls above used const variants + CPPUNIT_ASSERT( rtl_string_unittest_const_literal == false ); + CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function == false ); +} + +void test::ostring::StringLiterals::checkBuffer() +{ + rtl::OStringBuffer buf; + rtl_string_unittest_const_literal_function = false; + buf.append( "foo" ); + CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function == true ); + CPPUNIT_ASSERT_EQUAL( buf.toString(), rtl::OString( "foo" )); + rtl_string_unittest_const_literal_function = false; + buf.append( "bar" ); + CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function == true ); + CPPUNIT_ASSERT_EQUAL( buf.toString(), rtl::OString( "foobar" )); + rtl_string_unittest_const_literal_function = false; + buf.insert( 3, "baz" ); + CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function == true ); + CPPUNIT_ASSERT_EQUAL( buf.toString(), rtl::OString( "foobazbar" )); + + rtl::OString foobazbard( "foobazbard" ); + rtl::OString foodbazbard( "foodbazbard" ); + rtl_string_unittest_const_literal = false; // start checking for OString conversions + rtl_string_unittest_const_literal_function = false; // and check for const variants + char d[] = "d"; + CPPUNIT_ASSERT_EQUAL( buf.append( d ).toString(), foobazbard ); + CPPUNIT_ASSERT_EQUAL( buf.insert( 3, d ).toString(), foodbazbard ); + CPPUNIT_ASSERT( rtl_string_unittest_const_literal == false ); CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function == false ); } |