Simplify inline() even more

* Move handler call inside the inner loop's 'if handler != nil' clause
* Move appender of possible tail bytes outside of loop
* Get rid of outer loop
* Rename i -> beg

Again, this does not seem to gain much performance, but makes the code
significantly more readable.
This commit is contained in:
Vytautas Saltenis 2016-10-29 11:05:26 +03:00
parent 6438ce6de8
commit 9357a8f949

View File

@ -33,49 +33,38 @@ var (
// offset is the number of valid chars before the current cursor
func (p *parser) inline(currBlock *Node, data []byte) {
// this is called recursively: enforce a maximum depth
// handlers might call us recursively: enforce a maximum depth
if p.nesting >= p.maxNesting || len(data) == 0 {
return
}
p.nesting++
i, end := 0, 0
var handler inlineParser
for {
for ; end < len(data); end++ {
handler = p.inlineCallback[data[end]]
if handler != nil {
break
beg, end := 0, 0
for end < len(data) {
handler := p.inlineCallback[data[end]]
if handler != nil {
if consumed, node := handler(p, data, end); consumed == 0 {
// No action from the callback.
end++
} else {
// Copy inactive chars into the output.
currBlock.AppendChild(text(data[beg:end]))
if node != nil {
currBlock.AppendChild(node)
}
// Skip past whatever the callback used.
beg = end + consumed
end = beg
}
}
if end >= len(data) {
if data[end-1] == '\n' {
end--
}
currBlock.AppendChild(text(data[i:end]))
break
}
// call the trigger
if consumed, node := handler(p, data, end); consumed == 0 {
// No action from the callback.
end++
} else {
// Copy inactive chars into the output.
currBlock.AppendChild(text(data[i:end]))
if node != nil {
currBlock.AppendChild(node)
}
// Skip past whatever the callback used.
i = end + consumed
if i >= len(data) {
break
}
end = i
end++
}
}
if beg < len(data) {
if data[end-1] == '\n' {
end--
}
currBlock.AppendChild(text(data[beg:end]))
}
p.nesting--
}