header_utils
Loading...
Searching...
No Matches
Bytes

A fairly UB-free set of functions to operate on and convert between arrays of bytelike types. More...

Concepts

concept  ghassanpl::bytelike
 Represents any trivially copyable type that is trivially castable to an internal object representation (so a char range).
concept  ghassanpl::bytelike_range
 A range of bytelikes.

Functions

template<bytelike TO, typename FROM, size_t N = std::dynamic_extent>
std::span< TO > ghassanpl::as_bytelikes (std::span< FROM, N > elements) noexcept
 Converts a span of trivial values to a span of bytelike s.
template<typename TO, bytelike FROM>
TO ghassanpl::from_bytelikes (std::span< FROM const > from)
 Returns an object whose internal representation is initialized from the argument.
template<typename TO, bytelike FROM>
std::span< TO > ghassanpl::span_from_bytelikes (std::span< FROM > from)
 Returns an object whose internal representation is initialized from the argument.
template<bytelike TO, typename T>
auto ghassanpl::as_bytelikes (T &pod) noexcept
 Returns a span of bytelike s that represents the internal object representation of the argument.
template<bytelike FROM>
constexpr auto ghassanpl::to_u8 (FROM byte) noexcept
 Bit-casts the given bytelike to a uint8_t.
template<bytelike FROM>
constexpr auto ghassanpl::to_char (FROM byte) noexcept
 Bit-casts the given bytelike to a char.
template<bytelike FROM>
constexpr auto ghassanpl::to_byte (FROM byte) noexcept
 Bit-casts the given bytelike to a std::byte.
template<bytelike FROM>
constexpr auto ghassanpl::to_uchar (FROM byte) noexcept
 Bit-casts the given bytelike to a unsigned char.
template<bytelike FROM>
constexpr auto ghassanpl::to_char8 (FROM byte) noexcept
 Bit-casts the given bytelike to a char8_t.
template<bytelike FROM, size_t N = std::dynamic_extent>
auto ghassanpl::as_chars (std::span< FROM, N > bytes) noexcept
 Returns a span of chars that represents the bytelikes in bytes.
template<bytelike FROM, size_t N = std::dynamic_extent>
auto ghassanpl::as_bytes (std::span< FROM, N > bytes) noexcept
 Returns a span of std::bytes that represents the bytelikes in bytes.
template<bytelike FROM, size_t N = std::dynamic_extent>
auto ghassanpl::as_uchars (std::span< FROM, N > bytes) noexcept
 Returns a span of unsigned chars that represents the bytelikes in bytes.
template<bytelike FROM, size_t N = std::dynamic_extent>
auto ghassanpl::as_u8s (std::span< FROM, N > bytes) noexcept
 Returns a span of uint8_ts that represents the bytelikes in bytes.
template<bytelike FROM, size_t N = std::dynamic_extent>
auto ghassanpl::as_char8s (std::span< FROM, N > bytes) noexcept
 Returns a span of char8_ts that represents the bytelikes in bytes.
template<typename T>
auto ghassanpl::as_chars (T const &data) noexcept
 Returns a span of chars that represents the internal object representation of the data object.
template<typename T>
auto ghassanpl::as_bytes (T const &data) noexcept
 Returns a span of std::bytes that represents the internal object representation of the data object.
template<typename T>
auto ghassanpl::as_uchars (T const &data) noexcept
 Returns a span of unsigned chars that represents the internal object representation of the data object.
template<typename T>
auto ghassanpl::as_u8s (T const &data) noexcept
 Returns a span of uint8_ts that represents the internal object representation of the data object.
template<typename T>
auto ghassanpl::as_char8s (T const &data) noexcept
 Returns a span of char8_ts that represents the internal object representation of the data object.
template<bytelike B, size_t N = std::dynamic_extent>
bool ghassanpl::nth_bit (std::span< B const, N > range, size_t n) noexcept
 Returns the value of the nth bit in the range of bytelikes.
template<bytelike B, size_t N = std::dynamic_extent>
void ghassanpl::set_nth_bit (std::span< B, N > range, size_t n, bool value) noexcept
 Sets the value of the nth bit in the range of bytelikes.
template<typename T>
bool ghassanpl::nth_bit (T const &pod, size_t n) noexcept
 Returns the value of the nth bit in the internal object representation of the pod object.
template<typename T>
void ghassanpl::set_nth_bit (T &pod, size_t n, bool value) noexcept
 Sets the value of the nth bit in the internal object representation of the pod object.
template<size_t N, bytelike B, size_t SN = std::dynamic_extent>
bool ghassanpl::nth_bit (std::span< B const, SN > range) noexcept
 Returns the value of the Nth bit in the range of bytelikes.
template<size_t N, bytelike B, size_t SN = std::dynamic_extent>
void ghassanpl::set_nth_bit (std::span< B, SN > range, bool value) noexcept
 Sets the value of the Nth bit in the range of bytelikes.
template<size_t N, typename T>
bool ghassanpl::nth_bit (T const &pod) noexcept
 Returns the value of the Nth bit in the internal object representation of the pod object.
template<size_t N, typename T>
void ghassanpl::set_nth_bit (T &pod, bool value) noexcept
 Sets the value of the Nth bit in the internal object representation of the pod object.
template<bytelike B, std::integral T>
constexpr auto ghassanpl::to_bytelike_array (T value_)
 A constexpr function that converts an integral value to its constituent bytelikes TODO: This is NOT like a reinterpret_/bit_cast to u8s, because it's not endian-aware.
template<std::integral T>
constexpr auto ghassanpl::to_u8_array (T value)
template<size_t ALIGN, bytelike T, size_t N = std::dynamic_extent>
auto ghassanpl::align_front_to (std::span< T, N > bytes) noexcept -> align_front_to_result< T >
 Splits the argument span into two spans, the first one being the prefix, the second one having its data pointer aligned to the given alignment.
template<size_t ALIGN, bytelike T, size_t N = std::dynamic_extent>
constexpr auto ghassanpl::align_back_to (std::span< T, N > bytes) noexcept -> align_back_to_result< T >
 Splits the argument span into two spans, the first one having its size aligned to the given alignment, the second one being the suffix.
template<size_t ALIGN, bytelike T, size_t N = std::dynamic_extent>
auto ghassanpl::align_to (std::span< T, N > bytes) noexcept -> align_to_result< T >
 Splits the argument span into three spans, the first one being the prefix, the second one having its data and size aligned to the given alignment, the third one being the suffix.
template<typename TO, bytelike B, size_t N = std::dynamic_extent>
auto ghassanpl::aligned_span_cast (std::span< B, N > bytes) noexcept -> align_to_result< B, TO >
 Splits the argument span into three spans, the first one being the prefix, the second one capable of storing one ore more objects of type TO with the proper alignment, and the third one being the suffix.
template<typename TO, bytelike B, size_t N = std::dynamic_extent>
auto ghassanpl::aligned_span_cast_and_construct (std::span< B, N > bytes) noexcept -> align_to_result< B, TO >
 Does the same as aligned_span_cast but then default-constructs the elements of the of middle span.

Detailed Description

A fairly UB-free set of functions to operate on and convert between arrays of bytelike types.

Function Documentation

◆ align_back_to()

template<size_t ALIGN, bytelike T, size_t N = std::dynamic_extent>
auto ghassanpl::align_back_to ( std::span< T, N > bytes) -> align_back_to_result< T >
nodiscardconstexprnoexcept

Splits the argument span into two spans, the first one having its size aligned to the given alignment, the second one being the suffix.

Assumes/expects bytes.data() is aligned to the specified alignment.

Definition at line 254 of file bytes.h.

◆ align_front_to()

template<size_t ALIGN, bytelike T, size_t N = std::dynamic_extent>
auto ghassanpl::align_front_to ( std::span< T, N > bytes) -> align_front_to_result< T >
nodiscardnoexcept

Splits the argument span into two spans, the first one being the prefix, the second one having its data pointer aligned to the given alignment.

Definition at line 225 of file bytes.h.

◆ align_to()

template<size_t ALIGN, bytelike T, size_t N = std::dynamic_extent>
auto ghassanpl::align_to ( std::span< T, N > bytes) -> align_to_result< T >
nodiscardnoexcept

Splits the argument span into three spans, the first one being the prefix, the second one having its data and size aligned to the given alignment, the third one being the suffix.

Definition at line 284 of file bytes.h.

◆ aligned_span_cast()

template<typename TO, bytelike B, size_t N = std::dynamic_extent>
auto ghassanpl::aligned_span_cast ( std::span< B, N > bytes) -> align_to_result< B, TO >
nodiscardnoexcept

