Documenting how one can check for the end of the document. (#1907)

This commit is contained in:
Daniel Lemire
2022-10-04 20:23:52 -04:00
committed by GitHub
parent 5e6be3ed7a
commit 137cb14bcc
2 changed files with 12 additions and 0 deletions
+2
View File
@@ -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:
+10
View File
@@ -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;