Skip to content

Commit

Permalink
Adding fix for is_integer overflow.
Browse files Browse the repository at this point in the history
This should avoid using unnecessarily sized unsigned types, while also avoiding overflow.
  • Loading branch information
RealTimeChris committed Nov 27, 2024
1 parent ee79fe6 commit d88299d
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion include/fast_float/ascii_number.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,20 @@ template <typename UC> fastfloat_really_inline constexpr bool has_simd_opt() {
#endif
}

template <typename value_type> struct get_equal_sized_uint {
using type = std::conditional_t<
sizeof(value_type) == 4, uint32_t,
std::conditional_t<sizeof(value_type) == 2, uint16_t, uint8_t>>;
};

template <typename value_type>
using get_equal_sized_uint_t = typename get_equal_sized_uint<value_type>::type;

// Next function can be micro-optimized, but compilers are entirely
// able to optimize it well.
template <typename UC>
fastfloat_really_inline constexpr bool is_integer(UC c) noexcept {
return static_cast<uint8_t>(c - UC('0')) < 10;
return static_cast<get_equal_sized_uint_t<UC>>(c - UC('0')) < 10;
}

fastfloat_really_inline constexpr uint64_t byteswap(uint64_t val) {
Expand Down

0 comments on commit d88299d

Please sign in to comment.