summaryrefslogtreecommitdiff
path: root/src/lib/strconv/itoa.go
blob: 7f693ea8cfc07d524622950f2577a5735cef61cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 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

// Itob64 returns the string representation of i in the given base.
func Itob64(i int64, base uint) string {
	if i == 0 {
		return "0"
	}

	u := uint64(i);
	if i < 0 {
		u = -u;
	}

	// Assemble decimal in reverse order.
	var buf [32]byte;
	j := len(buf);
	b := uint64(base);
	for u > 0 {
		j--;
		buf[j] = "0123456789abcdefghijklmnopqrstuvwxyz"[u%b];
		u /= b;
	}

	if i < 0 {	// add sign
		j--;
		buf[j] = '-'
	}

	return string(buf[j:len(buf)])
}

// Itoa64 returns the decimal string representation of i.
func Itoa64(i int64) string {
	return Itob64(i, 10);
}

// Itob returns the string representation of i in the given base.
func Itob(i int, base uint) string {
	return Itob64(int64(i), base);
}

// Itoa returns the decimal string representation of i.
func Itoa(i int) string {
	return Itob64(int64(i), 10);
}