iostruct 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +24 -0
- data/Gemfile.lock +1 -1
- data/lib/iostruct/version.rb +1 -1
- data/lib/iostruct.rb +9 -1
- data/spec/iostruct_spec.rb +28 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0376aab4653f3f1fb656d82f679346bd1d04c6d8f80e17540a4a7fbb3bc470d7
|
4
|
+
data.tar.gz: 211894d68015bc52658e8e818296a42c115bdeda8cfa2cb6525cadb1e9554e9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79edc6088c3258506085cf22aee37e75386bab8309c478786ec3022f3895f34a4b05490640bca22db56b78242b500d7030d3fc6b141a24aa0f9cfc1681baef7d
|
7
|
+
data.tar.gz: 36ddc86813c0c0fc92874025fbe37f755a8f1cef22533949b101428b50ca2a0369aae9beddfe99a4b00d71c0db5985ec4f89b5beddefcc262ea6c91dfd2787b7
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# 0.4.0
|
2
|
+
|
3
|
+
- added `size` class method that returns SIZE constant
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
X = IOStruct.new('LL')
|
7
|
+
X::SIZE # 8
|
8
|
+
X.size # 8
|
9
|
+
```
|
10
|
+
|
11
|
+
# 0.3.0
|
12
|
+
|
13
|
+
- added `__offset` field:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
X = IOStruct.new('LL')
|
17
|
+
io = StringIO.new('x'*1000)
|
18
|
+
|
19
|
+
X.read(io).__offset # 0
|
20
|
+
X.read(io).__offset # 8
|
21
|
+
X.read(io).__offset # 16
|
22
|
+
|
23
|
+
X.read('abcd').__offset # nil
|
24
|
+
```
|
data/Gemfile.lock
CHANGED
data/lib/iostruct/version.rb
CHANGED
data/lib/iostruct.rb
CHANGED
@@ -94,9 +94,11 @@ module IOStruct
|
|
94
94
|
module ClassMethods
|
95
95
|
# src can be IO or String, or anything that responds to :read or :unpack
|
96
96
|
def read src, size = nil
|
97
|
+
pos = nil
|
97
98
|
size ||= const_get 'SIZE'
|
98
99
|
data =
|
99
100
|
if src.respond_to?(:read)
|
101
|
+
pos = src.tell
|
100
102
|
src.read(size).to_s
|
101
103
|
elsif src.respond_to?(:unpack)
|
102
104
|
src
|
@@ -106,11 +108,17 @@ module IOStruct
|
|
106
108
|
# if data.size < size
|
107
109
|
# $stderr.puts "[!] #{self.to_s} want #{size} bytes, got #{data.size}"
|
108
110
|
# end
|
109
|
-
new(*data.unpack(const_get('FORMAT')))
|
111
|
+
new(*data.unpack(const_get('FORMAT'))).tap{ |x| x.__offset = pos }
|
112
|
+
end
|
113
|
+
|
114
|
+
def size
|
115
|
+
self::SIZE
|
110
116
|
end
|
111
117
|
end # ClassMethods
|
112
118
|
|
113
119
|
module InstanceMethods
|
120
|
+
attr_accessor :__offset
|
121
|
+
|
114
122
|
def pack
|
115
123
|
to_a.pack self.class.const_get('FORMAT')
|
116
124
|
end
|
data/spec/iostruct_spec.rb
CHANGED
@@ -36,10 +36,14 @@ describe IOStruct do
|
|
36
36
|
expect(x.c).to eq 2
|
37
37
|
end
|
38
38
|
|
39
|
-
it "has correct
|
39
|
+
it "has correct SIZE" do
|
40
40
|
expect(struct::SIZE).to eq 2
|
41
41
|
end
|
42
42
|
|
43
|
+
it "has correct size" do
|
44
|
+
expect(struct.size).to eq 2
|
45
|
+
end
|
46
|
+
|
43
47
|
it "reads correct number of bytes from IO" do
|
44
48
|
io = StringIO.new(data*2)
|
45
49
|
x = struct.read(io)
|
@@ -96,4 +100,27 @@ describe IOStruct do
|
|
96
100
|
it "throws exception on unknown format" do
|
97
101
|
expect { IOStruct.new('K', :x) }.to raise_error('Unknown field type "K"')
|
98
102
|
end
|
103
|
+
|
104
|
+
context '__offset field' do
|
105
|
+
let(:data) { 0x100.times.to_a.pack('L*') }
|
106
|
+
let(:io) { StringIO.new(data) }
|
107
|
+
let(:struct) { IOStruct.new('LLLL', :a, :b, :c, :d) }
|
108
|
+
|
109
|
+
context 'when src is an IO' do
|
110
|
+
it 'is set to the current IO position' do
|
111
|
+
a = []
|
112
|
+
while !io.eof?
|
113
|
+
a << struct.read(io)
|
114
|
+
end
|
115
|
+
expect(a.map(&:__offset)).to eq (0...0x400).step(0x10).to_a
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
context 'when src is a string' do
|
120
|
+
it 'is nil' do
|
121
|
+
x = struct.read(data)
|
122
|
+
expect(x.__offset).to be_nil
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
99
126
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: iostruct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrey "Zed" Zaikin
|
@@ -60,6 +60,7 @@ extra_rdoc_files: []
|
|
60
60
|
files:
|
61
61
|
- ".gitignore"
|
62
62
|
- ".rspec"
|
63
|
+
- CHANGELOG.md
|
63
64
|
- Gemfile
|
64
65
|
- Gemfile.lock
|
65
66
|
- LICENSE.txt
|
@@ -89,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
90
|
- !ruby/object:Gem::Version
|
90
91
|
version: '0'
|
91
92
|
requirements: []
|
92
|
-
rubygems_version: 3.5.
|
93
|
+
rubygems_version: 3.5.22
|
93
94
|
signing_key:
|
94
95
|
specification_version: 4
|
95
96
|
summary: A Struct that can read/write itself from/to IO-like objects
|