all repos — grayfriday @ 44db7217089aa573472bdce02829528e8fb554eb

blackfriday fork with a few changes

rewrite of attrEscape: cleaner and faster
Russ Ross russ@russross.com
Fri, 24 Jun 2011 19:11:06 -0600
commit

44db7217089aa573472bdce02829528e8fb554eb

parent

a74678bf51d2cdcdeb81e3dae3ec5a6eb13cd432

1 files changed, 23 insertions(+), 15 deletions(-)

jump to
M html.gohtml.go

@@ -121,30 +121,38 @@ return r

} 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("&lt;") case '>': + if i > org { + out.Write(src[org:i]) + } + org = i + 1 out.WriteString("&gt;") case '&': + if i > org { + out.Write(src[org:i]) + } + org = i + 1 out.WriteString("&amp;") case '"': + if i > org { + out.Write(src[org:i]) + } + org = i + 1 out.WriteString("&quot;") } + } + if org < len(src) { + out.Write(src[org:]) } }