Skip to content

Commit 2e79f18

Browse files
Add an overload to xxhash to allow it to take a string constant (#189)
People were using the xxhash function with string constants like `xxhash32("Hello");` and it was compiling just fine, and then returning the hash of the first letter. This adds an overload which makes xxhash32("Hello"); use a template with an `N` array in order to hash that correctly.
1 parent 9758634 commit 2e79f18

2 files changed

Lines changed: 202 additions & 150 deletions

File tree

src/util/serialise/xxhash.hpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,13 @@ namespace util {
4444
uint32_t xxhash32(const char* input, const size_t& length, const uint32_t& seed = 0);
4545

4646
template <typename T>
47-
uint32_t xxhash32(const T* input, const uint32_t& seed = 0) {
48-
return xxhash32(reinterpret_cast<const char*>(input), sizeof(T), seed);
47+
uint32_t xxhash32(const T& input, const uint32_t& seed = 0) {
48+
return xxhash32(reinterpret_cast<const char*>(&input), sizeof(T), seed);
49+
}
50+
template <uint32_t S = 0, size_t N>
51+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
52+
uint32_t xxhash32(const char (&input)[N]) {
53+
return xxhash32(input, N - 1, S);
4954
}
5055

5156
/**
@@ -62,8 +67,13 @@ namespace util {
6267
uint64_t xxhash64(const char* input, const size_t& length, const uint64_t& seed = 0);
6368

6469
template <typename T>
65-
uint64_t xxhash64(const T* input, const uint64_t& seed = 0) {
66-
return xxhash64(reinterpret_cast<const char*>(input), sizeof(T), seed);
70+
uint64_t xxhash64(const T& input, const uint64_t& seed = 0) {
71+
return xxhash64(reinterpret_cast<const char*>(&input), sizeof(T), seed);
72+
}
73+
template <uint64_t S = 0, size_t N>
74+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
75+
uint64_t xxhash64(const char (&input)[N]) {
76+
return xxhash64(input, N - 1, S);
6777
}
6878

6979
} // namespace serialise

0 commit comments

Comments
 (0)