Cleanup a random bunch of repetitive loops

Replace them with helper function calls.
This commit is contained in:
Vytautas Šaltenis 2015-04-07 21:59:42 +03:00
parent 36787eca3a
commit f4655604b3
2 changed files with 18 additions and 34 deletions

View File

@ -196,11 +196,8 @@ func (p *parser) prefixHeader(out *bytes.Buffer, data []byte) int {
for level < 6 && data[level] == '#' { for level < 6 && data[level] == '#' {
level++ level++
} }
i, end := 0, 0 i := skipChar(data, level, ' ')
for i = level; data[i] == ' '; i++ { end := skipUntilChar(data, i, '\n')
}
for end = i; data[end] != '\n'; end++ {
}
skip := end skip := end
id := "" id := ""
if p.flags&EXTENSION_HEADER_IDS != 0 { if p.flags&EXTENSION_HEADER_IDS != 0 {
@ -245,13 +242,8 @@ func (p *parser) prefixHeader(out *bytes.Buffer, data []byte) int {
func (p *parser) isUnderlinedHeader(data []byte) int { func (p *parser) isUnderlinedHeader(data []byte) int {
// test of level 1 header // test of level 1 header
if data[0] == '=' { if data[0] == '=' {
i := 1 i := skipChar(data, 1, '=')
for data[i] == '=' { i = skipChar(data, i, ' ')
i++
}
for data[i] == ' ' {
i++
}
if data[i] == '\n' { if data[i] == '\n' {
return 1 return 1
} else { } else {
@ -261,13 +253,8 @@ func (p *parser) isUnderlinedHeader(data []byte) int {
// test of level 2 header // test of level 2 header
if data[0] == '-' { if data[0] == '-' {
i := 1 i := skipChar(data, 1, '-')
for data[i] == '-' { i = skipChar(data, i, ' ')
i++
}
for data[i] == ' ' {
i++
}
if data[i] == '\n' { if data[i] == '\n' {
return 2 return 2
} else { } else {
@ -596,10 +583,7 @@ func (p *parser) isFencedCode(data []byte, syntax **string, oldmarker string) (s
if syntax != nil { if syntax != nil {
syn := 0 syn := 0
i = skipChar(data, i, ' ')
for i < len(data) && data[i] == ' ' {
i++
}
if i >= len(data) { if i >= len(data) {
return return
@ -643,9 +627,7 @@ func (p *parser) isFencedCode(data []byte, syntax **string, oldmarker string) (s
*syntax = &language *syntax = &language
} }
for i < len(data) && data[i] == ' ' { i = skipChar(data, i, ' ')
i++
}
if i >= len(data) || data[i] != '\n' { if i >= len(data) || data[i] != '\n' {
return return
} }
@ -674,11 +656,7 @@ func (p *parser) fencedCode(out *bytes.Buffer, data []byte, doRender bool) int {
} }
// copy the current line // copy the current line
end := beg end := skipUntilChar(data, beg, '\n') + 1
for end < len(data) && data[end] != '\n' {
end++
}
end++
// did we reach the end of the buffer without a closing marker? // did we reach the end of the buffer without a closing marker?
if end >= len(data) { if end >= len(data) {
@ -781,9 +759,7 @@ func (p *parser) tableHeader(out *bytes.Buffer, data []byte) (size int, columns
if data[i] == '|' && !isBackslashEscaped(data, i) { if data[i] == '|' && !isBackslashEscaped(data, i) {
i++ i++
} }
for data[i] == ' ' { i = skipChar(data, i, ' ')
i++
}
// each column header is of form: / *:?-+:? *|/ with # dashes + # colons >= 3 // each column header is of form: / *:?-+:? *|/ with # dashes + # colons >= 3
// and trailing | optional on last column // and trailing | optional on last column

View File

@ -867,6 +867,14 @@ func skipSpace(tag []byte, i int) int {
return i return i
} }
func skipChar(data []byte, start int, char byte) int {
i := start
for i < len(data) && data[i] == char {
i++
}
return i
}
func doubleSpace(out *bytes.Buffer) { func doubleSpace(out *bytes.Buffer) {
if out.Len() > 0 { if out.Len() > 0 {
out.WriteByte('\n') out.WriteByte('\n')