PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/range.c

https://github.com/vuxuandung/ruby
C | 1419 lines | 888 code | 149 blank | 382 comment | 249 complexity | 8de1db581d8c8a2fbd9e5d6cafe42011 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0, 0BSD
  1. /**********************************************************************
  2. range.c -
  3. $Author$
  4. created at: Thu Aug 19 17:46:47 JST 1993
  5. Copyright (C) 1993-2007 Yukihiro Matsumoto
  6. **********************************************************************/
  7. #include "ruby/ruby.h"
  8. #include "ruby/encoding.h"
  9. #include "internal.h"
  10. #include "id.h"
  11. #ifdef HAVE_FLOAT_H
  12. #include <float.h>
  13. #endif
  14. #include <math.h>
  15. VALUE rb_cRange;
  16. static ID id_cmp, id_succ, id_beg, id_end, id_excl;
  17. #define RANGE_BEG(r) (RSTRUCT(r)->as.ary[0])
  18. #define RANGE_END(r) (RSTRUCT(r)->as.ary[1])
  19. #define RANGE_EXCL(r) (RSTRUCT(r)->as.ary[2])
  20. #define EXCL(r) RTEST(RANGE_EXCL(r))
  21. #define SET_EXCL(r,v) (RSTRUCT(r)->as.ary[2] = (v) ? Qtrue : Qfalse)
  22. static VALUE
  23. range_failed(void)
  24. {
  25. rb_raise(rb_eArgError, "bad value for range");
  26. return Qnil; /* dummy */
  27. }
  28. static VALUE
  29. range_check(VALUE *args)
  30. {
  31. return rb_funcall(args[0], id_cmp, 1, args[1]);
  32. }
  33. static void
  34. range_init(VALUE range, VALUE beg, VALUE end, int exclude_end)
  35. {
  36. VALUE args[2];
  37. args[0] = beg;
  38. args[1] = end;
  39. if (!FIXNUM_P(beg) || !FIXNUM_P(end)) {
  40. VALUE v;
  41. v = rb_rescue(range_check, (VALUE)args, range_failed, 0);
  42. if (NIL_P(v))
  43. range_failed();
  44. }
  45. SET_EXCL(range, exclude_end);
  46. RSTRUCT(range)->as.ary[0] = beg;
  47. RSTRUCT(range)->as.ary[1] = end;
  48. }
  49. VALUE
  50. rb_range_new(VALUE beg, VALUE end, int exclude_end)
  51. {
  52. VALUE range = rb_obj_alloc(rb_cRange);
  53. range_init(range, beg, end, exclude_end);
  54. return range;
  55. }
  56. /*
  57. * call-seq:
  58. * Range.new(begin, end, exclude_end=false) -> rng
  59. *
  60. * Constructs a range using the given +begin+ and +end+. If the +exclude_end+
  61. * parameter is omitted or is <code>false</code>, the +rng+ will include
  62. * the end object; otherwise, it will be excluded.
  63. */
  64. static VALUE
  65. range_initialize(int argc, VALUE *argv, VALUE range)
  66. {
  67. VALUE beg, end, flags;
  68. rb_scan_args(argc, argv, "21", &beg, &end, &flags);
  69. /* Ranges are immutable, so that they should be initialized only once. */
  70. if (RANGE_EXCL(range) != Qnil) {
  71. rb_name_error(idInitialize, "`initialize' called twice");
  72. }
  73. range_init(range, beg, end, RTEST(flags));
  74. return Qnil;
  75. }
  76. #define range_initialize_copy rb_struct_init_copy /* :nodoc: */
  77. /*
  78. * call-seq:
  79. * rng.exclude_end? -> true or false
  80. *
  81. * Returns <code>true</code> if the range excludes its end value.
  82. *
  83. * (1..5).exclude_end? #=> false
  84. * (1...5).exclude_end? #=> true
  85. */
  86. static VALUE
  87. range_exclude_end_p(VALUE range)
  88. {
  89. return EXCL(range) ? Qtrue : Qfalse;
  90. }
  91. static VALUE
  92. recursive_equal(VALUE range, VALUE obj, int recur)
  93. {
  94. if (recur) return Qtrue; /* Subtle! */
  95. if (!rb_equal(RANGE_BEG(range), RANGE_BEG(obj)))
  96. return Qfalse;
  97. if (!rb_equal(RANGE_END(range), RANGE_END(obj)))
  98. return Qfalse;
  99. if (EXCL(range) != EXCL(obj))
  100. return Qfalse;
  101. return Qtrue;
  102. }
  103. /*
  104. * call-seq:
  105. * rng == obj -> true or false
  106. *
  107. * Returns <code>true</code> only if +obj+ is a Range, has equivalent
  108. * begin and end items (by comparing them with <code>==</code>), and has
  109. * the same #exclude_end? setting as the range.
  110. *
  111. * (0..2) == (0..2) #=> true
  112. * (0..2) == Range.new(0,2) #=> true
  113. * (0..2) == (0...2) #=> false
  114. *
  115. */
  116. static VALUE
  117. range_eq(VALUE range, VALUE obj)
  118. {
  119. if (range == obj)
  120. return Qtrue;
  121. if (!rb_obj_is_kind_of(obj, rb_cRange))
  122. return Qfalse;
  123. return rb_exec_recursive_paired(recursive_equal, range, obj, obj);
  124. }
  125. static int
  126. r_lt(VALUE a, VALUE b)
  127. {
  128. VALUE r = rb_funcall(a, id_cmp, 1, b);
  129. if (NIL_P(r))
  130. return (int)Qfalse;
  131. if (rb_cmpint(r, a, b) < 0)
  132. return (int)Qtrue;
  133. return (int)Qfalse;
  134. }
  135. static int
  136. r_le(VALUE a, VALUE b)
  137. {
  138. int c;
  139. VALUE r = rb_funcall(a, id_cmp, 1, b);
  140. if (NIL_P(r))
  141. return (int)Qfalse;
  142. c = rb_cmpint(r, a, b);
  143. if (c == 0)
  144. return (int)INT2FIX(0);
  145. if (c < 0)
  146. return (int)Qtrue;
  147. return (int)Qfalse;
  148. }
  149. static VALUE
  150. recursive_eql(VALUE range, VALUE obj, int recur)
  151. {
  152. if (recur) return Qtrue; /* Subtle! */
  153. if (!rb_eql(RANGE_BEG(range), RANGE_BEG(obj)))
  154. return Qfalse;
  155. if (!rb_eql(RANGE_END(range), RANGE_END(obj)))
  156. return Qfalse;
  157. if (EXCL(range) != EXCL(obj))
  158. return Qfalse;
  159. return Qtrue;
  160. }
  161. /*
  162. * call-seq:
  163. * rng.eql?(obj) -> true or false
  164. *
  165. * Returns <code>true</code> only if +obj+ is a Range, has equivalent
  166. * begin and end items (by comparing them with <code>eql?</code>),
  167. * and has the same #exclude_end? setting as the range.
  168. *
  169. * (0..2).eql?(0..2) #=> true
  170. * (0..2).eql?(Range.new(0,2)) #=> true
  171. * (0..2).eql?(0...2) #=> false
  172. *
  173. */
  174. static VALUE
  175. range_eql(VALUE range, VALUE obj)
  176. {
  177. if (range == obj)
  178. return Qtrue;
  179. if (!rb_obj_is_kind_of(obj, rb_cRange))
  180. return Qfalse;
  181. return rb_exec_recursive_paired(recursive_eql, range, obj, obj);
  182. }
  183. static VALUE
  184. recursive_hash(VALUE range, VALUE dummy, int recur)
  185. {
  186. st_index_t hash = EXCL(range);
  187. VALUE v;
  188. hash = rb_hash_start(hash);
  189. if (!recur) {
  190. v = rb_hash(RANGE_BEG(range));
  191. hash = rb_hash_uint(hash, NUM2LONG(v));
  192. v = rb_hash(RANGE_END(range));
  193. hash = rb_hash_uint(hash, NUM2LONG(v));
  194. }
  195. hash = rb_hash_uint(hash, EXCL(range) << 24);
  196. hash = rb_hash_end(hash);
  197. return LONG2FIX(hash);
  198. }
  199. /*
  200. * call-seq:
  201. * rng.hash -> fixnum
  202. *
  203. * Compute a hash-code for this range. Two ranges with equal
  204. * begin and end points (using <code>eql?</code>), and the same
  205. * #exclude_end? value will generate the same hash-code.
  206. */
  207. static VALUE
  208. range_hash(VALUE range)
  209. {
  210. return rb_exec_recursive_outer(recursive_hash, range, 0);
  211. }
  212. static void
  213. range_each_func(VALUE range, VALUE (*func) (VALUE, void *), void *arg)
  214. {
  215. int c;
  216. VALUE b = RANGE_BEG(range);
  217. VALUE e = RANGE_END(range);
  218. VALUE v = b;
  219. if (EXCL(range)) {
  220. while (r_lt(v, e)) {
  221. (*func) (v, arg);
  222. v = rb_funcall(v, id_succ, 0, 0);
  223. }
  224. }
  225. else {
  226. while ((c = r_le(v, e)) != Qfalse) {
  227. (*func) (v, arg);
  228. if (c == (int)INT2FIX(0))
  229. break;
  230. v = rb_funcall(v, id_succ, 0, 0);
  231. }
  232. }
  233. }
  234. static VALUE
  235. sym_step_i(VALUE i, void *arg)
  236. {
  237. VALUE *iter = arg;
  238. if (FIXNUM_P(iter[0])) {
  239. iter[0] -= INT2FIX(1) & ~FIXNUM_FLAG;
  240. }
  241. else {
  242. iter[0] = rb_funcall(iter[0], '-', 1, INT2FIX(1));
  243. }
  244. if (iter[0] == INT2FIX(0)) {
  245. rb_yield(rb_str_intern(i));
  246. iter[0] = iter[1];
  247. }
  248. return Qnil;
  249. }
  250. static VALUE
  251. step_i(VALUE i, void *arg)
  252. {
  253. VALUE *iter = arg;
  254. if (FIXNUM_P(iter[0])) {
  255. iter[0] -= INT2FIX(1) & ~FIXNUM_FLAG;
  256. }
  257. else {
  258. iter[0] = rb_funcall(iter[0], '-', 1, INT2FIX(1));
  259. }
  260. if (iter[0] == INT2FIX(0)) {
  261. rb_yield(i);
  262. iter[0] = iter[1];
  263. }
  264. return Qnil;
  265. }
  266. static int
  267. discrete_object_p(VALUE obj)
  268. {
  269. if (rb_obj_is_kind_of(obj, rb_cTime)) return FALSE; /* until Time#succ removed */
  270. return rb_respond_to(obj, id_succ);
  271. }
  272. static VALUE
  273. range_step_size(VALUE range, VALUE args)
  274. {
  275. VALUE b = RANGE_BEG(range), e = RANGE_END(range);
  276. VALUE step = INT2FIX(1);
  277. if (args) {
  278. step = RARRAY_PTR(args)[0];
  279. if (!rb_obj_is_kind_of(step, rb_cNumeric)) {
  280. step = rb_to_int(step);
  281. }
  282. }
  283. if (rb_funcall(step, '<', 1, INT2FIX(0))) {
  284. rb_raise(rb_eArgError, "step can't be negative");
  285. }
  286. else if (!rb_funcall(step, '>', 1, INT2FIX(0))) {
  287. rb_raise(rb_eArgError, "step can't be 0");
  288. }
  289. if (rb_obj_is_kind_of(b, rb_cNumeric) && rb_obj_is_kind_of(e, rb_cNumeric)) {
  290. return num_interval_step_size(b, e, step, EXCL(range));
  291. }
  292. return Qnil;
  293. }
  294. /*
  295. * call-seq:
  296. * rng.step(n=1) {| obj | block } -> rng
  297. * rng.step(n=1) -> an_enumerator
  298. *
  299. * Iterates over the range, passing each <code>n</code>th element to the block.
  300. * If begin and end are numeric, +n+ is added for each iteration.
  301. * Otherwise <code>step</code> invokes <code>succ</code> to iterate through
  302. * range elements.
  303. *
  304. * If no block is given, an enumerator is returned instead.
  305. *
  306. * range = Xs.new(1)..Xs.new(10)
  307. * range.step(2) {|x| puts x}
  308. * puts
  309. * range.step(3) {|x| puts x}
  310. *
  311. * <em>produces:</em>
  312. *
  313. * 1 x
  314. * 3 xxx
  315. * 5 xxxxx
  316. * 7 xxxxxxx
  317. * 9 xxxxxxxxx
  318. *
  319. * 1 x
  320. * 4 xxxx
  321. * 7 xxxxxxx
  322. * 10 xxxxxxxxxx
  323. *
  324. * See Range for the definition of class Xs.
  325. */
  326. static VALUE
  327. range_step(int argc, VALUE *argv, VALUE range)
  328. {
  329. VALUE b, e, step, tmp;
  330. RETURN_SIZED_ENUMERATOR(range, argc, argv, range_step_size);
  331. b = RANGE_BEG(range);
  332. e = RANGE_END(range);
  333. if (argc == 0) {
  334. step = INT2FIX(1);
  335. }
  336. else {
  337. rb_scan_args(argc, argv, "01", &step);
  338. if (!rb_obj_is_kind_of(step, rb_cNumeric)) {
  339. step = rb_to_int(step);
  340. }
  341. if (rb_funcall(step, '<', 1, INT2FIX(0))) {
  342. rb_raise(rb_eArgError, "step can't be negative");
  343. }
  344. else if (!rb_funcall(step, '>', 1, INT2FIX(0))) {
  345. rb_raise(rb_eArgError, "step can't be 0");
  346. }
  347. }
  348. if (FIXNUM_P(b) && FIXNUM_P(e) && FIXNUM_P(step)) { /* fixnums are special */
  349. long end = FIX2LONG(e);
  350. long i, unit = FIX2LONG(step);
  351. if (!EXCL(range))
  352. end += 1;
  353. i = FIX2LONG(b);
  354. while (i < end) {
  355. rb_yield(LONG2NUM(i));
  356. if (i + unit < i) break;
  357. i += unit;
  358. }
  359. }
  360. else if (SYMBOL_P(b) && SYMBOL_P(e)) { /* symbols are special */
  361. VALUE args[2], iter[2];
  362. args[0] = rb_sym_to_s(e);
  363. args[1] = EXCL(range) ? Qtrue : Qfalse;
  364. iter[0] = INT2FIX(1);
  365. iter[1] = step;
  366. rb_block_call(rb_sym_to_s(b), rb_intern("upto"), 2, args, sym_step_i, (VALUE)iter);
  367. }
  368. else if (ruby_float_step(b, e, step, EXCL(range))) {
  369. /* done */
  370. }
  371. else if (rb_obj_is_kind_of(b, rb_cNumeric) ||
  372. !NIL_P(rb_check_to_integer(b, "to_int")) ||
  373. !NIL_P(rb_check_to_integer(e, "to_int"))) {
  374. ID op = EXCL(range) ? '<' : idLE;
  375. VALUE v = b;
  376. int i = 0;
  377. while (RTEST(rb_funcall(v, op, 1, e))) {
  378. rb_yield(v);
  379. i++;
  380. v = rb_funcall(b, '+', 1, rb_funcall(INT2NUM(i), '*', 1, step));
  381. }
  382. }
  383. else {
  384. tmp = rb_check_string_type(b);
  385. if (!NIL_P(tmp)) {
  386. VALUE args[2], iter[2];
  387. b = tmp;
  388. args[0] = e;
  389. args[1] = EXCL(range) ? Qtrue : Qfalse;
  390. iter[0] = INT2FIX(1);
  391. iter[1] = step;
  392. rb_block_call(b, rb_intern("upto"), 2, args, step_i, (VALUE)iter);
  393. }
  394. else {
  395. VALUE args[2];
  396. if (!discrete_object_p(b)) {
  397. rb_raise(rb_eTypeError, "can't iterate from %s",
  398. rb_obj_classname(b));
  399. }
  400. args[0] = INT2FIX(1);
  401. args[1] = step;
  402. range_each_func(range, step_i, args);
  403. }
  404. }
  405. return range;
  406. }
  407. /*
  408. * call-seq:
  409. * rng.bsearch {|obj| block } -> value
  410. *
  411. * By using binary search, finds a value in range which meets the given
  412. * condition in O(log n) where n is the size of the array.
  413. *
  414. * You can use this method in two use cases: a find-minimum mode and
  415. * a find-any mode. In either case, the elements of the array must be
  416. * monotone (or sorted) with respect to the block.
  417. *
  418. * In find-minimum mode (this is a good choice for typical use case),
  419. * the block must return true or false, and there must be a value x
  420. * so that:
  421. *
  422. * - the block returns false for any value which is less than x, and
  423. * - the block returns true for any value which is greater than or
  424. * equal to i.
  425. *
  426. * If x is within the range, this method returns the value x.
  427. * Otherwise, it returns nil.
  428. *
  429. * ary = [0, 4, 7, 10, 12]
  430. * (0...ary.size).bsearch {|i| ary[i] >= 4 } #=> 1
  431. * (0...ary.size).bsearch {|i| ary[i] >= 6 } #=> 2
  432. * (0...ary.size).bsearch {|i| ary[i] >= 8 } #=> 3
  433. * (0...ary.size).bsearch {|i| ary[i] >= 100 } #=> nil
  434. *
  435. * (0.0...Float::INFINITY).bsearch {|x| Math.log(x) >= 0 } #=> 1.0
  436. *
  437. * In find-any mode (this behaves like libc's bsearch(3)), the block
  438. * must return a number, and there must be two values x and y (x <= y)
  439. * so that:
  440. *
  441. * - the block returns a positive number for v if v < x,
  442. * - the block returns zero for v if x <= v < y, and
  443. * - the block returns a negative number for v if y <= v.
  444. *
  445. * This method returns any value which is within the intersection of
  446. * the given range and x...y (if any). If there is no value that
  447. * satisfies the condition, it returns nil.
  448. *
  449. * ary = [0, 100, 100, 100, 200]
  450. * (0..4).bsearch {|i| 100 - ary[i] } #=> 1, 2 or 3
  451. * (0..4).bsearch {|i| 300 - ary[i] } #=> nil
  452. * (0..4).bsearch {|i| 50 - ary[i] } #=> nil
  453. *
  454. * You must not mix the two modes at a time; the block must always
  455. * return either true/false, or always return a number. It is
  456. * undefined which value is actually picked up at each iteration.
  457. */
  458. static VALUE
  459. range_bsearch(VALUE range)
  460. {
  461. VALUE beg, end;
  462. int smaller, satisfied = 0;
  463. #define BSEARCH_CHECK(val) \
  464. do { \
  465. VALUE v = rb_yield(val); \
  466. if (FIXNUM_P(v)) { \
  467. if (FIX2INT(v) == 0) return val; \
  468. smaller = FIX2INT(v) < 0; \
  469. } \
  470. else if (v == Qtrue) { \
  471. satisfied = 1; \
  472. smaller = 1; \
  473. } \
  474. else if (v == Qfalse || v == Qnil) { \
  475. smaller = 0; \
  476. } \
  477. else if (rb_obj_is_kind_of(v, rb_cNumeric)) { \
  478. int cmp = rb_cmpint(rb_funcall(v, id_cmp, 1, INT2FIX(0)), v, INT2FIX(0)); \
  479. if (!cmp) return val; \
  480. smaller = cmp < 0; \
  481. } \
  482. else { \
  483. smaller = RTEST(v); \
  484. } \
  485. } while (0)
  486. beg = RANGE_BEG(range);
  487. end = RANGE_END(range);
  488. if (FIXNUM_P(beg) && FIXNUM_P(end)) {
  489. long low = FIX2LONG(beg);
  490. long high = FIX2LONG(end);
  491. long mid, org_high;
  492. if (EXCL(range)) high--;
  493. org_high = high;
  494. while (low < high) {
  495. mid = low + ((high - low) / 2);
  496. BSEARCH_CHECK(INT2FIX(mid));
  497. if (smaller) {
  498. high = mid;
  499. }
  500. else {
  501. low = mid + 1;
  502. }
  503. }
  504. if (low == org_high) {
  505. BSEARCH_CHECK(INT2FIX(low));
  506. if (!smaller) return Qnil;
  507. }
  508. if (!satisfied) return Qnil;
  509. return INT2FIX(low);
  510. }
  511. else if (RB_TYPE_P(beg, T_FLOAT) || RB_TYPE_P(end, T_FLOAT)) {
  512. double low = RFLOAT_VALUE(rb_Float(beg));
  513. double high = RFLOAT_VALUE(rb_Float(end));
  514. double mid, org_high;
  515. int count;
  516. org_high = high;
  517. #ifdef FLT_RADIX
  518. #ifdef DBL_MANT_DIG
  519. #define BSEARCH_MAXCOUNT (((FLT_RADIX) - 1) * (DBL_MANT_DIG + DBL_MAX_EXP) + 100)
  520. #else
  521. #define BSEARCH_MAXCOUNT (53 + 1023 + 100)
  522. #endif
  523. #else
  524. #define BSEARCH_MAXCOUNT (53 + 1023 + 100)
  525. #endif
  526. if (isinf(high) && high > 0) {
  527. /* the range is (low..INFINITY) */
  528. double nhigh = 1.0, inc;
  529. if (nhigh < low) nhigh = low;
  530. count = BSEARCH_MAXCOUNT;
  531. /* find upper bound by checking low, low*2, low*4, ... */
  532. while (count >= 0 && !isinf(nhigh)) {
  533. BSEARCH_CHECK(DBL2NUM(nhigh));
  534. if (smaller) break;
  535. high = nhigh;
  536. nhigh *= 2;
  537. count--;
  538. }
  539. if (isinf(nhigh) || count < 0) {
  540. /* upper bound not found; then, search in very near INFINITY */
  541. /* (x..INFINITY where x is not INFINITY but x*2 is INFINITY) */
  542. inc = high / 2;
  543. count = BSEARCH_MAXCOUNT;
  544. while (count >= 0 && inc > 0) {
  545. nhigh = high + inc;
  546. if (!isinf(nhigh)) {
  547. BSEARCH_CHECK(DBL2NUM(nhigh));
  548. if (smaller) {
  549. /* upper bound found; */
  550. /* the desired value is within high..nhigh */
  551. low = high;
  552. high = nhigh;
  553. goto binsearch;
  554. }
  555. else {
  556. high = nhigh;
  557. }
  558. }
  559. inc /= 2;
  560. count--;
  561. }
  562. /* lower bound not found; */
  563. /* there is no candidate except INFINITY itself */
  564. high *= 2; /* generate INFINITY */
  565. if (isinf(high) && !EXCL(range)) {
  566. BSEARCH_CHECK(DBL2NUM(high));
  567. if (!satisfied) return Qnil;
  568. if (smaller) return DBL2NUM(high);
  569. }
  570. return Qnil;
  571. }
  572. /* upper bound found; the desired value is within low..nhigh */
  573. high = nhigh;
  574. }
  575. if (isinf(low) && low < 0) {
  576. /* the range is (-INFINITY..high) */
  577. volatile double nlow = -1.0, dec;
  578. if (nlow > high) nlow = high;
  579. count = BSEARCH_MAXCOUNT;
  580. /* find lower bound by checking low, low*2, low*4, ... */
  581. while (count >= 0 && !isinf(nlow)) {
  582. BSEARCH_CHECK(DBL2NUM(nlow));
  583. if (!smaller) break;
  584. low = nlow;
  585. nlow *= 2;
  586. count--;
  587. }
  588. if (isinf(nlow) || count < 0) {
  589. /* lower bound not found; then, search in very near -INFINITY */
  590. /* (-INFINITY..x where x is not -INFINITY but x*2 is -INFINITY) */
  591. dec = low / 2;
  592. count = BSEARCH_MAXCOUNT;
  593. while (count >= 0 && dec < 0) {
  594. nlow = low + dec;
  595. if (!isinf(nlow)) {
  596. BSEARCH_CHECK(DBL2NUM(nlow));
  597. if (!smaller) {
  598. /* lower bound found; */
  599. /* the desired value is within nlow..low */
  600. high = low;
  601. low = nlow;
  602. goto binsearch;
  603. }
  604. else {
  605. low = nlow;
  606. }
  607. }
  608. dec /= 2;
  609. count--;
  610. }
  611. /* lower bound not found; */
  612. /* there is no candidate except -INFINITY itself */
  613. nlow = low * 2; /* generate -INFINITY */
  614. if (isinf(nlow)) {
  615. BSEARCH_CHECK(DBL2NUM(nlow));
  616. if (!satisfied) return Qnil;
  617. if (smaller) return DBL2NUM(nlow);
  618. }
  619. if (!satisfied) return Qnil;
  620. return DBL2NUM(low);
  621. }
  622. low = nlow;
  623. }
  624. binsearch:
  625. /* find the desired value within low..high */
  626. /* where low is not -INFINITY and high is not INFINITY */
  627. count = BSEARCH_MAXCOUNT;
  628. while (low < high && count >= 0) {
  629. mid = low + ((high - low) / 2);
  630. BSEARCH_CHECK(DBL2NUM(mid));
  631. if (smaller) {
  632. high = mid;
  633. }
  634. else {
  635. low = mid;
  636. }
  637. count--;
  638. }
  639. BSEARCH_CHECK(DBL2NUM(low));
  640. if (!smaller) {
  641. BSEARCH_CHECK(DBL2NUM(high));
  642. if (!smaller) {
  643. return Qnil;
  644. }
  645. low = high;
  646. }
  647. if (!satisfied) return Qnil;
  648. if (EXCL(range) && low >= org_high) return Qnil;
  649. return DBL2NUM(low);
  650. #undef BSEARCH_MAXCOUNT
  651. }
  652. else if (!NIL_P(rb_check_to_integer(beg, "to_int")) &&
  653. !NIL_P(rb_check_to_integer(end, "to_int"))) {
  654. VALUE low = beg;
  655. VALUE high = end;
  656. VALUE mid, org_high;
  657. if (EXCL(range)) high = rb_funcall(high, '-', 1, INT2FIX(1));
  658. org_high = high;
  659. while (rb_cmpint(rb_funcall(low, id_cmp, 1, high), low, high) < 0) {
  660. mid = rb_funcall(rb_funcall(high, '+', 1, low), '/', 1, INT2FIX(2));
  661. BSEARCH_CHECK(mid);
  662. if (smaller) {
  663. high = mid;
  664. }
  665. else {
  666. low = rb_funcall(mid, '+', 1, INT2FIX(1));
  667. }
  668. }
  669. if (rb_equal(low, org_high)) {
  670. BSEARCH_CHECK(low);
  671. if (!smaller) return Qnil;
  672. }
  673. if (!satisfied) return Qnil;
  674. return low;
  675. }
  676. else {
  677. rb_raise(rb_eTypeError, "can't do binary search for %s", rb_obj_classname(beg));
  678. }
  679. return range;
  680. }
  681. static VALUE
  682. each_i(VALUE v, void *arg)
  683. {
  684. rb_yield(v);
  685. return Qnil;
  686. }
  687. static VALUE
  688. sym_each_i(VALUE v, void *arg)
  689. {
  690. rb_yield(rb_str_intern(v));
  691. return Qnil;
  692. }
  693. /*
  694. * call-seq:
  695. * rng.size -> num
  696. *
  697. * Returns the number of elements in the range.
  698. *
  699. * (10..20).size #=> 11
  700. */
  701. static VALUE
  702. range_size(VALUE range)
  703. {
  704. VALUE b = RANGE_BEG(range), e = RANGE_END(range);
  705. if (rb_obj_is_kind_of(b, rb_cNumeric) && rb_obj_is_kind_of(e, rb_cNumeric)) {
  706. return num_interval_step_size(b, e, INT2FIX(1), EXCL(range));
  707. }
  708. return Qnil;
  709. }
  710. /*
  711. * call-seq:
  712. * rng.each {| i | block } -> rng
  713. * rng.each -> an_enumerator
  714. *
  715. * Iterates over the elements of range, passing each in turn to the
  716. * block.
  717. *
  718. * The +each+ method can only be used if the begin object of the range
  719. * supports the +succ+ method. A TypeError is raised if the object
  720. * does not have +succ+ method defined (like Float).
  721. *
  722. * If no block is given, an enumerator is returned instead.
  723. *
  724. * (10..15).each {|n| print n, ' ' }
  725. * # prints: 10 11 12 13 14 15
  726. *
  727. * (2.5..5).each {|n| print n, ' ' }
  728. * # raises: TypeError: can't iterate from Float
  729. */
  730. static VALUE
  731. range_each(VALUE range)
  732. {
  733. VALUE beg, end;
  734. RETURN_SIZED_ENUMERATOR(range, 0, 0, range_size);
  735. beg = RANGE_BEG(range);
  736. end = RANGE_END(range);
  737. if (FIXNUM_P(beg) && FIXNUM_P(end)) { /* fixnums are special */
  738. long lim = FIX2LONG(end);
  739. long i;
  740. if (!EXCL(range))
  741. lim += 1;
  742. for (i = FIX2LONG(beg); i < lim; i++) {
  743. rb_yield(LONG2FIX(i));
  744. }
  745. }
  746. else if (SYMBOL_P(beg) && SYMBOL_P(end)) { /* symbols are special */
  747. VALUE args[2];
  748. args[0] = rb_sym_to_s(end);
  749. args[1] = EXCL(range) ? Qtrue : Qfalse;
  750. rb_block_call(rb_sym_to_s(beg), rb_intern("upto"), 2, args, sym_each_i, 0);
  751. }
  752. else {
  753. VALUE tmp = rb_check_string_type(beg);
  754. if (!NIL_P(tmp)) {
  755. VALUE args[2];
  756. args[0] = end;
  757. args[1] = EXCL(range) ? Qtrue : Qfalse;
  758. rb_block_call(tmp, rb_intern("upto"), 2, args, rb_yield, 0);
  759. }
  760. else {
  761. if (!discrete_object_p(beg)) {
  762. rb_raise(rb_eTypeError, "can't iterate from %s",
  763. rb_obj_classname(beg));
  764. }
  765. range_each_func(range, each_i, NULL);
  766. }
  767. }
  768. return range;
  769. }
  770. /*
  771. * call-seq:
  772. * rng.begin -> obj
  773. *
  774. * Returns the object that defines the beginning of the range.
  775. *
  776. * (1..10).begin #=> 1
  777. */
  778. static VALUE
  779. range_begin(VALUE range)
  780. {
  781. return RANGE_BEG(range);
  782. }
  783. /*
  784. * call-seq:
  785. * rng.end -> obj
  786. *
  787. * Returns the object that defines the end of the range.
  788. *
  789. * (1..10).end #=> 10
  790. * (1...10).end #=> 10
  791. */
  792. static VALUE
  793. range_end(VALUE range)
  794. {
  795. return RANGE_END(range);
  796. }
  797. static VALUE
  798. first_i(VALUE i, VALUE *ary)
  799. {
  800. long n = NUM2LONG(ary[0]);
  801. if (n <= 0) {
  802. rb_iter_break();
  803. }
  804. rb_ary_push(ary[1], i);
  805. n--;
  806. ary[0] = INT2NUM(n);
  807. return Qnil;
  808. }
  809. /*
  810. * call-seq:
  811. * rng.first -> obj
  812. * rng.first(n) -> an_array
  813. *
  814. * Returns the first object in the range, or an array of the first +n+
  815. * elements.
  816. *
  817. * (10..20).first #=> 10
  818. * (10..20).first(3) #=> [10, 11, 12]
  819. */
  820. static VALUE
  821. range_first(int argc, VALUE *argv, VALUE range)
  822. {
  823. VALUE n, ary[2];
  824. if (argc == 0) return RANGE_BEG(range);
  825. rb_scan_args(argc, argv, "1", &n);
  826. ary[0] = n;
  827. ary[1] = rb_ary_new2(NUM2LONG(n));
  828. rb_block_call(range, idEach, 0, 0, first_i, (VALUE)ary);
  829. return ary[1];
  830. }
  831. /*
  832. * call-seq:
  833. * rng.last -> obj
  834. * rng.last(n) -> an_array
  835. *
  836. * Returns the last object in the range,
  837. * or an array of the last +n+ elements.
  838. *
  839. * Note that with no arguments +last+ will return the object that defines
  840. * the end of the range even if #exclude_end? is +true+.
  841. *
  842. * (10..20).last #=> 20
  843. * (10...20).last #=> 20
  844. * (10..20).last(3) #=> [18, 19, 20]
  845. * (10...20).last(3) #=> [17, 18, 19]
  846. */
  847. static VALUE
  848. range_last(int argc, VALUE *argv, VALUE range)
  849. {
  850. if (argc == 0) return RANGE_END(range);
  851. return rb_ary_last(argc, argv, rb_Array(range));
  852. }
  853. /*
  854. * call-seq:
  855. * rng.min -> obj
  856. * rng.min {| a,b | block } -> obj
  857. *
  858. * Returns the minimum value in the range. Returns +nil+ if the begin
  859. * value of the range is larger than the end value.
  860. *
  861. * Can be given an optional block to override the default comparison
  862. * method <code>a <=> b</code>.
  863. *
  864. * (10..20).min #=> 10
  865. */
  866. static VALUE
  867. range_min(VALUE range)
  868. {
  869. if (rb_block_given_p()) {
  870. return rb_call_super(0, 0);
  871. }
  872. else {
  873. VALUE b = RANGE_BEG(range);
  874. VALUE e = RANGE_END(range);
  875. int c = rb_cmpint(rb_funcall(b, id_cmp, 1, e), b, e);
  876. if (c > 0 || (c == 0 && EXCL(range)))
  877. return Qnil;
  878. return b;
  879. }
  880. }
  881. /*
  882. * call-seq:
  883. * rng.max -> obj
  884. * rng.max {| a,b | block } -> obj
  885. *
  886. * Returns the maximum value in the range. Returns +nil+ if the begin
  887. * value of the range larger than the end value.
  888. *
  889. * Can be given an optional block to override the default comparison
  890. * method <code>a <=> b</code>.
  891. *
  892. * (10..20).max #=> 20
  893. */
  894. static VALUE
  895. range_max(VALUE range)
  896. {
  897. VALUE e = RANGE_END(range);
  898. int nm = FIXNUM_P(e) || rb_obj_is_kind_of(e, rb_cNumeric);
  899. if (rb_block_given_p() || (EXCL(range) && !nm)) {
  900. return rb_call_super(0, 0);
  901. }
  902. else {
  903. VALUE b = RANGE_BEG(range);
  904. int c = rb_cmpint(rb_funcall(b, id_cmp, 1, e), b, e);
  905. if (c > 0)
  906. return Qnil;
  907. if (EXCL(range)) {
  908. if (!FIXNUM_P(e) && !rb_obj_is_kind_of(e, rb_cInteger)) {
  909. rb_raise(rb_eTypeError, "cannot exclude non Integer end value");
  910. }
  911. if (c == 0) return Qnil;
  912. if (!FIXNUM_P(b) && !rb_obj_is_kind_of(b,rb_cInteger)) {
  913. rb_raise(rb_eTypeError, "cannot exclude end value with non Integer begin value");
  914. }
  915. if (FIXNUM_P(e)) {
  916. return LONG2NUM(FIX2LONG(e) - 1);
  917. }
  918. return rb_funcall(e, '-', 1, INT2FIX(1));
  919. }
  920. return e;
  921. }
  922. }
  923. int
  924. rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
  925. {
  926. VALUE b, e;
  927. int excl;
  928. if (rb_obj_is_kind_of(range, rb_cRange)) {
  929. b = RANGE_BEG(range);
  930. e = RANGE_END(range);
  931. excl = EXCL(range);
  932. }
  933. else {
  934. if (!rb_respond_to(range, id_beg)) return (int)Qfalse;
  935. if (!rb_respond_to(range, id_end)) return (int)Qfalse;
  936. b = rb_funcall(range, id_beg, 0);
  937. e = rb_funcall(range, id_end, 0);
  938. excl = RTEST(rb_funcall(range, rb_intern("exclude_end?"), 0));
  939. }
  940. *begp = b;
  941. *endp = e;
  942. *exclp = excl;
  943. return (int)Qtrue;
  944. }
  945. VALUE
  946. rb_range_beg_len(VALUE range, long *begp, long *lenp, long len, int err)
  947. {
  948. long beg, end, origbeg, origend;
  949. VALUE b, e;
  950. int excl;
  951. if (!rb_range_values(range, &b, &e, &excl))
  952. return Qfalse;
  953. beg = NUM2LONG(b);
  954. end = NUM2LONG(e);
  955. origbeg = beg;
  956. origend = end;
  957. if (beg < 0) {
  958. beg += len;
  959. if (beg < 0)
  960. goto out_of_range;
  961. }
  962. if (end < 0)
  963. end += len;
  964. if (!excl)
  965. end++; /* include end point */
  966. if (err == 0 || err == 2) {
  967. if (beg > len)
  968. goto out_of_range;
  969. if (end > len)
  970. end = len;
  971. }
  972. len = end - beg;
  973. if (len < 0)
  974. len = 0;
  975. *begp = beg;
  976. *lenp = len;
  977. return Qtrue;
  978. out_of_range:
  979. if (err) {
  980. rb_raise(rb_eRangeError, "%ld..%s%ld out of range",
  981. origbeg, excl ? "." : "", origend);
  982. }
  983. return Qnil;
  984. }
  985. /*
  986. * call-seq:
  987. * rng.to_s -> string
  988. *
  989. * Convert this range object to a printable form (using #to_s to convert the
  990. * begin and end objects).
  991. */
  992. static VALUE
  993. range_to_s(VALUE range)
  994. {
  995. VALUE str, str2;
  996. str = rb_obj_as_string(RANGE_BEG(range));
  997. str2 = rb_obj_as_string(RANGE_END(range));
  998. str = rb_str_dup(str);
  999. rb_str_cat(str, "...", EXCL(range) ? 3 : 2);
  1000. rb_str_append(str, str2);
  1001. OBJ_INFECT(str, str2);
  1002. return str;
  1003. }
  1004. static VALUE
  1005. inspect_range(VALUE range, VALUE dummy, int recur)
  1006. {
  1007. VALUE str, str2;
  1008. if (recur) {
  1009. return rb_str_new2(EXCL(range) ? "(... ... ...)" : "(... .. ...)");
  1010. }
  1011. str = rb_inspect(RANGE_BEG(range));
  1012. str2 = rb_inspect(RANGE_END(range));
  1013. str = rb_str_dup(str);
  1014. rb_str_cat(str, "...", EXCL(range) ? 3 : 2);
  1015. rb_str_append(str, str2);
  1016. OBJ_INFECT(str, str2);
  1017. return str;
  1018. }
  1019. /*
  1020. * call-seq:
  1021. * rng.inspect -> string
  1022. *
  1023. * Convert this range object to a printable form (using
  1024. * <code>inspect</code> to convert the begin and end
  1025. * objects).
  1026. */
  1027. static VALUE
  1028. range_inspect(VALUE range)
  1029. {
  1030. return rb_exec_recursive(inspect_range, range, 0);
  1031. }
  1032. /*
  1033. * call-seq:
  1034. * rng === obj -> true or false
  1035. *
  1036. * Returns <code>true</code> if +obj+ is an element of the range,
  1037. * <code>false</code> otherwise. Conveniently, <code>===</code> is the
  1038. * comparison operator used by <code>case</code> statements.
  1039. *
  1040. * case 79
  1041. * when 1..50 then print "low\n"
  1042. * when 51..75 then print "medium\n"
  1043. * when 76..100 then print "high\n"
  1044. * end
  1045. *
  1046. * <em>produces:</em>
  1047. *
  1048. * high
  1049. */
  1050. static VALUE
  1051. range_eqq(VALUE range, VALUE val)
  1052. {
  1053. return rb_funcall(range, rb_intern("include?"), 1, val);
  1054. }
  1055. /*
  1056. * call-seq:
  1057. * rng.member?(obj) -> true or false
  1058. * rng.include?(obj) -> true or false
  1059. *
  1060. * Returns <code>true</code> if +obj+ is an element of
  1061. * the range, <code>false</code> otherwise. If begin and end are
  1062. * numeric, comparison is done according to the magnitude of the values.
  1063. *
  1064. * ("a".."z").include?("g") #=> true
  1065. * ("a".."z").include?("A") #=> false
  1066. * ("a".."z").include?("cc") #=> false
  1067. */
  1068. static VALUE
  1069. range_include(VALUE range, VALUE val)
  1070. {
  1071. VALUE beg = RANGE_BEG(range);
  1072. VALUE end = RANGE_END(range);
  1073. int nv = FIXNUM_P(beg) || FIXNUM_P(end) ||
  1074. rb_obj_is_kind_of(beg, rb_cNumeric) ||
  1075. rb_obj_is_kind_of(end, rb_cNumeric);
  1076. if (nv ||
  1077. !NIL_P(rb_check_to_integer(beg, "to_int")) ||
  1078. !NIL_P(rb_check_to_integer(end, "to_int"))) {
  1079. if (r_le(beg, val)) {
  1080. if (EXCL(range)) {
  1081. if (r_lt(val, end))
  1082. return Qtrue;
  1083. }
  1084. else {
  1085. if (r_le(val, end))
  1086. return Qtrue;
  1087. }
  1088. }
  1089. return Qfalse;
  1090. }
  1091. else if (RB_TYPE_P(beg, T_STRING) && RB_TYPE_P(end, T_STRING) &&
  1092. RSTRING_LEN(beg) == 1 && RSTRING_LEN(end) == 1) {
  1093. if (NIL_P(val)) return Qfalse;
  1094. if (RB_TYPE_P(val, T_STRING)) {
  1095. if (RSTRING_LEN(val) == 0 || RSTRING_LEN(val) > 1)
  1096. return Qfalse;
  1097. else {
  1098. char b = RSTRING_PTR(beg)[0];
  1099. char e = RSTRING_PTR(end)[0];
  1100. char v = RSTRING_PTR(val)[0];
  1101. if (ISASCII(b) && ISASCII(e) && ISASCII(v)) {
  1102. if (b <= v && v < e) return Qtrue;
  1103. if (!EXCL(range) && v == e) return Qtrue;
  1104. return Qfalse;
  1105. }
  1106. }
  1107. }
  1108. }
  1109. /* TODO: ruby_frame->this_func = rb_intern("include?"); */
  1110. return rb_call_super(1, &val);
  1111. }
  1112. /*
  1113. * call-seq:
  1114. * rng.cover?(obj) -> true or false
  1115. *
  1116. * Returns <code>true</code> if +obj+ is between the begin and end of
  1117. * the range.
  1118. *
  1119. * This tests <code>begin <= obj <= end</code> when #exclude_end? is +false+
  1120. * and <code>begin <= obj < end</code> when #exclude_end? is +true+.
  1121. *
  1122. * ("a".."z").cover?("c") #=> true
  1123. * ("a".."z").cover?("5") #=> false
  1124. * ("a".."z").cover?("cc") #=> true
  1125. */
  1126. static VALUE
  1127. range_cover(VALUE range, VALUE val)
  1128. {
  1129. VALUE beg, end;
  1130. beg = RANGE_BEG(range);
  1131. end = RANGE_END(range);
  1132. if (r_le(beg, val)) {
  1133. if (EXCL(range)) {
  1134. if (r_lt(val, end))
  1135. return Qtrue;
  1136. }
  1137. else {
  1138. if (r_le(val, end))
  1139. return Qtrue;
  1140. }
  1141. }
  1142. return Qfalse;
  1143. }
  1144. static VALUE
  1145. range_dumper(VALUE range)
  1146. {
  1147. VALUE v;
  1148. NEWOBJ_OF(m, struct RObject, rb_cObject, T_OBJECT);
  1149. v = (VALUE)m;
  1150. rb_ivar_set(v, id_excl, RANGE_EXCL(range));
  1151. rb_ivar_set(v, id_beg, RANGE_BEG(range));
  1152. rb_ivar_set(v, id_end, RANGE_END(range));
  1153. return v;
  1154. }
  1155. static VALUE
  1156. range_loader(VALUE range, VALUE obj)
  1157. {
  1158. if (!RB_TYPE_P(obj, T_OBJECT) || RBASIC(obj)->klass != rb_cObject) {
  1159. rb_raise(rb_eTypeError, "not a dumped range object");
  1160. }
  1161. RSTRUCT(range)->as.ary[0] = rb_ivar_get(obj, id_beg);
  1162. RSTRUCT(range)->as.ary[1] = rb_ivar_get(obj, id_end);
  1163. RSTRUCT(range)->as.ary[2] = rb_ivar_get(obj, id_excl);
  1164. return range;
  1165. }
  1166. static VALUE
  1167. range_alloc(VALUE klass)
  1168. {
  1169. /* rb_struct_alloc_noinit itself should not be used because
  1170. * rb_marshal_define_compat uses equality of allocaiton function */
  1171. return rb_struct_alloc_noinit(klass);
  1172. }
  1173. /* A <code>Range</code> represents an interval---a set of values with a
  1174. * beginning and an end. Ranges may be constructed using the
  1175. * <em>s</em><code>..</code><em>e</em> and
  1176. * <em>s</em><code>...</code><em>e</em> literals, or with
  1177. * Range::new. Ranges constructed using <code>..</code>
  1178. * run from the beginning to the end inclusively. Those created using
  1179. * <code>...</code> exclude the end value. When used as an iterator,
  1180. * ranges return each value in the sequence.
  1181. *
  1182. * (-1..-5).to_a #=> []
  1183. * (-5..-1).to_a #=> [-5, -4, -3, -2, -1]
  1184. * ('a'..'e').to_a #=> ["a", "b", "c", "d", "e"]
  1185. * ('a'...'e').to_a #=> ["a", "b", "c", "d"]
  1186. *
  1187. * == Custom Objects in Ranges
  1188. *
  1189. * Ranges can be constructed using any objects that can be compared
  1190. * using the <code><=></code> operator.
  1191. * Methods that treat the range as a sequence (#each and methods inherited
  1192. * from Enumerable) expect the begin object to implement a
  1193. * <code>succ</code> method to return the next object in sequence.
  1194. * The #step and #include? methods require the begin
  1195. * object to implement <code>succ</code> or to be numeric.
  1196. *
  1197. * In the <code>Xs</code> class below both <code><=></code> and
  1198. * <code>succ</code> are implemented so <code>Xs</code> can be used
  1199. * to construct ranges. Note that the Comparable module is included
  1200. * so the <code>==</code> method is defined in terms of <code><=></code>.
  1201. *
  1202. * class Xs # represent a string of 'x's
  1203. * include Comparable
  1204. * attr :length
  1205. * def initialize(n)
  1206. * @length = n
  1207. * end
  1208. * def succ
  1209. * Xs.new(@length + 1)
  1210. * end
  1211. * def <=>(other)
  1212. * @length <=> other.length
  1213. * end
  1214. * def to_s
  1215. * sprintf "%2d #{inspect}", @length
  1216. * end
  1217. * def inspect
  1218. * 'x' * @length
  1219. * end
  1220. * end
  1221. *
  1222. * An example of using <code>Xs</code> to construct a range:
  1223. *
  1224. * r = Xs.new(3)..Xs.new(6) #=> xxx..xxxxxx
  1225. * r.to_a #=> [xxx, xxxx, xxxxx, xxxxxx]
  1226. * r.member?(Xs.new(5)) #=> true
  1227. *
  1228. */
  1229. void
  1230. Init_Range(void)
  1231. {
  1232. #undef rb_intern
  1233. #define rb_intern(str) rb_intern_const(str)
  1234. id_cmp = rb_intern("<=>");
  1235. id_succ = rb_intern("succ");
  1236. id_beg = rb_intern("begin");
  1237. id_end = rb_intern("end");
  1238. id_excl = rb_intern("excl");
  1239. rb_cRange = rb_struct_define_without_accessor(
  1240. "Range", rb_cObject, range_alloc,
  1241. "begin", "end", "excl", NULL);
  1242. rb_include_module(rb_cRange, rb_mEnumerable);
  1243. rb_marshal_define_compat(rb_cRange, rb_cObject, range_dumper, range_loader);
  1244. rb_define_method(rb_cRange, "initialize", range_initialize, -1);
  1245. rb_define_method(rb_cRange, "initialize_copy", range_initialize_copy, 1);
  1246. rb_define_method(rb_cRange, "==", range_eq, 1);
  1247. rb_define_method(rb_cRange, "===", range_eqq, 1);
  1248. rb_define_method(rb_cRange, "eql?", range_eql, 1);
  1249. rb_define_method(rb_cRange, "hash", range_hash, 0);
  1250. rb_define_method(rb_cRange, "each", range_each, 0);
  1251. rb_define_method(rb_cRange, "step", range_step, -1);
  1252. rb_define_method(rb_cRange, "bsearch", range_bsearch, 0);
  1253. rb_define_method(rb_cRange, "begin", range_begin, 0);
  1254. rb_define_method(rb_cRange, "end", range_end, 0);
  1255. rb_define_method(rb_cRange, "first", range_first, -1);
  1256. rb_define_method(rb_cRange, "last", range_last, -1);
  1257. rb_define_method(rb_cRange, "min", range_min, 0);
  1258. rb_define_method(rb_cRange, "max", range_max, 0);
  1259. rb_define_method(rb_cRange, "size", range_size, 0);
  1260. rb_define_method(rb_cRange, "to_s", range_to_s, 0);
  1261. rb_define_method(rb_cRange, "inspect", range_inspect, 0);
  1262. rb_define_method(rb_cRange, "exclude_end?", range_exclude_end_p, 0);
  1263. rb_define_method(rb_cRange, "member?", range_include, 1);
  1264. rb_define_method(rb_cRange, "include?", range_include, 1);
  1265. rb_define_method(rb_cRange, "cover?", range_cover, 1);
  1266. }