diff --git a/html.go b/html.go index bce2809..2032b7d 100644 --- a/html.go +++ b/html.go @@ -127,45 +127,36 @@ func HtmlRenderer(flags int, title string, css string) Renderer { } } +// Using if statements is a bit faster than a switch statement. As the compiler +// improves, this should be unnecessary this is only worthwhile because +// attrEscape is the single largest CPU user in normal use. +// Also tried using map, but that gave a ~3x slowdown. +func escapeSingleChar(char byte) (string, bool) { + if char == '"' { + return """, true + } + if char == '&' { + return "&", true + } + if char == '<' { + return "<", true + } + if char == '>' { + return ">", true + } + return "", false +} + func attrEscape(out *bytes.Buffer, src []byte) { org := 0 for i, ch := range src { - // using if statements is a bit faster than a switch statement. - // as the compiler improves, this should be unnecessary - // this is only worthwhile because attrEscape is the single - // largest CPU user in normal use - if ch == '"' { + if entity, ok := escapeSingleChar(ch); ok { if i > org { // copy all the normal characters since the last escape out.Write(src[org:i]) } org = i + 1 - out.WriteString(""") - continue - } - if ch == '&' { - if i > org { - out.Write(src[org:i]) - } - org = i + 1 - out.WriteString("&") - continue - } - if ch == '<' { - if i > org { - out.Write(src[org:i]) - } - org = i + 1 - out.WriteString("<") - continue - } - if ch == '>' { - if i > org { - out.Write(src[org:i]) - } - org = i + 1 - out.WriteString(">") - continue + out.WriteString(entity) } } if org < len(src) {