NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
hexadecimal.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_UTILITY_HEXADECIMAL_HPP__
2#define NEFORCE_CORE_UTILITY_HEXADECIMAL_HPP__
3
11
14NEFORCE_BEGIN_NAMESPACE__
15
21
29struct NEFORCE_API hexadecimal : iobject<hexadecimal>, ipackage<hexadecimal, int64_t> {
30public:
32 using base = ipackage;
33
34public:
35 static constexpr byte_t invalid_xdigit = numeric_traits<byte_t>::max();
36
42 static constexpr byte_t xdigit_value(const char c) noexcept {
43 if (c >= '0' && c <= '9') {
44 return c - '0';
45 }
46 if (c >= 'a' && c <= 'f') {
47 return 10 + (c - 'a');
48 }
49 if (c >= 'A' && c <= 'F') {
50 return 10 + (c - 'A');
51 }
52 return invalid_xdigit;
53 }
54
55 static NEFORCE_CONSTEXPR20 pair<bool, byte_t> xdigit_value(const char high, const char low) noexcept {
56 const byte_t xhigh = xdigit_value(high);
57 const byte_t xlow = xdigit_value(low);
58 if (xhigh == invalid_xdigit || xlow == invalid_xdigit) {
59 return {false, invalid_xdigit};
60 }
61 return pair<bool, byte_t>{true, high << 4 | low};
62 }
63
64private:
74 static NEFORCE_CONSTEXPR20 value_type parse_view(const string_view view) {
75 if (view.empty()) {
76 return 0;
77 }
78
79 bool negative = false;
80 size_t start = 0;
81
82 while (start < view.size() && is_space(view[start])) {
83 ++start;
84 }
85 if (start == view.size()) {
86 return 0;
87 }
88
89 if (view[start] == '-') {
90 negative = true;
91 ++start;
92 } else if (view[start] == '+') {
93 ++start;
94 }
95
96 if (start + 1 < view.size() && view[start] == '0' && (view[start + 1] == 'x' || view[start + 1] == 'X')) {
97 start += 2;
98 }
99
100 uint64_t result = 0;
101 while (start < view.size()) {
102 const char c = view[start++];
103 if (is_xdigit(c)) {
104 const int digit = xdigit_value(c);
105 if (result > (numeric_traits<uint64_t>::max() >> 4)) {
106 NEFORCE_THROW_EXCEPTION(value_exception("Hexadecimal value too large"));
107 }
108 result = (result << 4) | static_cast<uint64_t>(digit);
109 } else if (!is_space(c)) {
110 NEFORCE_THROW_EXCEPTION(value_exception("Invalid hexadecimal character"));
111 }
112 }
113
114 if (negative) {
115 if (result > static_cast<uint64_t>(numeric_traits<int64_t>::max()) + 1) {
116 NEFORCE_THROW_EXCEPTION(value_exception("Hexadecimal value out of range"));
117 }
118 return -static_cast<int64_t>(result);
119 }
120 if (result > static_cast<uint64_t>(numeric_traits<int64_t>::max())) {
121 NEFORCE_THROW_EXCEPTION(value_exception("Hexadecimal value out of range"));
122 }
123 return static_cast<value_type>(result);
124 }
125
126public:
131 explicit constexpr hexadecimal(const int16_t value) noexcept :
132 base(value) {}
133
138 explicit constexpr hexadecimal(const int32_t value) noexcept :
139 base(value) {}
140
145 explicit constexpr hexadecimal(const uint16_t value) noexcept :
146 base(value) {}
147
152 explicit constexpr hexadecimal(const uint32_t value) noexcept :
153 base(value) {}
154
159 explicit constexpr hexadecimal(const uint64_t value) noexcept :
160 base(value) {}
161
167 NEFORCE_CONSTEXPR20 explicit hexadecimal(const string_view view) :
168 base(parse_view(view)) {}
169
175 NEFORCE_CONSTEXPR20 explicit hexadecimal(const char* str) :
176 hexadecimal(string_view(str)) {}
177
183 NEFORCE_CONSTEXPR20 explicit hexadecimal(const string& str) :
184 hexadecimal(str.view()) {}
185
186 constexpr hexadecimal() noexcept = default;
187 NEFORCE_CONSTEXPR20 ~hexadecimal() = default;
188
189 constexpr hexadecimal(const hexadecimal&) noexcept = default;
190 constexpr hexadecimal(hexadecimal&&) noexcept = default;
191
192 constexpr hexadecimal& operator=(const hexadecimal& other) noexcept = default;
193 constexpr hexadecimal& operator=(hexadecimal&& other) noexcept = default;
194
195 explicit constexpr hexadecimal(const value_type value) noexcept :
196 base(value) {}
197
198 constexpr hexadecimal& operator=(const value_type value) noexcept {
199 value_ = value;
200 return *this;
201 }
202
207 NEFORCE_NODISCARD explicit constexpr operator bool() const noexcept { return value_ != static_cast<value_type>(0); }
208
215 NEFORCE_NODISCARD constexpr bool get_bit(const size_t position) const {
216 if (position >= 64) {
217 NEFORCE_THROW_EXCEPTION(value_exception("Bit position out of range"));
218 }
219 return (value_ >> position) & 1;
220 }
221
229 constexpr hexadecimal& set_bit(const size_t position, const bool bit_value_ = true) {
230 if (position >= 64) {
231 NEFORCE_THROW_EXCEPTION(value_exception("Bit position out of range"));
232 }
233 if (bit_value_) {
234 value_ |= (1ULL << position);
235 } else {
236 value_ &= ~(1ULL << position);
237 }
238 return *this;
239 }
240
247 constexpr hexadecimal& flip_bit(const size_t position) {
248 if (position >= 64) {
249 NEFORCE_THROW_EXCEPTION(value_exception("Bit position out of range"));
250 }
251 value_ ^= (1ULL << position);
252 return *this;
253 }
254
259 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 string to_string() const;
260
267 NEFORCE_NODISCARD static NEFORCE_CONSTEXPR20 hexadecimal parse(const string_view str) { return hexadecimal(str); }
268};
269
270template <>
271struct unpackage<hexadecimal> {
272 using type = int64_t;
273};
274
276
277NEFORCE_CONSTEXPR20 string hexadecimal::to_string() const { return format("{:#x}", *this); }
278
280 // Packages
282
283NEFORCE_BEGIN_LITERALS__
284
290
297NEFORCE_CONSTEXPR20 hexadecimal operator""_hex(const char* str, const size_t len) {
298 return hexadecimal{string_view(str, len)};
299}
300
306constexpr hexadecimal operator""_hex(const unsigned long long value) {
307 return hexadecimal{static_cast<int64_t>(value)};
308}
309 // UserLiterals
311
312NEFORCE_END_LITERALS__
313
314NEFORCE_END_NAMESPACE__
315#endif // NEFORCE_CORE_UTILITY_HEXADECIMAL_HPP__
NEFORCE_NODISCARD constexpr size_type size() const noexcept
获取字符串长度
NEFORCE_NODISCARD constexpr bool empty() const noexcept
检查是否为空
static NEFORCE_NODISCARD constexpr T max() noexcept
获取类型的最大值
字符串格式化功能
NEFORCE_CONST_FUNCTION NEFORCE_CONSTEXPR14 bool is_xdigit(const CharT c) noexcept
检查字符是否为十六进制数字
NEFORCE_PURE_FUNCTION NEFORCE_CONSTEXPR14 bool is_space(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位无符号整数类型
short int16_t
16位有符号整数类型
int int32_t
32位有符号整数类型
NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 string format(const string_view fmt, Args &&... args)
格式化字符串
basic_string_view< char > string_view
字符字符串视图
可解析对象接口
十六进制数值包装类
constexpr hexadecimal(const int32_t value) noexcept
从32位有符号整数构造
NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 string to_string() const
转换为字符串
constexpr hexadecimal(const uint16_t value) noexcept
从16位无符号整数构造
NEFORCE_CONSTEXPR20 hexadecimal(const char *str)
从C风格字符串构造
constexpr hexadecimal & set_bit(const size_t position, const bool bit_value_=true)
设置指定位的值
ipackage base
基类类型
constexpr hexadecimal(const uint32_t value) noexcept
从32位无符号整数构造
constexpr hexadecimal(const int16_t value) noexcept
从16位有符号整数构造
constexpr hexadecimal & flip_bit(const size_t position)
翻转指定位
NEFORCE_CONSTEXPR20 hexadecimal(const string &str)
从字符串对象构造
constexpr hexadecimal(const uint64_t value) noexcept
从64位无符号整数构造
static constexpr byte_t xdigit_value(const char c) noexcept
将十六进制字符转换为对应的数值
int64_t value_type
值类型
NEFORCE_CONSTEXPR20 hexadecimal(const string_view view)
从字符串视图构造
NEFORCE_NODISCARD constexpr bool get_bit(const size_t position) const
获取指定位的值
static NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 hexadecimal parse(const string_view str)
从字符串解析十六进制值
可解析对象接口
NEFORCE_NODISCARD constexpr package_type value() const noexcept
存储两个值的元组对
类型解包器模板
变量处理异常