From b77c02fce0ac336d1de6c63d2d7da783f9597d76 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Tue, 27 Mar 2012 15:44:41 +0900 Subject: benchmarking shows << to be faster than "%s" % x --- lib/axlsx/workbook/worksheet/cell.rb | 42 ++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index 46f6bac3..59ce0838 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -282,7 +282,7 @@ module Axlsx end def run_xml_string - str = [] + str = "" if is_text_run? keys = self.instance_values.reject{|key, value| value == nil }.keys & INLINE_STYLES keys.delete ['value', 'type'] @@ -290,20 +290,18 @@ module Axlsx keys.each do |key| case key when 'font_name' - str << "" % @font_name if @font_name + str << "" when 'color' str << self.instance_values[key].to_xml_string else - "<%s val='%s'/>" % [key, self.instance_values[key]] + "<" << key << "val='" << self.instance_values[key] << "'/>" end end - str << "" - str << "%s" % value.to_s - str << "" + str << "" << "" << value.to_s << "" else - str << "%s" % value.to_s + str << "" << value.to_s << "" end - str.join + str end # builds an xml text run based on this cells attributes. This is extracted from to_xml so that shared strings can use it. # @param [Nokogiri::XML::Builder] xml The document builder instance this output will be added to. @@ -340,33 +338,29 @@ module Axlsx # Serializes the cell # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] xml text for the cell - FORMULA = "%s" - SHARED_STRING = "%i" - INLINE_STRING = "%s" - OTHER = "%s" - BOOLEAN = "%s" def to_xml_string - if @type == :string + case @type + when :string #parse formula if @value.start_with?('=') - FORMULA % [r, style, value.to_s.gsub('=', '')] + '' << value.to_s.gsub('=', '') << '' else #parse shared if @ssti - SHARED_STRING % [r, style, ssti] + '' << ssti << '' else - INLINE_STRING % [r, style, run_xml_string] + '' << run_xml_string << '' end end - elsif @type == :date + when :date # TODO: See if this is subject to the same restriction as Time below - OTHER % [r, style, DateTimeConverter::date_to_serial(@value)] - elsif @type == :time - OTHER % [r, style, DateTimeConverter::time_to_serial(@value)] - elsif @type == :boolean - BOOLEAN % [r, style, value] + '' << DateTimeConverter::date_to_serial(@value) << '' + when :time + '' << DateTimeConverter::time_to_serial(@value) << '' + when :boolean + '' << ssti << '' else - OTHER % [r, style, value] + '' << @value.to_s << '' end end -- cgit v1.2.3