From fa865c7dbc7f64c8b6fe6e12165b9d72de1ca919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Teichgr=C3=A4ber?= Date: Fri, 3 Dec 2010 22:20:41 +0100 Subject: [PATCH] output.go: make htmlOut.str faster --- output.go | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/output.go b/output.go index beb8171..7d33c5e 100644 --- a/output.go +++ b/output.go @@ -80,28 +80,42 @@ func (w *htmlOut) s(s string) *htmlOut { /* print string, escaping for HTML * If obfuscate selected, convert characters to hex or decimal entities at random */ -func (w *htmlOut) str(hs string, obfuscate bool) *htmlOut { - for _, r := range hs { +func (w *htmlOut) str(s string, obfuscate bool) *htmlOut { + var ws string + var i0 = 0 + + for i, r := range s { switch r { case '&': - w.s("&") + ws = "&" case '<': - w.s("<") + ws = "<" case '>': - w.s(">") + ws = ">" case '"': - w.s(""") + ws = """ default: if obfuscate { if rand.Intn(1) == 0 { - w.s(fmt.Sprintf("&#%d;", r)) + ws = fmt.Sprintf("&#%d;", r) } else { - w.s(fmt.Sprintf("&#%x;", r)) + ws = fmt.Sprintf("&#%x;", r) } } else { - w.WriteRune(r) + if i0 == -1 { + i0 = i + } + continue } } + if i0 != -1 { + w.WriteString(s[i0:i]) + i0 = -1 + } + w.WriteString(ws) + } + if i0 != -1 { + w.WriteString(s[i0:]) } return w }