Ada
4.1.1
Fast spec-compliant URL parser
Toggle main menu visibility
Loading...
Searching...
No Matches
serializers.cpp
Go to the documentation of this file.
1
#include "
ada/serializers.h
"
2
#include "
ada/common_defs.h
"
3
4
#include <array>
5
#include <cstdint>
6
#include <cstring>
7
#include <string>
8
9
namespace
ada::serializers
{
10
namespace
{
11
12
// Digit pair LUT for fast decimal write: index 0..99 -> two chars.
13
constexpr
std::array<char, 200>
make_digit_pairs
() noexcept {
14
std::array<char, 200> t{};
15
for
(
size_t
i = 0; i < 100; ++i) {
16
t[i * 2] =
static_cast<
char
>
(
'0'
+ i / 10);
17
t[i * 2 + 1] =
static_cast<
char
>
(
'0'
+ i % 10);
18
}
19
return
t;
20
}
21
22
constexpr
auto
digit_pairs
=
make_digit_pairs
();
23
24
ada_really_inline
char
*
write_u8
(
char
* p, uint8_t v)
noexcept
{
25
if
(v < 10) {
26
*p++ =
static_cast<
char
>
(
'0'
+ v);
27
}
else
if
(v < 100) {
28
std::memcpy(p, &
digit_pairs
[
static_cast<
size_t
>
(v) * 2], 2);
29
p += 2;
30
}
else
{
31
*p++ =
static_cast<
char
>
(
'0'
+ v / 100);
32
std::memcpy(p, &
digit_pairs
[
static_cast<
size_t
>
(v % 100) * 2], 2);
33
p += 2;
34
}
35
return
p;
36
}
37
38
ada_really_inline
char
*
write_hex_u16
(
char
* p, uint16_t v)
noexcept
{
39
static
constexpr
char
kHex[] =
"0123456789abcdef"
;
40
if
(v >= 0x1000) {
41
*p++ = kHex[(v >> 12) & 0xf];
42
*p++ = kHex[(v >> 8) & 0xf];
43
*p++ = kHex[(v >> 4) & 0xf];
44
*p++ = kHex[v & 0xf];
45
}
else
if
(v >= 0x100) {
46
*p++ = kHex[(v >> 8) & 0xf];
47
*p++ = kHex[(v >> 4) & 0xf];
48
*p++ = kHex[v & 0xf];
49
}
else
if
(v >= 0x10) {
50
*p++ = kHex[(v >> 4) & 0xf];
51
*p++ = kHex[v & 0xf];
52
}
else
{
53
*p++ = kHex[v & 0xf];
54
}
55
return
p;
56
}
57
58
}
// namespace
59
60
void
find_longest_sequence_of_ipv6_pieces
(
61
const
std::array<uint16_t, 8>& address,
size_t
& compress,
62
size_t
& compress_length)
noexcept
{
63
compress = 0;
64
compress_length = 0;
65
size_t
i = 0;
66
while
(i < 8) {
67
if
(address[i] != 0) {
68
++i;
69
continue
;
70
}
71
size_t
next = i + 1;
72
while
(next < 8 && address[next] == 0) {
73
++next;
74
}
75
const
size_t
count = next - i;
76
if
(count > compress_length) {
77
compress_length = count;
78
compress = i;
79
}
80
i = next;
81
}
82
}
83
84
std::string
ipv6
(
const
std::array<uint16_t, 8>& address) {
85
size_t
compress = 0;
86
size_t
compress_length = 0;
87
find_longest_sequence_of_ipv6_pieces
(address, compress, compress_length);
88
89
// Skip compression when the longest zero run is a single piece.
90
if
(compress_length <= 1) {
91
compress = compress_length = 8;
92
}
93
94
// Max: '[' + 8*4 hex + 7 ':' + ']' = 41
95
char
output[4 * 8 + 7 + 2];
96
char
* point = output;
97
*point++ =
'['
;
98
size_t
piece_index = 0;
99
while
(
true
) {
100
if
(piece_index == compress) {
101
*point++ =
':'
;
102
// If we skip a value initially, we need to write '::'.
103
if
(piece_index == 0) {
104
*point++ =
':'
;
105
}
106
piece_index += compress_length;
107
if
(piece_index == 8) {
108
break
;
109
}
110
}
111
point = write_hex_u16(point, address[piece_index]);
112
++piece_index;
113
if
(piece_index == 8) {
114
break
;
115
}
116
*point++ =
':'
;
117
}
118
*point++ =
']'
;
119
return
std::string(output,
static_cast<
size_t
>
(point - output));
120
}
121
122
std::string
ipv4
(
const
uint64_t address) {
123
std::string output(15,
'\0'
);
124
char
* point = output.data();
125
point = write_u8(point,
static_cast<
uint8_t
>
(address >> 24));
126
*point++ =
'.'
;
127
point = write_u8(point,
static_cast<
uint8_t
>
(address >> 16));
128
*point++ =
'.'
;
129
point = write_u8(point,
static_cast<
uint8_t
>
(address >> 8));
130
*point++ =
'.'
;
131
point = write_u8(point,
static_cast<
uint8_t
>
(address));
132
output.resize(
static_cast<
size_t
>
(point - output.data()));
133
return
output;
134
}
135
136
}
// namespace ada::serializers
common_defs.h
Cross-platform compiler macros and common definitions.
ada_really_inline
#define ada_really_inline
Definition
common_defs.h:85
ada::serializers::anonymous_namespace{serializers.cpp}::write_u8
ada_really_inline char * write_u8(char *p, uint8_t v) noexcept
Definition
serializers.cpp:24
ada::serializers::anonymous_namespace{serializers.cpp}::make_digit_pairs
constexpr std::array< char, 200 > make_digit_pairs() noexcept
Definition
serializers.cpp:13
ada::serializers::anonymous_namespace{serializers.cpp}::write_hex_u16
ada_really_inline char * write_hex_u16(char *p, uint16_t v) noexcept
Definition
serializers.cpp:38
ada::serializers::anonymous_namespace{serializers.cpp}::digit_pairs
constexpr auto digit_pairs
Definition
serializers.cpp:22
ada::serializers
IP address serialization functions.
ada::serializers::find_longest_sequence_of_ipv6_pieces
void find_longest_sequence_of_ipv6_pieces(const std::array< uint16_t, 8 > &address, size_t &compress, size_t &compress_length) noexcept
Definition
serializers.cpp:60
ada::serializers::ipv6
std::string ipv6(const std::array< uint16_t, 8 > &address)
Definition
serializers.cpp:84
ada::serializers::ipv4
std::string ipv4(uint64_t address)
Definition
serializers.cpp:122
serializers.h
IP address serialization utilities.