use proper length for mrb_get_values_at()

separate mrb_range_beg_len() into two: the one truncates range into the
sequence size, and the one does not.  #values_at uses the latter.
parent 206c96e4
...@@ -131,7 +131,7 @@ mrb_ary_values_at(mrb_state *mrb, mrb_value self) ...@@ -131,7 +131,7 @@ mrb_ary_values_at(mrb_state *mrb, mrb_value self)
mrb_get_args(mrb, "*", &argv, &argc); mrb_get_args(mrb, "*", &argv, &argc);
return mrb_get_values_at(mrb, self, MRB_INT_MAX, argc, argv, mrb_ary_ref); return mrb_get_values_at(mrb, self, RARRAY_LEN(self), argc, argv, mrb_ary_ref);
} }
void void
......
...@@ -843,7 +843,7 @@ mrb_struct_values_at(mrb_state *mrb, mrb_value self) ...@@ -843,7 +843,7 @@ mrb_struct_values_at(mrb_state *mrb, mrb_value self)
mrb_get_args(mrb, "*", &argv, &argc); mrb_get_args(mrb, "*", &argv, &argc);
return mrb_get_values_at(mrb, self, MRB_INT_MAX, argc, argv, struct_values_at_getter); return mrb_get_values_at(mrb, self, RSTRUCT_LEN(self), argc, argv, struct_values_at_getter);
} }
/* /*
......
...@@ -235,7 +235,7 @@ mrb_range_include(mrb_state *mrb, mrb_value range) ...@@ -235,7 +235,7 @@ mrb_range_include(mrb_state *mrb, mrb_value range)
} }
mrb_bool mrb_bool
mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, mrb_int len) range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, mrb_int len, mrb_bool trunc)
{ {
mrb_int beg, end, b, e; mrb_int beg, end, b, e;
struct RRange *r = mrb_range_ptr(range); struct RRange *r = mrb_range_ptr(range);
...@@ -250,11 +250,14 @@ mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, ...@@ -250,11 +250,14 @@ mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp,
if (beg < 0) return FALSE; if (beg < 0) return FALSE;
} }
if (trunc) {
if (beg > len) return FALSE; if (beg > len) return FALSE;
if (end > len) end = len; if (end > len) end = len;
}
if (end < 0) end += len; if (end < 0) end += len;
if (!r->excl && end < len) end++; /* include end point */ if (!r->excl && (!trunc || end < len))
end++; /* include end point */
len = end - beg; len = end - beg;
if (len < 0) len = 0; if (len < 0) len = 0;
...@@ -263,6 +266,12 @@ mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, ...@@ -263,6 +266,12 @@ mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp,
return TRUE; return TRUE;
} }
mrb_bool
mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, mrb_int len)
{
return range_beg_len(mrb, range, begp, lenp, len, TRUE);
}
/* 15.2.14.4.12(x) */ /* 15.2.14.4.12(x) */
/* /*
* call-seq: * call-seq:
...@@ -381,8 +390,8 @@ mrb_get_values_at(mrb_state *mrb, mrb_value obj, mrb_int olen, mrb_int argc, con ...@@ -381,8 +390,8 @@ mrb_get_values_at(mrb_state *mrb, mrb_value obj, mrb_int olen, mrb_int argc, con
if (mrb_fixnum_p(argv[i])) { if (mrb_fixnum_p(argv[i])) {
mrb_ary_push(mrb, result, func(mrb, obj, mrb_fixnum(argv[i]))); mrb_ary_push(mrb, result, func(mrb, obj, mrb_fixnum(argv[i])));
} }
else if (mrb_range_beg_len(mrb, argv[i], &beg, &len, olen)) { else if (range_beg_len(mrb, argv[i], &beg, &len, olen, FALSE)) {
mrb_int const end = RARRAY_LEN(obj) < beg + len ? RARRAY_LEN(obj) : beg + len; mrb_int const end = olen < beg + len ? olen : beg + len;
for (j = beg; j < end; ++j) { for (j = beg; j < end; ++j) {
mrb_ary_push(mrb, result, func(mrb, obj, j)); mrb_ary_push(mrb, result, func(mrb, obj, j));
} }
......
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