Skip to content

Commit a9578fa

Browse files
dmitshurpull[bot]
authored andcommitted
cmd/dist: copy trailing text more directly in testJSONFilter.process
Use io.Copy¹ that matches the comment more closely, avoids the possibility of needing a bigger array, and is slightly shorter. Its downside is that it takes two w.Write calls instead of one. ¹ Admittedly, it was temping to use io.CopyBuffer since the 'data' byte slice becomes a viable buffer after its contents are written. I resisted that temptation for two reasons. One, it would need the io.Reader returned by dec.Buffered() (currently a *bytes.Reader) to not implement the io.WriterTo interface for any chance of making a positive difference. This seems not very likely. Two, to avoid burdening anyone with determining that io.CopyBuffer won't panic without 'if len(data) == 0 && data != nil { data = nil }' because json.Marshal never returns an empty but non-nil byte slice. Change-Id: I33c53d9d990f6ee79cd3ab90f12e3b575b9ebe72 Reviewed-on: https://go-review.googlesource.com/c/go/+/497736 Reviewed-by: Austin Clements <[email protected]> Auto-Submit: Dmitri Shuralyov <[email protected]> TryBot-Bypass: Dmitri Shuralyov <[email protected]> Run-TryBot: Dmitri Shuralyov <[email protected]>
1 parent a35b42b commit a9578fa

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

src/cmd/dist/testjson.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,10 @@ func (f *testJSONFilter) process(line []byte) {
100100
// Should never happen.
101101
panic(fmt.Sprintf("failed to round-trip JSON %q: %s", string(line), err))
102102
}
103+
f.w.Write(data)
103104
// Copy any trailing text. We expect at most a "\n" here, but
104105
// there could be other text and we want to feed that through.
105-
extra, _ := io.ReadAll(dec.Buffered())
106-
data = append(data, extra...)
107-
f.w.Write(data)
106+
io.Copy(f.w, dec.Buffered())
108107
return
109108
}
110109
}

0 commit comments

Comments
 (0)