| 16 | // Inspired by https://github.com/elliotgoodrich/SSO-23 |
| 17 | |
| 18 | class SimpleString |
| 19 | { |
| 20 | public: |
| 21 | SimpleString() |
| 22 | { |
| 23 | _storage.soo.capacity_left = CAPACITY; |
| 24 | _storage.soo.data[0] = '\0'; |
| 25 | } |
| 26 | |
| 27 | SimpleString(const std::string& str) : SimpleString(str.data(), str.size()) |
| 28 | {} |
| 29 | |
| 30 | SimpleString(const std::string_view& str) : SimpleString(str.data(), str.size()) |
| 31 | {} |
| 32 | |
| 33 | SimpleString(const SimpleString& other) : SimpleString(other.data(), other.size()) |
| 34 | {} |
| 35 | |
| 36 | SimpleString& operator=(const SimpleString& other) |
| 37 | { |
| 38 | if(this != &other) |
| 39 | { |
| 40 | this->~SimpleString(); |
| 41 | createImpl(other.data(), other.size()); |
| 42 | } |
| 43 | return *this; |
| 44 | } |
| 45 | |
| 46 | SimpleString(SimpleString&& other) noexcept : SimpleString() |
| 47 | { |
| 48 | std::swap(_storage, other._storage); |
| 49 | } |
| 50 | |
| 51 | SimpleString& operator=(SimpleString&& other) noexcept |
| 52 | { |
| 53 | if(this != &other) |
| 54 | { |
| 55 | std::swap(_storage, other._storage); |
| 56 | } |
| 57 | return *this; |
| 58 | } |
| 59 | |
| 60 | SimpleString(const char* input_data) : SimpleString(input_data, strlen(input_data)) |
| 61 | {} |
| 62 | |
| 63 | SimpleString(const char* input_data, std::size_t size) |
| 64 | { |
| 65 | createImpl(input_data, size); |
| 66 | } |
| 67 | |
| 68 | ~SimpleString() |
| 69 | { |
| 70 | if(!isSOO()) |
| 71 | { |
| 72 | delete[] _storage.str.data; |
| 73 | } |
| 74 | _storage.soo.capacity_left = CAPACITY; |
| 75 | } |