bindata 1.4.4 → 1.4.5
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of bindata might be problematic. Click here for more details.
- data/ChangeLog +5 -0
- data/lib/bindata.rb +1 -1
- data/lib/bindata/string.rb +19 -3
- data/manual.md +11 -1
- data/spec/string_spec.rb +42 -0
- metadata +4 -4
data/ChangeLog
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
= BinData Changelog
|
2
2
|
|
3
|
+
== Version 1.4.5 (2012-07-24)
|
4
|
+
|
5
|
+
* Added the :pad_front option for padding to occur at the front of a String.
|
6
|
+
(suggested by Eduardo Mourão).
|
7
|
+
|
3
8
|
== Version 1.4.4 (2012-06-21)
|
4
9
|
|
5
10
|
* Fixed bug where user defined boolean primitive wouldn't set its
|
data/lib/bindata.rb
CHANGED
data/lib/bindata/string.rb
CHANGED
@@ -41,13 +41,16 @@ module BinData
|
|
41
41
|
# <tt>:pad_byte</tt>:: The byte to use when padding a string to a
|
42
42
|
# set length. Valid values are Integers and
|
43
43
|
# Strings of length 1. "\0" is the default.
|
44
|
+
# <tt>:pad_front</tt>:: Signifies that the padding occurs at the front
|
45
|
+
# of the string rather than the end. Default
|
46
|
+
# is false.
|
44
47
|
# <tt>:trim_padding</tt>:: Boolean, default false. If set, #value will
|
45
48
|
# return the value with all pad_bytes trimmed
|
46
49
|
# from the end of the string. The value will
|
47
50
|
# not be trimmed when writing.
|
48
51
|
class String < BinData::BasePrimitive
|
49
52
|
|
50
|
-
optional_parameters :read_length, :length, :trim_padding
|
53
|
+
optional_parameters :read_length, :length, :trim_padding, :pad_front, :pad_left
|
51
54
|
default_parameters :pad_byte => "\0"
|
52
55
|
mutually_exclusive_parameters :read_length, :length
|
53
56
|
mutually_exclusive_parameters :length, :value
|
@@ -59,6 +62,10 @@ module BinData
|
|
59
62
|
|
60
63
|
params.warn_renamed_parameter(:pad_char, :pad_byte) # Remove this line in the future
|
61
64
|
|
65
|
+
if params.has_parameter?(:pad_left)
|
66
|
+
params[:pad_front] = params.delete(:pad_left)
|
67
|
+
end
|
68
|
+
|
62
69
|
if params.has_parameter?(:pad_byte)
|
63
70
|
byte = params[:pad_byte]
|
64
71
|
params[:pad_byte] = sanitized_pad_byte(byte)
|
@@ -120,12 +127,21 @@ module BinData
|
|
120
127
|
elsif str.length > len
|
121
128
|
str.slice(0, len)
|
122
129
|
else
|
123
|
-
|
130
|
+
padding = (eval_parameter(:pad_byte) * (len - str.length))
|
131
|
+
if get_parameter(:pad_front)
|
132
|
+
padding + str
|
133
|
+
else
|
134
|
+
str + padding
|
135
|
+
end
|
124
136
|
end
|
125
137
|
end
|
126
138
|
|
127
139
|
def trim_padding(str)
|
128
|
-
|
140
|
+
if get_parameter(:pad_front)
|
141
|
+
str.sub(/\A#{eval_parameter(:pad_byte)}*/, "")
|
142
|
+
else
|
143
|
+
str.sub(/#{eval_parameter(:pad_byte)}*\z/, "")
|
144
|
+
end
|
129
145
|
end
|
130
146
|
|
131
147
|
def value_to_binary_string(val)
|
data/manual.md
CHANGED
@@ -40,7 +40,7 @@ manipulating.
|
|
40
40
|
It supports all the common datatypes that are found in structured binary
|
41
41
|
data. Support for dependent and variable length fields is built in.
|
42
42
|
|
43
|
-
Last updated: 2012-
|
43
|
+
Last updated: 2012-07-24
|
44
44
|
|
45
45
|
## License
|
46
46
|
|
@@ -578,6 +578,16 @@ There are several parameters that are specific to fixed sized strings.
|
|
578
578
|
obj #=> "abcdef"
|
579
579
|
{:ruby}
|
580
580
|
|
581
|
+
`:pad_front` or `:pad_left`
|
582
|
+
|
583
|
+
: Boolean, default `false`. Signifies that the padding occurs at the front
|
584
|
+
of the string rather than the end.
|
585
|
+
|
586
|
+
obj = BinData::String.new(:length => 6, :pad_front => true)
|
587
|
+
obj.assign("abcd")
|
588
|
+
obj.snapshot #=> "\000\000abcd"
|
589
|
+
{:ruby}
|
590
|
+
|
581
591
|
`:pad_byte`
|
582
592
|
|
583
593
|
: Defaults to `"\0"`. The character to use when padding a string to a
|
data/spec/string_spec.rb
CHANGED
@@ -218,6 +218,48 @@ describe BinData::String, "with :trim_padding" do
|
|
218
218
|
end
|
219
219
|
end
|
220
220
|
|
221
|
+
describe BinData::String, "with :pad_front" do
|
222
|
+
it "set false is the default" do
|
223
|
+
str1 = BinData::String.new(:length => 5)
|
224
|
+
str2 = BinData::String.new(:length => 5, :pad_front => false)
|
225
|
+
str1.assign("abc")
|
226
|
+
str2.assign("abc")
|
227
|
+
str1.should == "abc\0\0"
|
228
|
+
str2.should == "abc\0\0"
|
229
|
+
end
|
230
|
+
|
231
|
+
it "pads to the front" do
|
232
|
+
str = BinData::String.new(:length => 5, :pad_byte => 'R', :pad_front => true)
|
233
|
+
str.assign("abc")
|
234
|
+
str.should == "RRabc"
|
235
|
+
end
|
236
|
+
|
237
|
+
it "can alternatively be accesses by :pad_left" do
|
238
|
+
str = BinData::String.new(:length => 5, :pad_byte => 'R', :pad_left => true)
|
239
|
+
str.assign("abc")
|
240
|
+
str.should == "RRabc"
|
241
|
+
end
|
242
|
+
|
243
|
+
context "and :trim_padding" do
|
244
|
+
subject { BinData::String.new(:length => 5, :pad_byte => 'R', :pad_front => true, :trim_padding => true) }
|
245
|
+
|
246
|
+
it "assigns" do
|
247
|
+
subject.assign("abc")
|
248
|
+
subject.should == "abc"
|
249
|
+
end
|
250
|
+
|
251
|
+
it "has to_binary_s" do
|
252
|
+
subject.assign("abc")
|
253
|
+
subject.to_binary_s.should == "RRabc"
|
254
|
+
end
|
255
|
+
|
256
|
+
it "reads" do
|
257
|
+
subject.read "RRabc"
|
258
|
+
subject.should == "abc"
|
259
|
+
end
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
221
263
|
describe BinData::String, "with Ruby 1.9 encodings" do
|
222
264
|
if RUBY_VERSION >= "1.9"
|
223
265
|
class UTF8String < BinData::String
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bindata
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 13
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 4
|
9
|
-
-
|
10
|
-
version: 1.4.
|
9
|
+
- 5
|
10
|
+
version: 1.4.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dion Mendel
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-07-24 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rspec
|