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_NORMALIZED_HPP
 
12 -
#define BOOST_URL_DETAIL_NORMALIZED_HPP
 
13 -

 
14 -
#include <boost/core/detail/string_view.hpp>
 
15 -
#include "boost/url/segments_encoded_view.hpp"
 
16 -

 
17 -
namespace boost {
 
18 -
namespace urls {
 
19 -
namespace detail {
 
20 -

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

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

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

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

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

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

 
66 -
private:
 
67 -
    std::size_t h_;
 
68 -
};
 
69 -

 
70 -
void
 
71 -
pop_encoded_front(
 
72 -
    core::string_view& s,
 
73 -
    char& c,
 
74 -
    std::size_t& n) noexcept;
 
75 -

 
76 -
// compare two core::string_views as if they are both
 
77 -
// percent-decoded
 
78 -
int
 
79 -
compare_encoded(
 
80 -
    core::string_view lhs,
 
81 -
    core::string_view rhs) noexcept;
 
82 -

 
83 -
// compare two core::string_views as if they are both
 
84 -
// percent-decoded but do not consider the special
 
85 -
// query chars ("&=+") equivalent unless they are
 
86 -
// both decoded or encoded the same way, because
 
87 -
// that gives them different meanings
 
88 -
int
 
89 -
compare_encoded_query(
 
90 -
    core::string_view lhs,
 
91 -
    core::string_view rhs) noexcept;
 
92 -

 
93 -
// digest a core::string_view as if it were
 
94 -
// percent-decoded
 
95 -
void
 
96 -
digest_encoded(
 
97 -
    core::string_view s,
 
98 -
    fnv_1a& hasher) noexcept;
 
99 -

 
100 -
void
 
101 -
digest(
 
102 -
    core::string_view s,
 
103 -
    fnv_1a& hasher) noexcept;
 
104 -

 
105 -
// check if core::string_view lhs starts with core::string_view
 
106 -
// rhs as if they are both percent-decoded. If
 
107 -
// lhs starts with rhs, return number of chars
 
108 -
// matched in the encoded core::string_view
 
109 -
std::size_t
 
110 -
path_starts_with(
 
111 -
    core::string_view lhs,
 
112 -
    core::string_view rhs) noexcept;
 
113 -

 
114 -
// check if core::string_view lhs ends with core::string_view
 
115 -
// rhs as if they are both percent-decoded. If
 
116 -
// lhs ends with rhs, return number of chars
 
117 -
// matched in the encoded core::string_view
 
118 -
std::size_t
 
119 -
path_ends_with(
 
120 -
    core::string_view lhs,
 
121 -
    core::string_view rhs) noexcept;
 
122 -

 
123 -
// compare two core::string_views as if they are both
 
124 -
// percent-decoded and lowercase
 
125 -
int
 
126 -
ci_compare_encoded(
 
127 -
    core::string_view lhs,
 
128 -
    core::string_view rhs) noexcept;
 
129 -

 
130 -
// digest a core::string_view as if it were decoded
 
131 -
// and lowercase
 
132 -
void
 
133 -
ci_digest_encoded(
 
134 -
    core::string_view s,
 
135 -
    fnv_1a& hasher) noexcept;
 
136 -

 
137 -
// compare two ascii core::string_views
 
138 -
int
 
139 -
compare(
 
140 -
    core::string_view lhs,
 
141 -
    core::string_view rhs) noexcept;
 
142 -

 
143 -
// compare two core::string_views as if they are both
 
144 -
// lowercase
 
145 -
int
 
146 -
ci_compare(
 
147 -
    core::string_view lhs,
 
148 -
    core::string_view rhs) noexcept;
 
149 -

 
150 -
// digest a core::string_view as if it were lowercase
 
151 -
void
 
152 -
ci_digest(
 
153 -
    core::string_view s,
 
154 -
    fnv_1a& hasher) noexcept;
 
155 -

 
156 -
BOOST_URL_DECL
 
157 -
std::size_t
 
158 -
remove_dot_segments(
 
159 -
    char* dest,
 
160 -
    char const* end,
 
161 -
    core::string_view input) noexcept;
 
162 -

 
163 -
void
 
164 -
pop_last_segment(
 
165 -
    core::string_view& str,
 
166 -
    core::string_view& seg,
 
167 -
    std::size_t& level,
 
168 -
    bool remove_unmatched) noexcept;
 
169 -

 
170 -
char
 
171 -
path_pop_back( core::string_view& s );
 
172 -

 
173 -
void
 
174 -
normalized_path_digest(
 
175 -
    core::string_view str,
 
176 -
    bool remove_unmatched,
 
177 -
    fnv_1a& hasher) noexcept;
 
178 -

 
179 -
int
 
180 -
segments_compare(
 
181 -
    segments_encoded_view seg0,
 
182 -
    segments_encoded_view seg1) noexcept;
 
183 -

 
184 -
} // detail
 
185 -
} // urls
 
186 -
} // boost
 
187 -

 
188 -
#endif