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