iostruct 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/iostruct/version.rb +1 -1
- data/lib/iostruct.rb +18 -4
- data/spec/iostruct_spec.rb +27 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 535a978c6d95f5213d7a7e63523b46788adc2d6ca83c714758e9f30f927f546f
|
4
|
+
data.tar.gz: 5f52aea99a2615221a62e9b7d3228f129f1320e2a21c8b65eae3fec644e4007a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a7489c0f185fb36a4e54c51273c2b87bd344754522e6584b329f529ed0ae00c8c287a1abf224b4cd3bfc1ed44fa5694c8036c89b3ef881231f8cfb4f773b6b0
|
7
|
+
data.tar.gz: 4fa626885e4446c846594c1be13b67649928e45e919aabc660cc2c414c7cb225efb29841e5bd856737d905d988f5dd2219dd3c1622c976cb2e656799beb2a0fe
|
data/lib/iostruct/version.rb
CHANGED
data/lib/iostruct.rb
CHANGED
@@ -57,7 +57,7 @@ module IOStruct
|
|
57
57
|
fields = []
|
58
58
|
fmt.scan(/([a-z])(\d*)/i).map do |type,len|
|
59
59
|
size, klass = FMTSPEC[type] || raise("Unknown field type #{type.inspect}")
|
60
|
-
len =
|
60
|
+
len = len.empty? ? 1 : len.to_i
|
61
61
|
case type
|
62
62
|
when 'A', 'a', 'x'
|
63
63
|
fields << FieldInfo.new(klass, size*len, offset) if klass
|
@@ -129,8 +129,18 @@ module IOStruct
|
|
129
129
|
end # InstanceMethods
|
130
130
|
|
131
131
|
module HexInspect
|
132
|
-
def
|
133
|
-
|
132
|
+
def to_s
|
133
|
+
s = "<#{self.class.to_s} " + to_h.map do |k, v|
|
134
|
+
if v.is_a?(Integer) && v > 9
|
135
|
+
"#{k}=0x%x" % v
|
136
|
+
else
|
137
|
+
"#{k}=#{v.inspect}"
|
138
|
+
end
|
139
|
+
end.join(' ') + ">"
|
140
|
+
end
|
141
|
+
|
142
|
+
def to_table
|
143
|
+
@fmtstr_tbl = "<#{self.class.to_s} " + self.class.const_get('FIELDS').map do |name, f|
|
134
144
|
fmt =
|
135
145
|
case
|
136
146
|
when f.type == Integer
|
@@ -142,7 +152,11 @@ module IOStruct
|
|
142
152
|
end
|
143
153
|
"#{name}=#{fmt}"
|
144
154
|
end.join(' ') + ">"
|
145
|
-
sprintf @
|
155
|
+
sprintf @fmtstr_tbl, *to_a.map{ |v| v.is_a?(String) ? v.inspect : v }
|
156
|
+
end
|
157
|
+
|
158
|
+
def inspect
|
159
|
+
to_s
|
146
160
|
end
|
147
161
|
end
|
148
162
|
end # IOStruct
|
data/spec/iostruct_spec.rb
CHANGED
@@ -25,6 +25,33 @@ describe IOStruct do
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
+
context "zero-length strings" do
|
29
|
+
let(:data) { [1, 2].pack('CC') }
|
30
|
+
let(:struct) { IOStruct.new('C a0 C', :a, :b, :c) }
|
31
|
+
|
32
|
+
it "deserializes" do
|
33
|
+
x = struct.read(data)
|
34
|
+
expect(x.a).to eq 1
|
35
|
+
expect(x.b).to eq ""
|
36
|
+
expect(x.c).to eq 2
|
37
|
+
end
|
38
|
+
|
39
|
+
it "has correct size" do
|
40
|
+
expect(struct::SIZE).to eq 2
|
41
|
+
end
|
42
|
+
|
43
|
+
it "reads correct number of bytes from IO" do
|
44
|
+
io = StringIO.new(data*2)
|
45
|
+
x = struct.read(io)
|
46
|
+
expect(io.pos).to eq 2
|
47
|
+
end
|
48
|
+
|
49
|
+
it "serializes" do
|
50
|
+
x = struct.read(data)
|
51
|
+
expect(x.pack).to eq data
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
28
55
|
it "skips on 'x'" do
|
29
56
|
a = [12345, 56789]
|
30
57
|
data = a.pack('L2')
|
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.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrey "Zed" Zaikin
|
@@ -89,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
89
|
- !ruby/object:Gem::Version
|
90
90
|
version: '0'
|
91
91
|
requirements: []
|
92
|
-
rubygems_version: 3.5.
|
92
|
+
rubygems_version: 3.5.3
|
93
93
|
signing_key:
|
94
94
|
specification_version: 4
|
95
95
|
summary: A Struct that can read/write itself from/to IO-like objects
|