gpt4 book ai didi

c++ - 使用 C++ 用户定义的文字来初始化数组

转载 作者:IT老高 更新时间:2023-10-28 23:00:52 39 4
gpt4 key购买 nike

我有一堆测试 vector ,以十六进制字符串的形式呈现:

MSG: 6BC1BEE22E409F96E93D7E117393172A
MAC: 070A16B46B4D4144F79BDD9DD04A287C
MSG: 6BC1BEE22E409F96E93D7E117393172AAE2D8A57
MAC: 7D85449EA6EA19C823A7BF78837DFADE

等等。我需要以某种方式将它们放入 C++ 程序中,而不需要太多的编辑。有多种选择:

  • 将测试 vector 手动编辑为 0x6B,0xC1,0xBE,... 形式
  • 手动将测试 vector 编辑为“6BC1BEE22E409F96E93D7E117393172A”形式,并编写一个函数在运行时将其转换为字节数组。
  • 编写程序来解析测试 vector 并输出 C++ 代码。

但我最终使用的是:

  • 用户定义的文字,

因为好玩。我定义了一个助手类 HexByteArray和一个用户定义的文字运算符 HexByteArray operator "" _$ (const char* s)解析 "0xXX...XX" 形式的字符串, 其中 XX...XX是偶数个十六进制数字。 HexByteArray包括到 const uint8_t* 的转换运算符和 std::vector<uint8_t> .所以现在我可以写例如

struct {
std::vector<uint8_t> MSG ;
uint8_t* MAC ;
} Test1 = {
0x6BC1BEE22E409F96E93D7E117393172A_$,
0x070A16B46B4D4144F79BDD9DD04A287C_$
} ;

效果很好。但现在这是我的问题:我也可以对数组执行此操作吗?例如:

uint8_t MAC[16] = 0x070A16B46B4D4144F79BDD9DD04A287C_$ ;

甚至

uint8_t MAC[] = 0x070A16B46B4D4144F79BDD9DD04A287C_$ ;

我不知道如何进行这项工作。要初始化一个数组,我似乎需要一个 std::initializer_list .但据我所知,只有编译器可以实例化这样的东西。有什么想法吗?


这是我的代码:

HexByteArray.h

#include <cstdint>
#include <vector>

class HexByteArray
{
public:
HexByteArray (const char* s) ;
~HexByteArray() { delete[] a ; }

operator const uint8_t*() && { const uint8_t* t = a ; a = 0 ; return t ; }
operator std::vector<uint8_t>() &&
{
std::vector<uint8_t> v ( a, a + len ) ;
a = 0 ;
return v ;
}

class ErrorInvalidPrefix { } ;
class ErrorHexDigit { } ;
class ErrorOddLength { } ;

private:
const uint8_t* a = 0 ;
size_t len ;
} ;

inline HexByteArray operator "" _$ (const char* s)
{
return HexByteArray (s) ;
}

HexByteArray.cpp

#include "HexByteArray.h"

#include <cctype>
#include <cstring>

HexByteArray::HexByteArray (const char* s)
{
if (s[0] != '0' || toupper (s[1]) != 'X') throw ErrorInvalidPrefix() ;
s += 2 ;

// Special case: 0x0_$ is an empty array (because 0x_$ is invalid C++ syntax)
if (!strcmp (s, "0"))
{
a = nullptr ; len = 0 ;
}
else
{
for (len = 0 ; s[len] ; len++) if (!isxdigit (s[len])) throw ErrorHexDigit() ;
if (len & 1) throw ErrorOddLength() ;
len /= 2 ;
uint8_t* t = new uint8_t[len] ;
for (size_t i = 0 ; i < len ; i++, s += 2)
sscanf (s, "%2hhx", &t[i]) ;
a = t ;
}
}

最佳答案

使用 numeric literal operator template , 签名:

template <char...>
result_type operator "" _x();

另外,由于数据在编译时是已知的,我们不妨将所有内容都设为 constexpr。请注意,我们使用 std::array而不是 C 样式的数组:

#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

示例用法:

auto data_array = 0x6BC1BEE22E409F96E93D7E117393172A_$ .to_array();
std::vector<std::uint8_t> data_vector = 0x6BC1BEE22E409F96E93D7E117393172A_$;

附带说明,identifier 中的 $实际上是一个 gcc 扩展,所以它是非标准的 C++。考虑使用 _$ 以外的 UDL。

关于c++ - 使用 C++ 用户定义的文字来初始化数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53913800/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com