]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Improve Bytes::{erase,insert} signatures
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 9 Nov 2025 12:49:22 +0000 (13:49 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 12 Nov 2025 20:14:01 +0000 (21:14 +0100)
src/ccache/util/bytes.cpp
src/ccache/util/bytes.hpp

index e4f5463085afd59afcf13b761acc6c236885bbdb..66d1e9fc04bfae5a8a41b82d0d62fbb13c0c794a 100644 (file)
@@ -127,35 +127,30 @@ Bytes::reserve(size_t size) noexcept
 }
 
 void
-Bytes::insert(const uint8_t* pos,
-              const uint8_t* first,
-              const uint8_t* last) noexcept
+Bytes::insert(const void* pos, const void* data, size_t size) noexcept
 {
-  const size_t inserted_size = last - first;
-  if (inserted_size == 0) {
+  if (size == 0) {
     return;
   }
-  const size_t offset = pos - m_data.get();
-  if (m_size + inserted_size > m_capacity) {
-    m_capacity = std::max(2 * m_capacity, m_size + inserted_size);
+  const size_t offset = reinterpret_cast<const uint8_t*>(pos) - m_data.get();
+  if (m_size + size > m_capacity) {
+    m_capacity = std::max(2 * m_capacity, m_size + size);
     // In C++20, use std::make_unique_for_overwrite instead.
     auto new_data = std::unique_ptr<uint8_t[]>(new uint8_t[m_capacity]);
     if (offset > 0) {
       std::memcpy(new_data.get(), m_data.get(), offset);
     }
     if (m_size > offset) {
-      std::memcpy(new_data.get() + offset + inserted_size,
-                  m_data.get() + offset,
-                  m_size - offset);
+      std::memcpy(
+        new_data.get() + offset + size, m_data.get() + offset, m_size - offset);
     }
     m_data = std::move(new_data);
   } else if (m_size > offset) {
-    std::memmove(m_data.get() + offset + inserted_size,
-                 m_data.get() + offset,
-                 m_size - offset);
+    std::memmove(
+      m_data.get() + offset + size, m_data.get() + offset, m_size - offset);
   }
-  std::memcpy(m_data.get() + offset, first, inserted_size);
-  m_size += inserted_size;
+  std::memcpy(m_data.get() + offset, data, size);
+  m_size += size;
 }
 
 void
@@ -176,12 +171,12 @@ Bytes::resize(size_t size) noexcept
 }
 
 void
-Bytes::erase(const uint8_t* pos, const size_t size) noexcept
+Bytes::erase(const void* pos, const size_t size) noexcept
 {
   if (size == 0) {
     return;
   }
-  const size_t offset = pos - m_data.get();
+  const size_t offset = reinterpret_cast<const uint8_t*>(pos) - m_data.get();
   if (offset + size < m_size) {
     std::memmove(m_data.get() + offset,
                  m_data.get() + offset + size,
@@ -190,10 +185,4 @@ Bytes::erase(const uint8_t* pos, const size_t size) noexcept
   m_size -= size;
 }
 
-void
-Bytes::erase(const uint8_t* first, const uint8_t* last) noexcept
-{
-  erase(first, last - first);
-}
-
 } // namespace util
index 2780de07b44e68c1d8c3591653e4c684feb46117..ba71cad0233b9e90d537b446c64f062af6d27293 100644 (file)
@@ -87,17 +87,12 @@ public:
 
   void push_back(uint8_t value) noexcept;
 
-  void insert(const uint8_t* pos,
-              const uint8_t* first,
-              const uint8_t* last) noexcept;
-  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;
-  void insert(const uint8_t* pos, nonstd::span<const uint8_t> data) noexcept;
-
-  void erase(const uint8_t* pos, const size_t size) noexcept;
-  void erase(const uint8_t* first, const uint8_t* last) noexcept;
+  void insert(const void* pos, const void* first, const void* last) noexcept;
+  void insert(const void* pos, const void* data, size_t size) noexcept;
+  void insert(const void* pos, nonstd::span<const uint8_t> data) noexcept;
+
+  void erase(const void* pos, size_t size) noexcept;
+  void erase(const void* first, const void* last) noexcept;
 
 private:
   std::unique_ptr<uint8_t[]> m_data;
@@ -241,31 +236,26 @@ Bytes::clear() noexcept
 }
 
 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
+Bytes::insert(const void* pos, const void* first, const void* last) noexcept
 {
   return insert(pos,
-                reinterpret_cast<const uint8_t*>(first),
-                reinterpret_cast<const uint8_t*>(last));
+                first,
+                reinterpret_cast<const uint8_t*>(last)
+                  - reinterpret_cast<const uint8_t*>(first));
 }
 
 inline void
-Bytes::insert(const uint8_t* pos, const char* data, size_t size) noexcept
+Bytes::insert(const void* pos, nonstd::span<const uint8_t> data) noexcept
 {
-  return insert(pos, data, data + size);
+  return insert(pos, &*data.begin(), data.size());
 }
 
 inline void
-Bytes::insert(const uint8_t* pos, nonstd::span<const uint8_t> data) noexcept
+Bytes::erase(const void* first, const void* last) noexcept
 {
-  return insert(pos, &*data.begin(), &*data.end());
+  erase(first,
+        reinterpret_cast<const uint8_t*>(last)
+          - reinterpret_cast<const uint8_t*>(first));
 }
 
 } // namespace util