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();
113 return other.size() == N && std::strncmp(data(), other.data(), N) == 0;
118 const size_t N = size();
119 return other.size() != N || std::strncmp(data(), other.data(), N) != 0;
124 return !(*
this > other);
129 return !(*
this < other);
134 const size_t min_size = std::min(size(), other.size());
135 int cmp = std::memcmp(data(), other.data(), min_size);
140 return size() < other.size();
145 const size_t min_size = std::min(size(), other.size());
146 int cmp = std::memcmp(data(), other.data(), min_size);
151 return size() > other.size();
156 return (_storage.soo.capacity_left & IS_LONG_BIT) == 0;
162 char* data =
nullptr;
163 std::size_t size = 0;
166 constexpr static std::size_t CAPACITY = 15;
167 constexpr static std::size_t IS_LONG_BIT = 1 << 7;
168 constexpr static std::size_t LONG_MASK = (~std::size_t(0)) >> 1;
169 constexpr static std::size_t MAX_SIZE = 100UL * 1024UL * 1024UL;
178 uint8_t capacity_left = CAPACITY;
183 void createImpl(
const char* input_data, std::size_t size)
187 throw std::invalid_argument(
"size too large for a simple string");
192 _storage.str.size = size;
193 _storage.soo.capacity_left = IS_LONG_BIT;
194 _storage.str.data =
new char[size + 1];
195 std::memcpy(_storage.str.data, input_data, size);
196 _storage.str.data[size] =
'\0';
200 _storage.soo.capacity_left = uint8_t(CAPACITY - size);
203 std::memcpy(_storage.soo.data, input_data, size);
207 _storage.soo.data[size] =
'\0';
Definition: simple_string.hpp:19