You can now create Bytes from strings and vice versa.

This commit is contained in:
David Given
2021-01-04 23:31:54 +01:00
parent fd4d1c4bb7
commit 629af2a697
3 changed files with 16 additions and 0 deletions

View File

@@ -41,6 +41,12 @@ Bytes::Bytes(const uint8_t* ptr, size_t len):
_high(len)
{}
Bytes::Bytes(const std::string& s):
_data(createVector((const uint8_t*)&s[0], s.size())),
_low(0),
_high(s.size())
{}
Bytes::Bytes(std::initializer_list<uint8_t> data):
_data(createVector(data)),
_low(0),

View File

@@ -41,6 +41,8 @@ public:
uint8_t* begin() { checkWritable(); return &(*_data)[_low]; }
uint8_t* end() { checkWritable(); return &(*_data)[_high]; }
operator const std::string () const { return std::string(cbegin(), cend()); }
void boundsCheck(unsigned pos) const;
void checkWritable();
void adjustBounds(unsigned pos);

View File

@@ -124,6 +124,13 @@ static void test_tobits()
}));
}
static void test_tostring()
{
std::string s = "Hello, world";
Bytes b(s);
assert(b == s);
}
int main(int argc, const char* argv[])
{
test_bounds();
@@ -133,5 +140,6 @@ int main(int argc, const char* argv[])
test_writes();
test_slice();
test_tobits();
test_tostring();
return 0;
}