Splits the argument span into three spans, the first one being the prefix, the second one capable of storing one ore more objects of type TO with the proper alignment, and the third one being the suffix.

Definition at line 311 of file bytes.h.

◆ aligned_span_cast_and_construct()

template<typename TO, bytelike B, size_t N = std::dynamic_extent>
auto ghassanpl::aligned_span_cast_and_construct ( std::span< B, N > bytes) -> align_to_result< B, TO >
nodiscardnoexcept

Does the same as aligned_span_cast but then default-constructs the elements of the of middle span.

Definition at line 323 of file bytes.h.

◆ as_bytelikes() [1/2]

template<bytelike TO, typename FROM, size_t N = std::dynamic_extent>
std::span< TO > ghassanpl::as_bytelikes ( std::span< FROM, N > elements)
nodiscardnoexcept

Converts a span of trivial values to a span of bytelike s.

Definition at line 40 of file bytes.h.

◆ as_bytelikes() [2/2]

template<bytelike TO, typename T>
auto ghassanpl::as_bytelikes ( T & pod)
nodiscardnoexcept

Returns a span of bytelike s that represents the internal object representation of the argument.

Definition at line 71 of file bytes.h.

◆ as_bytes() [1/2]

template<bytelike FROM, size_t N = std::dynamic_extent>
auto ghassanpl::as_bytes ( std::span< FROM, N > bytes)
nodiscardnoexcept

Returns a span of std::bytes that represents the bytelikes in bytes.

Definition at line 95 of file bytes.h.

◆ as_bytes() [2/2]

template<typename T>
auto ghassanpl::as_bytes ( T const & data)
nodiscardnoexcept

Returns a span of std::bytes that represents the internal object representation of the data object.

Definition at line 109 of file bytes.h.

◆ as_char8s() [1/2]

template<bytelike FROM, size_t N = std::dynamic_extent>
auto ghassanpl::as_char8s ( std::span< FROM, N > bytes)
nodiscardnoexcept

Returns a span of char8_ts that represents the bytelikes in bytes.

Definition at line 104 of file bytes.h.

◆ as_char8s() [2/2]

template<typename T>
auto ghassanpl::as_char8s ( T const & data)
nodiscardnoexcept

Returns a span of char8_ts that represents the internal object representation of the data object.

Definition at line 115 of file bytes.h.

◆ as_chars() [1/2]

template<bytelike FROM, size_t N = std::dynamic_extent>
auto ghassanpl::as_chars ( std::span< FROM, N > bytes)
nodiscardnoexcept

Returns a span of chars that represents the bytelikes in bytes.

Definition at line 92 of file bytes.h.

◆ as_chars() [2/2]

template<typename T>
auto ghassanpl::as_chars ( T const & data)
nodiscardnoexcept

Returns a span of chars that represents the internal object representation of the data object.

Definition at line 107 of file bytes.h.

◆ as_u8s() [1/2]

template<bytelike FROM, size_t N = std::dynamic_extent>
auto ghassanpl::as_u8s ( std::span< FROM, N > bytes)
nodiscardnoexcept

Returns a span of uint8_ts that represents the bytelikes in bytes.

Definition at line 101 of file bytes.h.

◆ as_u8s() [2/2]

template<typename T>
auto ghassanpl::as_u8s ( T const & data)
nodiscardnoexcept

Returns a span of uint8_ts that represents the internal object representation of the data object.

Definition at line 113 of file bytes.h.

◆ as_uchars() [1/2]

template<bytelike FROM, size_t N = std::dynamic_extent>
auto ghassanpl::as_uchars ( std::span< FROM, N > bytes)
nodiscardnoexcept

Returns a span of unsigned chars that represents the bytelikes in bytes.

Definition at line 98 of file bytes.h.

◆ as_uchars() [2/2]

template<typename T>
auto ghassanpl::as_uchars ( T const & data)
nodiscardnoexcept

Returns a span of unsigned chars that represents the internal object representation of the data object.

Definition at line 111 of file bytes.h.

◆ from_bytelikes()

template<typename TO, bytelike FROM>
TO ghassanpl::from_bytelikes ( std::span< FROM const > from)
nodiscard

Returns an object whose internal representation is initialized from the argument.

