Skip to content

Commit 4646cc0

Browse files
committed
dont use memcpy
Signed-off-by: Shikhar <[email protected]>
1 parent bdb25af commit 4646cc0

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

include/fast_float/ascii_number.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,13 +528,29 @@ parse_int_string(UC const *p, UC const *pend, T &value,
528528
uint32_t as_int;
529529
} digits;
530530

531-
memcpy(&digits.as_int, p, 4);
531+
if (cpp20_and_in_constexpr()) {
532+
digits.as_int = 0;
533+
for (size_t j = 0; j < 4 && j < len; ++j) {
534+
digits.as_str[j] = static_cast<uint8_t>(p[j]);
535+
}
536+
} else {
537+
uint32_t b0 = static_cast<uint8_t>(p[0]);
538+
uint32_t b1 = (len > 1) ? static_cast<uint8_t>(p[1]) : 0xFFu;
539+
uint32_t b2 = (len > 2) ? static_cast<uint8_t>(p[2]) : 0xFFu;
540+
uint32_t b3 = (len > 3) ? static_cast<uint8_t>(p[3]) : 0xFFu;
541+
#if FASTFLOAT_IS_BIG_ENDIAN
542+
digits.as_int = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
543+
#else
544+
digits.as_int = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24);
545+
#endif
546+
}
547+
532548
uint32_t magic =
533549
((digits.as_int + 0x46464646u) | (digits.as_int - 0x30303030u)) &
534550
0x80808080u;
535551
uint32_t tz = (uint32_t)std::__countr_zero(magic); // 7, 15, 23, 31, or 32
536552
uint32_t nd = (tz == 32) ? 4 : (tz >> 3);
537-
nd = std::min((size_t)nd, len);
553+
nd = (uint32_t)std::min((size_t)nd, len);
538554
if (nd == 0) {
539555
if (has_leading_zeros) {
540556
value = 0;
@@ -548,7 +564,7 @@ parse_int_string(UC const *p, UC const *pend, T &value,
548564
}
549565
if (nd > 3) {
550566
const UC *q = p + nd;
551-
uint32_t rem = len - nd;
567+
size_t rem = len - nd;
552568
while (rem) {
553569
if (*q < UC('0') || *q > UC('9'))
554570
break;

0 commit comments

Comments
 (0)