Text_Diff_Engine_string::diff

Advertisement

Summery Summery

Parses a unified or context diff.

Syntax Syntax

Text_Diff_Engine_string::diff( string $diff, string $mode = 'autodetect' )

Description Description

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.

Parameters Parameters

$diff

(Required) The diff content.

$mode

(Optional) The diff mode of the content in $diff. One of 'context', 'unified', or 'autodetect'.

Default value: 'autodetect'

Return Return

(array) List of all diff operations.

Source Source

File: wp-includes/Text/Diff/Engine/string.php

    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);
        }
    }

Advertisement

Advertisement

Leave a Reply