From e87a807885cecd8080ceb8b3df3470e0216b33c5 Mon Sep 17 00:00:00 2001 From: Jim Teeuwen Date: Wed, 29 Jan 2014 11:34:37 +0100 Subject: [PATCH] Avoid expensive fmt.Fprintf calls in `StringWriter.Write` inner loop. This addresses issue 14. --- stringwriter.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/stringwriter.go b/stringwriter.go index 278f35b..77daa04 100644 --- a/stringwriter.go +++ b/stringwriter.go @@ -5,10 +5,11 @@ package bindata import ( - "fmt" "io" ) +const lowerHex = "0123456789abcdef" + type StringWriter struct { io.Writer c int @@ -19,8 +20,13 @@ func (w *StringWriter) Write(p []byte) (n int, err error) { return } - for n = range p { - fmt.Fprintf(w.Writer, "\\x%02x", p[n]) + buf := []byte(`\x00`) + var b byte + + for n, b = range p { + buf[2] = lowerHex[b/16] + buf[3] = lowerHex[b%16] + w.Writer.Write(buf) w.c++ }