packed_struct 0.3.0 → 0.4.1
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/lib/packed_struct/directive.rb +19 -9
- data/lib/packed_struct/modifier.rb +24 -6
- data/lib/packed_struct/package.rb +8 -0
- data/lib/packed_struct/version.rb +1 -1
- metadata +2 -2
@@ -145,14 +145,16 @@ module PackedStruct
|
|
145
145
|
return if finalized?
|
146
146
|
|
147
147
|
modifiers.each do |modifier|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
148
|
+
modifier.type.each_with_index do |type, i|
|
149
|
+
case type
|
150
|
+
when :endian, :signedness, :precision, :type, :string_type
|
151
|
+
tags[type] = modifier.value[i]
|
152
|
+
when :size
|
153
|
+
tags[:size] = modifier.value[i] unless tags[:size]
|
154
|
+
else
|
155
|
+
raise UnknownModifierError,
|
156
|
+
"Unknown modifier: #{type}"
|
157
|
+
end
|
156
158
|
end
|
157
159
|
end
|
158
160
|
|
@@ -160,6 +162,14 @@ module PackedStruct
|
|
160
162
|
cache_string
|
161
163
|
end
|
162
164
|
|
165
|
+
# Returns whether or not this directive depends on another
|
166
|
+
# directive for its value.
|
167
|
+
#
|
168
|
+
# @return [Boolean]
|
169
|
+
def undefined_size?
|
170
|
+
tags[:size].is_a?(Symbol)
|
171
|
+
end
|
172
|
+
|
163
173
|
# Turn the directive into a string, with the given data. It
|
164
174
|
# shouldn't need the data unless +tags[:size]+ is a Symbol.
|
165
175
|
#
|
@@ -167,7 +177,7 @@ module PackedStruct
|
|
167
177
|
# the length.
|
168
178
|
# @return [String]
|
169
179
|
def to_s(data = {})
|
170
|
-
return @_cache
|
180
|
+
return @_cache unless undefined_size? || !@_cache
|
171
181
|
return "x" * (tags[:size] || 1) if name == :null
|
172
182
|
|
173
183
|
out = case tags[:type]
|
@@ -8,27 +8,28 @@ module PackedStruct
|
|
8
8
|
@value = nil
|
9
9
|
end
|
10
10
|
|
11
|
-
# The type of modifier it is. Has
|
12
|
-
# +:endian+, +:size+,
|
11
|
+
# The type of modifier it is. Has four possible values:
|
12
|
+
# +:endian+, +:size+, +:length+, +:type+, +:string_type+,
|
13
|
+
# and +:signedness+.
|
13
14
|
#
|
14
|
-
# @return [Symbol]
|
15
|
+
# @return [Array<Symbol>]
|
15
16
|
# @!parse
|
16
17
|
# attr_reader :type
|
17
18
|
def type
|
18
19
|
compile! unless @compiled
|
19
|
-
@type
|
20
|
+
[@type].flatten
|
20
21
|
end
|
21
22
|
|
22
23
|
# The value of the modifier. Has multiple possible values,
|
23
24
|
# including: +:little+, +:big+, +:short+, +:int+, +:long+,
|
24
25
|
# +:float+, +:null+, +:string+, +:unsigned+, +:signed+.
|
25
26
|
#
|
26
|
-
# @return [Symbol]
|
27
|
+
# @return [Array<Symbol>]
|
27
28
|
# @!parse
|
28
29
|
# attr_reader :value
|
29
30
|
def value
|
30
31
|
compile! unless @compiled
|
31
|
-
@value
|
32
|
+
[@value].flatten
|
32
33
|
end
|
33
34
|
|
34
35
|
# Compiles the modifier into a usable format. Stores the values
|
@@ -64,6 +65,23 @@ module PackedStruct
|
|
64
65
|
when :hex, :base64, :bit
|
65
66
|
@type = :string_type
|
66
67
|
@value = @name
|
68
|
+
when :binary
|
69
|
+
@type = :string_type
|
70
|
+
@value = :bit
|
71
|
+
when :uint8, :uint16, :uint32, :sint8, :sint16, :sint32,
|
72
|
+
:int8, :int16, :int32 #/([us]?)int(8|16|32)/
|
73
|
+
|
74
|
+
@name.to_s =~ /([us]?)int(8|16|32)/
|
75
|
+
@type = [:signedness, :size]
|
76
|
+
@value = []
|
77
|
+
|
78
|
+
if $1 == "u"
|
79
|
+
@value.push(:unsigned)
|
80
|
+
else
|
81
|
+
@value.push(:signed)
|
82
|
+
end
|
83
|
+
|
84
|
+
@value.push($2.to_i)
|
67
85
|
else
|
68
86
|
raise UnknownModifierError, "Unkown modifier: #{@name}"
|
69
87
|
end
|
@@ -61,6 +61,7 @@ module PackedStruct
|
|
61
61
|
def unpack(string)
|
62
62
|
total = ""
|
63
63
|
parts = {}
|
64
|
+
|
64
65
|
directives.each_with_index do |directive, i|
|
65
66
|
total << directive.to_s(parts)
|
66
67
|
parts[directive.name] = string.unpack(total)[i]
|
@@ -116,6 +117,13 @@ module PackedStruct
|
|
116
117
|
directives.map(&:finalize!)
|
117
118
|
end
|
118
119
|
|
120
|
+
# Checks to see if it has a field with the given name.
|
121
|
+
#
|
122
|
+
# @return [Boolean]
|
123
|
+
def has_field?(field)
|
124
|
+
!!directives.select { |d| d.name == field }.first
|
125
|
+
end
|
126
|
+
|
119
127
|
# Inspects the package.
|
120
128
|
#
|
121
129
|
# @return [String]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: packed_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-07-04 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! ' Cleans up the string mess when packing items (in Array#pack) and
|
15
15
|
unpacking items (in String#unpack).
|