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

ruby-changes:29187

From: akr <ko1@a...>
Date: Wed, 12 Jun 2013 06:40:10 +0900 (JST)
Subject: [ruby-changes:29187] akr:r41239 (trunk): * internal.h (INTEGER_PACK_NEGATIVE): Defined.

akr	2013-06-12 06:39:55 +0900 (Wed, 12 Jun 2013)

  New Revision: 41239

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

  Log:
    * internal.h (INTEGER_PACK_NEGATIVE): Defined.
      (rb_integer_unpack): sign argument removed.
    
    * bignum.c (rb_integer_unpack): sign argument removed.
      Non-negative integers generated by default.
      INTEGER_PACK_NEGATIVE flag is used to generate non-positive integers.
    
    * pack.c (pack_unpack): Follow the above change.
    
    * random.c (int_pair_to_real_inclusive): Ditto.
      (make_seed_value): Ditto.
      (mt_state): Ditto.
      (limited_big_rand): Ditto.
    
    * marshal.c (r_object0): Ditto.

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c
    trunk/ext/-test-/bignum/pack.c
    trunk/internal.h
    trunk/marshal.c
    trunk/pack.c
    trunk/random.c
    trunk/test/-ext-/bignum/test_pack.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 41238)
+++ ChangeLog	(revision 41239)
@@ -1,3 +1,21 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Jun 12 06:35:01 2013  Tanaka Akira  <akr@f...>
+
+	* internal.h (INTEGER_PACK_NEGATIVE): Defined.
+	  (rb_integer_unpack): sign argument removed.
+
+	* bignum.c (rb_integer_unpack): sign argument removed.
+	  Non-negative integers generated by default.
+	  INTEGER_PACK_NEGATIVE flag is used to generate non-positive integers.
+
+	* pack.c (pack_unpack): Follow the above change.
+
+	* random.c (int_pair_to_real_inclusive): Ditto.
+	  (make_seed_value): Ditto.
+	  (mt_state): Ditto.
+	  (limited_big_rand): Ditto.
+
+	* marshal.c (r_object0): Ditto.
+
 Wed Jun 12 00:07:46 2013  Kouhei Sutou  <kou@c...>
 
 	* lib/xmlrpc/client.rb (XMLRPC::Client#parse_set_cookies): Extract.
Index: pack.c
===================================================================
--- pack.c	(revision 41238)
+++ pack.c	(revision 41239)
@@ -2142,7 +2142,7 @@ pack_unpack(VALUE str, VALUE fmt) https://github.com/ruby/ruby/blob/trunk/pack.c#L2142
                     }
                     else {
                         s++;
-                        UNPACK_PUSH(rb_integer_unpack(1, s0, s-s0, 1, 1, INTEGER_PACK_BIG_ENDIAN));
+                        UNPACK_PUSH(rb_integer_unpack(s0, s-s0, 1, 1, INTEGER_PACK_BIG_ENDIAN));
                         len--;
                         s0 = s;
                     }
Index: ext/-test-/bignum/pack.c
===================================================================
--- ext/-test-/bignum/pack.c	(revision 41238)
+++ ext/-test-/bignum/pack.c	(revision 41239)
@@ -54,11 +54,11 @@ rb_integer_pack_2comp_m(VALUE val, VALUE https://github.com/ruby/ruby/blob/trunk/ext/-test-/bignum/pack.c#L54
 }
 
 static VALUE
-rb_integer_unpack_m(VALUE klass, VALUE sign, VALUE buf, VALUE numwords, VALUE wordsize, VALUE nails, VALUE flags)
+rb_integer_unpack_m(VALUE klass, VALUE buf, VALUE numwords, VALUE wordsize, VALUE nails, VALUE flags)
 {
     StringValue(buf);
 
-    return rb_integer_unpack(NUM2INT(sign), RSTRING_PTR(buf),
+    return rb_integer_unpack(RSTRING_PTR(buf),
             NUM2SIZET(numwords), NUM2SIZET(wordsize),
             NUM2SIZET(nails), NUM2INT(flags));
 }
@@ -69,7 +69,7 @@ Init_pack(VALUE klass) https://github.com/ruby/ruby/blob/trunk/ext/-test-/bignum/pack.c#L69
     rb_define_method(rb_cInteger, "test_pack_raw", rb_integer_pack_raw_m, 5);
     rb_define_method(rb_cInteger, "test_pack", rb_integer_pack_m, 4);
     rb_define_method(rb_cInteger, "test_pack_2comp", rb_integer_pack_2comp_m, 4);
-    rb_define_singleton_method(rb_cInteger, "test_unpack", rb_integer_unpack_m, 6);
+    rb_define_singleton_method(rb_cInteger, "test_unpack", rb_integer_unpack_m, 5);
     rb_define_const(rb_cInteger, "INTEGER_PACK_MSWORD_FIRST", INT2NUM(INTEGER_PACK_MSWORD_FIRST));
     rb_define_const(rb_cInteger, "INTEGER_PACK_LSWORD_FIRST", INT2NUM(INTEGER_PACK_LSWORD_FIRST));
     rb_define_const(rb_cInteger, "INTEGER_PACK_MSBYTE_FIRST", INT2NUM(INTEGER_PACK_MSBYTE_FIRST));
@@ -77,4 +77,6 @@ Init_pack(VALUE klass) https://github.com/ruby/ruby/blob/trunk/ext/-test-/bignum/pack.c#L77
     rb_define_const(rb_cInteger, "INTEGER_PACK_NATIVE_BYTE_ORDER", INT2NUM(INTEGER_PACK_NATIVE_BYTE_ORDER));
     rb_define_const(rb_cInteger, "INTEGER_PACK_LITTLE_ENDIAN", INT2NUM(INTEGER_PACK_LITTLE_ENDIAN));
     rb_define_const(rb_cInteger, "INTEGER_PACK_BIG_ENDIAN", INT2NUM(INTEGER_PACK_BIG_ENDIAN));
+    rb_define_const(rb_cInteger, "INTEGER_PACK_FORCE_BIGNUM", INT2NUM(INTEGER_PACK_FORCE_BIGNUM));
+    rb_define_const(rb_cInteger, "INTEGER_PACK_NEGATIVE", INT2NUM(INTEGER_PACK_NEGATIVE));
 }
Index: internal.h
===================================================================
--- internal.h	(revision 41238)
+++ internal.h	(revision 41239)
@@ -63,6 +63,7 @@ extern "C" { https://github.com/ruby/ruby/blob/trunk/internal.h#L63
 #define INTEGER_PACK_NATIVE_BYTE_ORDER  0x40
 /* For rb_integer_unpack: */
 #define INTEGER_PACK_FORCE_BIGNUM       0x100
+#define INTEGER_PACK_NEGATIVE           0x200
 /* Combinations: */
 #define INTEGER_PACK_LITTLE_ENDIAN \
     (INTEGER_PACK_LSWORD_FIRST | \
@@ -448,7 +449,7 @@ VALUE rb_thread_io_blocking_region(rb_bl https://github.com/ruby/ruby/blob/trunk/internal.h#L449
 
 /* bignum.c */
 int rb_integer_pack(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags);
-VALUE rb_integer_unpack(int sign, const void *words, size_t numwords, size_t wordsize, size_t nails, int flags);
+VALUE rb_integer_unpack(const void *words, size_t numwords, size_t wordsize, size_t nails, int flags);
 int rb_integer_pack_2comp(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags);
 
 /* io.c */
Index: bignum.c
===================================================================
--- bignum.c	(revision 41238)
+++ bignum.c	(revision 41239)
@@ -1178,8 +1178,6 @@ integer_unpack_push_bits(int data, int n https://github.com/ruby/ruby/blob/trunk/bignum.c#L1178
 /*
  * Import an integer into a buffer.
  *
- * [sign] signedness of the value.
- *   -1 for non-positive.  0 or 1 for non-negative.
  * [words] buffer to import.
  * [numwords] the size of given buffer as number of words.
  * [wordsize] the size of word as number of bytes.
@@ -1187,17 +1185,20 @@ integer_unpack_push_bits(int data, int n https://github.com/ruby/ruby/blob/trunk/bignum.c#L1185
  *   Most significant nails bits of each word are ignored.
  * [flags] bitwise or of constants which name starts "INTEGER_PACK_".
  *   It specifies word order and byte order.
- *   Also, INTEGER_PACK_FORCE_BIGNUM specifies that the result will be a Bignum
- *   even if it is representable as a Fixnum.
+ *   [INTEGER_PACK_FORCE_BIGNUM] the result will be a Bignum
+ *     even if it is representable as a Fixnum.
+ *   [INTEGER_PACK_NEGATIVE] Returns non-positive value.
+ *     (Returns non-negative value if not specified.)
  *
  * This function returns the imported integer as Fixnum or Bignum.
  */
 VALUE
-rb_integer_unpack(int sign, const void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
+rb_integer_unpack(const void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
 {
     VALUE result;
     const unsigned char *buf = words;
     size_t num_bdigits;
+    int sign = (flags & INTEGER_PACK_NEGATIVE) ? -1 : 1;
 
     BDIGIT *dp;
     BDIGIT *de;
@@ -1215,8 +1216,6 @@ rb_integer_unpack(int sign, const void * https://github.com/ruby/ruby/blob/trunk/bignum.c#L1216
     int numbits_in_dd;
 
     validate_integer_pack_format(numwords, wordsize, nails, flags);
-    if (sign != 1 && sign != 0 && sign != -1)
-        rb_raise(rb_eArgError, "unexpected sign: %d", sign);
 
     if (numwords <= (SIZE_MAX - (SIZEOF_BDIGITS*CHAR_BIT-1)) / CHAR_BIT / wordsize) {
         num_bdigits = integer_unpack_num_bdigits_small(numwords, wordsize, nails);
Index: marshal.c
===================================================================
--- marshal.c	(revision 41238)
+++ marshal.c	(revision 41239)
@@ -1616,8 +1616,8 @@ r_object0(struct load_arg *arg, int *ivp https://github.com/ruby/ruby/blob/trunk/marshal.c#L1616
 	    sign = r_byte(arg);
 	    len = r_long(arg);
 	    data = r_bytes0(len * 2, arg);
-            v = rb_integer_unpack(sign == '-' ? -1 : +1, RSTRING_PTR(data), len, 2, 0,
-                INTEGER_PACK_LITTLE_ENDIAN);
+            v = rb_integer_unpack(RSTRING_PTR(data), len, 2, 0,
+                INTEGER_PACK_LITTLE_ENDIAN | (sign == '-' ? INTEGER_PACK_NEGATIVE : 0));
 	    rb_str_resize(data, 0L);
 	    v = r_entry(v, arg);
             v = r_leave(v, arg);
Index: test/-ext-/bignum/test_pack.rb
===================================================================
--- test/-ext-/bignum/test_pack.rb	(revision 41238)
+++ test/-ext-/bignum/test_pack.rb	(revision 41239)
@@ -13,6 +13,7 @@ class TestBignum < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/-ext-/bignum/test_pack.rb#L13
     NATIVE_BYTE_ORDER = Integer::INTEGER_PACK_NATIVE_BYTE_ORDER
     LITTLE_ENDIAN = Integer::INTEGER_PACK_LITTLE_ENDIAN
     BIG_ENDIAN = Integer::INTEGER_PACK_BIG_ENDIAN
+    NEGATIVE = Integer::INTEGER_PACK_NEGATIVE
 
     def test_pack_zero
       assert_equal([0, ""], 0.test_pack(0, 1, 0, BIG_ENDIAN))
@@ -117,48 +118,48 @@ class TestBignum < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/-ext-/bignum/test_pack.rb#L118
     end
 
     def test_unpack_zero
-      assert_equal(0, Integer.test_unpack(0, "", 0, 1, 0, BIG_ENDIAN))
+      assert_equal(0, Integer.test_unpack("", 0, 1, 0, BIG_ENDIAN))
     end
 
     def test_unpack_argument_check
-      assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 2, 1, 0, MSBYTE_FIRST) }
-      assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 1, 0, MSWORD_FIRST) }
-      assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 0, 0, BIG_ENDIAN) }
-      assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 1, 8, BIG_ENDIAN) }
+      assert_raise(ArgumentError) { Integer.test_unpack("x", 2, 1, 0, MSBYTE_FIRST) }
+      assert_raise(ArgumentError) { Integer.test_unpack("x", 1, 1, 0, MSWORD_FIRST) }
+      assert_raise(ArgumentError) { Integer.test_unpack("x", 1, 0, 0, BIG_ENDIAN) }
+      assert_raise(ArgumentError) { Integer.test_unpack("x", 1, 1, 8, BIG_ENDIAN) }
 
       # assume sizeof(ssize_t) == sizeof(intptr_t)
-      assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 1 << ([""].pack("p").length * 8 - 1), 0, BIG_ENDIAN) }
+      assert_raise(ArgumentError) { Integer.test_unpack("x", 1, 1 << ([""].pack("p").length * 8 - 1), 0, BIG_ENDIAN) }
     end
 
     def test_unpack_wordsize
