Text_Diff_Engine_string::diff()publicWP 1.0

Parses a unified or context diff.

First param contains the whole diff and the second can be used to force a specific diff type. If the second parameter is 'autodetect', the diff will be examined to find out which type of diff this is.

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

Хуков нет.

Возвращает

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

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

$Text_Diff_Engine_string = new Text_Diff_Engine_string();
$Text_Diff_Engine_string->diff( $diff, $mode );
$diff(строка) (обязательный)
The diff content.
$mode(строка)
The diff mode of the content in $diff. One of 'context', 'unified', or 'autodetect'.
По умолчанию: 'autodetect'

Код Text_Diff_Engine_string::diff() WP 6.5.2

function diff($diff, $mode = 'autodetect')
{
    // Detect line breaks.
    $lnbr = "\n";
    if (strpos($diff, "\r\n") !== false) {
        $lnbr = "\r\n";
    } elseif (strpos($diff, "\r") !== false) {
        $lnbr = "\r";
    }

    // Make sure we have a line break at the EOF.
    if (substr($diff, -strlen($lnbr)) != $lnbr) {
        $diff .= $lnbr;
    }

    if ($mode != 'autodetect' && $mode != 'context' && $mode != 'unified') {
        return PEAR::raiseError('Type of diff is unsupported');
    }

    if ($mode == 'autodetect') {
        $context = strpos($diff, '***');
        $unified = strpos($diff, '---');
        if ($context === $unified) {
            return PEAR::raiseError('Type of diff could not be detected');
        } elseif ($context === false || $unified === false) {
            $mode = $context !== false ? 'context' : 'unified';
        } else {
            $mode = $context < $unified ? 'context' : 'unified';
        }
    }

    // Split by new line and remove the diff header, if there is one.
    $diff = explode($lnbr, $diff);
    if (($mode == 'context' && strpos($diff[0], '***') === 0) ||
        ($mode == 'unified' && strpos($diff[0], '---') === 0)) {
        array_shift($diff);
        array_shift($diff);
    }

    if ($mode == 'context') {
        return $this->parseContextDiff($diff);
    } else {
        return $this->parseUnifiedDiff($diff);
    }
}