NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
charset.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_STRING_CHARSET_HPP__
2#define NEFORCE_CORE_STRING_CHARSET_HPP__
3
11
14NEFORCE_BEGIN_NAMESPACE__
15
21
28class charset {
29public:
33 constexpr charset() noexcept = default;
34
40 NEFORCE_NODISCARD static constexpr charset from_char(const char c) noexcept {
41 charset cs;
42 cs.insert(c);
43 return cs;
44 }
45
54 NEFORCE_NODISCARD static constexpr charset range(const char lo, const char hi) noexcept {
55 charset cs;
56 const auto u_lo = static_cast<byte_t>(lo);
57 const auto u_hi = static_cast<byte_t>(hi);
58 if (u_lo > u_hi) {
59 return cs;
60 }
61
62 for (int w = 0; w < 4; ++w) {
63 const unsigned word_start = static_cast<unsigned>(w) * 64;
64 const unsigned word_end = word_start + 63;
65 if (u_hi < word_start || u_lo > word_end) {
66 continue;
67 }
68 const unsigned local_lo = (u_lo > word_start) ? (u_lo - word_start) : 0;
69 const unsigned local_hi = (u_hi < word_end) ? (u_hi - word_start) : 63;
70 const uint64_t mask = (~uint64_t{0} >> (63 - local_hi)) & (~uint64_t{0} << local_lo);
71 cs.bits_[w] = mask;
72 }
73 return cs;
74 }
75
80 constexpr void insert(const char c) noexcept {
81 const auto idx = static_cast<byte_t>(c);
82 bits_[idx >> 6] |= (uint64_t{1} << (idx & 63));
83 }
84
89 constexpr void erase(const char c) noexcept {
90 const auto idx = static_cast<byte_t>(c);
91 bits_[idx >> 6] &= ~(uint64_t{1} << (idx & 63));
92 }
93
99 NEFORCE_NODISCARD constexpr bool contains(const char c) const noexcept {
100 const auto idx = static_cast<byte_t>(c);
101 return (bits_[idx >> 6] & (uint64_t{1} << (idx & 63))) != 0;
102 }
103
108 NEFORCE_NODISCARD constexpr bool empty() const noexcept { return (bits_[0] | bits_[1] | bits_[2] | bits_[3]) == 0; }
109
115 NEFORCE_NODISCARD constexpr charset operator|(const charset other) const noexcept {
116 charset result;
117 result.bits_[0] = bits_[0] | other.bits_[0];
118 result.bits_[1] = bits_[1] | other.bits_[1];
119 result.bits_[2] = bits_[2] | other.bits_[2];
120 result.bits_[3] = bits_[3] | other.bits_[3];
121 return result;
122 }
123
129 NEFORCE_NODISCARD constexpr charset operator&(const charset other) const noexcept {
130 charset result;
131 result.bits_[0] = bits_[0] & other.bits_[0];
132 result.bits_[1] = bits_[1] & other.bits_[1];
133 result.bits_[2] = bits_[2] & other.bits_[2];
134 result.bits_[3] = bits_[3] & other.bits_[3];
135 return result;
136 }
137
142 NEFORCE_NODISCARD constexpr charset operator~() const noexcept {
143 charset result;
144 result.bits_[0] = ~bits_[0];
145 result.bits_[1] = ~bits_[1];
146 result.bits_[2] = ~bits_[2];
147 result.bits_[3] = ~bits_[3];
148 return result;
149 }
150
156 NEFORCE_NODISCARD constexpr charset operator-(const charset other) const noexcept { return *this & ~other; }
157
163 constexpr charset& operator|=(const charset other) noexcept {
164 bits_[0] |= other.bits_[0];
165 bits_[1] |= other.bits_[1];
166 bits_[2] |= other.bits_[2];
167 bits_[3] |= other.bits_[3];
168 return *this;
169 }
170
176 constexpr charset& operator&=(const charset other) noexcept {
177 bits_[0] &= other.bits_[0];
178 bits_[1] &= other.bits_[1];
179 bits_[2] &= other.bits_[2];
180 bits_[3] &= other.bits_[3];
181 return *this;
182 }
183
189 constexpr charset& operator-=(const charset other) noexcept {
190 bits_[0] &= ~other.bits_[0];
191 bits_[1] &= ~other.bits_[1];
192 bits_[2] &= ~other.bits_[2];
193 bits_[3] &= ~other.bits_[3];
194 return *this;
195 }
196
202 NEFORCE_NODISCARD constexpr bool operator==(const charset other) const noexcept {
203 return bits_[0] == other.bits_[0] && bits_[1] == other.bits_[1] && bits_[2] == other.bits_[2] &&
204 bits_[3] == other.bits_[3];
205 }
206
212 NEFORCE_NODISCARD constexpr bool operator!=(const charset other) const noexcept { return !(*this == other); }
213
218 NEFORCE_NODISCARD static constexpr charset empty_set() noexcept { return charset{}; }
219
224 NEFORCE_NODISCARD static constexpr charset universe() noexcept {
225 charset cs;
226 cs.bits_[0] = ~uint64_t{0};
227 cs.bits_[1] = ~uint64_t{0};
228 cs.bits_[2] = ~uint64_t{0};
229 cs.bits_[3] = ~uint64_t{0};
230 return cs;
231 }
232
237 NEFORCE_NODISCARD static constexpr charset ascii_alpha_lower() noexcept { return range('a', 'z'); }
238
243 NEFORCE_NODISCARD static constexpr charset ascii_alpha_upper() noexcept { return range('A', 'Z'); }
244
249 NEFORCE_NODISCARD static constexpr charset ascii_alpha() noexcept {
251 }
252
257 NEFORCE_NODISCARD static constexpr charset ascii_digit() noexcept { return range('0', '9'); }
258
263 NEFORCE_NODISCARD static constexpr charset ascii_alnum() noexcept { return ascii_alpha() | ascii_digit(); }
264
269 NEFORCE_NODISCARD static constexpr charset ascii_hex_digit() noexcept {
270 return ascii_digit() | range('A', 'F') | range('a', 'f');
271 }
272
277 NEFORCE_NODISCARD static constexpr charset ascii_blank() noexcept {
278 charset cs;
279 cs.bits_[0] = constants::BLANK_MASK;
280 return cs;
281 }
282
287 NEFORCE_NODISCARD static constexpr charset ascii_space() noexcept {
288 charset cs;
289 cs.bits_[0] = constants::SPACE_MASK;
290 return cs;
291 }
292
297 NEFORCE_NODISCARD static constexpr charset ascii_punct() noexcept {
298 charset cs;
299 cs.bits_[0] = constants::PUNCT_MASK_LOW;
300 cs.bits_[1] = constants::PUNCT_MASK_HIGH;
301 return cs;
302 }
303
308 NEFORCE_NODISCARD static constexpr charset ascii_cntrl() noexcept {
309 charset cs;
310 cs.bits_[0] = constants::CNTRL_MASK_LOW;
311 cs.bits_[1] = constants::CNTRL_MASK_HIGH;
312 return cs;
313 }
314
319 NEFORCE_NODISCARD static constexpr charset ascii_print() noexcept { return range(' ', '~'); }
320
325 NEFORCE_NODISCARD static constexpr charset ascii_graph() noexcept { return ascii_print() - ascii_blank(); }
326
327private:
328 uint64_t bits_[4] = {};
329};
330 // Charset
332
333NEFORCE_END_NAMESPACE__
334#endif // NEFORCE_CORE_STRING_CHARSET_HPP__
字符类型分类和转换函数
static constexpr charset ascii_digit() noexcept
ASCII 数字 '0'~'9'
constexpr bool operator!=(const charset other) const noexcept
不等比较
constexpr void erase(const char c) noexcept
移除字符
static constexpr charset ascii_hex_digit() noexcept
ASCII 十六进制数字 0~9, A~F, a~f
static constexpr charset ascii_print() noexcept
ASCII 可打印字符 32~126
static constexpr charset ascii_alpha() noexcept
ASCII 字母 'a'~'z' | 'A'~'Z'
constexpr bool contains(const char c) const noexcept
检查字符是否在集合中
static constexpr charset range(const char lo, const char hi) noexcept
从字符范围构造字符集 [lo, hi]
constexpr charset & operator&=(const charset other) noexcept
交集
constexpr charset operator-(const charset other) const noexcept
差集
constexpr charset operator&(const charset other) const noexcept
交集
static constexpr charset universe() noexcept
全集
static constexpr charset ascii_punct() noexcept
ASCII 标点符号
constexpr charset & operator-=(const charset other) noexcept
差集
static constexpr charset ascii_cntrl() noexcept
ASCII 控制字符 0~31 与 127
static constexpr charset ascii_blank() noexcept
ASCII 空白字符
static constexpr charset ascii_graph() noexcept
ASCII 图形字符
constexpr charset operator|(const charset other) const noexcept
并集
constexpr bool operator==(const charset other) const noexcept
相等比较
static constexpr charset ascii_alpha_upper() noexcept
ASCII 大写字母 'A'~'Z'
constexpr charset & operator|=(const charset other) noexcept
并集
static constexpr charset empty_set() noexcept
空集
static constexpr charset from_char(const char c) noexcept
从单个字符构造字符集
static constexpr charset ascii_alpha_lower() noexcept
ASCII 小写字母 'a'~'z'
constexpr charset operator~() const noexcept
补集
static constexpr charset ascii_space() noexcept
ASCII 空白字符集
constexpr void insert(const char c) noexcept
插入字符
constexpr bool empty() const noexcept
检查字符集是否为空
static constexpr charset ascii_alnum() noexcept
ASCII 字母与数字
constexpr charset() noexcept=default
默认构造函数,构造空字符集
unsigned char byte_t
字节类型,定义为无符号字符
unsigned long uint64_t
64位无符号整数类型
类型萃取