Ver código fonte

base: tweak the asserts in the String class, add String::Sub() method

for substrings, and the corresponding unit tests.
legacy
Sam Hocevar sam 12 anos atrás
pai
commit
41b752e635
3 arquivos alterados com 27 adições e 3 exclusões
  1. +1
    -0
      src/lol/base/array.h
  2. +13
    -3
      src/lol/base/string.h
  3. +13
    -0
      test/unit/string.cpp

+ 1
- 0
src/lol/base/array.h Ver arquivo

@@ -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];


+ 13
- 3
src/lol/base/string.h Ver arquivo

@@ -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);


+ 13
- 0
test/unit/string.cpp Ver arquivo

@@ -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 */


Carregando…
Cancelar
Salvar