|
| 1 | +// Copyright 2019 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Package diff provides basic text comparison (like Unix's diff(1)). |
| 6 | +package diff |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +// Format returns a formatted diff of the two texts, |
| 14 | +// showing the entire text and the minimum line-level |
| 15 | +// additions and removals to turn text1 into text2. |
| 16 | +// (That is, lines only in text1 appear with a leading -, |
| 17 | +// and lines only in text2 appear with a leading +.) |
| 18 | +func Format(text1, text2 string) string { |
| 19 | + if text1 != "" && !strings.HasSuffix(text1, "\n") { |
| 20 | + text1 += "(missing final newline)" |
| 21 | + } |
| 22 | + lines1 := strings.Split(text1, "\n") |
| 23 | + lines1 = lines1[:len(lines1)-1] // remove empty string after final line |
| 24 | + if text2 != "" && !strings.HasSuffix(text2, "\n") { |
| 25 | + text2 += "(missing final newline)" |
| 26 | + } |
| 27 | + lines2 := strings.Split(text2, "\n") |
| 28 | + lines2 = lines2[:len(lines2)-1] // remove empty string after final line |
| 29 | + |
| 30 | + // Naive dynamic programming algorithm for edit distance. |
| 31 | + // https://en.wikipedia.org/wiki/Wagner–Fischer_algorithm |
| 32 | + // dist[i][j] = edit distance between lines1[:len(lines1)-i] and lines2[:len(lines2)-j] |
| 33 | + // (The reversed indices make following the minimum cost path |
| 34 | + // visit lines in the same order as in the text.) |
| 35 | + dist := make([][]int, len(lines1)+1) |
| 36 | + for i := range dist { |
| 37 | + dist[i] = make([]int, len(lines2)+1) |
| 38 | + if i == 0 { |
| 39 | + for j := range dist[0] { |
| 40 | + dist[0][j] = j |
| 41 | + } |
| 42 | + continue |
| 43 | + } |
| 44 | + for j := range dist[i] { |
| 45 | + if j == 0 { |
| 46 | + dist[i][0] = i |
| 47 | + continue |
| 48 | + } |
| 49 | + cost := dist[i][j-1] + 1 |
| 50 | + if cost > dist[i-1][j]+1 { |
| 51 | + cost = dist[i-1][j] + 1 |
| 52 | + } |
| 53 | + if lines1[len(lines1)-i] == lines2[len(lines2)-j] { |
| 54 | + if cost > dist[i-1][j-1] { |
| 55 | + cost = dist[i-1][j-1] |
| 56 | + } |
| 57 | + } |
| 58 | + dist[i][j] = cost |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + var buf strings.Builder |
| 63 | + i, j := len(lines1), len(lines2) |
| 64 | + for i > 0 || j > 0 { |
| 65 | + cost := dist[i][j] |
| 66 | + if i > 0 && j > 0 && cost == dist[i-1][j-1] && lines1[len(lines1)-i] == lines2[len(lines2)-j] { |
| 67 | + fmt.Fprintf(&buf, " %s\n", lines1[len(lines1)-i]) |
| 68 | + i-- |
| 69 | + j-- |
| 70 | + } else if i > 0 && cost == dist[i-1][j]+1 { |
| 71 | + fmt.Fprintf(&buf, "-%s\n", lines1[len(lines1)-i]) |
| 72 | + i-- |
| 73 | + } else { |
| 74 | + fmt.Fprintf(&buf, "+%s\n", lines2[len(lines2)-j]) |
| 75 | + j-- |
| 76 | + } |
| 77 | + } |
| 78 | + return buf.String() |
| 79 | +} |
0 commit comments