From 5325d0140732d8287c5bb102ea8fbfc4fb0ef83f Mon Sep 17 00:00:00 2001 From: Chen Yufei Date: Wed, 3 Jun 2015 00:47:10 +0800 Subject: [PATCH] Remove unused function copyNWithBuf. --- util.go | 61 ---------------------------------------------------- util_test.go | 54 ---------------------------------------------- 2 files changed, 115 deletions(-) diff --git a/util.go b/util.go index b3e3134..03ae87a 100644 --- a/util.go +++ b/util.go @@ -281,67 +281,6 @@ func copyN(dst io.Writer, src *bufio.Reader, n, rdSize int) (err error) { return err } -// copyNWithBuf copys N bytes from src to dst, using the specified buf as buffer. pre and -// end are written to w before and after the n bytes. copyN will try to -// minimize number of writes. -// No longer used now. -func copyNWithBuf(dst io.Writer, src io.Reader, n int, buf, pre, end []byte) (err error) { - // XXX well, this is complicated in order to save writes - var nn int - bufLen := len(buf) - var b []byte - for n != 0 { - if pre != nil { - if len(pre) >= bufLen { - // pre is larger than bufLen, can't save write operation here - if _, err = dst.Write(pre); err != nil { - return - } - pre = nil - continue - } - // append pre to buf to save one write - copy(buf, pre) - if len(pre)+n < bufLen { - // only need to read n bytes - b = buf[len(pre) : len(pre)+n] - } else { - b = buf[len(pre):] - } - } else { - if n < bufLen { - b = buf[:n] - } else { - b = buf - } - } - if nn, err = src.Read(b); err != nil { - return - } - n -= nn - if pre != nil { - // nn is how much we need to write next - nn += len(pre) - pre = nil - } - // see if we can append end in buffer to save one write - if n == 0 && end != nil && nn+len(end) <= bufLen { - copy(buf[nn:], end) - nn += len(end) - end = nil - } - if _, err = dst.Write(buf[:nn]); err != nil { - return - } - } - if end != nil { - if _, err = dst.Write(end); err != nil { - return - } - } - return -} - func md5sum(ss ...string) string { h := md5.New() for _, s := range ss { diff --git a/util_test.go b/util_test.go index 031374d..42a623d 100644 --- a/util_test.go +++ b/util_test.go @@ -202,60 +202,6 @@ func TestCopyN(t *testing.T) { } } -func TestCopyNWithBuf(t *testing.T) { - testStr := "hello world" - src := bytes.NewBufferString(testStr) - dst := new(bytes.Buffer) - buf := make([]byte, 5) - - copyNWithBuf(dst, src, len(testStr), buf, nil, nil) - if dst.String() != "hello world" { - t.Error("copy without pre and end failed, got:", dst.String()) - } - - src.Reset() - dst.Reset() - src.WriteString(testStr) - copyNWithBuf(dst, src, len(testStr), buf, []byte("by cyf "), nil) - if dst.String() != "by cyf hello world" { - t.Error("copy with pre no end failed, got:", dst.String()) - } - - src.Reset() - dst.Reset() - src.WriteString(testStr) - copyNWithBuf(dst, src, len(testStr), buf, []byte("by cyf "), []byte(" welcome")) - if dst.String() != "by cyf hello world welcome" { - t.Error("copy with both pre and end failed, got:", dst.String()) - } - - src.Reset() - dst.Reset() - src.WriteString(testStr) - copyNWithBuf(dst, src, len(testStr), buf, []byte("pre longer then buffer "), []byte(" welcome")) - if dst.String() != "pre longer then buffer hello world welcome" { - t.Error("copy with long pre failed, got:", dst.String()) - } - - src.Reset() - dst.Reset() - testStr = "34" - src.WriteString(testStr) - copyNWithBuf(dst, src, len(testStr), buf, []byte("12"), []byte(" welcome")) - if dst.String() != "1234 welcome" { - t.Error("copy len(pre)+size