Commit 39493be2 authored by David Symonds's avatar David Symonds

io: rename Copyn to CopyN.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/5157045
parent f18e4e44
...@@ -13,6 +13,7 @@ GOFILES=\ ...@@ -13,6 +13,7 @@ GOFILES=\
httpheaders.go\ httpheaders.go\
httpserver.go\ httpserver.go\
imagenew.go\ imagenew.go\
iocopyn.go\
main.go\ main.go\
math.go\ math.go\
netdial.go\ netdial.go\
......
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"go/ast"
)
var ioCopyNFix = fix{
"iocopyn",
ioCopyN,
`Rename io.Copyn to io.CopyN.
http://codereview.appspot.com/5157045
`,
}
func init() {
register(ioCopyNFix)
}
func ioCopyN(f *ast.File) bool {
if !imports(f, "io") {
return false
}
fixed := false
walk(f, func(n interface{}) {
if expr, ok := n.(ast.Expr); ok {
if isPkgDot(expr, "io", "Copyn") {
expr.(*ast.SelectorExpr).Sel.Name = "CopyN"
fixed = true
return
}
}
})
return fixed
}
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
func init() {
addTestCases(ioCopyNTests)
}
var ioCopyNTests = []testCase{
{
Name: "io.CopyN.0",
In: `package main
import (
"io"
)
func f() {
io.Copyn(dst, src)
foo.Copyn(dst, src)
}
`,
Out: `package main
import (
"io"
)
func f() {
io.CopyN(dst, src)
foo.Copyn(dst, src)
}
`,
},
}
...@@ -94,7 +94,7 @@ func (tr *Reader) skipUnread() { ...@@ -94,7 +94,7 @@ func (tr *Reader) skipUnread() {
return return
} }
} }
_, tr.err = io.Copyn(ioutil.Discard, tr.r, nr) _, tr.err = io.CopyN(ioutil.Discard, tr.r, nr)
} }
func (tr *Reader) verifyChecksum(header []byte) bool { func (tr *Reader) verifyChecksum(header []byte) bool {
......
...@@ -219,7 +219,7 @@ func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirec ...@@ -219,7 +219,7 @@ func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirec
w.WriteHeader(code) w.WriteHeader(code)
if r.Method != "HEAD" { if r.Method != "HEAD" {
io.Copyn(w, f, size) io.CopyN(w, f, size)
} }
} }
......
...@@ -474,7 +474,7 @@ func TestTransportGzip(t *testing.T) { ...@@ -474,7 +474,7 @@ func TestTransportGzip(t *testing.T) {
gz, _ := gzip.NewWriter(w) gz, _ := gzip.NewWriter(w)
gz.Write([]byte(testString)) gz.Write([]byte(testString))
if req.FormValue("body") == "large" { if req.FormValue("body") == "large" {
io.Copyn(gz, rand.Reader, nRandBytes) io.CopyN(gz, rand.Reader, nRandBytes)
} }
gz.Close() gz.Close()
})) }))
......
...@@ -256,15 +256,15 @@ func ReadFull(r Reader, buf []byte) (n int, err os.Error) { ...@@ -256,15 +256,15 @@ func ReadFull(r Reader, buf []byte) (n int, err os.Error) {
return ReadAtLeast(r, buf, len(buf)) return ReadAtLeast(r, buf, len(buf))
} }
// Copyn copies n bytes (or until an error) from src to dst. // CopyN copies n bytes (or until an error) from src to dst.
// It returns the number of bytes copied and the earliest // It returns the number of bytes copied and the earliest
// error encountered while copying. Because Read can // error encountered while copying. Because Read can
// return the full amount requested as well as an error // return the full amount requested as well as an error
// (including os.EOF), so can Copyn. // (including os.EOF), so can CopyN.
// //
// If dst implements the ReaderFrom interface, // If dst implements the ReaderFrom interface,
// the copy is implemented by calling dst.ReadFrom(src). // the copy is implemented by calling dst.ReadFrom(src).
func Copyn(dst Writer, src Reader, n int64) (written int64, err os.Error) { func CopyN(dst Writer, src Reader, n int64) (written int64, err os.Error) {
// If the writer has a ReadFrom method, use it to do the copy. // If the writer has a ReadFrom method, use it to do the copy.
// Avoids a buffer allocation and a copy. // Avoids a buffer allocation and a copy.
if rt, ok := dst.(ReaderFrom); ok { if rt, ok := dst.(ReaderFrom); ok {
......
...@@ -19,7 +19,7 @@ type Buffer struct { ...@@ -19,7 +19,7 @@ type Buffer struct {
WriterTo // conflicts with and hides bytes.Buffer's WriterTo. WriterTo // conflicts with and hides bytes.Buffer's WriterTo.
} }
// Simple tests, primarily to verify the ReadFrom and WriteTo callouts inside Copy and Copyn. // Simple tests, primarily to verify the ReadFrom and WriteTo callouts inside Copy and CopyN.
func TestCopy(t *testing.T) { func TestCopy(t *testing.T) {
rb := new(Buffer) rb := new(Buffer)
...@@ -51,33 +51,33 @@ func TestCopyWriteTo(t *testing.T) { ...@@ -51,33 +51,33 @@ func TestCopyWriteTo(t *testing.T) {
} }
} }
func TestCopyn(t *testing.T) { func TestCopyN(t *testing.T) {
rb := new(Buffer) rb := new(Buffer)
wb := new(Buffer) wb := new(Buffer)
rb.WriteString("hello, world.") rb.WriteString("hello, world.")
Copyn(wb, rb, 5) CopyN(wb, rb, 5)
if wb.String() != "hello" { if wb.String() != "hello" {
t.Errorf("Copyn did not work properly") t.Errorf("CopyN did not work properly")
} }
} }
func TestCopynReadFrom(t *testing.T) { func TestCopyNReadFrom(t *testing.T) {
rb := new(Buffer) rb := new(Buffer)
wb := new(bytes.Buffer) // implements ReadFrom. wb := new(bytes.Buffer) // implements ReadFrom.
rb.WriteString("hello") rb.WriteString("hello")
Copyn(wb, rb, 5) CopyN(wb, rb, 5)
if wb.String() != "hello" { if wb.String() != "hello" {
t.Errorf("Copyn did not work properly") t.Errorf("CopyN did not work properly")
} }
} }
func TestCopynWriteTo(t *testing.T) { func TestCopyNWriteTo(t *testing.T) {
rb := new(bytes.Buffer) // implements WriteTo. rb := new(bytes.Buffer) // implements WriteTo.
wb := new(Buffer) wb := new(Buffer)
rb.WriteString("hello, world.") rb.WriteString("hello, world.")
Copyn(wb, rb, 5) CopyN(wb, rb, 5)
if wb.String() != "hello" { if wb.String() != "hello" {
t.Errorf("Copyn did not work properly") t.Errorf("CopyN did not work properly")
} }
} }
...@@ -89,30 +89,30 @@ func (w *noReadFrom) Write(p []byte) (n int, err os.Error) { ...@@ -89,30 +89,30 @@ func (w *noReadFrom) Write(p []byte) (n int, err os.Error) {
return w.w.Write(p) return w.w.Write(p)
} }
func TestCopynEOF(t *testing.T) { func TestCopyNEOF(t *testing.T) {
// Test that EOF behavior is the same regardless of whether // Test that EOF behavior is the same regardless of whether
// argument to Copyn has ReadFrom. // argument to CopyN has ReadFrom.
b := new(bytes.Buffer) b := new(bytes.Buffer)
n, err := Copyn(&noReadFrom{b}, strings.NewReader("foo"), 3) n, err := CopyN(&noReadFrom{b}, strings.NewReader("foo"), 3)
if n != 3 || err != nil { if n != 3 || err != nil {
t.Errorf("Copyn(noReadFrom, foo, 3) = %d, %v; want 3, nil", n, err) t.Errorf("CopyN(noReadFrom, foo, 3) = %d, %v; want 3, nil", n, err)
} }
n, err = Copyn(&noReadFrom{b}, strings.NewReader("foo"), 4) n, err = CopyN(&noReadFrom{b}, strings.NewReader("foo"), 4)
if n != 3 || err != os.EOF { if n != 3 || err != os.EOF {
t.Errorf("Copyn(noReadFrom, foo, 4) = %d, %v; want 3, EOF", n, err) t.Errorf("CopyN(noReadFrom, foo, 4) = %d, %v; want 3, EOF", n, err)
} }
n, err = Copyn(b, strings.NewReader("foo"), 3) // b has read from n, err = CopyN(b, strings.NewReader("foo"), 3) // b has read from
if n != 3 || err != nil { if n != 3 || err != nil {
t.Errorf("Copyn(bytes.Buffer, foo, 3) = %d, %v; want 3, nil", n, err) t.Errorf("CopyN(bytes.Buffer, foo, 3) = %d, %v; want 3, nil", n, err)
} }
n, err = Copyn(b, strings.NewReader("foo"), 4) // b has read from n, err = CopyN(b, strings.NewReader("foo"), 4) // b has read from
if n != 3 || err != os.EOF { if n != 3 || err != os.EOF {
t.Errorf("Copyn(bytes.Buffer, foo, 4) = %d, %v; want 3, EOF", n, err) t.Errorf("CopyN(bytes.Buffer, foo, 4) = %d, %v; want 3, EOF", n, err)
} }
} }
......
...@@ -47,7 +47,7 @@ func (r *Reader) ReadForm(maxMemory int64) (f *Form, err os.Error) { ...@@ -47,7 +47,7 @@ func (r *Reader) ReadForm(maxMemory int64) (f *Form, err os.Error) {
if filename == "" { if filename == "" {
// value, store as string in memory // value, store as string in memory
n, err := io.Copyn(&b, p, maxValueBytes) n, err := io.CopyN(&b, p, maxValueBytes)
if err != nil && err != os.EOF { if err != nil && err != os.EOF {
return nil, err return nil, err
} }
...@@ -64,7 +64,7 @@ func (r *Reader) ReadForm(maxMemory int64) (f *Form, err os.Error) { ...@@ -64,7 +64,7 @@ func (r *Reader) ReadForm(maxMemory int64) (f *Form, err os.Error) {
Filename: filename, Filename: filename,
Header: p.Header, Header: p.Header,
} }
n, err := io.Copyn(&b, p, maxMemory+1) n, err := io.CopyN(&b, p, maxMemory+1)
if err != nil && err != os.EOF { if err != nil && err != os.EOF {
return nil, err return nil, err
} }
......
...@@ -146,7 +146,7 @@ func (bp *Part) Read(p []byte) (n int, err os.Error) { ...@@ -146,7 +146,7 @@ func (bp *Part) Read(p []byte) (n int, err os.Error) {
return 0, io.ErrUnexpectedEOF return 0, io.ErrUnexpectedEOF
} }
if nCopy > 0 { if nCopy > 0 {
if _, err := io.Copyn(bp.buffer, bp.mr.bufReader, int64(nCopy)); err != nil { if _, err := io.CopyN(bp.buffer, bp.mr.bufReader, int64(nCopy)); err != nil {
return 0, err return 0, err
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment