Skip to content

Commit 3222e0e

Browse files
committed
✨ Add uint64 support to byterator
Problem: - `byterator` doesn't support 64 bits operations. Solution: - Add readu64, peeku64 and writeu64 implementations.
1 parent c57967c commit 3222e0e

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

include/stdx/byterator.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,16 @@ template <typename T> class byterator {
274274
template <typename V> [[nodiscard]] auto writeu32(V &&v) {
275275
return write(static_cast<std::uint32_t>(std::forward<V>(v)));
276276
}
277+
278+
template <typename V = std::uint64_t> [[nodiscard]] auto peeku64() {
279+
return peek<std::uint64_t, V>();
280+
}
281+
template <typename V = std::uint64_t> [[nodiscard]] auto readu64() {
282+
return read<std::uint64_t, V>();
283+
}
284+
template <typename V> [[nodiscard]] auto writeu64(V &&v) {
285+
return write(static_cast<std::uint64_t>(std::forward<V>(v)));
286+
}
277287
};
278288

279289
template <typename It, std::enable_if_t<detail::is_byteratorish_v<It>, int> = 0>

test/byterator.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,43 @@ TEST_CASE("write uint32_t", "[byterator]") {
219219
CHECK((i == std::end(a)));
220220
}
221221

222+
TEST_CASE("peek uint64_t", "[byterator]") {
223+
const auto a = std::array{stdx::to_be<std::uint16_t>(0x0102),
224+
stdx::to_be<std::uint16_t>(0x0304),
225+
stdx::to_be<std::uint16_t>(0x0506),
226+
stdx::to_be<std::uint16_t>(0x0708)};
227+
auto i = stdx::byterator{std::begin(a)};
228+
static_assert(std::is_same_v<decltype(i.readu64()), std::uint64_t>);
229+
CHECK(i.peeku64() == stdx::to_be<std::uint64_t>(0x0102030405060708));
230+
CHECK((i == std::begin(a)));
231+
}
232+
233+
TEST_CASE("read uint64_t", "[byterator]") {
234+
const auto a = std::array{stdx::to_be<std::uint16_t>(0x0102),
235+
stdx::to_be<std::uint16_t>(0x0304),
236+
stdx::to_be<std::uint16_t>(0x0506),
237+
stdx::to_be<std::uint16_t>(0x0708)};
238+
auto i = stdx::byterator{std::begin(a)};
239+
static_assert(std::is_same_v<decltype(i.readu64()), std::uint64_t>);
240+
CHECK(i.readu64() == stdx::to_be<std::uint64_t>(0x0102030405060708));
241+
CHECK((i == std::end(a)));
242+
}
243+
244+
TEST_CASE("write uint64_t", "[byterator]") {
245+
auto a = std::array{stdx::to_be<std::uint16_t>(0x0102),
246+
stdx::to_be<std::uint16_t>(0x0304),
247+
stdx::to_be<std::uint16_t>(0x0506),
248+
stdx::to_be<std::uint16_t>(0x0708)};
249+
auto i = stdx::byterator{std::begin(a)};
250+
i.writeu64(stdx::to_be<std::uint64_t>(0x05060708090A0B0C));
251+
CHECK(a[0] == stdx::to_be<std::uint16_t>(0x0506));
252+
CHECK(a[1] == stdx::to_be<std::uint16_t>(0x0708));
253+
CHECK(a[2] == stdx::to_be<std::uint16_t>(0x090A));
254+
CHECK(a[3] == stdx::to_be<std::uint16_t>(0x0B0C));
255+
CHECK((i == std::end(a)));
256+
}
257+
258+
222259
namespace {
223260
enum struct E : std::uint8_t { A = 1, B = 2, C = 3 };
224261
}

0 commit comments

Comments
 (0)