Advertisement
camilosasuketbs

Untitled

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