]> Cypherpunks repositories - gostls13.git/commitdiff
archive/tar: reduce allocations in formatOctal
authorJoe Tsai <joetsai@digital-static.net>
Wed, 12 Oct 2016 18:36:18 +0000 (11:36 -0700)
committerJoe Tsai <thebrokentoaster@gmail.com>
Wed, 12 Oct 2016 21:27:51 +0000 (21:27 +0000)
Change-Id: I9ddb7d2a97d28aba7a107b65f278993daf7807fa
Reviewed-on: https://go-review.googlesource.com/30960
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/archive/tar/strconv.go

index 2619bcdde433978ce77854b59671e36d8d6c98dd..45e46b13b8cb2892e2ba98f3502e846c7407bea5 100644 (file)
@@ -156,12 +156,11 @@ func (p *parser) parseOctal(b []byte) int64 {
        return int64(x)
 }
 
-// Encode x as an octal ASCII string and write it into b with leading zeros.
 func (f *formatter) formatOctal(b []byte, x int64) {
        s := strconv.FormatInt(x, 8)
-       // leading zeros, but leave room for a NUL.
-       for len(s)+1 < len(b) {
-               s = "0" + s
+       // Add leading zeros, but leave room for a NUL.
+       if n := len(b) - len(s) - 1; n > 0 {
+               s = strings.Repeat("0", n) + s
        }
        f.formatString(b, s)
 }