From: Joel Rosdahl Date: Sun, 16 Oct 2022 08:11:14 +0000 (+0200) Subject: enhance: Add more util::Bytes::insert variants X-Git-Tag: v4.7~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=46fcf1ef0b737522228db34d1e293b5221f08589;p=thirdparty%2Fccache.git enhance: Add more util::Bytes::insert variants --- diff --git a/src/util/Bytes.hpp b/src/util/Bytes.hpp index 7716f1382..ce9a6a2b3 100644 --- a/src/util/Bytes.hpp +++ b/src/util/Bytes.hpp @@ -74,10 +74,15 @@ public: void reserve(size_t size) noexcept; void clear() noexcept; + void resize(size_t size) noexcept; // Note: New bytes will be uninitialized. + void insert(const uint8_t* pos, const uint8_t* first, const uint8_t* last) noexcept; - void resize(size_t size) noexcept; // Note: New bytes will be uninitialized. + void + insert(const uint8_t* pos, const uint8_t* data, const size_t size) noexcept; + void insert(const uint8_t* pos, const char* first, const char* last) noexcept; + void insert(const uint8_t* pos, const char* data, size_t size) noexcept; private: uint8_t* m_data = nullptr; @@ -213,4 +218,26 @@ Bytes::clear() noexcept m_size = 0; } +inline void +Bytes::insert(const uint8_t* pos, + const uint8_t* data, + const size_t size) noexcept +{ + return insert(pos, data, data + size); +} + +inline void +Bytes::insert(const uint8_t* pos, const char* first, const char* last) noexcept +{ + return insert(pos, + reinterpret_cast(first), + reinterpret_cast(last)); +} + +inline void +Bytes::insert(const uint8_t* pos, const char* data, size_t size) noexcept +{ + return insert(pos, data, data + size); +} + } // namespace util diff --git a/unittest/test_util_Bytes.cpp b/unittest/test_util_Bytes.cpp index 4ff07b008..5be88575c 100644 --- a/unittest/test_util_Bytes.cpp +++ b/unittest/test_util_Bytes.cpp @@ -331,6 +331,44 @@ TEST_CASE("Basics") CHECK(bytes2[12] == 'a'); CHECK(bytes2[13] == 'x'); } + + SUBCASE("Insert util::Bytes data and size") + { + Bytes bytes2; + + bytes2.insert(bytes2.end(), bytes1.data(), bytes1.size()); + CHECK(bytes2.size() == 3); + CHECK(bytes2.capacity() == 3); + CHECK(bytes2[0] == 'a'); + CHECK(bytes2[1] == 'b'); + CHECK(bytes2[2] == 'c'); + } + + SUBCASE("Insert const char* first and last") + { + Bytes bytes2; + std::string data("abc"); + + bytes2.insert(bytes2.end(), data.data(), data.data() + data.size()); + CHECK(bytes2.size() == 3); + CHECK(bytes2.capacity() == 3); + CHECK(bytes2[0] == 'a'); + CHECK(bytes2[1] == 'b'); + CHECK(bytes2[2] == 'c'); + } + + SUBCASE("Insert const char* data and size") + { + Bytes bytes2; + std::string data("abc"); + + bytes2.insert(bytes2.end(), data.data(), data.size()); + CHECK(bytes2.size() == 3); + CHECK(bytes2.capacity() == 3); + CHECK(bytes2[0] == 'a'); + CHECK(bytes2[1] == 'b'); + CHECK(bytes2[2] == 'c'); + } } TEST_CASE("Conversion to span")