Skip to content

Commit c781559

Browse files
committed
Changed WriteUnifiedDiff() to use strings.Builder.
1 parent 68f206a commit c781559

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

difflib/difflib.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -816,14 +816,16 @@ type UnifiedDiff struct {
816816
// 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'.
817817
// The modification times are normally expressed in the ISO 8601 format.
818818
func WriteUnifiedDiff(writer io.Writer, diff UnifiedDiff) error {
819-
buf := bufio.NewWriter(writer)
820-
defer buf.Flush()
819+
//buf := bufio.NewWriter(writer)
820+
//defer buf.Flush()
821+
var bld strings.Builder
822+
bld.Reset()
821823
wf := func(format string, args ...interface{}) error {
822-
_, err := buf.WriteString(fmt.Sprintf(format, args...))
824+
_, err := fmt.Fprintf(&bld, format, args...)
823825
return err
824826
}
825827
ws := func(s string) error {
826-
_, err := buf.WriteString(s)
828+
_, err := bld.WriteString(s)
827829
return err
828830
}
829831

@@ -887,6 +889,9 @@ func WriteUnifiedDiff(writer io.Writer, diff UnifiedDiff) error {
887889
}
888890
}
889891
}
892+
buf := bufio.NewWriter(writer)
893+
buf.WriteString(bld.String())
894+
buf.Flush()
890895
return nil
891896
}
892897

0 commit comments

Comments
 (0)