diff --git a/src/lol/base/array.h b/src/lol/base/array.h index 33996097..81f8a43d 100644 --- a/src/lol/base/array.h +++ b/src/lol/base/array.h @@ -175,6 +175,7 @@ public: void Remove(int pos, int todelete = 1) { ASSERT(pos >= 0); + ASSERT(todelete >= 0); ASSERT(pos + todelete <= m_count); for (int i = pos; i + todelete < m_count; i++) m_data[i] = m_data[i + todelete]; diff --git a/src/lol/base/string.h b/src/lol/base/string.h index 1ecbfe88..2eeebe37 100644 --- a/src/lol/base/string.h +++ b/src/lol/base/string.h @@ -64,15 +64,17 @@ public: inline char &operator [](int n) { + /* Allow n == Count() because we might have reasonable reasons + * to access that hidden null character. */ ASSERT(n >= 0); - ASSERT(n < Count() || (!n && !Count())); + ASSERT(n <= Count()); return ((Super &)*this)[n]; } inline char const &operator [](int n) const { ASSERT(n >= 0); - ASSERT(n < Count() || (!n && !Count())); + ASSERT(n <= Count()); return ((Super const &)*this)[n]; } @@ -95,11 +97,19 @@ public: void Resize(int count) { - ASSERT(count >= 0, "count = %d", count); + ASSERT(count >= 0); ((Super &)*this).Resize(count + 1); ((Super &)*this).Last() = '\0'; } + String Sub(int start, int count) const + { + ASSERT(start >= 0); + ASSERT(count >= 0); + ASSERT(start + count <= Count()); + return String(&(*this)[start], count); + } + inline String operator +(String const &s) const { String ret(*this); diff --git a/test/unit/string.cpp b/test/unit/string.cpp index 789ec96f..674664f5 100644 --- a/test/unit/string.cpp +++ b/test/unit/string.cpp @@ -133,6 +133,19 @@ LOLUNIT_FIXTURE(StringTest) LOLUNIT_ASSERT(s3 == s4); } + + LOLUNIT_TEST(SubString) + { + String s1 = "Hello World"; + + String s2 = "Hello"; + String s3 = s1.Sub(0, 5); + LOLUNIT_ASSERT(s3 == s2); + + String s4 = "World"; + String s5 = s4.Sub(6, 5); + LOLUNIT_ASSERT(s5 == s4); + } }; } /* namespace lol */