PHP Class DiffMatchPatch\Diff

Author: Neil Fraser ([email protected])
Author: Daniil Skrobov ([email protected])
Show file Open project: yetanotherape/diff-match-patch Class Usage Examples

Protected Properties

Property Type Description
$changes array
$editCost Cost of an empty edit operation in terms of edit characters.
$timeout Number of seconds to map a diff before giving up (0 for infinity).
$toolkit DiffToolkit

Public Methods

Method Description
__construct ( string | null $text1 = null, string | null $text2 = null ) Init object and call main(), if texts passed.
cleanupEfficiency ( ) Reduce the number of edits by eliminating operationally trivial equalities.
cleanupMerge ( ) Reorder and merge like edit sections. Merge equalities.
cleanupSemantic ( ) Reduce the number of edits by eliminating semantically trivial equalities.
cleanupSemanticLossless ( ) Look for single edits surrounded on both sides by equalities which can be shifted sideways to align the edit to a word boundary.
fromDelta ( string $text1, string $delta ) Given the original text1, and an encoded string which describes the operations required to transform text1 into text2, compute the full diff.
getChanges ( ) : array
getEditCost ( ) : integer
getTimeout ( ) : float
getToolkit ( ) : DiffToolkit
levenshtein ( ) : integer Compute the Levenshtein distance; the number of inserted, deleted or substituted characters.
main ( string $text1, string $text2, boolean $checklines = true, integer $deadline = null ) : self Find the differences between two texts. Simplifies the problem by stripping any common prefix or suffix off the texts before diffing.
prettyHtml ( ) : string Convert a diff array into a pretty HTML report.
setChanges ( array $changes )
setEditCost ( integer $editCost )
setTimeout ( $timeout )
setToolkit ( DiffToolkit $toolkit )
text1 ( ) : string Compute and return the source text (all equalities and deletions).
text2 ( ) : string Compute and return the destination text (all equalities and insertions).
toDelta ( ) : string Crush the diff into an encoded string which describes the operations required to transform text1 into text2.
xIndex ( integer $loc ) : integer Compute and return location in text2 equivalent to the $loc in text1.

Protected Methods

Method Description
bisect ( string $text1, string $text2, integer $deadline ) : array Find the 'middle snake' of a diff, split the problem in two and return the recursively constructed diff.
bisectSplit ( string $text1, string $text2, integer $x, integer $y, integer $deadline ) : array Given the location of the 'middle snake', split the diff in two parts and recurse.
cleanupSemanticScore ( string $one, string $two ) : integer Given two strings, compute a score representing whether the internal boundary falls on logical boundaries.
compute ( string $text1, string $text2, boolean $checklines, integer $deadline ) : array Find the differences between two texts. Assumes that the texts do not have any common prefix or suffix.
lineMode ( string $text1, string $text2, integer $deadline ) : array Do a quick line-level diff on both strings, then rediff the parts for greater accuracy.

Method Details

__construct() public method

Init object and call main(), if texts passed.
public __construct ( string | null $text1 = null, string | null $text2 = null )
$text1 string | null
$text2 string | null

bisect() protected method

See Myers 1986 paper: An O(ND) Difference Algorithm and Its Variations.
protected bisect ( string $text1, string $text2, integer $deadline ) : array
$text1 string Old string to be diffed.
$text2 string New string to be diffed.
$deadline integer Time at which to bail if not yet complete.
return array Array of diff arrays.

bisectSplit() protected method

Given the location of the 'middle snake', split the diff in two parts and recurse.
protected bisectSplit ( string $text1, string $text2, integer $x, integer $y, integer $deadline ) : array
$text1 string Old string to be diffed.
$text2 string New string to be diffed.
$x integer Index of split point in text1.
$y integer Index of split point in text2.
$deadline integer Time at which to bail if not yet complete.
return array Array of diff arrays.

cleanupEfficiency() public method

