Diff
in package
General API for generating and formatting diffs - the differences between two sequences of strings.
The original PHP version of this code was written by Geoffrey T. Dairiki dairiki@dairiki.org, and is used/adapted with his permission.
Copyright 2004 Geoffrey T. Dairiki dairiki@dairiki.org Copyright 2004-2017 Horde LLC (http://www.horde.org/)
See the enclosed file LICENSE for license information (LGPL). If you did not receive this file, see http://www.horde.org/licenses/lgpl21.
Tags
Table of Contents
- $edits : OperationList
- __construct() : mixed
- Computes diffs between sequences of strings.
- countAddedLines() : int
- returns the number of new (added) lines in a given diff.
- countDeletedLines() : int
- Returns the number of deleted (removed) lines in a given diff.
- fromFileLineArrays() : Diff
- Shortcut constructor, internally creating the Engine instance.
- fromString() : Diff
- Shortcut constructor, internally creating the Engine instance.
- getDiff() : mixed
- Returns the array of differences.
- getFinal() : array<string|int, mixed>
- Gets the final set of lines.
- getOriginal() : array<string|int, mixed>
- Gets the original set of lines.
- isEmpty() : bool
- Checks for an empty diff.
- lcs() : int
- Computes the length of the Longest Common Subsequence (LCS).
- reverse() : Diff
- Computes a reversed diff.
- trimNewlines() : mixed
- Removes trailing newlines from a line of text. This is meant to be used with array_walk().
- _check() : mixed
- Checks a diff for validity.
Properties
$edits
protected
OperationList
$edits
Methods
__construct()
Computes diffs between sequences of strings.
public
__construct(OperationList $edits) : mixed
Parameters
- $edits : OperationList
Return values
mixed —countAddedLines()
returns the number of new (added) lines in a given diff.
public
countAddedLines() : int
Return values
int —The number of new lines
countDeletedLines()
Returns the number of deleted (removed) lines in a given diff.
public
countDeletedLines() : int
Return values
int —The number of deleted lines
fromFileLineArrays()
Shortcut constructor, internally creating the Engine instance.
public
static fromFileLineArrays([array<string|int, mixed> $fromLines = [] ][, array<string|int, mixed> $toLines = [] ][, string $engineClass = 'auto' ][, array<string|int, mixed> $engineParams = [] ]) : Diff
Default is Auto, meaning it will use XDiffEngine if available, otherwise resort to NativeEngine If you really care about what engine provides the OperationList, implement your own
Use this to create
- NativeEngine
- XDiffEngine
- ShellEngine
- "Auto": The most appropriate engine to deal with two arrays of file lines
- Explicitly any other engine that initializes from two arrays of lines
Parameters
- $fromLines : array<string|int, mixed> = []
- $toLines : array<string|int, mixed> = []
- $engineClass : string = 'auto'
- $engineParams : array<string|int, mixed> = []
Return values
Diff —fromString()
Shortcut constructor, internally creating the Engine instance.
public
static fromString(string $diff[, string $engineClass = 'auto' ][, mixed $engineParams = ['mode' => 'autodetect'] ]) : Diff
Default is Auto, meaning it will use XDiffEngine if available, otherwise resort to NativeEngine If you really care about what engine provides the OperationList, implement your own
Use this to create
- StringEngine
- "Auto": The most appropriate engine to deal with a single string diff source
- Explicitly any other engine that initializes from a single string diff source
Parameters
- $diff : string
- $engineClass : string = 'auto'
- $engineParams : mixed = ['mode' => 'autodetect']
Return values
Diff —getDiff()
Returns the array of differences.
public
getDiff() : mixed
Return values
mixed —getFinal()
Gets the final set of lines.
public
getFinal() : array<string|int, mixed>
This reconstructs the $to_lines parameter passed to the constructor.
Return values
array<string|int, mixed> —The sequence of strings.
getOriginal()
Gets the original set of lines.
public
getOriginal() : array<string|int, mixed>
This reconstructs the $from_lines parameter passed to the constructor.
Return values
array<string|int, mixed> —The original sequence of strings.
isEmpty()
Checks for an empty diff.
public
isEmpty() : bool
Return values
bool —True if two sequences were identical.
lcs()
Computes the length of the Longest Common Subsequence (LCS).
public
lcs() : int
This is mostly for diagnostic purposes.
Return values
int —The length of the LCS.
reverse()
Computes a reversed diff.
public
reverse() : Diff
Example:
$diff = new Horde_Text_Diff($lines1, $lines2);
$rev = $diff->reverse();
Return values
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.
trimNewlines()
Removes trailing newlines from a line of text. This is meant to be used with array_walk().
public
static trimNewlines(string &$line, int $key) : mixed
Parameters
- $line : string
-
The line to trim.
- $key : int
-
The index of the line in the array. Not used.
Return values
mixed —_check()
Checks a diff for validity.
protected
_check(mixed $from_lines, mixed $to_lines) : mixed
This is here only for debugging purposes.
Parameters
- $from_lines : mixed
- $to_lines : mixed