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);