summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Rosdahl <joel@rosdahl.net>2022-10-16 10:11:14 +0200
committerJoel Rosdahl <joel@rosdahl.net>2022-10-16 10:50:09 +0200
commit46fcf1ef0b737522228db34d1e293b5221f08589 (patch)
treecf282296ddc4c3f735baaf0818915d202bbeb91d
parent552d736e12931f1c9e286c8495a4687457921ba8 (diff)
downloadccache-46fcf1ef0b737522228db34d1e293b5221f08589.tar.gz
enhance: Add more util::Bytes::insert variants
-rw-r--r--src/util/Bytes.hpp29
-rw-r--r--unittest/test_util_Bytes.cpp38
2 files changed, 66 insertions, 1 deletions
diff --git a/src/util/Bytes.hpp b/src/util/Bytes.hpp
index 7716f138..ce9a6a2b 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<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
diff --git a/unittest/test_util_Bytes.cpp b/unittest/test_util_Bytes.cpp
index 4ff07b00..5be88575 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")