Sane C++ Libraries
C++ Platform Abstraction Libraries
StringHashFNV.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4
5namespace SC
6{
7namespace detail
8{
9template <unsigned int N, unsigned int I>
10struct StringHashFNVImpl
11{
12 static constexpr unsigned int Hash(const char (&str)[N])
13 {
14 return (StringHashFNVImpl<N, I - 1>::Hash(str) ^ static_cast<unsigned int>(str[I - 1])) * 16777619u;
15 }
16};
17
18template <unsigned int N>
19struct StringHashFNVImpl<N, 1>
20{
21 static constexpr unsigned int Hash(const char (&str)[N])
22 {
23 return (2166136261u ^ static_cast<unsigned int>(str[0])) * 16777619u;
24 }
25};
26} // namespace detail
27
30
32template <unsigned int N>
33constexpr unsigned int StringHashFNV(const char (&str)[N])
34{
35 return detail::StringHashFNVImpl<N, N>::Hash(str);
36}
38
39} // namespace SC
constexpr unsigned int StringHashFNV(const char(&str)[N])
Compute compile time FNV hash for a char array.
Definition: StringHashFNV.h:33