/proc/self/cwd/cpp/htmlparser/atomutil.cc
Line | Count | Source (jump to first uncovered line) |
1 | | #include "cpp/htmlparser/atomutil.h" |
2 | | |
3 | | #include "cpp/htmlparser/hash.h" |
4 | | |
5 | | namespace htmlparser { |
6 | | |
7 | 13.3M | Atom AtomUtil::ToAtom(const std::string& s) { |
8 | 13.3M | if (s.empty() || s.size() > kMaxAtomLength) { |
9 | 1.39k | return Atom::UNKNOWN; |
10 | 1.39k | } |
11 | | |
12 | 13.3M | uint32_t hash = Hash::FNVHash(s, kInitialHashValue); |
13 | 13.3M | uint32_t table_index = hash & (kNamesHashTable.size() - 1); |
14 | 13.3M | uint32_t atom_value = kNamesHashTable[table_index]; |
15 | 13.3M | int atom_len = atom_value & 0xff; |
16 | 13.3M | if (atom_len == s.size() && ToString(atom_value) == s) { |
17 | 6.44M | return CastToAtom(atom_value); |
18 | 6.44M | } |
19 | | |
20 | 6.88M | table_index = (hash >> 16) & (kNamesHashTable.size() - 1); |
21 | 6.88M | atom_value = kNamesHashTable[table_index]; |
22 | 6.88M | atom_len = atom_value & 0xff; |
23 | 6.88M | if (atom_len == s.size() && ToString(atom_value).compare(s) == 0) { |
24 | 685k | return CastToAtom(atom_value); |
25 | 685k | } |
26 | | |
27 | 6.20M | return Atom::UNKNOWN; |
28 | 6.88M | } |
29 | | |
30 | 9.31M | std::string AtomUtil::ToString(Atom a, std::string_view unknown_tag_name) { |
31 | 9.31M | if (a == Atom::UNKNOWN) return std::string(unknown_tag_name); |
32 | 9.31M | uint32_t start = static_cast<uint32_t>(a) >> 8; |
33 | 9.31M | uint32_t n = static_cast<uint32_t>(a) & 0xff; |
34 | 9.31M | if ((start + n) > kAtomText.size()) { |
35 | 0 | return std::string(unknown_tag_name); |
36 | 0 | } |
37 | 9.31M | return std::string(kAtomText.substr(start, n)); |
38 | 9.31M | } |
39 | | |
40 | | } // namespace htmlparser |