Skip to content

Commit

Permalink
Merge pull request #120 from tenderlove/always-return-ascii-8bit
Browse files Browse the repository at this point in the history
`to_binary_s` should always return ASCII-8BIT encoded strings
  • Loading branch information
Dion Mendel authored Feb 21, 2020
2 parents 2f22031 + 26d35a1 commit 8acb1b2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/bindata/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ def num_bytes
def to_binary_s(&block)
io = BinData::IO.create_string_io
write(io, &block)
io.rewind
io.read
io.string
end

# Returns the hexadecimal string representation of this data object.
Expand Down
19 changes: 19 additions & 0 deletions test/buffer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ class IntBuffer < BinData::Buffer
obj.to_binary_s.must_equal_binary "\000\003\000\000\000"
end

it "returns binary encoded data" do
obj = IntBuffer.new(3)
obj.to_binary_s.encoding.must_equal Encoding::ASCII_8BIT
end

it "returns binary encoded data despite Encoding.default_internal" do
w, $-w = $-w, false
before_enc = Encoding.default_internal

begin
Encoding.default_internal = Encoding::UTF_8
obj = IntBuffer.new(3)
obj.to_binary_s.encoding.must_equal Encoding::ASCII_8BIT
ensure
Encoding.default_internal = before_enc
$-w = w
end
end

it "has total num_bytes" do
obj = IntBuffer.new
assert obj.clear?
Expand Down

1 comment on commit 8acb1b2

@Chilly76
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.