Changeset 2267


Ignore:
Timestamp:
Jan 23, 2013, 7:29:41 PM (7 years ago)
Author:
sam
Message:

base: tweak the asserts in the String class, add String::Sub() method
for substrings, and the corresponding unit tests.

Location:
trunk
Files:
3 edited

Legend:

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

    r2266 r2267  
    176176    {
    177177        ASSERT(pos >= 0);
     178        ASSERT(todelete >= 0);
    178179        ASSERT(pos + todelete <= m_count);
    179180        for (int i = pos; i + todelete < m_count; i++)
  • trunk/src/lol/base/string.h

    r2266 r2267  
    6565    inline char &operator [](int n)
    6666    {
     67        /* Allow n == Count() because we might have reasonable reasons
     68         * to access that hidden null character. */
    6769        ASSERT(n >= 0);
    68         ASSERT(n < Count() || (!n && !Count()));
     70        ASSERT(n <= Count());
    6971        return ((Super &)*this)[n];
    7072    }
     
    7375    {
    7476        ASSERT(n >= 0);
    75         ASSERT(n < Count() || (!n && !Count()));
     77        ASSERT(n <= Count());
    7678        return ((Super const &)*this)[n];
    7779    }
     
    9698    void Resize(int count)
    9799    {
    98         ASSERT(count >= 0, "count = %d", count);
     100        ASSERT(count >= 0);
    99101        ((Super &)*this).Resize(count + 1);
    100102        ((Super &)*this).Last() = '\0';
     103    }
     104
     105    String Sub(int start, int count) const
     106    {
     107        ASSERT(start >= 0);
     108        ASSERT(count >= 0);
     109        ASSERT(start + count <= Count());
     110        return String(&(*this)[start], count);
    101111    }
    102112
  • trunk/test/unit/string.cpp

    r2216 r2267  
    134134        LOLUNIT_ASSERT(s3 == s4);
    135135    }
     136
     137    LOLUNIT_TEST(SubString)
     138    {
     139        String s1 = "Hello World";
     140
     141        String s2 = "Hello";
     142        String s3 = s1.Sub(0, 5);
     143        LOLUNIT_ASSERT(s3 == s2);
     144
     145        String s4 = "World";
     146        String s5 = s4.Sub(6, 5);
     147        LOLUNIT_ASSERT(s5 == s4);
     148    }
    136149};
    137150
Note: See TracChangeset for help on using the changeset viewer.