DiffMatchPatch\DiffToolkit::linesToCharsMunge PHP Method

linesToCharsMunge() protected method

Modifies $lineArray and $lineHash. TODO try to fix it!
protected linesToCharsMunge ( string $text, array &$lineArray, array &$lineHash ) : string
$text string String to encode.
$lineArray array
$lineHash array
return string Encoded string.
    protected function linesToCharsMunge($text, array &$lineArray, array &$lineHash)
    {
        // Simple string concat is even faster than implode() in PHP.
        $chars = '';
        $delimiter = iconv('UTF-8', mb_internal_encoding(), "\n");
        // TODO optimize code
        // explode('\n', $text) would temporarily double our memory footprint,
        // but mb_strpos() and mb_substr() work slow
        $lines = explode($delimiter, $text);
        foreach ($lines as $i => $line) {
            if (mb_strlen($line)) {
                if (isset($lines[$i + 1])) {
                    $line .= $delimiter;
                }
                if (isset($lineHash[$line])) {
                    $chars .= Utils::unicodeChr($lineHash[$line]);
                } else {
                    $lineArray[] = $line;
                    $lineHash[$line] = count($lineArray) - 1;
                    $chars .= Utils::unicodeChr(count($lineArray) - 1);
                }
            }
        }
        //        // Walk the text, pulling out a substring for each line.
        //        // explode('\n', $text) would temporarily double our memory footprint.
        //        // Modifying text would create many large strings to garbage collect.
        //        $lineStart = 0;
        //        $lineEnd = -1;
        //        $textLen = mb_strlen($text);
        //        while ($lineEnd < $textLen - 1) {
        //            $lineEnd = mb_strpos($text, "\n", $lineStart);
        //            if ($lineEnd === false) {
        //                $lineEnd = $textLen - 1;
        //            }
        //            $line = mb_substr($text, $lineStart, $lineEnd + 1 - $lineStart);
        //            $lineStart = $lineEnd + 1;
        //
        //            if (isset($lineHash[$line])) {
        //                $chars .= Utils::unicodeChr($lineHash[$line]);
        //            } else {
        //                $lineArray[] = $line;
        //                $lineHash[$line] = count($lineArray) - 1;
        //                $chars .= Utils::unicodeChr(count($lineArray) - 1);
        //            }
        //        }
        return $chars;
    }