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

ruby-changes:42755

From: akr <ko1@a...>
Date: Sat, 30 Apr 2016 11:30:50 +0900 (JST)
Subject: [ruby-changes:42755] akr:r54829 (trunk): {Fixnum, Bignum}#~ is unified into Integer.

akr	2016-04-30 12:27:20 +0900 (Sat, 30 Apr 2016)

  New Revision: 54829

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54829

  Log:
    {Fixnum,Bignum}#~ is unified into Integer.
    
    * numeric.c (int_comp): {Fixnum,Bignum}#~ is unified into
      Integer.
    
    * bignum.c (rb_big_neg): Don't define Bignum#~.
    
    * internal.h (rb_big_neg): Declared.

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c
    trunk/internal.h
    trunk/numeric.c
Index: internal.h
===================================================================
--- internal.h	(revision 54828)
+++ internal.h	(revision 54829)
@@ -781,6 +781,7 @@ size_t rb_big_size(VALUE); https://github.com/ruby/ruby/blob/trunk/internal.h#L781
 VALUE rb_integer_float_cmp(VALUE x, VALUE y);
 VALUE rb_integer_float_eq(VALUE x, VALUE y);
 VALUE rb_cstr_parse_inum(const char *str, ssize_t len, char **endp, int base);
+VALUE rb_big_neg(VALUE x);
 VALUE rb_big_aref(VALUE x, VALUE y);
 VALUE rb_big_abs(VALUE x);
 VALUE rb_big_size_m(VALUE big);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 54828)
+++ ChangeLog	(revision 54829)
@@ -1,3 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Apr 30 12:25:43 2016  Tanaka Akira  <akr@f...>
+
+	* numeric.c (int_comp): {Fixnum,Bignum}#~ is unified into
+	  Integer.
+
+	* bignum.c (rb_big_neg): Don't define Bignum#~.
+
+	* internal.h (rb_big_neg): Declared.
+
 Sat Apr 30 12:07:42 2016  Tanaka Akira  <akr@f...>
 
 	* numeric.c (int_and): {Fixnum,Bignum}#& is unified into
Index: bignum.c
===================================================================
--- bignum.c	(revision 54828)
+++ bignum.c	(revision 54829)
@@ -5546,19 +5546,7 @@ rb_big_uminus(VALUE x) https://github.com/ruby/ruby/blob/trunk/bignum.c#L5546
     return bignorm(z);
 }
 
-/*
- * call-seq:
- *     ~big  ->  integer
- *
- * Inverts the bits in big. As Bignums are conceptually infinite
- * length, the result acts as if it had an infinite number of one
- * bits to the left. In hex representations, this is displayed
- * as two periods to the left of the digits.
- *
- *   sprintf("%X", ~0x1122334455)    #=> "..FEEDDCCBBAA"
- */
-
-static VALUE
+VALUE
 rb_big_neg(VALUE x)
 {
     VALUE z = rb_big_clone(x);
@@ -6963,7 +6951,6 @@ Init_Bignum(void) https://github.com/ruby/ruby/blob/trunk/bignum.c#L6951
     rb_define_method(rb_cBignum, "remainder", rb_big_remainder, 1);
     rb_define_method(rb_cBignum, "fdiv", rb_big_fdiv, 1);
     rb_define_method(rb_cBignum, "**", rb_big_pow, 1);
-    rb_define_method(rb_cBignum, "~", rb_big_neg, 0);
 
     rb_define_method(rb_cBignum, "==", rb_big_eq, 1);
     rb_define_method(rb_cBignum, ">", big_gt, 1);
Index: numeric.c
===================================================================
--- numeric.c	(revision 54828)
+++ numeric.c	(revision 54829)
@@ -3854,11 +3854,19 @@ fix_le(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3854
 }
 
 /*
- * Document-method: Fixnum#~
+ * Document-method: Integer#~
  * call-seq:
- *   ~fix  ->  integer
+ *   ~integer  ->  integer
  *
  * One's complement: returns a number where each bit is flipped.
+ *
+ * Inverts the bits in a integer. As Integers are conceptually infinite
+ * length, the result acts as if it had an infinite number of one
+ * bits to the left. In hex representations, this is displayed
+ * as two periods to the left of the digits.
+ *
+ *   sprintf("%X", ~0x1122334455)    #=> "..FEEDDCCBBAA"
+ *
  */
 
 static VALUE
@@ -3867,6 +3875,18 @@ fix_rev(VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3875
     return ~num | FIXNUM_FLAG;
 }
 
+static VALUE
+int_comp(VALUE num)
+{
+    if (FIXNUM_P(num)) {
+	return fix_rev(num);
+    }
+    else if (RB_TYPE_P(num, T_BIGNUM)) {
+	return rb_big_neg(num);
+    }
+    return Qnil;
+}
+
 static int
 bit_coerce(VALUE *x, VALUE *y)
 {
@@ -4791,7 +4811,7 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4811
     rb_define_method(rb_cFixnum, "<",  fix_lt, 1);
     rb_define_method(rb_cFixnum, "<=", fix_le, 1);
 
-    rb_define_method(rb_cFixnum, "~", fix_rev, 0);
+    rb_define_method(rb_cInteger, "~", int_comp, 0);
     rb_define_method(rb_cInteger, "&", int_and, 1);
     rb_define_method(rb_cInteger, "|", int_or,  1);
     rb_define_method(rb_cInteger, "^", int_xor, 1);

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

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