mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
st.c: update the key too if changed
* st.c (st_update): fix a bug that the key was not updated even if it was changed by the callback function. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46720 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
9f051c80dc
commit
e6be6ebc77
1 changed files with 18 additions and 0 deletions
18
st.c
18
st.c
|
@ -834,6 +834,7 @@ st_update(st_table *table, st_data_t key, st_update_callback_func *func, st_data
|
||||||
existing = 1;
|
existing = 1;
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
|
const st_data_t old_key = key;
|
||||||
retval = (*func)(&key, &value, arg, existing);
|
retval = (*func)(&key, &value, arg, existing);
|
||||||
if (!table->entries_packed) {
|
if (!table->entries_packed) {
|
||||||
FIND_ENTRY(table, ptr, hash_val, bin_pos);
|
FIND_ENTRY(table, ptr, hash_val, bin_pos);
|
||||||
|
@ -845,6 +846,14 @@ st_update(st_table *table, st_data_t key, st_update_callback_func *func, st_data
|
||||||
add_packed_direct(table, key, value, hash_val);
|
add_packed_direct(table, key, value, hash_val);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (old_key != PKEY(table, i)) return -1;
|
||||||
|
if (old_key != key) {
|
||||||
|
if (do_hash(key, table) != hash_val &&
|
||||||
|
!EQUAL(table, key, old_key)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
PKEY(table, i) = key;
|
||||||
|
}
|
||||||
PVAL_SET(table, i, value);
|
PVAL_SET(table, i, value);
|
||||||
break;
|
break;
|
||||||
case ST_DELETE:
|
case ST_DELETE:
|
||||||
|
@ -863,6 +872,7 @@ st_update(st_table *table, st_data_t key, st_update_callback_func *func, st_data
|
||||||
existing = 1;
|
existing = 1;
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
|
const st_data_t old_key = key;
|
||||||
retval = (*func)(&key, &value, arg, existing);
|
retval = (*func)(&key, &value, arg, existing);
|
||||||
unpacked:
|
unpacked:
|
||||||
switch (retval) {
|
switch (retval) {
|
||||||
|
@ -871,6 +881,14 @@ st_update(st_table *table, st_data_t key, st_update_callback_func *func, st_data
|
||||||
add_direct(table, key, value, hash_val, hash_pos(hash_val, table->num_bins));
|
add_direct(table, key, value, hash_val, hash_pos(hash_val, table->num_bins));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (old_key != ptr->key) return -1;
|
||||||
|
if (old_key != key) {
|
||||||
|
if (do_hash(key, table) != hash_val &&
|
||||||
|
!EQUAL(table, key, old_key)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
ptr->key = key;
|
||||||
|
}
|
||||||
ptr->record = value;
|
ptr->record = value;
|
||||||
break;
|
break;
|
||||||
case ST_DELETE:
|
case ST_DELETE:
|
||||||
|
|
Loading…
Reference in a new issue