bin_struct 0.5.2 → 0.5.3

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.
@@ -36,7 +36,7 @@ module BinStruct
36
36
  # @return [Hash{::String => Integer}]
37
37
  attr_reader :enum
38
38
 
39
- # @param [Hash] options
39
+ # @param options [Hash]
40
40
  # @see Int#initialize
41
41
  # @option options [Hash{::String => Integer}] :enum enumerated values. Default value is taken from
42
42
  # first element unless given. This option is mandatory.
@@ -52,7 +52,7 @@ module BinStruct
52
52
  end
53
53
 
54
54
  # Setter for value attribute
55
- # @param [#to_i, String,nil] value value as an Integer or as a String
55
+ # @param value [#to_i, String,nil] value as an Integer or as a String
56
56
  # from enumration
57
57
  # @return [Integer]
58
58
  # @raise [ArgumentError] String value is unknown
@@ -89,7 +89,7 @@ module BinStruct
89
89
  # Enumeration on one byte. See {Enum}.
90
90
  # @author LemonTree55
91
91
  class Int8Enum < Enum
92
- # @param [Hash] options
92
+ # @param options [Hash]
93
93
  # @option options [Hash{::String => Integer}] :enum enumerated values. Default value is taken from
94
94
  # first element unless given. This option is mandatory.
95
95
  # @option options [Integer,::String] :value
@@ -106,7 +106,7 @@ module BinStruct
106
106
  # Enumeration on 2-byte integer. See {Enum}.
107
107
  # @author LemonTree55
108
108
  class Int16Enum < Enum
109
- # @param [Hash] options
109
+ # @param options [Hash]
110
110
  # @option options [Hash{::String => Integer}] :enum enumerated values. Default value is taken from
111
111
  # first element unless given. This option is mandatory.
112
112
  # @option options [:big,:little] :endian
@@ -127,7 +127,7 @@ module BinStruct
127
127
  class Int16beEnum < Int16Enum
128
128
  undef endian=
129
129
 
130
- # @param [Hash] options
130
+ # @param options [Hash]
131
131
  # @option options [Hash{::String => Integer}] :enum enumerated values. Default value is taken from
132
132
  # first element unless given. This option is mandatory.
133
133
  # @option options [Integer,::String] :value
@@ -145,7 +145,7 @@ module BinStruct
145
145
  class Int16leEnum < Int16Enum
146
146
  undef endian=
147
147
 
148
- # @param [Hash] options
148
+ # @param options [Hash]
149
149
  # @option options [Hash{::String => Integer}] :enum enumerated values. Default value is taken from
150
150
  # first element unless given. This option is mandatory.
151
151
  # @option options [Integer,::String] :value
@@ -160,7 +160,7 @@ module BinStruct
160
160
  # Enumeration on 4-byte integer. See {Enum}.
161
161
  # @author LemonTree55
162
162
  class Int32Enum < Enum
163
- # @param [Hash] options
163
+ # @param options [Hash]
164
164
  # @option options [Hash{::String => Integer}] :enum enumerated values. Default value is taken from
165
165
  # first element unless given. This option is mandatory.
166
166
  # @option options [:big,:little] :endian
@@ -181,7 +181,7 @@ module BinStruct
181
181
  class Int32beEnum < Int32Enum
182
182
  undef endian=
183
183
 
184
- # @param [Hash] options
184
+ # @param options [Hash]
185
185
  # @option options [Hash{::String => Integer}] :enum enumerated values. Default value is taken from
186
186
  # first element unless given. This option is mandatory.
187
187
  # @option options [Integer,::String] :value
@@ -199,7 +199,7 @@ module BinStruct
199
199
  class Int32leEnum < Int32Enum
200
200
  undef endian=
201
201
 
202
- # @param [Hash] options
202
+ # @param options [Hash]
203
203
  # @option options [Hash{::String => Integer}] :enum enumerated values. Default value is taken from
204
204
  # first element unless given. This option is mandatory.
205
205
  # @option options [Integer,::String] :value
@@ -18,7 +18,7 @@ module BinStruct
18
18
  # @return [Integer,nil]
19
19
  attr_accessor :value
20
20
  # Integer endianness
21
- # @return [:little,:big,:native,nil]
21
+ # @return [Symbol,nil] +:big:, +:little+, +:native+ or +nil+
22
22
  attr_accessor :endian
23
23
  # Integer size, in bytes
24
24
  # @return [Integer]
@@ -27,9 +27,9 @@ module BinStruct
27
27
  # @return [Integer]
28
28
  attr_accessor :default
29
29
 
30
- # @param [Hash] options
30
+ # @param options [Hash]
31
31
  # @option options [Integer, nil] :value Value to set Int to
32
- # @option options [:little,:big,:native,nil] :endian Int's endianess
32
+ # @option options [Symbol,nil] :endian Int's endianess
33
33
  # @option options [Integer,nil] :width Int's width in bytes
34
34
  # @option options [Integer] :default Default value to use when {#value} is not set (Default to +0+).
35
35
  # @author LemonTree55
@@ -42,7 +42,7 @@ module BinStruct
42
42
 
43
43
  # @abstract
44
44
  # Read an Int from a binary string or an integer
45
- # @param [#to_s] str
45
+ # @param str [#to_s]
46
46
  # @return [self]
47
47
  # @raise [Error] when reading +#to_s+ objects with abstract Int class.
48
48
  # @author LemonTree55
@@ -70,7 +70,7 @@ module BinStruct
70
70
  alias to_human to_i
71
71
 
72
72
  # Initialize value from an Integer.
73
- # @param [Integer] value
73
+ # @param value [Integer]
74
74
  # @return [self]
75
75
  def from_human(value)
76
76
  @value = value
@@ -103,6 +103,8 @@ module BinStruct
103
103
 
104
104
  private
105
105
 
106
+ # Format string, tailord to integer {#width}
107
+ # @return [String]
106
108
  def format_str
107
109
  "%-16s (0x%0#{width * 2}x)"
108
110
  end
@@ -111,7 +113,7 @@ module BinStruct
111
113
  # One byte unsigned integer
112
114
  # @author LemonTree55
113
115
  class Int8 < Int
114
- # @param [Hash] options
116
+ # @param options [Hash]
115
117
  # @option options [Integer,nil] :value
116
118
  def initialize(options = {})
117
119
  options[:endian] = nil
@@ -124,7 +126,7 @@ module BinStruct
124
126
  # One byte signed integer
125
127
  # @author LemonTree55
126
128
  class SInt8 < Int
127
- # @param [Hash] options
129
+ # @param options [Hash]
128
130
  # @option options [Integer,nil] :value
129
131
  def initialize(options = {})
130
132
  options[:endian] = nil
@@ -138,7 +140,7 @@ module BinStruct
138
140
  # @author Sylvain Daubert (2016-2024)
139
141
  # @author LemonTree55
140
142
  class Int16 < Int
141
- # @param [Hash] options
143
+ # @param options [Hash]
142
144
  # @option options [Integer,nil] :value
143
145
  # @option options [:big,:little,:native] :endian
144
146
  def initialize(options = {})
@@ -160,7 +162,7 @@ module BinStruct
160
162
  class Int16le < Int16
161
163
  undef endian=
162
164
 
163
- # @param [Hash] options
165
+ # @param options [Hash]
164
166
  # @option options [Integer,nil] :value
165
167
  def initialize(options = {})
166
168
  opts = { value: options[:value], endian: :little }
@@ -173,7 +175,7 @@ module BinStruct
173
175
  class Int16n < Int16
174
176
  undef endian=
175
177
 
176
- # @param [Hash] options
178
+ # @param options [Hash]
177
179
  # @option options [Integer,nil] :value
178
180
  def initialize(options = {})
179
181
  opts = { value: options[:value], endian: :native }
@@ -185,7 +187,7 @@ module BinStruct
185
187
  # @author Sylvain Daubert (2016-2024)
186
188
  # @author LemonTree55
187
189
  class SInt16 < Int16
188
- # @param [Hash] options
190
+ # @param options [Hash]
189
191
  # @option options [Integer,nil] :value
190
192
  # @option options [:big,:little,:native] :endian
191
193
  def initialize(options = {})
@@ -207,7 +209,7 @@ module BinStruct
207
209
  class SInt16le < SInt16
208
210
  undef endian=
209
211
 
210
- # @param [Hash] options
212
+ # @param options [Hash]
211
213
  # @option options [Integer,nil] :value
212
214
  # @author LemonTree55
213
215
  def initialize(options = {})
@@ -222,7 +224,7 @@ module BinStruct
222
224
  class SInt16n < SInt16
223
225
  undef endian=
224
226
 
225
- # @param [Hash] options
227
+ # @param options [Hash]
226
228
  # @option options [Integer,nil] :value
227
229
  def initialize(options = {})
228
230
  opts = { value: options[:value], endian: :native }
@@ -233,7 +235,7 @@ module BinStruct
233
235
  # 3-byte unsigned integer
234
236
  # @author LemonTree55
235
237
  class Int24 < Int
236
- # @param [Hash] options
238
+ # @param options [Hash]
237
239
  # @option options [Integer,nil] :value
238
240
  # @option options [:big, :little, :native] :endian
239
241
  def initialize(options = {})
@@ -252,7 +254,7 @@ module BinStruct
252
254
  end
253
255
 
254
256
  # Read a 3-byte Int from a binary string
255
- # @param [::String] value
257
+ # @param value [::String]
256
258
  # @return [self]
257
259
  def read(value)
258
260
  return self if value.nil?
@@ -291,7 +293,7 @@ module BinStruct
291
293
  class Int24le < Int24
292
294
  undef endian=
293
295
 
294
- # @param [Hash] options
296
+ # @param options [Hash]
295
297
  # @option options [Integer] :value
296
298
  def initialize(options = {})
297
299
  opts = { value: options[:value], endian: :little }
@@ -304,7 +306,7 @@ module BinStruct
304
306
  class Int24n < Int24
305
307
  undef endian=
306
308
 
307
- # @param [Hash] options
309
+ # @param options [Hash]
308
310
  # @option options [Integer] :value
309
311
  def initialize(options = {})
310
312
  opts = { value: options[:value], endian: :little }
@@ -315,7 +317,7 @@ module BinStruct
315
317
  # 4-byte unsigned integer
316
318
  # @author LemonTree55
317
319
  class Int32 < Int
318
- # @param [Hash] options
320
+ # @param options [Hash]
319
321
  # @option options [Integer,nil] :value
320
322
  # @option options [:big,:little,:native] :endian
321
323
  def initialize(options = {})
@@ -336,7 +338,7 @@ module BinStruct
336
338
  class Int32le < Int32
337
339
  undef endian=
338
340
 
339
- # @param [Hash] options
341
+ # @param options [Hash]
340
342
  # @option options [Integer,nil] :value
341
343
  def initialize(options = {})
342
344
  opts = { value: options[:value], endian: :little }
@@ -349,7 +351,7 @@ module BinStruct
349
351
  class Int32n < Int32
350
352
  undef endian=
351
353
 
352
- # @param [Hash] options
354
+ # @param options [Hash]
353
355
  # @option options [Integer,nil] :value
354
356
  def initialize(options = {})
355
357
  opts = { value: options[:value], endian: :native }
@@ -360,7 +362,7 @@ module BinStruct
360
362
  # 4-byte signed integer
361
363
  # @author LemonTree55
362
364
  class SInt32 < Int32
363
- # @param [Hash] options
365
+ # @param options [Hash]
364
366
  # @option options [Integer] :value
365
367
  # @option options [:big,:little,:native] :endian
366
368
  def initialize(options = {})
@@ -381,7 +383,7 @@ module BinStruct
381
383
  class SInt32le < SInt32
382
384
  undef endian=
383
385
 
384
- # @param [Hash] options
386
+ # @param options [Hash]
385
387
  # @option options [Integer] :value
386
388
  def initialize(options = {})
387
389
  opts = { value: options[:value], endian: :little }
@@ -394,7 +396,7 @@ module BinStruct
394
396
  class SInt32n < SInt32
395
397
  undef endian=
396
398
 
397
- # @param [Hash] options
399
+ # @param options [Hash]
398
400
  # @option options [Integer] :value
399
401
  def initialize(options = {})
400
402
  opts = { value: options[:value], endian: :native }
@@ -405,7 +407,7 @@ module BinStruct
405
407
  # 8-byte unsigned integer
406
408
  # @author LemonTree55
407
409
  class Int64 < Int
408
- # @param [Hash] options
410
+ # @param options [Hash]
409
411
  # @option options [Integer] :value
410
412
  # @option options [:big,:little,:native] :endian
411
413
  def initialize(options = {})
@@ -428,7 +430,7 @@ module BinStruct
428
430
  class Int64le < Int64
429
431
  undef endian=
430
432
 
431
- # @param [Hash] options
433
+ # @param options [Hash]
432
434
  # @option options [Integer] :value
433
435
  def initialize(options = {})
434
436
  opts = { value: options[:value], endian: :little }
@@ -441,7 +443,7 @@ module BinStruct
441
443
  class Int64n < Int64
442
444
  undef endian=
443
445
 
444
- # @param [Hash] options
446
+ # @param options [Hash]
445
447
  # @option options [Integer] :value
446
448
  def initialize(options = {})
447
449
  opts = { value: options[:value], endian: :native }
@@ -452,7 +454,7 @@ module BinStruct
452
454
  # 8-byte signed integer
453
455
  # @author LemonTree55
454
456
  class SInt64 < Int64
455
- # @param [Hash] options
457
+ # @param options [Hash]
456
458
  # @option options [Integer] :value
457
459
  # @option options [:big,:little,:native] :endian
458
460
  def initialize(options = {})
@@ -473,7 +475,7 @@ module BinStruct
473
475
  class SInt64le < SInt64
474
476
  undef endian=
475
477
 
476
- # @param [Hash] options
478
+ # @param options [Hash]
477
479
  # @option options [Integer] :value
478
480
  def initialize(options = {})
479
481
  opts = { value: options[:value], endian: :little }
@@ -486,7 +488,7 @@ module BinStruct
486
488
  class SInt64n < SInt64
487
489
  undef endian=
488
490
 
489
- # @param [Hash] options
491
+ # @param options [Hash]
490
492
  # @option options [Integer] :value
491
493
  def initialize(options = {})
492
494
  opts = { value: options[:value], endian: :native }
@@ -25,7 +25,7 @@ module BinStruct
25
25
  # @return [::String]
26
26
  attr_reader :string
27
27
 
28
- # @param [Hash] options
28
+ # @param options [Hash]
29
29
  # @option options [Class] :length_type should be a {Int} subclass. Default to {Int8}.
30
30
  # @option options [::String] :value String value. Default to +""+
31
31
  def initialize(options = {})
@@ -35,26 +35,27 @@ module BinStruct
35
35
  end
36
36
 
37
37
  # Populate IntString from a binary String
38
- # @param [::String] str
38
+ # @param str [::String]
39
39
  # @return [self]
40
40
  def read(str)
41
+ return self if str.nil?
42
+
43
+ str = str.b
41
44
  unless str[0, @length.width].size == @length.width
42
45
  raise Error,
43
- "String too short for type #{@length.class.to_s.gsub(/.*::/, '')}"
46
+ "String too short for #{@length.width}-byte length"
44
47
  end
45
- @length.read str[0, @length.width]
46
- @string.read str[@length.width, @length.to_i]
48
+ @length.read(str[0, @length.width])
49
+ @string.read(str[@length.width, @length.to_i])
47
50
  self
48
51
  end
49
52
 
50
53
  # Set length
51
- # @param [Integer] len
54
+ # @param len [Integer]
52
55
  # @return [Integer]
53
56
  def length=(len)
54
57
  @length.from_human(len)
55
- # rubocop:disable Lint/Void
56
- len
57
- # rubocop:enable Lint/Void
58
+ len # rubocop:disable Lint/Void
58
59
  end
59
60
 
60
61
  # Get length as registered in +IntLength+
@@ -64,10 +65,10 @@ module BinStruct
64
65
  end
65
66
 
66
67
  # Set string without setting {#length}
67
- # @param [#to_s] str
68
+ # @param str [#to_s]
68
69
  # @return [::String]
69
70
  def string=(str)
70
- @length.value = str.to_s.size
71
+ @length.value = str.to_s.bytesize
71
72
  @string.read(str)
72
73
  end
73
74
 
@@ -78,7 +79,7 @@ module BinStruct
78
79
  end
79
80
 
80
81
  # Set from a human readable string
81
- # @param [::String] str
82
+ # @param str [::String]
82
83
  # @return [self]
83
84
  def from_human(str)
84
85
  @string.read(str)
@@ -18,7 +18,7 @@ module BinStruct
18
18
 
19
19
  # Initialize +length_from+ capacity.
20
20
  # Should be called by extended object's initialize method.
21
- # @param [Hash] options
21
+ # @param options [Hash]
22
22
  # @option options [Int,Proc] :length_from object or proc from which
23
23
  # takes length when reading
24
24
  # @return [void]
@@ -27,7 +27,7 @@ module BinStruct
27
27
  end
28
28
 
29
29
  # Return a substring from +str+ of length given in another object.
30
- # @param [#to_s] str
30
+ # @param str [#to_s]
31
31
  # @return [::String]
32
32
  def read_with_length_from(str)
33
33
  s = str.to_s.b
@@ -29,7 +29,7 @@ module BinStruct
29
29
  define_attr :b0, Int8
30
30
 
31
31
  # Read a human-readable string to populate object
32
- # @param [::String] str
32
+ # @param str [::String]
33
33
  # @return [self]
34
34
  # @raise [ArgumentError] OUI cannot be recognized from +str+
35
35
  def from_human(str)
@@ -55,7 +55,7 @@ module BinStruct
55
55
  # @return [Integer]
56
56
  attr_reader :static_length
57
57
 
58
- # @param [Hash] options
58
+ # @param options [Hash]
59
59
  # @option options [Int,Proc] :length_from object or proc from which
60
60
  # takes length when reading
61
61
  # @option options [Integer] :static_length set a static length for this string
@@ -68,13 +68,12 @@ module BinStruct
68
68
 
69
69
  # Initialize object on copying:
70
70
  # * duplicate underlying Ruby String
71
- # @return [void]
72
- def initialize_copy(_orig)
71
+ def initialize_copy(*)
73
72
  @string = @string.dup
74
73
  end
75
74
 
76
75
  # Populate String from a binary String. Limit length using {LengthFrom} or {#static_length}, if one is set.
77
- # @param [::String,nil] str
76
+ # @param str [::String,nil]
78
77
  # @return [self]
79
78
  def read(str)
80
79
  s = read_with_length_from(str)
@@ -107,7 +106,7 @@ module BinStruct
107
106
  end
108
107
 
109
108
  # Append the given string to String
110
- # @param [#to_s] str
109
+ # @param str [#to_s]
111
110
  # @return [self]
112
111
  def <<(str)
113
112
  @string << str.to_s.b
@@ -131,6 +130,8 @@ module BinStruct
131
130
 
132
131
  private
133
132
 
133
+ # @param str [String]
134
+ # @return [void]
134
135
  def register_internal_string(str)
135
136
  @string = str.b
136
137
  end