Skip to content

Commit 017ffa6

Browse files
committed
fix WriteContextDiff fmt.Sprintf format error
1 parent 901e4f8 commit 017ffa6

File tree

2 files changed

+20
-51
lines changed

2 files changed

+20
-51
lines changed

difflib/difflib.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -673,13 +673,17 @@ func WriteContextDiff(writer io.Writer, diff ContextDiff) error {
673673
buf := bufio.NewWriter(writer)
674674
defer buf.Flush()
675675
var diffErr error
676-
w := func(format string, args ...interface{}) {
676+
wf := func(format string, args ...interface{}) {
677677
_, err := buf.WriteString(fmt.Sprintf(format, args...))
678678
if diffErr == nil && err != nil {
679679
diffErr = err
680680
}
681681
}
682-
682+
ws := func(s string) error {
683+
_, err := buf.WriteString(s)
684+
return err
685+
}
686+
683687
if len(diff.Eol) == 0 {
684688
diff.Eol = "\n"
685689
}
@@ -704,39 +708,39 @@ func WriteContextDiff(writer io.Writer, diff ContextDiff) error {
704708
if len(diff.ToDate) > 0 {
705709
toDate = "\t" + diff.ToDate
706710
}
707-
w("*** %s%s%s", diff.FromFile, fromDate, diff.Eol)
708-
w("--- %s%s%s", diff.ToFile, toDate, diff.Eol)
711+
wf("*** %s%s%s", diff.FromFile, fromDate, diff.Eol)
712+
wf("--- %s%s%s", diff.ToFile, toDate, diff.Eol)
709713
}
710714

711715
first, last := g[0], g[len(g)-1]
712-
w("***************" + diff.Eol)
716+
wf("***************" + diff.Eol)
713717

714718
range1 := formatRangeContext(first.I1, last.I2)
715-
w("*** %s ****%s", range1, diff.Eol)
719+
wf("*** %s ****%s", range1, diff.Eol)
716720
for _, c := range g {
717721
if c.Tag == 'r' || c.Tag == 'd' {
718722
for _, cc := range g {
719723
if cc.Tag == 'i' {
720724
continue
721725
}
722726
for _, line := range diff.A[cc.I1:cc.I2] {
723-
w(prefix[cc.Tag] + line)
727+
ws(prefix[cc.Tag] + line)
724728
}
725729
}
726730
break
727731
}
728732
}
729733

730734
range2 := formatRangeContext(first.J1, last.J2)
731-
w("--- %s ----%s", range2, diff.Eol)
735+
wf("--- %s ----%s", range2, diff.Eol)
732736
for _, c := range g {
733737
if c.Tag == 'r' || c.Tag == 'i' {
734738
for _, cc := range g {
735739
if cc.Tag == 'd' {
736740
continue
737741
}
738742
for _, line := range diff.B[cc.J1:cc.J2] {
739-
w(prefix[cc.Tag] + line)
743+
ws(prefix[cc.Tag] + line)
740744
}
741745
}
742746
break

difflib/difflib_test.go

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -107,43 +107,7 @@ func ExampleGetUnifiedDiffCode() {
107107
two
108108
three
109109
four
110-
fmt.Printf("%s,%T",a,b)
111-
`
112-
b := `zero
113-
one
114-
three
115-
four`
116-
diff := UnifiedDiff{
117-
A: SplitLines(a),
118-
B: SplitLines(b),
119-
FromFile: "Original",
120-
FromDate: "2005-01-26 23:30:50",
121-
ToFile: "Current",
122-
ToDate: "2010-04-02 10:20:52",
123-
Context: 3,
124-
}
125-
result, _ := GetUnifiedDiffString(diff)
126-
fmt.Println(strings.Replace(result, "\t", " ", -1))
127-
// Output:
128-
// --- Original 2005-01-26 23:30:50
129-
// +++ Current 2010-04-02 10:20:52
130-
// @@ -1,6 +1,4 @@
131-
// +zero
132-
// one
133-
// -two
134-
// three
135-
// four
136-
// -fmt.Printf("%s,%T",a,b)
137-
// -
138-
}
139-
140-
func ExampleGetUnifiedDiffString() {
141-
a := `one
142-
two
143-
three
144-
four
145-
fmt.Printf("%s,%T",a,b)
146-
`
110+
fmt.Printf("%s,%T",a,b)`
147111
b := `zero
148112
one
149113
three
@@ -162,21 +126,21 @@ four`
162126
// Output:
163127
// --- Original 2005-01-26 23:30:50
164128
// +++ Current 2010-04-02 10:20:52
165-
// @@ -1,6 +1,4 @@
129+
// @@ -1,5 +1,4 @@
166130
// +zero
167131
// one
168132
// -two
169133
// three
170134
// four
171135
// -fmt.Printf("%s,%T",a,b)
172-
// -
173136
}
174137

175138
func ExampleGetContextDiffCode() {
176139
a := `one
177140
two
178141
three
179-
four`
142+
four
143+
fmt.Printf("%s,%T",a,b)`
180144
b := `zero
181145
one
182146
tree
@@ -190,16 +154,17 @@ four`
190154
Eol: "\n",
191155
}
192156
result, _ := GetContextDiffString(diff)
193-
fmt.Printf(strings.Replace(result, "\t", " ", -1))
157+
fmt.Print(strings.Replace(result, "\t", " ", -1))
194158
// Output:
195159
// *** Original
196160
// --- Current
197161
// ***************
198-
// *** 1,4 ****
162+
// *** 1,5 ****
199163
// one
200164
// ! two
201165
// ! three
202166
// four
167+
// - fmt.Printf("%s,%T",a,b)
203168
// --- 1,4 ----
204169
// + zero
205170
// one

0 commit comments

Comments
 (0)