summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGeremia Taglialatela <[email protected]>2023-06-05 15:53:15 +0200
committerGeremia Taglialatela <[email protected]>2023-06-05 17:27:41 +0200
commit7fdbd745a2c914001a2174aeadae26b9c970ffaf (patch)
tree9b1a440ef0bee0ecaaeb7c991dcc9d8639cb7b6d
parenta2eeaeefca8deb3d26f3f8dc62976fe6bf1dafcf (diff)
downloadcaxlsx-7fdbd745a2c914001a2174aeadae26b9c970ffaf.tar.gz
caxlsx-7fdbd745a2c914001a2174aeadae26b9c970ffaf.zip
Fix Style/FormatString offenses
`Kernel#format` is faster and will avoid to allocate an array compared to `String#%`. ``` IPS: kernel_format: 3877614.2 i/s string_percent: 3531475.0 i/s - 1.10x (± 0.00) slower Memory: kernel_format: 160 allocated string_percent: 200 allocated - 1.25x more ```
-rw-r--r--.rubocop_todo.yml12
-rw-r--r--lib/axlsx/drawing/pic.rb2
-rw-r--r--lib/axlsx/drawing/view_3D.rb2
-rw-r--r--lib/axlsx/drawing/vml_drawing.rb2
-rw-r--r--lib/axlsx/util/accessors.rb2
-rw-r--r--lib/axlsx/util/validators.rb10
-rw-r--r--lib/axlsx/workbook/worksheet/worksheet.rb4
-rw-r--r--test/drawing/tc_vml_drawing.rb6
8 files changed, 17 insertions, 23 deletions
diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml
index 2359f950..67e59726 100644
--- a/.rubocop_todo.yml
+++ b/.rubocop_todo.yml
@@ -187,18 +187,6 @@ Style/ExpandPathArguments:
- 'axlsx.gemspec'
# This cop supports safe autocorrection (--autocorrect).
-# Configuration parameters: EnforcedStyle.
-# SupportedStyles: format, sprintf, percent
-Style/FormatString:
- Exclude:
- - 'lib/axlsx/drawing/pic.rb'
- - 'lib/axlsx/drawing/view_3D.rb'
- - 'lib/axlsx/drawing/vml_drawing.rb'
- - 'lib/axlsx/util/accessors.rb'
- - 'lib/axlsx/util/validators.rb'
- - 'lib/axlsx/workbook/worksheet/worksheet.rb'
-
-# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: MaxUnannotatedPlaceholdersAllowed, AllowedMethods, AllowedPatterns.
# SupportedStyles: annotated, template, unannotated
Style/FormatStringToken:
diff --git a/lib/axlsx/drawing/pic.rb b/lib/axlsx/drawing/pic.rb
index 150d6dc5..c9efb378 100644
--- a/lib/axlsx/drawing/pic.rb
+++ b/lib/axlsx/drawing/pic.rb
@@ -124,7 +124,7 @@ module Axlsx
# The part name for this image used in serialization and relationship building
# @return [String]
def pn
- "#{IMAGE_PN % [(index + 1), extname]}"
+ format(IMAGE_PN, index + 1, extname)
end
# The relationship object for this pic.
diff --git a/lib/axlsx/drawing/view_3D.rb b/lib/axlsx/drawing/view_3D.rb
index 9b3489a3..5b29b296 100644
--- a/lib/axlsx/drawing/view_3D.rb
+++ b/lib/axlsx/drawing/view_3D.rb
@@ -111,7 +111,7 @@ module Axlsx
val = Axlsx.instance_values_for(self)[name]
return "" if val.nil?
- "<%s:%s val='%s'/>" % [namespace, Axlsx::camel(name, false), val]
+ format("<%s:%s val='%s'/>", namespace, Axlsx::camel(name, false), val)
end
end
end
diff --git a/lib/axlsx/drawing/vml_drawing.rb b/lib/axlsx/drawing/vml_drawing.rb
index 364ce19a..15698486 100644
--- a/lib/axlsx/drawing/vml_drawing.rb
+++ b/lib/axlsx/drawing/vml_drawing.rb
@@ -14,7 +14,7 @@ module Axlsx
# The part name for this vml drawing
# @return [String]
def pn
- "#{VML_DRAWING_PN}" % (@comments.worksheet.index + 1)
+ VML_DRAWING_PN % (@comments.worksheet.index + 1)
end
# serialize the vml_drawing to xml.
diff --git a/lib/axlsx/util/accessors.rb b/lib/axlsx/util/accessors.rb
index d653e32f..dfc3b1cd 100644
--- a/lib/axlsx/util/accessors.rb
+++ b/lib/axlsx/util/accessors.rb
@@ -56,7 +56,7 @@ module Axlsx
symbols.each do |symbol|
attr_reader symbol
- module_eval(SETTER % [symbol, validator, symbol], __FILE__, __LINE__)
+ module_eval(format(SETTER, symbol, validator, symbol), __FILE__, __LINE__)
end
end
end
diff --git a/lib/axlsx/util/validators.rb b/lib/axlsx/util/validators.rb
index 48c12c13..1357f4f3 100644
--- a/lib/axlsx/util/validators.rb
+++ b/lib/axlsx/util/validators.rb
@@ -10,7 +10,7 @@ module Axlsx
# @raise [ArgumentError] Raised if the value provided is not in the list of choices.
# @return [Boolean] true if validation succeeds.
def self.validate(name, choices, v)
- raise ArgumentError, (ERR_RESTRICTION % [v.to_s, name, choices.inspect]) unless choices.include?(v)
+ raise ArgumentError, format(ERR_RESTRICTION, v.to_s, name, choices.inspect) unless choices.include?(v)
true
end
@@ -31,7 +31,7 @@ module Axlsx
else
min < value && value < max
end
- raise ArgumentError, (ERR_RANGE % [value.inspect, min.to_s, max.to_s, inclusive]) unless passes
+ raise ArgumentError, format(ERR_RANGE, value.inspect, min.to_s, max.to_s, inclusive) unless passes
end
end
@@ -41,7 +41,7 @@ module Axlsx
# @param [Regexp] regex The regular expression to evaluate
# @param [Any] v The value to validate.
def self.validate(name, regex, v)
- raise ArgumentError, (ERR_REGEX % [v.inspect, regex.to_s]) unless v.respond_to?(:to_s) && regex.match?(v.to_s)
+ raise ArgumentError, format(ERR_REGEX, v.inspect, regex.to_s) unless v.respond_to?(:to_s) && regex.match?(v.to_s)
end
end
@@ -56,14 +56,14 @@ module Axlsx
# @see validate_boolean
def self.validate(name, types, v, other = false)
if other.is_a?(Proc) && !other.call(v)
- raise ArgumentError, (ERR_TYPE % [v.inspect, name, types.inspect])
+ raise ArgumentError, format(ERR_TYPE, v.inspect, name, types.inspect)
end
v_class = v.is_a?(Class) ? v : v.class
Array(types).each do |t|
return if v_class <= t
end
- raise ArgumentError, (ERR_TYPE % [v.inspect, name, types.inspect])
+ raise ArgumentError, format(ERR_TYPE, v.inspect, name, types.inspect)
end
end
diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb
index 29a22db1..07221b3f 100644
--- a/lib/axlsx/workbook/worksheet/worksheet.rb
+++ b/lib/axlsx/workbook/worksheet/worksheet.rb
@@ -675,9 +675,9 @@ module Axlsx
if parts.size > 2
raise ArgumentError, (ERR_CELL_REFERENCE_INVALID % cell_def)
elsif parts.first.nil?
- raise ArgumentError, (ERR_CELL_REFERENCE_MISSING_CELL % [cell_def.split(":").first, cell_def])
+ raise ArgumentError, format(ERR_CELL_REFERENCE_MISSING_CELL, cell_def.split(":").first, cell_def)
elsif parts.last.nil?
- raise ArgumentError, (ERR_CELL_REFERENCE_MISSING_CELL % [cell_def.split(":").last, cell_def])
+ raise ArgumentError, format(ERR_CELL_REFERENCE_MISSING_CELL, cell_def.split(":").last, cell_def)
end
range(*parts)
diff --git a/test/drawing/tc_vml_drawing.rb b/test/drawing/tc_vml_drawing.rb
index fa939f56..e089457e 100644
--- a/test/drawing/tc_vml_drawing.rb
+++ b/test/drawing/tc_vml_drawing.rb
@@ -16,6 +16,12 @@ class TestVmlDrawing < Test::Unit::TestCase
assert_raise(ArgumentError) { Axlsx::VmlDrawing.new }
end
+ def test_pn
+ str = @vml_drawing.pn
+
+ assert_equal("drawings/vmlDrawing1.vml", str)
+ end
+
def test_to_xml_string
str = @vml_drawing.to_xml_string
doc = Nokogiri::XML(str)