Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions include/wabt/lexer-source-line-finder.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ class LexerSourceLineFinder {
std::unique_ptr<LexerSource> source_;
std::vector<OffsetRange> line_ranges_;
Offset next_line_start_;
bool last_cr_; // Last read character was a '\r' (carriage return).
bool eof_;
};

} // namespace wabt
Expand Down
47 changes: 19 additions & 28 deletions src/lexer-source-line-finder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ namespace wabt {

LexerSourceLineFinder::LexerSourceLineFinder(
std::unique_ptr<LexerSource> source)
: source_(std::move(source)),
next_line_start_(0),
last_cr_(false),
eof_(false) {
: source_(std::move(source)), next_line_start_(0) {
source_->Seek(0);
// Line 0 should not be used; but it makes indexing simpler.
line_ranges_.emplace_back(0, 0);
Expand Down Expand Up @@ -82,42 +79,36 @@ Result LexerSourceLineFinder::GetLineOffsets(int find_line,
return Result::Ok;
}

const size_t kBufferSize = 1 << 16;
std::vector<char> buffer(kBufferSize);

assert(!line_ranges_.empty());
Offset buffer_file_offset = 0;
while (!IsLineCached(find_line) && !eof_) {
CHECK_RESULT(source_->Tell(&buffer_file_offset));
size_t read_size = source_->Fill(buffer.data(), buffer.size());
if (read_size < buffer.size()) {
eof_ = true;
}

for (auto iter = buffer.begin(), end = iter + read_size; iter < end;
++iter) {
if (*iter == '\n') {
// Don't include \n or \r in the line range.
Offset line_offset =
buffer_file_offset + (iter - buffer.begin()) - last_cr_;
assert(!line_ranges_.empty() && next_line_start_ <= source_->size());
const char* data = reinterpret_cast<const char*>(source_->data());
const char* end = data + source_->size();
const char* ptr = data + next_line_start_;

if (ptr < end) {
while (!IsLineCached(find_line) && ptr < end) {
if (*ptr == '\n') {
Offset line_offset = static_cast<Offset>(ptr - data);
if (line_offset > next_line_start_ && ptr[-1] == '\r') {
line_offset--;
}
line_ranges_.emplace_back(next_line_start_, line_offset);
next_line_start_ = line_offset + last_cr_ + 1;
next_line_start_ = static_cast<Offset>(ptr - data) + 1;
}
last_cr_ = *iter == '\r';
ptr++;
}

if (eof_) {
if (ptr == end) {
// Add the final line as an empty range.
Offset end = buffer_file_offset + read_size;
line_ranges_.emplace_back(next_line_start_, end);
Offset line_offset = static_cast<Offset>(ptr - data);
line_ranges_.emplace_back(next_line_start_, line_offset);
}
}

if (IsLineCached(find_line)) {
*out_range = GetCachedLine(find_line);
return Result::Ok;
} else {
assert(eof_);
assert(ptr == end);
return Result::Error;
}
}
Expand Down
Loading