Using a C++ user-defined literal to initialise an array
Use a numeric literal operator template, with the signature:
template <char...>
result_type operator "" _x();
Also, since the data is known at compile-time, we might as well make everything constexpr
. Note that we use std::array
instead of C-style arrays:
#include <cstdint>
#include <array>
#include <vector>
// Constexpr hex parsing algorithm follows:
struct InvalidHexDigit {};
struct InvalidPrefix {};
struct OddLength {};
constexpr std::uint8_t hex_value(char c)
{
if ('0' <= c && c <= '9') return c - '0';
// This assumes ASCII:
if ('A' <= c && c <= 'F') return c - 'A' + 10;
if ('a' <= c && c <= 'f') return c - 'a' + 10;
// In constexpr-land, this is a compile-time error if execution reaches it:
// The weird `if (c == c)` is to work around gcc 8.2 erroring out here even though
// execution doesn't reach it.
if (c == c) throw InvalidHexDigit{};
}
constexpr std::uint8_t parse_single(char a, char b)
{
return (hex_value(a) << 4) | hex_value(b);
}
template <typename Iter, typename Out>
constexpr auto parse_hex(Iter begin, Iter end, Out out)
{
if (end - begin <= 2) throw InvalidPrefix{};
if (begin[0] != '0' || begin[1] != 'x') throw InvalidPrefix{};
if ((end - begin) % 2 != 0) throw OddLength{};
begin += 2;
while (begin != end)
{
*out = parse_single(*begin, *(begin + 1));
begin += 2;
++out;
}
return out;
}
// Make this a template to defer evaluation until later
template <char... cs>
struct HexByteArray {
static constexpr auto to_array()
{
constexpr std::array<char, sizeof...(cs)> data{cs...};
std::array<std::uint8_t, (sizeof...(cs) / 2 - 1)> result{};
parse_hex(data.begin(), data.end(), result.begin());
return result;
}
constexpr operator std::array<std::uint8_t, (sizeof...(cs) / 2)>() const
{
return to_array();
}
operator std::vector<std::uint8_t>() const
{
constexpr auto tmp = to_array();
return std::vector<std::uint8_t>{tmp.begin(), tmp.end()};
}
};
template <char... cs>
constexpr auto operator"" _$()
{
static_assert(sizeof...(cs) % 2 == 0, "Must be an even number of chars");
return HexByteArray<cs...>{};
}
Demo
Example usage:
auto data_array = 0x6BC1BEE22E409F96E93D7E117393172A_$ .to_array();
std::vector<std::uint8_t> data_vector = 0x6BC1BEE22E409F96E93D7E117393172A_$;
As a side note, $
in an identifier is actually a gcc extension, so it's non-standard C++. Consider using a UDL other than _$
.
This will make it
namespace detail{
template <std::size_t C> constexpr std::integral_constant<std::size_t, C> int_c{ };
template <char c>
class hex_decimal_t
{
constexpr static std::uint8_t get_value() {
constexpr std::uint8_t k = c - '0';
if constexpr (k >= 0 && k <= 9) { return k; }
else if constexpr (k >= 17 && k <= 22) { return k - 7; }
else if constexpr (k >= 49 && k <= 54) { return k - 39; }
else { return std::uint8_t(-1); }
}
public:
static constexpr std::uint8_t value = get_value();
constexpr operator auto() const{
return value;
}
};
template <char C> constexpr hex_decimal_t<C> hex_decimal{ };
template <bool B> using bool_type = std::integral_constant<bool, B>;
template <char... cs> struct is_valid_hex : std::false_type { };
template <char... cs> struct is_valid_hex<'0', 'x', cs...> : bool_type<((hex_decimal<cs> != std::uint8_t(-1)) && ...)>{};
template <char... cs> struct is_valid_hex<'0', 'X', cs...> : bool_type<((hex_decimal<cs> != std::uint8_t(-1)) && ...)>{};
template <std::size_t... Is>
constexpr auto expand_over(std::index_sequence<0, Is...>)
{
return [](auto&& f) -> decltype(auto) {
return decltype(f)(f)(int_c<Is>...);
};
}
template <class T,class... F>
constexpr auto select(T, F&&... f) {
return std::get<T{}>(std::forward_as_tuple(std::forward<F>(f)...));
}
}
template <char... ds>
constexpr auto operator "" _H()
{
static_assert(detail::is_valid_hex<ds...>{} || sizeof...(ds) < 3, "Not a valid hex number");
static_assert(!(sizeof...(ds) > 3 && sizeof...(ds) & 0x1), "Hex string must have even length");
constexpr int Sz = sizeof...(ds);
constexpr auto expand = detail::select(detail::int_c<(Sz > 3)>,
[] { return detail::expand_over(std::make_index_sequence<2>{}); },
[] { return detail::expand_over(std::make_index_sequence<Sz/2>{}); }
)();
if constexpr (Sz <= 3) {
return expand([](auto... Is) {
constexpr std::array digs{ds...};
return std::array { (detail::hex_decimal<digs[2 * Is]>)... };
});
} else {
return expand([](auto... Is) {
constexpr std::array digs{ds...};
return std::array { ((detail::hex_decimal<digs[2 * Is]> << 4) | detail::hex_decimal<digs[2 * Is + 1]>)... };
});
}
}
constexpr auto arr = 0x070A16B46B4D4144F79BDD9DD04A287C_H;
static_assert(arr.size() == 16);
static_assert(std::get<0>(arr) == 0x7);
static_assert(std::get<arr.size() - 1>(arr) == 0x7C);
Live demo
A completely compile-time static_assert version based on @Justin's answer.
The udl operator returns std::array
directly. You can simply define other udl operators that return std::tuple<std::integral_constant<char, c>...>
or even vector
using the HexArrayBuilder
implementation class.
This is c++11 version. The static_assert
that checks character validity can be written in c++17 way like the commented line.
#include <array>
#include <type_traits>
#include <tuple>
struct HexArrayHelper {
static constexpr bool valid(char c) { return ('0' <= c && c <= '9') || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f'); }
static constexpr char hex_value(char c) {
return ('0' <= c && c <= '9') ? c - '0'
: ('A' <= c && c <= 'F') ? c - 'A' + 10
: c - 'a' + 10;
}
static constexpr char build(char a, char b) {
return (hex_value(a) << 4) + hex_value(b);
};
};
template <char... cs>
struct HexArray {
static constexpr std::array<char, sizeof...(cs)> to_array() { return {cs...}; }
static constexpr std::tuple<std::integral_constant<char, cs>...> to_tuple() { return {}; }
};
template <typename T, char... cs>
struct HexArrayBuilder : T {};
template <char... built, char a, char b, char... cs>
struct HexArrayBuilder<HexArray<built...>, a, b, cs...> : HexArrayBuilder<HexArray<built..., HexArrayHelper::build(a, b)>, cs...> {
static_assert(HexArrayHelper::valid(a) && HexArrayHelper::valid(b), "Invalid hex character");
};
template <char zero, char x, char... cs>
struct HexByteArray : HexArrayBuilder<HexArray<>, cs...> {
static_assert(zero == '0' && (x == 'x' || x == 'X'), "Invalid prefix");
// static_assert(std::conjunction<std::bool_constant<HexArrayHelper::valid(cs)>...>::value, "Invalid hex character");
};
template <char... cs>
constexpr auto operator"" _hexarr() -> std::array<char, sizeof...(cs) / 2 - 1> {
static_assert(sizeof...(cs) % 2 == 0 && sizeof...(cs) >= 2, "Must be an even number of chars");
return HexByteArray<cs...>::to_array();
}
auto x = 0X1102030405060708abcdef_hexarr;