From eeea31408b5c92a3677e9b65fe168dab1377344d Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Mon, 2 Nov 2015 22:08:52 +0000 Subject: [PATCH] Fix operator+ semantics in CBotString --- src/CBot/CBotDll.h | 2 +- src/CBot/CBotString.cpp | 16 ++++------------ test/unit/CBot/CBotString_test.cpp | 9 ++++++--- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/CBot/CBotDll.h b/src/CBot/CBotDll.h index 9027dfb0..c05e1a50 100644 --- a/src/CBot/CBotDll.h +++ b/src/CBot/CBotDll.h @@ -397,8 +397,8 @@ public: const CBotString& operator=(const CBotString& stringSrc); const CBotString& operator=(const char ch); const CBotString& operator=(const char* pString); - const CBotString& operator+(const CBotString& str); friend CBotString operator+(const CBotString& string, const char* lpsz); + CBotString operator+(const CBotString& str); const CBotString& operator+=(const char ch); const CBotString& operator+=(const CBotString& str); diff --git a/src/CBot/CBotString.cpp b/src/CBot/CBotString.cpp index a4240b98..5f7792c1 100644 --- a/src/CBot/CBotString.cpp +++ b/src/CBot/CBotString.cpp @@ -344,19 +344,11 @@ CBotString operator+(const CBotString& string, const char * lpsz) return s; } -const CBotString& CBotString::operator+(const CBotString& stringSrc) +CBotString CBotString::operator+(const CBotString& stringSrc) { - char* p = new char[m_lg+stringSrc.m_lg+1]; - - if (m_ptr!=nullptr) strcpy(p, m_ptr); - char* pp = p + m_lg; - if (stringSrc.m_ptr!=nullptr) strcpy(pp, stringSrc.m_ptr); - - delete[] m_ptr; - m_ptr = p; - m_lg += stringSrc.m_lg; - - return *this; + CBotString s(*this); + s += stringSrc; + return s; } const CBotString& CBotString::operator=(const char ch) diff --git a/test/unit/CBot/CBotString_test.cpp b/test/unit/CBot/CBotString_test.cpp index 22171c6a..ebec2024 100644 --- a/test/unit/CBot/CBotString_test.cpp +++ b/test/unit/CBot/CBotString_test.cpp @@ -271,10 +271,13 @@ TEST(CBotString_Test, operatorAdd) CBotString botStr4("Colobot"); //-- C string - const char cStr1[7] = "olobot"; + const char* cStr1 = "olobot"; - botStr1 + botStr2; - botStr1 + botStr3; + botStr1 = botStr1 + botStr2; + botStr1 = botStr1 + botStr3; + EXPECT_STREQ(botStr1.CStr(), botStr4.CStr()); + + botStr1 = "C" + botStr3; EXPECT_STREQ(botStr1.CStr(), botStr4.CStr()); botStr1.Empty();