File indexing completed on 2025-12-16 09:41:01
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 #ifndef ABSL_STRINGS_CHARSET_H_
0047 #define ABSL_STRINGS_CHARSET_H_
0048
0049 #include <cstddef>
0050 #include <cstdint>
0051 #include <cstring>
0052
0053 #include "absl/base/macros.h"
0054 #include "absl/base/port.h"
0055 #include "absl/strings/string_view.h"
0056
0057 namespace absl {
0058
0059 class CharSet {
0060 public:
0061 constexpr CharSet() : m_() {}
0062
0063
0064 constexpr explicit CharSet(absl::string_view str) : m_() {
0065 for (char c : str) {
0066 SetChar(static_cast<unsigned char>(c));
0067 }
0068 }
0069
0070 constexpr bool contains(char c) const {
0071 return ((m_[static_cast<unsigned char>(c) / 64] >>
0072 (static_cast<unsigned char>(c) % 64)) &
0073 0x1) == 0x1;
0074 }
0075
0076 constexpr bool empty() const {
0077 for (uint64_t c : m_) {
0078 if (c != 0) return false;
0079 }
0080 return true;
0081 }
0082
0083
0084 static constexpr CharSet Char(char x) {
0085 return CharSet(CharMaskForWord(x, 0), CharMaskForWord(x, 1),
0086 CharMaskForWord(x, 2), CharMaskForWord(x, 3));
0087 }
0088
0089
0090 static constexpr CharSet Range(char lo, char hi) {
0091 return CharSet(RangeForWord(lo, hi, 0), RangeForWord(lo, hi, 1),
0092 RangeForWord(lo, hi, 2), RangeForWord(lo, hi, 3));
0093 }
0094
0095 friend constexpr CharSet operator&(const CharSet& a, const CharSet& b) {
0096 return CharSet(a.m_[0] & b.m_[0], a.m_[1] & b.m_[1], a.m_[2] & b.m_[2],
0097 a.m_[3] & b.m_[3]);
0098 }
0099
0100 friend constexpr CharSet operator|(const CharSet& a, const CharSet& b) {
0101 return CharSet(a.m_[0] | b.m_[0], a.m_[1] | b.m_[1], a.m_[2] | b.m_[2],
0102 a.m_[3] | b.m_[3]);
0103 }
0104
0105 friend constexpr CharSet operator~(const CharSet& a) {
0106 return CharSet(~a.m_[0], ~a.m_[1], ~a.m_[2], ~a.m_[3]);
0107 }
0108
0109
0110 static constexpr CharSet AsciiUppercase() { return CharSet::Range('A', 'Z'); }
0111 static constexpr CharSet AsciiLowercase() { return CharSet::Range('a', 'z'); }
0112 static constexpr CharSet AsciiDigits() { return CharSet::Range('0', '9'); }
0113 static constexpr CharSet AsciiAlphabet() {
0114 return AsciiLowercase() | AsciiUppercase();
0115 }
0116 static constexpr CharSet AsciiAlphanumerics() {
0117 return AsciiDigits() | AsciiAlphabet();
0118 }
0119 static constexpr CharSet AsciiHexDigits() {
0120 return AsciiDigits() | CharSet::Range('A', 'F') | CharSet::Range('a', 'f');
0121 }
0122 static constexpr CharSet AsciiPrintable() {
0123 return CharSet::Range(0x20, 0x7e);
0124 }
0125 static constexpr CharSet AsciiWhitespace() { return CharSet("\t\n\v\f\r "); }
0126 static constexpr CharSet AsciiPunctuation() {
0127 return AsciiPrintable() & ~AsciiWhitespace() & ~AsciiAlphanumerics();
0128 }
0129
0130 private:
0131 constexpr CharSet(uint64_t b0, uint64_t b1, uint64_t b2, uint64_t b3)
0132 : m_{b0, b1, b2, b3} {}
0133
0134 static constexpr uint64_t RangeForWord(char lo, char hi, uint64_t word) {
0135 return OpenRangeFromZeroForWord(static_cast<unsigned char>(hi) + 1, word) &
0136 ~OpenRangeFromZeroForWord(static_cast<unsigned char>(lo), word);
0137 }
0138
0139
0140 static constexpr uint64_t OpenRangeFromZeroForWord(uint64_t upper,
0141 uint64_t word) {
0142 return (upper <= 64 * word) ? 0
0143 : (upper >= 64 * (word + 1))
0144 ? ~static_cast<uint64_t>(0)
0145 : (~static_cast<uint64_t>(0) >> (64 - upper % 64));
0146 }
0147
0148 static constexpr uint64_t CharMaskForWord(char x, uint64_t word) {
0149 return (static_cast<unsigned char>(x) / 64 == word)
0150 ? (static_cast<uint64_t>(1)
0151 << (static_cast<unsigned char>(x) % 64))
0152 : 0;
0153 }
0154
0155 constexpr void SetChar(unsigned char c) {
0156 m_[c / 64] |= static_cast<uint64_t>(1) << (c % 64);
0157 }
0158
0159 uint64_t m_[4];
0160 };
0161
0162 }
0163
0164 #endif