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 +
    fnv_1a(std::size_t salt) noexcept
 
41 +
    : h_(init_hash + salt)
 
42 +
    {
 
43 +
    }
 
44 +

 
45 +
    void
 
46 +
    put(char c) noexcept
 
47 +
    {
 
48 +
        h_ ^= c;
 
49 +
        h_ *= prime;
 
50 +
    }
 
51 +

 
52 +
    void
 
53 +
    put(core::string_view s) noexcept
 
54 +
    {
 
55 +
        for (char c: s)
 
56 +
        {
 
57 +
            put(c);
 
58 +
        }
 
59 +
    }
 
60 +

 
61 +
    digest_type
 
62 +
    digest() const noexcept
 
63 +
    {
 
64 +
        return h_;
 
65 +
    }
 
66 +

 
67 +
private:
 
68 +
    std::size_t h_;
 
69 +
};
 
70 +

 
71 +
} // detail
 
72 +
} // urls
 
73 +
} // boost
 
74 +

 
75 +
#endif