ruby-changes:25217
From: eregon <ko1@a...>
Date: Fri, 19 Oct 2012 22:13:58 +0900 (JST)
Subject: [ruby-changes:25217] eregon:r37269 (trunk): set encoding to ASCII for appropriate String#unpack modifiers
eregon 2012-10-19 22:13:32 +0900 (Fri, 19 Oct 2012) New Revision: 37269 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=37269 Log: 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] Modified files: trunk/ChangeLog trunk/pack.c trunk/test/ruby/test_pack.rb trunk/test/test_securerandom.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 37268) +++ ChangeLog (revision 37269) @@ -1,3 +1,14 @@ +Fri Oct 19 22:11:55 2012 Benoit Daloze <eregontp@g...> + + * 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@a...> * method.h (rb_method_cfunc_t::invoker): add new field (func ptr) Index: pack.c =================================================================== --- pack.c (revision 37268) +++ pack.c (revision 37269) @@ -1459,7 +1459,7 @@ 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 @@ 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 @@ 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 @@ 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) Index: test/ruby/test_pack.rb =================================================================== --- test/ruby/test_pack.rb (revision 37268) +++ test/ruby/test_pack.rb (revision 37269) @@ -280,6 +280,9 @@ 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 @@ 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 Index: test/test_securerandom.rb =================================================================== --- test/test_securerandom.rb (revision 37268) +++ test/test_securerandom.rb (revision 37269) @@ -111,6 +111,10 @@ 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| -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/