MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
char_types.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_STRING_CHAR_TYPES_HPP__
2#define MSTL_CORE_STRING_CHAR_TYPES_HPP__
3
11
14
20
23
30MSTL_INLINE17 constexpr uint64_t BLANK_MASK =
31 (1ULL << 9) | // \t
32 (1ULL << 32); // space
33
40MSTL_INLINE17 constexpr uint64_t SPACE_MASK =
41 BLANK_MASK |
42 (1ULL << 10) | // \n
43 (1ULL << 11) | // \v
44 (1ULL << 12) | // \f
45 (1ULL << 13); // \r
46
53MSTL_INLINE17 constexpr uint64_t PUNCT_MASK_LOW =
54 (1ULL << 33) | (1ULL << 34) | (1ULL << 35) | (1ULL << 36) | // !"#$ (33~36 < 64)
55 (1ULL << 37) | (1ULL << 38) | (1ULL << 39) | (1ULL << 40) | // %&'() (37~40 < 64)
56 (1ULL << 41) | (1ULL << 42) | (1ULL << 43) | (1ULL << 44) | // *+,- (41~44 < 64)
57 (1ULL << 45) | (1ULL << 46) | (1ULL << 47) | (1ULL << 58) | // ./: (45~47,58 < 64)
58 (1ULL << 59) | (1ULL << 60) | (1ULL << 61) | (1ULL << 62) | // ;<=> (59~62 < 64)
59 (1ULL << 63); // ?
60
67MSTL_INLINE17 constexpr uint64_t PUNCT_MASK_HIGH =
68 (1ULL << (64 - 64)) | (1ULL << (65 - 64)) | (1ULL << (91 - 64)) | // @(64)、A(65)、[(91)
69 (1ULL << (92 - 64)) | (1ULL << (93 - 64)) | (1ULL << (94 - 64)) |(1ULL << (95 - 64)) | // \\‍(92)、](93)、^(94)、_(95)
70 (1ULL << (96 - 64)) | (1ULL << (123 - 64)) | (1ULL << (124 - 64)) | (1ULL << (125 - 64)) | // `(96)、{(123)、|(124)、}(125)
71 (1ULL << (126 - 64)); // ~(126)
72
79MSTL_INLINE17 constexpr uint64_t CNTRL_MASK_LOW =
80 (1ULL << 0) | (1ULL << 1) | (1ULL << 2) | (1ULL << 3) | // 0-3 (0~3 < 64)
81 (1ULL << 4) | (1ULL << 5) | (1ULL << 6) | (1ULL << 7) | // 4-7 (4~7 < 64)
82 (1ULL << 8) | (1ULL << 9) | (1ULL << 10) | (1ULL << 11) | // 8-11 (8~11 < 64)
83 (1ULL << 12) | (1ULL << 13) | (1ULL << 14) | (1ULL << 15) | // 12-15 (12~15 < 64)
84 (1ULL << 16) | (1ULL << 17) | (1ULL << 18) | (1ULL << 19) | // 16-19 (16~19 < 64)
85 (1ULL << 20) | (1ULL << 21) | (1ULL << 22) | (1ULL << 23) | // 20-23 (20~23 < 64)
86 (1ULL << 24) | (1ULL << 25) | (1ULL << 26) | (1ULL << 27) | // 24-27 (24~27 < 64)
87 (1ULL << 28) | (1ULL << 29) | (1ULL << 30) | (1ULL << 31); // 30-31 (30~31 < 64)
88
95MSTL_INLINE17 constexpr uint64_t CNTRL_MASK_HIGH =
96 (1ULL << (127 - 64)); // DEL(127)
97
100
101
112template <typename CharT>
113MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 bool is_ctype(const CharT c, uint64_t mask_low, uint64_t mask_high) noexcept {
114 static_assert(is_character_v<CharT>, "character type is necessary");
115 const auto uc = static_cast<make_unsigned_t<CharT>>(c);
116 if (uc > 127) return false;
117 if (uc <= 63) return (mask_low & (1ULL << uc)) != 0;
118 const auto offset = uc - 64;
119 return (mask_high & (1ULL << offset)) != 0;
120}
121
128template <typename CharT>
129MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 bool is_punct(const CharT c) noexcept {
130 return _MSTL is_ctype(c, _CONSTANTS PUNCT_MASK_LOW, _CONSTANTS PUNCT_MASK_HIGH);
131}
132
141template <typename CharT>
142MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 bool is_cntrl(const CharT c) noexcept {
143 return _MSTL is_ctype(c, _CONSTANTS CNTRL_MASK_LOW, _CONSTANTS CNTRL_MASK_HIGH);
144}
145
154template <typename CharT>
155MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 bool is_print(const CharT c) noexcept {
156 static_assert(is_character_v<CharT>, "character type is necessary");
157 const auto uc = static_cast<make_unsigned_t<CharT>>(c);
158 return uc <= 127 && !_MSTL is_cntrl(c);
159}
160
169template <typename CharT>
170MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 bool is_blank(const CharT c) noexcept {
171 static_assert(is_character_v<CharT>, "character type is necessary");
172 const auto uc = static_cast<make_unsigned_t<CharT>>(c);
173 return uc < 64 && (_CONSTANTS BLANK_MASK & (1ULL << uc)) != 0;
174}
175
184template <typename CharT>
185MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 bool is_graph(const CharT c) noexcept {
186 return _MSTL is_print(c) && !_MSTL is_blank(c);
187}
188
197template <typename CharT>
198MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 bool is_ascii(const CharT c) noexcept {
199 static_assert(is_character_v<CharT>, "character type is necessary");
200 const auto uc = static_cast<make_unsigned_t<CharT>>(c);
201 return uc <= 127;
202}
203
212template <typename CharT>
213MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 bool is_space(const CharT c) noexcept {
214 static_assert(is_character_v<CharT>, "character type is necessary");
215 const auto uc = static_cast<make_unsigned_t<CharT>>(c);
216 return uc < 64 && (_CONSTANTS SPACE_MASK & (1ULL << uc)) != 0;
217}
218
227template <typename CharT>
228MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 bool is_alpha(const CharT c) noexcept {
229 static_assert(is_character_v<CharT>, "character type is necessary");
230 const auto uc = static_cast<make_unsigned_t<CharT>>(c);
231 if (uc > 127) return false;
232 return (uc & 0xDF) >= 'A' && (uc & 0xDF) <= 'Z';
233}
234
243template <typename CharT>
244MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 bool is_digit(const CharT c) noexcept {
245 static_assert(is_character_v<CharT>, "character type is necessary");
246 const auto uc = static_cast<make_unsigned_t<CharT>>(c);
247 if (uc > 127) return false;
248 return (uc & 0xF0) == 0x30 && (uc & 0x0F) <= 9;
249}
250
259template <typename CharT>
260MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 bool is_xdigit(const CharT c) noexcept {
261 static_assert(is_character_v<CharT>, "character type is necessary");
262 const auto uc = static_cast<make_unsigned_t<CharT>>(c);
263 if (uc > 127) return false;
264 const bool is_09 = (uc & 0xF0) == 0x30 && (uc & 0x0F) <= 0x09;
265 const bool is_AF = (uc & 0xF0) == 0x40 && (uc & 0x0F) >= 0x01 && (uc & 0x0F) <= 0x06;
266 const bool is_af = (uc & 0xF0) == 0x60 && (uc & 0x0F) >= 0x01 && (uc & 0x0F) <= 0x06;
267 return is_09 || is_AF || is_af;
268}
269
276template <typename CharT>
277MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 bool is_alpha_or_digit(const CharT c) noexcept {
278 return _MSTL is_alpha(c) || _MSTL is_digit(c);
279}
280
287template <typename CharT>
288MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 bool is_digit_or_alpha(const CharT c) noexcept {
289 return _MSTL is_digit(c) || _MSTL is_alpha(c);
290}
291 // CharTypeChecking
293
299
307MSTL_CONST_FUNCTION constexpr bool is_high_surrogate(const char16_t c) noexcept {
308 return c >= 0xD800 && c <= 0xDBFF;
309}
310
318MSTL_CONST_FUNCTION constexpr bool is_low_surrogate(const char16_t c) noexcept {
319 return c >= 0xDC00 && c <= 0xDFFF;
320}
321
330MSTL_CONST_FUNCTION constexpr uint32_t combine_surrogates(const char16_t high, const char16_t low) noexcept {
331 return 0x10000 + ((static_cast<uint32_t>(high) - 0xD800) << 10) + (static_cast<uint32_t>(low) - 0xDC00);
332}
333 // UnicodeSurrogate
335
337#endif // MSTL_CORE_STRING_CHAR_TYPES_HPP__
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 bool is_blank(const CharT c) noexcept
检查字符是否为空白字符
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 bool is_graph(const CharT c) noexcept
检查字符是否为图形字符
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 bool is_cntrl(const CharT c) noexcept
检查字符是否为控制字符
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 bool is_ascii(const CharT c) noexcept
检查字符是否为ASCII字符
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 bool is_digit_or_alpha(const CharT c) noexcept
检查字符是否为数字或字母
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 bool is_alpha(const CharT c) noexcept
检查字符是否为字母
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 bool is_print(const CharT c) noexcept
检查字符是否为可打印字符
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 bool is_ctype(const CharT c, uint64_t mask_low, uint64_t mask_high) noexcept
通用字符类型检查函数
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 bool is_space(const CharT c) noexcept
检查字符是否为空白字符
MSTL_PURE_FUNCTION MSTL_CONSTEXPR14 bool is_punct(const CharT c) noexcept
检查字符是否为标点符号
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 bool is_alpha_or_digit(const CharT c) noexcept
检查字符是否为字母或数字
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 bool is_digit(const CharT c) noexcept
检查字符是否为数字
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 bool is_xdigit(const CharT c) noexcept
检查字符是否为十六进制数字
unsigned int uint32_t
32位无符号整数类型
unsigned long long uint64_t
64位无符号整数类型
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_CONSTANTS__
结束constants命名空间
#define _CONSTANTS
constants命名空间前缀
#define MSTL_BEGIN_CONSTANTS__
开始constants命名空间
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
typename make_unsigned< T >::type make_unsigned_t
make_unsigned的便捷别名
MSTL_CONST_FUNCTION constexpr bool is_high_surrogate(const char16_t c) noexcept
检查字符是否为高代理项
MSTL_CONST_FUNCTION constexpr bool is_low_surrogate(const char16_t c) noexcept
检查字符是否为低代理项
MSTL_CONST_FUNCTION constexpr uint32_t combine_surrogates(const char16_t high, const char16_t low) noexcept
组合高代理项和低代理项为完整的Unicode码点
MSTL类型萃取