mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #5586 from kinglozzer/tokenisedregex-perf
Improve performance of TokenisedRegularExpression
This commit is contained in:
commit
128eaaeefc
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user