diff

Line-by-line LCS text diffing with unified diff output and change stats.

v1.0.0Text & stringsbundledimport "diff";
Overview Installation Documentation Examples

Overview

A zero-dependency, LCS-based line-by-line text diffing library for HolyPHP.

bundled — works offline zero dependencies patch lcs

Quick start

import "diff";

$old = "line 1\nline 2\nline 3\n";
$new = "line 1\nline 2 modified\nline 3\nline 4\n";

$unified = diff_unified($old, $new, "old.txt", "new.txt");
println($unified);

$stats = diff_stats($old, $new);
println("Added: " . $stats["added"] . ", Removed: " . $stats["removed"]);

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install diff

This adds the library to hphp.yaml, installs it into .hphp/packages/diff/ and pins the exact version in hphp.lock:

require:
  diff: "^1.0"

Use it

import "diff";

diff is bundled with the compiler, so import "diff"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install diff          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install diff
Packagediff
Version1.0.0 (follows the compiler)
Entry filediff.hphp
Sourcepackages/lib/diff ↗

Documentation

API Reference

diff_lines(string $old, string $new): array

Computes a line-by-line diff using the Longest Common Subsequence (LCS) algorithm.

diff_unified(string $old, string $new, string $oldLabel = "a", string $newLabel = "b", int $context = 3): string

Generates a standard unified diff string with hunk headers (@@ -oldStart,oldCount +newStart,newCount @@).

diff_stats(string $old, string $new): array

Returns summary counts of line changes between $old and $new.

diff_has_changes(string $old, string $new): bool

Returns true if $old and $new differ on any line, false otherwise.

Error Handling

All functions validate their inputs and throw an Exception with a clear diff: ... message on invalid arguments:

Examples

01Quick start

Bundled with the compiler — import "diff"; works offline in any file, or add it to a project with hphp install diff.

import "diff";

$old = "line 1\nline 2\nline 3\n";
$new = "line 1\nline 2 modified\nline 3\nline 4\n";

$unified = diff_unified($old, $new, "old.txt", "new.txt");
println($unified);

$stats = diff_stats($old, $new);
println("Added: " . $stats["added"] . ", Removed: " . $stats["removed"]);
Try it
hphp install diff
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_diff.hphp.