Open
Description
The RE2::Set
class allows to match a string against several regular expressions in a single pass, seemingly more efficiently than piping all the regexes together :
https://github.com/google/re2/blob/f2cc1aeb5de463c45d020c446cbcb028385b49f3/re2/set.h#L21-L23
It could be exposed to JavaScript code by :
- accepting an
Array
of patterns as the first parameter of the constructor ; - adding functionality to the matching methods to use
RE2::Set
to identify which one of the patterns matches (it doesn't seem to go further than that), and then use regularRE2
s corresponding to the identified patterns to get more information ; - in
.exec()
, returning the index of the pattern which matched and/or the pattern itself as properties of the returned array (maybe withSymbol
keys, to eliminate the risks of name collisions with future properties that could be defined by the ECMAScript spec) ; - returning the piped patterns in the
source
property, for compatibility ; - exposing a new
sources
property (or a property with aSymbol
key) containing the individual patterns ; - either returning an array of the translated patterns in the
internalSource
property, or applying the same process as for thesource
property.
Use cases could be optimizing anything that boils down to this kind of code :
let match;
if ((match = re1.exec(str)) !== null) {
// …
} else if ((match = re2.exec(str)) !== null) {
// …
} else if ((match = re3.exec(str)) !== null) {
// …
} /* potentially lots of other cases */ else {
// …
}
For example, a HTTP router, a lexer …
What do you think about such a feature?