ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
Text_Diff Class Reference
+ Inheritance diagram for Text_Diff:
+ Collaboration diagram for Text_Diff:

Public Member Functions

 Text_Diff ($from_lines, $to_lines)
 Computes diffs between sequences of strings.
 getDiff ()
 Returns the array of differences.
 reverse ()
 Computes a reversed diff.
 isEmpty ()
 Checks for an empty diff.
 lcs ()
 Computes the length of the Longest Common Subsequence (LCS).
 getOriginal ()
 Gets the original set of lines.
 getFinal ()
 Gets the final set of lines.
 _trimNewlines (&$line, $key)
 Removes trailing newlines from a line of text.
 _check ($from_lines, $to_lines)
 Checks a diff for validity.

Data Fields

 $_edits

Detailed Description

Definition at line 16 of file Diff.php.

Member Function Documentation

Text_Diff::_check (   $from_lines,
  $to_lines 
)

Checks a diff for validity.

This is here only for debugging purposes.

Definition at line 165 of file Diff.php.

References getFinal(), getOriginal(), and reverse().

{
if (serialize($from_lines) != serialize($this->getOriginal())) {
trigger_error("Reconstructed original doesn't match", E_USER_ERROR);
}
if (serialize($to_lines) != serialize($this->getFinal())) {
trigger_error("Reconstructed final doesn't match", E_USER_ERROR);
}
$rev = $this->reverse();
if (serialize($to_lines) != serialize($rev->getOriginal())) {
trigger_error("Reversed original doesn't match", E_USER_ERROR);
}
if (serialize($from_lines) != serialize($rev->getFinal())) {
trigger_error("Reversed final doesn't match", E_USER_ERROR);
}
$prevtype = null;
foreach ($this->_edits as $edit) {
if ($prevtype == get_class($edit)) {
trigger_error("Edit sequence is non-optimal", E_USER_ERROR);
}
$prevtype = get_class($edit);
}
return true;
}

+ Here is the call graph for this function:

Text_Diff::_trimNewlines ( $line,
  $key 
)

Removes trailing newlines from a line of text.

This is meant to be used with array_walk().

Parameters
string$lineThe line to trim.
integer$keyThe index of the line in the array. Not used.

Definition at line 155 of file Diff.php.

{
$line = str_replace(array("\n", "\r"), '', $line);
}
Text_Diff::getDiff ( )

Returns the array of differences.

Definition at line 49 of file Diff.php.

References $_edits.

{
return $this->_edits;
}
Text_Diff::getFinal ( )

Gets the final set of lines.

This reconstructs the $to_lines parameter passed to the constructor.

Returns
array The sequence of strings.

Definition at line 137 of file Diff.php.

Referenced by _check().

{
$lines = array();
foreach ($this->_edits as $edit) {
if ($edit->final) {
array_splice($lines, count($lines), 0, $edit->final);
}
}
return $lines;
}

+ Here is the caller graph for this function:

Text_Diff::getOriginal ( )

Gets the original set of lines.

This reconstructs the $from_lines parameter passed to the constructor.

Returns
array The original sequence of strings.

Definition at line 119 of file Diff.php.

Referenced by _check().

{
$lines = array();
foreach ($this->_edits as $edit) {
if ($edit->orig) {
array_splice($lines, count($lines), 0, $edit->orig);
}
}
return $lines;
}

+ Here is the caller graph for this function:

Text_Diff::isEmpty ( )

Checks for an empty diff.

Returns
boolean True if two sequences were identical.

Definition at line 84 of file Diff.php.

{
foreach ($this->_edits as $edit) {
if (!$edit instanceof Text_Diff_Op_copy) {
return false;
}
}
return true;
}
Text_Diff::lcs ( )

Computes the length of the Longest Common Subsequence (LCS).

This is mostly for diagnostic purposes.

Returns
integer The length of the LCS.

Definition at line 101 of file Diff.php.

{
$lcs = 0;
foreach ($this->_edits as $edit) {
if ($edit instanceof Text_Diff_Op_copy) {
$lcs += count($edit->orig);
}
}
return $lcs;
}
Text_Diff::reverse ( )

Computes a reversed diff.

Example: $diff = &new Text_Diff($lines1, $lines2); $rev = $diff->reverse();

Returns
Text_Diff A Diff object representing the inverse of the original diff. Note that we purposely don't return a reference here, since this essentially is a clone() method.

Definition at line 68 of file Diff.php.

Referenced by _check().

{
$rev = clone($obj);
$rev->_edits = array();
foreach ($this->_edits as $edit) {
$rev->_edits[] = $edit->reverse();
}
return $rev;
}

+ Here is the caller graph for this function:

Text_Diff::Text_Diff (   $from_lines,
  $to_lines 
)

Computes diffs between sequences of strings.

Parameters
array$from_linesAn array of strings. Typically these are lines from a file.
array$to_linesAn array of strings.

Definition at line 32 of file Diff.php.

Referenced by Text_MappedDiff\Text_MappedDiff().

{
array_walk($from_lines, array($this, '_trimNewlines'));
array_walk($to_lines, array($this, '_trimNewlines'));
if (extension_loaded('xdiff')) {
$engine = new Text_Diff_Engine_xdiff();
} else {
$engine = new Text_Diff_Engine_native();
}
$this->_edits = $engine->diff($from_lines, $to_lines);
}

+ Here is the caller graph for this function:

Field Documentation

Text_Diff::$_edits

Definition at line 23 of file Diff.php.

Referenced by getDiff().


The documentation for this class was generated from the following file: