Skip to content

Commit 34b2af3

Browse files
authored
perf(io): refactor to not read line beyond the end of "import" (#519)
1 parent 5a57f5e commit 34b2af3

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

source/io.h

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,22 @@ auto is_preprocessor(
114114
auto starts_with_import(std::string const& line)
115115
-> bool
116116
{
117-
auto import_first =
118-
std::find_if_not(
119-
line.data(),
120-
line.data()+line.length(),
121-
[](char c) { return std::isspace(c); }
122-
);
123-
auto import_last =
124-
std::find_if(
125-
import_first,
126-
line.data()+line.length(),
127-
[](char c) { return std::isspace(c); }
128-
);
129-
return std::string_view{import_first, import_last} == "import";
117+
auto i = 0;
118+
119+
// find first non-whitespace character
120+
if (!move_next(line, i, isspace)) {
121+
return false;
122+
}
123+
124+
static constexpr auto import_keyword = std::string_view{"import"};
125+
126+
// the first token must begin with 'import'
127+
if (!std::string_view(line).substr(i).starts_with(import_keyword)) {
128+
return false;
129+
}
130+
131+
// and not be immediately followed by an _identifier-continue_
132+
return !is_identifier_continue(line[i + import_keyword.size()]);
130133
}
131134

132135

0 commit comments

Comments
 (0)