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/st.h"
39#include "internal/symbol.h"
40#include "internal/thread.h"
41#include "internal/time.h"
42#include "internal/vm.h"
46#include "ruby_assert.h"
74#define SET_DEFAULT(hash, ifnone) ( \
75 FL_UNSET_RAW(hash, RHASH_PROC_DEFAULT), \
76 RHASH_SET_IFNONE(hash, ifnone))
78#define SET_PROC_DEFAULT(hash, proc) set_proc_default(hash, proc)
80#define COPY_DEFAULT(hash, hash2) copy_default(RHASH(hash), RHASH(hash2))
83copy_default(
struct RHash *hash,
const struct RHash *hash2)
85 hash->basic.
flags &= ~RHASH_PROC_DEFAULT;
86 hash->basic.
flags |= hash2->basic.
flags & RHASH_PROC_DEFAULT;
110VALUE rb_cHash_empty_frozen;
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 = RSHIFT(RSYMBOL(a)->hashval, 1);
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);
216obj_any_hash(
VALUE obj)
222 if (cme && METHOD_ENTRY_BASIC(cme)) {
224 if (cme->def->type == VM_METHOD_TYPE_CFUNC && cme->def->body.cfunc.func == (rb_cfunc_t)rb_obj_hash) {
225 hval = rb_obj_hash(obj);
228 hval = rb_vm_call0(GET_EC(), obj, id_hash, 0, 0, cme, 0);
234 hval = rb_exec_recursive_outer_mid(hash_recursive, obj, 0, id_hash);
241 sign = rb_integer_pack(hval, &ul, 1,
sizeof(ul), 0,
259 return any_hash(a, obj_any_hash);
265 return LONG2FIX(any_hash(obj, obj_any_hash));
274static const uint64_t prime1 = ((uint64_t)0x2e0bb864 << 32) | 0xe9ea7df5;
275static const uint32_t prime2 = 0x830fcab9;
278static inline uint64_t
279mult_and_mix(uint64_t m1, uint64_t m2)
281#if defined HAVE_UINT128_T
282 uint128_t r = (uint128_t) m1 * (uint128_t) m2;
283 return (uint64_t) (r >> 64) ^ (uint64_t) r;
285 uint64_t hm1 = m1 >> 32, hm2 = m2 >> 32;
286 uint64_t lm1 = m1, lm2 = m2;
287 uint64_t v64_128 = hm1 * hm2;
288 uint64_t v32_96 = hm1 * lm2 + lm1 * hm2;
289 uint64_t v1_32 = lm1 * lm2;
291 return (v64_128 + (v32_96 >> 32)) ^ ((v32_96 << 32) + v1_32);
295static inline uint64_t
296key64_hash(uint64_t key, uint32_t seed)
298 return mult_and_mix(key + seed, prime1);
302#define st_index_hash(index) key64_hash(rb_hash_start(index), prime2)
305rb_objid_hash(st_index_t index)
307 return (
long)st_index_hash(index);
313 VALUE object_id = rb_obj_id(obj);
315 object_id = rb_big_hash(object_id);
317#if SIZEOF_LONG == SIZEOF_VOIDP
318 return (st_index_t)st_index_hash((st_index_t)
NUM2LONG(object_id));
319#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
320 return (st_index_t)st_index_hash((st_index_t)
NUM2LL(object_id));
355rb_obj_hash(
VALUE obj)
357 long hnum = any_hash(obj, objid_hash);
366#define rb_ident_cmp st_numcmp
369rb_ident_hash(st_data_t n)
378 n ^= dbl_to_index(rb_float_value(n));
382 return (st_index_t)st_index_hash((st_index_t)n);
385#define identhash rb_hashtype_ident
391#define RHASH_IDENTHASH_P(hash) (RHASH_TYPE(hash) == &identhash)
392#define RHASH_STRING_KEY_P(hash, key) (!RHASH_IDENTHASH_P(hash) && (rb_obj_class(key) == rb_cString))
394typedef st_index_t st_hash_t;
404#define RHASH_AR_TABLE_MAX_BOUND RHASH_AR_TABLE_MAX_SIZE
406#define RHASH_AR_TABLE_REF(hash, n) (&RHASH_AR_TABLE(hash)->pairs[n])
407#define RHASH_AR_CLEARED_HINT 0xff
409static inline st_hash_t
410ar_do_hash(st_data_t key)
412 return (st_hash_t)rb_any_hash(key);
415static inline ar_hint_t
416ar_do_hash_hint(st_hash_t hash_value)
418 return (ar_hint_t)hash_value;
421static inline ar_hint_t
422ar_hint(
VALUE hash,
unsigned int index)
424 return RHASH_AR_TABLE(hash)->ar_hint.ary[index];
428ar_hint_set_hint(
VALUE hash,
unsigned int index, ar_hint_t hint)
430 RHASH_AR_TABLE(hash)->ar_hint.ary[index] = hint;
434ar_hint_set(
VALUE hash,
unsigned int index, st_hash_t hash_value)
436 ar_hint_set_hint(hash, index, ar_do_hash_hint(hash_value));
440ar_clear_entry(
VALUE hash,
unsigned int index)
444 ar_hint_set_hint(hash, index, RHASH_AR_CLEARED_HINT);
448ar_cleared_entry(
VALUE hash,
unsigned int index)
450 if (ar_hint(hash, index) == RHASH_AR_CLEARED_HINT) {
455 return UNDEF_P(pair->key);
463ar_set_entry(
VALUE hash,
unsigned int index, st_data_t key, st_data_t val, st_hash_t hash_value)
468 ar_hint_set(hash, index, hash_value);
471#define RHASH_AR_TABLE_SIZE(h) (HASH_ASSERT(RHASH_AR_TABLE_P(h)), \
472 RHASH_AR_TABLE_SIZE_RAW(h))
474#define RHASH_AR_TABLE_BOUND_RAW(h) \
475 ((unsigned int)((RBASIC(h)->flags >> RHASH_AR_TABLE_BOUND_SHIFT) & \
476 (RHASH_AR_TABLE_BOUND_MASK >> RHASH_AR_TABLE_BOUND_SHIFT)))
478#define RHASH_ST_TABLE_SET(h, s) rb_hash_st_table_set(h, s)
479#define RHASH_TYPE(hash) (RHASH_AR_TABLE_P(hash) ? &objhash : RHASH_ST_TABLE(hash)->type)
481#define HASH_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(HASH_DEBUG, expr, #expr)
483static inline unsigned int
484RHASH_AR_TABLE_BOUND(
VALUE h)
486 HASH_ASSERT(RHASH_AR_TABLE_P(h));
487 const unsigned int bound = RHASH_AR_TABLE_BOUND_RAW(h);
488 HASH_ASSERT(bound <= RHASH_AR_TABLE_MAX_SIZE);
493#define hash_verify(hash) hash_verify_(hash, __FILE__, __LINE__)
496hash_verify_(
VALUE hash,
const char *file,
int line)
500 if (RHASH_AR_TABLE_P(hash)) {
501 unsigned i, n = 0, bound = RHASH_AR_TABLE_BOUND(hash);
503 for (i=0; i<bound; i++) {
505 if (!ar_cleared_entry(hash, i)) {
509 HASH_ASSERT(!UNDEF_P(k));
510 HASH_ASSERT(!UNDEF_P(v));
514 if (n != RHASH_AR_TABLE_SIZE(hash)) {
515 rb_bug(
"n:%u, RHASH_AR_TABLE_SIZE:%u", n, RHASH_AR_TABLE_SIZE(hash));
519 HASH_ASSERT(RHASH_ST_TABLE(hash) != NULL);
520 HASH_ASSERT(RHASH_AR_TABLE_SIZE_RAW(hash) == 0);
521 HASH_ASSERT(RHASH_AR_TABLE_BOUND_RAW(hash) == 0);
528#define hash_verify(h) ((void)0)
532RHASH_TABLE_EMPTY_P(
VALUE hash)
537#define RHASH_SET_ST_FLAG(h) FL_SET_RAW(h, RHASH_ST_TABLE_FLAG)
538#define RHASH_UNSET_ST_FLAG(h) FL_UNSET_RAW(h, RHASH_ST_TABLE_FLAG)
543 st_init_existing_table_with_size(RHASH_ST_TABLE(hash),
type, size);
544 RHASH_SET_ST_FLAG(hash);
550 HASH_ASSERT(st != NULL);
551 RHASH_SET_ST_FLAG(hash);
553 *RHASH_ST_TABLE(hash) = *st;
557RHASH_AR_TABLE_BOUND_SET(
VALUE h, st_index_t n)
559 HASH_ASSERT(RHASH_AR_TABLE_P(h));
560 HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_BOUND);
562 RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
563 RBASIC(h)->flags |= n << RHASH_AR_TABLE_BOUND_SHIFT;
567RHASH_AR_TABLE_SIZE_SET(
VALUE h, st_index_t n)
569 HASH_ASSERT(RHASH_AR_TABLE_P(h));
570 HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_SIZE);
572 RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
573 RBASIC(h)->flags |= n << RHASH_AR_TABLE_SIZE_SHIFT;
577HASH_AR_TABLE_SIZE_ADD(
VALUE h, st_index_t n)
579 HASH_ASSERT(RHASH_AR_TABLE_P(h));
581 RHASH_AR_TABLE_SIZE_SET(h, RHASH_AR_TABLE_SIZE(h) + n);
586#define RHASH_AR_TABLE_SIZE_INC(h) HASH_AR_TABLE_SIZE_ADD(h, 1)
589RHASH_AR_TABLE_SIZE_DEC(
VALUE h)
591 HASH_ASSERT(RHASH_AR_TABLE_P(h));
592 int new_size = RHASH_AR_TABLE_SIZE(h) - 1;
595 RHASH_AR_TABLE_SIZE_SET(h, new_size);
598 RHASH_AR_TABLE_SIZE_SET(h, 0);
599 RHASH_AR_TABLE_BOUND_SET(h, 0);
605RHASH_AR_TABLE_CLEAR(
VALUE h)
607 RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
608 RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
610 memset(RHASH_AR_TABLE(h), 0,
sizeof(
ar_table));
613NOINLINE(
static int ar_equal(
VALUE x,
VALUE y));
618 return rb_any_cmp(x, y) == 0;
622ar_find_entry_hint(
VALUE hash, ar_hint_t hint, st_data_t key)
624 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
625 const ar_hint_t *hints = RHASH_AR_TABLE(hash)->ar_hint.ary;
629 for (i = 0; i < bound; i++) {
630 if (hints[i] == hint) {
632 if (ar_equal(key, pair->key)) {
633 RB_DEBUG_COUNTER_INC(artable_hint_hit);
639 static char fname[256];
642 if (pid != getpid()) {
643 snprintf(fname,
sizeof(fname),
"/tmp/ruby-armiss.%d", pid = getpid());
644 if ((fp = fopen(fname,
"w")) == NULL) rb_bug(
"fopen");
647 st_hash_t h1 = ar_do_hash(key);
648 st_hash_t h2 = ar_do_hash(pair->key);
650 fprintf(fp,
"miss: hash_eq:%d hints[%d]:%02x hint:%02x\n"
652 " pair->key:%016lx %s\n",
653 h1 == h2, i, hints[i], hint,
654 h1, rb_obj_info(key), h2, rb_obj_info(pair->key));
656 RB_DEBUG_COUNTER_INC(artable_hint_miss);
660 RB_DEBUG_COUNTER_INC(artable_hint_notfound);
661 return RHASH_AR_TABLE_MAX_BOUND;
665ar_find_entry(
VALUE hash, st_hash_t hash_value, st_data_t key)
667 ar_hint_t hint = ar_do_hash_hint(hash_value);
668 return ar_find_entry_hint(hash, hint, key);
672hash_ar_free_and_clear_table(
VALUE hash)
674 RHASH_AR_TABLE_CLEAR(hash);
676 HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
677 HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
680void rb_st_add_direct_with_hash(
st_table *tab, st_data_t key, st_data_t value, st_hash_t hash);
682enum ar_each_key_type {
689ar_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)
691 for (
int i = 0; i < max; i++) {
695 case ar_each_key_copy:
696 dst_keys[i] = pair->key;
698 case ar_each_key_cmp:
699 if (dst_keys[i] != pair->key)
return 1;
701 case ar_each_key_insert:
702 if (UNDEF_P(pair->key))
continue;
703 rb_st_add_direct_with_hash(new_tab, pair->key, pair->val, hashes[i]);
712ar_force_convert_table(
VALUE hash,
const char *file,
int line)
714 if (RHASH_ST_TABLE_P(hash)) {
715 return RHASH_ST_TABLE(hash);
718 ar_table *ar = RHASH_AR_TABLE(hash);
719 st_hash_t hashes[RHASH_AR_TABLE_MAX_SIZE];
720 unsigned int bound, size;
724 st_data_t keys[RHASH_AR_TABLE_MAX_SIZE];
725 bound = RHASH_AR_TABLE_BOUND(hash);
726 size = RHASH_AR_TABLE_SIZE(hash);
727 ar_each_key(ar, bound, ar_each_key_copy, keys, NULL, NULL);
729 for (
unsigned int i = 0; i < bound; i++) {
731 hashes[i] = UNDEF_P(keys[i]) ? 0 : ar_do_hash(keys[i]);
735 if (UNLIKELY(!RHASH_AR_TABLE_P(hash)))
return RHASH_ST_TABLE(hash);
736 if (UNLIKELY(RHASH_AR_TABLE_BOUND(hash) != bound))
continue;
737 if (UNLIKELY(ar_each_key(ar, bound, ar_each_key_cmp, keys, NULL, NULL)))
continue;
743 st_init_existing_table_with_size(new_tab, &objhash, size);
744 ar_each_key(ar, bound, ar_each_key_insert, NULL, new_tab, hashes);
745 hash_ar_free_and_clear_table(hash);
746 RHASH_ST_TABLE_SET(hash, new_tab);
747 return RHASH_ST_TABLE(hash);
752ar_compact_table(
VALUE hash)
754 const unsigned bound = RHASH_AR_TABLE_BOUND(hash);
755 const unsigned size = RHASH_AR_TABLE_SIZE(hash);
764 for (i=0; i<bound; i++) {
765 if (ar_cleared_entry(hash, i)) {
767 for (; j<bound; j++) {
768 if (!ar_cleared_entry(hash, j)) {
770 ar_hint_set_hint(hash, i, (st_hash_t)ar_hint(hash, j));
771 ar_clear_entry(hash, j);
782 HASH_ASSERT(i<=bound);
784 RHASH_AR_TABLE_BOUND_SET(hash, size);
791ar_add_direct_with_hash(
VALUE hash, st_data_t key, st_data_t val, st_hash_t hash_value)
793 unsigned bin = RHASH_AR_TABLE_BOUND(hash);
795 if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_SIZE) {
799 if (UNLIKELY(bin >= RHASH_AR_TABLE_MAX_BOUND)) {
800 bin = ar_compact_table(hash);
802 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
804 ar_set_entry(hash, bin, key, val, hash_value);
805 RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
806 RHASH_AR_TABLE_SIZE_INC(hash);
812ensure_ar_table(
VALUE hash)
814 if (!RHASH_AR_TABLE_P(hash)) {
815 rb_raise(
rb_eRuntimeError,
"hash representation was changed during iteration");
820ar_general_foreach(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
822 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
823 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
825 for (i = 0; i < bound; i++) {
826 if (ar_cleared_entry(hash, i))
continue;
829 st_data_t key = (st_data_t)pair->key;
830 st_data_t val = (st_data_t)pair->val;
831 enum st_retval retval = (*func)(key, val, arg, 0);
832 ensure_ar_table(hash);
843 (*replace)(&key, &val, arg, TRUE);
846 HASH_ASSERT(pair == RHASH_AR_TABLE_REF(hash, i));
848 pair->key = (
VALUE)key;
849 pair->val = (
VALUE)val;
853 ar_clear_entry(hash, i);
854 RHASH_AR_TABLE_SIZE_DEC(hash);
863ar_foreach_with_replace(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
865 return ar_general_foreach(hash, func, replace, arg);
869 st_foreach_callback_func *func;
874apply_functor(st_data_t k, st_data_t v, st_data_t d,
int _)
876 const struct functor *f = (
void *)d;
877 return f->func(k, v, f->arg);
881ar_foreach(
VALUE hash, st_foreach_callback_func *func, st_data_t arg)
883 const struct functor f = { func, arg };
884 return ar_general_foreach(hash, apply_functor, NULL, (st_data_t)&f);
888ar_foreach_check(
VALUE hash, st_foreach_check_callback_func *func, st_data_t arg,
891 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
892 unsigned i, ret = 0, bound = RHASH_AR_TABLE_BOUND(hash);
893 enum st_retval retval;
898 for (i = 0; i < bound; i++) {
899 if (ar_cleared_entry(hash, i))
continue;
901 pair = RHASH_AR_TABLE_REF(hash, i);
903 hint = ar_hint(hash, i);
905 retval = (*func)(key, pair->val, arg, 0);
906 ensure_ar_table(hash);
911 pair = RHASH_AR_TABLE_REF(hash, i);
912 if (pair->key == never)
break;
913 ret = ar_find_entry_hint(hash, hint, key);
914 if (ret == RHASH_AR_TABLE_MAX_BOUND) {
915 (*func)(0, 0, arg, 1);
925 if (!ar_cleared_entry(hash, i)) {
926 ar_clear_entry(hash, i);
927 RHASH_AR_TABLE_SIZE_DEC(hash);
938ar_update(
VALUE hash, st_data_t key,
939 st_update_callback_func *func, st_data_t arg)
941 int retval, existing;
942 unsigned bin = RHASH_AR_TABLE_MAX_BOUND;
943 st_data_t value = 0, old_key;
944 st_hash_t hash_value = ar_do_hash(key);
946 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
951 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
952 bin = ar_find_entry(hash, hash_value, key);
953 existing = (bin != RHASH_AR_TABLE_MAX_BOUND) ? TRUE : FALSE;
965 retval = (*func)(&key, &value, arg, existing);
967 ensure_ar_table(hash);
972 if (ar_add_direct_with_hash(hash, key, value, hash_value)) {
978 if (old_key != key) {
986 ar_clear_entry(hash, bin);
987 RHASH_AR_TABLE_SIZE_DEC(hash);
995ar_insert(
VALUE hash, st_data_t key, st_data_t value)
997 unsigned bin = RHASH_AR_TABLE_BOUND(hash);
998 st_hash_t hash_value = ar_do_hash(key);
1000 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1005 bin = ar_find_entry(hash, hash_value, key);
1006 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1007 if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_SIZE) {
1010 else if (bin >= RHASH_AR_TABLE_MAX_BOUND) {
1011 bin = ar_compact_table(hash);
1013 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
1015 ar_set_entry(hash, bin, key, value, hash_value);
1016 RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
1017 RHASH_AR_TABLE_SIZE_INC(hash);
1021 RHASH_AR_TABLE_REF(hash, bin)->val = value;
1027ar_lookup(
VALUE hash, st_data_t key, st_data_t *value)
1029 if (RHASH_AR_TABLE_SIZE(hash) == 0) {
1033 st_hash_t hash_value = ar_do_hash(key);
1034 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1036 return st_lookup(RHASH_ST_TABLE(hash), key, value);
1038 unsigned bin = ar_find_entry(hash, hash_value, key);
1040 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1044 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
1045 if (value != NULL) {
1046 *value = RHASH_AR_TABLE_REF(hash, bin)->val;
1054ar_delete(
VALUE hash, st_data_t *key, st_data_t *value)
1057 st_hash_t hash_value = ar_do_hash(*key);
1059 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1061 return st_delete(RHASH_ST_TABLE(hash), key, value);
1064 bin = ar_find_entry(hash, hash_value, *key);
1066 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1067 if (value != 0) *value = 0;
1075 ar_clear_entry(hash, bin);
1076 RHASH_AR_TABLE_SIZE_DEC(hash);
1082ar_shift(
VALUE hash, st_data_t *key, st_data_t *value)
1084 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
1085 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1087 for (i = 0; i < bound; i++) {
1088 if (!ar_cleared_entry(hash, i)) {
1090 if (value != 0) *value = pair->val;
1092 ar_clear_entry(hash, i);
1093 RHASH_AR_TABLE_SIZE_DEC(hash);
1098 if (value != NULL) *value = 0;
1103ar_keys(
VALUE hash, st_data_t *keys, st_index_t size)
1105 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1106 st_data_t *keys_start = keys, *keys_end = keys + size;
1108 for (i = 0; i < bound; i++) {
1109 if (keys == keys_end) {
1113 if (!ar_cleared_entry(hash, i)) {
1114 *keys++ = RHASH_AR_TABLE_REF(hash, i)->key;
1119 return keys - keys_start;
1123ar_values(
VALUE hash, st_data_t *values, st_index_t size)
1125 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1126 st_data_t *values_start = values, *values_end = values + size;
1128 for (i = 0; i < bound; i++) {
1129 if (values == values_end) {
1133 if (!ar_cleared_entry(hash, i)) {
1134 *values++ = RHASH_AR_TABLE_REF(hash, i)->val;
1139 return values - values_start;
1145 ar_table *old_tab = RHASH_AR_TABLE(hash2);
1146 ar_table *new_tab = RHASH_AR_TABLE(hash1);
1148 *new_tab = *old_tab;
1149 RHASH_AR_TABLE(hash1)->ar_hint.word = RHASH_AR_TABLE(hash2)->ar_hint.word;
1150 RHASH_AR_TABLE_BOUND_SET(hash1, RHASH_AR_TABLE_BOUND(hash2));
1151 RHASH_AR_TABLE_SIZE_SET(hash1, RHASH_AR_TABLE_SIZE(hash2));
1153 rb_gc_writebarrier_remember(hash1);
1161 if (RHASH_AR_TABLE(hash) != NULL) {
1162 RHASH_AR_TABLE_SIZE_SET(hash, 0);
1163 RHASH_AR_TABLE_BOUND_SET(hash, 0);
1166 HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
1167 HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
1172hash_st_free(
VALUE hash)
1174 HASH_ASSERT(RHASH_ST_TABLE_P(hash));
1176 st_table *tab = RHASH_ST_TABLE(hash);
1179 xfree(tab->entries);
1183hash_st_free_and_clear_table(
VALUE hash)
1187 RHASH_ST_CLEAR(hash);
1191rb_hash_free(
VALUE hash)
1193 if (RHASH_ST_TABLE_P(hash)) {
1198typedef int st_foreach_func(st_data_t, st_data_t, st_data_t);
1202 st_foreach_func *func;
1207foreach_safe_i(st_data_t key, st_data_t value, st_data_t args,
int error)
1212 if (error)
return ST_STOP;
1213 status = (*arg->func)(key, value, arg->arg);
1214 if (status == ST_CONTINUE) {
1226 arg.func = (st_foreach_func *)func;
1228 if (st_foreach_check(table, foreach_safe_i, (st_data_t)&arg, 0)) {
1237 rb_foreach_func *func;
1242hash_iter_status_check(
int status)
1257hash_ar_foreach_iter(st_data_t key, st_data_t value, st_data_t argp,
int error)
1261 if (error)
return ST_STOP;
1263 int status = (*arg->func)((
VALUE)key, (
VALUE)value, arg->arg);
1265 return hash_iter_status_check(status);
1269hash_foreach_iter(st_data_t key, st_data_t value, st_data_t argp,
int error)
1273 if (error)
return ST_STOP;
1275 int status = (*arg->func)((
VALUE)key, (
VALUE)value, arg->arg);
1277 return hash_iter_status_check(status);
1281iter_lev_in_ivar(
VALUE hash)
1286 HASH_ASSERT(lev >= 0);
1287 return (
unsigned long)lev;
1293iter_lev_in_ivar_set(
VALUE hash,
unsigned long lev)
1295 HASH_ASSERT(lev >= RHASH_LEV_MAX);
1297 rb_ivar_set_internal(hash, id_hash_iter_lev,
LONG2FIX((
long)lev));
1300static inline unsigned long
1301iter_lev_in_flags(
VALUE hash)
1303 return (
unsigned long)((
RBASIC(hash)->flags >> RHASH_LEV_SHIFT) & RHASH_LEV_MAX);
1307iter_lev_in_flags_set(
VALUE hash,
unsigned long lev)
1309 HASH_ASSERT(lev <= RHASH_LEV_MAX);
1310 RBASIC(hash)->flags = ((
RBASIC(hash)->flags & ~RHASH_LEV_MASK) | ((
VALUE)lev << RHASH_LEV_SHIFT));
1314hash_iterating_p(
VALUE hash)
1316 return iter_lev_in_flags(hash) > 0;
1320hash_iter_lev_inc(
VALUE hash)
1322 unsigned long lev = iter_lev_in_flags(hash);
1323 if (lev == RHASH_LEV_MAX) {
1324 lev = iter_lev_in_ivar(hash) + 1;
1331 iter_lev_in_flags_set(hash, lev);
1332 if (lev < RHASH_LEV_MAX)
return;
1334 iter_lev_in_ivar_set(hash, lev);
1338hash_iter_lev_dec(
VALUE hash)
1340 unsigned long lev = iter_lev_in_flags(hash);
1341 if (lev == RHASH_LEV_MAX) {
1342 lev = iter_lev_in_ivar(hash);
1343 if (lev > RHASH_LEV_MAX) {
1344 iter_lev_in_ivar_set(hash, lev-1);
1347 rb_attr_delete(hash, id_hash_iter_lev);
1349 else if (lev == 0) {
1352 iter_lev_in_flags_set(hash, lev - 1);
1356hash_foreach_ensure(
VALUE hash)
1358 hash_iter_lev_dec(hash);
1364rb_hash_stlike_foreach(
VALUE hash, st_foreach_callback_func *func, st_data_t arg)
1366 if (RHASH_AR_TABLE_P(hash)) {
1367 return ar_foreach(hash, func, arg);
1370 return st_foreach(RHASH_ST_TABLE(hash), func, arg);
1376rb_hash_stlike_foreach_with_replace(
VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
1378 if (RHASH_AR_TABLE_P(hash)) {
1379 return ar_foreach_with_replace(hash, func, replace, arg);
1382 return st_foreach_with_replace(RHASH_ST_TABLE(hash), func, replace, arg);
1387hash_foreach_call(
VALUE arg)
1391 if (RHASH_AR_TABLE_P(hash)) {
1392 ret = ar_foreach_check(hash, hash_ar_foreach_iter,
1393 (st_data_t)arg, (st_data_t)
Qundef);
1395 else if (RHASH_ST_TABLE_P(hash)) {
1396 ret = st_foreach_check(RHASH_ST_TABLE(hash), hash_foreach_iter,
1397 (st_data_t)arg, (st_data_t)
Qundef);
1400 rb_raise(
rb_eRuntimeError,
"ret: %d, hash modified during iteration", ret);
1406rb_hash_foreach(
VALUE hash, rb_foreach_func *func,
VALUE farg)
1410 if (RHASH_TABLE_EMPTY_P(hash))
1413 arg.func = (rb_foreach_func *)func;
1416 hash_foreach_call((
VALUE)&arg);
1419 hash_iter_lev_inc(hash);
1420 rb_ensure(hash_foreach_call, (
VALUE)&arg, hash_foreach_ensure, hash);
1425void rb_st_compact_table(
st_table *tab);
1428compact_after_delete(
VALUE hash)
1430 if (!hash_iterating_p(hash) && RHASH_ST_TABLE_P(hash)) {
1431 rb_st_compact_table(RHASH_ST_TABLE(hash));
1439 const size_t size =
sizeof(
struct RHash) + (st ? sizeof(
st_table) : sizeof(ar_table));
1441 NEWOBJ_OF(hash, struct RHash, klass, T_HASH | wb | flags, size, 0);
1449hash_alloc(VALUE klass)
1456empty_hash_alloc(
VALUE klass)
1458 RUBY_DTRACE_CREATE_HOOK(HASH, 0);
1460 return hash_alloc(klass);
1472 if (rb_hash_compare_by_id_p(basis)) {
1473 return rb_hash_compare_by_id(hash);
1479rb_hash_new_with_size(st_index_t size)
1481 bool st = size > RHASH_AR_TABLE_MAX_SIZE;
1485 hash_st_table_init(ret, &objhash, size);
1494 return rb_hash_new_with_size((st_index_t)
capa);
1500 if (RHASH_AR_TABLE_P(hash)) {
1501 if (RHASH_AR_TABLE_P(ret)) {
1505 st_table *tab = RHASH_ST_TABLE(ret);
1506 st_init_existing_table_with_size(tab, &objhash, RHASH_AR_TABLE_SIZE(hash));
1508 int bound = RHASH_AR_TABLE_BOUND(hash);
1509 for (
int i = 0; i < bound; i++) {
1510 if (ar_cleared_entry(hash, i))
continue;
1513 st_add_direct(tab, pair->key, pair->val);
1522 RHASH_SET_ST_FLAG(ret);
1523 st_replace(RHASH_ST_TABLE(ret), RHASH_ST_TABLE(hash));
1525 rb_gc_writebarrier_remember(ret);
1531hash_dup_with_compare_by_id(
VALUE hash)
1534 if (RHASH_ST_TABLE_P(hash)) {
1535 RHASH_SET_ST_FLAG(dup);
1538 RHASH_UNSET_ST_FLAG(dup);
1541 return hash_copy(dup, hash);
1557 if (rb_obj_exivar_p(hash)) {
1564rb_hash_resurrect(
VALUE hash)
1571rb_hash_modify_check(
VALUE hash)
1573 rb_check_frozen(hash);
1577rb_hash_tbl_raw(
VALUE hash,
const char *file,
int line)
1579 return ar_force_convert_table(hash, file, line);
1583rb_hash_tbl(
VALUE hash,
const char *file,
int line)
1586 return rb_hash_tbl_raw(hash, file, line);
1590rb_hash_modify(
VALUE hash)
1592 rb_hash_modify_check(hash);
1595NORETURN(
static void no_new_key(
void));
1599 rb_raise(
rb_eRuntimeError,
"can't add a new key into hash during iteration");
1607#define NOINSERT_UPDATE_CALLBACK(func) \
1609func##_noinsert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
1611 if (!existing) no_new_key(); \
1612 return func(key, val, (struct update_arg *)arg, existing); \
1616func##_insert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
1618 return func(key, val, (struct update_arg *)arg, existing); \
1623 st_update_callback_func *func;
1629typedef int (*tbl_update_func)(st_data_t *, st_data_t *, st_data_t, int);
1632rb_hash_stlike_update(
VALUE hash, st_data_t key, st_update_callback_func *func, st_data_t arg)
1634 if (RHASH_AR_TABLE_P(hash)) {
1635 int result = ar_update(hash, key, func, arg);
1637 ar_force_convert_table(hash, __FILE__, __LINE__);
1644 return st_update(RHASH_ST_TABLE(hash), key, func, arg);
1648tbl_update_modify(st_data_t *key, st_data_t *val, st_data_t arg,
int existing)
1651 st_data_t old_key = *key;
1652 st_data_t old_value = *val;
1653 VALUE hash = p->hash;
1654 int ret = (p->func)(key, val, arg, existing);
1659 if (!existing || *key != old_key || *val != old_value) {
1660 rb_hash_modify(hash);
1667 rb_hash_modify(hash);
1675tbl_update(
VALUE hash,
VALUE key, tbl_update_func func, st_data_t optional_arg)
1678 .arg = optional_arg,
1685 int ret = rb_hash_stlike_update(hash, key, tbl_update_modify, (st_data_t)&arg);
1694#define UPDATE_CALLBACK(iter_p, func) ((iter_p) ? func##_noinsert : func##_insert)
1696#define RHASH_UPDATE_ITER(h, iter_p, key, func, a) do { \
1697 tbl_update((h), (key), UPDATE_CALLBACK(iter_p, func), (st_data_t)(a)); \
1700#define RHASH_UPDATE(hash, key, func, arg) \
1701 RHASH_UPDATE_ITER(hash, hash_iterating_p(hash), key, func, arg)
1709 if (n != 2 && (n >= 0 || n < -3)) {
1710 if (n < 0) n = -n-1;
1711 rb_raise(
rb_eTypeError,
"default_proc takes two arguments (2 for %d)", n);
1722 rb_hash_modify(hash);
1724 if (capa_value !=
INT2FIX(0)) {
1726 if (
capa > 0 &&
RHASH_SIZE(hash) == 0 && RHASH_AR_TABLE_P(hash)) {
1727 hash_st_table_init(hash, &objhash,
capa);
1731 if (!
NIL_P(block)) {
1732 if (ifnone_unset !=
Qtrue) {
1736 SET_PROC_DEFAULT(hash, block);
1787rb_hash_s_create(
int argc,
VALUE *argv,
VALUE klass)
1792 tmp = rb_hash_s_try_convert(
Qnil, argv[0]);
1798 tmp = rb_hash_to_a(tmp);
1801 hash = hash_alloc(klass);
1803 hash_copy(hash, tmp);
1814 hash = hash_alloc(klass);
1821 rb_raise(rb_eArgError,
"wrong element type %s at %ld (expected array)",
1822 rb_builtin_class_name(e), i);
1826 rb_raise(rb_eArgError,
"invalid number of elements (%ld for 1..2)",
1832 rb_hash_aset(hash, key, val);
1838 if (argc % 2 != 0) {
1839 rb_raise(rb_eArgError,
"odd number of arguments for Hash");
1842 hash = hash_alloc(klass);
1843 rb_hash_bulk_insert(argc, argv, hash);
1849rb_to_hash_type(
VALUE hash)
1851 return rb_convert_type_with_id(hash,
T_HASH,
"Hash", idTo_hash);
1853#define to_hash rb_to_hash_type
1858 return rb_check_convert_type_with_id(hash,
T_HASH,
"Hash", idTo_hash);
1874rb_hash_s_try_convert(
VALUE dummy,
VALUE hash)
1876 return rb_check_hash_type(hash);
1895rb_hash_s_ruby2_keywords_hash_p(
VALUE dummy,
VALUE hash)
1898 return RBOOL(RHASH(hash)->basic.flags & RHASH_PASS_AS_KEYWORDS);
1917rb_hash_s_ruby2_keywords_hash(
VALUE dummy,
VALUE hash)
1920 VALUE tmp = rb_hash_dup(hash);
1922 rb_hash_compare_by_id(tmp);
1924 RHASH(tmp)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
1936 if (RHASH_AR_TABLE_P(arg)) {
1937 ar_insert(arg, (st_data_t)key, (st_data_t)value);
1940 st_insert(RHASH_ST_TABLE(arg), (st_data_t)key, (st_data_t)value);
1959rb_hash_rehash(
VALUE hash)
1964 if (hash_iterating_p(hash)) {
1967 rb_hash_modify_check(hash);
1968 if (RHASH_AR_TABLE_P(hash)) {
1969 tmp = hash_alloc(0);
1970 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
1972 hash_ar_free_and_clear_table(hash);
1975 else if (RHASH_ST_TABLE_P(hash)) {
1976 st_table *old_tab = RHASH_ST_TABLE(hash);
1977 tmp = hash_alloc(0);
1979 hash_st_table_init(tmp, old_tab->type, old_tab->num_entries);
1980 tbl = RHASH_ST_TABLE(tmp);
1982 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
1985 RHASH_ST_TABLE_SET(hash, tbl);
1986 RHASH_ST_CLEAR(tmp);
1995 VALUE args[2] = {hash, key};
2000rb_hash_default_unredefined(
VALUE hash)
2004 return !!BASIC_OP_UNREDEFINED_P(BOP_DEFAULT, HASH_REDEFINED_OP_FLAG);
2007 return LIKELY(rb_method_basic_definition_p(klass, id_default));
2016 if (LIKELY(rb_hash_default_unredefined(hash))) {
2018 if (LIKELY(!
FL_TEST_RAW(hash, RHASH_PROC_DEFAULT)))
return ifnone;
2019 if (UNDEF_P(key))
return Qnil;
2020 return call_default_proc(ifnone, hash, key);
2028hash_stlike_lookup(
VALUE hash, st_data_t key, st_data_t *pval)
2032 if (RHASH_AR_TABLE_P(hash)) {
2033 return ar_lookup(hash, key, pval);
2036 extern st_index_t rb_iseq_cdhash_hash(
VALUE);
2038 RHASH_ST_TABLE(hash)->
type->hash == rb_ident_hash ||
2039 RHASH_ST_TABLE(hash)->
type->hash == rb_iseq_cdhash_hash);
2040 return st_lookup(RHASH_ST_TABLE(hash), key, pval);
2045rb_hash_stlike_lookup(
VALUE hash, st_data_t key, st_data_t *pval)
2047 return hash_stlike_lookup(hash, key, pval);
2072 if (hash_stlike_lookup(hash, key, &val)) {
2076 return rb_hash_default_value(hash, key);
2085 if (hash_stlike_lookup(hash, key, &val)) {
2096 return rb_hash_lookup2(hash, key,
Qnil);
2126rb_hash_fetch_m(
int argc,
VALUE *argv,
VALUE hash)
2136 if (block_given && argc == 2) {
2137 rb_warn(
"block supersedes default value argument");
2140 if (hash_stlike_lookup(hash, key, &val)) {
2147 else if (argc == 1) {
2153 rb_key_err_raise(rb_sprintf(
"key not found: %"PRIsVALUE, desc), hash, key);
2164 return rb_hash_fetch_m(1, &key, hash);
2188rb_hash_default(
int argc,
VALUE *argv,
VALUE hash)
2194 if (
FL_TEST(hash, RHASH_PROC_DEFAULT)) {
2195 if (argc == 0)
return Qnil;
2196 return call_default_proc(ifnone, hash, argv[0]);
2217 rb_hash_modify_check(hash);
2218 SET_DEFAULT(hash, ifnone);
2235rb_hash_default_proc(
VALUE hash)
2237 if (
FL_TEST(hash, RHASH_PROC_DEFAULT)) {
2258rb_hash_set_default_proc(
VALUE hash,
VALUE proc)
2262 rb_hash_modify_check(hash);
2264 SET_DEFAULT(hash, proc);
2267 b = rb_check_convert_type_with_id(proc,
T_DATA,
"Proc", idTo_proc);
2270 "wrong default_proc type %s (expected Proc)",
2274 SET_PROC_DEFAULT(hash, proc);
2314 rb_hash_foreach(hash, key_i, (
VALUE)args);
2320rb_hash_stlike_delete(
VALUE hash, st_data_t *pkey, st_data_t *pval)
2322 if (RHASH_AR_TABLE_P(hash)) {
2323 return ar_delete(hash, pkey, pval);
2326 return st_delete(RHASH_ST_TABLE(hash), pkey, pval);
2338 st_data_t ktmp = (st_data_t)key, val;
2340 if (rb_hash_stlike_delete(hash, &ktmp, &val)) {
2356 VALUE deleted_value = rb_hash_delete_entry(hash, key);
2358 if (!UNDEF_P(deleted_value)) {
2359 return deleted_value;
2405 rb_hash_modify_check(hash);
2406 val = rb_hash_delete_entry(hash, key);
2408 if (!UNDEF_P(val)) {
2409 compact_after_delete(hash);
2454rb_hash_shift(
VALUE hash)
2458 rb_hash_modify_check(hash);
2459 if (RHASH_AR_TABLE_P(hash)) {
2461 if (!hash_iterating_p(hash)) {
2462 if (ar_shift(hash, &var.key, &var.val)) {
2467 rb_hash_foreach(hash, shift_i_safe, (
VALUE)&var);
2468 if (!UNDEF_P(var.key)) {
2469 rb_hash_delete_entry(hash, var.key);
2474 if (RHASH_ST_TABLE_P(hash)) {
2476 if (!hash_iterating_p(hash)) {
2477 if (st_shift(RHASH_ST_TABLE(hash), &var.key, &var.val)) {
2482 rb_hash_foreach(hash, shift_i_safe, (
VALUE)&var);
2483 if (!UNDEF_P(var.key)) {
2484 rb_hash_delete_entry(hash, var.key);
2496 rb_hash_modify(hash);
2505 return rb_hash_size(hash);
2529 rb_hash_modify_check(hash);
2530 if (!RHASH_TABLE_EMPTY_P(hash)) {
2531 rb_hash_foreach(hash, delete_if_i, hash);
2532 compact_after_delete(hash);
2557rb_hash_reject_bang(
VALUE hash)
2562 rb_hash_modify(hash);
2564 if (!n)
return Qnil;
2565 rb_hash_foreach(hash, delete_if_i, hash);
2590rb_hash_reject(
VALUE hash)
2595 result = hash_dup_with_compare_by_id(hash);
2597 rb_hash_foreach(result, delete_if_i, result);
2598 compact_after_delete(result);
2617rb_hash_slice(
int argc,
VALUE *argv,
VALUE hash)
2620 VALUE key, value, result;
2623 return copy_compare_by_id(rb_hash_new(), hash);
2625 result = copy_compare_by_id(rb_hash_new_with_size(argc), hash);
2627 for (i = 0; i < argc; i++) {
2629 value = rb_hash_lookup2(hash, key,
Qundef);
2630 if (!UNDEF_P(value))
2631 rb_hash_aset(result, key, value);
2652rb_hash_except(
int argc,
VALUE *argv,
VALUE hash)
2657 result = hash_dup_with_compare_by_id(hash);
2659 for (i = 0; i < argc; i++) {
2661 rb_hash_delete(result, key);
2663 compact_after_delete(result);
2686rb_hash_values_at(
int argc,
VALUE *argv,
VALUE hash)
2691 for (i=0; i<argc; i++) {
2722rb_hash_fetch_values(
int argc,
VALUE *argv,
VALUE hash)
2727 for (i=0; i<argc; i++) {
2728 rb_ary_push(result, rb_hash_fetch(hash, argv[i]));
2737 rb_hash_modify(hash);
2760rb_hash_select(
VALUE hash)
2765 result = hash_dup_with_compare_by_id(hash);
2767 rb_hash_foreach(result, keep_if_i, result);
2768 compact_after_delete(result);
2794rb_hash_select_bang(
VALUE hash)
2799 rb_hash_modify_check(hash);
2801 if (!n)
return Qnil;
2802 rb_hash_foreach(hash, keep_if_i, hash);
2825rb_hash_keep_if(
VALUE hash)
2828 rb_hash_modify_check(hash);
2829 if (!RHASH_TABLE_EMPTY_P(hash)) {
2830 rb_hash_foreach(hash, keep_if_i, hash);
2853 rb_hash_modify_check(hash);
2855 if (hash_iterating_p(hash)) {
2856 rb_hash_foreach(hash, clear_i, 0);
2858 else if (RHASH_AR_TABLE_P(hash)) {
2862 st_clear(RHASH_ST_TABLE(hash));
2863 compact_after_delete(hash);
2870hash_aset(st_data_t *key, st_data_t *val,
struct update_arg *arg,
int existing)
2877rb_hash_key_str(
VALUE key)
2880 return rb_fstring(key);
2888hash_aset_str(st_data_t *key, st_data_t *val,
struct update_arg *arg,
int existing)
2891 *key = rb_hash_key_str(*key);
2893 return hash_aset(key, val, arg, existing);
2896NOINSERT_UPDATE_CALLBACK(hash_aset)
2897NOINSERT_UPDATE_CALLBACK(hash_aset_str)
2931 bool iter_p = hash_iterating_p(hash);
2933 rb_hash_modify(hash);
2935 if (!RHASH_STRING_KEY_P(hash, key)) {
2936 RHASH_UPDATE_ITER(hash, iter_p, key, hash_aset, val);
2939 RHASH_UPDATE_ITER(hash, iter_p, key, hash_aset_str, val);
2968 rb_hash_modify_check(hash);
2969 if (hash == hash2)
return hash;
2970 if (hash_iterating_p(hash)) {
2973 hash2 = to_hash(hash2);
2975 COPY_DEFAULT(hash, hash2);
2977 if (RHASH_AR_TABLE_P(hash)) {
2978 hash_ar_free_and_clear_table(hash);
2981 hash_st_free_and_clear_table(hash);
2984 hash_copy(hash, hash2);
3025rb_hash_empty_p(
VALUE hash)
3058rb_hash_each_value(
VALUE hash)
3061 rb_hash_foreach(hash, each_value_i, 0);
3092rb_hash_each_key(
VALUE hash)
3095 rb_hash_foreach(hash, each_key_i, 0);
3138rb_hash_each_pair(
VALUE hash)
3141 if (rb_block_pair_yield_optimizable())
3142 rb_hash_foreach(hash, each_pair_i_fast, 0);
3144 rb_hash_foreach(hash, each_pair_i, 0);
3158 VALUE trans = p->trans, result = p->result;
3159 VALUE new_key = rb_hash_lookup2(trans, key,
Qundef);
3160 if (UNDEF_P(new_key)) {
3166 rb_hash_aset(result, new_key, value);
3174 rb_hash_aset(result, new_key, value);
3267rb_hash_transform_keys(
int argc,
VALUE *argv,
VALUE hash)
3274 transarg.trans = to_hash(argv[0]);
3280 result = rb_hash_new();
3282 if (transarg.trans) {
3283 transarg.result = result;
3284 rb_hash_foreach(hash, transform_keys_hash_i, (
VALUE)&transarg);
3287 rb_hash_foreach(hash, transform_keys_i, result);
3391rb_hash_transform_keys_bang(
int argc,
VALUE *argv,
VALUE hash)
3394 int block_given = 0;
3398 trans = to_hash(argv[0]);
3404 rb_hash_modify_check(hash);
3405 if (!RHASH_TABLE_EMPTY_P(hash)) {
3407 VALUE new_keys = hash_alloc(0);
3409 rb_hash_foreach(hash, flatten_i, pairs);
3416 else if (!UNDEF_P(new_key = rb_hash_lookup2(trans, key,
Qundef))) {
3419 else if (block_given) {
3426 if (!hash_stlike_lookup(new_keys, key, NULL)) {
3427 rb_hash_stlike_delete(hash, &key, NULL);
3429 rb_hash_aset(hash, new_key, val);
3430 rb_hash_aset(new_keys, new_key,
Qnil);
3433 rb_hash_clear(new_keys);
3435 compact_after_delete(hash);
3440transform_values_foreach_func(st_data_t key, st_data_t value, st_data_t argp,
int error)
3446transform_values_foreach_replace(st_data_t *key, st_data_t *value, st_data_t argp,
int existing)
3450 rb_hash_modify(hash);
3456transform_values_call(
VALUE hash)
3458 rb_hash_stlike_foreach_with_replace(hash, transform_values_foreach_func, transform_values_foreach_replace, hash);
3463transform_values(
VALUE hash)
3465 hash_iter_lev_inc(hash);
3466 rb_ensure(transform_values_call, hash, hash_foreach_ensure, hash);
3488rb_hash_transform_values(
VALUE hash)
3493 result = hash_dup_with_compare_by_id(hash);
3494 SET_DEFAULT(result,
Qnil);
3497 transform_values(result);
3498 compact_after_delete(result);
3526rb_hash_transform_values_bang(
VALUE hash)
3529 rb_hash_modify_check(hash);
3531 if (!RHASH_TABLE_EMPTY_P(hash)) {
3532 transform_values(hash);
3559rb_hash_to_a(
VALUE hash)
3564 rb_hash_foreach(hash, to_a_i, ary);
3570symbol_key_needs_quote(
VALUE str)
3572 long len = RSTRING_LEN(str);
3573 if (
len == 0 || !rb_str_symname_p(str))
return true;
3574 const char *s = RSTRING_PTR(str);
3576 if (first ==
'@' || first ==
'$' || first ==
'!')
return true;
3577 if (!at_char_boundary(s, s +
len - 1,
RSTRING_END(str), rb_enc_get(str)))
return false;
3578 switch (s[
len - 1]) {
3609 quote = symbol_key_needs_quote(str2);
3614 if (RSTRING_LEN(str) > 1) {
3618 rb_enc_copy(str, str2);
3635inspect_hash(
VALUE hash,
VALUE dummy,
int recur)
3641 rb_hash_foreach(hash, inspect_i, str);
3660rb_hash_inspect(
VALUE hash)
3676rb_hash_to_hash(
VALUE hash)
3688 rb_raise(
rb_eTypeError,
"wrong element type %s (expected array)",
3689 rb_builtin_class_name(arg));
3692 rb_raise(rb_eArgError,
"element has wrong array length (expected 2, was %ld)",
3707rb_hash_to_h_block(
VALUE hash)
3710 rb_hash_foreach(hash, to_h_i, h);
3735rb_hash_to_h(
VALUE hash)
3738 return rb_hash_to_h_block(hash);
3742 hash = hash_dup(hash,
rb_cHash, flags & RHASH_PROC_DEFAULT);
3767rb_hash_keys(
VALUE hash)
3772 if (size == 0)
return keys;
3774 if (ST_DATA_COMPATIBLE_P(
VALUE)) {
3776 if (RHASH_AR_TABLE_P(hash)) {
3777 size = ar_keys(hash, ptr, size);
3780 st_table *table = RHASH_ST_TABLE(hash);
3781 size = st_keys(table, ptr, size);
3784 rb_gc_writebarrier_remember(keys);
3785 rb_ary_set_len(keys, size);
3788 rb_hash_foreach(hash, keys_i, keys);
3814rb_hash_values(
VALUE hash)
3820 if (size == 0)
return values;
3822 if (ST_DATA_COMPATIBLE_P(
VALUE)) {
3823 if (RHASH_AR_TABLE_P(hash)) {
3824 rb_gc_writebarrier_remember(values);
3826 size = ar_values(hash, ptr, size);
3829 else if (RHASH_ST_TABLE_P(hash)) {
3830 st_table *table = RHASH_ST_TABLE(hash);
3831 rb_gc_writebarrier_remember(values);
3833 size = st_values(table, ptr, size);
3836 rb_ary_set_len(values, size);
3839 rb_hash_foreach(hash, values_i, values);
3861 return RBOOL(hash_stlike_lookup(hash, key, NULL));
3892 rb_hash_foreach(hash, rb_hash_search_value, (
VALUE)data);
3908 if (!hash_stlike_lookup(data->hash, key, &val2)) {
3922recursive_eql(
VALUE hash,
VALUE dt,
int recur)
3926 if (recur)
return Qtrue;
3928 data->result =
Qtrue;
3929 rb_hash_foreach(hash, eql_i, dt);
3931 return data->result;
3939 if (hash1 == hash2)
return Qtrue;
3945 if (
rb_eql(hash2, hash1)) {
3958 if (!RHASH_TABLE_EMPTY_P(hash1) && !RHASH_TABLE_EMPTY_P(hash2)) {
3959 if (RHASH_TYPE(hash1) != RHASH_TYPE(hash2)) {
3971 FL_TEST(hash1, RHASH_PROC_DEFAULT) ==
FL_TEST(hash2, RHASH_PROC_DEFAULT)))
4007 return hash_equal(hash1, hash2, FALSE);
4034 return hash_equal(hash1, hash2, TRUE);
4040 st_index_t *hval = (st_index_t *)arg;
4041 st_index_t hdata[2];
4043 hdata[0] = rb_hash(key);
4044 hdata[1] = rb_hash(val);
4045 *hval ^= st_hash(hdata,
sizeof(hdata), 0);
4067rb_hash_hash(
VALUE hash)
4073 rb_hash_foreach(hash, hash_i, (
VALUE)&hval);
4082 rb_hash_aset(hash, value, key);
4106rb_hash_invert(
VALUE hash)
4110 rb_hash_foreach(hash, rb_hash_invert_i, h);
4117 rb_hash_aset(hash, key, value);
4122 VALUE hash, newvalue, *argv;
4129rb_hash_update_block_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
4133 VALUE newvalue = ua->newvalue, hash = arg->hash;
4136 hash_iter_lev_inc(hash);
4137 ua->iterating =
true;
4139 hash_iter_lev_dec(hash);
4140 ua->iterating =
false;
4142 else if (RHASH_STRING_KEY_P(hash, k) && !
RB_OBJ_FROZEN(k)) {
4143 *key = (st_data_t)rb_hash_key_str(k);
4145 *value = (st_data_t)newvalue;
4149NOINSERT_UPDATE_CALLBACK(rb_hash_update_block_callback)
4155 ua->newvalue = value;
4156 RHASH_UPDATE(ua->hash, key, rb_hash_update_block_callback, args);
4161rb_hash_update_call(
VALUE args)
4165 for (
int i = 0; i < arg->argc; i++){
4166 VALUE hash = to_hash(arg->argv[i]);
4167 if (arg->block_given) {
4168 rb_hash_foreach(hash, rb_hash_update_block_i, args);
4171 rb_hash_foreach(hash, rb_hash_update_i, arg->hash);
4178rb_hash_update_ensure(
VALUE args)
4181 if (ua->iterating) hash_iter_lev_dec(ua->hash);
4229rb_hash_update(
int argc,
VALUE *argv,
VALUE self)
4240 rb_hash_modify(self);
4241 return rb_ensure(rb_hash_update_call, arg, rb_hash_update_ensure, arg);
4251rb_hash_update_func_callback(st_data_t *key, st_data_t *value,
struct update_arg *arg,
int existing)
4254 VALUE newvalue = uf_arg->value;
4257 newvalue = (*uf_arg->func)((
VALUE)*key, (
VALUE)*value, newvalue);
4263NOINSERT_UPDATE_CALLBACK(rb_hash_update_func_callback)
4269 VALUE hash = arg->hash;
4272 RHASH_UPDATE(hash, key, rb_hash_update_func_callback, (
VALUE)arg);
4279 rb_hash_modify(hash1);
4280 hash2 = to_hash(hash2);
4285 rb_hash_foreach(hash2, rb_hash_update_func_i, (
VALUE)&arg);
4288 rb_hash_foreach(hash2, rb_hash_update_i, hash1);
4340rb_hash_merge(
int argc,
VALUE *argv,
VALUE self)
4342 return rb_hash_update(argc, argv, copy_compare_by_id(rb_hash_dup(self), self));
4357assoc_lookup(
VALUE arg)
4361 if (st_lookup(p->tbl, p->key, &data))
return (
VALUE)data;
4399 if (RHASH_ST_TABLE_P(hash) && !RHASH_IDENTHASH_P(hash)) {
4401 st_table assoctable = *RHASH_ST_TABLE(hash);
4403 .compare = assoc_cmp,
4404 .hash = assoctable.type->hash,
4408 .key = (st_data_t)key,
4412 value = assoc_lookup(arg);
4415 hash_iter_lev_inc(hash);
4416 value =
rb_ensure(assoc_lookup, arg, hash_foreach_ensure, hash);
4424 rb_hash_foreach(hash, assoc_i, (
VALUE)args);
4463 rb_hash_foreach(hash, rassoc_i, (
VALUE)args);
4516rb_hash_flatten(
int argc,
VALUE *argv,
VALUE hash)
4525 if (level == 0)
return rb_hash_to_a(hash);
4528 rb_hash_foreach(hash, flatten_i, ary);
4533 rb_funcallv(ary, id_flatten_bang, 1, &ary_flatten_level);
4535 else if (level < 0) {
4537 rb_funcallv(ary, id_flatten_bang, 0, 0);
4542 rb_hash_foreach(hash, flatten_i, ary);
4570rb_hash_compact(
VALUE hash)
4572 VALUE result = rb_hash_dup(hash);
4574 rb_hash_foreach(result, delete_if_nil, result);
4575 compact_after_delete(result);
4577 else if (rb_hash_compare_by_id_p(hash)) {
4578 result = rb_hash_compare_by_id(result);
4600rb_hash_compact_bang(
VALUE hash)
4603 rb_hash_modify_check(hash);
4606 rb_hash_foreach(hash, delete_if_nil, hash);
4640rb_hash_compare_by_id(
VALUE hash)
4645 if (rb_hash_compare_by_id_p(hash))
return hash;
4647 rb_hash_modify_check(hash);
4648 if (hash_iterating_p(hash)) {
4652 if (RHASH_TABLE_EMPTY_P(hash)) {
4655 ar_force_convert_table(hash, __FILE__, __LINE__);
4656 HASH_ASSERT(RHASH_ST_TABLE_P(hash));
4658 RHASH_ST_TABLE(hash)->type = &identhash;
4663 tmp = hash_alloc(0);
4664 hash_st_table_init(tmp, &identhash,
RHASH_SIZE(hash));
4665 identtable = RHASH_ST_TABLE(tmp);
4667 rb_hash_foreach(hash, rb_hash_rehash_i, (
VALUE)tmp);
4672 RHASH_ST_TABLE_SET(hash, identtable);
4673 RHASH_ST_CLEAR(tmp);
4695rb_hash_compare_by_id_p(
VALUE hash)
4697 return RBOOL(RHASH_IDENTHASH_P(hash));
4701rb_ident_hash_new(
void)
4703 VALUE hash = rb_hash_new();
4704 hash_st_table_init(hash, &identhash, 0);
4709rb_ident_hash_new_with_size(st_index_t size)
4711 VALUE hash = rb_hash_new();
4712 hash_st_table_init(hash, &identhash, size);
4717rb_init_identtable(
void)
4719 return st_init_table(&identhash);
4796rb_hash_any_p(
int argc,
VALUE *argv,
VALUE hash)
4805 rb_warn(
"given block not used");
4809 rb_hash_foreach(hash, any_p_i_pattern, (
VALUE)args);
4816 if (rb_block_pair_yield_optimizable())
4817 rb_hash_foreach(hash, any_p_i_fast, (
VALUE)args);
4819 rb_hash_foreach(hash, any_p_i, (
VALUE)args);
4860rb_hash_dig(
int argc,
VALUE *argv,
VALUE self)
4863 self = rb_hash_aref(self, *argv);
4864 if (!--argc)
return self;
4866 return rb_obj_dig(argc, argv, self,
Qnil);
4874 if (!UNDEF_P(v) &&
rb_equal(value, v))
return ST_CONTINUE;
4885 rb_hash_foreach(hash1, hash_le_i, (
VALUE)args);
4911 other = to_hash(other);
4913 return hash_le(hash, other);
4940 other = to_hash(other);
4942 return hash_le(hash, other);
4967 other = to_hash(other);
4969 return hash_le(other, hash);
4996 other = to_hash(other);
4998 return hash_le(other, hash);
5005 return rb_hash_aref(hash, *argv);
5024rb_hash_to_proc(
VALUE hash)
5026 return rb_func_lambda_new(hash_proc_call, hash, 1, 1);
5031rb_hash_deconstruct_keys(
VALUE hash,
VALUE keys)
5037add_new_i(st_data_t *key, st_data_t *val, st_data_t arg,
int existing)
5039 if (existing)
return ST_STOP;
5054 if (RHASH_AR_TABLE_P(hash)) {
5055 ret = ar_update(hash, (st_data_t)key, add_new_i, (st_data_t)val);
5057 ar_force_convert_table(hash, __FILE__, __LINE__);
5062 tbl = RHASH_TBL_RAW(hash);
5063 ret = st_update(tbl, (st_data_t)key, add_new_i, (st_data_t)val);
5074key_stringify(
VALUE key)
5077 rb_hash_key_str(key) : key;
5081ar_bulk_insert(
VALUE hash,
long argc,
const VALUE *argv)
5084 for (i = 0; i < argc; ) {
5085 st_data_t k = key_stringify(argv[i++]);
5086 st_data_t v = argv[i++];
5087 ar_insert(hash, k, v);
5096 HASH_ASSERT(argc % 2 == 0);
5098 st_index_t size = argc / 2;
5100 if (RHASH_AR_TABLE_P(hash) &&
5101 (RHASH_AR_TABLE_SIZE(hash) + size <= RHASH_AR_TABLE_MAX_SIZE)) {
5102 ar_bulk_insert(hash, argc, argv);
5105 rb_hash_bulk_insert_into_st_table(argc, argv, hash);
5110static char **origenviron;
5112#define GET_ENVIRON(e) ((e) = rb_w32_get_environ())
5113#define FREE_ENVIRON(e) rb_w32_free_environ(e)
5114static char **my_environ;
5116#define environ my_environ
5118#define getenv(n) rb_w32_ugetenv(n)
5119#elif defined(__APPLE__)
5121#define environ (*_NSGetEnviron())
5122#define GET_ENVIRON(e) (e)
5123#define FREE_ENVIRON(e)
5125extern char **environ;
5126#define GET_ENVIRON(e) (e)
5127#define FREE_ENVIRON(e)
5129#ifdef ENV_IGNORECASE
5130#define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
5131#define ENVNMATCH(s1, s2, n) (STRNCASECMP((s1), (s2), (n)) == 0)
5133#define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
5134#define ENVNMATCH(s1, s2, n) (memcmp((s1), (s2), (n)) == 0)
5137#define ENV_LOCKING() RB_VM_LOCKING()
5143 return rb_utf8_encoding();
5145 return rb_locale_encoding();
5161 return env_enc_str_new(ptr,
len, enc);
5167 if (!ptr)
return Qnil;
5168 return env_str_new(ptr, strlen(ptr), enc);
5172getenv_with_lock(
const char *name)
5177 const char *val = getenv(name);
5178 ret = env_str_new2(val, enc);
5184has_env_with_lock(
const char *name)
5192 return val ? true :
false;
5195static const char TZ_ENV[] =
"TZ";
5198get_env_cstr(
VALUE str,
const char *name)
5202 if (!rb_enc_asciicompat(enc)) {
5203 rb_raise(rb_eArgError,
"bad environment variable %s: ASCII incompatible encoding: %s",
5204 name, rb_enc_name(enc));
5206 var = RSTRING_PTR(str);
5207 if (memchr(var,
'\0', RSTRING_LEN(str))) {
5208 rb_raise(rb_eArgError,
"bad environment variable %s: contains null byte", name);
5210 return rb_str_fill_terminator(str, 1);
5213#define get_env_ptr(var, val) \
5214 (var = get_env_cstr(val, #var))
5216static inline const char *
5217env_name(
volatile VALUE *s)
5221 get_env_ptr(name, *s);
5225#define env_name(s) env_name(&(s))
5230reset_by_modified_env(
const char *nam,
const char *val)
5238 if (ENVMATCH(nam, TZ_ENV)) {
5239 ruby_reset_timezone(val);
5244env_delete(
VALUE name)
5246 const char *nam = env_name(name);
5247 reset_by_modified_env(nam, NULL);
5248 VALUE val = getenv_with_lock(nam);
5251 ruby_setenv(nam, 0);
5286 val = env_delete(name);
5306 const char *nam = env_name(name);
5307 VALUE env = getenv_with_lock(nam);
5347 if (block_given && argc == 2) {
5348 rb_warn(
"block supersedes default value argument");
5350 nam = env_name(key);
5351 env = getenv_with_lock(nam);
5354 if (block_given)
return rb_yield(key);
5356 rb_key_err_raise(rb_sprintf(
"key not found: \"%"PRIsVALUE
"\"", key), envtbl, key);
5363#if defined(_WIN32) || (defined(HAVE_SETENV) && defined(HAVE_UNSETENV))
5366in_origenv(
const char *str)
5369 for (env = origenviron; *env; ++env) {
5370 if (*env == str)
return 1;
5376envix(
const char *nam)
5380 register int i,
len = strlen(nam);
5383 env = GET_ENVIRON(environ);
5384 for (i = 0; env[i]; i++) {
5385 if (ENVNMATCH(env[i],nam,
len) && env[i][
len] ==
'=')
5388 FREE_ENVIRON(environ);
5393#if defined(_WIN32) || \
5394 (defined(__sun) && !(defined(HAVE_SETENV) && defined(HAVE_UNSETENV)))
5396NORETURN(
static void invalid_envname(
const char *name));
5399invalid_envname(
const char *name)
5405check_envname(
const char *name)
5407 if (strchr(name,
'=')) {
5408 invalid_envname(name);
5415ruby_setenv(
const char *name,
const char *value)
5423 check_envname(name);
5424 len = MultiByteToWideChar(CP_UTF8, 0, name, -1, NULL, 0);
5427 len2 = MultiByteToWideChar(CP_UTF8, 0, value, -1, NULL, 0);
5429 wvalue = wname +
len;
5430 MultiByteToWideChar(CP_UTF8, 0, name, -1, wname,
len);
5431 MultiByteToWideChar(CP_UTF8, 0, value, -1, wvalue, len2);
5435 MultiByteToWideChar(CP_UTF8, 0, name, -1, wname,
len);
5436 wvalue = wname +
len;
5443 failed = _wputenv_s(wname, wvalue);
5449 if (!value || !*value) {
5451 if (!SetEnvironmentVariableW(wname, value ? wvalue : NULL) &&
5452 GetLastError() != ERROR_ENVVAR_NOT_FOUND)
goto fail;
5456 invalid_envname(name);
5458#elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV)
5462 ret = setenv(name, value, 1);
5465 if (ret) rb_sys_fail_sprintf(
"setenv(%s)", name);
5475 ret = unsetenv(name);
5478 if (ret) rb_sys_fail_sprintf(
"unsetenv(%s)", name);
5486 size_t len, mem_size;
5487 char **env_ptr, *str, *mem_ptr;
5489 check_envname(name);
5492 mem_size =
len + strlen(value) + 2;
5493 mem_ptr = malloc(mem_size);
5494 if (mem_ptr == NULL)
5495 rb_sys_fail_sprintf(
"malloc(%"PRIuSIZE
")", mem_size);
5496 snprintf(mem_ptr, mem_size,
"%s=%s", name, value);
5500 for (env_ptr = GET_ENVIRON(environ); (str = *env_ptr) != 0; ++env_ptr) {
5501 if (!strncmp(str, name,
len) && str[
len] ==
'=') {
5502 if (!in_origenv(str)) free(str);
5503 while ((env_ptr[0] = env_ptr[1]) != 0) env_ptr++;
5512 ret = putenv(mem_ptr);
5517 rb_sys_fail_sprintf(
"putenv(%s)", name);
5527 if (environ == origenviron) {
5532 for (max = i; environ[max]; max++) ;
5533 tmpenv =
ALLOC_N(
char*, max+2);
5534 for (j=0; j<max; j++)
5541 char **envp = origenviron;
5542 while (*envp && *envp != environ[i]) envp++;
5546 while (environ[i]) {
5547 environ[i] = environ[i+1];
5554 if (!value)
goto finish;
5559 len = strlen(name) + strlen(value) + 2;
5561 snprintf(environ[i],
len,
"%s=%s",name,value);
5569ruby_unsetenv(
const char *name)
5571 ruby_setenv(name, 0);
5619 return env_aset(nm, val);
5635 get_env_ptr(name, nm);
5636 get_env_ptr(value, val);
5638 ruby_setenv(name, value);
5639 reset_by_modified_env(name, value);
5646 rb_encoding *enc = raw ? 0 : rb_locale_encoding();
5650 char **env = GET_ENVIRON(environ);
5652 char *s = strchr(*env,
'=');
5654 const char *p = *env;
5661 FREE_ENVIRON(environ);
5683 return env_keys(FALSE);
5693 env = GET_ENVIRON(environ);
5694 for (; *env ; ++env) {
5695 if (strchr(*env,
'=')) {
5699 FREE_ENVIRON(environ);
5723env_each_key(
VALUE ehash)
5729 keys = env_keys(FALSE);
5743 char **env = GET_ENVIRON(environ);
5746 char *s = strchr(*env,
'=');
5752 FREE_ENVIRON(environ);
5773 return env_values();
5794env_each_value(
VALUE ehash)
5800 values = env_values();
5826env_each_pair(
VALUE ehash)
5836 char **env = GET_ENVIRON(environ);
5839 char *s = strchr(*env,
'=');
5846 FREE_ENVIRON(environ);
5849 if (rb_block_pair_yield_optimizable()) {
5886env_reject_bang(
VALUE ehash)
5893 keys = env_keys(FALSE);
5894 RBASIC_CLEAR_CLASS(keys);
5905 if (del == 0)
return Qnil;
5930env_delete_if(
VALUE ehash)
5933 env_reject_bang(ehash);
5961 for (i=0; i<argc; i++) {
5987env_select(
VALUE ehash)
5994 result = rb_hash_new();
5995 keys = env_keys(FALSE);
6001 rb_hash_aset(result, key, val);
6046env_select_bang(
VALUE ehash)
6053 keys = env_keys(FALSE);
6054 RBASIC_CLEAR_CLASS(keys);
6065 if (del == 0)
return Qnil;
6088env_keep_if(
VALUE ehash)
6091 env_select_bang(ehash);
6111 VALUE key, value, result;
6114 return rb_hash_new();
6116 result = rb_hash_new_with_size(argc);
6118 for (i = 0; i < argc; i++) {
6120 value = rb_f_getenv(
Qnil, key);
6122 rb_hash_aset(result, key, value);
6134 keys = env_keys(TRUE);
6137 const char *nam = RSTRING_PTR(key);
6138 ruby_setenv(nam, 0);
6157 return rb_env_clear();
6188 char **env = GET_ENVIRON(environ);
6190 const char *s = strchr(*env,
'=');
6192 if (env != environ) {
6203 FREE_ENVIRON(environ);
6227 char **env = GET_ENVIRON(environ);
6229 char *s = strchr(*env,
'=');
6232 env_str_new2(s+1, enc)));
6236 FREE_ENVIRON(environ);
6257env_size_with_lock(
void)
6262 char **env = GET_ENVIRON(environ);
6264 FREE_ENVIRON(environ);
6283 return INT2FIX(env_size_with_lock());
6302 char **env = GET_ENVIRON(environ);
6306 FREE_ENVIRON(environ);
6309 return RBOOL(empty);
6338 const char *s = env_name(key);
6339 return RBOOL(has_env_with_lock(s));
6365 const char *s = env_name(key);
6366 VALUE e = getenv_with_lock(s);
6397 char **env = GET_ENVIRON(environ);
6399 char *s = strchr(*env,
'=');
6401 long len = strlen(s);
6402 if (RSTRING_LEN(obj) ==
len && strncmp(s, RSTRING_PTR(obj),
len) == 0) {
6409 FREE_ENVIRON(environ);
6438 char **env = GET_ENVIRON(environ);
6441 const char *p = *env;
6442 char *s = strchr(p,
'=');
6444 long len = strlen(s);
6445 if (RSTRING_LEN(obj) ==
len && strncmp(s, RSTRING_PTR(obj),
len) == 0) {
6452 FREE_ENVIRON(environ);
6482 char **env = GET_ENVIRON(environ);
6484 char *s = strchr(*env,
'=');
6486 long len = strlen(s);
6487 if (RSTRING_LEN(value) ==
len && strncmp(s, RSTRING_PTR(value),
len) == 0) {
6488 str = env_str_new(*env, s-*env-1, enc);
6494 FREE_ENVIRON(environ);
6503 VALUE hash = rb_hash_new();
6507 char **env = GET_ENVIRON(environ);
6509 char *s = strchr(*env,
'=');
6511 rb_hash_aset(hash, env_str_new(*env, s-*env, enc),
6512 env_str_new2(s+1, enc));
6516 FREE_ENVIRON(environ);
6531 return env_to_hash();
6546 return env_to_hash();
6570 VALUE hash = env_to_hash();
6572 hash = rb_hash_to_h_block(hash);
6590 VALUE key, hash = env_to_hash();
6592 for (i = 0; i < argc; i++) {
6594 rb_hash_delete(hash, key);
6618 return rb_hash_delete_if(env_to_hash());
6621NORETURN(
static VALUE env_freeze(
VALUE self));
6630env_freeze(
VALUE self)
6659 char **env = GET_ENVIRON(environ);
6661 const char *p = *env;
6662 char *s = strchr(p,
'=');
6664 key = env_str_new(p, s-p, enc);
6665 VALUE val = env_str_new2(getenv(RSTRING_PTR(key)), enc);
6669 FREE_ENVIRON(environ);
6697 return rb_hash_invert(env_to_hash());
6704 const char *keyptr, *eptr;
6712 if (elen != keylen)
continue;
6713 if (!ENVNMATCH(keyptr, eptr, elen))
continue;
6725 keylist_delete(keys, key);
6753 keys = env_keys(TRUE);
6754 if (env == hash)
return env;
6755 hash = to_hash(hash);
6756 rb_hash_foreach(hash, env_replace_i, keys);
6776 if (!
NIL_P(oldval)) {
6823 env_update_block_i : env_update_i;
6824 for (
int i = 0; i < argc; ++i) {
6825 VALUE hash = argv[i];
6826 if (env == hash)
continue;
6827 hash = to_hash(hash);
6828 rb_hash_foreach(hash, func, 0);
6848 rb_get_freeze_opt(1, &opt);
6852 rb_raise(
rb_eTypeError,
"Cannot clone ENV, use ENV.to_h to get a copy of ENV as a hash");
6866 rb_raise(
rb_eTypeError,
"Cannot dup ENV, use ENV.to_h to get a copy of ENV as a hash");
6877 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
7347 id_hash_iter_lev = rb_make_internal_id();
7445 rb_cHash_empty_frozen = rb_hash_freeze(rb_hash_new());
7446 RB_OBJ_SET_SHAREABLE(rb_cHash_empty_frozen);
7447 rb_vm_register_global_object(rb_cHash_empty_frozen);
7613 origenviron = environ;
7616 RB_OBJ_SET_SHAREABLE(envtbl);
7681 HASH_ASSERT(
sizeof(ar_hint_t) * RHASH_AR_TABLE_MAX_SIZE ==
sizeof(
VALUE));
7684#include "hash.rbinc"
#define RUBY_ASSERT(...)
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_OBJ_FROZEN(VALUE obj)
Checks if an object is frozen.
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 TYPE(_)
Old name of rb_type.
#define rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
#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 CLASS_OF
Old name of rb_class_of.
#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_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
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.
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_ary_delete_at(VALUE ary, long pos)
Destructively removes an element which resides at the specific index of the passed array.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, long len)
Destructively appends multiple elements at the end of the array.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_clear(VALUE ary)
Destructively removes everything form an array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
#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().
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_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.
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
st_index_t rb_str_hash(VALUE str)
Calculates a hash value of a string.
VALUE rb_str_buf_append(VALUE dst, VALUE src)
Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of C's.
st_index_t rb_hash_start(st_index_t i)
Starts a series of hashing.
VALUE rb_str_inspect(VALUE str)
Generates a "readable" version of the receiver.
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().
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
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 StringValue(v)
Ensures that the parameter object is a String.
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
#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.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.