From: Joel Rosdahl Date: Sun, 18 Feb 2024 08:40:15 +0000 (+0100) Subject: enhance: Add util::Bytes::insert for span X-Git-Tag: v4.10~89 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=498b19353175c6d824587908e9362436e24b2ebf;p=thirdparty%2Fccache.git enhance: Add util::Bytes::insert for span --- diff --git a/src/util/Bytes.hpp b/src/util/Bytes.hpp index 3d328d40..33032d08 100644 --- a/src/util/Bytes.hpp +++ b/src/util/Bytes.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2022-2023 Joel Rosdahl and other contributors +// Copyright (C) 2022-2024 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -84,6 +84,7 @@ public: 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; + void insert(const uint8_t* pos, nonstd::span data) noexcept; private: std::unique_ptr m_data; @@ -238,4 +239,10 @@ Bytes::insert(const uint8_t* pos, const char* data, size_t size) noexcept return insert(pos, data, data + size); } +inline void +Bytes::insert(const uint8_t* pos, nonstd::span data) noexcept +{ + return insert(pos, &*data.begin(), &*data.end()); +} + } // namespace util diff --git a/unittest/test_util_Bytes.cpp b/unittest/test_util_Bytes.cpp index 5be88575..7c130d52 100644 --- a/unittest/test_util_Bytes.cpp +++ b/unittest/test_util_Bytes.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2021-2022 Joel Rosdahl and other contributors +// Copyright (C) 2021-2024 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -369,6 +369,19 @@ TEST_CASE("Basics") CHECK(bytes2[1] == 'b'); CHECK(bytes2[2] == 'c'); } + + SUBCASE("Insert span") + { + Bytes bytes2; + nonstd::span span(bytes1.begin(), bytes1.end()); + + bytes2.insert(bytes2.end(), span); + 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")