namespace bing {
class String {
public:
template<class T>
void swap(T& a1, T& a2) {
T tmp = a1;
a1 = a2;
a2 = tmp;
}
typedef char* iterator;
friend ostream& operator<<(ostream& _cout, const bing::String& s);
friend istream& operator>>(istream& _cin, bing::String& s);
size_t find(char c, size_t pos = 0) const {
if (pos < _size) {
for (int i = pos; i < _size; i++) {
if (_str[i] == c) {
return i;
}
}
}
return -1;
}
size_t find(const char* s, size_t pos = 0) const {
assert(s);
assert(pos < _size);
const char* str = _str + pos;
while (*str) {
const char* cur = str;
const char* match = s;
while (*match && *match == *cur) {
cur++;
match++;
}
if (*match == '\0') {
return str - _str;
}
else {
str++;
}
}
return -1;
}
String& insert(size_t pos, char c) {
assert(pos <= _size);
if (_size == _capcity) {
reserve(_capcity * 2);
}
for (int i = _size; i >= (int)pos; i--) {
_str[i + 1] = _str[i];
}
_str[pos] = c;
_size++;
return *this;
}
String& insert(size_t pos, const char* str) {
size_t len = strlen(str);
if (_size + len > _capcity) {
reserve(_capcity * 2);
}
while (*str != '\0') {
this->insert(pos, *str);
str++;
pos++;
}
return *this;
}
char& operator[](size_t index) {
assert(index < _size);
return _str[index];
}
const char& operator[](size_t index)const {
assert(index < _size);
return _str[index];
}
bool operator<(const String& s) {
int res = strcmp(_str, s._str);
if (res < 0) {
return true;
}
return false;
}
bool operator<=(const String& s) {
return !(*this > s);
}
bool operator>(const String& s) {
int res = strcmp(_str, s._str);
if (res > 0) {
return true;
}
return false;
}
bool operator>=(const String& s) {
return !(*this < s);
}
bool operator==(const String& s) {
int res = strcmp(_str, s._str);
if (res == 0) {
return true;
}
return false;
}
bool operator!=(const String& s) {
return !(*this == s);
}
iterator begin() {
return _str;
}
iterator end() {
return _str + _size;
}
size_t size()const {
return _size;
}
size_t capacity()const {
return _capcity;
}
bool empty()const {
return _size == 0;
}
void resize(size_t newSize, char c = '\0') {
if (newSize > _size) {
if (newSize > _capcity) {
reserve(newSize);
}
memset(_str + _size, c, newSize - _size);
}
_size = newSize;
_str[_size] = '\0';
}
void reserve(size_t newCapacity) {
if (newCapacity > _capcity) {
char* str = new char[newCapacity + 1];
strcpy(str, _str);
delete[] _str;
_str = str;
_capcity = newCapacity;
}
}
void PushBack(char c) {
if (_size == _capcity) {
reserve(_capcity * 2);
}
_str[_size] = c;
_size++;
_str[_size] = '\0';
}
String& operator+=(char c) {
PushBack(c);
return *this;
}
void Append(const char* str) {
while (*str != '\0') {
this->PushBack(*str);
str++;
}
}
String& operator+=(const char* str) {
Append(str);
return *this;
}
void clear() {
_size = 0;
_str[_size] = '\0';
}
void swap(String& s) {
swap(_str, s._str);
swap(_size, s._size);
swap(_capcity, s._capcity);
}
const char* c_str() const {
return _str;
}
String& erase(size_t pos, size_t len) {
assert(pos < _size);
if (pos + len >= _size) {
_str[pos] = '\0';
_size = pos;
}
else {
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
return *this;
}
String()
: _str(new char[16])
, _capcity(0)
, _size(0)
{
_str[_size] = '\0';
_capcity = 15;
}
String(const char* str)
{
_size = strlen(str);
_str = new char[_size + 1];
_capcity = _size;
strcpy(_str, str);
}
String(const String& str)
:_str(new char[strlen(str._str) + 1])
, _size(str._size)
, _capcity(str._capcity)
{
strcpy(_str, str._str);
}
String& operator=(const String s) {
if (this != &s) {
char* tmp = new char[s._capcity + 1];
strcpy(tmp, s._str);
delete[] _str;
_str = tmp;
_size = s._size;
_capcity = s._capcity;
}
return *this;
}
~String() {
if (_str) {
delete[] _str;
_str = nullptr;
}
}
private:
char* _str;
size_t _size;
size_t _capcity;
};
}