1
0
mirror of https://github.com/golang/go synced 2024-10-04 10:31:22 -06:00
go/src/pkg/strconv/itoa.go

58 lines
1.4 KiB
Go
Raw Normal View History

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package strconv
// FormatUint returns the string representation of i in the given base.
func FormatUint(i uint64, base int) string {
u := i
if base < 2 || 36 < base {
panic("invalid base " + Itoa(base))
}
if u == 0 {
return "0"
}
// Assemble decimal in reverse order.
var buf [64]byte
j := len(buf)
b := uint64(base)
for u > 0 {
j--
buf[j] = "0123456789abcdefghijklmnopqrstuvwxyz"[u%b]
u /= b
}
return string(buf[j:])
}
// FormatInt returns the string representation of i in the given base.
func FormatInt(i int64, base int) string {
if i == 0 {
return "0"
}
if i < 0 {
return "-" + FormatUint(-uint64(i), base)
}
return FormatUint(uint64(i), base)
}
// Itoa is shorthand for FormatInt(i, 10).
func Itoa(i int) string {
return FormatInt(int64(i), 10)
}
// AppendInt appends the string form of the integer i,
// as generated by FormatInt, to dst and returns the extended buffer.
func AppendInt(dst []byte, i int64, base int) []byte {
return append(dst, FormatInt(i, base)...)
}
// AppendUint appends the string form of the unsigned integer i,
// as generated by FormatUint, to dst and returns the extended buffer.
func AppendUint(dst []byte, i uint64, base int) []byte {
return append(dst, FormatUint(i, base)...)
}