Skip to content

Commit ba1cab6

Browse files
JohelEGPhsutter
andauthored
perf(io): excise last use of std::regex (#515)
* perf(io): excise last use of `std::regex` * Change `&*x.end()` to `x.data()+x.length()` to avoid UB Signed-off-by: Herb Sutter <[email protected]> --------- Signed-off-by: Herb Sutter <[email protected]> Co-authored-by: Herb Sutter <[email protected]>
1 parent 379ae03 commit ba1cab6

File tree

1 file changed

+3
-11
lines changed

1 file changed

+3
-11
lines changed

source/io.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <fstream>
2323
#include <ostream>
2424
#include <iterator>
25-
#include <regex>
2625
#include <cctype>
2726

2827

@@ -115,16 +114,9 @@ auto is_preprocessor(
115114
auto starts_with_import(std::string const& line)
116115
-> bool
117116
{
118-
const auto ws = std::regex("\\s+"); // whitespace
119-
auto i = 1;
120-
for (std::sregex_token_iterator iter(std::begin(line), std::end(line), ws, -1);
121-
iter != std::sregex_token_iterator();
122-
++iter, ++i) {
123-
if (iter->length() > 0) {
124-
return *iter == "import";
125-
}
126-
}
127-
return false;
117+
auto import_first = std::find_if_not(line.data(), line.data()+line.length(), [](char c) { return std::isspace(c); });
118+
auto import_last = std::find_if(import_first, line.data()+line.length(), [](char c) { return std::isspace(c); });
119+
return std::string_view{import_first, import_last} == "import";
128120
}
129121

130122

0 commit comments

Comments
 (0)