Skip to content

Commit

Permalink
Add compatibility with --enable-frozen-string-literal
Browse files Browse the repository at this point in the history
Fix: #168
Ref: https://bugs.ruby-lang.org/issues/20205

Ruby 3.4 will emit deprecation warnings when mutating string
literals, and a future version is likely to make frozen string
literals the default.
  • Loading branch information
byroot committed Mar 28, 2024
1 parent fe11372 commit 938b5fc
Show file tree
Hide file tree
Showing 15 changed files with 31 additions and 12 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: ["2.7", "3.0", "3.1", "3.2"]
ruby: ["2.7", "3.0", "3.1", "3.2", "3.3"]
rubyopt: [""]
include:
- ruby: "3.3"
rubyopt: "--enable-frozen-string-literal --debug-frozen-string-literal"

steps:
- name: Checkout code
Expand All @@ -25,4 +29,4 @@ jobs:
run: bundle lock

- name: Run tests
run: bundle exec rake test
run: bundle exec rake test RUBYOPT="${{ matrix.rubyopt }}"
1 change: 1 addition & 0 deletions lib/combine_pdf.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true

require 'zlib'
require 'securerandom'
Expand Down
1 change: 1 addition & 0 deletions lib/combine_pdf/api.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true

module CombinePDF
module_function
Expand Down
1 change: 1 addition & 0 deletions lib/combine_pdf/basic_writer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true
########################################################
## Thoughts from reading the ISO 32000-1:2008
## this file is part of the CombinePDF library and the code
Expand Down
3 changes: 2 additions & 1 deletion lib/combine_pdf/decrypt.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true
########################################################
## Thoughts from reading the ISO 32000-1:2008
## this file is part of the CombinePDF library and the code
Expand Down Expand Up @@ -137,7 +138,7 @@ def decrypt_AES(encrypted, encrypted_id, encrypted_generation, _encrypted_filter
object_key = @key.dup
object_key << [encrypted_id].pack('i')[0..2]
object_key << [encrypted_generation].pack('i')[0..1]
object_key << 'sAlT'.force_encoding(Encoding::ASCII_8BIT)
object_key << 'sAlT'.b
key_length = object_key.length < 16 ? object_key.length : 16

begin
Expand Down
2 changes: 2 additions & 0 deletions lib/combine_pdf/exceptions.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module CombinePDF
class EncryptionError < StandardError
end
Expand Down
1 change: 1 addition & 0 deletions lib/combine_pdf/filter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true
########################################################
## Thoughts from reading the ISO 32000-1:2008
## this file is part of the CombinePDF library and the code
Expand Down
1 change: 1 addition & 0 deletions lib/combine_pdf/fonts.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true
########################################################
## Thoughts from reading the ISO 32000-1:2008
## this file is part of the CombinePDF library and the code
Expand Down
1 change: 1 addition & 0 deletions lib/combine_pdf/page_methods.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true
########################################################
## Thoughts from reading the ISO 32000-1:2008
## this file is part of the CombinePDF library and the code
Expand Down
4 changes: 2 additions & 2 deletions lib/combine_pdf/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def _parse_
##########################################
elsif @scanner.scan(/\(/)
# warn "Found a literal string"
str = ''.force_encoding(Encoding::ASCII_8BIT)
str = ''.b
count = 1
while count > 0 && @scanner.rest?
scn = @scanner.scan_until(/[\(\)]/)
Expand Down Expand Up @@ -379,7 +379,7 @@ def _parse_
length = 0 if(length < 0)
length -= 1 if(@scanner.string[old_pos + length - 1] == "\n")
length -= 1 if(@scanner.string[old_pos + length - 1] == "\r")
str = (length > 0) ? @scanner.string.slice(old_pos, length) : ''
str = (length > 0) ? @scanner.string.slice(old_pos, length) : +''

# warn "CombinePDF parser: detected Stream #{str.length} bytes long #{str[0..3]}...#{str[-4..-1]}"

Expand Down
2 changes: 2 additions & 0 deletions lib/combine_pdf/pdf_protected.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true
########################################################
## Thoughts from reading the ISO 32000-1:2008
## this file is part of the CombinePDF library and the code
Expand Down Expand Up @@ -186,6 +187,7 @@ def remove_old_ids
POSSIBLE_NAME_TREES = [:Dests, :AP, :Pages, :IDS, :Templates, :URLS, :JavaScript, :EmbeddedFiles, :AlternatePresentations, :Renditions].to_set.freeze

def rebuild_names(name_tree = nil, base = 'CombinePDF_0000000')
base = +base
if name_tree
return nil unless name_tree.is_a?(Hash)
name_tree = name_tree[:referenced_object] || name_tree
Expand Down
7 changes: 4 additions & 3 deletions lib/combine_pdf/pdf_public.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- encoding : utf-8 -*-
########################################################
## frozen_string_literal: true
#######################################################
## Thoughts from reading the ISO 32000-1:2008
## this file is part of the CombinePDF library and the code
## is subject to the same license.
Expand Down Expand Up @@ -93,7 +94,7 @@ def initialize(parser = nil)
@version = 0
@viewer_preferences = {}
@info = {}
parser ||= PDFParser.new('')
parser ||= PDFParser.new(+'')
raise TypeError, "initialization error, expecting CombinePDF::PDFParser or nil, but got #{parser.class.name}" unless parser.is_a? PDFParser
@objects = parser.parse

Expand Down Expand Up @@ -216,7 +217,7 @@ def to_pdf(options = {})
# when finished, remove the numbering system and keep only pointers
remove_old_ids
# output the pdf stream
out.join("\n".force_encoding(Encoding::ASCII_8BIT)).force_encoding(Encoding::ASCII_8BIT)
out.join("\n").force_encoding(Encoding::ASCII_8BIT)
end

# this method returns all the pages cataloged in the catalog.
Expand Down
5 changes: 3 additions & 2 deletions lib/combine_pdf/renderer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module CombinePDF
################################################################
## These are common functions, used within the different classes
Expand Down Expand Up @@ -123,12 +124,12 @@ def format_hash_to_pdf(object)
# if the object is not a simple object, it is a dictionary
# A dictionary shall be written as a sequence of key-value pairs enclosed in double angle brackets (<<...>>)
# (using LESS-THAN SIGNs (3Ch) and GREATER-THAN SIGNs (3Eh)).
out << "<<\n".force_encoding(Encoding::ASCII_8BIT)
out << "<<\n".b
object.each do |key, value|
out << "#{object_to_pdf key} #{object_to_pdf value}\n".force_encoding(Encoding::ASCII_8BIT) unless PDF::PRIVATE_HASH_KEYS.include? key
end
object.delete :Length
out << '>>'.force_encoding(Encoding::ASCII_8BIT)
out << '>>'.b
out << "\nstream\n#{object[:raw_stream_content]}\nendstream".force_encoding(Encoding::ASCII_8BIT) if object[:raw_stream_content]
out << "\nendobj\n" if object[:indirect_reference_id]
out.join.force_encoding(Encoding::ASCII_8BIT)
Expand Down
4 changes: 3 additions & 1 deletion lib/combine_pdf/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module CombinePDF
VERSION = '1.0.26'.freeze
VERSION = '1.0.26'
end
2 changes: 1 addition & 1 deletion test/combine_pdf/renderer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_object(object)

def test_numeric_array_to_pdf
input = [1.234567, 0.000054, 5, -0.000099]
expected = "[1.234567 0.000054 5 -0.000099]".force_encoding('BINARY')
expected = "[1.234567 0.000054 5 -0.000099]".b
actual = TestRenderer.new.test_object(input)

assert_equal(expected, actual)
Expand Down

0 comments on commit 938b5fc

Please sign in to comment.