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

Merge pull request #4467 from dearblue/naming-mrb_range_beg_len

Name the return value of `mrb_range_beg_len()`
parents c6f30e8f 56e0e193
...@@ -64,7 +64,13 @@ MRB_API struct RRange* mrb_range_ptr(mrb_state *mrb, mrb_value range); ...@@ -64,7 +64,13 @@ MRB_API struct RRange* mrb_range_ptr(mrb_state *mrb, mrb_value range);
*/ */
MRB_API mrb_value mrb_range_new(mrb_state *mrb, mrb_value start, mrb_value end, mrb_bool exclude); MRB_API mrb_value mrb_range_new(mrb_state *mrb, mrb_value start, mrb_value end, mrb_bool exclude);
MRB_API mrb_int mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, mrb_int len, mrb_bool trunc); enum mrb_range_beg_len {
MRB_RANGE_TYPE_MISMATCH = 0, /* (failure) not range */
MRB_RANGE_OK = 1, /* (success) range */
MRB_RANGE_OUT = 2 /* (failure) out of range */
};
MRB_API enum mrb_range_beg_len mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, mrb_int len, mrb_bool trunc);
mrb_value mrb_get_values_at(mrb_state *mrb, mrb_value obj, mrb_int olen, mrb_int argc, const mrb_value *argv, mrb_value (*func)(mrb_state*, mrb_value, mrb_int)); mrb_value mrb_get_values_at(mrb_state *mrb, mrb_value obj, mrb_int olen, mrb_int argc, const mrb_value *argv, mrb_value (*func)(mrb_state*, mrb_value, mrb_int));
void mrb_gc_mark_range(mrb_state *mrb, struct RRange *r); void mrb_gc_mark_range(mrb_state *mrb, struct RRange *r);
......
...@@ -145,7 +145,7 @@ mrb_ary_slice_bang(mrb_state *mrb, mrb_value self) ...@@ -145,7 +145,7 @@ mrb_ary_slice_bang(mrb_state *mrb, mrb_value self)
mrb_get_args(mrb, "o|i", &index, &len); mrb_get_args(mrb, "o|i", &index, &len);
switch (mrb_type(index)) { switch (mrb_type(index)) {
case MRB_TT_RANGE: case MRB_TT_RANGE:
if (mrb_range_beg_len(mrb, index, &i, &len, ARY_LEN(a), TRUE) == 1) { if (mrb_range_beg_len(mrb, index, &i, &len, ARY_LEN(a), TRUE) == MRB_RANGE_OK) {
goto delete_pos_len; goto delete_pos_len;
} }
else { else {
......
...@@ -22,7 +22,7 @@ mrb_f_caller(mrb_state *mrb, mrb_value self) ...@@ -22,7 +22,7 @@ mrb_f_caller(mrb_state *mrb, mrb_value self)
case 1: case 1:
if (mrb_type(v) == MRB_TT_RANGE) { if (mrb_type(v) == MRB_TT_RANGE) {
mrb_int beg, len; mrb_int beg, len;
if (mrb_range_beg_len(mrb, v, &beg, &len, bt_len, TRUE) == 1) { if (mrb_range_beg_len(mrb, v, &beg, &len, bt_len, TRUE) == MRB_RANGE_OK) {
lev = beg; lev = beg;
n = len; n = len;
} }
......
...@@ -58,11 +58,11 @@ mrb_str_byteslice(mrb_state *mrb, mrb_value str) ...@@ -58,11 +58,11 @@ mrb_str_byteslice(mrb_state *mrb, mrb_value str)
len = RSTRING_LEN(str); len = RSTRING_LEN(str);
switch (mrb_range_beg_len(mrb, a1, &beg, &len, len, TRUE)) { switch (mrb_range_beg_len(mrb, a1, &beg, &len, len, TRUE)) {
case 0: /* not range */ case MRB_RANGE_TYPE_MISMATCH:
break; break;
case 1: /* range */ case MRB_RANGE_OK:
return mrb_str_substr(mrb, str, beg, len); return mrb_str_substr(mrb, str, beg, len);
case 2: /* out of range */ case MRB_RANGE_OUT:
mrb_raisef(mrb, E_RANGE_ERROR, "%S out of range", a1); mrb_raisef(mrb, E_RANGE_ERROR, "%S out of range", a1);
break; break;
} }
......
...@@ -858,7 +858,7 @@ mrb_ary_aget(mrb_state *mrb, mrb_value self) ...@@ -858,7 +858,7 @@ mrb_ary_aget(mrb_state *mrb, mrb_value self)
switch (mrb_type(index)) { switch (mrb_type(index)) {
/* a[n..m] */ /* a[n..m] */
case MRB_TT_RANGE: case MRB_TT_RANGE:
if (mrb_range_beg_len(mrb, index, &i, &len, ARY_LEN(a), TRUE) == 1) { if (mrb_range_beg_len(mrb, index, &i, &len, ARY_LEN(a), TRUE) == MRB_RANGE_OK) {
return ary_subseq(mrb, a, i, len); return ary_subseq(mrb, a, i, len);
} }
else { else {
...@@ -927,13 +927,13 @@ mrb_ary_aset(mrb_state *mrb, mrb_value self) ...@@ -927,13 +927,13 @@ mrb_ary_aset(mrb_state *mrb, mrb_value self)
if (mrb_get_args(mrb, "oo|o", &v1, &v2, &v3) == 2) { if (mrb_get_args(mrb, "oo|o", &v1, &v2, &v3) == 2) {
/* a[n..m] = v */ /* a[n..m] = v */
switch (mrb_range_beg_len(mrb, v1, &i, &len, RARRAY_LEN(self), FALSE)) { switch (mrb_range_beg_len(mrb, v1, &i, &len, RARRAY_LEN(self), FALSE)) {
case 0: /* not range */ case MRB_RANGE_TYPE_MISMATCH:
mrb_ary_set(mrb, self, aget_index(mrb, v1), v2); mrb_ary_set(mrb, self, aget_index(mrb, v1), v2);
break; break;
case 1: /* range */ case MRB_RANGE_OK:
mrb_ary_splice(mrb, self, i, len, v2); mrb_ary_splice(mrb, self, i, len, v2);
break; break;
case 2: /* out of range */ case MRB_RANGE_OUT:
mrb_raisef(mrb, E_RANGE_ERROR, "%S out of range", v1); mrb_raisef(mrb, E_RANGE_ERROR, "%S out of range", v1);
break; break;
} }
......
...@@ -352,7 +352,7 @@ mrb_get_values_at(mrb_state *mrb, mrb_value obj, mrb_int olen, mrb_int argc, con ...@@ -352,7 +352,7 @@ 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, FALSE) == 1) { else if (mrb_range_beg_len(mrb, argv[i], &beg, &len, olen, FALSE) == MRB_RANGE_OK) {
mrb_int const end = olen < beg + len ? olen : 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));
...@@ -398,13 +398,13 @@ mrb_range_new(mrb_state *mrb, mrb_value beg, mrb_value end, mrb_bool excl) ...@@ -398,13 +398,13 @@ mrb_range_new(mrb_state *mrb, mrb_value beg, mrb_value end, mrb_bool excl)
return mrb_range_value(r); return mrb_range_value(r);
} }
MRB_API mrb_int MRB_API enum mrb_range_beg_len
mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, mrb_int len, mrb_bool trunc) mrb_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; mrb_int beg, end;
struct RRange *r; struct RRange *r;
if (mrb_type(range) != MRB_TT_RANGE) return 0; if (mrb_type(range) != MRB_TT_RANGE) return MRB_RANGE_TYPE_MISMATCH;
r = mrb_range_ptr(mrb, range); r = mrb_range_ptr(mrb, range);
beg = mrb_int(mrb, RANGE_BEG(r)); beg = mrb_int(mrb, RANGE_BEG(r));
...@@ -412,11 +412,11 @@ mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, ...@@ -412,11 +412,11 @@ mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp,
if (beg < 0) { if (beg < 0) {
beg += len; beg += len;
if (beg < 0) return 2; if (beg < 0) return MRB_RANGE_OUT;
} }
if (trunc) { if (trunc) {
if (beg > len) return 2; if (beg > len) return MRB_RANGE_OUT;
if (end > len) end = len; if (end > len) end = len;
} }
...@@ -427,7 +427,7 @@ mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, ...@@ -427,7 +427,7 @@ mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp,
*begp = beg; *begp = beg;
*lenp = len; *lenp = len;
return 1; return MRB_RANGE_OK;
} }
void void
......
...@@ -1041,9 +1041,9 @@ num_index: ...@@ -1041,9 +1041,9 @@ num_index:
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 1: case MRB_RANGE_OK:
return str_subseq(mrb, str, beg, len); return str_subseq(mrb, str, beg, len);
case 2: case MRB_RANGE_OUT:
return mrb_nil_value(); return mrb_nil_value();
default: default:
break; break;
......
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