aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Möhrmann <martisch@uos.de>2014-12-31 18:45:05 +0100
committerMikio Hara <mikioh.mikioh@gmail.com>2015-01-22 13:01:28 +0000
commit494b4ce2a7bc9fd4813ea546ef589b3ce0fc980f (patch)
tree4cec3e530a1218b8d62ccf701ae2bc790b50b900
parentafe6668216516b012a09f6c7166009761e965f5b (diff)
downloadgo-494b4ce2a7bc9fd4813ea546ef589b3ce0fc980f.tar.xz
net: simplify itoa conversions
Rename itod to uitoa to have consistent naming with other itoa functions. Reduce redundant code by calling uitoa from itoa. Reduce buffer to maximally needed size for conversion of 64bit integers. Adjust calls to itoa functions in package net to use new name for itod. Avoid calls to itoa if uitoa suffices. Change-Id: I79deaede4d4b0c076a99a4f4dd6f644ba1daec53 Reviewed-on: https://go-review.googlesource.com/2212 Reviewed-by: Mikio Hara <mikioh.mikioh@gmail.com>
-rw-r--r--src/net/dnsclient.go4
-rw-r--r--src/net/ip.go10
-rw-r--r--src/net/ipsock.go2
-rw-r--r--src/net/parse.go49
4 files changed, 26 insertions, 39 deletions
diff --git a/src/net/dnsclient.go b/src/net/dnsclient.go
index e8014e4ffc..56e2a94965 100644
--- a/src/net/dnsclient.go
+++ b/src/net/dnsclient.go
@@ -43,8 +43,8 @@ func reverseaddr(addr string) (arpa string, err error) {
return "", &DNSError{Err: "unrecognized address", Name: addr}
}
if ip.To4() != nil {
- return itoa(int(ip[15])) + "." + itoa(int(ip[14])) + "." + itoa(int(ip[13])) + "." +
- itoa(int(ip[12])) + ".in-addr.arpa.", nil
+ return uitoa(uint(ip[15])) + "." + uitoa(uint(ip[14])) + "." + uitoa(uint(ip[13])) + "." +
+ uitoa(uint(ip[12])) + ".in-addr.arpa.", nil
}
// Must be IPv6
buf := make([]byte, 0, len(ip)*4+len("ip6.arpa."))
diff --git a/src/net/ip.go b/src/net/ip.go
index 4a93e97b39..7bcc40e8f6 100644
--- a/src/net/ip.go
+++ b/src/net/ip.go
@@ -267,10 +267,10 @@ func (ip IP) String() string {
// If IPv4, use dotted notation.
if p4 := p.To4(); len(p4) == IPv4len {
- return itod(uint(p4[0])) + "." +
- itod(uint(p4[1])) + "." +
- itod(uint(p4[2])) + "." +
- itod(uint(p4[3]))
+ return uitoa(uint(p4[0])) + "." +
+ uitoa(uint(p4[1])) + "." +
+ uitoa(uint(p4[2])) + "." +
+ uitoa(uint(p4[3]))
}
if len(p) != IPv6len {
return "?"
@@ -491,7 +491,7 @@ func (n *IPNet) String() string {
if l == -1 {
return nn.String() + "/" + m.String()
}
- return nn.String() + "/" + itod(uint(l))
+ return nn.String() + "/" + uitoa(uint(l))
}
// Parse IPv4 address (d.d.d.d).
diff --git a/src/net/ipsock.go b/src/net/ipsock.go
index dda8578030..858c6ef12c 100644
--- a/src/net/ipsock.go
+++ b/src/net/ipsock.go
@@ -303,7 +303,7 @@ func zoneToString(zone int) string {
if ifi, err := InterfaceByIndex(zone); err == nil {
return ifi.Name
}
- return itod(uint(zone))
+ return uitoa(uint(zone))
}
func zoneToInt(zone string) int {
diff --git a/src/net/parse.go b/src/net/parse.go
index e1d0130c9a..ad901fff27 100644
--- a/src/net/parse.go
+++ b/src/net/parse.go
@@ -171,43 +171,30 @@ func xtoi2(s string, e byte) (byte, bool) {
return byte(n), ok && ei == 2
}
-// Integer to decimal.
-func itoa(i int) string {
- var buf [30]byte
- n := len(buf)
- neg := false
- if i < 0 {
- i = -i
- neg = true
+// Convert integer to decimal string.
+func itoa(val int) string {
+ if val < 0 {
+ return "-" + uitoa(uint(-val))
}
- ui := uint(i)
- for ui > 0 || n == len(buf) {
- n--
- buf[n] = byte('0' + ui%10)
- ui /= 10
- }
- if neg {
- n--
- buf[n] = '-'
- }
- return string(buf[n:])
+ return uitoa(uint(val))
}
-// Convert i to decimal string.
-func itod(i uint) string {
- if i == 0 {
+// Convert unsigned integer to decimal string.
+func uitoa(val uint) string {
+ if val == 0 { // avoid string allocation
return "0"
}
-
- // Assemble decimal in reverse order.
- var b [32]byte
- bp := len(b)
- for ; i > 0; i /= 10 {
- bp--
- b[bp] = byte(i%10) + '0'
+ var buf [20]byte // big enough for 64bit value base 10
+ i := len(buf) - 1
+ for val >= 10 {
+ q := val / 10
+ buf[i] = byte('0' + val - q*10)
+ i--
+ val = q
}
-
- return string(b[bp:])
+ // val < 10
+ buf[i] = byte('0' + val)
+ return string(buf[i:])
}
// Convert i to a hexadecimal string. Leading zeros are not printed.