From 8bc895562c85e4689544277f5a8168a1822b0b50 Mon Sep 17 00:00:00 2001 From: NAITOH Jun Date: Sat, 13 Jan 2024 22:34:40 +0900 Subject: [PATCH] Add compact option to @source.match Improve processing speed by returning @scanner.captures.compact if @compact=true and @scanner if compact=false. --- lib/rexml/parsers/baseparser.rb | 4 ++-- lib/rexml/source.rb | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/rexml/parsers/baseparser.rb b/lib/rexml/parsers/baseparser.rb index c25f2dfa..bee4ee48 100644 --- a/lib/rexml/parsers/baseparser.rb +++ b/lib/rexml/parsers/baseparser.rb @@ -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 diff --git a/lib/rexml/source.rb b/lib/rexml/source.rb index bad32ec2..b732d04b 100644 --- a/lib/rexml/source.rb +++ b/lib/rexml/source.rb @@ -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 @@ -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 @@ -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?