include/boost/url/detail/fnv_1a.hpp

100.0% Lines (13/13) 100.0% Functions (4/4) 100.0% Branches (2/2)
include/boost/url/detail/fnv_1a.hpp
Line Branch Hits Source Code
1 //
2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
3 // Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/boostorg/url
9 //
10
11 #ifndef BOOST_URL_DETAIL_FNV_1A_HPP
12 #define BOOST_URL_DETAIL_FNV_1A_HPP
13
14 #include <boost/url/detail/config.hpp>
15 #include <boost/core/detail/string_view.hpp>
16 #include <cstddef>
17
18 namespace boost {
19 namespace urls {
20 namespace detail {
21
22 class fnv_1a
23 {
24 public:
25 using digest_type = std::size_t;
26
27 #if BOOST_URL_ARCH == 64
28 static constexpr std::size_t const prime =
29 static_cast<std::size_t>(0x100000001B3ULL);
30 static constexpr std::size_t init_hash =
31 static_cast<std::size_t>(0xcbf29ce484222325ULL);
32 #else
33 static constexpr std::size_t const prime =
34 static_cast<std::size_t>(0x01000193UL);
35 static constexpr std::size_t init_hash =
36 static_cast<std::size_t>(0x811C9DC5UL);
37 #endif
38
39 explicit
40 304 fnv_1a(std::size_t salt) noexcept
41 304 : h_(init_hash + salt)
42 {
43 304 }
44
45 void
46 4550 put(char c) noexcept
47 {
48 4550 h_ ^= c;
49 4550 h_ *= prime;
50 4550 }
51
52 void
53 304 put(core::string_view s) noexcept
54 {
55
2/2
✓ Branch 2 taken 96 times.
✓ Branch 3 taken 304 times.
400 for (char c: s)
56 {
57 96 put(c);
58 }
59 304 }
60
61 digest_type
62 304 digest() const noexcept
63 {
64 304 return h_;
65 }
66
67 private:
68 std::size_t h_;
69 };
70
71 } // detail
72 } // urls
73 } // boost
74
75 #endif
76