ary_join_1: do not goto into a branch

I'm not necessarily against every goto in general, but jumping into a
branch is definitely a bad idea.  Better refactor.
This commit is contained in:
卜部昌平 2020-06-11 11:31:27 +09:00
parent 4dc83eefce
commit 73f98d25eb
Notes: git 2020-06-29 11:07:17 +09:00

61
array.c
View File

@ -2944,26 +2944,18 @@ ary_join_0(VALUE ary, VALUE sep, long max, VALUE result)
} }
static void static void
ary_join_1(VALUE obj, VALUE ary, VALUE sep, long i, VALUE result, int *first) ary_join_1_str(VALUE dst, VALUE src, int *first)
{ {
VALUE val, tmp; rb_str_buf_append(dst, src);
for (; i<RARRAY_LEN(ary); i++) {
if (i > 0 && !NIL_P(sep))
rb_str_buf_append(result, sep);
val = RARRAY_AREF(ary, i);
if (RB_TYPE_P(val, T_STRING)) {
str_join:
rb_str_buf_append(result, val);
if (*first) { if (*first) {
rb_enc_copy(result, val); rb_enc_copy(dst, src);
*first = FALSE; *first = FALSE;
} }
} }
else if (RB_TYPE_P(val, T_ARRAY)) {
obj = val; static void
ary_join: ary_join_1_ary(VALUE obj, VALUE ary, VALUE sep, VALUE result, VALUE val, int *first)
{
if (val == ary) { if (val == ary) {
rb_raise(rb_eArgError, "recursive array join"); rb_raise(rb_eArgError, "recursive array join");
} }
@ -2977,21 +2969,32 @@ ary_join_1(VALUE obj, VALUE ary, VALUE sep, long i, VALUE result, int *first)
args[3] = (VALUE)first; args[3] = (VALUE)first;
rb_exec_recursive(recursive_join, obj, (VALUE)args); rb_exec_recursive(recursive_join, obj, (VALUE)args);
} }
}
static void
ary_join_1(VALUE obj, VALUE ary, VALUE sep, long i, VALUE result, int *first)
{
VALUE val, tmp;
for (; i<RARRAY_LEN(ary); i++) {
if (i > 0 && !NIL_P(sep))
rb_str_buf_append(result, sep);
val = RARRAY_AREF(ary, i);
if (RB_TYPE_P(val, T_STRING)) {
ary_join_1_str(result, val, first);
}
else if (RB_TYPE_P(val, T_ARRAY)) {
ary_join_1_ary(val, ary, sep, result, val, first);
}
else if (!NIL_P(tmp = rb_check_string_type(val))) {
ary_join_1_str(result, tmp, first);
}
else if (!NIL_P(tmp = rb_check_array_type(val))) {
ary_join_1_ary(val, ary, sep, result, tmp, first);
} }
else { else {
tmp = rb_check_string_type(val); ary_join_1_str(result, rb_obj_as_string(val), first);
if (!NIL_P(tmp)) {
val = tmp;
goto str_join;
}
tmp = rb_check_array_type(val);
if (!NIL_P(tmp)) {
obj = val;
val = tmp;
goto ary_join;
}
val = rb_obj_as_string(val);
goto str_join;
} }
} }
} }