Skip to content

Commit

Permalink
replacing string_matches with one-liner
Browse files Browse the repository at this point in the history
  • Loading branch information
vonDonnerstein committed Dec 30, 2016
1 parent 04fc079 commit 9e97458
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 21 deletions.
18 changes: 0 additions & 18 deletions src/PEGParser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,6 @@ function make_node(rule, value, first, last, children::Array)
return result
end


# TODO: there should be string functions that already do this
function string_matches(expected::Char, actual::AbstractString, first::Int, last::Int)
if first > length(actual)
return false
end

return char(actual[first]) == expected;
end

function string_matches(expected::AbstractString, actual::AbstractString, first::Int, last::Int)
if last - 1 > length(actual)
return false;
end

return expected == actual[first:last-1];
end

include("parse.jl")

end
5 changes: 2 additions & 3 deletions src/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,12 @@ end
function parse_newcachekey(grammar::Grammar, rule::Terminal, text::AbstractString, pos::Int, cache)
local size::Int = length(rule.value)

if string_matches(rule.value, text, pos, pos+size)
size = length(rule.value)
if ismatch(Regex("\^$(rule.value)"),text[pos:end])
node = make_node(rule, text[pos:pos+size-1], pos, pos+size, [])
return (node, pos+size, nothing)
end

len = min(pos+length(rule.value)-1, length(text))
len = min(pos+size-1, length(text))
return (nothing, pos, ParseError("'$(text[pos:len])' does not match '$(rule.value)'. At pos: $pos"))
end

Expand Down

0 comments on commit 9e97458

Please sign in to comment.