Ada 2.8.0
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
helpers.h
Go to the documentation of this file.
1
5#ifndef ADA_HELPERS_H
6#define ADA_HELPERS_H
7
8#include "ada/common_defs.h"
9#include "ada/state.h"
10#include "ada/url_base.h"
11
12#include <string_view>
13#include <optional>
14
23namespace ada::helpers {
24
28template <typename out_iter>
29void encode_json(std::string_view view, out_iter out);
30
44ada_really_inline std::optional<std::string_view> prune_hash(
45 std::string_view& input) noexcept;
46
53ada_really_inline bool shorten_path(std::string& path,
54 ada::scheme::type type) noexcept;
55
62ada_really_inline bool shorten_path(std::string_view& path,
63 ada::scheme::type type) noexcept;
64
76ada_really_inline void parse_prepared_path(std::string_view input,
78 std::string& path);
79
84ada_really_inline void remove_ascii_tab_or_newline(std::string& input) noexcept;
85
91ada_really_inline std::string_view substring(std::string_view input,
92 size_t pos) noexcept;
93
98bool overlaps(std::string_view input1, const std::string& input2) noexcept;
99
105ada_really_inline std::string_view substring(const std::string& input,
106 size_t pos1,
107 size_t pos2) noexcept {
108#if ADA_DEVELOPMENT_CHECKS
109 if (pos2 < pos1) {
110 std::cerr << "Negative-length substring: [" << pos1 << " to " << pos2 << ")"
111 << std::endl;
112 abort();
113 }
114#endif
115 return std::string_view(input.data() + pos1, pos2 - pos1);
116}
117
123ada_really_inline void resize(std::string_view& input, size_t pos) noexcept;
124
130ada_really_inline std::pair<size_t, bool> get_host_delimiter_location(
131 const bool is_special, std::string_view& view) noexcept;
132
138ada_really_inline void trim_c0_whitespace(std::string_view& input) noexcept;
139
145template <class url_type>
146ada_really_inline void strip_trailing_spaces_from_opaque_path(
147 url_type& url) noexcept;
148
154find_authority_delimiter_special(std::string_view view) noexcept;
155
161find_authority_delimiter(std::string_view view) noexcept;
162
166template <typename T, typename... Args>
167inline void inner_concat(std::string& buffer, T t) {
168 buffer.append(t);
169}
170
174template <typename T, typename... Args>
175inline void inner_concat(std::string& buffer, T t, Args... args) {
176 buffer.append(t);
177 return inner_concat(buffer, args...);
178}
179
185template <typename... Args>
186std::string concat(Args... args) {
187 std::string answer;
188 inner_concat(answer, args...);
189 return answer;
190}
191
196inline int leading_zeroes(uint32_t input_num) noexcept {
197#if ADA_REGULAR_VISUAL_STUDIO
198 unsigned long leading_zero(0);
199 unsigned long in(input_num);
200 return _BitScanReverse(&leading_zero, in) ? int(31 - leading_zero) : 32;
201#else
202 return __builtin_clz(input_num);
203#endif // ADA_REGULAR_VISUAL_STUDIO
204}
205
212inline int fast_digit_count(uint32_t x) noexcept {
213 auto int_log2 = [](uint32_t z) -> int {
214 return 31 - ada::helpers::leading_zeroes(z | 1);
215 };
216 // Compiles to very few instructions. Note that the
217 // table is static and thus effectively a constant.
218 // We leave it inside the function because it is meaningless
219 // outside of it (this comes at no performance cost).
220 const static uint64_t table[] = {
221 4294967296, 8589934582, 8589934582, 8589934582, 12884901788,
222 12884901788, 12884901788, 17179868184, 17179868184, 17179868184,
223 21474826480, 21474826480, 21474826480, 21474826480, 25769703776,
224 25769703776, 25769703776, 30063771072, 30063771072, 30063771072,
225 34349738368, 34349738368, 34349738368, 34349738368, 38554705664,
226 38554705664, 38554705664, 41949672960, 41949672960, 41949672960,
227 42949672960, 42949672960};
228 return int((x + table[int_log2(x)]) >> 32);
229}
230} // namespace ada::helpers
231
232#endif // ADA_HELPERS_H
Common definitions for cross-platform compiler support.
#define ada_really_inline
Definition common_defs.h:84
Includes the definitions for helper functions.
const uint32_t table[8000][2]
Definition ada_idna.cpp:584
Definitions for the states of the URL state machine.
Declaration for the basic URL definitions.