Advertisement
camilosasuketbs

Untitled

Oct 27th, 2021
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1.  
  2. class String {
  3. private:
  4. mutable size_t size = 0;
  5. mutable size_t m_sizeAlloc = 0;
  6. mutable char* str = nullptr;
  7.  
  8. void swap(String& s) {
  9. std::swap(size, s.size);
  10. std::swap(str, s.str);
  11. std::swap(m_sizeAlloc, s.m_sizeAlloc);
  12. }
  13.  
  14. void allocate_add_up(size_t s)
  15. {
  16.  
  17. ensure_size(size + s);
  18.  
  19. }
  20.  
  21. void ensure_size(size_t sNew)
  22. {
  23.  
  24. auto sizeNew = sNew;
  25.  
  26. if (sizeNew > m_sizeAlloc)
  27. {
  28.  
  29. m_sizeAlloc = sizeNew + 8;
  30.  
  31. }
  32.  
  33. auto pstrNew = new char[m_sizeAlloc];
  34.  
  35. if (str == nullptr)
  36. {
  37.  
  38. str = pstrNew;
  39.  
  40. }
  41. else
  42. {
  43.  
  44. memcpy(pstrNew, str, size);
  45.  
  46. delete[] str;
  47.  
  48. str = pstrNew;
  49.  
  50. }
  51.  
  52. size = sizeNew;
  53.  
  54. }
  55.  
  56. String(size_t n) :
  57. String()
  58. {
  59.  
  60. allocate_add_up(n);
  61.  
  62. }
  63.  
  64.  
  65. public:
  66.  
  67. String(): str(nullptr), size(0)
  68. {
  69.  
  70. }
  71.  
  72. String(char ch) :
  73. String()
  74. {
  75.  
  76. allocate_add_up(1);
  77.  
  78. str[0] = ch;
  79.  
  80. }
  81.  
  82.  
  83. String(size_t n = 1, char ch = '\0') :
  84. String()
  85. {
  86.  
  87. allocate_add_up(n);
  88.  
  89. memset(str, ch, n);
  90.  
  91. }
  92.  
  93.  
  94. String(const char* cstr) :
  95. String()
  96. {
  97.  
  98. auto nlen = strlen(cstr);
  99.  
  100. allocate_add_up(nlen);
  101.  
  102. memcpy(str, cstr, nlen);
  103.  
  104. }
  105.  
  106.  
  107. String(const String& s) :
  108. String()
  109. {
  110.  
  111. allocate_add_up(s.size);
  112.  
  113. memcpy(str, s.str, size);
  114.  
  115. }
  116.  
  117. String& operator=(String s) {
  118. swap(s);
  119.  
  120. return *this;
  121. }
  122.  
  123. // String& operator=(const char* cstr) {
  124. // size = std::strlen(cstr);
  125. // if (str != nullptr) clear();
  126. // str = new char[size];
  127. // memcpy(str, cstr, size);
  128.  
  129. // return *this;
  130. // }
  131.  
  132.  
  133. const String& operator=(char cstr) const
  134. {
  135.  
  136. ensure_size(1);
  137.  
  138. str[0] = cstr;
  139.  
  140. return *this;
  141.  
  142. }
  143.  
  144.  
  145. const String& operator=(const char* cstr) const
  146. {
  147.  
  148. nlen = strlen(cstr);
  149.  
  150. ensure_size(nlen);
  151.  
  152. memcpy(str, cstr, nlen);
  153.  
  154. return *this;
  155.  
  156. }
  157.  
  158.  
  159. char& operator[](size_t index)
  160. {
  161.  
  162. return str[index];
  163.  
  164. }
  165.  
  166.  
  167. const char& operator[](size_t index) const {
  168.  
  169. return str[index];
  170. }
  171.  
  172. void push_back(char s)
  173. {
  174.  
  175. allocate_add_up(1);
  176.  
  177. str[size-1] = s;
  178.  
  179. }
  180.  
  181. void pop_back() {
  182. if (size == 0)
  183. {
  184.  
  185. throw "invalid call";
  186.  
  187. }
  188. size--;
  189. }
  190.  
  191. char& front() {
  192.  
  193. return str[0];
  194. }
  195.  
  196. char& back() {
  197.  
  198. return str[size - 1];
  199. }
  200.  
  201. char front() const {
  202.  
  203. return str[0];
  204. }
  205.  
  206. char back() const {
  207.  
  208. return str[size - 1];
  209. }
  210.  
  211. String& operator+=(const String& s) {
  212.  
  213. size_t currSize = size;
  214.  
  215. allocate_add_up(s.size);
  216.  
  217. memcpy(str + currSize, s.str, s.size);
  218.  
  219. return *this;
  220.  
  221. }
  222.  
  223. String operator+(const String& str) {
  224. String temp = *this;
  225. temp += str;
  226.  
  227. return temp;
  228. }
  229.  
  230. String substr(size_t start, size_t count) const {
  231. String temp(count);
  232. memcpy(temp.str, str + start, count);
  233.  
  234. return temp;
  235. }
  236.  
  237. void clear() const {
  238. size = 0;
  239. if (str != nullptr) delete[] str;
  240. }
  241.  
  242. bool empty() const {
  243.  
  244. return size == 0 ? true : false;
  245. }
  246.  
  247. size_t length() const {
  248.  
  249. return size;
  250. }
  251.  
  252. size_t find(const String& sub) const {
  253. if (size < sub.size) return size;
  254.  
  255. for (size_t i = 0, j = 0; i < size; ++i) {
  256. if (j == sub.size) {
  257. return i - j;
  258. }
  259. str[i] == sub[j] ? ++j : j = 0;
  260. }
  261.  
  262. return size;
  263. }
  264.  
  265. size_t rfind(const String& sub) const {
  266. if (size < sub.size) return size;
  267.  
  268. for (size_t i = size - 1, j = 0; i > 0; --i) {
  269. if (j == sub.size) {
  270. return i + j;
  271. }
  272.  
  273. str[i] == sub[j] ? ++j : j = 0;
  274. }
  275.  
  276. return size;
  277. }
  278.  
  279. ~String() {
  280. delete[] str;
  281. }
  282.  
  283.  
  284. String str() { return str + '\0'; }
  285.  
  286. };
  287.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement