mirror of
https://github.com/boostorg/charconv.git
synced 2026-02-23 03:32:12 +00:00
@@ -24,7 +24,7 @@ BOOST_CHARCONV_CXX14_CONSTEXPR int num_digits(T x) noexcept
|
||||
|
||||
while (x)
|
||||
{
|
||||
x /= 10;
|
||||
x /= static_cast<T>(10);
|
||||
++digits;
|
||||
}
|
||||
|
||||
|
||||
@@ -238,7 +238,7 @@ BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_128integer_impl(char* first, c
|
||||
#endif
|
||||
)
|
||||
{
|
||||
if (value < 0)
|
||||
if (value < static_cast<Integer>(0))
|
||||
{
|
||||
is_negative = true;
|
||||
unsigned_value = -(static_cast<Unsigned_Integer>(value));
|
||||
@@ -270,7 +270,7 @@ BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_128integer_impl(char* first, c
|
||||
// If the value fits into 64 bits use the other method of processing
|
||||
if (converted_value < (std::numeric_limits<std::uint64_t>::max)())
|
||||
{
|
||||
return to_chars_integer_impl(first, last, static_cast<std::uint64_t>(value));
|
||||
return to_chars_integer_impl(first, last, static_cast<std::uint64_t>(converted_value));
|
||||
}
|
||||
|
||||
constexpr std::uint32_t ten_9 = UINT32_C(1000000000);
|
||||
@@ -278,7 +278,7 @@ BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_128integer_impl(char* first, c
|
||||
int num_chars[5] {};
|
||||
int i = 0;
|
||||
|
||||
while (converted_value != 0)
|
||||
while (converted_value != static_cast<Unsigned_Integer>(0))
|
||||
{
|
||||
auto digits = static_cast<std::uint32_t>(converted_value % ten_9);
|
||||
num_chars[i] = num_digits(digits);
|
||||
@@ -320,7 +320,7 @@ BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_integer_impl(char* first, char
|
||||
return {last, std::errc::invalid_argument};
|
||||
}
|
||||
|
||||
if (value == 0)
|
||||
if (value == static_cast<Integer>(0))
|
||||
{
|
||||
*first++ = '0';
|
||||
return {first, std::errc()};
|
||||
@@ -331,7 +331,7 @@ BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_integer_impl(char* first, char
|
||||
|
||||
BOOST_IF_CONSTEXPR (std::is_signed<Integer>::value)
|
||||
{
|
||||
if (value < 0)
|
||||
if (value < static_cast<Integer>(0))
|
||||
{
|
||||
*first++ = '-';
|
||||
unsigned_value = static_cast<Unsigned_Integer>(detail::apply_sign(value));
|
||||
@@ -358,7 +358,7 @@ BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_integer_impl(char* first, char
|
||||
switch (base)
|
||||
{
|
||||
case 2:
|
||||
while (unsigned_value != 0)
|
||||
while (unsigned_value != static_cast<Unsigned_Integer>(0))
|
||||
{
|
||||
*end-- = static_cast<char>(zero + (unsigned_value & 1U)); // 1<<1 - 1
|
||||
unsigned_value >>= static_cast<Unsigned_Integer>(1);
|
||||
@@ -366,7 +366,7 @@ BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_integer_impl(char* first, char
|
||||
break;
|
||||
|
||||
case 4:
|
||||
while (unsigned_value != 0)
|
||||
while (unsigned_value != static_cast<Unsigned_Integer>(0))
|
||||
{
|
||||
*end-- = static_cast<char>(zero + (unsigned_value & 3U)); // 1<<2 - 1
|
||||
unsigned_value >>= static_cast<Unsigned_Integer>(2);
|
||||
@@ -374,7 +374,7 @@ BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_integer_impl(char* first, char
|
||||
break;
|
||||
|
||||
case 8:
|
||||
while (unsigned_value != 0)
|
||||
while (unsigned_value != static_cast<Unsigned_Integer>(0))
|
||||
{
|
||||
*end-- = static_cast<char>(zero + (unsigned_value & 7U)); // 1<<3 - 1
|
||||
unsigned_value >>= static_cast<Unsigned_Integer>(3);
|
||||
@@ -382,7 +382,7 @@ BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_integer_impl(char* first, char
|
||||
break;
|
||||
|
||||
case 16:
|
||||
while (unsigned_value != 0)
|
||||
while (unsigned_value != static_cast<Unsigned_Integer>(0))
|
||||
{
|
||||
*end-- = digit_table[static_cast<std::size_t>(unsigned_value & 15U)]; // 1<<4 - 1
|
||||
unsigned_value >>= static_cast<Unsigned_Integer>(4);
|
||||
@@ -390,7 +390,7 @@ BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_integer_impl(char* first, char
|
||||
break;
|
||||
|
||||
case 32:
|
||||
while (unsigned_value != 0)
|
||||
while (unsigned_value != static_cast<Unsigned_Integer>(0))
|
||||
{
|
||||
*end-- = digit_table[static_cast<std::size_t>(unsigned_value & 31U)]; // 1<<5 - 1
|
||||
unsigned_value >>= static_cast<Unsigned_Integer>(5);
|
||||
@@ -398,7 +398,7 @@ BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_integer_impl(char* first, char
|
||||
break;
|
||||
|
||||
default:
|
||||
while (unsigned_value != 0)
|
||||
while (unsigned_value != static_cast<Unsigned_Integer>(0))
|
||||
{
|
||||
*end-- = digit_table[static_cast<std::size_t>(unsigned_value % unsigned_base)];
|
||||
unsigned_value /= unsigned_base;
|
||||
|
||||
@@ -74,3 +74,4 @@ run github_issue_266.cpp ;
|
||||
run github_issue_267.cpp ;
|
||||
run github_issue_280.cpp ;
|
||||
run github_issue_282.cpp ;
|
||||
run github_issue_int128_320.cpp ;
|
||||
|
||||
@@ -18,7 +18,6 @@ core
|
||||
|
||||
# Secondary dependencies
|
||||
|
||||
static_assert
|
||||
throw_exception
|
||||
)
|
||||
|
||||
|
||||
60
test/github_issue_int128_320.cpp
Normal file
60
test/github_issue_int128_320.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
// Copyright 2026 Matt Borland
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// https://www.boost.org/LICENSE_1_0.txt
|
||||
//
|
||||
// See: https://github.com/cppalliance/int128/issues/320
|
||||
|
||||
#include <boost/charconv.hpp>
|
||||
|
||||
#ifdef BOOST_CHARCONV_HAS_INT128
|
||||
|
||||
#if defined(__GNUC__) && __GNUC__ == 12
|
||||
# pragma GCC diagnostic push
|
||||
# pragma GCC diagnostic ignored "-Wstringop-overflow"
|
||||
#endif
|
||||
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
|
||||
void toChars(char* ptr, char* ptrEnd, boost::int128_type i128)
|
||||
{
|
||||
auto r = boost::charconv::to_chars(ptr, ptrEnd, i128);
|
||||
*r.ptr = '\0';
|
||||
}
|
||||
|
||||
void toChars(char* ptr, char* ptrEnd, boost::uint128_type u128)
|
||||
{
|
||||
auto r = boost::charconv::to_chars(ptr, ptrEnd, u128);
|
||||
*r.ptr = '\0';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
boost::int128_type i128 = -123;
|
||||
boost::uint128_type u128 = 123;
|
||||
|
||||
std::int64_t ref_value = -123;
|
||||
|
||||
for (int i = 1; i < 5; ++i)
|
||||
{
|
||||
i128 *= i;
|
||||
u128 *= static_cast<unsigned>(i);
|
||||
ref_value *= i;
|
||||
|
||||
char buf[64] = {};
|
||||
toChars(buf, buf + 64, i128);
|
||||
BOOST_TEST_CSTR_EQ(buf, std::to_string(ref_value).c_str());
|
||||
|
||||
toChars(buf, buf + 64, u128);
|
||||
BOOST_TEST_CSTR_EQ(buf, std::to_string(std::abs(ref_value)).c_str());
|
||||
};
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
int main() { return 0; }
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user