Merge pull request #5586 from kinglozzer/tokenisedregex-perf

Improve performance of TokenisedRegularExpression
This commit is contained in:
Daniel Hensby 2016-05-24 17:44:42 +01:00
commit 128eaaeefc

View File

@ -13,32 +13,40 @@ class TokenisedRegularExpression {
*/ */
protected $expression; protected $expression;
/**
* The first expression to match
*/
protected $firstMatch;
public function __construct($expression) { public function __construct($expression) {
$this->expression = $expression; $this->expression = $expression;
$this->firstMatch = is_array($expression[0]) ? $expression[0][0] : $expression[0];
} }
public function findAll($tokens) { public function findAll($tokens) {
$tokenTypes = array(); $tokenTypes = array();
foreach($tokens as $i => $token) { foreach($tokens as $i => $token) {
if(is_array($token)) { if(is_array($token)) {
$tokenTypes[$i] = $token[0]; $tokenType = $token[0];
} else { } else {
$tokenTypes[$i] = $token; $tokenType = $token;
// Pre-process string tokens for matchFrom() // Pre-process string tokens for matchFrom()
$tokens[$i] = array($token, $token); $tokens[$i] = array($token, $token);
} }
if ($tokenType == $this->firstMatch) {
$tokenTypes[$i] = $tokenType;
}
} }
$startKeys = array_keys($tokenTypes, is_array($this->expression[0])
? $this->expression[0][0] : $this->expression[0]);
$allMatches = array(); $allMatches = array();
foreach($tokenTypes as $startKey => $dud) {
foreach($startKeys as $startKey) {
$matches = array(); $matches = array();
if($this->matchFrom($startKey, 0, $tokens, $matches)) { if($this->matchFrom($startKey, 0, $tokens, $matches)) {
$allMatches[] = $matches; $allMatches[] = $matches;
} }
} }
return $allMatches; return $allMatches;
} }