Function regex_syntax::hir::lift_common_prefix

source ·
fn lift_common_prefix(hirs: Vec<Hir>) -> Result<Hir, Vec<Hir>>
Expand description

Looks for a common prefix in the list of alternation branches given. If one is found, then an equivalent but (hopefully) simplified Hir is returned. Otherwise, the original given list of branches is returned unmodified.

This is not quite as good as it could be. Right now, it requires that all branches are ‘Concat’ expressions. It also doesn’t do well with literals. For example, given ‘foofoo|foobar’, it will not refactor it to ‘foo(?:foo|bar)’ because literals are flattened into their own special concatenation. (One wonders if perhaps ‘Literal’ should be a single atom instead of a string of bytes because of this. Otherwise, handling the current representation in this routine will be pretty gnarly. Sigh.)