From 22a3e5b744f34ee53bd864384eedb821791d4951 Mon Sep 17 00:00:00 2001 From: Vytautas Saltenis Date: Sat, 29 Oct 2016 12:09:34 +0300 Subject: [PATCH] Avoid calling bytes.Split() in appendLanguageAttr This avoids some allocs. --- html.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/html.go b/html.go index 2785bba..527367d 100644 --- a/html.go +++ b/html.go @@ -307,11 +307,14 @@ func isSmartypantable(node *Node) bool { } 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 } - return attrs + endOfLang := bytes.IndexAny(info, "\t ") + if endOfLang < 0 { + endOfLang = len(info) + } + return append(attrs, fmt.Sprintf("class=\"language-%s\"", info[:endOfLang])) } func (r *HTMLRenderer) tag(w io.Writer, name []byte, attrs []string) {