Skip to content

Commit

Permalink
use templated is_negative
Browse files Browse the repository at this point in the history
  • Loading branch information
nickaein committed May 19, 2021
1 parent db78ac1 commit 25ed410
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
17 changes: 15 additions & 2 deletions include/nlohmann/detail/output/serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,20 @@ class serializer
}
}

template <typename NumberType,
enable_if_t<std::is_signed<NumberType>::value, int> = 0>
bool is_negative_number(NumberType x)
{
return x < 0;
}

template < typename NumberType,
enable_if_t < std::is_unsigned<NumberType>::value, int > = 0 >
bool is_negative_number(NumberType /*unused*/)
{
return false;
}

/*!
@brief dump an integer
Expand Down Expand Up @@ -701,12 +715,11 @@ class serializer
// use a pointer to fill the buffer
auto buffer_ptr = number_buffer.begin();

const bool is_negative = std::is_same<NumberType, number_integer_t>::value && !(x >= 0); // see issue #755
number_unsigned_t abs_value;

unsigned int n_chars;

if (is_negative)
if (is_negative_number(x))
{
*buffer_ptr = '-';
abs_value = remove_sign(static_cast<number_integer_t>(x));
Expand Down
17 changes: 15 additions & 2 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16096,6 +16096,20 @@ class serializer
}
}

template <typename NumberType,
enable_if_t<std::is_signed<NumberType>::value, int> = 0>
bool is_negative_number(NumberType x)
{
return x < 0;
}

template < typename NumberType,
enable_if_t < std::is_unsigned<NumberType>::value, int > = 0 >
bool is_negative_number(NumberType /*unused*/)
{
return false;
}

/*!
@brief dump an integer

Expand Down Expand Up @@ -16138,12 +16152,11 @@ class serializer
// use a pointer to fill the buffer
auto buffer_ptr = number_buffer.begin();

const bool is_negative = std::is_same<NumberType, number_integer_t>::value && !(x >= 0); // see issue #755
number_unsigned_t abs_value;

unsigned int n_chars;

if (is_negative)
if (is_negative_number(x))
{
*buffer_ptr = '-';
abs_value = remove_sign(static_cast<number_integer_t>(x));
Expand Down

0 comments on commit 25ed410

Please sign in to comment.