Adding fix for is_integer overflow.

This should avoid using unnecessarily sized unsigned types, while also avoiding overflow.
This commit is contained in:
RealTimeChris 2024-11-27 07:41:08 -05:00
parent ee79fe6c7e
commit d88299dd4c

View File

@ -28,11 +28,20 @@ template <typename UC> fastfloat_really_inline constexpr bool has_simd_opt() {
#endif #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 // Next function can be micro-optimized, but compilers are entirely
// able to optimize it well. // able to optimize it well.
template <typename UC> template <typename UC>
fastfloat_really_inline constexpr bool is_integer(UC c) noexcept { 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) { fastfloat_really_inline constexpr uint64_t byteswap(uint64_t val) {