Skip to content

Commit e48e0c1

Browse files
committed
perf: use std::vector and std::find_if over std::regex
1 parent 91a4196 commit e48e0c1

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

source/lex.h

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -933,17 +933,18 @@ auto lex_line(
933933
//G any Cpp1-and-Cpp2 keyword
934934
//G one of: 'import' 'module' 'export' 'is' 'as'
935935
//G
936-
auto do_is_keyword = [&](std::regex const& r) {
937-
std::cmatch m;
938-
if (std::regex_search(&line[i], m, r)) {
939-
assert (m.position(0) == 0);
936+
auto do_is_keyword = [&](std::vector<std::string_view> const& r) {
937+
auto m = std::find_if(r.begin(), r.end(), [&](std::string_view s) {
938+
return std::string_view(line).substr(unsafe_narrow<std::size_t>(i)).starts_with(s);
939+
});
940+
if (m != r.end()) {
940941
// If we matched and what's next is EOL or a non-identifier char, we matched!
941942
if (
942-
i+m[0].length() == std::ssize(line) // EOL
943-
|| !is_identifier_continue(line[i+m[0].length()]) // non-identifier char
943+
i+std::ssize(*m) == std::ssize(line) // EOL
944+
|| !is_identifier_continue(line[unsafe_narrow<std::size_t>(i)+std::size(*m)]) // non-identifier char
944945
)
945946
{
946-
return static_cast<int>(m[0].length());
947+
return static_cast<int>(std::ssize(*m));
947948
}
948949
}
949950
return 0;
@@ -955,46 +956,46 @@ auto lex_line(
955956
// reserve all the ones Cpp1 has both for compatibility and to not give up a keyword
956957
// Some keywords like "delete" and "union" are not in this list because we reject them elsewhere
957958
// Cpp2 also adds a couple, notably "is" and "as"
958-
const auto keys = std::regex(
959-
"^alignas|^alignof|^asm|^as|^auto|"
960-
"^bool|^break|"
961-
"^case|^catch|^char16_t|^char32_t|^char8_t|^char|^co_await|^co_return|"
962-
"^co_yield|^concept|^const_cast|^consteval|^constexpr|^constinit|^const|^continue|"
963-
"^decltype|^default|^double|^do|^dynamic_cast|"
964-
"^else|^enum|^explicit|^export|^extern|"
965-
"^float|^for|^friend|"
966-
"^goto|"
967-
"^if|^import|^inline|^int|^is|"
968-
"^long|"
969-
"^module|^mutable|"
970-
"^namespace|^noexcept|"
971-
"^operator|"
972-
"^private|^protected|^public|"
973-
"^register|^reinterpret_cast|^requires|^return|"
974-
"^short|^signed|^sizeof|^static_assert|^static_cast|^static|^switch|"
975-
"^template|^this|^thread_local|^throws|^throw|^try|^typedef|^typeid|^typename|"
976-
"^unsigned|^using|"
977-
"^virtual|^void|^volatile|"
978-
"^wchar_t|^while"
979-
);
959+
static const auto keys = std::vector<std::string_view>{
960+
"alignas", "alignof", "asm", "as", "auto",
961+
"bool", "break",
962+
"case", "catch", "char16_t", "char32_t", "char8_t", "char", "co_await", "co_return",
963+
"co_yield", "concept", "const_cast", "consteval", "constexpr", "constinit", "const", "continue",
964+
"decltype", "default", "double", "do", "dynamic_cast",
965+
"else", "enum", "explicit", "export", "extern",
966+
"float", "for", "friend",
967+
"goto",
968+
"if", "import", "inline", "int", "is",
969+
"long",
970+
"module", "mutable",
971+
"namespace", "noexcept",
972+
"operator",
973+
"private", "protected", "public",
974+
"register", "reinterpret_cast", "requires", "return",
975+
"short", "signed", "sizeof", "static_assert", "static_cast", "static", "switch",
976+
"template", "this", "thread_local", "throws", "throw", "try", "typedef", "typeid", "typename",
977+
"unsigned", "using",
978+
"virtual", "void", "volatile",
979+
"wchar_t", "while"
980+
};
980981

981982
return do_is_keyword(keys);
982983
};
983984

984985
auto peek_is_cpp2_fundamental_type_keyword = [&]
985986
{
986-
const auto keys = std::regex(
987-
"^i8|^i16|^i32|^i64|^longdouble|^longlong|^u8|^u16|^u32|^u64|^ulong|^ulonglong|^ushort"
988-
);
987+
static const auto keys = std::vector<std::string_view>{
988+
"i8", "i16", "i32", "i64", "longdouble", "longlong", "u8", "u16", "u32", "u64", "ulong", "ulonglong", "ushort"
989+
};
989990

990991
return do_is_keyword(keys);
991992
};
992993

993994
auto peek_is_cpp1_multi_token_fundamental_keyword = [&]
994995
{
995-
const auto multi_keys = std::regex(
996-
"^char16_t|^char32_t|^char8_t|^char|^double|^float|^int|^long|^short|^signed|^unsigned"
997-
);
996+
static const auto multi_keys = std::vector<std::string_view>{
997+
"char16_t", "char32_t", "char8_t", "char", "double", "float", "int", "long", "short", "signed", "unsigned"
998+
};
998999
return do_is_keyword(multi_keys);
9991000
};
10001001

0 commit comments

Comments
 (0)