Parcourir la source

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

for substrings, and the corresponding unit tests.
legacy
Sam Hocevar sam il y a 12 ans
Parent
révision
41b752e635
3 fichiers modifiés avec 27 ajouts et 3 suppressions
  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 Voir le fichier

@@ -175,6 +175,7 @@ public:
void Remove(int pos, int todelete = 1) void Remove(int pos, int todelete = 1)
{ {
ASSERT(pos >= 0); ASSERT(pos >= 0);
ASSERT(todelete >= 0);
ASSERT(pos + todelete <= m_count); ASSERT(pos + todelete <= m_count);
for (int i = pos; i + todelete < m_count; i++) for (int i = pos; i + todelete < m_count; i++)
m_data[i] = m_data[i + todelete]; m_data[i] = m_data[i + todelete];


+ 13
- 3
src/lol/base/string.h Voir le fichier

@@ -64,15 +64,17 @@ public:


inline char &operator [](int n) 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 >= 0);
ASSERT(n < Count() || (!n && !Count()));
ASSERT(n <= Count());
return ((Super &)*this)[n]; return ((Super &)*this)[n];
} }


inline char const &operator [](int n) const inline char const &operator [](int n) const
{ {
ASSERT(n >= 0); ASSERT(n >= 0);
ASSERT(n < Count() || (!n && !Count()));
ASSERT(n <= Count());
return ((Super const &)*this)[n]; return ((Super const &)*this)[n];
} }


@@ -95,11 +97,19 @@ public:


void Resize(int count) void Resize(int count)
{ {
ASSERT(count >= 0, "count = %d", count);
ASSERT(count >= 0);
((Super &)*this).Resize(count + 1); ((Super &)*this).Resize(count + 1);
((Super &)*this).Last() = '\0'; ((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 inline String operator +(String const &s) const
{ {
String ret(*this); String ret(*this);


+ 13
- 0
test/unit/string.cpp Voir le fichier

@@ -133,6 +133,19 @@ LOLUNIT_FIXTURE(StringTest)


LOLUNIT_ASSERT(s3 == s4); 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 */ } /* namespace lol */


Chargement…
Annuler
Enregistrer