summaryrefslogtreecommitdiffhomepage
path: root/lib/axlsx/doc_props
diff options
context:
space:
mode:
authorPaul Kmiec <[email protected]>2023-05-05 08:58:17 -0700
committerPaul Kmiec <[email protected]>2023-05-05 09:29:58 -0700
commit67aefd7705df82e43a8670102400a5abab49f6e8 (patch)
tree6872755fccedb331f4553c72b642c6c86a700bff /lib/axlsx/doc_props
parent1c355c83a9603f835dfe59ef4473df2b8cc3534c (diff)
downloadcaxlsx-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/doc_props')
-rw-r--r--lib/axlsx/doc_props/app.rb2
-rw-r--r--lib/axlsx/doc_props/core.rb10
2 files changed, 6 insertions, 6 deletions
diff --git a/lib/axlsx/doc_props/app.rb b/lib/axlsx/doc_props/app.rb
index 3eae1e28..40a8184d 100644
--- a/lib/axlsx/doc_props/app.rb
+++ b/lib/axlsx/doc_props/app.rb
@@ -221,7 +221,7 @@ module Axlsx
# @return [String]
def to_xml_string(str = +'')
str << '<?xml version="1.0" encoding="UTF-8"?>'
- str << (+'<Properties xmlns="' << APP_NS << '" xmlns:vt="' << APP_NS_VT << '">')
+ str << '<Properties xmlns="' << APP_NS << '" xmlns:vt="' << APP_NS_VT << '">'
Axlsx.instance_values_for(self).each do |key, value|
node_name = Axlsx.camel(key)
str << "<#{node_name}>#{value}</#{node_name}>"
diff --git a/lib/axlsx/doc_props/core.rb b/lib/axlsx/doc_props/core.rb
index 8d3292e9..8dbe4cf4 100644
--- a/lib/axlsx/doc_props/core.rb
+++ b/lib/axlsx/doc_props/core.rb
@@ -24,11 +24,11 @@ module Axlsx
# @return [String]
def to_xml_string(str = +'')
str << '<?xml version="1.0" encoding="UTF-8"?>'
- str << (+'<cp:coreProperties xmlns:cp="' << CORE_NS << '" xmlns:dc="' << CORE_NS_DC << '" ')
- str << (+'xmlns:dcmitype="' << CORE_NS_DCMIT << '" xmlns:dcterms="' << CORE_NS_DCT << '" ')
- str << (+'xmlns:xsi="' << CORE_NS_XSI << '">')
- str << (+'<dc:creator>' << self.creator << '</dc:creator>')
- str << (+'<dcterms:created xsi:type="dcterms:W3CDTF">' << (created || Time.now).strftime('%Y-%m-%dT%H:%M:%S') << 'Z</dcterms:created>')
+ str << '<cp:coreProperties xmlns:cp="' << CORE_NS << '" xmlns:dc="' << CORE_NS_DC << '" '
+ str << 'xmlns:dcmitype="' << CORE_NS_DCMIT << '" xmlns:dcterms="' << CORE_NS_DCT << '" '
+ str << 'xmlns:xsi="' << CORE_NS_XSI << '">'
+ str << '<dc:creator>' << self.creator << '</dc:creator>'
+ str << '<dcterms:created xsi:type="dcterms:W3CDTF">' << (created || Time.now).strftime('%Y-%m-%dT%H:%M:%S') << 'Z</dcterms:created>'
str << '<cp:revision>0</cp:revision>'
str << '</cp:coreProperties>'
end