Skip to content

Commit cb5b74e

Browse files
committed
Fix iterator_input_adapter for usigned and signed char
1 parent edffad0 commit cb5b74e

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

include/nlohmann/detail/input/input_adapters.hpp

+22-3
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,25 @@ class input_stream_adapter
132132
};
133133
#endif // JSON_NO_IO
134134

135+
// Primary template of json_char_traits calls std char_traits
136+
template<typename T>
137+
struct char_traits : std::char_traits<T>
138+
{};
139+
140+
// Explicitly define char traits for unsigned char since it is not standard
141+
template<>
142+
struct char_traits<unsigned char> : std::char_traits<char>
143+
{
144+
using char_type = signed char;
145+
};
146+
147+
// Explicitly define char traits for signed char since it is not standard
148+
template<>
149+
struct char_traits<signed char> : std::char_traits<char>
150+
{
151+
using char_type = unsigned char;
152+
};
153+
135154
// General-purpose iterator-based adapter. It might not be as fast as
136155
// theoretically possible for some containers, but it is extremely versatile.
137156
template<typename IteratorType>
@@ -144,16 +163,16 @@ class iterator_input_adapter
144163
: current(std::move(first)), end(std::move(last))
145164
{}
146165

147-
typename std::char_traits<char_type>::int_type get_character()
166+
typename char_traits<char_type>::int_type get_character()
148167
{
149168
if (JSON_HEDLEY_LIKELY(current != end))
150169
{
151-
auto result = std::char_traits<char_type>::to_int_type(*current);
170+
auto result = char_traits<char_type>::to_int_type(*current);
152171
std::advance(current, 1);
153172
return result;
154173
}
155174

156-
return std::char_traits<char_type>::eof();
175+
return char_traits<char_type>::eof();
157176
}
158177

159178
private:

single_include/nlohmann/json.hpp

+22-3
Original file line numberDiff line numberDiff line change
@@ -6208,6 +6208,25 @@ class input_stream_adapter
62086208
};
62096209
#endif // JSON_NO_IO
62106210

6211+
// Primary template of json_char_traits calls std char_traits
6212+
template<typename T>
6213+
struct char_traits : std::char_traits<T>
6214+
{};
6215+
6216+
// Explicitly define char traits for unsigned char since it is not standard
6217+
template<>
6218+
struct char_traits<unsigned char> : std::char_traits<char>
6219+
{
6220+
using char_type = signed char;
6221+
};
6222+
6223+
// Explicitly define char traits for signed char since it is not standard
6224+
template<>
6225+
struct char_traits<signed char> : std::char_traits<char>
6226+
{
6227+
using char_type = unsigned char;
6228+
};
6229+
62116230
// General-purpose iterator-based adapter. It might not be as fast as
62126231
// theoretically possible for some containers, but it is extremely versatile.
62136232
template<typename IteratorType>
@@ -6220,16 +6239,16 @@ class iterator_input_adapter
62206239
: current(std::move(first)), end(std::move(last))
62216240
{}
62226241

6223-
typename std::char_traits<char_type>::int_type get_character()
6242+
typename char_traits<char_type>::int_type get_character()
62246243
{
62256244
if (JSON_HEDLEY_LIKELY(current != end))
62266245
{
6227-
auto result = std::char_traits<char_type>::to_int_type(*current);
6246+
auto result = char_traits<char_type>::to_int_type(*current);
62286247
std::advance(current, 1);
62296248
return result;
62306249
}
62316250

6232-
return std::char_traits<char_type>::eof();
6251+
return char_traits<char_type>::eof();
62336252
}
62346253

62356254
private:

0 commit comments

Comments
 (0)