[前][次][番号順一覧][スレッド一覧]

ruby-changes:39345

From: nobu <ko1@a...>
Date: Wed, 29 Jul 2015 14:54:33 +0900 (JST)
Subject: [ruby-changes:39345] nobu:r51426 (trunk): hash.c: fix symbol hash

nobu	2015-07-29 14:54:21 +0900 (Wed, 29 Jul 2015)

  New Revision: 51426

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=51426

  Log:
    hash.c: fix symbol hash
    
    * hash.c (rb_sym_hash): return same value as rb_any_hash() of
      Symbol.  [Bug #9381]

  Modified files:
    trunk/ChangeLog
    trunk/hash.c
    trunk/string.c
    trunk/test/ruby/test_hash.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 51425)
+++ ChangeLog	(revision 51426)
@@ -1,4 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
-Wed Jul 29 14:53:32 2015  Nobuyoshi Nakada  <nobu@r...>
+Wed Jul 29 14:54:16 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* hash.c (rb_sym_hash): return same value as rb_any_hash() of
+	  Symbol.  [Bug #9381]
 
 	* hash.c (rb_any_hash): fix Float hash.  rb_dbl_hash() returns a
 	  Fixnum, but not a long.  [Bug #9381]
Index: string.c
===================================================================
--- string.c	(revision 51425)
+++ string.c	(revision 51426)
@@ -9149,6 +9149,8 @@ rb_to_symbol(VALUE name) https://github.com/ruby/ruby/blob/trunk/string.c#L9149
     return rb_str_intern(name);
 }
 
+VALUE rb_sym_hash(VALUE);
+
 /*
  *  A <code>String</code> object holds and manipulates an arbitrary sequence of
  *  bytes, typically representing characters. String objects may be created
@@ -9311,6 +9313,7 @@ Init_String(void) https://github.com/ruby/ruby/blob/trunk/string.c#L9313
     rb_undef_method(CLASS_OF(rb_cSymbol), "new");
     rb_define_singleton_method(rb_cSymbol, "all_symbols", rb_sym_all_symbols, 0); /* in symbol.c */
 
+    rb_define_method(rb_cSymbol, "hash", rb_sym_hash, 0); /* in hash.c */
     rb_define_method(rb_cSymbol, "==", sym_equal, 1);
     rb_define_method(rb_cSymbol, "===", sym_equal, 1);
     rb_define_method(rb_cSymbol, "inspect", sym_inspect, 0);
Index: hash.c
===================================================================
--- hash.c	(revision 51425)
+++ hash.c	(revision 51426)
@@ -129,6 +129,21 @@ rb_hash(VALUE obj) https://github.com/ruby/ruby/blob/trunk/hash.c#L129
 
 long rb_objid_hash(st_index_t index);
 
+VALUE
+rb_sym_hash(VALUE sym)
+{
+    st_index_t hnum;
+
+    if (STATIC_SYM_P(sym)) {
+	sym >>= (RUBY_SPECIAL_SHIFT + ID_SCOPE_SHIFT);
+	hnum = rb_objid_hash((st_index_t)sym);
+    }
+    else {
+	hnum = RSYMBOL(sym)->hashval;
+    }
+    return LONG2FIX(hnum);
+}
+
 static st_index_t
 rb_any_hash(VALUE a)
 {
Index: test/ruby/test_hash.rb
===================================================================
--- test/ruby/test_hash.rb	(revision 51425)
+++ test/ruby/test_hash.rb	(revision 51426)
@@ -1282,6 +1282,7 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L1282
     bad = [
       5, true, false, nil,
       0.0, 1.72723e-77,
+      :foo, "dsym_#{self.object_id.to_s(16)}_#{Time.now.to_i.to_s(16)}".to_sym,
     ].select do |x|
       hash = {x => bug9381}
       hash[wrapper.new(x)] != bug9381

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]