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

ruby-changes:30687

From: akr <ko1@a...>
Date: Mon, 2 Sep 2013 01:47:26 +0900 (JST)
Subject: [ruby-changes:30687] akr:r42766 (trunk): * bignum.c (big2str_generic): Reduce arguments.

akr	2013-09-02 01:47:20 +0900 (Mon, 02 Sep 2013)

  New Revision: 42766

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

  Log:
    * bignum.c (big2str_generic): Reduce arguments.
      (big2str_gmp): Ditto.
      (rb_big2str1): Follow the above change.

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42765)
+++ ChangeLog	(revision 42766)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Sep  2 01:46:14 2013  Tanaka Akira  <akr@f...>
+
+	* bignum.c (big2str_generic): Reduce arguments.
+	  (big2str_gmp): Ditto.
+	  (rb_big2str1): Follow the above change.
+
 Mon Sep  2 00:08:08 2013  Tanaka Akira  <akr@f...>
 
 	* process.c (get_mach_timebase_info): Extracted from rb_clock_gettime.
Index: bignum.c
===================================================================
--- bignum.c	(revision 42765)
+++ bignum.c	(revision 42766)
@@ -4397,12 +4397,29 @@ rb_big2str_poweroftwo(VALUE x, int base) https://github.com/ruby/ruby/blob/trunk/bignum.c#L4397
 }
 
 static VALUE
-big2str_generic(VALUE x, int base, BDIGIT *xds, size_t xn)
+big2str_generic(VALUE x, int base)
 {
+    BDIGIT *xds;
+    size_t xn;
     struct big2str_struct b2s_data;
     int power_level;
     VALUE power;
 
+    xds = BDIGITS(x);
+    xn = RBIGNUM_LEN(x);
+    BARY_TRUNC(xds, xn);
+
+    if (xn == 0) {
+	return rb_usascii_str_new2("0");
+    }
+
+    if (base < 2 || 36 < base)
+	rb_raise(rb_eArgError, "invalid radix %d", base);
+
+    if (xn >= LONG_MAX/BITSPERDIG) {
+        rb_raise(rb_eRangeError, "bignum too big to convert into `string'");
+    }
+
     power_level = 0;
     power = power_cache_get_power(base, power_level, NULL);
     while (power_level < MAX_BASE36_POWER_TABLE_ENTRIES &&
@@ -4452,63 +4469,54 @@ big2str_generic(VALUE x, int base, BDIGI https://github.com/ruby/ruby/blob/trunk/bignum.c#L4469
     *b2s_data.ptr = '\0';
     rb_str_resize(b2s_data.result, (long)(b2s_data.ptr - RSTRING_PTR(b2s_data.result)));
 
+    RB_GC_GUARD(x);
     return b2s_data.result;
 }
 
 VALUE
 rb_big2str_generic(VALUE x, int base)
 {
-    BDIGIT *xds;
-    size_t xn;
-
-    xds = BDIGITS(x);
-    xn = RBIGNUM_LEN(x);
-    BARY_TRUNC(xds, xn);
-
-    return big2str_generic(x, base, xds, xn);
+    return big2str_generic(x, base);
 }
 
 #ifdef USE_GMP
 VALUE
-big2str_gmp(int negative_p, int base, BDIGIT *xds, size_t xn)
+big2str_gmp(VALUE x, int base)
 {
     const size_t nails = (sizeof(BDIGIT)-SIZEOF_BDIGITS)*CHAR_BIT;
-    mpz_t x;
+    mpz_t mx;
     size_t size;
     VALUE str;
-    char *p;
+    BDIGIT *xds = BDIGITS(x);
+    size_t xn = RBIGNUM_LEN(x);
 
-    mpz_init(x);
-    mpz_import(x, xn, -1, sizeof(BDIGIT), 0, nails, xds);
+    mpz_init(mx);
+    mpz_import(mx, xn, -1, sizeof(BDIGIT), 0, nails, xds);
 
-    size = mpz_sizeinbase(x, base);
+    size = mpz_sizeinbase(mx, base);
 
-    if (negative_p) {
+    if (RBIGNUM_NEGATIVE_P(x)) {
+        mpz_neg(mx, mx);
         str = rb_usascii_str_new(0, size+1);
-        p = RSTRING_PTR(str);
-        *p++ = '-';
     }
     else {
         str = rb_usascii_str_new(0, size);
-        p = RSTRING_PTR(str);
     }
-    mpz_get_str(p, base, x);
-    mpz_clear(x);
+    mpz_get_str(RSTRING_PTR(str), base, mx);
+    mpz_clear(mx);
 
     if (RSTRING_PTR(str)[RSTRING_LEN(str)-1] == '\0') {
         rb_str_set_len(str, RSTRING_LEN(str)-1);
     }
 
+    RB_GC_GUARD(x);
     return str;
 }
 
 VALUE
 rb_big2str_gmp(VALUE x, int base)
 {
-    VALUE str;
-    str = big2str_gmp(RBIGNUM_NEGATIVE_P(x), base, BDIGITS(x), RBIGNUM_LEN(x));
-    RB_GC_GUARD(x);
-    return str;
+    return big2str_gmp(x, base);
 }
 #endif
 
@@ -4522,6 +4530,7 @@ rb_big2str1(VALUE x, int base) https://github.com/ruby/ruby/blob/trunk/bignum.c#L4530
 	return rb_fix2str(x, base);
     }
 
+    bigtrunc(x);
     xds = BDIGITS(x);
     xn = RBIGNUM_LEN(x);
     BARY_TRUNC(xds, xn);
@@ -4544,14 +4553,11 @@ rb_big2str1(VALUE x, int base) https://github.com/ruby/ruby/blob/trunk/bignum.c#L4553
 
 #ifdef USE_GMP
     if (GMP_BIG2STR_DIGITS < xn) {
-        VALUE str;
-        str = big2str_gmp(RBIGNUM_NEGATIVE_P(x), base, xds, xn);
-        RB_GC_GUARD(x);
-        return str;
+        return big2str_gmp(x, base);
     }
 #endif
 
-    return big2str_generic(x, base, xds, xn);
+    return big2str_generic(x, base);
 }
 
 /* deprecated */

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

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