Skip to content

Commit

Permalink
Merge pull request #19 from asterite/optimizations
Browse files Browse the repository at this point in the history
Optimizations
  • Loading branch information
icyleaf authored Sep 11, 2019
2 parents f357c7a + 50ee837 commit d6fc062
Show file tree
Hide file tree
Showing 9 changed files with 275 additions and 185 deletions.
7 changes: 5 additions & 2 deletions src/markd/node.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module Markd

property type : Type

property data = {} of String => DataValue
property(data) { {} of String => DataValue }
property source_pos = { {1, 1}, {0, 0} }
property text = ""
property? open = true
Expand Down Expand Up @@ -126,7 +126,10 @@ module Markd
io << " @type=" << @type
io << " @parent=" << @parent if @parent
io << " @next=" << @next if @next
io << " @data=" << @data if @data.size > 0

data = @data
io << " @data=" << data if data && !data.empty?

io << ">"
nil
end
Expand Down
28 changes: 11 additions & 17 deletions src/markd/parsers/block.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ module Markd::Parser
@oldtip = @tip
@last_matched_container = @tip

@lines = [] of String
@line = ""

@current_line = 0
@line_size = 0
@offset = 0
@column = 0
@last_line_length = 0
Expand All @@ -53,12 +51,8 @@ module Markd::Parser
end

def parse(source : String)
Utils.timer("preparing input", @options.time) do
prepare_input(source)
end

Utils.timer("block parsing", @options.time) do
parse_blocks
parse_blocks(source)
end

Utils.timer("inline parsing", @options.time) do
Expand All @@ -68,18 +62,18 @@ module Markd::Parser
@document
end

private def prepare_input(source)
@lines = source.lines
@line_size = @lines.size
# ignore last blank line created by final newline
@line_size -= 1 if source[-1]? == '\n'
end
private def parse_blocks(source)
lines_size = 0
source.each_line do |line|
process_line(line)
lines_size += 1
end

private def parse_blocks
@lines.each { |line| process_line(line) }
# ignore last blank line created by final newline
lines_size -= 1 if source.ends_with?('\n')

while tip = tip?
token(tip, @line_size)
token(tip, lines_size)
end
end

Expand Down Expand Up @@ -132,7 +126,7 @@ module Markd::Parser
end
end

matched = RULES.values.each do |rule|
matched = RULES.each_value do |rule|
case rule.match(self, container)
when Rule::MatchValue::Container
container = tip
Expand Down
Loading

0 comments on commit d6fc062

Please sign in to comment.