@@ -293,44 +293,23 @@ protected function linesToCharsMunge($text, array &$lineArray, array &$lineHash)
293293 // explode('\n', $text) would temporarily double our memory footprint,
294294 // but mb_strpos() and mb_substr() work slow
295295 $ lines = explode ($ delimiter , $ text );
296+
297+ $ last_line_has_delimiter = end ($ lines ) === '' ;
298+ if ($ last_line_has_delimiter ) {
299+ array_pop ($ lines );
300+ }
301+
296302 foreach ($ lines as $ i => $ line ) {
297- if (mb_strlen ($ line )) {
298- if (isset ($ lines [$ i + 1 ])) {
299- $ line .= $ delimiter ;
300- }
301- if (isset ($ lineHash [$ line ])) {
302- $ chars .= Utils::unicodeChr ($ lineHash [$ line ]);
303- } else {
304- $ lineArray [] = $ line ;
305- $ lineHash [$ line ] = count ($ lineArray ) - 1 ;
306- $ chars .= Utils::unicodeChr (count ($ lineArray ) - 1 );
307- }
303+ if (isset ($ lines [$ i + 1 ]) || $ last_line_has_delimiter ) {
304+ $ line .= $ delimiter ;
308305 }
306+ if (!isset ($ lineHash [$ line ])) {
307+ $ lineArray [] = $ line ;
308+ $ lineHash [$ line ] = count ($ lineArray ) - 1 ;
309+ }
310+ $ chars .= Utils::unicodeChr ($ lineHash [$ line ]);
309311 }
310312
311- // // Walk the text, pulling out a substring for each line.
312- // // explode('\n', $text) would temporarily double our memory footprint.
313- // // Modifying text would create many large strings to garbage collect.
314- // $lineStart = 0;
315- // $lineEnd = -1;
316- // $textLen = mb_strlen($text);
317- // while ($lineEnd < $textLen - 1) {
318- // $lineEnd = mb_strpos($text, "\n", $lineStart);
319- // if ($lineEnd === false) {
320- // $lineEnd = $textLen - 1;
321- // }
322- // $line = mb_substr($text, $lineStart, $lineEnd + 1 - $lineStart);
323- // $lineStart = $lineEnd + 1;
324- //
325- // if (isset($lineHash[$line])) {
326- // $chars .= Utils::unicodeChr($lineHash[$line]);
327- // } else {
328- // $lineArray[] = $line;
329- // $lineHash[$line] = count($lineArray) - 1;
330- // $chars .= Utils::unicodeChr(count($lineArray) - 1);
331- // }
332- // }
333-
334313 return $ chars ;
335314 }
336315
0 commit comments