Skip to content

Commit 7041f91

Browse files
committed
2 parents 600f236 + 1ad224e commit 7041f91

File tree

4 files changed

+226
-0
lines changed

4 files changed

+226
-0
lines changed

include/fast_float/ascii_number.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,92 @@ parse_int_string(UC const *p, UC const *pend, T &value,
550550

551551
auto const *const start_digits = p;
552552

553+
FASTFLOAT_IF_CONSTEXPR17((std::is_same<T, std::uint8_t>::value)) {
554+
const size_t len = (size_t)(pend - p);
555+
if (len == 0) {
556+
if (has_leading_zeros) {
557+
value = 0;
558+
answer.ec = std::errc();
559+
answer.ptr = p;
560+
} else {
561+
answer.ec = std::errc::invalid_argument;
562+
answer.ptr = first;
563+
}
564+
return answer;
565+
}
566+
567+
union {
568+
uint8_t as_str[4];
569+
uint32_t as_int;
570+
} digits;
571+
572+
if (cpp20_and_in_constexpr()) {
573+
digits.as_int = 0;
574+
for (size_t j = 0; j < 4 && j < len; ++j) {
575+
digits.as_str[j] = static_cast<uint8_t>(p[j]);
576+
}
577+
} else if (len >= 4) {
578+
memcpy(&digits.as_int, p, 4);
579+
} else {
580+
uint32_t b0 = static_cast<uint8_t>(p[0]);
581+
uint32_t b1 = (len > 1) ? static_cast<uint8_t>(p[1]) : 0xFFu;
582+
uint32_t b2 = (len > 2) ? static_cast<uint8_t>(p[2]) : 0xFFu;
583+
uint32_t b3 = 0xFFu;
584+
#if FASTFLOAT_IS_BIG_ENDIAN
585+
digits.as_int = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
586+
#else
587+
digits.as_int = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24);
588+
#endif
589+
}
590+
591+
uint32_t magic =
592+
((digits.as_int + 0x46464646u) | (digits.as_int - 0x30303030u)) &
593+
0x80808080u;
594+
uint32_t tz = (uint32_t)countr_zero_32(magic); // 7, 15, 23, 31, or 32
595+
uint32_t nd = (tz == 32) ? 4 : (tz >> 3);
596+
nd = (uint32_t)std::min((size_t)nd, len);
597+
if (nd == 0) {
598+
if (has_leading_zeros) {
599+
value = 0;
600+
answer.ec = std::errc();
601+
answer.ptr = p;
602+
return answer;
603+
}
604+
answer.ec = std::errc::invalid_argument;
605+
answer.ptr = first;
606+
return answer;
607+
}
608+
if (nd > 3) {
609+
const UC *q = p + nd;
610+
size_t rem = len - nd;
611+
while (rem) {
612+
if (*q < UC('0') || *q > UC('9'))
613+
break;
614+
++q;
615+
--rem;
616+
}
617+
answer.ec = std::errc::result_out_of_range;
618+
answer.ptr = q;
619+
return answer;
620+
}
621+
622+
digits.as_int ^= 0x30303030u;
623+
digits.as_int <<= ((4 - nd) * 8);
624+
625+
uint32_t check = ((digits.as_int >> 24) & 0xff) |
626+
((digits.as_int >> 8) & 0xff00) |
627+
((digits.as_int << 8) & 0xff0000);
628+
if (check > 0x00020505) {
629+
answer.ec = std::errc::result_out_of_range;
630+
answer.ptr = p + nd;
631+
return answer;
632+
}
633+
value = (uint8_t)((0x640a01 * digits.as_int) >> 24);
634+
answer.ec = std::errc();
635+
answer.ptr = p + nd;
636+
return answer;
637+
}
638+
553639
// Parse digits
554640
am_mant_t i = 0;
555641
if (options.base == 10) {

include/fast_float/float_common.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,52 @@ leading_zeroes(uint64_t input_num) noexcept {
398398
#endif
399399
}
400400

401+
/* Helper C++14 constexpr generic implementation of countr_zero for 32-bit */
402+
fastfloat_really_inline FASTFLOAT_CONSTEXPR14 int
403+
countr_zero_generic_32(uint32_t input_num) {
404+
if (input_num == 0) {
405+
return 32;
406+
}
407+
int last_bit = 0;
408+
if (!(input_num & 0x0000FFFF)) {
409+
input_num >>= 16;
410+
last_bit |= 16;
411+
}
412+
if (!(input_num & 0x00FF)) {
413+
input_num >>= 8;
414+
last_bit |= 8;
415+
}
416+
if (!(input_num & 0x0F)) {
417+
input_num >>= 4;
418+
last_bit |= 4;
419+
}
420+
if (!(input_num & 0x3)) {
421+
input_num >>= 2;
422+
last_bit |= 2;
423+
}
424+
if (!(input_num & 0x1)) {
425+
last_bit |= 1;
426+
}
427+
return last_bit;
428+
}
429+
430+
/* count trailing zeroes for 32-bit integers */
431+
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 int
432+
countr_zero_32(uint32_t input_num) {
433+
if (cpp20_and_in_constexpr()) {
434+
return countr_zero_generic_32(input_num);
435+
}
436+
#ifdef FASTFLOAT_VISUAL_STUDIO
437+
unsigned long trailing_zero = 0;
438+
if (_BitScanForward(&trailing_zero, input_num)) {
439+
return (int)trailing_zero;
440+
}
441+
return 32;
442+
#else
443+
return input_num == 0 ? 32 : __builtin_ctz(input_num);
444+
#endif
445+
}
446+
401447
// slow emulation routine for 32-bit
402448
fastfloat_really_inline constexpr uint64_t emulu(uint32_t x,
403449
uint32_t y) noexcept {

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ endif()
9494
option(FASTFLOAT_EXHAUSTIVE "Exhaustive tests" OFF)
9595

9696
if (FASTFLOAT_EXHAUSTIVE)
97+
fast_float_add_cpp_test(ipv4_test)
9798
fast_float_add_cpp_test(short_random_string)
9899
fast_float_add_cpp_test(exhaustive32_midpoint)
99100
fast_float_add_cpp_test(random_string)

tests/ipv4_test.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
#include <charconv>
3+
#include <cstdint>
4+
#include <iostream>
5+
#include <algorithm>
6+
#include "fast_float/fast_float.h"
7+
8+
char *uint8_to_chars_manual(char *ptr, uint8_t value) {
9+
if (value == 0) {
10+
*ptr++ = '0';
11+
return ptr;
12+
}
13+
char *start = ptr;
14+
while (value > 0) {
15+
*ptr++ = '0' + (value % 10);
16+
value /= 10;
17+
}
18+
// Reverse the digits written so far
19+
std::reverse(start, ptr);
20+
return ptr;
21+
}
22+
23+
void uint32_to_ipv4_string(uint32_t ip, char *buffer) {
24+
uint8_t octets[4] = {static_cast<uint8_t>(ip >> 24),
25+
static_cast<uint8_t>(ip >> 16),
26+
static_cast<uint8_t>(ip >> 8), static_cast<uint8_t>(ip)};
27+
28+
char *ptr = buffer;
29+
30+
for (int i = 0; i < 4; ++i) {
31+
ptr = uint8_to_chars_manual(ptr, octets[i]);
32+
33+
if (i < 3) {
34+
*ptr++ = '.';
35+
}
36+
}
37+
*ptr = '\0';
38+
}
39+
40+
fastfloat_really_inline uint32_t ipv4_string_to_uint32(const char *str,
41+
const char *end) {
42+
uint32_t ip = 0;
43+
const char *current = str;
44+
45+
for (int i = 0; i < 4; ++i) {
46+
uint8_t value;
47+
auto r = fast_float::from_chars(current, end, value);
48+
if (r.ec != std::errc()) {
49+
throw std::invalid_argument("Invalid IP address format");
50+
}
51+
current = r.ptr;
52+
ip = (ip << 8) | value;
53+
54+
if (i < 3) {
55+
if (current == end || *current++ != '.') {
56+
throw std::invalid_argument("Invalid IP address format");
57+
}
58+
}
59+
}
60+
return ip;
61+
}
62+
63+
bool test_all_ipv4_conversions() {
64+
std::cout << "Testing all IPv4 conversions... 0, 1000, 2000, 3000, 4000, "
65+
"5000, 6000, 7000, 8000, 9000, ..."
66+
<< std::endl;
67+
char buffer[16];
68+
for (uint64_t ip = 0; ip <= 0xFFFFFFFF; ip += 1000) {
69+
if (ip % 10000000 == 0) {
70+
std::cout << "." << std::flush;
71+
}
72+
uint32_to_ipv4_string(static_cast<uint32_t>(ip), buffer);
73+
const char *end = buffer + strlen(buffer);
74+
uint32_t parsed_ip = ipv4_string_to_uint32(buffer, end);
75+
if (parsed_ip != ip) {
76+
std::cerr << "Mismatch: original " << ip << ", parsed " << parsed_ip
77+
<< std::endl;
78+
return false;
79+
}
80+
}
81+
std::cout << std::endl;
82+
return true;
83+
}
84+
85+
int main() {
86+
if (test_all_ipv4_conversions()) {
87+
std::cout << "All IPv4 conversions passed!" << std::endl;
88+
return EXIT_SUCCESS;
89+
} else {
90+
std::cerr << "IPv4 conversion test failed!" << std::endl;
91+
return EXIT_FAILURE;
92+
}
93+
}

0 commit comments

Comments
 (0)