Skip to content

Commit

Permalink
Remove runtime dependency on base64 gem to fix Ruby 3.3 warning (#63)
Browse files Browse the repository at this point in the history
* Add base64 dependency to fix Ruby 3.3 warning

* Remove runtime dependency on base64 gem

* Clarify pack/unpack1 with comments

* Replace unpack1 with unpack()[0] for old Ruby
  • Loading branch information
mattbrictson authored Dec 25, 2023
1 parent 67c4724 commit 4b87dcf
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ else
gem "rake", "~> 13.0"
end

if Gem::Requirement.new(">= 3.3").satisfied_by?(Gem::Version.new(RUBY_VERSION))
gem "base64"
end

gem "test-unit", "~> 3.5"
1 change: 0 additions & 1 deletion lib/plist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# Distributed under the MIT License
#

require 'base64'
require 'cgi'
require 'stringio'

Expand Down
6 changes: 2 additions & 4 deletions lib/plist/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,9 @@ def tag(type, contents, level, &block)

def data_tag(data, level)
# note that apple plists are wrapped at a different length then
# what ruby's base64 wraps by default.
# I used #encode64 instead of #b64encode (which allows a length arg)
# because b64encode is b0rked and ignores the length arg.
# what ruby's pack wraps by default.
tag('data', nil, level) do
Base64.encode64(data)
[data].pack("m") # equivalent to Base64.encode64(data)
.gsub(/\s+/, '')
.scan(/.{1,68}/o)
.collect { |line| indent(line, level) }
Expand Down
4 changes: 2 additions & 2 deletions lib/plist/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,10 @@ def to_ruby
end
end

require 'base64'
class PData < PTag
def to_ruby
data = Base64.decode64(text.gsub(/\s+/, '')) unless text.nil?
# unpack("m")[0] is equivalent to Base64.decode64
data = text.gsub(/\s+/, '').unpack("m")[0] unless text.nil?
begin
return Marshal.load(data) if options[:marshal]
rescue Exception
Expand Down
1 change: 1 addition & 0 deletions test/test_parser.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'test/unit'
require 'base64'
require 'plist'

class TestParser < Test::Unit::TestCase
Expand Down

0 comments on commit 4b87dcf

Please sign in to comment.