Skip to content

Commit

Permalink
Support frozen string literals
Browse files Browse the repository at this point in the history
  • Loading branch information
dmendel committed Apr 21, 2021
1 parent a6d900a commit a2f3ef8
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 14 deletions.
1 change: 1 addition & 0 deletions ChangeLog.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
== Version 2.4.9 (xxxx-xx-xx)

* Change example from Fixnum to Integer. Thanks to Tim Chambers.
* Now works with frozen string literals. Requested by Jeremy Evans.

== Version 2.4.8 (2020-07-21)

Expand Down
2 changes: 1 addition & 1 deletion lib/bindata/alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def initialize(io)
end
def readbytes(n)
n.times.inject("") do |bytes, _|
bytes << @io.readbits(8, :big).chr
bytes += @io.readbits(8, :big).chr
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/bindata/int.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def create_read_assemble_code(nbits, endian, signed)
parts = (0...nwords).collect do |i|
"(ints.at(#{idx[i]}) << #{bits_per_word(nbits) * i})"
end
parts[0].sub!(/ << 0\b/, "") # Remove " << 0" for optimisation
parts[0] = parts[0].sub(/ << 0\b/, "") # Remove " << 0" for optimisation

parts.join(" + ")
end
Expand All @@ -132,7 +132,7 @@ def val_as_packed_words(nbits, endian, signed)
mask = (1 << bits_per_word(nbits)) - 1

vals = (0...nwords).collect { |i| "val >> #{bits_per_word(nbits) * i}" }
vals[0].sub!(/ >> 0\b/, "") # Remove " >> 0" for optimisation
vals[0] = vals[0].sub(/ >> 0\b/, "") # Remove " >> 0" for optimisation
vals.reverse! if (endian == :big)

vals = vals.collect { |val| "#{val} & #{mask}" } # TODO: "& mask" is needed to work around jruby bug. Remove this line when fixed.
Expand Down Expand Up @@ -160,7 +160,7 @@ def pack_directive(nbits, endian, signed)
directives = { 8 => "C", 16 => "S", 32 => "L", 64 => "Q" }

d = directives[bits_per_word(nbits)]
d << ((endian == :big) ? ">" : "<") unless d == "C"
d += ((endian == :big) ? ">" : "<") unless d == "C"

if signed == :signed && directives.key?(nbits)
(d * nwords).downcase
Expand Down
6 changes: 3 additions & 3 deletions lib/bindata/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def read_raw_with_readahead(n)

unless @read_data.empty? || @in_readahead
bytes_to_consume = [n, @read_data.length].min
data << @read_data.slice!(0, bytes_to_consume)
data += @read_data.slice!(0, bytes_to_consume)
n -= bytes_to_consume

if @read_data.empty?
Expand All @@ -180,10 +180,10 @@ class << self
end

raw_data = @raw_io.read(n)
data << raw_data if raw_data
data += raw_data if raw_data

if @in_readahead
@read_data << data
@read_data += data
end

@offset += data.size
Expand Down
2 changes: 1 addition & 1 deletion lib/bindata/stringz.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def read_and_return_value(io)
# read until zero byte or we have read in the max number of bytes
while ch != "\0" && i != max_length
ch = io.readbytes(1)
str << ch
str += ch
i += 1
end

Expand Down
4 changes: 2 additions & 2 deletions test/bits_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ def generate_bit_classes_to_test(endian, signed)

(start .. 64).each do |nbits|
name = "#{base}#{nbits}"
name << "le" if endian == :little
name += "le" if endian == :little
obj = BinData.const_get(name).new
bits << [obj, nbits]
end

(start .. 64).each do |nbits|
name = "#{base}"
name << "Le" if endian == :little
name += "Le" if endian == :little
obj = BinData.const_get(name).new(nbits: nbits)
bits << [obj, nbits]
end
Expand Down
6 changes: 3 additions & 3 deletions test/delayed_io_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ class IntDelayedIO < BinData::DelayedIO
end

it "writes explicitly" do
io = StringIO.new "\001\002\003\004\005\006\007\010\011"
io = StringIO.new "\001\002\003\004\005\006\007\010\011".dup
obj = IntDelayedIO.new(3)
obj.write(io)
obj.write_now!
io.value.must_equal "\001\002\003\004\005\000\003\010\011"
end

it "writes explicitly after setting abs_offset" do
io = StringIO.new "\001\002\003\004\005\006\007\010\011"
io = StringIO.new "\001\002\003\004\005\006\007\010\011".dup
obj = IntDelayedIO.new(7)
obj.write(io)

Expand Down Expand Up @@ -139,7 +139,7 @@ class StringDelayedIO < BinData::DelayedIO
end

it "writes explicitly" do
io = StringIO.new "\001\002\003\004\005\006\007\010\011\012\013\014\015"
io = StringIO.new "\001\002\003\004\005\006\007\010\011\012\013\014\015".dup
obj = StringDelayedIO.new(str: "hello")
obj.write(io)
obj.write_now!
Expand Down
2 changes: 1 addition & 1 deletion test/int_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def gen_test_int
end

def int_to_binary_str(val)
str = "".force_encoding(Encoding::BINARY)
str = "".dup.force_encoding(Encoding::BINARY)
v = val & ((1 << (@nbytes * 8)) - 1)
@nbytes.times do
str.concat(v & 0xff)
Expand Down

0 comments on commit a2f3ef8

Please sign in to comment.