aboutsummaryrefslogtreecommitdiffstats
path: root/hash.c
diff options
context:
space:
mode:
authornormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-12-30 00:00:23 +0000
committernormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-12-30 00:00:23 +0000
commit15caeb0fffa4e027677893a0c4892e57dcc90e9b (patch)
tree8964cc9540122e2f985d81c8879494ebad0b840d /hash.c
parent70f9d38ab337f5517aeee97e23656bd219c9d140 (diff)
downloadruby-15caeb0fffa4e027677893a0c4892e57dcc90e9b.tar.gz
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
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c24
1 files changed, 15 insertions, 9 deletions
diff --git a/hash.c b/hash.c
index 4452eeed34..b4e86bdfb1 100644
--- a/hash.c
+++ b/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
hash_aset_str(st_data_t *key, st_data_t *val, struct update_arg *arg, int existing)
{
if (!existing && !RB_OBJ_FROZEN(*key)) {
- VALUE k;
-
- if (!RB_OBJ_TAINTED(*key) &&
- (k = fstring_existing_str(*key)) != Qnil) {
- *key = k;
- }
- else {
- *key = rb_str_new_frozen(*key);
- }
+ *key = rb_hash_key_str(*key);
}
return hash_aset(key, val, arg, existing);
}