diff options
| author | Martin Möhrmann <martisch@uos.de> | 2014-12-31 18:45:05 +0100 |
|---|---|---|
| committer | Mikio Hara <mikioh.mikioh@gmail.com> | 2015-01-22 13:01:28 +0000 |
| commit | 494b4ce2a7bc9fd4813ea546ef589b3ce0fc980f (patch) | |
| tree | 4cec3e530a1218b8d62ccf701ae2bc790b50b900 /src/net/parse.go | |
| parent | afe6668216516b012a09f6c7166009761e965f5b (diff) | |
| download | go-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>
Diffstat (limited to 'src/net/parse.go')
| -rw-r--r-- | src/net/parse.go | 49 |
1 files changed, 18 insertions, 31 deletions
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. |
