ruby-changes:61306
From: =E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3 <ko1@a...>
Date: Thu, 21 May 2020 09:59:54 +0900 (JST)
Subject: [ruby-changes:61306] 43725c6091 (master): rb_scan_args_set: make it slightly readable
https://git.ruby-lang.org/ruby.git/commit/?id=43725c6091 From 43725c60915753a88224583fafc87c0bac7c5422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?= <shyouhei@r...> Date: Wed, 20 May 2020 15:44:37 +0900 Subject: rb_scan_args_set: make it slightly readable This is a pure refactoring that improves understanding of what is going on. Not a big win though. diff --git a/include/ruby/internal/scan_args.h b/include/ruby/internal/scan_args.h index 3ad4e95..dae9d65 100644 --- a/include/ruby/internal/scan_args.h +++ b/include/ruby/internal/scan_args.h @@ -269,6 +269,7 @@ rb_scan_args_set(int kw_flag, int argc, const VALUE *argv, https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/scan_args.h#L269 #define rb_scan_args_next_param() vars[vari++] const int n_mand = n_lead + n_trail; + /* capture an option hash - phase 1: pop from the argv */ if (f_hash && argc > 0) { VALUE last = argv[argc - 1]; if (rb_scan_args_keyword_p(kw_flag, last)) { @@ -282,13 +283,14 @@ rb_scan_args_set(int kw_flag, int argc, const VALUE *argv, https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/scan_args.h#L283 } /* capture leading mandatory arguments */ - for (i = n_lead; i-- > 0; ) { + for (i = 0; i < n_lead; i++) { var = rb_scan_args_next_param(); if (var) *var = argv[argi]; argi++; } + /* capture optional arguments */ - for (i = n_opt; i-- > 0; ) { + for (i = 0; i < n_opt; i++) { var = rb_scan_args_next_param(); if (argi < argc - n_trail) { if (var) *var = argv[argi]; @@ -298,6 +300,7 @@ rb_scan_args_set(int kw_flag, int argc, const VALUE *argv, https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/scan_args.h#L300 if (var) *var = Qnil; } } + /* capture variable length arguments */ if (f_var) { int n_var = argc - argi - n_trail; @@ -311,17 +314,20 @@ rb_scan_args_set(int kw_flag, int argc, const VALUE *argv, https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/scan_args.h#L314 if (var) *var = rb_ary_new(); } } + /* capture trailing mandatory arguments */ - for (i = n_trail; i-- > 0; ) { + for (i = 0; i < n_trail; i++) { var = rb_scan_args_next_param(); if (var) *var = argv[argi]; argi++; } + /* capture an option hash - phase 2: assignment */ if (f_hash) { var = rb_scan_args_next_param(); if (var) *var = hash; } + /* capture iterator block */ if (f_block) { var = rb_scan_args_next_param(); @@ -333,12 +339,12 @@ rb_scan_args_set(int kw_flag, int argc, const VALUE *argv, https://github.com/ruby/ruby/blob/trunk/include/ruby/internal/scan_args.h#L339 } } - if (argi < argc) { - argc_error: - rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt); + if (argi == argc) { + return argc; } - return argc; + argc_error: + rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt); #undef rb_scan_args_next_param } -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/