char_map.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 Y_ABSL_STRINGS_INTERNAL_CHAR_MAP_H_
  20. #define Y_ABSL_STRINGS_INTERNAL_CHAR_MAP_H_
  21. #include <cstddef>
  22. #include <cstdint>
  23. #include <cstring>
  24. #include "y_absl/base/macros.h"
  25. #include "y_absl/base/port.h"
  26. namespace y_absl {
  27. Y_ABSL_NAMESPACE_BEGIN
  28. namespace strings_internal {
  29. class Charmap {
  30. public:
  31. constexpr Charmap() : m_() {}
  32. // Initializes with a given char*. Note that NUL is not treated as
  33. // a terminator, but rather a char to be flicked.
  34. Charmap(const char* str, int len) : m_() {
  35. while (len--) SetChar(*str++);
  36. }
  37. // Initializes with a given char*. NUL is treated as a terminator
  38. // and will not be in the charmap.
  39. explicit Charmap(const char* str) : m_() {
  40. while (*str) SetChar(*str++);
  41. }
  42. constexpr bool contains(unsigned char c) const {
  43. return (m_[c / 64] >> (c % 64)) & 0x1;
  44. }
  45. // Returns true if and only if a character exists in both maps.
  46. bool IntersectsWith(const Charmap& c) const {
  47. for (size_t i = 0; i < Y_ABSL_ARRAYSIZE(m_); ++i) {
  48. if ((m_[i] & c.m_[i]) != 0) return true;
  49. }
  50. return false;
  51. }
  52. bool IsZero() const {
  53. for (uint64_t c : m_) {
  54. if (c != 0) return false;
  55. }
  56. return true;
  57. }
  58. // Containing only a single specified char.
  59. static constexpr Charmap Char(char x) {
  60. return Charmap(CharMaskForWord(x, 0), CharMaskForWord(x, 1),
  61. CharMaskForWord(x, 2), CharMaskForWord(x, 3));
  62. }
  63. // Containing all the chars in the C-string 's'.
  64. static constexpr Charmap FromString(const char* s) {
  65. Charmap ret;
  66. while (*s) ret = ret | Char(*s++);
  67. return ret;
  68. }
  69. // Containing all the chars in the closed interval [lo,hi].
  70. static constexpr Charmap Range(char lo, char hi) {
  71. return Charmap(RangeForWord(lo, hi, 0), RangeForWord(lo, hi, 1),
  72. RangeForWord(lo, hi, 2), RangeForWord(lo, hi, 3));
  73. }
  74. friend constexpr Charmap operator&(const Charmap& a, const Charmap& b) {
  75. return Charmap(a.m_[0] & b.m_[0], a.m_[1] & b.m_[1], a.m_[2] & b.m_[2],
  76. a.m_[3] & b.m_[3]);
  77. }
  78. friend constexpr Charmap operator|(const Charmap& a, const Charmap& b) {
  79. return Charmap(a.m_[0] | b.m_[0], a.m_[1] | b.m_[1], a.m_[2] | b.m_[2],
  80. a.m_[3] | b.m_[3]);
  81. }
  82. friend constexpr Charmap operator~(const Charmap& a) {
  83. return Charmap(~a.m_[0], ~a.m_[1], ~a.m_[2], ~a.m_[3]);
  84. }
  85. private:
  86. constexpr Charmap(uint64_t b0, uint64_t b1, uint64_t b2, uint64_t b3)
  87. : m_{b0, b1, b2, b3} {}
  88. static constexpr uint64_t RangeForWord(char lo, char hi, uint64_t word) {
  89. return OpenRangeFromZeroForWord(static_cast<unsigned char>(hi) + 1, word) &
  90. ~OpenRangeFromZeroForWord(static_cast<unsigned char>(lo), word);
  91. }
  92. // All the chars in the specified word of the range [0, upper).
  93. static constexpr uint64_t OpenRangeFromZeroForWord(uint64_t upper,
  94. uint64_t word) {
  95. return (upper <= 64 * word)
  96. ? 0
  97. : (upper >= 64 * (word + 1))
  98. ? ~static_cast<uint64_t>(0)
  99. : (~static_cast<uint64_t>(0) >> (64 - upper % 64));
  100. }
  101. static constexpr uint64_t CharMaskForWord(char x, uint64_t word) {
  102. const auto unsigned_x = static_cast<unsigned char>(x);
  103. return (unsigned_x / 64 == word)
  104. ? (static_cast<uint64_t>(1) << (unsigned_x % 64))
  105. : 0;
  106. }
  107. void SetChar(char c) {
  108. const auto unsigned_c = static_cast<unsigned char>(c);
  109. m_[unsigned_c / 64] |= static_cast<uint64_t>(1) << (unsigned_c % 64);
  110. }
  111. uint64_t m_[4];
  112. };
  113. // Mirror the char-classifying predicates in <cctype>
  114. constexpr Charmap UpperCharmap() { return Charmap::Range('A', 'Z'); }
  115. constexpr Charmap LowerCharmap() { return Charmap::Range('a', 'z'); }
  116. constexpr Charmap DigitCharmap() { return Charmap::Range('0', '9'); }
  117. constexpr Charmap AlphaCharmap() { return LowerCharmap() | UpperCharmap(); }
  118. constexpr Charmap AlnumCharmap() { return DigitCharmap() | AlphaCharmap(); }
  119. constexpr Charmap XDigitCharmap() {
  120. return DigitCharmap() | Charmap::Range('A', 'F') | Charmap::Range('a', 'f');
  121. }
  122. constexpr Charmap PrintCharmap() { return Charmap::Range(0x20, 0x7e); }
  123. constexpr Charmap SpaceCharmap() { return Charmap::FromString("\t\n\v\f\r "); }
  124. constexpr Charmap CntrlCharmap() {
  125. return Charmap::Range(0, 0x7f) & ~PrintCharmap();
  126. }
  127. constexpr Charmap BlankCharmap() { return Charmap::FromString("\t "); }
  128. constexpr Charmap GraphCharmap() { return PrintCharmap() & ~SpaceCharmap(); }
  129. constexpr Charmap PunctCharmap() { return GraphCharmap() & ~AlnumCharmap(); }
  130. } // namespace strings_internal
  131. Y_ABSL_NAMESPACE_END
  132. } // namespace y_absl
  133. #endif // Y_ABSL_STRINGS_INTERNAL_CHAR_MAP_H_