diff options
| author | Paul Kmiec <[email protected]> | 2023-05-05 08:58:17 -0700 |
|---|---|---|
| committer | Paul Kmiec <[email protected]> | 2023-05-05 09:29:58 -0700 |
| commit | 67aefd7705df82e43a8670102400a5abab49f6e8 (patch) | |
| tree | 6872755fccedb331f4553c72b642c6c86a700bff /lib/axlsx/stylesheet | |
| parent | 1c355c83a9603f835dfe59ef4473df2b8cc3534c (diff) | |
| download | caxlsx-67aefd7705df82e43a8670102400a5abab49f6e8.tar.gz caxlsx-67aefd7705df82e43a8670102400a5abab49f6e8.zip | |
Pipe output directly to str and avoid additional memory allocations
Currently, there are lots of examples of code like this,
```
str << ('<tag ' << foo << ' ' << bar << '/>')
```
which create the string for the tag in memory before piping to str.
We can avoid creating all of these intermediate strings by dropping
the paranthesis and piping directly to str.
This relies on the `str` passed around to handle lots of small
appends. This is a problem when using RubyZip, but that is solved
in the next commit.
Diffstat (limited to 'lib/axlsx/stylesheet')
| -rw-r--r-- | lib/axlsx/stylesheet/border_pr.rb | 4 | ||||
| -rw-r--r-- | lib/axlsx/stylesheet/font.rb | 2 | ||||
| -rw-r--r-- | lib/axlsx/stylesheet/gradient_stop.rb | 2 | ||||
| -rw-r--r-- | lib/axlsx/stylesheet/pattern_fill.rb | 2 | ||||
| -rw-r--r-- | lib/axlsx/stylesheet/styles.rb | 2 |
5 files changed, 6 insertions, 6 deletions
diff --git a/lib/axlsx/stylesheet/border_pr.rb b/lib/axlsx/stylesheet/border_pr.rb index 154b25f4..ad4e29fa 100644 --- a/lib/axlsx/stylesheet/border_pr.rb +++ b/lib/axlsx/stylesheet/border_pr.rb @@ -63,9 +63,9 @@ module Axlsx # @param [String] str # @return [String] def to_xml_string(str = +'') - str << (+'<' << @name.to_s << ' style="' << @style.to_s << '">') + str << '<' << @name.to_s << ' style="' << @style.to_s << '">' @color.to_xml_string(str) if @color.is_a?(Color) - str << (+'</' << @name.to_s << '>') + str << '</' << @name.to_s << '>' end end end diff --git a/lib/axlsx/stylesheet/font.rb b/lib/axlsx/stylesheet/font.rb index 463b8fc0..8b183b6e 100644 --- a/lib/axlsx/stylesheet/font.rb +++ b/lib/axlsx/stylesheet/font.rb @@ -151,7 +151,7 @@ module Axlsx def to_xml_string(str = +'') str << '<font>' Axlsx.instance_values_for(self).each do |k, v| - v.is_a?(Color) ? v.to_xml_string(str) : (str << (+'<' << k.to_s << ' val="' << Axlsx.booleanize(v).to_s << '"/>')) + v.is_a?(Color) ? v.to_xml_string(str) : (str << '<' << k.to_s << ' val="' << Axlsx.booleanize(v).to_s << '"/>') end str << '</font>' end diff --git a/lib/axlsx/stylesheet/gradient_stop.rb b/lib/axlsx/stylesheet/gradient_stop.rb index 83f39a55..dd0ea00f 100644 --- a/lib/axlsx/stylesheet/gradient_stop.rb +++ b/lib/axlsx/stylesheet/gradient_stop.rb @@ -30,7 +30,7 @@ module Axlsx # @param [String] str # @return [String] def to_xml_string(str = +'') - str << (+'<stop position="' << position.to_s << '">') + str << '<stop position="' << position.to_s << '">' self.color.to_xml_string(str) str << '</stop>' end diff --git a/lib/axlsx/stylesheet/pattern_fill.rb b/lib/axlsx/stylesheet/pattern_fill.rb index cabce9f5..9f92dc00 100644 --- a/lib/axlsx/stylesheet/pattern_fill.rb +++ b/lib/axlsx/stylesheet/pattern_fill.rb @@ -59,7 +59,7 @@ module Axlsx # @param [String] str # @return [String] def to_xml_string(str = +'') - str << (+'<patternFill patternType="' << patternType.to_s << '">') + str << '<patternFill patternType="' << patternType.to_s << '">' if fgColor.is_a?(Color) fgColor.to_xml_string str, "fgColor" end diff --git a/lib/axlsx/stylesheet/styles.rb b/lib/axlsx/stylesheet/styles.rb index 809630f2..7b44c023 100644 --- a/lib/axlsx/stylesheet/styles.rb +++ b/lib/axlsx/stylesheet/styles.rb @@ -486,7 +486,7 @@ module Axlsx # @param [String] str # @return [String] def to_xml_string(str = +'') - str << (+'<styleSheet xmlns="' << XML_NS << '">') + str << '<styleSheet xmlns="' << XML_NS << '">' instance_vals = Axlsx.instance_values_for(self) [:numFmts, :fonts, :fills, :borders, :cellStyleXfs, :cellXfs, :cellStyles, :dxfs, :tableStyles].each do |key| instance_vals[key.to_s].to_xml_string(str) unless instance_vals[key.to_s].nil? |
