Ada 4.1.1
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/url_base.h"
10
11#include <cstddef>
12#include <cstdint>
13#include <optional>
14#include <string>
15#include <string_view>
16#include <utility>
17
18#if ADA_DEVELOPMENT_CHECKS
19#include <cstdlib>
20#include <iostream>
21#endif // ADA_DEVELOPMENT_CHECKS
22
31namespace ada::helpers {
32
36template <typename out_iter>
37void encode_json(std::string_view view, out_iter out);
38
52ada_really_inline std::optional<std::string_view> prune_hash(
53 std::string_view& input) noexcept;
54
61ada_really_inline bool shorten_path(std::string& path, ada::scheme::type type);
62
69ada_really_inline bool shorten_path(std::string_view& path,
71
83ada_really_inline void parse_prepared_path(std::string_view input,
85 std::string& path);
86
91ada_really_inline void remove_ascii_tab_or_newline(std::string& input);
92
97ada_really_inline constexpr std::string_view substring(std::string_view input,
98 size_t pos);
99
104bool overlaps(std::string_view input1, const std::string& input2) noexcept;
105
111ada_really_inline constexpr std::string_view substring(std::string_view input,
112 size_t pos1,
113 size_t pos2) {
114#if ADA_DEVELOPMENT_CHECKS
115 if (pos2 < pos1) {
116 std::cerr << "Negative-length substring: [" << pos1 << " to " << pos2 << ")"
117 << std::endl;
118 abort();
119 }
120#endif
121 return input.substr(pos1, pos2 - pos1);
122}
123
129ada_really_inline void resize(std::string_view& input, size_t pos) noexcept;
130
136ada_really_inline std::pair<size_t, bool> get_host_delimiter_location(
137 bool is_special, std::string_view& view) noexcept;
138
144void trim_c0_whitespace(std::string_view& input) noexcept;
145
151template <class url_type>
152ada_really_inline void strip_trailing_spaces_from_opaque_path(url_type& url);
153
159find_authority_delimiter_special(std::string_view view) noexcept;
160
166find_authority_delimiter(std::string_view view) noexcept;
167
171template <typename T, typename... Args>
172inline void inner_concat(std::string& buffer, T t) {
173 buffer.append(t);
174}
175
179template <typename T, typename... Args>
180inline void inner_concat(std::string& buffer, T t, Args... args) {
181 buffer.append(t);
182 return inner_concat(buffer, args...);
183}
184
190template <typename... Args>
191std::string concat(Args... args) {
192 std::string answer;
193 inner_concat(answer, args...);
194 return answer;
195}
196
201inline int leading_zeroes(uint32_t input_num) noexcept {
202#if ADA_REGULAR_VISUAL_STUDIO
203 unsigned long leading_zero(0);
204 unsigned long in(input_num);
205 return _BitScanReverse(&leading_zero, in) ? int(31 - leading_zero) : 32;
206#else
207 return __builtin_clz(input_num);
208#endif // ADA_REGULAR_VISUAL_STUDIO
209}
210
217inline int fast_digit_count(uint32_t x) noexcept {
218 auto int_log2 = [](uint32_t z) -> int {
219 return 31 - ada::helpers::leading_zeroes(z | 1);
220 };
221 // Compiles to very few instructions. Note that the
222 // table is static and thus effectively a constant.
223 // We leave it inside the function because it is meaningless
224 // outside of it (this comes at no performance cost).
225 const static uint64_t table[] = {
226 4294967296, 8589934582, 8589934582, 8589934582, 12884901788,
227 12884901788, 12884901788, 17179868184, 17179868184, 17179868184,
228 21474826480, 21474826480, 21474826480, 21474826480, 25769703776,
229 25769703776, 25769703776, 30063771072, 30063771072, 30063771072,
230 34349738368, 34349738368, 34349738368, 34349738368, 38554705664,
231 38554705664, 38554705664, 41949672960, 41949672960, 41949672960,
232 42949672960, 42949672960};
233 return int((x + table[int_log2(x)]) >> 32);
234}
235} // namespace ada::helpers
236
237#endif // ADA_HELPERS_H
Cross-platform compiler macros and common definitions.
#define ada_really_inline
Definition common_defs.h:85
Includes the definitions for helper functions.
type
Enumeration of URL scheme types.
Definition scheme.h:43
Represents a parsed URL with individual string components.
Definition url.h:64
Base class and common definitions for URL types.