Skip to content
Browse files

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
  • Loading branch information...
1 parent f4dbc7a commit b0e40509c3b60242c6a1b0e67276849eea505977 @eregon eregon committed Oct 19, 2012
Showing with 25 additions and 4 deletions.
  1. +11 −0 ChangeLog
  2. +4 −4 pack.c
  3. +6 −0 test/ruby/test_pack.rb
  4. +4 −0 test/test_securerandom.rb
View
11 ChangeLog
@@ -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)
View
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)
View
6 test/ruby/test_pack.rb
@@ -280,6 +280,9 @@ def test_pack_unpack_bB
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 @@ def test_pack_unpack_hH
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
View
4 test/test_securerandom.rb
@@ -111,6 +111,10 @@ def test_s_hex
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|

0 comments on commit b0e4050

Please sign in to comment.
Something went wrong with that request. Please try again.