Merge pull request #149 from tw4452852/fenced_code

Delete unnecessary copy of input when enable fenced code extension
This commit is contained in:
Vytautas Šaltenis 2015-02-11 10:22:51 +02:00
commit 77efab57b2
2 changed files with 21 additions and 13 deletions

View File

@ -556,9 +556,12 @@ func (p *parser) isFencedCode(data []byte, syntax **string, oldmarker string) (s
skip = 0
// skip up to three spaces
for i < 3 && data[i] == ' ' {
for i < len(data) && i < 3 && data[i] == ' ' {
i++
}
if i >= len(data) {
return
}
// check for the marker characters: ~ or `
if data[i] != '~' && data[i] != '`' {
@ -568,11 +571,15 @@ func (p *parser) isFencedCode(data []byte, syntax **string, oldmarker string) (s
c := data[i]
// the whole line must be the same char or whitespace
for data[i] == c {
for i < len(data) && data[i] == c {
size++
i++
}
if i >= len(data) {
return
}
// the marker char must occur at least 3 times
if size < 3 {
return
@ -587,22 +594,26 @@ func (p *parser) isFencedCode(data []byte, syntax **string, oldmarker string) (s
if syntax != nil {
syn := 0
for data[i] == ' ' {
for i < len(data) && data[i] == ' ' {
i++
}
if i >= len(data) {
return
}
syntaxStart := i
if data[i] == '{' {
i++
syntaxStart++
for data[i] != '}' && data[i] != '\n' {
for i < len(data) && data[i] != '}' && data[i] != '\n' {
syn++
i++
}
if data[i] != '}' {
if i >= len(data) || data[i] != '}' {
return
}
@ -619,7 +630,7 @@ func (p *parser) isFencedCode(data []byte, syntax **string, oldmarker string) (s
i++
} else {
for !isspace(data[i]) {
for i < len(data) && !isspace(data[i]) {
syn++
i++
}
@ -629,10 +640,10 @@ func (p *parser) isFencedCode(data []byte, syntax **string, oldmarker string) (s
*syntax = &language
}
for data[i] == ' ' {
for i < len(data) && data[i] == ' ' {
i++
}
if data[i] != '\n' {
if i >= len(data) || data[i] != '\n' {
return
}
@ -661,7 +672,7 @@ func (p *parser) fencedCode(out *bytes.Buffer, data []byte, doRender bool) int {
// copy the current line
end := beg
for data[end] != '\n' {
for end < len(data) && data[end] != '\n' {
end++
}
end++

View File

@ -327,10 +327,7 @@ func firstPass(p *parser, input []byte) []byte {
if p.flags&EXTENSION_FENCED_CODE != 0 {
// when last line was none blank and a fenced code block comes after
if beg >= lastFencedCodeBlockEnd {
// tmp var so we don't modify beyond bounds of `input`
var tmp = make([]byte, len(input[beg:]), len(input[beg:])+1)
copy(tmp, input[beg:])
if i := p.fencedCode(&out, append(tmp, '\n'), false); i > 0 {
if i := p.fencedCode(&out, input[beg:], false); i > 0 {
if !lastLineWasBlank {
out.WriteByte('\n') // need to inject additional linebreak
}