Advertisement
filashkov

Untitled

Apr 19th, 2021
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. enum
  5. {
  6.     EXP_VALUE = 2
  7. };
  8.  
  9.  
  10.  
  11. class String
  12. {
  13.     class Buffer;
  14.     Buffer* buffer = nullptr;
  15. public:
  16.     String(const char* s = nullptr)
  17.     {
  18.         size_t len = strlen(s);
  19.         if (len != 0)
  20.         {
  21.  
  22.         }
  23.     }
  24.     String(const String& copy_string)
  25.     {
  26.  
  27.     }
  28.     ~String()
  29.     {
  30.  
  31.     }
  32.    
  33.     String& operator= (const String& assign_string)
  34.     {
  35.  
  36.     }
  37.  
  38.     String& operator+= (const String& concat_string)
  39.     {
  40.  
  41.     }
  42.  
  43.     char& operator[] (const size_t index)
  44.     {
  45.         return buffer->get_index(index);
  46.     }
  47.  
  48.     char operator[] (const size_t index) const
  49.     {
  50.  
  51.     }
  52. };
  53.  
  54. class String::Buffer
  55. {
  56.     void* owner_address = nullptr;
  57.     char* data = nullptr;
  58.     size_t length = 0;
  59.     size_t capacity = 0;
  60.     void make_correct_for_length(size_t required_length)
  61.     {
  62.         if (required_length >= capacity)
  63.         {
  64.             capacity = EXP_VALUE * capacity + 1;
  65.             char* new_data = new char[capacity];
  66.             for (size_t i = 0; i < length; i++)
  67.             {
  68.                 new_data[i] = data[i];
  69.             }
  70.             delete[] data;
  71.             data = new_data;
  72.         }
  73.     }
  74. public:
  75.     void add(const char* text, size_t len)
  76.     {
  77.         make_correct_for_length(length + len);
  78.         for (size_t i = 0; i < len; i++)
  79.         {
  80.             data[length + i] = text[i];
  81.         }
  82.     }
  83.     char& get_index(size_t index) const
  84.     {
  85.         return data[index];
  86.     }
  87.     Buffer() {};
  88.     ~Buffer()
  89.     {
  90.         if (length != 0)
  91.         {
  92.             delete[] data;
  93.         }
  94.     }
  95. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement