37class bytestring_view {
40 using reference =
const char&;
41 using value_type =
const char;
42 using iterator =
const char*;
43 using const_iterator =
const char*;
49 bytestring_view(
const BS& data) ZEN_NOEXCEPT:
50 ptr(data.ptr), sz(data.sz) {}
52 bool operator==(
const char* other)
const ZEN_NOEXCEPT {
53 return string_equal_helper(ptr, other, sz);
56 template<std::
size_t N>
61 for (
auto [c1, c2]:
zip(*
this, other)) {
71 bool operator!=(
const T& other)
const ZEN_NOEXCEPT {
72 return !(*
this == other);
75 const char& operator[](std::size_t index)
const ZEN_NOEXCEPT {
79 iterator begin() ZEN_NOEXCEPT {
83 iterator end() ZEN_NOEXCEPT {
87 const_iterator begin()
const ZEN_NOEXCEPT {
91 const_iterator end()
const ZEN_NOEXCEPT {
95 const_iterator cbegin()
const ZEN_NOEXCEPT {
99 const_iterator cend()
const ZEN_NOEXCEPT {
113class basic_bytestring {
115 friend class bytestring_view;
124 using reference =
char&;
125 using value_type = char;
126 using iterator =
char*;
127 using const_iterator =
const char*;
128 using view = bytestring_view;
130 basic_bytestring(std::size_t max_sz) ZEN_NOEXCEPT:
132 ptr =
static_cast<char*
>(malloc(max_sz));
133 if (ptr ==
nullptr) {
134 ZEN_PANIC(
"insufficient memory");
139 basic_bytestring() ZEN_NOEXCEPT:
140 basic_bytestring(N) {}
142 basic_bytestring(
const char*
const other, std::size_t other_sz) ZEN_NOEXCEPT:
143 basic_bytestring(other_sz) {
145 memcpy(ptr, other, sz);
148 basic_bytestring(
const char*
const other) ZEN_NOEXCEPT:
149 basic_bytestring(other, strlen(other)) {}
151 basic_bytestring(
const std::string& str) ZEN_NOEXCEPT:
152 basic_bytestring(str.c_str(), str.size()) {}
154 basic_bytestring(
const basic_bytestring& other) ZEN_NOEXCEPT:
155 basic_bytestring(other.ptr, other.sz) {}
157 basic_bytestring(basic_bytestring&& other) ZEN_NOEXCEPT:
158 ptr(std::move(other.ptr)),
159 max_sz(std::move(other.max_sz)),
160 sz(std::move(other.sz)) {
164 iterator begin() ZEN_NOEXCEPT {
168 iterator end() ZEN_NOEXCEPT {
172 const_iterator begin()
const ZEN_NOEXCEPT {
176 const_iterator end()
const ZEN_NOEXCEPT {
180 const_iterator cbegin()
const ZEN_NOEXCEPT {
184 const_iterator cend()
const ZEN_NOEXCEPT {
188 bool operator==(
const char* other)
const ZEN_NOEXCEPT {
189 return string_equal_helper(ptr, other, sz);
192 template<std::
size_t N2>
193 bool operator==(
const basic_bytestring<N2>& other)
const ZEN_NOEXCEPT {
194 if (sz != other.sz) {
197 for (std::size_t i = 0; i < sz; ++i) {
198 if (ptr[i] != other.ptr[i]) {
205 char& operator[](std::size_t index) ZEN_NOEXCEPT {
209 const char& operator[](std::size_t index)
const ZEN_NOEXCEPT {
213 bytestring_view as_view()
const ZEN_NOEXCEPT {
214 return bytestring_view(*
this);
217 std::size_t capacity()
const ZEN_NOEXCEPT {
221 std::size_t size()
const ZEN_NOEXCEPT {
225 const char* c_str()
const ZEN_NOEXCEPT {
229 char* data()
const ZEN_NOEXCEPT {
233 std::string to_std_string()
const ZEN_NOEXCEPT {
234 return std::string(ptr, sz);
237 void resize(std::size_t new_sz) ZEN_NOEXCEPT{
238 ZEN_ASSERT(new_sz <= sz);
243 ~basic_bytestring() ZEN_NOEXCEPT {
244 if (ptr !=
nullptr) {