Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
scheme-inl.h
Go to the documentation of this file.
1
5#ifndef ADA_SCHEME_INL_H
6#define ADA_SCHEME_INL_H
7
8#include "ada/scheme.h"
9
10#include <cstddef>
11#include <cstdint>
12#include <string_view>
13
14namespace ada::scheme {
15
20namespace details {
21// for use with is_special and get_special_port
22// Spaces, if present, are removed from URL.
23constexpr std::string_view is_special_list[] = {"http", " ", "https", "ws",
24 "ftp", "wss", "file", " "};
25// for use with get_special_port
26constexpr uint16_t special_ports[] = {80, 0, 443, 80, 21, 443, 0, 0};
27
28// @private
29// convert a string_view to a 64-bit integer key for fast comparison
30constexpr uint64_t make_key(std::string_view sv) {
31 uint64_t val = 0;
32 for (size_t i = 0; i < sv.size(); i++)
33 val |= (uint64_t)(uint8_t)sv[i] << (i * 8);
34 return val;
35}
36// precomputed keys for the special schemes, indexed by a hash of the input
37// string
38constexpr uint64_t scheme_keys[] = {
39 make_key("http"), // 0: HTTP
40 0, // 1: sentinel
41 make_key("https"), // 2: HTTPS
42 make_key("ws"), // 3: WS
43 make_key("ftp"), // 4: FTP
44 make_key("wss"), // 5: WSS
45 make_key("file"), // 6: FILE
46 0, // 7: sentinel
47};
48
49// @private
50// branchless load of up to 5 characters into a uint64_t, padding with zeros if
51// n < 5
52inline uint64_t branchless_load5(const char* p, size_t n) {
53 uint64_t input = (uint8_t)p[0];
54 input |= ((uint64_t)(uint8_t)p[n > 1] << 8) & (0 - (uint64_t)(n > 1));
55 input |= ((uint64_t)(uint8_t)p[(n > 2) * 2] << 16) & (0 - (uint64_t)(n > 2));
56 input |= ((uint64_t)(uint8_t)p[(n > 3) * 3] << 24) & (0 - (uint64_t)(n > 3));
57 input |= ((uint64_t)(uint8_t)p[(n > 4) * 4] << 32) & (0 - (uint64_t)(n > 4));
58 return input;
59}
60} // namespace details
61
62
85
86ada_really_inline constexpr bool is_special(std::string_view scheme) {
87 if (scheme.empty()) {
88 return false;
89 }
90 int hash_value = (2 * scheme.size() + (unsigned)(scheme[0])) & 7;
91 const std::string_view target = details::is_special_list[hash_value];
92 return (target[0] == scheme[0]) && (target.substr(1) == scheme.substr(1));
93}
94constexpr uint16_t get_special_port(std::string_view scheme) noexcept {
95 if (scheme.empty()) {
96 return 0;
97 }
98 int hash_value = (2 * scheme.size() + (unsigned)(scheme[0])) & 7;
99 const std::string_view target = details::is_special_list[hash_value];
100 if (scheme.size() == target.size() &&
101 details::branchless_load5(scheme.data(), scheme.size()) ==
102 details::scheme_keys[hash_value]) {
103 return details::special_ports[hash_value];
104 } else {
105 return 0;
106 }
107}
108constexpr uint16_t get_special_port(ada::scheme::type type) noexcept {
109 return details::special_ports[int(type)];
110}
111constexpr ada::scheme::type get_scheme_type(std::string_view scheme) noexcept {
112 if (scheme.empty()) {
114 }
115 int hash_value = (2 * scheme.size() + (unsigned)(scheme[0])) & 7;
116 const std::string_view target = details::is_special_list[hash_value];
117 if (scheme.size() == target.size() &&
118 details::branchless_load5(scheme.data(), scheme.size()) ==
119 details::scheme_keys[hash_value]) {
120 return ada::scheme::type(hash_value);
121 } else {
123 }
124}
125
126} // namespace ada::scheme
127
128#endif // ADA_SCHEME_INL_H
#define ada_really_inline
Definition common_defs.h:85
Includes the definitions for scheme specific entities.
constexpr std::string_view is_special_list[]
Definition scheme-inl.h:23
constexpr uint64_t scheme_keys[]
Definition scheme-inl.h:38
constexpr uint16_t special_ports[]
Definition scheme-inl.h:26
uint64_t branchless_load5(const char *p, size_t n)
Definition scheme-inl.h:52
constexpr uint64_t make_key(std::string_view sv)
Definition scheme-inl.h:30
URL scheme utilities and constants.
Definition scheme-inl.h:14
constexpr ada::scheme::type get_scheme_type(std::string_view scheme) noexcept
Definition scheme-inl.h:111
type
Enumeration of URL scheme types.
Definition scheme.h:43
@ NOT_SPECIAL
Definition scheme.h:45
constexpr uint16_t get_special_port(std::string_view scheme) noexcept
Definition scheme-inl.h:94
URL scheme type definitions and utilities.