@@ -781,7 +781,7 @@ ar_add_direct_with_hash(VALUE hash, st_data_t key, st_data_t val, st_hash_t hash
781
781
}
782
782
783
783
static int
784
- ar_foreach (VALUE hash , int (* func )(ANYARGS ), st_data_t arg )
784
+ ar_general_foreach (VALUE hash , int (* func )(ANYARGS ), st_update_callback_func * replace , st_data_t arg )
785
785
{
786
786
if (RHASH_AR_TABLE_SIZE (hash ) > 0 ) {
787
787
unsigned i , bound = RHASH_AR_TABLE_BOUND (hash );
@@ -797,9 +797,22 @@ ar_foreach(VALUE hash, int (*func)(ANYARGS), st_data_t arg)
797
797
case ST_CONTINUE :
798
798
break ;
799
799
case ST_CHECK :
800
- case ST_REPLACE :
801
800
case ST_STOP :
802
801
return 0 ;
802
+ case ST_REPLACE :
803
+ if (replace ) {
804
+ VALUE key ;
805
+ VALUE value ;
806
+
807
+ key = cur_entry -> key ;
808
+ value = cur_entry -> record ;
809
+ retval = (* replace )(& key , & value , arg , TRUE);
810
+
811
+ ar_table_entry * entry = RHASH_AR_TABLE_REF (hash , i );
812
+ entry -> key = key ;
813
+ entry -> record = value ;
814
+ }
815
+ break ;
803
816
case ST_DELETE :
804
817
ar_clear_entry (RHASH_AR_TABLE_REF (hash , i ));
805
818
RHASH_AR_TABLE_SIZE_DEC (hash );
@@ -810,6 +823,18 @@ ar_foreach(VALUE hash, int (*func)(ANYARGS), st_data_t arg)
810
823
return 0 ;
811
824
}
812
825
826
+ static int
827
+ ar_foreach_with_replace (VALUE hash , int (* func )(ANYARGS ), st_update_callback_func * replace , st_data_t arg )
828
+ {
829
+ return ar_general_foreach (hash , func , replace , arg );
830
+ }
831
+
832
+ static int
833
+ ar_foreach (VALUE hash , int (* func )(ANYARGS ), st_data_t arg )
834
+ {
835
+ return ar_general_foreach (hash , func , NULL , arg );
836
+ }
837
+
813
838
static int
814
839
ar_foreach_check (VALUE hash , int (* func )(ANYARGS ), st_data_t arg ,
815
840
st_data_t never )
@@ -1259,6 +1284,17 @@ rb_hash_stlike_foreach(VALUE hash, int (*func)(ANYARGS), st_data_t arg)
1259
1284
}
1260
1285
}
1261
1286
1287
+ int
1288
+ rb_hash_stlike_foreach_with_replace (VALUE hash , int (* func )(ANYARGS ), st_update_callback_func * replace , st_data_t arg )
1289
+ {
1290
+ if (RHASH_AR_TABLE_P (hash )) {
1291
+ return ar_foreach_with_replace (hash , func , replace , arg );
1292
+ }
1293
+ else {
1294
+ return st_foreach_with_replace (RHASH_ST_TABLE (hash ), func , replace , arg );
1295
+ }
1296
+ }
1297
+
1262
1298
static VALUE
1263
1299
hash_foreach_call (VALUE arg )
1264
1300
{
0 commit comments