@@ -116,15 +116,39 @@ simdjson_really_inline document_stream::iterator::iterator(document_stream& _str
116
116
}
117
117
118
118
simdjson_really_inline simdjson_result<element> document_stream::iterator::operator *() noexcept {
119
- // Once we have yielded any errors, we're finished.
120
- if (stream.error ) { finished = true ; return stream.error ; }
119
+ // Note that in case of error, we do not yet mark
120
+ // the iterator as "finished": this detection is done
121
+ // in the operator++ function since it is possible
122
+ // to call operator++ repeatedly while omitting
123
+ // calls to operator*.
124
+ if (stream.error ) { return stream.error ; }
121
125
return stream.parser ->doc .root ();
122
126
}
123
127
124
128
simdjson_really_inline document_stream::iterator& document_stream::iterator::operator ++() noexcept {
129
+ // If there is an error, then we want the iterator
130
+ // to be finished, no matter what. (E.g., we do not
131
+ // keep generating documents with errors, or go beyond
132
+ // a document with errors.)
133
+ //
134
+ // Users do not have to call "operator*()" when they use operator++,
135
+ // so we need to end the stream in the operator++ function.
136
+ //
137
+ // Note that setting finished = true is essential otherwise
138
+ // we would enter an infinite loop.
139
+ if (stream.error ) { finished = true ; }
140
+ // Note that stream.error() is guarded against error conditions
141
+ // (it will immediately return if stream.error casts to false).
142
+ // In effect, this next function does nothing when (stream.error)
143
+ // is true (hence the risk of an infinite loop).
125
144
stream.next ();
126
145
// If that was the last document, we're finished.
146
+ // It is the only type of error we do not want to appear
147
+ // in operator*.
127
148
if (stream.error == EMPTY) { finished = true ; }
149
+ // If we had any other kind of error (not EMPTY) then we want
150
+ // to pass it along to the operator* and we cannot mark the result
151
+ // as "finished" just yet.
128
152
return *this ;
129
153
}
130
154
@@ -168,6 +192,7 @@ simdjson_really_inline std::string_view document_stream::iterator::source() cons
168
192
169
193
170
194
inline void document_stream::next () noexcept {
195
+ // We always enter at once once in an error condition.
171
196
if (error) { return ; }
172
197
173
198
// Load the next document from the batch
0 commit comments