diff
import "std/text" (included in import "std") · namespace std.text
The differences between two texts: line by line, word by word or character by character, as a list of runs, a unified diff (what diff -u and git diff print), or HTML with <del> and <ins>.
grav
import "std"
fn main() {
let before = "host = localhost\nport = 8080\ndebug = true\n"
let after = "host = localhost\nport = 9090\ndebug = true\nworkers = 4\n"
print(Diff.unified(before, after, "a/app.toml", "b/app.toml"))
for (op in Diff.words("the quick brown fox", "the slow brown fox")) {
print("${op.kind} '${op.text}'")
}
print(Diff.html(Diff.words("Total: KES 1,200", "Total: KES 1,500")))
print(Diff.similarity("kitten", "sitting"))
}--- a/app.toml
+++ b/app.toml
@@ -1,3 +1,4 @@
host = localhost
-port = 8080
+port = 9090
debug = true
+workers = 4
Equal 'the '
Delete 'quick'
Insert 'slow'
Equal ' brown fox'
Total: KES 1,<del>200</del><ins>500</ins>
0.6153846153846154- Runs:
Diff.lines,Diff.wordsandDiff.charsreturnDiffOps: the kind (Equal,Delete,Insert), the text, where it starts in the old and new text (oldStart,newStart, counted in tokens) and how many tokens it has (count). Words are runs of letters and digits; spaces and punctuation are tokens of their own. Characters are UTF-8 code points. unified(a, b, fromName, toName, context)writes hunks withcontextunchanged lines around each change (3 by default) and marks a last line without a newline the waydiffdoes; it is""when the texts are equal.similarityis twice the characters the texts share over the characters in both: 1 for equal texts, 0 for texts with nothing in common.- The algorithm is Myers' O(ND) diff in its linear-space form, in C. Tokens are interned to integers first and the common start and end are trimmed, so a small edit in a large file is fast, and memory stays proportional to the input however different the texts are.
API
enum DiffKind
Values
DiffKind.EqualDiffKind.DeleteDiffKind.Insert
class DiffOp
A run of equal, deleted or inserted tokens.
Constructors
new DiffOp(kind: DiffKind, text: string, oldStart: int, newStart: int, count: int)
Fields
kind: DiffKindtext: string— The run's text (whole lines with their "\n" for a line diff).oldStart: int— Where the run starts in the old and the new text, in tokens (lines, words or characters), 0-based.newStart: intcount: int— How many tokens it has.
Methods
fn toString() -> string
class Diff
Line, word and character diffs.
Static members
static fn lines(a: string, b: string) -> Array<DiffOp>— The runs that turnaintob, line by line.static fn words(a: string, b: string) -> Array<DiffOp>— The runs, word by word (spaces and punctuation are tokens of their own).static fn chars(a: string, b: string) -> Array<DiffOp>— The runs, character by character (UTF-8 code points).static fn same(a: string, b: string) -> bool— Whether two texts are equal (no diff needed).static fn similarity(a: string, b: string) -> double— How alike two texts are, from 0 to 1: twice the characters they share over the characters in both (1 for equal texts).static fn html(ops: Array<DiffOp>) -> string— The runs as HTML: deleted text in <del>, inserted text in <ins>, everything escaped.static fn unified(a: string, b: string, fromName: string = "a", toName: string = "b", context: int = 3) -> string— A unified diff of two texts withcontextunchanged lines around each change; "" when they are equal.