Skip to content

Exposing RE2::Set #43

Open
Open
@Exter-N

Description

@Exter-N

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 regular RE2s 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 with Symbol 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 a Symbol key) containing the individual patterns ;
  • either returning an array of the translated patterns in the internalSource property, or applying the same process as for the source 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?

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions