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
64 static NEFORCE_CONSTEXPR20 pair<bool, byte_t> xdigit_value(const char high, const char low) noexcept {
65 const byte_t xhigh = xdigit_value(high);
66 const byte_t xlow = xdigit_value(low);
67 if (xhigh == invalid_xdigit || xlow == invalid_xdigit) {
68 return {false, invalid_xdigit};
69 }
70 return pair<bool, byte_t>{true, xhigh << 4 | xlow};
71 }
72
73private:
83 static NEFORCE_CONSTEXPR20 value_type parse_view(const string_view view) {
84 if (view.empty()) {
85 return 0;
86 }
87
88 bool negative = false;
89 size_t start = 0;
90
91 while (start < view.size() && is_space(view[start])) {
92 ++start;
93 }
94 if (start == view.size()) {
95 return 0;
96 }
97
98 if (view[start] == '-') {
99 negative = true;
100 ++start;
101 } else if (view[start] == '+') {
102 ++start;
103 }
104
105 if (start + 1 < view.size() && view[start] == '0' && (view[start + 1] == 'x' || view[start + 1] == 'X')) {
106 start += 2;
107 }
108
109 uint64_t result = 0;
110 while (start < view.size()) {
111 const char c = view[start++];
112 if (is_xdigit(c)) {
113 const int digit = xdigit_value(c);
114 if (result > (numeric_traits<uint64_t>::max() >> 4)) {
115 NEFORCE_THROW_EXCEPTION(value_exception("Hexadecimal value too large"));
116 }
117 result = (result << 4) | static_cast<uint64_t>(digit);
118 } else if (!is_space(c)) {
119 NEFORCE_THROW_EXCEPTION(value_exception("Invalid hexadecimal character"));
120 }
121 }
122
123 if (negative) {
124 if (result > static_cast<uint64_t>(numeric_traits<int64_t>::max()) + 1) {
125 NEFORCE_THROW_EXCEPTION(value_exception("Hexadecimal value out of range"));
126 }
127 return -static_cast<int64_t>(result);
128 }
129 if (result > static_cast<uint64_t>(numeric_traits<int64_t>::max())) {
130 NEFORCE_THROW_EXCEPTION(value_exception("Hexadecimal value out of range"));
131 }
132 return static_cast<value_type>(result);
133 }
134
135public:
140 explicit constexpr hexadecimal(const int16_t value) noexcept :
141 base(value) {}
142
147 explicit constexpr hexadecimal(const int32_t value) noexcept :
148 base(value) {}
149
154 explicit constexpr hexadecimal(const uint16_t value) noexcept :
155 base(value) {}
156
161 explicit constexpr hexadecimal(const uint32_t value) noexcept :
162 base(value) {}
163
168 explicit constexpr hexadecimal(const uint64_t value) noexcept :
169 base(static_cast<int64_t>(value)) {}
170
176 NEFORCE_CONSTEXPR20 explicit hexadecimal(const string_view view) :
177 base(parse_view(view)) {}
178
184 NEFORCE_CONSTEXPR20 explicit hexadecimal(const char* str) :
185 hexadecimal(string_view(str)) {}
186
192 NEFORCE_CONSTEXPR20 explicit hexadecimal(const string& str) :
193 hexadecimal(str.view()) {}
194
195 constexpr hexadecimal() noexcept = default;
196 NEFORCE_CONSTEXPR20 ~hexadecimal() = default;
197
198 constexpr hexadecimal(const hexadecimal&) noexcept = default;
199 constexpr hexadecimal(hexadecimal&&) noexcept = default;
200
201 constexpr hexadecimal& operator=(const hexadecimal& other) noexcept = default;
202 constexpr hexadecimal& operator=(hexadecimal&& other) noexcept = default;
203
204 explicit constexpr hexadecimal(const value_type value) noexcept :
205 base(value) {}
206
207 constexpr hexadecimal& operator=(const value_type value) noexcept {
208 value_ = value;
209 return *this;
210 }
211
216 NEFORCE_NODISCARD explicit constexpr operator bool() const noexcept { return value_ != static_cast<value_type>(0); }
217
224 NEFORCE_NODISCARD constexpr bool get_bit(const size_t position) const {
225 if (position >= 64) {
226 NEFORCE_THROW_EXCEPTION(value_exception("Bit position out of range"));
227 }
228 return ((value_ >> position) & static_cast<package_type>(1)) != 0;
229 }
230
238 constexpr hexadecimal& set_bit(const size_t position, const bool bit_value_ = true) {
239 if (position >= 64) {
240 NEFORCE_THROW_EXCEPTION(value_exception("Bit position out of range"));
241 }
242 if (bit_value_) {
243 value_ |= static_cast<int64_t>(1ULL << position);
244 } else {
245 value_ &= static_cast<int64_t>(~(1ULL << position));
246 }
247 return *this;
248 }
249
256 constexpr hexadecimal& flip_bit(const size_t position) {
257 if (position >= 64) {
258 NEFORCE_THROW_EXCEPTION(value_exception("Bit position out of range"));
259 }
260 value_ ^= static_cast<int64_t>(1ULL << position);
261 return *this;
262 }
263
268 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 string to_string() const;
269
276 NEFORCE_NODISCARD static NEFORCE_CONSTEXPR20 hexadecimal parse(const string_view str) { return hexadecimal(str); }
277};
278
279template <>
280struct unpackage<hexadecimal> {
281 using type = int64_t;
282};
283
285
286NEFORCE_CONSTEXPR20 string hexadecimal::to_string() const { return format("{:#x}", *this); }
287
289 // Packages
291
292NEFORCE_BEGIN_LITERALS__
293
299
306NEFORCE_CONSTEXPR20 hexadecimal operator""_hex(const char* str, const size_t len) {
307 return hexadecimal{string_view(str, len)};
308}
309
315constexpr hexadecimal operator""_hex(const unsigned long long value) {
316 return hexadecimal{static_cast<int64_t>(value)};
317}
318 // UserLiterals
320
321NEFORCE_END_LITERALS__
322
323NEFORCE_END_NAMESPACE__
324#endif // NEFORCE_CORE_UTILITY_HEXADECIMAL_HPP__
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位无符号整数类型
short int16_t
16位有符号整数类型
int int32_t
32位有符号整数类型
constexpr string format(const string_view fmt, Args &&... args)
格式化字符串
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)
从字符串视图构造
ipackage base
基类类型
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
将十六进制字符转换为对应的数值
int64_t value_type
值类型
constexpr hexadecimal(const char *str)
从C风格字符串构造
static constexpr hexadecimal parse(const string_view str)
从字符串解析十六进制值
可解析对象接口
constexpr package_type value() const noexcept
存储两个值的元组对
类型解包器模板
变量处理异常