Skip to content

Commit 643dea1

Browse files
authored
Merge pull request #222 from jvillavc/jorgevil/byterator-64bits
✨ Add uint64 support to `byterator`
2 parents c57967c + 6ba5e7b commit 643dea1

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,39 @@ TEST_CASE("write uint32_t", "[byterator]") {
219219
CHECK((i == std::end(a)));
220220
}
221221

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

0 commit comments

Comments
 (0)