mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
hash literal deduplicates like Hash#[]=
From: Eric Wong <e@80x24.org> * hash.c (rb_hash_key_str): new function (hash_aset_str): use rb_hash_key_str * internal.h: add rb_hash_key_str * st.c (st_stringify): use rb_hash_key_str * test/ruby/test_hash.rb (test_NEWHASH_fstring_key): dynamic key [ruby-core:84554] [Feature #14258] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61514 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
634a48c5c1
commit
fc939f6697
4 changed files with 21 additions and 11 deletions
24
hash.c
24
hash.c
|
@ -1582,19 +1582,25 @@ fstring_existing_str(VALUE str)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VALUE
|
||||||
|
rb_hash_key_str(VALUE key)
|
||||||
|
{
|
||||||
|
VALUE k;
|
||||||
|
|
||||||
|
if (!RB_OBJ_TAINTED(key) &&
|
||||||
|
(k = fstring_existing_str(key)) != Qnil) {
|
||||||
|
return k;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return rb_str_new_frozen(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
hash_aset_str(st_data_t *key, st_data_t *val, struct update_arg *arg, int existing)
|
hash_aset_str(st_data_t *key, st_data_t *val, struct update_arg *arg, int existing)
|
||||||
{
|
{
|
||||||
if (!existing && !RB_OBJ_FROZEN(*key)) {
|
if (!existing && !RB_OBJ_FROZEN(*key)) {
|
||||||
VALUE k;
|
*key = rb_hash_key_str(*key);
|
||||||
|
|
||||||
if (!RB_OBJ_TAINTED(*key) &&
|
|
||||||
(k = fstring_existing_str(*key)) != Qnil) {
|
|
||||||
*key = k;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
*key = rb_str_new_frozen(*key);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return hash_aset(key, val, arg, existing);
|
return hash_aset(key, val, arg, existing);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1284,6 +1284,7 @@ st_table *rb_init_identtable(void);
|
||||||
st_table *rb_init_identtable_with_size(st_index_t size);
|
st_table *rb_init_identtable_with_size(st_index_t size);
|
||||||
VALUE rb_hash_compare_by_id_p(VALUE hash);
|
VALUE rb_hash_compare_by_id_p(VALUE hash);
|
||||||
VALUE rb_to_hash_type(VALUE obj);
|
VALUE rb_to_hash_type(VALUE obj);
|
||||||
|
VALUE rb_hash_key_str(VALUE);
|
||||||
|
|
||||||
#define RHASH_TBL_RAW(h) rb_hash_tbl_raw(h)
|
#define RHASH_TBL_RAW(h) rb_hash_tbl_raw(h)
|
||||||
VALUE rb_hash_keys(VALUE hash);
|
VALUE rb_hash_keys(VALUE hash);
|
||||||
|
|
4
st.c
4
st.c
|
@ -2114,8 +2114,8 @@ st_rehash(st_table *tab)
|
||||||
static st_data_t
|
static st_data_t
|
||||||
st_stringify(VALUE key)
|
st_stringify(VALUE key)
|
||||||
{
|
{
|
||||||
return (rb_obj_class(key) == rb_cString) ?
|
return (rb_obj_class(key) == rb_cString && !RB_OBJ_FROZEN(key)) ?
|
||||||
rb_str_new_frozen(key) : key;
|
rb_hash_key_str(key) : key;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
|
|
@ -298,6 +298,9 @@ class TestHash < Test::Unit::TestCase
|
||||||
b = {"ABC" => :t}
|
b = {"ABC" => :t}
|
||||||
assert_same a.keys[0], b.keys[0]
|
assert_same a.keys[0], b.keys[0]
|
||||||
assert_same "ABC".freeze, a.keys[0]
|
assert_same "ABC".freeze, a.keys[0]
|
||||||
|
var = +'ABC'
|
||||||
|
c = { var => :t }
|
||||||
|
assert_same "ABC".freeze, c.keys[0]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_tainted_string_key
|
def test_tainted_string_key
|
||||||
|
|
Loading…
Add table
Reference in a new issue