Text_Diff_Engine_string::parseUnifiedDiff()publicWP 1.0

Parses an array containing the unified diff.

Метод класса: Text_Diff_Engine_string{}

Хуков нет.

Возвращает

Массив. List of all diff operations.

Использование

$Text_Diff_Engine_string = new Text_Diff_Engine_string();
$Text_Diff_Engine_string->parseUnifiedDiff( $diff );
$diff(массив) (обязательный)
Array of lines.

Код Text_Diff_Engine_string::parseUnifiedDiff() WP 6.5.2

function parseUnifiedDiff($diff)
{
    $edits = array();
    $end = count($diff) - 1;
    for ($i = 0; $i < $end;) {
        $diff1 = array();
        switch (substr($diff[$i], 0, 1)) {
        case ' ':
            do {
                $diff1[] = substr($diff[$i], 1);
            } while (++$i < $end && substr($diff[$i], 0, 1) == ' ');
            $edits[] = new Text_Diff_Op_copy($diff1);
            break;

        case '+':
            // get all new lines
            do {
                $diff1[] = substr($diff[$i], 1);
            } while (++$i < $end && substr($diff[$i], 0, 1) == '+');
            $edits[] = new Text_Diff_Op_add($diff1);
            break;

        case '-':
            // get changed or removed lines
            $diff2 = array();
            do {
                $diff1[] = substr($diff[$i], 1);
            } while (++$i < $end && substr($diff[$i], 0, 1) == '-');

            while ($i < $end && substr($diff[$i], 0, 1) == '+') {
                $diff2[] = substr($diff[$i++], 1);
            }
            if (count($diff2) == 0) {
                $edits[] = new Text_Diff_Op_delete($diff1);
            } else {
                $edits[] = new Text_Diff_Op_change($diff1, $diff2);
            }
            break;

        default:
            $i++;
            break;
        }
    }

    return $edits;
}