14#include "ruby/internal/config.h"
19# ifdef HAVE_CRT_EXTERNS_H
20# include <crt_externs.h>
22# include "missing/crt_externs.h"
26#include "debug_counter.h"
29#include "internal/array.h"
30#include "internal/bignum.h"
31#include "internal/basic_operators.h"
32#include "internal/class.h"
33#include "internal/cont.h"
34#include "internal/error.h"
35#include "internal/hash.h"
36#include "internal/object.h"
37#include "internal/proc.h"
38#include "internal/symbol.h"
39#include "internal/thread.h"
40#include "internal/time.h"
41#include "internal/vm.h"
45#include "ruby_assert.h"
73#include "internal/gc.h"
76#define SET_DEFAULT(hash, ifnone) ( \
77 FL_UNSET_RAW(hash, RHASH_PROC_DEFAULT), \
78 RHASH_SET_IFNONE(hash, ifnone))
80#define SET_PROC_DEFAULT(hash, proc) set_proc_default(hash, proc)
82#define COPY_DEFAULT(hash, hash2) copy_default(RHASH(hash), RHASH(hash2))
85copy_default(
struct RHash *hash,
const struct RHash *hash2)
87 hash->basic.
flags &= ~RHASH_PROC_DEFAULT;
88 hash->basic.
flags |= hash2->basic.
flags & RHASH_PROC_DEFAULT;
107 return rb_obj_freeze(hash);
113static ID id_hash, id_flatten_bang;
114static ID id_hash_iter_lev;
116#define id_default idDefault
128 if (a == b)
return 0;
133 if (UNDEF_P(a) || UNDEF_P(b))
return -1;
142hash_recursive(
VALUE obj,
VALUE arg,
int recurse)
144 if (recurse)
return INT2FIX(0);
145 return rb_funcallv(obj, id_hash, 0, 0);
148static long rb_objid_hash(st_index_t index);
151dbl_to_index(
double d)
153 union {
double d; st_index_t i;} u;
159rb_dbl_long_hash(
double d)
162 if (d == 0.0) d = 0.0;
163#if SIZEOF_INT == SIZEOF_VOIDP
166 return rb_objid_hash(dbl_to_index(d));
171any_hash(
VALUE a, st_index_t (*other_func)(
VALUE))
183 hnum = RSYMBOL(a)->hashval;
190 hnum = rb_objid_hash((st_index_t)a);
196 hval = rb_big_hash(a);
200 hnum = rb_dbl_long_hash(rb_float_value(a));
203 hnum = other_func(a);
213obj_any_hash(
VALUE obj)
215 VALUE hval = rb_check_funcall_basic_kw(obj, id_hash,
rb_mKernel, 0, 0, 0);
218 hval = rb_exec_recursive_outer_mid(hash_recursive, obj, 0, id_hash);
225 sign = rb_integer_pack(hval, &ul, 1,
sizeof(ul), 0,
243 return any_hash(a, obj_any_hash);
249 return LONG2FIX(any_hash(obj, obj_any_hash));
258static const uint64_t prime1 = ((uint64_t)0x2e0bb864 << 32) | 0xe9ea7df5;
259static const uint32_t prime2 = 0x830fcab9;
262static inline uint64_t
263mult_and_mix(uint64_t m1, uint64_t m2)
265#if defined HAVE_UINT128_T
266 uint128_t r = (uint128_t) m1 * (uint128_t) m2;
267 return (uint64_t) (r >> 64) ^ (uint64_t) r;
269 uint64_t hm1 = m1 >> 32, hm2 = m2 >> 32;
270 uint64_t lm1 = m1, lm2 = m2;
271 uint64_t v64_128 = hm1 * hm2;
272 uint64_t v32_96 = hm1 * lm2 + lm1 * hm2;
273 uint64_t v1_32 = lm1 * lm2;
275 return (v64_128 + (v32_96 >> 32)) ^ ((v32_96 << 32) + v1_32);
279static inline uint64_t
280key64_hash(uint64_t key, uint32_t seed)
282 return mult_and_mix(key + seed, prime1);
286#define st_index_hash(index) key64_hash(rb_hash_start(index), prime2)
289rb_objid_hash(st_index_t index)
291 return (
long)st_index_hash(index);
297 VALUE object_id = rb_obj_id(obj);
299 object_id = rb_big_hash(object_id);
301#if SIZEOF_LONG == SIZEOF_VOIDP
302 return (st_index_t)st_index_hash((st_index_t)
NUM2LONG(object_id));
303#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
304 return (st_index_t)st_index_hash((st_index_t)
NUM2LL(object_id));
312rb_obj_hash(
VALUE obj)
314 long hnum = any_hash(obj, objid_hash);
323#define rb_ident_cmp st_numcmp
326rb_ident_hash(st_data_t n)
335 n ^= dbl_to_index(rb_float_value(n));
339 return (st_index_t)st_index_hash((st_index_t)n);
342#define identhash rb_hashtype_ident
348typedef st_index_t st_hash_t;
358#define RHASH_AR_TABLE_MAX_BOUND RHASH_AR_TABLE_MAX_SIZE
360#define RHASH_AR_TABLE_REF(hash, n) (&RHASH_AR_TABLE(hash)->pairs[n])
361#define RHASH_AR_CLEARED_HINT 0xff
363static inline st_hash_t
364ar_do_hash(st_data_t key)
366 return (st_hash_t)rb_any_hash(key);
369static inline ar_hint_t
370ar_do_hash_hint(st_hash_t hash_value)
372 return (ar_hint_t)hash_value;
375static inline ar_hint_t
376ar_hint(
VALUE hash,
unsigned int index)
378 return RHASH_AR_TABLE(hash)->ar_hint.ary[index];
382ar_hint_set_hint(
VALUE hash,
unsigned int index, ar_hint_t hint)
384 RHASH_AR_TABLE(hash)->ar_hint.ary[index] = hint;
388ar_hint_set(
VALUE hash,
unsigned int index, st_hash_t hash_value)
390 ar_hint_set_hint(hash, index, ar_do_hash_hint(hash_value));
394ar_clear_entry(
VALUE hash,
unsigned int index)
398 ar_hint_set_hint(hash, index, RHASH_AR_CLEARED_HINT);
402ar_cleared_entry(
VALUE hash,
unsigned int index)
404 if (ar_hint(hash, index) == RHASH_AR_CLEARED_HINT) {
409 return UNDEF_P(pair->key);
417ar_set_entry(
VALUE hash,
unsigned int index, st_data_t key, st_data_t val, st_hash_t hash_value)
422 ar_hint_set(hash, index, hash_value);
425#define RHASH_AR_TABLE_SIZE(h) (HASH_ASSERT(RHASH_AR_TABLE_P(h)), \
426 RHASH_AR_TABLE_SIZE_RAW(h))
428#define RHASH_AR_TABLE_BOUND_RAW(h) \
429 ((unsigned int)((RBASIC(h)->flags >> RHASH_AR_TABLE_BOUND_SHIFT) & \
430 (RHASH_AR_TABLE_BOUND_MASK >> RHASH_AR_TABLE_BOUND_SHIFT)))
432#define RHASH_ST_TABLE_SET(h, s) rb_hash_st_table_set(h, s)
433#define RHASH_TYPE(hash) (RHASH_AR_TABLE_P(hash) ? &objhash : RHASH_ST_TABLE(hash)->type)
435#define HASH_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(HASH_DEBUG, expr, #expr)
437static inline unsigned int
438RHASH_AR_TABLE_BOUND(
VALUE h)
440 HASH_ASSERT(RHASH_AR_TABLE_P(h));
441 const unsigned int bound = RHASH_AR_TABLE_BOUND_RAW(h);
442 HASH_ASSERT(bound <= RHASH_AR_TABLE_MAX_SIZE);
447#define hash_verify(hash) hash_verify_(hash, __FILE__, __LINE__)
450rb_hash_dump(
VALUE hash)
452 rb_obj_info_dump(hash);
454 if (RHASH_AR_TABLE_P(hash)) {
455 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
457 fprintf(stderr,
" size:%u bound:%u\n",
458 RHASH_AR_TABLE_SIZE(hash), bound);
460 for (i=0; i<bound; i++) {
463 if (!ar_cleared_entry(hash, i)) {
464 char b1[0x100], b2[0x100];
468 fprintf(stderr,
" %d key:%s val:%s hint:%02x\n", i,
469 rb_raw_obj_info(b1, 0x100, k),
470 rb_raw_obj_info(b2, 0x100, v),
474 fprintf(stderr,
" %d empty\n", i);
481hash_verify_(
VALUE hash,
const char *file,
int line)
483 HASH_ASSERT(RB_TYPE_P(hash,
T_HASH));
485 if (RHASH_AR_TABLE_P(hash)) {
486 unsigned i, n = 0, bound = RHASH_AR_TABLE_BOUND(hash);
488 for (i=0; i<bound; i++) {
490 if (!ar_cleared_entry(hash, i)) {
494 HASH_ASSERT(!UNDEF_P(k));
495 HASH_ASSERT(!UNDEF_P(v));
499 if (n != RHASH_AR_TABLE_SIZE(hash)) {
500 rb_bug(
"n:%u, RHASH_AR_TABLE_SIZE:%u", n, RHASH_AR_TABLE_SIZE(hash));
504 HASH_ASSERT(RHASH_ST_TABLE(hash) != NULL);
505 HASH_ASSERT(RHASH_AR_TABLE_SIZE_RAW(hash) == 0);
506 HASH_ASSERT(RHASH_AR_TABLE_BOUND_RAW(hash) == 0);
513#define hash_verify(h) ((void)0)
517RHASH_TABLE_EMPTY_P(
VALUE hash)
522#define RHASH_SET_ST_FLAG(h) FL_SET_RAW(h, RHASH_ST_TABLE_FLAG)
523#define RHASH_UNSET_ST_FLAG(h) FL_UNSET_RAW(h, RHASH_ST_TABLE_FLAG)
528 st_init_existing_table_with_size(RHASH_ST_TABLE(hash),
type, size);
529 RHASH_SET_ST_FLAG(hash);
535 HASH_ASSERT(st != NULL);
536 RHASH_SET_ST_FLAG(hash);
538 *RHASH_ST_TABLE(hash) = *st;
542RHASH_AR_TABLE_BOUND_SET(
VALUE h, st_index_t n)
544 HASH_ASSERT(RHASH_AR_TABLE_P(h));
545 HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_BOUND);
547 RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
548 RBASIC(h)->flags |= n << RHASH_AR_TABLE_BOUND_SHIFT;
552RHASH_AR_TABLE_SIZE_SET(
VALUE h, st_index_t n)
554 HASH_ASSERT(RHASH_AR_TABLE_P(h));
555 HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_SIZE);
557 RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
558 RBASIC(h)->flags |= n << RHASH_AR_TABLE_SIZE_SHIFT;
562HASH_AR_TABLE_SIZE_ADD(
VALUE h, st_index_t n)
564 HASH_ASSERT(RHASH_AR_TABLE_P(h));
566 RHASH_AR_TABLE_SIZE_SET(h, RHASH_AR_TABLE_SIZE(h) + n);
571#define RHASH_AR_TABLE_SIZE_INC(h) HASH_AR_TABLE_SIZE_ADD(h, 1)
574RHASH_AR_TABLE_SIZE_DEC(
VALUE h)
576 HASH_ASSERT(RHASH_AR_TABLE_P(h));
577 int new_size = RHASH_AR_TABLE_SIZE(h) - 1;
580 RHASH_AR_TABLE_SIZE_SET(h, new_size);
583 RHASH_AR_TABLE_SIZE_SET(h, 0);
584 RHASH_AR_TABLE_BOUND_SET(h, 0);
590RHASH_AR_TABLE_CLEAR(
VALUE h)
592 RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
593 RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
595 memset(RHASH_AR_TABLE(h), 0,
sizeof(
ar_table));
598NOINLINE(
static int ar_equal(
VALUE x,
VALUE y));
603 return rb_any_cmp(x, y) == 0;
607ar_find_entry_hint(
VALUE hash, ar_hint_t hint, st_data_t key)
609 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
610 const ar_hint_t *hints = RHASH_AR_TABLE(hash)->ar_hint.ary;
614 for (i = 0; i < bound; i++) {
615 if (hints[i] == hint) {
617 if (ar_equal(key, pair->key)) {
618 RB_DEBUG_COUNTER_INC(artable_hint_hit);
624 static char fname[256];
627 if (pid != getpid()) {
628 snprintf(fname,
sizeof(fname),
"/tmp/ruby-armiss.%d", pid = getpid());
629 if ((fp = fopen(fname,
"w")) == NULL) rb_bug(
"fopen");
632 st_hash_t h1 = ar_do_hash(key);
633 st_hash_t h2 = ar_do_hash(pair->key);
635 fprintf(fp,
"miss: hash_eq:%d hints[%d]:%02x hint:%02x\n"
637 " pair->key:%016lx %s\n",
638 h1 == h2, i, hints[i], hint,
639 h1, rb_obj_info(key), h2, rb_obj_info(pair->key));
641 RB_DEBUG_COUNTER_INC(artable_hint_miss);
645 RB_DEBUG_COUNTER_INC(artable_hint_notfound);
646 return RHASH_AR_TABLE_MAX_BOUND;
650ar_find_entry(
VALUE hash, st_hash_t hash_value, st_data_t key)
652 ar_hint_t hint = ar_do_hash_hint(hash_value);
653 return ar_find_entry_hint(hash, hint, key);
657hash_ar_free_and_clear_table(
VALUE hash)
659 RHASH_AR_TABLE_CLEAR(hash);
661 HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
662 HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
665void rb_st_add_direct_with_hash(
st_table *tab, st_data_t key, st_data_t value, st_hash_t hash);
667enum ar_each_key_type {
674ar_each_key(
ar_table *ar,
int max,
enum ar_each_key_type
type, st_data_t *dst_keys,
st_table *new_tab, st_hash_t *hashes)
676 for (
int i = 0; i < max; i++) {
680 case ar_each_key_copy:
681 dst_keys[i] = pair->key;
683 case ar_each_key_cmp:
684 if (dst_keys[i] != pair->key)
return 1;
686 case ar_each_key_insert:
687 if (UNDEF_P(pair->key))
continue;
688 rb_st_add_direct_with_hash(new_tab, pair->key, pair->val, hashes[i]);
697ar_force_convert_table(
VALUE hash,
const char *file,
int line)
699 if (RHASH_ST_TABLE_P(hash)) {
700 return RHASH_ST_TABLE(hash);
703 ar_table *ar = RHASH_AR_TABLE(hash);
704 st_hash_t hashes[RHASH_AR_TABLE_MAX_SIZE];
705 unsigned int bound, size;
709 st_data_t keys[RHASH_AR_TABLE_MAX_SIZE];
710 bound = RHASH_AR_TABLE_BOUND(hash);
711 size = RHASH_AR_TABLE_SIZE(hash);
712 ar_each_key(ar, bound, ar_each_key_copy, keys, NULL, NULL);
714 for (
unsigned int i = 0; i < bound; i++) {
716 hashes[i] = UNDEF_P(keys[i]) ? 0 : ar_do_hash(keys[i]);
720 if (UNLIKELY(!RHASH_AR_TABLE_P(hash)))
return RHASH_ST_TABLE(hash);
721 if (UNLIKELY(RHASH_AR_TABLE_BOUND(hash) != bound))
continue;
722 if (UNLIKELY(ar_each_key(ar, bound, ar_each_key_cmp, keys, NULL, NULL)))
continue;
728 rb_st_init_existing_table_with_size(new_tab, &objhash, size);
729 ar_each_key(ar, bound, ar_each_key_insert, NULL, new_tab, hashes);
730 hash_ar_free_and_clear_table(hash);
731 RHASH_ST_TABLE_SET(hash, new_tab);
732 return RHASH_ST_TABLE(hash);
737ar_compact_table(
VALUE hash)
739 const unsigned bound = RHASH_AR_TABLE_BOUND(hash);
740 const unsigned size = RHASH_AR_TABLE_SIZE(hash);
749 for (i=0; i<bound; i++) {
750 if (ar_cleared_entry(hash, i)) {
752 for (; j<bound; j++) {
753 if (!ar_cleared_entry(hash, j)) {
755 ar_hint_set_hint(hash, i, (st_hash_t)ar_hint(hash, j));
756 ar_clear_entry(hash, j);
767 HASH_ASSERT(i<=bound);
769 RHASH_AR_TABLE_BOUND_SET(hash, size);
776ar_add_direct_with_hash(
VALUE hash, st_data_t key, st_data_t val, st_hash_t hash_value)
778 unsigned bin = RHASH_AR_TABLE_BOUND(hash);
780 if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_SIZE) {
784 if (UNLIKELY(bin >= RHASH_AR_TABLE_MAX_BOUND)) {
785 bin = ar_compact_table(hash);
787 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
789 ar_set_entry(hash, bin, key, val, hash_value);
790 RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
791 RHASH_AR_TABLE_SIZE_INC(hash);
797ensure_ar_table(
VALUE hash)
799 if (!RHASH_AR_TABLE_P(hash)) {
800 rb_raise(
rb_eRuntimeError,
"hash representation was changed during iteration");
805ar_general_foreach(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
807 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
808 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
810 for (i = 0; i < bound; i++) {
811 if (ar_cleared_entry(hash, i))
continue;
814 enum st_retval retval = (*func)(pair->key, pair->val, arg, 0);
815 ensure_ar_table(hash);
826 VALUE key = pair->key;
827 VALUE val = pair->val;
828 retval = (*replace)(&key, &val, arg, TRUE);
837 ar_clear_entry(hash, i);
838 RHASH_AR_TABLE_SIZE_DEC(hash);
847ar_foreach_with_replace(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
849 return ar_general_foreach(hash, func, replace, arg);
853 st_foreach_callback_func *func;
858apply_functor(st_data_t k, st_data_t v, st_data_t d,
int _)
860 const struct functor *f = (
void *)d;
861 return f->func(k, v, f->arg);
865ar_foreach(
VALUE hash, st_foreach_callback_func *func, st_data_t arg)
867 const struct functor f = { func, arg };
868 return ar_general_foreach(hash, apply_functor, NULL, (st_data_t)&f);
872ar_foreach_check(
VALUE hash, st_foreach_check_callback_func *func, st_data_t arg,
875 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
876 unsigned i, ret = 0, bound = RHASH_AR_TABLE_BOUND(hash);
877 enum st_retval retval;
882 for (i = 0; i < bound; i++) {
883 if (ar_cleared_entry(hash, i))
continue;
885 pair = RHASH_AR_TABLE_REF(hash, i);
887 hint = ar_hint(hash, i);
889 retval = (*func)(key, pair->val, arg, 0);
890 ensure_ar_table(hash);
895 pair = RHASH_AR_TABLE_REF(hash, i);
896 if (pair->key == never)
break;
897 ret = ar_find_entry_hint(hash, hint, key);
898 if (ret == RHASH_AR_TABLE_MAX_BOUND) {
899 retval = (*func)(0, 0, arg, 1);
909 if (!ar_cleared_entry(hash, i)) {
910 ar_clear_entry(hash, i);
911 RHASH_AR_TABLE_SIZE_DEC(hash);
922ar_update(
VALUE hash, st_data_t key,
923 st_update_callback_func *func, st_data_t arg)
925 int retval, existing;
926 unsigned bin = RHASH_AR_TABLE_MAX_BOUND;
927 st_data_t value = 0, old_key;
928 st_hash_t hash_value = ar_do_hash(key);
930 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
935 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
936 bin = ar_find_entry(hash, hash_value, key);
937 existing = (bin != RHASH_AR_TABLE_MAX_BOUND) ? TRUE : FALSE;
949 retval = (*func)(&key, &value, arg, existing);
951 ensure_ar_table(hash);
956 if (ar_add_direct_with_hash(hash, key, value, hash_value)) {
962 if (old_key != key) {
970 ar_clear_entry(hash, bin);
971 RHASH_AR_TABLE_SIZE_DEC(hash);
979ar_insert(
VALUE hash, st_data_t key, st_data_t value)
981 unsigned bin = RHASH_AR_TABLE_BOUND(hash);
982 st_hash_t hash_value = ar_do_hash(key);
984 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
989 bin = ar_find_entry(hash, hash_value, key);
990 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
991 if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_SIZE) {
994 else if (bin >= RHASH_AR_TABLE_MAX_BOUND) {
995 bin = ar_compact_table(hash);
997 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
999 ar_set_entry(hash, bin, key, value, hash_value);
1000 RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
1001 RHASH_AR_TABLE_SIZE_INC(hash);
1005 RHASH_AR_TABLE_REF(hash, bin)->val = value;
1011ar_lookup(
VALUE hash, st_data_t key, st_data_t *value)
1013 if (RHASH_AR_TABLE_SIZE(hash) == 0) {
1017 st_hash_t hash_value = ar_do_hash(key);
1018 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1020 return st_lookup(RHASH_ST_TABLE(hash), key, value);
1022 unsigned bin = ar_find_entry(hash, hash_value, key);
1024 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1028 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
1029 if (value != NULL) {
1030 *value = RHASH_AR_TABLE_REF(hash, bin)->val;
1038ar_delete(
VALUE hash, st_data_t *key, st_data_t *value)
1041 st_hash_t hash_value = ar_do_hash(*key);
1043 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1045 return st_delete(RHASH_ST_TABLE(hash), key, value);
1048 bin = ar_find_entry(hash, hash_value, *key);
1050 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1051 if (value != 0) *value = 0;
1059 ar_clear_entry(hash, bin);
1060 RHASH_AR_TABLE_SIZE_DEC(hash);
1066ar_shift(
VALUE hash, st_data_t *key, st_data_t *value)
1068 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
1069 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1071 for (i = 0; i < bound; i++) {
1072 if (!ar_cleared_entry(hash, i)) {
1074 if (value != 0) *value = pair->val;
1076 ar_clear_entry(hash, i);
1077 RHASH_AR_TABLE_SIZE_DEC(hash);
1082 if (value != NULL) *value = 0;
1087ar_keys(
VALUE hash, st_data_t *keys, st_index_t size)
1089 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1090 st_data_t *keys_start = keys, *keys_end = keys + size;
1092 for (i = 0; i < bound; i++) {
1093 if (keys == keys_end) {
1097 if (!ar_cleared_entry(hash, i)) {
1098 *keys++ = RHASH_AR_TABLE_REF(hash, i)->key;
1103 return keys - keys_start;
1107ar_values(
VALUE hash, st_data_t *values, st_index_t size)
1109 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1110 st_data_t *values_start = values, *values_end = values + size;
1112 for (i = 0; i < bound; i++) {
1113 if (values == values_end) {
1117 if (!ar_cleared_entry(hash, i)) {
1118 *values++ = RHASH_AR_TABLE_REF(hash, i)->val;
1123 return values - values_start;
1129 ar_table *old_tab = RHASH_AR_TABLE(hash2);
1130 ar_table *new_tab = RHASH_AR_TABLE(hash1);
1132 *new_tab = *old_tab;
1133 RHASH_AR_TABLE(hash1)->ar_hint.word = RHASH_AR_TABLE(hash2)->ar_hint.word;
1134 RHASH_AR_TABLE_BOUND_SET(hash1, RHASH_AR_TABLE_BOUND(hash2));
1135 RHASH_AR_TABLE_SIZE_SET(hash1, RHASH_AR_TABLE_SIZE(hash2));
1137 rb_gc_writebarrier_remember(hash1);
1145 if (RHASH_AR_TABLE(hash) != NULL) {
1146 RHASH_AR_TABLE_SIZE_SET(hash, 0);
1147 RHASH_AR_TABLE_BOUND_SET(hash, 0);
1150 HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
1151 HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
1156hash_st_free(
VALUE hash)
1158 HASH_ASSERT(RHASH_ST_TABLE_P(hash));
1160 st_table *tab = RHASH_ST_TABLE(hash);
1163 xfree(tab->entries);
1167hash_st_free_and_clear_table(
VALUE hash)
1171 RHASH_ST_CLEAR(hash);
1175rb_hash_free(
VALUE hash)
1177 if (RHASH_ST_TABLE_P(hash)) {
1182typedef int st_foreach_func(st_data_t, st_data_t, st_data_t);
1186 st_foreach_func *func;
1191foreach_safe_i(st_data_t key, st_data_t value, st_data_t args,
int error)
1196 if (error)
return ST_STOP;
1197 status = (*arg->func)(key, value, arg->arg);
1198 if (status == ST_CONTINUE) {
1210 arg.func = (st_foreach_func *)func;
1212 if (st_foreach_check(table, foreach_safe_i, (st_data_t)&arg, 0)) {
1221 rb_foreach_func *func;
1226hash_iter_status_check(
int status)
1241hash_ar_foreach_iter(st_data_t key, st_data_t value, st_data_t argp,
int error)
1245 if (error)
return ST_STOP;
1247 int status = (*arg->func)((
VALUE)key, (
VALUE)value, arg->arg);
1250 return hash_iter_status_check(status);
1254hash_foreach_iter(st_data_t key, st_data_t value, st_data_t argp,
int error)
1258 if (error)
return ST_STOP;
1260 st_table *tbl = RHASH_ST_TABLE(arg->hash);
1261 int status = (*arg->func)((
VALUE)key, (
VALUE)value, arg->arg);
1263 if (RHASH_ST_TABLE(arg->hash) != tbl) {
1267 return hash_iter_status_check(status);
1271iter_lev_in_ivar(
VALUE hash)
1276 HASH_ASSERT(lev >= 0);
1277 return (
unsigned long)lev;
1283iter_lev_in_ivar_set(
VALUE hash,
unsigned long lev)
1285 HASH_ASSERT(lev >= RHASH_LEV_MAX);
1287 rb_ivar_set_internal(hash, id_hash_iter_lev,
LONG2FIX((
long)lev));
1290static inline unsigned long
1291iter_lev_in_flags(
VALUE hash)
1293 return (
unsigned long)((
RBASIC(hash)->flags >> RHASH_LEV_SHIFT) & RHASH_LEV_MAX);
1297iter_lev_in_flags_set(
VALUE hash,
unsigned long lev)
1299 HASH_ASSERT(lev <= RHASH_LEV_MAX);
1300 RBASIC(hash)->flags = ((
RBASIC(hash)->flags & ~RHASH_LEV_MASK) | ((
VALUE)lev << RHASH_LEV_SHIFT));
1304hash_iterating_p(
VALUE hash)
1306 return iter_lev_in_flags(hash) > 0;
1310hash_iter_lev_inc(
VALUE hash)
1312 unsigned long lev = iter_lev_in_flags(hash);
1313 if (lev == RHASH_LEV_MAX) {
1314 lev = iter_lev_in_ivar(hash) + 1;
1321 iter_lev_in_flags_set(hash, lev);
1322 if (lev < RHASH_LEV_MAX)
return;
1324 iter_lev_in_ivar_set(hash, lev);
1328hash_iter_lev_dec(
VALUE hash)
1330 unsigned long lev = iter_lev_in_flags(hash);
1331 if (lev == RHASH_LEV_MAX) {
1332 lev = iter_lev_in_ivar(hash);
1333 if (lev > RHASH_LEV_MAX) {
1334 iter_lev_in_ivar_set(hash, lev-1);
1337 rb_attr_delete(hash, id_hash_iter_lev);
1339 else if (lev == 0) {
1342 iter_lev_in_flags_set(hash, lev - 1);
1346hash_foreach_ensure_rollback(
VALUE hash)
1348 hash_iter_lev_inc(hash);
1353hash_foreach_ensure(
VALUE hash)
1355 hash_iter_lev_dec(hash);
1360rb_hash_stlike_foreach(
VALUE hash, st_foreach_callback_func *func, st_data_t arg)
1362 if (RHASH_AR_TABLE_P(hash)) {
1363 return ar_foreach(hash, func, arg);
1366 return st_foreach(RHASH_ST_TABLE(hash), func, arg);
1371rb_hash_stlike_foreach_with_replace(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
1373 if (RHASH_AR_TABLE_P(hash)) {
1374 return ar_foreach_with_replace(hash, func, replace, arg);
1377 return st_foreach_with_replace(RHASH_ST_TABLE(hash), func, replace, arg);
1382hash_foreach_call(
VALUE arg)
1386 if (RHASH_AR_TABLE_P(hash)) {
1387 ret = ar_foreach_check(hash, hash_ar_foreach_iter,
1388 (st_data_t)arg, (st_data_t)
Qundef);
1390 else if (RHASH_ST_TABLE_P(hash)) {
1391 ret = st_foreach_check(RHASH_ST_TABLE(hash), hash_foreach_iter,
1392 (st_data_t)arg, (st_data_t)
Qundef);
1395 rb_raise(
rb_eRuntimeError,
"ret: %d, hash modified during iteration", ret);
1401rb_hash_foreach(
VALUE hash, rb_foreach_func *func,
VALUE farg)
1405 if (RHASH_TABLE_EMPTY_P(hash))
1408 arg.func = (rb_foreach_func *)func;
1410 if (RB_OBJ_FROZEN(hash)) {
1411 hash_foreach_call((
VALUE)&arg);
1414 hash_iter_lev_inc(hash);
1415 rb_ensure(hash_foreach_call, (
VALUE)&arg, hash_foreach_ensure, hash);
1420void rb_st_compact_table(
st_table *tab);
1423compact_after_delete(
VALUE hash)
1425 if (!hash_iterating_p(hash) && RHASH_ST_TABLE_P(hash)) {
1426 rb_st_compact_table(RHASH_ST_TABLE(hash));
1434 const size_t size =
sizeof(
struct RHash) + (st ? sizeof(
st_table) : sizeof(ar_table));
1436 NEWOBJ_OF(hash, struct RHash, klass, T_HASH | wb | flags, size, 0);
1444hash_alloc(VALUE klass)
1451empty_hash_alloc(
VALUE klass)
1453 RUBY_DTRACE_CREATE_HOOK(HASH, 0);
1455 return hash_alloc(klass);
1467 if (rb_hash_compare_by_id_p(basis)) {
1468 return rb_hash_compare_by_id(hash);
1474rb_hash_new_with_size(st_index_t size)
1476 bool st = size > RHASH_AR_TABLE_MAX_SIZE;
1480 hash_st_table_init(ret, &objhash, size);
1489 return rb_hash_new_with_size((st_index_t)
capa);
1495 if (RHASH_AR_TABLE_P(hash)) {
1496 if (RHASH_AR_TABLE_P(ret)) {
1500 st_table *tab = RHASH_ST_TABLE(ret);
1501 rb_st_init_existing_table_with_size(tab, &objhash, RHASH_AR_TABLE_SIZE(hash));
1503 int bound = RHASH_AR_TABLE_BOUND(hash);
1504 for (
int i = 0; i < bound; i++) {
1505 if (ar_cleared_entry(hash, i))
continue;
1508 st_add_direct(tab, pair->key, pair->val);
1517 RHASH_SET_ST_FLAG(ret);
1518 st_replace(RHASH_ST_TABLE(ret), RHASH_ST_TABLE(hash));
1520 rb_gc_writebarrier_remember(ret);
1526hash_dup_with_compare_by_id(
VALUE hash)
1528 return hash_copy(copy_compare_by_id(rb_hash_new(), hash), hash);
1543 flags & (
FL_EXIVAR|RHASH_PROC_DEFAULT));
1550rb_hash_resurrect(
VALUE hash)
1557rb_hash_modify_check(
VALUE hash)
1562RUBY_FUNC_EXPORTED
struct st_table *
1563rb_hash_tbl_raw(
VALUE hash,
const char *file,
int line)
1565 return ar_force_convert_table(hash, file, line);
1569rb_hash_tbl(
VALUE hash,
const char *file,
int line)
1572 return rb_hash_tbl_raw(hash, file, line);
1576rb_hash_modify(
VALUE hash)
1578 rb_hash_modify_check(hash);
1581NORETURN(
static void no_new_key(
void));
1585 rb_raise(
rb_eRuntimeError,
"can't add a new key into hash during iteration");
1593#define NOINSERT_UPDATE_CALLBACK(func) \
1595func##_noinsert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
1597 if (!existing) no_new_key(); \
1598 return func(key, val, (struct update_arg *)arg, existing); \
1602func##_insert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
1604 return func(key, val, (struct update_arg *)arg, existing); \
1609 st_update_callback_func *func;
1615typedef int (*tbl_update_func)(st_data_t *, st_data_t *, st_data_t, int);
1618rb_hash_stlike_update(
VALUE hash, st_data_t key, st_update_callback_func *func, st_data_t arg)
1620 if (RHASH_AR_TABLE_P(hash)) {
1621 int result = ar_update(hash, key, func, arg);
1623 ar_force_convert_table(hash, __FILE__, __LINE__);
1630 return st_update(RHASH_ST_TABLE(hash), key, func, arg);
1634tbl_update_modify(st_data_t *key, st_data_t *val, st_data_t arg,
int existing)
1637 st_data_t old_key = *key;
1638 st_data_t old_value = *val;
1639 VALUE hash = p->hash;
1640 int ret = (p->func)(key, val, arg, existing);
1645 if (!existing || *key != old_key || *val != old_value) {
1646 rb_hash_modify(hash);
1653 rb_hash_modify(hash);
1661tbl_update(
VALUE hash,
VALUE key, tbl_update_func func, st_data_t optional_arg)
1664 .arg = optional_arg,
1668 .value = (
VALUE)optional_arg,
1671 int ret = rb_hash_stlike_update(hash, key, tbl_update_modify, (st_data_t)&arg);
1680#define UPDATE_CALLBACK(iter_p, func) ((iter_p) ? func##_noinsert : func##_insert)
1682#define RHASH_UPDATE_ITER(h, iter_p, key, func, a) do { \
1683 tbl_update((h), (key), UPDATE_CALLBACK(iter_p, func), (st_data_t)(a)); \
1686#define RHASH_UPDATE(hash, key, func, arg) \
1687 RHASH_UPDATE_ITER(hash, hash_iterating_p(hash), key, func, arg)
1695 if (n != 2 && (n >= 0 || n < -3)) {
1696 if (n < 0) n = -n-1;
1697 rb_raise(
rb_eTypeError,
"default_proc takes two arguments (2 for %d)", n);
1737rb_hash_initialize(
int argc,
VALUE *argv,
VALUE hash)
1739 rb_hash_modify(hash);
1748 VALUE options, ifnone;
1752 rb_warn_deprecated_to_remove(
"3.4",
"Calling Hash.new with keyword arguments",
"Hash.new({ key: value })");
1798rb_hash_s_create(
int argc,
VALUE *argv,
VALUE klass)
1803 tmp = rb_hash_s_try_convert(
Qnil, argv[0]);
1809 tmp = rb_hash_to_a(tmp);
1812 hash = hash_alloc(klass);
1814 hash_copy(hash, tmp);
1819 tmp = rb_check_array_type(argv[0]);
1825 hash = hash_alloc(klass);
1828 VALUE v = rb_check_array_type(e);
1832 rb_raise(rb_eArgError,
"wrong element type %s at %ld (expected array)",
1833 rb_builtin_class_name(e), i);
1837 rb_raise(rb_eArgError,
"invalid number of elements (%ld for 1..2)",
1843 rb_hash_aset(hash, key, val);
1849 if (argc % 2 != 0) {
1850 rb_raise(rb_eArgError,
"odd number of arguments for Hash");
1853 hash = hash_alloc(klass);
1854 rb_hash_bulk_insert(argc, argv, hash);
1860rb_to_hash_type(
VALUE hash)
1862 return rb_convert_type_with_id(hash,
T_HASH,
"Hash", idTo_hash);
1864#define to_hash rb_to_hash_type
1869 return rb_check_convert_type_with_id(hash,
T_HASH,
"Hash", idTo_hash);
1886rb_hash_s_try_convert(
VALUE dummy,
VALUE hash)
1888 return rb_check_hash_type(hash);
1907rb_hash_s_ruby2_keywords_hash_p(
VALUE dummy,
VALUE hash)
1910 return RBOOL(RHASH(hash)->basic.flags & RHASH_PASS_AS_KEYWORDS);
1929rb_hash_s_ruby2_keywords_hash(
VALUE dummy,
VALUE hash)
1932 VALUE tmp = rb_hash_dup(hash);
1934 rb_hash_compare_by_id(tmp);
1936 RHASH(tmp)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
1948 if (RHASH_AR_TABLE_P(arg)) {
1949 ar_insert(arg, (st_data_t)key, (st_data_t)value);
1952 st_insert(RHASH_ST_TABLE(arg), (st_data_t)key, (st_data_t)value);
1970rb_hash_rehash(
VALUE hash)
1975 if (hash_iterating_p(hash)) {
1978 rb_hash_modify_check(hash);
1979 if (RHASH_AR_TABLE_P(hash)) {
1980 tmp = hash_alloc(0);
1981 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
1983 hash_ar_free_and_clear_table(hash);
1986 else if (RHASH_ST_TABLE_P(hash)) {
1987 st_table *old_tab = RHASH_ST_TABLE(hash);
1988 tmp = hash_alloc(0);
1990 hash_st_table_init(tmp, old_tab->type, old_tab->num_entries);
1991 tbl = RHASH_ST_TABLE(tmp);
1993 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
1996 RHASH_ST_TABLE_SET(hash, tbl);
1997 RHASH_ST_CLEAR(tmp);
2006 VALUE args[2] = {hash, key};
2011rb_hash_default_unredefined(
VALUE hash)
2015 return !!BASIC_OP_UNREDEFINED_P(BOP_DEFAULT, HASH_REDEFINED_OP_FLAG);
2018 return LIKELY(rb_method_basic_definition_p(klass, id_default));
2027 if (LIKELY(rb_hash_default_unredefined(hash))) {
2029 if (LIKELY(!
FL_TEST_RAW(hash, RHASH_PROC_DEFAULT)))
return ifnone;
2030 if (UNDEF_P(key))
return Qnil;
2031 return call_default_proc(ifnone, hash, key);
2039hash_stlike_lookup(
VALUE hash, st_data_t key, st_data_t *pval)
2043 if (RHASH_AR_TABLE_P(hash)) {
2044 return ar_lookup(hash, key, pval);
2047 extern st_index_t rb_iseq_cdhash_hash(
VALUE);
2049 RHASH_ST_TABLE(hash)->
type->hash == rb_ident_hash ||
2050 RHASH_ST_TABLE(hash)->
type->hash == rb_iseq_cdhash_hash);
2051 return st_lookup(RHASH_ST_TABLE(hash), key, pval);
2056rb_hash_stlike_lookup(
VALUE hash, st_data_t key, st_data_t *pval)
2058 return hash_stlike_lookup(hash, key, pval);
2080 if (hash_stlike_lookup(hash, key, &val)) {
2084 return rb_hash_default_value(hash, key);
2093 if (hash_stlike_lookup(hash, key, &val)) {
2104 return rb_hash_lookup2(hash, key,
Qnil);
2131rb_hash_fetch_m(
int argc,
VALUE *argv,
VALUE hash)
2141 if (block_given && argc == 2) {
2142 rb_warn(
"block supersedes default value argument");
2145 if (hash_stlike_lookup(hash, key, &val)) {
2152 else if (argc == 1) {
2158 rb_key_err_raise(rb_sprintf(
"key not found: %"PRIsVALUE, desc), hash, key);
2169 return rb_hash_fetch_m(1, &key, hash);
2193rb_hash_default(
int argc,
VALUE *argv,
VALUE hash)
2199 if (
FL_TEST(hash, RHASH_PROC_DEFAULT)) {
2200 if (argc == 0)
return Qnil;
2201 return call_default_proc(ifnone, hash, argv[0]);
2222 rb_hash_modify_check(hash);
2223 SET_DEFAULT(hash, ifnone);
2240rb_hash_default_proc(
VALUE hash)
2242 if (
FL_TEST(hash, RHASH_PROC_DEFAULT)) {
2263rb_hash_set_default_proc(
VALUE hash,
VALUE proc)
2267 rb_hash_modify_check(hash);
2269 SET_DEFAULT(hash, proc);
2272 b = rb_check_convert_type_with_id(proc,
T_DATA,
"Proc", idTo_proc);
2275 "wrong default_proc type %s (expected Proc)",
2279 SET_PROC_DEFAULT(hash, proc);
2316 rb_hash_foreach(hash, key_i, (
VALUE)args);
2322rb_hash_stlike_delete(
VALUE hash, st_data_t *pkey, st_data_t *pval)
2324 if (RHASH_AR_TABLE_P(hash)) {
2325 return ar_delete(hash, pkey, pval);
2328 return st_delete(RHASH_ST_TABLE(hash), pkey, pval);
2340 st_data_t ktmp = (st_data_t)key, val;
2342 if (rb_hash_stlike_delete(hash, &ktmp, &val)) {
2358 VALUE deleted_value = rb_hash_delete_entry(hash, key);
2360 if (!UNDEF_P(deleted_value)) {
2361 return deleted_value;
2400 rb_hash_modify_check(hash);
2401 val = rb_hash_delete_entry(hash, key);
2403 if (!UNDEF_P(val)) {
2404 compact_after_delete(hash);
2447rb_hash_shift(
VALUE hash)
2451 rb_hash_modify_check(hash);
2452 if (RHASH_AR_TABLE_P(hash)) {
2454 if (!hash_iterating_p(hash)) {
2455 if (ar_shift(hash, &var.key, &var.val)) {
2456 return rb_assoc_new(var.key, var.val);
2460 rb_hash_foreach(hash, shift_i_safe, (
VALUE)&var);
2461 if (!UNDEF_P(var.key)) {
2462 rb_hash_delete_entry(hash, var.key);
2463 return rb_assoc_new(var.key, var.val);
2467 if (RHASH_ST_TABLE_P(hash)) {
2469 if (!hash_iterating_p(hash)) {
2470 if (st_shift(RHASH_ST_TABLE(hash), &var.key, &var.val)) {
2471 return rb_assoc_new(var.key, var.val);
2475 rb_hash_foreach(hash, shift_i_safe, (
VALUE)&var);
2476 if (!UNDEF_P(var.key)) {
2477 rb_hash_delete_entry(hash, var.key);
2478 return rb_assoc_new(var.key, var.val);
2489 rb_hash_modify(hash);
2498 return rb_hash_size(hash);
2522 rb_hash_modify_check(hash);
2523 if (!RHASH_TABLE_EMPTY_P(hash)) {
2524 rb_hash_foreach(hash, delete_if_i, hash);
2525 compact_after_delete(hash);
2549rb_hash_reject_bang(
VALUE hash)
2554 rb_hash_modify(hash);
2556 if (!n)
return Qnil;
2557 rb_hash_foreach(hash, delete_if_i, hash);
2581rb_hash_reject(
VALUE hash)
2586 result = hash_dup_with_compare_by_id(hash);
2588 rb_hash_foreach(result, delete_if_i, result);
2589 compact_after_delete(result);
2606rb_hash_slice(
int argc,
VALUE *argv,
VALUE hash)
2609 VALUE key, value, result;
2612 return copy_compare_by_id(rb_hash_new(), hash);
2614 result = copy_compare_by_id(rb_hash_new_with_size(argc), hash);
2616 for (i = 0; i < argc; i++) {
2618 value = rb_hash_lookup2(hash, key,
Qundef);
2619 if (!UNDEF_P(value))
2620 rb_hash_aset(result, key, value);
2638rb_hash_except(
int argc,
VALUE *argv,
VALUE hash)
2643 result = hash_dup_with_compare_by_id(hash);
2645 for (i = 0; i < argc; i++) {
2647 rb_hash_delete(result, key);
2649 compact_after_delete(result);
2668rb_hash_values_at(
int argc,
VALUE *argv,
VALUE hash)
2673 for (i=0; i<argc; i++) {
2674 rb_ary_push(result, rb_hash_aref(hash, argv[i]));
2700rb_hash_fetch_values(
int argc,
VALUE *argv,
VALUE hash)
2705 for (i=0; i<argc; i++) {
2706 rb_ary_push(result, rb_hash_fetch(hash, argv[i]));
2715 rb_hash_modify(hash);
2737rb_hash_select(
VALUE hash)
2742 result = hash_dup_with_compare_by_id(hash);
2744 rb_hash_foreach(result, keep_if_i, result);
2745 compact_after_delete(result);
2768rb_hash_select_bang(
VALUE hash)
2773 rb_hash_modify_check(hash);
2775 if (!n)
return Qnil;
2776 rb_hash_foreach(hash, keep_if_i, hash);
2799rb_hash_keep_if(
VALUE hash)
2802 rb_hash_modify_check(hash);
2803 if (!RHASH_TABLE_EMPTY_P(hash)) {
2804 rb_hash_foreach(hash, keep_if_i, hash);
2825 rb_hash_modify_check(hash);
2827 if (hash_iterating_p(hash)) {
2828 rb_hash_foreach(hash, clear_i, 0);
2830 else if (RHASH_AR_TABLE_P(hash)) {
2834 st_clear(RHASH_ST_TABLE(hash));
2835 compact_after_delete(hash);
2842hash_aset(st_data_t *key, st_data_t *val,
struct update_arg *arg,
int existing)
2849rb_hash_key_str(
VALUE key)
2852 return rb_fstring(key);
2855 return rb_str_new_frozen(key);
2860hash_aset_str(st_data_t *key, st_data_t *val,
struct update_arg *arg,
int existing)
2862 if (!existing && !RB_OBJ_FROZEN(*key)) {
2863 *key = rb_hash_key_str(*key);
2865 return hash_aset(key, val, arg, existing);
2868NOINSERT_UPDATE_CALLBACK(hash_aset)
2869NOINSERT_UPDATE_CALLBACK(hash_aset_str)
2898 bool iter_p = hash_iterating_p(hash);
2900 rb_hash_modify(hash);
2903 RHASH_UPDATE_ITER(hash, iter_p, key, hash_aset, val);
2906 RHASH_UPDATE_ITER(hash, iter_p, key, hash_aset_str, val);
2924 rb_hash_modify_check(hash);
2925 if (hash == hash2)
return hash;
2926 if (hash_iterating_p(hash)) {
2929 hash2 = to_hash(hash2);
2931 COPY_DEFAULT(hash, hash2);
2933 if (RHASH_AR_TABLE_P(hash)) {
2934 hash_ar_free_and_clear_table(hash);
2937 hash_st_free_and_clear_table(hash);
2940 hash_copy(hash, hash2);
2978rb_hash_empty_p(
VALUE hash)
3015rb_hash_each_value(
VALUE hash)
3018 rb_hash_foreach(hash, each_value_i, 0);
3053rb_hash_each_key(
VALUE hash)
3056 rb_hash_foreach(hash, each_key_i, 0);
3063 rb_yield(rb_assoc_new(key, value));
3104rb_hash_each_pair(
VALUE hash)
3107 if (rb_block_pair_yield_optimizable())
3108 rb_hash_foreach(hash, each_pair_i_fast, 0);
3110 rb_hash_foreach(hash, each_pair_i, 0);
3124 VALUE trans = p->trans, result = p->result;
3125 VALUE new_key = rb_hash_lookup2(trans, key,
Qundef);
3126 if (UNDEF_P(new_key)) {
3132 rb_hash_aset(result, new_key, value);
3140 rb_hash_aset(result, new_key, value);
3182rb_hash_transform_keys(
int argc,
VALUE *argv,
VALUE hash)
3189 transarg.trans = to_hash(argv[0]);
3195 result = rb_hash_new();
3197 if (transarg.trans) {
3198 transarg.result = result;
3199 rb_hash_foreach(hash, transform_keys_hash_i, (
VALUE)&transarg);
3202 rb_hash_foreach(hash, transform_keys_i, result);
3222rb_hash_transform_keys_bang(
int argc,
VALUE *argv,
VALUE hash)
3225 int block_given = 0;
3229 trans = to_hash(argv[0]);
3235 rb_hash_modify_check(hash);
3236 if (!RHASH_TABLE_EMPTY_P(hash)) {
3238 VALUE new_keys = hash_alloc(0);
3240 rb_hash_foreach(hash, flatten_i, pairs);
3247 else if (!UNDEF_P(new_key = rb_hash_lookup2(trans, key,
Qundef))) {
3250 else if (block_given) {
3257 if (!hash_stlike_lookup(new_keys, key, NULL)) {
3258 rb_hash_stlike_delete(hash, &key, NULL);
3260 rb_hash_aset(hash, new_key, val);
3261 rb_hash_aset(new_keys, new_key,
Qnil);
3263 rb_ary_clear(pairs);
3264 rb_hash_clear(new_keys);
3266 compact_after_delete(hash);
3271transform_values_foreach_func(st_data_t key, st_data_t value, st_data_t argp,
int error)
3277transform_values_foreach_replace(st_data_t *key, st_data_t *value, st_data_t argp,
int existing)
3281 rb_hash_modify(hash);
3307rb_hash_transform_values(
VALUE hash)
3312 result = hash_dup_with_compare_by_id(hash);
3313 SET_DEFAULT(result,
Qnil);
3316 rb_hash_stlike_foreach_with_replace(result, transform_values_foreach_func, transform_values_foreach_replace, result);
3317 compact_after_delete(result);
3339rb_hash_transform_values_bang(
VALUE hash)
3342 rb_hash_modify_check(hash);
3344 if (!RHASH_TABLE_EMPTY_P(hash)) {
3345 rb_hash_stlike_foreach_with_replace(hash, transform_values_foreach_func, transform_values_foreach_replace, hash);
3354 rb_ary_push(ary, rb_assoc_new(key, value));
3369rb_hash_to_a(
VALUE hash)
3374 rb_hash_foreach(hash, to_a_i, ary);
3385 if (RSTRING_LEN(str) > 1) {
3389 rb_enc_copy(str, str2);
3391 rb_str_buf_append(str, str2);
3394 rb_str_buf_append(str, str2);
3400inspect_hash(
VALUE hash,
VALUE dummy,
int recur)
3406 rb_hash_foreach(hash, inspect_i, str);
3424rb_hash_inspect(
VALUE hash)
3438rb_hash_to_hash(
VALUE hash)
3448 pair = rb_check_array_type(arg);
3450 rb_raise(
rb_eTypeError,
"wrong element type %s (expected array)",
3451 rb_builtin_class_name(arg));
3454 rb_raise(rb_eArgError,
"element has wrong array length (expected 2, was %ld)",
3469rb_hash_to_h_block(
VALUE hash)
3472 rb_hash_foreach(hash, to_h_i, h);
3496rb_hash_to_h(
VALUE hash)
3499 return rb_hash_to_h_block(hash);
3503 hash = hash_dup(hash,
rb_cHash, flags & RHASH_PROC_DEFAULT);
3511 rb_ary_push(ary, key);
3525rb_hash_keys(
VALUE hash)
3528 VALUE keys = rb_ary_new_capa(size);
3530 if (size == 0)
return keys;
3532 if (ST_DATA_COMPATIBLE_P(
VALUE)) {
3534 if (RHASH_AR_TABLE_P(hash)) {
3535 size = ar_keys(hash, ptr, size);
3538 st_table *table = RHASH_ST_TABLE(hash);
3539 size = st_keys(table, ptr, size);
3542 rb_gc_writebarrier_remember(keys);
3543 rb_ary_set_len(keys, size);
3546 rb_hash_foreach(hash, keys_i, keys);
3555 rb_ary_push(ary, value);
3569rb_hash_values(
VALUE hash)
3574 values = rb_ary_new_capa(size);
3575 if (size == 0)
return values;
3577 if (ST_DATA_COMPATIBLE_P(
VALUE)) {
3578 if (RHASH_AR_TABLE_P(hash)) {
3579 rb_gc_writebarrier_remember(values);
3581 size = ar_values(hash, ptr, size);
3584 else if (RHASH_ST_TABLE_P(hash)) {
3585 st_table *table = RHASH_ST_TABLE(hash);
3586 rb_gc_writebarrier_remember(values);
3588 size = st_values(table, ptr, size);
3591 rb_ary_set_len(values, size);
3595 rb_hash_foreach(hash, values_i, values);
3614 return RBOOL(hash_stlike_lookup(hash, key, NULL));
3644 rb_hash_foreach(hash, rb_hash_search_value, (
VALUE)data);
3660 if (!hash_stlike_lookup(data->hash, key, &val2)) {
3674recursive_eql(
VALUE hash,
VALUE dt,
int recur)
3678 if (recur)
return Qtrue;
3680 data->result =
Qtrue;
3681 rb_hash_foreach(hash, eql_i, dt);
3683 return data->result;
3691 if (hash1 == hash2)
return Qtrue;
3692 if (!RB_TYPE_P(hash2,
T_HASH)) {
3697 if (
rb_eql(hash2, hash1)) {
3710 if (!RHASH_TABLE_EMPTY_P(hash1) && !RHASH_TABLE_EMPTY_P(hash2)) {
3711 if (RHASH_TYPE(hash1) != RHASH_TYPE(hash2)) {
3723 FL_TEST(hash1, RHASH_PROC_DEFAULT) ==
FL_TEST(hash2, RHASH_PROC_DEFAULT)))
3751 return hash_equal(hash1, hash2, FALSE);
3776 return hash_equal(hash1, hash2, TRUE);
3782 st_index_t *hval = (st_index_t *)arg;
3783 st_index_t hdata[2];
3785 hdata[0] = rb_hash(key);
3786 hdata[1] = rb_hash(val);
3787 *hval ^= st_hash(hdata,
sizeof(hdata), 0);
3806rb_hash_hash(
VALUE hash)
3812 rb_hash_foreach(hash, hash_i, (
VALUE)&hval);
3821 rb_hash_aset(hash, value, key);
3841rb_hash_invert(
VALUE hash)
3845 rb_hash_foreach(hash, rb_hash_invert_i, h);
3850rb_hash_update_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
3856NOINSERT_UPDATE_CALLBACK(rb_hash_update_callback)
3861 RHASH_UPDATE(hash, key, rb_hash_update_callback, value);
3866rb_hash_update_block_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
3868 st_data_t newvalue = arg->arg;
3877NOINSERT_UPDATE_CALLBACK(rb_hash_update_block_callback)
3882 RHASH_UPDATE(hash, key, rb_hash_update_block_callback, value);
3935rb_hash_update(
int argc,
VALUE *argv,
VALUE self)
3940 rb_hash_modify(self);
3941 for (i = 0; i < argc; i++){
3942 VALUE hash = to_hash(argv[i]);
3944 rb_hash_foreach(hash, rb_hash_update_block_i, self);
3947 rb_hash_foreach(hash, rb_hash_update_i, self);
3960rb_hash_update_func_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
3963 VALUE newvalue = uf_arg->value;
3966 newvalue = (*uf_arg->func)((
VALUE)*key, (
VALUE)*value, newvalue);
3972NOINSERT_UPDATE_CALLBACK(rb_hash_update_func_callback)
3978 VALUE hash = arg->hash;
3981 RHASH_UPDATE(hash, key, rb_hash_update_func_callback, (
VALUE)arg);
3988 rb_hash_modify(hash1);
3989 hash2 = to_hash(hash2);
3994 rb_hash_foreach(hash2, rb_hash_update_func_i, (
VALUE)&arg);
3997 rb_hash_foreach(hash2, rb_hash_update_i, hash1);
4054rb_hash_merge(
int argc,
VALUE *argv,
VALUE self)
4056 return rb_hash_update(argc, argv, copy_compare_by_id(rb_hash_dup(self), self));
4071assoc_lookup(
VALUE arg)
4075 if (st_lookup(p->tbl, p->key, &data))
return (
VALUE)data;
4085 args[1] = rb_assoc_new(key, val);
4109 if (RHASH_ST_TABLE_P(hash) && RHASH_ST_TABLE(hash)->
type != &identhash) {
4111 st_table assoctable = *RHASH_ST_TABLE(hash);
4113 .compare = assoc_cmp,
4114 .hash = assoctable.type->hash,
4118 .key = (st_data_t)key,
4121 if (RB_OBJ_FROZEN(hash)) {
4122 value = assoc_lookup(arg);
4125 hash_iter_lev_inc(hash);
4126 value =
rb_ensure(assoc_lookup, arg, hash_foreach_ensure, hash);
4129 if (!UNDEF_P(value))
return rb_assoc_new(key, value);
4134 rb_hash_foreach(hash, assoc_i, (
VALUE)args);
4144 args[1] = rb_assoc_new(key, val);
4170 rb_hash_foreach(hash, rassoc_i, (
VALUE)args);
4181 rb_ary_cat(ary, pair, 2);
4218rb_hash_flatten(
int argc,
VALUE *argv,
VALUE hash)
4227 if (level == 0)
return rb_hash_to_a(hash);
4230 rb_hash_foreach(hash, flatten_i, ary);
4235 rb_funcallv(ary, id_flatten_bang, 1, &ary_flatten_level);
4237 else if (level < 0) {
4239 rb_funcallv(ary, id_flatten_bang, 0, 0);
4244 rb_hash_foreach(hash, flatten_i, ary);
4270rb_hash_compact(
VALUE hash)
4272 VALUE result = rb_hash_dup(hash);
4274 rb_hash_foreach(result, delete_if_nil, result);
4275 compact_after_delete(result);
4277 else if (rb_hash_compare_by_id_p(hash)) {
4278 result = rb_hash_compare_by_id(result);
4295rb_hash_compact_bang(
VALUE hash)
4298 rb_hash_modify_check(hash);
4301 rb_hash_foreach(hash, delete_if_nil, hash);
4337rb_hash_compare_by_id(
VALUE hash)
4342 if (rb_hash_compare_by_id_p(hash))
return hash;
4344 rb_hash_modify_check(hash);
4345 if (hash_iterating_p(hash)) {
4349 if (RHASH_TABLE_EMPTY_P(hash)) {
4352 ar_force_convert_table(hash, __FILE__, __LINE__);
4353 HASH_ASSERT(RHASH_ST_TABLE_P(hash));
4355 RHASH_ST_TABLE(hash)->type = &identhash;
4360 tmp = hash_alloc(0);
4361 hash_st_table_init(tmp, &identhash,
RHASH_SIZE(hash));
4362 identtable = RHASH_ST_TABLE(tmp);
4364 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
4369 RHASH_ST_TABLE_SET(hash, identtable);
4370 RHASH_ST_CLEAR(tmp);
4384rb_hash_compare_by_id_p(
VALUE hash)
4386 return RBOOL(RHASH_ST_TABLE_P(hash) && RHASH_ST_TABLE(hash)->
type == &identhash);
4390rb_ident_hash_new(
void)
4392 VALUE hash = rb_hash_new();
4393 hash_st_table_init(hash, &identhash, 0);
4398rb_ident_hash_new_with_size(st_index_t size)
4400 VALUE hash = rb_hash_new();
4401 hash_st_table_init(hash, &identhash, size);
4406rb_init_identtable(
void)
4408 return st_init_table(&identhash);
4479rb_hash_any_p(
int argc,
VALUE *argv,
VALUE hash)
4488 rb_warn(
"given block not used");
4492 rb_hash_foreach(hash, any_p_i_pattern, (
VALUE)args);
4499 if (rb_block_pair_yield_optimizable())
4500 rb_hash_foreach(hash, any_p_i_fast, (
VALUE)args);
4502 rb_hash_foreach(hash, any_p_i, (
VALUE)args);
4537rb_hash_dig(
int argc,
VALUE *argv,
VALUE self)
4540 self = rb_hash_aref(self, *argv);
4541 if (!--argc)
return self;
4543 return rb_obj_dig(argc, argv, self,
Qnil);
4551 if (!UNDEF_P(v) &&
rb_equal(value, v))
return ST_CONTINUE;
4562 rb_hash_foreach(hash1, hash_le_i, (
VALUE)args);
4580 other = to_hash(other);
4582 return hash_le(hash, other);
4599 other = to_hash(other);
4601 return hash_le(hash, other);
4618 other = to_hash(other);
4620 return hash_le(other, hash);
4637 other = to_hash(other);
4639 return hash_le(other, hash);
4646 return rb_hash_aref(hash, *argv);
4662rb_hash_to_proc(
VALUE hash)
4664 return rb_func_lambda_new(hash_proc_call, hash, 1, 1);
4669rb_hash_deconstruct_keys(
VALUE hash,
VALUE keys)
4675add_new_i(st_data_t *key, st_data_t *val, st_data_t arg,
int existing)
4678 if (existing)
return ST_STOP;
4697 if (RHASH_AR_TABLE_P(hash)) {
4698 ret = ar_update(hash, (st_data_t)key, add_new_i, (st_data_t)args);
4702 ar_force_convert_table(hash, __FILE__, __LINE__);
4705 tbl = RHASH_TBL_RAW(hash);
4706 return st_update(tbl, (st_data_t)key, add_new_i, (st_data_t)args);
4711key_stringify(
VALUE key)
4714 rb_hash_key_str(key) : key;
4718ar_bulk_insert(
VALUE hash,
long argc,
const VALUE *argv)
4721 for (i = 0; i < argc; ) {
4722 st_data_t k = key_stringify(argv[i++]);
4723 st_data_t v = argv[i++];
4724 ar_insert(hash, k, v);
4733 HASH_ASSERT(argc % 2 == 0);
4735 st_index_t size = argc / 2;
4737 if (RHASH_AR_TABLE_P(hash) &&
4738 (RHASH_AR_TABLE_SIZE(hash) + size <= RHASH_AR_TABLE_MAX_SIZE)) {
4739 ar_bulk_insert(hash, argc, argv);
4742 rb_hash_bulk_insert_into_st_table(argc, argv, hash);
4747static char **origenviron;
4749#define GET_ENVIRON(e) ((e) = rb_w32_get_environ())
4750#define FREE_ENVIRON(e) rb_w32_free_environ(e)
4751static char **my_environ;
4753#define environ my_environ
4755#define getenv(n) rb_w32_ugetenv(n)
4756#elif defined(__APPLE__)
4758#define environ (*_NSGetEnviron())
4759#define GET_ENVIRON(e) (e)
4760#define FREE_ENVIRON(e)
4762extern char **environ;
4763#define GET_ENVIRON(e) (e)
4764#define FREE_ENVIRON(e)
4766#ifdef ENV_IGNORECASE
4767#define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
4768#define ENVNMATCH(s1, s2, n) (STRNCASECMP((s1), (s2), (n)) == 0)
4770#define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
4771#define ENVNMATCH(s1, s2, n) (memcmp((s1), (s2), (n)) == 0)
4774#define ENV_LOCK() RB_VM_LOCK_ENTER()
4775#define ENV_UNLOCK() RB_VM_LOCK_LEAVE()
4781 return rb_utf8_encoding();
4783 return rb_locale_encoding();
4797env_str_new(
const char *ptr,
long len)
4799 return env_enc_str_new(ptr,
len, env_encoding());
4803env_str_new2(
const char *ptr)
4805 if (!ptr)
return Qnil;
4806 return env_str_new(ptr, strlen(ptr));
4810getenv_with_lock(
const char *name)
4815 const char *val = getenv(name);
4816 ret = env_str_new2(val);
4823has_env_with_lock(
const char *name)
4833 return val ? true :
false;
4836static const char TZ_ENV[] =
"TZ";
4839get_env_cstr(
VALUE str,
const char *name)
4843 if (!rb_enc_asciicompat(enc)) {
4844 rb_raise(rb_eArgError,
"bad environment variable %s: ASCII incompatible encoding: %s",
4845 name, rb_enc_name(enc));
4847 var = RSTRING_PTR(str);
4848 if (memchr(var,
'\0', RSTRING_LEN(str))) {
4849 rb_raise(rb_eArgError,
"bad environment variable %s: contains null byte", name);
4851 return rb_str_fill_terminator(str, 1);
4854#define get_env_ptr(var, val) \
4855 (var = get_env_cstr(val, #var))
4857static inline const char *
4858env_name(
volatile VALUE *s)
4862 get_env_ptr(name, *s);
4866#define env_name(s) env_name(&(s))
4871reset_by_modified_env(
const char *nam)
4879 if (ENVMATCH(nam, TZ_ENV)) {
4880 ruby_reset_timezone();
4885env_delete(
VALUE name)
4887 const char *nam = env_name(name);
4888 reset_by_modified_env(nam);
4889 VALUE val = getenv_with_lock(nam);
4892 ruby_setenv(nam, 0);
4927 val = env_delete(name);
4947 const char *nam = env_name(name);
4948 VALUE env = getenv_with_lock(nam);
4988 if (block_given && argc == 2) {
4989 rb_warn(
"block supersedes default value argument");
4991 nam = env_name(key);
4992 env = getenv_with_lock(nam);
4995 if (block_given)
return rb_yield(key);
4997 rb_key_err_raise(rb_sprintf(
"key not found: \"%"PRIsVALUE
"\"", key), envtbl, key);
5004#if defined(_WIN32) || (defined(HAVE_SETENV) && defined(HAVE_UNSETENV))
5007in_origenv(
const char *str)
5010 for (env = origenviron; *env; ++env) {
5011 if (*env == str)
return 1;
5017envix(
const char *nam)
5021 register int i,
len = strlen(nam);
5024 env = GET_ENVIRON(environ);
5025 for (i = 0; env[i]; i++) {
5026 if (ENVNMATCH(env[i],nam,
len) && env[i][
len] ==
'=')
5029 FREE_ENVIRON(environ);
5036getenvsize(
const WCHAR* p)
5038 const WCHAR* porg = p;
5039 while (*p++) p += lstrlenW(p) + 1;
5040 return p - porg + 1;
5044getenvblocksize(
void)
5054check_envsize(
size_t n)
5056 if (_WIN32_WINNT < 0x0600 && rb_w32_osver() < 6) {
5060 WCHAR* p = GetEnvironmentStringsW();
5063 FreeEnvironmentStringsW(p);
5064 if (n >= getenvblocksize()) {
5072#if defined(_WIN32) || \
5073 (defined(__sun) && !(defined(HAVE_SETENV) && defined(HAVE_UNSETENV)))
5075NORETURN(
static void invalid_envname(
const char *name));
5078invalid_envname(
const char *name)
5084check_envname(
const char *name)
5086 if (strchr(name,
'=')) {
5087 invalid_envname(name);
5094ruby_setenv(
const char *name,
const char *value)
5097# if defined(MINGW_HAS_SECURE_API) || RUBY_MSVCRT_VERSION >= 80
5098# define HAVE__WPUTENV_S 1
5105 check_envname(name);
5106 len = MultiByteToWideChar(CP_UTF8, 0, name, -1, NULL, 0);
5109 len2 = MultiByteToWideChar(CP_UTF8, 0, value, -1, NULL, 0);
5110 if (check_envsize((
size_t)
len + len2)) {
5114 wvalue = wname +
len;
5115 MultiByteToWideChar(CP_UTF8, 0, name, -1, wname,
len);
5116 MultiByteToWideChar(CP_UTF8, 0, value, -1, wvalue, len2);
5117#ifndef HAVE__WPUTENV_S
5118 wname[
len-1] = L
'=';
5123 MultiByteToWideChar(CP_UTF8, 0, name, -1, wname,
len);
5124 wvalue = wname +
len;
5126#ifndef HAVE__WPUTENV_S
5127 wname[
len-1] = L
'=';
5133#ifndef HAVE__WPUTENV_S
5134 failed = _wputenv(wname);
5136 failed = _wputenv_s(wname, wvalue);
5144 if (!value || !*value) {
5146 if (!SetEnvironmentVariable(name, value) &&
5147 GetLastError() != ERROR_ENVVAR_NOT_FOUND)
goto fail;
5151 invalid_envname(name);
5153#elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV)
5158 ret = setenv(name, value, 1);
5162 if (ret) rb_sys_fail_sprintf(
"setenv(%s)", name);
5175 ret = unsetenv(name);
5179 if (ret) rb_sys_fail_sprintf(
"unsetenv(%s)", name);
5187 size_t len, mem_size;
5188 char **env_ptr, *str, *mem_ptr;
5190 check_envname(name);
5193 mem_size =
len + strlen(value) + 2;
5194 mem_ptr = malloc(mem_size);
5195 if (mem_ptr == NULL)
5196 rb_sys_fail_sprintf(
"malloc(%"PRIuSIZE
")", mem_size);
5197 snprintf(mem_ptr, mem_size,
"%s=%s", name, value);
5202 for (env_ptr = GET_ENVIRON(environ); (str = *env_ptr) != 0; ++env_ptr) {
5203 if (!strncmp(str, name,
len) && str[
len] ==
'=') {
5204 if (!in_origenv(str)) free(str);
5205 while ((env_ptr[0] = env_ptr[1]) != 0) env_ptr++;
5216 ret = putenv(mem_ptr);
5222 rb_sys_fail_sprintf(
"putenv(%s)", name);
5233 if (environ == origenviron) {
5238 for (max = i; environ[max]; max++) ;
5239 tmpenv =
ALLOC_N(
char*, max+2);
5240 for (j=0; j<max; j++)
5247 char **envp = origenviron;
5248 while (*envp && *envp != environ[i]) envp++;
5252 while (environ[i]) {
5253 environ[i] = environ[i+1];
5260 if (!value)
goto finish;
5265 len = strlen(name) + strlen(value) + 2;
5267 snprintf(environ[i],
len,
"%s=%s",name,value);
5276ruby_unsetenv(
const char *name)
5278 ruby_setenv(name, 0);
5326 return env_aset(nm, val);
5342 get_env_ptr(name, nm);
5343 get_env_ptr(value, val);
5345 ruby_setenv(name, value);
5346 reset_by_modified_env(name);
5353 rb_encoding *enc = raw ? 0 : rb_locale_encoding();
5354 VALUE ary = rb_ary_new();
5358 char **env = GET_ENVIRON(environ);
5360 char *s = strchr(*env,
'=');
5362 const char *p = *env;
5365 rb_ary_push(ary, e);
5369 FREE_ENVIRON(environ);
5392 return env_keys(FALSE);
5403 env = GET_ENVIRON(environ);
5404 for (; *env ; ++env) {
5405 if (strchr(*env,
'=')) {
5409 FREE_ENVIRON(environ);
5434env_each_key(
VALUE ehash)
5440 keys = env_keys(FALSE);
5450 VALUE ary = rb_ary_new();
5454 char **env = GET_ENVIRON(environ);
5457 char *s = strchr(*env,
'=');
5459 rb_ary_push(ary, env_str_new2(s+1));
5463 FREE_ENVIRON(environ);
5485 return env_values();
5506env_each_value(
VALUE ehash)
5512 values = env_values();
5538env_each_pair(
VALUE ehash)
5544 VALUE ary = rb_ary_new();
5548 char **env = GET_ENVIRON(environ);
5551 char *s = strchr(*env,
'=');
5553 rb_ary_push(ary, env_str_new(*env, s-*env));
5554 rb_ary_push(ary, env_str_new2(s+1));
5558 FREE_ENVIRON(environ);
5562 if (rb_block_pair_yield_optimizable()) {
5599env_reject_bang(
VALUE ehash)
5606 keys = env_keys(FALSE);
5607 RBASIC_CLEAR_CLASS(keys);
5618 if (del == 0)
return Qnil;
5643env_delete_if(
VALUE ehash)
5646 env_reject_bang(ehash);
5673 result = rb_ary_new();
5674 for (i=0; i<argc; i++) {
5675 rb_ary_push(result, rb_f_getenv(
Qnil, argv[i]));
5700env_select(
VALUE ehash)
5707 result = rb_hash_new();
5708 keys = env_keys(FALSE);
5714 rb_hash_aset(result, key, val);
5759env_select_bang(
VALUE ehash)
5766 keys = env_keys(FALSE);
5767 RBASIC_CLEAR_CLASS(keys);
5778 if (del == 0)
return Qnil;
5801env_keep_if(
VALUE ehash)
5804 env_select_bang(ehash);
5824 VALUE key, value, result;
5827 return rb_hash_new();
5829 result = rb_hash_new_with_size(argc);
5831 for (i = 0; i < argc; i++) {
5833 value = rb_f_getenv(
Qnil, key);
5835 rb_hash_aset(result, key, value);
5847 keys = env_keys(TRUE);
5850 const char *nam = RSTRING_PTR(key);
5851 ruby_setenv(nam, 0);
5870 return rb_env_clear();
5902 char **env = GET_ENVIRON(environ);
5904 char *s = strchr(*env,
'=');
5906 if (env != environ) {
5914 rb_str_buf_append(str, i);
5918 FREE_ENVIRON(environ);
5939 VALUE ary = rb_ary_new();
5943 char **env = GET_ENVIRON(environ);
5945 char *s = strchr(*env,
'=');
5947 rb_ary_push(ary, rb_assoc_new(env_str_new(*env, s-*env),
5948 env_str_new2(s+1)));
5952 FREE_ENVIRON(environ);
5974env_size_with_lock(
void)
5980 char **env = GET_ENVIRON(environ);
5982 FREE_ENVIRON(environ);
6002 return INT2FIX(env_size_with_lock());
6022 char **env = GET_ENVIRON(environ);
6026 FREE_ENVIRON(environ);
6030 return RBOOL(empty);
6059 const char *s = env_name(key);
6060 return RBOOL(has_env_with_lock(s));
6086 const char *s = env_name(key);
6087 VALUE e = getenv_with_lock(s);
6090 return rb_assoc_new(key, e);
6119 char **env = GET_ENVIRON(environ);
6121 char *s = strchr(*env,
'=');
6123 long len = strlen(s);
6124 if (RSTRING_LEN(obj) ==
len && strncmp(s, RSTRING_PTR(obj),
len) == 0) {
6131 FREE_ENVIRON(environ);
6162 char **env = GET_ENVIRON(environ);
6165 const char *p = *env;
6166 char *s = strchr(p,
'=');
6168 long len = strlen(s);
6169 if (RSTRING_LEN(obj) ==
len && strncmp(s, RSTRING_PTR(obj),
len) == 0) {
6170 result = rb_assoc_new(
rb_str_new(p, s-p-1), obj);
6176 FREE_ENVIRON(environ);
6207 char **env = GET_ENVIRON(environ);
6209 char *s = strchr(*env,
'=');
6211 long len = strlen(s);
6212 if (RSTRING_LEN(value) ==
len && strncmp(s, RSTRING_PTR(value),
len) == 0) {
6213 str = env_str_new(*env, s-*env-1);
6219 FREE_ENVIRON(environ);
6229 VALUE hash = rb_hash_new();
6233 char **env = GET_ENVIRON(environ);
6235 char *s = strchr(*env,
'=');
6237 rb_hash_aset(hash, env_str_new(*env, s-*env),
6242 FREE_ENVIRON(environ);
6258 return env_to_hash();
6273 return env_to_hash();
6297 VALUE hash = env_to_hash();
6299 hash = rb_hash_to_h_block(hash);
6317 VALUE key, hash = env_to_hash();
6319 for (i = 0; i < argc; i++) {
6321 rb_hash_delete(hash, key);
6345 return rb_hash_delete_if(env_to_hash());
6348NORETURN(
static VALUE env_freeze(
VALUE self));
6357env_freeze(
VALUE self)
6386 char **env = GET_ENVIRON(environ);
6388 const char *p = *env;
6389 char *s = strchr(p,
'=');
6391 key = env_str_new(p, s-p);
6392 VALUE val = env_str_new2(getenv(RSTRING_PTR(key)));
6393 result = rb_assoc_new(key, val);
6396 FREE_ENVIRON(environ);
6425 return rb_hash_invert(env_to_hash());
6432 const char *keyptr, *eptr;
6440 if (elen != keylen)
continue;
6441 if (!ENVNMATCH(keyptr, eptr, elen))
continue;
6442 rb_ary_delete_at(keys, i);
6453 keylist_delete(keys, key);
6481 keys = env_keys(TRUE);
6482 if (env == hash)
return env;
6483 hash = to_hash(hash);
6484 rb_hash_foreach(hash, env_replace_i, keys);
6504 if (!
NIL_P(oldval)) {
6551 env_update_block_i : env_update_i;
6552 for (
int i = 0; i < argc; ++i) {
6553 VALUE hash = argv[i];
6554 if (env == hash)
continue;
6555 hash = to_hash(hash);
6556 rb_hash_foreach(hash, func, 0);
6576 rb_get_freeze_opt(1, &opt);
6580 rb_raise(
rb_eTypeError,
"Cannot clone ENV, use ENV.to_h to get a copy of ENV as a hash");
6594 rb_raise(
rb_eTypeError,
"Cannot dup ENV, use ENV.to_h to get a copy of ENV as a hash");
6605 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
7091 id_hash_iter_lev = rb_make_internal_id();
7353 origenviron = environ;
7423 ruby_register_rollback_func_for_ensure(hash_foreach_ensure, hash_foreach_ensure_rollback);
7425 HASH_ASSERT(
sizeof(ar_hint_t) * RHASH_AR_TABLE_MAX_SIZE ==
sizeof(
VALUE));
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
static bool RB_FL_ANY_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_ANY().
@ RUBY_FL_SHAREABLE
This flag has something to do with Ractor.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
void rb_extend_object(VALUE obj, VALUE module)
Extend the object with the module.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
int rb_block_given_p(void)
Determines if the current method is given a block.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define TYPE(_)
Old name of rb_type.
#define NEWOBJ_OF
Old name of RB_NEWOBJ_OF.
#define rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
#define FL_EXIVAR
Old name of RUBY_FL_EXIVAR.
#define NUM2LL
Old name of RB_NUM2LL.
#define REALLOC_N
Old name of RB_REALLOC_N.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define T_NIL
Old name of RUBY_T_NIL.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define rb_str_buf_new2
Old name of rb_str_buf_new_cstr.
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define T_DATA
Old name of RUBY_T_DATA.
#define LONG2FIX
Old name of RB_INT2FIX.
#define STATIC_SYM_P
Old name of RB_STATIC_SYM_P.
#define T_TRUE
Old name of RUBY_T_TRUE.
#define T_HASH
Old name of RUBY_T_HASH.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define rb_usascii_str_new2
Old name of rb_usascii_str_new_cstr.
#define T_FALSE
Old name of RUBY_T_FALSE.
#define FIXNUM_MIN
Old name of RUBY_FIXNUM_MIN.
#define FLONUM_P
Old name of RB_FLONUM_P.
#define Qtrue
Old name of RUBY_Qtrue.
#define ST2FIX
Old name of RB_ST2FIX.
#define FIXNUM_MAX
Old name of RUBY_FIXNUM_MAX.
#define NUM2INT
Old name of RB_NUM2INT.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
#define POSFIXABLE
Old name of RB_POSFIXABLE.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define FL_TEST
Old name of RB_FL_TEST.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define OBJ_WB_UNPROTECT
Old name of RB_OBJ_WB_UNPROTECT.
#define rb_ary_new2
Old name of rb_ary_new_capa.
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
void rb_syserr_fail_str(int e, VALUE mesg)
Identical to rb_syserr_fail(), except it takes the message in Ruby's String instead of C's.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_mKernel
Kernel module.
VALUE rb_any_to_s(VALUE obj)
Generates a textual representation of the given object.
VALUE rb_mEnumerable
Enumerable module.
int rb_eql(VALUE lhs, VALUE rhs)
Checks for equality of the passed objects, in terms of Object#eql?.
VALUE rb_cHash
Hash class.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
VALUE rb_cString
String class.
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
VALUE rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *enc)
Identical to rb_external_str_new(), except it additionally takes an encoding.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
#define RGENGC_WB_PROTECTED_HASH
This is a compile-time flag to enable/disable write barrier for struct RHash.
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Means either INTEGER_PACK_MSBYTE_FIRST or INTEGER_PACK_LSBYTE_FIRST, depending on the host processor'...
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
This roughly resembles return enum_for(__callee__) unless block_given?.
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
#define rb_check_frozen
Just another name of rb_check_frozen.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_hash_update_func(VALUE newkey, VALUE oldkey, VALUE value)
Type of callback functions to pass to rb_hash_update_by().
#define st_foreach_safe
Just another name of rb_st_foreach_safe.
VALUE rb_proc_lambda_p(VALUE recv)
Queries if the given object is a lambda.
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
VALUE rb_proc_call_with_block(VALUE recv, int argc, const VALUE *argv, VALUE proc)
Identical to rb_proc_call(), except you can additionally pass another proc object,...
int rb_proc_arity(VALUE recv)
Queries the number of mandatory arguments of the given Proc.
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
#define rb_hash_uint(h, i)
Just another name of st_hash_uint.
#define rb_hash_end(h)
Just another name of st_hash_end.
int rb_str_hash_cmp(VALUE str1, VALUE str2)
Compares two strings.
VALUE rb_str_ellipsize(VALUE str, long len)
Shortens str and adds three dots, an ellipsis, if it is longer than len characters.
st_index_t rb_memhash(const void *ptr, long len)
This is a universal hash function.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
#define rb_str_buf_cat
Just another name of rb_str_cat.
st_index_t rb_str_hash(VALUE str)
Calculates a hash value of a string.
st_index_t rb_hash_start(st_index_t i)
Starts a series of hashing.
VALUE rb_str_buf_cat_ascii(VALUE dst, const char *src)
Identical to rb_str_cat_cstr(), except it additionally assumes the source string be a NUL terminated ...
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
#define rb_utf8_str_new(str, len)
Identical to rb_str_new, except it generates a string of "UTF-8" encoding.
VALUE rb_exec_recursive(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
"Recursion" API entry point.
VALUE rb_exec_recursive_paired(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE p, VALUE h)
Identical to rb_exec_recursive(), except it checks for the recursion on the ordered pair of { g,...
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
void rb_define_global_const(const char *name, VALUE val)
Identical to rb_define_const(), except it defines that of "global", i.e.
int capa
Designed capacity of the buffer.
int len
Length of the buffer.
char * ruby_strdup(const char *str)
This is our own version of strdup(3) that uses ruby_xmalloc() instead of system malloc (benefits our ...
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
VALUE rb_yield_values(int n,...)
Identical to rb_yield(), except it takes variadic number of parameters and pass them to the block.
VALUE rb_yield_values2(int n, const VALUE *argv)
Identical to rb_yield_values(), except it takes the parameters as a C array instead of variadic argum...
VALUE rb_yield(VALUE val)
Yields the block.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE type(ANYARGS)
ANYARGS-ed function type.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
void rb_copy_generic_ivar(VALUE clone, VALUE obj)
Copies the list of instance variables.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_PTR_USE(ary, ptr_name, expr)
Declares a section of code where raw pointers are used.
#define RARRAY_AREF(a, i)
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define RBASIC(obj)
Convenient casting macro.
#define RHASH_SET_IFNONE(h, ifnone)
Destructively updates the default value of the hash.
#define RHASH_SIZE(h)
Queries the size of the hash.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
#define SafeStringValue(v)
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
@ RUBY_SPECIAL_SHIFT
Least significant 8 bits are reserved.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
VALUE flags
Per-object flags.
This is the struct that holds necessary info for a struct.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.