From: Nobuyoshi Nakada Date: 2008-03-02T00:29:10+09:00 Subject: Re: Ruby String hash key overflow when converting to Fixnum. Hi, At Sat, 1 Mar 2008 23:50:37 +0900, Chiyuan Zhang wrote in [ruby-core:15713]: > Another more efficient way is to perform truncation when calculating > String hash to make the result meet what we geet in Ruby world. Here's > the patch: Then, shouldn't all results be in the Fixnum range? Index: hash.c =================================================================== --- hash.c (revision 15666) +++ hash.c (working copy) @@ -76,13 +76,14 @@ rb_any_hash(VALUE a) { VALUE hval; + int hnum; switch (TYPE(a)) { case T_FIXNUM: case T_SYMBOL: - return (int)a; + hnum = (int)a; break; case T_STRING: - return rb_str_hash(a); + hnum = rb_str_hash(a); break; @@ -92,6 +93,8 @@ rb_any_hash(VALUE a) hval = rb_funcall(hval, '%', 1, INT2FIX(536870923)); } - return (int)FIX2LONG(hval); + hnum = (int)FIX2LONG(hval); } + hnum <<= 1; + return RSHIFT(hnum, 1); } -- Nobu Nakada