diff options
author | Urban Müller | 2011-11-08 15:03:11 +0000 |
---|---|---|
committer | Urban Müller | 2011-11-08 15:03:11 +0000 |
commit | baff0f7a13d4338dde7e08405e421666e1135929 (patch) | |
tree | 302505b7ac150fa6ca418ffea23e1a9677d6d3e8 /it.class | |
parent | e3dfb20dc72ec8699293f1d46d6caf5d9332423e (diff) | |
download | itools-baff0f7a13d4338dde7e08405e421666e1135929.tar.gz itools-baff0f7a13d4338dde7e08405e421666e1135929.tar.bz2 itools-baff0f7a13d4338dde7e08405e421666e1135929.zip |
faster it::match for simple cases
Diffstat (limited to 'it.class')
-rw-r--r-- | it.class | 36 |
1 files changed, 15 insertions, 21 deletions
@@ -23,7 +23,6 @@ class it { - /** * Create config class with static members initialized (e.g. $home). * NOTE: PHP5 ONLY @@ -349,32 +348,28 @@ static function convertregex($pattern, $p = array()) * @param $p contains pattern modifiers, @see convertregex() * @return Matched string or false */ -static function match($pattern, $string, $p = array()) +static function match($pattern, $string, $p = null) { - $flags = 0; - $p += array('locale' => 'de_CH'); - - if($p['offset_capture']) - $flags |= PREG_OFFSET_CAPTURE; + if (!preg_match('/\\\\[wb]|[!\x80-\xff]/i', $pattern) && !$p) + $r = preg_match('!' . $pattern . '!i', $string, $m); # fast path for simple patterns + else + { + $flags = $p['offset_capture'] ? PREG_OFFSET_CAPTURE : 0; - $oldlocale = setlocale(LC_CTYPE, 0); - setlocale(LC_CTYPE, $p['locale']); + $oldlocale = setlocale(LC_CTYPE, 0); + setlocale(LC_CTYPE, $p['locale'] ? $p['locale'] : "de_CH"); - if ($p['all']) - $r = preg_match_all(it::convertregex($pattern, $p), $string, $m, $flags | PREG_PATTERN_ORDER, $p['offset']); - else - $r = preg_match(it::convertregex($pattern, $p), $string, $m, $flags, $p['offset']); + if ($p['all']) + $r = preg_match_all(it::convertregex($pattern, $p), $string, $m, $flags | PREG_PATTERN_ORDER, $p['offset']); + else + $r = preg_match(it::convertregex($pattern, $p), $string, $m, $flags, $p['offset']); - setlocale(LC_CTYPE, $oldlocale); + setlocale(LC_CTYPE, $oldlocale); + } if (!$r) # no match { - static $backtrackerror; - - if (!isset($backtrackerror)) - $backtrackerror = defined('PREG_BACKTRACK_LIMIT_ERROR') ? constant('PREG_BACKTRACK_LIMIT_ERROR') : 0; - - if ($backtrackerror && preg_last_error() == $backtrackerror) # Silence phpversionlint.php commit hook: function_exists('preg_last_error') + if (preg_last_error() == PREG_BACKTRACK_LIMIT_ERROR) # Silence phpversionlint.php commit hook: function_exists('preg_last_error') it::error("Exceeded pcre.backtrack_limit of " . ini_get('pcre.backtrack_limit') . " bytes"); $result = $p['all'] ? array() : null; @@ -846,5 +841,4 @@ function pipe($cmd, $args = array()) } } - ?> |