From 703a5dd3e0effdc808b9b59af21e5012f3d91942 Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 28 Apr 2018 11:16:54 +0000 Subject: [PATCH] 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63290 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- internal.h | 3 ++- range.c | 62 +++++++++++++++++++++++------------------------------- string.c | 18 +++++++--------- 3 files changed, 36 insertions(+), 47 deletions(-) diff --git a/internal.h b/internal.h index 3515767b36..85370ec0d7 100644 --- a/internal.h +++ b/internal.h @@ -2012,7 +2012,8 @@ VALUE rb_gcd_gmp(VALUE x, VALUE y); /* 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 */ diff --git a/range.c b/range.c index 88a35abc4e..7f59a97725 100644 --- a/range.c +++ b/range.c @@ -236,7 +236,7 @@ range_hash(VALUE range) } 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_call_func *func, VALUE arg) 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, arg)) 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)) rb_yield(i); iter[0] = iter[1]; } - return Qnil; + return 0; } static int @@ -426,17 +426,16 @@ range_step(int argc, VALUE *argv, VALUE range) } 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 range) 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) 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) } } 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 { diff --git a/string.c b/string.c index f989466d32..c48f63be16 100644 --- a/string.c +++ b/string.c @@ -4192,8 +4192,6 @@ all_digits_p(const char *s, long len) 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 beg) 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 excl, int (*each)(VALUE, VALUE), VALUE a } 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, VALUE (*each)(VALUE, VALUE), VALUE arg) 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, VALUE (*each)(VALUE, VALUE), VALUE arg) 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, VALUE (*each)(VALUE, VALUE), VALUE arg) 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 end, VALUE val, VALUE exclusive) } #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; }