marcandre-packable 1.1.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +8 -8
- data/VERSION.yml +1 -1
- data/lib/packable/extensions/float.rb +1 -1
- data/lib/packable/extensions/integer.rb +2 -2
- data/test/packing_test.rb +2 -1
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -70,20 +70,20 @@ Although the standard string formats can still be used, it is possible to pass a
|
|
70
70
|
These are the options for core types:
|
71
71
|
|
72
72
|
=== Integer
|
73
|
-
+bytes
|
74
|
-
+endian
|
75
|
-
+signed
|
73
|
+
[+bytes+] Number of bytes (default is 4) to use.
|
74
|
+
[+endian+] Either <tt>:big</tt> (or :network, default) or <tt>:little</tt>.
|
75
|
+
[+signed+] Either +true+ (default) or not. This will make a difference only when unpacking.
|
76
76
|
|
77
77
|
=== Float
|
78
|
-
+precision
|
79
|
-
+endian
|
78
|
+
[+precision+] Either <tt>:single</tt> (default) or <tt>:double</tt>.
|
79
|
+
[+endian+] Either <tt>:big</tt> (or :network, default) or <tt>:little</tt>.
|
80
80
|
|
81
81
|
=== String
|
82
|
-
+bytes
|
83
|
-
+fill
|
82
|
+
[+bytes+] Total length (default is the full length)
|
83
|
+
[+fill+] The string to use for filling when packing a string shorter than the specified bytes option. Default is a space.
|
84
84
|
|
85
85
|
=== Array
|
86
|
-
+repeat
|
86
|
+
[+repeat+] This option can be used (when packing only) to repeat the current option. A value of <tt>:all</tt> will mean for all remaining elements of the array.
|
87
87
|
|
88
88
|
When unpacking, it is necessary to specify the class in addition to any option, like so:
|
89
89
|
|
data/VERSION.yml
CHANGED
@@ -20,7 +20,7 @@ module Packable
|
|
20
20
|
|
21
21
|
module ClassMethods #:nodoc:
|
22
22
|
def pack_option_to_format(options)
|
23
|
-
format = {:big => "G", :
|
23
|
+
format = {:big => "G", :network => "G", :little => "E"}[options[:endian]]
|
24
24
|
format.downcase! if options[:precision] == :single
|
25
25
|
format
|
26
26
|
end
|
@@ -25,13 +25,13 @@ module Packable
|
|
25
25
|
val >>= 8
|
26
26
|
byte.chr
|
27
27
|
end
|
28
|
-
chars.reverse!
|
28
|
+
chars.reverse! unless options[:endian] == :little
|
29
29
|
io << chars.join
|
30
30
|
end
|
31
31
|
|
32
32
|
module ClassMethods #:nodoc:
|
33
33
|
def unpack_string(s,options)
|
34
|
-
s = s.reverse if options[:endian
|
34
|
+
s = s.reverse if options[:endian] == :little
|
35
35
|
r = 0
|
36
36
|
s.each_byte {|b| r = (r << 8) + b}
|
37
37
|
r -= 1 << (8 * options[:bytes]) if options[:signed] && (1 == r >> (8 * options[:bytes] - 1))
|
data/test/packing_test.rb
CHANGED
@@ -43,7 +43,8 @@ class TestingPack < Test::Unit::TestCase
|
|
43
43
|
end
|
44
44
|
|
45
45
|
def test_integer
|
46
|
-
assert_equal "\002\001\000", 258.pack(:bytes => 3, :endian => :
|
46
|
+
assert_equal "\002\001\000", 258.pack(:bytes => 3, :endian => :little)
|
47
|
+
assert_equal 258, Integer.unpack("\002\001\000", :bytes => 3, :endian => :little)
|
47
48
|
assert_equal (1<<24)-1, -1.pack(:bytes => 3).unpack(Integer, :bytes => 3, :signed => false)
|
48
49
|
assert_equal -1, -1.pack(:bytes => 3).unpack(Integer, :bytes => 3, :signed => true)
|
49
50
|
end
|