-      assert_equal(1, Integer.test_unpack(1, "\x01", 1, 1, 0, BIG_ENDIAN))
-      assert_equal(1, Integer.test_unpack(1, "\x00\x01", 1, 2, 0, BIG_ENDIAN))
-      assert_equal(1, Integer.test_unpack(1, "\x00\x00\x01", 1, 3, 0, BIG_ENDIAN))
-      assert_equal(1, Integer.test_unpack(1, "\x01", 1, 1, 0, LITTLE_ENDIAN))
-      assert_equal(1, Integer.test_unpack(1, "\x01\x00", 1, 2, 0, LITTLE_ENDIAN))
-      assert_equal(1, Integer.test_unpack(1, "\x01\x00\x00", 1, 3, 0, LITTLE_ENDIAN))
+      assert_equal(1, Integer.test_unpack("\x01", 1, 1, 0, BIG_ENDIAN))
+      assert_equal(1, Integer.test_unpack("\x00\x01", 1, 2, 0, BIG_ENDIAN))
+      assert_equal(1, Integer.test_unpack("\x00\x00\x01", 1, 3, 0, BIG_ENDIAN))
+      assert_equal(1, Integer.test_unpack("\x01", 1, 1, 0, LITTLE_ENDIAN))
+      assert_equal(1, Integer.test_unpack("\x01\x00", 1, 2, 0, LITTLE_ENDIAN))
+      assert_equal(1, Integer.test_unpack("\x01\x00\x00", 1, 3, 0, LITTLE_ENDIAN))
     end
 
     def test_unpack_wordorder_and_endian
-      assert_equal(0x01020304, Integer.test_unpack(1, "\x01\x02\x03\x04", 2, 2, 0, MSWORD_FIRST|MSBYTE_FIRST))
-      assert_equal(0x02010403, Integer.test_unpack(1, "\x01\x02\x03\x04", 2, 2, 0, MSWORD_FIRST|LSBYTE_FIRST))
-      assert_equal(0x03040102, Integer.test_unpack(1, "\x01\x02\x03\x04", 2, 2, 0, LSWORD_FIRST|MSBYTE_FIRST))
-      assert_equal(0x04030201, Integer.test_unpack(1, "\x01\x02\x03\x04", 2, 2, 0, LSWORD_FIRST|LSBYTE_FIRST))
+      assert_equal(0x01020304, Integer.test_unpack("\x01\x02\x03\x04", 2, 2, 0, MSWORD_FIRST|MSBYTE_FIRST))
+      assert_equal(0x02010403, Integer.test_unpack("\x01\x02\x03\x04", 2, 2, 0, MSWORD_FIRST|LSBYTE_FIRST))
+      assert_equal(0x03040102, Integer.test_unpack("\x01\x02\x03\x04", 2, 2, 0, LSWORD_FIRST|MSBYTE_FIRST))
+      assert_equal(0x04030201, Integer.test_unpack("\x01\x02\x03\x04", 2, 2, 0, LSWORD_FIRST|LSBYTE_FIRST))
     end
 
     def test_unpack_native_endian
-      assert_equal("\x12\x34".unpack("S!")[0], Integer.test_unpack(1, "\x12\x34", 1, 2, 0, MSWORD_FIRST|NATIVE_BYTE_ORDER))
+      assert_equal("\x12\x34".unpack("S!")[0], Integer.test_unpack("\x12\x34", 1, 2, 0, MSWORD_FIRST|NATIVE_BYTE_ORDER))
     end
 
     def test_unpack_nail
