Skip to content

Commit 112c7db

Browse files
committed
Performance and memory optimizations
1 parent cbb9c1f commit 112c7db

File tree

6 files changed

+26
-15
lines changed

6 files changed

+26
-15
lines changed

lib/rexml/attribute.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# frozen_string_literal: false
1+
# frozen_string_literal: true
22
require_relative "namespace"
33
require_relative 'text'
44

@@ -119,10 +119,13 @@ def hash
119119
# b = Attribute.new( "ns:x", "y" )
120120
# b.to_string # -> "ns:x='y'"
121121
def to_string
122+
value = to_s
122123
if @element and @element.context and @element.context[:attribute_quote] == :quote
123-
%Q^#@expanded_name="#{to_s().gsub(/"/, '"')}"^
124+
value = value.gsub('"', '"') if value.include?('"')
125+
%Q^#@expanded_name="#{value}"^
124126
else
125-
"#@expanded_name='#{to_s().gsub(/'/, ''')}'"
127+
value = value.gsub("'", ''') if value.include?("'")
128+
"#@expanded_name='#{value}'"
126129
end
127130
end
128131

@@ -192,7 +195,7 @@ def node_type
192195
end
193196

194197
def inspect
195-
rv = ""
198+
rv = +""
196199
write( rv )
197200
rv
198201
end

lib/rexml/entity.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ def to_s
133133
# doctype.entity('yada').value #-> "nanoo bar nanoo"
134134
def value
135135
if @value
136+
return @value unless @value.match?(PEREFERENCE_RE)
137+
136138
matches = @value.scan(PEREFERENCE_RE)
137139
rv = @value.clone
138140
if @parent

lib/rexml/formatters/pretty.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# frozen_string_literal: false
1+
# frozen_string_literal: true
22
require_relative 'default'
33

44
module REXML
@@ -58,7 +58,7 @@ def write_element(node, output)
5858
skip = false
5959
if compact
6060
if node.children.inject(true) {|s,c| s & c.kind_of?(Text)}
61-
string = ""
61+
string = +""
6262
old_level = @level
6363
@level = 0
6464
node.children.each { |child| write( child, string ) }

lib/rexml/namespace.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# frozen_string_literal: false
1+
# frozen_string_literal: true
22

33
require_relative 'xmltokens'
44

@@ -10,21 +10,25 @@ module Namespace
1010
# The expanded name of the object, valid if name is set
1111
attr_accessor :prefix
1212
include XMLTokens
13+
NAME_WITHOUT_NAMESPACE = /\A#{NCNAME_STR}\z/
1314
NAMESPLIT = /^(?:(#{NCNAME_STR}):)?(#{NCNAME_STR})/u
1415

1516
# Sets the name and the expanded name
1617
def name=( name )
1718
@expanded_name = name
18-
case name
19-
when NAMESPLIT
19+
if name.match?(NAME_WITHOUT_NAMESPACE)
20+
@prefix = ""
21+
@namespace = ""
22+
@name = name
23+
elsif name =~ NAMESPLIT
2024
if $1
2125
@prefix = $1
2226
else
2327
@prefix = ""
2428
@namespace = ""
2529
end
2630
@name = $2
27-
when ""
31+
elsif name == ""
2832
@prefix = nil
2933
@namespace = nil
3034
@name = nil

lib/rexml/text.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# frozen_string_literal: false
1+
# frozen_string_literal: true
22
require_relative 'security'
33
require_relative 'entity'
44
require_relative 'doctype'
@@ -131,7 +131,7 @@ def parent= parent
131131
def Text.check string, pattern, doctype
132132

133133
# illegal anywhere
134-
if string !~ VALID_XML_CHARS
134+
if !string.match?(VALID_XML_CHARS)
135135
if String.method_defined? :encode
136136
string.chars.each do |c|
137137
case c.ord
@@ -371,7 +371,7 @@ def Text::normalize( input, doctype=nil, entity_filter=nil )
371371
copy = input.to_s
372372
# Doing it like this rather than in a loop improves the speed
373373
#copy = copy.gsub( EREFERENCE, '&' )
374-
copy = copy.gsub( "&", "&" )
374+
copy = copy.gsub( "&", "&" ) if copy.include?("&")
375375
if doctype
376376
# Replace all ampersands that aren't part of an entity
377377
doctype.entities.each_value do |entity|
@@ -382,7 +382,9 @@ def Text::normalize( input, doctype=nil, entity_filter=nil )
382382
else
383383
# Replace all ampersands that aren't part of an entity
384384
DocType::DEFAULT_ENTITIES.each_value do |entity|
385-
copy = copy.gsub(entity.value, "&#{entity.name};" )
385+
if copy.include?(entity.value)
386+
copy = copy.gsub(entity.value, "&#{entity.name};" )
387+
end
386388
end
387389
end
388390
copy

test/test_core.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,7 @@ def test_ticket_91
14231423
d.root.add_element( "bah" )
14241424
p=REXML::Formatters::Pretty.new(2)
14251425
p.compact = true # Don't add whitespace to text nodes unless necessary
1426-
p.write(d,out="")
1426+
p.write(d,out=+"")
14271427
assert_equal( expected, out )
14281428
end
14291429

0 commit comments

Comments
 (0)