Ada 3.4.3
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 <string>
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, ada::scheme::type type);
58
65ada_really_inline bool shorten_path(std::string_view& path,
67
79ada_really_inline void parse_prepared_path(std::string_view input,
81 std::string& path);
82
87ada_really_inline void remove_ascii_tab_or_newline(std::string& input);
88
93ada_really_inline constexpr std::string_view substring(std::string_view input,
94 size_t pos);
95
100bool overlaps(std::string_view input1, const std::string& input2) noexcept;
101
107ada_really_inline constexpr std::string_view substring(std::string_view input,
108 size_t pos1,
109 size_t pos2) {
110#if ADA_DEVELOPMENT_CHECKS
111 if (pos2 < pos1) {
112 std::cerr << "Negative-length substring: [" << pos1 << " to " << pos2 << ")"
113 << std::endl;
114 abort();
115 }
116#endif
117 return input.substr(pos1, pos2 - pos1);
118}
119
125ada_really_inline void resize(std::string_view& input, size_t pos) noexcept;
126
132ada_really_inline std::pair<size_t, bool> get_host_delimiter_location(
133 bool is_special, std::string_view& view) noexcept;
134
140void trim_c0_whitespace(std::string_view& input) noexcept;
141
147template <class url_type>
148ada_really_inline void strip_trailing_spaces_from_opaque_path(url_type& url);
149
155find_authority_delimiter_special(std::string_view view) noexcept;
156
162find_authority_delimiter(std::string_view view) noexcept;
163
167template <typename T, typename... Args>
168inline void inner_concat(std::string& buffer, T t) {
169 buffer.append(t);
170}
171
175template <typename T, typename... Args>
176inline void inner_concat(std::string& buffer, T t, Args... args) {
177 buffer.append(t);
178 return inner_concat(buffer, args...);
179}
180
186template <typename... Args>
187std::string concat(Args... args) {
188 std::string answer;
189 inner_concat(answer, args...);
190 return answer;
191}
192
197inline int leading_zeroes(uint32_t input_num) noexcept {
198#if ADA_REGULAR_VISUAL_STUDIO
199 unsigned long leading_zero(0);
200 unsigned long in(input_num);
201 return _BitScanReverse(&leading_zero, in) ? int(31 - leading_zero) : 32;
202#else
203 return __builtin_clz(input_num);
204#endif // ADA_REGULAR_VISUAL_STUDIO
205}
206
213inline int fast_digit_count(uint32_t x) noexcept {
214 auto int_log2 = [](uint32_t z) -> int {
215 return 31 - ada::helpers::leading_zeroes(z | 1);
216 };
217 // Compiles to very few instructions. Note that the
218 // table is static and thus effectively a constant.
219 // We leave it inside the function because it is meaningless
220 // outside of it (this comes at no performance cost).
221 const static uint64_t table[] = {
222 4294967296, 8589934582, 8589934582, 8589934582, 12884901788,
223 12884901788, 12884901788, 17179868184, 17179868184, 17179868184,
224 21474826480, 21474826480, 21474826480, 21474826480, 25769703776,
225 25769703776, 25769703776, 30063771072, 30063771072, 30063771072,
226 34349738368, 34349738368, 34349738368, 34349738368, 38554705664,
227 38554705664, 38554705664, 41949672960, 41949672960, 41949672960,
228 42949672960, 42949672960};
229 return int((x + table[int_log2(x)]) >> 32);
230}
231} // namespace ada::helpers
232
233#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:41
Represents a parsed URL with individual string components.
Definition url.h:62
Base class and common definitions for URL types.