all repos — grayfriday @ 77efab57b2f74dd3f9051c79752b2e8995c8b789

blackfriday fork with a few changes

Merge pull request #149 from tw4452852/fenced_code

Delete unnecessary copy of input when enable fenced code extension
Vytautas Ĺ altenis vytas@rtfb.lt
Wed, 11 Feb 2015 10:22:51 +0200
commit

77efab57b2f74dd3f9051c79752b2e8995c8b789

parent

4f4aec0109b267fb6886c09cfb8926a1db61519f

2 files changed, 21 insertions(+), 13 deletions(-)

jump to
M block.goblock.go

@@ -556,9 +556,12 @@ i, size := 0, 0

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 @@

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 @@

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 @@ }

i++ } else { - for !isspace(data[i]) { + for i < len(data) && !isspace(data[i]) { syn++ i++ }

@@ -629,10 +640,10 @@ language := string(data[syntaxStart : syntaxStart+syn])

*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 @@ }

// copy the current line end := beg - for data[end] != '\n' { + for end < len(data) && data[end] != '\n' { end++ } end++
M markdown.gomarkdown.go

@@ -327,10 +327,7 @@

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 }