bindata 2.4.9 → 2.4.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a511668b89503e38575dbc2eb0f02e542556c63bf008294a2a99a4f116639d74
4
- data.tar.gz: 259742f4fe4f2632b0edca7bf155277198291663546ff3c148d1d515ac8e76d5
3
+ metadata.gz: a8da7873dc256b291c991e6577d886a78ba27b347b6ef03dd55b7d23ae797611
4
+ data.tar.gz: c44844d397e1d1c8a332c99bc16f18a64a979f8251ce3dc2e71246882e40f10c
5
5
  SHA512:
6
- metadata.gz: 1e2f3a1a41443ec3ddfc44734bd7c1e03d6c957ea86d86fa68f0ec913df63ac5c2a32e26ef73fe2d8abe889fdfe916d2e9fc97686ede3fd6e1e51598480acbb5
7
- data.tar.gz: e3f21a9882a1a259044cfd284308ba6d13d1bef9674b03c7a501fae53a30fac5b868a9d5a11bc10302889bd03d5e73e6e9203c1b5b395b9ba12a33ed9b4c7f72
6
+ metadata.gz: 391299bd3e2f04892a42b1fffabd7e494872f4085d2bef50f6e37b267a7e496ecd61081cd838550d87e5702dcefc8ed63c76446fa72ba02a52e1b60cb13a941a
7
+ data.tar.gz: ebf86b0c30ceea48215d9beaa39706c9fed31b2b1da788b2c9ddaf1d861ac4ff33a27b7a3211d6e097e077da8625ef6ad2d21a40a6dc9232bc88ec8ad39a0143
data/ChangeLog.rdoc CHANGED
@@ -1,5 +1,13 @@
1
1
  = BinData Changelog
2
2
 
3
+ == Version 2.4.11 (2022-09-27)
4
+
5
+ * Make DelayedIO work with :onlyif. Reported by Spencer McIntyre.
6
+
7
+ == Version 2.4.10 (2021-05-18)
8
+
9
+ * Improve speed of dynamic object creation. Reported by Charlie Ablett.
10
+
3
11
  == Version 2.4.9 (2021-04-22)
4
12
 
5
13
  * Change example from Fixnum to Integer. Thanks to Tim Chambers.
data/lib/bindata/bits.rb CHANGED
@@ -115,14 +115,14 @@ module BinData
115
115
  end
116
116
 
117
117
  if signed == :signed
118
- max = (1 << (nbits - 1)) - 1
119
- min = -(max + 1)
118
+ max = "max = (1 << (#{nbits} - 1)) - 1"
119
+ min = "min = -(max + 1)"
120
120
  else
121
- min = 0
122
- max = (1 << nbits) - 1
121
+ min = "min = 0"
122
+ max = "max = (1 << #{nbits}) - 1"
123
123
  end
124
124
 
125
- clamp = "(val < #{min}) ? #{min} : (val > #{max}) ? #{max} : val"
125
+ clamp = "(#{max}; #{min}; val = (val < min) ? min : (val > max) ? max : val)"
126
126
 
127
127
  if nbits == 1
128
128
  # allow single bits to be used as booleans
@@ -116,9 +116,25 @@ module BinData
116
116
  0
117
117
  end
118
118
 
119
+ def eval_parameter_with_delayed_io(key, overrides = nil)
120
+ result = eval_parameter_without_delayed_io(key, overrides)
121
+
122
+ # Delay processing :onlyif until we do the actual read/write
123
+ result = true if key == :onlyif && ! result
124
+
125
+ result
126
+ end
127
+ alias_method :eval_parameter_without_delayed_io, :eval_parameter
128
+ alias_method :eval_parameter, :eval_parameter_with_delayed_io
129
+
130
+ def include_obj?
131
+ ! has_parameter?(:onlyif) || eval_parameter_without_delayed_io(:onlyif)
132
+ end
133
+
119
134
  # DelayedIO objects aren't read when #read is called.
120
135
  # The reading is delayed until this method is called.
121
136
  def read_now!
137
+ return unless include_obj?
122
138
  raise IOError, "read from where?" unless @read_io
123
139
 
124
140
  @read_io.seekbytes(abs_offset - @read_io.offset)
@@ -130,7 +146,9 @@ module BinData
130
146
  # DelayedIO objects aren't written when #write is called.
131
147
  # The writing is delayed until this method is called.
132
148
  def write_now!
149
+ return unless include_obj?
133
150
  raise IOError, "write to where?" unless @write_io
151
+
134
152
  @write_io.seekbytes(abs_offset - @write_io.offset)
135
153
  @type.do_write(@write_io)
136
154
  end
data/lib/bindata/int.rb CHANGED
@@ -59,14 +59,16 @@ module BinData
59
59
 
60
60
  def create_clamp_code(nbits, signed)
61
61
  if signed == :signed
62
- max = (1 << (nbits - 1)) - 1
63
- min = -(max + 1)
62
+ max = "max = (1 << (#{nbits} - 1)) - 1"
63
+ min = "min = -(max + 1)"
64
64
  else
65
- max = (1 << nbits) - 1
66
- min = 0
65
+ max = "max = (1 << #{nbits}) - 1"
66
+ min = "min = 0"
67
67
  end
68
68
 
69
- "val = (val < #{min}) ? #{min} : (val > #{max}) ? #{max} : val"
69
+ clamp = "(#{max}; #{min}; val = (val < min) ? min : (val > max) ? max : val)"
70
+
71
+ "val = #{clamp}"
70
72
  end
71
73
 
72
74
  def create_read_code(nbits, endian, signed)
@@ -1,3 +1,3 @@
1
1
  module BinData
2
- VERSION = "2.4.9"
2
+ VERSION = "2.4.11"
3
3
  end
@@ -184,6 +184,51 @@ describe BinData::DelayedIO, "inside a Record" do
184
184
  end
185
185
  end
186
186
 
187
+ describe BinData::DelayedIO, "inside a Record with onlyif" do
188
+ class DelayedIOOnlyIfRecord < BinData::Record
189
+ endian :little
190
+
191
+ uint8 :flag
192
+ delayed_io :my_int1, read_abs_offset: 4, onlyif: -> { flag != 0 } do
193
+ uint16 initial_value: 6
194
+ end
195
+ delayed_io :my_int2, read_abs_offset: 2, onlyif: -> { flag == 0 } do
196
+ uint16 initial_value: 7
197
+ end
198
+ end
199
+
200
+ it "reads" do
201
+ obj = DelayedIOOnlyIfRecord.read "\x01\x00\x03\x0012345"
202
+ obj.num_bytes.must_equal 1
203
+ obj.snapshot.must_equal({flag: 1, my_int1: 6, my_int2: 7})
204
+ end
205
+
206
+ it "reads explicitly when flag is set" do
207
+ obj = DelayedIOOnlyIfRecord.read "\x01\xff\x01\x00\x02\x00"
208
+ obj.my_int1.read_now!
209
+ obj.my_int2.read_now!
210
+ obj.num_bytes.must_equal 1
211
+ obj.snapshot.must_equal({flag: 1, my_int1: 2, my_int2: 7})
212
+ end
213
+
214
+ it "reads explicitly when flag is not set" do
215
+ obj = DelayedIOOnlyIfRecord.read "\x00\xff\x01\x00\x02\x00"
216
+ obj.my_int1.read_now!
217
+ obj.my_int2.read_now!
218
+ obj.num_bytes.must_equal 1
219
+ obj.snapshot.must_equal({flag: 0, my_int1: 6, my_int2: 1})
220
+ end
221
+
222
+ it "writes" do
223
+ obj = DelayedIOOnlyIfRecord.new(flag:1, my_int1: 3, my_int2: 4)
224
+ io = StringIO.new
225
+ obj.write(io)
226
+ obj.my_int1.write_now!
227
+ obj.my_int2.write_now!
228
+ io.value.must_equal "\x01\x00\x00\x00\x03\x00"
229
+ end
230
+ end
231
+
187
232
  describe BinData::DelayedIO, "with auto_call" do
188
233
  class AutoCallDelayedIORecord < BinData::Record
189
234
  auto_call_delayed_io
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bindata
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.9
4
+ version: 2.4.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dion Mendel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-21 00:00:00.000000000 Z
11
+ date: 2022-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake