source: EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-range-ext/src/range.c@ 439

Last change on this file since 439 was 439, checked in by coas-nagasima, 4 years ago

mrubyを2.1.1に更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 4.1 KB
Line 
1#include <mruby.h>
2#include <mruby/range.h>
3#include <math.h>
4
5static mrb_bool
6r_le(mrb_state *mrb, mrb_value a, mrb_value b)
7{
8 mrb_int n = mrb_cmp(mrb, a, b);
9
10 if (n == 0 || n == -1) return TRUE;
11 return FALSE;
12}
13
14static mrb_bool
15r_lt(mrb_state *mrb, mrb_value a, mrb_value b)
16{
17 return mrb_cmp(mrb, a, b) == -1;
18}
19
20/*
21 * call-seq:
22 * rng.cover?(obj) -> true or false
23 *
24 * Returns <code>true</code> if +obj+ is between the begin and end of
25 * the range.
26 *
27 * This tests <code>begin <= obj <= end</code> when #exclude_end? is +false+
28 * and <code>begin <= obj < end</code> when #exclude_end? is +true+.
29 *
30 * ("a".."z").cover?("c") #=> true
31 * ("a".."z").cover?("5") #=> false
32 * ("a".."z").cover?("cc") #=> true
33 */
34static mrb_value
35range_cover(mrb_state *mrb, mrb_value range)
36{
37 mrb_value val;
38 struct RRange *r = mrb_range_ptr(mrb, range);
39 mrb_value beg, end;
40
41 mrb_get_args(mrb, "o", &val);
42
43 beg = RANGE_BEG(r);
44 end = RANGE_END(r);
45
46 if (r_le(mrb, beg, val)) {
47 if (RANGE_EXCL(r)) {
48 if (r_lt(mrb, val, end))
49 return mrb_true_value();
50 }
51 else {
52 if (r_le(mrb, val, end))
53 return mrb_true_value();
54 }
55 }
56
57 return mrb_false_value();
58}
59
60/*
61 * call-seq:
62 * rng.last -> obj
63 * rng.last(n) -> an_array
64 *
65 * Returns the last object in the range,
66 * or an array of the last +n+ elements.
67 *
68 * Note that with no arguments +last+ will return the object that defines
69 * the end of the range even if #exclude_end? is +true+.
70 *
71 * (10..20).last #=> 20
72 * (10...20).last #=> 20
73 * (10..20).last(3) #=> [18, 19, 20]
74 * (10...20).last(3) #=> [17, 18, 19]
75 */
76static mrb_value
77range_last(mrb_state *mrb, mrb_value range)
78{
79 mrb_value num;
80 mrb_value array;
81
82 if (mrb_get_args(mrb, "|o", &num) == 0) {
83 return mrb_range_end(mrb, range);
84 }
85
86 array = mrb_funcall(mrb, range, "to_a", 0);
87 return mrb_funcall(mrb, array, "last", 1, mrb_to_int(mrb, num));
88}
89
90/*
91 * call-seq:
92 * rng.size -> num
93 *
94 * Returns the number of elements in the range. Both the begin and the end of
95 * the Range must be Numeric, otherwise nil is returned.
96 *
97 * (10..20).size #=> 11
98 * ('a'..'z').size #=> nil
99 */
100
101#ifndef MRB_WITHOUT_FLOAT
102static mrb_value
103range_size(mrb_state *mrb, mrb_value range)
104{
105 struct RRange *r = mrb_range_ptr(mrb, range);
106 mrb_value beg, end;
107 mrb_float beg_f, end_f;
108 mrb_bool num_p = TRUE;
109 mrb_bool excl;
110
111 beg = RANGE_BEG(r);
112 end = RANGE_END(r);
113 excl = RANGE_EXCL(r);
114 if (mrb_fixnum_p(beg)) {
115 beg_f = (mrb_float)mrb_fixnum(beg);
116 }
117 else if (mrb_float_p(beg)) {
118 beg_f = mrb_float(beg);
119 }
120 else {
121 num_p = FALSE;
122 }
123 if (mrb_fixnum_p(end)) {
124 end_f = (mrb_float)mrb_fixnum(end);
125 }
126 else if (mrb_float_p(end)) {
127 end_f = mrb_float(end);
128 }
129 else {
130 num_p = FALSE;
131 }
132 if (num_p) {
133 mrb_float n = end_f - beg_f;
134 mrb_float err = (fabs(beg_f) + fabs(end_f) + fabs(end_f-beg_f)) * MRB_FLOAT_EPSILON;
135
136 if (err>0.5) err=0.5;
137 if (excl) {
138 if (n<=0) return mrb_fixnum_value(0);
139 if (n<1)
140 n = 0;
141 else
142 n = floor(n - err);
143 }
144 else {
145 if (n<0) return mrb_fixnum_value(0);
146 n = floor(n + err);
147 }
148 if (isinf(n+1))
149 return mrb_float_value(mrb, INFINITY);
150 return mrb_fixnum_value((mrb_int)n+1);
151 }
152 return mrb_nil_value();
153}
154#else
155static mrb_value
156range_size(mrb_state *mrb, mrb_value range)
157{
158 struct RRange *r = mrb_range_ptr(mrb, range);
159 mrb_value beg, end;
160 mrb_int excl;
161
162 beg = RANGE_BEG(r);
163 end = RANGE_END(r);
164 excl = RANGE_EXCL(r) ? 0 : 1;
165
166 if (mrb_fixnum_p(beg) && mrb_fixnum_p(end)) {
167 mrb_int a = mrb_fixnum(beg);
168 mrb_int b = mrb_fixnum(end);
169 mrb_int c = b - a + excl;
170
171 return mrb_fixnum_value(c);
172 }
173 return mrb_nil_value();
174}
175#endif /* MRB_WITHOUT_FLOAT */
176
177void
178mrb_mruby_range_ext_gem_init(mrb_state* mrb)
179{
180 struct RClass * s = mrb_class_get(mrb, "Range");
181
182 mrb_define_method(mrb, s, "cover?", range_cover, MRB_ARGS_REQ(1));
183 mrb_define_method(mrb, s, "last", range_last, MRB_ARGS_OPT(1));
184 mrb_define_method(mrb, s, "size", range_size, MRB_ARGS_NONE());
185}
186
187void
188mrb_mruby_range_ext_gem_final(mrb_state* mrb)
189{
190}
Note: See TracBrowser for help on using the repository browser.