summaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2021-03-20 23:41:41 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2021-03-20 23:42:15 +0900
commit0b6554e65b902a977012150ba3ae2b170a3c061e (patch)
treec7b8158c07d83b705f262522695802ba50990b06 /hash.c
parent71ba09632ba81c91ce22ca900cf01da2d9e0d53f (diff)
Refactor hash update callbacks
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c33
1 files changed, 13 insertions, 20 deletions
diff --git a/hash.c b/hash.c
index 8f51f467c2..378afa736a 100644
--- a/hash.c
+++ b/hash.c
@@ -3870,20 +3870,25 @@ rb_hash_invert(VALUE hash)
}
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)
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
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)