Definition at line 48 of file bytes.h.

◆ nth_bit() [1/4]

template<bytelike B, size_t N = std::dynamic_extent>
bool ghassanpl::nth_bit ( std::span< B const, N > range,
size_t n )
nodiscardnoexcept

Returns the value of the nth bit in the range of bytelikes.

Definition at line 119 of file bytes.h.

◆ nth_bit() [2/4]

template<size_t N, bytelike B, size_t SN = std::dynamic_extent>
bool ghassanpl::nth_bit ( std::span< B const, SN > range)
nodiscardnoexcept

Returns the value of the Nth bit in the range of bytelikes.

Definition at line 160 of file bytes.h.

◆ nth_bit() [3/4]

template<size_t N, typename T>
bool ghassanpl::nth_bit ( T const & pod)
nodiscardnoexcept

Returns the value of the Nth bit in the internal object representation of the pod object.

Definition at line 186 of file bytes.h.

◆ nth_bit() [4/4]

template<typename T>
bool ghassanpl::nth_bit ( T const & pod,
size_t n )
nodiscardnoexcept

Returns the value of the nth bit in the internal object representation of the pod object.

Definition at line 145 of file bytes.h.

◆ set_nth_bit() [1/4]

template<bytelike B, size_t N = std::dynamic_extent>
void ghassanpl::set_nth_bit ( std::span< B, N > range,
size_t n,
bool value )
noexcept

Sets the value of the nth bit in the range of bytelikes.

Definition at line 130 of file bytes.h.

◆ set_nth_bit() [2/4]

template<size_t N, bytelike B, size_t SN = std::dynamic_extent>
void ghassanpl::set_nth_bit ( std::span< B, SN > range,
bool value )
noexcept

Sets the value of the Nth bit in the range of bytelikes.

Definition at line 171 of file bytes.h.

◆ set_nth_bit() [3/4]

template<size_t N, typename T>
void ghassanpl::set_nth_bit ( T & pod,
bool value )
noexcept

Sets the value of the Nth bit in the internal object representation of the pod object.

Definition at line 194 of file bytes.h.

◆ set_nth_bit() [4/4]

template<typename T>
void ghassanpl::set_nth_bit ( T & pod,
size_t n,
bool value )
noexcept

Sets the value of the nth bit in the internal object representation of the pod object.

Definition at line 153 of file bytes.h.

◆ span_from_bytelikes()

template<typename TO, bytelike FROM>
std::span< TO > ghassanpl::span_from_bytelikes ( std::span< FROM > from)
nodiscard

Returns an object whose internal representation is initialized from the argument.

Definition at line 61 of file bytes.h.

◆ to_byte()

template<bytelike FROM>
auto ghassanpl::to_byte ( FROM byte)
nodiscardconstexprnoexcept

Bit-casts the given bytelike to a std::byte.

Definition at line 84 of file bytes.h.

◆ to_bytelike_array()

template<bytelike B, std::integral T>
auto ghassanpl::to_bytelike_array ( T value_)
nodiscardconstexpr

A constexpr function that converts an integral value to its constituent bytelikes TODO: This is NOT like a reinterpret_/bit_cast to u8s, because it's not endian-aware.

Definition at line 202 of file bytes.h.

◆ to_char()

template<bytelike FROM>
auto ghassanpl::to_char ( FROM byte)
nodiscardconstexprnoexcept

Bit-casts the given bytelike to a char.

Definition at line 82 of file bytes.h.

◆ to_char8()

template<bytelike FROM>
auto ghassanpl::to_char8 ( FROM byte)
nodiscardconstexprnoexcept

Bit-casts the given bytelike to a char8_t.

Definition at line 88 of file bytes.h.

◆ to_u8()

template<bytelike FROM>
auto ghassanpl::to_u8 ( FROM byte)
nodiscardconstexprnoexcept

Bit-casts the given bytelike to a uint8_t.

Definition at line 80 of file bytes.h.

◆ to_u8_array()

template<std::integral T>
auto ghassanpl::to_u8_array ( T value)
constexpr

Definition at line 214 of file bytes.h.

◆ to_uchar()

template<bytelike FROM>
auto ghassanpl::to_uchar ( FROM byte)
nodiscardconstexprnoexcept

Bit-casts the given bytelike to a unsigned char.

Definition at line 86 of file bytes.h.