Advertisement
avr39ripe

PV913StringHandmade

Jul 1st, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class String
  4. {
  5.     uint32_t size;
  6.     uint32_t capacity;
  7.     char* str;
  8. public:
  9.     String(const char* strP)
  10.         : size{ strP ? strlen(strP) : 0 }, capacity{ size + 1 }, str{ new char[capacity] }
  11.     {
  12.         if (strP)
  13.         {
  14.             strcpy_s(str, capacity, strP);
  15.         }
  16.         else
  17.         {
  18.             *str = '\0';
  19.         }
  20.     };
  21.  
  22.     String(uint32_t reserveSize)
  23.         : size{ 0 }, capacity{ reserveSize }, str{new char[reserveSize]}
  24.     {
  25.         *str = '\0';
  26.     };
  27.  
  28.     String(const String& stringP);
  29.     String& operator=(const String& stringP);
  30.     String& operator=(const char* strP);
  31.     uint32_t length()const { return size; };
  32.     uint32_t getCapacity()const { return capacity; };
  33.     const char* c_str()const { return str; };
  34.     String& reserve(uint32_t reserveSize);
  35.     String& shrinkToFit();
  36.     friend std::ostream& operator<<(std::ostream& out, const String& lhs);
  37.     ~String() { delete[] str; };
  38. };
  39.  
  40. String::String(const String& stringP)
  41.     :size{ stringP.size }, capacity{ size + 1 }, str{ new char[capacity] }
  42. {
  43.     strcpy_s(str, capacity, stringP.str);
  44. };
  45.  
  46. String& String::operator=(const String& stringP)
  47. {
  48.     if (this != &stringP)
  49.     {
  50.         size = stringP.size;
  51.  
  52.         if (size + 1 > capacity)
  53.         {
  54.             delete[] str;
  55.             capacity = size + 1;
  56.             str = new char[capacity];
  57.         }
  58.  
  59.         strcpy_s(str, capacity, stringP.str);
  60.     }
  61.     return *this;
  62. }
  63.  
  64. String& String::operator=(const char* strP)
  65. {
  66.     size = strlen(strP);
  67.  
  68.     if (size + 1 > capacity)
  69.     {
  70.         delete[] str;
  71.         capacity = size + 1;
  72.         str = new char[capacity];
  73.     }
  74.  
  75.     strcpy_s(str, capacity, strP);
  76.  
  77.     return *this;
  78. }
  79.  
  80. String& String::reserve(uint32_t reserveSize)
  81. {
  82.  
  83.     if (str and (reserveSize > capacity))
  84.     {
  85.         char* newStr{ new char[reserveSize] };
  86.         strcpy_s(newStr, capacity, str);
  87.         delete[] str;
  88.         str = newStr;
  89.         capacity = reserveSize;
  90.     }
  91.     return *this;
  92. }
  93.  
  94. String& String::shrinkToFit()
  95. {
  96.     if (capacity > size + 1)
  97.     {
  98.         capacity = size + 1;
  99.         char* newStr{ new char[capacity] };
  100.         strcpy_s(newStr, capacity, str);
  101.         delete[] str;
  102.         str = newStr;
  103.     }
  104.     return *this;
  105. }
  106.  
  107. std::ostream& operator<<(std::ostream& out, const String& lhs)
  108. {
  109.     out << lhs.str;
  110.     return out;
  111. }
  112.  
  113. int main()
  114. {
  115.     char str[1024]{ "Hello!" };
  116.  
  117.     String str1{ "First string!" };
  118.     std::cout << " str1 = " << str1
  119.         << " str1.length = " << str1.length()
  120.         << " str1.capacity = " << str1.getCapacity() << '\n';
  121.  
  122.     std::cout << '\n';
  123.  
  124.     String str2{""};
  125.     std::cout << " str2 = " << str2
  126.         << " str2.length = " << str2.length()
  127.         << " str2.capacity = " << str2.getCapacity() << '\n';
  128.  
  129.     std::cout << '\n';
  130.  
  131.     str2 = "Assign test! :)";
  132.     str2.reserve(100);
  133.     str2.reserve(100);
  134.     str2 = "Some really, really, REALLLLY long string!";
  135.     str2.shrinkToFit();
  136.     str2.shrinkToFit();
  137.  
  138.     std::cout << " str2 = " << str2
  139.         << " str2.length = " << str2.length()
  140.         << " str2.capacity = " << str2.getCapacity() << '\n';
  141.  
  142.     std::cout << '\n';
  143.  
  144.     String str3{ 5 };
  145.     str3 = "Hi!";
  146.     std::cout << " str3 = " << str3
  147.         << " str3.length = " << str3.length()
  148.         << " str3.capacity = " << str3.getCapacity() << '\n';
  149.  
  150.     String str4{""};
  151.     str4 = str3;
  152.     std::cout << " str4 = " << str4
  153.         << " str4.length = " << str4.length()
  154.         << " str4.capacity = " << str4.getCapacity() << '\n';
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement