iostruct 0.2.0 → 0.3.0

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: 7b5299fc722f0b62051802a5ec32ac37de25c48b92a5899a77da7494aa6dfa73
4
- data.tar.gz: dd50d4aa68a79fded59a6720867bea0b4961ca7513d79cf124bda6ad9d5e40fc
3
+ metadata.gz: 4a17b782202e0503788fd3b16dffdebddecd62becb30c52ba28161ebde1e300d
4
+ data.tar.gz: d42484281a468a378902d4589b67201ad4f134660559766690e48849b9f2a46c
5
5
  SHA512:
6
- metadata.gz: b3434b61ba2cac8b9465a91da45d7dd5ee5aadd5eeaba77c3d115a564b7dbb31ecdb37e579bd3b41132af5eff42ad687c12ebf4d69da5a4015e23fa317e5b133
7
- data.tar.gz: 852776b5bbaacf3ac4107a9755879ca986841e7d415bc16310e7ee6f565b6335b58c2d0730fd5de20ecde7843abe64c30c898855bb48e918e8bd7f83b527b5a0
6
+ metadata.gz: db34080f66b2815628e20a6e7b9331089d55a011dd35ae90b9e8f7758cb97332fad7b9caadfcc977a93e104cb0d9b9e3c932e82a2070e54205ba3bb7a52337d4
7
+ data.tar.gz: d25f3804da77a52630bb4492548c3ab71124c06610558dde277ea2c769a826ce8a720a5df99b2ab3691791b090f6ec4774b65e8072117383ab389e4095451a0f
data/CHANGELOG.md ADDED
@@ -0,0 +1,14 @@
1
+ # 0.3.0
2
+
3
+ - added `__offset` field:
4
+
5
+ ```ruby
6
+ X = IOStruct.new('LL')
7
+ io = StringIO.new('x'*1000)
8
+
9
+ X.read(io).__offset # 0
10
+ X.read(io).__offset # 8
11
+ X.read(io).__offset # 16
12
+
13
+ X.read('abcd').__offset # nil
14
+ ```
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- iostruct (0.1.0)
4
+ iostruct (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IOStruct
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
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,13 @@ 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 }
110
112
  end
111
113
  end # ClassMethods
112
114
 
113
115
  module InstanceMethods
116
+ attr_accessor :__offset
117
+
114
118
  def pack
115
119
  to_a.pack self.class.const_get('FORMAT')
116
120
  end
@@ -96,4 +96,27 @@ describe IOStruct do
96
96
  it "throws exception on unknown format" do
97
97
  expect { IOStruct.new('K', :x) }.to raise_error('Unknown field type "K"')
98
98
  end
99
+
100
+ context '__offset field' do
101
+ let(:data) { 0x100.times.to_a.pack('L*') }
102
+ let(:io) { StringIO.new(data) }
103
+ let(:struct) { IOStruct.new('LLLL', :a, :b, :c, :d) }
104
+
105
+ context 'when src is an IO' do
106
+ it 'is set to the current IO position' do
107
+ a = []
108
+ while !io.eof?
109
+ a << struct.read(io)
110
+ end
111
+ expect(a.map(&:__offset)).to eq (0...0x400).step(0x10).to_a
112
+ end
113
+ end
114
+
115
+ context 'when src is a string' do
116
+ it 'is nil' do
117
+ x = struct.read(data)
118
+ expect(x.__offset).to be_nil
119
+ end
120
+ end
121
+ end
99
122
  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.2.0
4
+ version: 0.3.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.11
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