Skip to content

Commit bbe57ea

Browse files
Fix naming of the parameter block and allow a JunkLine hook in it.
These changes should be backwards-compatible.
1 parent 3189546 commit bbe57ea

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

difflib/difflib.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -791,15 +791,17 @@ func formatRangeUnified(start, stop int) string {
791791
}
792792

793793
// Unified diff parameters
794-
type UnifiedDiff struct {
795-
A []string // First sequence lines
796-
FromFile string // First file name
797-
FromDate string // First file time
798-
B []string // Second sequence lines
799-
ToFile string // Second file name
800-
ToDate string // Second file time
801-
Eol string // Headers end of line, defaults to LF
802-
Context int // Number of context lines
794+
type LineDiffParams struct {
795+
A []string // First sequence lines
796+
FromFile string // First file name
797+
FromDate string // First file time
798+
B []string // Second sequence lines
799+
ToFile string // Second file name
800+
ToDate string // Second file time
801+
Eol string // Headers end of line, defaults to LF
802+
Context int // Number of context lines
803+
AutoJunk bool // If true, use autojunking
804+
IsJunkLine func(string)bool // How to spot junk lines
803805
}
804806

805807
// Compare two sequences of lines; generate the delta as a unified diff.
@@ -821,7 +823,7 @@ type UnifiedDiff struct {
821823
// times. Any or all of these may be specified using strings for
822824
// 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'.
823825
// The modification times are normally expressed in the ISO 8601 format.
824-
func WriteUnifiedDiff(writer io.Writer, diff UnifiedDiff) error {
826+
func WriteUnifiedDiff(writer io.Writer, diff LineDiffParams) error {
825827
//buf := bufio.NewWriter(writer)
826828
//defer buf.Flush()
827829
var bld strings.Builder
@@ -841,6 +843,9 @@ func WriteUnifiedDiff(writer io.Writer, diff UnifiedDiff) error {
841843

842844
started := false
843845
m := NewMatcher(diff.A, diff.B)
846+
if diff.AutoJunk || diff.IsJunkLine != nil {
847+
m = NewMatcherWithJunk(diff.A, diff.B, diff.AutoJunk, diff.IsJunkLine)
848+
}
844849
for _, g := range m.GetGroupedOpCodes(diff.Context) {
845850
if !started {
846851
started = true
@@ -902,7 +907,7 @@ func WriteUnifiedDiff(writer io.Writer, diff UnifiedDiff) error {
902907
}
903908

904909
// Like WriteUnifiedDiff but returns the diff a string.
905-
func GetUnifiedDiffString(diff UnifiedDiff) (string, error) {
910+
func GetUnifiedDiffString(diff LineDiffParams) (string, error) {
906911
w := &bytes.Buffer{}
907912
err := WriteUnifiedDiff(w, diff)
908913
return string(w.Bytes()), err
@@ -922,7 +927,9 @@ func formatRangeContext(start, stop int) string {
922927
return fmt.Sprintf("%d,%d", beginning, beginning+length-1)
923928
}
924929

925-
type ContextDiff UnifiedDiff
930+
// For backward compatibility. Ugh.
931+
type ContextDiff = LineDiffParams
932+
type UnifiedDiff = LineDiffParams
926933

927934
// Compare two sequences of lines; generate the delta as a context diff.
928935
//
@@ -941,7 +948,7 @@ type ContextDiff UnifiedDiff
941948
// strings for diff.FromFile, diff.ToFile, diff.FromDate, diff.ToDate.
942949
// The modification times are normally expressed in the ISO 8601 format.
943950
// If not specified, the strings default to blanks.
944-
func WriteContextDiff(writer io.Writer, diff ContextDiff) error {
951+
func WriteContextDiff(writer io.Writer, diff LineDiffParams) error {
945952
buf := bufio.NewWriter(writer)
946953
defer buf.Flush()
947954
var diffErr error
@@ -971,6 +978,9 @@ func WriteContextDiff(writer io.Writer, diff ContextDiff) error {
971978

972979
started := false
973980
m := NewMatcher(diff.A, diff.B)
981+
if diff.AutoJunk || diff.IsJunkLine != nil {
982+
m = NewMatcherWithJunk(diff.A, diff.B, diff.AutoJunk, diff.IsJunkLine)
983+
}
974984
for _, g := range m.GetGroupedOpCodes(diff.Context) {
975985
if !started {
976986
started = true
@@ -1026,15 +1036,15 @@ func WriteContextDiff(writer io.Writer, diff ContextDiff) error {
10261036
return diffErr
10271037
}
10281038

1029-
// Like WriteContextDiff but returns the diff a string.
1030-
func GetContextDiffString(diff ContextDiff) (string, error) {
1039+
// Like WriteContextDiff but returns the diff as a string.
1040+
func GetContextDiffString(diff LineDiffParams) (string, error) {
10311041
w := &bytes.Buffer{}
10321042
err := WriteContextDiff(w, diff)
10331043
return string(w.Bytes()), err
10341044
}
10351045

10361046
// Split a string on "\n" while preserving them. The output can be used
1037-
// as input for UnifiedDiff and ContextDiff structures.
1047+
// as input for LineDiffParams.
10381048
func SplitLines(s string) []string {
10391049
lines := strings.SplitAfter(s, "\n")
10401050
lines[len(lines)-1] += "\n"

difflib/difflib_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ fmt.Printf("%s,%T",a,b)`
121121
one
122122
three
123123
four`
124-
diff := UnifiedDiff{
124+
diff := LineDiffParams{
125125
A: SplitLines(a),
126126
B: SplitLines(b),
127127
FromFile: "Original",
@@ -274,7 +274,7 @@ func TestSFBugsRatioForNullSeqn(t *testing.T) {
274274
func TestSFBugsComparingEmptyLists(t *testing.T) {
275275
groups := NewMatcher(nil, nil).GetGroupedOpCodes(-1)
276276
assertEqual(t, len(groups), 0)
277-
diff := UnifiedDiff{
277+
diff := LineDiffParams{
278278
FromFile: "Original",
279279
ToFile: "Current",
280280
Context: 3,
@@ -326,7 +326,7 @@ func TestOutputFormatRangeFormatContext(t *testing.T) {
326326
}
327327

328328
func TestOutputFormatTabDelimiter(t *testing.T) {
329-
diff := UnifiedDiff{
329+
diff := LineDiffParams{
330330
A: splitChars("one"),
331331
B: splitChars("two"),
332332
FromFile: "Original",
@@ -350,7 +350,7 @@ func TestOutputFormatTabDelimiter(t *testing.T) {
350350
}
351351

352352
func TestOutputFormatNoTrailingTabOnEmptyFiledate(t *testing.T) {
353-
diff := UnifiedDiff{
353+
diff := LineDiffParams{
354354
A: splitChars("one"),
355355
B: splitChars("two"),
356356
FromFile: "Original",
@@ -367,7 +367,7 @@ func TestOutputFormatNoTrailingTabOnEmptyFiledate(t *testing.T) {
367367
}
368368

369369
func TestOmitFilenames(t *testing.T) {
370-
diff := UnifiedDiff{
370+
diff := LineDiffParams{
371371
A: SplitLines("o\nn\ne\n"),
372372
B: SplitLines("t\nw\no\n"),
373373
Eol: "\n",
@@ -600,7 +600,7 @@ func TestGetUnifiedDiffString(t *testing.T) {
600600
A := "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine\nten\n"
601601
B := "one\ntwo\nthr33\nfour\nfive\nsix\nseven\neight\nnine\nten\n"
602602
// Build diff
603-
diff := UnifiedDiff{A: SplitLines(A),
603+
diff := LineDiffParams{A: SplitLines(A),
604604
FromFile: "file", FromDate: "then",
605605
B: SplitLines(B),
606606
ToFile: "tile", ToDate: "now", Eol: "", Context: 1}

0 commit comments

Comments
 (0)