• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
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  * http://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 
16 #ifndef PANDA_VERIF_PARSER_CHARSET_H_
17 #define PANDA_VERIF_PARSER_CHARSET_H_
18 
19 #include "macros.h"
20 
21 #include <cstdint>
22 
23 namespace panda::parser {
24 
25 template <typename Char>
26 class charset {
27 public:
operator()28     constexpr bool operator()(Char c_) const
29     {
30         uint8_t c = static_cast<uint8_t>(c_);
31         return (bitmap[(c) >> 0x6U] & (0x1ULL << ((c)&0x3FU))) != 0;
32     }
33 
charset()34     charset() {}
35     charset(const charset &c) = default;
36     charset(charset &&c) = default;
37     charset &operator=(const charset &c) = default;
38     charset &operator=(charset &&c) = default;
39     ~charset() = default;
40 
charset(Char * s)41     constexpr charset(Char *s)
42     {
43         ASSERT(s != nullptr);
44         while (*s) {
45             uint8_t c = static_cast<uint8_t>(*s);
46             // NB!: 1ULL (64bit) required, bug in clang optimizations on -O2 and higher
47             bitmap[(c) >> 0x6U] = static_cast<uint64_t>(bitmap[(c) >> 0x6U] | (0x1ULL << ((c)&0x3FU)));
48             ++s;
49         }
50     }
51 
52     constexpr charset operator+(const charset &c) const
53     {
54         charset cs;
55         cs.bitmap[0x0] = bitmap[0x0] | c.bitmap[0x0];
56         cs.bitmap[0x1] = bitmap[0x1] | c.bitmap[0x1];
57         cs.bitmap[0x2] = bitmap[0x2] | c.bitmap[0x2];
58         cs.bitmap[0x3] = bitmap[0x3] | c.bitmap[0x3];
59         return cs;
60     }
61 
62     constexpr charset operator-(const charset &c) const
63     {
64         charset cs;
65         cs.bitmap[0x0] = bitmap[0x0] & ~c.bitmap[0x0];
66         cs.bitmap[0x1] = bitmap[0x1] & ~c.bitmap[0x1];
67         cs.bitmap[0x2] = bitmap[0x2] & ~c.bitmap[0x2];
68         cs.bitmap[0x3] = bitmap[0x3] & ~c.bitmap[0x3];
69         return cs;
70     }
71 
72     constexpr charset operator!() const
73     {
74         charset cs;
75         cs.bitmap[0x0] = ~bitmap[0x0];
76         cs.bitmap[0x1] = ~bitmap[0x1];
77         cs.bitmap[0x2] = ~bitmap[0x2];
78         cs.bitmap[0x3] = ~bitmap[0x3];
79         return cs;
80     }
81 
82 private:
83     uint64_t bitmap[4] = {
84         0x0ULL,
85     };
86 };
87 
88 }  // namespace panda::parser
89 
90 #endif  //! PANDA_VERIF_PARSER_CHARSET_H_
91