From 57814351b9ab4cc36bffb98c85c2bbc186668bd7 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 23 Jan 2013 20:36:03 +0000 Subject: [PATCH] base: optimise array concatenation, fix string concatenation, and fix string concatenation unit tests. --- src/lol/base/array.h | 14 +++++--------- src/lol/base/string.h | 6 +++--- test/unit/string.cpp | 12 ++++++------ 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/lol/base/array.h b/src/lol/base/array.h index 81f8a43d..998b5150 100644 --- a/src/lol/base/array.h +++ b/src/lol/base/array.h @@ -95,26 +95,22 @@ public: return *this; } - ArrayBase& operator+=(ARRAY const &that) + ArrayBase& operator+=(ArrayBase const &that) { int todo = that.m_count; Reserve(m_count + that.m_count); for (int i = 0; i < todo; i++) - *this << that[i]; + new(&m_data[m_count + i]) Element(that[i]); + m_count += todo; return *this; } ARRAY operator+(ARRAY const &that) const { - /* FIXME: upon return, this makes a copy of the temporary object; - * use either C++11 move semantics, or add a special flag to the - * object indicating we're a temporary about to be destroyed */ ARRAY ret; ret.Reserve(m_count + that.m_count); - for (int i = 0; i < m_count; i++) - ret << (*this)[i]; - for (int i = 0; i < that.m_count; i++) - ret << that[i]; + ret += *this; + ret += that; return ret; } diff --git a/src/lol/base/string.h b/src/lol/base/string.h index 2eeebe37..7d660ca5 100644 --- a/src/lol/base/string.h +++ b/src/lol/base/string.h @@ -118,9 +118,9 @@ public: inline String& operator +=(String const &s) { - /* Ignore the trailing zero we don't want */ - --m_count; - (Super &)*this += (Super const &)s; + int old_count = Count(); + Resize(Count() + s.Count()); + memcpy(&(*this)[old_count], &s[0], Count() - old_count); return *this; } diff --git a/test/unit/string.cpp b/test/unit/string.cpp index 11c32ce9..ea0e6101 100644 --- a/test/unit/string.cpp +++ b/test/unit/string.cpp @@ -140,13 +140,13 @@ LOLUNIT_FIXTURE(StringTest) { String s1 = "Hello World"; - String s2 = "Hello"; - String s3 = s1.Sub(0, 5); - LOLUNIT_ASSERT(s3 == s2); + String s2 = s1.Sub(0, 5); + String s3 = "Hello"; + LOLUNIT_ASSERT(s2 == s3); - String s4 = "World"; - String s5 = s4.Sub(6, 5); - LOLUNIT_ASSERT(s5 == s4); + String s4 = s1.Sub(6, 5); + String s5 = "World"; + LOLUNIT_ASSERT(s4 == s5); } };