std/diff
Differences between texts, and what is done with them.
import "std/diff"
print(unified_diff(read_text("old.txt"), read_text("new.txt"), "old.txt", "new.txt"))
str patched = patch_text(read_text("old.txt"), read_text("fix.patch"))?
MergeResult m = merge3(base, ours, theirs)
print(m.conflicts)A difference is a list of DiffChanges, in order: a_count items of the
first sequence from a_start became b_count items of the second from
b_start; what is between changes is the same in both.
| Function | Result |
|---|---|
diff_items(a, b, algorithm) |
the changes between two str[]: by "myers" (the default: the fewest items deleted and inserted, in linear space), "patience", or "histogram", which keep moved and repeated lines apart better |
diff_lines(a, b, algorithm) |
between the lines of two texts |
diff_words(a, b), diff_chars(a, b) |
between their words, spaces, and punctuation, or their characters as people see them |
lines_with_ends(text) |
the lines, each with its line end |
unified_diff(a, b, a_name, b_name, context, algorithm) |
the unified diff of two texts, with context lines (3) around each change; "" when they are the same |
parse_patch(text) |
the FilePatches of a unified diff, as diff -u and git diff write it: each with old_name, new_name, and hunks |
apply_patch(text, patch) |
the text with a FilePatch applied; a hunk not where it says is looked for nearby, and one found nowhere is an ERROR of kind "patch_failed" |
patch_text(text, patch) |
the same, from the text of a diff of one file |
merge3(base, ours, theirs, ...) |
a MergeResult, with the merged text and the number of conflicts |
inline_changes(old_line, new_line) |
the removed and added byte Spans (start, stop) of the words that changed, to highlight |
merge3 takes a change only one side made, and the same change made on
both; changes of both sides to the same or adjacent lines are a conflict,
between <<<<<<< ours, =======, and >>>>>>> theirs. Its named
arguments ours_label, theirs_label, and base_label name them, and
show_base: true adds the base's lines after ||||||| base, as
git merge-file --diff3 does.