summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-06-19 14:57:57 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-06-19 14:57:57 (GMT)
commit4f029e31f570d8701c463060b929700fddcf62ec (patch)
treeed5f0bde348075d69f82d96032a04b6d799a8c04
parentde63d714e920f97265f9e4c4d17c363ca55e69c5 (diff)
* array.c (rb_ary_equal, rb_ary_eql, rb_ary_hash, rb_ary_cmp):
Make Array#eql?, #hash, #== and #<=> use rb_exec_recursive() and handle recursive data properly. [ruby-dev:35181] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8_5@17438 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog6
-rw-r--r--array.c99
-rw-r--r--version.h8
3 files changed, 80 insertions, 33 deletions
diff --git a/ChangeLog b/ChangeLog
index 8514205..7e96d3e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Thu Jun 19 23:57:54 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * array.c (rb_ary_equal, rb_ary_eql, rb_ary_hash, rb_ary_cmp):
+ Make Array#eql?, #hash, #== and #<=> use rb_exec_recursive() and
+ handle recursive data properly. [ruby-dev:35181]
+
Wed Jun 18 15:17:30 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* marshal.c (w_object, marshal_dump, r_object0, marshal_load): search
diff --git a/array.c b/array.c
index baae270..bfa6d1f 100644
--- a/array.c
+++ b/array.c
@@ -2454,6 +2454,20 @@ rb_ary_rassoc(ary, value)
return Qnil;
}
+static VALUE
+recursive_equal(ary1, ary2)
+ VALUE ary1, ary2;
+{
+ long i;
+
+ if (rb_inspecting_p(ary1)) return Qfalse;
+ for (i=0; i<RARRAY(ary1)->len; i++) {
+ if (!rb_equal(rb_ary_elt(ary1, i), rb_ary_elt(ary2, i)))
+ return Qfalse;
+ }
+ return Qtrue;
+}
+
/*
* call-seq:
* array == other_array -> bool
@@ -2472,8 +2486,6 @@ static VALUE
rb_ary_equal(ary1, ary2)
VALUE ary1, ary2;
{
- long i;
-
if (ary1 == ary2) return Qtrue;
if (TYPE(ary2) != T_ARRAY) {
if (!rb_respond_to(ary2, rb_intern("to_ary"))) {
@@ -2482,8 +2494,18 @@ rb_ary_equal(ary1, ary2)
return rb_equal(ary2, ary1);
}
if (RARRAY(ary1)->len != RARRAY(ary2)->len) return Qfalse;
+ return rb_protect_inspect(recursive_equal, ary1, ary2);
+}
+
+static VALUE
+recursive_eql(ary1, ary2)
+ VALUE ary1, ary2;
+{
+ long i;
+
+ if (rb_inspecting_p(ary1)) return Qfalse;
for (i=0; i<RARRAY(ary1)->len; i++) {
- if (!rb_equal(rb_ary_elt(ary1, i), rb_ary_elt(ary2, i)))
+ if (!rb_eql(rb_ary_elt(ary1, i), rb_ary_elt(ary2, i)))
return Qfalse;
}
return Qtrue;
@@ -2501,33 +2523,24 @@ static VALUE
rb_ary_eql(ary1, ary2)
VALUE ary1, ary2;
{
- long i;
-
if (ary1 == ary2) return Qtrue;
if (TYPE(ary2) != T_ARRAY) return Qfalse;
if (RARRAY(ary1)->len != RARRAY(ary2)->len) return Qfalse;
- for (i=0; i<RARRAY(ary1)->len; i++) {
- if (!rb_eql(rb_ary_elt(ary1, i), rb_ary_elt(ary2, i)))
- return Qfalse;
- }
- return Qtrue;
+ return rb_protect_inspect(recursive_eql, ary1, ary2);
}
-/*
- * call-seq:
- * array.hash -> fixnum
- *
- * Compute a hash-code for this array. Two arrays with the same content
- * will have the same hash code (and will compare using <code>eql?</code>).
- */
+static VALUE recursive_hash _((VALUE ary));
static VALUE
-rb_ary_hash(ary)
+recursive_hash(ary)
VALUE ary;
{
long i, h;
VALUE n;
+ if (rb_inspecting_p(ary)) {
+ return LONG2FIX(0);
+ }
h = RARRAY(ary)->len;
for (i=0; i<RARRAY(ary)->len; i++) {
h = (h << 1) | (h<0 ? 1 : 0);
@@ -2539,6 +2552,21 @@ rb_ary_hash(ary)
/*
* call-seq:
+ * array.hash -> fixnum
+ *
+ * Compute a hash-code for this array. Two arrays with the same content
+ * will have the same hash code (and will compare using <code>eql?</code>).
+ */
+
+static VALUE
+rb_ary_hash(ary)
+ VALUE ary;
+{
+ return rb_protect_inspect(recursive_hash, ary, 0);
+}
+
+/*
+ * call-seq:
* array.include?(obj) -> true or false
*
* Returns <code>true</code> if the given object is present in
@@ -2565,6 +2593,25 @@ rb_ary_includes(ary, item)
return Qfalse;
}
+VALUE
+recursive_cmp(ary1, ary2)
+ VALUE ary1, ary2;
+{
+ long i, len;
+
+ if (rb_inspecting_p(ary1)) return Qfalse;
+ len = RARRAY(ary1)->len;
+ if (len > RARRAY(ary2)->len) {
+ len = RARRAY(ary2)->len;
+ }
+ for (i=0; i<len; i++) {
+ VALUE v = rb_funcall(rb_ary_elt(ary1, i), id_cmp, 1, rb_ary_elt(ary2, i));
+ if (v != INT2FIX(0)) {
+ return v;
+ }
+ }
+ return Qundef;
+}
/*
* call-seq:
@@ -2590,19 +2637,13 @@ VALUE
rb_ary_cmp(ary1, ary2)
VALUE ary1, ary2;
{
- long i, len;
+ long len;
+ VALUE v;
ary2 = to_ary(ary2);
- len = RARRAY(ary1)->len;
- if (len > RARRAY(ary2)->len) {
- len = RARRAY(ary2)->len;
- }
- for (i=0; i<len; i++) {
- VALUE v = rb_funcall(rb_ary_elt(ary1, i), id_cmp, 1, rb_ary_elt(ary2, i));
- if (v != INT2FIX(0)) {
- return v;
- }
- }
+ if (ary1 == ary2) return INT2FIX(0);
+ v = rb_protect_inspect(recursive_cmp, ary1, ary2);
+ if (v != Qundef) return v;
len = RARRAY(ary1)->len - RARRAY(ary2)->len;
if (len == 0) return INT2FIX(0);
if (len > 0) return INT2FIX(1);
diff --git a/version.h b/version.h
index ac4f71d..3c8eb48 100644
--- a/version.h
+++ b/version.h
@@ -1,15 +1,15 @@
#define RUBY_VERSION "1.8.5"
-#define RUBY_RELEASE_DATE "2008-06-18"
+#define RUBY_RELEASE_DATE "2008-06-19"
#define RUBY_VERSION_CODE 185
-#define RUBY_RELEASE_CODE 20080618
-#define RUBY_PATCHLEVEL 226
+#define RUBY_RELEASE_CODE 20080619
+#define RUBY_PATCHLEVEL 227
#define RUBY_VERSION_MAJOR 1
#define RUBY_VERSION_MINOR 8
#define RUBY_VERSION_TEENY 5
#define RUBY_RELEASE_YEAR 2008
#define RUBY_RELEASE_MONTH 6
-#define RUBY_RELEASE_DAY 18
+#define RUBY_RELEASE_DAY 19
#ifdef RUBY_EXTERN
RUBY_EXTERN const char ruby_version[];