Skip to content
Snippets Groups Projects
Commit 87277dd6 authored by Jared Hancock's avatar Jared Hancock
Browse files

mail-parse: Optimize StringView::split

Remove usage of preg_match_all for large strings
parent 2c006544
Branches
Tags
No related merge requests found
...@@ -525,8 +525,11 @@ class Mail_mimeDecode extends PEAR ...@@ -525,8 +525,11 @@ class Mail_mimeDecode extends PEAR
$boundary = $bs_possible; $boundary = $bs_possible;
} }
if ($input instanceof StringView) if ($input instanceof StringView) {
return $input->split('--' . $boundary); $parts = $input->split('--' . $boundary);
array_shift($parts);
return $parts;
}
$tmp = explode('--' . $boundary, $input); $tmp = explode('--' . $boundary, $input);
...@@ -898,26 +901,28 @@ class StringView { ...@@ -898,26 +901,28 @@ class StringView {
$end ? min($this->start + $end, $this->end ?: PHP_INT_MAX) : $this->end); $end ? min($this->start + $end, $this->end ?: PHP_INT_MAX) : $this->end);
} }
function split($boundary) { function split($token) {
$matches = array(); $ltoken = strlen($token);
if (!preg_match_all('/^' . preg_quote($boundary) . '/m', $this->string,
$matches, PREG_OFFSET_CAPTURE, $this->start))
return array();
$windows = array(); $windows = array();
foreach ($matches[0] as $i => $m) { $offset = $this->start;
$start = $m[1] + strlen($m[0]); for ($i = 0;; $i++) {
$windows[$i] = array('start' => $offset);
$offset = strpos($this->string, $token, $offset);
if (!$offset || ($this->end && $offset >= $this->end))
break;
// Enforce local window // Enforce local window
if ($i > 0) $windows[$i]['stop'] = min($this->end ?: $offset, $offset);
$windows[$i-1]['stop'] = min($this->end ?: $m[1], $m[1]); $offset += $ltoken;
if ($this->end && $start > $this->end) if ($this->end && $offset > $this->end)
break; break;
$windows[$i]['start'] = $m[1] + strlen($m[0]);
} }
$parts = array(); $parts = array();
foreach ($windows as $w) { foreach ($windows as $w) {
$parts[] = new StringView($this->string, $w['start'], @$w['stop'] ?: false); $parts[] = new static($this->string, $w['start'], @$w['stop'] ?: false);
} }
return $parts; return $parts;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment