]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
enhance: Add more util::Bytes::insert variants
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 16 Oct 2022 08:11:14 +0000 (10:11 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 16 Oct 2022 08:50:09 +0000 (10:50 +0200)
src/util/Bytes.hpp
unittest/test_util_Bytes.cpp

index 7716f1382ceb0077d889d8e098002cbfcaba8615..ce9a6a2b35a35655d0fae439a43ed99c7df1347a 100644 (file)
@@ -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<const uint8_t*>(first),
+                reinterpret_cast<const uint8_t*>(last));
+}
+
+inline void
+Bytes::insert(const uint8_t* pos, const char* data, size_t size) noexcept
+{
+  return insert(pos, data, data + size);
+}
+
 } // namespace util
index 4ff07b00818cac9fe7ba634f7e25dd18fd5e2572..5be88575c3c4140c8dc8f120390287c794ced381 100644 (file)
@@ -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")