]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
enhance: Add util::Bytes::insert for span<const uint8_t>
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 18 Feb 2024 08:40:15 +0000 (09:40 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 18 Feb 2024 11:37:57 +0000 (12:37 +0100)
src/util/Bytes.hpp
unittest/test_util_Bytes.cpp

index 3d328d40c306a4c1e4ac42766969d3528939fa81..33032d085f3315e0bdbcfc9b11bf7ffda76e4e5e 100644 (file)
@@ -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<const uint8_t> data) noexcept;
 
 private:
   std::unique_ptr<uint8_t[]> 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<const uint8_t> data) noexcept
+{
+  return insert(pos, &*data.begin(), &*data.end());
+}
+
 } // namespace util
index 5be88575c3c4140c8dc8f120390287c794ced381..7c130d526412839b9ecd7f21a1125d9db5f64ac7 100644 (file)
@@ -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<const uint8_t> 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")