From 171f4ad7d68f8b9e93fa9e65b31bf313d8c57296 Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Sat, 27 May 2023 12:33:34 +0200 Subject: Improve absolute cell reference method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also inverts `string.match(REGEX)` with `REGEX.match(string)` because of uniformity and because it has been tested 5% more performant on Ruby 3.2. Performance on older Rubies is the same ``` REGEX.match(string): 1234330.7 i/s string.match(REGEX): 1172670.1 i/s - 1.05x (± 0.00) slower ``` --- lib/axlsx/workbook/worksheet/cell.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index 09cf2064..b4f826c2 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -73,6 +73,9 @@ module Axlsx CELL_TYPES = [:date, :time, :float, :integer, :richtext, :string, :boolean, :iso_8601, :text].freeze + # A regular expression to match the alpha(column)numeric(row) reference of a cell + CELL_REFERENCE_REGEX = /([A-Z]+)([0-9]+)/.freeze + # The index of the cellXfs item to be applied to this cell. # @return [Integer] # @see Axlsx::Styles @@ -345,11 +348,11 @@ module Axlsx Axlsx::cell_r index, @row.row_index end - # @return [String] The absolute alpha(column)numeric(row) reference for this sell. + # @return [String] The absolute alpha(column)numeric(row) reference for this cell. # @example Absolute Cell Reference # ws.rows.first.cells.first.r #=> "$A$1" def r_abs - "$#{r.match(/([A-Z]+)([0-9]+)/)[1, 2].join('$')}" + "$#{CELL_REFERENCE_REGEX.match(r)[1, 2].join('$')}" end # @return [Integer] The cellXfs item index applied to this cell. -- cgit v1.2.3