21 static ID id_beg, id_end, id_excl, id_integer_p, id_div;
23 #define id_succ idSucc 27 #define RANGE_BEG(r) (RSTRUCT(r)->as.ary[0]) 28 #define RANGE_END(r) (RSTRUCT(r)->as.ary[1]) 29 #define RANGE_EXCL(r) (RSTRUCT(r)->as.ary[2]) 30 #define RANGE_SET_BEG(r, v) (RSTRUCT_SET(r, 0, v)) 31 #define RANGE_SET_END(r, v) (RSTRUCT_SET(r, 1, v)) 32 #define RANGE_SET_EXCL(r, v) (RSTRUCT_SET(r, 2, v)) 33 #define RBOOL(v) ((v) ? Qtrue : Qfalse) 35 #define EXCL(r) RTEST(RANGE_EXCL(r)) 58 range_init(range, beg, end,
RBOOL(exclude_end));
84 VALUE beg, end, flags;
88 range_init(range, beg, end,
RBOOL(
RTEST(flags)));
120 if (recur)
return Qtrue;
175 if (recur)
return Qtrue;
247 while (r_less(v, e) < 0) {
248 (*func) (v, arg, 0, 0, 0);
253 while ((c = r_less(v, e)) <= 0) {
254 (*func) (v, arg, 0, 0, 0);
298 discrete_object_p(
VALUE obj)
305 linear_object_p(
VALUE obj)
320 check_step_domain(
VALUE step)
389 VALUE b, e, step, tmp;
400 step = check_step_domain(step);
412 if (i + unit < i)
break;
418 VALUE args[2], iter[2];
446 VALUE args[2], iter[2];
458 if (!discrete_object_p(b)) {
464 range_each_func(range, step_i, (
VALUE)args);
470 #if SIZEOF_DOUBLE == 8 && defined(HAVE_INT64_T) 477 int64_as_double_to_num(int64_t i)
479 union int64_double convert;
491 double_as_int64(
double d)
493 union int64_double convert;
495 return d < 0 ? -convert.i : convert.i;
500 is_integer_p(
VALUE v)
578 #define BSEARCH_CHECK(expr) \ 580 VALUE val = (expr); \ 581 VALUE v = rb_yield(val); \ 583 if (v == INT2FIX(0)) return val; \ 584 smaller = (SIGNED_VALUE)v < 0; \ 586 else if (v == Qtrue) { \ 590 else if (v == Qfalse || v == Qnil) { \ 593 else if (rb_obj_is_kind_of(v, rb_cNumeric)) { \ 594 int cmp = rb_cmpint(rb_funcall(v, id_cmp, 1, INT2FIX(0)), v, INT2FIX(0)); \ 595 if (!cmp) return val; \ 599 rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE \ 600 " (must be numeric, true, false or nil)", \ 605 #define BSEARCH(conv) \ 607 RETURN_ENUMERATOR(range, 0, 0); \ 608 if (EXCL(range)) high--; \ 610 while (low < high) { \ 611 mid = ((high < 0) == (low < 0)) ? low + ((high - low) / 2) \ 612 : (low < -high) ? -((-1 - low - high)/2 + 1) : (low + high) / 2; \ 613 BSEARCH_CHECK(conv(mid)); \ 621 if (low == org_high) { \ 622 BSEARCH_CHECK(conv(low)); \ 623 if (!smaller) return Qnil; \ 638 #if SIZEOF_DOUBLE == 8 && defined(HAVE_INT64_T) 642 int64_t mid, org_high;
643 BSEARCH(int64_as_double_to_num);
646 else if (is_integer_p(beg) && is_integer_p(end)) {
666 if (!smaller)
return Qnil;
715 return range_size(
range);
755 for (i =
FIX2LONG(beg); i < lim; i++) {
777 if (!discrete_object_p(beg)) {
781 range_each_func(range, each_i, 0);
913 else if (
argc != 0) {
914 return range_first(
argc,
argv, range);
922 if (c > 0 || (c == 0 &&
EXCL(range)))
965 if (c == 0)
return Qnil;
1005 long beg, end, origbeg, origend;
1024 if (err == 0 || err == 2) {
1041 origbeg, excl ?
"." :
"", origend);
1149 linear_object_p(beg) || linear_object_p(end);
1154 return r_cover_p(
range, beg, end, val);
1187 return r_cover_p(
range, beg, end, val);
1193 if (r_less(beg, val) <= 0) {
1195 if (r_less(val, end) <= -excl)
1218 VALUE beg, end, excl;
1224 range_modify(
range);
1235 range_alloc(
VALUE klass)
1303 #define rb_intern(str) rb_intern_const(str) 1313 "begin",
"end",
"excl",
NULL);
VALUE rb_ary_last(int argc, const VALUE *argv, VALUE ary)
int rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
int rb_block_given_p(void)
Determines if the current method is given a block.
void rb_raise(VALUE exc, const char *fmt,...)
VALUE rb_str_cat(VALUE, const char *, long)
st_index_t rb_hash_end(st_index_t)
VALUE rb_struct_init_copy(VALUE copy, VALUE s)
VALUE rb_struct_alloc_noinit(VALUE)
VALUE rb_ary_push(VALUE ary, VALUE item)
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
VALUE rb_ivar_get(VALUE, ID)
#define OPTIMIZED_CMP(a, b, data)
VALUE rb_exec_recursive(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE)
VALUE rb_obj_alloc(VALUE)
Allocates an instance of klass.
void rb_include_module(VALUE klass, VALUE module)
VALUE rb_block_call(VALUE, ID, int, const VALUE *, rb_block_call_func_t, VALUE)
#define BSEARCH_CHECK(expr)
#define RGENGC_WB_PROTECTED_OBJECT
VALUE rb_inspect(VALUE)
Convenient wrapper of Object::inspect.
VALUE rb_range_new(VALUE beg, VALUE end, int exclude_end)
#define rb_name_err_raise(mesg, recv, name)
const char * rb_obj_classname(VALUE)
#define NEWOBJ_OF(obj, type, klass, flags)
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
#define RB_TYPE_P(obj, type)
VALUE rb_obj_is_kind_of(VALUE, VALUE)
call-seq: obj.is_a?(class) -> true or false obj.kind_of?(class) -> true or false
VALUE rb_block_call_func(RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg))
VALUE rb_equal(VALUE, VALUE)
call-seq: obj === other -> true or false
RUBY_EXTERN VALUE rb_cObject
VALUE rb_exec_recursive_paired(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE, VALUE)
VALUE rb_obj_as_string(VALUE)
VALUE ruby_num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl)
VALUE rb_check_to_integer(VALUE, const char *)
Tries to convert val into Integer.
#define range(low, item, hi)
VALUE rb_Array(VALUE)
Equivalent to Kernel#Array in Ruby.
int ruby_float_step(VALUE from, VALUE to, VALUE step, int excl)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
VALUE rb_ivar_set(VALUE, ID, VALUE)
#define RANGE_SET_END(r, v)
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
RUBY_EXTERN VALUE rb_cInteger
VALUE rb_check_funcall(VALUE, ID, int, const VALUE *)
VALUE rb_call_super(int, const VALUE *)
#define rb_cmpint(cmp, a, b)
RUBY_EXTERN VALUE rb_cNumeric
#define RANGE_SET_EXCL(r, v)
int rb_respond_to(VALUE, ID)
register unsigned int len
VALUE rb_range_beg_len(VALUE range, long *begp, long *lenp, long len, int err)
#define RARRAY_AREF(a, i)
VALUE rb_Float(VALUE)
Equivalent to Kernel#Float in Ruby.
VALUE rb_check_string_type(VALUE)
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE(*dumper)(VALUE), VALUE(*loader)(VALUE, VALUE))
st_index_t rb_hash_uint(st_index_t, st_index_t)
#define RETURN_ENUMERATOR(obj, argc, argv)
int rb_eql(VALUE, VALUE)
Determines if obj1 and obj2 are equal in terms of Object::eql?.
VALUE rb_struct_define_without_accessor(const char *, VALUE, rb_alloc_func_t,...)
VALUE rb_str_include_range_p(VALUE beg, VALUE end, VALUE val, VALUE exclusive)
#define rb_check_frozen(obj)
VALUE rb_str_intern(VALUE)
#define SPECIAL_CONST_P(x)
RUBY_EXTERN VALUE rb_cTime
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
VALUE rb_str_append(VALUE, VALUE)
#define RANGE_SET_BEG(r, v)
st_index_t rb_hash_start(st_index_t)
VALUE rb_to_int(VALUE)
Converts val into Integer.