Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 150 lines (121 sloc) 2.844 kb
817a62d 2000-05-01
matz authored
1 /**********************************************************************
99d0354 Initial revision
matz authored
2
3 compar.c -
4
5 $Author$
6 $Date$
7 created at: Thu Aug 26 14:39:48 JST 1993
8
42bc5d2 Updated Copyrights of Matz to 2003.
michal authored
9 Copyright (C) 1993-2003 Yukihiro Matsumoto
99d0354 Initial revision
matz authored
10
817a62d 2000-05-01
matz authored
11 **********************************************************************/
99d0354 Initial revision
matz authored
12
13 #include "ruby.h"
14
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
15 VALUE rb_mComparable;
99d0354 Initial revision
matz authored
16
17 static ID cmp;
18
33a031b * file.c (rb_find_file_ext): should not terminate searching with
matz authored
19 int
293fdb4 * ext/pty/pty.c (pty_finalize_syswait): join (using Thread#value)
matz authored
20 rb_cmpint(val, a, b)
21 VALUE val, a, b;
33a031b * file.c (rb_find_file_ext): should not terminate searching with
matz authored
22 {
293fdb4 * ext/pty/pty.c (pty_finalize_syswait): join (using Thread#value)
matz authored
23 if (NIL_P(val)) {
24 rb_cmperr(a, b);
25 }
33a031b * file.c (rb_find_file_ext): should not terminate searching with
matz authored
26 if (FIXNUM_P(val)) return FIX2INT(val);
27 if (TYPE(val) == T_BIGNUM) {
28 if (RBIGNUM(val)->sign) return 1;
29 return -1;
30 }
31 if (RTEST(rb_funcall(val, '>', 1, INT2FIX(0)))) return 1;
32 if (RTEST(rb_funcall(val, '<', 1, INT2FIX(0)))) return -1;
33 return 0;
34 }
35
a6810a3 @nobu * compar.c (rb_cmperr): raise comparison failure.
nobu authored
36 void
37 rb_cmperr(x, y)
38 VALUE x, y;
f1a5c1c * compar.c (cmp_gt): raises ArgumentError when "<=>" give nil.
matz authored
39 {
a6810a3 @nobu * compar.c (rb_cmperr): raise comparison failure.
nobu authored
40 const char *classname;
41
42 if (SPECIAL_CONST_P(y)) {
43 y = rb_inspect(y);
44 classname = StringValuePtr(y);
45 }
46 else {
47 classname = rb_obj_classname(y);
48 }
293fdb4 * ext/pty/pty.c (pty_finalize_syswait): join (using Thread#value)
matz authored
49 rb_raise(rb_eArgError, "comparison of %s with %s failed",
a6810a3 @nobu * compar.c (rb_cmperr): raise comparison failure.
nobu authored
50 rb_obj_classname(x), classname);
f1a5c1c * compar.c (cmp_gt): raises ArgumentError when "<=>" give nil.
matz authored
51 }
52
a6810a3 @nobu * compar.c (rb_cmperr): raise comparison failure.
nobu authored
53 #define cmperr() (rb_cmperr(x, y), Qnil)
54
f1a5c1c * compar.c (cmp_gt): raises ArgumentError when "<=>" give nil.
matz authored
55 static VALUE
c6da742 031031
matz authored
56 cmp_eq(a)
57 VALUE *a;
58 {
59 VALUE c = rb_funcall(a[0], cmp, 1, a[1]);
60
61 if (NIL_P(c)) return Qnil;
62 if (rb_cmpint(c, a[0], a[1]) == 0) return Qtrue;
63 return Qfalse;
64 }
65
66 static VALUE
67 cmp_failed()
68 {
69 return Qnil;
70 }
71
72 static VALUE
21d0b07 1.4.1
matz authored
73 cmp_equal(x, y)
74 VALUE x, y;
75 {
c6da742 031031
matz authored
76 VALUE a[2];
21d0b07 1.4.1
matz authored
77
33a031b * file.c (rb_find_file_ext): should not terminate searching with
matz authored
78 if (x == y) return Qtrue;
f1a5c1c * compar.c (cmp_gt): raises ArgumentError when "<=>" give nil.
matz authored
79
c6da742 031031
matz authored
80 a[0] = x; a[1] = y;
81 return rb_rescue(cmp_eq, (VALUE)a, cmp_failed, 0);
21d0b07 1.4.1
matz authored
82 }
83
84 static VALUE
99d0354 Initial revision
matz authored
85 cmp_gt(x, y)
86 VALUE x, y;
87 {
88 VALUE c = rb_funcall(x, cmp, 1, y);
89
f1a5c1c * compar.c (cmp_gt): raises ArgumentError when "<=>" give nil.
matz authored
90 if (NIL_P(c)) return cmperr();
293fdb4 * ext/pty/pty.c (pty_finalize_syswait): join (using Thread#value)
matz authored
91 if (rb_cmpint(c, x, y) > 0) return Qtrue;
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
92 return Qfalse;
99d0354 Initial revision
matz authored
93 }
94
95 static VALUE
96 cmp_ge(x, y)
97 VALUE x, y;
98 {
99 VALUE c = rb_funcall(x, cmp, 1, y);
100
f1a5c1c * compar.c (cmp_gt): raises ArgumentError when "<=>" give nil.
matz authored
101 if (NIL_P(c)) return cmperr();
293fdb4 * ext/pty/pty.c (pty_finalize_syswait): join (using Thread#value)
matz authored
102 if (rb_cmpint(c, x, y) >= 0) return Qtrue;
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
103 return Qfalse;
99d0354 Initial revision
matz authored
104 }
105
106 static VALUE
107 cmp_lt(x, y)
108 VALUE x, y;
109 {
110 VALUE c = rb_funcall(x, cmp, 1, y);
111
f1a5c1c * compar.c (cmp_gt): raises ArgumentError when "<=>" give nil.
matz authored
112 if (NIL_P(c)) return cmperr();
293fdb4 * ext/pty/pty.c (pty_finalize_syswait): join (using Thread#value)
matz authored
113 if (rb_cmpint(c, x, y) < 0) return Qtrue;
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
114 return Qfalse;
99d0354 Initial revision
matz authored
115 }
116
117 static VALUE
118 cmp_le(x, y)
119 VALUE x, y;
120 {
121 VALUE c = rb_funcall(x, cmp, 1, y);
122
f1a5c1c * compar.c (cmp_gt): raises ArgumentError when "<=>" give nil.
matz authored
123 if (NIL_P(c)) return cmperr();
293fdb4 * ext/pty/pty.c (pty_finalize_syswait): join (using Thread#value)
matz authored
124 if (rb_cmpint(c, x, y) <= 0) return Qtrue;
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
125 return Qfalse;
99d0354 Initial revision
matz authored
126 }
127
128 static VALUE
129 cmp_between(x, min, max)
130 VALUE x, min, max;
131 {
909cfc6 * string.c (rb_str_cmp_m): return nil if str2 does not respond to
matz authored
132 if (RTEST(cmp_lt(x, min))) return Qfalse;
133 if (RTEST(cmp_gt(x, max))) return Qfalse;
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
134 return Qtrue;
99d0354 Initial revision
matz authored
135 }
136
137 void
138 Init_Comparable()
139 {
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
140 rb_mComparable = rb_define_module("Comparable");
21d0b07 1.4.1
matz authored
141 rb_define_method(rb_mComparable, "==", cmp_equal, 1);
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
142 rb_define_method(rb_mComparable, ">", cmp_gt, 1);
143 rb_define_method(rb_mComparable, ">=", cmp_ge, 1);
144 rb_define_method(rb_mComparable, "<", cmp_lt, 1);
145 rb_define_method(rb_mComparable, "<=", cmp_le, 1);
146 rb_define_method(rb_mComparable, "between?", cmp_between, 2);
99d0354 Initial revision
matz authored
147
148 cmp = rb_intern("<=>");
149 }
Something went wrong with that request. Please try again.