bit-struct 0.13.1 → 0.13.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/History.txt CHANGED
@@ -1,3 +1,9 @@
1
+ bit-struct 0.13
2
+
3
+ - Packaged as a gem using bones.
4
+
5
+ - Added nest fields defined with blocks.
6
+
1
7
  bit-struct 0.12
2
8
 
3
9
  - Added vectors.
data/TODO CHANGED
@@ -1,15 +1,10 @@
1
- website:
2
-
3
- scp doc/ vjoel@rubyforge.org:/var/www/gforge-projects/bit-struct/
4
-
5
-
6
1
  easy way to define wrappers
7
2
 
8
-
9
- to do:
10
-
11
3
  generate C code from bit-struct spec?
12
4
 
5
+ write extension to extract and insert bit-fields, and use that instead
6
+ of pack/unpack
7
+
13
8
  variable-length embedded fields, with referenced length field
14
9
 
15
10
  terminated arrays?
data/TODO-ALSO CHANGED
@@ -1,22 +1,5 @@
1
1
  rest :name, :terminator => ...
2
2
 
3
- > * Allow block also for "nest" ?
4
- >
5
- > It may sound redundant in the first place, but when it's just for
6
- > providing more structure without requiring an extra class definition, it
7
- > could be useful:
8
- >
9
- > class Foo < BitStruct
10
- > nest :coord do
11
- > signed :x, 8
12
- > signed :y, 8
13
- > end
14
- > end
15
- >
16
- > Foo.new.coord.x
17
-
18
- Agree completely.
19
-
20
3
  > * Is explicit :length for vector necessary?
21
4
  >
22
5
  > class Tag < BitStruct
@@ -0,0 +1,23 @@
1
+ require 'bit-struct'
2
+
3
+ class Container < BitStruct
4
+ nest :n, "Nest" do
5
+ unsigned :x, 5
6
+ unsigned :y, 3
7
+ char :s, 5*8
8
+ end
9
+ end
10
+
11
+ cont = Container.new
12
+
13
+ n = cont.n
14
+
15
+ n.x = 5
16
+ n.y = 0
17
+ n.s = " xyz "
18
+
19
+ cont.n = n # note copy semantics here!
20
+
21
+ p cont
22
+ puts
23
+ puts cont.inspect_detailed
@@ -9,7 +9,7 @@
9
9
  # The String#replace method is useful.
10
10
  #
11
11
  class BitStruct < String
12
- VERSION = "0.13.1"
12
+ VERSION = "0.13.2"
13
13
 
14
14
  class Field
15
15
  # Offset of field in bits.
@@ -54,15 +54,10 @@ class BitStruct
54
54
  # with the given _name_ and _nested_class_. Length is determined from
55
55
  # _nested_class_.
56
56
  #
57
- # In _rest_:
58
- #
59
57
  # If a class is provided, use it for the Field class (i.e. <=NestedField).
60
58
  # If a string is provided, use it for the display_name.
61
59
  # If a hash is provided, use it for options.
62
60
  #
63
- # WARNING: the accessors have COPY semantics, not reference. When you call a
64
- # reader method to get the nested structure, you get a *copy* of that data.
65
- #
66
61
  # For example:
67
62
  #
68
63
  # class Sub < BitStruct
@@ -77,6 +72,19 @@ class BitStruct
77
72
  #
78
73
  # p a # ==> #<A n=#<Sub x=0>>
79
74
  #
75
+ # If a block is given, use it to define the nested fields. For example, the
76
+ # following is equivalent to the above example:
77
+ #
78
+ # class A < BitStruct
79
+ # nest :n do
80
+ # unsigned :x, 8
81
+ # end
82
+ # end
83
+ #
84
+ # WARNING: the accessors have COPY semantics, not reference. When you call a
85
+ # reader method to get the nested structure, you get a *copy* of that data.
86
+ # Expressed in terms of the examples above:
87
+ #
80
88
  # # This fails to set x in a.
81
89
  # a.n.x = 3
82
90
  # p a # ==> #<A n=#<Sub x=0>>
@@ -87,8 +95,21 @@ class BitStruct
87
95
  # a.n = n
88
96
  # p a # ==> #<A n=#<Sub x=3>>
89
97
  #
90
- def nest(name, nested_class, *rest)
98
+ def nest(name, *rest, &block)
91
99
  opts = parse_options(rest, name, NestedField)
100
+ nested_class = opts[:nested_class]
101
+
102
+ unless (block and not nested_class) or (nested_class and not block)
103
+ raise ArgumentError,
104
+ "nested field must have either a nested_class option or a block," +
105
+ " but not both"
106
+ end
107
+
108
+ unless nested_class
109
+ nested_class = Class.new(BitStruct)
110
+ nested_class.class_eval(&block)
111
+ end
112
+
92
113
  opts[:default] ||= nested_class.initial_value.dup
93
114
  opts[:nested_class] = nested_class
94
115
  field = add_field(name, nested_class.bit_length, opts)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bit-struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.1
4
+ version: 0.13.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel VanderWerf
@@ -53,6 +53,7 @@ files:
53
53
  - examples/md.rb
54
54
  - examples/modular-def.rb
55
55
  - examples/native.rb
56
+ - examples/nested-block.rb
56
57
  - examples/nested.rb
57
58
  - examples/pad.rb
58
59
  - examples/ping-recv.rb