Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class String {
- private:
- mutable size_t size = 0;
- mutable size_t m_sizeAlloc = 0;
- mutable char* str = nullptr;
- void swap(String& s) {
- std::swap(size, s.size);
- std::swap(str, s.str);
- std::swap(m_sizeAlloc, s.m_sizeAlloc);
- }
- void allocate_add_up(size_t s)
- {
- ensure_size(size + s);
- }
- void ensure_size(size_t sNew)
- {
- auto sizeNew = sNew;
- if (sizeNew > m_sizeAlloc)
- {
- m_sizeAlloc = sizeNew + 8;
- }
- auto pstrNew = new char[m_sizeAlloc];
- if (str == nullptr)
- {
- str = pstrNew;
- }
- else
- {
- memcpy(pstrNew, str, size);
- delete[] str;
- str = pstrNew;
- }
- size = sizeNew;
- }
- String(size_t n) :
- String()
- {
- allocate_add_up(n);
- }
- public:
- String(): str(nullptr), size(0)
- {
- }
- String(char ch) :
- String()
- {
- allocate_add_up(1);
- str[0] = ch;
- }
- String(size_t n = 1, char ch = '\0') :
- String()
- {
- allocate_add_up(n);
- memset(str, ch, n);
- }
- String(const char* cstr) :
- String()
- {
- auto nlen = strlen(cstr);
- allocate_add_up(nlen);
- memcpy(str, cstr, nlen);
- }
- String(const String& s) :
- String()
- {
- allocate_add_up(s.size);
- memcpy(str, s.str, size);
- }
- String& operator=(String s) {
- swap(s);
- return *this;
- }
- // String& operator=(const char* cstr) {
- // size = std::strlen(cstr);
- // if (str != nullptr) clear();
- // str = new char[size];
- // memcpy(str, cstr, size);
- // return *this;
- // }
- const String& operator=(char cstr) const
- {
- ensure_size(1);
- str[0] = cstr;
- return *this;
- }
- const String& operator=(const char* cstr) const
- {
- nlen = strlen(cstr);
- ensure_size(nlen);
- memcpy(str, cstr, nlen);
- return *this;
- }
- char& operator[](size_t index)
- {
- return str[index];
- }
- const char& operator[](size_t index) const {
- return str[index];
- }
- void push_back(char s)
- {
- allocate_add_up(1);
- str[size-1] = s;
- }
- void pop_back() {
- if (size == 0)
- {
- throw "invalid call";
- }
- size--;
- }
- char& front() {
- return str[0];
- }
- char& back() {
- return str[size - 1];
- }
- char front() const {
- return str[0];
- }
- char back() const {
- return str[size - 1];
- }
- String& operator+=(const String& s) {
- size_t currSize = size;
- allocate_add_up(s.size);
- memcpy(str + currSize, s.str, s.size);
- return *this;
- }
- String operator+(const String& str) {
- String temp = *this;
- temp += str;
- return temp;
- }
- String substr(size_t start, size_t count) const {
- String temp(count);
- memcpy(temp.str, str + start, count);
- return temp;
- }
- void clear() const {
- size = 0;
- if (str != nullptr) delete[] str;
- }
- bool empty() const {
- return size == 0 ? true : false;
- }
- size_t length() const {
- return size;
- }
- size_t find(const String& sub) const {
- if (size < sub.size) return size;
- for (size_t i = 0, j = 0; i < size; ++i) {
- if (j == sub.size) {
- return i - j;
- }
- str[i] == sub[j] ? ++j : j = 0;
- }
- return size;
- }
- size_t rfind(const String& sub) const {
- if (size < sub.size) return size;
- for (size_t i = size - 1, j = 0; i > 0; --i) {
- if (j == sub.size) {
- return i + j;
- }
- str[i] == sub[j] ? ++j : j = 0;
- }
- return size;
- }
- ~String() {
- delete[] str;
- }
- String str() { return str + '\0'; }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement