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.
| Type | Intent | Optional | 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 |
||
| integer, | intent(in), | optional | :: | context |
number of unchanged lines shown around each change |
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. 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 character(len=:), allocatable :: diff type(string_t), allocatable :: la(:), lb(:) integer, allocatable :: ia(:), ib(:) type(edit_t) :: e type(string_builder_t) :: buf integer :: ctx, k, first, last, i, j, ka, kb, na, nb, start_a, start_b diff = '' if (a == b .and. len(a) == len(b)) return ctx = 3 if (present(context)) ctx = context la = split_lines(a) lb = split_lines(b) call line_ids(la, lb, ia, ib) allocate (e%op(size(ia) + size(ib))) call lcs(ia, 1, size(ia), ib, 1, size(ib), e) call buf%append('--- '//name//new_line('a')//'+++ '//name//new_line('a')) ! hunks: runs of edits with at most 2*ctx equal lines between them, plus context around k = 1 i = 1 ! next line of a j = 1 ! next line of b do while (k <= e%n) if (e%op(k) == OP_EQUAL) then k = k + 1 i = i + 1 j = j + 1 cycle end if first = k last = k do while (last < e%n) ! extend over changes separated by short runs of equal lines ka = last + 1 do while (ka <= e%n) if (e%op(ka) /= OP_EQUAL) exit ka = ka + 1 end do if (ka > e%n .or. ka - last - 1 > 2*ctx) exit last = ka end do ! the hunk covers edits first-ctx .. last+ctx (equal lines at both ends) ka = first nb = 0 do while (ka > 1 .and. nb < ctx) if (e%op(ka - 1) /= OP_EQUAL) exit ka = ka - 1 nb = nb + 1 end do start_a = i - nb start_b = j - nb kb = last nb = 0 do while (kb < e%n .and. nb < ctx) if (e%op(kb + 1) /= OP_EQUAL) exit kb = kb + 1 nb = nb + 1 end do na = count(e%op(ka:kb) /= OP_INSERT) nb = count(e%op(ka:kb) /= OP_DELETE) call buf%append('@@ -'//range_text(start_a, na)//' +'//range_text(start_b, nb)//' @@'//new_line('a')) i = start_a j = start_b do k = ka, kb select case (e%op(k)) case (OP_EQUAL) call add_line(' ', la(i)%s) i = i + 1 j = j + 1 case (OP_DELETE) call add_line('-', la(i)%s) i = i + 1 case (OP_INSERT) call add_line('+', lb(j)%s) j = j + 1 end select end do k = kb + 1 end do diff = buf%get() contains subroutine add_line(mark, line) !! Append `line` to the diff after `mark`, with a "No newline" note if it has no line ending. character, intent(in) :: mark !! ` ` for context, `-` for a deleted line, `+` for an inserted one character(len=*), intent(in) :: line !! the line, with its line ending call buf%append(mark//line) if (len(line) == 0) then call buf%append(new_line('a')//'\ No newline at end of file'//new_line('a')) else if (line(len(line):len(line)) /= achar(10)) then call buf%append(new_line('a')//'\ No newline at end of file'//new_line('a')) end if end subroutine add_line end function unified_diff