/root/doris/be/src/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 | | #pragma once |
17 | | |
18 | | #include <string.h> |
19 | | |
20 | | class Charmap { |
21 | | public: |
22 | | // Initializes with given uint32 values. For instance, the first |
23 | | // variable contains bits for values 0x1F (US) down to 0x00 (NUL). |
24 | | Charmap(uint32 b0, uint32 b1, uint32 b2, uint32 b3, uint32 b4, uint32 b5, uint32 b6, |
25 | 0 | uint32 b7) { |
26 | 0 | m_[0] = b0; |
27 | 0 | m_[1] = b1; |
28 | 0 | m_[2] = b2; |
29 | 0 | m_[3] = b3; |
30 | 0 | m_[4] = b4; |
31 | 0 | m_[5] = b5; |
32 | 0 | m_[6] = b6; |
33 | 0 | m_[7] = b7; |
34 | 0 | } |
35 | | |
36 | | // Initializes with a given char*. Note that NUL is not treated as |
37 | | // a terminator, but rather a char to be flicked. |
38 | 0 | Charmap(const char* str, int len) { Init(str, len); } |
39 | | |
40 | | // Initializes with a given char*. NUL is treated as a terminator |
41 | | // and will not be in the charmap. |
42 | 1 | explicit Charmap(const char* str) { Init(str, strlen(str)); } |
43 | | |
44 | 0 | bool contains(unsigned char c) const { return (m_[c >> 5] >> (c & 0x1f)) & 0x1; } |
45 | | |
46 | | // Returns true if and only if a character exists in both maps. |
47 | 0 | bool IntersectsWith(const Charmap& c) const { |
48 | 0 | for (int i = 0; i < 8; ++i) { |
49 | 0 | if ((m_[i] & c.m_[i]) != 0) return true; |
50 | 0 | } |
51 | 0 | return false; |
52 | 0 | } |
53 | | |
54 | 0 | bool IsZero() const { |
55 | 0 | for (uint32 c : m_) { |
56 | 0 | if (c != 0) return false; |
57 | 0 | } |
58 | 0 | return true; |
59 | 0 | } |
60 | | |
61 | | protected: |
62 | | uint32 m_[8]; |
63 | | |
64 | 1 | void Init(const char* str, int len) { |
65 | 1 | memset(&m_, 0, sizeof m_); |
66 | 66 | for (int i = 0; i < len; ++i) { |
67 | 65 | unsigned char value = static_cast<unsigned char>(str[i]); |
68 | 65 | m_[value >> 5] |= 1UL << (value & 0x1f); |
69 | 65 | } |
70 | 1 | } |
71 | | }; |