preformat: use bytes.Buffer instead of concatenating strings

This commit is contained in:
Michael Teichgräber 2010-11-24 18:54:12 +01:00
parent 67459a2e6a
commit 4995379a8e

View File

@ -21,6 +21,7 @@ package markdown
import ( import (
"strings" "strings"
"bytes"
) )
// Markdown Extensions: // Markdown Extensions:
@ -115,16 +116,17 @@ func preformat(text string) (s string) {
charstotab := TABSTOP charstotab := TABSTOP
i0 := 0 i0 := 0
b := bytes.NewBuffer(make([]byte, 0, len(text)+256))
for i, _ := range text { for i, _ := range text {
switch text[i] { switch text[i] {
case '\t': case '\t':
s += text[i0:i] b.WriteString(text[i0:i])
for ; charstotab > 0; charstotab-- { for ; charstotab > 0; charstotab-- {
s += " " b.WriteByte(' ')
} }
i0 = i + 1 i0 = i + 1
case '\n': case '\n':
s += text[i0 : i+1] b.WriteString(text[i0 : i+1])
i0 = i + 1 i0 = i + 1
charstotab = TABSTOP charstotab = TABSTOP
default: default:
@ -134,5 +136,7 @@ func preformat(text string) (s string) {
charstotab = TABSTOP charstotab = TABSTOP
} }
} }
return s + text[i0:] + "\n\n" b.WriteString(text[i0:])
b.WriteString("\n\n")
return b.String()
} }