/Users/deen/code/yugabyte-db/src/yb/gutil/charmap.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Character Map Class |
2 | | // |
3 | | // Originally written by Daniel Dulitz |
4 | | // Yanked out from url.cc on February 2003 by Wei-Hwa Huang |
5 | | // |
6 | | // Copyright (C) Google, 2001. |
7 | | // |
8 | | // A fast, bit-vector map for 8-bit unsigned characters. |
9 | | // |
10 | | // Internally stores 256 bits in an array of 8 uint32s. |
11 | | // See changelist history for micro-optimization attempts. |
12 | | // Does quick bit-flicking to lookup needed characters. |
13 | | // |
14 | | // This class is useful for non-character purposes as well. |
15 | | |
16 | | // |
17 | | // The following only applies to changes made to this file as part of YugaByte development. |
18 | | // |
19 | | // Portions Copyright (c) YugaByte, Inc. |
20 | | // |
21 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
22 | | // in compliance with the License. You may obtain a copy of the License at |
23 | | // |
24 | | // http://www.apache.org/licenses/LICENSE-2.0 |
25 | | // |
26 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
27 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
28 | | // or implied. See the License for the specific language governing permissions and limitations |
29 | | // under the License. |
30 | | // |
31 | | #ifndef YB_GUTIL_CHARMAP_H |
32 | | #define YB_GUTIL_CHARMAP_H |
33 | | |
34 | | #include <string.h> |
35 | | |
36 | | #include "yb/gutil/integral_types.h" |
37 | | #include "yb/gutil/macros.h" |
38 | | #include "yb/gutil/type_traits.h" |
39 | | |
40 | | class Charmap { |
41 | | public: |
42 | | // Initializes with given uint32 values. For instance, the first |
43 | | // variable contains bits for values 0x1F (US) down to 0x00 (NUL). |
44 | | Charmap(uint32 b0, uint32 b1, uint32 b2, uint32 b3, |
45 | 0 | uint32 b4, uint32 b5, uint32 b6, uint32 b7) { |
46 | 0 | m_[0] = b0; |
47 | 0 | m_[1] = b1; |
48 | 0 | m_[2] = b2; |
49 | 0 | m_[3] = b3; |
50 | 0 | m_[4] = b4; |
51 | 0 | m_[5] = b5; |
52 | 0 | m_[6] = b6; |
53 | 0 | m_[7] = b7; |
54 | 0 | } |
55 | | |
56 | | // Initializes with a given char*. Note that NUL is not treated as |
57 | | // a terminator, but rather a char to be flicked. |
58 | 0 | Charmap(const char* str, int len) { |
59 | 0 | Init(str, len); |
60 | 0 | } |
61 | | |
62 | | // Initializes with a given char*. NUL is treated as a terminator |
63 | | // and will not be in the charmap. |
64 | 32.7k | explicit Charmap(const char* str) { |
65 | 32.7k | Init(str, strlen(str)); |
66 | 32.7k | } |
67 | | |
68 | 0 | bool contains(unsigned char c) const { |
69 | 0 | return (m_[c >> 5] >> (c & 0x1f)) & 0x1; |
70 | 0 | } |
71 | | |
72 | | // Returns true if and only if a character exists in both maps. |
73 | 0 | bool IntersectsWith(const Charmap& c) const { |
74 | 0 | for (int i = 0; i < 8; ++i) { |
75 | 0 | if ((m_[i] & c.m_[i]) != 0) |
76 | 0 | return true; |
77 | 0 | } |
78 | 0 | return false; |
79 | 0 | } |
80 | | |
81 | 0 | bool IsZero() const { |
82 | 0 | for (uint32 c : m_) { |
83 | 0 | if (c != 0) |
84 | 0 | return false; |
85 | 0 | } |
86 | 0 | return true; |
87 | 0 | } |
88 | | |
89 | | protected: |
90 | | uint32 m_[8]; |
91 | | |
92 | 32.7k | void Init(const char* str, size_t len) { |
93 | 32.7k | memset(&m_, 0, sizeof m_); |
94 | 2.16M | for (size_t i = 0; i < len; ++i2.12M ) { |
95 | 2.12M | unsigned char value = static_cast<unsigned char>(str[i]); |
96 | 2.12M | m_[value >> 5] |= 1UL << (value & 0x1f); |
97 | 2.12M | } |
98 | 32.7k | } |
99 | | }; |
100 | | DECLARE_POD(Charmap); |
101 | | |
102 | | #endif // YB_GUTIL_CHARMAP_H |