29#ifndef EXTRACT_APPEND_HPP_INCLUDED
30#define EXTRACT_APPEND_HPP_INCLUDED
67concept integral_or_byte = std::integral<T> || std::is_same_v<std::remove_cv_t<T>, std::byte>;
106template <std::endian BufEndian,
integral_or_
byte T>
108 auto value_representation = std::bit_cast<std::array<std::byte,
sizeof(T)>>(T{});
109 std::ranges::copy (buf, buf +
sizeof(T), value_representation.begin());
110 auto tmp_val = std::bit_cast<T>(value_representation);
111 if constexpr (BufEndian != std::endian::native &&
sizeof(T) != 1u) {
139template <std::endian BufEndian,
integral_or_
byte T>
140constexpr std::size_t
append_val(std::byte* buf,
const T& val)
noexcept {
142 if constexpr (BufEndian != std::endian::native &&
sizeof(T) != 1u) {
145 auto value_representation = std::bit_cast<std::array<std::byte,
sizeof(T)>>(tmp_val);
146 std::ranges::copy (value_representation, buf );
183template<std::
unsigned_
integral T>
186 std::size_t output_size = 0;
192 output[output_size] = std::bit_cast<std::byte>(
static_cast<std::uint8_t
>((
static_cast<std::uint8_t
> (val & 127)) | 128));
197 output[output_size++] = std::bit_cast<std::byte>(
static_cast<std::uint8_t
>(
static_cast<std::uint8_t
> (val) & 127));
216template<std::
unsigned_
integral T>
219 for (std::size_t i = 0; i < input_size; ++i) {
220 ret |= (std::bit_cast<std::uint8_t>(input[i]) & 127) << (7 * i);
222 if(!(std::bit_cast<std::uint8_t>(input[i]) & 128)) {
This is an implementation of the C++ 23 std::byteswap function, for use in pre C++ 23 applications.
constexpr T byteswap(T value) noexcept
Perform an in-place byte swap on an integral type (if size of the type is greater than one).
Definition byteswap.hpp:41
Definition extract_append.hpp:67