Skip to content

Commit

Permalink
Add compact option to @source.match
Browse files Browse the repository at this point in the history
Improve processing speed by returning @scanner.captures.compact if @compact=true and @scanner if compact=false.
  • Loading branch information
naitoh committed Jan 13, 2024
1 parent 8227cc2 commit 8bc8955
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/rexml/parsers/baseparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ def pull_event
return [ :elementdecl, @source.match( ELEMENTDECL_PATTERN, true )[1] ]

when ENTITY_START
match = @source.match( ENTITYDECL, true ).compact
match[0] = :entitydecl
match = @source.match( ENTITYDECL, true, true )
match = match.nil? ? [:entitydecl] : [:entitydecl, *match]
ref = false
if match[1] == '%'
ref = true
Expand Down
17 changes: 13 additions & 4 deletions lib/rexml/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,18 @@ def encoding=(enc)
def read
end

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

if @scanner.matched?
compact ? @scanner.captures.compact : @scanner
else
nil
end
end

# @return true if the Source is exhausted
Expand Down Expand Up @@ -156,7 +161,7 @@ def read
end
end

def match( pattern, cons=false )
def match( pattern, cons=false, compact=false)
if cons
@scanner.scan(pattern)
else
Expand All @@ -175,7 +180,11 @@ def match( pattern, cons=false )
end
end

@scanner.matched? ? [@scanner.matched, *@scanner.captures] : nil
if @scanner.matched?
compact ? @scanner.captures.compact : @scanner
else
nil
end
end

def empty?
Expand Down

0 comments on commit 8bc8955

Please sign in to comment.