Ada 2.8.0
Fast spec-compliant URL parser
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 "ada/common_defs.h"
9
10#include <algorithm>
11#include <string_view>
12#include <cstring>
13
14namespace ada::checkers {
15
16inline bool has_hex_prefix_unsafe(std::string_view input) {
17 // This is actually efficient code, see has_hex_prefix for the assembly.
18 uint32_t value_one = 1;
19 bool is_little_endian = (reinterpret_cast<char*>(&value_one)[0] == 1);
20 uint16_t word0x{};
21 std::memcpy(&word0x, "0x", 2); // we would use bit_cast in C++20 and the
22 // function could be constexpr.
23 uint16_t two_first_bytes{};
24 std::memcpy(&two_first_bytes, input.data(), 2);
25 if (is_little_endian) {
26 two_first_bytes |= 0x2000;
27 } else {
28 two_first_bytes |= 0x020;
29 }
30 return two_first_bytes == word0x;
31}
32
33inline bool has_hex_prefix(std::string_view input) {
34 return input.size() >= 2 && has_hex_prefix_unsafe(input);
35}
36
37constexpr bool is_digit(char x) noexcept { return (x >= '0') & (x <= '9'); }
38
39constexpr char to_lower(char x) noexcept { return (x | 0x20); }
40
41constexpr bool is_alpha(char x) noexcept {
42 return (to_lower(x) >= 'a') && (to_lower(x) <= 'z');
43}
44
45inline constexpr bool is_windows_drive_letter(std::string_view input) noexcept {
46 return input.size() >= 2 &&
47 (is_alpha(input[0]) && ((input[1] == ':') || (input[1] == '|'))) &&
48 ((input.size() == 2) || (input[2] == '/' || input[2] == '\\' ||
49 input[2] == '?' || input[2] == '#'));
50}
51
53 std::string_view input) noexcept {
54 return input.size() >= 2 && (is_alpha(input[0]) && (input[1] == ':'));
55}
56
57ada_really_inline bool begins_with(std::string_view view,
58 std::string_view prefix) {
59 // in C++20, you have view.begins_with(prefix)
60 // std::equal is constexpr in C++20
61 return view.size() >= prefix.size() &&
62 std::equal(prefix.begin(), prefix.end(), view.begin());
63}
64
65} // namespace ada::checkers
66
67#endif // ADA_CHECKERS_INL_H
Common definitions for cross-platform compiler support.
#define ada_really_inline
Definition common_defs.h:84
Includes the definitions for validation functions.
bool has_hex_prefix_unsafe(std::string_view input)
constexpr bool is_normalized_windows_drive_letter(std::string_view input) noexcept
constexpr bool is_windows_drive_letter(std::string_view input) noexcept
constexpr char to_lower(char x) noexcept
bool has_hex_prefix(std::string_view input)
constexpr bool is_alpha(char x) noexcept
constexpr bool is_digit(char x) noexcept
ada_really_inline bool begins_with(std::string_view view, std::string_view prefix)