From 1eace991536b47f2281bac0ab6ba11611745dec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Lalinsk=C3=BD?= Date: Wed, 25 Mar 2009 17:52:23 +0000 Subject: [PATCH] Detach before appending a character git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@944552 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- taglib/toolkit/tstring.cpp | 2 ++ tests/test_string.cpp | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp index 79b564fa..fa93c4cf 100644 --- a/taglib/toolkit/tstring.cpp +++ b/taglib/toolkit/tstring.cpp @@ -560,6 +560,8 @@ String &String::operator+=(wchar_t c) String &String::operator+=(char c) { + detach(); + d->data += uchar(c); return *this; } diff --git a/tests/test_string.cpp b/tests/test_string.cpp index b1b2c83d..c3b42ea4 100644 --- a/tests/test_string.cpp +++ b/tests/test_string.cpp @@ -37,6 +37,8 @@ class TestString : public CppUnit::TestFixture CPPUNIT_TEST(testUTF16Decode); CPPUNIT_TEST(testUTF16DecodeInvalidBOM); CPPUNIT_TEST(testUTF16DecodeEmptyWithBOM); + CPPUNIT_TEST(testAppendCharDetach); + CPPUNIT_TEST(testAppendStringDetach); CPPUNIT_TEST_SUITE_END(); public: @@ -131,6 +133,24 @@ public: CPPUNIT_ASSERT_EQUAL(String(), String(b, String::UTF16)); } + void testAppendStringDetach() + { + String a("a"); + String b = a; + a += "b"; + CPPUNIT_ASSERT_EQUAL(String("ab"), a); + CPPUNIT_ASSERT_EQUAL(String("a"), b); + } + + void testAppendCharDetach() + { + String a("a"); + String b = a; + a += 'b'; + CPPUNIT_ASSERT_EQUAL(String("ab"), a); + CPPUNIT_ASSERT_EQUAL(String("a"), b); + } + }; CPPUNIT_TEST_SUITE_REGISTRATION(TestString);