Replace deprecated std::char_traits calls with custom char_traits

This commit is contained in:
Colby Haskell 2023-11-05 12:05:46 -05:00
parent 088115b86e
commit b425bcaae0
4 changed files with 114 additions and 104 deletions

View File

@ -72,7 +72,7 @@ class binary_reader
using binary_t = typename BasicJsonType::binary_t; using binary_t = typename BasicJsonType::binary_t;
using json_sax_t = SAX; using json_sax_t = SAX;
using char_type = typename InputAdapterType::char_type; using char_type = typename InputAdapterType::char_type;
using char_int_type = typename std::char_traits<char_type>::int_type; using char_int_type = typename char_traits<char_type>::int_type;
public: public:
/*! /*!
@ -145,7 +145,7 @@ class binary_reader
get(); get();
} }
if (JSON_HEDLEY_UNLIKELY(current != std::char_traits<char_type>::eof())) if (JSON_HEDLEY_UNLIKELY(current != char_traits<char_type>::eof()))
{ {
return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read, return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read,
exception_message(input_format, concat("expected end of input; last byte: 0x", get_token_string()), "value"), nullptr)); exception_message(input_format, concat("expected end of input; last byte: 0x", get_token_string()), "value"), nullptr));
@ -228,7 +228,7 @@ class binary_reader
exception_message(input_format_t::bson, concat("string length must be at least 1, is ", std::to_string(len)), "string"), nullptr)); exception_message(input_format_t::bson, concat("string length must be at least 1, is ", std::to_string(len)), "string"), nullptr));
} }
return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != std::char_traits<char_type>::eof(); return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != char_traits<char_type>::eof();
} }
/*! /*!
@ -422,7 +422,7 @@ class binary_reader
switch (get_char ? get() : current) switch (get_char ? get() : current)
{ {
// EOF // EOF
case std::char_traits<char_type>::eof(): case char_traits<char_type>::eof():
return unexpect_eof(input_format_t::cbor, "value"); return unexpect_eof(input_format_t::cbor, "value");
// Integer 0x00..0x17 (0..23) // Integer 0x00..0x17 (0..23)
@ -1197,7 +1197,7 @@ class binary_reader
switch (get()) switch (get())
{ {
// EOF // EOF
case std::char_traits<char_type>::eof(): case char_traits<char_type>::eof():
return unexpect_eof(input_format_t::msgpack, "value"); return unexpect_eof(input_format_t::msgpack, "value");
// positive fixint // positive fixint
@ -2299,7 +2299,7 @@ class binary_reader
{ {
switch (prefix) switch (prefix)
{ {
case std::char_traits<char_type>::eof(): // EOF case char_traits<char_type>::eof(): // EOF
return unexpect_eof(input_format, "value"); return unexpect_eof(input_format, "value");
case 'T': // true case 'T': // true
@ -2744,7 +2744,7 @@ class binary_reader
This function provides the interface to the used input adapter. It does This function provides the interface to the used input adapter. It does
not throw in case the input reached EOF, but returns a -'ve valued not throw in case the input reached EOF, but returns a -'ve valued
`std::char_traits<char_type>::eof()` in that case. `char_traits<char_type>::eof()` in that case.
@return character read from the input @return character read from the input
*/ */
@ -2886,7 +2886,7 @@ class binary_reader
JSON_HEDLEY_NON_NULL(3) JSON_HEDLEY_NON_NULL(3)
bool unexpect_eof(const input_format_t format, const char* context) const bool unexpect_eof(const input_format_t format, const char* context) const
{ {
if (JSON_HEDLEY_UNLIKELY(current == std::char_traits<char_type>::eof())) if (JSON_HEDLEY_UNLIKELY(current == char_traits<char_type>::eof()))
{ {
return sax->parse_error(chars_read, "<end of file>", return sax->parse_error(chars_read, "<end of file>",
parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr)); parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
@ -2953,7 +2953,7 @@ class binary_reader
InputAdapterType ia; InputAdapterType ia;
/// the current character /// the current character
char_int_type current = std::char_traits<char_type>::eof(); char_int_type current = char_traits<char_type>::eof();
/// the number of characters read /// the number of characters read
std::size_t chars_read = 0; std::size_t chars_read = 0;

View File

@ -133,49 +133,6 @@ class input_stream_adapter
}; };
#endif // JSON_NO_IO #endif // JSON_NO_IO
// Primary template of json_char_traits calls std char_traits
template<typename T>
struct char_traits : std::char_traits<T>
{};
// Explicitly define char traits for unsigned char since it is not standard
template<>
struct char_traits<unsigned char> : std::char_traits<char>
{
using char_type = unsigned char;
using int_type = uint64_t;
// Redefine to_int_type function
static int_type to_int_type(char_type c)
{
return static_cast<int_type>(c);
}
static int_type eof()
{
return static_cast<int_type>(EOF);
}
};
// Explicitly define char traits for signed char since it is not standard
template<>
struct char_traits<signed char> : std::char_traits<char>
{
using char_type = signed char;
using int_type = uint64_t;
// Redefine to_int_type function
static int_type to_int_type(char_type c)
{
return static_cast<int_type>(c);
}
static int_type eof()
{
return static_cast<int_type>(EOF);
}
};
// General-purpose iterator-based adapter. It might not be as fast as // General-purpose iterator-based adapter. It might not be as fast as
// theoretically possible for some containers, but it is extremely versatile. // theoretically possible for some containers, but it is extremely versatile.
template<typename IteratorType> template<typename IteratorType>

View File

@ -12,6 +12,7 @@
#include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type #include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
#include <utility> // declval #include <utility> // declval
#include <tuple> // tuple #include <tuple> // tuple
#include <string> // char_traits
#include <nlohmann/detail/iterators/iterator_traits.hpp> #include <nlohmann/detail/iterators/iterator_traits.hpp>
#include <nlohmann/detail/macro_scope.hpp> #include <nlohmann/detail/macro_scope.hpp>
@ -181,6 +182,53 @@ struct actual_object_comparator
template<typename BasicJsonType> template<typename BasicJsonType>
using actual_object_comparator_t = typename actual_object_comparator<BasicJsonType>::type; using actual_object_comparator_t = typename actual_object_comparator<BasicJsonType>::type;
/////////////////
// char_traits //
/////////////////
// Primary template of char_traits calls std char_traits
template<typename T>
struct char_traits : std::char_traits<T>
{};
// Explicitly define char traits for unsigned char since it is not standard
template<>
struct char_traits<unsigned char> : std::char_traits<char>
{
using char_type = unsigned char;
using int_type = uint64_t;
// Redefine to_int_type function
static int_type to_int_type(char_type c)
{
return static_cast<int_type>(c);
}
static constexpr int_type eof()
{
return static_cast<int_type>(EOF);
}
};
// Explicitly define char traits for signed char since it is not standard
template<>
struct char_traits<signed char> : std::char_traits<char>
{
using char_type = signed char;
using int_type = uint64_t;
// Redefine to_int_type function
static int_type to_int_type(char_type c)
{
return static_cast<int_type>(c);
}
static constexpr int_type eof()
{
return static_cast<int_type>(EOF);
}
};
/////////////////// ///////////////////
// is_ functions // // is_ functions //
/////////////////// ///////////////////

View File

@ -3242,6 +3242,7 @@ NLOHMANN_JSON_NAMESPACE_END
#include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type #include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
#include <utility> // declval #include <utility> // declval
#include <tuple> // tuple #include <tuple> // tuple
#include <string> // char_traits
// #include <nlohmann/detail/iterators/iterator_traits.hpp> // #include <nlohmann/detail/iterators/iterator_traits.hpp>
// __ _____ _____ _____ // __ _____ _____ _____
@ -3594,6 +3595,53 @@ struct actual_object_comparator
template<typename BasicJsonType> template<typename BasicJsonType>
using actual_object_comparator_t = typename actual_object_comparator<BasicJsonType>::type; using actual_object_comparator_t = typename actual_object_comparator<BasicJsonType>::type;
/////////////////
// char_traits //
/////////////////
// Primary template of char_traits calls std char_traits
template<typename T>
struct char_traits : std::char_traits<T>
{};
// Explicitly define char traits for unsigned char since it is not standard
template<>
struct char_traits<unsigned char> : std::char_traits<char>
{
using char_type = unsigned char;
using int_type = uint64_t;
// Redefine to_int_type function
static int_type to_int_type(char_type c)
{
return static_cast<int_type>(c);
}
static constexpr int_type eof()
{
return static_cast<int_type>(EOF);
}
};
// Explicitly define char traits for signed char since it is not standard
template<>
struct char_traits<signed char> : std::char_traits<char>
{
using char_type = signed char;
using int_type = uint64_t;
// Redefine to_int_type function
static int_type to_int_type(char_type c)
{
return static_cast<int_type>(c);
}
static constexpr int_type eof()
{
return static_cast<int_type>(EOF);
}
};
/////////////////// ///////////////////
// is_ functions // // is_ functions //
/////////////////// ///////////////////
@ -6216,49 +6264,6 @@ class input_stream_adapter
}; };
#endif // JSON_NO_IO #endif // JSON_NO_IO
// Primary template of json_char_traits calls std char_traits
template<typename T>
struct char_traits : std::char_traits<T>
{};
// Explicitly define char traits for unsigned char since it is not standard
template<>
struct char_traits<unsigned char> : std::char_traits<char>
{
using char_type = unsigned char;
using int_type = uint64_t;
// Redefine to_int_type function
static int_type to_int_type(char_type c)
{
return static_cast<int_type>(c);
}
static int_type eof()
{
return static_cast<int_type>(EOF);
}
};
// Explicitly define char traits for signed char since it is not standard
template<>
struct char_traits<signed char> : std::char_traits<char>
{
using char_type = signed char;
using int_type = uint64_t;
// Redefine to_int_type function
static int_type to_int_type(char_type c)
{
return static_cast<int_type>(c);
}
static int_type eof()
{
return static_cast<int_type>(EOF);
}
};
// General-purpose iterator-based adapter. It might not be as fast as // General-purpose iterator-based adapter. It might not be as fast as
// theoretically possible for some containers, but it is extremely versatile. // theoretically possible for some containers, but it is extremely versatile.
template<typename IteratorType> template<typename IteratorType>
@ -9200,7 +9205,7 @@ class binary_reader
using binary_t = typename BasicJsonType::binary_t; using binary_t = typename BasicJsonType::binary_t;
using json_sax_t = SAX; using json_sax_t = SAX;
using char_type = typename InputAdapterType::char_type; using char_type = typename InputAdapterType::char_type;
using char_int_type = typename std::char_traits<char_type>::int_type; using char_int_type = typename char_traits<char_type>::int_type;
public: public:
/*! /*!
@ -9273,7 +9278,7 @@ class binary_reader
get(); get();
} }
if (JSON_HEDLEY_UNLIKELY(current != std::char_traits<char_type>::eof())) if (JSON_HEDLEY_UNLIKELY(current != char_traits<char_type>::eof()))
{ {
return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read, return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read,
exception_message(input_format, concat("expected end of input; last byte: 0x", get_token_string()), "value"), nullptr)); exception_message(input_format, concat("expected end of input; last byte: 0x", get_token_string()), "value"), nullptr));
@ -9356,7 +9361,7 @@ class binary_reader
exception_message(input_format_t::bson, concat("string length must be at least 1, is ", std::to_string(len)), "string"), nullptr)); exception_message(input_format_t::bson, concat("string length must be at least 1, is ", std::to_string(len)), "string"), nullptr));
} }
return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != std::char_traits<char_type>::eof(); return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != char_traits<char_type>::eof();
} }
/*! /*!
@ -9550,7 +9555,7 @@ class binary_reader
switch (get_char ? get() : current) switch (get_char ? get() : current)
{ {
// EOF // EOF
case std::char_traits<char_type>::eof(): case char_traits<char_type>::eof():
return unexpect_eof(input_format_t::cbor, "value"); return unexpect_eof(input_format_t::cbor, "value");
// Integer 0x00..0x17 (0..23) // Integer 0x00..0x17 (0..23)
@ -10325,7 +10330,7 @@ class binary_reader
switch (get()) switch (get())
{ {
// EOF // EOF
case std::char_traits<char_type>::eof(): case char_traits<char_type>::eof():
return unexpect_eof(input_format_t::msgpack, "value"); return unexpect_eof(input_format_t::msgpack, "value");
// positive fixint // positive fixint
@ -11427,7 +11432,7 @@ class binary_reader
{ {
switch (prefix) switch (prefix)
{ {
case std::char_traits<char_type>::eof(): // EOF case char_traits<char_type>::eof(): // EOF
return unexpect_eof(input_format, "value"); return unexpect_eof(input_format, "value");
case 'T': // true case 'T': // true
@ -11872,7 +11877,7 @@ class binary_reader
This function provides the interface to the used input adapter. It does This function provides the interface to the used input adapter. It does
not throw in case the input reached EOF, but returns a -'ve valued not throw in case the input reached EOF, but returns a -'ve valued
`std::char_traits<char_type>::eof()` in that case. `char_traits<char_type>::eof()` in that case.
@return character read from the input @return character read from the input
*/ */
@ -12014,7 +12019,7 @@ class binary_reader
JSON_HEDLEY_NON_NULL(3) JSON_HEDLEY_NON_NULL(3)
bool unexpect_eof(const input_format_t format, const char* context) const bool unexpect_eof(const input_format_t format, const char* context) const
{ {
if (JSON_HEDLEY_UNLIKELY(current == std::char_traits<char_type>::eof())) if (JSON_HEDLEY_UNLIKELY(current == char_traits<char_type>::eof()))
{ {
return sax->parse_error(chars_read, "<end of file>", return sax->parse_error(chars_read, "<end of file>",
parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr)); parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
@ -12081,7 +12086,7 @@ class binary_reader
InputAdapterType ia; InputAdapterType ia;
/// the current character /// the current character
char_int_type current = std::char_traits<char_type>::eof(); char_int_type current = char_traits<char_type>::eof();
/// the number of characters read /// the number of characters read
std::size_t chars_read = 0; std::size_t chars_read = 0;