mirror of
https://github.com/nlohmann/json.git
synced 2026-11-02 19:05:35 +00:00
accept() checks strings without copying them (#5978)
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
1 parent
fb515ddc34
commit
517444b504
5 files changed
+265
-104
No files matched your search
@@ -2982,6 +2982,67 @@ TEST_CASE("accept() agrees with parse() on floats around the overflow limit")
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("accept() agrees with parse() on strings, which it checks without copying them")
|
||||
{
|
||||
// accept() scans strings and object keys with a separate instantiation of
|
||||
// the string scanner that does not copy them into the token buffer; it
|
||||
// must accept and reject exactly what parse() does
|
||||
const std::vector<std::string> pieces =
|
||||
{
|
||||
"", "abc", R"(\"\\\/\b\f\n\r\t)", R"(\u0041\u00e4\u20AC)", "\\ud83d\\ude00", "\\uD83D\\uDE00",
|
||||
"\\ud800", "\\udc00", "\\ud800\\u0041", "\\ud800x", "\\udbff\\udfff", "\\u12", "\\u12g4", "\\x", "\\",
|
||||
"\xc3\xa4", "\xe2\x82\xac", "\xf0\x9f\x98\x80", "\xf4\x8f\xbf\xbf", "\xc0\xaf", "\xed\xa0\x80", "\xf4\x90\x80\x80",
|
||||
"\xff", "\xe2\x82", "\xc3", "\x01", "\x1f", "\x7f", "\n", std::string(1, '\0')
|
||||
};
|
||||
|
||||
for (std::size_t run = 0; run <= 70; ++run)
|
||||
{
|
||||
for (const std::string& piece : pieces)
|
||||
{
|
||||
// a run of ordinary characters puts the piece at every offset of the bulk scanners
|
||||
const std::string str = std::string(run, 'a') + piece;
|
||||
std::string opened = "\"";
|
||||
opened += str;
|
||||
std::string closed = opened;
|
||||
closed += '"';
|
||||
std::vector<std::string> texts;
|
||||
texts.push_back(closed);
|
||||
texts.emplace_back("[");
|
||||
texts.back() += closed;
|
||||
texts.back() += ']';
|
||||
texts.emplace_back("{");
|
||||
texts.back() += closed;
|
||||
texts.back() += ":1}";
|
||||
texts.emplace_back("{\"k\":");
|
||||
texts.back() += closed;
|
||||
texts.back() += ',';
|
||||
texts.back() += closed;
|
||||
texts.back() += ":[";
|
||||
texts.back() += closed;
|
||||
texts.back() += "]}";
|
||||
texts.emplace_back("[");
|
||||
texts.back() += opened;
|
||||
for (const std::string& text : texts)
|
||||
{
|
||||
CAPTURE(text)
|
||||
for (int options = 0; options < 4; ++options)
|
||||
{
|
||||
const bool ignore_comments = (options & 1) != 0;
|
||||
const bool ignore_trailing_commas = (options & 2) != 0;
|
||||
const bool parses = !json::parse(text, nullptr, false, ignore_comments, ignore_trailing_commas).is_discarded();
|
||||
CHECK(json::accept(text, ignore_comments, ignore_trailing_commas) == parses);
|
||||
|
||||
std::istringstream stream(text);
|
||||
CHECK(json::accept(stream, ignore_comments, ignore_trailing_commas) == parses);
|
||||
|
||||
const std::list<char> chars(text.begin(), text.end());
|
||||
CHECK(json::accept(chars.begin(), chars.end(), ignore_comments, ignore_trailing_commas) == parses);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
// a DOM parser that reports parse errors without throwing, as in the sax_no_exception example
|
||||
|
||||
Reference in new issue
Block a user