Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 136 lines (110 sloc) 2.691 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
21d0b07 1.4.1
matz authored
56 cmp_equal(x, y)
57 VALUE x, y;
58 {
33a031b * file.c (rb_find_file_ext): should not terminate searching with
matz authored
59 int c;
21d0b07 1.4.1
matz authored
60
33a031b * file.c (rb_find_file_ext): should not terminate searching with
matz authored
61 if (x == y) return Qtrue;
f1a5c1c * compar.c (cmp_gt): raises ArgumentError when "<=>" give nil.
matz authored
62
33a031b * file.c (rb_find_file_ext): should not terminate searching with
matz authored
63 c = rb_funcall(x, cmp, 1, y);
f1a5c1c * compar.c (cmp_gt): raises ArgumentError when "<=>" give nil.
matz authored
64 if (NIL_P(c)) return Qnil;
4112162 * variable.c (rb_copy_generic_ivar): remove old generic instance
matz authored
65 if (c == INT2FIX(0)) return Qtrue;
293fdb4 * ext/pty/pty.c (pty_finalize_syswait): join (using Thread#value)
matz authored
66 if (rb_cmpint(c, x, y) == 0) return Qtrue;
f09bd39 * object.c (Init_Object): should do exact match for Module#==.
matz authored
67 return Qfalse;
21d0b07 1.4.1
matz authored
68 }
69
70 static VALUE
99d0354 Initial revision
matz authored
71 cmp_gt(x, y)
72 VALUE x, y;
73 {
74 VALUE c = rb_funcall(x, cmp, 1, y);
75
f1a5c1c * compar.c (cmp_gt): raises ArgumentError when "<=>" give nil.
matz authored
76 if (NIL_P(c)) return cmperr();
293fdb4 * ext/pty/pty.c (pty_finalize_syswait): join (using Thread#value)
matz authored
77 if (rb_cmpint(c, x, y) > 0) return Qtrue;
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
78 return Qfalse;
99d0354 Initial revision
matz authored
79 }
80
81 static VALUE
82 cmp_ge(x, y)
83 VALUE x, y;
84 {
85 VALUE c = rb_funcall(x, cmp, 1, y);
86
f1a5c1c * compar.c (cmp_gt): raises ArgumentError when "<=>" give nil.
matz authored
87 if (NIL_P(c)) return cmperr();
293fdb4 * ext/pty/pty.c (pty_finalize_syswait): join (using Thread#value)
matz authored
88 if (rb_cmpint(c, x, y) >= 0) return Qtrue;
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
89 return Qfalse;
99d0354 Initial revision
matz authored
90 }
91
92 static VALUE
93 cmp_lt(x, y)
94 VALUE x, y;
95 {
96 VALUE c = rb_funcall(x, cmp, 1, y);
97
f1a5c1c * compar.c (cmp_gt): raises ArgumentError when "<=>" give nil.
matz authored
98 if (NIL_P(c)) return cmperr();
293fdb4 * ext/pty/pty.c (pty_finalize_syswait): join (using Thread#value)
matz authored
99 if (rb_cmpint(c, x, y) < 0) return Qtrue;
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
100 return Qfalse;
99d0354 Initial revision
matz authored
101 }
102
103 static VALUE
104 cmp_le(x, y)
105 VALUE x, y;
106 {
107 VALUE c = rb_funcall(x, cmp, 1, y);
108
f1a5c1c * compar.c (cmp_gt): raises ArgumentError when "<=>" give nil.
matz authored
109 if (NIL_P(c)) return cmperr();
293fdb4 * ext/pty/pty.c (pty_finalize_syswait): join (using Thread#value)
matz authored
110 if (rb_cmpint(c, x, y) <= 0) return Qtrue;
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
111 return Qfalse;
99d0354 Initial revision
matz authored
112 }
113
114 static VALUE
115 cmp_between(x, min, max)
116 VALUE x, min, max;
117 {
909cfc6 * string.c (rb_str_cmp_m): return nil if str2 does not respond to
matz authored
118 if (RTEST(cmp_lt(x, min))) return Qfalse;
119 if (RTEST(cmp_gt(x, max))) return Qfalse;
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
120 return Qtrue;
99d0354 Initial revision
matz authored
121 }
122
123 void
124 Init_Comparable()
125 {
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
126 rb_mComparable = rb_define_module("Comparable");
21d0b07 1.4.1
matz authored
127 rb_define_method(rb_mComparable, "==", cmp_equal, 1);
a2af08d This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
128 rb_define_method(rb_mComparable, ">", cmp_gt, 1);
129 rb_define_method(rb_mComparable, ">=", cmp_ge, 1);
130 rb_define_method(rb_mComparable, "<", cmp_lt, 1);
131 rb_define_method(rb_mComparable, "<=", cmp_le, 1);
132 rb_define_method(rb_mComparable, "between?", cmp_between, 2);
99d0354 Initial revision
matz authored
133
134 cmp = rb_intern("<=>");
135 }
Something went wrong with that request. Please try again.