ruby-changes:53985
From: nobu <ko1@a...>
Date: Wed, 5 Dec 2018 10:09:50 +0900 (JST)
Subject: [ruby-changes:53985] nobu:r66205 (trunk): Prefer rb_check_arity when 0 or 1 arguments
nobu 2018-12-05 10:09:44 +0900 (Wed, 05 Dec 2018) New Revision: 66205 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66205 Log: Prefer rb_check_arity when 0 or 1 arguments Modified files: trunk/array.c trunk/class.c trunk/dir.c trunk/enum.c trunk/object.c Index: enum.c =================================================================== --- enum.c (revision 66204) +++ enum.c (revision 66205) @@ -294,7 +294,7 @@ enum_find(int argc, VALUE *argv, VALUE o https://github.com/ruby/ruby/blob/trunk/enum.c#L294 struct MEMO *memo; VALUE if_none; - rb_scan_args(argc, argv, "01", &if_none); + if_none = rb_check_arity(argc, 0, 1) ? argv[0] : Qnil; RETURN_ENUMERATOR(obj, argc, argv); memo = MEMO_NEW(Qundef, 0, 0); rb_block_call(obj, id_each, 0, 0, find_i, (VALUE)memo); @@ -1699,9 +1699,7 @@ enum_min(int argc, VALUE *argv, VALUE ob https://github.com/ruby/ruby/blob/trunk/enum.c#L1699 VALUE result; VALUE num; - rb_scan_args(argc, argv, "01", &num); - - if (!NIL_P(num)) + if (rb_check_arity(argc, 0, 1) && !NIL_P(num = argv[0])) return rb_nmin_run(obj, num, 0, 0, 0); m->min = Qundef; @@ -1793,9 +1791,7 @@ enum_max(int argc, VALUE *argv, VALUE ob https://github.com/ruby/ruby/blob/trunk/enum.c#L1791 VALUE result; VALUE num; - rb_scan_args(argc, argv, "01", &num); - - if (!NIL_P(num)) + if (rb_check_arity(argc, 0, 1) && !NIL_P(num = argv[0])) return rb_nmin_run(obj, num, 0, 1, 0); m->max = Qundef; Index: array.c =================================================================== --- array.c (revision 66204) +++ array.c (revision 66205) @@ -2351,8 +2351,12 @@ rb_ary_join_m(int argc, VALUE *argv, VAL https://github.com/ruby/ruby/blob/trunk/array.c#L2351 { VALUE sep; - rb_scan_args(argc, argv, "01", &sep); - if (NIL_P(sep)) sep = rb_output_fs; + if (rb_check_arity(argc, 0, 1) == 0) { + sep = rb_output_fs; + } + else if (NIL_P(sep = argv[0])) { + sep = rb_output_fs; + } return rb_ary_join(ary, sep); } @@ -3448,10 +3452,7 @@ rb_ary_slice_bang(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/array.c#L3452 return arg2; } - if (argc != 1) { - /* error report */ - rb_scan_args(argc, argv, "11", NULL, NULL); - } + rb_check_arity(argc, 1, 2); arg1 = argv[0]; if (!FIXNUM_P(arg1)) { @@ -4735,9 +4736,7 @@ rb_ary_max(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L4736 VALUE num; long i; - rb_scan_args(argc, argv, "01", &num); - - if (!NIL_P(num)) + if (rb_check_arity(argc, 0, 1) && !NIL_P(num = argv[0])) return rb_nmin_run(ary, num, 0, 1, 1); if (rb_block_given_p()) { @@ -4790,9 +4789,7 @@ rb_ary_min(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L4789 VALUE num; long i; - rb_scan_args(argc, argv, "01", &num); - - if (!NIL_P(num)) + if (rb_check_arity(argc, 0, 1) && !NIL_P(num = argv[0])) return rb_nmin_run(ary, num, 0, 0, 1); if (rb_block_given_p()) { @@ -5001,7 +4998,7 @@ rb_ary_count(int argc, VALUE *argv, VALU https://github.com/ruby/ruby/blob/trunk/array.c#L4998 { long i, n = 0; - if (argc == 0) { + if (rb_check_arity(argc, 0, 1) == 0) { VALUE v; if (!rb_block_given_p()) @@ -5013,9 +5010,8 @@ rb_ary_count(int argc, VALUE *argv, VALU https://github.com/ruby/ruby/blob/trunk/array.c#L5010 } } else { - VALUE obj; + VALUE obj = argv[0]; - rb_scan_args(argc, argv, "1", &obj); if (rb_block_given_p()) { rb_warn("given block not used"); } @@ -5111,7 +5107,7 @@ rb_ary_flatten_bang(int argc, VALUE *arg https://github.com/ruby/ruby/blob/trunk/array.c#L5107 int mod = 0, level = -1; VALUE result, lv; - rb_scan_args(argc, argv, "01", &lv); + lv = (rb_check_arity(argc, 0, 1) ? argv[0] : Qnil); rb_ary_modify_check(ary); if (!NIL_P(lv)) level = NUM2INT(lv); if (level == 0) return Qnil; @@ -5154,11 +5150,12 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L5150 rb_ary_flatten(int argc, VALUE *argv, VALUE ary) { int mod = 0, level = -1; - VALUE result, lv; + VALUE result; - rb_scan_args(argc, argv, "01", &lv); - if (!NIL_P(lv)) level = NUM2INT(lv); - if (level == 0) return ary_make_shared_copy(ary); + if (rb_check_arity(argc, 0, 1) && !NIL_P(argv[0])) { + level = NUM2INT(argv[0]); + if (level == 0) return ary_make_shared_copy(ary); + } result = flatten(ary, level, &mod); OBJ_INFECT(result, ary); @@ -5295,7 +5292,7 @@ rb_ary_sample(int argc, VALUE *argv, VAL https://github.com/ruby/ruby/blob/trunk/array.c#L5292 } } len = RARRAY_LEN(ary); - if (argc == 0) { + if (rb_check_arity(argc, 0, 1) == 0) { if (len < 2) i = 0; else @@ -5303,7 +5300,7 @@ rb_ary_sample(int argc, VALUE *argv, VAL https://github.com/ruby/ruby/blob/trunk/array.c#L5300 return rb_ary_elt(ary, i); } - rb_scan_args(argc, argv, "1", &nv); + nv = argv[0]; n = NUM2LONG(nv); if (n < 0) rb_raise(rb_eArgError, "negative sample number"); if (n > len) n = len; @@ -5457,16 +5454,15 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L5454 rb_ary_cycle(int argc, VALUE *argv, VALUE ary) { long n, i; - VALUE nv = Qnil; - rb_scan_args(argc, argv, "01", &nv); + rb_check_arity(argc, 0, 1); RETURN_SIZED_ENUMERATOR(ary, argc, argv, rb_ary_cycle_size); - if (NIL_P(nv)) { + if (argc == 0 || NIL_P(argv[0])) { n = -1; } else { - n = NUM2LONG(nv); + n = NUM2LONG(argv[0]); if (n <= 0) return Qnil; } @@ -5628,13 +5624,13 @@ rb_ary_permutation_size(VALUE ary, VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L5624 static VALUE rb_ary_permutation(int argc, VALUE *argv, VALUE ary) { - VALUE num; long r, n, i; n = RARRAY_LEN(ary); /* Array length */ RETURN_SIZED_ENUMERATOR(ary, argc, argv, rb_ary_permutation_size); /* Return enumerator if no block */ - rb_scan_args(argc, argv, "01", &num); - r = NIL_P(num) ? n : NUM2LONG(num); /* Permutation size from argument */ + r = n; + if (rb_check_arity(argc, 0, 1) && !NIL_P(argv[0])) + r = NUM2LONG(argv[0]); /* Permutation size from argument */ if (r < 0 || n < r) { /* no permutations: yield nothing */ @@ -6306,8 +6302,7 @@ rb_ary_sum(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L6302 long i, n; int block_given; - if (rb_scan_args(argc, argv, "01", &v) == 0) - v = LONG2FIX(0); + v = (rb_check_arity(argc, 0, 1) ? argv[0] : LONG2FIX(0)); block_given = rb_block_given_p(); Index: class.c =================================================================== --- class.c (revision 66204) +++ class.c (revision 66205) @@ -1178,17 +1178,10 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/class.c#L1178 class_instance_method_list(int argc, const VALUE *argv, VALUE mod, int obj, int (*func) (st_data_t, st_data_t, st_data_t)) { VALUE ary; - int recur, prepended = 0; + int recur = TRUE, prepended = 0; struct method_entry_arg me_arg; - if (argc == 0) { - recur = TRUE; - } - else { - VALUE r; - rb_scan_args(argc, argv, "01", &r); - recur = RTEST(r); - } + if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]); if (!recur && RCLASS_ORIGIN(mod) != mod) { mod = RCLASS_ORIGIN(mod); @@ -1417,25 +1410,21 @@ rb_obj_public_methods(int argc, const VA https://github.com/ruby/ruby/blob/trunk/class.c#L1410 VALUE rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj) { - VALUE recur, ary, klass, origin; + VALUE ary, klass, origin; struct method_entry_arg me_arg; struct rb_id_table *mtbl; + int recur = TRUE; - if (argc == 0) { - recur = Qtrue; - } - else { - rb_scan_args(argc, argv, "01", &recur); - } + if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]); klass = CLASS_OF(obj); origin = RCLASS_ORIGIN(klass); me_arg.list = st_init_numtable(); - me_arg.recur = RTEST(recur); + me_arg.recur = recur; if (klass && FL_TEST(klass, FL_SINGLETON)) { if ((mtbl = RCLASS_M_TBL(origin)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg); klass = RCLASS_SUPER(klass); } - if (RTEST(recur)) { + if (recur) { while (klass && (FL_TEST(klass, FL_SINGLETON) || RB_TYPE_P(klass, T_ICLASS))) { if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg); klass = RCLASS_SUPER(klass); Index: object.c =================================================================== --- object.c (revision 66204) +++ object.c (revision 66205) @@ -2061,11 +2061,11 @@ rb_class_initialize(int argc, VALUE *arg https://github.com/ruby/ruby/blob/trunk/object.c#L2061 if (RCLASS_SUPER(klass) != 0 || klass == rb_cBasicObject) { rb_raise(rb_eTypeError, "already initialized class"); } - if (argc == 0) { + if (rb_check_arity(argc, 0, 1) == 0) { super = rb_cObject; } else { - rb_scan_args(argc, argv, "01", &super); + super = argv[0]; rb_check_inheritable(super); if (super != rb_cBasicObject && !RCLASS_SUPER(super)) { rb_raise(rb_eTypeError, "can't inherit uninitialized class"); Index: dir.c =================================================================== --- dir.c (revision 66204) +++ dir.c (revision 66205) @@ -1080,9 +1080,8 @@ dir_s_chdir(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/dir.c#L1080 { VALUE path = Qnil; - if (rb_scan_args(argc, argv, "01", &path) == 1) { - FilePathValue(path); - path = rb_str_encode_ospath(path); + if (rb_check_arity(argc, 0, 1) == 1) { + path = rb_str_encode_ospath(rb_get_path(argv[0])); } else { const char *dist = getenv("HOME"); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/