diff options
author | Darren McCleary <darren.rmc@gmail.com> | 2017-08-30 13:26:28 -0400 |
---|---|---|
committer | Darren McCleary <darren.rmc@gmail.com> | 2017-08-30 13:26:28 -0400 |
commit | 7c53431dc36059d8b42cf5a946b1f88ab54a28a4 (patch) | |
tree | 1cfd77c877dcf5af7a468249907ebd6db6fa1626 | |
parent | 41ab61b9c9cea6554839b654462e207b251bfa40 (diff) |
Added flag to print PDF byte diffs
-rw-r--r-- | compare.go | 16 |
1 files changed, 8 insertions, 8 deletions
@@ -75,9 +75,9 @@ func writeBytes(leadStr string, startPos int, sl []byte) { fmt.Printf("|\n") } -func checkBytes(pos int, sl1, sl2 []byte) (eq bool) { +func checkBytes(pos int, sl1, sl2 []byte, printDiff bool) (eq bool) { eq = bytes.Equal(sl1, sl2) - if !eq { + if !eq && printDiff { writeBytes("<", pos, sl1) writeBytes(">", pos, sl2) } @@ -86,7 +86,7 @@ func checkBytes(pos int, sl1, sl2 []byte) (eq bool) { // CompareBytes compares the bytes referred to by sl1 with those referred to by // sl2. Nil is returned if the buffers are equal, otherwise an error. -func CompareBytes(sl1, sl2 []byte) (err error) { +func CompareBytes(sl1, sl2 []byte, printDiff bool) (err error) { var posStart, posEnd, len1, len2, length int var diffs bool @@ -101,7 +101,7 @@ func CompareBytes(sl1, sl2 []byte) (err error) { if posEnd > length { posEnd = length } - if !checkBytes(posStart, sl1[posStart:posEnd], sl2[posStart:posEnd]) { + if !checkBytes(posStart, sl1[posStart:posEnd], sl2[posStart:posEnd], printDiff) { diffs = true } posStart = posEnd @@ -115,13 +115,13 @@ func CompareBytes(sl1, sl2 []byte) (err error) { // ComparePDFs reads and compares the full contents of the two specified // readers byte-for-byte. Nil is returned if the buffers are equal, otherwise // an error. -func ComparePDFs(rdr1, rdr2 io.Reader) (err error) { +func ComparePDFs(rdr1, rdr2 io.Reader, printDiff bool) (err error) { var b1, b2 *bytes.Buffer _, err = b1.ReadFrom(rdr1) if err == nil { _, err = b2.ReadFrom(rdr2) if err == nil { - err = CompareBytes(b1.Bytes(), b2.Bytes()) + err = CompareBytes(b1.Bytes(), b2.Bytes(), printDiff) } } return @@ -130,13 +130,13 @@ func ComparePDFs(rdr1, rdr2 io.Reader) (err error) { // ComparePDFFiles reads and compares the full contents of the two specified // files byte-for-byte. Nil is returned if the file contents are equal, or if // the second file is missing, otherwise an error. -func ComparePDFFiles(file1Str, file2Str string) (err error) { +func ComparePDFFiles(file1Str, file2Str string, printDiff bool) (err error) { var sl1, sl2 []byte sl1, err = ioutil.ReadFile(file1Str) if err == nil { sl2, err = ioutil.ReadFile(file2Str) if err == nil { - err = CompareBytes(sl1, sl2) + err = CompareBytes(sl1, sl2, printDiff) } else { // Second file is missing; treat this as success err = nil |