-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnschar.hpp
57 lines (48 loc) · 1.43 KB
/
dnschar.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <string>
#define dnsname_unlikely(X) __builtin_expect(!!(X), 0)
struct DNSCharTraits: std::char_traits<char> {
static constexpr bool
is_upper (char_type const c) noexcept {
return ((c >= 'A') && (c <= 'Z'));
}
static constexpr bool
is_lower (char_type const c) noexcept {
return ((c >= 'a') && (c <= 'z'));
}
static void
lower (char_type& c) noexcept {
if (is_upper(c)) { c |= 0x20; }
}
static void
lower (char_type& c1, char_type& c2) noexcept {
lower (c1);
lower (c2);
}
static int_type
compare (char_type const* s1, char_type const* s2, std::size_t n) noexcept {
while (n--) {
auto c1 = static_cast<unsigned char>(*s1);
auto c2 = static_cast<unsigned char>(*s2);
int_type const d = { c1 - c2 };
if ((!d) || dnsname_unlikely ((d == -0x20) && is_upper (*s1))
|| dnsname_unlikely ((d == 0x20) && is_lower (*s1))) {
++s1;
++s2;
continue;
}
return d;
}
return 0;
}
static bool
eq (char_type c1, char_type c2) noexcept {
lower (c1, c2);
return (c1 == c2);
}
static bool
lt (char_type c1, char_type c2) noexcept {
lower (c1, c2);
return (static_cast<unsigned char>(c1) < static_cast<unsigned char>(c2));
}
};
#undef dnsname_unlikely