Commit fafa745e authored by Mathieu STEFANI's avatar Mathieu STEFANI

Moved Reverter inside Cursor

parent b0aba763
...@@ -89,7 +89,7 @@ namespace Private { ...@@ -89,7 +89,7 @@ namespace Private {
Parser::State Parser::State
Parser::RequestLineStep::apply(Cursor& cursor) { Parser::RequestLineStep::apply(Cursor& cursor) {
Reverter reverter(cursor); Cursor::Revert revert(cursor);
auto tryMatch = [&](const char* const str) { auto tryMatch = [&](const char* const str) {
const size_t len = std::strlen(str); const size_t len = std::strlen(str);
...@@ -165,17 +165,17 @@ namespace Private { ...@@ -165,17 +165,17 @@ namespace Private {
if (!cursor.advance(2)) return State::Again; if (!cursor.advance(2)) return State::Again;
reverter.clear(); revert.ignore();
return State::Next; return State::Next;
} }
Parser::State Parser::State
Parser::HeadersStep::apply(Cursor& cursor) { Parser::HeadersStep::apply(Cursor& cursor) {
Reverter reverter(cursor); Cursor::Revert revert(cursor);
while (!cursor.eol()) { while (!cursor.eol()) {
Reverter headerReverter(cursor); Cursor::Revert headerRevert(cursor);
// Read the header name // Read the header name
size_t start = cursor; size_t start = cursor;
...@@ -205,7 +205,7 @@ namespace Private { ...@@ -205,7 +205,7 @@ namespace Private {
// CRLF // CRLF
if (!cursor.advance(2)) return State::Again; if (!cursor.advance(2)) return State::Again;
headerReverter.clear(); headerRevert.ignore();
} }
return Parser::State::Next; return Parser::State::Next;
......
...@@ -138,6 +138,32 @@ namespace Private { ...@@ -138,6 +138,32 @@ namespace Private {
}; };
struct Cursor { struct Cursor {
struct Revert {
Revert(Cursor& cursor)
: cursor(cursor)
, pos(cursor.value)
, active(true)
{ }
void revert() {
cursor.value = pos;
}
void ignore() {
active = false;
}
~Revert() {
if (active) cursor.value = pos;
}
Cursor& cursor;
size_t pos;
bool active;
};
static constexpr int Eof = -1; static constexpr int Eof = -1;
Cursor(const Buffer &buffer, size_t initialPos = 0) Cursor(const Buffer &buffer, size_t initialPos = 0)
...@@ -162,31 +188,6 @@ namespace Private { ...@@ -162,31 +188,6 @@ namespace Private {
size_t value; size_t value;
}; };
struct Reverter {
Reverter(Cursor& cursor)
: cursor(cursor)
, pos(cursor.value)
, active(true)
{ }
void revert() {
cursor.value = pos;
}
void clear() {
active = false;
}
~Reverter() {
if (active) cursor.value = pos;
}
Cursor& cursor;
size_t pos;
bool active;
};
enum class State { Again, Next, Done }; enum class State { Again, Next, Done };
struct Step { struct Step {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment