Remove the suboptimal .canPeek() and .peek() methods

This commit is contained in:
Rangi42
2024-03-28 14:49:36 -04:00
parent 83c0634f15
commit 6f74e4fb9c
2 changed files with 8 additions and 15 deletions

View File

@@ -692,25 +692,25 @@ int LexerState::peek(uint8_t distance) {
assert(distance == 0 || distance == 1);
for (Expansion &exp : expansions) {
// An expansion that has reached its end will have `exp->offset` == `exp->size()`,
// An expansion that has reached its end will have `exp.offset` == `exp.size()`,
// and `.peek()` will continue with its parent
assert(exp.offset <= exp.size());
if (exp.canPeek(distance))
return exp.peek(distance);
if (exp.offset + distance < exp.size())
return (uint8_t)(*exp.contents)[exp.offset + distance];
distance -= exp.size() - exp.offset;
}
if (auto *view = std::get_if<ViewedContent>(&content); view) {
if (view->canPeek(distance))
return view->peek(distance);
if (view->offset + distance < view->span.size)
return (uint8_t)view->span.ptr[view->offset + distance];
} else {
assert(std::holds_alternative<BufferedContent>(content));
auto &cbuf = std::get<BufferedContent>(content);
assert(distance < LEXER_BUF_SIZE);
if (!cbuf.canPeek(distance))
if (cbuf.size <= distance)
cbuf.refill(); // Buffer isn't full enough, read some chars in
if (cbuf.canPeek(distance))
return cbuf.peek(distance);
if (cbuf.size > distance)
return (uint8_t)cbuf.buf[(cbuf.offset + distance) % LEXER_BUF_SIZE];
}
// If there aren't enough chars, give up
return EOF;