marcandre-packable 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,5 +1,10 @@
1
1
  = Packable --- History
2
2
 
3
+ == Version 1.3 - April 8, 2009
4
+ Added :endian => :native for Integers and Floats (thanks to Jay Daley)
5
+ Raises an exception if :endian is not one of :little, :big, :network or :native
6
+ Added some tests for Bignum
7
+
3
8
  == Version 1.2 - April 2nd, 2009
4
9
  Compatible with ruby 1.9.1.
5
10
  The 'jungle_survival_kit' is now in its own 'backports' gem.
data/README.rdoc CHANGED
@@ -73,12 +73,12 @@ These are the options for core types:
73
73
 
74
74
  === Integer
75
75
  [+bytes+] Number of bytes (default is 4) to use.
76
- [+endian+] Either <tt>:big</tt> (or :network, default) or <tt>:little</tt>.
76
+ [+endian+] Either <tt>:big</tt> (or <tt>:network</tt>, default), <tt>:little</tt> or <tt>:native</tt>.
77
77
  [+signed+] Either +true+ (default) or +false+. This will make a difference only when unpacking.
78
78
 
79
79
  === Float
80
80
  [+precision+] Either <tt>:single</tt> (default) or <tt>:double</tt>.
81
- [+endian+] Either <tt>:big</tt> (or :network, default) or <tt>:little</tt>.
81
+ [+endian+] Either <tt>:big</tt> (or <tt>:network</tt>, default), <tt>:little</tt> or <tt>:native</tt>.
82
82
 
83
83
  === String
84
84
  [+bytes+] Total length (default is the full length)
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
+ :patch: 0
2
3
  :major: 1
3
- :minor: 2
4
- :patch: 1
4
+ :minor: 3
@@ -19,8 +19,11 @@ module Packable
19
19
  end
20
20
 
21
21
  module ClassMethods #:nodoc:
22
+ ENDIAN_TO_FORMAT = Hash.new{|h, endian| raise ArgumentError, "Endian #{endian} is not valid. It must be one of #{h.keys.join(', ')}"}.
23
+ merge!(:big => "G", :network => "G", :little => "E", :native => "F").freeze
24
+
22
25
  def pack_option_to_format(options)
23
- format = {:big => "G", :network => "G", :little => "E"}[options[:endian]]
26
+ format = ENDIAN_TO_FORMAT[options[:endian]]
24
27
  format.downcase! if options[:precision] == :single
25
28
  format
26
29
  end
@@ -1,6 +1,9 @@
1
1
  module Packable
2
2
  module Extensions #:nodoc:
3
3
  module Integer #:nodoc:
4
+ NEEDS_REVERSAL = Hash.new{|h, endian| raise ArgumentError, "Endian #{endian} is not valid. It must be one of #{h.keys.join(', ')}"}.
5
+ merge!(:little => true, :big => false, :network => false, :native => "*\x00\x00\x00".unpack('L').first == 42).freeze
6
+
4
7
  def self.included(base)
5
8
  base.class_eval do
6
9
  include Packable
@@ -25,13 +28,13 @@ module Packable
25
28
  val >>= 8
26
29
  byte.chr
27
30
  end
28
- chars.reverse! unless options[:endian] == :little
31
+ chars.reverse! unless NEEDS_REVERSAL[options[:endian]]
29
32
  io << chars.join
30
33
  end
31
34
 
32
35
  module ClassMethods #:nodoc:
33
36
  def unpack_string(s,options)
34
- s = s.reverse if options[:endian] == :little
37
+ s = s.reverse if NEEDS_REVERSAL[options[:endian]]
35
38
  r = 0
36
39
  s.each_byte {|b| r = (r << 8) + b}
37
40
  r -= 1 << (8 * options[:bytes]) if options[:signed] && (1 == r >> (8 * options[:bytes] - 1))
data/test/packing_test.rb CHANGED
@@ -47,6 +47,20 @@ class TestingPack < Test::Unit::TestCase
47
47
  assert_equal 258, Integer.unpack("\002\001\000", :bytes => 3, :endian => :little)
48
48
  assert_equal (1<<24)-1, -1.pack(:bytes => 3).unpack(Integer, :bytes => 3, :signed => false)
49
49
  assert_equal -1, -1.pack(:bytes => 3).unpack(Integer, :bytes => 3, :signed => true)
50
+ assert_equal 42, 42.pack('L').unpack(Integer, :bytes => 4, :endian => :native)
51
+ assert_raise(ArgumentError){ 42.pack(:endian => "Geronimo")}
52
+ end
53
+
54
+ def test_bignum
55
+ assert_equal 1.pack(:long), ((1 << 69) + 1).pack(:long)
56
+ assert_equal "*" + ("\000" * 15), (42 << (8*15)).pack(:bytes => 16)
57
+ assert_equal 42 << (8*15), (42 << (8*15)).pack(:bytes => 16).unpack(Integer, :bytes => 16)
58
+ assert_equal 42 << (8*15), (42 << (8*15)).pack(:bytes => 16).unpack(Bignum, :bytes => 16)
59
+ end
60
+
61
+ def test_float
62
+ assert_equal Math::PI.pack(:precision => :double, :endian => :native), Math::PI.pack('F')
63
+ assert_raise(ArgumentError){ Math::PI.pack(:endian => "Geronimo")}
50
64
  end
51
65
 
52
66
  def test_io
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: marcandre-packable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Marc-Andr\xC3\xA9 Lafortune"
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-07 00:00:00 -07:00
12
+ date: 2009-04-08 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency