Fix char_int_type in lexer

This commit is contained in:
Colby Haskell 2023-11-05 12:34:07 -05:00
parent b425bcaae0
commit 6c83919adb
2 changed files with 29 additions and 26 deletions

View File

@ -21,6 +21,7 @@
#include <nlohmann/detail/input/input_adapters.hpp> #include <nlohmann/detail/input/input_adapters.hpp>
#include <nlohmann/detail/input/position_t.hpp> #include <nlohmann/detail/input/position_t.hpp>
#include <nlohmann/detail/macro_scope.hpp> #include <nlohmann/detail/macro_scope.hpp>
#include <nlohmann/detail/meta/type_traits.hpp>
NLOHMANN_JSON_NAMESPACE_BEGIN NLOHMANN_JSON_NAMESPACE_BEGIN
namespace detail namespace detail
@ -115,7 +116,7 @@ class lexer : public lexer_base<BasicJsonType>
using number_float_t = typename BasicJsonType::number_float_t; using number_float_t = typename BasicJsonType::number_float_t;
using string_t = typename BasicJsonType::string_t; using string_t = typename BasicJsonType::string_t;
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:
using token_type = typename lexer_base<BasicJsonType>::token_type; using token_type = typename lexer_base<BasicJsonType>::token_type;
@ -265,7 +266,7 @@ class lexer : public lexer_base<BasicJsonType>
switch (get()) switch (get())
{ {
// end of file while parsing string // end of file while parsing string
case std::char_traits<char_type>::eof(): case char_traits<char_type>::eof():
{ {
error_message = "invalid string: missing closing quote"; error_message = "invalid string: missing closing quote";
return token_type::parse_error; return token_type::parse_error;
@ -854,7 +855,7 @@ class lexer : public lexer_base<BasicJsonType>
{ {
case '\n': case '\n':
case '\r': case '\r':
case std::char_traits<char_type>::eof(): case char_traits<char_type>::eof():
case '\0': case '\0':
return true; return true;
@ -871,7 +872,7 @@ class lexer : public lexer_base<BasicJsonType>
{ {
switch (get()) switch (get())
{ {
case std::char_traits<char_type>::eof(): case char_traits<char_type>::eof():
case '\0': case '\0':
{ {
error_message = "invalid comment; missing closing '*/'"; error_message = "invalid comment; missing closing '*/'";
@ -1300,10 +1301,10 @@ scan_number_done:
token_type scan_literal(const char_type* literal_text, const std::size_t length, token_type scan_literal(const char_type* literal_text, const std::size_t length,
token_type return_type) token_type return_type)
{ {
JSON_ASSERT(std::char_traits<char_type>::to_char_type(current) == literal_text[0]); JSON_ASSERT(char_traits<char_type>::to_char_type(current) == literal_text[0]);
for (std::size_t i = 1; i < length; ++i) for (std::size_t i = 1; i < length; ++i)
{ {
if (JSON_HEDLEY_UNLIKELY(std::char_traits<char_type>::to_char_type(get()) != literal_text[i])) if (JSON_HEDLEY_UNLIKELY(char_traits<char_type>::to_char_type(get()) != literal_text[i]))
{ {
error_message = "invalid literal"; error_message = "invalid literal";
return token_type::parse_error; return token_type::parse_error;
@ -1321,7 +1322,7 @@ scan_number_done:
{ {
token_buffer.clear(); token_buffer.clear();
token_string.clear(); token_string.clear();
token_string.push_back(std::char_traits<char_type>::to_char_type(current)); token_string.push_back(char_traits<char_type>::to_char_type(current));
} }
/* /*
@ -1329,7 +1330,7 @@ scan_number_done:
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 not throw in case the input reached EOF, but returns a
`std::char_traits<char>::eof()` in that case. Stores the scanned characters `char_traits<char>::eof()` in that case. Stores the scanned characters
for use in error messages. for use in error messages.
@return character read from the input @return character read from the input
@ -1349,9 +1350,9 @@ scan_number_done:
current = ia.get_character(); current = ia.get_character();
} }
if (JSON_HEDLEY_LIKELY(current != std::char_traits<char_type>::eof())) if (JSON_HEDLEY_LIKELY(current != char_traits<char_type>::eof()))
{ {
token_string.push_back(std::char_traits<char_type>::to_char_type(current)); token_string.push_back(char_traits<char_type>::to_char_type(current));
} }
if (current == '\n') if (current == '\n')
@ -1390,7 +1391,7 @@ scan_number_done:
--position.chars_read_current_line; --position.chars_read_current_line;
} }
if (JSON_HEDLEY_LIKELY(current != std::char_traits<char_type>::eof())) if (JSON_HEDLEY_LIKELY(current != char_traits<char_type>::eof()))
{ {
JSON_ASSERT(!token_string.empty()); JSON_ASSERT(!token_string.empty());
token_string.pop_back(); token_string.pop_back();
@ -1584,7 +1585,7 @@ scan_number_done:
// end of input (the null byte is needed when parsing from // end of input (the null byte is needed when parsing from
// string literals) // string literals)
case '\0': case '\0':
case std::char_traits<char_type>::eof(): case char_traits<char_type>::eof():
return token_type::end_of_input; return token_type::end_of_input;
// error // error
@ -1602,7 +1603,7 @@ scan_number_done:
const bool ignore_comments = false; const bool ignore_comments = false;
/// 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();
/// whether the next get() call should just return current /// whether the next get() call should just return current
bool next_unget = false; bool next_unget = false;

View File

@ -7381,6 +7381,8 @@ NLOHMANN_JSON_NAMESPACE_END
// #include <nlohmann/detail/macro_scope.hpp> // #include <nlohmann/detail/macro_scope.hpp>
// #include <nlohmann/detail/meta/type_traits.hpp>
NLOHMANN_JSON_NAMESPACE_BEGIN NLOHMANN_JSON_NAMESPACE_BEGIN
namespace detail namespace detail
@ -7475,7 +7477,7 @@ class lexer : public lexer_base<BasicJsonType>
using number_float_t = typename BasicJsonType::number_float_t; using number_float_t = typename BasicJsonType::number_float_t;
using string_t = typename BasicJsonType::string_t; using string_t = typename BasicJsonType::string_t;
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:
using token_type = typename lexer_base<BasicJsonType>::token_type; using token_type = typename lexer_base<BasicJsonType>::token_type;
@ -7625,7 +7627,7 @@ class lexer : public lexer_base<BasicJsonType>
switch (get()) switch (get())
{ {
// end of file while parsing string // end of file while parsing string
case std::char_traits<char_type>::eof(): case char_traits<char_type>::eof():
{ {
error_message = "invalid string: missing closing quote"; error_message = "invalid string: missing closing quote";
return token_type::parse_error; return token_type::parse_error;
@ -8214,7 +8216,7 @@ class lexer : public lexer_base<BasicJsonType>
{ {
case '\n': case '\n':
case '\r': case '\r':
case std::char_traits<char_type>::eof(): case char_traits<char_type>::eof():
case '\0': case '\0':
return true; return true;
@ -8231,7 +8233,7 @@ class lexer : public lexer_base<BasicJsonType>
{ {
switch (get()) switch (get())
{ {
case std::char_traits<char_type>::eof(): case char_traits<char_type>::eof():
case '\0': case '\0':
{ {
error_message = "invalid comment; missing closing '*/'"; error_message = "invalid comment; missing closing '*/'";
@ -8660,10 +8662,10 @@ scan_number_done:
token_type scan_literal(const char_type* literal_text, const std::size_t length, token_type scan_literal(const char_type* literal_text, const std::size_t length,
token_type return_type) token_type return_type)
{ {
JSON_ASSERT(std::char_traits<char_type>::to_char_type(current) == literal_text[0]); JSON_ASSERT(char_traits<char_type>::to_char_type(current) == literal_text[0]);
for (std::size_t i = 1; i < length; ++i) for (std::size_t i = 1; i < length; ++i)
{ {
if (JSON_HEDLEY_UNLIKELY(std::char_traits<char_type>::to_char_type(get()) != literal_text[i])) if (JSON_HEDLEY_UNLIKELY(char_traits<char_type>::to_char_type(get()) != literal_text[i]))
{ {
error_message = "invalid literal"; error_message = "invalid literal";
return token_type::parse_error; return token_type::parse_error;
@ -8681,7 +8683,7 @@ scan_number_done:
{ {
token_buffer.clear(); token_buffer.clear();
token_string.clear(); token_string.clear();
token_string.push_back(std::char_traits<char_type>::to_char_type(current)); token_string.push_back(char_traits<char_type>::to_char_type(current));
} }
/* /*
@ -8689,7 +8691,7 @@ scan_number_done:
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 not throw in case the input reached EOF, but returns a
`std::char_traits<char>::eof()` in that case. Stores the scanned characters `char_traits<char>::eof()` in that case. Stores the scanned characters
for use in error messages. for use in error messages.
@return character read from the input @return character read from the input
@ -8709,9 +8711,9 @@ scan_number_done:
current = ia.get_character(); current = ia.get_character();
} }
if (JSON_HEDLEY_LIKELY(current != std::char_traits<char_type>::eof())) if (JSON_HEDLEY_LIKELY(current != char_traits<char_type>::eof()))
{ {
token_string.push_back(std::char_traits<char_type>::to_char_type(current)); token_string.push_back(char_traits<char_type>::to_char_type(current));
} }
if (current == '\n') if (current == '\n')
@ -8750,7 +8752,7 @@ scan_number_done:
--position.chars_read_current_line; --position.chars_read_current_line;
} }
if (JSON_HEDLEY_LIKELY(current != std::char_traits<char_type>::eof())) if (JSON_HEDLEY_LIKELY(current != char_traits<char_type>::eof()))
{ {
JSON_ASSERT(!token_string.empty()); JSON_ASSERT(!token_string.empty());
token_string.pop_back(); token_string.pop_back();
@ -8944,7 +8946,7 @@ scan_number_done:
// end of input (the null byte is needed when parsing from // end of input (the null byte is needed when parsing from
// string literals) // string literals)
case '\0': case '\0':
case std::char_traits<char_type>::eof(): case char_traits<char_type>::eof():
return token_type::end_of_input; return token_type::end_of_input;
// error // error
@ -8962,7 +8964,7 @@ scan_number_done:
const bool ignore_comments = false; const bool ignore_comments = false;
/// 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();
/// whether the next get() call should just return current /// whether the next get() call should just return current
bool next_unget = false; bool next_unget = false;