From 62d94344d79ace2c05585213e5d06531400d90fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johel=20Ernesto=20Guerrero=20Pe=C3=B1a?= Date: Sun, 18 Jun 2023 15:44:02 -0400 Subject: [PATCH] perf(io): refactor to not read line beyond the end of "import" --- source/io.h | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/source/io.h b/source/io.h index 2832ee4b9f..9bb2b31e16 100644 --- a/source/io.h +++ b/source/io.h @@ -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()]); }