1  
//
1  
//
2  
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
2  
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
3  
// Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
3  
// Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
4  
//
4  
//
5  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
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)
6  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7  
//
7  
//
8  
// Official repository: https://github.com/boostorg/url
8  
// Official repository: https://github.com/boostorg/url
9  
//
9  
//
10  

10  

11  
#ifndef BOOST_URL_DETAIL_FNV_1A_HPP
11  
#ifndef BOOST_URL_DETAIL_FNV_1A_HPP
12  
#define BOOST_URL_DETAIL_FNV_1A_HPP
12  
#define BOOST_URL_DETAIL_FNV_1A_HPP
13  

13  

14  
#include <boost/url/detail/config.hpp>
14  
#include <boost/url/detail/config.hpp>
15  
#include <boost/core/detail/string_view.hpp>
15  
#include <boost/core/detail/string_view.hpp>
16  
#include <cstddef>
16  
#include <cstddef>
17  

17  

18  
namespace boost {
18  
namespace boost {
19  
namespace urls {
19  
namespace urls {
20  
namespace detail {
20  
namespace detail {
21  

21  

22  
class fnv_1a
22  
class fnv_1a
23  
{
23  
{
24  
public:
24  
public:
25  
    using digest_type = std::size_t;
25  
    using digest_type = std::size_t;
26  

26  

27  
#if BOOST_URL_ARCH == 64
27  
#if BOOST_URL_ARCH == 64
28  
    static constexpr std::size_t const prime =
28  
    static constexpr std::size_t const prime =
29  
        static_cast<std::size_t>(0x100000001B3ULL);
29  
        static_cast<std::size_t>(0x100000001B3ULL);
30  
    static constexpr std::size_t init_hash  =
30  
    static constexpr std::size_t init_hash  =
31  
        static_cast<std::size_t>(0xcbf29ce484222325ULL);
31  
        static_cast<std::size_t>(0xcbf29ce484222325ULL);
32  
#else
32  
#else
33  
    static constexpr std::size_t const prime =
33  
    static constexpr std::size_t const prime =
34  
        static_cast<std::size_t>(0x01000193UL);
34  
        static_cast<std::size_t>(0x01000193UL);
35  
    static constexpr std::size_t init_hash  =
35  
    static constexpr std::size_t init_hash  =
36  
        static_cast<std::size_t>(0x811C9DC5UL);
36  
        static_cast<std::size_t>(0x811C9DC5UL);
37  
#endif
37  
#endif
38  

38  

39  
    explicit
39  
    explicit
40  
    fnv_1a(std::size_t salt) noexcept
40  
    fnv_1a(std::size_t salt) noexcept
41  
    : h_(init_hash + salt)
41  
    : h_(init_hash + salt)
42  
    {
42  
    {
43  
    }
43  
    }
44  

44  

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

51  

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

60  

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

66  

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

70  

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

74  

75  
#endif
75  
#endif