diff options
author | Aleksander Machniak <alec@alec.pl> | 2014-05-12 10:20:42 +0200 |
---|---|---|
committer | Aleksander Machniak <alec@alec.pl> | 2014-05-12 10:20:42 +0200 |
commit | d9d276ea700186a11c6525fec375160a76229b76 (patch) | |
tree | d51c7b7f7f4203fce6972c4bf9ee96eee0248f25 | |
parent | b5bb6479bcd3a9c3e8a9fc5af730dd64a6e15966 (diff) |
Improve _convert_line() performance
-rw-r--r-- | program/lib/Roundcube/rcube_text2html.php | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/program/lib/Roundcube/rcube_text2html.php b/program/lib/Roundcube/rcube_text2html.php index 5da771ac8..6032726b0 100644 --- a/program/lib/Roundcube/rcube_text2html.php +++ b/program/lib/Roundcube/rcube_text2html.php @@ -259,14 +259,20 @@ class rcube_text2html // replace spaces with non-breaking spaces if ($is_flowed) { - $text = preg_replace_callback('/(^|[^ ])( +)/', function($matches) { - if (!strlen($matches[2])) { - return str_repeat($nbsp, strlen($matches[2])); - } - else { - return $matches[1] . ' ' . str_repeat($nbsp, strlen($matches[2])-1); - } - }, $text); + $pos = 0; + $diff = 0; + $len = strlen($nbsp); + $copy = $text; + + while (($pos = strpos($text, ' ', $pos)) !== false) { + if ($pos == 0 || $text[$pos-1] == ' ') { + $copy = substr_replace($copy, $nbsp, $pos + $diff, 1); + $diff += $len - 1; + } + $pos++; + } + + $text = $copy; } else { $text = str_replace(' ', $nbsp, $text); |