1#ifndef NEFORCE_CORE_STRING_CHARSET_HPP__
2#define NEFORCE_CORE_STRING_CHARSET_HPP__
14NEFORCE_BEGIN_NAMESPACE__
54 NEFORCE_NODISCARD
static constexpr charset range(
const char lo,
const char hi)
noexcept {
56 const auto u_lo =
static_cast<byte_t>(lo);
57 const auto u_hi =
static_cast<byte_t>(hi);
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) {
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;
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));
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));
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;
108 NEFORCE_NODISCARD
constexpr bool empty() const noexcept {
return (bits_[0] | bits_[1] | bits_[2] | bits_[3]) == 0; }
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];
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];
144 result.bits_[0] = ~bits_[0];
145 result.bits_[1] = ~bits_[1];
146 result.bits_[2] = ~bits_[2];
147 result.bits_[3] = ~bits_[3];
164 bits_[0] |= other.bits_[0];
165 bits_[1] |= other.bits_[1];
166 bits_[2] |= other.bits_[2];
167 bits_[3] |= other.bits_[3];
177 bits_[0] &= other.bits_[0];
178 bits_[1] &= other.bits_[1];
179 bits_[2] &= other.bits_[2];
180 bits_[3] &= other.bits_[3];
190 bits_[0] &= ~other.bits_[0];
191 bits_[1] &= ~other.bits_[1];
192 bits_[2] &= ~other.bits_[2];
193 bits_[3] &= ~other.bits_[3];
203 return bits_[0] == other.bits_[0] && bits_[1] == other.bits_[1] && bits_[2] == other.bits_[2] &&
204 bits_[3] == other.bits_[3];
212 NEFORCE_NODISCARD
constexpr bool operator!=(
const charset other)
const noexcept {
return !(*
this == other); }
279 cs.bits_[0] = constants::BLANK_MASK;
289 cs.bits_[0] = constants::SPACE_MASK;
299 cs.bits_[0] = constants::PUNCT_MASK_LOW;
300 cs.bits_[1] = constants::PUNCT_MASK_HIGH;
310 cs.bits_[0] = constants::CNTRL_MASK_LOW;
311 cs.bits_[1] = constants::CNTRL_MASK_HIGH;
333NEFORCE_END_NAMESPACE__
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位无符号整数类型