2 ms·
The regex macro has two primary benefits: 1. You cannot compile your program with an invalid regex. 2. The regex runs in a specialized VM corresponding to the
by burntsushi 12y ago
The regex macro has two primary benefits:
1. You cannot compile your program with an invalid regex.
2. The regex runs in a specialized VM corresponding to the specific bytecodes for a regex. This results in an across-the-board performance improvement, mostly because there are some places that can now use stack allocation instead of heap allocation. (But it's still a full NFA simulation, so it isn't going to, say, beat RE2/C++ quite yet.)
You can take a look at the syntax extension here: https://github.com/rust-lang/rust/blob/master/src/libregex_macros/lib.rs https://github.com/rust-lang/rust/blob/master/src/libregex_m...
Basically, if CTFE allows you to write functions that produce AST transforms, then the `regex!` macro should be convertible to that form. Most of the syntax extension is a quasiquoted copy of the general VM: https://github.com/rust-lang/rust/blob/master/src/libregex/vm.rs https://github.com/rust-lang/rust/blob/master/src/libregex/v... --- The actual syntax extension bits are pretty minimal.
(There are quite a few tricks employed to get native and dynamic regexes to have the same API. It's the reason why the main `Regex` type is actually an `enum`! https://github.com/rust-lang/rust/blob/master/src/libregex/re.rs#L105 https://github.com/rust-lang/rust/blob/master/src/libregex/r...)
Full disclaimer: the `regex!` macro does have a couple downsides. #1, it's a syntax extension, so it has a dynamic dependency on `libsyntax`. #2, it ends up producing a lot of code and bloating your binary if you use it a lot (you might start noticing at after the first few dozen, and you might start cursing it after the first few hundred).