performance fix: with autolinking on, it is almost twice as fast now
Russ Ross russ@russross.com
Mon, 30 May 2011 15:36:31 -0600
3 files changed,
28 insertions(+),
19 deletions(-)
M
html.go
→
html.go
@@ -427,9 +427,12 @@ * Pretty print: if we get an email address as
* an actual URI, e.g. `mailto:foo@bar.com`, we don't * want to print the `mailto:` prefix */ - if bytes.HasPrefix(link, []byte("mailto:")) { + switch { + case bytes.HasPrefix(link, []byte("mailto://")): + attrEscape(out, link[9:]) + case bytes.HasPrefix(link, []byte("mailto:")): attrEscape(out, link[7:]) - } else { + default: attrEscape(out, link) }
M
inline.go
→
inline.go
@@ -536,14 +536,22 @@ return end
} func inlineAutolink(out *bytes.Buffer, rndr *render, data []byte, offset int) int { + // quick check to rule out most false hits on ':' + if len(data) < offset + 3 || data[offset+1] != '/' || data[offset+2] != '/' { + return 0 + } + + // scan backward for a word boundary + rewind := 0 + for offset - rewind > 0 && rewind <= 7 && !isspace(data[offset-rewind-1]) && !isspace(data[offset-rewind-1]) { + rewind++ + } + if rewind > 6 { // longest supported protocol is "mailto" which has 6 letters + return 0 + } + orig_data := data - data = data[offset:] - - if offset > 0 { - if !isspace(orig_data[offset-1]) && !ispunct(orig_data[offset-1]) { - return 0 - } - } + data = data[offset-rewind:] if !isSafeLink(data) { return 0@@ -577,7 +585,7 @@ copen = 0
} if copen != 0 { - buf_end := offset + link_end - 2 + buf_end := offset - rewind + link_end - 2 open_delim := 1@@ -618,6 +626,11 @@ link_end--
} } + // we were triggered on the ':', so we need to rewind the output a bit + if out.Len() >= rewind { + out.Truncate(len(out.Bytes()) - rewind) + } + if rndr.mk.autolink != nil { u_link := bytes.NewBuffer(nil) unescapeText(u_link, data[:link_end])@@ -625,7 +638,7 @@
rndr.mk.autolink(out, u_link.Bytes(), LINK_TYPE_NORMAL, rndr.mk.opaque) } - return link_end + return link_end - rewind } var validUris = [][]byte{[]byte("http://"), []byte("https://"), []byte("ftp://"), []byte("mailto://")}
M
markdown.go
→
markdown.go
@@ -187,14 +187,7 @@ rndr.inline['\\'] = inlineEscape
rndr.inline['&'] = inlineEntity if extensions&EXTENSION_AUTOLINK != 0 { - rndr.inline['h'] = inlineAutolink // http, https - rndr.inline['H'] = inlineAutolink - - rndr.inline['f'] = inlineAutolink // ftp - rndr.inline['F'] = inlineAutolink - - rndr.inline['m'] = inlineAutolink // mailto - rndr.inline['M'] = inlineAutolink + rndr.inline[':'] = inlineAutolink } // first pass: look for references, copy everything else