• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 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 ark::parser {
24 
25 template <typename Char>
26 class Charset {
27 public:
operator()28     constexpr bool operator()(Char chartype) const
29     {
30         auto c = static_cast<uint8_t>(chartype);
31         // NOLINTNEXTLINE(readability-magic-numbers)
32         return (bitmap_[(c) >> 0x6U] & (0x1ULL << ((c)&0x3FU))) != 0;
33     }
34 
35     Charset() = default;
36     Charset(const Charset &c) = default;
37     Charset(Charset &&c) = default;
38     Charset &operator=(const Charset &c) = default;
39     Charset &operator=(Charset &&c) = default;
40     ~Charset() = default;
41 
42     // NOLINTNEXTLINE(google-explicit-constructor)
Charset(Char * s)43     constexpr Charset(Char *s)
44     {
45         ASSERT(s != nullptr);
46         while (*s) {
47             auto c = static_cast<uint8_t>(*s);
48             // NB!: 1ULL (64bit) required, bug in clang optimizations on -O2 and higher
49             // NOLINTNEXTLINE(readability-magic-numbers)
50             bitmap_[(c) >> 0x6U] = static_cast<uint64_t>(bitmap_[(c) >> 0x6U] | (0x1ULL << ((c)&0x3FU)));
51             // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
52             ++s;
53         }
54     }
55 
56     constexpr Charset operator+(const Charset &c) const
57     {
58         Charset cs;
59         cs.bitmap_[0x0] = bitmap_[0x0] | c.bitmap_[0x0];
60         cs.bitmap_[0x1] = bitmap_[0x1] | c.bitmap_[0x1];
61         cs.bitmap_[0x2] = bitmap_[0x2] | c.bitmap_[0x2];
62         cs.bitmap_[0x3] = bitmap_[0x3] | c.bitmap_[0x3];
63         return cs;
64     }
65 
66     constexpr Charset operator-(const Charset &c) const
67     {
68         Charset cs;
69         cs.bitmap_[0x0] = bitmap_[0x0] & ~c.bitmap_[0x0];
70         cs.bitmap_[0x1] = bitmap_[0x1] & ~c.bitmap_[0x1];
71         cs.bitmap_[0x2] = bitmap_[0x2] & ~c.bitmap_[0x2];
72         cs.bitmap_[0x3] = bitmap_[0x3] & ~c.bitmap_[0x3];
73         return cs;
74     }
75 
76     constexpr Charset operator!() const
77     {
78         Charset cs;
79         cs.bitmap_[0x0] = ~bitmap_[0x0];
80         cs.bitmap_[0x1] = ~bitmap_[0x1];
81         cs.bitmap_[0x2] = ~bitmap_[0x2];
82         cs.bitmap_[0x3] = ~bitmap_[0x3];
83         return cs;
84     }
85 
86 private:
87     // NOLINTNEXTLINE(modernize-avoid-c-arrays)
88     uint64_t bitmap_[4] = {
89         0x0ULL,
90     };
91 };
92 
93 }  // namespace ark::parser
94 
95 #endif  // PANDA_VERIF_PARSER_CHARSET_H_
96