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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 21f656cdac9fe221206c29eff87e7c068a4460a1cc7922eb25858ec0d657d840
4
- data.tar.gz: 8eca414c087981d96cf919e96a7554dfa4bdb9336af39f56b4950410d815a9bc
3
+ metadata.gz: 535a978c6d95f5213d7a7e63523b46788adc2d6ca83c714758e9f30f927f546f
4
+ data.tar.gz: 5f52aea99a2615221a62e9b7d3228f129f1320e2a21c8b65eae3fec644e4007a
5
5
  SHA512:
6
- metadata.gz: 8d0e8bdad84fb2e8995680dcf9ca7330868101bc196f4ff2d6cde7ec393b9c1c819a1a756dd939e002a8be1b35178ccce8328ef65e54ed85c915d6a5d6801ab9
7
- data.tar.gz: 739cf3c06238c1e7601d52d60e625eec4bfa703c3ffb1503f6ed670200801c63c7913c58b30af860aed907ea5adab0ccbfb2981332b533790d33374e82a9eabb
6
+ metadata.gz: 5a7489c0f185fb36a4e54c51273c2b87bd344754522e6584b329f529ed0ae00c8c287a1abf224b4cd3bfc1ed44fa5694c8036c89b3ef881231f8cfb4f773b6b0
7
+ data.tar.gz: 4fa626885e4446c846594c1be13b67649928e45e919aabc660cc2c414c7cb225efb29841e5bd856737d905d988f5dd2219dd3c1622c976cb2e656799beb2a0fe
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IOStruct
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
5
5
  end
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 = [len.to_i, 1].max
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 inspect
133
- @fmtstr ||= "<#{self.class.to_s} " + self.class.const_get('FIELDS').map do |name, f|
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 @fmtstr, *to_a.map{ |v| v.is_a?(String) ? v.inspect : v }
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
@@ -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.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.6
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