Avoid calling bytes.Split() in appendLanguageAttr This avoids some allocs.
Vytautas Saltenis vytas@uber.com
Sat, 29 Oct 2016 12:09:34 +0300
1 files changed,
7 insertions(+),
4 deletions(-)
jump to
M
html.go
→
html.go
@@ -307,11 +307,14 @@ return pt != Link && pt != CodeBlock && pt != Code
} func appendLanguageAttr(attrs []string, info []byte) []string { - infoWords := bytes.Split(info, []byte("\t ")) - if len(infoWords) > 0 && len(infoWords[0]) > 0 { - attrs = append(attrs, fmt.Sprintf("class=\"language-%s\"", infoWords[0])) + if len(info) == 0 { + return attrs + } + endOfLang := bytes.IndexAny(info, "\t ") + if endOfLang < 0 { + endOfLang = len(info) } - return attrs + return append(attrs, fmt.Sprintf("class=\"language-%s\"", info[:endOfLang])) } func (r *HTMLRenderer) tag(w io.Writer, name []byte, attrs []string) {