Skip to content

perf(io): refactor to not read line beyond the end of "import" #519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2023
Merged
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
29 changes: 16 additions & 13 deletions source/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,22 @@ auto is_preprocessor(
auto starts_with_import(std::string const& line)
-> bool
{
auto import_first =
std::find_if_not(
line.data(),
line.data()+line.length(),
[](char c) { return std::isspace(c); }
);
auto import_last =
std::find_if(
import_first,
line.data()+line.length(),
[](char c) { return std::isspace(c); }
);
return std::string_view{import_first, import_last} == "import";
auto i = 0;

// find first non-whitespace character
if (!move_next(line, i, isspace)) {
return false;
}

static constexpr auto import_keyword = std::string_view{"import"};

// the first token must begin with 'import'
if (!std::string_view(line).substr(i).starts_with(import_keyword)) {
return false;
}

// and not be immediately followed by an _identifier-continue_
return !is_identifier_continue(line[i + import_keyword.size()]);
}


Expand Down