-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix iterator_input_adapter consuming input before it should #3442
base: develop
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5533,6 +5533,7 @@ class input_stream_adapter | |
}; | ||
#endif // JSON_NO_IO | ||
|
||
|
||
// General-purpose iterator-based adapter. It might not be as fast as | ||
// theoretically possible for some containers, but it is extremely versatile. | ||
template<typename IteratorType> | ||
|
@@ -5542,30 +5543,46 @@ class iterator_input_adapter | |
using char_type = typename std::iterator_traits<IteratorType>::value_type; | ||
|
||
iterator_input_adapter(IteratorType first, IteratorType last) | ||
: current(std::move(first)), end(std::move(last)) | ||
: current(std::move(first)), end(std::move(last)), current_has_been_consumed(false) | ||
{} | ||
|
||
typename std::char_traits<char_type>::int_type get_character() | ||
{ | ||
if (JSON_HEDLEY_LIKELY(current != end)) | ||
|
||
if (JSON_HEDLEY_LIKELY(current_has_been_consumed)) | ||
{ | ||
auto result = std::char_traits<char_type>::to_int_type(*current); | ||
std::advance(current, 1); | ||
return result; | ||
} | ||
|
||
return std::char_traits<char_type>::eof(); | ||
if (JSON_HEDLEY_LIKELY(current != end)) | ||
{ | ||
current_has_been_consumed = true; | ||
return std::char_traits<char_type>::to_int_type(*current); | ||
} | ||
else | ||
{ | ||
current_has_been_consumed = false; | ||
return std::char_traits<char_type>::eof(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And clang-tidy doesn't like else after return. It wants this instead:
|
||
|
||
} | ||
|
||
private: | ||
IteratorType current; | ||
IteratorType end; | ||
mutable IteratorType current; | ||
const IteratorType end; | ||
mutable bool current_has_been_consumed; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
clang-tidy seems to insist on this: (Didn't know/remember that.) CI should turn green after these two changes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use mutable bool current_has_been_consumed = false; This library has a troubled relationship to braced initializers... :-) |
||
|
||
template<typename BaseInputAdapter, size_t T> | ||
friend struct wide_string_input_helper; | ||
|
||
bool empty() const | ||
{ | ||
if (JSON_HEDLEY_LIKELY(current_has_been_consumed)) | ||
{ | ||
std::advance(current, 1); | ||
current_has_been_consumed = false; | ||
} | ||
|
||
return current == end; | ||
} | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may as well remove the member initializer here, since you've already initialized it in the declaration.
I don't know if clang-tidy, etc. will complain. There're various checks for redundant stuff.
Edit: clang-tidy did in fact complain:
error: member initializer for 'current_has_been_consumed' is redundant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it is better to keep it in the constructor (maybe explicitly delete the default constructor) and remove the initialisation in the declaration?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pick one. I prefer in-class myself, so I'd remove the member initializer as suggested.
The default constructor is implicitly deleted because we have a user-defined constructor, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is actually correct