Changeset 2269


Ignore:
Timestamp:
Jan 23, 2013, 9:36:03 PM (7 years ago)
Author:
sam
Message:

base: optimise array concatenation, fix string concatenation, and fix
string concatenation unit tests.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lol/base/array.h

    r2267 r2269  
    9696    }
    9797
    98     ArrayBase& operator+=(ARRAY const &that)
     98    ArrayBase& operator+=(ArrayBase const &that)
    9999    {
    100100        int todo = that.m_count;
    101101        Reserve(m_count + that.m_count);
    102102        for (int i = 0; i < todo; i++)
    103             *this << that[i];
     103            new(&m_data[m_count + i]) Element(that[i]);
     104        m_count += todo;
    104105        return *this;
    105106    }
     
    107108    ARRAY operator+(ARRAY const &that) const
    108109    {
    109         /* FIXME: upon return, this makes a copy of the temporary object;
    110          * use either C++11 move semantics, or add a special flag to the
    111          * object indicating we're a temporary about to be destroyed */
    112110        ARRAY ret;
    113111        ret.Reserve(m_count + that.m_count);
    114         for (int i = 0; i < m_count; i++)
    115             ret << (*this)[i];
    116         for (int i = 0; i < that.m_count; i++)
    117             ret << that[i];
     112        ret += *this;
     113        ret += that;
    118114        return ret;
    119115    }
  • trunk/src/lol/base/string.h

    r2267 r2269  
    119119    inline String& operator +=(String const &s)
    120120    {
    121         /* Ignore the trailing zero we don't want */
    122         --m_count;
    123         (Super &)*this += (Super const &)s;
     121        int old_count = Count();
     122        Resize(Count() + s.Count());
     123        memcpy(&(*this)[old_count], &s[0], Count() - old_count);
    124124        return *this;
    125125    }
  • trunk/test/unit/string.cpp

    r2268 r2269  
    141141        String s1 = "Hello World";
    142142
    143         String s2 = "Hello";
    144         String s3 = s1.Sub(0, 5);
    145         LOLUNIT_ASSERT(s3 == s2);
     143        String s2 = s1.Sub(0, 5);
     144        String s3 = "Hello";
     145        LOLUNIT_ASSERT(s2 == s3);
    146146
    147         String s4 = "World";
    148         String s5 = s4.Sub(6, 5);
    149         LOLUNIT_ASSERT(s5 == s4);
     147        String s4 = s1.Sub(6, 5);
     148        String s5 = "World";
     149        LOLUNIT_ASSERT(s4 == s5);
    150150    }
    151151};
Note: See TracChangeset for help on using the changeset viewer.