forked from fastfloat/fast_float
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipv4_test.cpp
More file actions
93 lines (82 loc) · 2.35 KB
/
ipv4_test.cpp
File metadata and controls
93 lines (82 loc) · 2.35 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <charconv>
#include <cstdint>
#include <iostream>
#include <algorithm>
#include "fast_float/fast_float.h"
char *uint8_to_chars_manual(char *ptr, uint8_t value) {
if (value == 0) {
*ptr++ = '0';
return ptr;
}
char *start = ptr;
while (value > 0) {
*ptr++ = '0' + (value % 10);
value /= 10;
}
// Reverse the digits written so far
std::reverse(start, ptr);
return ptr;
}
void uint32_to_ipv4_string(uint32_t ip, char *buffer) {
uint8_t octets[4] = {static_cast<uint8_t>(ip >> 24),
static_cast<uint8_t>(ip >> 16),
static_cast<uint8_t>(ip >> 8), static_cast<uint8_t>(ip)};
char *ptr = buffer;
for (int i = 0; i < 4; ++i) {
ptr = uint8_to_chars_manual(ptr, octets[i]);
if (i < 3) {
*ptr++ = '.';
}
}
*ptr = '\0';
}
fastfloat_really_inline uint32_t ipv4_string_to_uint32(const char *str,
const char *end) {
uint32_t ip = 0;
const char *current = str;
for (int i = 0; i < 4; ++i) {
uint8_t value;
auto r = fast_float::from_chars(current, end, value);
if (r.ec != std::errc()) {
throw std::invalid_argument("Invalid IP address format");
}
current = r.ptr;
ip = (ip << 8) | value;
if (i < 3) {
if (current == end || *current++ != '.') {
throw std::invalid_argument("Invalid IP address format");
}
}
}
return ip;
}
bool test_all_ipv4_conversions() {
std::cout << "Testing all IPv4 conversions... 0, 1000, 2000, 3000, 4000, "
"5000, 6000, 7000, 8000, 9000, ..."
<< std::endl;
char buffer[16];
for (uint64_t ip = 0; ip <= 0xFFFFFFFF; ip += 1000) {
if (ip % 10000000 == 0) {
std::cout << "." << std::flush;
}
uint32_to_ipv4_string(static_cast<uint32_t>(ip), buffer);
const char *end = buffer + strlen(buffer);
uint32_t parsed_ip = ipv4_string_to_uint32(buffer, end);
if (parsed_ip != ip) {
std::cerr << "Mismatch: original " << ip << ", parsed " << parsed_ip
<< std::endl;
return false;
}
}
std::cout << std::endl;
return true;
}
int main() {
if (test_all_ipv4_conversions()) {
std::cout << "All IPv4 conversions passed!" << std::endl;
return EXIT_SUCCESS;
} else {
std::cerr << "IPv4 conversion test failed!" << std::endl;
return EXIT_FAILURE;
}
}