Advertisement
KShah

Untitled

Nov 11th, 2021
1,019
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. class String{
  5. private:
  6.     char* str = nullptr;
  7.     size_t sz = 0;
  8.     size_t buffer = 0;
  9.    
  10.     void increaseBuffer() {
  11.         buffer *= 2;
  12.         char* newstr = new char[buffer];
  13.         memcpy(newstr, str, sz);
  14.         delete[] str;
  15.         str = newstr;
  16.     }
  17.  
  18.     void decreaseBuffer() {
  19.         buffer /= 2;
  20.         char* newstr = new char[buffer];
  21.         memcpy(newstr, str, sz);
  22.         delete[] str;
  23.         str = newstr;
  24.     }
  25.  
  26. public:
  27.     String() {}
  28.  
  29.     String(const size_t n, const char c): str(new char[n]), sz(n), buffer(n) {
  30.         memset(str, c, n);
  31.     }
  32.  
  33.     String(const char* s): str(new char[strlen(s)]), sz(strlen(s)), buffer(strlen(s)) {
  34.         memcpy(str, s, buffer);
  35.     }
  36.  
  37.     String(const String& other): String(other.sz, '\0') {
  38.         memcpy(str, other.str, sz);
  39.     }  
  40.  
  41.     ~String() {
  42.         delete[] str;
  43.     }
  44.  
  45.     size_t length() const {
  46.         return sz;
  47.     };
  48.  
  49.     void push_back(const char c) {
  50.         if (sz == buffer) {
  51.             increaseBuffer();
  52.         }
  53.         str[sz++] = c;
  54.     }
  55.  
  56.     void pop_back() {
  57.         if (sz <= (buffer / 4)) {
  58.             decreaseBuffer();
  59.         }
  60.         sz--;
  61.     }
  62.  
  63.     char& front() {
  64.         return str[0];
  65.     }
  66.  
  67.     const char& front() const {
  68.         return str[0];
  69.     }
  70.  
  71.     char& back() {
  72.         return str[sz - 1];
  73.     }
  74.  
  75.     const char& back() const {
  76.         return str[sz - 1];
  77.     }
  78.  
  79.     size_t find(const String& other) const {
  80.         int flag = 1;
  81.         for (size_t i = 0; i < sz; ++i) {
  82.             for (size_t j = 0; j < other.length(); ++j) {
  83.                 if (i + j >= sz || str[i + j] != other[j]) {
  84.                     flag = 0;
  85.                     break;
  86.                 }
  87.             }
  88.             if (flag == 1) {
  89.                 return i;
  90.             }
  91.             flag = 1;
  92.         }
  93.         return sz;
  94.     }
  95.    
  96.     size_t rfind(const String& other) const {
  97.         int flag = 1;
  98.         for (size_t i = 0; i < sz; ++i) {
  99.             for (size_t j = 0; j < other.length(); ++j) {
  100.                 if (sz - 1 - i + j >= sz || str[sz - 1 - i + j] != other[j]) {
  101.                     flag = 0;
  102.                     break;
  103.                 }
  104.             }      
  105.             if (flag == 1) {
  106.                 return sz - 1 - i;
  107.             }
  108.             flag = 1;
  109.         }
  110.         return sz;
  111.     }
  112.    
  113.     String substr(size_t start, size_t count) const {
  114.         String ans(count, '\0');
  115.         for (size_t i = 0; i < count; ++i) {
  116.             ans[i] = str[start + i];
  117.         }
  118.         return ans;
  119.     }
  120.  
  121.     bool empty() const {
  122.         return !sz;
  123.     }
  124.  
  125.     void clear() {
  126.         delete[] str;
  127.         str = new char[1];
  128.         sz = 0;
  129.         buffer = 1;
  130.     }
  131.  
  132.     String& operator=(const String& other) {
  133.         String copy = other;
  134.         sz = copy.sz;
  135.         buffer = copy.buffer;
  136.         delete[] str;
  137.         str = new char[buffer];
  138.         memcpy(str, copy.str, sz);
  139.         return *this;
  140.     }
  141.  
  142.     char& operator[](const size_t pos) {
  143.         return str[pos];
  144.     }
  145.  
  146.     const char& operator[](const size_t pos) const {
  147.         return str[pos];
  148.     }
  149.  
  150.     String& operator+=(const String& rhs) {
  151.         for (size_t i = 0; i < rhs.sz; ++i) {
  152.             push_back(str[i]);
  153.         }
  154.         return *this;
  155.     }
  156.  
  157.     String& operator+=(const char rhs) {
  158.         push_back(rhs);
  159.         return *this;
  160.     }
  161. };
  162.  
  163. bool operator==(const String& lhs, const String& rhs) {
  164.     if (lhs.length() != rhs.length()) {
  165.         return false;
  166.     }
  167.     for (size_t i = 0; i < lhs.length(); ++i) {
  168.         if (lhs[i] != rhs[i]){
  169.             return false;
  170.         }
  171.     }
  172.     return true;
  173. }
  174.  
  175. String operator+(const char first, const String& second) {
  176.     String copy = second;
  177.     copy += first;
  178.     return copy;
  179. }
  180.  
  181. String operator+(const String& first, const char second) {
  182.     return second + first;
  183. }
  184.  
  185. String operator+(const String& first, const String& second) {
  186.     String copy = first;
  187.     copy += second;
  188.     return copy;
  189. }
  190.  
  191. std::ostream& operator<<(std::ostream& out, const String& s) {
  192.     for (size_t i = 0; i < s.length(); ++i){
  193.         out << s[i];
  194.     }
  195.     return out;
  196. }
  197.  
  198. std::istream& operator>>(std::istream& in, String& s) {
  199.     char c;
  200.     while (in >> c) {
  201.         s.push_back(c);
  202.     }
  203.     return in;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement