Ada 2.9.2
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
15#if ADA_DEVELOPMENT_CHECKS
16#include <iostream>
17#endif // ADA_DEVELOPMENT_CHECKS
18
27namespace ada::helpers {
28
32template <typename out_iter>
33void encode_json(std::string_view view, out_iter out);
34
48ada_really_inline std::optional<std::string_view> prune_hash(
49 std::string_view& input) noexcept;
50
57ada_really_inline bool shorten_path(std::string& path,
58 ada::scheme::type type) noexcept;
59
66ada_really_inline bool shorten_path(std::string_view& path,
67 ada::scheme::type type) noexcept;
68
80ada_really_inline void parse_prepared_path(std::string_view input,
82 std::string& path);
83
88ada_really_inline void remove_ascii_tab_or_newline(std::string& input) noexcept;
89
95ada_really_inline constexpr std::string_view substring(std::string_view input,
96 size_t pos) noexcept;
97
102bool overlaps(std::string_view input1, const std::string& input2) noexcept;
103
109ada_really_inline constexpr std::string_view substring(std::string_view input,
110 size_t pos1,
111 size_t pos2) noexcept {
112#if ADA_DEVELOPMENT_CHECKS
113 if (pos2 < pos1) {
114 std::cerr << "Negative-length substring: [" << pos1 << " to " << pos2 << ")"
115 << std::endl;
116 abort();
117 }
118#endif
119 return input.substr(pos1, pos2 - pos1);
120}
121
127ada_really_inline void resize(std::string_view& input, size_t pos) noexcept;
128
134ada_really_inline std::pair<size_t, bool> get_host_delimiter_location(
135 bool is_special, std::string_view& view) noexcept;
136
142ada_really_inline void trim_c0_whitespace(std::string_view& input) noexcept;
143
149template <class url_type>
150ada_really_inline void strip_trailing_spaces_from_opaque_path(
151 url_type& url) noexcept;
152
158find_authority_delimiter_special(std::string_view view) noexcept;
159
165find_authority_delimiter(std::string_view view) noexcept;
166
170template <typename T, typename... Args>
171inline void inner_concat(std::string& buffer, T t) {
172 buffer.append(t);
173}
174
178template <typename T, typename... Args>
179inline void inner_concat(std::string& buffer, T t, Args... args) {
180 buffer.append(t);
181 return inner_concat(buffer, args...);
182}
183
189template <typename... Args>
190std::string concat(Args... args) {
191 std::string answer;
192 inner_concat(answer, args...);
193 return answer;
194}
195
200inline int leading_zeroes(uint32_t input_num) noexcept {
201#if ADA_REGULAR_VISUAL_STUDIO
202 unsigned long leading_zero(0);
203 unsigned long in(input_num);
204 return _BitScanReverse(&leading_zero, in) ? int(31 - leading_zero) : 32;
205#else
206 return __builtin_clz(input_num);
207#endif // ADA_REGULAR_VISUAL_STUDIO
208}
209
216inline int fast_digit_count(uint32_t x) noexcept {
217 auto int_log2 = [](uint32_t z) -> int {
218 return 31 - ada::helpers::leading_zeroes(z | 1);
219 };
220 // Compiles to very few instructions. Note that the
221 // table is static and thus effectively a constant.
222 // We leave it inside the function because it is meaningless
223 // outside of it (this comes at no performance cost).
224 const static uint64_t table[] = {
225 4294967296, 8589934582, 8589934582, 8589934582, 12884901788,
226 12884901788, 12884901788, 17179868184, 17179868184, 17179868184,
227 21474826480, 21474826480, 21474826480, 21474826480, 25769703776,
228 25769703776, 25769703776, 30063771072, 30063771072, 30063771072,
229 34349738368, 34349738368, 34349738368, 34349738368, 38554705664,
230 38554705664, 38554705664, 41949672960, 41949672960, 41949672960,
231 42949672960, 42949672960};
232 return int((x + table[int_log2(x)]) >> 32);
233}
234} // namespace ada::helpers
235
236#endif // ADA_HELPERS_H
Common definitions for cross-platform compiler support.
#define ada_really_inline
Definition common_defs.h:77
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.