iostruct 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 78f534f107abf828f64d62134f93a553cb5af56af1860c338e0844b49cb482ba
4
+ data.tar.gz: cdd0dfc48cf8a171bfcfc25fbe630182424008376e245b6a4816b12ed183940e
5
+ SHA512:
6
+ metadata.gz: 534ccfd92eadd911de930d08cc787dab929d0a2af5210da3700b5f6ea98f444896d9cdc674b657216f03da753d902acf80a55ae21cb4943b34fd7e065ab8a0af
7
+ data.tar.gz: 660427b2d4652c94791a8552dd2542614fd6b7e04a895b0ab7d1ffeeabfc508b77c02d61788bd1a9a46f9c2880257cddf274297885e4b0c11d8e6f141632a518
data/Gemfile.lock CHANGED
@@ -1,17 +1,20 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- iostruct (0.0.2)
4
+ iostruct (0.0.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- rake (10.0.3)
9
+ rake (13.0.6)
10
10
 
11
11
  PLATFORMS
12
12
  ruby
13
13
 
14
14
  DEPENDENCIES
15
- bundler (~> 1.3)
15
+ bundler
16
16
  iostruct!
17
17
  rake
18
+
19
+ BUNDLED WITH
20
+ 2.1.4
data/iostruct.gemspec CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |s|
17
17
  s.require_paths = ["lib"]
18
18
  s.summary = "A Struct that can read/write itself from/to IO-like objects"
19
19
 
20
- s.add_development_dependency "bundler", "~> 1.3"
20
+ s.add_development_dependency "bundler"
21
21
  s.add_development_dependency "rake"
22
22
  end
23
23
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IOStruct
2
- VERSION = "0.0.4"
4
+ VERSION = "0.0.5"
3
5
  end
data/lib/iostruct.rb CHANGED
@@ -1,14 +1,45 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IOStruct
4
+
5
+ # https://apidock.com/ruby/String/unpack
6
+ FIELD_SIZES = {
7
+ 'C' => 1, # Integer | 8-bit unsigned (unsigned char)
8
+ 'S' => 2, # Integer | 16-bit unsigned, native endian (uint16_t)
9
+ 'L' => 4, # Integer | 32-bit unsigned, native endian (uint32_t)
10
+ 'Q' => 8, # Integer | 64-bit unsigned, native endian (uint64_t)
11
+ 'c' => 1, # Integer | 8-bit signed (signed char)
12
+ 's' => 2, # Integer | 16-bit signed, native endian (int16_t)
13
+ 'l' => 4, # Integer | 32-bit signed, native endian (int32_t)
14
+ 'q' => 8, # Integer | 64-bit signed, native endian (int64_t)
15
+
16
+ 'n' => 2, # Integer | 16-bit unsigned, network (big-endian) byte order
17
+ 'N' => 4, # Integer | 32-bit unsigned, network (big-endian) byte order
18
+ 'v' => 2, # Integer | 16-bit unsigned, VAX (little-endian) byte order
19
+ 'V' => 4, # Integer | 32-bit unsigned, VAX (little-endian) byte order
20
+
21
+ 'A' => 1, # String | arbitrary binary string (remove trailing nulls and ASCII spaces)
22
+ 'a' => 1, # String | arbitrary binary string
23
+
24
+ 'D' => 8, # Float | double-precision, native format
25
+ 'd' => 8,
26
+ 'F' => 4, # Float | single-precision, native format
27
+ 'f' => 4,
28
+ 'E' => 8, # Float | double-precision, little-endian byte order
29
+ 'e' => 4, # Float | single-precision, little-endian byte order
30
+ 'G' => 8, # Float | double-precision, network (big-endian) byte order
31
+ 'g' => 4, # Float | single-precision, network (big-endian) byte order
32
+
33
+ 'x' => 1, # --- | skip forward one byte
34
+ }.freeze
35
+
2
36
  def self.new fmt, *args
3
37
  size = fmt.scan(/([a-z])(\d*)/i).map do |f,len|
4
- [len.to_i, 1].max *
5
- case f
6
- when /[AaCcx]/ then 1
7
- when 'v' then 2
8
- when 'V','l','L' then 4
9
- when 'Q' then 8
10
- else raise "unknown fmt #{f.inspect}"
11
- end
38
+ if (field_size = FIELD_SIZES[f])
39
+ [len.to_i, 1].max * field_size
40
+ else
41
+ raise "Unknown fmt #{f.inspect}"
42
+ end
12
43
  end.inject(&:+)
13
44
 
14
45
  Struct.new( *args ).tap do |x|
@@ -4,26 +4,46 @@ require 'stringio'
4
4
  describe IOStruct do
5
5
  describe "#read" do
6
6
  it "reads from IO" do
7
- a = [12345,56789]
7
+ a = [12345, 56789]
8
8
  data = a.pack('L2')
9
9
  x = IOStruct.new('LL', :x, :y).read(StringIO.new(data))
10
- x.x.should == a[0]
11
- x.y.should == a[1]
10
+ expect(x.x).to eq a[0]
11
+ expect(x.y).to eq a[1]
12
12
  end
13
13
 
14
14
  it "reads from String" do
15
- a = [12345,56789]
15
+ a = [12345, 56789]
16
16
  data = a.pack('L2')
17
17
  x = IOStruct.new('LL', :x, :y).read(data)
18
- x.x.should == a[0]
19
- x.y.should == a[1]
18
+ expect(x.x).to eq a[0]
19
+ expect(x.y).to eq a[1]
20
20
  end
21
21
  end
22
22
 
23
23
  it "skips on 'x'" do
24
- a = [12345,56789]
24
+ a = [12345, 56789]
25
25
  data = a.pack('L2')
26
26
  x = IOStruct.new('x4L', :y).read(data)
27
- x.y.should == a[1]
27
+ expect(x.y).to eq a[1]
28
+ end
29
+
30
+ it "unpacks big-endian" do
31
+ a = [12345, 56789]
32
+ data = a.pack('nN')
33
+ x = IOStruct.new('nN', :x, :y).read(data)
34
+ expect(x.x).to eq a[0]
35
+ expect(x.y).to eq a[1]
36
+ end
37
+
38
+ it "unpacks little-endian" do
39
+ a = [12345, 56789]
40
+ data = a.pack('vV')
41
+ x = IOStruct.new('vV', :x, :y).read(data)
42
+ expect(x.x).to eq a[0]
43
+ expect(x.y).to eq a[1]
44
+ end
45
+
46
+ it "throws exception on unknown format" do
47
+ expect { IOStruct.new('K', :x) }.to raise_error('Unknown fmt "K"')
28
48
  end
29
49
  end
data/spec/spec_helper.rb CHANGED
@@ -2,6 +2,5 @@ require File.join(File.dirname(__FILE__), '../lib/iostruct.rb')
2
2
 
3
3
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
4
4
  RSpec.configure do |config|
5
- config.treat_symbols_as_metadata_keys_with_true_values = true
6
5
  config.run_all_when_everything_filtered = true
7
6
  end
metadata CHANGED
@@ -1,12 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iostruct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
5
- prerelease:
4
+ version: 0.0.5
6
5
  platform: ruby
7
6
  authors:
8
7
  - Andrey "Zed" Zaikin
9
- autorequire:
8
+ autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
11
  date: 2013-01-08 00:00:00.000000000 Z
@@ -14,43 +13,39 @@ dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
- version: '1.3'
19
+ version: '0'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
- version: '1.3'
26
+ version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
- description:
41
+ description:
47
42
  email: zed.0xff@gmail.com
48
43
  executables: []
49
44
  extensions: []
50
45
  extra_rdoc_files: []
51
46
  files:
52
- - .gitignore
53
- - .rspec
47
+ - ".gitignore"
48
+ - ".rspec"
54
49
  - Gemfile
55
50
  - Gemfile.lock
56
51
  - LICENSE.txt
@@ -64,26 +59,24 @@ files:
64
59
  homepage: http://github.com/zed-0xff/iostruct
65
60
  licenses:
66
61
  - MIT
67
- post_install_message:
62
+ metadata: {}
63
+ post_install_message:
68
64
  rdoc_options: []
69
65
  require_paths:
70
66
  - lib
71
67
  required_ruby_version: !ruby/object:Gem::Requirement
72
- none: false
73
68
  requirements:
74
- - - ! '>='
69
+ - - ">="
75
70
  - !ruby/object:Gem::Version
76
71
  version: '0'
77
72
  required_rubygems_version: !ruby/object:Gem::Requirement
78
- none: false
79
73
  requirements:
80
- - - ! '>='
74
+ - - ">="
81
75
  - !ruby/object:Gem::Version
82
76
  version: '0'
83
77
  requirements: []
84
- rubyforge_project:
85
- rubygems_version: 1.8.24
86
- signing_key:
87
- specification_version: 3
78
+ rubygems_version: 3.3.7
79
+ signing_key:
80
+ specification_version: 4
88
81
  summary: A Struct that can read/write itself from/to IO-like objects
89
82
  test_files: []