From 6ba5e7b2755962d92d590f66b5919560f53fd145 Mon Sep 17 00:00:00 2001 From: Jorge Villavicencio Date: Thu, 1 May 2025 09:43:28 -0700 Subject: [PATCH] :sparkles: Add uint64 support to `byterator` Problem: - `byterator` doesn't support 64 bits operations. Solution: - Add readu64, peeku64 and writeu64 implementations. --- include/stdx/byterator.hpp | 10 ++++++++++ test/byterator.cpp | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/include/stdx/byterator.hpp b/include/stdx/byterator.hpp index ec71fb1..e06208b 100644 --- a/include/stdx/byterator.hpp +++ b/include/stdx/byterator.hpp @@ -274,6 +274,16 @@ template class byterator { template [[nodiscard]] auto writeu32(V &&v) { return write(static_cast(std::forward(v))); } + + template [[nodiscard]] auto peeku64() { + return peek(); + } + template [[nodiscard]] auto readu64() { + return read(); + } + template [[nodiscard]] auto writeu64(V &&v) { + return write(static_cast(std::forward(v))); + } }; template , int> = 0> diff --git a/test/byterator.cpp b/test/byterator.cpp index b58914f..23d86de 100644 --- a/test/byterator.cpp +++ b/test/byterator.cpp @@ -219,6 +219,39 @@ TEST_CASE("write uint32_t", "[byterator]") { CHECK((i == std::end(a))); } +TEST_CASE("peek uint64_t", "[byterator]") { + auto const a = std::array{ + stdx::to_be(0x0102), stdx::to_be(0x0304), + stdx::to_be(0x0506), stdx::to_be(0x0708)}; + auto i = stdx::byterator{std::begin(a)}; + static_assert(std::is_same_v); + CHECK(i.peeku64() == stdx::to_be(0x0102030405060708)); + CHECK((i == std::begin(a))); +} + +TEST_CASE("read uint64_t", "[byterator]") { + auto const a = std::array{ + stdx::to_be(0x0102), stdx::to_be(0x0304), + stdx::to_be(0x0506), stdx::to_be(0x0708)}; + auto i = stdx::byterator{std::begin(a)}; + static_assert(std::is_same_v); + CHECK(i.readu64() == stdx::to_be(0x0102030405060708)); + CHECK((i == std::end(a))); +} + +TEST_CASE("write uint64_t", "[byterator]") { + auto a = std::array{ + stdx::to_be(0x0102), stdx::to_be(0x0304), + stdx::to_be(0x0506), stdx::to_be(0x0708)}; + auto i = stdx::byterator{std::begin(a)}; + i.writeu64(stdx::to_be(0x05060708090A0B0C)); + CHECK(a[0] == stdx::to_be(0x0506)); + CHECK(a[1] == stdx::to_be(0x0708)); + CHECK(a[2] == stdx::to_be(0x090A)); + CHECK(a[3] == stdx::to_be(0x0B0C)); + CHECK((i == std::end(a))); +} + namespace { enum struct E : std::uint8_t { A = 1, B = 2, C = 3 }; }