package format
import (
"bytes"
"html/template"
)
func detectLine(data []byte) (int, int) {
lineFeed := bytes.IndexByte(data, '\n')
if lineFeed == -1 {
lineFeed = len(data)
}
lineEnd := lineFeed
if lineEnd > 0 {
c := data[lineEnd - 1]
if c == '\r' {
lineEnd -= 1
}
}
nextLine := lineFeed + 1
return lineEnd, nextLine
}
func Diff(text string) string {
data := []byte(text)
index := 0
var html bytes.Buffer
lineNo := 0
for index < len(data) {
lineEnd, nextLine := detectLine(data[index:])
lineEnd += index
nextLine += index
line := data[index:lineEnd]
lineNo += 1
if lineNo < 3 {
index = nextLine
continue
}
if len(line) < 1 {
html.WriteString("
\n");
} else {
c := line[0]
htmlLine := template.HTMLEscapeString(string(line))
if c == '+' {
html.WriteString("+")
html.WriteString("" + htmlLine[1:] + "
\n")
} else if c == '-' {
html.WriteString("-")
html.WriteString("" + htmlLine[1:] + "
\n")
} else if c == '@' {
html.WriteString("@")
html.WriteString(htmlLine[1:] + "
\n")
} else if c == ' ' {
html.WriteString(" ")
html.WriteString(htmlLine[1:] + "
\n")
} else {
html.WriteString(htmlLine + "
\n")
}
}
index = nextLine
}
return html.String()
}