BashSpark
Loading...
Searching...
No Matches
hash.h
Go to the documentation of this file.
1
43#pragma once
44
45#include <cstdint>
46#include <string>
47#include <string_view>
48
49namespace bs {
51 constexpr std::uint64_t FNV_OFFSET_BASIS = 2166136261;
53 constexpr std::uint64_t FNVPrime = 16777619;
54
65 constexpr std::uint64_t hash(const char *const pString, const std::size_t nLength) {
66 std::uint64_t nHash = FNV_OFFSET_BASIS;
67
68 for (std::size_t i = 0; i < nLength; i++) {
69 nHash ^= pString[i]; // XOR byte into the hash
70 nHash *= FNVPrime; // Multiply by FNV prime
71 }
72
73 return nHash;
74 }
75
85 constexpr std::uint64_t hash(const std::string_view sString) {
86 return hash(sString.data(), sString.length());
87 }
88
98 inline std::uint64_t hash(const std::string &sString) {
99 return hash(sString.data(), sString.length());
100 }
101
111 constexpr std::uint64_t hash(const char *const pString) {
112 return hash(std::string_view(pString));
113 }
114}
BashSpark main namespace.
Definition command.h:39
constexpr std::uint64_t hash(const char *const pString, const std::size_t nLength)
Hashes a string.
Definition hash.h:65
constexpr std::uint64_t FNV_OFFSET_BASIS
FVN-1 offset basis.
Definition hash.h:51
constexpr std::uint64_t FNVPrime
FVN-1 prime number.
Definition hash.h:53