Skip to content

Commit 31bbeb2

Browse files
committed
Cleanup codes
1 parent 85b270b commit 31bbeb2

File tree

5 files changed

+26
-67
lines changed

5 files changed

+26
-67
lines changed

_benchmark/cmark/Makefile

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,22 @@ ifeq ($(OS),Windows_NT)
44
CMARK_BIN=cmark_benchmark.exe
55
CMARK_RUN=bash -c "PATH=./cmark-master/build/src:$${PATH} ./$(CMARK_BIN)"
66
endif
7-
ifneq ($(WSL_INTEROP),)
8-
CMARK_BIN=cmark_benchmark.exe
9-
CMARK_RUN=cp ./cmark-master/build-mingw/windows/bin/libcmark.dll . && ./$(CMARK_BIN); rm -f libcmark.dll
10-
endif
117

128
.PHONY: run
139

1410
run: $(CMARK_BIN)
1511
@ $(CMARK_RUN)
16-
@ if [ -z "$${WSL_INTEROP}" ]; then \
17-
go run ./goldmark_benchmark.go; \
18-
else \
19-
GOOS=windows GOARCH=amd64 go build -o goldmark_benchmark.exe ./goldmark_benchmark.go && ./goldmark_benchmark.exe; \
20-
fi
12+
go run ./goldmark_benchmark.go
2113

2214
./cmark-master/Makefile:
2315
wget -nc -O cmark.zip https://github.com/commonmark/cmark/archive/master.zip
2416
unzip cmark.zip
2517
rm -f cmark.zip
26-
@ if [ -z "$${WSL_INTEROP}" ]; then \
27-
cd cmark-master && make; \
28-
else \
29-
cd cmark-master && make mingw; \
30-
fi
18+
cd cmark-master && make
3119

32-
$(CMARK_BIN): ./cmark-master/Makefile
33-
@ if [ -z "$${WSL_INTEROP}" ]; then \
34-
gcc -I./cmark-master/build/src -I./cmark-master/src cmark_benchmark.c -o $(CMARK_BIN) -L./cmark-master/build/src -lcmark; \
35-
else \
36-
i686-w64-mingw32-gcc -I./cmark-master/build-mingw/windows/include cmark_benchmark.c -o $(CMARK_BIN) -L./cmark-master/build-mingw/windows/lib -lcmark.dll; \
37-
fi
20+
$(CMARK_BIN): ./cmark-master/Makefile cmark_benchmark.c
21+
gcc -I./cmark-master/build/src -I./cmark-master/src cmark_benchmark.c -o $(CMARK_BIN) -L./cmark-master/build/src -lcmark; \
3822

3923
.PHONY: clean
4024
clean:
4125
rm -f $(CMARK_BIN)
42-
rm -f goldmark_benchmark.exe

ast/block.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
// A BaseBlock struct implements the Node interface partialliy.
1111
type BaseBlock struct {
1212
BaseNode
13+
lines textm.Segments
1314
blankPreviousLines bool
14-
lines *textm.Segments
1515
}
1616

1717
// Type implements Node.Type.
@@ -36,15 +36,12 @@ func (b *BaseBlock) SetBlankPreviousLines(v bool) {
3636

3737
// Lines implements Node.Lines.
3838
func (b *BaseBlock) Lines() *textm.Segments {
39-
if b.lines == nil {
40-
b.lines = textm.NewSegments()
41-
}
42-
return b.lines
39+
return &b.lines
4340
}
4441

4542
// SetLines implements Node.SetLines.
4643
func (b *BaseBlock) SetLines(v *textm.Segments) {
47-
b.lines = v
44+
b.lines = *v
4845
}
4946

5047
// A Document struct is a root node of Markdown text.

parser/parser.go

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,47 +1030,35 @@ type lineStat struct {
10301030
}
10311031

10321032
func isBlankLine(lineNum, level int, stats []lineStat) bool {
1033-
ret := true
1034-
for i := len(stats) - 1 - level; i >= 0; i-- {
1035-
ret = false
1033+
l := len(stats)
1034+
if l == 0 {
1035+
return true
1036+
}
1037+
for i := l - 1 - level; i >= 0; i-- {
10361038
s := stats[i]
1037-
if s.lineNum == lineNum {
1038-
if s.level < level && s.isBlank {
1039-
return true
1040-
} else if s.level == level {
1041-
return s.isBlank
1042-
}
1043-
}
1044-
if s.lineNum < lineNum {
1045-
return ret
1039+
if s.lineNum == lineNum && s.level <= level {
1040+
return s.isBlank
1041+
} else if s.lineNum < lineNum {
1042+
break
10461043
}
10471044
}
1048-
return ret
1045+
return false
10491046
}
10501047

10511048
func (p *parser) parseBlocks(parent ast.Node, reader text.Reader, pc Context) {
1052-
pc.SetOpenedBlocks([]Block{})
1049+
pc.SetOpenedBlocks(nil)
10531050
blankLines := make([]lineStat, 0, 128)
1054-
var isBlank bool
10551051
for { // process blocks separated by blank lines
1056-
_, lines, ok := reader.SkipBlankLines()
1052+
_, _, ok := reader.SkipBlankLines()
10571053
if !ok {
10581054
return
10591055
}
1060-
lineNum, _ := reader.Position()
1061-
if lines != 0 {
1062-
blankLines = blankLines[0:0]
1063-
l := len(pc.OpenedBlocks())
1064-
for i := 0; i < l; i++ {
1065-
blankLines = append(blankLines, lineStat{lineNum - 1, i, lines != 0})
1066-
}
1067-
}
1068-
isBlank = isBlankLine(lineNum-1, 0, blankLines)
10691056
// first, we try to open blocks
1070-
if p.openBlocks(parent, isBlank, reader, pc) != newBlocksOpened {
1057+
if p.openBlocks(parent, true, reader, pc) != newBlocksOpened {
10711058
return
10721059
}
10731060
reader.AdvanceLine()
1061+
blankLines = blankLines[0:0]
10741062
for { // process opened blocks line by line
10751063
openedBlocks := pc.OpenedBlocks()
10761064
l := len(openedBlocks)
@@ -1096,15 +1084,15 @@ func (p *parser) parseBlocks(parent ast.Node, reader text.Reader, pc Context) {
10961084
// When current node is a container block and has no children,
10971085
// we try to open new child nodes
10981086
if state&HasChildren != 0 && i == lastIndex {
1099-
isBlank = isBlankLine(lineNum-1, i+1, blankLines)
1087+
isBlank := isBlankLine(lineNum-1, i+1, blankLines)
11001088
p.openBlocks(be.Node, isBlank, reader, pc)
11011089
break
11021090
}
11031091
continue
11041092
}
11051093
}
11061094
// current node may be closed or lazy continuation
1107-
isBlank = isBlankLine(lineNum-1, i, blankLines)
1095+
isBlank := isBlankLine(lineNum-1, i, blankLines)
11081096
thisParent := parent
11091097
if i != 0 {
11101098
thisParent = openedBlocks[i-1].Node

text/reader.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,9 @@ func (r *reader) AdvanceLine() {
229229
return
230230
}
231231
r.pos.Stop = r.sourceLength
232-
for i := r.pos.Start; i < r.sourceLength; i++ {
233-
c := r.source[i]
234-
if c == '\n' {
235-
r.pos.Stop = i + 1
236-
break
237-
}
232+
i := bytes.IndexByte(r.source[r.pos.Start:], '\n')
233+
if i != -1 {
234+
r.pos.Stop = r.pos.Start + i + 1
238235
}
239236
r.line++
240237
r.pos.Padding = 0

text/segment.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,11 @@ func NewSegments() *Segments {
176176

177177
// Append appends the given segment after the tail of the collection.
178178
func (s *Segments) Append(t Segment) {
179-
if s.values == nil {
180-
s.values = make([]Segment, 0, 20)
181-
}
182179
s.values = append(s.values, t)
183180
}
184181

185182
// AppendAll appends all elements of given segments after the tail of the collection.
186183
func (s *Segments) AppendAll(t []Segment) {
187-
if s.values == nil {
188-
s.values = make([]Segment, 0, 20)
189-
}
190184
s.values = append(s.values, t...)
191185
}
192186

0 commit comments

Comments
 (0)