Coverage Report

Created: 2025-07-23 06:45

/proc/self/cwd/cpp/htmlparser/hash.h
Line
Count
Source (jump to first uncovered line)
1
#ifndef CPP_HTMLPARSER_HASH_H_
2
#define CPP_HTMLPARSER_HASH_H_
3
4
#include <string_view>
5
6
namespace htmlparser {
7
8
// Source: https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function
9
class Hash {
10
 public:
11
13.3M
  static uint32_t FNVHash(std::string_view s, uint32_t h = 2166136261) {
12
22.2M
    for (unsigned char c : s) {
13
22.2M
      h ^= c;
14
22.2M
      h *= 16777619;  // FNV prime.
15
22.2M
    }
16
13.3M
    return h;
17
13.3M
  }
18
19
  static uint64_t FNVHash64(std::string_view s,
20
0
                            uint64_t h = 14695981039346656037u) {
21
0
    for (unsigned char c : s) {
22
0
      h ^= c;
23
0
      h *= 1099511628211;  // FNV prime.
24
0
    }
25
0
    return h;
26
0
  }
27
28
  // FNV-1a. Alternate. Same as above except the order of XOR and multiply
29
  // is reversed.
30
  static uint64_t FNV_AHash64(std::string_view s,
31
0
                              uint64_t h = 14695981039346656037u) {
32
0
    for (unsigned char c : s) {
33
0
      h *= 1099511628211;  // FNV prime.
34
0
      h ^= c;
35
0
    }
36
0
    return h;
37
0
  }
38
39
 private:
40
  // No objects of this class.
41
  Hash() = delete;
42
};
43
44
}  // namespace htmlparser
45
46
#endif  // CPP_HTMLPARSER_HASH_H_