1#ifndef NEFORCE_CORE_UTILITY_HEXADECIMAL_HPP__
2#define NEFORCE_CORE_UTILITY_HEXADECIMAL_HPP__
14NEFORCE_BEGIN_NAMESPACE__
43 if (c >=
'0' && c <=
'9') {
46 if (c >=
'a' && c <=
'f') {
47 return 10 + (c -
'a');
49 if (c >=
'A' && c <=
'F') {
50 return 10 + (c -
'A');
52 return invalid_xdigit;
67 if (xhigh == invalid_xdigit || xlow == invalid_xdigit) {
68 return {
false, invalid_xdigit};
83 static NEFORCE_CONSTEXPR20 value_type parse_view(
const string_view view) {
88 bool negative =
false;
94 if (start == view.
size()) {
98 if (view[start] ==
'-') {
101 }
else if (view[start] ==
'+') {
105 if (start + 1 < view.
size() && view[start] ==
'0' && (view[start + 1] ==
'x' || view[start + 1] ==
'X')) {
110 while (start < view.
size()) {
111 const char c = view[start++];
113 const int digit = xdigit_value(c);
115 NEFORCE_THROW_EXCEPTION(value_exception(
"Hexadecimal value too large"));
117 result = (result << 4) | static_cast<uint64_t>(digit);
119 NEFORCE_THROW_EXCEPTION(value_exception(
"Invalid hexadecimal character"));
125 NEFORCE_THROW_EXCEPTION(value_exception(
"Hexadecimal value out of range"));
127 return -
static_cast<int64_t>(result);
130 NEFORCE_THROW_EXCEPTION(value_exception(
"Hexadecimal value out of range"));
132 return static_cast<value_type
>(result);
177 base(parse_view(view)) {}
204 explicit constexpr
hexadecimal(const value_type value) noexcept :
207 constexpr hexadecimal& operator=(
const value_type value)
noexcept {
216 NEFORCE_NODISCARD
explicit constexpr operator bool() const noexcept {
return value_ !=
static_cast<value_type>(0); }
224 NEFORCE_NODISCARD
constexpr bool get_bit(
const size_t position)
const {
225 if (position >= 64) {
239 if (position >= 64) {
257 if (position >= 64) {
268 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20
string to_string()
const;
292NEFORCE_BEGIN_LITERALS__
306NEFORCE_CONSTEXPR20
hexadecimal operator""_hex(
const char* str,
const size_t len) {
315constexpr hexadecimal operator""_hex(
const unsigned long long value) {
321NEFORCE_END_LITERALS__
323NEFORCE_END_NAMESPACE__
constexpr size_type size() const noexcept
获取字符串长度
constexpr bool empty() const noexcept
检查是否为空
static constexpr T max() noexcept
获取类型的最大值
constexpr bool is_space(const CharT c) noexcept
检查字符是否为空白字符
constexpr bool is_xdigit(const CharT c) noexcept
检查字符是否为十六进制数字
unsigned char byte_t
字节类型,定义为无符号字符
unsigned int uint32_t
32位无符号整数类型
long long int64_t
64位有符号整数类型
unsigned short uint16_t
16位无符号整数类型
unsigned long long uint64_t
64位无符号整数类型
basic_string_view< char > string_view
字符字符串视图
constexpr bool get_bit(const size_t position) const
获取指定位的值
constexpr hexadecimal(const int32_t value) noexcept
从32位有符号整数构造
static constexpr pair< bool, byte_t > xdigit_value(const char high, const char low) noexcept
将两个十六进制数字字符转换为对应的字节值。
constexpr hexadecimal(const uint16_t value) noexcept
从16位无符号整数构造
constexpr hexadecimal & set_bit(const size_t position, const bool bit_value_=true)
设置指定位的值
constexpr hexadecimal(const string_view view)
从字符串视图构造
constexpr hexadecimal(const uint32_t value) noexcept
从32位无符号整数构造
constexpr hexadecimal(const string &str)
从字符串对象构造
constexpr hexadecimal(const int16_t value) noexcept
从16位有符号整数构造
constexpr hexadecimal & flip_bit(const size_t position)
翻转指定位
constexpr hexadecimal(const uint64_t value) noexcept
从64位无符号整数构造
constexpr string to_string() const
转换为字符串
static constexpr byte_t xdigit_value(const char c) noexcept
将十六进制字符转换为对应的数值
constexpr hexadecimal(const char *str)
从C风格字符串构造
static constexpr hexadecimal parse(const string_view str)
从字符串解析十六进制值
constexpr package_type value() const noexcept