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

ruby-changes:4420

From: ko1@a...
Date: Mon, 7 Apr 2008 02:12:09 +0900 (JST)
Subject: [ruby-changes:4420] matz - Ruby:r15911 (trunk): * bignum.c (rb_cmpint): moved from compar.c, to check bignum

matz	2008-04-07 02:11:50 +0900 (Mon, 07 Apr 2008)

  New Revision: 15911

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c
    trunk/compar.c
    trunk/range.c
    trunk/test/ruby/test_range.rb
    trunk/version.h

  Log:
    * bignum.c (rb_cmpint): moved from compar.c, to check bignum
      zero.
    
    * range.c (range_step): add step for each iteration if begin and
      end are numeric.  [ruby-core:15990]

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/version.h?r1=15911&r2=15910&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=15911&r2=15910&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/bignum.c?r1=15911&r2=15910&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_range.rb?r1=15911&r2=15910&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/range.c?r1=15911&r2=15910&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/compar.c?r1=15911&r2=15910&diff_format=u

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 15910)
+++ ChangeLog	(revision 15911)
@@ -22,6 +22,11 @@
 
 	* lib/webrick/httprequest.rb: Fix redefined method warning.
 
+Sat Apr  5 02:13:52 2008  Yukihiro Matsumoto  <matz@r...>
+
+	* bignum.c (rb_cmpint): moved from compar.c, to check bignum
+	  zero.
+
 Fri Apr  4 23:24:06 2008  NARUSE, Yui  <naruse@r...>
 
 	* re.c (rb_memsearch_qs): wrong boundary condition.
@@ -38,6 +43,11 @@
 	* lib/net/pop.rb (Net::POP3::do_finish): clear @n_mails and
 	  @n_bytes as well.  [ruby-core:16144]
 
+Fri Apr  4 01:59:30 2008  Yukihiro Matsumoto  <matz@r...>
+
+	* range.c (range_step): add step for each iteration if begin and
+	  end are numeric.  [ruby-core:15990]
+
 Fri Apr  4 00:42:26 2008  Yukihiro Matsumoto  <matz@r...>
 
 	* bignum.c (Init_Bignum): rdiv method removed.  [ruby-dev:34242]
Index: range.c
===================================================================
--- range.c	(revision 15910)
+++ range.c	(revision 15911)
@@ -223,10 +223,12 @@
 }
 
 static void
-range_each_func(VALUE range, VALUE (*func) (VALUE, void *), VALUE v, VALUE e,
-		void *arg)
+range_each_func(VALUE range, VALUE (*func) (VALUE, void *), void *arg)
 {
     int c;
+    VALUE b = RANGE_BEG(range);
+    VALUE e = RANGE_END(range);
+    VALUE v;
 
     if (EXCL(range)) {
 	while (r_lt(v, e)) {
@@ -267,7 +269,7 @@
  *     rng.step(n=1) {| obj | block }    => rng
  *  
  *  Iterates over <i>rng</i>, passing each <i>n</i>th element to the block. If
- *  the range contains numbers or strings, natural ordering is used.  Otherwise
+ *  the range contains numbers, <i>n</i> is added for each iteration.  Otherwise
  *  <code>step</code> invokes <code>succ</code> to iterate through range
  *  elements. The following code uses class <code>Xs</code>, which is defined
  *  in the class-level documentation.
@@ -293,8 +295,9 @@
 static VALUE
 range_step(int argc, VALUE *argv, VALUE range)
 {
-    VALUE b, e, step;
+    VALUE b, e, step, tmp, c;
     long unit;
+    int nv;
 
     RETURN_ENUMERATOR(range, argc, argv);
 
@@ -312,14 +315,15 @@
 	else {
 	    VALUE tmp = rb_to_int(step);
 	    unit = rb_cmpint(tmp, step, INT2FIX(0));
-	    step = tmp;
 	}
     }
     if (unit < 0) {
 	rb_raise(rb_eArgError, "step can't be negative");
     }
-    if (unit == 0)
+    if (unit == 0) {
 	rb_raise(rb_eArgError, "step can't be 0");
+    }
+
     if (FIXNUM_P(b) && FIXNUM_P(e) && FIXNUM_P(step)) { /* fixnums are special */
 	long end = FIX2LONG(e);
 	long i;
@@ -332,9 +336,20 @@
 	    if (i + unit < i) break;
 	    i += unit;
 	}
+
     }
+    else if (rb_obj_is_kind_of(b, rb_cNumeric) ||
+	     !NIL_P(rb_check_to_integer(b, "to_int")) ||
+	     !NIL_P(rb_check_to_integer(e, "to_int"))) {
+	ID op = EXCL(range) ? '<' : rb_intern("<=");
+
+	while (RTEST(rb_funcall(b, op, 1, e))) {
+	    rb_yield(b);
+	    b = rb_funcall(b, '+', 1, step);
+	}
+    }
     else {
-	VALUE tmp = rb_check_string_type(b);
+	tmp = rb_check_string_type(b);
 
 	if (!NIL_P(tmp)) {
 	    VALUE args[2], iter[2];
@@ -365,7 +380,7 @@
 	    }
 	    args[0] = INT2FIX(1);
 	    args[1] = step;
-	    range_each_func(range, step_i, b, e, args);
+	    range_each_func(range, step_i, args);
 	}
     }
     return range;
@@ -428,7 +443,7 @@
 	rb_block_call(beg, rb_intern("upto"), 2, args, rb_yield, 0);
     }
     else {
-	range_each_func(range, each_i, beg, end, NULL);
+	range_each_func(range, each_i, NULL);
     }
     return range;
 }
Index: compar.c
===================================================================
--- compar.c	(revision 15910)
+++ compar.c	(revision 15911)
@@ -15,22 +15,6 @@
 
 static ID cmp;
 
-int
-rb_cmpint(VALUE val, VALUE a, VALUE b)
-{
-    if (NIL_P(val)) {
-	rb_cmperr(a, b);
-    }
-    if (FIXNUM_P(val)) return FIX2INT(val);
-    if (TYPE(val) == T_BIGNUM) {
-	if (RBIGNUM_SIGN(val)) return 1;
-	return -1;
-    }
-    if (RTEST(rb_funcall(val, '>', 1, INT2FIX(0)))) return 1;
-    if (RTEST(rb_funcall(val, '<', 1, INT2FIX(0)))) return -1;
-    return 0;
-}
-
 void
 rb_cmperr(VALUE x, VALUE y)
 {
Index: version.h
===================================================================
--- version.h	(revision 15910)
+++ version.h	(revision 15911)
@@ -1,7 +1,7 @@
 #define RUBY_VERSION "1.9.0"
-#define RUBY_RELEASE_DATE "2008-04-06"
+#define RUBY_RELEASE_DATE "2008-04-07"
 #define RUBY_VERSION_CODE 190
-#define RUBY_RELEASE_CODE 20080406
+#define RUBY_RELEASE_CODE 20080407
 #define RUBY_PATCHLEVEL 0
 
 #define RUBY_VERSION_MAJOR 1
@@ -9,7 +9,7 @@
 #define RUBY_VERSION_TEENY 0
 #define RUBY_RELEASE_YEAR 2008
 #define RUBY_RELEASE_MONTH 4
-#define RUBY_RELEASE_DAY 6
+#define RUBY_RELEASE_DAY 7
 
 #ifdef RUBY_EXTERN
 RUBY_EXTERN const char ruby_version[];
Index: bignum.c
===================================================================
--- bignum.c	(revision 15910)
+++ bignum.c	(revision 15911)
@@ -50,6 +50,23 @@
     return 1;
 }
 
+int
+rb_cmpint(VALUE val, VALUE a, VALUE b)
+{
+    if (NIL_P(val)) {
+	rb_cmperr(a, b);
+    }
+    if (FIXNUM_P(val)) return FIX2INT(val);
+    if (TYPE(val) == T_BIGNUM) {
+	if (BIGZEROP(val)) return 0;
+	if (RBIGNUM_SIGN(val)) return 1;
+	return -1;
+    }
+    if (RTEST(rb_funcall(val, '>', 1, INT2FIX(0)))) return 1;
+    if (RTEST(rb_funcall(val, '<', 1, INT2FIX(0)))) return -1;
+    return 0;
+}
+
 #define RBIGNUM_SET_LEN(b,l) \
   ((RBASIC(b)->flags & RBIGNUM_EMBED_FLAG) ? \
    (RBASIC(b)->flags = (RBASIC(b)->flags & ~RBIGNUM_EMBED_LEN_MASK) | \
Index: test/ruby/test_range.rb
===================================================================
--- test/ruby/test_range.rb	(revision 15910)
+++ test/ruby/test_range.rb	(revision 15911)
@@ -114,12 +114,6 @@
     (0..10).step(2) {|x| a << x }
     assert_equal([0, 2, 4, 6, 8, 10], a)
 
-    o = Object.new
-    def o.to_int; 2; end
-    a = []
-    (0..10).step(o) {|x| a << x }
-    assert_equal([0, 2, 4, 6, 8, 10], a)
-
     assert_raise(ArgumentError) { (0..10).step(-1) { } }
     assert_raise(ArgumentError) { (0..10).step(0) { } }
 

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

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