forked from elliotchance/c2go
/
test.go
40 lines (34 loc) · 934 Bytes
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package util
import (
"fmt"
"math"
"strconv"
"strings"
)
// ShowDiff will print two strings vertically next to each other so that line
// differences are easier to read.
func ShowDiff(a, b string) string {
aLines := strings.Split(a, "\n")
bLines := strings.Split(b, "\n")
maxLines := int(math.Max(float64(len(aLines)), float64(len(bLines))))
out := "\n"
for lineNumber := 0; lineNumber < maxLines; lineNumber++ {
aLine := ""
bLine := ""
// Replace NULL characters with a dot. Otherwise the strings will look
// exactly the same but have different length (and therfore not be
// equal).
if lineNumber < len(aLines) {
aLine = strconv.Quote(aLines[lineNumber])
}
if lineNumber < len(bLines) {
bLine = strconv.Quote(bLines[lineNumber])
}
diffFlag := " "
if aLine != bLine {
diffFlag = "*"
}
out += fmt.Sprintf("%s %3d %-40s%-40s\n", diffFlag, lineNumber+1, aLine, bLine)
}
return out
}