Skip to content

Commit

Permalink
use .bytes instead of .chars to avoid utf encoding issues
Browse files Browse the repository at this point in the history
  • Loading branch information
pkmiec committed Sep 5, 2011
1 parent af64d6b commit 32295b0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/amq/hacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ def self.unpack_64_big_endian(data)
else
def self.pack_64_big_endian(long_long)
result = [long_long].pack(Q)
result.chars.to_a.reverse.join
result.bytes.to_a.reverse.map(&:chr).join
end

def self.unpack_64_big_endian(data)
data = data.chars.to_a.reverse.join
data = data.bytes.to_a.reverse.map(&:chr).join
data.unpack(Q)
end
end
Expand Down
39 changes: 30 additions & 9 deletions spec/amq/hacks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,42 @@ module AMQ
}
}

describe ".pack_64_big_endian" do
it "packs integers into big-endian string" do
examples.each do |key, value|
described_class.pack_64_big_endian(key).should == value
end
it "packs integers into big-endian string" do
examples.each do |key, value|
described_class.pack_64_big_endian(key).should == value
end
end

it "should unpack string representation into integer" do
examples.each do |key, value|
described_class.unpack_64_big_endian(value)[0].should == key
end
end

if RUBY_VERSION < '1.9'
describe "with utf encoding" do
before do
$KCODE = 'u'
end

after do
$KCODE = 'NONE'
end

it "packs integers into big-endian string" do
examples.each do |key, value|
described_class.pack_64_big_endian(key).should == value
end
end

describe ".unpack_64_big_endian" do
it "should unpack string representation into integer" do
examples.each do |key, value|
described_class.unpack_64_big_endian(value)[0].should == key
it "should unpack string representation into integer" do
examples.each do |key, value|
described_class.unpack_64_big_endian(value)[0].should == key
end
end
end
end

end
end
end

0 comments on commit 32295b0

Please sign in to comment.