wppaste
WordPress

Text_Diff_Engine_string

Since
0.2.0
Source
wp-includes/Text/Diff/Engine/string.php:23
Parses unified or context diffs output from eg. the diff utility.

Description

Example:

$patch = file_get_contents('example.patch');
$diff = new Text_Diff('string', array($patch));
$renderer = new Text_Diff_Renderer_inline();
echo $renderer->render($diff);

Copyright 2005 Örjan Persson [email protected] Copyright 2005-2010 The Horde Project (http://www.horde.org/)

See the enclosed file COPYING for license information (LGPL). If you did not receive this file, see https://opensource.org/license/lgpl-2-1/.

Compatibility

WordPress
since 0.2.0
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0).

Methods · 3

Source code

class Text_Diff_Engine_string {     /**     * 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.     *     * @param string $diff  The diff content.     * @param string $mode  The diff mode of the content in $diff. One of     *                      'context', 'unified', or 'autodetect'.     *     * @return array  List of all diff operations.     */    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);        }    }     /**     * Parses an array containing the unified diff.     *     * @param array $diff  Array of lines.     *     * @return array  List of all diff operations.     */    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);

Changelog

Introduced in 0.2.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/Text/Diff/Engine/string.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.