]> Cypherpunks repositories - gostls13.git/commitdiff
archive/tar: simplify bytediff logic
authorJoe Tsai <joetsai@digital-static.net>
Fri, 18 Sep 2015 21:30:15 +0000 (14:30 -0700)
committerJoe Tsai <thebrokentoaster@gmail.com>
Fri, 11 Aug 2017 03:12:02 +0000 (03:12 +0000)
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: Ian Lance Taylor <iant@golang.org>
src/archive/tar/writer_test.go

index d88b8f41ca8a8c49ff916dcba20a27b4ac3b17f8..7712217cd8c98838b2a15d0832fc3dbc6bda6ce4 100644 (file)
@@ -6,7 +6,7 @@ package tar
 
 import (
        "bytes"
-       "fmt"
+       "encoding/hex"
        "io"
        "io/ioutil"
        "os"
@@ -18,40 +18,26 @@ import (
        "time"
 )
 
-// Render byte array in a two-character hexadecimal string, spaced for easy visual inspection.
-func bytestr(offset int, b []byte) string {
-       const rowLen = 32
-       s := fmt.Sprintf("%04x ", offset)
-       for _, ch := range b {
-               switch {
-               case '0' <= ch && ch <= '9', 'A' <= ch && ch <= 'Z', 'a' <= ch && ch <= 'z':
-                       s += fmt.Sprintf("  %c", ch)
-               default:
-                       s += fmt.Sprintf(" %02x", ch)
-               }
-       }
-       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)
+func bytediff(a []byte, b []byte) (s string) {
+       var ax = strings.Split(hex.Dump(a), "\n")
+       var bx = strings.Split(hex.Dump(b), "\n")
+       for i := 0; i < len(ax) || i < len(bx); i++ {
+               var sa, sb = "", ""
+               if i < len(ax) {
+                       sa = ax[i]
                }
-               if nb > len(b) {
-                       nb = len(b)
+               if i < len(bx) {
+                       sb = bx[i]
                }
-               sa := bytestr(offset, a[0:na])
-               sb := bytestr(offset, b[0:nb])
                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
 }