1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // Character Map Class
16 //
17 // A fast, bit-vector map for 8-bit unsigned characters.
18 // This class is useful for non-character purposes as well.
19 
20 #ifndef ABSL_STRINGS_INTERNAL_CHAR_MAP_H_
21 #define ABSL_STRINGS_INTERNAL_CHAR_MAP_H_
22 
23 #include <cstddef>
24 #include <cstdint>
25 #include <cstring>
26 
27 #include "absl/base/macros.h"
28 #include "absl/base/port.h"
29 
30 namespace absl {
31 ABSL_NAMESPACE_BEGIN
32 namespace strings_internal {
33 
34 class Charmap {
35  public:
Charmap()36   constexpr Charmap() : m_() {}
37 
38   // Initializes with a given char*.  Note that NUL is not treated as
39   // a terminator, but rather a char to be flicked.
Charmap(const char * str,int len)40   Charmap(const char* str, int len) : m_() {
41     while (len--) SetChar(*str++);
42   }
43 
44   // Initializes with a given char*.  NUL is treated as a terminator
45   // and will not be in the charmap.
Charmap(const char * str)46   explicit Charmap(const char* str) : m_() {
47     while (*str) SetChar(*str++);
48   }
49 
contains(unsigned char c)50   constexpr bool contains(unsigned char c) const {
51     return (m_[c / 64] >> (c % 64)) & 0x1;
52   }
53 
54   // Returns true if and only if a character exists in both maps.
IntersectsWith(const Charmap & c)55   bool IntersectsWith(const Charmap& c) const {
56     for (size_t i = 0; i < ABSL_ARRAYSIZE(m_); ++i) {
57       if ((m_[i] & c.m_[i]) != 0) return true;
58     }
59     return false;
60   }
61 
IsZero()62   bool IsZero() const {
63     for (uint64_t c : m_) {
64       if (c != 0) return false;
65     }
66     return true;
67   }
68 
69   // Containing only a single specified char.
Char(char x)70   static constexpr Charmap Char(char x) {
71     return Charmap(CharMaskForWord(x, 0), CharMaskForWord(x, 1),
72                    CharMaskForWord(x, 2), CharMaskForWord(x, 3));
73   }
74 
75   // Containing all the chars in the C-string 's'.
FromString(const char * s)76   static constexpr Charmap FromString(const char* s) {
77     Charmap ret;
78     while (*s) ret = ret | Char(*s++);
79     return ret;
80   }
81 
82   // Containing all the chars in the closed interval [lo,hi].
Range(char lo,char hi)83   static constexpr Charmap Range(char lo, char hi) {
84     return Charmap(RangeForWord(lo, hi, 0), RangeForWord(lo, hi, 1),
85                    RangeForWord(lo, hi, 2), RangeForWord(lo, hi, 3));
86   }
87 
88   friend constexpr Charmap operator&(const Charmap& a, const Charmap& b) {
89     return Charmap(a.m_[0] & b.m_[0], a.m_[1] & b.m_[1], a.m_[2] & b.m_[2],
90                    a.m_[3] & b.m_[3]);
91   }
92 
93   friend constexpr Charmap operator|(const Charmap& a, const Charmap& b) {
94     return Charmap(a.m_[0] | b.m_[0], a.m_[1] | b.m_[1], a.m_[2] | b.m_[2],
95                    a.m_[3] | b.m_[3]);
96   }
97 
98   friend constexpr Charmap operator~(const Charmap& a) {
99     return Charmap(~a.m_[0], ~a.m_[1], ~a.m_[2], ~a.m_[3]);
100   }
101 
102  private:
Charmap(uint64_t b0,uint64_t b1,uint64_t b2,uint64_t b3)103   constexpr Charmap(uint64_t b0, uint64_t b1, uint64_t b2, uint64_t b3)
104       : m_{b0, b1, b2, b3} {}
105 
RangeForWord(char lo,char hi,uint64_t word)106   static constexpr uint64_t RangeForWord(char lo, char hi, uint64_t word) {
107     return OpenRangeFromZeroForWord(static_cast<unsigned char>(hi) + 1, word) &
108            ~OpenRangeFromZeroForWord(static_cast<unsigned char>(lo), word);
109   }
110 
111   // All the chars in the specified word of the range [0, upper).
OpenRangeFromZeroForWord(uint64_t upper,uint64_t word)112   static constexpr uint64_t OpenRangeFromZeroForWord(uint64_t upper,
113                                                      uint64_t word) {
114     return (upper <= 64 * word)
115                ? 0
116                : (upper >= 64 * (word + 1))
117                      ? ~static_cast<uint64_t>(0)
118                      : (~static_cast<uint64_t>(0) >> (64 - upper % 64));
119   }
120 
CharMaskForWord(char x,uint64_t word)121   static constexpr uint64_t CharMaskForWord(char x, uint64_t word) {
122     const auto unsigned_x = static_cast<unsigned char>(x);
123     return (unsigned_x / 64 == word)
124                ? (static_cast<uint64_t>(1) << (unsigned_x % 64))
125                : 0;
126   }
127 
SetChar(char c)128   void SetChar(char c) {
129     const auto unsigned_c = static_cast<unsigned char>(c);
130     m_[unsigned_c / 64] |= static_cast<uint64_t>(1) << (unsigned_c % 64);
131   }
132 
133   uint64_t m_[4];
134 };
135 
136 // Mirror the char-classifying predicates in <cctype>
UpperCharmap()137 constexpr Charmap UpperCharmap() { return Charmap::Range('A', 'Z'); }
LowerCharmap()138 constexpr Charmap LowerCharmap() { return Charmap::Range('a', 'z'); }
DigitCharmap()139 constexpr Charmap DigitCharmap() { return Charmap::Range('0', '9'); }
AlphaCharmap()140 constexpr Charmap AlphaCharmap() { return LowerCharmap() | UpperCharmap(); }
AlnumCharmap()141 constexpr Charmap AlnumCharmap() { return DigitCharmap() | AlphaCharmap(); }
XDigitCharmap()142 constexpr Charmap XDigitCharmap() {
143   return DigitCharmap() | Charmap::Range('A', 'F') | Charmap::Range('a', 'f');
144 }
PrintCharmap()145 constexpr Charmap PrintCharmap() { return Charmap::Range(0x20, 0x7e); }
SpaceCharmap()146 constexpr Charmap SpaceCharmap() { return Charmap::FromString("\t\n\v\f\r "); }
CntrlCharmap()147 constexpr Charmap CntrlCharmap() {
148   return Charmap::Range(0, 0x7f) & ~PrintCharmap();
149 }
BlankCharmap()150 constexpr Charmap BlankCharmap() { return Charmap::FromString("\t "); }
GraphCharmap()151 constexpr Charmap GraphCharmap() { return PrintCharmap() & ~SpaceCharmap(); }
PunctCharmap()152 constexpr Charmap PunctCharmap() { return GraphCharmap() & ~AlnumCharmap(); }
153 
154 }  // namespace strings_internal
155 ABSL_NAMESPACE_END
156 }  // namespace absl
157 
158 #endif  // ABSL_STRINGS_INTERNAL_CHAR_MAP_H_
159