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

ruby-changes:19291

From: tadf <ko1@a...>
Date: Sun, 24 Apr 2011 22:24:09 +0900 (JST)
Subject: [ruby-changes:19291] Ruby:r31330 (trunk): * complex.c, rational.c: omitted some method calls.

tadf	2011-04-24 22:24:02 +0900 (Sun, 24 Apr 2011)

  New Revision: 31330

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

  Log:
    * complex.c, rational.c: omitted some method calls.

  Modified files:
    trunk/ChangeLog
    trunk/complex.c
    trunk/rational.c

Index: complex.c
===================================================================
--- complex.c	(revision 31329)
+++ complex.c	(revision 31330)
@@ -1,5 +1,5 @@
 /*
-  complex.c: Coded by Tadayoshi Funaba 2008-2010
+  complex.c: Coded by Tadayoshi Funaba 2008-2011
 
   This implementation is based on Keiju Ishitsuka's Complex library
   which is written in ruby.
@@ -162,8 +162,21 @@
 fun1(real)
 fun1(real_p)
 
-fun1(to_f)
-fun1(to_i)
+inline static VALUE
+f_to_i(VALUE x)
+{
+    if (TYPE(x) == T_STRING)
+	return rb_str_to_inum(x, 10, 0);
+    return rb_funcall(x, id_to_i, 0);
+}
+inline static VALUE
+f_to_f(VALUE x)
+{
+    if (TYPE(x) == T_STRING)
+	return DBL2NUM(rb_str_to_dbl(x, 0));
+    return rb_funcall(x, id_to_f, 0);
+}
+
 fun1(to_r)
 fun1(to_s)
 
@@ -1438,15 +1451,6 @@
 #define id_post_match rb_intern("post_match")
 #define f_post_match(x) rb_funcall((x), id_post_match, 0)
 
-#define id_split rb_intern("split")
-#define f_split(x,y) rb_funcall((x), id_split, 1, (y))
-
-#define id_include_p rb_intern("include?")
-#define f_include_p(x,y) rb_funcall((x), id_include_p, 1, (y))
-
-#define id_count rb_intern("count")
-#define f_count(x,y) rb_funcall((x), id_count, 1, (y))
-
 #define id_gsub_bang rb_intern("gsub!")
 #define f_gsub_bang(x,y,z) rb_funcall((x), id_gsub_bang, 2, (y), (z))
 
@@ -1512,17 +1516,17 @@
 	r = INT2FIX(0);
 	i = INT2FIX(0);
 	if (!NIL_P(sr)) {
-	    if (f_include_p(sr, a_slash))
+	    if (strchr(RSTRING_PTR(sr), '/'))
 		r = f_to_r(sr);
-	    else if (f_gt_p(f_count(sr, a_dot_and_an_e), INT2FIX(0)))
+	    else if (strchr(RSTRING_PTR(sr), '.'))
 		r = f_to_f(sr);
 	    else
 		r = f_to_i(sr);
 	}
 	if (!NIL_P(si)) {
-	    if (f_include_p(si, a_slash))
+	    if (strchr(RSTRING_PTR(si), '/'))
 		i = f_to_r(si);
-	    else if (f_gt_p(f_count(si, a_dot_and_an_e), INT2FIX(0)))
+	    else if (strchr(RSTRING_PTR(si), '.'))
 		i = f_to_f(si);
 	    else
 		i = f_to_i(si);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 31329)
+++ ChangeLog	(revision 31330)
@@ -1,3 +1,7 @@
+Sun Apr 24 22:19:05 2011  Tadayoshi Funaba  <tadf@d...>
+
+	* complex.c, rational.c: omitted some method calls.
+
 Sun Apr 24 02:57:27 2011  Tadayoshi Funaba  <tadf@d...>
 
 	* ext/date/date_parse.c (n2i): takes long.
Index: rational.c
===================================================================
--- rational.c	(revision 31329)
+++ rational.c	(revision 31330)
@@ -1,5 +1,5 @@
 /*
-  rational.c: Coded by Tadayoshi Funaba 2008-2010
+  rational.c: Coded by Tadayoshi Funaba 2008-2011
 
   This implementation is based on Keiju Ishitsuka's Rational library
   which is written in ruby.
@@ -136,8 +136,22 @@
 fun1(inspect)
 fun1(integer_p)
 fun1(negate)
-fun1(to_f)
-fun1(to_i)
+
+inline static VALUE
+f_to_i(VALUE x)
+{
+    if (TYPE(x) == T_STRING)
+	return rb_str_to_inum(x, 10, 0);
+    return rb_funcall(x, id_to_i, 0);
+}
+inline static VALUE
+f_to_f(VALUE x)
+{
+    if (TYPE(x) == T_STRING)
+	return DBL2NUM(rb_str_to_dbl(x, 0));
+    return rb_funcall(x, id_to_f, 0);
+}
+
 fun1(to_s)
 fun1(truncate)
 
@@ -153,6 +167,8 @@
 fun2(fdiv)
 fun2(idiv)
 
+#define f_expt10(x) f_expt(INT2FIX(10), x)
+
 inline static VALUE
 f_negative_p(VALUE x)
 {
@@ -1208,7 +1224,7 @@
     if (!k_integer_p(n))
 	rb_raise(rb_eTypeError, "not an integer");
 
-    b = f_expt(INT2FIX(10), n);
+    b = f_expt10(n);
     s = f_mul(self, b);
 
     s = (*func)(s);
@@ -2035,25 +2051,37 @@
 	{
 	    VALUE a;
 
-	    a = f_split(nu, an_e_pat);
-	    ifp = RARRAY_PTR(a)[0];
-	    if (RARRAY_LEN(a) != 2)
+	    if (!strpbrk(RSTRING_PTR(nu), "eE")) {
+		ifp = nu; /* not a copy */
 		exp = Qnil;
