facebook / hhvm

A virtual machine for executing programs written in Hack.
https://hhvm.com
Other
18.13k stars 2.99k forks source link

add support for PCRE Marks #3886

Open staabm opened 9 years ago

staabm commented 9 years ago

PCRE Marks are supported by php since 5.6 http://grokbase.com/t/php/php-internals/142wrqvs7p/add-support-for-pcre-marks

see also https://github.com/nikic/FastRoute/pull/22

staabm commented 8 years ago

issue can be reproduced by running the unit tests in https://github.com/nikic/FastRoute/blob/master/test/Dispatcher/MarkBasedTest.php

nikic commented 8 years ago

Isolated repro: https://3v4l.org/bu2jg

fredemmott commented 8 years ago

Thanks - just out of curiosity as a user, are there advantages of MARK over named captures? https://3v4l.org/gfOnZ

nikic commented 8 years ago

@fredemmott The main advantage is that there's only one "capture". So if you want to determine which of a number of paths a regex has taken, instead of

(?<a>...) | (?<b>...) | (?<c>...) | (?<d>...) | ...

which will create one capturing group for each path, with exactly one of them having a non-empty value (assuming you don't have any subexpressions matching an empty string). With marks, that would be

(*MARK:A)... | (*MARK:B) ... | (*MARK:C) ... | (*MARK:D) ... | ...

and you end up with a single pseudo-capture telling you which path was taken. This avoid creating a huge capture array and you also don't need to scan throw it to find what you want.

fredemmott commented 8 years ago

Thanks, TIL :)