Skip to content

Commit

Permalink
optimize: static allocation for output symbols
Browse files Browse the repository at this point in the history
80.8 ns/op
  • Loading branch information
felixge committed Jan 11, 2015
1 parent 0d3ae7f commit a48d892
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions xxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -54,31 +61,31 @@ 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]
for _, c := range b {
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
}

0 comments on commit a48d892

Please sign in to comment.