[前][次][番号順一覧][スレッド一覧]

ruby-changes:9043

From: nobu <ko1@a...>
Date: Mon, 8 Dec 2008 18:32:05 +0900 (JST)
Subject: [ruby-changes:9043] Ruby:r20580 (trunk, ruby_1_8): * pack.c (pack_pack): fixed length for odd length string.

nobu	2008-12-08 18:31:44 +0900 (Mon, 08 Dec 2008)

  New Revision: 20580

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=20580

  Log:
    * pack.c (pack_pack): fixed length for odd length string.
      [ruby-dev:37283]

  Modified files:
    branches/ruby_1_8/ChangeLog
    branches/ruby_1_8/pack.c
    branches/ruby_1_8/test/ruby/test_pack.rb
    trunk/ChangeLog
    trunk/pack.c
    trunk/test/ruby/test_pack.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 20579)
+++ ChangeLog	(revision 20580)
@@ -1,3 +1,8 @@
+Mon Dec  8 18:31:41 2008  Nobuyoshi Nakada  <nobu@r...>
+
+	* pack.c (pack_pack): fixed length for odd length string.
+	  [ruby-dev:37283]
+
 Mon Dec  8 11:28:14 2008  Nobuyoshi Nakada  <nobu@r...>
 
 	* ruby.c (proc_options): added version, copyright, usage, yydebug,
Index: pack.c
===================================================================
--- pack.c	(revision 20579)
+++ pack.c	(revision 20580)
@@ -620,7 +620,7 @@
 		    long i, j = 0;
 
 		    if (len > plen) {
-			j = (len - plen + 1)/2;
+			j = (len + 1) / 2 - (plen + 1) / 2;
 			len = plen;
 		    }
 		    for (i=0; i++ < len; ptr++) {
@@ -651,7 +651,7 @@
 		    long i, j = 0;
 
 		    if (len > plen) {
-			j = (len - plen + 1)/2;
+			j = (len + 1) / 2 - (plen + 1) / 2;
 			len = plen;
 		    }
 		    for (i=0; i++ < len; ptr++) {
Index: test/ruby/test_pack.rb
===================================================================
--- test/ruby/test_pack.rb	(revision 20579)
+++ test/ruby/test_pack.rb	(revision 20580)
@@ -179,11 +179,23 @@
     assert_equal("", ["10ef"].pack("h0"))
     assert_equal("\x01\x0e", ["10ef"].pack("h3"))
     assert_equal("\x01\xfe\x0", ["10ef"].pack("h5"))
+    assert_equal("\xff\x0f", ["fff"].pack("h3"))
+    assert_equal("\xff\x0f", ["fff"].pack("h4"))
+    assert_equal("\xff\x0f\0", ["fff"].pack("h5"))
+    assert_equal("\xff\x0f\0", ["fff"].pack("h6"))
+    assert_equal("\xff\x0f\0\0", ["fff"].pack("h7"))
+    assert_equal("\xff\x0f\0\0", ["fff"].pack("h8"))
 
     assert_equal("\x10\xef", ["10ef"].pack("H*"))
     assert_equal("", ["10ef"].pack("H0"))
     assert_equal("\x10\xe0", ["10ef"].pack("H3"))
     assert_equal("\x10\xef\x0", ["10ef"].pack("H5"))
+    assert_equal("\xff\xf0", ["fff"].pack("H3"))
+    assert_equal("\xff\xf0", ["fff"].pack("H4"))
+    assert_equal("\xff\xf0\0", ["fff"].pack("H5"))
+    assert_equal("\xff\xf0\0", ["fff"].pack("H6"))
+    assert_equal("\xff\xf0\0\0", ["fff"].pack("H7"))
+    assert_equal("\xff\xf0\0\0", ["fff"].pack("H8"))
 
     assert_equal(["10ef"], "\x01\xfe".unpack("h*"))
     assert_equal([""], "\x01\xfe".unpack("h0"))
Index: ruby_1_8/ChangeLog
===================================================================
--- ruby_1_8/ChangeLog	(revision 20579)
+++ ruby_1_8/ChangeLog	(revision 20580)
@@ -1,3 +1,8 @@
+Mon Dec  8 18:31:41 2008  Nobuyoshi Nakada  <nobu@r...>
+
+	* pack.c (pack_pack): fixed length for odd length string.
+	  [ruby-dev:37283]
+
 Mon Dec  8 13:51:41 2008  Nobuyoshi Nakada  <nobu@r...>
 
 	* eval.c (rb_yield_0): Qundef means no argument.  [ruby-Bugs-22525]
Index: ruby_1_8/pack.c
===================================================================
--- ruby_1_8/pack.c	(revision 20579)
+++ ruby_1_8/pack.c	(revision 20580)
@@ -609,7 +609,7 @@
 		    long i, j = 0;
 
 		    if (len > plen) {
-			j = (len - plen + 1)/2;
+			j = (len + 1) / 2 - (plen + 1) / 2;
 			len = plen;
 		    }
 		    for (i=0; i++ < len; ptr++) {
@@ -640,7 +640,7 @@
 		    long i, j = 0;
 
 		    if (len > plen) {
-			j = (len - plen + 1)/2;
+			j = (len + 1) / 2 - (plen + 1) / 2;
 			len = plen;
 		    }
 		    for (i=0; i++ < len; ptr++) {
Index: ruby_1_8/test/ruby/test_pack.rb
===================================================================
--- ruby_1_8/test/ruby/test_pack.rb	(revision 20579)
+++ ruby_1_8/test/ruby/test_pack.rb	(revision 20580)
@@ -57,4 +57,44 @@
     assert_raises(RangeError) { [0x80000000].pack("U") }
     assert_raises(RangeError) { [0x100000000].pack("U") }
   end
+
+  def test_pack_unpack_hH
+    assert_equal("\x01\xfe", ["10ef"].pack("h*"))
+    assert_equal("", ["10ef"].pack("h0"))
+    assert_equal("\x01\x0e", ["10ef"].pack("h3"))
+    assert_equal("\x01\xfe\x0", ["10ef"].pack("h5"))
+    assert_equal("\xff\x0f", ["fff"].pack("h3"))
+    assert_equal("\xff\x0f", ["fff"].pack("h4"))
+    assert_equal("\xff\x0f\0", ["fff"].pack("h5"))
+    assert_equal("\xff\x0f\0", ["fff"].pack("h6"))
+    assert_equal("\xff\x0f\0\0", ["fff"].pack("h7"))
+    assert_equal("\xff\x0f\0\0", ["fff"].pack("h8"))
+
+    assert_equal("\x10\xef", ["10ef"].pack("H*"))
+    assert_equal("", ["10ef"].pack("H0"))
+    assert_equal("\x10\xe0", ["10ef"].pack("H3"))
+    assert_equal("\x10\xef\x0", ["10ef"].pack("H5"))
+    assert_equal("\xff\xf0", ["fff"].pack("H3"))
+    assert_equal("\xff\xf0", ["fff"].pack("H4"))
+    assert_equal("\xff\xf0\0", ["fff"].pack("H5"))
+    assert_equal("\xff\xf0\0", ["fff"].pack("H6"))
+    assert_equal("\xff\xf0\0\0", ["fff"].pack("H7"))
+    assert_equal("\xff\xf0\0\0", ["fff"].pack("H8"))
+
+    assert_equal(["10ef"], "\x01\xfe".unpack("h*"))
+    assert_equal([""], "\x01\xfe".unpack("h0"))
+    assert_equal(["1"], "\x01\xfe".unpack("h1"))
+    assert_equal(["10"], "\x01\xfe".unpack("h2"))
+    assert_equal(["10e"], "\x01\xfe".unpack("h3"))
+    assert_equal(["10ef"], "\x01\xfe".unpack("h4"))
+    assert_equal(["10ef"], "\x01\xfe".unpack("h5"))
+
+    assert_equal(["10ef"], "\x10\xef".unpack("H*"))
+    assert_equal([""], "\x10\xef".unpack("H0"))
+    assert_equal(["1"], "\x10\xef".unpack("H1"))
+    assert_equal(["10"], "\x10\xef".unpack("H2"))
+    assert_equal(["10e"], "\x10\xef".unpack("H3"))
+    assert_equal(["10ef"], "\x10\xef".unpack("H4"))
+    assert_equal(["10ef"], "\x10\xef".unpack("H5"))
+  end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]