Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)
compar.c
Go to the documentation of this file.
1/**********************************************************************
2
3 compar.c -
4
5 $Author$
6 created at: Thu Aug 26 14:39:48 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
12#include "id.h"
13#include "internal.h"
14#include "internal/compar.h"
15#include "internal/error.h"
16#include "internal/vm.h"
17#include "ruby/ruby.h"
18
20
21static VALUE
22rb_cmp(VALUE x, VALUE y)
23{
24 return rb_funcallv(x, idCmp, 1, &y);
25}
26
27void
29{
30 VALUE classname;
31
32 if (SPECIAL_CONST_P(y) || BUILTIN_TYPE(y) == T_FLOAT) {
33 classname = rb_inspect(y);
34 }
35 else {
36 classname = rb_obj_class(y);
37 }
38 rb_raise(rb_eArgError, "comparison of %"PRIsVALUE" with %"PRIsVALUE" failed",
39 rb_obj_class(x), classname);
40}
41
42static VALUE
43invcmp_recursive(VALUE x, VALUE y, int recursive)
44{
45 if (recursive) return Qnil;
46 return rb_cmp(y, x);
47}
48
51{
52 VALUE invcmp = rb_exec_recursive(invcmp_recursive, x, y);
53 if (invcmp == Qundef || NIL_P(invcmp)) {
54 return Qnil;
55 }
56 else {
57 int result = -rb_cmpint(invcmp, x, y);
58 return INT2FIX(result);
59 }
60}
61
62static VALUE
63cmp_eq_recursive(VALUE arg1, VALUE arg2, int recursive)
64{
65 if (recursive) return Qnil;
66 return rb_cmp(arg1, arg2);
67}
68
69/*
70 * call-seq:
71 * obj == other -> true or false
72 *
73 * Compares two objects based on the receiver's <code><=></code>
74 * method, returning true if it returns 0. Also returns true if
75 * _obj_ and _other_ are the same object.
76 */
77
78static VALUE
79cmp_equal(VALUE x, VALUE y)
80{
81 VALUE c;
82 if (x == y) return Qtrue;
83
84 c = rb_exec_recursive_paired_outer(cmp_eq_recursive, x, y, y);
85
86 if (NIL_P(c)) return Qfalse;
87 if (rb_cmpint(c, x, y) == 0) return Qtrue;
88 return Qfalse;
89}
90
91static int
92cmpint(VALUE x, VALUE y)
93{
94 return rb_cmpint(rb_cmp(x, y), x, y);
95}
96
97/*
98 * call-seq:
99 * obj > other -> true or false
100 *
101 * Compares two objects based on the receiver's <code><=></code>
102 * method, returning true if it returns a value greater than 0.
103 */
104
105static VALUE
106cmp_gt(VALUE x, VALUE y)
107{
108 if (cmpint(x, y) > 0) return Qtrue;
109 return Qfalse;
110}
111
112/*
113 * call-seq:
114 * obj >= other -> true or false
115 *
116 * Compares two objects based on the receiver's <code><=></code>
117 * method, returning true if it returns a value greater than or equal to 0.
118 */
119
120static VALUE
121cmp_ge(VALUE x, VALUE y)
122{
123 if (cmpint(x, y) >= 0) return Qtrue;
124 return Qfalse;
125}
126
127/*
128 * call-seq:
129 * obj < other -> true or false
130 *
131 * Compares two objects based on the receiver's <code><=></code>
132 * method, returning true if it returns a value less than 0.
133 */
134
135static VALUE
136cmp_lt(VALUE x, VALUE y)
137{
138 if (cmpint(x, y) < 0) return Qtrue;
139 return Qfalse;
140}
141
142/*
143 * call-seq:
144 * obj <= other -> true or false
145 *
146 * Compares two objects based on the receiver's <code><=></code>
147 * method, returning true if it returns a value less than or equal to 0.
148 */
149
150static VALUE
151cmp_le(VALUE x, VALUE y)
152{
153 if (cmpint(x, y) <= 0) return Qtrue;
154 return Qfalse;
155}
156
157/*
158 * call-seq:
159 * obj.between?(min, max) -> true or false
160 *
161 * Returns <code>false</code> if _obj_ <code><=></code> _min_ is less
162 * than zero or if _obj_ <code><=></code> _max_ is greater than zero,
163 * <code>true</code> otherwise.
164 *
165 * 3.between?(1, 5) #=> true
166 * 6.between?(1, 5) #=> false
167 * 'cat'.between?('ant', 'dog') #=> true
168 * 'gnu'.between?('ant', 'dog') #=> false
169 *
170 */
171
172static VALUE
173cmp_between(VALUE x, VALUE min, VALUE max)
174{
175 if (cmpint(x, min) < 0) return Qfalse;
176 if (cmpint(x, max) > 0) return Qfalse;
177 return Qtrue;
178}
179
180/*
181 * call-seq:
182 * obj.clamp(min, max) -> obj
183 * obj.clamp(range) -> obj
184 *
185 * In <code>(min, max)</code> form, returns _min_ if _obj_
186 * <code><=></code> _min_ is less than zero, _max_ if _obj_
187 * <code><=></code> _max_ is greater than zero, and _obj_
188 * otherwise.
189 *
190 * 12.clamp(0, 100) #=> 12
191 * 523.clamp(0, 100) #=> 100
192 * -3.123.clamp(0, 100) #=> 0
193 *
194 * 'd'.clamp('a', 'f') #=> 'd'
195 * 'z'.clamp('a', 'f') #=> 'f'
196 *
197 * In <code>(range)</code> form, returns _range.begin_ if _obj_
198 * <code><=></code> _range.begin_ is less than zero, _range.end_
199 * if _obj_ <code><=></code> _range.end_ is greater than zero, and
200 * _obj_ otherwise.
201 *
202 * 12.clamp(0..100) #=> 12
203 * 523.clamp(0..100) #=> 100
204 * -3.123.clamp(0..100) #=> 0
205 *
206 * 'd'.clamp('a'..'f') #=> 'd'
207 * 'z'.clamp('a'..'f') #=> 'f'
208 *
209 * If _range.begin_ is +nil+, it is considered smaller than _obj_,
210 * and if _range.end_ is +nil+, it is considered greater than
211 * _obj_.
212 *
213 * -20.clamp(0..) #=> 0
214 * 523.clamp(..100) #=> 100
215 *
216 * When _range.end_ is excluded and not +nil+, an exception is
217 * raised.
218 *
219 * 100.clamp(0...100) # ArgumentError
220 */
221
222static VALUE
223cmp_clamp(int argc, VALUE *argv, VALUE x)
224{
225 VALUE min, max;
226 int c, excl = 0;
227
228 if (rb_scan_args(argc, argv, "11", &min, &max) == 1) {
229 VALUE range = min;
230 if (!rb_range_values(range, &min, &max, &excl)) {
231 rb_raise(rb_eTypeError, "wrong argument type %s (expected Range)",
233 }
234 if (!NIL_P(max)) {
235 if (excl) rb_raise(rb_eArgError, "cannot clamp with an exclusive range");
236 }
237 }
238 if (!NIL_P(min) && !NIL_P(max) && cmpint(min, max) > 0) {
239 rb_raise(rb_eArgError, "min argument must be smaller than max argument");
240 }
241
242 if (!NIL_P(min)) {
243 c = cmpint(x, min);
244 if (c == 0) return x;
245 if (c < 0) return min;
246 }
247 if (!NIL_P(max)) {
248 c = cmpint(x, max);
249 if (c > 0) return max;
250 }
251 return x;
252}
253
254/*
255 * The Comparable mixin is used by classes whose objects may be
256 * ordered. The class must define the <code><=></code> operator,
257 * which compares the receiver against another object, returning a
258 * value less than 0, returning 0, or returning a value greater than 0,
259 * depending on whether the receiver is less than, equal to,
260 * or greater than the other object. If the other object is not
261 * comparable then the <code><=></code> operator should return +nil+.
262 * Comparable uses <code><=></code> to implement the conventional
263 * comparison operators (<code><</code>, <code><=</code>,
264 * <code>==</code>, <code>>=</code>, and <code>></code>) and the
265 * method <code>between?</code>.
266 *
267 * class SizeMatters
268 * include Comparable
269 * attr :str
270 * def <=>(other)
271 * str.size <=> other.str.size
272 * end
273 * def initialize(str)
274 * @str = str
275 * end
276 * def inspect
277 * @str
278 * end
279 * end
280 *
281 * s1 = SizeMatters.new("Z")
282 * s2 = SizeMatters.new("YY")
283 * s3 = SizeMatters.new("XXX")
284 * s4 = SizeMatters.new("WWWW")
285 * s5 = SizeMatters.new("VVVVV")
286 *
287 * s1 < s2 #=> true
288 * s4.between?(s1, s3) #=> false
289 * s4.between?(s3, s5) #=> true
290 * [ s3, s2, s5, s4, s1 ].sort #=> [Z, YY, XXX, WWWW, VVVVV]
291 *
292 */
293
294void
296{
297 rb_mComparable = rb_define_module("Comparable");
298 rb_define_method(rb_mComparable, "==", cmp_equal, 1);
299 rb_define_method(rb_mComparable, ">", cmp_gt, 1);
300 rb_define_method(rb_mComparable, ">=", cmp_ge, 1);
301 rb_define_method(rb_mComparable, "<", cmp_lt, 1);
302 rb_define_method(rb_mComparable, "<=", cmp_le, 1);
303 rb_define_method(rb_mComparable, "between?", cmp_between, 2);
304 rb_define_method(rb_mComparable, "clamp", cmp_clamp, -1);
305}
VALUE rb_invcmp(VALUE x, VALUE y)
Definition: compar.c:50
void Init_Comparable(void)
Definition: compar.c:295
VALUE rb_mComparable
Definition: compar.c:19
void rb_cmperr(VALUE x, VALUE y)
Definition: compar.c:28
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition: cxxanyargs.hpp:653
#define range(low, item, hi)
Definition: date_strftime.c:21
int max
Definition: enough.c:225
#define rb_cmpint(cmp, a, b)
#define PRIsVALUE
Definition: function.c:10
VALUE rb_define_module(const char *name)
Definition: class.c:871
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:2296
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2917
VALUE rb_eTypeError
Definition: error.c:1057
const char * rb_builtin_class_name(VALUE x)
Definition: error.c:937
VALUE rb_eArgError
Definition: error.c:1058
VALUE rb_obj_class(VALUE)
Definition: object.c:245
VALUE rb_inspect(VALUE)
Convenient wrapper of Object::inspect.
Definition: object.c:585
@ idCmp
Definition: id.h:84
int rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
Definition: range.c:1310
VALUE rb_exec_recursive_paired_outer(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE, VALUE)
Definition: thread.c:5395
VALUE rb_exec_recursive(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE)
Definition: thread.c:5360
Internal header for Comparable.
Internal header for RubyVM.
#define rb_funcallv(...)
Definition: internal.h:77
#define INT2FIX
Definition: long.h:48
int argc
Definition: ruby.c:240
char ** argv
Definition: ruby.c:241
#define Qundef
#define SPECIAL_CONST_P
#define Qtrue
#define Qnil
#define Qfalse
#define NIL_P
unsigned long VALUE
Definition: value.h:38
#define T_FLOAT
Definition: value_type.h:63
#define BUILTIN_TYPE
Definition: value_type.h:84