-      assert_equal(0b100011, Integer.test_unpack(1, "\x01\x00\x00\x00\x01\x01", 6, 1, 7, BIG_ENDIAN))
-      assert_equal(0x12345678, Integer.test_unpack(1, "\x01\x02\x03\x04\x05\x06\x07\x08", 8, 1, 4, BIG_ENDIAN))
-      assert_equal(0x12345678, Integer.test_unpack(1, "\x00\x12\x00\x34\x00\x56\x00\x78", 4, 2, 8, BIG_ENDIAN))
+      assert_equal(0b100011, Integer.test_unpack("\x01\x00\x00\x00\x01\x01", 6, 1, 7, BIG_ENDIAN))
+      assert_equal(0x12345678, Integer.test_unpack("\x01\x02\x03\x04\x05\x06\x07\x08", 8, 1, 4, BIG_ENDIAN))
+      assert_equal(0x12345678, Integer.test_unpack("\x00\x12\x00\x34\x00\x56\x00\x78", 4, 2, 8, BIG_ENDIAN))
     end
 
     def test_unpack_sign
-      assert_equal(-1, Integer.test_unpack(-1, "\x01", 1, 1, 0, BIG_ENDIAN))
-      assert_equal(-0x8070605040302010, Integer.test_unpack(-1, "\x80\x70\x60\x50\x40\x30\x20\x10", 8, 1, 0, BIG_ENDIAN))
+      assert_equal(-1, Integer.test_unpack("\x01", 1, 1, 0, BIG_ENDIAN|NEGATIVE))
+      assert_equal(-0x8070605040302010, Integer.test_unpack("\x80\x70\x60\x50\x40\x30\x20\x10", 8, 1, 0, BIG_ENDIAN|NEGATIVE))
     end
 
   end
Index: random.c
===================================================================
--- random.c	(revision 41238)
+++ random.c	(revision 41239)
@@ -274,14 +274,14 @@ int_pair_to_real_inclusive(uint32_t a, u https://github.com/ruby/ruby/blob/trunk/random.c#L274
     /* (a << 32) | b */
     xary[0] = a;
     xary[1] = b;
-    x = rb_integer_unpack(+1, xary, 2, sizeof(uint32_t), 0,
+    x = rb_integer_unpack(xary, 2, sizeof(uint32_t), 0,
         INTEGER_PACK_MSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER|
         INTEGER_PACK_FORCE_BIGNUM);
 
     /* (1 << 53) | 1 */
     mary[0] = 0x00200000;
     mary[1] = 0x00000001;
-    m = rb_integer_unpack(+1, mary, 2, sizeof(uint32_t), 0,
+    m = rb_integer_unpack(mary, 2, sizeof(uint32_t), 0,
         INTEGER_PACK_MSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER|
         INTEGER_PACK_FORCE_BIGNUM);
 
@@ -508,7 +508,7 @@ make_seed_value(const uint32_t *ptr) https://github.com/ruby/ruby/blob/trunk/random.c#L508
         len = DEFAULT_SEED_CNT;
     }
 
-    seed = rb_integer_unpack(+1, ptr, len, sizeof(uint32_t), 0,
+    seed = rb_integer_unpack(ptr, len, sizeof(uint32_t), 0,
         INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
 
     return seed;
@@ -571,7 +571,7 @@ random_copy(VALUE obj, VALUE orig) https://github.com/ruby/ruby/blob/trunk/random.c#L571
 static VALUE
 mt_state(const struct MT *mt)
 {
-    return rb_integer_unpack(1, mt->state, numberof(mt->state),
+    return rb_integer_unpack(mt->state, numberof(mt->state),
         sizeof(*mt->state), 0,
         INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
 }
@@ -778,7 +778,7 @@ limited_big_rand(struct MT *mt, VALUE li https://github.com/ruby/ruby/blob/trunk/random.c#L778
         }
         rnd_array[i] = rnd;
     }
-    val = rb_integer_unpack(+1, rnd_array, len, sizeof(uint32_t), 0,
+    val = rb_integer_unpack(rnd_array, len, sizeof(uint32_t), 0,
         INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
     ALLOCV_END(vtmp);
 

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

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