TODO refactor this Cap's code
public cleanupEfficiency ( )

cleanupMerge() public method

Any edit section can move as long as it doesn't cross an equality.
public cleanupMerge ( )

cleanupSemantic() public method

TODO refactor this cap's code
public cleanupSemantic ( )

cleanupSemanticLossless() public method

e.g: The cat came. -> The cat came.

cleanupSemanticScore() protected method

Scores range from 6 (best) to 0 (worst).
protected cleanupSemanticScore ( string $one, string $two ) : integer
$one string First string.
$two string Second string.
return integer The score.

compute() protected method

Find the differences between two texts. Assumes that the texts do not have any common prefix or suffix.
protected compute ( string $text1, string $text2, boolean $checklines, integer $deadline ) : array
$text1 string Old string to be diffed.
$text2 string New string to be diffed.
$checklines boolean Speedup flag. If false, then don't run a line-level diff first to identify the changed areas. If true, then run a faster, slightly less optimal diff.
$deadline integer Time when the diff should be complete by.
return array Array of changes.

fromDelta() public method

Given the original text1, and an encoded string which describes the operations required to transform text1 into text2, compute the full diff.
public fromDelta ( string $text1, string $delta )
$text1 string Source string for the diff.
$delta string Delta text.

getChanges() public method

public getChanges ( ) : array
return array

getEditCost() public method

public getEditCost ( ) : integer
return integer

getTimeout() public method

public getTimeout ( ) : float
return float

getToolkit() public method

public getToolkit ( ) : DiffToolkit
return DiffToolkit

levenshtein() public method

Compute the Levenshtein distance; the number of inserted, deleted or substituted characters.
public levenshtein ( ) : integer
return integer Number of changes.

lineMode() protected method

This speedup can produce non-minimal diffs.
protected lineMode ( string $text1, string $text2, integer $deadline ) : array
$text1 string Old string to be diffed.
$text2 string New string to be diffed.
$deadline integer Time when the diff should be complete by.
return array Array of changes.

main() public method

Find the differences between two texts. Simplifies the problem by stripping any common prefix or suffix off the texts before diffing.
public main ( string $text1, string $text2, boolean $checklines = true, integer $deadline = null ) : self
$text1 string Old string to be diffed.
$text2 string New string to be diffed.
$checklines boolean Optional speedup flag. If present and false, then don't run a line-level diff first to identify the changed areas. Defaults to true, which does a faster, slightly less optimal diff.
$deadline integer Optional time when the diff should be complete by. Used internally for recursive calls. Users should set $this->timeout instead.
return self

prettyHtml() public method

Convert a diff array into a pretty HTML report.
public prettyHtml ( ) : string
return string HTML representation.

setChanges() public method

public setChanges ( array $changes )
$changes array

setEditCost() public method

public setEditCost ( integer $editCost )
$editCost integer

setTimeout() public method

public setTimeout ( $timeout )
$timeout

setToolkit() public method

public setToolkit ( DiffToolkit $toolkit )
$toolkit DiffToolkit

text1() public method

Compute and return the source text (all equalities and deletions).
public text1 ( ) : string
return string Source text.

text2() public method

Compute and return the destination text (all equalities and insertions).
public text2 ( ) : string
return string Destination text.

toDelta() public method

E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'. Operations are tab-separated. Inserted text is escaped using %xx notation.
public toDelta ( ) : string
return string Delta text.

xIndex() public method

e.g. "The cat" vs "The big cat", 1->1, 5->8
public xIndex ( integer $loc ) : integer
$loc integer Location within text1.
return integer Location within text2.

Property Details

$changes protected property

protected array $changes
return array

$editCost protected property

Cost of an empty edit operation in terms of edit characters.
protected $editCost

$timeout protected property

Number of seconds to map a diff before giving up (0 for infinity).
protected $timeout

$toolkit protected property

protected DiffToolkit,diffmatchpatch $toolkit
return DiffToolkit