From a48d892de3fb625bdb3e8e367337578c766a42f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisendo=CC=88rfer?= Date: Sun, 11 Jan 2015 17:08:41 +0100 Subject: [PATCH] optimize: static allocation for output symbols 80.8 ns/op --- xxd.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/xxd.go b/xxd.go index fd07630..cb03afb 100644 --- a/xxd.go +++ b/xxd.go @@ -32,6 +32,13 @@ const ( line_length = 50 ) +var ( + space = []byte(" ") + doubleSpace = []byte(" ") + dot = []byte(".") + newline = []byte("\n") +) + func XXD(r io.Reader, w io.Writer) error { line_offset := 0 @@ -54,19 +61,19 @@ func XXD(r io.Reader, w io.Writer) error { w.Write(hexChar) if i%2 == 1 { - io.WriteString(w, " ") + w.Write(space) } } if n < len(buf) { for i := n; i < len(buf); i++ { - io.WriteString(w, " ") + w.Write(doubleSpace) if i%2 == 1 { - io.WriteString(w, " ") + w.Write(space) } } } - io.WriteString(w, " ") + w.Write(space) // Character values b := buf[:n] @@ -74,11 +81,11 @@ func XXD(r io.Reader, w io.Writer) error { if c > 0x1f && c < 0x7f { io.WriteString(w, string(c)) } else { - io.WriteString(w, ".") + w.Write(dot) } } - io.WriteString(w, "\n") + w.Write(newline) } return nil }