24#include <initializer_list>
45 std::size_t current_size, std::size_t additional_size) {
46 std::size_t next_size = current_size;
49 if (next_size <= current_size) {
51 const std::size_t max_size =
52 std::numeric_limits<std::size_t>::max();
53 if (max_size - current_size >= additional_size) {
56 throw std::bad_alloc();
58 if (next_size - current_size >= additional_size) {
109 [[nodiscard]]
const unsigned char*
data() const noexcept {
return data_; }
116 [[nodiscard]] std::size_t
size() const noexcept {
return size_; }
195 explicit binary(std::string_view data_string)
197 constexpr std::string_view hex_digits =
"0123456789ABCDEF";
198 unsigned int byte = 0U;
199 bool is_first_digit =
true;
200 std::size_t current_byte_index = 0U;
201 for (
char digit : data_string) {
202 const auto pos = hex_digits.find(digit);
203 if (pos == std::string_view::npos) {
204 throw std::invalid_argument(
"Invalid hex expression.");
206 if (is_first_digit) {
207 byte =
static_cast<unsigned int>(pos);
208 is_first_digit =
false;
211 byte |=
static_cast<unsigned int>(pos);
212 buffer_.data()[current_byte_index] =
213 static_cast<unsigned char>(byte);
214 ++current_byte_index;
215 is_first_digit =
true;
218 if (!is_first_digit) {
219 throw std::invalid_argument(
"Invalid hex expression.");
258 const std::size_t current_size =
size_;
259 const std::size_t remaining_capacity =
buffer_.size() -
size_;
260 if (remaining_capacity <
size) {
285 [[nodiscard]]
unsigned char&
operator[](std::size_t index)
noexcept {
295 [[nodiscard]]
unsigned char operator[](std::size_t index)
const noexcept {
304 [[nodiscard]]
unsigned char*
data() noexcept {
return buffer_.data(); }
311 [[nodiscard]]
const unsigned char*
data() const noexcept {
320 [[nodiscard]] std::size_t
size() const noexcept {
return size_; }
327 [[nodiscard]] std::size_t
capacity() const noexcept {
339 return size_ == other.size_ &&
340 std::memcmp(
buffer_.data(), other.buffer_.data(),
size_) == 0;
373 return binary(lhs) += rhs;
398 return !(lhs == rhs);
409 constexpr std::string_view hex_digits =
"0123456789ABCDEF";
410 for (std::size_t i = 0; i < value.
size(); ++i) {
411 const unsigned int byte = value.
data()[i];
413 stream << hex_digits[(
byte >> 4U) & 0x0FU];
415 stream << hex_digits[
byte & 0x0FU];
Definition of basic_binary_buffer class.
Class to refer binary data.
std::size_t size() const noexcept
Get the size of the data.
const unsigned char * data() const noexcept
Get the pointer to the data.
binary_view() noexcept
Constructor.
binary_view(const unsigned char *data, std::size_t size) noexcept
Constructor.
const unsigned char * data_
Data.
binary(binary_view data)
Constructor.
void reserve(std::size_t size)
Change the size of the internal buffer.
unsigned char * data() noexcept
Get the pointer to the data.
binary & operator+=(const binary &other)
Append another binary data.
binary(const std::vector< unsigned char > &data)
Constructor.
binary(std::size_t size)
Constructor.
std::size_t size() const noexcept
Get the size of the data.
binary(const unsigned char *data, std::size_t size)
Constructor.
bool operator==(const binary &other) const noexcept
Compare with another instance.
void resize(std::size_t size)
Change the size of this data.
binary(std::initializer_list< unsigned char > data)
Constructor.
unsigned char & operator[](std::size_t index) noexcept
Access to a byte.
binary(std::string_view data_string)
Constructor.
unsigned char operator[](std::size_t index) const noexcept
Access to a byte.
bool operator!=(const binary &other) const noexcept
Compare with another instance.
const unsigned char * data() const noexcept
Get the pointer to the data.
details::basic_binary_buffer buffer_
Buffer.
void append(const unsigned char *data, std::size_t size)
Append another binary data.
std::size_t capacity() const noexcept
Get the size of the internal buffer for data.
Class of basic buffers for binary data.
Namespace of internal implementations.
constexpr std::size_t default_binary_capacity
Default of the capacity of the buffer in msgpack_light::binary class.
std::size_t calculate_expanded_memory_buffer_size(std::size_t current_size, std::size_t additional_size)
Calculate the size of an expanded buffer.
Namespace of this project.
bool operator==(binary_view lhs, binary_view rhs)
Compare two binary data.
binary operator+(const binary &lhs, const binary &rhs)
Connect two binary data.
bool operator!=(binary_view lhs, binary_view rhs)
Compare two binary data.
std::ostream & operator<<(std::ostream &stream, const binary &value)
Format a value to a stream.