You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

char_map.h 6.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #ifndef ABSL_STRINGS_INTERNAL_CHAR_MAP_H_
  20. #define ABSL_STRINGS_INTERNAL_CHAR_MAP_H_
  21. #include <cstddef>
  22. #include <cstdint>
  23. #include <cstring>
  24. #include "absl/base/macros.h"
  25. #include "absl/base/port.h"
  26. namespace absl
  27. {
  28. ABSL_NAMESPACE_BEGIN
  29. namespace strings_internal
  30. {
  31. class Charmap
  32. {
  33. public:
  34. constexpr Charmap() :
  35. m_()
  36. {
  37. }
  38. // Initializes with a given char*. Note that NUL is not treated as
  39. // a terminator, but rather a char to be flicked.
  40. Charmap(const char* str, int len) :
  41. m_()
  42. {
  43. while (len--)
  44. SetChar(*str++);
  45. }
  46. // Initializes with a given char*. NUL is treated as a terminator
  47. // and will not be in the charmap.
  48. explicit Charmap(const char* str) :
  49. m_()
  50. {
  51. while (*str)
  52. SetChar(*str++);
  53. }
  54. constexpr bool contains(unsigned char c) const
  55. {
  56. return (m_[c / 64] >> (c % 64)) & 0x1;
  57. }
  58. // Returns true if and only if a character exists in both maps.
  59. bool IntersectsWith(const Charmap& c) const
  60. {
  61. for (size_t i = 0; i < ABSL_ARRAYSIZE(m_); ++i)
  62. {
  63. if ((m_[i] & c.m_[i]) != 0)
  64. return true;
  65. }
  66. return false;
  67. }
  68. bool IsZero() const
  69. {
  70. for (uint64_t c : m_)
  71. {
  72. if (c != 0)
  73. return false;
  74. }
  75. return true;
  76. }
  77. // Containing only a single specified char.
  78. static constexpr Charmap Char(char x)
  79. {
  80. return Charmap(CharMaskForWord(x, 0), CharMaskForWord(x, 1), CharMaskForWord(x, 2), CharMaskForWord(x, 3));
  81. }
  82. // Containing all the chars in the C-string 's'.
  83. // Note that this is expensively recursive because of the C++11 constexpr
  84. // formulation. Use only in constexpr initializers.
  85. static constexpr Charmap FromString(const char* s)
  86. {
  87. return *s == 0 ? Charmap() : (Char(*s) | FromString(s + 1));
  88. }
  89. // Containing all the chars in the closed interval [lo,hi].
  90. static constexpr Charmap Range(char lo, char hi)
  91. {
  92. return Charmap(RangeForWord(lo, hi, 0), RangeForWord(lo, hi, 1), RangeForWord(lo, hi, 2), RangeForWord(lo, hi, 3));
  93. }
  94. friend constexpr Charmap operator&(const Charmap& a, const Charmap& b)
  95. {
  96. return Charmap(a.m_[0] & b.m_[0], a.m_[1] & b.m_[1], a.m_[2] & b.m_[2], a.m_[3] & b.m_[3]);
  97. }
  98. friend constexpr Charmap operator|(const Charmap& a, const Charmap& b)
  99. {
  100. return Charmap(a.m_[0] | b.m_[0], a.m_[1] | b.m_[1], a.m_[2] | b.m_[2], a.m_[3] | b.m_[3]);
  101. }
  102. friend constexpr Charmap operator~(const Charmap& a)
  103. {
  104. return Charmap(~a.m_[0], ~a.m_[1], ~a.m_[2], ~a.m_[3]);
  105. }
  106. private:
  107. constexpr Charmap(uint64_t b0, uint64_t b1, uint64_t b2, uint64_t b3) :
  108. m_{b0, b1, b2, b3}
  109. {
  110. }
  111. static constexpr uint64_t RangeForWord(unsigned char lo, unsigned char hi, uint64_t word)
  112. {
  113. return OpenRangeFromZeroForWord(hi + 1, word) &
  114. ~OpenRangeFromZeroForWord(lo, word);
  115. }
  116. // All the chars in the specified word of the range [0, upper).
  117. static constexpr uint64_t OpenRangeFromZeroForWord(uint64_t upper, uint64_t word)
  118. {
  119. return (upper <= 64 * word) ? 0 : (upper >= 64 * (word + 1)) ? ~static_cast<uint64_t>(0) :
  120. (~static_cast<uint64_t>(0) >> (64 - upper % 64));
  121. }
  122. static constexpr uint64_t CharMaskForWord(unsigned char x, uint64_t word)
  123. {
  124. return (x / 64 == word) ? (static_cast<uint64_t>(1) << (x % 64)) : 0;
  125. }
  126. private:
  127. void SetChar(unsigned char c)
  128. {
  129. m_[c / 64] |= static_cast<uint64_t>(1) << (c % 64);
  130. }
  131. uint64_t m_[4];
  132. };
  133. // Mirror the char-classifying predicates in <cctype>
  134. constexpr Charmap UpperCharmap()
  135. {
  136. return Charmap::Range('A', 'Z');
  137. }
  138. constexpr Charmap LowerCharmap()
  139. {
  140. return Charmap::Range('a', 'z');
  141. }
  142. constexpr Charmap DigitCharmap()
  143. {
  144. return Charmap::Range('0', '9');
  145. }
  146. constexpr Charmap AlphaCharmap()
  147. {
  148. return LowerCharmap() | UpperCharmap();
  149. }
  150. constexpr Charmap AlnumCharmap()
  151. {
  152. return DigitCharmap() | AlphaCharmap();
  153. }
  154. constexpr Charmap XDigitCharmap()
  155. {
  156. return DigitCharmap() | Charmap::Range('A', 'F') | Charmap::Range('a', 'f');
  157. }
  158. constexpr Charmap PrintCharmap()
  159. {
  160. return Charmap::Range(0x20, 0x7e);
  161. }
  162. constexpr Charmap SpaceCharmap()
  163. {
  164. return Charmap::FromString("\t\n\v\f\r ");
  165. }
  166. constexpr Charmap CntrlCharmap()
  167. {
  168. return Charmap::Range(0, 0x7f) & ~PrintCharmap();
  169. }
  170. constexpr Charmap BlankCharmap()
  171. {
  172. return Charmap::FromString("\t ");
  173. }
  174. constexpr Charmap GraphCharmap()
  175. {
  176. return PrintCharmap() & ~SpaceCharmap();
  177. }
  178. constexpr Charmap PunctCharmap()
  179. {
  180. return GraphCharmap() & ~AlnumCharmap();
  181. }
  182. } // namespace strings_internal
  183. ABSL_NAMESPACE_END
  184. } // namespace absl
  185. #endif // ABSL_STRINGS_INTERNAL_CHAR_MAP_H_