-	    else
-		exp = RARRAY_PTR(a)[1];
+	    }
+	    else {
+		a = f_split(nu, an_e_pat);
+		ifp = RARRAY_PTR(a)[0];
+		if (RARRAY_LEN(a) != 2)
+		    exp = Qnil;
+		else
+		    exp = RARRAY_PTR(a)[1];
+	    }
 
-	    a = f_split(ifp, a_dot_pat);
-	    ip = RARRAY_PTR(a)[0];
-	    if (RARRAY_LEN(a) != 2)
+	    if (!strchr(RSTRING_PTR(ifp), '.')) {
+		ip = ifp; /* not a copy */
 		fp = Qnil;
-	    else
-		fp = RARRAY_PTR(a)[1];
+	    }
+	    else {
+		a = f_split(ifp, a_dot_pat);
+		ip = RARRAY_PTR(a)[0];
+		if (RARRAY_LEN(a) != 2)
+		    fp = Qnil;
+		else
+		    fp = RARRAY_PTR(a)[1];
+	    }
 	}
 
 	v = rb_rational_new1(f_to_i(ip));
 
 	if (!NIL_P(fp)) {
-	    char *p = StringValuePtr(fp);
+	    char *p = RSTRING_PTR(fp);
 	    long count = 0;
 	    VALUE l;
 
@@ -2062,16 +2090,15 @@
 		    count++;
 		p++;
 	    }
-
-	    l = f_expt(INT2FIX(10), LONG2NUM(count));
+	    l = f_expt10(LONG2NUM(count));
 	    v = f_mul(v, l);
 	    v = f_add(v, f_to_i(fp));
 	    v = f_div(v, l);
 	}
-	if (!NIL_P(si) && *StringValuePtr(si) == '-')
+	if (!NIL_P(si) && *RSTRING_PTR(si) == '-')
 	    v = f_negate(v);
 	if (!NIL_P(exp))
-	    v = f_mul(v, f_expt(INT2FIX(10), f_to_i(exp)));
+	    v = f_mul(v, f_expt10(f_to_i(exp)));
 #if 0
 	if (!NIL_P(de) && (!NIL_P(fp) || !NIL_P(exp)))
 	    return rb_assoc_new(v, rb_usascii_str_new2("dummy"));

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

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