From 610b5b2fd8a31ac3855088a0ea2aece8d88d7521 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Mon, 5 Mar 2012 13:36:05 -0800 Subject: [PATCH] net: remove all direct fmt and bytes imports Once dnsMsg stops using reflect, we lose even more indirect dependencies. R=rsc CC=golang-dev https://golang.org/cl/5751043 --- src/pkg/net/dnsclient.go | 20 ++++++++++---------- src/pkg/net/interface_linux.go | 10 ++++++---- src/pkg/net/sockopt.go | 3 +-- src/pkg/net/tcpsock_posix.go | 3 +-- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/pkg/net/dnsclient.go b/src/pkg/net/dnsclient.go index f4ed8b87cc1..e69cb3188bc 100644 --- a/src/pkg/net/dnsclient.go +++ b/src/pkg/net/dnsclient.go @@ -5,8 +5,6 @@ package net import ( - "bytes" - "fmt" "math/rand" "sort" ) @@ -45,20 +43,22 @@ func reverseaddr(addr string) (arpa string, err error) { return "", &DNSError{Err: "unrecognized address", Name: addr} } if ip.To4() != nil { - return fmt.Sprintf("%d.%d.%d.%d.in-addr.arpa.", ip[15], ip[14], ip[13], ip[12]), nil + return itoa(int(ip[15])) + "." + itoa(int(ip[14])) + "." + itoa(int(ip[13])) + "." + + itoa(int(ip[12])) + ".in-addr.arpa.", nil } // Must be IPv6 - var buf bytes.Buffer + buf := make([]byte, 0, len(ip)*4+len("ip6.arpa.")) // Add it, in reverse, to the buffer for i := len(ip) - 1; i >= 0; i-- { - s := fmt.Sprintf("%02x", ip[i]) - buf.WriteByte(s[1]) - buf.WriteByte('.') - buf.WriteByte(s[0]) - buf.WriteByte('.') + v := ip[i] + buf = append(buf, hexDigit[v&0xF]) + buf = append(buf, '.') + buf = append(buf, hexDigit[v>>4]) + buf = append(buf, '.') } // Append "ip6.arpa." and return (buf already has the final .) - return buf.String() + "ip6.arpa.", nil + buf = append(buf, "ip6.arpa."...) + return string(buf), nil } // Find answer for name in dns message. diff --git a/src/pkg/net/interface_linux.go b/src/pkg/net/interface_linux.go index 15c2f3781b1..8c9c3040874 100644 --- a/src/pkg/net/interface_linux.go +++ b/src/pkg/net/interface_linux.go @@ -7,7 +7,6 @@ package net import ( - "fmt" "os" "syscall" "unsafe" @@ -194,7 +193,9 @@ func parseProcNetIGMP(path string, ifi *Interface) []Addr { name = f[1] case len(f[0]) == 8: if ifi == nil || name == ifi.Name { - fmt.Sscanf(f[0], "%08x", &b) + for i := 0; i+1 < len(f[0]); i += 2 { + b[i/2], _ = xtoi2(f[0][i:i+2], 0) + } ifma := IPAddr{IP: IPv4(b[3], b[2], b[1], b[0])} ifmat = append(ifmat, ifma.toAddr()) } @@ -218,10 +219,11 @@ func parseProcNetIGMP6(path string, ifi *Interface) []Addr { continue } if ifi == nil || f[1] == ifi.Name { - fmt.Sscanf(f[2], "%32x", &b) + for i := 0; i+1 < len(f[2]); i += 2 { + b[i/2], _ = xtoi2(f[2][i:i+2], 0) + } ifma := IPAddr{IP: IP{b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15]}} ifmat = append(ifmat, ifma.toAddr()) - } } return ifmat diff --git a/src/pkg/net/sockopt.go b/src/pkg/net/sockopt.go index 0a051d7ae3c..0cd19266fe0 100644 --- a/src/pkg/net/sockopt.go +++ b/src/pkg/net/sockopt.go @@ -9,7 +9,6 @@ package net import ( - "bytes" "os" "syscall" "time" @@ -98,7 +97,7 @@ func setIPv4MreqToInterface(mreq *syscall.IPMreq, ifi *Interface) error { } } done: - if bytes.Equal(mreq.Multiaddr[:], IPv4zero.To4()) { + if bytesEqual(mreq.Multiaddr[:], IPv4zero.To4()) { return errNoSuchMulticastInterface } return nil diff --git a/src/pkg/net/tcpsock_posix.go b/src/pkg/net/tcpsock_posix.go index a073ab9f242..15f8efdd701 100644 --- a/src/pkg/net/tcpsock_posix.go +++ b/src/pkg/net/tcpsock_posix.go @@ -9,7 +9,6 @@ package net import ( - "fmt" "io" "os" "syscall" @@ -30,7 +29,7 @@ func sockaddrToTCP(sa syscall.Sockaddr) Addr { default: if sa != nil { // Diagnose when we will turn a non-nil sockaddr into a nil. - panic(fmt.Sprintf("unexpected type in sockaddrToTCP: %T", sa)) + panic("unexpected type in sockaddrToTCP") } } return nil