string.h
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;
namespace goat
{
class string
{
public:
typedef char* iterator;
typedef const char* const_iterator;
iterator begin();
iterator end();
void Printf();
const_iterator begin() const
{
return _str;
}
const_iterator end() const
{
return _str + _size;
}
string(const char* str = "");
string(const string& str);
~string();
void clear();
char& operator[](size_t pos);
char& operator[](size_t pos) const;
size_t size() const;
size_t capacity() const;
const char* c_str();
void reserve(size_t n);
void resize(size_t n);
void resize(size_t n, char ch);
void push_back(char ch);
void append(const char* ch);
void append(size_t pos, const char* ch);
string& insert(size_t pos, char ch);
string& insert(size_t pos, const char* ch);
void erase(size_t pos, size_t len = npos);
void swap(string& str);
string& operator=( string str)
{
swap(str);
return *this;
}
string& operator+=(char str)
{
push_back(str);
return *this;
}
string& operator+=(const char* str)
{
append(str);
return *this;
}
string& operator+=(const string& str)
{
append(str._str);
return *this;
}
bool operator <(const string& str1) const
{
return strcmp(_str, str1._str) < 0;
}
bool operator ==(const string& str1) const
{
return strcmp(_str, str1._str) == 0;
}
bool operator <=(const string& str1) const
{
return *this < str1 || *this == str1;
}
bool operator >(const string& str1) const
{
return !(*this <= str1);
}
bool operator >=(const string& str1) const
{
return !(*this < str1);
}
bool operator !=(const string& str1) const
{
return !(*this == str1);
}
size_t find(char ch, size_t pos = 0);
size_t find(const char* ch, size_t pos = 0);
string substr(size_t pos, size_t len = npos);
private:
char* _str;
size_t _size;
size_t _capacity;
public:
const static size_t npos = -1;
};
ostream& operator<<(ostream& out, const string& s);
istream& operator>>(istream& in, string& s);
istream& getline(istream& is, string& str);
}
string.cpp
#include"string.h"
goat::string::iterator goat::string::begin()
{
return _str;
}
goat::string::iterator goat::string::end()
{
return _str + _size;
}
goat::string::const_iterator goat::string::begin() const
{
return _str;
}
goat::string::const_iterator goat::string::end() const
{
return _str + _size;
}
void goat::string::Printf()
{
std::cout << c_str() << std::endl;
std::cout << std::endl;
}
goat::string::string(const char* str = "")
:_size(strlen(str))
, _capacity(_size)
{
_str = new char[_capacity + 1];
strcpy(_str, str);
}
goat::string::string(const string& str)
{
_str = new char[str._capacity + 1];
strcpy(_str, str._str);
_size = str._size;
_capacity = str._capacity;
}
goat::string::~string()
{
delete[] _str;
_str = nullptr;
_capacity = _size = 0;
}
void goat::string::clear()
{
_str[0] = '\0';
_size = 0;
}
char& goat::string::operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
char& goat::string::operator[](size_t pos) const
{
assert(pos < _size);
return _str[pos];
}
size_t goat::string::size() const
{
return _size;
}
size_t goat::string::capacity() const
{
return _capacity;
}
const char* goat::string::c_str()
{
return _str;
}
void goat::string::reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
void goat::string::resize(size_t n)
{
if (n <= _size)
{
_str[n] = '\0';
_size = n;
}
else
{
reserve(n);
while (_size < n)
{
_str[_size] = '\0';
++_size;
}
_str[_size] = '\0';
}
}
void goat::string::resize(size_t n, char ch)
{
if (n <= _size)
{
_str[n] = '\0';
_size = n;
}
else
{
reserve(n);
while (_size < n)
{
_str[_size] = ch;
++_size;
}
_str[_size] = '\0';
}
}
void goat::string::push_back(char ch)
{
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
_str[_size] = ch;
++_size;
_str[_size] = '\0';
}
void goat::string::append(const char* ch)
{
size_t size = strlen(ch);
if (_size + size > _capacity)
{
reserve(_size + size);
}
strcpy(_str + _size, ch);
_size += size;
}
void goat::string::append(size_t pos, const char* ch)
{
assert(pos < _size);
size_t size = strlen(ch);
if (_size + size > _capacity)
{
reserve(_size + size);
}
strcpy(_str + _size, ch);
_size += size;
}
goat::string& goat::string::insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
size_t end = _size + 1;
while (end > pos)
{
_str[end] = _str[end - 1];
--end;
}
_str[pos] = ch;
++_size;
return *this;
}
goat::string& goat::string::insert(size_t pos, const char* ch)
{
assert(pos <= _size);
size_t len = strlen(ch);
reserve(_size + len);
size_t end = _size + len + 1;
while (end < pos)
{
_str[end] = _str[end - len];
--end;
}
strncpy(_str + pos, ch, len);
_size += len;
_str[_size] = '\0';
return *this;
}
void goat::string::erase(size_t pos, size_t len = npos)
{
assert(pos < _size);
if (len == npos || len + pos >= _size)
{
_str[_size] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
}
void goat::string::swap(string& str)
{
std::swap(_str, str._str);
std::swap(_size, str._size);
std::swap(_capacity, str._capacity);
}
goat::string& goat::string::operator=(string str)
{
swap(str);
return *this;
}
size_t goat::string::find(char ch, size_t pos = 0)
{
assert(pos < _size);
for (size_t i = pos; i < _size; i++)
{
if (_str[i] == ch)
{
return i;
}
}
return npos;
}
size_t find(const char* ch, size_t pos = 0)
{
const char* p = strstr(_str, ch);
if (p)
{
return p - _str;
}
else
{
return npos;
}
return npos;
}
goat::string goat::string::substr(size_t pos, size_t len = npos)
{
string s;
size_t end = pos + _size;
if (len == npos || pos + len >= _size)
{
len = _size - pos;
s.reserve(len);
for (size_t i = pos; i < end; i++)
{
s += _str[i];
}
}
else
{
for (size_t i = pos; i < _size + len; i++)
{
s += _str[i];
}
}
return s;
}
ostream& goat::operator<<(ostream& out, const string& s)
{
for (auto ch : s)
{
out << ch;
}
return out;
}
istream& goat::operator>>(istream& in, string& s)
{
s.clear();
char ch;
ch = in.get();
while (ch != ' ' && ch != '\n')
{
s += ch;
ch = in.get();
}
return in;
}
istream& goat::getline(istream& is, string& str)
{
str.clear();
char ch;
ch = in.get();
char buff[128];
size_t i = 0;
while (ch != '\0')
{
buff[i++] = ch;
if (i == 127)
{
buff[127] = '\0';
str += buff;
i = 0;
}
ch = in.get();
}
if (i > 0)
{
buff[i] = '\0';
str += buff;
}
return in;
}