common.h:
#ifndef _COMMON
#define _COMMON
namespace chaos {
typedef long long llong;
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long long ulong;
typedef signed char schar;
typedef signed short sshort;
typedef signed int sint;
typedef signed long long slong;
}
#endif
#ifndef _STRING
#define _STRING
#include "common.h"
namespace chaos {
class String {
private:
char* str;
ushort len;
public:
String();
String(ushort);
String(const char*);
String(const String&);
String(const String&, ushort, ushort);
~String();
bool operator==(const String&);
bool operator!=(const String&);
String& operator=(const String&);
String& operator+=(const String&);
String operator+(const String&);
char* getStr() const;
ushort getLen() const;
void set(ushort, char);
char get(ushort) const;
template<class T> static String From(T value, ushort base);
template<class T> T to() const;
};
}
#endif
#include "string.h"
namespace chaos {
String::String() {
this->len = 0;
this->str = new char[this->len + 1];
}
String::String(ushort length) {
this->len = length;
this->str = new char[this->len + 1];
for (ushort i = 0; i < this->len; i++)
this->str[i] = '.';
this->str[this->len] = '\0';
}
String::String(const char* str) {
for (this->len = 0; str[this->len] != '\0'; this->len++);
this->str = new char[this->len + 1];
for (ushort i = 0; i <= this->len; i++)
this->str[i] = str[i];
}
String::String(const String& string) {
this->len = string.len;
this->str = new char[this->len + 1];
for (ushort i = 0; i <= this->len; i++)
this->str[i] = string.str[i];
}
String::String(const String& string, unsigned shortushort offset, unsigned shortushort length) {
// TODO: out of bounds?
this->len = length;
this->str = new char[this->len + 1];
for (ushort i = 0; i <= this->len; i++)
this->str[i] = string.str[offset + i];
}
String::~String() {
delete[] this->str;
}
bool String::operator==(const String& string) {
if (this->len != string.len) return false;
for (ushort i = 0; i < this->len; i++)
if (this->str[i] != string.str[i]) return false;
return true;
}
bool String::operator!=(const String& string) {
return !(*this == string);
}
String& String::operator=(const String& string) {
this->len = string.len;
delete[] this->str;
this->str = new char[this->len + 1];
for (ushort i = 0; i <= this->len; i++)
this->str[i] = string.str[i];
return *this;
}
String& String::operator+=(const String& string) {
char* str = new char[this->len + string.len + 1];
for (ushort i = 0; i < this->len; i++)
str[i] = this->str[i];
for (ushort i = 0; i <= string.len; i++)
str[this->len + i] = string.str[i];
delete[] this->str;
this->len = this->len + string.len;
this->str = str;
return *this;
}
String String::operator+(const String& string) {
char* str = new char[this->len + string.len + 1];
for (ushort i = 0; i < this->len; i++)
str[i] = this->str[i];
for (ushort i = 0; i <= string.len; i++)
str[this->len + i] = string.str[i];
return String(str);
}
char* String::getStr() const {
return this->str;
}
unsigned short String::getLen() const {
return this->len;
}
void String::set(ushort index, char value) {
// TODO: out of bounds?
this->str[index] = value;
}
char String::get(ushort index) const {
// TODO: out of bounds?
return this->str[index];
}
template<class T> String String::From(T value, ushort base) {
// TODO: check negative base
if (value == 0) return "0";
ushort length = value < 0 ? 1 : 0;
T temp = value;
while (temp != 0) {
length++;
temp /= base;
}
String result(length);
if (value < 0) {
result.str[0] = '-';
value = 0 - value;
}
while (value != 0) {
ushort rem = (value % base);
result.str[--length] = (rem <= 9) ? rem + '0' : (rem - 10) + 'a';
value /= base;
}
return result;
}
template<class T> T String::to() const {
T result = 0;
bool sign = str[0] == '-';
for (ushort i = sign; i < this->len; i++)
result = result * 10 + str[i] - '0';
return sign ? 0 - result : result;
}
template String String::From<char>(char, ushort);
template String String::From<short>(short, ushort);
template String String::From<int>(int, ushort);
template String String::From<llong>(llong, ushort);
template String String::From<uchar>(uchar, ushort);
template String String::From<ushort>(ushort, ushort);
template String String::From<uint>(uint, ushort);
template String String::From<ulong>(ulong, ushort);
template String String::From<schar>(schar, ushort);
template String String::From<sshort>(sshort, ushort);
template String String::From<sint>(sint, ushort);
template String String::From<slong>(slong, ushort);
template char String::to<char>() const;
template short String::to<short>() const;
template int String::to<int>() const;
template llong String::to<llong>() const;
template uchar String::to<uchar>() const;
template ushort String::to<ushort>() const;
template uint String::to<uint>() const;
template ulong String::to<ulong>() const;
template schar String::to<schar>() const;
template sshort String::to<sshort>() const;
template sint String::to<sint>() const;
template slong String::to<slong>() const;
}