Skip to content

Commit

Permalink
Change @scanner.match to respond nil/@scanner in order to impro…
Browse files Browse the repository at this point in the history
…ve processing speed.
  • Loading branch information
naitoh committed Jan 16, 2024
1 parent 61aff5c commit ba9f7fc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
3 changes: 1 addition & 2 deletions lib/rexml/parsers/baseparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,7 @@ def pull_event
return [ :elementdecl, @source.match( ELEMENTDECL_PATTERN, true )[1] ]

when ENTITY_START
match = @source.match( ENTITYDECL, true ).compact
match[0] = :entitydecl
match = [:entitydecl, *@source.match( ENTITYDECL, true ).captures.compact]
ref = false
if match[1] == '%'
ref = true
Expand Down
17 changes: 8 additions & 9 deletions lib/rexml/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,10 @@ def read

def match(pattern, cons=false)
if cons
@scanner.scan(pattern)
@scanner.scan(pattern).nil? ? nil : @scanner
else
@scanner.check(pattern)
@scanner.check(pattern).nil? ? nil : @scanner
end
@scanner.matched? ? [@scanner.matched, *@scanner.captures] : nil
end

# @return true if the Source is exhausted
Expand Down Expand Up @@ -158,24 +157,24 @@ def read

def match( pattern, cons=false )
if cons
@scanner.scan(pattern)
md = @scanner.scan(pattern)
else
@scanner.check(pattern)
md = @scanner.check(pattern)
end
while !@scanner.matched? and @source
while md.nil? and @source
begin
@scanner << readline
if cons
@scanner.scan(pattern)
md = @scanner.scan(pattern)
else
@scanner.check(pattern)
md = @scanner.check(pattern)
end
rescue
@source = nil
end
end

@scanner.matched? ? [@scanner.matched, *@scanner.captures] : nil
md.nil? ? nil : @scanner
end

def empty?
Expand Down
12 changes: 12 additions & 0 deletions test/test_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ def test_empty_value
REXML::Document.new(xml)
end
end

def test_empty_entity
xml = <<EOF
<!DOCTYPE root [
<!ENTITY>
]>
EOF

assert_raise(REXML::ParseException) do
REXML::Document.new(xml)
end
end
end
end

Expand Down

0 comments on commit ba9f7fc

Please sign in to comment.