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;
- }
- public:
- // String(): str(nullptr), size(0) {
- // }
- String(char ch) : size(1), str(new char[1]) {
- str[0] = ch;
- }
- String(size_t n = 1, char ch = '\0') : size(n), str(new char[n]) {
- memset(str, ch, n);
- }
- String(const char* cstr) : size(std::strlen(cstr)), str(new char[size]) {
- memcpy(str, cstr, size);
- }
- String(const String& s) : String(s.size, '\0') {
- 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) {
- ensure_size(1);
- str[0] = cstr;
- // if (str != nullptr) clear();
- // size = 1;
- // str = new char[size];
- // str[0] = cstr;
- return *this;
- }
- const String& operator=(const char* cstr) {
- size = strlen(cstr);
- ensure_size(size);
- memcpy(str, cstr, size);
- return *this;
- // if (str != nullptr) clear();
- // size = std::strlen(cstr);
- // str = new char[size];
- // memcpy(str, cstr, size);
- 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;
- // char* temp = new char(s);
- // *this += temp;
- // delete[] temp;
- }
- void pop_back() {
- if (size == 0)
- {
- throw "invalid call";
- }
- size--;
- // String s = substr(0, size - 1);
- // *this = s;
- }
- char& front() {
- return str[0];
- }
- char& back() {
- return str[size - 1];
- }
- char front() const {
- return str[0];
- }
- char back() const {
- return str[size - 1];
- }
- const char* c_str() const {
- return str;
- }
- String& operator+=(const String& s) {
- size_t currSize = size;
- allocate_add_up(s.size);
- memcpy(str + currSize, s.str, s.size);
- return *this;
- // char* currStr = nullptr;
- // std::swap(currStr, str);
- // clear();
- // size = currSize + s.size;
- // str = new char[size];
- // memcpy(str, currStr, currSize);
- // memcpy(str + currSize, s.str, s.size);
- // delete[] currStr;
- 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;
- auto p = str;
- auto pend = p + size - sub.size;
- auto psubend = sub.str + sub.size;
- while (p <= pend)
- {
- auto pitem = p;
- auto psubitem = sub.str;
- while (psubitem < psubend)
- {
- if (*pitem != *psubitem)
- {
- goto next;
- }
- pitem++;
- psubitem++;
- }
- return p - str;
- next:
- p++;
- }
- return size;
- }
- size_t rfind(const String& sub) const {
- if (size < sub.size) return size;
- auto searchsize = size - sub.size;
- auto pend = str + searchsize;
- auto p = pend;
- auto psubend = sub.str + sub.size;
- while (p >= str)
- {
- auto pitem = p;
- auto psubitem = sub.str;
- while (psubitem < psubend)
- {
- if (*pitem != *psubitem)
- {
- goto next;
- }
- pitem++;
- psubitem++;
- }
- return p - str;
- next:
- p--;
- }
- return size;
- }
- ~String() {
- delete[] str;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement