ruby-changes:54018
From: nobu <ko1@a...>
Date: Thu, 6 Dec 2018 16:49:32 +0900 (JST)
Subject: [ruby-changes:54018] nobu:r66238 (trunk): Prefer rb_check_arity when 0 or 1 arguments
nobu 2018-12-06 16:49:24 +0900 (Thu, 06 Dec 2018) New Revision: 66238 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66238 Log: Prefer rb_check_arity when 0 or 1 arguments Especially over checking argc then calling rb_scan_args just to raise an ArgumentError. Modified files: trunk/enum.c trunk/enumerator.c trunk/error.c trunk/gc.c trunk/io.c trunk/iseq.c trunk/proc.c trunk/range.c trunk/rational.c trunk/signal.c trunk/string.c trunk/thread.c trunk/thread_sync.c trunk/time.c trunk/transcode.c trunk/variable.c trunk/vm.c trunk/vm_eval.c Index: vm.c =================================================================== --- vm.c (revision 66237) +++ vm.c (revision 66238) @@ -422,7 +422,8 @@ vm_stat(int argc, VALUE *argv, VALUE sel https://github.com/ruby/ruby/blob/trunk/vm.c#L422 VALUE arg = Qnil; VALUE hash = Qnil, key = Qnil; - if (rb_scan_args(argc, argv, "01", &arg) == 1) { + if (rb_check_arity(argc, 0, 1) == 1) { + arg = argv[0]; if (SYMBOL_P(arg)) key = arg; else if (RB_TYPE_P(arg, T_HASH)) Index: transcode.c =================================================================== --- transcode.c (revision 66237) +++ transcode.c (revision 66238) @@ -4115,10 +4115,9 @@ econv_putback(int argc, VALUE *argv, VAL https://github.com/ruby/ruby/blob/trunk/transcode.c#L4115 int putbackable; VALUE str, max; - rb_scan_args(argc, argv, "01", &max); - - if (NIL_P(max)) + if (!rb_check_arity(argc, 0, 1) || NIL_P(max = argv[0])) { n = rb_econv_putbackable(ec); + } else { n = NUM2INT(max); putbackable = rb_econv_putbackable(ec); Index: variable.c =================================================================== --- variable.c (revision 66237) +++ variable.c (revision 66238) @@ -2728,16 +2728,11 @@ rb_const_list(void *data) https://github.com/ruby/ruby/blob/trunk/variable.c#L2728 VALUE rb_mod_constants(int argc, const VALUE *argv, VALUE mod) { - VALUE inherit; + bool inherit = TRUE; - if (argc == 0) { - inherit = Qtrue; - } - else { - rb_scan_args(argc, argv, "01", &inherit); - } + if (rb_check_arity(argc, 0, 1)) inherit = RTEST(argv[0]); - if (RTEST(inherit)) { + if (inherit) { return rb_const_list(rb_mod_const_of(mod, 0)); } else { @@ -3294,16 +3289,11 @@ cvar_list(void *data) https://github.com/ruby/ruby/blob/trunk/variable.c#L3289 VALUE rb_mod_class_variables(int argc, const VALUE *argv, VALUE mod) { - VALUE inherit; + bool inherit = TRUE; st_table *tbl; - if (argc == 0) { - inherit = Qtrue; - } - else { - rb_scan_args(argc, argv, "01", &inherit); - } - if (RTEST(inherit)) { + if (rb_check_arity(argc, 0, 1)) inherit = RTEST(argv[0]); + if (inherit) { tbl = mod_cvar_of(mod, 0); } else { Index: iseq.c =================================================================== --- iseq.c (revision 66237) +++ iseq.c (revision 66238) @@ -3183,8 +3183,7 @@ rb_iseqw_local_variables(VALUE iseqval) https://github.com/ruby/ruby/blob/trunk/iseq.c#L3183 static VALUE iseqw_to_binary(int argc, VALUE *argv, VALUE self) { - VALUE opt; - rb_scan_args(argc, argv, "01", &opt); + VALUE opt = !rb_check_arity(argc, 0, 1) ? Qnil : argv[0]; return rb_iseq_ibf_dump(iseqw_check(self), opt); } Index: enum.c =================================================================== --- enum.c (revision 66237) +++ enum.c (revision 66238) @@ -2009,11 +2009,11 @@ enum_min_by(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/enum.c#L2009 struct MEMO *memo; VALUE num; - rb_scan_args(argc, argv, "01", &num); + rb_check_arity(argc, 0, 1); RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size); - if (!NIL_P(num)) + if (argc && !NIL_P(num = argv[0])) return rb_nmin_run(obj, num, 1, 0, 0); memo = MEMO_NEW(Qundef, Qnil, 0); @@ -2116,11 +2116,11 @@ enum_max_by(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/enum.c#L2116 struct MEMO *memo; VALUE num; - rb_scan_args(argc, argv, "01", &num); + rb_check_arity(argc, 0, 1); RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size); - if (!NIL_P(num)) + if (argc && !NIL_P(num = argv[0])) return rb_nmin_run(obj, num, 1, 1, 0); memo = MEMO_NEW(Qundef, Qnil, 0); @@ -2971,10 +2971,10 @@ enum_cycle(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/enum.c#L2971 VALUE nv = Qnil; long n, i, len; - rb_scan_args(argc, argv, "01", &nv); + rb_check_arity(argc, 0, 1); RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_cycle_size); - if (NIL_P(nv)) { + if (!argc || NIL_P(nv = argv[0])) { n = -1; } else { @@ -3944,11 +3944,8 @@ enum_sum(int argc, VALUE* argv, VALUE ob https://github.com/ruby/ruby/blob/trunk/enum.c#L3944 VALUE beg, end; int excl; - if (rb_scan_args(argc, argv, "01", &memo.v) == 0) - memo.v = LONG2FIX(0); - + memo.v = (rb_check_arity(argc, 0, 1) == 0) ? LONG2FIX(0) : argv[0]; memo.block_given = rb_block_given_p(); - memo.n = 0; memo.r = Qundef; Index: proc.c =================================================================== --- proc.c (revision 66237) +++ proc.c (revision 66238) @@ -2993,8 +2993,7 @@ proc_curry(int argc, const VALUE *argv, https://github.com/ruby/ruby/blob/trunk/proc.c#L2993 int sarity, max_arity, min_arity = rb_proc_min_max_arity(self, &max_arity); VALUE arity; - rb_scan_args(argc, argv, "01", &arity); - if (NIL_P(arity)) { + if (rb_check_arity(argc, 0, 1) == 0 || NIL_P(arity = argv[0])) { arity = INT2FIX(min_arity); } else { Index: signal.c =================================================================== --- signal.c (revision 66237) +++ signal.c (revision 66238) @@ -391,7 +391,7 @@ interrupt_init(int argc, VALUE *argv, VA https://github.com/ruby/ruby/blob/trunk/signal.c#L391 VALUE args[2]; args[0] = INT2FIX(SIGINT); - rb_scan_args(argc, argv, "01", &args[1]); + args[1] = rb_check_arity(argc, 0, 1) ? argv[0] : Qnil; return rb_call_super(2, args); } Index: range.c =================================================================== --- range.c (revision 66237) +++ range.c (revision 66238) @@ -397,12 +397,7 @@ range_step(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/range.c#L397 b = RANGE_BEG(range); e = RANGE_END(range); - if (argc == 0) { - step = INT2FIX(1); - } - else { - rb_scan_args(argc, argv, "01", &step); - } + step = (!rb_check_arity(argc, 0, 1) ? INT2FIX(1) : argv[0]); if (!rb_block_given_p()) { if (rb_obj_is_kind_of(b, rb_cNumeric) && (NIL_P(e) || rb_obj_is_kind_of(e, rb_cNumeric))) { Index: enumerator.c =================================================================== --- enumerator.c (revision 66237) +++ enumerator.c (revision 66238) @@ -604,12 +604,9 @@ enumerator_with_index(int argc, VALUE *a https://github.com/ruby/ruby/blob/trunk/enumerator.c#L604 { VALUE memo; - rb_scan_args(argc, argv, "01", &memo); + rb_check_arity(argc, 0, 1); RETURN_SIZED_ENUMERATOR(obj, argc, argv, enumerator_enum_size); - if (NIL_P(memo)) - memo = INT2FIX(0); - else - memo = rb_to_int(memo); + memo = (!argc || NIL_P(memo = argv[0])) ? INT2FIX(0) : rb_to_int(memo); return enumerator_block_call(obj, enumerator_with_index_i, (VALUE)MEMO_NEW(memo, 0, 0)); } Index: string.c =================================================================== --- string.c (revision 66237) +++ string.c (revision 66238) @@ -5724,16 +5724,9 @@ rb_str_include(VALUE str, VALUE arg) https://github.com/ruby/ruby/blob/trunk/string.c#L5724 static VALUE rb_str_to_i(int argc, VALUE *argv, VALUE str) { - int base; + int base = 10; - if (argc == 0) base = 10; - else { - VALUE b; - - rb_scan_args(argc, argv, "01", &b); - base = NUM2INT(b); - } - if (base < 0) { + if (rb_check_arity(argc, 0, 1) && (base = NUM2INT(argv[0])) < 0) { rb_raise(rb_eArgError, "invalid radix %d", base); } return rb_str_to_inum(str, base, FALSE); @@ -9369,20 +9362,14 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/string.c#L9362 rb_str_sum(int argc, VALUE *argv, VALUE str) { VALUE vbits; - int bits; + int bits = 16; char *ptr, *p, *pend; long len; VALUE sum = INT2FIX(0); unsigned long sum0 = 0; - if (argc == 0) { - bits = 16; - } - else { - rb_scan_args(argc, argv, "01", &vbits); - bits = NUM2INT(vbits); - if (bits < 0) - bits = 0; + if (rb_check_arity(argc, 0, 1) && (bits = NUM2INT(argv[0])) < 0) { + bits = 0; } ptr = p = RSTRING_PTR(str); len = RSTRING_LEN(str); @@ -10397,7 +10384,7 @@ unicode_normalize_common(int argc, VALUE https://github.com/ruby/ruby/blob/trunk/string.c#L10384 UnicodeNormalizeRequired = 1; } argv2[0] = str; - rb_scan_args(argc, argv, "01", &argv2[1]); + if (rb_check_arity(argc, 0, 1)) argv2[1] = argv[0]; return rb_funcallv(mUnicodeNormalize, id, argc+1, argv2); } Index: io.c =================================================================== --- io.c (revision 66237) +++ io.c (revision 66238) @@ -7878,12 +7878,7 @@ rb_obj_display(int argc, VALUE *argv, VA https://github.com/ruby/ruby/blob/trunk/io.c#L7878 { VALUE out; - if (argc == 0) { - out = rb_stdout; - } - else { - rb_scan_args(argc, argv, "01", &out); - } + out = (!rb_check_arity(argc, 0, 1) ? rb_stdout : argv[0]); rb_io_write(out, self); return Qnil; Index: error.c =================================================================== --- error.c (revision 66237) +++ error.c (revision 66238) @@ -946,7 +946,7 @@ exc_initialize(int argc, VALUE *argv, VA https://github.com/ruby/ruby/blob/trunk/error.c#L946 { VALUE arg; - rb_scan_args(argc, argv, "01", &arg); + arg = (!rb_check_arity(argc, 0, 1) ? Qnil : argv[0]); return exc_init(exc, arg); } Index: thread_sync.c =================================================================== --- thread_sync.c (revision 66237) +++ thread_sync.c (revision 66238) @@ -517,7 +517,7 @@ mutex_sleep(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/thread_sync.c#L517 { VALUE timeout; - rb_scan_args(argc, argv, "01", &timeout); + timeout = rb_check_arity(argc, 0, 1) ? argv[0] : Qnil; return rb_mutex_sleep(self, timeout); } Index: thread.c =================================================================== --- thread.c (revision 66237) +++ thread.c (revision 66238) @@ -1111,19 +1111,18 @@ thread_join_m(int argc, VALUE *argv, VAL https://github.com/ruby/ruby/blob/trunk/thread.c#L1111 VALUE limit; rb_hrtime_t rel, *to = 0; - rb_scan_args(argc, argv, "01", &limit); - /* * This supports INFINITY and negative values, so we can't use * rb_time_interval right now... */ - switch (TYPE(limit)) { - case T_NIL: break; - case T_FIXNUM: + if (!rb_check_arity(argc, 0, 1) || NIL_P(argv[0])) { + /* unlimited */ + } + else if (FIXNUM_P(limit = argv[0])) { rel = rb_sec2hrtime(NUM2TIMET(limit)); to = &rel; - break; - default: + } + else { to = double2hrtime(&rel, rb_num2dbl(limit)); } @@ -2041,20 +2040,19 @@ rb_thread_pending_interrupt_p(int argc, https://github.com/ruby/ruby/blob/trunk/thread.c#L2040 if (rb_threadptr_pending_interrupt_empty_p(target_th)) { return Qfalse; } + if (rb_check_arity(argc, 0, 1)) { + VALUE err = argv[0]; + if (!rb_obj_is_kind_of(err, rb_cModule)) { + rb_raise(rb_eTypeError, "class or module required for rescue clause"); + } + if (rb_threadptr_pending_interrupt_include_p(target_th, err)) { + return Qtrue; + } + else { + return Qfalse; + } + } else { - if (argc == 1) { - VALUE err; - rb_scan_args(argc, argv, "01", &err); - if (!rb_obj_is_kind_of(err, rb_cModule)) { - rb_raise(rb_eTypeError, "class or module required for rescue clause"); - } - if (rb_threadptr_pending_interrupt_include_p(target_th, err)) { - return Qtrue; - } - else { - return Qfalse; - } - } return Qtrue; } } Index: rational.c =================================================================== --- rational.c (revision 66237) +++ rational.c (revision 66238) @@ -1385,10 +1385,10 @@ f_round_common(int argc, VALUE *argv, VA https://github.com/ruby/ruby/blob/trunk/rational.c#L1385 { VALUE n, b, s; - if (argc == 0) + if (rb_check_arity(argc, 0, 1) == 0) return (*func)(self); - rb_scan_args(argc, argv, "01", &n); + n = argv[0]; if (!k_integer_p(n)) rb_raise(rb_eTypeError, "not an integer"); @@ -1711,14 +1711,13 @@ nurat_rationalize(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/rational.c#L1711 { VALUE e, a, b, p, q; - if (argc == 0) + if (rb_check_arity(argc, 0, 1) == 0) return self; if (nurat_negative_p(self)) return rb_rational_uminus(nurat_rationalize(argc, argv, rb_rational_uminus(self))); - rb_scan_args(argc, argv, "01", &e); - e = f_abs(e); + e = f_abs(argv[0]); a = f_sub(self, e); b = f_add(self, e); @@ -2283,16 +2282,13 @@ rb_flt_rationalize(VALUE flt) https://github.com/ruby/ruby/blob/trunk/rational.c#L2282 static VALUE float_rationalize(int argc, VALUE *argv, VALUE self) { - VALUE e; double d = RFLOAT_VALUE(self); if (d < 0.0) return rb_rational_uminus(float_rationalize(argc, argv, DBL2NUM(-d))); - rb_scan_args(argc, argv, "01", &e); - - if (argc != 0) { - return rb_flt_rationalize_with_prec(self, e); + if (rb_check_arity(argc, 0, 1)) { + return rb_flt_rationalize_with_prec(self, argv[0]); } else { return rb_flt_rationalize(self); Index: gc.c =================================================================== --- gc.c (revision 66237) +++ gc.c (revision 66238) @@ -2722,12 +2722,7 @@ os_each_obj(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/gc.c#L2722 { VALUE of; - if (argc == 0) { - of = 0; - } - else { - rb_scan_args(argc, argv, "01", &of); - } + of = (!rb_check_arity(argc, 0, 1) ? 0 : argv[0]); RETURN_ENUMERATOR(os, 1, &of); return os_obj_of(of); } @@ -3487,9 +3482,10 @@ count_objects(int argc, VALUE *argv, VAL https://github.com/ruby/ruby/blob/trunk/gc.c#L3482 size_t freed = 0; size_t total = 0; size_t i; - VALUE hash; + VALUE hash = Qnil; - if (rb_scan_args(argc, argv, "01", &hash) == 1) { + if (rb_check_arity(argc, 0, 1) == 1) { + hash = argv[0]; if (!RB_TYPE_P(hash, T_HASH)) rb_raise(rb_eTypeError, "non-hash given"); } @@ -7077,13 +7073,13 @@ gc_latest_gc_info(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/gc.c#L7073 rb_objspace_t *objspace = &rb_objspace; VALUE arg = Qnil; - if (rb_scan_args(argc, argv, "01", &arg) == 1) { + if (rb_check_arity(argc, 0, 1) == 1) { + arg = argv[0]; if (!SYMBOL_P(arg) && !RB_TYPE_P(arg, T_HASH)) { rb_raise(rb_eTypeError, "non-hash or symbol given"); } } - - if (arg == Qnil) { + else { arg = rb_hash_new(); } @@ -7452,7 +7448,8 @@ gc_stat(int argc, VALUE *argv, VALUE sel https://github.com/ruby/ruby/blob/trunk/gc.c#L7448 { VALUE arg = Qnil; - if (rb_scan_args(argc, argv, "01", &arg) == 1) { + if (rb_check_arity(argc, 0, 1) == 1) { + arg = argv[0]; if (SYMBOL_P(arg)) { size_t value = gc_stat_internal(arg); return SIZET2NUM(value); @@ -7461,8 +7458,7 @@ gc_stat(int argc, VALUE *argv, VALUE sel https://github.com/ruby/ruby/blob/trunk/gc.c#L7458 rb_raise(rb_eTypeError, "non-hash or symbol given"); } } - - if (arg == Qnil) { + else { arg = rb_hash_new(); } gc_stat_internal(arg); @@ -9455,12 +9451,7 @@ gc_profile_report(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/gc.c#L9451 { VALUE out; - if (argc == 0) { - out = rb_stdout; - } - else { - rb_scan_args(argc, argv, "01", &out); - } + out = (!rb_check_arity(argc, 0, 1) ? rb_stdout : argv[0]); gc_profile_dump_on(out, rb_io_write); return Qnil; Index: vm_eval.c =================================================================== --- vm_eval.c (revision 66237) +++ vm_eval.c (revision 66238) @@ -1949,16 +1949,9 @@ catch_i(VALUE tag, VALUE data) https://github.com/ruby/ruby/blob/trunk/vm_eval.c#L1949 */ static VALUE -rb_f_catch(int argc, VALUE *argv) +rb_f_catch(int argc, VALUE *argv, VALUE self) { - VALUE tag; - - if (argc == 0) { - tag = rb_obj_alloc(rb_cObject); - } - else { - rb_scan_args(argc, argv, "01", &tag); - } + VALUE tag = rb_check_arity(argc, 0, 1) ? argv[0] : rb_obj_alloc(rb_cObject); return rb_catch_obj(tag, catch_i, 0); } Index: time.c =================================================================== --- time.c (revision 66237) +++ time.c (revision 66238) @@ -3764,9 +3764,8 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/time.c#L3764 time_localtime_m(int argc, VALUE *argv, VALUE time) { VALUE off; - rb_scan_args(argc, argv, "01", &off); - if (!NIL_P(off)) { + if (rb_check_arity(argc, 0, 1) && !NIL_P(off = argv[0])) { return time_zonelocal(time, off); } @@ -3881,9 +3880,8 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/time.c#L3880 time_getlocaltime(int argc, VALUE *argv, VALUE time) { VALUE off; - rb_scan_args(argc, argv, "01", &off); - if (!NIL_P(off)) { + if (rb_check_arity(argc, 0, 1) && !NIL_P(off = argv[0])) { VALUE zone = off; if (maybe_tzobj_p(zone)) { VALUE t = time_dup(time); @@ -4136,9 +4134,7 @@ time_round(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/time.c#L4134 long nd; struct time_object *tobj; - rb_scan_args(argc, argv, "01", &ndigits); - - if (NIL_P(ndigits)) + if (!rb_check_arity(argc, 0, 1) || NIL_P(ndigits = argv[0])) ndigits = INT2FIX(0); else ndigits = rb_to_int(ndigits); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/