mirror of
https://github.com/russross/blackfriday.git
synced 2024-03-22 13:40:34 +08:00
461803619b
Simplify and optimize escapeHTML as per @Ambrevar's suggestion: lean on the fact that we're dealing with bytes: declare a 256-element array with most of it filled with zeros, except for the few slots that need escaping. This avoids some conditionals in a tight loop. Also, uncomment it's benchmark.
35 lines
569 B
Go
35 lines
569 B
Go
package blackfriday
|
|
|
|
import (
|
|
"html"
|
|
"io"
|
|
)
|
|
|
|
var htmlEscaper = [256][]byte{
|
|
'&': []byte("&"),
|
|
'<': []byte("<"),
|
|
'>': []byte(">"),
|
|
'"': []byte("""),
|
|
}
|
|
|
|
func escapeHTML(w io.Writer, s []byte) {
|
|
var start, end int
|
|
for end < len(s) {
|
|
escSeq := htmlEscaper[s[end]]
|
|
if escSeq != nil {
|
|
w.Write(s[start:end])
|
|
w.Write(escSeq)
|
|
start = end + 1
|
|
}
|
|
end++
|
|
}
|
|
if start < len(s) && end <= len(s) {
|
|
w.Write(s[start:end])
|
|
}
|
|
}
|
|
|
|
func escLink(w io.Writer, text []byte) {
|
|
unesc := html.UnescapeString(string(text))
|
|
escapeHTML(w, []byte(unesc))
|
|
}
|