extract.native.hpp
1 2 3 4 5 6 7 8 9 10 11 | #pragma once #include <felspar/parse/concepts.hpp> #include <felspar/parse/exceptions.hpp> #include <felspar/parse/extract.detail.hpp> #include <cstdint> namespace felspar::parse::binary::native { |
Extract a value without checking that the span is large enough
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | template<concepts::numeric T> T unchecked_extract(std::span<std::byte const, sizeof(T)> const s) noexcept { if constexpr (sizeof(T) == 1u) { return static_cast<T>(s[0]); } else { return detail::native_extract<T>(s); } } template<concepts::numeric T> T unchecked_extract( std::span<std::uint8_t const, sizeof(T)> const s) noexcept { return unchecked_extract<T>(std::as_bytes(s)); } template<concepts::numeric T> T unchecked_extract(std::span<char const, sizeof(T)> const s) noexcept { return unchecked_extract<T>(std::as_bytes(s)); } |
Extract a numeric from a buffer
37 38 39 40 41 42 43 44 45 46 47 48 49 50 | template<concepts::numeric T> T extract( std::span<std::byte const> &s, felspar::source_location const &loc = felspar::source_location::current()) { buffer_too_small::check(sizeof(T), s.size(), loc); auto const v = unchecked_extract<T>( std::span<std::byte const, sizeof(T)>{s.data(), sizeof(T)}); s = s.subspan(sizeof(T)); return v; } } |