#include #define BS_FREE if (m_string != 0) delete m_string; #define BS_ALLOC m_string = new T[m_length]; #define BS_VRFY if (m_length == 0) { m_string = NULL; return; } using namespace CMem; template BasicString::BasicString() { m_string = NULL; m_length = 0; } template BasicString::BasicString(const T* string, u32int length) { m_string = NULL; affect(string, length); } template BasicString::BasicString(const BasicString &other) { m_string = NULL; affect(other); } template BasicString::BasicString(const T value, u32int count) { m_string = NULL; affect(value, count); } template BasicString::~BasicString() { BS_FREE; } template void BasicString::affect(const BasicString &other) { BS_FREE; m_length = other.m_length; BS_VRFY; BS_ALLOC; memcpy((u8int*)m_string, (u8int*)(other.m_string), m_length * sizeof(T)); } template void BasicString::affect(const T* string, u32int length) { BS_FREE; m_length = length; BS_VRFY; BS_ALLOC; memcpy((u8int*)string, (u8int*)string, m_length * sizeof(T)); } template void BasicString::affect(const T value, u32int count) { BS_FREE; m_length = count; BS_VRFY; BS_ALLOC; for (u32int i = 0; i < count; i++) { m_string[i] = value; } } template bool BasicString::compare(const BasicString &other) const { if (m_length != other.m_length) return false; for (u32int i = 0; i < m_length; i++) { if (m_string[i] != other.m_string[i]) return false; } return true; } template bool BasicString::compare(const T* string, u32int length) const { if (m_length != length) return false; for (u32int i = 0; i < m_length; i++) { if (m_string[i] != string[i]) return false; } return true; } template BasicString &BasicString::append(const BasicString &other) { T* newdata = new T[m_length + other.m_length]; for (u32int i = 0; i < m_length; i++) { newdata[i] = m_string[i]; } for (u32int i = 0; i < other.m_length; i++) { newdata[i + m_length] = other.m_string[i]; } BS_FREE; m_string = newdata; m_length += other.m_length; return *this; } template BasicString &BasicString::append(const T* string, u32int length) { T* newdata = new T[m_length + length]; for (u32int i = 0; i < m_length; i++) { newdata[i] = m_string[i]; } for (u32int i = 0; i < length; i++) { newdata[i + m_length] = string[i]; } BS_FREE; m_string = newdata; m_length += length; return *this; } template BasicString &BasicString::append(const T other) { T* newdata = new T[m_length + 1]; for (u32int i = 0; i < m_length; i++) { newdata[i] = m_string[i]; } BS_FREE; m_string = newdata; m_string[m_length] = other; m_length++; return *this; } template BasicString BasicString::concat(const BasicString &other) const { BasicString ret(*this); return (ret.append(other)); } template BasicString BasicString::concat(const T* string, u32int length) const { BasicString ret(*this); return (ret.append(string, length)); } template BasicString BasicString::concat(const T other) const { BasicString ret(*this); return (ret.append(other)); } template void BasicString::clear() { BS_FREE; m_string = 0; m_length = 0; } template bool BasicString::contains(const T& chr) const { for (u32int i = 0; i < m_length; i++) { if (m_string[i] == chr) return true; } return false; } template Vector< BasicString > BasicString::split(T sep) const { Vector< BasicString > ret; ret.push(BasicString()); for (u32int i = 0; i < m_length; i++) { if (m_string[i] == sep) { ret.push(BasicString()); } else { ret.back().append(m_string[i]); } } return ret; } template BasicString BasicString::substr(s32int start, s32int size) { if (size == 0) return BasicString(); if (start < 0) start = m_length - start; if (size == -1) size = m_length - start; BasicString ret; ret.m_string = new T[size + 1]; ret.m_length = size; memcpy((u8int*)ret.m_string, (u8int*)(&m_string[start]), size * sizeof(T)); return ret; } template bool BasicString::operator<(const BasicString& other) const { for (u32int i = 0; i < m_length && i < other.m_length; i++) { if (m_string[i] < other.m_string[i]) return true; } if (m_length < other.m_length) return true; return false; }