31template<
size_t SSO_SIZE = 256>
48 assign(str, std::strlen(str));
56 assign(str.data(), str.size());
63 assign(str.data(), str.size());
71 std::memcpy(data_.small, other.data_.small, other.size_ + 1);
75 assign(other.data(), other.size());
89 other.data_.heap.ptr =
nullptr;
91 other.is_small_ =
true;
108 if (
this != &other) {
118 if (
this != &other) {
132 other.data_.heap.ptr =
nullptr;
134 other.is_small_ =
true;
143 void assign(
const char* str,
size_t len) {
176 const char*
c_str() const noexcept {
183 const char*
data() const noexcept {
249 char* new_ptr =
new char[actual_capacity];
267 void append(
const char* str,
size_t len) {
268 size_t new_size =
size_ + len;
280 char* new_ptr =
new char[new_capacity];
285 std::memcpy(new_ptr +
size_, str, len);
286 new_ptr[new_size] =
'\0';
309 append(str.data(), str.size());
316 append(str, std::strlen(str));
338 operator std::string_view() const noexcept {
346 return size_ == other.size_ &&
347 std::memcmp(
data(), other.data(),
size_) == 0;
354 return !(*
this == other);
361 return size_ == str.size() &&
362 std::memcmp(
data(), str.data(),
size_) == 0;
Small String Optimization (SSO) implementation.
small_string(const char *str)
Construct from C-string.
bool operator!=(const small_string &other) const noexcept
Inequality comparison.
small_string(small_string &&other) noexcept
Move constructor.
small_string & operator+=(const char *str)
Append operator.
void reserve(size_t new_capacity)
Reserve capacity.
small_string & operator=(const small_string &other)
Copy assignment.
const char * c_str() const noexcept
Get C-string pointer.
void assign(const char *str, size_t len)
Assign from string data.
bool operator==(const small_string &other) const noexcept
Equality comparison.
bool operator==(const std::string &str) const noexcept
Comparison with std::string.
small_string(const small_string &other)
Copy constructor.
union kcenon::logger::small_string::data_union data_
const char * data() const noexcept
Get data pointer.
bool empty() const noexcept
Check if empty.
static size_t calculate_capacity(size_t required)
Calculate capacity for heap allocation.
void clear() noexcept
Clear the string.
size_t size() const noexcept
Get size.
memory_stats get_memory_stats() const noexcept
bool is_small() const noexcept
Check if using small string optimization.
void append(const std::string &str)
Append a string.
size_t capacity() const noexcept
Get capacity.
small_string & operator+=(const std::string &str)
Append operator.
small_string(const std::string &str)
Construct from std::string.
size_t length() const noexcept
Get length (same as size)
~small_string()
Destructor.
small_string(std::string_view str)
Construct from string_view.
small_string & operator=(small_string &&other) noexcept
Move assignment.
std::string to_string() const
Convert to std::string.
void append(const char *str, size_t len)
Append a string.
small_string() noexcept
Default constructor.
static constexpr size_t SSO_CAPACITY
Get memory usage statistics.
struct kcenon::logger::small_string::data_union::@0 heap