Extract range type check functions

This commit is contained in:
Nobuyoshi Nakada 2023-03-21 22:46:05 +09:00
Родитель 8286eed20b
Коммит 1a149aab77
1 изменённых файлов: 36 добавлений и 19 удалений

55
range.c
Просмотреть файл

@ -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)
{
@ -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)) {
@ -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
@ -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);