Browse Source

base: optimise array concatenation, fix string concatenation, and fix

string concatenation unit tests.
legacy
Sam Hocevar sam 12 years ago
parent
commit
57814351b9
3 changed files with 14 additions and 18 deletions
  1. +5
    -9
      src/lol/base/array.h
  2. +3
    -3
      src/lol/base/string.h
  3. +6
    -6
      test/unit/string.cpp

+ 5
- 9
src/lol/base/array.h View File

@@ -95,26 +95,22 @@ public:
return *this; return *this;
} }


ArrayBase& operator+=(ARRAY const &that)
ArrayBase& operator+=(ArrayBase const &that)
{ {
int todo = that.m_count; int todo = that.m_count;
Reserve(m_count + that.m_count); Reserve(m_count + that.m_count);
for (int i = 0; i < todo; i++) for (int i = 0; i < todo; i++)
*this << that[i];
new(&m_data[m_count + i]) Element(that[i]);
m_count += todo;
return *this; return *this;
} }


ARRAY operator+(ARRAY const &that) const 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; ARRAY ret;
ret.Reserve(m_count + that.m_count); 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; return ret;
} }




+ 3
- 3
src/lol/base/string.h View File

@@ -118,9 +118,9 @@ public:


inline String& operator +=(String const &s) 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; return *this;
} }




+ 6
- 6
test/unit/string.cpp View File

@@ -140,13 +140,13 @@ LOLUNIT_FIXTURE(StringTest)
{ {
String s1 = "Hello World"; 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);
} }
}; };




Loading…
Cancel
Save