@@ -2,7 +2,6 @@ package ext4
22
33import (
44 "bytes"
5- "fmt"
65 "testing"
76)
87
@@ -64,137 +63,3 @@ func TestMinString(t *testing.T) {
6463 })
6564 }
6665}
67-
68- // dumpByteSlice dump a byte slice in hex and optionally ASCII format.
69- // Optionally but position at the beginning of each row, like xxd.
70- // Optionally convert to ASCII at end of each row, like xxd.
71- // Can show positions at beginning of each row in hex, decimal or both.
72- // Can filter out all rows except those containing given positions in showOnlyBytes. If showOnlyBytes is nil, all rows are shown.
73- // If showOnlyBytes is not nil, even an empty slice, will only show those rows that contain the given positions.
74- func dumpByteSlice (b []byte , bytesPerRow int , showASCII , showPosHex , showPosDec bool , showOnlyBytes []int ) (out string ) {
75- var ascii []byte
76- // go through each byte.
77- // At each position:
78- // - if we are at the end of a row, print the ASCII representation of the row.
79- // - if we are at the middle of a row, add an extra space
80- // - if we are still in the byte slice, print the byte in hex with a space before it.
81- // - if we are past the end of the row, print spaces.
82- showOnlyMap := make (map [int ]bool )
83- for _ , v := range showOnlyBytes {
84- showOnlyMap [v ] = true
85- }
86- // run by rows
87- numRows := len (b ) / bytesPerRow
88- if len (b )% bytesPerRow != 0 {
89- numRows ++
90- }
91- for i := 0 ; i < numRows ; i ++ {
92- firstByte := i * bytesPerRow
93- lastByte := firstByte + bytesPerRow
94- var row string
95- // row header includes optional position numbers
96- if showPosHex {
97- row += fmt .Sprintf ("%08x " , firstByte )
98- }
99- if showPosDec {
100- row += fmt .Sprintf ("%4d " , firstByte )
101- }
102- row += ": "
103- for j := firstByte ; j < lastByte ; j ++ {
104- // every 8 bytes add extra spacing to make it easier to read
105- if j % 8 == 0 {
106- row += " "
107- }
108- // regular byte, print in hex
109- if j < len (b ) {
110- hex := fmt .Sprintf (" %02x" , b [j ])
111- if showOnlyBytes != nil && showOnlyMap [j ] {
112- hex = "\033 [1m\033 [31m" + hex + "\033 [0m"
113- }
114- row += hex
115- } else {
116- row += " "
117- }
118- switch {
119- case j >= len (b ):
120- // past end of byte slice, print spaces
121- ascii = append (ascii , ' ' )
122- case b [j ] < 32 || b [j ] > 126 :
123- // unprintable characters, print a dot
124- ascii = append (ascii , '.' )
125- default :
126- // printable characters, print the character
127- ascii = append (ascii , b [j ])
128- }
129- }
130- // end of row, print the ASCII representation and a newline
131- if showASCII {
132- row += fmt .Sprintf (" %s" , string (ascii ))
133- ascii = ascii [:0 ]
134- }
135- row += "\n "
136-
137- // calculate if we should include this row
138- var includeRow = true
139- if showOnlyBytes != nil {
140- includeRow = false
141- for j := firstByte ; j < lastByte ; j ++ {
142- if showOnlyMap [j ] {
143- includeRow = true
144- break
145- }
146- }
147- }
148- if includeRow {
149- out += row
150- }
151- }
152- return out
153- }
154-
155- // diff
156- type diff struct {
157- Offset int
158- ByteA byte
159- ByteB byte
160- }
161-
162- // compareByteSlices compares two byte slices position by position. If the byte slices are identical, diffs is length 0,
163- // otherwise it contains the positions of the differences.
164- func compareByteSlices (a , b []byte ) (diffs []diff ) {
165- maxSize := len (a )
166- if len (b ) > maxSize {
167- maxSize = len (b )
168- }
169- for i := 0 ; i < maxSize ; i ++ {
170- switch {
171- case i >= len (a ):
172- diffs = append (diffs , diff {Offset : i , ByteA : 0 , ByteB : b [i ]})
173- case i >= len (b ):
174- diffs = append (diffs , diff {Offset : i , ByteA : a [i ], ByteB : 0 })
175- case a [i ] != b [i ]:
176- diffs = append (diffs , diff {Offset : i , ByteA : a [i ], ByteB : b [i ]})
177- }
178- }
179- return diffs
180- }
181-
182- // dumpByteSlicesWithDiffs show two byte slices in hex and ASCII format, with differences highlighted.
183- //
184- //nolint:unparam // sure, bytesPerRow always is 32, but it could be something else
185- func dumpByteSlicesWithDiffs (a , b []byte , bytesPerRow int , showASCII , showPosHex , showPosDec bool ) (different bool , out string ) {
186- diffs := compareByteSlices (a , b )
187- // if there are no differences, just return an empty string
188- if len (diffs ) == 0 {
189- return false , ""
190- }
191-
192- showOnlyBytes := make ([]int , len (diffs ))
193- for i , d := range diffs {
194- showOnlyBytes [i ] = d .Offset
195- }
196- out = dumpByteSlice (a , bytesPerRow , showASCII , showPosHex , showPosDec , showOnlyBytes )
197- out += "\n "
198- out += dumpByteSlice (b , bytesPerRow , showASCII , showPosHex , showPosDec , showOnlyBytes )
199- return true , out
200- }
0 commit comments