Line | Branch | Exec | Source |
---|---|---|---|
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 | #include <boost/url/detail/normalize.hpp> | ||
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 | 276 | fnv_1a(std::size_t salt) noexcept | |
41 | 276 | : h_(init_hash + salt) | |
42 | { | ||
43 | 276 | } | |
44 | |||
45 | void | ||
46 | 3840 | put(char c) noexcept | |
47 | { | ||
48 | 3840 | h_ ^= c; | |
49 | 3840 | h_ *= prime; | |
50 | 3840 | } | |
51 | |||
52 | void | ||
53 | 276 | put(core::string_view s) noexcept | |
54 | { | ||
55 |
2/2✓ Branch 2 taken 18 times.
✓ Branch 3 taken 276 times.
|
294 | for (char c: s) |
56 | { | ||
57 | 18 | put(c); | |
58 | } | ||
59 | 276 | } | |
60 | |||
61 | digest_type | ||
62 | 276 | digest() const noexcept | |
63 | { | ||
64 | 276 | return h_; | |
65 | } | ||
66 | |||
67 | private: | ||
68 | std::size_t h_; | ||
69 | }; | ||
70 | |||
71 | void | ||
72 | pop_encoded_front( | ||
73 | core::string_view& s, | ||
74 | char& c, | ||
75 | std::size_t& n) noexcept; | ||
76 | |||
77 | // compare two core::string_views as if they are both | ||
78 | // percent-decoded | ||
79 | int | ||
80 | compare_encoded( | ||
81 | core::string_view lhs, | ||
82 | core::string_view rhs) noexcept; | ||
83 | |||
84 | // digest a core::string_view as if it were | ||
85 | // percent-decoded | ||
86 | void | ||
87 | digest_encoded( | ||
88 | core::string_view s, | ||
89 | fnv_1a& hasher) noexcept; | ||
90 | |||
91 | void | ||
92 | digest( | ||
93 | core::string_view s, | ||
94 | fnv_1a& hasher) noexcept; | ||
95 | |||
96 | // check if core::string_view lhs starts with core::string_view | ||
97 | // rhs as if they are both percent-decoded. If | ||
98 | // lhs starts with rhs, return number of chars | ||
99 | // matched in the encoded core::string_view | ||
100 | std::size_t | ||
101 | path_starts_with( | ||
102 | core::string_view lhs, | ||
103 | core::string_view rhs) noexcept; | ||
104 | |||
105 | // check if core::string_view lhs ends with core::string_view | ||
106 | // rhs as if they are both percent-decoded. If | ||
107 | // lhs ends with rhs, return number of chars | ||
108 | // matched in the encoded core::string_view | ||
109 | std::size_t | ||
110 | path_ends_with( | ||
111 | core::string_view lhs, | ||
112 | core::string_view rhs) noexcept; | ||
113 | |||
114 | // compare two core::string_views as if they are both | ||
115 | // percent-decoded and lowercase | ||
116 | int | ||
117 | ci_compare_encoded( | ||
118 | core::string_view lhs, | ||
119 | core::string_view rhs) noexcept; | ||
120 | |||
121 | // digest a core::string_view as if it were decoded | ||
122 | // and lowercase | ||
123 | void | ||
124 | ci_digest_encoded( | ||
125 | core::string_view s, | ||
126 | fnv_1a& hasher) noexcept; | ||
127 | |||
128 | // compare two ascii core::string_views | ||
129 | int | ||
130 | compare( | ||
131 | core::string_view lhs, | ||
132 | core::string_view rhs) noexcept; | ||
133 | |||
134 | // compare two core::string_views as if they are both | ||
135 | // lowercase | ||
136 | int | ||
137 | ci_compare( | ||
138 | core::string_view lhs, | ||
139 | core::string_view rhs) noexcept; | ||
140 | |||
141 | // digest a core::string_view as if it were lowercase | ||
142 | void | ||
143 | ci_digest( | ||
144 | core::string_view s, | ||
145 | fnv_1a& hasher) noexcept; | ||
146 | |||
147 | BOOST_URL_DECL | ||
148 | std::size_t | ||
149 | remove_dot_segments( | ||
150 | char* dest, | ||
151 | char const* end, | ||
152 | core::string_view s) noexcept; | ||
153 | |||
154 | void | ||
155 | pop_last_segment( | ||
156 | core::string_view& s, | ||
157 | core::string_view& c, | ||
158 | std::size_t& level, | ||
159 | bool r) noexcept; | ||
160 | |||
161 | char | ||
162 | path_pop_back( core::string_view& s ); | ||
163 | |||
164 | void | ||
165 | normalized_path_digest( | ||
166 | core::string_view s, | ||
167 | bool remove_unmatched, | ||
168 | fnv_1a& hasher) noexcept; | ||
169 | |||
170 | int | ||
171 | segments_compare( | ||
172 | segments_encoded_view seg0, | ||
173 | segments_encoded_view seg1) noexcept; | ||
174 | |||
175 | } // detail | ||
176 | } // urls | ||
177 | } // boost | ||
178 | |||
179 | #endif | ||
180 |