add 'notesRecord' to check footnote existence fast

It is necessary to use vector for 'notes' instead of map to keep
footnotes ordered. But checking for presence in a vector is not
efficient. So add a map variable 'notesRecord' to tackle this problem.
pull/366/head
choueric 2017-06-09 10:20:58 +08:00
parent a20399916c
commit 8c89af6200
2 changed files with 7 additions and 8 deletions

View File

@ -488,6 +488,7 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
}
p.notes = append(p.notes, ref)
p.notesRecord[string(ref.link)] = true
link = ref.link
title = ref.title
@ -501,6 +502,7 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
if t == linkDeferredFootnote && !p.isFootnote(lr) {
lr.noteId = len(p.notes) + 1
p.notes = append(p.notes, lr)
p.notesRecord[string(lr.link)] = true
}
// keep link and title from reference

View File

@ -218,7 +218,8 @@ type parser struct {
// Footnotes need to be ordered as well as available to quickly check for
// presence. If a ref is also a footnote, it's stored both in refs and here
// in notes. Slice is nil if footnotes not enabled.
notes []*reference
notes []*reference
notesRecord map[string]bool
}
func (p *parser) getRef(refid string) (ref *reference, found bool) {
@ -242,13 +243,8 @@ func (p *parser) getRef(refid string) (ref *reference, found bool) {
}
func (p *parser) isFootnote(ref *reference) bool {
for _, v := range p.notes {
if string(ref.link) == string(v.link) {
return true
}
}
return false
_, ok := p.notesRecord[string(ref.link)]
return ok
}
//
@ -386,6 +382,7 @@ func MarkdownOptions(input []byte, renderer Renderer, opts Options) []byte {
if extensions&EXTENSION_FOOTNOTES != 0 {
p.notes = make([]*reference, 0)
p.notesRecord = make(map[string]bool)
}
first := firstPass(p, input)