diff options
-rw-r--r-- | include/rtl/ustrbuf.hxx | 78 | ||||
-rw-r--r-- | sal/CppunitTest_sal_rtl_oustringbuffer.mk | 1 | ||||
-rw-r--r-- | sal/qa/rtl/oustringbuffer/test_oustringbuffer_assign.cxx | 82 |
3 files changed, 160 insertions, 1 deletions
diff --git a/include/rtl/ustrbuf.hxx b/include/rtl/ustrbuf.hxx index 8cfcabe043b4..ecae7f03e129 100644 --- a/include/rtl/ustrbuf.hxx +++ b/include/rtl/ustrbuf.hxx @@ -24,7 +24,7 @@ #include <cassert> #include <cstddef> -#include <string.h> +#include <cstring> #include <rtl/ustrbuf.h> #include <rtl/ustring.hxx> @@ -222,6 +222,82 @@ public: return *this; } + /** Assign from a string. + + @since LibreOffice 5.3 + */ + OUStringBuffer & operator =(OUString const & string) { + sal_Int32 n = string.getLength(); + if (n >= nCapacity) { + ensureCapacity(n + 16); //TODO: check for overflow + } + std::memcpy( + pData->buffer, string.pData->buffer, + (n + 1) * sizeof (sal_Unicode)); + pData->length = n; + return *this; + } + + /** Assign from a string literal. + + @since LibreOffice 5.3 + */ + template<typename T> + typename + libreoffice_internal::ConstCharArrayDetector<T, OUStringBuffer &>::Type + operator =(T & literal) { + assert( + libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + sal_Int32 const n + = libreoffice_internal::ConstCharArrayDetector<T>::length; + if (n >= nCapacity) { + ensureCapacity(n + 16); //TODO: check for overflow + } + char const * from + = libreoffice_internal::ConstCharArrayDetector<T>::toPointer( + literal); + sal_Unicode * to = pData->buffer; + for (sal_Int32 i = 0; i <= n; ++i) { + to[i] = from[i]; + } + pData->length = n; + return *this; + } + +#if defined LIBO_INTERNAL_ONLY + /** @overload @since LibreOffice 5.3 */ + template<typename T> + typename libreoffice_internal::ConstCharArrayDetector< + T, OUStringBuffer &>::TypeUtf16 + operator =(T & literal) { + sal_Int32 const n + = libreoffice_internal::ConstCharArrayDetector<T>::length; + if (n >= nCapacity) { + ensureCapacity(n + 16); //TODO: check for overflow + } + std::memcpy( + pData->buffer, + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal), + (n + 1) * sizeof (sal_Unicode)); //TODO: check for overflow + pData->length = n; + return *this; + } +#endif + +#if defined LIBO_INTERNAL_ONLY + /** @overload @since LibreOffice 5.3 */ + template<typename T1, typename T2> + OUStringBuffer & operator =(OUStringConcat<T1, T2> const & concat) { + sal_Int32 const n = concat.length(); + if (n >= nCapacity) { + ensureCapacity(n + 16); //TODO: check for overflow + } + *concat.addData(pData->buffer) = 0; + pData->length = n; + return *this; + } +#endif + /** Release the string data. */ diff --git a/sal/CppunitTest_sal_rtl_oustringbuffer.mk b/sal/CppunitTest_sal_rtl_oustringbuffer.mk index 775ba13d2aac..b94b55df8924 100644 --- a/sal/CppunitTest_sal_rtl_oustringbuffer.mk +++ b/sal/CppunitTest_sal_rtl_oustringbuffer.mk @@ -12,6 +12,7 @@ $(eval $(call gb_CppunitTest_CppunitTest,sal_rtl_oustringbuffer)) $(eval $(call gb_CppunitTest_add_exception_objects,sal_rtl_oustringbuffer,\ sal/qa/rtl/oustringbuffer/test_oustringbuffer_appendchar \ sal/qa/rtl/oustringbuffer/test_oustringbuffer_appenduninitialized \ + sal/qa/rtl/oustringbuffer/test_oustringbuffer_assign \ sal/qa/rtl/oustringbuffer/test_oustringbuffer_noadditional \ sal/qa/rtl/oustringbuffer/test_oustringbuffer_tostring \ sal/qa/rtl/oustringbuffer/test_oustringbuffer_utf32 \ diff --git a/sal/qa/rtl/oustringbuffer/test_oustringbuffer_assign.cxx b/sal/qa/rtl/oustringbuffer/test_oustringbuffer_assign.cxx new file mode 100644 index 000000000000..e2e6661e5bef --- /dev/null +++ b/sal/qa/rtl/oustringbuffer/test_oustringbuffer_assign.cxx @@ -0,0 +1,82 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <sal/config.h> + +#include <cppunit/TestFixture.h> +#include <cppunit/TestAssert.h> +#include <cppunit/extensions/HelperMacros.h> + +#include <rtl/ustrbuf.hxx> +#include <rtl/ustring.hxx> + +namespace { + +class Test: public CppUnit::TestFixture { +private: + void test() { + OUStringBuffer b1; + OUString s1("123456789012345"); + b1 = s1; + CPPUNIT_ASSERT_EQUAL(s1, b1.toString()); + CPPUNIT_ASSERT_EQUAL(16, b1.getCapacity()); + OUString s2("abc"); + b1 = s2; + CPPUNIT_ASSERT_EQUAL(s2, b1.toString()); + CPPUNIT_ASSERT_EQUAL(16, b1.getCapacity()); + OUString s3("1234567890123456"); + b1 = s3; + CPPUNIT_ASSERT_EQUAL(s3, b1.toString()); + CPPUNIT_ASSERT_EQUAL(32, b1.getCapacity()); + OUStringBuffer b2; + b2 = "123456789012345"; + CPPUNIT_ASSERT_EQUAL(s1, b2.toString()); + CPPUNIT_ASSERT_EQUAL(16, b2.getCapacity()); + b2 = "abc"; + CPPUNIT_ASSERT_EQUAL(s2, b2.toString()); + CPPUNIT_ASSERT_EQUAL(16, b2.getCapacity()); + b2 = "1234567890123456"; + CPPUNIT_ASSERT_EQUAL(s3, b2.toString()); + CPPUNIT_ASSERT_EQUAL(32, b2.getCapacity()); +#if HAVE_CXX11_UTF16_STRING_LITERAL \ + && (!defined SAL_W32 || defined __MINGW32__) + // sal_Unicode is still wchar_t not char16_t even for MSVC 2015 + OUStringBuffer b3; + b3 = u"123456789012345"; + CPPUNIT_ASSERT_EQUAL(s1, b3.toString()); + CPPUNIT_ASSERT_EQUAL(16, b3.getCapacity()); + b3 = u"abc"; + CPPUNIT_ASSERT_EQUAL(s2, b3.toString()); + CPPUNIT_ASSERT_EQUAL(16, b3.getCapacity()); + b3 = u"1234567890123456"; + CPPUNIT_ASSERT_EQUAL(s3, b3.toString()); + CPPUNIT_ASSERT_EQUAL(32, b3.getCapacity()); +#endif + OUStringBuffer b4; + b4 = OUStringLiteral("1") + "23456789012345"; + CPPUNIT_ASSERT_EQUAL(s1, b4.toString()); + CPPUNIT_ASSERT_EQUAL(16, b4.getCapacity()); + b4 = OUStringLiteral("a") + "bc"; + CPPUNIT_ASSERT_EQUAL(s2, b4.toString()); + CPPUNIT_ASSERT_EQUAL(16, b4.getCapacity()); + b4 = OUStringLiteral("1") + "234567890123456"; + CPPUNIT_ASSERT_EQUAL(s3, b4.toString()); + CPPUNIT_ASSERT_EQUAL(32, b4.getCapacity()); + } + + CPPUNIT_TEST_SUITE(Test); + CPPUNIT_TEST(test); + CPPUNIT_TEST_SUITE_END(); +}; + +} + +CPPUNIT_TEST_SUITE_REGISTRATION(Test); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |