mirror of
https://github.com/simdjson/simdjson
synced 2026-06-08 17:27:07 +00:00
Documenting how one can check for the end of the document. (#1907)
This commit is contained in:
@@ -1110,6 +1110,8 @@ for (auto val : doc) {
|
||||
std::cout << doc.current_location() << std::endl; // Throws OUT_OF_BOUNDS
|
||||
```
|
||||
|
||||
Conversely, if `doc.current_location()` succeeds, then the document has more content.
|
||||
|
||||
Finally, the `current_location()` method may also be used even when no exceptions/errors
|
||||
are thrown. This can be helpful for users that want to know the current state of iteration during parsing. For example:
|
||||
|
||||
|
||||
@@ -64,6 +64,16 @@ int main(int argc, const char *argv[]) {
|
||||
error = parser.iterate(docdata).get(doc);
|
||||
if(error != simdjson::SUCCESS) { std::cout << error << std::endl; return EXIT_FAILURE; }
|
||||
std::cout << doc;
|
||||
// check if there is more content
|
||||
const char * endofstream;
|
||||
if(doc.current_location().get(endofstream) == simdjson::SUCCESS) {
|
||||
// there is more content !
|
||||
// let us find what it is:
|
||||
size_t len = docdata.data() + docdata.size() - endofstream;
|
||||
std::string_view content{endofstream, len};
|
||||
std::cerr << "\nThere is leftover content: '" << content << "'" << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
simdjson::dom::parser parser;
|
||||
|
||||
Reference in New Issue
Block a user