diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2019-10-14 15:54:03 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2019-10-14 17:27:32 +0200 |
commit | 35e6ada72631379053fbf620e79ddaeafc9659e9 (patch) | |
tree | 0751a7df19a9cac6b4d0f63a99ad9f81a48f9af8 | |
parent | 6a9e20e16701014e432aeec7a4a6705a2d6013b9 (diff) |
Fix misuse of OStringLiteral
...(which got introduced with 9b5dad13b56bdde7c40970351af3da3a2c3c9350
"loplugin:stringadd look for unnecessary temporaries", and had reportedly
broken CppunitTest_sc_ucalc on tml's Windows build by hitting the
"strlen( str ) == N - 1" assert at include/rtl/string.hxx:1867), by introducing
rtl::OStringView (and rtl::OUStringView, for consistency).
Change-Id: I766b600274302ded66a6bffc91be189b20ed1ac3
Reviewed-on: https://gerrit.libreoffice.org/80778
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
-rw-r--r-- | include/rtl/stringconcat.hxx | 55 | ||||
-rw-r--r-- | opencl/source/openclwrapper.cxx | 2 | ||||
-rw-r--r-- | sal/qa/rtl/strings/test_ostring_concat.cxx | 3 | ||||
-rw-r--r-- | sal/qa/rtl/strings/test_oustring_concat.cxx | 3 |
4 files changed, 62 insertions, 1 deletions
diff --git a/include/rtl/stringconcat.hxx b/include/rtl/stringconcat.hxx index 3fd8b6720851..c075a29d9c80 100644 --- a/include/rtl/stringconcat.hxx +++ b/include/rtl/stringconcat.hxx @@ -480,6 +480,61 @@ struct ToStringHelper< OUStringNumber< T > > static const bool allowOUStringConcat = true; }; +// Abstractions over null-terminated char and sal_Unicode strings that sometimes are needed in +// concatenations of multiple such raw strings, as in +// +// char const * s1, s2; +// OString s = OStringView(s1) + s2; +// +// (Providing specializations of ToStringHelper<std::string_view> and +// ToStringHelper<std::u16string_view> would look dubious, as it would give meaning to expressions +// like +// +// std::string_view(s1) + s2 +// +// that do not involve any user-defined types.) + +class OStringView { +public: + explicit OStringView(char const * s): view_(s) {} + + std::size_t length() const { return view_.length(); } + + char const * data() const { return view_.data(); } + +private: + std::string_view view_; +}; + +template<> +struct ToStringHelper< OStringView > + { + static std::size_t length( const OStringView& v ) { return v.length(); } + static char* addData( char* buffer, const OStringView& v ) SAL_RETURNS_NONNULL { return addDataHelper( buffer, v.data(), v.length() ); } + static const bool allowOStringConcat = true; + static const bool allowOUStringConcat = false; + }; + +class OUStringView { +public: + explicit OUStringView(sal_Unicode const * s): view_(s) {} + + std::size_t length() const { return view_.length(); } + + sal_Unicode const * data() const { return view_.data(); } + +private: + std::u16string_view view_; +}; + +template<> +struct ToStringHelper< OUStringView > + { + static std::size_t length( const OUStringView& v ) { return v.length(); } + static sal_Unicode* addData( sal_Unicode* buffer, const OUStringView& v ) SAL_RETURNS_NONNULL { return addDataHelper( buffer, v.data(), v.length() ); } + static const bool allowOStringConcat = false; + static const bool allowOUStringConcat = true; + }; } // namespace diff --git a/opencl/source/openclwrapper.cxx b/opencl/source/openclwrapper.cxx index e1645ebe85ff..a1aa5e90950c 100644 --- a/opencl/source/openclwrapper.cxx +++ b/opencl/source/openclwrapper.cxx @@ -185,7 +185,7 @@ OString createFileName(cl_device_id deviceId, const char* clFileName) platformVersion, nullptr); // create hash for deviceName + driver version + platform version - OString aString = OStringLiteral(deviceName) + driverVersion + platformVersion; + OString aString = rtl::OStringView(deviceName) + driverVersion + platformVersion; OString aHash = generateMD5(aString.getStr(), aString.getLength()); return getCacheFolder() + fileName + "-" + aHash + ".bin"; diff --git a/sal/qa/rtl/strings/test_ostring_concat.cxx b/sal/qa/rtl/strings/test_ostring_concat.cxx index e676aab2d27e..fc310dbb58bd 100644 --- a/sal/qa/rtl/strings/test_ostring_concat.cxx +++ b/sal/qa/rtl/strings/test_ostring_concat.cxx @@ -83,6 +83,8 @@ void test::ostring::StringConcat::checkConcat() CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OString, char* > )), typeid( OString( "foo" ) + d4 )); CPPUNIT_ASSERT_EQUAL( OString( "fooabc" ), OString( OString( "foo" ) + d4 )); CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OString, char* > )), typeid( OString( "foo" ) + d4 )); + CPPUNIT_ASSERT_EQUAL( OString( "fooabc" ), OString( rtl::OStringView( "foo" ) + d4 )); + CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< rtl::OStringView, char* > )), typeid( rtl::OStringView( "foo" ) + d4 )); CPPUNIT_ASSERT_EQUAL( OString( "num10" ), OString( OString( "num" ) + OString::number( 10 ))); CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OString, OStringNumber< int > > )), typeid( OString( "num" ) + OString::number( 10 ))); @@ -157,6 +159,7 @@ void test::ostring::StringConcat::checkInvalid() CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OUString( "b" ))); CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OUStringBuffer( "b" ))); CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OUStringLiteral( "b" ))); + CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + rtl::OUStringView( u"b" ))); CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + 1 )); rtl_String* rs = nullptr; rtl_uString* rus = nullptr; diff --git a/sal/qa/rtl/strings/test_oustring_concat.cxx b/sal/qa/rtl/strings/test_oustring_concat.cxx index 6e1b209f7189..b67a2938ea36 100644 --- a/sal/qa/rtl/strings/test_oustring_concat.cxx +++ b/sal/qa/rtl/strings/test_oustring_concat.cxx @@ -74,6 +74,8 @@ void test::oustring::StringConcat::checkConcat() const sal_Unicode* d2 = u"xyz"; CPPUNIT_ASSERT_EQUAL( OUString( "fooxyz" ), OUString( OUString( "foo" ) + d2 )); CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, const sal_Unicode* > )), typeid( OUString( "foo" ) + d2 )); + CPPUNIT_ASSERT_EQUAL( OUString( "fooxyz" ), OUString( rtl::OUStringView( u"foo" ) + d2 )); + CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< rtl::OUStringView, const sal_Unicode* > )), typeid( rtl::OUStringView( u"foo" ) + d2 )); CPPUNIT_ASSERT_EQUAL( OUString( "num10" ), OUString( OUString( "num" ) + OUString::number( 10 ))); CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, OUStringNumber< int > > )), typeid( OUString( "num" ) + OUString::number( 10 ))); @@ -168,6 +170,7 @@ void test::oustring::StringConcat::checkInvalid() CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + d )); CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + static_cast<char*>(d) )); CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OStringLiteral( "b" ))); + CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + rtl::OStringView( "b" ))); CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + 1 )); rtl_String* rs = nullptr; rtl_uString* rus = nullptr; |