@@ -18,6 +18,8 @@ | |||||
#if !defined __LOL_BASE_ARRAY_H__ | #if !defined __LOL_BASE_ARRAY_H__ | ||||
#define __LOL_BASE_ARRAY_H__ | #define __LOL_BASE_ARRAY_H__ | ||||
#include <lol/base/assert.h> | |||||
#include <new> | #include <new> | ||||
#include <stdint.h> | #include <stdint.h> | ||||
@@ -118,21 +120,29 @@ public: | |||||
inline Element& operator[](int n) | inline Element& operator[](int n) | ||||
{ | { | ||||
/* Allow array[0] even if size is zero so that people can | |||||
* always use &array[0] to get a pointer to the data. */ | |||||
ASSERT(n >= 0); | |||||
ASSERT(n < m_count || (!n && !m_count)); | |||||
return m_data[n]; | return m_data[n]; | ||||
} | } | ||||
inline Element const& operator[](int n) const | inline Element const& operator[](int n) const | ||||
{ | { | ||||
ASSERT(n >= 0); | |||||
ASSERT(n < m_count || (!n && !m_count)); | |||||
return m_data[n]; | return m_data[n]; | ||||
} | } | ||||
inline Element& Last() | inline Element& Last() | ||||
{ | { | ||||
ASSERT(m_count > 0); | |||||
return m_data[m_count - 1]; | return m_data[m_count - 1]; | ||||
} | } | ||||
inline Element const& Last() const | inline Element const& Last() const | ||||
{ | { | ||||
ASSERT(m_count > 0); | |||||
return m_data[m_count - 1]; | return m_data[m_count - 1]; | ||||
} | } | ||||
@@ -158,11 +168,14 @@ public: | |||||
inline void Pop() | inline void Pop() | ||||
{ | { | ||||
ASSERT(m_count > 0); | |||||
Remove(m_count - 1, 1); | Remove(m_count - 1, 1); | ||||
} | } | ||||
void Remove(int pos, int todelete = 1) | void Remove(int pos, int todelete = 1) | ||||
{ | { | ||||
ASSERT(pos >= 0); | |||||
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]; | ||||
for (int i = m_count - todelete; i < m_count; i++) | for (int i = m_count - todelete; i < m_count; i++) | ||||
@@ -172,6 +185,7 @@ public: | |||||
void Resize(int count, Element e = Element()) | void Resize(int count, Element e = Element()) | ||||
{ | { | ||||
ASSERT(count > 0); | |||||
Reserve(count); | Reserve(count); | ||||
/* Too many elements? Remove them. */ | /* Too many elements? Remove them. */ | ||||
@@ -17,6 +17,7 @@ | |||||
#if !defined __LOL_BASE_STRING_H__ | #if !defined __LOL_BASE_STRING_H__ | ||||
#define __LOL_BASE_STRING_H__ | #define __LOL_BASE_STRING_H__ | ||||
#include <lol/base/assert.h> | |||||
#include <lol/base/array.h> | #include <lol/base/array.h> | ||||
#include <cstring> | #include <cstring> | ||||
@@ -41,6 +42,7 @@ public: | |||||
: Super() | : Super() | ||||
{ | { | ||||
using namespace std; | using namespace std; | ||||
ASSERT(str); | |||||
Resize((int)strlen(str)); | Resize((int)strlen(str)); | ||||
memcpy(&(*this)[0], str, Count() + 1); | memcpy(&(*this)[0], str, Count() + 1); | ||||
} | } | ||||
@@ -49,9 +51,10 @@ public: | |||||
: Super() | : Super() | ||||
{ | { | ||||
using namespace std; | using namespace std; | ||||
ASSERT(str); | |||||
Resize(count); | Resize(count); | ||||
memcpy(&(*this)[0], str, count); | memcpy(&(*this)[0], str, count); | ||||
(*this)[count] = '\0'; | |||||
((Super &)*this)[count] = '\0'; | |||||
} | } | ||||
inline String(String const &s) | inline String(String const &s) | ||||
@@ -61,21 +64,27 @@ public: | |||||
inline char &operator [](int n) | inline char &operator [](int n) | ||||
{ | { | ||||
ASSERT(n >= 0); | |||||
ASSERT(n < Count() || (!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 < Count() || (!n && !Count())); | |||||
return ((Super const &)*this)[n]; | return ((Super const &)*this)[n]; | ||||
} | } | ||||
inline char &Last() | inline char &Last() | ||||
{ | { | ||||
ASSERT(Count() > 0); | |||||
return (*this)[Count() - 1]; | return (*this)[Count() - 1]; | ||||
} | } | ||||
inline char const &Last() const | inline char const &Last() const | ||||
{ | { | ||||
ASSERT(Count() > 0); | |||||
return (*this)[Count() - 1]; | return (*this)[Count() - 1]; | ||||
} | } | ||||
@@ -86,6 +95,7 @@ public: | |||||
void Resize(int count) | void Resize(int count) | ||||
{ | { | ||||
ASSERT(count >= 0, "count = %d", count); | |||||
((Super &)*this).Resize(count + 1); | ((Super &)*this).Resize(count + 1); | ||||
((Super &)*this).Last() = '\0'; | ((Super &)*this).Last() = '\0'; | ||||
} | } | ||||