Unverified Commit 88cd1d65 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto Committed by GitHub

Merge pull request #4714 from dearblue/shrink-mrb_get_args

Shrink `mrb_get_args()`
parents cc636f33 62499c62
......@@ -619,6 +619,9 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
opt = FALSE;
i = 0;
while ((c = *format++)) {
mrb_value *argv = ARGV;
mrb_bool altmode;
switch (c) {
case '|': case '*': case '&': case '?':
break;
......@@ -634,6 +637,14 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
break;
}
if (*format == '!') {
format ++;
altmode = TRUE;
}
else {
altmode = FALSE;
}
switch (c) {
case 'o':
{
......@@ -641,7 +652,7 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
p = va_arg(ap, mrb_value*);
if (i < argc) {
*p = ARGV[arg_i++];
*p = argv[arg_i++];
i++;
}
}
......@@ -654,7 +665,7 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
if (i < argc) {
mrb_value ss;
ss = ARGV[arg_i++];
ss = argv[arg_i++];
if (!class_ptr_p(ss)) {
mrb_raisef(mrb, E_TYPE_ERROR, "%v is not class/module", ss);
}
......@@ -668,18 +679,12 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
mrb_value *p;
p = va_arg(ap, mrb_value*);
if (*format == '!') {
format++;
if (i < argc && mrb_nil_p(ARGV[arg_i])) {
*p = ARGV[arg_i++];
i++;
break;
}
}
if (i < argc) {
*p = ARGV[arg_i++];
mrb_to_str(mrb, *p);
*p = argv[arg_i++];
i++;
if (!(altmode && mrb_nil_p(*p))) {
mrb_to_str(mrb, *p);
}
}
}
break;
......@@ -688,17 +693,12 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
mrb_value *p;
p = va_arg(ap, mrb_value*);
if (*format == '!') {
format++;
if (i < argc && mrb_nil_p(ARGV[arg_i])) {
*p = ARGV[arg_i++];
i++;
break;
}
}
if (i < argc) {
*p = to_ary(mrb, ARGV[arg_i++]);
*p = argv[arg_i++];
i++;
if (!(altmode && mrb_nil_p(*p))) {
*p = to_ary(mrb, *p);
}
}
}
break;
......@@ -707,17 +707,12 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
mrb_value *p;
p = va_arg(ap, mrb_value*);
if (*format == '!') {
format++;
if (i < argc && mrb_nil_p(ARGV[arg_i])) {
*p = ARGV[arg_i++];
i++;
break;
}
}
if (i < argc) {
*p = to_hash(mrb, ARGV[arg_i++]);
*p = argv[arg_i++];
i++;
if (!(altmode && mrb_nil_p(*p))) {
*p = to_hash(mrb, *p);
}
}
}
break;
......@@ -729,21 +724,18 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
ps = va_arg(ap, char**);
pl = va_arg(ap, mrb_int*);
if (*format == '!') {
format++;
if (i < argc && mrb_nil_p(ARGV[arg_i])) {
if (i < argc) {
ss = argv[arg_i++];
i++;
if (altmode && mrb_nil_p(ss)) {
*ps = NULL;
*pl = 0;
i++; arg_i++;
break;
}
}
if (i < argc) {
ss = ARGV[arg_i++];
mrb_to_str(mrb, ss);
*ps = RSTRING_PTR(ss);
*pl = RSTRING_LEN(ss);
i++;
else {
mrb_to_str(mrb, ss);
*ps = RSTRING_PTR(ss);
*pl = RSTRING_LEN(ss);
}
}
}
break;
......@@ -753,19 +745,16 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
const char **ps;
ps = va_arg(ap, const char**);
if (*format == '!') {
format++;
if (i < argc && mrb_nil_p(ARGV[arg_i])) {
*ps = NULL;
i++; arg_i++;
break;
}
}
if (i < argc) {
ss = ARGV[arg_i++];
mrb_to_str(mrb, ss);
*ps = RSTRING_CSTR(mrb, ss);
ss = argv[arg_i++];
i++;
if (altmode && mrb_nil_p(ss)) {
*ps = NULL;
}
else {
mrb_to_str(mrb, ss);
*ps = RSTRING_CSTR(mrb, ss);
}
}
}
break;
......@@ -778,21 +767,19 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
pb = va_arg(ap, mrb_value**);
pl = va_arg(ap, mrb_int*);
if (*format == '!') {
format++;
if (i < argc && mrb_nil_p(ARGV[arg_i])) {
if (i < argc) {
aa = argv[arg_i++];
i++;
if (altmode && mrb_nil_p(aa)) {
*pb = 0;
*pl = 0;
i++; arg_i++;
break;
}
}
if (i < argc) {
aa = to_ary(mrb, ARGV[arg_i++]);
a = mrb_ary_ptr(aa);
*pb = ARY_PTR(a);
*pl = ARY_LEN(a);
i++;
else {
aa = to_ary(mrb, aa);
a = mrb_ary_ptr(aa);
*pb = ARY_PTR(a);
*pl = ARY_LEN(a);
}
}
}
break;
......@@ -803,7 +790,7 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
p = va_arg(ap, void**);
if (i < argc) {
ss = ARGV[arg_i];
ss = argv[arg_i];
if (mrb_type(ss) != MRB_TT_ISTRUCT)
{
mrb_raisef(mrb, E_TYPE_ERROR, "%v is not inline struct", ss);
......@@ -821,7 +808,7 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
p = va_arg(ap, mrb_float*);
if (i < argc) {
*p = mrb_to_flo(mrb, ARGV[arg_i]);
*p = mrb_to_flo(mrb, argv[arg_i]);
arg_i++;
i++;
}
......@@ -834,7 +821,7 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
p = va_arg(ap, mrb_int*);
if (i < argc) {
*p = mrb_fixnum(mrb_to_int(mrb, ARGV[arg_i]));
*p = mrb_fixnum(mrb_to_int(mrb, argv[arg_i]));
arg_i++;
i++;
}
......@@ -845,7 +832,7 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
mrb_bool *boolp = va_arg(ap, mrb_bool*);
if (i < argc) {
mrb_value b = ARGV[arg_i++];
mrb_value b = argv[arg_i++];
*boolp = mrb_test(b);
i++;
}
......@@ -859,7 +846,7 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
if (i < argc) {
mrb_value ss;
ss = ARGV[arg_i++];
ss = argv[arg_i++];
*symp = to_sym(mrb, ss);
i++;
}
......@@ -872,17 +859,15 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
datap = va_arg(ap, void**);
type = va_arg(ap, struct mrb_data_type const*);
if (*format == '!') {
format++;
if (i < argc && mrb_nil_p(ARGV[arg_i])) {
if (i < argc) {
mrb_value dd = argv[arg_i++];
i++;
if (altmode && mrb_nil_p(dd)) {
*datap = 0;
i++; arg_i++;
break;
}
}
if (i < argc) {
*datap = mrb_data_get_ptr(mrb, ARGV[arg_i++], type);
++i;
else {
*datap = mrb_data_get_ptr(mrb, dd, type);
}
}
}
break;
......@@ -898,11 +883,8 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
else {
bp = mrb->c->stack + mrb->c->ci->argc + 1;
}
if (*format == '!') {
format ++;
if (mrb_nil_p(*bp)) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "no block given");
}
if (altmode && mrb_nil_p(*bp)) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "no block given");
}
*p = *bp;
}
......@@ -924,22 +906,18 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
{
mrb_value **var;
mrb_int *pl;
mrb_bool nocopy = array_argv ? TRUE : FALSE;
mrb_bool nocopy = altmode || array_argv ? TRUE : FALSE;
if (*format == '!') {
format++;
nocopy = TRUE;
}
var = va_arg(ap, mrb_value**);
pl = va_arg(ap, mrb_int*);
if (argc > i) {
*pl = argc-i;
if (*pl > 0) {
if (nocopy) {
*var = ARGV+arg_i;
*var = argv+arg_i;
}
else {
mrb_value args = mrb_ary_new_from_values(mrb, *pl, ARGV+arg_i);
mrb_value args = mrb_ary_new_from_values(mrb, *pl, argv+arg_i);
RARRAY(args)->c = NULL;
*var = RARRAY_PTR(args);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment