ffmt_diff Module

Unified diffs of two texts, for --diff.

Lines are matched with a longest common subsequence computed by Hirschberg's algorithm, which needs memory linear in the number of lines, so a diff of a file whose every line changed is still cheap. Lines are compared as integers after hashing.


Uses

  • module~~ffmt_diff~~UsesGraph module~ffmt_diff ffmt_diff iso_fortran_env iso_fortran_env module~ffmt_diff->iso_fortran_env module~ffmt_strings ffmt_strings module~ffmt_diff->module~ffmt_strings

Used by

  • module~~ffmt_diff~~UsedByGraph module~ffmt_diff ffmt_diff module~ffmt_cli ffmt_cli module~ffmt_cli->module~ffmt_diff

Variables

Type Visibility Attributes Name Initial
integer, private, parameter :: OP_DELETE = 1
integer, private, parameter :: OP_EQUAL = 0
integer, private, parameter :: OP_INSERT = 2

Derived Types

type, private ::  edit_t

The edits that turn the lines of one text into those of the other, in order.

Components

Type Visibility Attributes Name Initial
integer, public :: n = 0

number of edits in op

integer, public, allocatable :: op(:)

OP_EQUAL, OP_DELETE (line of a) or OP_INSERT (line of b)


Functions

private pure function hash(s) result(h)

FNV-1a hash.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: s

the bytes to hash

Return Value integer(kind=int64)

private pure function lcs_lengths(a, b) result(row)

row(k): the length of the longest common subsequence of a and b(1:k).

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: a(:)

line numbers of the two texts

integer, intent(in) :: b(:)

line numbers of the two texts

Return Value integer, (0:size(b))

private pure function range_text(start, n) result(text)

start,n for a hunk header; start is the line before the hunk when it is empty.

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: start

first line of the hunk

integer, intent(in) :: n

number of lines in the hunk

Return Value character(len=:), allocatable

private function split_lines(text) result(lines)

The lines of text, each with its line ending (the last one may have none).

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: text

the text

Return Value type(string_t), allocatable, (:)

public function unified_diff(a, b, name, context) result(diff)

The changes from a to b as a unified diff with context lines of context (default 3), with name in the headers. Empty if the texts are equal.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: a

the old text

character(len=*), intent(in) :: b

the new text

character(len=*), intent(in) :: name

file name for the --- and +++ headers

integer, intent(in), optional :: context

number of unchanged lines shown around each change

Return Value character(len=:), allocatable


Subroutines

private recursive subroutine lcs(a, a0, a1, b, b0, b1, e)

Append the edits that turn a(a0:a1) into b(b0:b1) along a longest common subsequence (Hirschberg's algorithm).

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: a(:)

line numbers of the two texts, from line_ids

integer, intent(in) :: a0

the ranges of a and b to compare

integer, intent(in) :: a1

the ranges of a and b to compare

integer, intent(in) :: b(:)

line numbers of the two texts, from line_ids

integer, intent(in) :: b0

the ranges of a and b to compare

integer, intent(in) :: b1

the ranges of a and b to compare

type(edit_t), intent(inout) :: e

the edits so far; e%op must have room for the new ones

private subroutine line_ids(la, lb, ia, ib)

Number the distinct lines, so that equal lines have equal numbers.

Arguments

Type IntentOptional Attributes Name
type(string_t), intent(in) :: la(:)

the lines of the two texts

type(string_t), intent(in) :: lb(:)

the lines of the two texts

integer, intent(out), allocatable :: ia(:)

the number of each line of la and lb

integer, intent(out), allocatable :: ib(:)

the number of each line of la and lb