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;
|
||||
|
||||
/**
|
||||
* The first expression to match
|
||||
*/
|
||||
protected $firstMatch;
|
||||
|
||||
public function __construct($expression) {
|
||||
$this->expression = $expression;
|
||||
$this->firstMatch = is_array($expression[0]) ? $expression[0][0] : $expression[0];
|
||||
}
|
||||
|
||||
public function findAll($tokens) {
|
||||
$tokenTypes = array();
|
||||
foreach($tokens as $i => $token) {
|
||||
if(is_array($token)) {
|
||||
$tokenTypes[$i] = $token[0];
|
||||
$tokenType = $token[0];
|
||||
} else {
|
||||
$tokenTypes[$i] = $token;
|
||||
$tokenType = $token;
|
||||
// Pre-process string tokens for matchFrom()
|
||||
$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();
|
||||
|
||||
foreach($startKeys as $startKey) {
|
||||
foreach($tokenTypes as $startKey => $dud) {
|
||||
$matches = array();
|
||||
if($this->matchFrom($startKey, 0, $tokens, $matches)) {
|
||||
$allMatches[] = $matches;
|
||||
}
|
||||
}
|
||||
|
||||
return $allMatches;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user