Combine prime and parse

This commit is contained in:
2025-05-17 13:41:28 -04:00
parent e10fb92801
commit 37c860ce62
2 changed files with 9 additions and 14 deletions

View File

@@ -71,11 +71,6 @@ struct Parser2 {
std::ignore = push({N_WHITESPACE, N_VALUE});
}
void prime(char *buf, int len) {
this->buf = buf;
this->bufEnd = buf + len;
}
enum Status {
// Accept input
S_OK,
@@ -87,7 +82,11 @@ struct Parser2 {
S_OVERFLOW,
};
[[nodiscard]] Status parse() { return keepGoing(this); }
[[nodiscard]] Status parse(char *buf, int len) {
this->buf = buf;
this->bufEnd = buf + len;
return keepGoing(this);
}
Parser2(Parser2 const &) = delete;
Parser2 &operator=(Parser2 const &) = delete;

View File

@@ -542,18 +542,15 @@ TEST_CASE("parser2") {
Parser2 parser(&c, &state);
int i = 0;
for (; i < copy.length() - 1; ++i) {
parser.prime(copy.data() + i, 1);
REQUIRE(parser.parse() == Parser2::S_AGAIN);
REQUIRE(parser.parse(copy.data() + i, 1) == Parser2::S_AGAIN);
}
parser.prime(copy.data() + i, 1);
CHECK(parser.parse() == Parser2::S_OK);
CHECK(parser.parse(copy.data() + i, 1) == Parser2::S_OK);
puts("");
}
{
std::string copy = "{\"x\": [], \"y\": {}}";
Parser2 parser(&c, &state);
parser.prime(copy.data(), copy.length());
CHECK(parser.parse() == Parser2::S_OK);
CHECK(parser.parse(copy.data(), copy.length()) == Parser2::S_OK);
puts("");
}
}
@@ -578,8 +575,7 @@ TEST_CASE("bench2") {
bench.run("parser2", [&]() {
auto copy = json;
Parser2 parser(&c, nullptr);
parser.prime(copy.data(), copy.length());
bench.doNotOptimizeAway(parser.parse());
bench.doNotOptimizeAway(parser.parse(copy.data(), copy.length()));
});
}