ruby-changes:65524
From: Nobuyoshi <ko1@a...>
Date: Sat, 20 Mar 2021 23:59:21 +0900 (JST)
Subject: [ruby-changes:65524] 0b6554e65b (master): Refactor hash update callbacks
https://git.ruby-lang.org/ruby.git/commit/?id=0b6554e65b From 0b6554e65b902a977012150ba3ae2b170a3c061e Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Sat, 20 Mar 2021 23:41:41 +0900 Subject: Refactor hash update callbacks --- hash.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/hash.c b/hash.c index 8f51f46..378afa7 100644 --- a/hash.c +++ b/hash.c @@ -3870,20 +3870,25 @@ rb_hash_invert(VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L3870 } static int -rb_hash_update_callback(st_data_t *key, st_data_t *value, struct update_arg *arg, int existing) +hash_update_replace(st_data_t *key, st_data_t *value, struct update_arg *arg, int existing, st_data_t newvalue) { if (existing) { arg->old_value = *value; - arg->new_value = arg->arg; } else { arg->new_key = *key; - arg->new_value = arg->arg; } - *value = arg->arg; + arg->new_value = newvalue; + *value = newvalue; return ST_CONTINUE; } +static int +rb_hash_update_callback(st_data_t *key, st_data_t *value, struct update_arg *arg, int existing) +{ + return hash_update_replace(key, value, arg, existing, arg->arg); +} + NOINSERT_UPDATE_CALLBACK(rb_hash_update_callback) static int @@ -3896,18 +3901,12 @@ rb_hash_update_i(VALUE key, VALUE value, VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L3901 static int rb_hash_update_block_callback(st_data_t *key, st_data_t *value, struct update_arg *arg, int existing) { - VALUE newvalue = (VALUE)arg->arg; + st_data_t newvalue = arg->arg; if (existing) { - newvalue = rb_yield_values(3, (VALUE)*key, (VALUE)*value, newvalue); - arg->old_value = *value; - } - else { - arg->new_key = *key; + newvalue = (st_data_t)rb_yield_values(3, (VALUE)*key, (VALUE)*value, (VALUE)newvalue); } - arg->new_value = newvalue; - *value = newvalue; - return ST_CONTINUE; + return hash_update_replace(key, value, arg, existing, newvalue); } NOINSERT_UPDATE_CALLBACK(rb_hash_update_block_callback) @@ -4002,14 +4001,8 @@ rb_hash_update_func_callback(st_data_t *key, st_data_t *value, struct update_arg https://github.com/ruby/ruby/blob/trunk/hash.c#L4001 if (existing) { newvalue = (*uf_arg->func)((VALUE)*key, (VALUE)*value, newvalue); - arg->old_value = *value; } - else { - arg->new_key = *key; - } - arg->new_value = newvalue; - *value = newvalue; - return ST_CONTINUE; + return hash_update_replace(key, value, arg, existing, (st_data_t)newvalue); } NOINSERT_UPDATE_CALLBACK(rb_hash_update_func_callback) -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/