Skip to content

Commit

Permalink
Fixed Array#pack's handling of the 'h'/'H' formats.
Browse files Browse the repository at this point in the history
  • Loading branch information
tilman2 committed Jan 28, 2009
1 parent 6819570 commit 83ab3fd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
34 changes: 28 additions & 6 deletions kernel/common/pack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,35 @@ def hex_string(kind, t)
item = "" if item.nil?

size = parse_tail(t, kind, item.length)
str = item.scan(/..?/).first(size)
str = item[0, size].scan(/..?/)

@result << if kind == "h" then
str.map { |b| b.reverse.hex.chr }.join
else
str.map { |b| b. hex.chr }.join
end
numbers = if kind == "h" then
str.map { |b| b.reverse.hex }
else
str.map { |b| b. hex }
end

if kind == 'H' && !numbers.empty? && numbers.last < 16
numbers[-1] *= 16
end

diff = size - item.length

if diff > 0
# we couldn't read as much data as was requested,
# so we'll make up for that with a couple of zeroes.
if (item.length % 2) == 0
left = (diff / 2.0 + 0.5).to_i
else
left = diff / 2
end

left.times do
numbers << 0
end
end

@result << numbers.map { |n| n.chr }.join
end

def decimal(kind, t)
Expand Down
13 changes: 0 additions & 13 deletions spec/tags/frozen/core/array/pack_tags.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
fails:Array#pack with format 'H' encodes hexadecimal digits to byte sequence in the order of high-nibble first
fails:Array#pack with format 'H' ignores rest of the pack argument when the argument is too long
fails:Array#pack with format 'H' fills low-nibble of the last byte with 0 when count is odd
fails:Array#pack with format 'H' fills the rest bytes with 0 if pack argument has insufficient length
fails:Array#pack with format 'H' fills low-nibble of the last byte with 0 when count is odd even if pack argument has insufficient length
fails:Array#pack with format 'H' considers count = 1 if count omited
fails:Array#pack with format 'H' consumes only one array item per a format
fails:Array#pack with format 'h' ignores rest of the pack argument when the argument is too long
fails:Array#pack with format 'h' fills low-nibble of the last byte with 0 when count is odd
fails:Array#pack with format 'h' fills the rest bytes with 0 if pack argument has insufficient length
fails:Array#pack with format 'h' fills high-nibble of the last byte with 0 when count is odd even if pack argument has insufficient length
fails:Array#pack with format 'h' considers count = 1 if count omited
fails:Array#pack with format 'h' consumes only one array item per a format
fails:Array#pack with format 'n' regards negative values as 2's complement in order to converts it to positive
fails:Array#pack with format 'N' regards negative values as 2's complement in order to converts it to positive
fails:Array#pack with format 'q' returns a string containing 8 bytes for an integer
Expand Down

0 comments on commit 83ab3fd

Please sign in to comment.