Skip to content

Commit

Permalink
Stir the hash value more with encoding index
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Dec 16, 2023
1 parent 2f595c7 commit ab7f546
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 4 additions & 3 deletions string.c
Expand Up @@ -3586,11 +3586,12 @@ rb_str_prepend_multi(int argc, VALUE *argv, VALUE str)
st_index_t
rb_str_hash(VALUE str)
{
st_index_t h = rb_memhash((const void *)RSTRING_PTR(str), RSTRING_LEN(str));
int e = RSTRING_LEN(str) ? ENCODING_GET(str) : 0;
if (e && is_ascii_string(str)) {
e = 0;
if (e && !is_ascii_string(str)) {
h = rb_hash_end(rb_hash_uint32(h, (uint32_t)e));
}
return rb_memhash((const void *)RSTRING_PTR(str), RSTRING_LEN(str)) ^ e;
return h;
}

int
Expand Down
2 changes: 2 additions & 0 deletions test/ruby/test_string.rb
Expand Up @@ -1345,6 +1345,8 @@ def test_hash
bug9172 = '[ruby-core:58658] [Bug #9172]'
assert_not_equal(S("sub-setter").hash, S("discover").hash, bug9172)
assert_equal(S("").hash, S("".encode(Encoding::UTF_32BE)).hash)
h1, h2 = ["\x80", "\x81"].map {|c| c.b.hash ^ c.hash}
assert_not_equal(h1, h2)
end

def test_hex
Expand Down

0 comments on commit ab7f546

Please sign in to comment.