compile-time-regular-expressions
compile-time-regular-expressions copied to clipboard
Get first numbered capture utility function?
trafficstars
It looks like you've added functions for tokenizing / lexing. It might be useful to have a helper function to iterate across captures and/or to keep track of the first capture / index as part of the results.
enum class type {
unknown, identifier, number
};
struct lex_item {
type t;
std::string_view c;
};
std::optional<lex_item> lexer(std::string_view v) noexcept {
if (auto [m,id,num] = ctre::match<"([a-z]+)|([0-9]+)">(v); m) {
if (id) {
return lex_item{type::identifier, id};
} else if (num) {
return lex_item{type::number, num};
}
}
return std::nullopt;
}
EG:
auto result = ctre::starts_with<"([a-z]+)|([0-9]+)">(v);
return lex_item(ctre::first_capture(result).get_id(), ctre::first_capture(result).view());
Will likely improve on anything in a tight loop tokenizing results since it should remove a pretty significant branch.