simple_string.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <cstring>
5 #include <stdexcept>
6 #include <limits>
7 #include <cstdint>
8 #include <string_view>
9 
10 namespace SafeAny
11 {
12 
13 // Read only version of String that has size 16 bytes and can store
14 // in-place strings with size up to 15 bytes.
15 
16 // Inspired by https://github.com/elliotgoodrich/SSO-23
17 
19 {
20 public:
21  SimpleString(const std::string& str) : SimpleString(str.data(), str.size())
22  {}
23 
25  {}
26 
27  SimpleString(const SimpleString& other) : SimpleString(other.data(), other.size())
28  {}
29 
31  {
32  this->~SimpleString();
33  createImpl(other.data(), other.size());
34  return *this;
35  }
36 
37  SimpleString(SimpleString&& other) : SimpleString(nullptr, 0)
38  {
39  std::swap(_storage, other._storage);
40  }
41 
43  {
44  this->~SimpleString();
45 
46  std::swap(_storage, other._storage);
47  return *this;
48  }
49 
50  SimpleString(const char* input_data) : SimpleString(input_data, strlen(input_data))
51  {}
52 
53  SimpleString(const char* input_data, std::size_t size)
54  {
55  createImpl(input_data, size);
56  }
57 
59  {
60  if(!isSOO())
61  {
62  delete[] _storage.str.data;
63  }
64  _storage.soo.capacity_left = CAPACITY;
65  }
66 
67  std::string toStdString() const
68  {
69  return size() > 0 ? std::string(data(), size()) : std::string();
70  }
72  {
73  return size() > 0 ? std::string_view(data(), size()) : std::string_view();
74  }
75 
76  const char* data() const
77  {
78  if(isSOO())
79  {
80  return _storage.soo.data;
81  }
82  else
83  {
84  return _storage.str.data;
85  }
86  }
87 
88  std::size_t size() const
89  {
90  if(isSOO())
91  {
92  return CAPACITY - _storage.soo.capacity_left;
93  }
94  else
95  {
96  return _storage.str.size & LONG_MASK;
97  }
98  }
99 
100  bool operator==(const SimpleString& other) const
101  {
102  size_t N = size();
103  return other.size() == N && std::strncmp(data(), other.data(), N) == 0;
104  }
105 
106  bool operator!=(const SimpleString& other) const
107  {
108  size_t N = size();
109  return other.size() != N || std::strncmp(data(), other.data(), N) != 0;
110  }
111 
112  bool operator<=(const SimpleString& other) const
113  {
114  return std::strcmp(data(), other.data()) <= 0;
115  }
116 
117  bool operator>=(const SimpleString& other) const
118  {
119  return std::strcmp(data(), other.data()) >= 0;
120  }
121 
122  bool operator<(const SimpleString& other) const
123  {
124  return std::strcmp(data(), other.data()) < 0;
125  }
126 
127  bool operator>(const SimpleString& other) const
128  {
129  return std::strcmp(data(), other.data()) > 0;
130  }
131 
132  bool isSOO() const
133  {
134  return !(_storage.soo.capacity_left & IS_LONG_BIT);
135  }
136 
137 private:
138  struct String
139  {
140  char* data;
141  std::size_t size;
142  };
143 
144  constexpr static std::size_t CAPACITY = 15; // sizeof(String) - 1);
145  constexpr static std::size_t IS_LONG_BIT = 1 << 7;
146  constexpr static std::size_t LONG_MASK = (~std::size_t(0)) >> 1;
147  constexpr static std::size_t MAX_SIZE = 100UL * 1024UL * 1024UL;
148 
149  union
150  {
152 
153  struct SOO
154  {
155  char data[CAPACITY];
156  uint8_t capacity_left;
157  } soo;
158  } _storage;
159 
160 private:
161  void createImpl(const char* input_data, std::size_t size)
162  {
163  if(size > MAX_SIZE)
164  {
165  throw std::invalid_argument("size too large for a simple string");
166  }
167 
168  if(size > CAPACITY)
169  {
170  _storage.str.size = size;
171  _storage.soo.capacity_left = IS_LONG_BIT;
172  _storage.str.data = new char[size + 1];
173  std::memcpy(_storage.str.data, input_data, size);
174  _storage.str.data[size] = '\0';
175  }
176  else
177  {
178  _storage.soo.capacity_left = uint8_t(CAPACITY - size);
179  if(size > 0)
180  {
181  std::memcpy(_storage.soo.data, input_data, size);
182  }
183  if(size < CAPACITY)
184  {
185  _storage.soo.data[size] = '\0';
186  }
187  }
188  }
189 };
190 
191 } // namespace SafeAny
SafeAny::SimpleString::SimpleString
SimpleString(const std::string_view &str)
Definition: simple_string.hpp:24
SafeAny::SimpleString::data
const char * data() const
Definition: simple_string.hpp:76
SafeAny
Definition: convert_impl.hpp:21
SafeAny::SimpleString::soo
struct SafeAny::SimpleString::@15::SOO soo
SafeAny::SimpleString::operator=
SimpleString & operator=(SimpleString &&other)
Definition: simple_string.hpp:42
SafeAny::SimpleString::operator<=
bool operator<=(const SimpleString &other) const
Definition: simple_string.hpp:112
SafeAny::SimpleString::SimpleString
SimpleString(const char *input_data, std::size_t size)
Definition: simple_string.hpp:53
SafeAny::SimpleString::size
std::size_t size() const
Definition: simple_string.hpp:88
SafeAny::SimpleString::~SimpleString
~SimpleString()
Definition: simple_string.hpp:58
SafeAny::SimpleString::createImpl
void createImpl(const char *input_data, std::size_t size)
Definition: simple_string.hpp:161
SafeAny::SimpleString::capacity_left
uint8_t capacity_left
Definition: simple_string.hpp:156
SafeAny::SimpleString::String
Definition: simple_string.hpp:138
SafeAny::SimpleString::SimpleString
SimpleString(const char *input_data)
Definition: simple_string.hpp:50
SafeAny::SimpleString::MAX_SIZE
constexpr static std::size_t MAX_SIZE
Definition: simple_string.hpp:147
SafeAny::SimpleString::toStdString
std::string toStdString() const
Definition: simple_string.hpp:67
SafeAny::SimpleString::operator=
SimpleString & operator=(const SimpleString &other)
Definition: simple_string.hpp:30
SafeAny::SimpleString::SimpleString
SimpleString(const std::string &str)
Definition: simple_string.hpp:21
SafeAny::SimpleString::SimpleString
SimpleString(SimpleString &&other)
Definition: simple_string.hpp:37
SafeAny::SimpleString::operator<
bool operator<(const SimpleString &other) const
Definition: simple_string.hpp:122
SafeAny::SimpleString::toStdStringView
std::string_view toStdStringView() const
Definition: simple_string.hpp:71
SafeAny::SimpleString
Definition: simple_string.hpp:18
SafeAny::SimpleString::operator>
bool operator>(const SimpleString &other) const
Definition: simple_string.hpp:127
SafeAny::SimpleString::operator>=
bool operator>=(const SimpleString &other) const
Definition: simple_string.hpp:117
SafeAny::SimpleString::LONG_MASK
constexpr static std::size_t LONG_MASK
Definition: simple_string.hpp:146
SafeAny::SimpleString::IS_LONG_BIT
constexpr static std::size_t IS_LONG_BIT
Definition: simple_string.hpp:145
SafeAny::SimpleString::SimpleString
SimpleString(const SimpleString &other)
Definition: simple_string.hpp:27
SafeAny::SimpleString::CAPACITY
constexpr static std::size_t CAPACITY
Definition: simple_string.hpp:144
std
Definition: std.hpp:31
SafeAny::SimpleString::operator==
bool operator==(const SimpleString &other) const
Definition: simple_string.hpp:100
SafeAny::SimpleString::String::size
std::size_t size
Definition: simple_string.hpp:141
std::swap
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression, cppcoreguidelines-noexcept-swap, performance-noexcept-swap) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition: json.hpp:24537
SafeAny::SimpleString::_storage
union SafeAny::SimpleString::@15 _storage
SafeAny::SimpleString::operator!=
bool operator!=(const SimpleString &other) const
Definition: simple_string.hpp:106
SafeAny::SimpleString::str
String str
Definition: simple_string.hpp:151
SafeAny::SimpleString::data
char data[CAPACITY]
Definition: simple_string.hpp:155
lexy::_detail::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:192
SafeAny::SimpleString::String::data
char * data
Definition: simple_string.hpp:140
SafeAny::SimpleString::isSOO
bool isSOO() const
Definition: simple_string.hpp:132


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:17