Commit e17405d7 authored by Joe Tsai's avatar Joe Tsai Committed by Joe Tsai

archive/tar: simplify bytediff logic

The encoding/hex package provides a nice Dump formatter that
prints both hex and ASCII. Use that instead for better visual
debugging of binary diffs.

Change-Id: Iad1084e8e52d7d523595e97ae20912657cea2ab5
Reviewed-on: https://go-review.googlesource.com/14729
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 01e45c73
...@@ -6,7 +6,7 @@ package tar ...@@ -6,7 +6,7 @@ package tar
import ( import (
"bytes" "bytes"
"fmt" "encoding/hex"
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
...@@ -18,40 +18,26 @@ import ( ...@@ -18,40 +18,26 @@ import (
"time" "time"
) )
// Render byte array in a two-character hexadecimal string, spaced for easy visual inspection. // Render a pseudo-diff between two blocks of bytes.
func bytestr(offset int, b []byte) string { func bytediff(a []byte, b []byte) (s string) {
const rowLen = 32 var ax = strings.Split(hex.Dump(a), "\n")
s := fmt.Sprintf("%04x ", offset) var bx = strings.Split(hex.Dump(b), "\n")
for _, ch := range b { for i := 0; i < len(ax) || i < len(bx); i++ {
switch { var sa, sb = "", ""
case '0' <= ch && ch <= '9', 'A' <= ch && ch <= 'Z', 'a' <= ch && ch <= 'z': if i < len(ax) {
s += fmt.Sprintf(" %c", ch) sa = ax[i]
default:
s += fmt.Sprintf(" %02x", ch)
} }
if i < len(bx) {
sb = bx[i]
} }
return s
}
// Render a pseudo-diff between two blocks of bytes.
func bytediff(a []byte, b []byte) string {
const rowLen = 32
s := fmt.Sprintf("(%d bytes vs. %d bytes)\n", len(a), len(b))
for offset := 0; len(a)+len(b) > 0; offset += rowLen {
na, nb := rowLen, rowLen
if na > len(a) {
na = len(a)
}
if nb > len(b) {
nb = len(b)
}
sa := bytestr(offset, a[0:na])
sb := bytestr(offset, b[0:nb])
if sa != sb { if sa != sb {
s += fmt.Sprintf("-%v\n+%v\n", sa, sb) if len(sa) > 0 {
s += "+" + sa + "\n"
}
if len(sb) > 0 {
s += "-" + sb + "\n"
}
} }
a = a[na:]
b = b[nb:]
} }
return s return s
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment