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

ruby-changes:43672

From: nobu <ko1@a...>
Date: Mon, 25 Jul 2016 21:33:23 +0900 (JST)
Subject: [ruby-changes:43672] nobu:r55745 (trunk): range.c: check_step_domain

nobu	2016-07-25 21:33:15 +0900 (Mon, 25 Jul 2016)

  New Revision: 55745

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

  Log:
    range.c: check_step_domain
    
    * range.c (check_step_domain): check step argument domain by <=>
      method, instead of < and >.

  Modified files:
    trunk/ChangeLog
    trunk/range.c
Index: range.c
===================================================================
--- range.c	(revision 55744)
+++ range.c	(revision 55745)
@@ -334,22 +334,31 @@ linear_object_p(VALUE obj) https://github.com/ruby/ruby/blob/trunk/range.c#L334
 }
 
 static VALUE
-range_step_size(VALUE range, VALUE args, VALUE eobj)
+check_step_domain(VALUE step)
 {
-    VALUE b = RANGE_BEG(range), e = RANGE_END(range);
-    VALUE step = INT2FIX(1);
-    if (args) {
-	step = RARRAY_AREF(args, 0);
-	if (!rb_obj_is_kind_of(step, rb_cNumeric)) {
-	    step = rb_to_int(step);
-	}
+    VALUE zero = INT2FIX(0);
+    int cmp;
+    if (!rb_obj_is_kind_of(step, rb_cNumeric)) {
+	step = rb_to_int(step);
     }
-    if (rb_funcall(step, '<', 1, INT2FIX(0))) {
+    cmp = rb_cmpint(rb_funcallv(step, idCmp, 1, &zero), step, zero);
+    if (cmp < 0) {
 	rb_raise(rb_eArgError, "step can't be negative");
     }
-    else if (!rb_funcall(step, '>', 1, INT2FIX(0))) {
+    else if (cmp == 0) {
 	rb_raise(rb_eArgError, "step can't be 0");
     }
+    return step;
+}
+
+static VALUE
+range_step_size(VALUE range, VALUE args, VALUE eobj)
+{
+    VALUE b = RANGE_BEG(range), e = RANGE_END(range);
+    VALUE step = INT2FIX(1);
+    if (args) {
+	step = check_step_domain(RARRAY_AREF(args, 0));
+    }
 
     if (rb_obj_is_kind_of(b, rb_cNumeric) && rb_obj_is_kind_of(e, rb_cNumeric)) {
 	return ruby_num_interval_step_size(b, e, step, EXCL(range));
@@ -405,15 +414,7 @@ range_step(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/range.c#L414
     }
     else {
 	rb_scan_args(argc, argv, "01", &step);
-	if (!rb_obj_is_kind_of(step, rb_cNumeric)) {
-	    step = rb_to_int(step);
-	}
-	if (rb_funcall(step, '<', 1, INT2FIX(0))) {
-	    rb_raise(rb_eArgError, "step can't be negative");
-	}
-	else if (!rb_funcall(step, '>', 1, INT2FIX(0))) {
-	    rb_raise(rb_eArgError, "step can't be 0");
-	}
+	step = check_step_domain(step);
     }
 
     if (FIXNUM_P(b) && FIXNUM_P(e) && FIXNUM_P(step)) { /* fixnums are special */
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 55744)
+++ ChangeLog	(revision 55745)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Jul 25 21:33:13 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* range.c (check_step_domain): check step argument domain by <=>
+	  method, instead of < and >.
+
 Mon Jul 25 21:11:32 2016  Kazuhiro NISHIYAMA  <zn@m...>
 
 	* doc/maintainers.rdoc: fix filenames.

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

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