23 _storage.soo.capacity_left = CAPACITY;
24 _storage.soo.data[0] =
'\0';
27 SimpleString(
const std::string& str) : SimpleString(str.data(), str.size())
30 SimpleString(
const std::string_view& str) : SimpleString(str.data(), str.size())
41 createImpl(other.data(), other.size());
48 std::swap(_storage, other._storage);
55 std::swap(_storage, other._storage);
60 SimpleString(
const char* input_data) : SimpleString(input_data, strlen(input_data))
63 SimpleString(
const char* input_data, std::size_t size)
65 createImpl(input_data, size);
72 delete[] _storage.str.data;
74 _storage.soo.capacity_left = CAPACITY;
77 std::string toStdString()
const
79 return size() > 0 ? std::string(data(), size()) : std::string();
81 std::string_view toStdStringView()
const
83 return size() > 0 ? std::string_view(data(), size()) : std::string_view();
86 const char* data()
const
90 return _storage.soo.data;
94 return _storage.str.data;
98 std::size_t size()
const
102 return CAPACITY - _storage.soo.capacity_left;
106 return _storage.str.size & LONG_MASK;
112 const size_t N = size();
116 return other.size() == N && std::memcmp(data(), other.data(), N) == 0;
121 const size_t N = size();
122 return other.size() != N || std::memcmp(data(), other.data(), N) != 0;
127 return !(*
this > other);
132 return !(*
this < other);
137 const size_t min_size = std::min(size(), other.size());
138 int cmp = std::memcmp(data(), other.data(), min_size);
143 return size() < other.size();
148 const size_t min_size = std::min(size(), other.size());
149 int cmp = std::memcmp(data(), other.data(), min_size);
154 return size() > other.size();
159 return (_storage.soo.capacity_left & IS_LONG_BIT) == 0;
165 char* data =
nullptr;
166 std::size_t size = 0;
169 constexpr static std::size_t CAPACITY = 15;
170 constexpr static std::size_t IS_LONG_BIT = 1 << 7;
171 constexpr static std::size_t LONG_MASK = (~std::size_t(0)) >> 1;
172 constexpr static std::size_t MAX_SIZE = 100UL * 1024UL * 1024UL;
181 uint8_t capacity_left = CAPACITY;
186 void createImpl(
const char* input_data, std::size_t size)
190 throw std::invalid_argument(
"size too large for a simple string");
195 _storage.str.size = size;
196 _storage.soo.capacity_left = IS_LONG_BIT;
197 _storage.str.data =
new char[size + 1];
198 std::memcpy(_storage.str.data, input_data, size);
199 _storage.str.data[size] =
'\0';
203 _storage.soo.capacity_left = uint8_t(CAPACITY - size);
206 std::memcpy(_storage.soo.data, input_data, size);
210 _storage.soo.data[size] =
'\0';
Definition: simple_string.hpp:19