From d636d7907c46b728b07b58669ec1fa1158105579 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Tue, 5 Apr 2016 15:43:07 -0700 Subject: bytes: add ContainsAny This function is present in the strings package but missing from bytes, and we would like to keep the two packages consistent. Add it to bytes, and copy the test over as well. Fixes #15140 Change-Id: I5dbd28da83a9fe741885794ed15f2af2f826cb3c Reviewed-on: https://go-review.googlesource.com/21562 Reviewed-by: Brad Fitzpatrick --- src/bytes/bytes.go | 5 +++++ src/bytes/bytes_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) (limited to 'src/bytes') diff --git a/src/bytes/bytes.go b/src/bytes/bytes.go index 8a4409cb6b..698d881c9d 100644 --- a/src/bytes/bytes.go +++ b/src/bytes/bytes.go @@ -83,6 +83,11 @@ func Contains(b, subslice []byte) bool { return Index(b, subslice) != -1 } +// ContainsAny reports whether any of the UTF-8-encoded Unicode code points in chars are within b. +func ContainsAny(b []byte, chars string) bool { + return IndexAny(b, chars) >= 0 +} + // Index returns the index of the first instance of sep in s, or -1 if sep is not present in s. func Index(s, sep []byte) int { n := len(sep) diff --git a/src/bytes/bytes_test.go b/src/bytes/bytes_test.go index 1be29d6cc6..40e8d09b59 100644 --- a/src/bytes/bytes_test.go +++ b/src/bytes/bytes_test.go @@ -1218,6 +1218,33 @@ func TestContains(t *testing.T) { } } +var ContainsAnyTests = []struct { + b []byte + substr string + expected bool +}{ + {[]byte(""), "", false}, + {[]byte(""), "a", false}, + {[]byte(""), "abc", false}, + {[]byte("a"), "", false}, + {[]byte("a"), "a", true}, + {[]byte("aaa"), "a", true}, + {[]byte("abc"), "xyz", false}, + {[]byte("abc"), "xcz", true}, + {[]byte("a☺b☻c☹d"), "uvw☻xyz", true}, + {[]byte("aRegExp*"), ".(|)*+?^$[]", true}, + {[]byte(dots + dots + dots), " ", false}, +} + +func TestContainsAny(t *testing.T) { + for _, ct := range ContainsAnyTests { + if ContainsAny(ct.b, ct.substr) != ct.expected { + t.Errorf("ContainsAny(%s, %s) = %v, want %v", + ct.b, ct.substr, !ct.expected, ct.expected) + } + } +} + var makeFieldsInput = func() []byte { x := make([]byte, 1<<20) // Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space. -- cgit v1.3 From e88f89028a55acf9c8b76b7f6ca284c3f9eb4cbd Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Thu, 31 Mar 2016 16:05:23 -0700 Subject: bytes, string: add Reset method to Reader Currently, there is no easy allocation-free way to turn a []byte or string into an io.Reader. Thus, we add a Reset method to bytes.Reader and strings.Reader to allow the reuse of these Readers with another []byte or string. This is consistent with the fact that many standard library io.Readers already support a Reset method of some type: bufio.Reader flate.Reader gzip.Reader zlib.Reader debug/dwarf.LineReader bytes.Buffer crypto/rc4.Cipher Fixes #15033 Change-Id: I456fd1af77af6ef0b4ac6228b058ac1458ff3d19 Reviewed-on: https://go-review.googlesource.com/21386 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/bytes/reader.go | 3 +++ src/bytes/reader_test.go | 20 ++++++++++++++++++++ src/strings/reader.go | 3 +++ src/strings/reader_test.go | 20 ++++++++++++++++++++ 4 files changed, 46 insertions(+) (limited to 'src/bytes') diff --git a/src/bytes/reader.go b/src/bytes/reader.go index 5941ebdab4..7aa30578b3 100644 --- a/src/bytes/reader.go +++ b/src/bytes/reader.go @@ -146,5 +146,8 @@ func (r *Reader) WriteTo(w io.Writer) (n int64, err error) { return } +// Reset resets the Reader to be reading from b. +func (r *Reader) Reset(b []byte) { *r = Reader{b, 0, -1} } + // NewReader returns a new Reader reading from b. func NewReader(b []byte) *Reader { return &Reader{b, 0, -1} } diff --git a/src/bytes/reader_test.go b/src/bytes/reader_test.go index b929a28260..add985d57e 100644 --- a/src/bytes/reader_test.go +++ b/src/bytes/reader_test.go @@ -256,3 +256,23 @@ func TestReaderLenSize(t *testing.T) { t.Errorf("Size = %d; want 3", r.Size()) } } + +func TestReaderReset(t *testing.T) { + r := NewReader([]byte("世界")) + if _, _, err := r.ReadRune(); err != nil { + t.Errorf("ReadRune: unexpected error: %v", err) + } + + const want = "abcdef" + r.Reset([]byte(want)) + if err := r.UnreadRune(); err == nil { + t.Errorf("UnreadRune: expected error, got nil") + } + buf, err := ioutil.ReadAll(r) + if err != nil { + t.Errorf("ReadAll: unexpected error: %v", err) + } + if got := string(buf); got != want { + t.Errorf("ReadAll: got %q, want %q", got, want) + } +} diff --git a/src/strings/reader.go b/src/strings/reader.go index 248e55245c..737873c099 100644 --- a/src/strings/reader.go +++ b/src/strings/reader.go @@ -145,6 +145,9 @@ func (r *Reader) WriteTo(w io.Writer) (n int64, err error) { return } +// Reset resets the Reader to be reading from s. +func (r *Reader) Reset(s string) { *r = Reader{s, 0, -1} } + // NewReader returns a new Reader reading from s. // It is similar to bytes.NewBufferString but more efficient and read-only. func NewReader(s string) *Reader { return &Reader{s, 0, -1} } diff --git a/src/strings/reader_test.go b/src/strings/reader_test.go index 5003a37be4..7bca2e89a1 100644 --- a/src/strings/reader_test.go +++ b/src/strings/reader_test.go @@ -170,3 +170,23 @@ func TestReaderLenSize(t *testing.T) { t.Errorf("Size = %d; want 3", r.Size()) } } + +func TestReaderReset(t *testing.T) { + r := strings.NewReader("世界") + if _, _, err := r.ReadRune(); err != nil { + t.Errorf("ReadRune: unexpected error: %v", err) + } + + const want = "abcdef" + r.Reset(want) + if err := r.UnreadRune(); err == nil { + t.Errorf("UnreadRune: expected error, got nil") + } + buf, err := ioutil.ReadAll(r) + if err != nil { + t.Errorf("ReadAll: unexpected error: %v", err) + } + if got := string(buf); got != want { + t.Errorf("ReadAll: got %q, want %q", got, want) + } +} -- cgit v1.3 From 59af53d681845a8b0be2a728ca1b59aee5ad9ea6 Mon Sep 17 00:00:00 2001 From: Emmanuel Odeke Date: Thu, 7 Apr 2016 23:22:30 -0700 Subject: bytes: add ContainsRune Make package bytes consistent with strings by adding missing function ContainsRune. Fixes #15189 Change-Id: Ie09080b389e55bbe070c57aa3bd134053a805423 Reviewed-on: https://go-review.googlesource.com/21710 Run-TryBot: Rob Pike TryBot-Result: Gobot Gobot Reviewed-by: Rob Pike --- src/bytes/bytes.go | 5 +++++ src/bytes/bytes_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) (limited to 'src/bytes') diff --git a/src/bytes/bytes.go b/src/bytes/bytes.go index 698d881c9d..305c85d9f4 100644 --- a/src/bytes/bytes.go +++ b/src/bytes/bytes.go @@ -88,6 +88,11 @@ func ContainsAny(b []byte, chars string) bool { return IndexAny(b, chars) >= 0 } +// ContainsRune reports whether the Unicode code point r is within b. +func ContainsRune(b []byte, r rune) bool { + return IndexRune(b, r) >= 0 +} + // Index returns the index of the first instance of sep in s, or -1 if sep is not present in s. func Index(s, sep []byte) int { n := len(sep) diff --git a/src/bytes/bytes_test.go b/src/bytes/bytes_test.go index 40e8d09b59..620cfd1bce 100644 --- a/src/bytes/bytes_test.go +++ b/src/bytes/bytes_test.go @@ -1245,6 +1245,30 @@ func TestContainsAny(t *testing.T) { } } +var ContainsRuneTests = []struct { + b []byte + r rune + expected bool +}{ + {[]byte(""), 'a', false}, + {[]byte("a"), 'a', true}, + {[]byte("aaa"), 'a', true}, + {[]byte("abc"), 'y', false}, + {[]byte("abc"), 'c', true}, + {[]byte("a☺b☻c☹d"), 'x', false}, + {[]byte("a☺b☻c☹d"), '☻', true}, + {[]byte("aRegExp*"), '*', true}, +} + +func TestContainsRune(t *testing.T) { + for _, ct := range ContainsRuneTests { + if ContainsRune(ct.b, ct.r) != ct.expected { + t.Errorf("ContainsRune(%q, %q) = %v, want %v", + ct.b, ct.r, !ct.expected, ct.expected) + } + } +} + var makeFieldsInput = func() []byte { x := make([]byte, 1<<20) // Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space. -- cgit v1.3 From 381e5eee39edfb3a43e294023957aff05e9ff349 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 13 Apr 2016 04:35:37 +0000 Subject: all: use new io.SeekFoo constants instead of os.SEEK_FOO Automated change. Fixes #15269 Change-Id: I8deb2ac0101d3f7c390467ceb0a1561b72edbb2f Reviewed-on: https://go-review.googlesource.com/21962 Run-TryBot: Brad Fitzpatrick Reviewed-by: Andrew Gerrand TryBot-Result: Gobot Gobot --- src/archive/tar/reader.go | 5 ++--- src/archive/zip/reader.go | 2 +- src/bytes/reader_test.go | 21 ++++++++++----------- src/debug/elf/file.go | 8 ++++---- src/debug/pe/file.go | 8 ++++---- src/net/http/fs.go | 10 +++++----- src/net/sendfile_dragonfly.go | 2 +- src/net/sendfile_freebsd.go | 2 +- src/net/sendfile_solaris.go | 2 +- src/os/exec/exec_test.go | 2 +- src/strings/reader_test.go | 21 ++++++++++----------- 11 files changed, 40 insertions(+), 43 deletions(-) (limited to 'src/bytes') diff --git a/src/archive/tar/reader.go b/src/archive/tar/reader.go index c8cb69a178..741fe0152b 100644 --- a/src/archive/tar/reader.go +++ b/src/archive/tar/reader.go @@ -13,7 +13,6 @@ import ( "io" "io/ioutil" "math" - "os" "strconv" "strings" "time" @@ -523,10 +522,10 @@ func (tr *Reader) skipUnread() error { // io.Seeker, but calling Seek always returns an error and performs // no action. Thus, we try an innocent seek to the current position // to see if Seek is really supported. - pos1, err := sr.Seek(0, os.SEEK_CUR) + pos1, err := sr.Seek(0, io.SeekCurrent) if err == nil { // Seek seems supported, so perform the real Seek. - pos2, err := sr.Seek(dataSkip-1, os.SEEK_CUR) + pos2, err := sr.Seek(dataSkip-1, io.SeekCurrent) if err != nil { tr.err = err return tr.err diff --git a/src/archive/zip/reader.go b/src/archive/zip/reader.go index d741d105dc..f6c3ead3be 100644 --- a/src/archive/zip/reader.go +++ b/src/archive/zip/reader.go @@ -87,7 +87,7 @@ func (z *Reader) init(r io.ReaderAt, size int64) error { z.File = make([]*File, 0, end.directoryRecords) z.Comment = end.comment rs := io.NewSectionReader(r, 0, size) - if _, err = rs.Seek(int64(end.directoryOffset), os.SEEK_SET); err != nil { + if _, err = rs.Seek(int64(end.directoryOffset), io.SeekStart); err != nil { return err } buf := bufio.NewReader(rs) diff --git a/src/bytes/reader_test.go b/src/bytes/reader_test.go index add985d57e..9341cd5b45 100644 --- a/src/bytes/reader_test.go +++ b/src/bytes/reader_test.go @@ -9,7 +9,6 @@ import ( "fmt" "io" "io/ioutil" - "os" "sync" "testing" ) @@ -24,15 +23,15 @@ func TestReader(t *testing.T) { wantpos int64 seekerr string }{ - {seek: os.SEEK_SET, off: 0, n: 20, want: "0123456789"}, - {seek: os.SEEK_SET, off: 1, n: 1, want: "1"}, - {seek: os.SEEK_CUR, off: 1, wantpos: 3, n: 2, want: "34"}, - {seek: os.SEEK_SET, off: -1, seekerr: "bytes.Reader.Seek: negative position"}, - {seek: os.SEEK_SET, off: 1 << 33, wantpos: 1 << 33}, - {seek: os.SEEK_CUR, off: 1, wantpos: 1<<33 + 1}, - {seek: os.SEEK_SET, n: 5, want: "01234"}, - {seek: os.SEEK_CUR, n: 5, want: "56789"}, - {seek: os.SEEK_END, off: -1, n: 1, wantpos: 9, want: "9"}, + {seek: io.SeekStart, off: 0, n: 20, want: "0123456789"}, + {seek: io.SeekStart, off: 1, n: 1, want: "1"}, + {seek: io.SeekCurrent, off: 1, wantpos: 3, n: 2, want: "34"}, + {seek: io.SeekStart, off: -1, seekerr: "bytes.Reader.Seek: negative position"}, + {seek: io.SeekStart, off: 1 << 33, wantpos: 1 << 33}, + {seek: io.SeekCurrent, off: 1, wantpos: 1<<33 + 1}, + {seek: io.SeekStart, n: 5, want: "01234"}, + {seek: io.SeekCurrent, n: 5, want: "56789"}, + {seek: io.SeekEnd, off: -1, n: 1, wantpos: 9, want: "9"}, } for i, tt := range tests { @@ -63,7 +62,7 @@ func TestReader(t *testing.T) { func TestReadAfterBigSeek(t *testing.T) { r := NewReader([]byte("0123456789")) - if _, err := r.Seek(1<<31+5, os.SEEK_SET); err != nil { + if _, err := r.Seek(1<<31+5, io.SeekStart); err != nil { t.Fatal(err) } if n, err := r.Read(make([]byte, 10)); n != 0 || err != io.EOF { diff --git a/src/debug/elf/file.go b/src/debug/elf/file.go index c28a964b73..8fbf23fe5a 100644 --- a/src/debug/elf/file.go +++ b/src/debug/elf/file.go @@ -269,7 +269,7 @@ func NewFile(r io.ReaderAt) (*File, error) { switch f.Class { case ELFCLASS32: hdr := new(Header32) - sr.Seek(0, os.SEEK_SET) + sr.Seek(0, io.SeekStart) if err := binary.Read(sr, f.ByteOrder, hdr); err != nil { return nil, err } @@ -288,7 +288,7 @@ func NewFile(r io.ReaderAt) (*File, error) { shstrndx = int(hdr.Shstrndx) case ELFCLASS64: hdr := new(Header64) - sr.Seek(0, os.SEEK_SET) + sr.Seek(0, io.SeekStart) if err := binary.Read(sr, f.ByteOrder, hdr); err != nil { return nil, err } @@ -315,7 +315,7 @@ func NewFile(r io.ReaderAt) (*File, error) { f.Progs = make([]*Prog, phnum) for i := 0; i < phnum; i++ { off := phoff + int64(i)*int64(phentsize) - sr.Seek(off, os.SEEK_SET) + sr.Seek(off, io.SeekStart) p := new(Prog) switch f.Class { case ELFCLASS32: @@ -359,7 +359,7 @@ func NewFile(r io.ReaderAt) (*File, error) { names := make([]uint32, shnum) for i := 0; i < shnum; i++ { off := shoff + int64(i)*int64(shentsize) - sr.Seek(off, os.SEEK_SET) + sr.Seek(off, io.SeekStart) s := new(Section) switch f.Class { case ELFCLASS32: diff --git a/src/debug/pe/file.go b/src/debug/pe/file.go index 1acc368e1b..c68ff1bdce 100644 --- a/src/debug/pe/file.go +++ b/src/debug/pe/file.go @@ -150,7 +150,7 @@ func NewFile(r io.ReaderAt) (*File, error) { } else { base = int64(0) } - sr.Seek(base, os.SEEK_SET) + sr.Seek(base, io.SeekStart) if err := binary.Read(sr, binary.LittleEndian, &f.FileHeader); err != nil { return nil, err } @@ -161,7 +161,7 @@ func NewFile(r io.ReaderAt) (*File, error) { var ss []byte if f.FileHeader.NumberOfSymbols > 0 { // Get COFF string table, which is located at the end of the COFF symbol table. - sr.Seek(int64(f.FileHeader.PointerToSymbolTable+COFFSymbolSize*f.FileHeader.NumberOfSymbols), os.SEEK_SET) + sr.Seek(int64(f.FileHeader.PointerToSymbolTable+COFFSymbolSize*f.FileHeader.NumberOfSymbols), io.SeekStart) var l uint32 if err := binary.Read(sr, binary.LittleEndian, &l); err != nil { return nil, err @@ -172,7 +172,7 @@ func NewFile(r io.ReaderAt) (*File, error) { } // Process COFF symbol table. - sr.Seek(int64(f.FileHeader.PointerToSymbolTable), os.SEEK_SET) + sr.Seek(int64(f.FileHeader.PointerToSymbolTable), io.SeekStart) aux := uint8(0) for i := 0; i < int(f.FileHeader.NumberOfSymbols); i++ { cs := new(COFFSymbol) @@ -203,7 +203,7 @@ func NewFile(r io.ReaderAt) (*File, error) { } // Read optional header. - sr.Seek(base, os.SEEK_SET) + sr.Seek(base, io.SeekStart) if err := binary.Read(sr, binary.LittleEndian, &f.FileHeader); err != nil { return nil, err } diff --git a/src/net/http/fs.go b/src/net/http/fs.go index 5546d37516..c7a58a61df 100644 --- a/src/net/http/fs.go +++ b/src/net/http/fs.go @@ -121,11 +121,11 @@ func dirList(w ResponseWriter, f File) { // Note that *os.File implements the io.ReadSeeker interface. func ServeContent(w ResponseWriter, req *Request, name string, modtime time.Time, content io.ReadSeeker) { sizeFunc := func() (int64, error) { - size, err := content.Seek(0, os.SEEK_END) + size, err := content.Seek(0, io.SeekEnd) if err != nil { return 0, errSeeker } - _, err = content.Seek(0, os.SEEK_SET) + _, err = content.Seek(0, io.SeekStart) if err != nil { return 0, errSeeker } @@ -166,7 +166,7 @@ func serveContent(w ResponseWriter, r *Request, name string, modtime time.Time, var buf [sniffLen]byte n, _ := io.ReadFull(content, buf[:]) ctype = DetectContentType(buf[:n]) - _, err := content.Seek(0, os.SEEK_SET) // rewind to output whole file + _, err := content.Seek(0, io.SeekStart) // rewind to output whole file if err != nil { Error(w, "seeker can't seek", StatusInternalServerError) return @@ -213,7 +213,7 @@ func serveContent(w ResponseWriter, r *Request, name string, modtime time.Time, // A response to a request for a single range MUST NOT // be sent using the multipart/byteranges media type." ra := ranges[0] - if _, err := content.Seek(ra.start, os.SEEK_SET); err != nil { + if _, err := content.Seek(ra.start, io.SeekStart); err != nil { Error(w, err.Error(), StatusRequestedRangeNotSatisfiable) return } @@ -236,7 +236,7 @@ func serveContent(w ResponseWriter, r *Request, name string, modtime time.Time, pw.CloseWithError(err) return } - if _, err := content.Seek(ra.start, os.SEEK_SET); err != nil { + if _, err := content.Seek(ra.start, io.SeekStart); err != nil { pw.CloseWithError(err) return } diff --git a/src/net/sendfile_dragonfly.go b/src/net/sendfile_dragonfly.go index 17021c3801..d4b825c370 100644 --- a/src/net/sendfile_dragonfly.go +++ b/src/net/sendfile_dragonfly.go @@ -53,7 +53,7 @@ func sendFile(c *netFD, r io.Reader) (written int64, err error, handled bool) { // use the current position of the file -- if you pass it offset 0, it starts // from offset 0. There's no way to tell it "start from current position", so // we have to manage that explicitly. - pos, err := f.Seek(0, os.SEEK_CUR) + pos, err := f.Seek(0, io.SeekCurrent) if err != nil { return 0, err, false } diff --git a/src/net/sendfile_freebsd.go b/src/net/sendfile_freebsd.go index f7a8529560..18cbb27b53 100644 --- a/src/net/sendfile_freebsd.go +++ b/src/net/sendfile_freebsd.go @@ -53,7 +53,7 @@ func sendFile(c *netFD, r io.Reader) (written int64, err error, handled bool) { // use the current position of the file -- if you pass it offset 0, it starts // from offset 0. There's no way to tell it "start from current position", so // we have to manage that explicitly. - pos, err := f.Seek(0, os.SEEK_CUR) + pos, err := f.Seek(0, io.SeekCurrent) if err != nil { return 0, err, false } diff --git a/src/net/sendfile_solaris.go b/src/net/sendfile_solaris.go index 20d2cddeea..add70c3147 100644 --- a/src/net/sendfile_solaris.go +++ b/src/net/sendfile_solaris.go @@ -57,7 +57,7 @@ func sendFile(c *netFD, r io.Reader) (written int64, err error, handled bool) { // use the current position of the file -- if you pass it offset 0, it starts // from offset 0. There's no way to tell it "start from current position", so // we have to manage that explicitly. - pos, err := f.Seek(0, os.SEEK_CUR) + pos, err := f.Seek(0, io.SeekCurrent) if err != nil { return 0, err, false } diff --git a/src/os/exec/exec_test.go b/src/os/exec/exec_test.go index 1f2fd12add..ed2721bb5e 100644 --- a/src/os/exec/exec_test.go +++ b/src/os/exec/exec_test.go @@ -479,7 +479,7 @@ func TestExtraFiles(t *testing.T) { if err != nil { t.Fatalf("Write: %v", err) } - _, err = tf.Seek(0, os.SEEK_SET) + _, err = tf.Seek(0, io.SeekStart) if err != nil { t.Fatalf("Seek: %v", err) } diff --git a/src/strings/reader_test.go b/src/strings/reader_test.go index 7bca2e89a1..6e9d904b9d 100644 --- a/src/strings/reader_test.go +++ b/src/strings/reader_test.go @@ -9,7 +9,6 @@ import ( "fmt" "io" "io/ioutil" - "os" "strings" "sync" "testing" @@ -25,15 +24,15 @@ func TestReader(t *testing.T) { wantpos int64 seekerr string }{ - {seek: os.SEEK_SET, off: 0, n: 20, want: "0123456789"}, - {seek: os.SEEK_SET, off: 1, n: 1, want: "1"}, - {seek: os.SEEK_CUR, off: 1, wantpos: 3, n: 2, want: "34"}, - {seek: os.SEEK_SET, off: -1, seekerr: "strings.Reader.Seek: negative position"}, - {seek: os.SEEK_SET, off: 1 << 33, wantpos: 1 << 33}, - {seek: os.SEEK_CUR, off: 1, wantpos: 1<<33 + 1}, - {seek: os.SEEK_SET, n: 5, want: "01234"}, - {seek: os.SEEK_CUR, n: 5, want: "56789"}, - {seek: os.SEEK_END, off: -1, n: 1, wantpos: 9, want: "9"}, + {seek: io.SeekStart, off: 0, n: 20, want: "0123456789"}, + {seek: io.SeekStart, off: 1, n: 1, want: "1"}, + {seek: io.SeekCurrent, off: 1, wantpos: 3, n: 2, want: "34"}, + {seek: io.SeekStart, off: -1, seekerr: "strings.Reader.Seek: negative position"}, + {seek: io.SeekStart, off: 1 << 33, wantpos: 1 << 33}, + {seek: io.SeekCurrent, off: 1, wantpos: 1<<33 + 1}, + {seek: io.SeekStart, n: 5, want: "01234"}, + {seek: io.SeekCurrent, n: 5, want: "56789"}, + {seek: io.SeekEnd, off: -1, n: 1, wantpos: 9, want: "9"}, } for i, tt := range tests { @@ -64,7 +63,7 @@ func TestReader(t *testing.T) { func TestReadAfterBigSeek(t *testing.T) { r := strings.NewReader("0123456789") - if _, err := r.Seek(1<<31+5, os.SEEK_SET); err != nil { + if _, err := r.Seek(1<<31+5, io.SeekStart); err != nil { t.Fatal(err) } if n, err := r.Read(make([]byte, 10)); n != 0 || err != io.EOF { -- cgit v1.3 From 0da4dbe2322eb3b6224df35ce3e9fc83f104762b Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Thu, 14 Apr 2016 19:09:36 -0700 Subject: all: remove unnecessary type conversions cmd and runtime were handled separately, and I'm intentionally skipped syscall. This is the rest of the standard library. CL generated mechanically with github.com/mdempsky/unconvert. Change-Id: I9e0eff886974dedc37adb93f602064b83e469122 Reviewed-on: https://go-review.googlesource.com/22104 Reviewed-by: Brad Fitzpatrick Run-TryBot: Matthew Dempsky TryBot-Result: Gobot Gobot --- src/archive/tar/reader.go | 8 ++++---- src/archive/tar/writer.go | 2 +- src/bytes/reader.go | 2 +- src/compress/bzip2/bzip2.go | 8 ++++---- src/compress/flate/huffman_bit_writer.go | 2 +- src/compress/flate/reverse_bits.go | 2 +- src/compress/lzw/writer.go | 2 +- src/crypto/des/block.go | 2 +- src/crypto/tls/handshake_messages.go | 4 ++-- src/debug/dwarf/buf.go | 2 +- src/debug/dwarf/line.go | 2 +- src/debug/dwarf/typeunit.go | 4 ++-- src/debug/elf/elf.go | 4 ++-- src/debug/elf/file.go | 32 ++++++++++++++++---------------- src/debug/gosym/pclntab.go | 4 ++-- src/debug/gosym/symtab.go | 4 ++-- src/encoding/asn1/marshal.go | 6 +++--- src/encoding/binary/binary.go | 2 +- src/encoding/gob/encode.go | 2 +- src/go/ast/ast.go | 2 +- src/image/color/ycbcr.go | 18 +++++++++--------- src/image/draw/draw.go | 8 ++++---- src/math/big/float.go | 4 ++-- src/math/big/natconv.go | 2 +- src/math/big/ratconv.go | 2 +- src/net/interface_bsd.go | 4 ++-- src/net/mail/message.go | 2 +- src/net/parse.go | 4 ++-- src/os/exec_windows.go | 2 +- src/os/file_windows.go | 14 +++++++------- src/os/stat_darwin.go | 2 +- src/os/stat_dragonfly.go | 4 ++-- src/os/stat_freebsd.go | 2 +- src/os/stat_linux.go | 2 +- src/os/stat_nacl.go | 2 +- src/os/stat_netbsd.go | 4 ++-- src/os/stat_openbsd.go | 4 ++-- src/os/stat_plan9.go | 2 +- src/os/stat_solaris.go | 4 ++-- src/os/stat_windows.go | 2 +- src/os/types_windows.go | 2 +- src/reflect/type.go | 4 ++-- src/reflect/value.go | 26 +++++++++++++------------- src/regexp/onepass.go | 2 +- src/strconv/extfloat.go | 4 ++-- src/strings/reader.go | 2 +- src/sync/pool.go | 4 ++-- src/time/time.go | 8 ++++---- src/time/zoneinfo_windows.go | 2 +- src/unicode/letter.go | 2 +- 50 files changed, 120 insertions(+), 120 deletions(-) (limited to 'src/bytes') diff --git a/src/archive/tar/reader.go b/src/archive/tar/reader.go index 741fe0152b..b924eeb568 100644 --- a/src/archive/tar/reader.go +++ b/src/archive/tar/reader.go @@ -306,7 +306,7 @@ func mergePAX(hdr *Header, headers map[string]string) error { if err != nil { return err } - hdr.Size = int64(size) + hdr.Size = size default: if strings.HasPrefix(k, paxXattr) { if hdr.Xattrs == nil { @@ -346,7 +346,7 @@ func parsePAXTime(t string) (time.Time, error) { // Right truncate nano_buf = nano_buf[:maxNanoSecondIntSize] } - nanoseconds, err = strconv.ParseInt(string(nano_buf), 10, 0) + nanoseconds, err = strconv.ParseInt(nano_buf, 10, 0) if err != nil { return time.Time{}, err } @@ -378,14 +378,14 @@ func parsePAX(r io.Reader) (map[string]string, error) { } sbuf = residual - keyStr := string(key) + keyStr := key if keyStr == paxGNUSparseOffset || keyStr == paxGNUSparseNumBytes { // GNU sparse format 0.0 special key. Write to sparseMap instead of using the headers map. sparseMap.WriteString(value) sparseMap.Write([]byte{','}) } else { // Normal key. Set the value in the headers map. - headers[keyStr] = string(value) + headers[keyStr] = value } } if sparseMap.Len() != 0 { diff --git a/src/archive/tar/writer.go b/src/archive/tar/writer.go index 600ee4be09..944b2d4952 100644 --- a/src/archive/tar/writer.go +++ b/src/archive/tar/writer.go @@ -278,7 +278,7 @@ func (tw *Writer) writeHeader(hdr *Header, allowPax bool) error { return err } } - tw.nb = int64(hdr.Size) + tw.nb = hdr.Size tw.pad = (blockSize - (tw.nb % blockSize)) % blockSize _, tw.err = tw.w.Write(header) diff --git a/src/bytes/reader.go b/src/bytes/reader.go index 7aa30578b3..aa39890f3b 100644 --- a/src/bytes/reader.go +++ b/src/bytes/reader.go @@ -114,7 +114,7 @@ func (r *Reader) Seek(offset int64, whence int) (int64, error) { case 0: abs = offset case 1: - abs = int64(r.i) + offset + abs = r.i + offset case 2: abs = int64(len(r.s)) + offset default: diff --git a/src/compress/bzip2/bzip2.go b/src/compress/bzip2/bzip2.go index 90e9aebab6..42788443bc 100644 --- a/src/compress/bzip2/bzip2.go +++ b/src/compress/bzip2/bzip2.go @@ -75,7 +75,7 @@ func (bz2 *reader) setup(needMagic bool) error { } bz2.fileCRC = 0 - bz2.blockSize = 100 * 1000 * (int(level) - '0') + bz2.blockSize = 100 * 1000 * (level - '0') if bz2.blockSize > len(bz2.tt) { bz2.tt = make([]uint32, bz2.blockSize) } @@ -293,7 +293,7 @@ func (bz2 *reader) readBlock() (err error) { if c >= numHuffmanTrees { return StructuralError("tree index too large") } - treeIndexes[i] = uint8(mtfTreeDecoder.Decode(c)) + treeIndexes[i] = mtfTreeDecoder.Decode(c) } // The list of symbols for the move-to-front transform is taken from @@ -399,7 +399,7 @@ func (bz2 *reader) readBlock() (err error) { return StructuralError("repeats past end of block") } for i := 0; i < repeat; i++ { - b := byte(mtf.First()) + b := mtf.First() bz2.tt[bufIndex] = uint32(b) bz2.c[b]++ bufIndex++ @@ -420,7 +420,7 @@ func (bz2 *reader) readBlock() (err error) { // it's always referenced with a run-length of 1. Thus 0 // doesn't need to be encoded and we have |v-1| in the next // line. - b := byte(mtf.Decode(int(v - 1))) + b := mtf.Decode(int(v - 1)) if bufIndex >= bz2.blockSize { return StructuralError("data exceeds block size") } diff --git a/src/compress/flate/huffman_bit_writer.go b/src/compress/flate/huffman_bit_writer.go index 23f242f88e..d0206e59cf 100644 --- a/src/compress/flate/huffman_bit_writer.go +++ b/src/compress/flate/huffman_bit_writer.go @@ -436,7 +436,7 @@ func (w *huffmanBitWriter) writeBlock(tokens []token, eof bool, input []byte) { } dynamicHeader := int64(3+5+5+4+(3*numCodegens)) + w.codegenEncoding.bitLength(w.codegenFreq[:]) + - int64(extraBits) + + extraBits + int64(w.codegenFreq[16]*2) + int64(w.codegenFreq[17]*3) + int64(w.codegenFreq[18]*7) diff --git a/src/compress/flate/reverse_bits.go b/src/compress/flate/reverse_bits.go index c1a02720d1..6b222900c1 100644 --- a/src/compress/flate/reverse_bits.go +++ b/src/compress/flate/reverse_bits.go @@ -44,5 +44,5 @@ func reverseUint16(v uint16) uint16 { } func reverseBits(number uint16, bitLength byte) uint16 { - return reverseUint16(number << uint8(16-bitLength)) + return reverseUint16(number << (16 - bitLength)) } diff --git a/src/compress/lzw/writer.go b/src/compress/lzw/writer.go index 7367c29651..6ddb335f31 100644 --- a/src/compress/lzw/writer.go +++ b/src/compress/lzw/writer.go @@ -119,7 +119,7 @@ func (e *encoder) incHi() error { if err := e.write(e, clear); err != nil { return err } - e.width = uint(e.litWidth) + 1 + e.width = e.litWidth + 1 e.hi = clear + 1 e.overflow = clear << 1 for i := range e.table { diff --git a/src/crypto/des/block.go b/src/crypto/des/block.go index 26355a22e7..99338d62a6 100644 --- a/src/crypto/des/block.go +++ b/src/crypto/des/block.go @@ -72,7 +72,7 @@ func init() { for i := 0; i < 4; i++ { for j := 0; j < 16; j++ { f := uint64(sBoxes[s][i][j]) << (4 * (7 - uint(s))) - f = permuteBlock(uint64(f), permutationFunction[:]) + f = permuteBlock(f, permutationFunction[:]) feistelBox[s][16*i+j] = uint32(f) } } diff --git a/src/crypto/tls/handshake_messages.go b/src/crypto/tls/handshake_messages.go index 13d013a594..3f9a63b110 100644 --- a/src/crypto/tls/handshake_messages.go +++ b/src/crypto/tls/handshake_messages.go @@ -214,7 +214,7 @@ func (m *clientHelloMsg) marshal() []byte { z[4] = byte(l) z = z[5:] for _, pointFormat := range m.supportedPoints { - z[0] = byte(pointFormat) + z[0] = pointFormat z = z[1:] } } @@ -589,7 +589,7 @@ func (m *serverHelloMsg) marshal() []byte { z := x[39+len(m.sessionId):] z[0] = uint8(m.cipherSuite >> 8) z[1] = uint8(m.cipherSuite) - z[2] = uint8(m.compressionMethod) + z[2] = m.compressionMethod z = z[3:] if numExtensions > 0 { diff --git a/src/debug/dwarf/buf.go b/src/debug/dwarf/buf.go index 7443043c11..24d266db10 100644 --- a/src/debug/dwarf/buf.go +++ b/src/debug/dwarf/buf.go @@ -157,7 +157,7 @@ func (b *buf) addr() uint64 { case 4: return uint64(b.uint32()) case 8: - return uint64(b.uint64()) + return b.uint64() } b.error("unknown address size") return 0 diff --git a/src/debug/dwarf/line.go b/src/debug/dwarf/line.go index b3b91ade62..ed82feef92 100644 --- a/src/debug/dwarf/line.go +++ b/src/debug/dwarf/line.go @@ -361,7 +361,7 @@ func (r *LineReader) step(entry *LineEntry) bool { // Special opcode [DWARF2 6.2.5.1, DWARF4 6.2.5.1] adjustedOpcode := opcode - r.opcodeBase r.advancePC(adjustedOpcode / r.lineRange) - lineDelta := r.lineBase + int(adjustedOpcode)%r.lineRange + lineDelta := r.lineBase + adjustedOpcode%r.lineRange r.state.Line += lineDelta goto emit } diff --git a/src/debug/dwarf/typeunit.go b/src/debug/dwarf/typeunit.go index ed42547386..652e02d917 100644 --- a/src/debug/dwarf/typeunit.go +++ b/src/debug/dwarf/typeunit.go @@ -76,7 +76,7 @@ func (d *Data) parseTypes(name string, types []byte) error { data: b.bytes(int(n - (b.off - hdroff))), atable: atable, asize: int(asize), - vers: int(vers), + vers: vers, is64: dwarf64, }, toff: Offset(toff), @@ -101,7 +101,7 @@ func (d *Data) sigToType(sig uint64) (Type, error) { b := makeBuf(d, tu, tu.name, tu.off, tu.data) r := &typeUnitReader{d: d, tu: tu, b: b} - t, err := d.readType(tu.name, r, Offset(tu.toff), make(map[Offset]Type), nil) + t, err := d.readType(tu.name, r, tu.toff, make(map[Offset]Type), nil) if err != nil { return nil, err } diff --git a/src/debug/elf/elf.go b/src/debug/elf/elf.go index af881c2495..3f43d4d896 100644 --- a/src/debug/elf/elf.go +++ b/src/debug/elf/elf.go @@ -2060,8 +2060,8 @@ type Rela32 struct { Addend int32 /* Addend. */ } -func R_SYM32(info uint32) uint32 { return uint32(info >> 8) } -func R_TYPE32(info uint32) uint32 { return uint32(info & 0xff) } +func R_SYM32(info uint32) uint32 { return info >> 8 } +func R_TYPE32(info uint32) uint32 { return info & 0xff } func R_INFO32(sym, typ uint32) uint32 { return sym<<8 | typ } // ELF32 Symbol. diff --git a/src/debug/elf/file.go b/src/debug/elf/file.go index 8fbf23fe5a..c173ea9331 100644 --- a/src/debug/elf/file.go +++ b/src/debug/elf/file.go @@ -294,7 +294,7 @@ func NewFile(r io.ReaderAt) (*File, error) { } f.Type = Type(hdr.Type) f.Machine = Machine(hdr.Machine) - f.Entry = uint64(hdr.Entry) + f.Entry = hdr.Entry if v := Version(hdr.Version); v != f.Version { return nil, &FormatError{0, "mismatched ELF version", v} } @@ -341,12 +341,12 @@ func NewFile(r io.ReaderAt) (*File, error) { p.ProgHeader = ProgHeader{ Type: ProgType(ph.Type), Flags: ProgFlag(ph.Flags), - Off: uint64(ph.Off), - Vaddr: uint64(ph.Vaddr), - Paddr: uint64(ph.Paddr), - Filesz: uint64(ph.Filesz), - Memsz: uint64(ph.Memsz), - Align: uint64(ph.Align), + Off: ph.Off, + Vaddr: ph.Vaddr, + Paddr: ph.Paddr, + Filesz: ph.Filesz, + Memsz: ph.Memsz, + Align: ph.Align, } } p.sr = io.NewSectionReader(r, int64(p.Off), int64(p.Filesz)) @@ -374,8 +374,8 @@ func NewFile(r io.ReaderAt) (*File, error) { Addr: uint64(sh.Addr), Offset: uint64(sh.Off), FileSize: uint64(sh.Size), - Link: uint32(sh.Link), - Info: uint32(sh.Info), + Link: sh.Link, + Info: sh.Info, Addralign: uint64(sh.Addralign), Entsize: uint64(sh.Entsize), } @@ -388,13 +388,13 @@ func NewFile(r io.ReaderAt) (*File, error) { s.SectionHeader = SectionHeader{ Type: SectionType(sh.Type), Flags: SectionFlag(sh.Flags), - Offset: uint64(sh.Off), - FileSize: uint64(sh.Size), - Addr: uint64(sh.Addr), - Link: uint32(sh.Link), - Info: uint32(sh.Info), - Addralign: uint64(sh.Addralign), - Entsize: uint64(sh.Entsize), + Offset: sh.Off, + FileSize: sh.Size, + Addr: sh.Addr, + Link: sh.Link, + Info: sh.Info, + Addralign: sh.Addralign, + Entsize: sh.Entsize, } } s.sr = io.NewSectionReader(r, int64(s.Offset), int64(s.FileSize)) diff --git a/src/debug/gosym/pclntab.go b/src/debug/gosym/pclntab.go index 291f102262..e859d5aed5 100644 --- a/src/debug/gosym/pclntab.go +++ b/src/debug/gosym/pclntab.go @@ -207,8 +207,8 @@ func (t *LineTable) go12Funcs() []Func { funcs := make([]Func, n) for i := range funcs { f := &funcs[i] - f.Entry = uint64(t.uintptr(t.functab[2*i*int(t.ptrsize):])) - f.End = uint64(t.uintptr(t.functab[(2*i+2)*int(t.ptrsize):])) + f.Entry = t.uintptr(t.functab[2*i*int(t.ptrsize):]) + f.End = t.uintptr(t.functab[(2*i+2)*int(t.ptrsize):]) info := t.Data[t.uintptr(t.functab[(2*i+1)*int(t.ptrsize):]):] f.LineTable = t f.FrameSize = int(t.binary.Uint32(info[t.ptrsize+2*4:])) diff --git a/src/debug/gosym/symtab.go b/src/debug/gosym/symtab.go index 49e154fd8e..c8fa9a0b38 100644 --- a/src/debug/gosym/symtab.go +++ b/src/debug/gosym/symtab.go @@ -294,8 +294,8 @@ func NewTable(symtab []byte, pcln *LineTable) (*Table, error) { t.Syms = t.Syms[0 : n+1] ts := &t.Syms[n] ts.Type = s.typ - ts.Value = uint64(s.value) - ts.GoType = uint64(s.gotype) + ts.Value = s.value + ts.GoType = s.gotype switch s.typ { default: // rewrite name to use . instead of · (c2 b7) diff --git a/src/encoding/asn1/marshal.go b/src/encoding/asn1/marshal.go index 2b796c4e75..30797ef099 100644 --- a/src/encoding/asn1/marshal.go +++ b/src/encoding/asn1/marshal.go @@ -315,9 +315,9 @@ func marshalUTCTime(out *forkableWriter, t time.Time) (err error) { switch { case 1950 <= year && year < 2000: - err = marshalTwoDigits(out, int(year-1900)) + err = marshalTwoDigits(out, year-1900) case 2000 <= year && year < 2050: - err = marshalTwoDigits(out, int(year-2000)) + err = marshalTwoDigits(out, year-2000) default: return StructuralError{"cannot represent time as UTCTime"} } @@ -435,7 +435,7 @@ func marshalBody(out *forkableWriter, value reflect.Value, params fieldParameter return out.WriteByte(0) } case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return marshalInt64(out, int64(v.Int())) + return marshalInt64(out, v.Int()) case reflect.Struct: t := v.Type() diff --git a/src/encoding/binary/binary.go b/src/encoding/binary/binary.go index ada5768695..46c6add062 100644 --- a/src/encoding/binary/binary.go +++ b/src/encoding/binary/binary.go @@ -269,7 +269,7 @@ func Write(w io.Writer, order ByteOrder, data interface{}) error { case *uint8: b[0] = *v case uint8: - b[0] = byte(v) + b[0] = v case []uint8: bs = v case *int16: diff --git a/src/encoding/gob/encode.go b/src/encoding/gob/encode.go index 2b3a556eac..50cd6adb46 100644 --- a/src/encoding/gob/encode.go +++ b/src/encoding/gob/encode.go @@ -127,7 +127,7 @@ func (state *encoderState) encodeInt(i int64) { } else { x = uint64(i << 1) } - state.encodeUint(uint64(x)) + state.encodeUint(x) } // encOp is the signature of an encoding operator for a given type. diff --git a/src/go/ast/ast.go b/src/go/ast/ast.go index 5ab4283826..cca2d48bbd 100644 --- a/src/go/ast/ast.go +++ b/src/go/ast/ast.go @@ -99,7 +99,7 @@ func (g *CommentGroup) Text() string { } comments := make([]string, len(g.List)) for i, c := range g.List { - comments[i] = string(c.Text) + comments[i] = c.Text } lines := make([]string, 0, 10) // most comments are less than 10 lines diff --git a/src/image/color/ycbcr.go b/src/image/color/ycbcr.go index d2c5b569a7..2e985fece1 100644 --- a/src/image/color/ycbcr.go +++ b/src/image/color/ycbcr.go @@ -237,10 +237,10 @@ func RGBToCMYK(r, g, b uint8) (uint8, uint8, uint8, uint8) { // CMYKToRGB converts a CMYK quadruple to an RGB triple. func CMYKToRGB(c, m, y, k uint8) (uint8, uint8, uint8) { - w := uint32(0xffff - uint32(k)*0x101) - r := uint32(0xffff-uint32(c)*0x101) * w / 0xffff - g := uint32(0xffff-uint32(m)*0x101) * w / 0xffff - b := uint32(0xffff-uint32(y)*0x101) * w / 0xffff + w := 0xffff - uint32(k)*0x101 + r := (0xffff - uint32(c)*0x101) * w / 0xffff + g := (0xffff - uint32(m)*0x101) * w / 0xffff + b := (0xffff - uint32(y)*0x101) * w / 0xffff return uint8(r >> 8), uint8(g >> 8), uint8(b >> 8) } @@ -256,11 +256,11 @@ func (c CMYK) RGBA() (uint32, uint32, uint32, uint32) { // This code is a copy of the CMYKToRGB function above, except that it // returns values in the range [0, 0xffff] instead of [0, 0xff]. - w := uint32(0xffff - uint32(c.K)*0x101) - r := uint32(0xffff-uint32(c.C)*0x101) * w / 0xffff - g := uint32(0xffff-uint32(c.M)*0x101) * w / 0xffff - b := uint32(0xffff-uint32(c.Y)*0x101) * w / 0xffff - return uint32(r), uint32(g), uint32(b), 0xffff + w := 0xffff - uint32(c.K)*0x101 + r := (0xffff - uint32(c.C)*0x101) * w / 0xffff + g := (0xffff - uint32(c.M)*0x101) * w / 0xffff + b := (0xffff - uint32(c.Y)*0x101) * w / 0xffff + return r, g, b, 0xffff } // CMYKModel is the Model for CMYK colors. diff --git a/src/image/draw/draw.go b/src/image/draw/draw.go index 94e3575663..6a16cd39cf 100644 --- a/src/image/draw/draw.go +++ b/src/image/draw/draw.go @@ -634,10 +634,10 @@ func drawPaletted(dst Image, r image.Rectangle, src image.Image, sp image.Point, if !floydSteinberg { continue } - er -= int32(palette[bestIndex][0]) - eg -= int32(palette[bestIndex][1]) - eb -= int32(palette[bestIndex][2]) - ea -= int32(palette[bestIndex][3]) + er -= palette[bestIndex][0] + eg -= palette[bestIndex][1] + eb -= palette[bestIndex][2] + ea -= palette[bestIndex][3] } else { out.R = uint16(er) diff --git a/src/math/big/float.go b/src/math/big/float.go index 4b8ad388d3..7a9c2b3dfb 100644 --- a/src/math/big/float.go +++ b/src/math/big/float.go @@ -1008,9 +1008,9 @@ func (x *Float) Float64() (float64, Accuracy) { if r.form == inf || e > emax { // overflow if x.neg { - return float64(math.Inf(-1)), Below + return math.Inf(-1), Below } - return float64(math.Inf(+1)), Above + return math.Inf(+1), Above } // e <= emax diff --git a/src/math/big/natconv.go b/src/math/big/natconv.go index d2ce667fb6..e216bd288c 100644 --- a/src/math/big/natconv.go +++ b/src/math/big/natconv.go @@ -302,7 +302,7 @@ func (x nat) itoa(neg bool, base int) []byte { } } else { - bb, ndigits := maxPow(Word(b)) + bb, ndigits := maxPow(b) // construct table of successive squares of bb*leafSize to use in subdivisions // result (table != nil) <=> (len(x) > leafSize > 0) diff --git a/src/math/big/ratconv.go b/src/math/big/ratconv.go index 57df124e88..7c127f8585 100644 --- a/src/math/big/ratconv.go +++ b/src/math/big/ratconv.go @@ -178,7 +178,7 @@ func scanExponent(r io.ByteScanner, binExpOk bool) (exp int64, base int, err err } break // i > 0 } - digits = append(digits, byte(ch)) + digits = append(digits, ch) } // i > 0 => we have at least one digit diff --git a/src/net/interface_bsd.go b/src/net/interface_bsd.go index b173fbcefc..17c6dd3dcd 100644 --- a/src/net/interface_bsd.go +++ b/src/net/interface_bsd.go @@ -61,13 +61,13 @@ func newLink(m *syscall.InterfaceMessage) (*Interface, error) { m.Data = m.Data[unsafe.Offsetof(sa.Data):] var name [syscall.IFNAMSIZ]byte for i := 0; i < int(sa.Nlen); i++ { - name[i] = byte(m.Data[i]) + name[i] = m.Data[i] } ifi.Name = string(name[:sa.Nlen]) ifi.MTU = int(m.Header.Data.Mtu) addr := make([]byte, sa.Alen) for i := 0; i < int(sa.Alen); i++ { - addr[i] = byte(m.Data[int(sa.Nlen)+i]) + addr[i] = m.Data[int(sa.Nlen)+i] } ifi.HardwareAddr = addr[:sa.Alen] } diff --git a/src/net/mail/message.go b/src/net/mail/message.go index 9e3a103a4f..b40a314e33 100644 --- a/src/net/mail/message.go +++ b/src/net/mail/message.go @@ -477,7 +477,7 @@ func (p *addrParser) consumeAtom(dot bool, permissive bool) (atom string, err er if i < p.len() && p.s[i] > 127 { return "", errNonASCII } - atom, p.s = string(p.s[:i]), p.s[i:] + atom, p.s = p.s[:i], p.s[i:] if !permissive { if strings.HasPrefix(atom, ".") { return "", errors.New("mail: leading dot in atom") diff --git a/src/net/parse.go b/src/net/parse.go index eaaa1edf30..ed82a7769b 100644 --- a/src/net/parse.go +++ b/src/net/parse.go @@ -105,14 +105,14 @@ func splitAtBytes(s string, t string) []string { for i := 0; i < len(s); i++ { if byteIndex(t, s[i]) >= 0 { if last < i { - a[n] = string(s[last:i]) + a[n] = s[last:i] n++ } last = i + 1 } } if last < len(s) { - a[n] = string(s[last:]) + a[n] = s[last:] n++ } return a[0:n] diff --git a/src/os/exec_windows.go b/src/os/exec_windows.go index 3264271b2e..72b5a93199 100644 --- a/src/os/exec_windows.go +++ b/src/os/exec_windows.go @@ -104,7 +104,7 @@ func init() { defer syscall.LocalFree(syscall.Handle(uintptr(unsafe.Pointer(argv)))) Args = make([]string, argc) for i, v := range (*argv)[:argc] { - Args[i] = string(syscall.UTF16ToString((*v)[:])) + Args[i] = syscall.UTF16ToString((*v)[:]) } } diff --git a/src/os/file_windows.go b/src/os/file_windows.go index 7d04477d42..137f24a0a9 100644 --- a/src/os/file_windows.go +++ b/src/os/file_windows.go @@ -181,9 +181,9 @@ func (file *file) close() error { } var e error if file.isdir() { - e = syscall.FindClose(syscall.Handle(file.fd)) + e = syscall.FindClose(file.fd) } else { - e = syscall.CloseHandle(syscall.Handle(file.fd)) + e = syscall.CloseHandle(file.fd) } var err error if e != nil { @@ -216,7 +216,7 @@ func (file *File) readdir(n int) (fi []FileInfo, err error) { d := &file.dirinfo.data for n != 0 && !file.dirinfo.isempty { if file.dirinfo.needdata { - e := syscall.FindNextFile(syscall.Handle(file.fd), d) + e := syscall.FindNextFile(file.fd, d) if e != nil { if e == syscall.ERROR_NO_MORE_FILES { break @@ -230,7 +230,7 @@ func (file *File) readdir(n int) (fi []FileInfo, err error) { } } file.dirinfo.needdata = true - name := string(syscall.UTF16ToString(d.FileName[0:])) + name := syscall.UTF16ToString(d.FileName[0:]) if name == "." || name == ".." { // Useless names continue } @@ -288,7 +288,7 @@ func (f *File) readConsole(b []byte) (n int, err error) { } wchars := make([]uint16, nwc) pwc := &wchars[0] - nwc, err = windows.MultiByteToWideChar(acp, 2, pmb, int32(nmb), pwc, int32(nwc)) + nwc, err = windows.MultiByteToWideChar(acp, 2, pmb, int32(nmb), pwc, nwc) if err != nil { return 0, err } @@ -335,7 +335,7 @@ func (f *File) pread(b []byte, off int64) (n int, err error) { Offset: uint32(off), } var done uint32 - e = syscall.ReadFile(syscall.Handle(f.fd), b, &done, &o) + e = syscall.ReadFile(f.fd, b, &done, &o) if e != nil { if e == syscall.ERROR_HANDLE_EOF { // end of file @@ -415,7 +415,7 @@ func (f *File) pwrite(b []byte, off int64) (n int, err error) { Offset: uint32(off), } var done uint32 - e = syscall.WriteFile(syscall.Handle(f.fd), b, &done, &o) + e = syscall.WriteFile(f.fd, b, &done, &o) if e != nil { return 0, e } diff --git a/src/os/stat_darwin.go b/src/os/stat_darwin.go index 9dc7a99fb7..74214cefa4 100644 --- a/src/os/stat_darwin.go +++ b/src/os/stat_darwin.go @@ -11,7 +11,7 @@ import ( func fillFileStatFromSys(fs *fileStat, name string) { fs.name = basename(name) - fs.size = int64(fs.sys.Size) + fs.size = fs.sys.Size fs.modTime = timespecToTime(fs.sys.Mtimespec) fs.mode = FileMode(fs.sys.Mode & 0777) switch fs.sys.Mode & syscall.S_IFMT { diff --git a/src/os/stat_dragonfly.go b/src/os/stat_dragonfly.go index 69e63230eb..217bc6726d 100644 --- a/src/os/stat_dragonfly.go +++ b/src/os/stat_dragonfly.go @@ -11,7 +11,7 @@ import ( func fillFileStatFromSys(fs *fileStat, name string) { fs.name = basename(name) - fs.size = int64(fs.sys.Size) + fs.size = fs.sys.Size fs.modTime = timespecToTime(fs.sys.Mtim) fs.mode = FileMode(fs.sys.Mode & 0777) switch fs.sys.Mode & syscall.S_IFMT { @@ -42,7 +42,7 @@ func fillFileStatFromSys(fs *fileStat, name string) { } func timespecToTime(ts syscall.Timespec) time.Time { - return time.Unix(int64(ts.Sec), int64(ts.Nsec)) + return time.Unix(ts.Sec, ts.Nsec) } // For testing. diff --git a/src/os/stat_freebsd.go b/src/os/stat_freebsd.go index e9d38aa722..bab4ffa798 100644 --- a/src/os/stat_freebsd.go +++ b/src/os/stat_freebsd.go @@ -11,7 +11,7 @@ import ( func fillFileStatFromSys(fs *fileStat, name string) { fs.name = basename(name) - fs.size = int64(fs.sys.Size) + fs.size = fs.sys.Size fs.modTime = timespecToTime(fs.sys.Mtimespec) fs.mode = FileMode(fs.sys.Mode & 0777) switch fs.sys.Mode & syscall.S_IFMT { diff --git a/src/os/stat_linux.go b/src/os/stat_linux.go index 69e63230eb..d36afa9ffd 100644 --- a/src/os/stat_linux.go +++ b/src/os/stat_linux.go @@ -11,7 +11,7 @@ import ( func fillFileStatFromSys(fs *fileStat, name string) { fs.name = basename(name) - fs.size = int64(fs.sys.Size) + fs.size = fs.sys.Size fs.modTime = timespecToTime(fs.sys.Mtim) fs.mode = FileMode(fs.sys.Mode & 0777) switch fs.sys.Mode & syscall.S_IFMT { diff --git a/src/os/stat_nacl.go b/src/os/stat_nacl.go index d3bed14e43..0c53f2faa4 100644 --- a/src/os/stat_nacl.go +++ b/src/os/stat_nacl.go @@ -11,7 +11,7 @@ import ( func fillFileStatFromSys(fs *fileStat, name string) { fs.name = basename(name) - fs.size = int64(fs.sys.Size) + fs.size = fs.sys.Size fs.modTime = timespecToTime(fs.sys.Mtime, fs.sys.MtimeNsec) fs.mode = FileMode(fs.sys.Mode & 0777) switch fs.sys.Mode & syscall.S_IFMT { diff --git a/src/os/stat_netbsd.go b/src/os/stat_netbsd.go index e9d38aa722..11ebcacab8 100644 --- a/src/os/stat_netbsd.go +++ b/src/os/stat_netbsd.go @@ -11,7 +11,7 @@ import ( func fillFileStatFromSys(fs *fileStat, name string) { fs.name = basename(name) - fs.size = int64(fs.sys.Size) + fs.size = fs.sys.Size fs.modTime = timespecToTime(fs.sys.Mtimespec) fs.mode = FileMode(fs.sys.Mode & 0777) switch fs.sys.Mode & syscall.S_IFMT { @@ -42,7 +42,7 @@ func fillFileStatFromSys(fs *fileStat, name string) { } func timespecToTime(ts syscall.Timespec) time.Time { - return time.Unix(int64(ts.Sec), int64(ts.Nsec)) + return time.Unix(ts.Sec, int64(ts.Nsec)) } // For testing. diff --git a/src/os/stat_openbsd.go b/src/os/stat_openbsd.go index 69e63230eb..9df2d7f773 100644 --- a/src/os/stat_openbsd.go +++ b/src/os/stat_openbsd.go @@ -11,7 +11,7 @@ import ( func fillFileStatFromSys(fs *fileStat, name string) { fs.name = basename(name) - fs.size = int64(fs.sys.Size) + fs.size = fs.sys.Size fs.modTime = timespecToTime(fs.sys.Mtim) fs.mode = FileMode(fs.sys.Mode & 0777) switch fs.sys.Mode & syscall.S_IFMT { @@ -42,7 +42,7 @@ func fillFileStatFromSys(fs *fileStat, name string) { } func timespecToTime(ts syscall.Timespec) time.Time { - return time.Unix(int64(ts.Sec), int64(ts.Nsec)) + return time.Unix(ts.Sec, int64(ts.Nsec)) } // For testing. diff --git a/src/os/stat_plan9.go b/src/os/stat_plan9.go index a2df5fe139..96f056c111 100644 --- a/src/os/stat_plan9.go +++ b/src/os/stat_plan9.go @@ -20,7 +20,7 @@ func sameFile(fs1, fs2 *fileStat) bool { func fileInfoFromStat(d *syscall.Dir) FileInfo { fs := &fileStat{ name: d.Name, - size: int64(d.Length), + size: d.Length, modTime: time.Unix(int64(d.Mtime), 0), sys: d, } diff --git a/src/os/stat_solaris.go b/src/os/stat_solaris.go index 69e63230eb..217bc6726d 100644 --- a/src/os/stat_solaris.go +++ b/src/os/stat_solaris.go @@ -11,7 +11,7 @@ import ( func fillFileStatFromSys(fs *fileStat, name string) { fs.name = basename(name) - fs.size = int64(fs.sys.Size) + fs.size = fs.sys.Size fs.modTime = timespecToTime(fs.sys.Mtim) fs.mode = FileMode(fs.sys.Mode & 0777) switch fs.sys.Mode & syscall.S_IFMT { @@ -42,7 +42,7 @@ func fillFileStatFromSys(fs *fileStat, name string) { } func timespecToTime(ts syscall.Timespec) time.Time { - return time.Unix(int64(ts.Sec), int64(ts.Nsec)) + return time.Unix(ts.Sec, ts.Nsec) } // For testing. diff --git a/src/os/stat_windows.go b/src/os/stat_windows.go index b8f97ad60a..e55eeb0fdd 100644 --- a/src/os/stat_windows.go +++ b/src/os/stat_windows.go @@ -35,7 +35,7 @@ func (file *File) Stat() (FileInfo, error) { } var d syscall.ByHandleFileInformation - err = syscall.GetFileInformationByHandle(syscall.Handle(file.fd), &d) + err = syscall.GetFileInformationByHandle(file.fd, &d) if err != nil { return nil, &PathError{"GetFileInformationByHandle", file.name, err} } diff --git a/src/os/types_windows.go b/src/os/types_windows.go index 900d444b0e..ad4e863fcb 100644 --- a/src/os/types_windows.go +++ b/src/os/types_windows.go @@ -73,7 +73,7 @@ func (fs *fileStat) loadFileId() error { } defer syscall.CloseHandle(h) var i syscall.ByHandleFileInformation - err = syscall.GetFileInformationByHandle(syscall.Handle(h), &i) + err = syscall.GetFileInformationByHandle(h, &i) if err != nil { return err } diff --git a/src/reflect/type.go b/src/reflect/type.go index 3c7affcd7f..b8c778cc2b 100644 --- a/src/reflect/type.go +++ b/src/reflect/type.go @@ -967,7 +967,7 @@ func (t *rtype) Out(i int) Type { } func (t *funcType) in() []*rtype { - uadd := uintptr(unsafe.Sizeof(*t)) + uadd := unsafe.Sizeof(*t) if t.tflag&tflagUncommon != 0 { uadd += unsafe.Sizeof(uncommonType{}) } @@ -975,7 +975,7 @@ func (t *funcType) in() []*rtype { } func (t *funcType) out() []*rtype { - uadd := uintptr(unsafe.Sizeof(*t)) + uadd := unsafe.Sizeof(*t) if t.tflag&tflagUncommon != 0 { uadd += unsafe.Sizeof(uncommonType{}) } diff --git a/src/reflect/value.go b/src/reflect/value.go index d72c14e9e1..d4d317436a 100644 --- a/src/reflect/value.go +++ b/src/reflect/value.go @@ -666,7 +666,7 @@ func (v Value) Cap() int { case Array: return v.typ.Len() case Chan: - return int(chancap(v.pointer())) + return chancap(v.pointer()) case Slice: // Slice is always bigger than a word; assume flagIndir. return (*sliceHeader)(v.ptr).Cap @@ -885,7 +885,7 @@ func (v Value) Int() int64 { case Int32: return int64(*(*int32)(p)) case Int64: - return int64(*(*int64)(p)) + return *(*int64)(p) } panic(&ValueError{"reflect.Value.Int", v.kind()}) } @@ -1436,7 +1436,7 @@ func (v Value) SetCap(n int) { v.mustBeAssignable() v.mustBe(Slice) s := (*sliceHeader)(v.ptr) - if n < int(s.Len) || n > int(s.Cap) { + if n < s.Len || n > s.Cap { panic("reflect: slice capacity out of range in SetCap") } s.Cap = n @@ -1538,7 +1538,7 @@ func (v Value) Slice(i, j int) Value { case Slice: typ = (*sliceType)(unsafe.Pointer(v.typ)) s := (*sliceHeader)(v.ptr) - base = unsafe.Pointer(s.Data) + base = s.Data cap = s.Cap case String: @@ -1710,7 +1710,7 @@ func (v Value) Uint() uint64 { case Uint32: return uint64(*(*uint32)(p)) case Uint64: - return uint64(*(*uint64)(p)) + return *(*uint64)(p) case Uintptr: return uint64(*(*uintptr)(p)) } @@ -2267,13 +2267,13 @@ func makeInt(f flag, bits uint64, t Type) Value { ptr := unsafe_New(typ) switch typ.size { case 1: - *(*uint8)(unsafe.Pointer(ptr)) = uint8(bits) + *(*uint8)(ptr) = uint8(bits) case 2: - *(*uint16)(unsafe.Pointer(ptr)) = uint16(bits) + *(*uint16)(ptr) = uint16(bits) case 4: - *(*uint32)(unsafe.Pointer(ptr)) = uint32(bits) + *(*uint32)(ptr) = uint32(bits) case 8: - *(*uint64)(unsafe.Pointer(ptr)) = bits + *(*uint64)(ptr) = bits } return Value{typ, ptr, f | flagIndir | flag(typ.Kind())} } @@ -2285,9 +2285,9 @@ func makeFloat(f flag, v float64, t Type) Value { ptr := unsafe_New(typ) switch typ.size { case 4: - *(*float32)(unsafe.Pointer(ptr)) = float32(v) + *(*float32)(ptr) = float32(v) case 8: - *(*float64)(unsafe.Pointer(ptr)) = v + *(*float64)(ptr) = v } return Value{typ, ptr, f | flagIndir | flag(typ.Kind())} } @@ -2299,9 +2299,9 @@ func makeComplex(f flag, v complex128, t Type) Value { ptr := unsafe_New(typ) switch typ.size { case 8: - *(*complex64)(unsafe.Pointer(ptr)) = complex64(v) + *(*complex64)(ptr) = complex64(v) case 16: - *(*complex128)(unsafe.Pointer(ptr)) = v + *(*complex128)(ptr) = v } return Value{typ, ptr, f | flagIndir | flag(typ.Kind())} } diff --git a/src/regexp/onepass.go b/src/regexp/onepass.go index 5b82f9666e..4991954820 100644 --- a/src/regexp/onepass.go +++ b/src/regexp/onepass.go @@ -450,7 +450,7 @@ func makeOnePass(p *onePassProg) *onePassProg { for !instQueue.empty() { visitQueue.clear() pc := instQueue.next() - if !check(uint32(pc), m) { + if !check(pc, m) { p = notOnePass break } diff --git a/src/strconv/extfloat.go b/src/strconv/extfloat.go index 019b4eebdc..7033e96c39 100644 --- a/src/strconv/extfloat.go +++ b/src/strconv/extfloat.go @@ -311,9 +311,9 @@ func (f *extFloat) AssignDecimal(mantissa uint64, exp10 int, neg bool, trunc boo var extrabits uint if f.exp <= denormalExp { // f.mant * 2^f.exp is smaller than 2^(flt.bias+1). - extrabits = uint(63 - flt.mantbits + 1 + uint(denormalExp-f.exp)) + extrabits = 63 - flt.mantbits + 1 + uint(denormalExp-f.exp) } else { - extrabits = uint(63 - flt.mantbits) + extrabits = 63 - flt.mantbits } halfway := uint64(1) << (extrabits - 1) diff --git a/src/strings/reader.go b/src/strings/reader.go index 737873c099..74eed4d574 100644 --- a/src/strings/reader.go +++ b/src/strings/reader.go @@ -113,7 +113,7 @@ func (r *Reader) Seek(offset int64, whence int) (int64, error) { case 0: abs = offset case 1: - abs = int64(r.i) + offset + abs = r.i + offset case 2: abs = int64(len(r.s)) + offset default: diff --git a/src/sync/pool.go b/src/sync/pool.go index 4fb1a1af9d..2acf505f3c 100644 --- a/src/sync/pool.go +++ b/src/sync/pool.go @@ -179,8 +179,8 @@ func (p *Pool) pinSlow() *poolLocal { // If GOMAXPROCS changes between GCs, we re-allocate the array and lose the old one. size := runtime.GOMAXPROCS(0) local := make([]poolLocal, size) - atomic.StorePointer((*unsafe.Pointer)(&p.local), unsafe.Pointer(&local[0])) // store-release - atomic.StoreUintptr(&p.localSize, uintptr(size)) // store-release + atomic.StorePointer(&p.local, unsafe.Pointer(&local[0])) // store-release + atomic.StoreUintptr(&p.localSize, uintptr(size)) // store-release return &local[pid] } diff --git a/src/time/time.go b/src/time/time.go index 4b9a0db730..92d635eec5 100644 --- a/src/time/time.go +++ b/src/time/time.go @@ -606,7 +606,7 @@ func (d Duration) Hours() float64 { // Add returns the time t+d. func (t Time) Add(d Duration) Time { t.sec += int64(d / 1e9) - nsec := int32(t.nsec) + int32(d%1e9) + nsec := t.nsec + int32(d%1e9) if nsec >= 1e9 { t.sec++ nsec -= 1e9 @@ -623,7 +623,7 @@ func (t Time) Add(d Duration) Time { // will be returned. // To compute t-d for a duration d, use t.Add(-d). func (t Time) Sub(u Time) Duration { - d := Duration(t.sec-u.sec)*Second + Duration(int32(t.nsec)-int32(u.nsec)) + d := Duration(t.sec-u.sec)*Second + Duration(t.nsec-u.nsec) // Check for overflow or underflow. switch { case u.Add(d).Equal(t): @@ -1125,7 +1125,7 @@ func (t Time) Round(d Duration) Time { // but it's still here in case we change our minds. func div(t Time, d Duration) (qmod2 int, r Duration) { neg := false - nsec := int32(t.nsec) + nsec := t.nsec if t.sec < 0 { // Operate on absolute value. neg = true @@ -1159,7 +1159,7 @@ func div(t Time, d Duration) (qmod2 int, r Duration) { tmp := (sec >> 32) * 1e9 u1 := tmp >> 32 u0 := tmp << 32 - tmp = uint64(sec&0xFFFFFFFF) * 1e9 + tmp = (sec & 0xFFFFFFFF) * 1e9 u0x, u0 := u0, u0+tmp if u0 < u0x { u1++ diff --git a/src/time/zoneinfo_windows.go b/src/time/zoneinfo_windows.go index bcb8ccd563..c753119d5d 100644 --- a/src/time/zoneinfo_windows.go +++ b/src/time/zoneinfo_windows.go @@ -83,7 +83,7 @@ func extractCAPS(desc string) string { var short []rune for _, c := range desc { if 'A' <= c && c <= 'Z' { - short = append(short, rune(c)) + short = append(short, c) } } return string(short) diff --git a/src/unicode/letter.go b/src/unicode/letter.go index 8443ee51a2..ffa083eb57 100644 --- a/src/unicode/letter.go +++ b/src/unicode/letter.go @@ -217,7 +217,7 @@ func to(_case int, r rune, caseRange []CaseRange) rune { m := lo + (hi-lo)/2 cr := caseRange[m] if rune(cr.Lo) <= r && r <= rune(cr.Hi) { - delta := rune(cr.Delta[_case]) + delta := cr.Delta[_case] if delta > MaxRune { // In an Upper-Lower sequence, which always starts with // an UpperCase letter, the real deltas always look like: -- cgit v1.3