23#ifndef BM_BM_SIM_BYTECONTAINER_H_
24#define BM_BM_SIM_BYTECONTAINER_H_
30#include <boost/functional/hash.hpp>
32#include "short_alloc.h"
40 static constexpr size_t S = 16u;
41 static_assert(
sizeof(char) == 1,
"");
42 static_assert(
alignof(char) == 1,
"");
43 using _vector = std::vector<char, detail::short_alloc<char, S, 1> >;
46 using iterator = _vector::iterator;
47 using const_iterator = _vector::const_iterator;
48 using reference = _vector::reference;
49 using const_reference = _vector::const_reference;
50 using size_type = size_t;
60 : bytes(nbytes, c, _a) { }
64 : bytes(bytes.
begin(), bytes.
end(), _a) { }
68 : bytes(bytes, bytes + nbytes, _a) { }
70 static char char2digit(
char c) {
71 if (c >=
'0' && c <=
'9')
73 if (c >=
'A' && c <=
'F')
74 return (c -
'A' + 10);
75 if (c >=
'a' && c <=
'f')
76 return (c -
'a' + 10);
88 assert(hexstring[idx] !=
'-');
90 if (hexstring[idx] ==
'0' && hexstring[idx + 1] ==
'x') {
93 size_t size = hexstring.size();
94 assert((
size - idx) > 0);
96 if ((
size - idx) % 2 != 0) {
97 char c = char2digit(hexstring[idx++]);
101 for (; idx <
size; ) {
102 char c = char2digit(hexstring[idx++]) << 4;
103 c += char2digit(hexstring[idx++]);
109 : bytes(other.bytes, _a) { }
111 ByteContainer &operator=(
const ByteContainer &other) {
112 bytes.assign(other.begin(), other.end());
117 size_type
size() const noexcept {
return bytes.size(); }
120 void clear() {
return bytes.clear(); }
125 iterator
begin() {
return bytes.begin(); }
128 const_iterator
begin()
const {
return bytes.begin(); }
131 iterator
end() {
return bytes.end(); }
134 const_iterator
end()
const {
return bytes.end(); }
161 bytes.insert(
end(), byte_array, byte_array + nbytes);
167 bytes.insert(
end(), other.begin(), other.end());
177 bytes.insert(pos, other.
begin(), other.
end());
212 return bytes.front();
217 return bytes.front();
228 const char*
data() const noexcept {
234 return bytes == other.bytes;
239 return !(*
this == other);
262 for (
size_t i = 0; i <
size(); i++)
268 std::string
to_hex(
size_t start,
size_t s,
bool upper_case =
false)
const;
271 std::string
to_hex(
bool upper_case =
false)
const {
276 _vector::allocator_type::arena_type _a;
280struct ByteContainerKeyHash {
281 std::size_t operator()(
const ByteContainer& b)
const {
283 return boost::hash_range(b.begin(), b.end());
Definition bytecontainer.h:39
void clear()
Clears the contents of the container.
Definition bytecontainer.h:120
ByteContainer & append(const char *byte_array, size_t nbytes)
Appends a byte array to this container.
Definition bytecontainer.h:160
ByteContainer & append(const std::string &other)
Appends a binary string to this container.
Definition bytecontainer.h:166
void resize(size_t n, char c)
Definition bytecontainer.h:254
char * data() noexcept
Definition bytecontainer.h:223
void apply_mask(const ByteContainer &mask)
Definition bytecontainer.h:260
ByteContainer(const size_t nbytes, const char c='\x00')
Constructs the container with nbytes copies of elements with value c.
Definition bytecontainer.h:59
bool operator==(const ByteContainer &other) const
Returns true is the contents of the containers are equal.
Definition bytecontainer.h:233
ByteContainer(const std::string &hexstring)
Definition bytecontainer.h:83
void insert(iterator pos, const ByteContainer &other)
Inserts another ByteContainer object into this container, before pos.
Definition bytecontainer.h:175
bool operator!=(const ByteContainer &other) const
Returns true is the contents of the containers are not equal.
Definition bytecontainer.h:238
ByteContainer(const std::vector< char > &bytes)
Constructs the container by copying the bytes in vector bytes.
Definition bytecontainer.h:63
size_type size() const noexcept
Returns the number of bytes in the container.
Definition bytecontainer.h:117
iterator begin()
NC.
Definition bytecontainer.h:125
const_iterator begin() const
NC.
Definition bytecontainer.h:128
ByteContainer & append(const ByteContainer &other)
Definition bytecontainer.h:154
void resize(size_t n)
Resizes the container to contain bytes.
Definition bytecontainer.h:248
std::string to_hex(bool upper_case=false) const
Returns the hexadecimal representation of the byte container as a string.
Definition bytecontainer.h:271
reference operator[](size_type n)
Definition bytecontainer.h:187
const char * data() const noexcept
Definition bytecontainer.h:228
reference back()
Definition bytecontainer.h:200
iterator end()
NC.
Definition bytecontainer.h:131
const_reference operator[](size_type n) const
Definition bytecontainer.h:193
const_reference front() const
Definition bytecontainer.h:216
const_reference back() const
Definition bytecontainer.h:205
ByteContainer(const char *bytes, size_t nbytes)
Constructs the container by copying the bytes in this byte array.
Definition bytecontainer.h:67
void reserve(size_t n)
Increase the capacity of the container.
Definition bytecontainer.h:243
reference front()
Definition bytecontainer.h:211
const_iterator end() const
NC.
Definition bytecontainer.h:134
void push_back(char c)
Appends a character at the end of the container.
Definition bytecontainer.h:181
std::string to_hex(size_t start, size_t s, bool upper_case=false) const