set encoding to ASCII for appropriate String#unpack modifiers

* pack.c (pack_unpack): set encoding of the
  'H','h','B' and 'B' modifiers to US-ASCII.
* test/ruby/test_pack.rb: tests for the above.
  [ruby-core:47653][Bug #7050]
* test/test_securerandom.rb: tests for SecureRandom.hex
  from tenderlove. [ruby-core:46792][Bug #6799]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37269 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
eregon 2012-10-19 13:13:32 +00:00
Родитель f4dbc7a384
Коммит b0e40509c3
4 изменённых файлов: 25 добавлений и 4 удалений

Просмотреть файл

@ -1,3 +1,14 @@
Fri Oct 19 22:11:55 2012 Benoit Daloze <eregontp@gmail.com>
* pack.c (pack_unpack): set encoding of the
'H','h','B' and 'B' modifiers to US-ASCII.
* test/ruby/test_pack.rb: tests for the above.
[ruby-core:47653][Bug #7050]
* test/test_securerandom.rb: tests for SecureRandom.hex
from tenderlove. [ruby-core:46792][Bug #6799]
Fri Oct 19 19:29:11 2012 Koichi Sasada <ko1@atdot.net>
* method.h (rb_method_cfunc_t::invoker): add new field (func ptr)

8
pack.c
Просмотреть файл

@ -1459,7 +1459,7 @@ pack_unpack(VALUE str, VALUE fmt)
if (p[-1] == '*' || len > (send - s) * 8)
len = (send - s) * 8;
bits = 0;
UNPACK_PUSH(bitstr = rb_str_new(0, len));
UNPACK_PUSH(bitstr = rb_usascii_str_new(0, len));
t = RSTRING_PTR(bitstr);
for (i=0; i<len; i++) {
if (i & 7) bits >>= 1;
@ -1479,7 +1479,7 @@ pack_unpack(VALUE str, VALUE fmt)
if (p[-1] == '*' || len > (send - s) * 8)
len = (send - s) * 8;
bits = 0;
UNPACK_PUSH(bitstr = rb_str_new(0, len));
UNPACK_PUSH(bitstr = rb_usascii_str_new(0, len));
t = RSTRING_PTR(bitstr);
for (i=0; i<len; i++) {
if (i & 7) bits <<= 1;
@ -1499,7 +1499,7 @@ pack_unpack(VALUE str, VALUE fmt)
if (p[-1] == '*' || len > (send - s) * 2)
len = (send - s) * 2;
bits = 0;
UNPACK_PUSH(bitstr = rb_str_new(0, len));
UNPACK_PUSH(bitstr = rb_usascii_str_new(0, len));
t = RSTRING_PTR(bitstr);
for (i=0; i<len; i++) {
if (i & 1)
@ -1521,7 +1521,7 @@ pack_unpack(VALUE str, VALUE fmt)
if (p[-1] == '*' || len > (send - s) * 2)
len = (send - s) * 2;
bits = 0;
UNPACK_PUSH(bitstr = rb_str_new(0, len));
UNPACK_PUSH(bitstr = rb_usascii_str_new(0, len));
t = RSTRING_PTR(bitstr);
for (i=0; i<len; i++) {
if (i & 1)

Просмотреть файл

@ -280,6 +280,9 @@ class TestPack < Test::Unit::TestCase
assert_equal(["1"], "\x80".unpack("B1"))
assert_equal(["10"], "\x80".unpack("B2"))
assert_equal(["100"], "\x80".unpack("B3"))
assert_equal(Encoding::US_ASCII, "\xff\x00".unpack("b*")[0].encoding)
assert_equal(Encoding::US_ASCII, "\xff\x00".unpack("B*")[0].encoding)
end
def test_pack_unpack_hH
@ -320,6 +323,9 @@ class TestPack < Test::Unit::TestCase
assert_equal(["10e"], "\x10\xef".unpack("H3"))
assert_equal(["10ef"], "\x10\xef".unpack("H4"))
assert_equal(["10ef"], "\x10\xef".unpack("H5"))
assert_equal(Encoding::US_ASCII, "\x10\xef".unpack("h*")[0].encoding)
assert_equal(Encoding::US_ASCII, "\x10\xef".unpack("H*")[0].encoding)
end
def test_pack_unpack_cC

Просмотреть файл

@ -111,6 +111,10 @@ end
end
end
def test_hex_encoding
assert_equal(Encoding::US_ASCII, @it.hex.encoding)
end
def test_s_base64
assert_equal(16, @it.base64.unpack('m*')[0].size)
17.times do |idx|