Commit 494b4ce2 authored by Martin Möhrmann's avatar Martin Möhrmann Committed by Mikio Hara

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/2212Reviewed-by: default avatarMikio Hara <mikioh.mikioh@gmail.com>
parent afe66682
...@@ -43,8 +43,8 @@ func reverseaddr(addr string) (arpa string, err error) { ...@@ -43,8 +43,8 @@ func reverseaddr(addr string) (arpa string, err error) {
return "", &DNSError{Err: "unrecognized address", Name: addr} return "", &DNSError{Err: "unrecognized address", Name: addr}
} }
if ip.To4() != nil { if ip.To4() != nil {
return itoa(int(ip[15])) + "." + itoa(int(ip[14])) + "." + itoa(int(ip[13])) + "." + return uitoa(uint(ip[15])) + "." + uitoa(uint(ip[14])) + "." + uitoa(uint(ip[13])) + "." +
itoa(int(ip[12])) + ".in-addr.arpa.", nil uitoa(uint(ip[12])) + ".in-addr.arpa.", nil
} }
// Must be IPv6 // Must be IPv6
buf := make([]byte, 0, len(ip)*4+len("ip6.arpa.")) buf := make([]byte, 0, len(ip)*4+len("ip6.arpa."))
......
...@@ -267,10 +267,10 @@ func (ip IP) String() string { ...@@ -267,10 +267,10 @@ func (ip IP) String() string {
// If IPv4, use dotted notation. // If IPv4, use dotted notation.
if p4 := p.To4(); len(p4) == IPv4len { if p4 := p.To4(); len(p4) == IPv4len {
return itod(uint(p4[0])) + "." + return uitoa(uint(p4[0])) + "." +
itod(uint(p4[1])) + "." + uitoa(uint(p4[1])) + "." +
itod(uint(p4[2])) + "." + uitoa(uint(p4[2])) + "." +
itod(uint(p4[3])) uitoa(uint(p4[3]))
} }
if len(p) != IPv6len { if len(p) != IPv6len {
return "?" return "?"
...@@ -491,7 +491,7 @@ func (n *IPNet) String() string { ...@@ -491,7 +491,7 @@ func (n *IPNet) String() string {
if l == -1 { if l == -1 {
return nn.String() + "/" + m.String() return nn.String() + "/" + m.String()
} }
return nn.String() + "/" + itod(uint(l)) return nn.String() + "/" + uitoa(uint(l))
} }
// Parse IPv4 address (d.d.d.d). // Parse IPv4 address (d.d.d.d).
......
...@@ -303,7 +303,7 @@ func zoneToString(zone int) string { ...@@ -303,7 +303,7 @@ func zoneToString(zone int) string {
if ifi, err := InterfaceByIndex(zone); err == nil { if ifi, err := InterfaceByIndex(zone); err == nil {
return ifi.Name return ifi.Name
} }
return itod(uint(zone)) return uitoa(uint(zone))
} }
func zoneToInt(zone string) int { func zoneToInt(zone string) int {
......
...@@ -171,43 +171,30 @@ func xtoi2(s string, e byte) (byte, bool) { ...@@ -171,43 +171,30 @@ func xtoi2(s string, e byte) (byte, bool) {
return byte(n), ok && ei == 2 return byte(n), ok && ei == 2
} }
// Integer to decimal. // Convert integer to decimal string.
func itoa(i int) string { func itoa(val int) string {
var buf [30]byte if val < 0 {
n := len(buf) return "-" + uitoa(uint(-val))
neg := false }
if i < 0 { return uitoa(uint(val))
i = -i
neg = true
}
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:])
} }
// Convert i to decimal string. // Convert unsigned integer to decimal string.
func itod(i uint) string { func uitoa(val uint) string {
if i == 0 { if val == 0 { // avoid string allocation
return "0" return "0"
} }
var buf [20]byte // big enough for 64bit value base 10
// Assemble decimal in reverse order. i := len(buf) - 1
var b [32]byte for val >= 10 {
bp := len(b) q := val / 10
for ; i > 0; i /= 10 { buf[i] = byte('0' + val - q*10)
bp-- i--
b[bp] = byte(i%10) + '0' val = q
} }
// val < 10
return string(b[bp:]) buf[i] = byte('0' + val)
return string(buf[i:])
} }
// Convert i to a hexadecimal string. Leading zeros are not printed. // Convert i to a hexadecimal string. Leading zeros are not printed.
......
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