Ruby 3.3.7p123 (2025-01-15 revision be31f993d7fa0219d85f7b3c694d454da4ecc10b)
range.c
1/**********************************************************************
2
3 range.c -
4
5 $Author$
6 created at: Thu Aug 19 17:46:47 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
12#include "ruby/internal/config.h"
13
14#include <assert.h>
15#include <math.h>
16
17#ifdef HAVE_FLOAT_H
18#include <float.h>
19#endif
20
21#include "id.h"
22#include "internal.h"
23#include "internal/array.h"
24#include "internal/compar.h"
25#include "internal/enum.h"
26#include "internal/enumerator.h"
27#include "internal/error.h"
28#include "internal/numeric.h"
29#include "internal/range.h"
30
32static ID id_beg, id_end, id_excl;
33#define id_cmp idCmp
34#define id_succ idSucc
35#define id_min idMin
36#define id_max idMax
37
38static VALUE r_cover_p(VALUE, VALUE, VALUE, VALUE);
39
40#define RANGE_SET_BEG(r, v) (RSTRUCT_SET(r, 0, v))
41#define RANGE_SET_END(r, v) (RSTRUCT_SET(r, 1, v))
42#define RANGE_SET_EXCL(r, v) (RSTRUCT_SET(r, 2, v))
43
44#define EXCL(r) RTEST(RANGE_EXCL(r))
45
46static void
47range_init(VALUE range, VALUE beg, VALUE end, VALUE exclude_end)
48{
49 if ((!FIXNUM_P(beg) || !FIXNUM_P(end)) && !NIL_P(beg) && !NIL_P(end)) {
50 VALUE v;
51
52 v = rb_funcall(beg, id_cmp, 1, end);
53 if (NIL_P(v))
54 rb_raise(rb_eArgError, "bad value for range");
55 }
56
57 RANGE_SET_EXCL(range, exclude_end);
58 RANGE_SET_BEG(range, beg);
59 RANGE_SET_END(range, end);
60
61 if (CLASS_OF(range) == rb_cRange) {
62 rb_obj_freeze(range);
63 }
64}
65
67rb_range_new(VALUE beg, VALUE end, int exclude_end)
68{
70
71 range_init(range, beg, end, RBOOL(exclude_end));
72 return range;
73}
74
75static void
76range_modify(VALUE range)
77{
78 rb_check_frozen(range);
79 /* Ranges are immutable, so that they should be initialized only once. */
80 if (RANGE_EXCL(range) != Qnil) {
81 rb_name_err_raise("`initialize' called twice", range, ID2SYM(idInitialize));
82 }
83}
84
85/*
86 * call-seq:
87 * Range.new(begin, end, exclude_end = false) -> new_range
88 *
89 * Returns a new range based on the given objects +begin+ and +end+.
90 * Optional argument +exclude_end+ determines whether object +end+
91 * is included as the last object in the range:
92 *
93 * Range.new(2, 5).to_a # => [2, 3, 4, 5]
94 * Range.new(2, 5, true).to_a # => [2, 3, 4]
95 * Range.new('a', 'd').to_a # => ["a", "b", "c", "d"]
96 * Range.new('a', 'd', true).to_a # => ["a", "b", "c"]
97 *
98 */
99
100static VALUE
101range_initialize(int argc, VALUE *argv, VALUE range)
102{
103 VALUE beg, end, flags;
104
105 rb_scan_args(argc, argv, "21", &beg, &end, &flags);
106 range_modify(range);
107 range_init(range, beg, end, RBOOL(RTEST(flags)));
108 return Qnil;
109}
110
111/* :nodoc: */
112static VALUE
113range_initialize_copy(VALUE range, VALUE orig)
114{
115 range_modify(range);
116 rb_struct_init_copy(range, orig);
117 return range;
118}
119
120/*
121 * call-seq:
122 * exclude_end? -> true or false
123 *
124 * Returns +true+ if +self+ excludes its end value; +false+ otherwise:
125 *
126 * Range.new(2, 5).exclude_end? # => false
127 * Range.new(2, 5, true).exclude_end? # => true
128 * (2..5).exclude_end? # => false
129 * (2...5).exclude_end? # => true
130 */
131
132static VALUE
133range_exclude_end_p(VALUE range)
134{
135 return RBOOL(EXCL(range));
136}
137
138static VALUE
139recursive_equal(VALUE range, VALUE obj, int recur)
140{
141 if (recur) return Qtrue; /* Subtle! */
142 if (!rb_equal(RANGE_BEG(range), RANGE_BEG(obj)))
143 return Qfalse;
144 if (!rb_equal(RANGE_END(range), RANGE_END(obj)))
145 return Qfalse;
146
147 return RBOOL(EXCL(range) == EXCL(obj));
148}
149
150
151/*
152 * call-seq:
153 * self == other -> true or false
154 *
155 * Returns +true+ if and only if:
156 *
157 * - +other+ is a range.
158 * - <tt>other.begin == self.begin</tt>.
159 * - <tt>other.end == self.end</tt>.
160 * - <tt>other.exclude_end? == self.exclude_end?</tt>.
161 *
162 * Otherwise returns +false+.
163 *
164 * r = (1..5)
165 * r == (1..5) # => true
166 * r = Range.new(1, 5)
167 * r == 'foo' # => false
168 * r == (2..5) # => false
169 * r == (1..4) # => false
170 * r == (1...5) # => false
171 * r == Range.new(1, 5, true) # => false
172 *
173 * Note that even with the same argument, the return values of #== and #eql? can differ:
174 *
175 * (1..2) == (1..2.0) # => true
176 * (1..2).eql? (1..2.0) # => false
177 *
178 * Related: Range#eql?.
179 *
180 */
181
182static VALUE
183range_eq(VALUE range, VALUE obj)
184{
185 if (range == obj)
186 return Qtrue;
187 if (!rb_obj_is_kind_of(obj, rb_cRange))
188 return Qfalse;
189
190 return rb_exec_recursive_paired(recursive_equal, range, obj, obj);
191}
192
193/* compares _a_ and _b_ and returns:
194 * < 0: a < b
195 * = 0: a = b
196 * > 0: a > b or non-comparable
197 */
198static int
199r_less(VALUE a, VALUE b)
200{
201 VALUE r = rb_funcall(a, id_cmp, 1, b);
202
203 if (NIL_P(r))
204 return INT_MAX;
205 return rb_cmpint(r, a, b);
206}
207
208static VALUE
209recursive_eql(VALUE range, VALUE obj, int recur)
210{
211 if (recur) return Qtrue; /* Subtle! */
212 if (!rb_eql(RANGE_BEG(range), RANGE_BEG(obj)))
213 return Qfalse;
214 if (!rb_eql(RANGE_END(range), RANGE_END(obj)))
215 return Qfalse;
216
217 return RBOOL(EXCL(range) == EXCL(obj));
218}
219
220/*
221 * call-seq:
222 * eql?(other) -> true or false
223 *
224 * Returns +true+ if and only if:
225 *
226 * - +other+ is a range.
227 * - <tt>other.begin eql? self.begin</tt>.
228 * - <tt>other.end eql? self.end</tt>.
229 * - <tt>other.exclude_end? == self.exclude_end?</tt>.
230 *
231 * Otherwise returns +false+.
232 *
233 * r = (1..5)
234 * r.eql?(1..5) # => true
235 * r = Range.new(1, 5)
236 * r.eql?('foo') # => false
237 * r.eql?(2..5) # => false
238 * r.eql?(1..4) # => false
239 * r.eql?(1...5) # => false
240 * r.eql?(Range.new(1, 5, true)) # => false
241 *
242 * Note that even with the same argument, the return values of #== and #eql? can differ:
243 *
244 * (1..2) == (1..2.0) # => true
245 * (1..2).eql? (1..2.0) # => false
246 *
247 * Related: Range#==.
248 */
249
250static VALUE
251range_eql(VALUE range, VALUE obj)
252{
253 if (range == obj)
254 return Qtrue;
255 if (!rb_obj_is_kind_of(obj, rb_cRange))
256 return Qfalse;
257 return rb_exec_recursive_paired(recursive_eql, range, obj, obj);
258}
259
260/*
261 * call-seq:
262 * hash -> integer
263 *
264 * Returns the integer hash value for +self+.
265 * Two range objects +r0+ and +r1+ have the same hash value
266 * if and only if <tt>r0.eql?(r1)</tt>.
267 *
268 * Related: Range#eql?, Object#hash.
269 */
270
271static VALUE
272range_hash(VALUE range)
273{
274 st_index_t hash = EXCL(range);
275 VALUE v;
276
277 hash = rb_hash_start(hash);
278 v = rb_hash(RANGE_BEG(range));
279 hash = rb_hash_uint(hash, NUM2LONG(v));
280 v = rb_hash(RANGE_END(range));
281 hash = rb_hash_uint(hash, NUM2LONG(v));
282 hash = rb_hash_uint(hash, EXCL(range) << 24);
283 hash = rb_hash_end(hash);
284
285 return ST2FIX(hash);
286}
287
288static void
289range_each_func(VALUE range, int (*func)(VALUE, VALUE), VALUE arg)
290{
291 int c;
292 VALUE b = RANGE_BEG(range);
293 VALUE e = RANGE_END(range);
294 VALUE v = b;
295
296 if (EXCL(range)) {
297 while (r_less(v, e) < 0) {
298 if ((*func)(v, arg)) break;
299 v = rb_funcallv(v, id_succ, 0, 0);
300 }
301 }
302 else {
303 while ((c = r_less(v, e)) <= 0) {
304 if ((*func)(v, arg)) break;
305 if (!c) break;
306 v = rb_funcallv(v, id_succ, 0, 0);
307 }
308 }
309}
310
311static bool
312step_i_iter(VALUE arg)
313{
314 VALUE *iter = (VALUE *)arg;
315
316 if (FIXNUM_P(iter[0])) {
317 iter[0] -= INT2FIX(1) & ~FIXNUM_FLAG;
318 }
319 else {
320 iter[0] = rb_funcall(iter[0], '-', 1, INT2FIX(1));
321 }
322 if (iter[0] != INT2FIX(0)) return false;
323 iter[0] = iter[1];
324 return true;
325}
326
327static int
328sym_step_i(VALUE i, VALUE arg)
329{
330 if (step_i_iter(arg)) {
331 rb_yield(rb_str_intern(i));
332 }
333 return 0;
334}
335
336static int
337step_i(VALUE i, VALUE arg)
338{
339 if (step_i_iter(arg)) {
340 rb_yield(i);
341 }
342 return 0;
343}
344
345static int
346discrete_object_p(VALUE obj)
347{
348 return rb_respond_to(obj, id_succ);
349}
350
351static int
352linear_object_p(VALUE obj)
353{
354 if (FIXNUM_P(obj) || FLONUM_P(obj)) return TRUE;
355 if (SPECIAL_CONST_P(obj)) return FALSE;
356 switch (BUILTIN_TYPE(obj)) {
357 case T_FLOAT:
358 case T_BIGNUM:
359 return TRUE;
360 default:
361 break;
362 }
363 if (rb_obj_is_kind_of(obj, rb_cNumeric)) return TRUE;
364 if (rb_obj_is_kind_of(obj, rb_cTime)) return TRUE;
365 return FALSE;
366}
367
368static VALUE
369check_step_domain(VALUE step)
370{
371 VALUE zero = INT2FIX(0);
372 int cmp;
373 if (!rb_obj_is_kind_of(step, rb_cNumeric)) {
374 step = rb_to_int(step);
375 }
376 cmp = rb_cmpint(rb_funcallv(step, idCmp, 1, &zero), step, zero);
377 if (cmp < 0) {
378 rb_raise(rb_eArgError, "step can't be negative");
379 }
380 else if (cmp == 0) {
381 rb_raise(rb_eArgError, "step can't be 0");
382 }
383 return step;
384}
385
386static VALUE
387range_step_size(VALUE range, VALUE args, VALUE eobj)
388{
389 VALUE b = RANGE_BEG(range), e = RANGE_END(range);
390 VALUE step = INT2FIX(1);
391 if (args) {
392 step = check_step_domain(RARRAY_AREF(args, 0));
393 }
394
396 return ruby_num_interval_step_size(b, e, step, EXCL(range));
397 }
398 return Qnil;
399}
400
401/*
402 * call-seq:
403 * step(n = 1) {|element| ... } -> self
404 * step(n = 1) -> enumerator
405 *
406 * Iterates over the elements of +self+.
407 *
408 * With a block given and no argument,
409 * calls the block each element of the range; returns +self+:
410 *
411 * a = []
412 * (1..5).step {|element| a.push(element) } # => 1..5
413 * a # => [1, 2, 3, 4, 5]
414 * a = []
415 * ('a'..'e').step {|element| a.push(element) } # => "a".."e"
416 * a # => ["a", "b", "c", "d", "e"]
417 *
418 * With a block given and a positive integer argument +n+ given,
419 * calls the block with element +0+, element +n+, element <tt>2n</tt>, and so on:
420 *
421 * a = []
422 * (1..5).step(2) {|element| a.push(element) } # => 1..5
423 * a # => [1, 3, 5]
424 * a = []
425 * ('a'..'e').step(2) {|element| a.push(element) } # => "a".."e"
426 * a # => ["a", "c", "e"]
427 *
428 * With no block given, returns an enumerator,
429 * which will be of class Enumerator::ArithmeticSequence if +self+ is numeric;
430 * otherwise of class Enumerator:
431 *
432 * e = (1..5).step(2) # => ((1..5).step(2))
433 * e.class # => Enumerator::ArithmeticSequence
434 * ('a'..'e').step # => #<Enumerator: ...>
435 *
436 * Related: Range#%.
437 */
438static VALUE
439range_step(int argc, VALUE *argv, VALUE range)
440{
441 VALUE b, e, step, tmp;
442
443 b = RANGE_BEG(range);
444 e = RANGE_END(range);
445 step = (!rb_check_arity(argc, 0, 1) ? INT2FIX(1) : argv[0]);
446
447 if (!rb_block_given_p()) {
448 if (!rb_obj_is_kind_of(step, rb_cNumeric)) {
449 step = rb_to_int(step);
450 }
451 if (rb_equal(step, INT2FIX(0))) {
452 rb_raise(rb_eArgError, "step can't be 0");
453 }
454
455 const VALUE b_num_p = rb_obj_is_kind_of(b, rb_cNumeric);
456 const VALUE e_num_p = rb_obj_is_kind_of(e, rb_cNumeric);
457 if ((b_num_p && (NIL_P(e) || e_num_p)) || (NIL_P(b) && e_num_p)) {
458 return rb_arith_seq_new(range, ID2SYM(rb_frame_this_func()), argc, argv,
459 range_step_size, b, e, step, EXCL(range));
460 }
461
462 RETURN_SIZED_ENUMERATOR(range, argc, argv, range_step_size);
463 }
464
465 step = check_step_domain(step);
466 VALUE iter[2] = {INT2FIX(1), step};
467
468 if (FIXNUM_P(b) && NIL_P(e) && FIXNUM_P(step)) {
469 long i = FIX2LONG(b), unit = FIX2LONG(step);
470 do {
471 rb_yield(LONG2FIX(i));
472 i += unit; /* FIXABLE+FIXABLE never overflow */
473 } while (FIXABLE(i));
474 b = LONG2NUM(i);
475
476 for (;; b = rb_big_plus(b, step))
477 rb_yield(b);
478 }
479 else if (FIXNUM_P(b) && FIXNUM_P(e) && FIXNUM_P(step)) { /* fixnums are special */
480 long end = FIX2LONG(e);
481 long i, unit = FIX2LONG(step);
482
483 if (!EXCL(range))
484 end += 1;
485 i = FIX2LONG(b);
486 while (i < end) {
487 rb_yield(LONG2NUM(i));
488 if (i + unit < i) break;
489 i += unit;
490 }
491
492 }
493 else if (SYMBOL_P(b) && (NIL_P(e) || SYMBOL_P(e))) { /* symbols are special */
494 b = rb_sym2str(b);
495 if (NIL_P(e)) {
496 rb_str_upto_endless_each(b, sym_step_i, (VALUE)iter);
497 }
498 else {
499 rb_str_upto_each(b, rb_sym2str(e), EXCL(range), sym_step_i, (VALUE)iter);
500 }
501 }
502 else if (ruby_float_step(b, e, step, EXCL(range), TRUE)) {
503 /* done */
504 }
505 else if (rb_obj_is_kind_of(b, rb_cNumeric) ||
506 !NIL_P(rb_check_to_integer(b, "to_int")) ||
507 !NIL_P(rb_check_to_integer(e, "to_int"))) {
508 ID op = EXCL(range) ? '<' : idLE;
509 VALUE v = b;
510 int i = 0;
511
512 while (NIL_P(e) || RTEST(rb_funcall(v, op, 1, e))) {
513 rb_yield(v);
514 i++;
515 v = rb_funcall(b, '+', 1, rb_funcall(INT2NUM(i), '*', 1, step));
516 }
517 }
518 else {
519 tmp = rb_check_string_type(b);
520
521 if (!NIL_P(tmp)) {
522 b = tmp;
523 if (NIL_P(e)) {
524 rb_str_upto_endless_each(b, step_i, (VALUE)iter);
525 }
526 else {
527 rb_str_upto_each(b, e, EXCL(range), step_i, (VALUE)iter);
528 }
529 }
530 else {
531 if (!discrete_object_p(b)) {
532 rb_raise(rb_eTypeError, "can't iterate from %s",
534 }
535 if (!NIL_P(e))
536 range_each_func(range, step_i, (VALUE)iter);
537 else
538 for (;; b = rb_funcallv(b, id_succ, 0, 0))
539 step_i(b, (VALUE)iter);
540 }
541 }
542 return range;
543}
544
545/*
546 * call-seq:
547 * %(n) {|element| ... } -> self
548 * %(n) -> enumerator
549 *
550 * Iterates over the elements of +self+.
551 *
552 * With a block given, calls the block with selected elements of the range;
553 * returns +self+:
554 *
555 * a = []
556 * (1..5).%(2) {|element| a.push(element) } # => 1..5
557 * a # => [1, 3, 5]
558 * a = []
559 * ('a'..'e').%(2) {|element| a.push(element) } # => "a".."e"
560 * a # => ["a", "c", "e"]
561 *
562 * With no block given, returns an enumerator,
563 * which will be of class Enumerator::ArithmeticSequence if +self+ is numeric;
564 * otherwise of class Enumerator:
565 *
566 * e = (1..5) % 2 # => ((1..5).%(2))
567 * e.class # => Enumerator::ArithmeticSequence
568 * ('a'..'e') % 2 # => #<Enumerator: ...>
569 *
570 * Related: Range#step.
571 */
572static VALUE
573range_percent_step(VALUE range, VALUE step)
574{
575 return range_step(1, &step, range);
576}
577
578#if SIZEOF_DOUBLE == 8 && defined(HAVE_INT64_T)
579union int64_double {
580 int64_t i;
581 double d;
582};
583
584static VALUE
585int64_as_double_to_num(int64_t i)
586{
587 union int64_double convert;
588 if (i < 0) {
589 convert.i = -i;
590 return DBL2NUM(-convert.d);
591 }
592 else {
593 convert.i = i;
594 return DBL2NUM(convert.d);
595 }
596}
597
598static int64_t
599double_as_int64(double d)
600{
601 union int64_double convert;
602 convert.d = fabs(d);
603 return d < 0 ? -convert.i : convert.i;
604}
605#endif
606
607static int
608is_integer_p(VALUE v)
609{
610 if (rb_integer_type_p(v)) {
611 return true;
612 }
613
614 ID id_integer_p;
615 VALUE is_int;
616 CONST_ID(id_integer_p, "integer?");
617 is_int = rb_check_funcall(v, id_integer_p, 0, 0);
618 return RTEST(is_int) && !UNDEF_P(is_int);
619}
620
621static VALUE
622bsearch_integer_range(VALUE beg, VALUE end, int excl)
623{
624 VALUE satisfied = Qnil;
625 int smaller;
626
627#define BSEARCH_CHECK(expr) \
628 do { \
629 VALUE val = (expr); \
630 VALUE v = rb_yield(val); \
631 if (FIXNUM_P(v)) { \
632 if (v == INT2FIX(0)) return val; \
633 smaller = (SIGNED_VALUE)v < 0; \
634 } \
635 else if (v == Qtrue) { \
636 satisfied = val; \
637 smaller = 1; \
638 } \
639 else if (!RTEST(v)) { \
640 smaller = 0; \
641 } \
642 else if (rb_obj_is_kind_of(v, rb_cNumeric)) { \
643 int cmp = rb_cmpint(rb_funcall(v, id_cmp, 1, INT2FIX(0)), v, INT2FIX(0)); \
644 if (!cmp) return val; \
645 smaller = cmp < 0; \
646 } \
647 else { \
648 rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE \
649 " (must be numeric, true, false or nil)", \
650 rb_obj_class(v)); \
651 } \
652 } while (0)
653
654 VALUE low = rb_to_int(beg);
655 VALUE high = rb_to_int(end);
656 VALUE mid;
657 ID id_div;
658 CONST_ID(id_div, "div");
659
660 if (!excl) high = rb_funcall(high, '+', 1, INT2FIX(1));
661 low = rb_funcall(low, '-', 1, INT2FIX(1));
662
663 /*
664 * This loop must continue while low + 1 < high.
665 * Instead of checking low + 1 < high, check low < mid, where mid = (low + high) / 2.
666 * This is to avoid the cost of calculating low + 1 on each iteration.
667 * Note that this condition replacement is valid because Integer#div always rounds
668 * towards negative infinity.
669 */
670 while (mid = rb_funcall(rb_funcall(high, '+', 1, low), id_div, 1, INT2FIX(2)),
671 rb_cmpint(rb_funcall(low, id_cmp, 1, mid), low, mid) < 0) {
672 BSEARCH_CHECK(mid);
673 if (smaller) {
674 high = mid;
675 }
676 else {
677 low = mid;
678 }
679 }
680 return satisfied;
681}
682
683/*
684 * call-seq:
685 * bsearch {|obj| block } -> value
686 *
687 * Returns an element from +self+ selected by a binary search.
688 *
689 * See {Binary Searching}[rdoc-ref:bsearch.rdoc].
690 *
691 */
692
693static VALUE
694range_bsearch(VALUE range)
695{
696 VALUE beg, end, satisfied = Qnil;
697 int smaller;
698
699 /* Implementation notes:
700 * Floats are handled by mapping them to 64 bits integers.
701 * Apart from sign issues, floats and their 64 bits integer have the
702 * same order, assuming they are represented as exponent followed
703 * by the mantissa. This is true with or without implicit bit.
704 *
705 * Finding the average of two ints needs to be careful about
706 * potential overflow (since float to long can use 64 bits).
707 *
708 * The half-open interval (low, high] indicates where the target is located.
709 * The loop continues until low and high are adjacent.
710 *
711 * -1/2 can be either 0 or -1 in C89. However, when low and high are not adjacent,
712 * the rounding direction of mid = (low + high) / 2 does not affect the result of
713 * the binary search.
714 *
715 * Note that -0.0 is mapped to the same int as 0.0 as we don't want
716 * (-1...0.0).bsearch to yield -0.0.
717 */
718
719#define BSEARCH(conv, excl) \
720 do { \
721 RETURN_ENUMERATOR(range, 0, 0); \
722 if (!(excl)) high++; \
723 low--; \
724 while (low + 1 < high) { \
725 mid = ((high < 0) == (low < 0)) ? low + ((high - low) / 2) \
726 : (low + high) / 2; \
727 BSEARCH_CHECK(conv(mid)); \
728 if (smaller) { \
729 high = mid; \
730 } \
731 else { \
732 low = mid; \
733 } \
734 } \
735 return satisfied; \
736 } while (0)
737
738#define BSEARCH_FIXNUM(beg, end, excl) \
739 do { \
740 long low = FIX2LONG(beg); \
741 long high = FIX2LONG(end); \
742 long mid; \
743 BSEARCH(INT2FIX, (excl)); \
744 } while (0)
745
746 beg = RANGE_BEG(range);
747 end = RANGE_END(range);
748
749 if (FIXNUM_P(beg) && FIXNUM_P(end)) {
750 BSEARCH_FIXNUM(beg, end, EXCL(range));
751 }
752#if SIZEOF_DOUBLE == 8 && defined(HAVE_INT64_T)
753 else if (RB_FLOAT_TYPE_P(beg) || RB_FLOAT_TYPE_P(end)) {
754 int64_t low = double_as_int64(NIL_P(beg) ? -HUGE_VAL : RFLOAT_VALUE(rb_Float(beg)));
755 int64_t high = double_as_int64(NIL_P(end) ? HUGE_VAL : RFLOAT_VALUE(rb_Float(end)));
756 int64_t mid;
757 BSEARCH(int64_as_double_to_num, EXCL(range));
758 }
759#endif
760 else if (is_integer_p(beg) && is_integer_p(end)) {
761 RETURN_ENUMERATOR(range, 0, 0);
762 return bsearch_integer_range(beg, end, EXCL(range));
763 }
764 else if (is_integer_p(beg) && NIL_P(end)) {
765 VALUE diff = LONG2FIX(1);
766 RETURN_ENUMERATOR(range, 0, 0);
767 while (1) {
768 VALUE mid = rb_funcall(beg, '+', 1, diff);
769 BSEARCH_CHECK(mid);
770 if (smaller) {
771 if (FIXNUM_P(beg) && FIXNUM_P(mid)) {
772 BSEARCH_FIXNUM(beg, mid, false);
773 }
774 else {
775 return bsearch_integer_range(beg, mid, false);
776 }
777 }
778 diff = rb_funcall(diff, '*', 1, LONG2FIX(2));
779 beg = mid;
780 }
781 }
782 else if (NIL_P(beg) && is_integer_p(end)) {
783 VALUE diff = LONG2FIX(-1);
784 RETURN_ENUMERATOR(range, 0, 0);
785 while (1) {
786 VALUE mid = rb_funcall(end, '+', 1, diff);
787 BSEARCH_CHECK(mid);
788 if (!smaller) {
789 if (FIXNUM_P(mid) && FIXNUM_P(end)) {
790 BSEARCH_FIXNUM(mid, end, false);
791 }
792 else {
793 return bsearch_integer_range(mid, end, false);
794 }
795 }
796 diff = rb_funcall(diff, '*', 1, LONG2FIX(2));
797 end = mid;
798 }
799 }
800 else {
801 rb_raise(rb_eTypeError, "can't do binary search for %s", rb_obj_classname(beg));
802 }
803 return range;
804}
805
806static int
807each_i(VALUE v, VALUE arg)
808{
809 rb_yield(v);
810 return 0;
811}
812
813static int
814sym_each_i(VALUE v, VALUE arg)
815{
816 return each_i(rb_str_intern(v), arg);
817}
818
819/*
820 * call-seq:
821 * size -> non_negative_integer or Infinity or nil
822 *
823 * Returns the count of elements in +self+
824 * if both begin and end values are numeric;
825 * otherwise, returns +nil+:
826 *
827 * (1..4).size # => 4
828 * (1...4).size # => 3
829 * (1..).size # => Infinity
830 * ('a'..'z').size #=> nil
831 *
832 * Related: Range#count.
833 */
834
835static VALUE
836range_size(VALUE range)
837{
838 VALUE b = RANGE_BEG(range), e = RANGE_END(range);
841 return ruby_num_interval_step_size(b, e, INT2FIX(1), EXCL(range));
842 }
843 if (NIL_P(e)) {
844 return DBL2NUM(HUGE_VAL);
845 }
846 }
847 else if (NIL_P(b)) {
849 return DBL2NUM(HUGE_VAL);
850 }
851 }
852
853 return Qnil;
854}
855
856/*
857 * call-seq:
858 * to_a -> array
859 *
860 * Returns an array containing the elements in +self+, if a finite collection;
861 * raises an exception otherwise.
862 *
863 * (1..4).to_a # => [1, 2, 3, 4]
864 * (1...4).to_a # => [1, 2, 3]
865 * ('a'..'d').to_a # => ["a", "b", "c", "d"]
866 *
867 */
868
869static VALUE
870range_to_a(VALUE range)
871{
872 if (NIL_P(RANGE_END(range))) {
873 rb_raise(rb_eRangeError, "cannot convert endless range to an array");
874 }
875 return rb_call_super(0, 0);
876}
877
878static VALUE
879range_enum_size(VALUE range, VALUE args, VALUE eobj)
880{
881 return range_size(range);
882}
883
885static void
886range_each_bignum_endless(VALUE beg)
887{
888 for (;; beg = rb_big_plus(beg, INT2FIX(1))) {
889 rb_yield(beg);
890 }
892}
893
895static void
896range_each_fixnum_endless(VALUE beg)
897{
898 for (long i = FIX2LONG(beg); FIXABLE(i); i++) {
899 rb_yield(LONG2FIX(i));
900 }
901
902 range_each_bignum_endless(LONG2NUM(RUBY_FIXNUM_MAX + 1));
904}
905
906static VALUE
907range_each_fixnum_loop(VALUE beg, VALUE end, VALUE range)
908{
909 long lim = FIX2LONG(end) + !EXCL(range);
910 for (long i = FIX2LONG(beg); i < lim; i++) {
911 rb_yield(LONG2FIX(i));
912 }
913 return range;
914}
915
916/*
917 * call-seq:
918 * each {|element| ... } -> self
919 * each -> an_enumerator
920 *
921 * With a block given, passes each element of +self+ to the block:
922 *
923 * a = []
924 * (1..4).each {|element| a.push(element) } # => 1..4
925 * a # => [1, 2, 3, 4]
926 *
927 * Raises an exception unless <tt>self.first.respond_to?(:succ)</tt>.
928 *
929 * With no block given, returns an enumerator.
930 *
931 */
932
933static VALUE
934range_each(VALUE range)
935{
936 VALUE beg, end;
937 long i;
938
939 RETURN_SIZED_ENUMERATOR(range, 0, 0, range_enum_size);
940
941 beg = RANGE_BEG(range);
942 end = RANGE_END(range);
943
944 if (FIXNUM_P(beg) && NIL_P(end)) {
945 range_each_fixnum_endless(beg);
946 }
947 else if (FIXNUM_P(beg) && FIXNUM_P(end)) { /* fixnums are special */
948 return range_each_fixnum_loop(beg, end, range);
949 }
950 else if (RB_INTEGER_TYPE_P(beg) && (NIL_P(end) || RB_INTEGER_TYPE_P(end))) {
951 if (SPECIAL_CONST_P(end) || RBIGNUM_POSITIVE_P(end)) { /* end >= FIXNUM_MIN */
952 if (!FIXNUM_P(beg)) {
953 if (RBIGNUM_NEGATIVE_P(beg)) {
954 do {
955 rb_yield(beg);
956 } while (!FIXNUM_P(beg = rb_big_plus(beg, INT2FIX(1))));
957 if (NIL_P(end)) range_each_fixnum_endless(beg);
958 if (FIXNUM_P(end)) return range_each_fixnum_loop(beg, end, range);
959 }
960 else {
961 if (NIL_P(end)) range_each_bignum_endless(beg);
962 if (FIXNUM_P(end)) return range;
963 }
964 }
965 if (FIXNUM_P(beg)) {
966 i = FIX2LONG(beg);
967 do {
968 rb_yield(LONG2FIX(i));
969 } while (POSFIXABLE(++i));
970 beg = LONG2NUM(i);
971 }
972 ASSUME(!FIXNUM_P(beg));
973 ASSUME(!SPECIAL_CONST_P(end));
974 }
975 if (!FIXNUM_P(beg) && RBIGNUM_SIGN(beg) == RBIGNUM_SIGN(end)) {
976 if (EXCL(range)) {
977 while (rb_big_cmp(beg, end) == INT2FIX(-1)) {
978 rb_yield(beg);
979 beg = rb_big_plus(beg, INT2FIX(1));
980 }
981 }
982 else {
983 VALUE c;
984 while ((c = rb_big_cmp(beg, end)) != INT2FIX(1)) {
985 rb_yield(beg);
986 if (c == INT2FIX(0)) break;
987 beg = rb_big_plus(beg, INT2FIX(1));
988 }
989 }
990 }
991 }
992 else if (SYMBOL_P(beg) && (NIL_P(end) || SYMBOL_P(end))) { /* symbols are special */
993 beg = rb_sym2str(beg);
994 if (NIL_P(end)) {
995 rb_str_upto_endless_each(beg, sym_each_i, 0);
996 }
997 else {
998 rb_str_upto_each(beg, rb_sym2str(end), EXCL(range), sym_each_i, 0);
999 }
1000 }
1001 else {
1002 VALUE tmp = rb_check_string_type(beg);
1003
1004 if (!NIL_P(tmp)) {
1005 if (!NIL_P(end)) {
1006 rb_str_upto_each(tmp, end, EXCL(range), each_i, 0);
1007 }
1008 else {
1009 rb_str_upto_endless_each(tmp, each_i, 0);
1010 }
1011 }
1012 else {
1013 if (!discrete_object_p(beg)) {
1014 rb_raise(rb_eTypeError, "can't iterate from %s",
1015 rb_obj_classname(beg));
1016 }
1017 if (!NIL_P(end))
1018 range_each_func(range, each_i, 0);
1019 else
1020 for (;; beg = rb_funcallv(beg, id_succ, 0, 0))
1021 rb_yield(beg);
1022 }
1023 }
1024 return range;
1025}
1026
1028static void
1029range_reverse_each_bignum_beginless(VALUE end)
1030{
1032
1033 for (;; end = rb_big_minus(end, INT2FIX(1))) {
1034 rb_yield(end);
1035 }
1037}
1038
1039static void
1040range_reverse_each_bignum(VALUE beg, VALUE end)
1041{
1043
1044 VALUE c;
1045 while ((c = rb_big_cmp(beg, end)) != INT2FIX(1)) {
1046 rb_yield(end);
1047 if (c == INT2FIX(0)) break;
1048 end = rb_big_minus(end, INT2FIX(1));
1049 }
1050}
1051
1052static void
1053range_reverse_each_positive_bignum_section(VALUE beg, VALUE end)
1054{
1055 RUBY_ASSERT(!NIL_P(end));
1056
1057 if (FIXNUM_P(end) || RBIGNUM_NEGATIVE_P(end)) return;
1058
1059 if (NIL_P(beg) || FIXNUM_P(beg) || RBIGNUM_NEGATIVE_P(beg)) {
1060 beg = LONG2NUM(FIXNUM_MAX + 1);
1061 }
1062
1063 range_reverse_each_bignum(beg, end);
1064}
1065
1066static void
1067range_reverse_each_fixnum_section(VALUE beg, VALUE end)
1068{
1069 RUBY_ASSERT(!NIL_P(end));
1070
1071 if (!FIXNUM_P(beg)) {
1072 if (!NIL_P(beg) && RBIGNUM_POSITIVE_P(beg)) return;
1073
1074 beg = LONG2FIX(FIXNUM_MIN);
1075 }
1076
1077 if (!FIXNUM_P(end)) {
1078 if (RBIGNUM_NEGATIVE_P(end)) return;
1079
1080 end = LONG2FIX(FIXNUM_MAX);
1081 }
1082
1083 long b = FIX2LONG(beg);
1084 long e = FIX2LONG(end);
1085 for (long i = e; i >= b; --i) {
1086 rb_yield(LONG2FIX(i));
1087 }
1088}
1089
1090static void
1091range_reverse_each_negative_bignum_section(VALUE beg, VALUE end)
1092{
1093 RUBY_ASSERT(!NIL_P(end));
1094
1095 if (FIXNUM_P(end) || RBIGNUM_POSITIVE_P(end)) {
1096 end = LONG2NUM(FIXNUM_MIN - 1);
1097 }
1098
1099 if (NIL_P(beg)) {
1100 range_reverse_each_bignum_beginless(end);
1101 }
1102
1103 if (FIXNUM_P(beg) || RBIGNUM_POSITIVE_P(beg)) return;
1104
1105 range_reverse_each_bignum(beg, end);
1106}
1107
1108/*
1109 * call-seq:
1110 * reverse_each {|element| ... } -> self
1111 * reverse_each -> an_enumerator
1112 *
1113 * With a block given, passes each element of +self+ to the block in reverse order:
1114 *
1115 * a = []
1116 * (1..4).reverse_each {|element| a.push(element) } # => 1..4
1117 * a # => [4, 3, 2, 1]
1118 *
1119 * a = []
1120 * (1...4).reverse_each {|element| a.push(element) } # => 1...4
1121 * a # => [3, 2, 1]
1122 *
1123 * With no block given, returns an enumerator.
1124 *
1125 */
1126
1127static VALUE
1128range_reverse_each(VALUE range)
1129{
1130 RETURN_SIZED_ENUMERATOR(range, 0, 0, range_enum_size);
1131
1132 VALUE beg = RANGE_BEG(range);
1133 VALUE end = RANGE_END(range);
1134 int excl = EXCL(range);
1135
1136 if (NIL_P(end)) {
1137 rb_raise(rb_eTypeError, "can't iterate from %s",
1138 rb_obj_classname(end));
1139 }
1140
1141 if (FIXNUM_P(beg) && FIXNUM_P(end)) {
1142 if (excl) {
1143 if (end == LONG2FIX(FIXNUM_MIN)) return range;
1144
1145 end = rb_int_minus(end, INT2FIX(1));
1146 }
1147
1148 range_reverse_each_fixnum_section(beg, end);
1149 }
1150 else if ((NIL_P(beg) || RB_INTEGER_TYPE_P(beg)) && RB_INTEGER_TYPE_P(end)) {
1151 if (excl) {
1152 end = rb_int_minus(end, INT2FIX(1));
1153 }
1154 range_reverse_each_positive_bignum_section(beg, end);
1155 range_reverse_each_fixnum_section(beg, end);
1156 range_reverse_each_negative_bignum_section(beg, end);
1157 }
1158 else {
1159 return rb_call_super(0, NULL);
1160 }
1161
1162 return range;
1163}
1164
1165/*
1166 * call-seq:
1167 * self.begin -> object
1168 *
1169 * Returns the object that defines the beginning of +self+.
1170 *
1171 * (1..4).begin # => 1
1172 * (..2).begin # => nil
1173 *
1174 * Related: Range#first, Range#end.
1175 */
1176
1177static VALUE
1178range_begin(VALUE range)
1179{
1180 return RANGE_BEG(range);
1181}
1182
1183
1184/*
1185 * call-seq:
1186 * self.end -> object
1187 *
1188 * Returns the object that defines the end of +self+.
1189 *
1190 * (1..4).end # => 4
1191 * (1...4).end # => 4
1192 * (1..).end # => nil
1193 *
1194 * Related: Range#begin, Range#last.
1195 */
1196
1197
1198static VALUE
1199range_end(VALUE range)
1200{
1201 return RANGE_END(range);
1202}
1203
1204
1205static VALUE
1206first_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, cbarg))
1207{
1208 VALUE *ary = (VALUE *)cbarg;
1209 long n = NUM2LONG(ary[0]);
1210
1211 if (n <= 0) {
1212 rb_iter_break();
1213 }
1214 rb_ary_push(ary[1], i);
1215 n--;
1216 ary[0] = LONG2NUM(n);
1217 return Qnil;
1218}
1219
1220/*
1221 * call-seq:
1222 * first -> object
1223 * first(n) -> array
1224 *
1225 * With no argument, returns the first element of +self+, if it exists:
1226 *
1227 * (1..4).first # => 1
1228 * ('a'..'d').first # => "a"
1229 *
1230 * With non-negative integer argument +n+ given,
1231 * returns the first +n+ elements in an array:
1232 *
1233 * (1..10).first(3) # => [1, 2, 3]
1234 * (1..10).first(0) # => []
1235 * (1..4).first(50) # => [1, 2, 3, 4]
1236 *
1237 * Raises an exception if there is no first element:
1238 *
1239 * (..4).first # Raises RangeError
1240 */
1241
1242static VALUE
1243range_first(int argc, VALUE *argv, VALUE range)
1244{
1245 VALUE n, ary[2];
1246
1247 if (NIL_P(RANGE_BEG(range))) {
1248 rb_raise(rb_eRangeError, "cannot get the first element of beginless range");
1249 }
1250 if (argc == 0) return RANGE_BEG(range);
1251
1252 rb_scan_args(argc, argv, "1", &n);
1253 ary[0] = n;
1254 ary[1] = rb_ary_new2(NUM2LONG(n));
1255 rb_block_call(range, idEach, 0, 0, first_i, (VALUE)ary);
1256
1257 return ary[1];
1258}
1259
1260static VALUE
1261rb_int_range_last(int argc, VALUE *argv, VALUE range)
1262{
1263 static const VALUE ONE = INT2FIX(1);
1264
1265 VALUE b, e, len_1, len, nv, ary;
1266 int x;
1267 long n;
1268
1269 assert(argc > 0);
1270
1271 b = RANGE_BEG(range);
1272 e = RANGE_END(range);
1273 assert(RB_INTEGER_TYPE_P(b) && RB_INTEGER_TYPE_P(e));
1274
1275 x = EXCL(range);
1276
1277 len_1 = rb_int_minus(e, b);
1278 if (x) {
1279 e = rb_int_minus(e, ONE);
1280 len = len_1;
1281 }
1282 else {
1283 len = rb_int_plus(len_1, ONE);
1284 }
1285
1286 if (FIXNUM_ZERO_P(len) || rb_num_negative_p(len)) {
1287 return rb_ary_new_capa(0);
1288 }
1289
1290 rb_scan_args(argc, argv, "1", &nv);
1291 n = NUM2LONG(nv);
1292 if (n < 0) {
1293 rb_raise(rb_eArgError, "negative array size");
1294 }
1295
1296 nv = LONG2NUM(n);
1297 if (RTEST(rb_int_gt(nv, len))) {
1298 nv = len;
1299 n = NUM2LONG(nv);
1300 }
1301
1302 ary = rb_ary_new_capa(n);
1303 b = rb_int_minus(e, nv);
1304 while (n) {
1305 b = rb_int_plus(b, ONE);
1306 rb_ary_push(ary, b);
1307 --n;
1308 }
1309
1310 return ary;
1311}
1312
1313/*
1314 * call-seq:
1315 * last -> object
1316 * last(n) -> array
1317 *
1318 * With no argument, returns the last element of +self+, if it exists:
1319 *
1320 * (1..4).last # => 4
1321 * ('a'..'d').last # => "d"
1322 *
1323 * Note that +last+ with no argument returns the end element of +self+
1324 * even if #exclude_end? is +true+:
1325 *
1326 * (1...4).last # => 4
1327 * ('a'...'d').last # => "d"
1328 *
1329 * With non-negative integer argument +n+ given,
1330 * returns the last +n+ elements in an array:
1331 *
1332 * (1..10).last(3) # => [8, 9, 10]
1333 * (1..10).last(0) # => []
1334 * (1..4).last(50) # => [1, 2, 3, 4]
1335 *
1336 * Note that +last+ with argument does not return the end element of +self+
1337 * if #exclude_end? it +true+:
1338 *
1339 * (1...4).last(3) # => [1, 2, 3]
1340 * ('a'...'d').last(3) # => ["a", "b", "c"]
1341 *
1342 * Raises an exception if there is no last element:
1343 *
1344 * (1..).last # Raises RangeError
1345 *
1346 */
1347
1348static VALUE
1349range_last(int argc, VALUE *argv, VALUE range)
1350{
1351 VALUE b, e;
1352
1353 if (NIL_P(RANGE_END(range))) {
1354 rb_raise(rb_eRangeError, "cannot get the last element of endless range");
1355 }
1356 if (argc == 0) return RANGE_END(range);
1357
1358 b = RANGE_BEG(range);
1359 e = RANGE_END(range);
1360 if (RB_INTEGER_TYPE_P(b) && RB_INTEGER_TYPE_P(e) &&
1361 RB_LIKELY(rb_method_basic_definition_p(rb_cRange, idEach))) {
1362 return rb_int_range_last(argc, argv, range);
1363 }
1364 return rb_ary_last(argc, argv, rb_Array(range));
1365}
1366
1367
1368/*
1369 * call-seq:
1370 * min -> object
1371 * min(n) -> array
1372 * min {|a, b| ... } -> object
1373 * min(n) {|a, b| ... } -> array
1374 *
1375 * Returns the minimum value in +self+,
1376 * using method <tt><=></tt> or a given block for comparison.
1377 *
1378 * With no argument and no block given,
1379 * returns the minimum-valued element of +self+.
1380 *
1381 * (1..4).min # => 1
1382 * ('a'..'d').min # => "a"
1383 * (-4..-1).min # => -4
1384 *
1385 * With non-negative integer argument +n+ given, and no block given,
1386 * returns the +n+ minimum-valued elements of +self+ in an array:
1387 *
1388 * (1..4).min(2) # => [1, 2]
1389 * ('a'..'d').min(2) # => ["a", "b"]
1390 * (-4..-1).min(2) # => [-4, -3]
1391 * (1..4).min(50) # => [1, 2, 3, 4]
1392 *
1393 * If a block is given, it is called:
1394 *
1395 * - First, with the first two element of +self+.
1396 * - Then, sequentially, with the so-far minimum value and the next element of +self+.
1397 *
1398 * To illustrate:
1399 *
1400 * (1..4).min {|a, b| p [a, b]; a <=> b } # => 1
1401 *
1402 * Output:
1403 *
1404 * [2, 1]
1405 * [3, 1]
1406 * [4, 1]
1407 *
1408 * With no argument and a block given,
1409 * returns the return value of the last call to the block:
1410 *
1411 * (1..4).min {|a, b| -(a <=> b) } # => 4
1412 *
1413 * With non-negative integer argument +n+ given, and a block given,
1414 * returns the return values of the last +n+ calls to the block in an array:
1415 *
1416 * (1..4).min(2) {|a, b| -(a <=> b) } # => [4, 3]
1417 * (1..4).min(50) {|a, b| -(a <=> b) } # => [4, 3, 2, 1]
1418 *
1419 * Returns an empty array if +n+ is zero:
1420 *
1421 * (1..4).min(0) # => []
1422 * (1..4).min(0) {|a, b| -(a <=> b) } # => []
1423 *
1424 * Returns +nil+ or an empty array if:
1425 *
1426 * - The begin value of the range is larger than the end value:
1427 *
1428 * (4..1).min # => nil
1429 * (4..1).min(2) # => []
1430 * (4..1).min {|a, b| -(a <=> b) } # => nil
1431 * (4..1).min(2) {|a, b| -(a <=> b) } # => []
1432 *
1433 * - The begin value of an exclusive range is equal to the end value:
1434 *
1435 * (1...1).min # => nil
1436 * (1...1).min(2) # => []
1437 * (1...1).min {|a, b| -(a <=> b) } # => nil
1438 * (1...1).min(2) {|a, b| -(a <=> b) } # => []
1439 *
1440 * Raises an exception if either:
1441 *
1442 * - +self+ is a beginless range: <tt>(..4)</tt>.
1443 * - A block is given and +self+ is an endless range.
1444 *
1445 * Related: Range#max, Range#minmax.
1446 */
1447
1448
1449static VALUE
1450range_min(int argc, VALUE *argv, VALUE range)
1451{
1452 if (NIL_P(RANGE_BEG(range))) {
1453 rb_raise(rb_eRangeError, "cannot get the minimum of beginless range");
1454 }
1455
1456 if (rb_block_given_p()) {
1457 if (NIL_P(RANGE_END(range))) {
1458 rb_raise(rb_eRangeError, "cannot get the minimum of endless range with custom comparison method");
1459 }
1460 return rb_call_super(argc, argv);
1461 }
1462 else if (argc != 0) {
1463 return range_first(argc, argv, range);
1464 }
1465 else {
1466 VALUE b = RANGE_BEG(range);
1467 VALUE e = RANGE_END(range);
1468 int c = NIL_P(e) ? -1 : OPTIMIZED_CMP(b, e);
1469
1470 if (c > 0 || (c == 0 && EXCL(range)))
1471 return Qnil;
1472 return b;
1473 }
1474}
1475
1476/*
1477 * call-seq:
1478 * max -> object
1479 * max(n) -> array
1480 * max {|a, b| ... } -> object
1481 * max(n) {|a, b| ... } -> array
1482 *
1483 * Returns the maximum value in +self+,
1484 * using method <tt><=></tt> or a given block for comparison.
1485 *
1486 * With no argument and no block given,
1487 * returns the maximum-valued element of +self+.
1488 *
1489 * (1..4).max # => 4
1490 * ('a'..'d').max # => "d"
1491 * (-4..-1).max # => -1
1492 *
1493 * With non-negative integer argument +n+ given, and no block given,
1494 * returns the +n+ maximum-valued elements of +self+ in an array:
1495 *
1496 * (1..4).max(2) # => [4, 3]
1497 * ('a'..'d').max(2) # => ["d", "c"]
1498 * (-4..-1).max(2) # => [-1, -2]
1499 * (1..4).max(50) # => [4, 3, 2, 1]
1500 *
1501 * If a block is given, it is called:
1502 *
1503 * - First, with the first two element of +self+.
1504 * - Then, sequentially, with the so-far maximum value and the next element of +self+.
1505 *
1506 * To illustrate:
1507 *
1508 * (1..4).max {|a, b| p [a, b]; a <=> b } # => 4
1509 *
1510 * Output:
1511 *
1512 * [2, 1]
1513 * [3, 2]
1514 * [4, 3]
1515 *
1516 * With no argument and a block given,
1517 * returns the return value of the last call to the block:
1518 *
1519 * (1..4).max {|a, b| -(a <=> b) } # => 1
1520 *
1521 * With non-negative integer argument +n+ given, and a block given,
1522 * returns the return values of the last +n+ calls to the block in an array:
1523 *
1524 * (1..4).max(2) {|a, b| -(a <=> b) } # => [1, 2]
1525 * (1..4).max(50) {|a, b| -(a <=> b) } # => [1, 2, 3, 4]
1526 *
1527 * Returns an empty array if +n+ is zero:
1528 *
1529 * (1..4).max(0) # => []
1530 * (1..4).max(0) {|a, b| -(a <=> b) } # => []
1531 *
1532 * Returns +nil+ or an empty array if:
1533 *
1534 * - The begin value of the range is larger than the end value:
1535 *
1536 * (4..1).max # => nil
1537 * (4..1).max(2) # => []
1538 * (4..1).max {|a, b| -(a <=> b) } # => nil
1539 * (4..1).max(2) {|a, b| -(a <=> b) } # => []
1540 *
1541 * - The begin value of an exclusive range is equal to the end value:
1542 *
1543 * (1...1).max # => nil
1544 * (1...1).max(2) # => []
1545 * (1...1).max {|a, b| -(a <=> b) } # => nil
1546 * (1...1).max(2) {|a, b| -(a <=> b) } # => []
1547 *
1548 * Raises an exception if either:
1549 *
1550 * - +self+ is a endless range: <tt>(1..)</tt>.
1551 * - A block is given and +self+ is a beginless range.
1552 *
1553 * Related: Range#min, Range#minmax.
1554 *
1555 */
1556
1557static VALUE
1558range_max(int argc, VALUE *argv, VALUE range)
1559{
1560 VALUE e = RANGE_END(range);
1561 int nm = FIXNUM_P(e) || rb_obj_is_kind_of(e, rb_cNumeric);
1562
1563 if (NIL_P(RANGE_END(range))) {
1564 rb_raise(rb_eRangeError, "cannot get the maximum of endless range");
1565 }
1566
1567 VALUE b = RANGE_BEG(range);
1568
1569 if (rb_block_given_p() || (EXCL(range) && !nm) || argc) {
1570 if (NIL_P(b)) {
1571 rb_raise(rb_eRangeError, "cannot get the maximum of beginless range with custom comparison method");
1572 }
1573 return rb_call_super(argc, argv);
1574 }
1575 else {
1576 int c = NIL_P(b) ? -1 : OPTIMIZED_CMP(b, e);
1577
1578 if (c > 0)
1579 return Qnil;
1580 if (EXCL(range)) {
1581 if (!RB_INTEGER_TYPE_P(e)) {
1582 rb_raise(rb_eTypeError, "cannot exclude non Integer end value");
1583 }
1584 if (c == 0) return Qnil;
1585 if (!RB_INTEGER_TYPE_P(b)) {
1586 rb_raise(rb_eTypeError, "cannot exclude end value with non Integer begin value");
1587 }
1588 if (FIXNUM_P(e)) {
1589 return LONG2NUM(FIX2LONG(e) - 1);
1590 }
1591 return rb_funcall(e, '-', 1, INT2FIX(1));
1592 }
1593 return e;
1594 }
1595}
1596
1597/*
1598 * call-seq:
1599 * minmax -> [object, object]
1600 * minmax {|a, b| ... } -> [object, object]
1601 *
1602 * Returns a 2-element array containing the minimum and maximum value in +self+,
1603 * either according to comparison method <tt><=></tt> or a given block.
1604 *
1605 * With no block given, returns the minimum and maximum values,
1606 * using <tt><=></tt> for comparison:
1607 *
1608 * (1..4).minmax # => [1, 4]
1609 * (1...4).minmax # => [1, 3]
1610 * ('a'..'d').minmax # => ["a", "d"]
1611 * (-4..-1).minmax # => [-4, -1]
1612 *
1613 * With a block given, the block must return an integer:
1614 *
1615 * - Negative if +a+ is smaller than +b+.
1616 * - Zero if +a+ and +b+ are equal.
1617 * - Positive if +a+ is larger than +b+.
1618 *
1619 * The block is called <tt>self.size</tt> times to compare elements;
1620 * returns a 2-element Array containing the minimum and maximum values from +self+,
1621 * per the block:
1622 *
1623 * (1..4).minmax {|a, b| -(a <=> b) } # => [4, 1]
1624 *
1625 * Returns <tt>[nil, nil]</tt> if:
1626 *
1627 * - The begin value of the range is larger than the end value:
1628 *
1629 * (4..1).minmax # => [nil, nil]
1630 * (4..1).minmax {|a, b| -(a <=> b) } # => [nil, nil]
1631 *
1632 * - The begin value of an exclusive range is equal to the end value:
1633 *
1634 * (1...1).minmax # => [nil, nil]
1635 * (1...1).minmax {|a, b| -(a <=> b) } # => [nil, nil]
1636 *
1637 * Raises an exception if +self+ is a beginless or an endless range.
1638 *
1639 * Related: Range#min, Range#max.
1640 *
1641 */
1642
1643static VALUE
1644range_minmax(VALUE range)
1645{
1646 if (rb_block_given_p()) {
1647 return rb_call_super(0, NULL);
1648 }
1649 return rb_assoc_new(
1650 rb_funcall(range, id_min, 0),
1651 rb_funcall(range, id_max, 0)
1652 );
1653}
1654
1655int
1656rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
1657{
1658 VALUE b, e;
1659 int excl;
1660
1661 if (rb_obj_is_kind_of(range, rb_cRange)) {
1662 b = RANGE_BEG(range);
1663 e = RANGE_END(range);
1664 excl = EXCL(range);
1665 }
1666 else if (RTEST(rb_obj_is_kind_of(range, rb_cArithSeq))) {
1667 return (int)Qfalse;
1668 }
1669 else {
1670 VALUE x;
1671 b = rb_check_funcall(range, id_beg, 0, 0);
1672 if (UNDEF_P(b)) return (int)Qfalse;
1673 e = rb_check_funcall(range, id_end, 0, 0);
1674 if (UNDEF_P(e)) return (int)Qfalse;
1675 x = rb_check_funcall(range, rb_intern("exclude_end?"), 0, 0);
1676 if (UNDEF_P(x)) return (int)Qfalse;
1677 excl = RTEST(x);
1678 }
1679 *begp = b;
1680 *endp = e;
1681 *exclp = excl;
1682 return (int)Qtrue;
1683}
1684
1685/* Extract the components of a Range.
1686 *
1687 * You can use +err+ to control the behavior of out-of-range and exception.
1688 *
1689 * When +err+ is 0 or 2, if the begin offset is greater than +len+,
1690 * it is out-of-range. The +RangeError+ is raised only if +err+ is 2,
1691 * in this case. If +err+ is 0, +Qnil+ will be returned.
1692 *
1693 * When +err+ is 1, the begin and end offsets won't be adjusted even if they
1694 * are greater than +len+. It allows +rb_ary_aset+ extends arrays.
1695 *
1696 * If the begin component of the given range is negative and is too-large
1697 * abstract value, the +RangeError+ is raised only +err+ is 1 or 2.
1698 *
1699 * The case of <code>err = 0</code> is used in item accessing methods such as
1700 * +rb_ary_aref+, +rb_ary_slice_bang+, and +rb_str_aref+.
1701 *
1702 * The case of <code>err = 1</code> is used in Array's methods such as
1703 * +rb_ary_aset+ and +rb_ary_fill+.
1704 *
1705 * The case of <code>err = 2</code> is used in +rb_str_aset+.
1706 */
1707VALUE
1708rb_range_component_beg_len(VALUE b, VALUE e, int excl,
1709 long *begp, long *lenp, long len, int err)
1710{
1711 long beg, end;
1712
1713 beg = NIL_P(b) ? 0 : NUM2LONG(b);
1714 end = NIL_P(e) ? -1 : NUM2LONG(e);
1715 if (NIL_P(e)) excl = 0;
1716 if (beg < 0) {
1717 beg += len;
1718 if (beg < 0)
1719 goto out_of_range;
1720 }
1721 if (end < 0)
1722 end += len;
1723 if (!excl)
1724 end++; /* include end point */
1725 if (err == 0 || err == 2) {
1726 if (beg > len)
1727 goto out_of_range;
1728 if (end > len)
1729 end = len;
1730 }
1731 len = end - beg;
1732 if (len < 0)
1733 len = 0;
1734
1735 *begp = beg;
1736 *lenp = len;
1737 return Qtrue;
1738
1739 out_of_range:
1740 return Qnil;
1741}
1742
1743VALUE
1744rb_range_beg_len(VALUE range, long *begp, long *lenp, long len, int err)
1745{
1746 VALUE b, e;
1747 int excl;
1748
1749 if (!rb_range_values(range, &b, &e, &excl))
1750 return Qfalse;
1751
1752 VALUE res = rb_range_component_beg_len(b, e, excl, begp, lenp, len, err);
1753 if (NIL_P(res) && err) {
1754 rb_raise(rb_eRangeError, "%+"PRIsVALUE" out of range", range);
1755 }
1756
1757 return res;
1758}
1759
1760/*
1761 * call-seq:
1762 * to_s -> string
1763 *
1764 * Returns a string representation of +self+,
1765 * including <tt>begin.to_s</tt> and <tt>end.to_s</tt>:
1766 *
1767 * (1..4).to_s # => "1..4"
1768 * (1...4).to_s # => "1...4"
1769 * (1..).to_s # => "1.."
1770 * (..4).to_s # => "..4"
1771 *
1772 * Note that returns from #to_s and #inspect may differ:
1773 *
1774 * ('a'..'d').to_s # => "a..d"
1775 * ('a'..'d').inspect # => "\"a\"..\"d\""
1776 *
1777 * Related: Range#inspect.
1778 *
1779 */
1780
1781static VALUE
1782range_to_s(VALUE range)
1783{
1784 VALUE str, str2;
1785
1786 str = rb_obj_as_string(RANGE_BEG(range));
1787 str2 = rb_obj_as_string(RANGE_END(range));
1788 str = rb_str_dup(str);
1789 rb_str_cat(str, "...", EXCL(range) ? 3 : 2);
1790 rb_str_append(str, str2);
1791
1792 return str;
1793}
1794
1795static VALUE
1796inspect_range(VALUE range, VALUE dummy, int recur)
1797{
1798 VALUE str, str2 = Qundef;
1799
1800 if (recur) {
1801 return rb_str_new2(EXCL(range) ? "(... ... ...)" : "(... .. ...)");
1802 }
1803 if (!NIL_P(RANGE_BEG(range)) || NIL_P(RANGE_END(range))) {
1804 str = rb_str_dup(rb_inspect(RANGE_BEG(range)));
1805 }
1806 else {
1807 str = rb_str_new(0, 0);
1808 }
1809 rb_str_cat(str, "...", EXCL(range) ? 3 : 2);
1810 if (NIL_P(RANGE_BEG(range)) || !NIL_P(RANGE_END(range))) {
1811 str2 = rb_inspect(RANGE_END(range));
1812 }
1813 if (!UNDEF_P(str2)) rb_str_append(str, str2);
1814
1815 return str;
1816}
1817
1818/*
1819 * call-seq:
1820 * inspect -> string
1821 *
1822 * Returns a string representation of +self+,
1823 * including <tt>begin.inspect</tt> and <tt>end.inspect</tt>:
1824 *
1825 * (1..4).inspect # => "1..4"
1826 * (1...4).inspect # => "1...4"
1827 * (1..).inspect # => "1.."
1828 * (..4).inspect # => "..4"
1829 *
1830 * Note that returns from #to_s and #inspect may differ:
1831 *
1832 * ('a'..'d').to_s # => "a..d"
1833 * ('a'..'d').inspect # => "\"a\"..\"d\""
1834 *
1835 * Related: Range#to_s.
1836 *
1837 */
1838
1839
1840static VALUE
1841range_inspect(VALUE range)
1842{
1843 return rb_exec_recursive(inspect_range, range, 0);
1844}
1845
1846static VALUE range_include_internal(VALUE range, VALUE val);
1847VALUE rb_str_include_range_p(VALUE beg, VALUE end, VALUE val, VALUE exclusive);
1848
1849/*
1850 * call-seq:
1851 * self === object -> true or false
1852 *
1853 * Returns +true+ if +object+ is between <tt>self.begin</tt> and <tt>self.end</tt>.
1854 * +false+ otherwise:
1855 *
1856 * (1..4) === 2 # => true
1857 * (1..4) === 5 # => false
1858 * (1..4) === 'a' # => false
1859 * (1..4) === 4 # => true
1860 * (1...4) === 4 # => false
1861 * ('a'..'d') === 'c' # => true
1862 * ('a'..'d') === 'e' # => false
1863 *
1864 * A case statement uses method <tt>===</tt>, and so:
1865 *
1866 * case 79
1867 * when (1..50)
1868 * "low"
1869 * when (51..75)
1870 * "medium"
1871 * when (76..100)
1872 * "high"
1873 * end # => "high"
1874 *
1875 * case "2.6.5"
1876 * when ..."2.4"
1877 * "EOL"
1878 * when "2.4"..."2.5"
1879 * "maintenance"
1880 * when "2.5"..."3.0"
1881 * "stable"
1882 * when "3.1"..
1883 * "upcoming"
1884 * end # => "stable"
1885 *
1886 */
1887
1888static VALUE
1889range_eqq(VALUE range, VALUE val)
1890{
1891 return r_cover_p(range, RANGE_BEG(range), RANGE_END(range), val);
1892}
1893
1894
1895/*
1896 * call-seq:
1897 * include?(object) -> true or false
1898 *
1899 * Returns +true+ if +object+ is an element of +self+, +false+ otherwise:
1900 *
1901 * (1..4).include?(2) # => true
1902 * (1..4).include?(5) # => false
1903 * (1..4).include?(4) # => true
1904 * (1...4).include?(4) # => false
1905 * ('a'..'d').include?('b') # => true
1906 * ('a'..'d').include?('e') # => false
1907 * ('a'..'d').include?('B') # => false
1908 * ('a'..'d').include?('d') # => true
1909 * ('a'...'d').include?('d') # => false
1910 *
1911 * If begin and end are numeric, #include? behaves like #cover?
1912 *
1913 * (1..3).include?(1.5) # => true
1914 * (1..3).cover?(1.5) # => true
1915 *
1916 * But when not numeric, the two methods may differ:
1917 *
1918 * ('a'..'d').include?('cc') # => false
1919 * ('a'..'d').cover?('cc') # => true
1920 *
1921 * Related: Range#cover?.
1922 */
1923
1924static VALUE
1925range_include(VALUE range, VALUE val)
1926{
1927 VALUE ret = range_include_internal(range, val);
1928 if (!UNDEF_P(ret)) return ret;
1929 return rb_call_super(1, &val);
1930}
1931
1932static inline bool
1933range_integer_edge_p(VALUE beg, VALUE end)
1934{
1935 return (!NIL_P(rb_check_to_integer(beg, "to_int")) ||
1936 !NIL_P(rb_check_to_integer(end, "to_int")));
1937}
1938
1939static inline bool
1940range_string_range_p(VALUE beg, VALUE end)
1941{
1942 return RB_TYPE_P(beg, T_STRING) && RB_TYPE_P(end, T_STRING);
1943}
1944
1945static inline VALUE
1946range_include_fallback(VALUE beg, VALUE end, VALUE val)
1947{
1948 if (NIL_P(beg) && NIL_P(end)) {
1949 if (linear_object_p(val)) return Qtrue;
1950 }
1951
1952 if (NIL_P(beg) || NIL_P(end)) {
1953 rb_raise(rb_eTypeError, "cannot determine inclusion in beginless/endless ranges");
1954 }
1955
1956 return Qundef;
1957}
1958
1959static VALUE
1960range_include_internal(VALUE range, VALUE val)
1961{
1962 VALUE beg = RANGE_BEG(range);
1963 VALUE end = RANGE_END(range);
1964 int nv = FIXNUM_P(beg) || FIXNUM_P(end) ||
1965 linear_object_p(beg) || linear_object_p(end);
1966
1967 if (nv || range_integer_edge_p(beg, end)) {
1968 return r_cover_p(range, beg, end, val);
1969 }
1970 else if (range_string_range_p(beg, end)) {
1971 return rb_str_include_range_p(beg, end, val, RANGE_EXCL(range));
1972 }
1973
1974 return range_include_fallback(beg, end, val);
1975}
1976
1977static int r_cover_range_p(VALUE range, VALUE beg, VALUE end, VALUE val);
1978
1979/*
1980 * call-seq:
1981 * cover?(object) -> true or false
1982 * cover?(range) -> true or false
1983 *
1984 * Returns +true+ if the given argument is within +self+, +false+ otherwise.
1985 *
1986 * With non-range argument +object+, evaluates with <tt><=</tt> and <tt><</tt>.
1987 *
1988 * For range +self+ with included end value (<tt>#exclude_end? == false</tt>),
1989 * evaluates thus:
1990 *
1991 * self.begin <= object <= self.end
1992 *
1993 * Examples:
1994 *
1995 * r = (1..4)
1996 * r.cover?(1) # => true
1997 * r.cover?(4) # => true
1998 * r.cover?(0) # => false
1999 * r.cover?(5) # => false
2000 * r.cover?('foo') # => false
2001 *
2002 * r = ('a'..'d')
2003 * r.cover?('a') # => true
2004 * r.cover?('d') # => true
2005 * r.cover?(' ') # => false
2006 * r.cover?('e') # => false
2007 * r.cover?(0) # => false
2008 *
2009 * For range +r+ with excluded end value (<tt>#exclude_end? == true</tt>),
2010 * evaluates thus:
2011 *
2012 * r.begin <= object < r.end
2013 *
2014 * Examples:
2015 *
2016 * r = (1...4)
2017 * r.cover?(1) # => true
2018 * r.cover?(3) # => true
2019 * r.cover?(0) # => false
2020 * r.cover?(4) # => false
2021 * r.cover?('foo') # => false
2022 *
2023 * r = ('a'...'d')
2024 * r.cover?('a') # => true
2025 * r.cover?('c') # => true
2026 * r.cover?(' ') # => false
2027 * r.cover?('d') # => false
2028 * r.cover?(0) # => false
2029 *
2030 * With range argument +range+, compares the first and last
2031 * elements of +self+ and +range+:
2032 *
2033 * r = (1..4)
2034 * r.cover?(1..4) # => true
2035 * r.cover?(0..4) # => false
2036 * r.cover?(1..5) # => false
2037 * r.cover?('a'..'d') # => false
2038 *
2039 * r = (1...4)
2040 * r.cover?(1..3) # => true
2041 * r.cover?(1..4) # => false
2042 *
2043 * If begin and end are numeric, #cover? behaves like #include?
2044 *
2045 * (1..3).cover?(1.5) # => true
2046 * (1..3).include?(1.5) # => true
2047 *
2048 * But when not numeric, the two methods may differ:
2049 *
2050 * ('a'..'d').cover?('cc') # => true
2051 * ('a'..'d').include?('cc') # => false
2052 *
2053 * Returns +false+ if either:
2054 *
2055 * - The begin value of +self+ is larger than its end value.
2056 * - An internal call to <tt><=></tt> returns +nil+;
2057 * that is, the operands are not comparable.
2058 *
2059 * Beginless ranges cover all values of the same type before the end,
2060 * excluding the end for exclusive ranges. Beginless ranges cover
2061 * ranges that end before the end of the beginless range, or at the
2062 * end of the beginless range for inclusive ranges.
2063 *
2064 * (..2).cover?(1) # => true
2065 * (..2).cover?(2) # => true
2066 * (..2).cover?(3) # => false
2067 * (...2).cover?(2) # => false
2068 * (..2).cover?("2") # => false
2069 * (..2).cover?(..2) # => true
2070 * (..2).cover?(...2) # => true
2071 * (..2).cover?(.."2") # => false
2072 * (...2).cover?(..2) # => false
2073 *
2074 * Endless ranges cover all values of the same type after the
2075 * beginning. Endless exclusive ranges do not cover endless
2076 * inclusive ranges.
2077 *
2078 * (2..).cover?(1) # => false
2079 * (2..).cover?(3) # => true
2080 * (2...).cover?(3) # => true
2081 * (2..).cover?(2) # => true
2082 * (2..).cover?("2") # => false
2083 * (2..).cover?(2..) # => true
2084 * (2..).cover?(2...) # => true
2085 * (2..).cover?("2"..) # => false
2086 * (2...).cover?(2..) # => false
2087 * (2...).cover?(3...) # => true
2088 * (2...).cover?(3..) # => false
2089 * (3..).cover?(2..) # => false
2090 *
2091 * Ranges that are both beginless and endless cover all values and
2092 * ranges, and return true for all arguments, with the exception that
2093 * beginless and endless exclusive ranges do not cover endless
2094 * inclusive ranges.
2095 *
2096 * (nil...).cover?(Object.new) # => true
2097 * (nil...).cover?(nil...) # => true
2098 * (nil..).cover?(nil...) # => true
2099 * (nil...).cover?(nil..) # => false
2100 * (nil...).cover?(1..) # => false
2101 *
2102 * Related: Range#include?.
2103 *
2104 */
2105
2106static VALUE
2107range_cover(VALUE range, VALUE val)
2108{
2109 VALUE beg, end;
2110
2111 beg = RANGE_BEG(range);
2112 end = RANGE_END(range);
2113
2114 if (rb_obj_is_kind_of(val, rb_cRange)) {
2115 return RBOOL(r_cover_range_p(range, beg, end, val));
2116 }
2117 return r_cover_p(range, beg, end, val);
2118}
2119
2120static VALUE
2121r_call_max(VALUE r)
2122{
2123 return rb_funcallv(r, rb_intern("max"), 0, 0);
2124}
2125
2126static int
2127r_cover_range_p(VALUE range, VALUE beg, VALUE end, VALUE val)
2128{
2129 VALUE val_beg, val_end, val_max;
2130 int cmp_end;
2131
2132 val_beg = RANGE_BEG(val);
2133 val_end = RANGE_END(val);
2134
2135 if (!NIL_P(end) && NIL_P(val_end)) return FALSE;
2136 if (!NIL_P(beg) && NIL_P(val_beg)) return FALSE;
2137 if (!NIL_P(val_beg) && !NIL_P(val_end) && r_less(val_beg, val_end) > (EXCL(val) ? -1 : 0)) return FALSE;
2138 if (!NIL_P(val_beg) && !r_cover_p(range, beg, end, val_beg)) return FALSE;
2139
2140
2141 if (!NIL_P(val_end) && !NIL_P(end)) {
2142 VALUE r_cmp_end = rb_funcall(end, id_cmp, 1, val_end);
2143 if (NIL_P(r_cmp_end)) return FALSE;
2144 cmp_end = rb_cmpint(r_cmp_end, end, val_end);
2145 }
2146 else {
2147 cmp_end = r_less(end, val_end);
2148 }
2149
2150
2151 if (EXCL(range) == EXCL(val)) {
2152 return cmp_end >= 0;
2153 }
2154 else if (EXCL(range)) {
2155 return cmp_end > 0;
2156 }
2157 else if (cmp_end >= 0) {
2158 return TRUE;
2159 }
2160
2161 val_max = rb_rescue2(r_call_max, val, 0, Qnil, rb_eTypeError, (VALUE)0);
2162 if (NIL_P(val_max)) return FALSE;
2163
2164 return r_less(end, val_max) >= 0;
2165}
2166
2167static VALUE
2168r_cover_p(VALUE range, VALUE beg, VALUE end, VALUE val)
2169{
2170 if (NIL_P(beg) || r_less(beg, val) <= 0) {
2171 int excl = EXCL(range);
2172 if (NIL_P(end) || r_less(val, end) <= -excl)
2173 return Qtrue;
2174 }
2175 return Qfalse;
2176}
2177
2178static VALUE
2179range_dumper(VALUE range)
2180{
2181 VALUE v = rb_obj_alloc(rb_cObject);
2182
2183 rb_ivar_set(v, id_excl, RANGE_EXCL(range));
2184 rb_ivar_set(v, id_beg, RANGE_BEG(range));
2185 rb_ivar_set(v, id_end, RANGE_END(range));
2186 return v;
2187}
2188
2189static VALUE
2190range_loader(VALUE range, VALUE obj)
2191{
2192 VALUE beg, end, excl;
2193
2194 if (!RB_TYPE_P(obj, T_OBJECT) || RBASIC(obj)->klass != rb_cObject) {
2195 rb_raise(rb_eTypeError, "not a dumped range object");
2196 }
2197
2198 range_modify(range);
2199 beg = rb_ivar_get(obj, id_beg);
2200 end = rb_ivar_get(obj, id_end);
2201 excl = rb_ivar_get(obj, id_excl);
2202 if (!NIL_P(excl)) {
2203 range_init(range, beg, end, RBOOL(RTEST(excl)));
2204 }
2205 return range;
2206}
2207
2208static VALUE
2209range_alloc(VALUE klass)
2210{
2211 /* rb_struct_alloc_noinit itself should not be used because
2212 * rb_marshal_define_compat uses equality of allocation function */
2213 return rb_struct_alloc_noinit(klass);
2214}
2215
2216/*
2217 * call-seq:
2218 * count -> integer
2219 * count(object) -> integer
2220 * count {|element| ... } -> integer
2221 *
2222 * Returns the count of elements, based on an argument or block criterion, if given.
2223 *
2224 * With no argument and no block given, returns the number of elements:
2225 *
2226 * (1..4).count # => 4
2227 * (1...4).count # => 3
2228 * ('a'..'d').count # => 4
2229 * ('a'...'d').count # => 3
2230 * (1..).count # => Infinity
2231 * (..4).count # => Infinity
2232 *
2233 * With argument +object+, returns the number of +object+ found in +self+,
2234 * which will usually be zero or one:
2235 *
2236 * (1..4).count(2) # => 1
2237 * (1..4).count(5) # => 0
2238 * (1..4).count('a') # => 0
2239 *
2240 * With a block given, calls the block with each element;
2241 * returns the number of elements for which the block returns a truthy value:
2242 *
2243 * (1..4).count {|element| element < 3 } # => 2
2244 *
2245 * Related: Range#size.
2246 */
2247static VALUE
2248range_count(int argc, VALUE *argv, VALUE range)
2249{
2250 if (argc != 0) {
2251 /* It is odd for instance (1...).count(0) to return Infinity. Just let
2252 * it loop. */
2253 return rb_call_super(argc, argv);
2254 }
2255 else if (rb_block_given_p()) {
2256 /* Likewise it is odd for instance (1...).count {|x| x == 0 } to return
2257 * Infinity. Just let it loop. */
2258 return rb_call_super(argc, argv);
2259 }
2260
2261 VALUE beg = RANGE_BEG(range), end = RANGE_END(range);
2262
2263 if (NIL_P(beg) || NIL_P(end)) {
2264 /* We are confident that the answer is Infinity. */
2265 return DBL2NUM(HUGE_VAL);
2266 }
2267
2268 if (is_integer_p(beg)) {
2269 VALUE size = range_size(range);
2270 if (!NIL_P(size)) {
2271 return size;
2272 }
2273 }
2274
2275 return rb_call_super(argc, argv);
2276}
2277
2278static bool
2279empty_region_p(VALUE beg, VALUE end, int excl)
2280{
2281 if (NIL_P(beg)) return false;
2282 if (NIL_P(end)) return false;
2283 int less = r_less(beg, end);
2284 /* empty range */
2285 if (less > 0) return true;
2286 if (excl && less == 0) return true;
2287 return false;
2288}
2289
2290/*
2291 * call-seq:
2292 * overlap?(range) -> true or false
2293 *
2294 * Returns +true+ if +range+ overlaps with +self+, +false+ otherwise:
2295 *
2296 * (0..2).overlap?(1..3) #=> true
2297 * (0..2).overlap?(3..4) #=> false
2298 * (0..).overlap?(..0) #=> true
2299 *
2300 * With non-range argument, raises TypeError.
2301 *
2302 * (1..3).overlap?(1) # TypeError
2303 *
2304 * Returns +false+ if an internal call to <tt><=></tt> returns +nil+;
2305 * that is, the operands are not comparable.
2306 *
2307 * (1..3).overlap?('a'..'d') # => false
2308 *
2309 * Returns +false+ if +self+ or +range+ is empty. "Empty range" means
2310 * that its begin value is larger than, or equal for an exclusive
2311 * range, its end value.
2312 *
2313 * (4..1).overlap?(2..3) # => false
2314 * (4..1).overlap?(..3) # => false
2315 * (4..1).overlap?(2..) # => false
2316 * (2...2).overlap?(1..2) # => false
2317 *
2318 * (1..4).overlap?(3..2) # => false
2319 * (..4).overlap?(3..2) # => false
2320 * (1..).overlap?(3..2) # => false
2321 * (1..2).overlap?(2...2) # => false
2322 *
2323 * Returns +false+ if the begin value one of +self+ and +range+ is
2324 * larger than, or equal if the other is an exclusive range, the end
2325 * value of the other:
2326 *
2327 * (4..5).overlap?(2..3) # => false
2328 * (4..5).overlap?(2...4) # => false
2329 *
2330 * (1..2).overlap?(3..4) # => false
2331 * (1...3).overlap?(3..4) # => false
2332 *
2333 * Returns +false+ if the end value one of +self+ and +range+ is
2334 * larger than, or equal for an exclusive range, the end value of the
2335 * other:
2336 *
2337 * (4..5).overlap?(2..3) # => false
2338 * (4..5).overlap?(2...4) # => false
2339 *
2340 * (1..2).overlap?(3..4) # => false
2341 * (1...3).overlap?(3..4) # => false
2342 *
2343 * Note that the method wouldn't make any assumptions about the beginless
2344 * range being actually empty, even if its upper bound is the minimum
2345 * possible value of its type, so all this would return +true+:
2346 *
2347 * (...-Float::INFINITY).overlap?(...-Float::INFINITY) # => true
2348 * (..."").overlap?(..."") # => true
2349 * (...[]).overlap?(...[]) # => true
2350 *
2351 * Even if those ranges are effectively empty (no number can be smaller than
2352 * <tt>-Float::INFINITY</tt>), they are still considered overlapping
2353 * with themselves.
2354 *
2355 * Related: Range#cover?.
2356 */
2357
2358static VALUE
2359range_overlap(VALUE range, VALUE other)
2360{
2361 if (!rb_obj_is_kind_of(other, rb_cRange)) {
2362 rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Range)",
2363 rb_class_name(rb_obj_class(other)));
2364 }
2365
2366 VALUE self_beg = RANGE_BEG(range);
2367 VALUE self_end = RANGE_END(range);
2368 int self_excl = EXCL(range);
2369 VALUE other_beg = RANGE_BEG(other);
2370 VALUE other_end = RANGE_END(other);
2371 int other_excl = EXCL(other);
2372
2373 if (empty_region_p(self_beg, other_end, other_excl)) return Qfalse;
2374 if (empty_region_p(other_beg, self_end, self_excl)) return Qfalse;
2375
2376 if (!NIL_P(self_beg) && !NIL_P(other_beg)) {
2377 VALUE cmp = rb_funcall(self_beg, id_cmp, 1, other_beg);
2378 if (NIL_P(cmp)) return Qfalse;
2379 /* if both begin values are equal, no more comparisons needed */
2380 if (rb_cmpint(cmp, self_beg, other_beg) == 0) return Qtrue;
2381 }
2382 else if (NIL_P(self_beg) && NIL_P(other_beg)) {
2383 VALUE cmp = rb_funcall(self_end, id_cmp, 1, other_end);
2384 return RBOOL(!NIL_P(cmp));
2385 }
2386
2387 if (empty_region_p(self_beg, self_end, self_excl)) return Qfalse;
2388 if (empty_region_p(other_beg, other_end, other_excl)) return Qfalse;
2389
2390 return Qtrue;
2391}
2392
2393/* A \Range object represents a collection of values
2394 * that are between given begin and end values.
2395 *
2396 * You can create an \Range object explicitly with:
2397 *
2398 * - A {range literal}[rdoc-ref:syntax/literals.rdoc@Range+Literals]:
2399 *
2400 * # Ranges that use '..' to include the given end value.
2401 * (1..4).to_a # => [1, 2, 3, 4]
2402 * ('a'..'d').to_a # => ["a", "b", "c", "d"]
2403 * # Ranges that use '...' to exclude the given end value.
2404 * (1...4).to_a # => [1, 2, 3]
2405 * ('a'...'d').to_a # => ["a", "b", "c"]
2406 *
2407 * A range may be created using method Range.new:
2408 *
2409 * # Ranges that by default include the given end value.
2410 * Range.new(1, 4).to_a # => [1, 2, 3, 4]
2411 * Range.new('a', 'd').to_a # => ["a", "b", "c", "d"]
2412 * # Ranges that use third argument +exclude_end+ to exclude the given end value.
2413 * Range.new(1, 4, true).to_a # => [1, 2, 3]
2414 * Range.new('a', 'd', true).to_a # => ["a", "b", "c"]
2415 *
2416 * == Beginless Ranges
2417 *
2418 * A _beginless_ _range_ has a definite end value, but a +nil+ begin value.
2419 * Such a range includes all values up to the end value.
2420 *
2421 * r = (..4) # => nil..4
2422 * r.begin # => nil
2423 * r.include?(-50) # => true
2424 * r.include?(4) # => true
2425 *
2426 * r = (...4) # => nil...4
2427 * r.include?(4) # => false
2428 *
2429 * Range.new(nil, 4) # => nil..4
2430 * Range.new(nil, 4, true) # => nil...4
2431 *
2432 * A beginless range may be used to slice an array:
2433 *
2434 * a = [1, 2, 3, 4]
2435 * r = (..2) # => nil...2
2436 * a[r] # => [1, 2]
2437 *
2438 * \Method +each+ for a beginless range raises an exception.
2439 *
2440 * == Endless Ranges
2441 *
2442 * An _endless_ _range_ has a definite begin value, but a +nil+ end value.
2443 * Such a range includes all values from the begin value.
2444 *
2445 * r = (1..) # => 1..
2446 * r.end # => nil
2447 * r.include?(50) # => true
2448 *
2449 * Range.new(1, nil) # => 1..
2450 *
2451 * The literal for an endless range may be written with either two dots
2452 * or three.
2453 * The range has the same elements, either way.
2454 * But note that the two are not equal:
2455 *
2456 * r0 = (1..) # => 1..
2457 * r1 = (1...) # => 1...
2458 * r0.begin == r1.begin # => true
2459 * r0.end == r1.end # => true
2460 * r0 == r1 # => false
2461 *
2462 * An endless range may be used to slice an array:
2463 *
2464 * a = [1, 2, 3, 4]
2465 * r = (2..) # => 2..
2466 * a[r] # => [3, 4]
2467 *
2468 * \Method +each+ for an endless range calls the given block indefinitely:
2469 *
2470 * a = []
2471 * r = (1..)
2472 * r.each do |i|
2473 * a.push(i) if i.even?
2474 * break if i > 10
2475 * end
2476 * a # => [2, 4, 6, 8, 10]
2477 *
2478 * A range can be both beginless and endless. For literal beginless, endless
2479 * ranges, at least the beginning or end of the range must be given as an
2480 * explicit nil value. It is recommended to use an explicit nil beginning and
2481 * implicit nil end, since that is what Ruby uses for Range#inspect:
2482 *
2483 * (nil..) # => (nil..)
2484 * (..nil) # => (nil..)
2485 * (nil..nil) # => (nil..)
2486 *
2487 * == Ranges and Other Classes
2488 *
2489 * An object may be put into a range if its class implements
2490 * instance method <tt><=></tt>.
2491 * Ruby core classes that do so include Array, Complex, File::Stat,
2492 * Float, Integer, Kernel, Module, Numeric, Rational, String, Symbol, and Time.
2493 *
2494 * Example:
2495 *
2496 * t0 = Time.now # => 2021-09-19 09:22:48.4854986 -0500
2497 * t1 = Time.now # => 2021-09-19 09:22:56.0365079 -0500
2498 * t2 = Time.now # => 2021-09-19 09:23:08.5263283 -0500
2499 * (t0..t2).include?(t1) # => true
2500 * (t0..t1).include?(t2) # => false
2501 *
2502 * A range can be iterated over only if its elements
2503 * implement instance method +succ+.
2504 * Ruby core classes that do so include Integer, String, and Symbol
2505 * (but not the other classes mentioned above).
2506 *
2507 * Iterator methods include:
2508 *
2509 * - In \Range itself: #each, #step, and #%
2510 * - Included from module Enumerable: #each_entry, #each_with_index,
2511 * #each_with_object, #each_slice, #each_cons, and #reverse_each.
2512 *
2513 * Example:
2514 *
2515 * a = []
2516 * (1..4).each {|i| a.push(i) }
2517 * a # => [1, 2, 3, 4]
2518 *
2519 * == Ranges and User-Defined Classes
2520 *
2521 * A user-defined class that is to be used in a range
2522 * must implement instance <tt><=></tt>;
2523 * see Integer#<=>.
2524 * To make iteration available, it must also implement
2525 * instance method +succ+; see Integer#succ.
2526 *
2527 * The class below implements both <tt><=></tt> and +succ+,
2528 * and so can be used both to construct ranges and to iterate over them.
2529 * Note that the Comparable module is included
2530 * so the <tt>==</tt> method is defined in terms of <tt><=></tt>.
2531 *
2532 * # Represent a string of 'X' characters.
2533 * class Xs
2534 * include Comparable
2535 * attr_accessor :length
2536 * def initialize(n)
2537 * @length = n
2538 * end
2539 * def succ
2540 * Xs.new(@length + 1)
2541 * end
2542 * def <=>(other)
2543 * @length <=> other.length
2544 * end
2545 * def to_s
2546 * sprintf "%2d #{inspect}", @length
2547 * end
2548 * def inspect
2549 * 'X' * @length
2550 * end
2551 * end
2552 *
2553 * r = Xs.new(3)..Xs.new(6) #=> XXX..XXXXXX
2554 * r.to_a #=> [XXX, XXXX, XXXXX, XXXXXX]
2555 * r.include?(Xs.new(5)) #=> true
2556 * r.include?(Xs.new(7)) #=> false
2557 *
2558 * == What's Here
2559 *
2560 * First, what's elsewhere. \Class \Range:
2561 *
2562 * - Inherits from {class Object}[rdoc-ref:Object@What-27s+Here].
2563 * - Includes {module Enumerable}[rdoc-ref:Enumerable@What-27s+Here],
2564 * which provides dozens of additional methods.
2565 *
2566 * Here, class \Range provides methods that are useful for:
2567 *
2568 * - {Creating a Range}[rdoc-ref:Range@Methods+for+Creating+a+Range]
2569 * - {Querying}[rdoc-ref:Range@Methods+for+Querying]
2570 * - {Comparing}[rdoc-ref:Range@Methods+for+Comparing]
2571 * - {Iterating}[rdoc-ref:Range@Methods+for+Iterating]
2572 * - {Converting}[rdoc-ref:Range@Methods+for+Converting]
2573 * - {Methods for Working with JSON}[rdoc-ref:Range@Methods+for+Working+with+JSON]
2574 *
2575 * === Methods for Creating a \Range
2576 *
2577 * - ::new: Returns a new range.
2578 *
2579 * === Methods for Querying
2580 *
2581 * - #begin: Returns the begin value given for +self+.
2582 * - #bsearch: Returns an element from +self+ selected by a binary search.
2583 * - #count: Returns a count of elements in +self+.
2584 * - #end: Returns the end value given for +self+.
2585 * - #exclude_end?: Returns whether the end object is excluded.
2586 * - #first: Returns the first elements of +self+.
2587 * - #hash: Returns the integer hash code.
2588 * - #last: Returns the last elements of +self+.
2589 * - #max: Returns the maximum values in +self+.
2590 * - #min: Returns the minimum values in +self+.
2591 * - #minmax: Returns the minimum and maximum values in +self+.
2592 * - #size: Returns the count of elements in +self+.
2593 *
2594 * === Methods for Comparing
2595 *
2596 * - #==: Returns whether a given object is equal to +self+ (uses #==).
2597 * - #===: Returns whether the given object is between the begin and end values.
2598 * - #cover?: Returns whether a given object is within +self+.
2599 * - #eql?: Returns whether a given object is equal to +self+ (uses #eql?).
2600 * - #include? (aliased as #member?): Returns whether a given object
2601 * is an element of +self+.
2602 *
2603 * === Methods for Iterating
2604 *
2605 * - #%: Requires argument +n+; calls the block with each +n+-th element of +self+.
2606 * - #each: Calls the block with each element of +self+.
2607 * - #step: Takes optional argument +n+ (defaults to 1);
2608 * calls the block with each +n+-th element of +self+.
2609 *
2610 * === Methods for Converting
2611 *
2612 * - #inspect: Returns a string representation of +self+ (uses #inspect).
2613 * - #to_a (aliased as #entries): Returns elements of +self+ in an array.
2614 * - #to_s: Returns a string representation of +self+ (uses #to_s).
2615 *
2616 * === Methods for Working with \JSON
2617 *
2618 * - ::json_create: Returns a new \Range object constructed from the given object.
2619 * - #as_json: Returns a 2-element hash representing +self+.
2620 * - #to_json: Returns a \JSON string representing +self+.
2621 *
2622 * To make these methods available:
2623 *
2624 * require 'json/add/range'
2625 *
2626 */
2627
2628void
2629Init_Range(void)
2630{
2631 id_beg = rb_intern_const("begin");
2632 id_end = rb_intern_const("end");
2633 id_excl = rb_intern_const("excl");
2634
2636 "Range", rb_cObject, range_alloc,
2637 "begin", "end", "excl", NULL);
2638
2640 rb_marshal_define_compat(rb_cRange, rb_cObject, range_dumper, range_loader);
2641 rb_define_method(rb_cRange, "initialize", range_initialize, -1);
2642 rb_define_method(rb_cRange, "initialize_copy", range_initialize_copy, 1);
2643 rb_define_method(rb_cRange, "==", range_eq, 1);
2644 rb_define_method(rb_cRange, "===", range_eqq, 1);
2645 rb_define_method(rb_cRange, "eql?", range_eql, 1);
2646 rb_define_method(rb_cRange, "hash", range_hash, 0);
2647 rb_define_method(rb_cRange, "each", range_each, 0);
2648 rb_define_method(rb_cRange, "step", range_step, -1);
2649 rb_define_method(rb_cRange, "%", range_percent_step, 1);
2650 rb_define_method(rb_cRange, "reverse_each", range_reverse_each, 0);
2651 rb_define_method(rb_cRange, "bsearch", range_bsearch, 0);
2652 rb_define_method(rb_cRange, "begin", range_begin, 0);
2653 rb_define_method(rb_cRange, "end", range_end, 0);
2654 rb_define_method(rb_cRange, "first", range_first, -1);
2655 rb_define_method(rb_cRange, "last", range_last, -1);
2656 rb_define_method(rb_cRange, "min", range_min, -1);
2657 rb_define_method(rb_cRange, "max", range_max, -1);
2658 rb_define_method(rb_cRange, "minmax", range_minmax, 0);
2659 rb_define_method(rb_cRange, "size", range_size, 0);
2660 rb_define_method(rb_cRange, "to_a", range_to_a, 0);
2661 rb_define_method(rb_cRange, "entries", range_to_a, 0);
2662 rb_define_method(rb_cRange, "to_s", range_to_s, 0);
2663 rb_define_method(rb_cRange, "inspect", range_inspect, 0);
2664
2665 rb_define_method(rb_cRange, "exclude_end?", range_exclude_end_p, 0);
2666
2667 rb_define_method(rb_cRange, "member?", range_include, 1);
2668 rb_define_method(rb_cRange, "include?", range_include, 1);
2669 rb_define_method(rb_cRange, "cover?", range_cover, 1);
2670 rb_define_method(rb_cRange, "count", range_count, -1);
2671 rb_define_method(rb_cRange, "overlap?", range_overlap, 1);
2672}
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:177
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition class.c:1177
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
Definition class.c:2622
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:866
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1675
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
Definition value_type.h:87
#define RFLOAT_VALUE
Old name of rb_float_value.
Definition double.h:28
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
Definition assume.h:28
#define T_FLOAT
Old name of RUBY_T_FLOAT.
Definition value_type.h:64
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition value_type.h:57
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define FIXNUM_FLAG
Old name of RUBY_FIXNUM_FLAG.
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:203
#define FIXABLE
Old name of RB_FIXABLE.
Definition fixnum.h:25
#define LONG2FIX
Old name of RB_INT2FIX.
Definition long.h:49
#define ASSUME
Old name of RBIMPL_ASSUME.
Definition assume.h:27
#define LONG2NUM
Old name of RB_LONG2NUM.
Definition long.h:50
#define FIXNUM_MIN
Old name of RUBY_FIXNUM_MIN.
Definition fixnum.h:27
#define FLONUM_P
Old name of RB_FLONUM_P.
#define Qtrue
Old name of RUBY_Qtrue.
#define ST2FIX
Old name of RB_ST2FIX.
Definition st_data_t.h:33
#define FIXNUM_MAX
Old name of RUBY_FIXNUM_MAX.
Definition fixnum.h:26
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
Definition long.h:46
#define T_OBJECT
Old name of RUBY_T_OBJECT.
Definition value_type.h:75
#define NIL_P
Old name of RB_NIL_P.
#define POSFIXABLE
Old name of RB_POSFIXABLE.
Definition fixnum.h:29
#define DBL2NUM
Old name of rb_float_new.
Definition double.h:29
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
#define NUM2LONG
Old name of RB_NUM2LONG.
Definition long.h:51
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition symbol.h:47
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:651
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
void rb_iter_break(void)
Breaks from a block.
Definition vm.c:2052
VALUE rb_eRangeError
RangeError exception.
Definition error.c:1348
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1344
VALUE rb_cTime
Time class.
Definition time.c:668
VALUE rb_Float(VALUE val)
This is the logic behind Kernel#Float.
Definition object.c:3548
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
Definition object.c:2058
VALUE rb_mEnumerable
Enumerable module.
Definition enum.c:27
int rb_eql(VALUE lhs, VALUE rhs)
Checks for equality of the passed objects, in terms of Object#eql?.
Definition object.c:160
VALUE rb_cNumeric
Numeric class.
Definition numeric.c:196
VALUE rb_Array(VALUE val)
This is the logic behind Kernel#Array.
Definition object.c:3703
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:215
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
Definition object.c:645
VALUE rb_cRange
Range class.
Definition range.c:31
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
Definition object.c:147
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
Definition object.c:830
VALUE rb_check_to_integer(VALUE val, const char *mid)
Identical to rb_check_convert_type(), except the return value type is fixed to rb_cInteger.
Definition object.c:3132
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
Definition object.c:3145
#define RUBY_FIXNUM_MAX
Maximum possible value that a fixnum can represent.
Definition fixnum.h:55
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1121
VALUE rb_call_super(int argc, const VALUE *argv)
This resembles ruby's super.
Definition vm_eval.c:366
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
This roughly resembles return enum_for(__callee__) unless block_given?.
Definition enumerator.h:206
#define RETURN_ENUMERATOR(obj, argc, argv)
Identical to RETURN_SIZED_ENUMERATOR(), except its size is unknown.
Definition enumerator.h:239
#define rb_check_frozen
Just another name of rb_check_frozen.
Definition error.h:264
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:280
int rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
Deconstructs a range into its components.
Definition range.c:1656
VALUE rb_range_new(VALUE beg, VALUE end, int excl)
Creates a new Range.
Definition range.c:67
VALUE rb_range_beg_len(VALUE range, long *begp, long *lenp, long len, int err)
Deconstructs a numerical range.
Definition range.c:1744
#define rb_hash_uint(h, i)
Just another name of st_hash_uint.
Definition string.h:942
#define rb_hash_end(h)
Just another name of st_hash_end.
Definition string.h:945
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition string.c:3411
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1498
st_index_t rb_hash_start(st_index_t i)
Starts a series of hashing.
Definition random.c:1741
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
Definition string.c:2681
VALUE rb_struct_define_without_accessor(const char *name, VALUE super, rb_alloc_func_t func,...)
Identical to rb_struct_define(), except it does not define accessor methods.
Definition struct.c:472
VALUE rb_struct_alloc_noinit(VALUE klass)
Allocates an instance of the given class.
Definition struct.c:405
VALUE rb_exec_recursive(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
"Recursion" API entry point.
Definition thread.c:5266
VALUE rb_exec_recursive_paired(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE p, VALUE h)
Identical to rb_exec_recursive(), except it checks for the recursion on the ordered pair of { g,...
Definition thread.c:5277
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
Definition variable.c:1854
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
Definition variable.c:1340
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
Definition variable.c:402
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
Definition vm_method.c:2937
VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it returns RUBY_Qundef instead of raising rb_eNoMethodError.
Definition vm_eval.c:687
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:276
VALUE rb_sym2str(VALUE id)
Identical to rb_id2str(), except it takes an instance of rb_cSymbol rather than an ID.
Definition symbol.c:953
int len
Length of the buffer.
Definition io.h:8
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
Definition iterator.h:58
VALUE rb_yield(VALUE val)
Yields the block.
Definition vm_eval.c:1376
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE(*dumper)(VALUE), VALUE(*loader)(VALUE, VALUE))
Marshal format compatibility layer.
Definition marshal.c:150
VALUE rb_block_call(VALUE q, ID w, int e, const VALUE *r, type *t, VALUE y)
Call a method with a block.
VALUE rb_rescue2(type *q, VALUE w, type *e, VALUE r,...)
An equivalent of rescue clause.
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]]
Definition noreturn.h:38
#define RARRAY_AREF(a, i)
Definition rarray.h:403
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
#define RBIGNUM_SIGN
Just another name of rb_big_sign.
Definition rbignum.h:29
static bool RBIGNUM_NEGATIVE_P(VALUE b)
Checks if the bignum is negative.
Definition rbignum.h:74
static bool RBIGNUM_POSITIVE_P(VALUE b)
Checks if the bignum is positive.
Definition rbignum.h:61
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
Definition variable.c:417
#define RTEST
This is an old name of RB_TEST.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
Definition value_type.h:263
static bool rb_integer_type_p(VALUE obj)
Queries if the object is an instance of rb_cInteger.
Definition value_type.h:203