From d52600dfb373323641c95c3c9be952c32fee8a1a Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Wed, 5 Jul 2023 11:09:35 +0200 Subject: Fix offenses in sheet protection - Lint/AmbiguousOperatorPrecedence - Style/PerlBackrefs - Style/StringChars - Style/UnpackFirst --- .rubocop_todo.yml | 16 ---------------- lib/axlsx/workbook/worksheet/sheet_protection.rb | 10 +++++----- test/workbook/worksheet/tc_sheet_protection.rb | 2 +- 3 files changed, 6 insertions(+), 22 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 29c93858..ac807d56 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -31,7 +31,6 @@ Layout/HashAlignment: # This cop supports safe autocorrection (--autocorrect). Lint/AmbiguousOperatorPrecedence: Exclude: - - 'lib/axlsx/workbook/worksheet/sheet_protection.rb' - 'lib/axlsx/workbook/worksheet/worksheet.rb' # This cop supports unsafe autocorrection (--autocorrect-all). @@ -262,11 +261,6 @@ Style/PercentLiteralDelimiters: - 'test/workbook/worksheet/tc_pivot_table.rb' - 'test/workbook/worksheet/tc_table_style_info.rb' -# This cop supports safe autocorrection (--autocorrect). -Style/PerlBackrefs: - Exclude: - - 'test/workbook/worksheet/tc_sheet_protection.rb' - # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: EnforcedStyle. # SupportedStyles: same_as_string_literals, single_quotes, double_quotes @@ -299,11 +293,6 @@ Style/SlicingWithRange: - 'lib/axlsx/workbook/worksheet/pivot_table.rb' - 'lib/axlsx/workbook/worksheet/worksheet.rb' -# This cop supports unsafe autocorrection (--autocorrect-all). -Style/StringChars: - Exclude: - - 'lib/axlsx/workbook/worksheet/sheet_protection.rb' - # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline. # SupportedStyles: single_quotes, double_quotes @@ -332,11 +321,6 @@ Style/TrivialAccessors: - 'lib/axlsx/workbook/worksheet/rich_text_run.rb' - 'lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb' -# This cop supports safe autocorrection (--autocorrect). -Style/UnpackFirst: - Exclude: - - 'lib/axlsx/workbook/worksheet/sheet_protection.rb' - # This cop supports safe autocorrection (--autocorrect). Style/WhileUntilModifier: Exclude: diff --git a/lib/axlsx/workbook/worksheet/sheet_protection.rb b/lib/axlsx/workbook/worksheet/sheet_protection.rb index ce39b1b4..abf5e452 100644 --- a/lib/axlsx/workbook/worksheet/sheet_protection.rb +++ b/lib/axlsx/workbook/worksheet/sheet_protection.rb @@ -87,25 +87,25 @@ module Axlsx encoded_password = encode_password(password) password_as_hex = [encoded_password].pack("v") - password_as_string = password_as_hex.unpack("H*").first.upcase + password_as_string = password_as_hex.unpack1("H*").upcase password_as_string[2..3] + password_as_string[0..1] end # Encodes a given password # Based on the algorithm provided by Daniel Rentz of OpenOffice. - # http://www.openoffice.org/sc/excelfileformat.pdf, Revision 1.42, page 115 (21.05.2012) + # https://www.openoffice.org/sc/excelfileformat.pdf, Revision 1.42, page 115 (21.05.2012) # @return [String] def encode_password(password) i = 0 - chars = password.split("") + chars = password.chars count = chars.size chars.collect! do |char| i += 1 - char = char.unpack('c')[0] << i # ord << i + char = char.unpack1('c') << i # ord << i low_15 = char & 0x7fff - high_15 = char & 0x7fff << 15 + high_15 = char & (0x7fff << 15) high_15 = high_15 >> 15 low_15 | high_15 end diff --git a/test/workbook/worksheet/tc_sheet_protection.rb b/test/workbook/worksheet/tc_sheet_protection.rb index ebfff4b6..11a43ac8 100644 --- a/test/workbook/worksheet/tc_sheet_protection.rb +++ b/test/workbook/worksheet/tc_sheet_protection.rb @@ -57,7 +57,7 @@ class TestSheetProtection < Test::Unit::TestCase doc = Nokogiri::XML(@sp.to_xml_string) @options.each do |key, value| - assert(doc.xpath("//sheetProtection[@#{key.to_s.gsub(/_(.)/) { $1.upcase }}='#{value}']")) + assert(doc.xpath("//sheetProtection[@#{key.to_s.gsub(/_(.)/) { ::Regexp.last_match(1).upcase }}='#{value}']")) end end end -- cgit v1.2.3