From 44db7217089aa573472bdce02829528e8fb554eb Mon Sep 17 00:00:00 2001 From: Russ Ross Date: Fri, 24 Jun 2011 19:11:06 -0600 Subject: [PATCH] rewrite of attrEscape: cleaner and faster --- html.go | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/html.go b/html.go index 0dead5e..23e86dd 100644 --- a/html.go +++ b/html.go @@ -121,31 +121,39 @@ func HtmlTocRenderer(flags int) *Renderer { } func attrEscape(out *bytes.Buffer, src []byte) { - for i := 0; i < len(src); i++ { - // directly copy normal characters - org := i - for i < len(src) && src[i] != '<' && src[i] != '>' && src[i] != '&' && src[i] != '"' { - i++ - } - if i > org { - out.Write(src[org:i]) - } - - // escape a character - if i >= len(src) { - break - } - switch src[i] { + org := 0 + for i, ch := range src { + switch ch { case '<': + if i > org { + // copy all the normal characters since the last escape + out.Write(src[org:i]) + } + org = i + 1 out.WriteString("<") case '>': + if i > org { + out.Write(src[org:i]) + } + org = i + 1 out.WriteString(">") case '&': + if i > org { + out.Write(src[org:i]) + } + org = i + 1 out.WriteString("&") case '"': + if i > org { + out.Write(src[org:i]) + } + org = i + 1 out.WriteString(""") } } + if org < len(src) { + out.Write(src[org:]) + } } func htmlHeader(out *bytes.Buffer, text []byte, level int, opaque interface{}) {