From 7c53431dc36059d8b42cf5a946b1f88ab54a28a4 Mon Sep 17 00:00:00 2001 From: Darren McCleary Date: Wed, 30 Aug 2017 13:26:28 -0400 Subject: Added flag to print PDF byte diffs --- compare.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'compare.go') diff --git a/compare.go b/compare.go index 416e545..f0c286e 100644 --- a/compare.go +++ b/compare.go @@ -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 -- cgit v1.2.1-24-ge1ad