Replace the implementation of splitIntoLines by a new one that does

not need PHP's array_shift which tends to be very slow for arrays
with many thousand entries (fixes issue 804).
This commit is contained in:
Thomas Keller 2012-05-04 00:48:53 +02:00
parent 8db3c45763
commit b6acf4c0e2
2 changed files with 9 additions and 13 deletions

View File

@ -4,6 +4,7 @@
- Compatiblity fixes for PostgreSQL (fixes issue 800)
- Fix TOC generation in wiki (issue 803)
- Make the display of large files and diffs faster (fixes issue 804)
## Language and Translations

View File

@ -147,27 +147,22 @@ class IDF_FileUtil
* Splits a string into separate lines while retaining the individual
* line ending character for every line.
*
* OS9 line endings are not supported.
*
* @param string content
* @param boolean if true, skip completely empty lines
* @return string
*/
public static function splitIntoLines($content, $skipEmpty = false)
{
$flags = PREG_SPLIT_OFFSET_CAPTURE;
if ($skipEmpty) $flags |= PREG_SPLIT_NO_EMPTY;
$splitted = preg_split("/\r\n|\n/", $content, -1, $flags);
$last_off = -1;
$last_off = 0;
$lines = array();
while (($split = array_shift($splitted)) !== null) {
if ($last_off != -1) {
$lines[] .= substr($content, $last_off, $split[1] - $last_off);
while (preg_match("/\r\n|\n|\r/", $content, $m, PREG_OFFSET_CAPTURE, $last_off)) {
$next_off = strlen($m[0][0]) + $m[0][1];
$line = substr($content, $last_off, $next_off - $last_off);
$last_off = $next_off;
if ($line !== $m[0][0] || !$skipEmpty) $lines[] = $line;
}
$last_off = $split[1];
}
$lines[] = substr($content, $last_off);
$line = substr($content, $last_off);
if ($line !== false && strlen($line) > 0) $lines[] = $line;
return $lines;
}