Skip to content

Commit

Permalink
Extract range type check functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Apr 14, 2023
1 parent 8286eed commit 1a149aa
Showing 1 changed file with 36 additions and 19 deletions.
55 changes: 36 additions & 19 deletions range.c
Expand Up @@ -1769,6 +1769,35 @@ range_include(VALUE range, VALUE val)
return rb_call_super(1, &val);
}

static inline bool
range_integer_edge_p(VALUE beg, VALUE end)
{
return (!NIL_P(rb_check_to_integer(beg, "to_int")) ||
!NIL_P(rb_check_to_integer(end, "to_int")));
}

static inline bool
range_string_edge_p(VALUE beg, VALUE end)
{
return RB_TYPE_P(beg, T_STRING) || RB_TYPE_P(end, T_STRING);
}

static inline bool
range_string_range_p(VALUE beg, VALUE end)
{
return RB_TYPE_P(beg, T_STRING) && RB_TYPE_P(end, T_STRING);
}

static inline VALUE
range_include_fallback(VALUE beg, VALUE end)
{
if (NIL_P(beg) || NIL_P(end)) {
rb_raise(rb_eTypeError, "cannot determine inclusion in beginless/endless ranges");
}

return Qundef;
}

static VALUE
range_string_cover_internal(VALUE range, VALUE val)
{
Expand All @@ -1777,13 +1806,11 @@ range_string_cover_internal(VALUE range, VALUE val)
int nv = FIXNUM_P(beg) || FIXNUM_P(end) ||
linear_object_p(beg) || linear_object_p(end);

if (nv ||
!NIL_P(rb_check_to_integer(beg, "to_int")) ||
!NIL_P(rb_check_to_integer(end, "to_int"))) {
if (nv || range_integer_edge_p(beg, end)) {
return r_cover_p(range, beg, end, val);
}
else if (RB_TYPE_P(beg, T_STRING) || RB_TYPE_P(end, T_STRING)) {
if (RB_TYPE_P(beg, T_STRING) && RB_TYPE_P(end, T_STRING)) {
else if (range_string_edge_p(beg, end)) {
if (range_string_range_p(beg, end)) {
return r_cover_p(range, beg, end, val);
}
if (NIL_P(beg)) {
Expand All @@ -1801,11 +1828,7 @@ range_string_cover_internal(VALUE range, VALUE val)
}
}

if (NIL_P(beg) || NIL_P(end)) {
rb_raise(rb_eTypeError, "cannot determine inclusion in beginless/endless ranges");
}

return Qundef;
return range_include_fallback(beg, end);
}

static VALUE
Expand All @@ -1816,20 +1839,14 @@ range_include_internal(VALUE range, VALUE val)
int nv = FIXNUM_P(beg) || FIXNUM_P(end) ||
linear_object_p(beg) || linear_object_p(end);

if (nv ||
!NIL_P(rb_check_to_integer(beg, "to_int")) ||
!NIL_P(rb_check_to_integer(end, "to_int"))) {
if (nv || range_integer_edge_p(beg, end)) {
return r_cover_p(range, beg, end, val);
}
else if (RB_TYPE_P(beg, T_STRING) && RB_TYPE_P(end, T_STRING)) {
else if (range_string_range_p(beg, end)) {
return rb_str_include_range_p(beg, end, val, RANGE_EXCL(range));
}

if (NIL_P(beg) || NIL_P(end)) {
rb_raise(rb_eTypeError, "cannot determine inclusion in beginless/endless ranges");
}

return Qundef;
return range_include_fallback(beg, end);
}

static int r_cover_range_p(VALUE range, VALUE beg, VALUE end, VALUE val);
Expand Down

0 comments on commit 1a149aa

Please sign in to comment.