string.c: reorganize `str_convert_range` using `mrb_ensure_int_type`

parent b6d31810
...@@ -1108,6 +1108,9 @@ str_convert_range(mrb_state *mrb, mrb_value str, mrb_value indx, mrb_value alen, ...@@ -1108,6 +1108,9 @@ str_convert_range(mrb_state *mrb, mrb_value str, mrb_value indx, mrb_value alen,
} }
else { else {
switch (mrb_type(indx)) { switch (mrb_type(indx)) {
default:
indx = mrb_ensure_int_type(mrb, indx);
/* fall through */
case MRB_TT_INTEGER: case MRB_TT_INTEGER:
*beg = mrb_integer(indx); *beg = mrb_integer(indx);
*len = 1; *len = 1;
...@@ -1120,16 +1123,6 @@ str_convert_range(mrb_state *mrb, mrb_value str, mrb_value indx, mrb_value alen, ...@@ -1120,16 +1123,6 @@ str_convert_range(mrb_state *mrb, mrb_value str, mrb_value indx, mrb_value alen,
return STR_BYTE_RANGE_CORRECTED; return STR_BYTE_RANGE_CORRECTED;
case MRB_TT_RANGE: case MRB_TT_RANGE:
goto range_arg;
default:
indx = mrb_to_integer(mrb, indx);
if (mrb_integer_p(indx)) {
*beg = mrb_integer(indx);
*len = 1;
return STR_CHAR_RANGE;
}
range_arg:
*len = RSTRING_CHAR_LEN(str); *len = RSTRING_CHAR_LEN(str);
switch (mrb_range_beg_len(mrb, indx, beg, len, *len, TRUE)) { switch (mrb_range_beg_len(mrb, indx, beg, len, *len, TRUE)) {
case MRB_RANGE_OK: case MRB_RANGE_OK:
...@@ -1139,8 +1132,6 @@ range_arg: ...@@ -1139,8 +1132,6 @@ range_arg:
default: default:
break; break;
} }
mrb_raise(mrb, E_TYPE_ERROR, "can't convert to Integer");
} }
} }
return STR_OUT_OF_RANGE; return STR_OUT_OF_RANGE;
......
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