Advertisement
rudolf222222

Untitled

Nov 5th, 2022
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.58 KB | None | 0 0
  1. #include "string.hpp"
  2.  
  3. #include <type_traits>
  4. String::String(size_t size, char character) {
  5.   capacity_ = size;
  6.   size_ = size;
  7.   if (size == 0) {
  8.     capacity_ = 0;
  9.     size_ = 0;
  10.     string_ = nullptr;
  11.   } else {
  12.     string_ = new char[size + 1];
  13.     for (size_t i = 0; i < size; ++i) {
  14.       string_[i] = character;
  15.     }
  16.     string_[size] = '\0';
  17.   }
  18. }
  19. String::String(const char* const_str) {
  20.   if (const_str == nullptr) {
  21.     size_ = 0;
  22.     capacity_ = 0;
  23.     string_ = nullptr;
  24.   } else {
  25.     const size_t kLength = strlen(const_str);
  26.     string_ = new char[kLength + 1];
  27.     capacity_ = kLength;
  28.     size_ = capacity_;
  29.     for (size_t i = 0; i < kLength; ++i) {
  30.       string_[i] = const_str[i];
  31.     }
  32.     string_[size_] = '\0';
  33.   }
  34. }
  35. String::String(const String& source) {
  36.   if (source.capacity_ == 0) {
  37.     string_ = nullptr;
  38.     size_ = 0;
  39.     capacity_ = 0;
  40.   } else {
  41.     if (source.string_ == nullptr) {
  42.       const size_t kSize = source.capacity_;
  43.       string_ = new char[kSize + 1];
  44.       size_ = 0;
  45.       capacity_ = source.capacity_;
  46.     } else {
  47.       const size_t kSize = source.capacity_;
  48.       string_ = new char[kSize + 1];  // here
  49.       const size_t kEdge = source.size_;
  50.       for (size_t i = 0; i < kEdge; ++i) {
  51.         string_[i] = source[i];
  52.       }
  53.       string_[kEdge] = '\0';
  54.       size_ = source.size_;
  55.       capacity_ = source.capacity_;
  56.     }
  57.   }
  58. }
  59. String& String::operator=(const String& src) {
  60.   String copy = src;
  61.   Swap(copy);
  62.   return *this;
  63. }
  64. void String::Swap(String& src) {
  65.   char* temptr = string_;
  66.   size_t tempsize = size_;
  67.   size_t tempcapacity = capacity_;
  68.   capacity_ = src.capacity_;
  69.   size_ = src.size_;
  70.   string_ = src.string_;
  71.   src.string_ = temptr;
  72.   src.size_ = tempsize;
  73.   src.capacity_ = tempcapacity;
  74. }
  75. void String::PushBack(char character) {
  76.   if (size_ == capacity_) {
  77.     const size_t kCap = capacity_ + capacity_ + 100;
  78.     Reserve(kCap);
  79.     string_[size_] = character;
  80.     size_++;
  81.     string_[size_] = '\0';
  82.   } else {
  83.     string_[size_] = character;
  84.     size_++;
  85.     string_[size_] = '\0';
  86.   }
  87. }
  88. void String::Resize(size_t new_size) {
  89.   if (new_size == capacity_) {
  90.     return;
  91.   }
  92.   char* temp = new char[new_size + 1];
  93.   size_t edge = size_;
  94.   if (size_ > new_size) {
  95.     edge = new_size;
  96.   }
  97.   const size_t kEdge = edge;
  98.   for (size_t i = 0; i < kEdge; ++i) {
  99.     temp[i] = string_[i];
  100.   }
  101.   temp[kEdge] = '\0';
  102.   capacity_ = new_size;
  103.   delete[] string_;
  104.   string_ = temp;
  105.   size_ = new_size;
  106. }
  107. void String::Resize(size_t new_size, char character) {  // here
  108.   if (new_size == capacity_) {
  109.     return;
  110.   }
  111.   char* temp = new char[new_size + 1];
  112.   if (string_ != nullptr) {
  113.     size_t edge = size_;
  114.     if (size_ > new_size) {
  115.       edge = new_size;
  116.     }
  117.     const size_t kEdge = edge;
  118.     for (size_t i = 0; i < kEdge; ++i) {
  119.       temp[i] = string_[i];
  120.     }
  121.     temp[kEdge] = '\0';
  122.   }
  123.   capacity_ = new_size;
  124.   delete[] string_;
  125.   string_ = temp;
  126.   size_ = new_size;
  127.   const size_t kOldend = strlen(string_) - 1;
  128.   for (size_t i = kOldend; i < new_size; ++i) {
  129.     string_[i] = character;
  130.   }
  131.   string_[new_size] = '\0';
  132. }
  133. void String::PopBack() {
  134.   if (!Empty()) {
  135.     Back() = '\0';
  136.     string_[size_] = 0;
  137.     size_--;
  138.   }
  139. }
  140. String::String() {
  141.   size_ = static_cast<unsigned long>(0);
  142.   capacity_ = static_cast<unsigned long>(0);
  143.   string_ = nullptr;
  144. }
  145. String::~String() { delete[] string_; }
  146. unsigned long String::Size() const { return size_; }
  147. unsigned long& String::Size() { return size_; }
  148. unsigned long& String::Capacity() { return capacity_; }
  149. unsigned long String::Capacity() const { return capacity_; }
  150. const char* String::Data() { return const_cast<const char*>(string_); }
  151. const char* String::Data() const { return const_cast<const char*>(string_); }
  152. bool String::Empty() const { return size_ == 0; }
  153. char& String::operator[](size_t index) { return string_[index]; }
  154. const char& String::operator[](size_t index) const { return string_[index]; }
  155. char& String::Front() { return string_[0]; }
  156. const char& String::Front() const { return string_[0]; }
  157. const char& String::Back() const { return string_[strlen(string_) - 1]; }
  158. char& String::Back() { return string_[strlen(string_) - 1]; }
  159. void String::Clear() {
  160.   size_ = 0;
  161.   // capacity_ = 0;
  162.   // string_ = {0};
  163. }
  164. void String::ShrinkToFit() {
  165.   if (size_ < capacity_) {
  166.     const size_t kCap = size_;
  167.     char* temp = new char[kCap + 1];
  168.     if (string_ != nullptr) {
  169.       // memcpy(temp, string_, kCap);
  170.       for (size_t i = 0; i < kCap; ++i) {
  171.         temp[i] = string_[i];
  172.       }
  173.     }
  174.     temp[kCap] = '\0';
  175.     capacity_ = kCap;
  176.     delete[] string_;
  177.     string_ = temp;
  178.     size_ = kCap;
  179.   }
  180. }
  181. void String::Reserve(size_t new_cap) {
  182.   if (capacity_ < new_cap) {
  183.     char* temp = new char[new_cap + 1];
  184.     if (string_ != nullptr) {
  185.       // memcpy(temp, string_, strlen(string_));
  186.       const size_t kEdge = strlen(string_);
  187.       for (size_t i = 0; i < kEdge; ++i) {
  188.         temp[i] = string_[i];
  189.       }
  190.       temp[kEdge] = '\0';
  191.     }
  192.     delete[] string_;
  193.     string_ = temp;
  194.     capacity_ = new_cap;
  195.   }
  196. }
  197. std::ostream& operator<<(std::ostream& os, const String& str) {
  198.   if (str.string_ == nullptr) {
  199.     return os;
  200.   }
  201.   os << str.string_;
  202.   return os;
  203. }
  204. std::istream& operator>>(std::istream& is, String& str) {
  205.   char symbol = 0;
  206.   while (is.get(symbol)) {
  207.     str.PushBack(symbol);
  208.   }
  209.   return is;
  210. }
  211. String String::operator+(const String& other) const {
  212.   String temp = *this;
  213.   temp += other;
  214.   return temp;
  215. }
  216. String& String::operator+=(const String& other) {
  217.   if (capacity_ < size_ + other.size_) {
  218.     Reserve(size_ + other.size_);
  219.   }
  220.   for (size_t i = 0; i < other.size_; ++i) {
  221.     PushBack(other[i]);
  222.   }
  223.   return *this;
  224. }
  225. String& String::operator*=(size_t n) {
  226.   if (n == 0) {
  227.     Clear();
  228.     return *this;
  229.   }
  230.   Reserve(size_ * n + 1);
  231.   String copy = *this;
  232.   for (size_t i = 0; i < n - 1; ++i) {
  233.     *this += copy;
  234.   }
  235.   // string_[size_] = '\0';
  236.   return *this;
  237. }
  238. String String::operator*(size_t n) const {
  239.   String copy = *this;
  240.   return copy *= n;
  241. }
  242. String String::Join(const std::vector<String>& strings) const {
  243.   String result;
  244.   // result += *this;
  245.   for (size_t i = 0; i < strings.size(); ++i) {
  246.     result += strings[i];
  247.     if (i < strings.size() - 1) {
  248.       result += *this;
  249.     }
  250.   }
  251.   return result;
  252. }
  253. std::vector<String> String::Split(const char* separator) const {
  254.   std::vector<String> result;
  255.   String temp;
  256.   for (size_t i = 0; i < size_; ++i) {
  257.     if (string_[i] == separator[0]) {
  258.       String check;
  259.       for (size_t j = i; j < strlen(separator) + i; ++j) {
  260.         check.PushBack(string_[j]);
  261.       }
  262.       if (check.Empty() && strlen(separator) == 0) {
  263.         temp.ShrinkToFit();
  264.         result.push_back(temp);
  265.         temp.Free();
  266.       } else if (strcmp(check.Data(), separator) == 0) {
  267.         temp.ShrinkToFit();
  268.         result.push_back(temp);
  269.         temp.Free();
  270.       } else if (strcmp(check.Data(), separator) != 0) {
  271.         for (size_t k = i; k < i + strlen(separator); ++k) {
  272.           temp.PushBack(string_[k]);
  273.         }
  274.       }
  275.       i += strlen(separator) - 1;
  276.     } else {
  277.       temp.PushBack(string_[i]);
  278.     }
  279.   }
  280.   temp.ShrinkToFit();
  281.   result.push_back(temp);
  282.   return result;
  283. }
  284. void String::Free() { size_ = 0; }
  285. bool String::operator>=(const String& other) const {
  286.   return strcmp(const_cast<const char*>(string_),
  287.                 const_cast<const char*>(other.string_)) >= 0;
  288. }
  289. bool String::operator>(const String& other) const {
  290.   return strcmp(const_cast<const char*>(string_),
  291.                 const_cast<const char*>(other.string_)) > 0;
  292. }
  293. bool String::operator<(const String& other) const {
  294.   return strcmp(const_cast<const char*>(string_),
  295.                 const_cast<const char*>(other.string_)) < 0;
  296. }
  297. bool String::operator!=(const String& other) const {
  298.   return strcmp(const_cast<const char*>(string_),
  299.                 const_cast<const char*>(other.string_)) != 0;
  300. }
  301. bool String::operator==(const String& other) const {
  302.   if (size_ == 0 && other.size_ == 0) {
  303.     return true;
  304.   }
  305.   return strcmp(const_cast<const char*>(string_),
  306.                 const_cast<const char*>(other.string_)) == 0;
  307. }
  308. bool String::operator<=(const String& other) const {
  309.   return strcmp(const_cast<const char*>(string_),
  310.                 const_cast<const char*>(other.string_)) <= 0;
  311. }
  312. int main() {
  313.   String s;
  314.   s.Size() = -1;
  315.   s.Capacity() = -1;
  316.   if (s.Size() > 0) {
  317.     std::cout << "ok\n";
  318.   }
  319.   if (s.Capacity() > 0) {
  320.     std::cout << "ok\n";
  321.   }
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement