Ada
3.4.3
Fast spec-compliant URL parser
Toggle main menu visibility
Loading...
Searching...
No Matches
checkers-inl.h
Go to the documentation of this file.
1
5
#ifndef ADA_CHECKERS_INL_H
6
#define ADA_CHECKERS_INL_H
7
8
#include <bit>
9
#include <string_view>
10
#include "
ada/checkers.h
"
11
12
namespace
ada::checkers
{
13
14
constexpr
bool
has_hex_prefix_unsafe
(std::string_view input) {
15
// This is actually efficient code, see has_hex_prefix for the assembly.
16
constexpr
bool
is_little_endian = std::endian::native == std::endian::little;
17
constexpr
uint16_t word0x = 0x7830;
18
uint16_t two_first_bytes =
19
static_cast<
uint16_t
>
(input[0]) |
20
static_cast<
uint16_t
>
((
static_cast<
uint16_t
>
(input[1]) << 8));
21
if
constexpr
(is_little_endian) {
22
two_first_bytes |= 0x2000;
23
}
else
{
24
two_first_bytes |= 0x020;
25
}
26
return
two_first_bytes == word0x;
27
}
28
29
constexpr
bool
has_hex_prefix
(std::string_view input) {
30
return
input.size() >= 2 &&
has_hex_prefix_unsafe
(input);
31
}
32
33
constexpr
bool
is_digit
(
char
x)
noexcept
{
return
(x >=
'0'
) & (x <=
'9'
); }
34
35
constexpr
char
to_lower
(
char
x)
noexcept
{
return
(x | 0x20); }
36
37
constexpr
bool
is_alpha
(
char
x)
noexcept
{
38
return
(
to_lower
(x) >=
'a'
) && (
to_lower
(x) <=
'z'
);
39
}
40
41
constexpr
bool
is_windows_drive_letter
(std::string_view input)
noexcept
{
42
return
input.size() >= 2 &&
43
(
is_alpha
(input[0]) && ((input[1] ==
':'
) || (input[1] ==
'|'
))) &&
44
((input.size() == 2) || (input[2] ==
'/'
|| input[2] ==
'\\'
||
45
input[2] ==
'?'
|| input[2] ==
'#'
));
46
}
47
48
constexpr
bool
is_normalized_windows_drive_letter
(
49
std::string_view input)
noexcept
{
50
return
input.size() >= 2 && (
is_alpha
(input[0]) && (input[1] ==
':'
));
51
}
52
53
ada_really_inline
constexpr
uint64_t
try_parse_ipv4_fast
(
54
std::string_view input)
noexcept
{
55
const
char
* p = input.data();
56
const
char
*
const
pend = p + input.size();
57
58
uint32_t ipv4 = 0;
59
60
for
(
int
i = 0; i < 4; ++i) {
61
if
(p == pend) {
62
return
ipv4_fast_fail
;
63
}
64
65
uint32_t val;
66
char
c = *p;
67
if
(c >=
'0'
&& c <=
'9'
) {
68
val = c -
'0'
;
69
p++;
70
}
else
{
71
return
ipv4_fast_fail
;
72
}
73
74
if
(p < pend) {
75
c = *p;
76
if
(c >=
'0'
&& c <=
'9'
) {
77
if
(val == 0)
return
ipv4_fast_fail
;
78
val = val * 10 + (c -
'0'
);
79
p++;
80
if
(p < pend) {
81
c = *p;
82
if
(c >=
'0'
&& c <=
'9'
) {
83
val = val * 10 + (c -
'0'
);
84
p++;
85
if
(val > 255)
return
ipv4_fast_fail
;
86
}
87
}
88
}
89
}
90
91
ipv4 = (ipv4 << 8) | val;
92
93
if
(i < 3) {
94
if
(p == pend || *p !=
'.'
) {
95
return
ipv4_fast_fail
;
96
}
97
p++;
98
}
99
}
100
101
if
(p != pend) {
102
if
(p == pend - 1 && *p ==
'.'
) {
103
return
ipv4;
104
}
105
return
ipv4_fast_fail
;
106
}
107
108
return
ipv4;
109
}
110
111
}
// namespace ada::checkers
112
113
#endif
// ADA_CHECKERS_INL_H
checkers.h
Declarations for URL specific checkers used within Ada.
ada_really_inline
#define ada_really_inline
Definition
common_defs.h:85
ada::checkers
Includes the definitions for validation functions.
Definition
checkers-inl.h:12
ada::checkers::has_hex_prefix_unsafe
constexpr bool has_hex_prefix_unsafe(std::string_view input)
Definition
checkers-inl.h:14
ada::checkers::has_hex_prefix
constexpr bool has_hex_prefix(std::string_view input)
Definition
checkers-inl.h:29
ada::checkers::is_normalized_windows_drive_letter
constexpr bool is_normalized_windows_drive_letter(std::string_view input) noexcept
Definition
checkers-inl.h:48
ada::checkers::is_windows_drive_letter
constexpr bool is_windows_drive_letter(std::string_view input) noexcept
Definition
checkers-inl.h:41
ada::checkers::to_lower
constexpr char to_lower(char x) noexcept
Definition
checkers-inl.h:35
ada::checkers::ipv4_fast_fail
constexpr uint64_t ipv4_fast_fail
Definition
checkers.h:129
ada::checkers::is_alpha
constexpr bool is_alpha(char x) noexcept
Definition
checkers-inl.h:37
ada::checkers::is_digit
constexpr bool is_digit(char x) noexcept
Definition
checkers-inl.h:33
ada::checkers::try_parse_ipv4_fast
ada_really_inline constexpr uint64_t try_parse_ipv4_fast(std::string_view input) noexcept
Definition
checkers-inl.h:53