It is inconvenient to be unable to print a padded_string. (#713)

* It is inconvenient to be unable to print a padded_string.

* Allows us to print the padded_string even when it is embedded in result object when exceptions are enabled.
This commit is contained in:
Daniel Lemire 2020-04-14 19:07:32 -04:00 committed by GitHub
parent 334a486737
commit 8539896f3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 2 deletions

View File

@ -1215,7 +1215,16 @@ inline std::ostream& operator<<(std::ostream& out, const simdjson_result<dom::ar
* thrown). * thrown).
*/ */
inline std::ostream& operator<<(std::ostream& out, const simdjson_result<dom::object> &value) noexcept(false) { return out << minify<simdjson_result<dom::object>>(value); } inline std::ostream& operator<<(std::ostream& out, const simdjson_result<dom::object> &value) noexcept(false) { return out << minify<simdjson_result<dom::object>>(value); }
/**
* Send padded_string instance to an output stream.
*
* @param out The output stream.
* @param s The padded_string instance.
* @throw simdjson_error if the result being printed has an error. If there is an error with the
* underlying output stream, that error will be propagated (simdjson_error will not be
* thrown).
*/
inline std::ostream& operator<<(std::ostream& out, simdjson_result<padded_string> &s) noexcept(false) { return out << s.value(); }
#endif #endif
/** The result of a JSON navigation that may fail. */ /** The result of a JSON navigation that may fail. */

View File

@ -7,6 +7,7 @@
#include <cstring> #include <cstring>
#include <memory> #include <memory>
#include <string> #include <string>
#include <ostream>
namespace simdjson { namespace simdjson {
@ -110,6 +111,15 @@ private:
}; // padded_string }; // padded_string
/**
* Send padded_string instance to an output stream.
*
* @param out The output stream.
* @param s The padded_string instance.
* @throw if there is an error with the underlying output stream. simdjson itself will not throw.
*/
inline std::ostream& operator<<(std::ostream& out, const padded_string& s) { return out << s.data(); }
} // namespace simdjson } // namespace simdjson
// This is deliberately outside of simdjson so that people get it without having to use the namespace // This is deliberately outside of simdjson so that people get it without having to use the namespace

View File

@ -1607,7 +1607,7 @@ namespace format_tests {
const string MINIFIED(R"({"foo":1,"bar":[1,2,3],"baz":{"a":1,"b":2,"c":3}})"); const string MINIFIED(R"({"foo":1,"bar":[1,2,3],"baz":{"a":1,"b":2,"c":3}})");
bool assert_minified(ostringstream &actual, const std::string &expected=MINIFIED) { bool assert_minified(ostringstream &actual, const std::string &expected=MINIFIED) {
if (actual.str() != expected) { if (actual.str() != expected) {
cerr << "Failed to correctly minify " << DOCUMENT.data() << endl; cerr << "Failed to correctly minify " << DOCUMENT << endl;
cerr << "Expected: " << expected << endl; cerr << "Expected: " << expected << endl;
cerr << "Actual: " << actual.str() << endl; cerr << "Actual: " << actual.str() << endl;
return false; return false;