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

ruby-changes:51083

From: nobu <ko1@a...>
Date: Sat, 28 Apr 2018 20:17:00 +0900 (JST)
Subject: [ruby-changes:51083] nobu:r63290 (trunk): string.c: adjust to rb_str_upto_each

nobu	2018-04-28 20:16:54 +0900 (Sat, 28 Apr 2018)

  New Revision: 63290

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

  Log:
    string.c: adjust to rb_str_upto_each
    
    * range.c (range_each_func): adjust the signature of the callback
      function to rb_str_upto_each, and exit the loop if the callback
      returned non-zero.
    
    * string.c (rb_str_upto_endless_each): ditto.

  Modified files:
    trunk/internal.h
    trunk/range.c
    trunk/string.c
Index: string.c
===================================================================
--- string.c	(revision 63289)
+++ string.c	(revision 63290)
@@ -4192,8 +4192,6 @@ all_digits_p(const char *s, long len) https://github.com/ruby/ruby/blob/trunk/string.c#L4192
     return 1;
 }
 
-static VALUE str_upto_each(VALUE beg, VALUE end, int excl, int (*each)(VALUE, VALUE), VALUE);
-
 static int
 str_upto_i(VALUE str, VALUE arg)
 {
@@ -4240,11 +4238,11 @@ rb_str_upto(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/string.c#L4238
 
     rb_scan_args(argc, argv, "11", &end, &exclusive);
     RETURN_ENUMERATOR(beg, argc, argv);
-    return str_upto_each(beg, end, RTEST(exclusive), str_upto_i, Qnil);
+    return rb_str_upto_each(beg, end, RTEST(exclusive), str_upto_i, Qnil);
 }
 
-static VALUE
-str_upto_each(VALUE beg, VALUE end, int excl, int (*each)(VALUE, VALUE), VALUE arg)
+VALUE
+rb_str_upto_each(VALUE beg, VALUE end, int excl, int (*each)(VALUE, VALUE), VALUE arg)
 {
     VALUE current, after_end;
     ID succ;
@@ -4326,7 +4324,7 @@ str_upto_each(VALUE beg, VALUE end, int https://github.com/ruby/ruby/blob/trunk/string.c#L4324
 }
 
 VALUE
-rb_str_upto_endless_each(VALUE beg, VALUE (*each)(VALUE, VALUE), VALUE arg)
+rb_str_upto_endless_each(VALUE beg, int (*each)(VALUE, VALUE), VALUE arg)
 {
     VALUE current;
     ID succ;
@@ -4343,7 +4341,7 @@ rb_str_upto_endless_each(VALUE beg, VALU https://github.com/ruby/ruby/blob/trunk/string.c#L4341
 	    rb_encoding *usascii = rb_usascii_encoding();
 
 	    while (FIXABLE(bi)) {
-		(*each)(rb_enc_sprintf(usascii, "%.*ld", width, bi), arg);
+		if ((*each)(rb_enc_sprintf(usascii, "%.*ld", width, bi), arg)) break;
 		bi++;
 	    }
 	    b = LONG2NUM(bi);
@@ -4351,7 +4349,7 @@ rb_str_upto_endless_each(VALUE beg, VALU https://github.com/ruby/ruby/blob/trunk/string.c#L4349
 	args[0] = INT2FIX(width);
 	while (1) {
 	    args[1] = b;
-	    (*each)(rb_str_format(numberof(args), args, fmt), arg);
+	    if ((*each)(rb_str_format(numberof(args), args, fmt), arg)) break;
 	    b = rb_funcallv(b, succ, 0, 0);
 	}
     }
@@ -4359,7 +4357,7 @@ rb_str_upto_endless_each(VALUE beg, VALU https://github.com/ruby/ruby/blob/trunk/string.c#L4357
     current = rb_str_dup(beg);
     while (1) {
 	VALUE next = rb_funcallv(current, succ, 0, 0);
-	(*each)(current, arg);
+	if ((*each)(current, arg)) break;
 	current = next;
 	StringValue(current);
 	if (RSTRING_LEN(current) == 0)
@@ -4417,7 +4415,7 @@ rb_str_include_range_p(VALUE beg, VALUE https://github.com/ruby/ruby/blob/trunk/string.c#L4415
 	}
 #endif
     }
-    str_upto_each(beg, end, RTEST(exclusive), include_range_i, (VALUE)&val);
+    rb_str_upto_each(beg, end, RTEST(exclusive), include_range_i, (VALUE)&val);
 
     return NIL_P(val) ? Qtrue : Qfalse;
 }
Index: range.c
===================================================================
--- range.c	(revision 63289)
+++ range.c	(revision 63290)
@@ -236,7 +236,7 @@ range_hash(VALUE range) https://github.com/ruby/ruby/blob/trunk/range.c#L236
 }
 
 static void
-range_each_func(VALUE range, rb_block_call_func *func, VALUE arg)
+range_each_func(VALUE range, int (*func)(VALUE, VALUE), VALUE arg)
 {
     int c;
     VALUE b = RANGE_BEG(range);
@@ -245,21 +245,21 @@ range_each_func(VALUE range, rb_block_ca https://github.com/ruby/ruby/blob/trunk/range.c#L245
 
     if (EXCL(range)) {
 	while (r_less(v, e) < 0) {
-	    (*func) (v, arg, 0, 0, 0);
+	    if ((*func)(v, arg)) break;
 	    v = rb_funcallv(v, id_succ, 0, 0);
 	}
     }
     else {
 	while ((c = r_less(v, e)) <= 0) {
-	    (*func) (v, arg, 0, 0, 0);
+	    if ((*func)(v, arg)) break;;
 	    if (!c) break;
 	    v = rb_funcallv(v, id_succ, 0, 0);
 	}
     }
 }
 
-static VALUE
-sym_step_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, arg))
+static int
+sym_step_i(VALUE i, VALUE arg)
 {
     VALUE *iter = (VALUE *)arg;
 
@@ -273,11 +273,11 @@ sym_step_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, https://github.com/ruby/ruby/blob/trunk/range.c#L273
 	rb_yield(rb_str_intern(i));
 	iter[0] = iter[1];
     }
-    return Qnil;
+    return 0;
 }
 
-static VALUE
-step_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, arg))
+static int
+step_i(VALUE i, VALUE arg)
 {
     VALUE *iter = (VALUE *)arg;
 
@@ -291,7 +291,7 @@ step_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, arg https://github.com/ruby/ruby/blob/trunk/range.c#L291
 	rb_yield(i);
 	iter[0] = iter[1];
     }
-    return Qnil;
+    return 0;
 }
 
 static int
@@ -426,17 +426,16 @@ range_step(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/range.c#L426
 
     }
     else if (SYMBOL_P(b) && (NIL_P(e) || SYMBOL_P(e))) { /* symbols are special */
-	VALUE args[2], iter[2];
+	VALUE iter[2];
 	iter[0] = INT2FIX(1);
 	iter[1] = step;
 
+	b = rb_sym2str(b);
 	if (NIL_P(e)) {
-	    rb_str_upto_endless_each(rb_sym2str(b), (VALUE (*)(VALUE, VALUE))sym_step_i, (VALUE)iter);
+	    rb_str_upto_endless_each(b, sym_step_i, (VALUE)iter);
 	}
 	else {
-	    args[0] = rb_sym2str(e);
-	    args[1] = EXCL(range) ? Qtrue : Qfalse;
-	    rb_block_call(rb_sym2str(b), rb_intern("upto"), 2, args, sym_step_i, (VALUE)iter);
+	    rb_str_upto_each(b, rb_sym2str(e), EXCL(range), sym_step_i, (VALUE)iter);
 	}
     }
     else if (ruby_float_step(b, e, step, EXCL(range), TRUE)) {
@@ -459,19 +458,17 @@ range_step(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/range.c#L458
 	tmp = rb_check_string_type(b);
 
 	if (!NIL_P(tmp)) {
-	    VALUE args[2], iter[2];
+	    VALUE iter[2];
 
 	    b = tmp;
 	    iter[0] = INT2FIX(1);
 	    iter[1] = step;
 
 	    if (NIL_P(e)) {
-		rb_str_upto_endless_each(b, (VALUE (*)(VALUE, VALUE))step_i, (VALUE)iter);
+		rb_str_upto_endless_each(b, step_i, (VALUE)iter);
 	    }
 	    else {
-		args[0] = e;
-		args[1] = EXCL(range) ? Qtrue : Qfalse;
-		rb_block_call(b, rb_intern("upto"), 2, args, step_i, (VALUE)iter);
+		rb_str_upto_each(b, e, EXCL(range), step_i, (VALUE)iter);
 	    }
 	}
 	else {
@@ -722,18 +719,18 @@ range_bsearch(VALUE range) https://github.com/ruby/ruby/blob/trunk/range.c#L719
     return range;
 }
 
-static VALUE
-each_i(RB_BLOCK_CALL_FUNC_ARGLIST(v, arg))
+static int
+each_i(VALUE v, VALUE arg)
 {
     rb_yield(v);
-    return Qnil;
+    return 0;
 }
 
-static VALUE
-sym_each_i(RB_BLOCK_CALL_FUNC_ARGLIST(v, arg))
+static int
+sym_each_i(VALUE v, VALUE arg)
 {
     rb_yield(rb_str_intern(v));
-    return Qnil;
+    return 0;
 }
 
 /*
@@ -817,25 +814,18 @@ range_each(VALUE range) https://github.com/ruby/ruby/blob/trunk/range.c#L814
 	}
     }
     else if (SYMBOL_P(beg) && SYMBOL_P(end)) { /* symbols are special */
-	VALUE args[2];
-
-	args[0] = rb_sym2str(end);
-	args[1] = EXCL(range) ? Qtrue : Qfalse;
-	rb_block_call(rb_sym2str(beg), rb_intern("upto"), 2, args, sym_each_i, 0);
+	beg = rb_sym2str(beg);
+	rb_str_upto_each(beg, rb_sym2str(end), EXCL(range), sym_each_i, 0);
     }
     else {
 	VALUE tmp = rb_check_string_type(beg);
 
 	if (!NIL_P(tmp)) {
 	    if (!NIL_P(end)) {
-		VALUE args[2];
-
-		args[0] = end;
-		args[1] = EXCL(range) ? Qtrue : Qfalse;
-		rb_block_call(tmp, rb_intern("upto"), 2, args, each_i, 0);
+		rb_str_upto_each(tmp, end, EXCL(range), each_i, 0);
 	    }
 	    else {
-		rb_str_upto_endless_each(tmp, (VALUE (*)(VALUE, VALUE))each_i, 0);
+		rb_str_upto_endless_each(tmp, each_i, 0);
 	    }
 	}
 	else {
Index: internal.h
===================================================================
--- internal.h	(revision 63289)
+++ internal.h	(revision 63290)
@@ -2012,7 +2012,8 @@ VALUE rb_gcd_gmp(VALUE x, VALUE y); https://github.com/ruby/ruby/blob/trunk/internal.h#L2012
 /* internal use */
 VALUE rb_setup_fake_str(struct RString *fake_str, const char *name, long len, rb_encoding *enc);
 #endif
-VALUE rb_str_upto_endless_each(VALUE, VALUE (*each)(VALUE, VALUE), VALUE);
+VALUE rb_str_upto_each(VALUE, VALUE, int, int (*each)(VALUE, VALUE), VALUE);
+VALUE rb_str_upto_endless_each(VALUE, int (*each)(VALUE, VALUE), VALUE);
 
 /* thread.c (export) */
 int ruby_thread_has_gvl_p(void); /* for ext/fiddle/closure.c */

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

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