Subject: [PATCH 9/9] string.c, symbol.c: rb_str_weak_hash for symbols table and frozen strings

From: Sokolov Yura aka funny_falcon <[email-masked]>
Date: Thu, 3 Nov 2016 00:57:54 +0300

---
 internal.h |  1 +
 string.c   | 11 +++++++++++
 symbol.c   |  1 +
 3 files changed, 13 insertions(+)

diff --git a/internal.h b/internal.h
index f3d90da..bc46250 100644
--- a/internal.h
+++ b/internal.h
@@ -1416,6 +1416,7 @@ VALUE rb_enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl);
 #define is_ascii_string(str) (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT)
 #define is_broken_string(str) (rb_enc_str_coderange(str) == ENC_CODERANGE_BROKEN)
 size_t rb_str_memsize(VALUE);
+st_index_t rb_str_hash_weak(VALUE);
 VALUE rb_sym_proc_call(ID mid, int argc, const VALUE *argv, VALUE passed_proc);
 VALUE rb_sym_to_proc(VALUE sym);
 
diff --git a/string.c b/string.c
index 68cc400..fca41fc 100644
--- a/string.c
+++ b/string.c
@@ -245,6 +245,7 @@ static VALUE register_fstring(VALUE str);
 
 const struct st_hash_type rb_fstring_hash_type = {
     fstring_cmp,
+    rb_str_hash_weak,
     rb_str_hash,
 };
 
@@ -2938,6 +2939,16 @@ rb_str_hash(VALUE str)
     return rb_memhash((const void *)RSTRING_PTR(str), RSTRING_LEN(str)) ^ e;
 }
 
+st_index_t
+rb_str_hash_weak(VALUE str)
+{
+    int e = ENCODING_GET(str);
+    if (e && rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
+	e = 0;
+    }
+    return st_hash((const void *)RSTRING_PTR(str), RSTRING_LEN(str), e);
+}
+
 int
 rb_str_hash_cmp(VALUE str1, VALUE str2)
 {
diff --git a/symbol.c b/symbol.c
index facdb3c..613f7e7 100644
--- a/symbol.c
+++ b/symbol.c
@@ -68,6 +68,7 @@ static struct symbols {
 
 static const struct st_hash_type symhash = {
     rb_str_hash_cmp,
+    rb_str_hash_weak,
     rb_str_hash,
 };
 
-- 
2.7.4