Skip to content

Commit ecad0f5

Browse files
modernizing integer comparison (nlohmann#4559)
1 parent 663058e commit ecad0f5

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

include/nlohmann/detail/input/binary_reader.hpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class binary_reader
172172
std::int32_t document_size{};
173173
get_number<std::int32_t, true>(input_format_t::bson, document_size);
174174

175-
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
175+
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(std::numeric_limits<std::size_t>::max())))
176176
{
177177
return false;
178178
}
@@ -394,7 +394,7 @@ class binary_reader
394394
std::int32_t document_size{};
395395
get_number<std::int32_t, true>(input_format_t::bson, document_size);
396396

397-
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
397+
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(std::numeric_limits<std::size_t>::max())))
398398
{
399399
return false;
400400
}
@@ -654,7 +654,7 @@ class binary_reader
654654
}
655655

656656
case 0x9F: // array (indefinite length)
657-
return get_cbor_array(static_cast<std::size_t>(-1), tag_handler);
657+
return get_cbor_array(std::numeric_limits<std::size_t>::max(), tag_handler);
658658

659659
// map (0x00..0x17 pairs of data items follow)
660660
case 0xA0:
@@ -708,7 +708,7 @@ class binary_reader
708708
}
709709

710710
case 0xBF: // map (indefinite length)
711-
return get_cbor_object(static_cast<std::size_t>(-1), tag_handler);
711+
return get_cbor_object(std::numeric_limits<std::size_t>::max(), tag_handler);
712712

713713
case 0xC6: // tagged item
714714
case 0xC7:
@@ -1096,9 +1096,9 @@ class binary_reader
10961096
}
10971097

10981098
/*!
1099-
@param[in] len the length of the array or static_cast<std::size_t>(-1) for an
1099+
@param[in] len the length of the array or std::numeric_limits<std::size_t>::max() for an
11001100
array of indefinite size
1101-
@param[in] tag_handler how CBOR tags should be treated
1101+
@param[in] tag_handler how CBOR tags should be treated
11021102
@return whether array creation completed
11031103
*/
11041104
bool get_cbor_array(const std::size_t len,
@@ -1109,7 +1109,7 @@ class binary_reader
11091109
return false;
11101110
}
11111111

1112-
if (len != static_cast<std::size_t>(-1))
1112+
if (len != std::numeric_limits<std::size_t>::max())
11131113
{
11141114
for (std::size_t i = 0; i < len; ++i)
11151115
{
@@ -1134,7 +1134,7 @@ class binary_reader
11341134
}
11351135

11361136
/*!
1137-
@param[in] len the length of the object or static_cast<std::size_t>(-1) for an
1137+
@param[in] len the length of the object or std::numeric_limits<std::size_t>::max() for an
11381138
object of indefinite size
11391139
@param[in] tag_handler how CBOR tags should be treated
11401140
@return whether object creation completed
@@ -1150,7 +1150,7 @@ class binary_reader
11501150
if (len != 0)
11511151
{
11521152
string_t key;
1153-
if (len != static_cast<std::size_t>(-1))
1153+
if (len != std::numeric_limits<std::size_t>::max())
11541154
{
11551155
for (std::size_t i = 0; i < len; ++i)
11561156
{
@@ -2568,7 +2568,7 @@ class binary_reader
25682568
}
25692569
else
25702570
{
2571-
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast<std::size_t>(-1))))
2571+
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(std::numeric_limits<std::size_t>::max())))
25722572
{
25732573
return false;
25742574
}
@@ -2646,7 +2646,7 @@ class binary_reader
26462646
}
26472647
else
26482648
{
2649-
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast<std::size_t>(-1))))
2649+
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(std::numeric_limits<std::size_t>::max())))
26502650
{
26512651
return false;
26522652
}
@@ -2982,7 +2982,7 @@ class binary_reader
29822982
}
29832983

29842984
private:
2985-
static JSON_INLINE_VARIABLE constexpr std::size_t npos = static_cast<std::size_t>(-1);
2985+
static JSON_INLINE_VARIABLE constexpr std::size_t npos = std::numeric_limits<std::size_t>::max();
29862986

29872987
/// input adapter
29882988
InputAdapterType ia;

include/nlohmann/detail/input/json_sax.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class json_sax_dom_parser
242242
}
243243
#endif
244244

245-
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
245+
if (JSON_HEDLEY_UNLIKELY(len != std::numeric_limits<std::size_t>::max() && len > ref_stack.back()->max_size()))
246246
{
247247
JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
248248
}
@@ -291,7 +291,7 @@ class json_sax_dom_parser
291291
}
292292
#endif
293293

294-
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
294+
if (JSON_HEDLEY_UNLIKELY(len != std::numeric_limits<std::size_t>::max() && len > ref_stack.back()->max_size()))
295295
{
296296
JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
297297
}
@@ -559,7 +559,7 @@ class json_sax_dom_callback_parser
559559
#endif
560560

561561
// check object limit
562-
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
562+
if (JSON_HEDLEY_UNLIKELY(len != std::numeric_limits<std::size_t>::max() && len > ref_stack.back()->max_size()))
563563
{
564564
JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
565565
}
@@ -657,7 +657,7 @@ class json_sax_dom_callback_parser
657657
#endif
658658

659659
// check array limit
660-
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
660+
if (JSON_HEDLEY_UNLIKELY(len != std::numeric_limits<std::size_t>::max() && len > ref_stack.back()->max_size()))
661661
{
662662
JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
663663
}
@@ -946,7 +946,7 @@ class json_sax_acceptor
946946
return true;
947947
}
948948

949-
bool start_object(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
949+
bool start_object(std::size_t /*unused*/ = std::numeric_limits<std::size_t>::max())
950950
{
951951
return true;
952952
}
@@ -961,7 +961,7 @@ class json_sax_acceptor
961961
return true;
962962
}
963963

964-
bool start_array(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
964+
bool start_array(std::size_t /*unused*/ = std::numeric_limits<std::size_t>::max())
965965
{
966966
return true;
967967
}

0 commit comments

Comments
 (0)