-
-
Notifications
You must be signed in to change notification settings - Fork 60
Open
Labels
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
Arrayof patterns as the first parameter of the constructor ; - adding functionality to the matching methods to use
RE2::Setto identify which one of the patterns matches (it doesn't seem to go further than that), and then use regularRE2s 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 withSymbolkeys, to eliminate the risks of name collisions with future properties that could be defined by the ECMAScript spec) ; - returning the piped patterns in the
sourceproperty, for compatibility ; - exposing a new
sourcesproperty (or a property with aSymbolkey) containing the individual patterns ; - either returning an array of the translated patterns in the
internalSourceproperty, or applying the same process as for thesourceproperty.
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?