Edit File by line
/home/barbar84/public_h.../wp-inclu.../Text/Diff/Engine
File: string.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Parses unified or context diffs output from eg. the diff utility.
[2] Fix | Delete
*
[3] Fix | Delete
* Example:
[4] Fix | Delete
* <code>
[5] Fix | Delete
* $patch = file_get_contents('example.patch');
[6] Fix | Delete
* $diff = new Text_Diff('string', array($patch));
[7] Fix | Delete
* $renderer = new Text_Diff_Renderer_inline();
[8] Fix | Delete
* echo $renderer->render($diff);
[9] Fix | Delete
* </code>
[10] Fix | Delete
*
[11] Fix | Delete
* Copyright 2005 Örjan Persson <o@42mm.org>
[12] Fix | Delete
* Copyright 2005-2010 The Horde Project (http://www.horde.org/)
[13] Fix | Delete
*
[14] Fix | Delete
* See the enclosed file COPYING for license information (LGPL). If you did
[15] Fix | Delete
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
[16] Fix | Delete
*
[17] Fix | Delete
* @author Örjan Persson <o@42mm.org>
[18] Fix | Delete
* @package Text_Diff
[19] Fix | Delete
* @since 0.2.0
[20] Fix | Delete
*/
[21] Fix | Delete
class Text_Diff_Engine_string {
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Parses a unified or context diff.
[25] Fix | Delete
*
[26] Fix | Delete
* First param contains the whole diff and the second can be used to force
[27] Fix | Delete
* a specific diff type. If the second parameter is 'autodetect', the
[28] Fix | Delete
* diff will be examined to find out which type of diff this is.
[29] Fix | Delete
*
[30] Fix | Delete
* @param string $diff The diff content.
[31] Fix | Delete
* @param string $mode The diff mode of the content in $diff. One of
[32] Fix | Delete
* 'context', 'unified', or 'autodetect'.
[33] Fix | Delete
*
[34] Fix | Delete
* @return array List of all diff operations.
[35] Fix | Delete
*/
[36] Fix | Delete
function diff($diff, $mode = 'autodetect')
[37] Fix | Delete
{
[38] Fix | Delete
// Detect line breaks.
[39] Fix | Delete
$lnbr = "\n";
[40] Fix | Delete
if (strpos($diff, "\r\n") !== false) {
[41] Fix | Delete
$lnbr = "\r\n";
[42] Fix | Delete
} elseif (strpos($diff, "\r") !== false) {
[43] Fix | Delete
$lnbr = "\r";
[44] Fix | Delete
}
[45] Fix | Delete
[46] Fix | Delete
// Make sure we have a line break at the EOF.
[47] Fix | Delete
if (substr($diff, -strlen($lnbr)) != $lnbr) {
[48] Fix | Delete
$diff .= $lnbr;
[49] Fix | Delete
}
[50] Fix | Delete
[51] Fix | Delete
if ($mode != 'autodetect' && $mode != 'context' && $mode != 'unified') {
[52] Fix | Delete
return PEAR::raiseError('Type of diff is unsupported');
[53] Fix | Delete
}
[54] Fix | Delete
[55] Fix | Delete
if ($mode == 'autodetect') {
[56] Fix | Delete
$context = strpos($diff, '***');
[57] Fix | Delete
$unified = strpos($diff, '---');
[58] Fix | Delete
if ($context === $unified) {
[59] Fix | Delete
return PEAR::raiseError('Type of diff could not be detected');
[60] Fix | Delete
} elseif ($context === false || $unified === false) {
[61] Fix | Delete
$mode = $context !== false ? 'context' : 'unified';
[62] Fix | Delete
} else {
[63] Fix | Delete
$mode = $context < $unified ? 'context' : 'unified';
[64] Fix | Delete
}
[65] Fix | Delete
}
[66] Fix | Delete
[67] Fix | Delete
// Split by new line and remove the diff header, if there is one.
[68] Fix | Delete
$diff = explode($lnbr, $diff);
[69] Fix | Delete
if (($mode == 'context' && strpos($diff[0], '***') === 0) ||
[70] Fix | Delete
($mode == 'unified' && strpos($diff[0], '---') === 0)) {
[71] Fix | Delete
array_shift($diff);
[72] Fix | Delete
array_shift($diff);
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
if ($mode == 'context') {
[76] Fix | Delete
return $this->parseContextDiff($diff);
[77] Fix | Delete
} else {
[78] Fix | Delete
return $this->parseUnifiedDiff($diff);
[79] Fix | Delete
}
[80] Fix | Delete
}
[81] Fix | Delete
[82] Fix | Delete
/**
[83] Fix | Delete
* Parses an array containing the unified diff.
[84] Fix | Delete
*
[85] Fix | Delete
* @param array $diff Array of lines.
[86] Fix | Delete
*
[87] Fix | Delete
* @return array List of all diff operations.
[88] Fix | Delete
*/
[89] Fix | Delete
function parseUnifiedDiff($diff)
[90] Fix | Delete
{
[91] Fix | Delete
$edits = array();
[92] Fix | Delete
$end = count($diff) - 1;
[93] Fix | Delete
for ($i = 0; $i < $end;) {
[94] Fix | Delete
$diff1 = array();
[95] Fix | Delete
switch (substr($diff[$i], 0, 1)) {
[96] Fix | Delete
case ' ':
[97] Fix | Delete
do {
[98] Fix | Delete
$diff1[] = substr($diff[$i], 1);
[99] Fix | Delete
} while (++$i < $end && substr($diff[$i], 0, 1) == ' ');
[100] Fix | Delete
$edits[] = new Text_Diff_Op_copy($diff1);
[101] Fix | Delete
break;
[102] Fix | Delete
[103] Fix | Delete
case '+':
[104] Fix | Delete
// get all new lines
[105] Fix | Delete
do {
[106] Fix | Delete
$diff1[] = substr($diff[$i], 1);
[107] Fix | Delete
} while (++$i < $end && substr($diff[$i], 0, 1) == '+');
[108] Fix | Delete
$edits[] = new Text_Diff_Op_add($diff1);
[109] Fix | Delete
break;
[110] Fix | Delete
[111] Fix | Delete
case '-':
[112] Fix | Delete
// get changed or removed lines
[113] Fix | Delete
$diff2 = array();
[114] Fix | Delete
do {
[115] Fix | Delete
$diff1[] = substr($diff[$i], 1);
[116] Fix | Delete
} while (++$i < $end && substr($diff[$i], 0, 1) == '-');
[117] Fix | Delete
[118] Fix | Delete
while ($i < $end && substr($diff[$i], 0, 1) == '+') {
[119] Fix | Delete
$diff2[] = substr($diff[$i++], 1);
[120] Fix | Delete
}
[121] Fix | Delete
if (count($diff2) == 0) {
[122] Fix | Delete
$edits[] = new Text_Diff_Op_delete($diff1);
[123] Fix | Delete
} else {
[124] Fix | Delete
$edits[] = new Text_Diff_Op_change($diff1, $diff2);
[125] Fix | Delete
}
[126] Fix | Delete
break;
[127] Fix | Delete
[128] Fix | Delete
default:
[129] Fix | Delete
$i++;
[130] Fix | Delete
break;
[131] Fix | Delete
}
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
return $edits;
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
/**
[138] Fix | Delete
* Parses an array containing the context diff.
[139] Fix | Delete
*
[140] Fix | Delete
* @param array $diff Array of lines.
[141] Fix | Delete
*
[142] Fix | Delete
* @return array List of all diff operations.
[143] Fix | Delete
*/
[144] Fix | Delete
function parseContextDiff(&$diff)
[145] Fix | Delete
{
[146] Fix | Delete
$edits = array();
[147] Fix | Delete
$i = $max_i = $j = $max_j = 0;
[148] Fix | Delete
$end = count($diff) - 1;
[149] Fix | Delete
while ($i < $end && $j < $end) {
[150] Fix | Delete
while ($i >= $max_i && $j >= $max_j) {
[151] Fix | Delete
// Find the boundaries of the diff output of the two files
[152] Fix | Delete
for ($i = $j;
[153] Fix | Delete
$i < $end && substr($diff[$i], 0, 3) == '***';
[154] Fix | Delete
$i++);
[155] Fix | Delete
for ($max_i = $i;
[156] Fix | Delete
$max_i < $end && substr($diff[$max_i], 0, 3) != '---';
[157] Fix | Delete
$max_i++);
[158] Fix | Delete
for ($j = $max_i;
[159] Fix | Delete
$j < $end && substr($diff[$j], 0, 3) == '---';
[160] Fix | Delete
$j++);
[161] Fix | Delete
for ($max_j = $j;
[162] Fix | Delete
$max_j < $end && substr($diff[$max_j], 0, 3) != '***';
[163] Fix | Delete
$max_j++);
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
// find what hasn't been changed
[167] Fix | Delete
$array = array();
[168] Fix | Delete
while ($i < $max_i &&
[169] Fix | Delete
$j < $max_j &&
[170] Fix | Delete
strcmp($diff[$i], $diff[$j]) == 0) {
[171] Fix | Delete
$array[] = substr($diff[$i], 2);
[172] Fix | Delete
$i++;
[173] Fix | Delete
$j++;
[174] Fix | Delete
}
[175] Fix | Delete
[176] Fix | Delete
while ($i < $max_i && ($max_j-$j) <= 1) {
[177] Fix | Delete
if ($diff[$i] != '' && substr($diff[$i], 0, 1) != ' ') {
[178] Fix | Delete
break;
[179] Fix | Delete
}
[180] Fix | Delete
$array[] = substr($diff[$i++], 2);
[181] Fix | Delete
}
[182] Fix | Delete
[183] Fix | Delete
while ($j < $max_j && ($max_i-$i) <= 1) {
[184] Fix | Delete
if ($diff[$j] != '' && substr($diff[$j], 0, 1) != ' ') {
[185] Fix | Delete
break;
[186] Fix | Delete
}
[187] Fix | Delete
$array[] = substr($diff[$j++], 2);
[188] Fix | Delete
}
[189] Fix | Delete
if (count($array) > 0) {
[190] Fix | Delete
$edits[] = new Text_Diff_Op_copy($array);
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
if ($i < $max_i) {
[194] Fix | Delete
$diff1 = array();
[195] Fix | Delete
switch (substr($diff[$i], 0, 1)) {
[196] Fix | Delete
case '!':
[197] Fix | Delete
$diff2 = array();
[198] Fix | Delete
do {
[199] Fix | Delete
$diff1[] = substr($diff[$i], 2);
[200] Fix | Delete
if ($j < $max_j && substr($diff[$j], 0, 1) == '!') {
[201] Fix | Delete
$diff2[] = substr($diff[$j++], 2);
[202] Fix | Delete
}
[203] Fix | Delete
} while (++$i < $max_i && substr($diff[$i], 0, 1) == '!');
[204] Fix | Delete
$edits[] = new Text_Diff_Op_change($diff1, $diff2);
[205] Fix | Delete
break;
[206] Fix | Delete
[207] Fix | Delete
case '+':
[208] Fix | Delete
do {
[209] Fix | Delete
$diff1[] = substr($diff[$i], 2);
[210] Fix | Delete
} while (++$i < $max_i && substr($diff[$i], 0, 1) == '+');
[211] Fix | Delete
$edits[] = new Text_Diff_Op_add($diff1);
[212] Fix | Delete
break;
[213] Fix | Delete
[214] Fix | Delete
case '-':
[215] Fix | Delete
do {
[216] Fix | Delete
$diff1[] = substr($diff[$i], 2);
[217] Fix | Delete
} while (++$i < $max_i && substr($diff[$i], 0, 1) == '-');
[218] Fix | Delete
$edits[] = new Text_Diff_Op_delete($diff1);
[219] Fix | Delete
break;
[220] Fix | Delete
}
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
if ($j < $max_j) {
[224] Fix | Delete
$diff2 = array();
[225] Fix | Delete
switch (substr($diff[$j], 0, 1)) {
[226] Fix | Delete
case '+':
[227] Fix | Delete
do {
[228] Fix | Delete
$diff2[] = substr($diff[$j++], 2);
[229] Fix | Delete
} while ($j < $max_j && substr($diff[$j], 0, 1) == '+');
[230] Fix | Delete
$edits[] = new Text_Diff_Op_add($diff2);
[231] Fix | Delete
break;
[232] Fix | Delete
[233] Fix | Delete
case '-':
[234] Fix | Delete
do {
[235] Fix | Delete
$diff2[] = substr($diff[$j++], 2);
[236] Fix | Delete
} while ($j < $max_j && substr($diff[$j], 0, 1) == '-');
[237] Fix | Delete
$edits[] = new Text_Diff_Op_delete($diff2);
[238] Fix | Delete
break;
[239] Fix | Delete
}
[240] Fix | Delete
}
[241] Fix | Delete
}
[242] Fix | Delete
[243] Fix | Delete
return $edits;
[244] Fix | Delete
}
[245] Fix | Delete
[246] Fix | Delete
}
[247] Fix | Delete
[248] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function