From ea6a050b924759c865156357a44fd86e85fd1f4a Mon Sep 17 00:00:00 2001 From: Kyle Date: Tue, 22 Sep 2015 22:58:58 -0700 Subject: [PATCH] Add Itoa. --- lib/lib.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/lib.go b/lib/lib.go index 9d207ed..104aa65 100644 --- a/lib/lib.go +++ b/lib/lib.go @@ -54,3 +54,22 @@ func CheckFatal(err error, format string, a ...interface{}) { Err(ExitFailure, err, format, a...) } + +// Itoa provides cheap integer to fixed-width decimal ASCII. Give a +// negative width to avoid zero-padding. Adapted from the 'itoa' +// function in the log/log.go file in the standard library. +func Itoa(i int, wid int) string { + // Assemble decimal in reverse order. + var b [20]byte + bp := len(b) - 1 + for i >= 10 || wid > 1 { + wid-- + q := i / 10 + b[bp] = byte('0' + i - q*10) + bp-- + i = q + } + // i < 10 + b[bp] = byte('0' + i) + return string(b[bp:]) +}