Changed the behavior of mrb_range_beg_len(); close #3411

The new API is:

int mrb_range_beg_len(mrb, range, &beg, &len, len, trunc)

The new argument `trunc` is a boolean value that specifies
whether the function truncates the range. The new return value
is an integer instead of a boolean, that is:

 0: not a range
 1: range with proper edges
 2: out of range

To get the old behavior, you have to rewrite:

  mrb_range_beg_len(mrb, range, &beg, &len, len)

to:

  mrn_range_beg_len(mrb, range, &beg, &len, len, TRUE) == 1

[Breaking Change]
This commit is contained in:
Yukihiro "Matz" Matsumoto
2017-01-23 14:35:26 +09:00
parent 708088c5fa
commit 5e1d923381
6 changed files with 40 additions and 40 deletions
+15 -12
View File
@@ -1091,22 +1091,25 @@ num_index:
return mrb_nil_value();
case MRB_TT_RANGE:
/* check if indx is Range */
{
mrb_int beg, len;
goto range_arg;
len = RSTRING_CHAR_LEN(str);
if (mrb_range_beg_len(mrb, indx, &beg, &len, len)) {
return str_subseq(mrb, str, beg, len);
}
else {
return mrb_nil_value();
}
}
case MRB_TT_FLOAT:
default:
indx = mrb_Integer(mrb, indx);
if (mrb_nil_p(indx)) {
range_arg:
{
mrb_int beg, len;
len = RSTRING_CHAR_LEN(str);
switch (mrb_range_beg_len(mrb, indx, &beg, &len, len, TRUE)) {
case 1:
return str_subseq(mrb, str, beg, len);
case 2:
return mrb_nil_value();
default:
break;
}
}
mrb_raise(mrb, E_TYPE_ERROR, "can't convert to Fixnum");
}
idx = mrb_fixnum(indx);