Ruby: hash.c Source File

Ruby 3.3.6p108 (2024-11-05 revision 75015d4c1f6965b5e85e96fb309f1f2129f933c0)

hash.c
1/**********************************************************************
2
3 hash.c -
4
5 $Author$
6 created at: Mon Nov 22 18:51:18 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9 Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10 Copyright (C) 2000 Information-technology Promotion Agency, Japan
11
12**********************************************************************/
13
14#include "ruby/internal/config.h"
15
16#include <errno.h>
17
18#ifdef __APPLE__
19# ifdef HAVE_CRT_EXTERNS_H
20# include <crt_externs.h>
21# else
22# include "missing/crt_externs.h"
23# endif
24#endif
25
26#include "debug_counter.h"
27#include "id.h"
28#include "internal.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"
42#include "probes.h"
43#include "ruby/st.h"
44#include "ruby/util.h"
45#include "ruby_assert.h"
46#include "symbol.h"
47#include "ruby/thread_native.h"
48#include "ruby/ractor.h"
49#include "vm_sync.h"
50
51/* Flags of RHash
52 *
53 * 1: RHASH_PASS_AS_KEYWORDS
54 * The hash is flagged as Ruby 2 keywords hash.
55 * 2: RHASH_PROC_DEFAULT
56 * The hash has a default proc (rather than a default value).
57 * 3: RHASH_ST_TABLE_FLAG
58 * The hash uses a ST table (rather than an AR table).
59 * 4-7: RHASH_AR_TABLE_SIZE_MASK
60 * The size of the AR table.
61 * 8-11: RHASH_AR_TABLE_BOUND_MASK
62 * The bounds of the AR table.
63 * 13-19: RHASH_LEV_MASK
64 * The iterational level of the hash. Used to prevent modifications
65 * to the hash during interation.
66 */
67
68#ifndef HASH_DEBUG
69#define HASH_DEBUG 0
70#endif
71
72#if HASH_DEBUG
73#include "internal/gc.h"
74#endif
75
76#define SET_DEFAULT(hash, ifnone) ( \
77 FL_UNSET_RAW(hash, RHASH_PROC_DEFAULT), \
78 RHASH_SET_IFNONE(hash, ifnone))
79
80#define SET_PROC_DEFAULT(hash, proc) set_proc_default(hash, proc)
81
82#define COPY_DEFAULT(hash, hash2) copy_default(RHASH(hash), RHASH(hash2))
83
84static inline void
85copy_default(struct RHash *hash, const struct RHash *hash2)
86{
87 hash->basic.flags &= ~RHASH_PROC_DEFAULT;
88 hash->basic.flags |= hash2->basic.flags & RHASH_PROC_DEFAULT;
89 RHASH_SET_IFNONE(hash, RHASH_IFNONE((VALUE)hash2));
90}
91
92static VALUE rb_hash_s_try_convert(VALUE, VALUE);
93
94/*
95 * Hash WB strategy:
96 * 1. Check mutate st_* functions
97 * * st_insert()
98 * * st_insert2()
99 * * st_update()
100 * * st_add_direct()
101 * 2. Insert WBs
102 */
103
104 VALUE
105rb_hash_freeze(VALUE hash)
106{
107 return rb_obj_freeze(hash);
108}
109
110 VALUE rb_cHash;
111
112static VALUE envtbl;
113static ID id_hash, id_flatten_bang;
114static ID id_hash_iter_lev;
115
116#define id_default idDefault
117
118 VALUE
119rb_hash_set_ifnone(VALUE hash, VALUE ifnone)
120{
121 RB_OBJ_WRITE(hash, (&RHASH(hash)->ifnone), ifnone);
122 return hash;
123}
124
125int
126rb_any_cmp(VALUE a, VALUE b)
127{
128 if (a == b) return 0;
129 if (RB_TYPE_P(a, T_STRING) && RBASIC(a)->klass == rb_cString &&
130 RB_TYPE_P(b, T_STRING) && RBASIC(b)->klass == rb_cString) {
131 return rb_str_hash_cmp(a, b);
132 }
133 if (UNDEF_P(a) || UNDEF_P(b)) return -1;
134 if (SYMBOL_P(a) && SYMBOL_P(b)) {
135 return a != b;
136 }
137
138 return !rb_eql(a, b);
139}
140
141static VALUE
142hash_recursive(VALUE obj, VALUE arg, int recurse)
143{
144 if (recurse) return INT2FIX(0);
145 return rb_funcallv(obj, id_hash, 0, 0);
146}
147
148static long rb_objid_hash(st_index_t index);
149
150static st_index_t
151dbl_to_index(double d)
152{
153 union {double d; st_index_t i;} u;
154 u.d = d;
155 return u.i;
156}
157
158long
159rb_dbl_long_hash(double d)
160{
161 /* normalize -0.0 to 0.0 */
162 if (d == 0.0) d = 0.0;
163#if SIZEOF_INT == SIZEOF_VOIDP
164 return rb_memhash(&d, sizeof(d));
165#else
166 return rb_objid_hash(dbl_to_index(d));
167#endif
168}
169
170static inline long
171any_hash(VALUE a, st_index_t (*other_func)(VALUE))
172{
173 VALUE hval;
174 st_index_t hnum;
175
176 switch (TYPE(a)) {
177 case T_SYMBOL:
178 if (STATIC_SYM_P(a)) {
179 hnum = a >> (RUBY_SPECIAL_SHIFT + ID_SCOPE_SHIFT);
180 hnum = rb_hash_start(hnum);
181 }
182 else {
183 hnum = RSYMBOL(a)->hashval;
184 }
185 break;
186 case T_FIXNUM:
187 case T_TRUE:
188 case T_FALSE:
189 case T_NIL:
190 hnum = rb_objid_hash((st_index_t)a);
191 break;
192 case T_STRING:
193 hnum = rb_str_hash(a);
194 break;
195 case T_BIGNUM:
196 hval = rb_big_hash(a);
197 hnum = FIX2LONG(hval);
198 break;
199 case T_FLOAT: /* prevent pathological behavior: [Bug #10761] */
200 hnum = rb_dbl_long_hash(rb_float_value(a));
201 break;
202 default:
203 hnum = other_func(a);
204 }
205 if ((SIGNED_VALUE)hnum > 0)
206 hnum &= FIXNUM_MAX;
207 else
208 hnum |= FIXNUM_MIN;
209 return (long)hnum;
210}
211
212static st_index_t
213obj_any_hash(VALUE obj)
214{
215 VALUE hval = rb_check_funcall_basic_kw(obj, id_hash, rb_mKernel, 0, 0, 0);
216
217 if (UNDEF_P(hval)) {
218 hval = rb_exec_recursive_outer_mid(hash_recursive, obj, 0, id_hash);
219 }
220
221 while (!FIXNUM_P(hval)) {
222 if (RB_TYPE_P(hval, T_BIGNUM)) {
223 int sign;
224 unsigned long ul;
225 sign = rb_integer_pack(hval, &ul, 1, sizeof(ul), 0,
226 INTEGER_PACK_NATIVE_BYTE_ORDER);
227 if (sign < 0) {
228 hval = LONG2FIX(ul | FIXNUM_MIN);
229 }
230 else {
231 hval = LONG2FIX(ul & FIXNUM_MAX);
232 }
233 }
234 hval = rb_to_int(hval);
235 }
236
237 return FIX2LONG(hval);
238}
239
240st_index_t
241rb_any_hash(VALUE a)
242{
243 return any_hash(a, obj_any_hash);
244}
245
246 VALUE
247rb_hash(VALUE obj)
248{
249 return LONG2FIX(any_hash(obj, obj_any_hash));
250}
251
252
253/* Here is a hash function for 64-bit key. It is about 5 times faster
254 (2 times faster when uint128 type is absent) on Haswell than
255 tailored Spooky or City hash function can be. */
256
257/* Here we two primes with random bit generation. */
258static const uint64_t prime1 = ((uint64_t)0x2e0bb864 << 32) | 0xe9ea7df5;
259static const uint32_t prime2 = 0x830fcab9;
260
261
262static inline uint64_t
263mult_and_mix(uint64_t m1, uint64_t m2)
264{
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;
268#else
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;
274
275 return (v64_128 + (v32_96 >> 32)) ^ ((v32_96 << 32) + v1_32);
276#endif
277}
278
279static inline uint64_t
280key64_hash(uint64_t key, uint32_t seed)
281{
282 return mult_and_mix(key + seed, prime1);
283}
284
285/* Should cast down the result for each purpose */
286#define st_index_hash(index) key64_hash(rb_hash_start(index), prime2)
287
288static long
289rb_objid_hash(st_index_t index)
290{
291 return (long)st_index_hash(index);
292}
293
294static st_index_t
295objid_hash(VALUE obj)
296{
297 VALUE object_id = rb_obj_id(obj);
298 if (!FIXNUM_P(object_id))
299 object_id = rb_big_hash(object_id);
300
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));
305#endif
306}
307
311 VALUE
312rb_obj_hash(VALUE obj)
313{
314 long hnum = any_hash(obj, objid_hash);
315 return ST2FIX(hnum);
316}
317
318static const struct st_hash_type objhash = {
319 rb_any_cmp,
320 rb_any_hash,
321};
322
323#define rb_ident_cmp st_numcmp
324
325static st_index_t
326rb_ident_hash(st_data_t n)
327{
328#ifdef USE_FLONUM /* RUBY */
329 /*
330 * - flonum (on 64-bit) is pathologically bad, mix the actual
331 * float value in, but do not use the float value as-is since
332 * many integers get interpreted as 2.0 or -2.0 [Bug #10761]
333 */
334 if (FLONUM_P(n)) {
335 n ^= dbl_to_index(rb_float_value(n));
336 }
337#endif
338
339 return (st_index_t)st_index_hash((st_index_t)n);
340}
341
342#define identhash rb_hashtype_ident
343const struct st_hash_type rb_hashtype_ident = {
344 rb_ident_cmp,
345 rb_ident_hash,
346};
347
348#define RHASH_IDENTHASH_P(hash) (RHASH_TYPE(hash) == &identhash)
349#define RHASH_STRING_KEY_P(hash, key) (!RHASH_IDENTHASH_P(hash) && (rb_obj_class(key) == rb_cString))
350
351typedef st_index_t st_hash_t;
352
353/*
354 * RHASH_AR_TABLE_P(h):
355 * RHASH_AR_TABLE points to ar_table.
356 *
357 * !RHASH_AR_TABLE_P(h):
358 * RHASH_ST_TABLE points st_table.
359 */
360
361#define RHASH_AR_TABLE_MAX_BOUND RHASH_AR_TABLE_MAX_SIZE
362
363#define RHASH_AR_TABLE_REF(hash, n) (&RHASH_AR_TABLE(hash)->pairs[n])
364#define RHASH_AR_CLEARED_HINT 0xff
365
366static inline st_hash_t
367ar_do_hash(st_data_t key)
368{
369 return (st_hash_t)rb_any_hash(key);
370}
371
372static inline ar_hint_t
373ar_do_hash_hint(st_hash_t hash_value)
374{
375 return (ar_hint_t)hash_value;
376}
377
378static inline ar_hint_t
379ar_hint(VALUE hash, unsigned int index)
380{
381 return RHASH_AR_TABLE(hash)->ar_hint.ary[index];
382}
383
384static inline void
385ar_hint_set_hint(VALUE hash, unsigned int index, ar_hint_t hint)
386{
387 RHASH_AR_TABLE(hash)->ar_hint.ary[index] = hint;
388}
389
390static inline void
391ar_hint_set(VALUE hash, unsigned int index, st_hash_t hash_value)
392{
393 ar_hint_set_hint(hash, index, ar_do_hash_hint(hash_value));
394}
395
396static inline void
397ar_clear_entry(VALUE hash, unsigned int index)
398{
399 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, index);
400 pair->key = Qundef;
401 ar_hint_set_hint(hash, index, RHASH_AR_CLEARED_HINT);
402}
403
404static inline int
405ar_cleared_entry(VALUE hash, unsigned int index)
406{
407 if (ar_hint(hash, index) == RHASH_AR_CLEARED_HINT) {
408 /* RHASH_AR_CLEARED_HINT is only a hint, not mean cleared entry,
409 * so you need to check key == Qundef
410 */
411 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, index);
412 return UNDEF_P(pair->key);
413 }
414 else {
415 return FALSE;
416 }
417}
418
419static inline void
420ar_set_entry(VALUE hash, unsigned int index, st_data_t key, st_data_t val, st_hash_t hash_value)
421{
422 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, index);
423 pair->key = key;
424 pair->val = val;
425 ar_hint_set(hash, index, hash_value);
426}
427
428#define RHASH_AR_TABLE_SIZE(h) (HASH_ASSERT(RHASH_AR_TABLE_P(h)), \
429 RHASH_AR_TABLE_SIZE_RAW(h))
430
431#define RHASH_AR_TABLE_BOUND_RAW(h) \
432 ((unsigned int)((RBASIC(h)->flags >> RHASH_AR_TABLE_BOUND_SHIFT) & \
433 (RHASH_AR_TABLE_BOUND_MASK >> RHASH_AR_TABLE_BOUND_SHIFT)))
434
435#define RHASH_ST_TABLE_SET(h, s) rb_hash_st_table_set(h, s)
436#define RHASH_TYPE(hash) (RHASH_AR_TABLE_P(hash) ? &objhash : RHASH_ST_TABLE(hash)->type)
437
438#define HASH_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(HASH_DEBUG, expr, #expr)
439
440static inline unsigned int
441RHASH_AR_TABLE_BOUND(VALUE h)
442{
443 HASH_ASSERT(RHASH_AR_TABLE_P(h));
444 const unsigned int bound = RHASH_AR_TABLE_BOUND_RAW(h);
445 HASH_ASSERT(bound <= RHASH_AR_TABLE_MAX_SIZE);
446 return bound;
447}
448
449#if HASH_DEBUG
450#define hash_verify(hash) hash_verify_(hash, __FILE__, __LINE__)
451
452void
453rb_hash_dump(VALUE hash)
454{
455 rb_obj_info_dump(hash);
456
457 if (RHASH_AR_TABLE_P(hash)) {
458 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
459
460 fprintf(stderr, " size:%u bound:%u\n",
461 RHASH_AR_TABLE_SIZE(hash), bound);
462
463 for (i=0; i<bound; i++) {
464 st_data_t k, v;
465
466 if (!ar_cleared_entry(hash, i)) {
467 char b1[0x100], b2[0x100];
468 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
469 k = pair->key;
470 v = pair->val;
471 fprintf(stderr, " %d key:%s val:%s hint:%02x\n", i,
472 rb_raw_obj_info(b1, 0x100, k),
473 rb_raw_obj_info(b2, 0x100, v),
474 ar_hint(hash, i));
475 }
476 else {
477 fprintf(stderr, " %d empty\n", i);
478 }
479 }
480 }
481}
482
483static VALUE
484hash_verify_(VALUE hash, const char *file, int line)
485{
486 HASH_ASSERT(RB_TYPE_P(hash, T_HASH));
487
488 if (RHASH_AR_TABLE_P(hash)) {
489 unsigned i, n = 0, bound = RHASH_AR_TABLE_BOUND(hash);
490
491 for (i=0; i<bound; i++) {
492 st_data_t k, v;
493 if (!ar_cleared_entry(hash, i)) {
494 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
495 k = pair->key;
496 v = pair->val;
497 HASH_ASSERT(!UNDEF_P(k));
498 HASH_ASSERT(!UNDEF_P(v));
499 n++;
500 }
501 }
502 if (n != RHASH_AR_TABLE_SIZE(hash)) {
503 rb_bug("n:%u, RHASH_AR_TABLE_SIZE:%u", n, RHASH_AR_TABLE_SIZE(hash));
504 }
505 }
506 else {
507 HASH_ASSERT(RHASH_ST_TABLE(hash) != NULL);
508 HASH_ASSERT(RHASH_AR_TABLE_SIZE_RAW(hash) == 0);
509 HASH_ASSERT(RHASH_AR_TABLE_BOUND_RAW(hash) == 0);
510 }
511
512 return hash;
513}
514
515#else
516#define hash_verify(h) ((void)0)
517#endif
518
519static inline int
520RHASH_TABLE_EMPTY_P(VALUE hash)
521{
522 return RHASH_SIZE(hash) == 0;
523}
524
525#define RHASH_SET_ST_FLAG(h) FL_SET_RAW(h, RHASH_ST_TABLE_FLAG)
526#define RHASH_UNSET_ST_FLAG(h) FL_UNSET_RAW(h, RHASH_ST_TABLE_FLAG)
527
528static void
529hash_st_table_init(VALUE hash, const struct st_hash_type *type, st_index_t size)
530{
531 st_init_existing_table_with_size(RHASH_ST_TABLE(hash), type, size);
532 RHASH_SET_ST_FLAG(hash);
533}
534
535void
536rb_hash_st_table_set(VALUE hash, st_table *st)
537{
538 HASH_ASSERT(st != NULL);
539 RHASH_SET_ST_FLAG(hash);
540
541 *RHASH_ST_TABLE(hash) = *st;
542}
543
544static inline void
545RHASH_AR_TABLE_BOUND_SET(VALUE h, st_index_t n)
546{
547 HASH_ASSERT(RHASH_AR_TABLE_P(h));
548 HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_BOUND);
549
550 RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
551 RBASIC(h)->flags |= n << RHASH_AR_TABLE_BOUND_SHIFT;
552}
553
554static inline void
555RHASH_AR_TABLE_SIZE_SET(VALUE h, st_index_t n)
556{
557 HASH_ASSERT(RHASH_AR_TABLE_P(h));
558 HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_SIZE);
559
560 RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
561 RBASIC(h)->flags |= n << RHASH_AR_TABLE_SIZE_SHIFT;
562}
563
564static inline void
565HASH_AR_TABLE_SIZE_ADD(VALUE h, st_index_t n)
566{
567 HASH_ASSERT(RHASH_AR_TABLE_P(h));
568
569 RHASH_AR_TABLE_SIZE_SET(h, RHASH_AR_TABLE_SIZE(h) + n);
570
571 hash_verify(h);
572}
573
574#define RHASH_AR_TABLE_SIZE_INC(h) HASH_AR_TABLE_SIZE_ADD(h, 1)
575
576static inline void
577RHASH_AR_TABLE_SIZE_DEC(VALUE h)
578{
579 HASH_ASSERT(RHASH_AR_TABLE_P(h));
580 int new_size = RHASH_AR_TABLE_SIZE(h) - 1;
581
582 if (new_size != 0) {
583 RHASH_AR_TABLE_SIZE_SET(h, new_size);
584 }
585 else {
586 RHASH_AR_TABLE_SIZE_SET(h, 0);
587 RHASH_AR_TABLE_BOUND_SET(h, 0);
588 }
589 hash_verify(h);
590}
591
592static inline void
593RHASH_AR_TABLE_CLEAR(VALUE h)
594{
595 RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
596 RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
597
598 memset(RHASH_AR_TABLE(h), 0, sizeof(ar_table));
599}
600
601NOINLINE(static int ar_equal(VALUE x, VALUE y));
602
603static int
604ar_equal(VALUE x, VALUE y)
605{
606 return rb_any_cmp(x, y) == 0;
607}
608
609static unsigned
610ar_find_entry_hint(VALUE hash, ar_hint_t hint, st_data_t key)
611{
612 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
613 const ar_hint_t *hints = RHASH_AR_TABLE(hash)->ar_hint.ary;
614
615 /* if table is NULL, then bound also should be 0 */
616
617 for (i = 0; i < bound; i++) {
618 if (hints[i] == hint) {
619 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
620 if (ar_equal(key, pair->key)) {
621 RB_DEBUG_COUNTER_INC(artable_hint_hit);
622 return i;
623 }
624 else {
625#if 0
626 static int pid;
627 static char fname[256];
628 static FILE *fp;
629
630 if (pid != getpid()) {
631 snprintf(fname, sizeof(fname), "/tmp/ruby-armiss.%d", pid = getpid());
632 if ((fp = fopen(fname, "w")) == NULL) rb_bug("fopen");
633 }
634
635 st_hash_t h1 = ar_do_hash(key);
636 st_hash_t h2 = ar_do_hash(pair->key);
637
638 fprintf(fp, "miss: hash_eq:%d hints[%d]:%02x hint:%02x\n"
639 " key :%016lx %s\n"
640 " pair->key:%016lx %s\n",
641 h1 == h2, i, hints[i], hint,
642 h1, rb_obj_info(key), h2, rb_obj_info(pair->key));
643#endif
644 RB_DEBUG_COUNTER_INC(artable_hint_miss);
645 }
646 }
647 }
648 RB_DEBUG_COUNTER_INC(artable_hint_notfound);
649 return RHASH_AR_TABLE_MAX_BOUND;
650}
651
652static unsigned
653ar_find_entry(VALUE hash, st_hash_t hash_value, st_data_t key)
654{
655 ar_hint_t hint = ar_do_hash_hint(hash_value);
656 return ar_find_entry_hint(hash, hint, key);
657}
658
659static inline void
660hash_ar_free_and_clear_table(VALUE hash)
661{
662 RHASH_AR_TABLE_CLEAR(hash);
663
664 HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
665 HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
666}
667
668void rb_st_add_direct_with_hash(st_table *tab, st_data_t key, st_data_t value, st_hash_t hash); // st.c
669
670enum ar_each_key_type {
671 ar_each_key_copy,
672 ar_each_key_cmp,
673 ar_each_key_insert,
674};
675
676static inline int
677ar_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)
678{
679 for (int i = 0; i < max; i++) {
680 ar_table_pair *pair = &ar->pairs[i];
681
682 switch (type) {
683 case ar_each_key_copy:
684 dst_keys[i] = pair->key;
685 break;
686 case ar_each_key_cmp:
687 if (dst_keys[i] != pair->key) return 1;
688 break;
689 case ar_each_key_insert:
690 if (UNDEF_P(pair->key)) continue; // deleted entry
691 rb_st_add_direct_with_hash(new_tab, pair->key, pair->val, hashes[i]);
692 break;
693 }
694 }
695
696 return 0;
697}
698
699static st_table *
700ar_force_convert_table(VALUE hash, const char *file, int line)
701{
702 if (RHASH_ST_TABLE_P(hash)) {
703 return RHASH_ST_TABLE(hash);
704 }
705 else {
706 ar_table *ar = RHASH_AR_TABLE(hash);
707 st_hash_t hashes[RHASH_AR_TABLE_MAX_SIZE];
708 unsigned int bound, size;
709
710 // prepare hash values
711 do {
712 st_data_t keys[RHASH_AR_TABLE_MAX_SIZE];
713 bound = RHASH_AR_TABLE_BOUND(hash);
714 size = RHASH_AR_TABLE_SIZE(hash);
715 ar_each_key(ar, bound, ar_each_key_copy, keys, NULL, NULL);
716
717 for (unsigned int i = 0; i < bound; i++) {
718 // do_hash calls #hash method and it can modify hash object
719 hashes[i] = UNDEF_P(keys[i]) ? 0 : ar_do_hash(keys[i]);
720 }
721
722 // check if modified
723 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) return RHASH_ST_TABLE(hash);
724 if (UNLIKELY(RHASH_AR_TABLE_BOUND(hash) != bound)) continue;
725 if (UNLIKELY(ar_each_key(ar, bound, ar_each_key_cmp, keys, NULL, NULL))) continue;
726 } while (0);
727
728 // make st
729 st_table tab;
730 st_table *new_tab = &tab;
731 rb_st_init_existing_table_with_size(new_tab, &objhash, size);
732 ar_each_key(ar, bound, ar_each_key_insert, NULL, new_tab, hashes);
733 hash_ar_free_and_clear_table(hash);
734 RHASH_ST_TABLE_SET(hash, new_tab);
735 return RHASH_ST_TABLE(hash);
736 }
737}
738
739static int
740ar_compact_table(VALUE hash)
741{
742 const unsigned bound = RHASH_AR_TABLE_BOUND(hash);
743 const unsigned size = RHASH_AR_TABLE_SIZE(hash);
744
745 if (size == bound) {
746 return size;
747 }
748 else {
749 unsigned i, j=0;
750 ar_table_pair *pairs = RHASH_AR_TABLE(hash)->pairs;
751
752 for (i=0; i<bound; i++) {
753 if (ar_cleared_entry(hash, i)) {
754 if (j <= i) j = i+1;
755 for (; j<bound; j++) {
756 if (!ar_cleared_entry(hash, j)) {
757 pairs[i] = pairs[j];
758 ar_hint_set_hint(hash, i, (st_hash_t)ar_hint(hash, j));
759 ar_clear_entry(hash, j);
760 j++;
761 goto found;
762 }
763 }
764 /* non-empty is not found */
765 goto done;
766 found:;
767 }
768 }
769 done:
770 HASH_ASSERT(i<=bound);
771
772 RHASH_AR_TABLE_BOUND_SET(hash, size);
773 hash_verify(hash);
774 return size;
775 }
776}
777
778static int
779ar_add_direct_with_hash(VALUE hash, st_data_t key, st_data_t val, st_hash_t hash_value)
780{
781 unsigned bin = RHASH_AR_TABLE_BOUND(hash);
782
783 if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_SIZE) {
784 return 1;
785 }
786 else {
787 if (UNLIKELY(bin >= RHASH_AR_TABLE_MAX_BOUND)) {
788 bin = ar_compact_table(hash);
789 }
790 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
791
792 ar_set_entry(hash, bin, key, val, hash_value);
793 RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
794 RHASH_AR_TABLE_SIZE_INC(hash);
795 return 0;
796 }
797}
798
799static void
800ensure_ar_table(VALUE hash)
801{
802 if (!RHASH_AR_TABLE_P(hash)) {
803 rb_raise(rb_eRuntimeError, "hash representation was changed during iteration");
804 }
805}
806
807static int
808ar_general_foreach(VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
809{
810 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
811 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
812
813 for (i = 0; i < bound; i++) {
814 if (ar_cleared_entry(hash, i)) continue;
815
816 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
817 enum st_retval retval = (*func)(pair->key, pair->val, arg, 0);
818 ensure_ar_table(hash);
819 /* pair may be not valid here because of theap */
820
821 switch (retval) {
822 case ST_CONTINUE:
823 break;
824 case ST_CHECK:
825 case ST_STOP:
826 return 0;
827 case ST_REPLACE:
828 if (replace) {
829 VALUE key = pair->key;
830 VALUE val = pair->val;
831 retval = (*replace)(&key, &val, arg, TRUE);
832
833 // TODO: pair should be same as pair before.
834 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
835 pair->key = key;
836 pair->val = val;
837 }
838 break;
839 case ST_DELETE:
840 ar_clear_entry(hash, i);
841 RHASH_AR_TABLE_SIZE_DEC(hash);
842 break;
843 }
844 }
845 }
846 return 0;
847}
848
849static int
850ar_foreach_with_replace(VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
851{
852 return ar_general_foreach(hash, func, replace, arg);
853}
854
855 struct functor {
856 st_foreach_callback_func *func;
857 st_data_t arg;
858};
859
860static int
861apply_functor(st_data_t k, st_data_t v, st_data_t d, int _)
862{
863 const struct functor *f = (void *)d;
864 return f->func(k, v, f->arg);
865}
866
867static int
868ar_foreach(VALUE hash, st_foreach_callback_func *func, st_data_t arg)
869{
870 const struct functor f = { func, arg };
871 return ar_general_foreach(hash, apply_functor, NULL, (st_data_t)&f);
872}
873
874static int
875ar_foreach_check(VALUE hash, st_foreach_check_callback_func *func, st_data_t arg,
876 st_data_t never)
877{
878 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
879 unsigned i, ret = 0, bound = RHASH_AR_TABLE_BOUND(hash);
880 enum st_retval retval;
881 st_data_t key;
882 ar_table_pair *pair;
883 ar_hint_t hint;
884
885 for (i = 0; i < bound; i++) {
886 if (ar_cleared_entry(hash, i)) continue;
887
888 pair = RHASH_AR_TABLE_REF(hash, i);
889 key = pair->key;
890 hint = ar_hint(hash, i);
891
892 retval = (*func)(key, pair->val, arg, 0);
893 ensure_ar_table(hash);
894 hash_verify(hash);
895
896 switch (retval) {
897 case ST_CHECK: {
898 pair = RHASH_AR_TABLE_REF(hash, i);
899 if (pair->key == never) break;
900 ret = ar_find_entry_hint(hash, hint, key);
901 if (ret == RHASH_AR_TABLE_MAX_BOUND) {
902 retval = (*func)(0, 0, arg, 1);
903 return 2;
904 }
905 }
906 case ST_CONTINUE:
907 break;
908 case ST_STOP:
909 case ST_REPLACE:
910 return 0;
911 case ST_DELETE: {
912 if (!ar_cleared_entry(hash, i)) {
913 ar_clear_entry(hash, i);
914 RHASH_AR_TABLE_SIZE_DEC(hash);
915 }
916 break;
917 }
918 }
919 }
920 }
921 return 0;
922}
923
924static int
925ar_update(VALUE hash, st_data_t key,
926 st_update_callback_func *func, st_data_t arg)
927{
928 int retval, existing;
929 unsigned bin = RHASH_AR_TABLE_MAX_BOUND;
930 st_data_t value = 0, old_key;
931 st_hash_t hash_value = ar_do_hash(key);
932
933 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
934 // `#hash` changes ar_table -> st_table
935 return -1;
936 }
937
938 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
939 bin = ar_find_entry(hash, hash_value, key);
940 existing = (bin != RHASH_AR_TABLE_MAX_BOUND) ? TRUE : FALSE;
941 }
942 else {
943 existing = FALSE;
944 }
945
946 if (existing) {
947 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, bin);
948 key = pair->key;
949 value = pair->val;
950 }
951 old_key = key;
952 retval = (*func)(&key, &value, arg, existing);
953 /* pair can be invalid here because of theap */
954 ensure_ar_table(hash);
955
956 switch (retval) {
957 case ST_CONTINUE:
958 if (!existing) {
959 if (ar_add_direct_with_hash(hash, key, value, hash_value)) {
960 return -1;
961 }
962 }
963 else {
964 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, bin);
965 if (old_key != key) {
966 pair->key = key;
967 }
968 pair->val = value;
969 }
970 break;
971 case ST_DELETE:
972 if (existing) {
973 ar_clear_entry(hash, bin);
974 RHASH_AR_TABLE_SIZE_DEC(hash);
975 }
976 break;
977 }
978 return existing;
979}
980
981static int
982ar_insert(VALUE hash, st_data_t key, st_data_t value)
983{
984 unsigned bin = RHASH_AR_TABLE_BOUND(hash);
985 st_hash_t hash_value = ar_do_hash(key);
986
987 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
988 // `#hash` changes ar_table -> st_table
989 return -1;
990 }
991
992 bin = ar_find_entry(hash, hash_value, key);
993 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
994 if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_SIZE) {
995 return -1;
996 }
997 else if (bin >= RHASH_AR_TABLE_MAX_BOUND) {
998 bin = ar_compact_table(hash);
999 }
1000 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
1001
1002 ar_set_entry(hash, bin, key, value, hash_value);
1003 RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
1004 RHASH_AR_TABLE_SIZE_INC(hash);
1005 return 0;
1006 }
1007 else {
1008 RHASH_AR_TABLE_REF(hash, bin)->val = value;
1009 return 1;
1010 }
1011}
1012
1013static int
1014ar_lookup(VALUE hash, st_data_t key, st_data_t *value)
1015{
1016 if (RHASH_AR_TABLE_SIZE(hash) == 0) {
1017 return 0;
1018 }
1019 else {
1020 st_hash_t hash_value = ar_do_hash(key);
1021 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1022 // `#hash` changes ar_table -> st_table
1023 return st_lookup(RHASH_ST_TABLE(hash), key, value);
1024 }
1025 unsigned bin = ar_find_entry(hash, hash_value, key);
1026
1027 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1028 return 0;
1029 }
1030 else {
1031 HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
1032 if (value != NULL) {
1033 *value = RHASH_AR_TABLE_REF(hash, bin)->val;
1034 }
1035 return 1;
1036 }
1037 }
1038}
1039
1040static int
1041ar_delete(VALUE hash, st_data_t *key, st_data_t *value)
1042{
1043 unsigned bin;
1044 st_hash_t hash_value = ar_do_hash(*key);
1045
1046 if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
1047 // `#hash` changes ar_table -> st_table
1048 return st_delete(RHASH_ST_TABLE(hash), key, value);
1049 }
1050
1051 bin = ar_find_entry(hash, hash_value, *key);
1052
1053 if (bin == RHASH_AR_TABLE_MAX_BOUND) {
1054 if (value != 0) *value = 0;
1055 return 0;
1056 }
1057 else {
1058 if (value != 0) {
1059 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, bin);
1060 *value = pair->val;
1061 }
1062 ar_clear_entry(hash, bin);
1063 RHASH_AR_TABLE_SIZE_DEC(hash);
1064 return 1;
1065 }
1066}
1067
1068static int
1069ar_shift(VALUE hash, st_data_t *key, st_data_t *value)
1070{
1071 if (RHASH_AR_TABLE_SIZE(hash) > 0) {
1072 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1073
1074 for (i = 0; i < bound; i++) {
1075 if (!ar_cleared_entry(hash, i)) {
1076 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
1077 if (value != 0) *value = pair->val;
1078 *key = pair->key;
1079 ar_clear_entry(hash, i);
1080 RHASH_AR_TABLE_SIZE_DEC(hash);
1081 return 1;
1082 }
1083 }
1084 }
1085 if (value != NULL) *value = 0;
1086 return 0;
1087}
1088
1089static long
1090ar_keys(VALUE hash, st_data_t *keys, st_index_t size)
1091{
1092 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1093 st_data_t *keys_start = keys, *keys_end = keys + size;
1094
1095 for (i = 0; i < bound; i++) {
1096 if (keys == keys_end) {
1097 break;
1098 }
1099 else {
1100 if (!ar_cleared_entry(hash, i)) {
1101 *keys++ = RHASH_AR_TABLE_REF(hash, i)->key;
1102 }
1103 }
1104 }
1105
1106 return keys - keys_start;
1107}
1108
1109static long
1110ar_values(VALUE hash, st_data_t *values, st_index_t size)
1111{
1112 unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
1113 st_data_t *values_start = values, *values_end = values + size;
1114
1115 for (i = 0; i < bound; i++) {
1116 if (values == values_end) {
1117 break;
1118 }
1119 else {
1120 if (!ar_cleared_entry(hash, i)) {
1121 *values++ = RHASH_AR_TABLE_REF(hash, i)->val;
1122 }
1123 }
1124 }
1125
1126 return values - values_start;
1127}
1128
1129static ar_table*
1130ar_copy(VALUE hash1, VALUE hash2)
1131{
1132 ar_table *old_tab = RHASH_AR_TABLE(hash2);
1133 ar_table *new_tab = RHASH_AR_TABLE(hash1);
1134
1135 *new_tab = *old_tab;
1136 RHASH_AR_TABLE(hash1)->ar_hint.word = RHASH_AR_TABLE(hash2)->ar_hint.word;
1137 RHASH_AR_TABLE_BOUND_SET(hash1, RHASH_AR_TABLE_BOUND(hash2));
1138 RHASH_AR_TABLE_SIZE_SET(hash1, RHASH_AR_TABLE_SIZE(hash2));
1139
1140 rb_gc_writebarrier_remember(hash1);
1141
1142 return new_tab;
1143}
1144
1145static void
1146ar_clear(VALUE hash)
1147{
1148 if (RHASH_AR_TABLE(hash) != NULL) {
1149 RHASH_AR_TABLE_SIZE_SET(hash, 0);
1150 RHASH_AR_TABLE_BOUND_SET(hash, 0);
1151 }
1152 else {
1153 HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
1154 HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
1155 }
1156}
1157
1158static void
1159hash_st_free(VALUE hash)
1160{
1161 HASH_ASSERT(RHASH_ST_TABLE_P(hash));
1162
1163 st_table *tab = RHASH_ST_TABLE(hash);
1164
1165 xfree(tab->bins);
1166 xfree(tab->entries);
1167}
1168
1169static void
1170hash_st_free_and_clear_table(VALUE hash)
1171{
1172 hash_st_free(hash);
1173
1174 RHASH_ST_CLEAR(hash);
1175}
1176
1177void
1178rb_hash_free(VALUE hash)
1179{
1180 if (RHASH_ST_TABLE_P(hash)) {
1181 hash_st_free(hash);
1182 }
1183}
1184
1185typedef int st_foreach_func(st_data_t, st_data_t, st_data_t);
1186
1187 struct foreach_safe_arg {
1188 st_table *tbl;
1189 st_foreach_func *func;
1190 st_data_t arg;
1191};
1192
1193static int
1194foreach_safe_i(st_data_t key, st_data_t value, st_data_t args, int error)
1195{
1196 int status;
1197 struct foreach_safe_arg *arg = (void *)args;
1198
1199 if (error) return ST_STOP;
1200 status = (*arg->func)(key, value, arg->arg);
1201 if (status == ST_CONTINUE) {
1202 return ST_CHECK;
1203 }
1204 return status;
1205}
1206
1207void
1208 st_foreach_safe(st_table *table, st_foreach_func *func, st_data_t a)
1209{
1210 struct foreach_safe_arg arg;
1211
1212 arg.tbl = table;
1213 arg.func = (st_foreach_func *)func;
1214 arg.arg = a;
1215 if (st_foreach_check(table, foreach_safe_i, (st_data_t)&arg, 0)) {
1216 rb_raise(rb_eRuntimeError, "hash modified during iteration");
1217 }
1218}
1219
1220typedef int rb_foreach_func(VALUE, VALUE, VALUE);
1221
1222 struct hash_foreach_arg {
1223 VALUE hash;
1224 rb_foreach_func *func;
1225 VALUE arg;
1226};
1227
1228static int
1229hash_iter_status_check(int status)
1230{
1231 switch (status) {
1232 case ST_DELETE:
1233 return ST_DELETE;
1234 case ST_CONTINUE:
1235 break;
1236 case ST_STOP:
1237 return ST_STOP;
1238 }
1239
1240 return ST_CHECK;
1241}
1242
1243static int
1244hash_ar_foreach_iter(st_data_t key, st_data_t value, st_data_t argp, int error)
1245{
1246 struct hash_foreach_arg *arg = (struct hash_foreach_arg *)argp;
1247
1248 if (error) return ST_STOP;
1249
1250 int status = (*arg->func)((VALUE)key, (VALUE)value, arg->arg);
1251 /* TODO: rehash check? rb_raise(rb_eRuntimeError, "rehash occurred during iteration"); */
1252
1253 return hash_iter_status_check(status);
1254}
1255
1256static int
1257hash_foreach_iter(st_data_t key, st_data_t value, st_data_t argp, int error)
1258{
1259 struct hash_foreach_arg *arg = (struct hash_foreach_arg *)argp;
1260
1261 if (error) return ST_STOP;
1262
1263 st_table *tbl = RHASH_ST_TABLE(arg->hash);
1264 int status = (*arg->func)((VALUE)key, (VALUE)value, arg->arg);
1265
1266 if (RHASH_ST_TABLE(arg->hash) != tbl) {
1267 rb_raise(rb_eRuntimeError, "rehash occurred during iteration");
1268 }
1269
1270 return hash_iter_status_check(status);
1271}
1272
1273static unsigned long
1274iter_lev_in_ivar(VALUE hash)
1275{
1276 VALUE levval = rb_ivar_get(hash, id_hash_iter_lev);
1277 HASH_ASSERT(FIXNUM_P(levval));
1278 long lev = FIX2LONG(levval);
1279 HASH_ASSERT(lev >= 0);
1280 return (unsigned long)lev;
1281}
1282
1283void rb_ivar_set_internal(VALUE obj, ID id, VALUE val);
1284
1285static void
1286iter_lev_in_ivar_set(VALUE hash, unsigned long lev)
1287{
1288 HASH_ASSERT(lev >= RHASH_LEV_MAX);
1289 HASH_ASSERT(POSFIXABLE(lev)); /* POSFIXABLE means fitting to long */
1290 rb_ivar_set_internal(hash, id_hash_iter_lev, LONG2FIX((long)lev));
1291}
1292
1293static inline unsigned long
1294iter_lev_in_flags(VALUE hash)
1295{
1296 return (unsigned long)((RBASIC(hash)->flags >> RHASH_LEV_SHIFT) & RHASH_LEV_MAX);
1297}
1298
1299static inline void
1300iter_lev_in_flags_set(VALUE hash, unsigned long lev)
1301{
1302 HASH_ASSERT(lev <= RHASH_LEV_MAX);
1303 RBASIC(hash)->flags = ((RBASIC(hash)->flags & ~RHASH_LEV_MASK) | ((VALUE)lev << RHASH_LEV_SHIFT));
1304}
1305
1306static inline bool
1307hash_iterating_p(VALUE hash)
1308{
1309 return iter_lev_in_flags(hash) > 0;
1310}
1311
1312static void
1313hash_iter_lev_inc(VALUE hash)
1314{
1315 unsigned long lev = iter_lev_in_flags(hash);
1316 if (lev == RHASH_LEV_MAX) {
1317 lev = iter_lev_in_ivar(hash) + 1;
1318 if (!POSFIXABLE(lev)) { /* paranoiac check */
1319 rb_raise(rb_eRuntimeError, "too much nested iterations");
1320 }
1321 }
1322 else {
1323 lev += 1;
1324 iter_lev_in_flags_set(hash, lev);
1325 if (lev < RHASH_LEV_MAX) return;
1326 }
1327 iter_lev_in_ivar_set(hash, lev);
1328}
1329
1330static void
1331hash_iter_lev_dec(VALUE hash)
1332{
1333 unsigned long lev = iter_lev_in_flags(hash);
1334 if (lev == RHASH_LEV_MAX) {
1335 lev = iter_lev_in_ivar(hash);
1336 if (lev > RHASH_LEV_MAX) {
1337 iter_lev_in_ivar_set(hash, lev-1);
1338 return;
1339 }
1340 rb_attr_delete(hash, id_hash_iter_lev);
1341 }
1342 else if (lev == 0) {
1343 rb_raise(rb_eRuntimeError, "iteration level underflow");
1344 }
1345 iter_lev_in_flags_set(hash, lev - 1);
1346}
1347
1348static VALUE
1349hash_foreach_ensure_rollback(VALUE hash)
1350{
1351 hash_iter_lev_inc(hash);
1352 return 0;
1353}
1354
1355static VALUE
1356hash_foreach_ensure(VALUE hash)
1357{
1358 hash_iter_lev_dec(hash);
1359 return 0;
1360}
1361
1362int
1363rb_hash_stlike_foreach(VALUE hash, st_foreach_callback_func *func, st_data_t arg)
1364{
1365 if (RHASH_AR_TABLE_P(hash)) {
1366 return ar_foreach(hash, func, arg);
1367 }
1368 else {
1369 return st_foreach(RHASH_ST_TABLE(hash), func, arg);
1370 }
1371}
1372
1373int
1374rb_hash_stlike_foreach_with_replace(VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
1375{
1376 if (RHASH_AR_TABLE_P(hash)) {
1377 return ar_foreach_with_replace(hash, func, replace, arg);
1378 }
1379 else {
1380 return st_foreach_with_replace(RHASH_ST_TABLE(hash), func, replace, arg);
1381 }
1382}
1383
1384static VALUE
1385hash_foreach_call(VALUE arg)
1386{
1387 VALUE hash = ((struct hash_foreach_arg *)arg)->hash;
1388 int ret = 0;
1389 if (RHASH_AR_TABLE_P(hash)) {
1390 ret = ar_foreach_check(hash, hash_ar_foreach_iter,
1391 (st_data_t)arg, (st_data_t)Qundef);
1392 }
1393 else if (RHASH_ST_TABLE_P(hash)) {
1394 ret = st_foreach_check(RHASH_ST_TABLE(hash), hash_foreach_iter,
1395 (st_data_t)arg, (st_data_t)Qundef);
1396 }
1397 if (ret) {
1398 rb_raise(rb_eRuntimeError, "ret: %d, hash modified during iteration", ret);
1399 }
1400 return Qnil;
1401}
1402
1403void
1404rb_hash_foreach(VALUE hash, rb_foreach_func *func, VALUE farg)
1405{
1406 struct hash_foreach_arg arg;
1407
1408 if (RHASH_TABLE_EMPTY_P(hash))
1409 return;
1410 arg.hash = hash;
1411 arg.func = (rb_foreach_func *)func;
1412 arg.arg = farg;
1413 if (RB_OBJ_FROZEN(hash)) {
1414 hash_foreach_call((VALUE)&arg);
1415 }
1416 else {
1417 hash_iter_lev_inc(hash);
1418 rb_ensure(hash_foreach_call, (VALUE)&arg, hash_foreach_ensure, hash);
1419 }
1420 hash_verify(hash);
1421}
1422
1423void rb_st_compact_table(st_table *tab);
1424
1425static void
1426compact_after_delete(VALUE hash)
1427{
1428 if (!hash_iterating_p(hash) && RHASH_ST_TABLE_P(hash)) {
1429 rb_st_compact_table(RHASH_ST_TABLE(hash));
1430 }
1431}
1432
1433static VALUE
1434hash_alloc_flags(VALUE klass, VALUE flags, VALUE ifnone, bool st)
1435{
1436 const VALUE wb = (RGENGC_WB_PROTECTED_HASH ? FL_WB_PROTECTED : 0);
1437 const size_t size = sizeof(struct RHash) + (st ? sizeof(st_table) : sizeof(ar_table));
1438
1439 NEWOBJ_OF(hash, struct RHash, klass, T_HASH | wb | flags, size, 0);
1440
1441 RHASH_SET_IFNONE((VALUE)hash, ifnone);
1442
1443 return (VALUE)hash;
1444}
1445
1446static VALUE
1447hash_alloc(VALUE klass)
1448{
1449 /* Allocate to be able to fit both st_table and ar_table. */
1450 return hash_alloc_flags(klass, 0, Qnil, sizeof(st_table) > sizeof(ar_table));
1451}
1452
1453static VALUE
1454empty_hash_alloc(VALUE klass)
1455{
1456 RUBY_DTRACE_CREATE_HOOK(HASH, 0);
1457
1458 return hash_alloc(klass);
1459}
1460
1461 VALUE
1462rb_hash_new(void)
1463{
1464 return hash_alloc(rb_cHash);
1465}
1466
1467static VALUE
1468copy_compare_by_id(VALUE hash, VALUE basis)
1469{
1470 if (rb_hash_compare_by_id_p(basis)) {
1471 return rb_hash_compare_by_id(hash);
1472 }
1473 return hash;
1474}
1475
1476 VALUE
1477rb_hash_new_with_size(st_index_t size)
1478{
1479 bool st = size > RHASH_AR_TABLE_MAX_SIZE;
1480 VALUE ret = hash_alloc_flags(rb_cHash, 0, Qnil, st);
1481
1482 if (st) {
1483 hash_st_table_init(ret, &objhash, size);
1484 }
1485
1486 return ret;
1487}
1488
1489 VALUE
1490rb_hash_new_capa(long capa)
1491{
1492 return rb_hash_new_with_size((st_index_t)capa);
1493}
1494
1495static VALUE
1496hash_copy(VALUE ret, VALUE hash)
1497{
1498 if (RHASH_AR_TABLE_P(hash)) {
1499 if (RHASH_AR_TABLE_P(ret)) {
1500 ar_copy(ret, hash);
1501 }
1502 else {
1503 st_table *tab = RHASH_ST_TABLE(ret);
1504 rb_st_init_existing_table_with_size(tab, &objhash, RHASH_AR_TABLE_SIZE(hash));
1505
1506 int bound = RHASH_AR_TABLE_BOUND(hash);
1507 for (int i = 0; i < bound; i++) {
1508 if (ar_cleared_entry(hash, i)) continue;
1509
1510 ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
1511 st_add_direct(tab, pair->key, pair->val);
1512 RB_OBJ_WRITTEN(ret, Qundef, pair->key);
1513 RB_OBJ_WRITTEN(ret, Qundef, pair->val);
1514 }
1515 }
1516 }
1517 else {
1518 HASH_ASSERT(sizeof(st_table) <= sizeof(ar_table));
1519
1520 RHASH_SET_ST_FLAG(ret);
1521 st_replace(RHASH_ST_TABLE(ret), RHASH_ST_TABLE(hash));
1522
1523 rb_gc_writebarrier_remember(ret);
1524 }
1525 return ret;
1526}
1527
1528static VALUE
1529hash_dup_with_compare_by_id(VALUE hash)
1530{
1531 VALUE dup = hash_alloc_flags(rb_cHash, 0, Qnil, RHASH_ST_TABLE_P(hash));
1532 if (RHASH_ST_TABLE_P(hash)) {
1533 RHASH_SET_ST_FLAG(dup);
1534 }
1535 else {
1536 RHASH_UNSET_ST_FLAG(dup);
1537 }
1538
1539 return hash_copy(dup, hash);
1540}
1541
1542static VALUE
1543hash_dup(VALUE hash, VALUE klass, VALUE flags)
1544{
1545 return hash_copy(hash_alloc_flags(klass, flags, RHASH_IFNONE(hash), !RHASH_EMPTY_P(hash) && RHASH_ST_TABLE_P(hash)),
1546 hash);
1547}
1548
1549 VALUE
1550rb_hash_dup(VALUE hash)
1551{
1552 const VALUE flags = RBASIC(hash)->flags;
1553 VALUE ret = hash_dup(hash, rb_obj_class(hash),
1554 flags & (FL_EXIVAR|RHASH_PROC_DEFAULT));
1555 if (flags & FL_EXIVAR)
1556 rb_copy_generic_ivar(ret, hash);
1557 return ret;
1558}
1559
1560 VALUE
1561rb_hash_resurrect(VALUE hash)
1562{
1563 VALUE ret = hash_dup(hash, rb_cHash, 0);
1564 return ret;
1565}
1566
1567static void
1568rb_hash_modify_check(VALUE hash)
1569{
1570 rb_check_frozen(hash);
1571}
1572
1573RUBY_FUNC_EXPORTED struct st_table *
1574rb_hash_tbl_raw(VALUE hash, const char *file, int line)
1575{
1576 return ar_force_convert_table(hash, file, line);
1577}
1578
1579struct st_table *
1580rb_hash_tbl(VALUE hash, const char *file, int line)
1581{
1582 OBJ_WB_UNPROTECT(hash);
1583 return rb_hash_tbl_raw(hash, file, line);
1584}
1585
1586static void
1587rb_hash_modify(VALUE hash)
1588{
1589 rb_hash_modify_check(hash);
1590}
1591
1592NORETURN(static void no_new_key(void));
1593static void
1594no_new_key(void)
1595{
1596 rb_raise(rb_eRuntimeError, "can't add a new key into hash during iteration");
1597}
1598
1599 struct update_callback_arg {
1600 VALUE hash;
1601 st_data_t arg;
1602};
1603
1604#define NOINSERT_UPDATE_CALLBACK(func) \
1605static int \
1606func##_noinsert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
1607{ \
1608 if (!existing) no_new_key(); \
1609 return func(key, val, (struct update_arg *)arg, existing); \
1610} \
1611 \
1612static int \
1613func##_insert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
1614{ \
1615 return func(key, val, (struct update_arg *)arg, existing); \
1616}
1617
1618 struct update_arg {
1619 st_data_t arg;
1620 st_update_callback_func *func;
1621 VALUE hash;
1622 VALUE key;
1623 VALUE value;
1624};
1625
1626typedef int (*tbl_update_func)(st_data_t *, st_data_t *, st_data_t, int);
1627
1628int
1629rb_hash_stlike_update(VALUE hash, st_data_t key, st_update_callback_func *func, st_data_t arg)
1630{
1631 if (RHASH_AR_TABLE_P(hash)) {
1632 int result = ar_update(hash, key, func, arg);
1633 if (result == -1) {
1634 ar_force_convert_table(hash, __FILE__, __LINE__);
1635 }
1636 else {
1637 return result;
1638 }
1639 }
1640
1641 return st_update(RHASH_ST_TABLE(hash), key, func, arg);
1642}
1643
1644static int
1645tbl_update_modify(st_data_t *key, st_data_t *val, st_data_t arg, int existing)
1646{
1647 struct update_arg *p = (struct update_arg *)arg;
1648 st_data_t old_key = *key;
1649 st_data_t old_value = *val;
1650 VALUE hash = p->hash;
1651 int ret = (p->func)(key, val, arg, existing);
1652 switch (ret) {
1653 default:
1654 break;
1655 case ST_CONTINUE:
1656 if (!existing || *key != old_key || *val != old_value) {
1657 rb_hash_modify(hash);
1658 p->key = *key;
1659 p->value = *val;
1660 }
1661 break;
1662 case ST_DELETE:
1663 if (existing)
1664 rb_hash_modify(hash);
1665 break;
1666 }
1667
1668 return ret;
1669}
1670
1671static int
1672tbl_update(VALUE hash, VALUE key, tbl_update_func func, st_data_t optional_arg)
1673{
1674 struct update_arg arg = {
1675 .arg = optional_arg,
1676 .func = func,
1677 .hash = hash,
1678 .key = key,
1679 .value = (VALUE)optional_arg,
1680 };
1681
1682 int ret = rb_hash_stlike_update(hash, key, tbl_update_modify, (st_data_t)&arg);
1683
1684 /* write barrier */
1685 RB_OBJ_WRITTEN(hash, Qundef, arg.key);
1686 RB_OBJ_WRITTEN(hash, Qundef, arg.value);
1687
1688 return ret;
1689}
1690
1691#define UPDATE_CALLBACK(iter_p, func) ((iter_p) ? func##_noinsert : func##_insert)
1692
1693#define RHASH_UPDATE_ITER(h, iter_p, key, func, a) do { \
1694 tbl_update((h), (key), UPDATE_CALLBACK(iter_p, func), (st_data_t)(a)); \
1695} while (0)
1696
1697#define RHASH_UPDATE(hash, key, func, arg) \
1698 RHASH_UPDATE_ITER(hash, hash_iterating_p(hash), key, func, arg)
1699
1700static void
1701set_proc_default(VALUE hash, VALUE proc)
1702{
1703 if (rb_proc_lambda_p(proc)) {
1704 int n = rb_proc_arity(proc);
1705
1706 if (n != 2 && (n >= 0 || n < -3)) {
1707 if (n < 0) n = -n-1;
1708 rb_raise(rb_eTypeError, "default_proc takes two arguments (2 for %d)", n);
1709 }
1710 }
1711
1712 FL_SET_RAW(hash, RHASH_PROC_DEFAULT);
1713 RHASH_SET_IFNONE(hash, proc);
1714}
1715
1716/*
1717 * call-seq:
1718 * Hash.new(default_value = nil) -> new_hash
1719 * Hash.new {|hash, key| ... } -> new_hash
1720 *
1721 * Returns a new empty \Hash object.
1722 *
1723 * The initial default value and initial default proc for the new hash
1724 * depend on which form above was used. See {Default Values}[rdoc-ref:Hash@Default+Values].
1725 *
1726 * If neither an argument nor a block given,
1727 * initializes both the default value and the default proc to <tt>nil</tt>:
1728 * h = Hash.new
1729 * h.default # => nil
1730 * h.default_proc # => nil
1731 *
1732 * If argument <tt>default_value</tt> given but no block given,
1733 * initializes the default value to the given <tt>default_value</tt>
1734 * and the default proc to <tt>nil</tt>:
1735 * h = Hash.new(false)
1736 * h.default # => false
1737 * h.default_proc # => nil
1738 *
1739 * If a block given but no argument, stores the block as the default proc
1740 * and sets the default value to <tt>nil</tt>:
1741 * h = Hash.new {|hash, key| "Default value for #{key}" }
1742 * h.default # => nil
1743 * h.default_proc.class # => Proc
1744 * h[:nosuch] # => "Default value for nosuch"
1745 */
1746
1747static VALUE
1748rb_hash_initialize(int argc, VALUE *argv, VALUE hash)
1749{
1750 rb_hash_modify(hash);
1751
1752 if (rb_block_given_p()) {
1753 rb_check_arity(argc, 0, 0);
1754 SET_PROC_DEFAULT(hash, rb_block_proc());
1755 }
1756 else {
1757 rb_check_arity(argc, 0, 1);
1758
1759 VALUE options, ifnone;
1760 rb_scan_args(argc, argv, "01:", &ifnone, &options);
1761 if (NIL_P(ifnone) && !NIL_P(options)) {
1762 ifnone = options;
1763 rb_warn_deprecated_to_remove("3.4", "Calling Hash.new with keyword arguments", "Hash.new({ key: value })");
1764 }
1765 RHASH_SET_IFNONE(hash, ifnone);
1766 }
1767
1768 return hash;
1769}
1770
1771static VALUE rb_hash_to_a(VALUE hash);
1772
1773/*
1774 * call-seq:
1775 * Hash[] -> new_empty_hash
1776 * Hash[hash] -> new_hash
1777 * Hash[ [*2_element_arrays] ] -> new_hash
1778 * Hash[*objects] -> new_hash
1779 *
1780 * Returns a new \Hash object populated with the given objects, if any.
1781 * See Hash::new.
1782 *
1783 * With no argument, returns a new empty \Hash.
1784 *
1785 * When the single given argument is a \Hash, returns a new \Hash
1786 * populated with the entries from the given \Hash, excluding the
1787 * default value or proc.
1788 *
1789 * h = {foo: 0, bar: 1, baz: 2}
1790 * Hash[h] # => {:foo=>0, :bar=>1, :baz=>2}
1791 *
1792 * When the single given argument is an Array of 2-element Arrays,
1793 * returns a new \Hash object wherein each 2-element array forms a
1794 * key-value entry:
1795 *
1796 * Hash[ [ [:foo, 0], [:bar, 1] ] ] # => {:foo=>0, :bar=>1}
1797 *
1798 * When the argument count is an even number;
1799 * returns a new \Hash object wherein each successive pair of arguments
1800 * has become a key-value entry:
1801 *
1802 * Hash[:foo, 0, :bar, 1] # => {:foo=>0, :bar=>1}
1803 *
1804 * Raises an exception if the argument list does not conform to any
1805 * of the above.
1806 */
1807
1808static VALUE
1809rb_hash_s_create(int argc, VALUE *argv, VALUE klass)
1810{
1811 VALUE hash, tmp;
1812
1813 if (argc == 1) {
1814 tmp = rb_hash_s_try_convert(Qnil, argv[0]);
1815 if (!NIL_P(tmp)) {
1816 if (!RHASH_EMPTY_P(tmp) && rb_hash_compare_by_id_p(tmp)) {
1817 /* hash_copy for non-empty hash will copy compare_by_identity
1818 flag, but we don't want it copied. Work around by
1819 converting hash to flattened array and using that. */
1820 tmp = rb_hash_to_a(tmp);
1821 }
1822 else {
1823 hash = hash_alloc(klass);
1824 if (!RHASH_EMPTY_P(tmp))
1825 hash_copy(hash, tmp);
1826 return hash;
1827 }
1828 }
1829 else {
1830 tmp = rb_check_array_type(argv[0]);
1831 }
1832
1833 if (!NIL_P(tmp)) {
1834 long i;
1835
1836 hash = hash_alloc(klass);
1837 for (i = 0; i < RARRAY_LEN(tmp); ++i) {
1838 VALUE e = RARRAY_AREF(tmp, i);
1839 VALUE v = rb_check_array_type(e);
1840 VALUE key, val = Qnil;
1841
1842 if (NIL_P(v)) {
1843 rb_raise(rb_eArgError, "wrong element type %s at %ld (expected array)",
1844 rb_builtin_class_name(e), i);
1845 }
1846 switch (RARRAY_LEN(v)) {
1847 default:
1848 rb_raise(rb_eArgError, "invalid number of elements (%ld for 1..2)",
1849 RARRAY_LEN(v));
1850 case 2:
1851 val = RARRAY_AREF(v, 1);
1852 case 1:
1853 key = RARRAY_AREF(v, 0);
1854 rb_hash_aset(hash, key, val);
1855 }
1856 }
1857 return hash;
1858 }
1859 }
1860 if (argc % 2 != 0) {
1861 rb_raise(rb_eArgError, "odd number of arguments for Hash");
1862 }
1863
1864 hash = hash_alloc(klass);
1865 rb_hash_bulk_insert(argc, argv, hash);
1866 hash_verify(hash);
1867 return hash;
1868}
1869
1870 VALUE
1871rb_to_hash_type(VALUE hash)
1872{
1873 return rb_convert_type_with_id(hash, T_HASH, "Hash", idTo_hash);
1874}
1875#define to_hash rb_to_hash_type
1876
1877 VALUE
1878rb_check_hash_type(VALUE hash)
1879{
1880 return rb_check_convert_type_with_id(hash, T_HASH, "Hash", idTo_hash);
1881}
1882
1883/*
1884 * call-seq:
1885 * Hash.try_convert(obj) -> obj, new_hash, or nil
1886 *
1887 * If +obj+ is a \Hash object, returns +obj+.
1888 *
1889 * Otherwise if +obj+ responds to <tt>:to_hash</tt>,
1890 * calls <tt>obj.to_hash</tt> and returns the result.
1891 *
1892 * Returns +nil+ if +obj+ does not respond to <tt>:to_hash</tt>
1893 *
1894 * Raises an exception unless <tt>obj.to_hash</tt> returns a \Hash object.
1895 */
1896static VALUE
1897rb_hash_s_try_convert(VALUE dummy, VALUE hash)
1898{
1899 return rb_check_hash_type(hash);
1900}
1901
1902/*
1903 * call-seq:
1904 * Hash.ruby2_keywords_hash?(hash) -> true or false
1905 *
1906 * Checks if a given hash is flagged by Module#ruby2_keywords (or
1907 * Proc#ruby2_keywords).
1908 * This method is not for casual use; debugging, researching, and
1909 * some truly necessary cases like serialization of arguments.
1910 *
1911 * ruby2_keywords def foo(*args)
1912 * Hash.ruby2_keywords_hash?(args.last)
1913 * end
1914 * foo(k: 1) #=> true
1915 * foo({k: 1}) #=> false
1916 */
1917static VALUE
1918rb_hash_s_ruby2_keywords_hash_p(VALUE dummy, VALUE hash)
1919{
1920 Check_Type(hash, T_HASH);
1921 return RBOOL(RHASH(hash)->basic.flags & RHASH_PASS_AS_KEYWORDS);
1922}
1923
1924/*
1925 * call-seq:
1926 * Hash.ruby2_keywords_hash(hash) -> hash
1927 *
1928 * Duplicates a given hash and adds a ruby2_keywords flag.
1929 * This method is not for casual use; debugging, researching, and
1930 * some truly necessary cases like deserialization of arguments.
1931 *
1932 * h = {k: 1}
1933 * h = Hash.ruby2_keywords_hash(h)
1934 * def foo(k: 42)
1935 * k
1936 * end
1937 * foo(*[h]) #=> 1 with neither a warning or an error
1938 */
1939static VALUE
1940rb_hash_s_ruby2_keywords_hash(VALUE dummy, VALUE hash)
1941{
1942 Check_Type(hash, T_HASH);
1943 VALUE tmp = rb_hash_dup(hash);
1944 if (RHASH_EMPTY_P(hash) && rb_hash_compare_by_id_p(hash)) {
1945 rb_hash_compare_by_id(tmp);
1946 }
1947 RHASH(tmp)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
1948 return tmp;
1949}
1950
1951 struct rehash_arg {
1952 VALUE hash;
1953 st_table *tbl;
1954};
1955
1956static int
1957rb_hash_rehash_i(VALUE key, VALUE value, VALUE arg)
1958{
1959 if (RHASH_AR_TABLE_P(arg)) {
1960 ar_insert(arg, (st_data_t)key, (st_data_t)value);
1961 }
1962 else {
1963 st_insert(RHASH_ST_TABLE(arg), (st_data_t)key, (st_data_t)value);
1964 }
1965 return ST_CONTINUE;
1966}
1967
1968/*
1969 * call-seq:
1970 * hash.rehash -> self
1971 *
1972 * Rebuilds the hash table by recomputing the hash index for each key;
1973 * returns <tt>self</tt>.
1974 *
1975 * The hash table becomes invalid if the hash value of a key
1976 * has changed after the entry was created.
1977 * See {Modifying an Active Hash Key}[rdoc-ref:Hash@Modifying+an+Active+Hash+Key].
1978 */
1979
1980 VALUE
1981rb_hash_rehash(VALUE hash)
1982{
1983 VALUE tmp;
1984 st_table *tbl;
1985
1986 if (hash_iterating_p(hash)) {
1987 rb_raise(rb_eRuntimeError, "rehash during iteration");
1988 }
1989 rb_hash_modify_check(hash);
1990 if (RHASH_AR_TABLE_P(hash)) {
1991 tmp = hash_alloc(0);
1992 rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tmp);
1993
1994 hash_ar_free_and_clear_table(hash);
1995 ar_copy(hash, tmp);
1996 }
1997 else if (RHASH_ST_TABLE_P(hash)) {
1998 st_table *old_tab = RHASH_ST_TABLE(hash);
1999 tmp = hash_alloc(0);
2000
2001 hash_st_table_init(tmp, old_tab->type, old_tab->num_entries);
2002 tbl = RHASH_ST_TABLE(tmp);
2003
2004 rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tmp);
2005
2006 hash_st_free(hash);
2007 RHASH_ST_TABLE_SET(hash, tbl);
2008 RHASH_ST_CLEAR(tmp);
2009 }
2010 hash_verify(hash);
2011 return hash;
2012}
2013
2014static VALUE
2015call_default_proc(VALUE proc, VALUE hash, VALUE key)
2016{
2017 VALUE args[2] = {hash, key};
2018 return rb_proc_call_with_block(proc, 2, args, Qnil);
2019}
2020
2021static bool
2022rb_hash_default_unredefined(VALUE hash)
2023{
2024 VALUE klass = RBASIC_CLASS(hash);
2025 if (LIKELY(klass == rb_cHash)) {
2026 return !!BASIC_OP_UNREDEFINED_P(BOP_DEFAULT, HASH_REDEFINED_OP_FLAG);
2027 }
2028 else {
2029 return LIKELY(rb_method_basic_definition_p(klass, id_default));
2030 }
2031}
2032
2033 VALUE
2034rb_hash_default_value(VALUE hash, VALUE key)
2035{
2036 RUBY_ASSERT(RB_TYPE_P(hash, T_HASH));
2037
2038 if (LIKELY(rb_hash_default_unredefined(hash))) {
2039 VALUE ifnone = RHASH_IFNONE(hash);
2040 if (LIKELY(!FL_TEST_RAW(hash, RHASH_PROC_DEFAULT))) return ifnone;
2041 if (UNDEF_P(key)) return Qnil;
2042 return call_default_proc(ifnone, hash, key);
2043 }
2044 else {
2045 return rb_funcall(hash, id_default, 1, key);
2046 }
2047}
2048
2049static inline int
2050hash_stlike_lookup(VALUE hash, st_data_t key, st_data_t *pval)
2051{
2052 hash_verify(hash);
2053
2054 if (RHASH_AR_TABLE_P(hash)) {
2055 return ar_lookup(hash, key, pval);
2056 }
2057 else {
2058 extern st_index_t rb_iseq_cdhash_hash(VALUE);
2059 RUBY_ASSERT(RHASH_ST_TABLE(hash)->type->hash == rb_any_hash ||
2060 RHASH_ST_TABLE(hash)->type->hash == rb_ident_hash ||
2061 RHASH_ST_TABLE(hash)->type->hash == rb_iseq_cdhash_hash);
2062 return st_lookup(RHASH_ST_TABLE(hash), key, pval);
2063 }
2064}
2065
2066int
2067rb_hash_stlike_lookup(VALUE hash, st_data_t key, st_data_t *pval)
2068{
2069 return hash_stlike_lookup(hash, key, pval);
2070}
2071
2072/*
2073 * call-seq:
2074 * hash[key] -> value
2075 *
2076 * Returns the value associated with the given +key+, if found:
2077 * h = {foo: 0, bar: 1, baz: 2}
2078 * h[:foo] # => 0
2079 *
2080 * If +key+ is not found, returns a default value
2081 * (see {Default Values}[rdoc-ref:Hash@Default+Values]):
2082 * h = {foo: 0, bar: 1, baz: 2}
2083 * h[:nosuch] # => nil
2084 */
2085
2086 VALUE
2087rb_hash_aref(VALUE hash, VALUE key)
2088{
2089 st_data_t val;
2090
2091 if (hash_stlike_lookup(hash, key, &val)) {
2092 return (VALUE)val;
2093 }
2094 else {
2095 return rb_hash_default_value(hash, key);
2096 }
2097}
2098
2099 VALUE
2100rb_hash_lookup2(VALUE hash, VALUE key, VALUE def)
2101{
2102 st_data_t val;
2103
2104 if (hash_stlike_lookup(hash, key, &val)) {
2105 return (VALUE)val;
2106 }
2107 else {
2108 return def; /* without Hash#default */
2109 }
2110}
2111
2112 VALUE
2113rb_hash_lookup(VALUE hash, VALUE key)
2114{
2115 return rb_hash_lookup2(hash, key, Qnil);
2116}
2117
2118/*
2119 * call-seq:
2120 * hash.fetch(key) -> object
2121 * hash.fetch(key, default_value) -> object
2122 * hash.fetch(key) {|key| ... } -> object
2123 *
2124 * Returns the value for the given +key+, if found.
2125 * h = {foo: 0, bar: 1, baz: 2}
2126 * h.fetch(:bar) # => 1
2127 *
2128 * If +key+ is not found and no block was given,
2129 * returns +default_value+:
2130 * {}.fetch(:nosuch, :default) # => :default
2131 *
2132 * If +key+ is not found and a block was given,
2133 * yields +key+ to the block and returns the block's return value:
2134 * {}.fetch(:nosuch) {|key| "No key #{key}"} # => "No key nosuch"
2135 *
2136 * Raises KeyError if neither +default_value+ nor a block was given.
2137 *
2138 * Note that this method does not use the values of either #default or #default_proc.
2139 */
2140
2141static VALUE
2142rb_hash_fetch_m(int argc, VALUE *argv, VALUE hash)
2143{
2144 VALUE key;
2145 st_data_t val;
2146 long block_given;
2147
2148 rb_check_arity(argc, 1, 2);
2149 key = argv[0];
2150
2151 block_given = rb_block_given_p();
2152 if (block_given && argc == 2) {
2153 rb_warn("block supersedes default value argument");
2154 }
2155
2156 if (hash_stlike_lookup(hash, key, &val)) {
2157 return (VALUE)val;
2158 }
2159 else {
2160 if (block_given) {
2161 return rb_yield(key);
2162 }
2163 else if (argc == 1) {
2164 VALUE desc = rb_protect(rb_inspect, key, 0);
2165 if (NIL_P(desc)) {
2166 desc = rb_any_to_s(key);
2167 }
2168 desc = rb_str_ellipsize(desc, 65);
2169 rb_key_err_raise(rb_sprintf("key not found: %"PRIsVALUE, desc), hash, key);
2170 }
2171 else {
2172 return argv[1];
2173 }
2174 }
2175}
2176
2177 VALUE
2178rb_hash_fetch(VALUE hash, VALUE key)
2179{
2180 return rb_hash_fetch_m(1, &key, hash);
2181}
2182
2183/*
2184 * call-seq:
2185 * hash.default -> object
2186 * hash.default(key) -> object
2187 *
2188 * Returns the default value for the given +key+.
2189 * The returned value will be determined either by the default proc or by the default value.
2190 * See {Default Values}[rdoc-ref:Hash@Default+Values].
2191 *
2192 * With no argument, returns the current default value:
2193 * h = {}
2194 * h.default # => nil
2195 *
2196 * If +key+ is given, returns the default value for +key+,
2197 * regardless of whether that key exists:
2198 * h = Hash.new { |hash, key| hash[key] = "No key #{key}"}
2199 * h[:foo] = "Hello"
2200 * h.default(:foo) # => "No key foo"
2201 */
2202
2203static VALUE
2204rb_hash_default(int argc, VALUE *argv, VALUE hash)
2205{
2206 VALUE ifnone;
2207
2208 rb_check_arity(argc, 0, 1);
2209 ifnone = RHASH_IFNONE(hash);
2210 if (FL_TEST(hash, RHASH_PROC_DEFAULT)) {
2211 if (argc == 0) return Qnil;
2212 return call_default_proc(ifnone, hash, argv[0]);
2213 }
2214 return ifnone;
2215}
2216
2217/*
2218 * call-seq:
2219 * hash.default = value -> object
2220 *
2221 * Sets the default value to +value+; returns +value+:
2222 * h = {}
2223 * h.default # => nil
2224 * h.default = false # => false
2225 * h.default # => false
2226 *
2227 * See {Default Values}[rdoc-ref:Hash@Default+Values].
2228 */
2229
2230static VALUE
2231rb_hash_set_default(VALUE hash, VALUE ifnone)
2232{
2233 rb_hash_modify_check(hash);
2234 SET_DEFAULT(hash, ifnone);
2235 return ifnone;
2236}
2237
2238/*
2239 * call-seq:
2240 * hash.default_proc -> proc or nil
2241 *
2242 * Returns the default proc for +self+
2243 * (see {Default Values}[rdoc-ref:Hash@Default+Values]):
2244 * h = {}
2245 * h.default_proc # => nil
2246 * h.default_proc = proc {|hash, key| "Default value for #{key}" }
2247 * h.default_proc.class # => Proc
2248 */
2249
2250static VALUE
2251rb_hash_default_proc(VALUE hash)
2252{
2253 if (FL_TEST(hash, RHASH_PROC_DEFAULT)) {
2254 return RHASH_IFNONE(hash);
2255 }
2256 return Qnil;
2257}
2258
2259/*
2260 * call-seq:
2261 * hash.default_proc = proc -> proc
2262 *
2263 * Sets the default proc for +self+ to +proc+:
2264 * (see {Default Values}[rdoc-ref:Hash@Default+Values]):
2265 * h = {}
2266 * h.default_proc # => nil
2267 * h.default_proc = proc { |hash, key| "Default value for #{key}" }
2268 * h.default_proc.class # => Proc
2269 * h.default_proc = nil
2270 * h.default_proc # => nil
2271 */
2272
2273 VALUE
2274rb_hash_set_default_proc(VALUE hash, VALUE proc)
2275{
2276 VALUE b;
2277
2278 rb_hash_modify_check(hash);
2279 if (NIL_P(proc)) {
2280 SET_DEFAULT(hash, proc);
2281 return proc;
2282 }
2283 b = rb_check_convert_type_with_id(proc, T_DATA, "Proc", idTo_proc);
2284 if (NIL_P(b) || !rb_obj_is_proc(b)) {
2285 rb_raise(rb_eTypeError,
2286 "wrong default_proc type %s (expected Proc)",
2287 rb_obj_classname(proc));
2288 }
2289 proc = b;
2290 SET_PROC_DEFAULT(hash, proc);
2291 return proc;
2292}
2293
2294static int
2295key_i(VALUE key, VALUE value, VALUE arg)
2296{
2297 VALUE *args = (VALUE *)arg;
2298
2299 if (rb_equal(value, args[0])) {
2300 args[1] = key;
2301 return ST_STOP;
2302 }
2303 return ST_CONTINUE;
2304}
2305
2306/*
2307 * call-seq:
2308 * hash.key(value) -> key or nil
2309 *
2310 * Returns the key for the first-found entry with the given +value+
2311 * (see {Entry Order}[rdoc-ref:Hash@Entry+Order]):
2312 * h = {foo: 0, bar: 2, baz: 2}
2313 * h.key(0) # => :foo
2314 * h.key(2) # => :bar
2315 *
2316 * Returns +nil+ if no such value is found.
2317 */
2318
2319static VALUE
2320rb_hash_key(VALUE hash, VALUE value)
2321{
2322 VALUE args[2];
2323
2324 args[0] = value;
2325 args[1] = Qnil;
2326
2327 rb_hash_foreach(hash, key_i, (VALUE)args);
2328
2329 return args[1];
2330}
2331
2332int
2333rb_hash_stlike_delete(VALUE hash, st_data_t *pkey, st_data_t *pval)
2334{
2335 if (RHASH_AR_TABLE_P(hash)) {
2336 return ar_delete(hash, pkey, pval);
2337 }
2338 else {
2339 return st_delete(RHASH_ST_TABLE(hash), pkey, pval);
2340 }
2341}
2342
2343/*
2344 * delete a specified entry by a given key.
2345 * if there is the corresponding entry, return a value of the entry.
2346 * if there is no corresponding entry, return Qundef.
2347 */
2348 VALUE
2349rb_hash_delete_entry(VALUE hash, VALUE key)
2350{
2351 st_data_t ktmp = (st_data_t)key, val;
2352
2353 if (rb_hash_stlike_delete(hash, &ktmp, &val)) {
2354 return (VALUE)val;
2355 }
2356 else {
2357 return Qundef;
2358 }
2359}
2360
2361/*
2362 * delete a specified entry by a given key.
2363 * if there is the corresponding entry, return a value of the entry.
2364 * if there is no corresponding entry, return Qnil.
2365 */
2366 VALUE
2367rb_hash_delete(VALUE hash, VALUE key)
2368{
2369 VALUE deleted_value = rb_hash_delete_entry(hash, key);
2370
2371 if (!UNDEF_P(deleted_value)) { /* likely pass */
2372 return deleted_value;
2373 }
2374 else {
2375 return Qnil;
2376 }
2377}
2378
2379/*
2380 * call-seq:
2381 * hash.delete(key) -> value or nil
2382 * hash.delete(key) {|key| ... } -> object
2383 *
2384 * Deletes the entry for the given +key+ and returns its associated value.
2385 *
2386 * If no block is given and +key+ is found, deletes the entry and returns the associated value:
2387 * h = {foo: 0, bar: 1, baz: 2}
2388 * h.delete(:bar) # => 1
2389 * h # => {:foo=>0, :baz=>2}
2390 *
2391 * If no block given and +key+ is not found, returns +nil+.
2392 *
2393 * If a block is given and +key+ is found, ignores the block,
2394 * deletes the entry, and returns the associated value:
2395 * h = {foo: 0, bar: 1, baz: 2}
2396 * h.delete(:baz) { |key| raise 'Will never happen'} # => 2
2397 * h # => {:foo=>0, :bar=>1}
2398 *
2399 * If a block is given and +key+ is not found,
2400 * calls the block and returns the block's return value:
2401 * h = {foo: 0, bar: 1, baz: 2}
2402 * h.delete(:nosuch) { |key| "Key #{key} not found" } # => "Key nosuch not found"
2403 * h # => {:foo=>0, :bar=>1, :baz=>2}
2404 */
2405
2406static VALUE
2407rb_hash_delete_m(VALUE hash, VALUE key)
2408{
2409 VALUE val;
2410
2411 rb_hash_modify_check(hash);
2412 val = rb_hash_delete_entry(hash, key);
2413
2414 if (!UNDEF_P(val)) {
2415 compact_after_delete(hash);
2416 return val;
2417 }
2418 else {
2419 if (rb_block_given_p()) {
2420 return rb_yield(key);
2421 }
2422 else {
2423 return Qnil;
2424 }
2425 }
2426}
2427
2428 struct shift_var {
2429 VALUE key;
2430 VALUE val;
2431};
2432
2433static int
2434shift_i_safe(VALUE key, VALUE value, VALUE arg)
2435{
2436 struct shift_var *var = (struct shift_var *)arg;
2437
2438 var->key = key;
2439 var->val = value;
2440 return ST_STOP;
2441}
2442
2443/*
2444 * call-seq:
2445 * hash.shift -> [key, value] or nil
2446 *
2447 * Removes the first hash entry
2448 * (see {Entry Order}[rdoc-ref:Hash@Entry+Order]);
2449 * returns a 2-element Array containing the removed key and value:
2450 * h = {foo: 0, bar: 1, baz: 2}
2451 * h.shift # => [:foo, 0]
2452 * h # => {:bar=>1, :baz=>2}
2453 *
2454 * Returns nil if the hash is empty.
2455 */
2456
2457static VALUE
2458rb_hash_shift(VALUE hash)
2459{
2460 struct shift_var var;
2461
2462 rb_hash_modify_check(hash);
2463 if (RHASH_AR_TABLE_P(hash)) {
2464 var.key = Qundef;
2465 if (!hash_iterating_p(hash)) {
2466 if (ar_shift(hash, &var.key, &var.val)) {
2467 return rb_assoc_new(var.key, var.val);
2468 }
2469 }
2470 else {
2471 rb_hash_foreach(hash, shift_i_safe, (VALUE)&var);
2472 if (!UNDEF_P(var.key)) {
2473 rb_hash_delete_entry(hash, var.key);
2474 return rb_assoc_new(var.key, var.val);
2475 }
2476 }
2477 }
2478 if (RHASH_ST_TABLE_P(hash)) {
2479 var.key = Qundef;
2480 if (!hash_iterating_p(hash)) {
2481 if (st_shift(RHASH_ST_TABLE(hash), &var.key, &var.val)) {
2482 return rb_assoc_new(var.key, var.val);
2483 }
2484 }
2485 else {
2486 rb_hash_foreach(hash, shift_i_safe, (VALUE)&var);
2487 if (!UNDEF_P(var.key)) {
2488 rb_hash_delete_entry(hash, var.key);
2489 return rb_assoc_new(var.key, var.val);
2490 }
2491 }
2492 }
2493 return Qnil;
2494}
2495
2496static int
2497delete_if_i(VALUE key, VALUE value, VALUE hash)
2498{
2499 if (RTEST(rb_yield_values(2, key, value))) {
2500 rb_hash_modify(hash);
2501 return ST_DELETE;
2502 }
2503 return ST_CONTINUE;
2504}
2505
2506static VALUE
2507hash_enum_size(VALUE hash, VALUE args, VALUE eobj)
2508{
2509 return rb_hash_size(hash);
2510}
2511
2512/*
2513 * call-seq:
2514 * hash.delete_if {|key, value| ... } -> self
2515 * hash.delete_if -> new_enumerator
2516 *
2517 * If a block given, calls the block with each key-value pair;
2518 * deletes each entry for which the block returns a truthy value;
2519 * returns +self+:
2520 * h = {foo: 0, bar: 1, baz: 2}
2521 * h.delete_if {|key, value| value > 0 } # => {:foo=>0}
2522 *
2523 * If no block given, returns a new Enumerator:
2524 * h = {foo: 0, bar: 1, baz: 2}
2525 * e = h.delete_if # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:delete_if>
2526 * e.each { |key, value| value > 0 } # => {:foo=>0}
2527 */
2528
2529 VALUE
2530rb_hash_delete_if(VALUE hash)
2531{
2532 RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
2533 rb_hash_modify_check(hash);
2534 if (!RHASH_TABLE_EMPTY_P(hash)) {
2535 rb_hash_foreach(hash, delete_if_i, hash);
2536 compact_after_delete(hash);
2537 }
2538 return hash;
2539}
2540
2541/*
2542 * call-seq:
2543 * hash.reject! {|key, value| ... } -> self or nil
2544 * hash.reject! -> new_enumerator
2545 *
2546 * Returns +self+, whose remaining entries are those
2547 * for which the block returns +false+ or +nil+:
2548 * h = {foo: 0, bar: 1, baz: 2}
2549 * h.reject! {|key, value| value < 2 } # => {:baz=>2}
2550 *
2551 * Returns +nil+ if no entries are removed.
2552 *
2553 * Returns a new Enumerator if no block given:
2554 * h = {foo: 0, bar: 1, baz: 2}
2555 * e = h.reject! # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:reject!>
2556 * e.each {|key, value| key.start_with?('b') } # => {:foo=>0}
2557 */
2558
2559static VALUE
2560rb_hash_reject_bang(VALUE hash)
2561{
2562 st_index_t n;
2563
2564 RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
2565 rb_hash_modify(hash);
2566 n = RHASH_SIZE(hash);
2567 if (!n) return Qnil;
2568 rb_hash_foreach(hash, delete_if_i, hash);
2569 if (n == RHASH_SIZE(hash)) return Qnil;
2570 return hash;
2571}
2572
2573/*
2574 * call-seq:
2575 * hash.reject {|key, value| ... } -> new_hash
2576 * hash.reject -> new_enumerator
2577 *
2578 * Returns a new \Hash object whose entries are all those
2579 * from +self+ for which the block returns +false+ or +nil+:
2580 * h = {foo: 0, bar: 1, baz: 2}
2581 * h1 = h.reject {|key, value| key.start_with?('b') }
2582 * h1 # => {:foo=>0}
2583 *
2584 * Returns a new Enumerator if no block given:
2585 * h = {foo: 0, bar: 1, baz: 2}
2586 * e = h.reject # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:reject>
2587 * h1 = e.each {|key, value| key.start_with?('b') }
2588 * h1 # => {:foo=>0}
2589 */
2590
2591static VALUE
2592rb_hash_reject(VALUE hash)
2593{
2594 VALUE result;
2595
2596 RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
2597 result = hash_dup_with_compare_by_id(hash);
2598 if (!RHASH_EMPTY_P(hash)) {
2599 rb_hash_foreach(result, delete_if_i, result);
2600 compact_after_delete(result);
2601 }
2602 return result;
2603}
2604
2605/*
2606 * call-seq:
2607 * hash.slice(*keys) -> new_hash
2608 *
2609 * Returns a new \Hash object containing the entries for the given +keys+:
2610 * h = {foo: 0, bar: 1, baz: 2}
2611 * h.slice(:baz, :foo) # => {:baz=>2, :foo=>0}
2612 *
2613 * Any given +keys+ that are not found are ignored.
2614 */
2615
2616static VALUE
2617rb_hash_slice(int argc, VALUE *argv, VALUE hash)
2618{
2619 int i;
2620 VALUE key, value, result;
2621
2622 if (argc == 0 || RHASH_EMPTY_P(hash)) {
2623 return copy_compare_by_id(rb_hash_new(), hash);
2624 }
2625 result = copy_compare_by_id(rb_hash_new_with_size(argc), hash);
2626
2627 for (i = 0; i < argc; i++) {
2628 key = argv[i];
2629 value = rb_hash_lookup2(hash, key, Qundef);
2630 if (!UNDEF_P(value))
2631 rb_hash_aset(result, key, value);
2632 }
2633
2634 return result;
2635}
2636
2637/*
2638 * call-seq:
2639 * hsh.except(*keys) -> a_hash
2640 *
2641 * Returns a new \Hash excluding entries for the given +keys+:
2642 * h = { a: 100, b: 200, c: 300 }
2643 * h.except(:a) #=> {:b=>200, :c=>300}
2644 *
2645 * Any given +keys+ that are not found are ignored.
2646 */
2647
2648static VALUE
2649rb_hash_except(int argc, VALUE *argv, VALUE hash)
2650{
2651 int i;
2652 VALUE key, result;
2653
2654 result = hash_dup_with_compare_by_id(hash);
2655
2656 for (i = 0; i < argc; i++) {
2657 key = argv[i];
2658 rb_hash_delete(result, key);
2659 }
2660 compact_after_delete(result);
2661
2662 return result;
2663}
2664
2665/*
2666 * call-seq:
2667 * hash.values_at(*keys) -> new_array
2668 *
2669 * Returns a new Array containing values for the given +keys+:
2670 * h = {foo: 0, bar: 1, baz: 2}
2671 * h.values_at(:baz, :foo) # => [2, 0]
2672 *
2673 * The {default values}[rdoc-ref:Hash@Default+Values] are returned
2674 * for any keys that are not found:
2675 * h.values_at(:hello, :foo) # => [nil, 0]
2676 */
2677
2678static VALUE
2679rb_hash_values_at(int argc, VALUE *argv, VALUE hash)
2680{
2681 VALUE result = rb_ary_new2(argc);
2682 long i;
2683
2684 for (i=0; i<argc; i++) {
2685 rb_ary_push(result, rb_hash_aref(hash, argv[i]));
2686 }
2687 return result;
2688}
2689
2690/*
2691 * call-seq:
2692 * hash.fetch_values(*keys) -> new_array
2693 * hash.fetch_values(*keys) {|key| ... } -> new_array
2694 *
2695 * Returns a new Array containing the values associated with the given keys *keys:
2696 * h = {foo: 0, bar: 1, baz: 2}
2697 * h.fetch_values(:baz, :foo) # => [2, 0]
2698 *
2699 * Returns a new empty Array if no arguments given.
2700 *
2701 * When a block is given, calls the block with each missing key,
2702 * treating the block's return value as the value for that key:
2703 * h = {foo: 0, bar: 1, baz: 2}
2704 * values = h.fetch_values(:bar, :foo, :bad, :bam) {|key| key.to_s}
2705 * values # => [1, 0, "bad", "bam"]
2706 *
2707 * When no block is given, raises an exception if any given key is not found.
2708 */
2709
2710static VALUE
2711rb_hash_fetch_values(int argc, VALUE *argv, VALUE hash)
2712{
2713 VALUE result = rb_ary_new2(argc);
2714 long i;
2715
2716 for (i=0; i<argc; i++) {
2717 rb_ary_push(result, rb_hash_fetch(hash, argv[i]));
2718 }
2719 return result;
2720}
2721
2722static int
2723keep_if_i(VALUE key, VALUE value, VALUE hash)
2724{
2725 if (!RTEST(rb_yield_values(2, key, value))) {
2726 rb_hash_modify(hash);
2727 return ST_DELETE;
2728 }
2729 return ST_CONTINUE;
2730}
2731
2732/*
2733 * call-seq:
2734 * hash.select {|key, value| ... } -> new_hash
2735 * hash.select -> new_enumerator
2736 *
2737 * Returns a new \Hash object whose entries are those for which the block returns a truthy value:
2738 * h = {foo: 0, bar: 1, baz: 2}
2739 * h.select {|key, value| value < 2 } # => {:foo=>0, :bar=>1}
2740 *
2741 * Returns a new Enumerator if no block given:
2742 * h = {foo: 0, bar: 1, baz: 2}
2743 * e = h.select # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:select>
2744 * e.each {|key, value| value < 2 } # => {:foo=>0, :bar=>1}
2745 */
2746
2747static VALUE
2748rb_hash_select(VALUE hash)
2749{
2750 VALUE result;
2751
2752 RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
2753 result = hash_dup_with_compare_by_id(hash);
2754 if (!RHASH_EMPTY_P(hash)) {
2755 rb_hash_foreach(result, keep_if_i, result);
2756 compact_after_delete(result);
2757 }
2758 return result;
2759}
2760
2761/*
2762 * call-seq:
2763 * hash.select! {|key, value| ... } -> self or nil
2764 * hash.select! -> new_enumerator
2765 *
2766 * Returns +self+, whose entries are those for which the block returns a truthy value:
2767 * h = {foo: 0, bar: 1, baz: 2}
2768 * h.select! {|key, value| value < 2 } => {:foo=>0, :bar=>1}
2769 *
2770 * Returns +nil+ if no entries were removed.
2771 *
2772 * Returns a new Enumerator if no block given:
2773 * h = {foo: 0, bar: 1, baz: 2}
2774 * e = h.select! # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:select!>
2775 * e.each { |key, value| value < 2 } # => {:foo=>0, :bar=>1}
2776 */
2777
2778static VALUE
2779rb_hash_select_bang(VALUE hash)
2780{
2781 st_index_t n;
2782
2783 RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
2784 rb_hash_modify_check(hash);
2785 n = RHASH_SIZE(hash);
2786 if (!n) return Qnil;
2787 rb_hash_foreach(hash, keep_if_i, hash);
2788 if (n == RHASH_SIZE(hash)) return Qnil;
2789 return hash;
2790}
2791
2792/*
2793 * call-seq:
2794 * hash.keep_if {|key, value| ... } -> self
2795 * hash.keep_if -> new_enumerator
2796 *
2797 * Calls the block for each key-value pair;
2798 * retains the entry if the block returns a truthy value;
2799 * otherwise deletes the entry; returns +self+.
2800 * h = {foo: 0, bar: 1, baz: 2}
2801 * h.keep_if { |key, value| key.start_with?('b') } # => {:bar=>1, :baz=>2}
2802 *
2803 * Returns a new Enumerator if no block given:
2804 * h = {foo: 0, bar: 1, baz: 2}
2805 * e = h.keep_if # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:keep_if>
2806 * e.each { |key, value| key.start_with?('b') } # => {:bar=>1, :baz=>2}
2807 */
2808
2809static VALUE
2810rb_hash_keep_if(VALUE hash)
2811{
2812 RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
2813 rb_hash_modify_check(hash);
2814 if (!RHASH_TABLE_EMPTY_P(hash)) {
2815 rb_hash_foreach(hash, keep_if_i, hash);
2816 }
2817 return hash;
2818}
2819
2820static int
2821clear_i(VALUE key, VALUE value, VALUE dummy)
2822{
2823 return ST_DELETE;
2824}
2825
2826/*
2827 * call-seq:
2828 * hash.clear -> self
2829 *
2830 * Removes all hash entries; returns +self+.
2831 */
2832
2833 VALUE
2834rb_hash_clear(VALUE hash)
2835{
2836 rb_hash_modify_check(hash);
2837
2838 if (hash_iterating_p(hash)) {
2839 rb_hash_foreach(hash, clear_i, 0);
2840 }
2841 else if (RHASH_AR_TABLE_P(hash)) {
2842 ar_clear(hash);
2843 }
2844 else {
2845 st_clear(RHASH_ST_TABLE(hash));
2846 compact_after_delete(hash);
2847 }
2848
2849 return hash;
2850}
2851
2852static int
2853hash_aset(st_data_t *key, st_data_t *val, struct update_arg *arg, int existing)
2854{
2855 *val = arg->arg;
2856 return ST_CONTINUE;
2857}
2858
2859 VALUE
2860rb_hash_key_str(VALUE key)
2861{
2862 if (!RB_FL_ANY_RAW(key, FL_EXIVAR) && RBASIC_CLASS(key) == rb_cString) {
2863 return rb_fstring(key);
2864 }
2865 else {
2866 return rb_str_new_frozen(key);
2867 }
2868}
2869
2870static int
2871hash_aset_str(st_data_t *key, st_data_t *val, struct update_arg *arg, int existing)
2872{
2873 if (!existing && !RB_OBJ_FROZEN(*key)) {
2874 *key = rb_hash_key_str(*key);
2875 }
2876 return hash_aset(key, val, arg, existing);
2877}
2878
2879NOINSERT_UPDATE_CALLBACK(hash_aset)
2880NOINSERT_UPDATE_CALLBACK(hash_aset_str)
2881
2882/*
2883 * call-seq:
2884 * hash[key] = value -> value
2885 * hash.store(key, value)
2886 *
2887 * Associates the given +value+ with the given +key+; returns +value+.
2888 *
2889 * If the given +key+ exists, replaces its value with the given +value+;
2890 * the ordering is not affected
2891 * (see {Entry Order}[rdoc-ref:Hash@Entry+Order]):
2892 * h = {foo: 0, bar: 1}
2893 * h[:foo] = 2 # => 2
2894 * h.store(:bar, 3) # => 3
2895 * h # => {:foo=>2, :bar=>3}
2896 *
2897 * If +key+ does not exist, adds the +key+ and +value+;
2898 * the new entry is last in the order
2899 * (see {Entry Order}[rdoc-ref:Hash@Entry+Order]):
2900 * h = {foo: 0, bar: 1}
2901 * h[:baz] = 2 # => 2
2902 * h.store(:bat, 3) # => 3
2903 * h # => {:foo=>0, :bar=>1, :baz=>2, :bat=>3}
2904 */
2905
2906 VALUE
2907rb_hash_aset(VALUE hash, VALUE key, VALUE val)
2908{
2909 bool iter_p = hash_iterating_p(hash);
2910
2911 rb_hash_modify(hash);
2912
2913 if (!RHASH_STRING_KEY_P(hash, key)) {
2914 RHASH_UPDATE_ITER(hash, iter_p, key, hash_aset, val);
2915 }
2916 else {
2917 RHASH_UPDATE_ITER(hash, iter_p, key, hash_aset_str, val);
2918 }
2919 return val;
2920}
2921
2922/*
2923 * call-seq:
2924 * hash.replace(other_hash) -> self
2925 *
2926 * Replaces the entire contents of +self+ with the contents of +other_hash+;
2927 * returns +self+:
2928 * h = {foo: 0, bar: 1, baz: 2}
2929 * h.replace({bat: 3, bam: 4}) # => {:bat=>3, :bam=>4}
2930 */
2931
2932static VALUE
2933rb_hash_replace(VALUE hash, VALUE hash2)
2934{
2935 rb_hash_modify_check(hash);
2936 if (hash == hash2) return hash;
2937 if (hash_iterating_p(hash)) {
2938 rb_raise(rb_eRuntimeError, "can't replace hash during iteration");
2939 }
2940 hash2 = to_hash(hash2);
2941
2942 COPY_DEFAULT(hash, hash2);
2943
2944 if (RHASH_AR_TABLE_P(hash)) {
2945 hash_ar_free_and_clear_table(hash);
2946 }
2947 else {
2948 hash_st_free_and_clear_table(hash);
2949 }
2950
2951 hash_copy(hash, hash2);
2952
2953 return hash;
2954}
2955
2956/*
2957 * call-seq:
2958 * hash.length -> integer
2959 * hash.size -> integer
2960 *
2961 * Returns the count of entries in +self+:
2962 *
2963 * {foo: 0, bar: 1, baz: 2}.length # => 3
2964 *
2965 */
2966
2967 VALUE
2968rb_hash_size(VALUE hash)
2969{
2970 return INT2FIX(RHASH_SIZE(hash));
2971}
2972
2973size_t
2974rb_hash_size_num(VALUE hash)
2975{
2976 return (long)RHASH_SIZE(hash);
2977}
2978
2979/*
2980 * call-seq:
2981 * hash.empty? -> true or false
2982 *
2983 * Returns +true+ if there are no hash entries, +false+ otherwise:
2984 * {}.empty? # => true
2985 * {foo: 0, bar: 1, baz: 2}.empty? # => false
2986 */
2987
2988static VALUE
2989rb_hash_empty_p(VALUE hash)
2990{
2991 return RBOOL(RHASH_EMPTY_P(hash));
2992}
2993
2994static int
2995each_value_i(VALUE key, VALUE value, VALUE _)
2996{
2997 rb_yield(value);
2998 return ST_CONTINUE;
2999}
3000
3001/*
3002 * call-seq:
3003 * hash.each_value {|value| ... } -> self
3004 * hash.each_value -> new_enumerator
3005 *
3006 * Calls the given block with each value; returns +self+:
3007 * h = {foo: 0, bar: 1, baz: 2}
3008 * h.each_value {|value| puts value } # => {:foo=>0, :bar=>1, :baz=>2}
3009 * Output:
3010 * 0
3011 * 1
3012 * 2
3013 *
3014 * Returns a new Enumerator if no block given:
3015 * h = {foo: 0, bar: 1, baz: 2}
3016 * e = h.each_value # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:each_value>
3017 * h1 = e.each {|value| puts value }
3018 * h1 # => {:foo=>0, :bar=>1, :baz=>2}
3019 * Output:
3020 * 0
3021 * 1
3022 * 2
3023 */
3024
3025static VALUE
3026rb_hash_each_value(VALUE hash)
3027{
3028 RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
3029 rb_hash_foreach(hash, each_value_i, 0);
3030 return hash;
3031}
3032
3033static int
3034each_key_i(VALUE key, VALUE value, VALUE _)
3035{
3036 rb_yield(key);
3037 return ST_CONTINUE;
3038}
3039
3040/*
3041 * call-seq:
3042 * hash.each_key {|key| ... } -> self
3043 * hash.each_key -> new_enumerator
3044 *
3045 * Calls the given block with each key; returns +self+:
3046 * h = {foo: 0, bar: 1, baz: 2}
3047 * h.each_key {|key| puts key } # => {:foo=>0, :bar=>1, :baz=>2}
3048 * Output:
3049 * foo
3050 * bar
3051 * baz
3052 *
3053 * Returns a new Enumerator if no block given:
3054 * h = {foo: 0, bar: 1, baz: 2}
3055 * e = h.each_key # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:each_key>
3056 * h1 = e.each {|key| puts key }
3057 * h1 # => {:foo=>0, :bar=>1, :baz=>2}
3058 * Output:
3059 * foo
3060 * bar
3061 * baz
3062 */
3063static VALUE
3064rb_hash_each_key(VALUE hash)
3065{
3066 RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
3067 rb_hash_foreach(hash, each_key_i, 0);
3068 return hash;
3069}
3070
3071static int
3072each_pair_i(VALUE key, VALUE value, VALUE _)
3073{
3074 rb_yield(rb_assoc_new(key, value));
3075 return ST_CONTINUE;
3076}
3077
3078static int
3079each_pair_i_fast(VALUE key, VALUE value, VALUE _)
3080{
3081 VALUE argv[2];
3082 argv[0] = key;
3083 argv[1] = value;
3084 rb_yield_values2(2, argv);
3085 return ST_CONTINUE;
3086}
3087
3088/*
3089 * call-seq:
3090 * hash.each {|key, value| ... } -> self
3091 * hash.each_pair {|key, value| ... } -> self
3092 * hash.each -> new_enumerator
3093 * hash.each_pair -> new_enumerator
3094 *
3095 * Calls the given block with each key-value pair; returns +self+:
3096 * h = {foo: 0, bar: 1, baz: 2}
3097 * h.each_pair {|key, value| puts "#{key}: #{value}"} # => {:foo=>0, :bar=>1, :baz=>2}
3098 * Output:
3099 * foo: 0
3100 * bar: 1
3101 * baz: 2
3102 *
3103 * Returns a new Enumerator if no block given:
3104 * h = {foo: 0, bar: 1, baz: 2}
3105 * e = h.each_pair # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:each_pair>
3106 * h1 = e.each {|key, value| puts "#{key}: #{value}"}
3107 * h1 # => {:foo=>0, :bar=>1, :baz=>2}
3108 * Output:
3109 * foo: 0
3110 * bar: 1
3111 * baz: 2
3112 */
3113
3114static VALUE
3115rb_hash_each_pair(VALUE hash)
3116{
3117 RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
3118 if (rb_block_pair_yield_optimizable())
3119 rb_hash_foreach(hash, each_pair_i_fast, 0);
3120 else
3121 rb_hash_foreach(hash, each_pair_i, 0);
3122 return hash;
3123}
3124
3125 struct transform_keys_args{
3126 VALUE trans;
3127 VALUE result;
3128 int block_given;
3129};
3130
3131static int
3132transform_keys_hash_i(VALUE key, VALUE value, VALUE transarg)
3133{
3134 struct transform_keys_args *p = (void *)transarg;
3135 VALUE trans = p->trans, result = p->result;
3136 VALUE new_key = rb_hash_lookup2(trans, key, Qundef);
3137 if (UNDEF_P(new_key)) {
3138 if (p->block_given)
3139 new_key = rb_yield(key);
3140 else
3141 new_key = key;
3142 }
3143 rb_hash_aset(result, new_key, value);
3144 return ST_CONTINUE;
3145}
3146
3147static int
3148transform_keys_i(VALUE key, VALUE value, VALUE result)
3149{
3150 VALUE new_key = rb_yield(key);
3151 rb_hash_aset(result, new_key, value);
3152 return ST_CONTINUE;
3153}
3154
3155/*
3156 * call-seq:
3157 * hash.transform_keys {|key| ... } -> new_hash
3158 * hash.transform_keys(hash2) -> new_hash
3159 * hash.transform_keys(hash2) {|other_key| ...} -> new_hash
3160 * hash.transform_keys -> new_enumerator
3161 *
3162 * Returns a new \Hash object; each entry has:
3163 * * A key provided by the block.
3164 * * The value from +self+.
3165 *
3166 * An optional hash argument can be provided to map keys to new keys.
3167 * Any key not given will be mapped using the provided block,
3168 * or remain the same if no block is given.
3169 *
3170 * Transform keys:
3171 * h = {foo: 0, bar: 1, baz: 2}
3172 * h1 = h.transform_keys {|key| key.to_s }
3173 * h1 # => {"foo"=>0, "bar"=>1, "baz"=>2}
3174 *
3175 * h.transform_keys(foo: :bar, bar: :foo)
3176 * #=> {bar: 0, foo: 1, baz: 2}
3177 *
3178 * h.transform_keys(foo: :hello, &:to_s)
3179 * #=> {:hello=>0, "bar"=>1, "baz"=>2}
3180 *
3181 * Overwrites values for duplicate keys:
3182 * h = {foo: 0, bar: 1, baz: 2}
3183 * h1 = h.transform_keys {|key| :bat }
3184 * h1 # => {:bat=>2}
3185 *
3186 * Returns a new Enumerator if no block given:
3187 * h = {foo: 0, bar: 1, baz: 2}
3188 * e = h.transform_keys # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:transform_keys>
3189 * h1 = e.each { |key| key.to_s }
3190 * h1 # => {"foo"=>0, "bar"=>1, "baz"=>2}
3191 */
3192static VALUE
3193rb_hash_transform_keys(int argc, VALUE *argv, VALUE hash)
3194{
3195 VALUE result;
3196 struct transform_keys_args transarg = {0};
3197
3198 argc = rb_check_arity(argc, 0, 1);
3199 if (argc > 0) {
3200 transarg.trans = to_hash(argv[0]);
3201 transarg.block_given = rb_block_given_p();
3202 }
3203 else {
3204 RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
3205 }
3206 result = rb_hash_new();
3207 if (!RHASH_EMPTY_P(hash)) {
3208 if (transarg.trans) {
3209 transarg.result = result;
3210 rb_hash_foreach(hash, transform_keys_hash_i, (VALUE)&transarg);
3211 }
3212 else {
3213 rb_hash_foreach(hash, transform_keys_i, result);
3214 }
3215 }
3216
3217 return result;
3218}
3219
3220static int flatten_i(VALUE key, VALUE val, VALUE ary);
3221
3222/*
3223 * call-seq:
3224 * hash.transform_keys! {|key| ... } -> self
3225 * hash.transform_keys!(hash2) -> self
3226 * hash.transform_keys!(hash2) {|other_key| ...} -> self
3227 * hash.transform_keys! -> new_enumerator
3228 *
3229 * Same as Hash#transform_keys but modifies the receiver in place
3230 * instead of returning a new hash.
3231 */
3232static VALUE
3233rb_hash_transform_keys_bang(int argc, VALUE *argv, VALUE hash)
3234{
3235 VALUE trans = 0;
3236 int block_given = 0;
3237
3238 argc = rb_check_arity(argc, 0, 1);
3239 if (argc > 0) {
3240 trans = to_hash(argv[0]);
3241 block_given = rb_block_given_p();
3242 }
3243 else {
3244 RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
3245 }
3246 rb_hash_modify_check(hash);
3247 if (!RHASH_TABLE_EMPTY_P(hash)) {
3248 long i;
3249 VALUE new_keys = hash_alloc(0);
3250 VALUE pairs = rb_ary_hidden_new(RHASH_SIZE(hash) * 2);
3251 rb_hash_foreach(hash, flatten_i, pairs);
3252 for (i = 0; i < RARRAY_LEN(pairs); i += 2) {
3253 VALUE key = RARRAY_AREF(pairs, i), new_key, val;
3254
3255 if (!trans) {
3256 new_key = rb_yield(key);
3257 }
3258 else if (!UNDEF_P(new_key = rb_hash_lookup2(trans, key, Qundef))) {
3259 /* use the transformed key */
3260 }
3261 else if (block_given) {
3262 new_key = rb_yield(key);
3263 }
3264 else {
3265 new_key = key;
3266 }
3267 val = RARRAY_AREF(pairs, i+1);
3268 if (!hash_stlike_lookup(new_keys, key, NULL)) {
3269 rb_hash_stlike_delete(hash, &key, NULL);
3270 }
3271 rb_hash_aset(hash, new_key, val);
3272 rb_hash_aset(new_keys, new_key, Qnil);
3273 }
3274 rb_ary_clear(pairs);
3275 rb_hash_clear(new_keys);
3276 }
3277 compact_after_delete(hash);
3278 return hash;
3279}
3280
3281static int
3282transform_values_foreach_func(st_data_t key, st_data_t value, st_data_t argp, int error)
3283{
3284 return ST_REPLACE;
3285}
3286
3287static int
3288transform_values_foreach_replace(st_data_t *key, st_data_t *value, st_data_t argp, int existing)
3289{
3290 VALUE new_value = rb_yield((VALUE)*value);
3291 VALUE hash = (VALUE)argp;
3292 rb_hash_modify(hash);
3293 RB_OBJ_WRITE(hash, value, new_value);
3294 return ST_CONTINUE;
3295}
3296
3297/*
3298 * call-seq:
3299 * hash.transform_values {|value| ... } -> new_hash
3300 * hash.transform_values -> new_enumerator
3301 *
3302 * Returns a new \Hash object; each entry has:
3303 * * A key from +self+.
3304 * * A value provided by the block.
3305 *
3306 * Transform values:
3307 * h = {foo: 0, bar: 1, baz: 2}
3308 * h1 = h.transform_values {|value| value * 100}
3309 * h1 # => {:foo=>0, :bar=>100, :baz=>200}
3310 *
3311 * Returns a new Enumerator if no block given:
3312 * h = {foo: 0, bar: 1, baz: 2}
3313 * e = h.transform_values # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:transform_values>
3314 * h1 = e.each { |value| value * 100}
3315 * h1 # => {:foo=>0, :bar=>100, :baz=>200}
3316 */
3317static VALUE
3318rb_hash_transform_values(VALUE hash)
3319{
3320 VALUE result;
3321
3322 RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
3323 result = hash_dup_with_compare_by_id(hash);
3324 SET_DEFAULT(result, Qnil);
3325
3326 if (!RHASH_EMPTY_P(hash)) {
3327 rb_hash_stlike_foreach_with_replace(result, transform_values_foreach_func, transform_values_foreach_replace, result);
3328 compact_after_delete(result);
3329 }
3330
3331 return result;
3332}
3333
3334/*
3335 * call-seq:
3336 * hash.transform_values! {|value| ... } -> self
3337 * hash.transform_values! -> new_enumerator
3338 *
3339 * Returns +self+, whose keys are unchanged, and whose values are determined by the given block.
3340 * h = {foo: 0, bar: 1, baz: 2}
3341 * h.transform_values! {|value| value * 100} # => {:foo=>0, :bar=>100, :baz=>200}
3342 *
3343 * Returns a new Enumerator if no block given:
3344 * h = {foo: 0, bar: 1, baz: 2}
3345 * e = h.transform_values! # => #<Enumerator: {:foo=>0, :bar=>100, :baz=>200}:transform_values!>
3346 * h1 = e.each {|value| value * 100}
3347 * h1 # => {:foo=>0, :bar=>100, :baz=>200}
3348 */
3349static VALUE
3350rb_hash_transform_values_bang(VALUE hash)
3351{
3352 RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
3353 rb_hash_modify_check(hash);
3354
3355 if (!RHASH_TABLE_EMPTY_P(hash)) {
3356 rb_hash_stlike_foreach_with_replace(hash, transform_values_foreach_func, transform_values_foreach_replace, hash);
3357 }
3358
3359 return hash;
3360}
3361
3362static int
3363to_a_i(VALUE key, VALUE value, VALUE ary)
3364{
3365 rb_ary_push(ary, rb_assoc_new(key, value));
3366 return ST_CONTINUE;
3367}
3368
3369/*
3370 * call-seq:
3371 * hash.to_a -> new_array
3372 *
3373 * Returns a new Array of 2-element Array objects;
3374 * each nested Array contains a key-value pair from +self+:
3375 * h = {foo: 0, bar: 1, baz: 2}
3376 * h.to_a # => [[:foo, 0], [:bar, 1], [:baz, 2]]
3377 */
3378
3379static VALUE
3380rb_hash_to_a(VALUE hash)
3381{
3382 VALUE ary;
3383
3384 ary = rb_ary_new_capa(RHASH_SIZE(hash));
3385 rb_hash_foreach(hash, to_a_i, ary);
3386
3387 return ary;
3388}
3389
3390static int
3391inspect_i(VALUE key, VALUE value, VALUE str)
3392{
3393 VALUE str2;
3394
3395 str2 = rb_inspect(key);
3396 if (RSTRING_LEN(str) > 1) {
3397 rb_str_buf_cat_ascii(str, ", ");
3398 }
3399 else {
3400 rb_enc_copy(str, str2);
3401 }
3402 rb_str_buf_append(str, str2);
3403 rb_str_buf_cat_ascii(str, "=>");
3404 str2 = rb_inspect(value);
3405 rb_str_buf_append(str, str2);
3406
3407 return ST_CONTINUE;
3408}
3409
3410static VALUE
3411inspect_hash(VALUE hash, VALUE dummy, int recur)
3412{
3413 VALUE str;
3414
3415 if (recur) return rb_usascii_str_new2("{...}");
3416 str = rb_str_buf_new2("{");
3417 rb_hash_foreach(hash, inspect_i, str);
3418 rb_str_buf_cat2(str, "}");
3419
3420 return str;
3421}
3422
3423/*
3424 * call-seq:
3425 * hash.inspect -> new_string
3426 *
3427 * Returns a new String containing the hash entries:
3428
3429 * h = {foo: 0, bar: 1, baz: 2}
3430 * h.inspect # => "{:foo=>0, :bar=>1, :baz=>2}"
3431 *
3432 */
3433
3434static VALUE
3435rb_hash_inspect(VALUE hash)
3436{
3437 if (RHASH_EMPTY_P(hash))
3438 return rb_usascii_str_new2("{}");
3439 return rb_exec_recursive(inspect_hash, hash, 0);
3440}
3441
3442/*
3443 * call-seq:
3444 * hash.to_hash -> self
3445 *
3446 * Returns +self+.
3447 */
3448static VALUE
3449rb_hash_to_hash(VALUE hash)
3450{
3451 return hash;
3452}
3453
3454 VALUE
3455rb_hash_set_pair(VALUE hash, VALUE arg)
3456{
3457 VALUE pair;
3458
3459 pair = rb_check_array_type(arg);
3460 if (NIL_P(pair)) {
3461 rb_raise(rb_eTypeError, "wrong element type %s (expected array)",
3462 rb_builtin_class_name(arg));
3463 }
3464 if (RARRAY_LEN(pair) != 2) {
3465 rb_raise(rb_eArgError, "element has wrong array length (expected 2, was %ld)",
3466 RARRAY_LEN(pair));
3467 }
3468 rb_hash_aset(hash, RARRAY_AREF(pair, 0), RARRAY_AREF(pair, 1));
3469 return hash;
3470}
3471
3472static int
3473to_h_i(VALUE key, VALUE value, VALUE hash)
3474{
3475 rb_hash_set_pair(hash, rb_yield_values(2, key, value));
3476 return ST_CONTINUE;
3477}
3478
3479static VALUE
3480rb_hash_to_h_block(VALUE hash)
3481{
3482 VALUE h = rb_hash_new_with_size(RHASH_SIZE(hash));
3483 rb_hash_foreach(hash, to_h_i, h);
3484 return h;
3485}
3486
3487/*
3488 * call-seq:
3489 * hash.to_h -> self or new_hash
3490 * hash.to_h {|key, value| ... } -> new_hash
3491 *
3492 * For an instance of \Hash, returns +self+.
3493 *
3494 * For a subclass of \Hash, returns a new \Hash
3495 * containing the content of +self+.
3496 *
3497 * When a block is given, returns a new \Hash object
3498 * whose content is based on the block;
3499 * the block should return a 2-element Array object
3500 * specifying the key-value pair to be included in the returned Array:
3501 * h = {foo: 0, bar: 1, baz: 2}
3502 * h1 = h.to_h {|key, value| [value, key] }
3503 * h1 # => {0=>:foo, 1=>:bar, 2=>:baz}
3504 */
3505
3506static VALUE
3507rb_hash_to_h(VALUE hash)
3508{
3509 if (rb_block_given_p()) {
3510 return rb_hash_to_h_block(hash);
3511 }
3512 if (rb_obj_class(hash) != rb_cHash) {
3513 const VALUE flags = RBASIC(hash)->flags;
3514 hash = hash_dup(hash, rb_cHash, flags & RHASH_PROC_DEFAULT);
3515 }
3516 return hash;
3517}
3518
3519static int
3520keys_i(VALUE key, VALUE value, VALUE ary)
3521{
3522 rb_ary_push(ary, key);
3523 return ST_CONTINUE;
3524}
3525
3526/*
3527 * call-seq:
3528 * hash.keys -> new_array
3529 *
3530 * Returns a new Array containing all keys in +self+:
3531 * h = {foo: 0, bar: 1, baz: 2}
3532 * h.keys # => [:foo, :bar, :baz]
3533 */
3534
3535 VALUE
3536rb_hash_keys(VALUE hash)
3537{
3538 st_index_t size = RHASH_SIZE(hash);
3539 VALUE keys = rb_ary_new_capa(size);
3540
3541 if (size == 0) return keys;
3542
3543 if (ST_DATA_COMPATIBLE_P(VALUE)) {
3544 RARRAY_PTR_USE(keys, ptr, {
3545 if (RHASH_AR_TABLE_P(hash)) {
3546 size = ar_keys(hash, ptr, size);
3547 }
3548 else {
3549 st_table *table = RHASH_ST_TABLE(hash);
3550 size = st_keys(table, ptr, size);
3551 }
3552 });
3553 rb_gc_writebarrier_remember(keys);
3554 rb_ary_set_len(keys, size);
3555 }
3556 else {
3557 rb_hash_foreach(hash, keys_i, keys);
3558 }
3559
3560 return keys;
3561}
3562
3563static int
3564values_i(VALUE key, VALUE value, VALUE ary)
3565{
3566 rb_ary_push(ary, value);
3567 return ST_CONTINUE;
3568}
3569
3570/*
3571 * call-seq:
3572 * hash.values -> new_array
3573 *
3574 * Returns a new Array containing all values in +self+:
3575 * h = {foo: 0, bar: 1, baz: 2}
3576 * h.values # => [0, 1, 2]
3577 */
3578
3579 VALUE
3580rb_hash_values(VALUE hash)
3581{
3582 VALUE values;
3583 st_index_t size = RHASH_SIZE(hash);
3584
3585 values = rb_ary_new_capa(size);
3586 if (size == 0) return values;
3587
3588 if (ST_DATA_COMPATIBLE_P(VALUE)) {
3589 if (RHASH_AR_TABLE_P(hash)) {
3590 rb_gc_writebarrier_remember(values);
3591 RARRAY_PTR_USE(values, ptr, {
3592 size = ar_values(hash, ptr, size);
3593 });
3594 }
3595 else if (RHASH_ST_TABLE_P(hash)) {
3596 st_table *table = RHASH_ST_TABLE(hash);
3597 rb_gc_writebarrier_remember(values);
3598 RARRAY_PTR_USE(values, ptr, {
3599 size = st_values(table, ptr, size);
3600 });
3601 }
3602 rb_ary_set_len(values, size);
3603 }
3604
3605 else {
3606 rb_hash_foreach(hash, values_i, values);
3607 }
3608
3609 return values;
3610}
3611
3612/*
3613 * call-seq:
3614 * hash.include?(key) -> true or false
3615 * hash.has_key?(key) -> true or false
3616 * hash.key?(key) -> true or false
3617 * hash.member?(key) -> true or false
3618 *
3619 * Returns +true+ if +key+ is a key in +self+, otherwise +false+.
3620 */
3621
3622 VALUE
3623rb_hash_has_key(VALUE hash, VALUE key)
3624{
3625 return RBOOL(hash_stlike_lookup(hash, key, NULL));
3626}
3627
3628static int
3629rb_hash_search_value(VALUE key, VALUE value, VALUE arg)
3630{
3631 VALUE *data = (VALUE *)arg;
3632
3633 if (rb_equal(value, data[1])) {
3634 data[0] = Qtrue;
3635 return ST_STOP;
3636 }
3637 return ST_CONTINUE;
3638}
3639
3640/*
3641 * call-seq:
3642 * hash.has_value?(value) -> true or false
3643 * hash.value?(value) -> true or false
3644 *
3645 * Returns +true+ if +value+ is a value in +self+, otherwise +false+.
3646 */
3647
3648static VALUE
3649rb_hash_has_value(VALUE hash, VALUE val)
3650{
3651 VALUE data[2];
3652
3653 data[0] = Qfalse;
3654 data[1] = val;
3655 rb_hash_foreach(hash, rb_hash_search_value, (VALUE)data);
3656 return data[0];
3657}
3658
3659 struct equal_data {
3660 VALUE result;
3661 VALUE hash;
3662 int eql;
3663};
3664
3665static int
3666eql_i(VALUE key, VALUE val1, VALUE arg)
3667{
3668 struct equal_data *data = (struct equal_data *)arg;
3669 st_data_t val2;
3670
3671 if (!hash_stlike_lookup(data->hash, key, &val2)) {
3672 data->result = Qfalse;
3673 return ST_STOP;
3674 }
3675 else {
3676 if (!(data->eql ? rb_eql(val1, (VALUE)val2) : (int)rb_equal(val1, (VALUE)val2))) {
3677 data->result = Qfalse;
3678 return ST_STOP;
3679 }
3680 return ST_CONTINUE;
3681 }
3682}
3683
3684static VALUE
3685recursive_eql(VALUE hash, VALUE dt, int recur)
3686{
3687 struct equal_data *data;
3688
3689 if (recur) return Qtrue; /* Subtle! */
3690 data = (struct equal_data*)dt;
3691 data->result = Qtrue;
3692 rb_hash_foreach(hash, eql_i, dt);
3693
3694 return data->result;
3695}
3696
3697static VALUE
3698hash_equal(VALUE hash1, VALUE hash2, int eql)
3699{
3700 struct equal_data data;
3701
3702 if (hash1 == hash2) return Qtrue;
3703 if (!RB_TYPE_P(hash2, T_HASH)) {
3704 if (!rb_respond_to(hash2, idTo_hash)) {
3705 return Qfalse;
3706 }
3707 if (eql) {
3708 if (rb_eql(hash2, hash1)) {
3709 return Qtrue;
3710 }
3711 else {
3712 return Qfalse;
3713 }
3714 }
3715 else {
3716 return rb_equal(hash2, hash1);
3717 }
3718 }
3719 if (RHASH_SIZE(hash1) != RHASH_SIZE(hash2))
3720 return Qfalse;
3721 if (!RHASH_TABLE_EMPTY_P(hash1) && !RHASH_TABLE_EMPTY_P(hash2)) {
3722 if (RHASH_TYPE(hash1) != RHASH_TYPE(hash2)) {
3723 return Qfalse;
3724 }
3725 else {
3726 data.hash = hash2;
3727 data.eql = eql;
3728 return rb_exec_recursive_paired(recursive_eql, hash1, hash2, (VALUE)&data);
3729 }
3730 }
3731
3732#if 0
3733 if (!(rb_equal(RHASH_IFNONE(hash1), RHASH_IFNONE(hash2)) &&
3734 FL_TEST(hash1, RHASH_PROC_DEFAULT) == FL_TEST(hash2, RHASH_PROC_DEFAULT)))
3735 return Qfalse;
3736#endif
3737 return Qtrue;
3738}
3739
3740/*
3741 * call-seq:
3742 * hash == object -> true or false
3743 *
3744 * Returns +true+ if all of the following are true:
3745 * * +object+ is a \Hash object.
3746 * * +hash+ and +object+ have the same keys (regardless of order).
3747 * * For each key +key+, <tt>hash[key] == object[key]</tt>.
3748 *
3749 * Otherwise, returns +false+.
3750 *
3751 * Equal:
3752 * h1 = {foo: 0, bar: 1, baz: 2}
3753 * h2 = {foo: 0, bar: 1, baz: 2}
3754 * h1 == h2 # => true
3755 * h3 = {baz: 2, bar: 1, foo: 0}
3756 * h1 == h3 # => true
3757 */
3758
3759static VALUE
3760rb_hash_equal(VALUE hash1, VALUE hash2)
3761{
3762 return hash_equal(hash1, hash2, FALSE);
3763}
3764
3765/*
3766 * call-seq:
3767 * hash.eql? object -> true or false
3768 *
3769 * Returns +true+ if all of the following are true:
3770 * * +object+ is a \Hash object.
3771 * * +hash+ and +object+ have the same keys (regardless of order).
3772 * * For each key +key+, <tt>h[key] eql? object[key]</tt>.
3773 *
3774 * Otherwise, returns +false+.
3775 *
3776 * Equal:
3777 * h1 = {foo: 0, bar: 1, baz: 2}
3778 * h2 = {foo: 0, bar: 1, baz: 2}
3779 * h1.eql? h2 # => true
3780 * h3 = {baz: 2, bar: 1, foo: 0}
3781 * h1.eql? h3 # => true
3782 */
3783
3784static VALUE
3785rb_hash_eql(VALUE hash1, VALUE hash2)
3786{
3787 return hash_equal(hash1, hash2, TRUE);
3788}
3789
3790static int
3791hash_i(VALUE key, VALUE val, VALUE arg)
3792{
3793 st_index_t *hval = (st_index_t *)arg;
3794 st_index_t hdata[2];
3795
3796 hdata[0] = rb_hash(key);
3797 hdata[1] = rb_hash(val);
3798 *hval ^= st_hash(hdata, sizeof(hdata), 0);
3799 return ST_CONTINUE;
3800}
3801
3802/*
3803 * call-seq:
3804 * hash.hash -> an_integer
3805 *
3806 * Returns the Integer hash-code for the hash.
3807 *
3808 * Two \Hash objects have the same hash-code if their content is the same
3809 * (regardless of order):
3810 * h1 = {foo: 0, bar: 1, baz: 2}
3811 * h2 = {baz: 2, bar: 1, foo: 0}
3812 * h2.hash == h1.hash # => true
3813 * h2.eql? h1 # => true
3814 */
3815
3816static VALUE
3817rb_hash_hash(VALUE hash)
3818{
3819 st_index_t size = RHASH_SIZE(hash);
3820 st_index_t hval = rb_hash_start(size);
3821 hval = rb_hash_uint(hval, (st_index_t)rb_hash_hash);
3822 if (size) {
3823 rb_hash_foreach(hash, hash_i, (VALUE)&hval);
3824 }
3825 hval = rb_hash_end(hval);
3826 return ST2FIX(hval);
3827}
3828
3829static int
3830rb_hash_invert_i(VALUE key, VALUE value, VALUE hash)
3831{
3832 rb_hash_aset(hash, value, key);
3833 return ST_CONTINUE;
3834}
3835
3836/*
3837 * call-seq:
3838 * hash.invert -> new_hash
3839 *
3840 * Returns a new \Hash object with the each key-value pair inverted:
3841 * h = {foo: 0, bar: 1, baz: 2}
3842 * h1 = h.invert
3843 * h1 # => {0=>:foo, 1=>:bar, 2=>:baz}
3844 *
3845 * Overwrites any repeated new keys:
3846 * (see {Entry Order}[rdoc-ref:Hash@Entry+Order]):
3847 * h = {foo: 0, bar: 0, baz: 0}
3848 * h.invert # => {0=>:baz}
3849 */
3850
3851static VALUE
3852rb_hash_invert(VALUE hash)
3853{
3854 VALUE h = rb_hash_new_with_size(RHASH_SIZE(hash));
3855
3856 rb_hash_foreach(hash, rb_hash_invert_i, h);
3857 return h;
3858}
3859
3860static int
3861rb_hash_update_i(VALUE key, VALUE value, VALUE hash)
3862{
3863 rb_hash_aset(hash, key, value);
3864 return ST_CONTINUE;
3865}
3866
3867static int
3868rb_hash_update_block_callback(st_data_t *key, st_data_t *value, struct update_arg *arg, int existing)
3869{
3870 st_data_t newvalue = arg->arg;
3871
3872 if (existing) {
3873 newvalue = (st_data_t)rb_yield_values(3, (VALUE)*key, (VALUE)*value, (VALUE)newvalue);
3874 }
3875 else if (RHASH_STRING_KEY_P(arg->hash, *key) && !RB_OBJ_FROZEN(*key)) {
3876 *key = rb_hash_key_str(*key);
3877 }
3878 *value = newvalue;
3879 return ST_CONTINUE;
3880}
3881
3882NOINSERT_UPDATE_CALLBACK(rb_hash_update_block_callback)
3883
3884static int
3885rb_hash_update_block_i(VALUE key, VALUE value, VALUE hash)
3886{
3887 RHASH_UPDATE(hash, key, rb_hash_update_block_callback, value);
3888 return ST_CONTINUE;
3889}
3890
3891/*
3892 * call-seq:
3893 * hash.merge! -> self
3894 * hash.merge!(*other_hashes) -> self
3895 * hash.merge!(*other_hashes) { |key, old_value, new_value| ... } -> self
3896 *
3897 * Merges each of +other_hashes+ into +self+; returns +self+.
3898 *
3899 * Each argument in +other_hashes+ must be a \Hash.
3900 *
3901 * With arguments and no block:
3902 * * Returns +self+, after the given hashes are merged into it.
3903 * * The given hashes are merged left to right.
3904 * * Each new entry is added at the end.
3905 * * Each duplicate-key entry's value overwrites the previous value.
3906 *
3907 * Example:
3908 * h = {foo: 0, bar: 1, baz: 2}
3909 * h1 = {bat: 3, bar: 4}
3910 * h2 = {bam: 5, bat:6}
3911 * h.merge!(h1, h2) # => {:foo=>0, :bar=>4, :baz=>2, :bat=>6, :bam=>5}
3912 *
3913 * With arguments and a block:
3914 * * Returns +self+, after the given hashes are merged.
3915 * * The given hashes are merged left to right.
3916 * * Each new-key entry is added at the end.
3917 * * For each duplicate key:
3918 * * Calls the block with the key and the old and new values.
3919 * * The block's return value becomes the new value for the entry.
3920 *
3921 * Example:
3922 * h = {foo: 0, bar: 1, baz: 2}
3923 * h1 = {bat: 3, bar: 4}
3924 * h2 = {bam: 5, bat:6}
3925 * h3 = h.merge!(h1, h2) { |key, old_value, new_value| old_value + new_value }
3926 * h3 # => {:foo=>0, :bar=>5, :baz=>2, :bat=>9, :bam=>5}
3927 *
3928 * With no arguments:
3929 * * Returns +self+, unmodified.
3930 * * The block, if given, is ignored.
3931 *
3932 * Example:
3933 * h = {foo: 0, bar: 1, baz: 2}
3934 * h.merge # => {:foo=>0, :bar=>1, :baz=>2}
3935 * h1 = h.merge! { |key, old_value, new_value| raise 'Cannot happen' }
3936 * h1 # => {:foo=>0, :bar=>1, :baz=>2}
3937 */
3938
3939static VALUE
3940rb_hash_update(int argc, VALUE *argv, VALUE self)
3941{
3942 int i;
3943 bool block_given = rb_block_given_p();
3944
3945 rb_hash_modify(self);
3946 for (i = 0; i < argc; i++){
3947 VALUE hash = to_hash(argv[i]);
3948 if (block_given) {
3949 rb_hash_foreach(hash, rb_hash_update_block_i, self);
3950 }
3951 else {
3952 rb_hash_foreach(hash, rb_hash_update_i, self);
3953 }
3954 }
3955 return self;
3956}
3957
3958 struct update_func_arg {
3959 VALUE hash;
3960 VALUE value;
3961 rb_hash_update_func *func;
3962};
3963
3964static int
3965rb_hash_update_func_callback(st_data_t *key, st_data_t *value, struct update_arg *arg, int existing)
3966{
3967 struct update_func_arg *uf_arg = (struct update_func_arg *)arg->arg;
3968 VALUE newvalue = uf_arg->value;
3969
3970 if (existing) {
3971 newvalue = (*uf_arg->func)((VALUE)*key, (VALUE)*value, newvalue);
3972 }
3973 *value = newvalue;
3974 return ST_CONTINUE;
3975}
3976
3977NOINSERT_UPDATE_CALLBACK(rb_hash_update_func_callback)
3978
3979static int
3980rb_hash_update_func_i(VALUE key, VALUE value, VALUE arg0)
3981{
3982 struct update_func_arg *arg = (struct update_func_arg *)arg0;
3983 VALUE hash = arg->hash;
3984
3985 arg->value = value;
3986 RHASH_UPDATE(hash, key, rb_hash_update_func_callback, (VALUE)arg);
3987 return ST_CONTINUE;
3988}
3989
3990 VALUE
3991rb_hash_update_by(VALUE hash1, VALUE hash2, rb_hash_update_func *func)
3992{
3993 rb_hash_modify(hash1);
3994 hash2 = to_hash(hash2);
3995 if (func) {
3996 struct update_func_arg arg;
3997 arg.hash = hash1;
3998 arg.func = func;
3999 rb_hash_foreach(hash2, rb_hash_update_func_i, (VALUE)&arg);
4000 }
4001 else {
4002 rb_hash_foreach(hash2, rb_hash_update_i, hash1);
4003 }
4004 return hash1;
4005}
4006
4007/*
4008 * call-seq:
4009 * hash.merge -> copy_of_self
4010 * hash.merge(*other_hashes) -> new_hash
4011 * hash.merge(*other_hashes) { |key, old_value, new_value| ... } -> new_hash
4012 *
4013 * Returns the new \Hash formed by merging each of +other_hashes+
4014 * into a copy of +self+.
4015 *
4016 * Each argument in +other_hashes+ must be a \Hash.
4017 *
4018 * ---
4019 *
4020 * With arguments and no block:
4021 * * Returns the new \Hash object formed by merging each successive
4022 * \Hash in +other_hashes+ into +self+.
4023 * * Each new-key entry is added at the end.
4024 * * Each duplicate-key entry's value overwrites the previous value.
4025 *
4026 * Example:
4027 * h = {foo: 0, bar: 1, baz: 2}
4028 * h1 = {bat: 3, bar: 4}
4029 * h2 = {bam: 5, bat:6}
4030 * h.merge(h1, h2) # => {:foo=>0, :bar=>4, :baz=>2, :bat=>6, :bam=>5}
4031 *
4032 * With arguments and a block:
4033 * * Returns a new \Hash object that is the merge of +self+ and each given hash.
4034 * * The given hashes are merged left to right.
4035 * * Each new-key entry is added at the end.
4036 * * For each duplicate key:
4037 * * Calls the block with the key and the old and new values.
4038 * * The block's return value becomes the new value for the entry.
4039 *
4040 * Example:
4041 * h = {foo: 0, bar: 1, baz: 2}
4042 * h1 = {bat: 3, bar: 4}
4043 * h2 = {bam: 5, bat:6}
4044 * h3 = h.merge(h1, h2) { |key, old_value, new_value| old_value + new_value }
4045 * h3 # => {:foo=>0, :bar=>5, :baz=>2, :bat=>9, :bam=>5}
4046 *
4047 * With no arguments:
4048 * * Returns a copy of +self+.
4049 * * The block, if given, is ignored.
4050 *
4051 * Example:
4052 * h = {foo: 0, bar: 1, baz: 2}
4053 * h.merge # => {:foo=>0, :bar=>1, :baz=>2}
4054 * h1 = h.merge { |key, old_value, new_value| raise 'Cannot happen' }
4055 * h1 # => {:foo=>0, :bar=>1, :baz=>2}
4056 */
4057
4058static VALUE
4059rb_hash_merge(int argc, VALUE *argv, VALUE self)
4060{
4061 return rb_hash_update(argc, argv, copy_compare_by_id(rb_hash_dup(self), self));
4062}
4063
4064static int
4065assoc_cmp(VALUE a, VALUE b)
4066{
4067 return !RTEST(rb_equal(a, b));
4068}
4069
4070 struct assoc_arg {
4071 st_table *tbl;
4072 st_data_t key;
4073};
4074
4075static VALUE
4076assoc_lookup(VALUE arg)
4077{
4078 struct assoc_arg *p = (struct assoc_arg*)arg;
4079 st_data_t data;
4080 if (st_lookup(p->tbl, p->key, &data)) return (VALUE)data;
4081 return Qundef;
4082}
4083
4084static int
4085assoc_i(VALUE key, VALUE val, VALUE arg)
4086{
4087 VALUE *args = (VALUE *)arg;
4088
4089 if (RTEST(rb_equal(args[0], key))) {
4090 args[1] = rb_assoc_new(key, val);
4091 return ST_STOP;
4092 }
4093 return ST_CONTINUE;
4094}
4095
4096/*
4097 * call-seq:
4098 * hash.assoc(key) -> new_array or nil
4099 *
4100 * If the given +key+ is found, returns a 2-element Array containing that key and its value:
4101 * h = {foo: 0, bar: 1, baz: 2}
4102 * h.assoc(:bar) # => [:bar, 1]
4103 *
4104 * Returns +nil+ if key +key+ is not found.
4105 */
4106
4107static VALUE
4108rb_hash_assoc(VALUE hash, VALUE key)
4109{
4110 VALUE args[2];
4111
4112 if (RHASH_EMPTY_P(hash)) return Qnil;
4113
4114 if (RHASH_ST_TABLE_P(hash) && !RHASH_IDENTHASH_P(hash)) {
4115 VALUE value = Qundef;
4116 st_table assoctable = *RHASH_ST_TABLE(hash);
4117 assoctable.type = &(struct st_hash_type){
4118 .compare = assoc_cmp,
4119 .hash = assoctable.type->hash,
4120 };
4121 VALUE arg = (VALUE)&(struct assoc_arg){
4122 .tbl = &assoctable,
4123 .key = (st_data_t)key,
4124 };
4125
4126 if (RB_OBJ_FROZEN(hash)) {
4127 value = assoc_lookup(arg);
4128 }
4129 else {
4130 hash_iter_lev_inc(hash);
4131 value = rb_ensure(assoc_lookup, arg, hash_foreach_ensure, hash);
4132 }
4133 hash_verify(hash);
4134 if (!UNDEF_P(value)) return rb_assoc_new(key, value);
4135 }
4136
4137 args[0] = key;
4138 args[1] = Qnil;
4139 rb_hash_foreach(hash, assoc_i, (VALUE)args);
4140 return args[1];
4141}
4142
4143static int
4144rassoc_i(VALUE key, VALUE val, VALUE arg)
4145{
4146 VALUE *args = (VALUE *)arg;
4147
4148 if (RTEST(rb_equal(args[0], val))) {
4149 args[1] = rb_assoc_new(key, val);
4150 return ST_STOP;
4151 }
4152 return ST_CONTINUE;
4153}
4154
4155/*
4156 * call-seq:
4157 * hash.rassoc(value) -> new_array or nil
4158 *
4159 * Returns a new 2-element Array consisting of the key and value
4160 * of the first-found entry whose value is <tt>==</tt> to value
4161 * (see {Entry Order}[rdoc-ref:Hash@Entry+Order]):
4162 * h = {foo: 0, bar: 1, baz: 1}
4163 * h.rassoc(1) # => [:bar, 1]
4164 *
4165 * Returns +nil+ if no such value found.
4166 */
4167
4168static VALUE
4169rb_hash_rassoc(VALUE hash, VALUE obj)
4170{
4171 VALUE args[2];
4172
4173 args[0] = obj;
4174 args[1] = Qnil;
4175 rb_hash_foreach(hash, rassoc_i, (VALUE)args);
4176 return args[1];
4177}
4178
4179static int
4180flatten_i(VALUE key, VALUE val, VALUE ary)
4181{
4182 VALUE pair[2];
4183
4184 pair[0] = key;
4185 pair[1] = val;
4186 rb_ary_cat(ary, pair, 2);
4187
4188 return ST_CONTINUE;
4189}
4190
4191/*
4192 * call-seq:
4193 * hash.flatten -> new_array
4194 * hash.flatten(level) -> new_array
4195 *
4196 * Returns a new Array object that is a 1-dimensional flattening of +self+.
4197 *
4198 * ---
4199 *
4200 * By default, nested Arrays are not flattened:
4201 * h = {foo: 0, bar: [:bat, 3], baz: 2}
4202 * h.flatten # => [:foo, 0, :bar, [:bat, 3], :baz, 2]
4203 *
4204 * Takes the depth of recursive flattening from Integer argument +level+:
4205 * h = {foo: 0, bar: [:bat, [:baz, [:bat, ]]]}
4206 * h.flatten(1) # => [:foo, 0, :bar, [:bat, [:baz, [:bat]]]]
4207 * h.flatten(2) # => [:foo, 0, :bar, :bat, [:baz, [:bat]]]
4208 * h.flatten(3) # => [:foo, 0, :bar, :bat, :baz, [:bat]]
4209 * h.flatten(4) # => [:foo, 0, :bar, :bat, :baz, :bat]
4210 *
4211 * When +level+ is negative, flattens all nested Arrays:
4212 * h = {foo: 0, bar: [:bat, [:baz, [:bat, ]]]}
4213 * h.flatten(-1) # => [:foo, 0, :bar, :bat, :baz, :bat]
4214 * h.flatten(-2) # => [:foo, 0, :bar, :bat, :baz, :bat]
4215 *
4216 * When +level+ is zero, returns the equivalent of #to_a :
4217 * h = {foo: 0, bar: [:bat, 3], baz: 2}
4218 * h.flatten(0) # => [[:foo, 0], [:bar, [:bat, 3]], [:baz, 2]]
4219 * h.flatten(0) == h.to_a # => true
4220 */
4221
4222static VALUE
4223rb_hash_flatten(int argc, VALUE *argv, VALUE hash)
4224{
4225 VALUE ary;
4226
4227 rb_check_arity(argc, 0, 1);
4228
4229 if (argc) {
4230 int level = NUM2INT(argv[0]);
4231
4232 if (level == 0) return rb_hash_to_a(hash);
4233
4234 ary = rb_ary_new_capa(RHASH_SIZE(hash) * 2);
4235 rb_hash_foreach(hash, flatten_i, ary);
4236 level--;
4237
4238 if (level > 0) {
4239 VALUE ary_flatten_level = INT2FIX(level);
4240 rb_funcallv(ary, id_flatten_bang, 1, &ary_flatten_level);
4241 }
4242 else if (level < 0) {
4243 /* flatten recursively */
4244 rb_funcallv(ary, id_flatten_bang, 0, 0);
4245 }
4246 }
4247 else {
4248 ary = rb_ary_new_capa(RHASH_SIZE(hash) * 2);
4249 rb_hash_foreach(hash, flatten_i, ary);
4250 }
4251
4252 return ary;
4253}
4254
4255static int
4256delete_if_nil(VALUE key, VALUE value, VALUE hash)
4257{
4258 if (NIL_P(value)) {
4259 return ST_DELETE;
4260 }
4261 return ST_CONTINUE;
4262}
4263
4264/*
4265 * call-seq:
4266 * hash.compact -> new_hash
4267 *
4268 * Returns a copy of +self+ with all +nil+-valued entries removed:
4269 * h = {foo: 0, bar: nil, baz: 2, bat: nil}
4270 * h1 = h.compact
4271 * h1 # => {:foo=>0, :baz=>2}
4272 */
4273
4274static VALUE
4275rb_hash_compact(VALUE hash)
4276{
4277 VALUE result = rb_hash_dup(hash);
4278 if (!RHASH_EMPTY_P(hash)) {
4279 rb_hash_foreach(result, delete_if_nil, result);
4280 compact_after_delete(result);
4281 }
4282 else if (rb_hash_compare_by_id_p(hash)) {
4283 result = rb_hash_compare_by_id(result);
4284 }
4285 return result;
4286}
4287
4288/*
4289 * call-seq:
4290 * hash.compact! -> self or nil
4291 *
4292 * Returns +self+ with all its +nil+-valued entries removed (in place):
4293 * h = {foo: 0, bar: nil, baz: 2, bat: nil}
4294 * h.compact! # => {:foo=>0, :baz=>2}
4295 *
4296 * Returns +nil+ if no entries were removed.
4297 */
4298
4299static VALUE
4300rb_hash_compact_bang(VALUE hash)
4301{
4302 st_index_t n;
4303 rb_hash_modify_check(hash);
4304 n = RHASH_SIZE(hash);
4305 if (n) {
4306 rb_hash_foreach(hash, delete_if_nil, hash);
4307 if (n != RHASH_SIZE(hash))
4308 return hash;
4309 }
4310 return Qnil;
4311}
4312
4313/*
4314 * call-seq:
4315 * hash.compare_by_identity -> self
4316 *
4317 * Sets +self+ to consider only identity in comparing keys;
4318 * two keys are considered the same only if they are the same object;
4319 * returns +self+.
4320 *
4321 * By default, these two object are considered to be the same key,
4322 * so +s1+ will overwrite +s0+:
4323 * s0 = 'x'
4324 * s1 = 'x'
4325 * h = {}
4326 * h.compare_by_identity? # => false
4327 * h[s0] = 0
4328 * h[s1] = 1
4329 * h # => {"x"=>1}
4330 *
4331 * After calling \#compare_by_identity, the keys are considered to be different,
4332 * and therefore do not overwrite each other:
4333 * h = {}
4334 * h.compare_by_identity # => {}
4335 * h.compare_by_identity? # => true
4336 * h[s0] = 0
4337 * h[s1] = 1
4338 * h # => {"x"=>0, "x"=>1}
4339 */
4340
4341 VALUE
4342rb_hash_compare_by_id(VALUE hash)
4343{
4344 VALUE tmp;
4345 st_table *identtable;
4346
4347 if (rb_hash_compare_by_id_p(hash)) return hash;
4348
4349 rb_hash_modify_check(hash);
4350 if (hash_iterating_p(hash)) {
4351 rb_raise(rb_eRuntimeError, "compare_by_identity during iteration");
4352 }
4353
4354 if (RHASH_TABLE_EMPTY_P(hash)) {
4355 // Fast path: There's nothing to rehash, so we don't need a `tmp` table.
4356 // We're most likely an AR table, so this will need an allocation.
4357 ar_force_convert_table(hash, __FILE__, __LINE__);
4358 HASH_ASSERT(RHASH_ST_TABLE_P(hash));
4359
4360 RHASH_ST_TABLE(hash)->type = &identhash;
4361 }
4362 else {
4363 // Slow path: Need to rehash the members of `self` into a new
4364 // `tmp` table using the new `identhash` compare/hash functions.
4365 tmp = hash_alloc(0);
4366 hash_st_table_init(tmp, &identhash, RHASH_SIZE(hash));
4367 identtable = RHASH_ST_TABLE(tmp);
4368
4369 rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tmp);
4370 rb_hash_free(hash);
4371
4372 // We know for sure `identtable` is an st table,
4373 // so we can skip `ar_force_convert_table` here.
4374 RHASH_ST_TABLE_SET(hash, identtable);
4375 RHASH_ST_CLEAR(tmp);
4376 }
4377
4378 return hash;
4379}
4380
4381/*
4382 * call-seq:
4383 * hash.compare_by_identity? -> true or false
4384 *
4385 * Returns +true+ if #compare_by_identity has been called, +false+ otherwise.
4386 */
4387
4388 VALUE
4389rb_hash_compare_by_id_p(VALUE hash)
4390{
4391 return RBOOL(RHASH_IDENTHASH_P(hash));
4392}
4393
4394 VALUE
4395rb_ident_hash_new(void)
4396{
4397 VALUE hash = rb_hash_new();
4398 hash_st_table_init(hash, &identhash, 0);
4399 return hash;
4400}
4401
4402 VALUE
4403rb_ident_hash_new_with_size(st_index_t size)
4404{
4405 VALUE hash = rb_hash_new();
4406 hash_st_table_init(hash, &identhash, size);
4407 return hash;
4408}
4409
4410 st_table *
4411rb_init_identtable(void)
4412{
4413 return st_init_table(&identhash);
4414}
4415
4416static int
4417any_p_i(VALUE key, VALUE value, VALUE arg)
4418{
4419 VALUE ret = rb_yield(rb_assoc_new(key, value));
4420 if (RTEST(ret)) {
4421 *(VALUE *)arg = Qtrue;
4422 return ST_STOP;
4423 }
4424 return ST_CONTINUE;
4425}
4426
4427static int
4428any_p_i_fast(VALUE key, VALUE value, VALUE arg)
4429{
4430 VALUE ret = rb_yield_values(2, key, value);
4431 if (RTEST(ret)) {
4432 *(VALUE *)arg = Qtrue;
4433 return ST_STOP;
4434 }
4435 return ST_CONTINUE;
4436}
4437
4438static int
4439any_p_i_pattern(VALUE key, VALUE value, VALUE arg)
4440{
4441 VALUE ret = rb_funcall(((VALUE *)arg)[1], idEqq, 1, rb_assoc_new(key, value));
4442 if (RTEST(ret)) {
4443 *(VALUE *)arg = Qtrue;
4444 return ST_STOP;
4445 }
4446 return ST_CONTINUE;
4447}
4448
4449/*
4450 * call-seq:
4451 * hash.any? -> true or false
4452 * hash.any?(object) -> true or false
4453 * hash.any? {|key, value| ... } -> true or false
4454 *
4455 * Returns +true+ if any element satisfies a given criterion;
4456 * +false+ otherwise.
4457 *
4458 * If +self+ has no element, returns +false+ and argument or block
4459 * are not used.
4460 *
4461 * With no argument and no block,
4462 * returns +true+ if +self+ is non-empty; +false+ if empty.
4463 *
4464 * With argument +object+ and no block,
4465 * returns +true+ if for any key +key+
4466 * <tt>h.assoc(key) == object</tt>:
4467 * h = {foo: 0, bar: 1, baz: 2}
4468 * h.any?([:bar, 1]) # => true
4469 * h.any?([:bar, 0]) # => false
4470 * h.any?([:baz, 1]) # => false
4471 *
4472 * With no argument and a block,
4473 * calls the block with each key-value pair;
4474 * returns +true+ if the block returns any truthy value,
4475 * +false+ otherwise:
4476 * h = {foo: 0, bar: 1, baz: 2}
4477 * h.any? {|key, value| value < 3 } # => true
4478 * h.any? {|key, value| value > 3 } # => false
4479 *
4480 * Related: Enumerable#any?
4481 */
4482
4483static VALUE
4484rb_hash_any_p(int argc, VALUE *argv, VALUE hash)
4485{
4486 VALUE args[2];
4487 args[0] = Qfalse;
4488
4489 rb_check_arity(argc, 0, 1);
4490 if (RHASH_EMPTY_P(hash)) return Qfalse;
4491 if (argc) {
4492 if (rb_block_given_p()) {
4493 rb_warn("given block not used");
4494 }
4495 args[1] = argv[0];
4496
4497 rb_hash_foreach(hash, any_p_i_pattern, (VALUE)args);
4498 }
4499 else {
4500 if (!rb_block_given_p()) {
4501 /* yields pairs, never false */
4502 return Qtrue;
4503 }
4504 if (rb_block_pair_yield_optimizable())
4505 rb_hash_foreach(hash, any_p_i_fast, (VALUE)args);
4506 else
4507 rb_hash_foreach(hash, any_p_i, (VALUE)args);
4508 }
4509 return args[0];
4510}
4511
4512/*
4513 * call-seq:
4514 * hash.dig(key, *identifiers) -> object
4515 *
4516 * Finds and returns the object in nested objects
4517 * that is specified by +key+ and +identifiers+.
4518 * The nested objects may be instances of various classes.
4519 * See {Dig Methods}[rdoc-ref:dig_methods.rdoc].
4520 *
4521 * Nested Hashes:
4522 * h = {foo: {bar: {baz: 2}}}
4523 * h.dig(:foo) # => {:bar=>{:baz=>2}}
4524 * h.dig(:foo, :bar) # => {:baz=>2}
4525 * h.dig(:foo, :bar, :baz) # => 2
4526 * h.dig(:foo, :bar, :BAZ) # => nil
4527 *
4528 * Nested Hashes and Arrays:
4529 * h = {foo: {bar: [:a, :b, :c]}}
4530 * h.dig(:foo, :bar, 2) # => :c
4531 *
4532 * This method will use the {default values}[rdoc-ref:Hash@Default+Values]
4533 * for keys that are not present:
4534 * h = {foo: {bar: [:a, :b, :c]}}
4535 * h.dig(:hello) # => nil
4536 * h.default_proc = -> (hash, _key) { hash }
4537 * h.dig(:hello, :world) # => h
4538 * h.dig(:hello, :world, :foo, :bar, 2) # => :c
4539 */
4540
4541static VALUE
4542rb_hash_dig(int argc, VALUE *argv, VALUE self)
4543{
4544 rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
4545 self = rb_hash_aref(self, *argv);
4546 if (!--argc) return self;
4547 ++argv;
4548 return rb_obj_dig(argc, argv, self, Qnil);
4549}
4550
4551static int
4552hash_le_i(VALUE key, VALUE value, VALUE arg)
4553{
4554 VALUE *args = (VALUE *)arg;
4555 VALUE v = rb_hash_lookup2(args[0], key, Qundef);
4556 if (!UNDEF_P(v) && rb_equal(value, v)) return ST_CONTINUE;
4557 args[1] = Qfalse;
4558 return ST_STOP;
4559}
4560
4561static VALUE
4562hash_le(VALUE hash1, VALUE hash2)
4563{
4564 VALUE args[2];
4565 args[0] = hash2;
4566 args[1] = Qtrue;
4567 rb_hash_foreach(hash1, hash_le_i, (VALUE)args);
4568 return args[1];
4569}
4570
4571/*
4572 * call-seq:
4573 * hash <= other_hash -> true or false
4574 *
4575 * Returns +true+ if +hash+ is a subset of +other_hash+, +false+ otherwise:
4576 * h1 = {foo: 0, bar: 1}
4577 * h2 = {foo: 0, bar: 1, baz: 2}
4578 * h1 <= h2 # => true
4579 * h2 <= h1 # => false
4580 * h1 <= h1 # => true
4581 */
4582static VALUE
4583rb_hash_le(VALUE hash, VALUE other)
4584{
4585 other = to_hash(other);
4586 if (RHASH_SIZE(hash) > RHASH_SIZE(other)) return Qfalse;
4587 return hash_le(hash, other);
4588}
4589
4590/*
4591 * call-seq:
4592 * hash < other_hash -> true or false
4593 *
4594 * Returns +true+ if +hash+ is a proper subset of +other_hash+, +false+ otherwise:
4595 * h1 = {foo: 0, bar: 1}
4596 * h2 = {foo: 0, bar: 1, baz: 2}
4597 * h1 < h2 # => true
4598 * h2 < h1 # => false
4599 * h1 < h1 # => false
4600 */
4601static VALUE
4602rb_hash_lt(VALUE hash, VALUE other)
4603{
4604 other = to_hash(other);
4605 if (RHASH_SIZE(hash) >= RHASH_SIZE(other)) return Qfalse;
4606 return hash_le(hash, other);
4607}
4608
4609/*
4610 * call-seq:
4611 * hash >= other_hash -> true or false
4612 *
4613 * Returns +true+ if +hash+ is a superset of +other_hash+, +false+ otherwise:
4614 * h1 = {foo: 0, bar: 1, baz: 2}
4615 * h2 = {foo: 0, bar: 1}
4616 * h1 >= h2 # => true
4617 * h2 >= h1 # => false
4618 * h1 >= h1 # => true
4619 */
4620static VALUE
4621rb_hash_ge(VALUE hash, VALUE other)
4622{
4623 other = to_hash(other);
4624 if (RHASH_SIZE(hash) < RHASH_SIZE(other)) return Qfalse;
4625 return hash_le(other, hash);
4626}
4627
4628/*
4629 * call-seq:
4630 * hash > other_hash -> true or false
4631 *
4632 * Returns +true+ if +hash+ is a proper superset of +other_hash+, +false+ otherwise:
4633 * h1 = {foo: 0, bar: 1, baz: 2}
4634 * h2 = {foo: 0, bar: 1}
4635 * h1 > h2 # => true
4636 * h2 > h1 # => false
4637 * h1 > h1 # => false
4638 */
4639static VALUE
4640rb_hash_gt(VALUE hash, VALUE other)
4641{
4642 other = to_hash(other);
4643 if (RHASH_SIZE(hash) <= RHASH_SIZE(other)) return Qfalse;
4644 return hash_le(other, hash);
4645}
4646
4647static VALUE
4648hash_proc_call(RB_BLOCK_CALL_FUNC_ARGLIST(key, hash))
4649{
4650 rb_check_arity(argc, 1, 1);
4651 return rb_hash_aref(hash, *argv);
4652}
4653
4654/*
4655 * call-seq:
4656 * hash.to_proc -> proc
4657 *
4658 * Returns a Proc object that maps a key to its value:
4659 * h = {foo: 0, bar: 1, baz: 2}
4660 * proc = h.to_proc
4661 * proc.class # => Proc
4662 * proc.call(:foo) # => 0
4663 * proc.call(:bar) # => 1
4664 * proc.call(:nosuch) # => nil
4665 */
4666static VALUE
4667rb_hash_to_proc(VALUE hash)
4668{
4669 return rb_func_lambda_new(hash_proc_call, hash, 1, 1);
4670}
4671
4672/* :nodoc: */
4673static VALUE
4674rb_hash_deconstruct_keys(VALUE hash, VALUE keys)
4675{
4676 return hash;
4677}
4678
4679static int
4680add_new_i(st_data_t *key, st_data_t *val, st_data_t arg, int existing)
4681{
4682 VALUE *args = (VALUE *)arg;
4683 if (existing) return ST_STOP;
4684 RB_OBJ_WRITTEN(args[0], Qundef, (VALUE)*key);
4685 RB_OBJ_WRITE(args[0], (VALUE *)val, args[1]);
4686 return ST_CONTINUE;
4687}
4688
4689/*
4690 * add +key+ to +val+ pair if +hash+ does not contain +key+.
4691 * returns non-zero if +key+ was contained.
4692 */
4693int
4694rb_hash_add_new_element(VALUE hash, VALUE key, VALUE val)
4695{
4696 st_table *tbl;
4697 int ret = 0;
4698 VALUE args[2];
4699 args[0] = hash;
4700 args[1] = val;
4701
4702 if (RHASH_AR_TABLE_P(hash)) {
4703 ret = ar_update(hash, (st_data_t)key, add_new_i, (st_data_t)args);
4704 if (ret != -1) {
4705 return ret;
4706 }
4707 ar_force_convert_table(hash, __FILE__, __LINE__);
4708 }
4709
4710 tbl = RHASH_TBL_RAW(hash);
4711 return st_update(tbl, (st_data_t)key, add_new_i, (st_data_t)args);
4712
4713}
4714
4715static st_data_t
4716key_stringify(VALUE key)
4717{
4718 return (rb_obj_class(key) == rb_cString && !RB_OBJ_FROZEN(key)) ?
4719 rb_hash_key_str(key) : key;
4720}
4721
4722static void
4723ar_bulk_insert(VALUE hash, long argc, const VALUE *argv)
4724{
4725 long i;
4726 for (i = 0; i < argc; ) {
4727 st_data_t k = key_stringify(argv[i++]);
4728 st_data_t v = argv[i++];
4729 ar_insert(hash, k, v);
4730 RB_OBJ_WRITTEN(hash, Qundef, k);
4731 RB_OBJ_WRITTEN(hash, Qundef, v);
4732 }
4733}
4734
4735void
4736rb_hash_bulk_insert(long argc, const VALUE *argv, VALUE hash)
4737{
4738 HASH_ASSERT(argc % 2 == 0);
4739 if (argc > 0) {
4740 st_index_t size = argc / 2;
4741
4742 if (RHASH_AR_TABLE_P(hash) &&
4743 (RHASH_AR_TABLE_SIZE(hash) + size <= RHASH_AR_TABLE_MAX_SIZE)) {
4744 ar_bulk_insert(hash, argc, argv);
4745 }
4746 else {
4747 rb_hash_bulk_insert_into_st_table(argc, argv, hash);
4748 }
4749 }
4750}
4751
4752static char **origenviron;
4753#ifdef _WIN32
4754#define GET_ENVIRON(e) ((e) = rb_w32_get_environ())
4755#define FREE_ENVIRON(e) rb_w32_free_environ(e)
4756static char **my_environ;
4757#undef environ
4758#define environ my_environ
4759#undef getenv
4760#define getenv(n) rb_w32_ugetenv(n)
4761#elif defined(__APPLE__)
4762#undef environ
4763#define environ (*_NSGetEnviron())
4764#define GET_ENVIRON(e) (e)
4765#define FREE_ENVIRON(e)
4766#else
4767extern char **environ;
4768#define GET_ENVIRON(e) (e)
4769#define FREE_ENVIRON(e)
4770#endif
4771#ifdef ENV_IGNORECASE
4772#define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
4773#define ENVNMATCH(s1, s2, n) (STRNCASECMP((s1), (s2), (n)) == 0)
4774#else
4775#define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
4776#define ENVNMATCH(s1, s2, n) (memcmp((s1), (s2), (n)) == 0)
4777#endif
4778
4779#define ENV_LOCK() RB_VM_LOCK_ENTER()
4780#define ENV_UNLOCK() RB_VM_LOCK_LEAVE()
4781
4782static inline rb_encoding *
4783env_encoding(void)
4784{
4785#ifdef _WIN32
4786 return rb_utf8_encoding();
4787#else
4788 return rb_locale_encoding();
4789#endif
4790}
4791
4792static VALUE
4793env_enc_str_new(const char *ptr, long len, rb_encoding *enc)
4794{
4795 VALUE str = rb_external_str_new_with_enc(ptr, len, enc);
4796
4797 rb_obj_freeze(str);
4798 return str;
4799}
4800
4801static VALUE
4802env_str_new(const char *ptr, long len)
4803{
4804 return env_enc_str_new(ptr, len, env_encoding());
4805}
4806
4807static VALUE
4808env_str_new2(const char *ptr)
4809{
4810 if (!ptr) return Qnil;
4811 return env_str_new(ptr, strlen(ptr));
4812}
4813
4814static VALUE
4815getenv_with_lock(const char *name)
4816{
4817 VALUE ret;
4818 ENV_LOCK();
4819 {
4820 const char *val = getenv(name);
4821 ret = env_str_new2(val);
4822 }
4823 ENV_UNLOCK();
4824 return ret;
4825}
4826
4827static bool
4828has_env_with_lock(const char *name)
4829{
4830 const char *val;
4831
4832 ENV_LOCK();
4833 {
4834 val = getenv(name);
4835 }
4836 ENV_UNLOCK();
4837
4838 return val ? true : false;
4839}
4840
4841static const char TZ_ENV[] = "TZ";
4842
4843static void *
4844get_env_cstr(VALUE str, const char *name)
4845{
4846 char *var;
4847 rb_encoding *enc = rb_enc_get(str);
4848 if (!rb_enc_asciicompat(enc)) {
4849 rb_raise(rb_eArgError, "bad environment variable %s: ASCII incompatible encoding: %s",
4850 name, rb_enc_name(enc));
4851 }
4852 var = RSTRING_PTR(str);
4853 if (memchr(var, '\0', RSTRING_LEN(str))) {
4854 rb_raise(rb_eArgError, "bad environment variable %s: contains null byte", name);
4855 }
4856 return rb_str_fill_terminator(str, 1); /* ASCII compatible */
4857}
4858
4859#define get_env_ptr(var, val) \
4860 (var = get_env_cstr(val, #var))
4861
4862static inline const char *
4863env_name(volatile VALUE *s)
4864{
4865 const char *name;
4866 SafeStringValue(*s);
4867 get_env_ptr(name, *s);
4868 return name;
4869}
4870
4871#define env_name(s) env_name(&(s))
4872
4873static VALUE env_aset(VALUE nm, VALUE val);
4874
4875static void
4876reset_by_modified_env(const char *nam)
4877{
4878 /*
4879 * ENV['TZ'] = nil has a special meaning.
4880 * TZ is no longer considered up-to-date and ruby call tzset() as needed.
4881 * It could be useful if sysadmin change /etc/localtime.
4882 * This hack might works only on Linux glibc.
4883 */
4884 if (ENVMATCH(nam, TZ_ENV)) {
4885 ruby_reset_timezone();
4886 }
4887}
4888
4889static VALUE
4890env_delete(VALUE name)
4891{
4892 const char *nam = env_name(name);
4893 reset_by_modified_env(nam);
4894 VALUE val = getenv_with_lock(nam);
4895
4896 if (!NIL_P(val)) {
4897 ruby_setenv(nam, 0);
4898 }
4899 return val;
4900}
4901
4902/*
4903 * call-seq:
4904 * ENV.delete(name) -> value
4905 * ENV.delete(name) { |name| block } -> value
4906 * ENV.delete(missing_name) -> nil
4907 * ENV.delete(missing_name) { |name| block } -> block_value
4908 *
4909 * Deletes the environment variable with +name+ if it exists and returns its value:
4910 * ENV['foo'] = '0'
4911 * ENV.delete('foo') # => '0'
4912 *
4913 * If a block is not given and the named environment variable does not exist, returns +nil+.
4914 *
4915 * If a block given and the environment variable does not exist,
4916 * yields +name+ to the block and returns the value of the block:
4917 * ENV.delete('foo') { |name| name * 2 } # => "foofoo"
4918 *
4919 * If a block given and the environment variable exists,
4920 * deletes the environment variable and returns its value (ignoring the block):
4921 * ENV['foo'] = '0'
4922 * ENV.delete('foo') { |name| raise 'ignored' } # => "0"
4923 *
4924 * Raises an exception if +name+ is invalid.
4925 * See {Invalid Names and Values}[rdoc-ref:ENV@Invalid+Names+and+Values].
4926 */
4927static VALUE
4928env_delete_m(VALUE obj, VALUE name)
4929{
4930 VALUE val;
4931
4932 val = env_delete(name);
4933 if (NIL_P(val) && rb_block_given_p()) val = rb_yield(name);
4934 return val;
4935}
4936
4937/*
4938 * call-seq:
4939 * ENV[name] -> value
4940 *
4941 * Returns the value for the environment variable +name+ if it exists:
4942 * ENV['foo'] = '0'
4943 * ENV['foo'] # => "0"
4944 * Returns +nil+ if the named variable does not exist.
4945 *
4946 * Raises an exception if +name+ is invalid.
4947 * See {Invalid Names and Values}[rdoc-ref:ENV@Invalid+Names+and+Values].
4948 */
4949static VALUE
4950rb_f_getenv(VALUE obj, VALUE name)
4951{
4952 const char *nam = env_name(name);
4953 VALUE env = getenv_with_lock(nam);
4954 return env;
4955}
4956
4957/*
4958 * call-seq:
4959 * ENV.fetch(name) -> value
4960 * ENV.fetch(name, default) -> value
4961 * ENV.fetch(name) { |name| block } -> value
4962 *
4963 * If +name+ is the name of an environment variable, returns its value:
4964 * ENV['foo'] = '0'
4965 * ENV.fetch('foo') # => '0'
4966 * Otherwise if a block is given (but not a default value),
4967 * yields +name+ to the block and returns the block's return value:
4968 * ENV.fetch('foo') { |name| :need_not_return_a_string } # => :need_not_return_a_string
4969 * Otherwise if a default value is given (but not a block), returns the default value:
4970 * ENV.delete('foo')
4971 * ENV.fetch('foo', :default_need_not_be_a_string) # => :default_need_not_be_a_string
4972 * If the environment variable does not exist and both default and block are given,
4973 * issues a warning ("warning: block supersedes default value argument"),
4974 * yields +name+ to the block, and returns the block's return value:
4975 * ENV.fetch('foo', :default) { |name| :block_return } # => :block_return
4976 * Raises KeyError if +name+ is valid, but not found,
4977 * and neither default value nor block is given:
4978 * ENV.fetch('foo') # Raises KeyError (key not found: "foo")
4979 * Raises an exception if +name+ is invalid.
4980 * See {Invalid Names and Values}[rdoc-ref:ENV@Invalid+Names+and+Values].
4981 */
4982static VALUE
4983env_fetch(int argc, VALUE *argv, VALUE _)
4984{
4985 VALUE key;
4986 long block_given;
4987 const char *nam;
4988 VALUE env;
4989
4990 rb_check_arity(argc, 1, 2);
4991 key = argv[0];
4992 block_given = rb_block_given_p();
4993 if (block_given && argc == 2) {
4994 rb_warn("block supersedes default value argument");
4995 }
4996 nam = env_name(key);
4997 env = getenv_with_lock(nam);
4998
4999 if (NIL_P(env)) {
5000 if (block_given) return rb_yield(key);
5001 if (argc == 1) {
5002 rb_key_err_raise(rb_sprintf("key not found: \"%"PRIsVALUE"\"", key), envtbl, key);
5003 }
5004 return argv[1];
5005 }
5006 return env;
5007}
5008
5009#if defined(_WIN32) || (defined(HAVE_SETENV) && defined(HAVE_UNSETENV))
5010#elif defined __sun
5011static int
5012in_origenv(const char *str)
5013{
5014 char **env;
5015 for (env = origenviron; *env; ++env) {
5016 if (*env == str) return 1;
5017 }
5018 return 0;
5019}
5020#else
5021static int
5022envix(const char *nam)
5023{
5024 // should be locked
5025
5026 register int i, len = strlen(nam);
5027 char **env;
5028
5029 env = GET_ENVIRON(environ);
5030 for (i = 0; env[i]; i++) {
5031 if (ENVNMATCH(env[i],nam,len) && env[i][len] == '=')
5032 break; /* memcmp must come first to avoid */
5033 } /* potential SEGV's */
5034 FREE_ENVIRON(environ);
5035 return i;
5036}
5037#endif
5038
5039#if defined(_WIN32)
5040static size_t
5041getenvsize(const WCHAR* p)
5042{
5043 const WCHAR* porg = p;
5044 while (*p++) p += lstrlenW(p) + 1;
5045 return p - porg + 1;
5046}
5047
5048static size_t
5049getenvblocksize(void)
5050{
5051#ifdef _MAX_ENV
5052 return _MAX_ENV;
5053#else
5054 return 32767;
5055#endif
5056}
5057
5058static int
5059check_envsize(size_t n)
5060{
5061 if (_WIN32_WINNT < 0x0600 && rb_w32_osver() < 6) {
5062 /* https://msdn.microsoft.com/en-us/library/windows/desktop/ms682653(v=vs.85).aspx */
5063 /* Windows Server 2003 and Windows XP: The maximum size of the
5064 * environment block for the process is 32,767 characters. */
5065 WCHAR* p = GetEnvironmentStringsW();
5066 if (!p) return -1; /* never happen */
5067 n += getenvsize(p);
5068 FreeEnvironmentStringsW(p);
5069 if (n >= getenvblocksize()) {
5070 return -1;
5071 }
5072 }
5073 return 0;
5074}
5075#endif
5076
5077#if defined(_WIN32) || \
5078 (defined(__sun) && !(defined(HAVE_SETENV) && defined(HAVE_UNSETENV)))
5079
5080NORETURN(static void invalid_envname(const char *name));
5081
5082static void
5083invalid_envname(const char *name)
5084{
5085 rb_syserr_fail_str(EINVAL, rb_sprintf("ruby_setenv(%s)", name));
5086}
5087
5088static const char *
5089check_envname(const char *name)
5090{
5091 if (strchr(name, '=')) {
5092 invalid_envname(name);
5093 }
5094 return name;
5095}
5096#endif
5097
5098void
5099ruby_setenv(const char *name, const char *value)
5100{
5101#if defined(_WIN32)
5102# if defined(MINGW_HAS_SECURE_API) || RUBY_MSVCRT_VERSION >= 80
5103# define HAVE__WPUTENV_S 1
5104# endif
5105 VALUE buf;
5106 WCHAR *wname;
5107 WCHAR *wvalue = 0;
5108 int failed = 0;
5109 int len;
5110 check_envname(name);
5111 len = MultiByteToWideChar(CP_UTF8, 0, name, -1, NULL, 0);
5112 if (value) {
5113 int len2;
5114 len2 = MultiByteToWideChar(CP_UTF8, 0, value, -1, NULL, 0);
5115 if (check_envsize((size_t)len + len2)) { /* len and len2 include '\0' */
5116 goto fail; /* 2 for '=' & '\0' */
5117 }
5118 wname = ALLOCV_N(WCHAR, buf, len + len2);
5119 wvalue = wname + len;
5120 MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, len);
5121 MultiByteToWideChar(CP_UTF8, 0, value, -1, wvalue, len2);
5122#ifndef HAVE__WPUTENV_S
5123 wname[len-1] = L'=';
5124#endif
5125 }
5126 else {
5127 wname = ALLOCV_N(WCHAR, buf, len + 1);
5128 MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, len);
5129 wvalue = wname + len;
5130 *wvalue = L'\0';
5131#ifndef HAVE__WPUTENV_S
5132 wname[len-1] = L'=';
5133#endif
5134 }
5135
5136 ENV_LOCK();
5137 {
5138#ifndef HAVE__WPUTENV_S
5139 failed = _wputenv(wname);
5140#else
5141 failed = _wputenv_s(wname, wvalue);
5142#endif
5143 }
5144 ENV_UNLOCK();
5145
5146 ALLOCV_END(buf);
5147 /* even if putenv() failed, clean up and try to delete the
5148 * variable from the system area. */
5149 if (!value || !*value) {
5150 /* putenv() doesn't handle empty value */
5151 if (!SetEnvironmentVariable(name, value) &&
5152 GetLastError() != ERROR_ENVVAR_NOT_FOUND) goto fail;
5153 }
5154 if (failed) {
5155 fail:
5156 invalid_envname(name);
5157 }
5158#elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV)
5159 if (value) {
5160 int ret;
5161 ENV_LOCK();
5162 {
5163 ret = setenv(name, value, 1);
5164 }
5165 ENV_UNLOCK();
5166
5167 if (ret) rb_sys_fail_sprintf("setenv(%s)", name);
5168 }
5169 else {
5170#ifdef VOID_UNSETENV
5171 ENV_LOCK();
5172 {
5173 unsetenv(name);
5174 }
5175 ENV_UNLOCK();
5176#else
5177 int ret;
5178 ENV_LOCK();
5179 {
5180 ret = unsetenv(name);
5181 }
5182 ENV_UNLOCK();
5183
5184 if (ret) rb_sys_fail_sprintf("unsetenv(%s)", name);
5185#endif
5186 }
5187#elif defined __sun
5188 /* Solaris 9 (or earlier) does not have setenv(3C) and unsetenv(3C). */
5189 /* The below code was tested on Solaris 10 by:
5190 % ./configure ac_cv_func_setenv=no ac_cv_func_unsetenv=no
5191 */
5192 size_t len, mem_size;
5193 char **env_ptr, *str, *mem_ptr;
5194
5195 check_envname(name);
5196 len = strlen(name);
5197 if (value) {
5198 mem_size = len + strlen(value) + 2;
5199 mem_ptr = malloc(mem_size);
5200 if (mem_ptr == NULL)
5201 rb_sys_fail_sprintf("malloc(%"PRIuSIZE")", mem_size);
5202 snprintf(mem_ptr, mem_size, "%s=%s", name, value);
5203 }
5204
5205 ENV_LOCK();
5206 {
5207 for (env_ptr = GET_ENVIRON(environ); (str = *env_ptr) != 0; ++env_ptr) {
5208 if (!strncmp(str, name, len) && str[len] == '=') {
5209 if (!in_origenv(str)) free(str);
5210 while ((env_ptr[0] = env_ptr[1]) != 0) env_ptr++;
5211 break;
5212 }
5213 }
5214 }
5215 ENV_UNLOCK();
5216
5217 if (value) {
5218 int ret;
5219 ENV_LOCK();
5220 {
5221 ret = putenv(mem_ptr);
5222 }
5223 ENV_UNLOCK();
5224
5225 if (ret) {
5226 free(mem_ptr);
5227 rb_sys_fail_sprintf("putenv(%s)", name);
5228 }
5229 }
5230#else /* WIN32 */
5231 size_t len;
5232 int i;
5233
5234 ENV_LOCK();
5235 {
5236 i = envix(name); /* where does it go? */
5237
5238 if (environ == origenviron) { /* need we copy environment? */
5239 int j;
5240 int max;
5241 char **tmpenv;
5242
5243 for (max = i; environ[max]; max++) ;
5244 tmpenv = ALLOC_N(char*, max+2);
5245 for (j=0; j<max; j++) /* copy environment */
5246 tmpenv[j] = ruby_strdup(environ[j]);
5247 tmpenv[max] = 0;
5248 environ = tmpenv; /* tell exec where it is now */
5249 }
5250
5251 if (environ[i]) {
5252 char **envp = origenviron;
5253 while (*envp && *envp != environ[i]) envp++;
5254 if (!*envp)
5255 xfree(environ[i]);
5256 if (!value) {
5257 while (environ[i]) {
5258 environ[i] = environ[i+1];
5259 i++;
5260 }
5261 goto finish;
5262 }
5263 }
5264 else { /* does not exist yet */
5265 if (!value) goto finish;
5266 REALLOC_N(environ, char*, i+2); /* just expand it a bit */
5267 environ[i+1] = 0; /* make sure it's null terminated */
5268 }
5269
5270 len = strlen(name) + strlen(value) + 2;
5271 environ[i] = ALLOC_N(char, len);
5272 snprintf(environ[i],len,"%s=%s",name,value); /* all that work just for this */
5273
5274 finish:;
5275 }
5276 ENV_UNLOCK();
5277#endif /* WIN32 */
5278}
5279
5280void
5281ruby_unsetenv(const char *name)
5282{
5283 ruby_setenv(name, 0);
5284}
5285
5286/*
5287 * call-seq:
5288 * ENV[name] = value -> value
5289 * ENV.store(name, value) -> value
5290 *
5291 * Creates, updates, or deletes the named environment variable, returning the value.
5292 * Both +name+ and +value+ may be instances of String.
5293 * See {Valid Names and Values}[rdoc-ref:ENV@Valid+Names+and+Values].
5294 *
5295 * - If the named environment variable does not exist:
5296 * - If +value+ is +nil+, does nothing.
5297 * ENV.clear
5298 * ENV['foo'] = nil # => nil
5299 * ENV.include?('foo') # => false
5300 * ENV.store('bar', nil) # => nil
5301 * ENV.include?('bar') # => false
5302 * - If +value+ is not +nil+, creates the environment variable with +name+ and +value+:
5303 * # Create 'foo' using ENV.[]=.
5304 * ENV['foo'] = '0' # => '0'
5305 * ENV['foo'] # => '0'
5306 * # Create 'bar' using ENV.store.
5307 * ENV.store('bar', '1') # => '1'
5308 * ENV['bar'] # => '1'
5309 * - If the named environment variable exists:
5310 * - If +value+ is not +nil+, updates the environment variable with value +value+:
5311 * # Update 'foo' using ENV.[]=.
5312 * ENV['foo'] = '2' # => '2'
5313 * ENV['foo'] # => '2'
5314 * # Update 'bar' using ENV.store.
5315 * ENV.store('bar', '3') # => '3'
5316 * ENV['bar'] # => '3'
5317 * - If +value+ is +nil+, deletes the environment variable:
5318 * # Delete 'foo' using ENV.[]=.
5319 * ENV['foo'] = nil # => nil
5320 * ENV.include?('foo') # => false
5321 * # Delete 'bar' using ENV.store.
5322 * ENV.store('bar', nil) # => nil
5323 * ENV.include?('bar') # => false
5324 *
5325 * Raises an exception if +name+ or +value+ is invalid.
5326 * See {Invalid Names and Values}[rdoc-ref:ENV@Invalid+Names+and+Values].
5327 */
5328static VALUE
5329env_aset_m(VALUE obj, VALUE nm, VALUE val)
5330{
5331 return env_aset(nm, val);
5332}
5333
5334static VALUE
5335env_aset(VALUE nm, VALUE val)
5336{
5337 char *name, *value;
5338
5339 if (NIL_P(val)) {
5340 env_delete(nm);
5341 return Qnil;
5342 }
5343 SafeStringValue(nm);
5344 SafeStringValue(val);
5345 /* nm can be modified in `val.to_str`, don't get `name` before
5346 * check for `val` */
5347 get_env_ptr(name, nm);
5348 get_env_ptr(value, val);
5349
5350 ruby_setenv(name, value);
5351 reset_by_modified_env(name);
5352 return val;
5353}
5354
5355static VALUE
5356env_keys(int raw)
5357{
5358 rb_encoding *enc = raw ? 0 : rb_locale_encoding();
5359 VALUE ary = rb_ary_new();
5360
5361 ENV_LOCK();
5362 {
5363 char **env = GET_ENVIRON(environ);
5364 while (*env) {
5365 char *s = strchr(*env, '=');
5366 if (s) {
5367 const char *p = *env;
5368 size_t l = s - p;
5369 VALUE e = raw ? rb_utf8_str_new(p, l) : env_enc_str_new(p, l, enc);
5370 rb_ary_push(ary, e);
5371 }
5372 env++;
5373 }
5374 FREE_ENVIRON(environ);
5375 }
5376 ENV_UNLOCK();
5377
5378 return ary;
5379}
5380
5381/*
5382 * call-seq:
5383 * ENV.keys -> array of names
5384 *
5385 * Returns all variable names in an Array:
5386 * ENV.replace('foo' => '0', 'bar' => '1')
5387 * ENV.keys # => ['bar', 'foo']
5388 * The order of the names is OS-dependent.
5389 * See {About Ordering}[rdoc-ref:ENV@About+Ordering].
5390 *
5391 * Returns the empty Array if ENV is empty.
5392 */
5393
5394static VALUE
5395env_f_keys(VALUE _)
5396{
5397 return env_keys(FALSE);
5398}
5399
5400static VALUE
5401rb_env_size(VALUE ehash, VALUE args, VALUE eobj)
5402{
5403 char **env;
5404 long cnt = 0;
5405
5406 ENV_LOCK();
5407 {
5408 env = GET_ENVIRON(environ);
5409 for (; *env ; ++env) {
5410 if (strchr(*env, '=')) {
5411 cnt++;
5412 }
5413 }
5414 FREE_ENVIRON(environ);
5415 }
5416 ENV_UNLOCK();
5417
5418 return LONG2FIX(cnt);
5419}
5420
5421/*
5422 * call-seq:
5423 * ENV.each_key { |name| block } -> ENV
5424 * ENV.each_key -> an_enumerator
5425 *
5426 * Yields each environment variable name:
5427 * ENV.replace('foo' => '0', 'bar' => '1') # => ENV
5428 * names = []
5429 * ENV.each_key { |name| names.push(name) } # => ENV
5430 * names # => ["bar", "foo"]
5431 *
5432 * Returns an Enumerator if no block given:
5433 * e = ENV.each_key # => #<Enumerator: {"bar"=>"1", "foo"=>"0"}:each_key>
5434 * names = []
5435 * e.each { |name| names.push(name) } # => ENV
5436 * names # => ["bar", "foo"]
5437 */
5438static VALUE
5439env_each_key(VALUE ehash)
5440{
5441 VALUE keys;
5442 long i;
5443
5444 RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
5445 keys = env_keys(FALSE);
5446 for (i=0; i<RARRAY_LEN(keys); i++) {
5447 rb_yield(RARRAY_AREF(keys, i));
5448 }
5449 return ehash;
5450}
5451
5452static VALUE
5453env_values(void)
5454{
5455 VALUE ary = rb_ary_new();
5456
5457 ENV_LOCK();
5458 {
5459 char **env = GET_ENVIRON(environ);
5460
5461 while (*env) {
5462 char *s = strchr(*env, '=');
5463 if (s) {
5464 rb_ary_push(ary, env_str_new2(s+1));
5465 }
5466 env++;
5467 }
5468 FREE_ENVIRON(environ);
5469 }
5470 ENV_UNLOCK();
5471
5472 return ary;
5473}
5474
5475/*
5476 * call-seq:
5477 * ENV.values -> array of values
5478 *
5479 * Returns all environment variable values in an Array:
5480 * ENV.replace('foo' => '0', 'bar' => '1')
5481 * ENV.values # => ['1', '0']
5482 * The order of the values is OS-dependent.
5483 * See {About Ordering}[rdoc-ref:ENV@About+Ordering].
5484 *
5485 * Returns the empty Array if ENV is empty.
5486 */
5487static VALUE
5488env_f_values(VALUE _)
5489{
5490 return env_values();
5491}
5492
5493/*
5494 * call-seq:
5495 * ENV.each_value { |value| block } -> ENV
5496 * ENV.each_value -> an_enumerator
5497 *
5498 * Yields each environment variable value:
5499 * ENV.replace('foo' => '0', 'bar' => '1') # => ENV
5500 * values = []
5501 * ENV.each_value { |value| values.push(value) } # => ENV
5502 * values # => ["1", "0"]
5503 *
5504 * Returns an Enumerator if no block given:
5505 * e = ENV.each_value # => #<Enumerator: {"bar"=>"1", "foo"=>"0"}:each_value>
5506 * values = []
5507 * e.each { |value| values.push(value) } # => ENV
5508 * values # => ["1", "0"]
5509 */
5510static VALUE
5511env_each_value(VALUE ehash)
5512{
5513 VALUE values;
5514 long i;
5515
5516 RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
5517 values = env_values();
5518 for (i=0; i<RARRAY_LEN(values); i++) {
5519 rb_yield(RARRAY_AREF(values, i));
5520 }
5521 return ehash;
5522}
5523
5524/*
5525 * call-seq:
5526 * ENV.each { |name, value| block } -> ENV
5527 * ENV.each -> an_enumerator
5528 * ENV.each_pair { |name, value| block } -> ENV
5529 * ENV.each_pair -> an_enumerator
5530 *
5531 * Yields each environment variable name and its value as a 2-element Array:
5532 * h = {}
5533 * ENV.each_pair { |name, value| h[name] = value } # => ENV
5534 * h # => {"bar"=>"1", "foo"=>"0"}
5535 *
5536 * Returns an Enumerator if no block given:
5537 * h = {}
5538 * e = ENV.each_pair # => #<Enumerator: {"bar"=>"1", "foo"=>"0"}:each_pair>
5539 * e.each { |name, value| h[name] = value } # => ENV
5540 * h # => {"bar"=>"1", "foo"=>"0"}
5541 */
5542static VALUE
5543env_each_pair(VALUE ehash)
5544{
5545 long i;
5546
5547 RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
5548
5549 VALUE ary = rb_ary_new();
5550
5551 ENV_LOCK();
5552 {
5553 char **env = GET_ENVIRON(environ);
5554
5555 while (*env) {
5556 char *s = strchr(*env, '=');
5557 if (s) {
5558 rb_ary_push(ary, env_str_new(*env, s-*env));
5559 rb_ary_push(ary, env_str_new2(s+1));
5560 }
5561 env++;
5562 }
5563 FREE_ENVIRON(environ);
5564 }
5565 ENV_UNLOCK();
5566
5567 if (rb_block_pair_yield_optimizable()) {
5568 for (i=0; i<RARRAY_LEN(ary); i+=2) {
5569 rb_yield_values(2, RARRAY_AREF(ary, i), RARRAY_AREF(ary, i+1));
5570 }
5571 }
5572 else {
5573 for (i=0; i<RARRAY_LEN(ary); i+=2) {
5574 rb_yield(rb_assoc_new(RARRAY_AREF(ary, i), RARRAY_AREF(ary, i+1)));
5575 }
5576 }
5577
5578 return ehash;
5579}
5580
5581/*
5582 * call-seq:
5583 * ENV.reject! { |name, value| block } -> ENV or nil
5584 * ENV.reject! -> an_enumerator
5585 *
5586 * Similar to ENV.delete_if, but returns +nil+ if no changes were made.
5587 *
5588 * Yields each environment variable name and its value as a 2-element Array,
5589 * deleting each environment variable for which the block returns a truthy value,
5590 * and returning ENV (if any deletions) or +nil+ (if not):
5591 * ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
5592 * ENV.reject! { |name, value| name.start_with?('b') } # => ENV
5593 * ENV # => {"foo"=>"0"}
5594 * ENV.reject! { |name, value| name.start_with?('b') } # => nil
5595 *
5596 * Returns an Enumerator if no block given:
5597 * ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
5598 * e = ENV.reject! # => #<Enumerator: {"bar"=>"1", "baz"=>"2", "foo"=>"0"}:reject!>
5599 * e.each { |name, value| name.start_with?('b') } # => ENV
5600 * ENV # => {"foo"=>"0"}
5601 * e.each { |name, value| name.start_with?('b') } # => nil
5602 */
5603static VALUE
5604env_reject_bang(VALUE ehash)
5605{
5606 VALUE keys;
5607 long i;
5608 int del = 0;
5609
5610 RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
5611 keys = env_keys(FALSE);
5612 RBASIC_CLEAR_CLASS(keys);
5613 for (i=0; i<RARRAY_LEN(keys); i++) {
5614 VALUE val = rb_f_getenv(Qnil, RARRAY_AREF(keys, i));
5615 if (!NIL_P(val)) {
5616 if (RTEST(rb_yield_values(2, RARRAY_AREF(keys, i), val))) {
5617 env_delete(RARRAY_AREF(keys, i));
5618 del++;
5619 }
5620 }
5621 }
5622 RB_GC_GUARD(keys);
5623 if (del == 0) return Qnil;
5624 return envtbl;
5625}
5626
5627/*
5628 * call-seq:
5629 * ENV.delete_if { |name, value| block } -> ENV
5630 * ENV.delete_if -> an_enumerator
5631 *
5632 * Yields each environment variable name and its value as a 2-element Array,
5633 * deleting each environment variable for which the block returns a truthy value,
5634 * and returning ENV (regardless of whether any deletions):
5635 * ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
5636 * ENV.delete_if { |name, value| name.start_with?('b') } # => ENV
5637 * ENV # => {"foo"=>"0"}
5638 * ENV.delete_if { |name, value| name.start_with?('b') } # => ENV
5639 *
5640 * Returns an Enumerator if no block given:
5641 * ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
5642 * e = ENV.delete_if # => #<Enumerator: {"bar"=>"1", "baz"=>"2", "foo"=>"0"}:delete_if!>
5643 * e.each { |name, value| name.start_with?('b') } # => ENV
5644 * ENV # => {"foo"=>"0"}
5645 * e.each { |name, value| name.start_with?('b') } # => ENV
5646 */
5647static VALUE
5648env_delete_if(VALUE ehash)
5649{
5650 RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
5651 env_reject_bang(ehash);
5652 return envtbl;
5653}
5654
5655/*
5656 * call-seq:
5657 * ENV.values_at(*names) -> array of values
5658 *
5659 * Returns an Array containing the environment variable values associated with
5660 * the given names:
5661 * ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
5662 * ENV.values_at('foo', 'baz') # => ["0", "2"]
5663 *
5664 * Returns +nil+ in the Array for each name that is not an ENV name:
5665 * ENV.values_at('foo', 'bat', 'bar', 'bam') # => ["0", nil, "1", nil]
5666 *
5667 * Returns an empty Array if no names given.
5668 *
5669 * Raises an exception if any name is invalid.
5670 * See {Invalid Names and Values}[rdoc-ref:ENV@Invalid+Names+and+Values].
5671 */
5672static VALUE
5673env_values_at(int argc, VALUE *argv, VALUE _)
5674{
5675 VALUE result;
5676 long i;
5677
5678 result = rb_ary_new();
5679 for (i=0; i<argc; i++) {
5680 rb_ary_push(result, rb_f_getenv(Qnil, argv[i]));
5681 }
5682 return result;
5683}
5684
5685/*
5686 * call-seq:
5687 * ENV.select { |name, value| block } -> hash of name/value pairs
5688 * ENV.select -> an_enumerator
5689 * ENV.filter { |name, value| block } -> hash of name/value pairs
5690 * ENV.filter -> an_enumerator
5691 *
5692 * Yields each environment variable name and its value as a 2-element Array,
5693 * returning a Hash of the names and values for which the block returns a truthy value:
5694 * ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
5695 * ENV.select { |name, value| name.start_with?('b') } # => {"bar"=>"1", "baz"=>"2"}
5696 * ENV.filter { |name, value| name.start_with?('b') } # => {"bar"=>"1", "baz"=>"2"}
5697 *
5698 * Returns an Enumerator if no block given:
5699 * e = ENV.select # => #<Enumerator: {"bar"=>"1", "baz"=>"2", "foo"=>"0"}:select>
5700 * e.each { |name, value | name.start_with?('b') } # => {"bar"=>"1", "baz"=>"2"}
5701 * e = ENV.filter # => #<Enumerator: {"bar"=>"1", "baz"=>"2", "foo"=>"0"}:filter>
5702 * e.each { |name, value | name.start_with?('b') } # => {"bar"=>"1", "baz"=>"2"}
5703 */
5704static VALUE
5705env_select(VALUE ehash)
5706{
5707 VALUE result;
5708 VALUE keys;
5709 long i;
5710
5711 RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
5712 result = rb_hash_new();
5713 keys = env_keys(FALSE);
5714 for (i = 0; i < RARRAY_LEN(keys); ++i) {
5715 VALUE key = RARRAY_AREF(keys, i);
5716 VALUE val = rb_f_getenv(Qnil, key);
5717 if (!NIL_P(val)) {
5718 if (RTEST(rb_yield_values(2, key, val))) {
5719 rb_hash_aset(result, key, val);
5720 }
5721 }
5722 }
5723 RB_GC_GUARD(keys);
5724
5725 return result;
5726}
5727
5728/*
5729 * call-seq:
5730 * ENV.select! { |name, value| block } -> ENV or nil
5731 * ENV.select! -> an_enumerator
5732 * ENV.filter! { |name, value| block } -> ENV or nil
5733 * ENV.filter! -> an_enumerator
5734 *
5735 * Yields each environment variable name and its value as a 2-element Array,
5736 * deleting each entry for which the block returns +false+ or +nil+,
5737 * and returning ENV if any deletions made, or +nil+ otherwise:
5738 *
5739 * ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
5740 * ENV.select! { |name, value| name.start_with?('b') } # => ENV
5741 * ENV # => {"bar"=>"1", "baz"=>"2"}
5742 * ENV.select! { |name, value| true } # => nil
5743 *
5744 * ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
5745 * ENV.filter! { |name, value| name.start_with?('b') } # => ENV
5746 * ENV # => {"bar"=>"1", "baz"=>"2"}
5747 * ENV.filter! { |name, value| true } # => nil
5748 *
5749 * Returns an Enumerator if no block given:
5750 *
5751 * ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
5752 * e = ENV.select! # => #<Enumerator: {"bar"=>"1", "baz"=>"2"}:select!>
5753 * e.each { |name, value| name.start_with?('b') } # => ENV
5754 * ENV # => {"bar"=>"1", "baz"=>"2"}
5755 * e.each { |name, value| true } # => nil
5756 *
5757 * ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
5758 * e = ENV.filter! # => #<Enumerator: {"bar"=>"1", "baz"=>"2"}:filter!>
5759 * e.each { |name, value| name.start_with?('b') } # => ENV
5760 * ENV # => {"bar"=>"1", "baz"=>"2"}
5761 * e.each { |name, value| true } # => nil
5762 */
5763static VALUE
5764env_select_bang(VALUE ehash)
5765{
5766 VALUE keys;
5767 long i;
5768 int del = 0;
5769
5770 RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
5771 keys = env_keys(FALSE);
5772 RBASIC_CLEAR_CLASS(keys);
5773 for (i=0; i<RARRAY_LEN(keys); i++) {
5774 VALUE val = rb_f_getenv(Qnil, RARRAY_AREF(keys, i));
5775 if (!NIL_P(val)) {
5776 if (!RTEST(rb_yield_values(2, RARRAY_AREF(keys, i), val))) {
5777 env_delete(RARRAY_AREF(keys, i));
5778 del++;
5779 }
5780 }
5781 }
5782 RB_GC_GUARD(keys);
5783 if (del == 0) return Qnil;
5784 return envtbl;
5785}
5786
5787/*
5788 * call-seq:
5789 * ENV.keep_if { |name, value| block } -> ENV
5790 * ENV.keep_if -> an_enumerator
5791 *
5792 * Yields each environment variable name and its value as a 2-element Array,
5793 * deleting each environment variable for which the block returns +false+ or +nil+,
5794 * and returning ENV:
5795 * ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
5796 * ENV.keep_if { |name, value| name.start_with?('b') } # => ENV
5797 * ENV # => {"bar"=>"1", "baz"=>"2"}
5798 *
5799 * Returns an Enumerator if no block given:
5800 * ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
5801 * e = ENV.keep_if # => #<Enumerator: {"bar"=>"1", "baz"=>"2", "foo"=>"0"}:keep_if>
5802 * e.each { |name, value| name.start_with?('b') } # => ENV
5803 * ENV # => {"bar"=>"1", "baz"=>"2"}
5804 */
5805static VALUE
5806env_keep_if(VALUE ehash)
5807{
5808 RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
5809 env_select_bang(ehash);
5810 return envtbl;
5811}
5812
5813/*
5814 * call-seq:
5815 * ENV.slice(*names) -> hash of name/value pairs
5816 *
5817 * Returns a Hash of the given ENV names and their corresponding values:
5818 * ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2', 'bat' => '3')
5819 * ENV.slice('foo', 'baz') # => {"foo"=>"0", "baz"=>"2"}
5820 * ENV.slice('baz', 'foo') # => {"baz"=>"2", "foo"=>"0"}
5821 * Raises an exception if any of the +names+ is invalid
5822 * (see {Invalid Names and Values}[rdoc-ref:ENV@Invalid+Names+and+Values]):
5823 * ENV.slice('foo', 'bar', :bat) # Raises TypeError (no implicit conversion of Symbol into String)
5824 */
5825static VALUE
5826env_slice(int argc, VALUE *argv, VALUE _)
5827{
5828 int i;
5829 VALUE key, value, result;
5830
5831 if (argc == 0) {
5832 return rb_hash_new();
5833 }
5834 result = rb_hash_new_with_size(argc);
5835
5836 for (i = 0; i < argc; i++) {
5837 key = argv[i];
5838 value = rb_f_getenv(Qnil, key);
5839 if (value != Qnil)
5840 rb_hash_aset(result, key, value);
5841 }
5842
5843 return result;
5844}
5845
5846 VALUE
5847rb_env_clear(void)
5848{
5849 VALUE keys;
5850 long i;
5851
5852 keys = env_keys(TRUE);
5853 for (i=0; i<RARRAY_LEN(keys); i++) {
5854 VALUE key = RARRAY_AREF(keys, i);
5855 const char *nam = RSTRING_PTR(key);
5856 ruby_setenv(nam, 0);
5857 }
5858 RB_GC_GUARD(keys);
5859 return envtbl;
5860}
5861
5862/*
5863 * call-seq:
5864 * ENV.clear -> ENV
5865 *
5866 * Removes every environment variable; returns ENV:
5867 * ENV.replace('foo' => '0', 'bar' => '1')
5868 * ENV.size # => 2
5869 * ENV.clear # => ENV
5870 * ENV.size # => 0
5871 */
5872static VALUE
5873env_clear(VALUE _)
5874{
5875 return rb_env_clear();
5876}
5877
5878/*
5879 * call-seq:
5880 * ENV.to_s -> "ENV"
5881 *
5882 * Returns String 'ENV':
5883 * ENV.to_s # => "ENV"
5884 */
5885static VALUE
5886env_to_s(VALUE _)
5887{
5888 return rb_usascii_str_new2("ENV");
5889}
5890
5891/*
5892 * call-seq:
5893 * ENV.inspect -> a_string
5894 *
5895 * Returns the contents of the environment as a String:
5896 * ENV.replace('foo' => '0', 'bar' => '1')
5897 * ENV.inspect # => "{\"bar\"=>\"1\", \"foo\"=>\"0\"}"
5898 */
5899static VALUE
5900env_inspect(VALUE _)
5901{
5902 VALUE i;
5903 VALUE str = rb_str_buf_new2("{");
5904
5905 ENV_LOCK();
5906 {
5907 char **env = GET_ENVIRON(environ);
5908 while (*env) {
5909 char *s = strchr(*env, '=');
5910
5911 if (env != environ) {
5912 rb_str_buf_cat2(str, ", ");
5913 }
5914 if (s) {
5915 rb_str_buf_cat2(str, "\"");
5916 rb_str_buf_cat(str, *env, s-*env);
5917 rb_str_buf_cat2(str, "\"=>");
5918 i = rb_inspect(rb_str_new2(s+1));
5919 rb_str_buf_append(str, i);
5920 }
5921 env++;
5922 }
5923 FREE_ENVIRON(environ);
5924 }
5925 ENV_UNLOCK();
5926
5927 rb_str_buf_cat2(str, "}");
5928
5929 return str;
5930}
5931
5932/*
5933 * call-seq:
5934 * ENV.to_a -> array of 2-element arrays
5935 *
5936 * Returns the contents of ENV as an Array of 2-element Arrays,
5937 * each of which is a name/value pair:
5938 * ENV.replace('foo' => '0', 'bar' => '1')
5939 * ENV.to_a # => [["bar", "1"], ["foo", "0"]]
5940 */
5941static VALUE
5942env_to_a(VALUE _)
5943{
5944 VALUE ary = rb_ary_new();
5945
5946 ENV_LOCK();
5947 {
5948 char **env = GET_ENVIRON(environ);
5949 while (*env) {
5950 char *s = strchr(*env, '=');
5951 if (s) {
5952 rb_ary_push(ary, rb_assoc_new(env_str_new(*env, s-*env),
5953 env_str_new2(s+1)));
5954 }
5955 env++;
5956 }
5957 FREE_ENVIRON(environ);
5958 }
5959 ENV_UNLOCK();
5960
5961 return ary;
5962}
5963
5964/*
5965 * call-seq:
5966 * ENV.rehash -> nil
5967 *
5968 * (Provided for compatibility with Hash.)
5969 *
5970 * Does not modify ENV; returns +nil+.
5971 */
5972static VALUE
5973env_none(VALUE _)
5974{
5975 return Qnil;
5976}
5977
5978static int
5979env_size_with_lock(void)
5980{
5981 int i = 0;
5982
5983 ENV_LOCK();
5984 {
5985 char **env = GET_ENVIRON(environ);
5986 while (env[i]) i++;
5987 FREE_ENVIRON(environ);
5988 }
5989 ENV_UNLOCK();
5990
5991 return i;
5992}
5993
5994/*
5995 * call-seq:
5996 * ENV.length -> an_integer
5997 * ENV.size -> an_integer
5998 *
5999 * Returns the count of environment variables:
6000 * ENV.replace('foo' => '0', 'bar' => '1')
6001 * ENV.length # => 2
6002 * ENV.size # => 2
6003 */
6004static VALUE
6005env_size(VALUE _)
6006{
6007 return INT2FIX(env_size_with_lock());
6008}
6009
6010/*
6011 * call-seq:
6012 * ENV.empty? -> true or false
6013 *
6014 * Returns +true+ when there are no environment variables, +false+ otherwise:
6015 * ENV.clear
6016 * ENV.empty? # => true
6017 * ENV['foo'] = '0'
6018 * ENV.empty? # => false
6019 */
6020static VALUE
6021env_empty_p(VALUE _)
6022{
6023 bool empty = true;
6024
6025 ENV_LOCK();
6026 {
6027 char **env = GET_ENVIRON(environ);
6028 if (env[0] != 0) {
6029 empty = false;
6030 }
6031 FREE_ENVIRON(environ);
6032 }
6033 ENV_UNLOCK();
6034
6035 return RBOOL(empty);
6036}
6037
6038/*
6039 * call-seq:
6040 * ENV.include?(name) -> true or false
6041 * ENV.has_key?(name) -> true or false
6042 * ENV.member?(name) -> true or false
6043 * ENV.key?(name) -> true or false
6044 *
6045 * Returns +true+ if there is an environment variable with the given +name+:
6046 * ENV.replace('foo' => '0', 'bar' => '1')
6047 * ENV.include?('foo') # => true
6048 * Returns +false+ if +name+ is a valid String and there is no such environment variable:
6049 * ENV.include?('baz') # => false
6050 * Returns +false+ if +name+ is the empty String or is a String containing character <code>'='</code>:
6051 * ENV.include?('') # => false
6052 * ENV.include?('=') # => false
6053 * Raises an exception if +name+ is a String containing the NUL character <code>"\0"</code>:
6054 * ENV.include?("\0") # Raises ArgumentError (bad environment variable name: contains null byte)
6055 * Raises an exception if +name+ has an encoding that is not ASCII-compatible:
6056 * ENV.include?("\xa1\xa1".force_encoding(Encoding::UTF_16LE))
6057 * # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: UTF-16LE)
6058 * Raises an exception if +name+ is not a String:
6059 * ENV.include?(Object.new) # TypeError (no implicit conversion of Object into String)
6060 */
6061static VALUE
6062env_has_key(VALUE env, VALUE key)
6063{
6064 const char *s = env_name(key);
6065 return RBOOL(has_env_with_lock(s));
6066}
6067
6068/*
6069 * call-seq:
6070 * ENV.assoc(name) -> [name, value] or nil
6071 *
6072 * Returns a 2-element Array containing the name and value of the environment variable
6073 * for +name+ if it exists:
6074 * ENV.replace('foo' => '0', 'bar' => '1')
6075 * ENV.assoc('foo') # => ['foo', '0']
6076 * Returns +nil+ if +name+ is a valid String and there is no such environment variable.
6077 *
6078 * Returns +nil+ if +name+ is the empty String or is a String containing character <code>'='</code>.
6079 *
6080 * Raises an exception if +name+ is a String containing the NUL character <code>"\0"</code>:
6081 * ENV.assoc("\0") # Raises ArgumentError (bad environment variable name: contains null byte)
6082 * Raises an exception if +name+ has an encoding that is not ASCII-compatible:
6083 * ENV.assoc("\xa1\xa1".force_encoding(Encoding::UTF_16LE))
6084 * # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: UTF-16LE)
6085 * Raises an exception if +name+ is not a String:
6086 * ENV.assoc(Object.new) # TypeError (no implicit conversion of Object into String)
6087 */
6088static VALUE
6089env_assoc(VALUE env, VALUE key)
6090{
6091 const char *s = env_name(key);
6092 VALUE e = getenv_with_lock(s);
6093
6094 if (!NIL_P(e)) {
6095 return rb_assoc_new(key, e);
6096 }
6097 else {
6098 return Qnil;
6099 }
6100}
6101
6102/*
6103 * call-seq:
6104 * ENV.value?(value) -> true or false
6105 * ENV.has_value?(value) -> true or false
6106 *
6107 * Returns +true+ if +value+ is the value for some environment variable name, +false+ otherwise:
6108 * ENV.replace('foo' => '0', 'bar' => '1')
6109 * ENV.value?('0') # => true
6110 * ENV.has_value?('0') # => true
6111 * ENV.value?('2') # => false
6112 * ENV.has_value?('2') # => false
6113 */
6114static VALUE
6115env_has_value(VALUE dmy, VALUE obj)
6116{
6117 obj = rb_check_string_type(obj);
6118 if (NIL_P(obj)) return Qnil;
6119
6120 VALUE ret = Qfalse;
6121
6122 ENV_LOCK();
6123 {
6124 char **env = GET_ENVIRON(environ);
6125 while (*env) {
6126 char *s = strchr(*env, '=');
6127 if (s++) {
6128 long len = strlen(s);
6129 if (RSTRING_LEN(obj) == len && strncmp(s, RSTRING_PTR(obj), len) == 0) {
6130 ret = Qtrue;
6131 break;
6132 }
6133 }
6134 env++;
6135 }
6136 FREE_ENVIRON(environ);
6137 }
6138 ENV_UNLOCK();
6139
6140 return ret;
6141}
6142
6143/*
6144 * call-seq:
6145 * ENV.rassoc(value) -> [name, value] or nil
6146 *
6147 * Returns a 2-element Array containing the name and value of the
6148 * *first* *found* environment variable that has value +value+, if one
6149 * exists:
6150 * ENV.replace('foo' => '0', 'bar' => '0')
6151 * ENV.rassoc('0') # => ["bar", "0"]
6152 * The order in which environment variables are examined is OS-dependent.
6153 * See {About Ordering}[rdoc-ref:ENV@About+Ordering].
6154 *
6155 * Returns +nil+ if there is no such environment variable.
6156 */
6157static VALUE
6158env_rassoc(VALUE dmy, VALUE obj)
6159{
6160 obj = rb_check_string_type(obj);
6161 if (NIL_P(obj)) return Qnil;
6162
6163 VALUE result = Qnil;
6164
6165 ENV_LOCK();
6166 {
6167 char **env = GET_ENVIRON(environ);
6168
6169 while (*env) {
6170 const char *p = *env;
6171 char *s = strchr(p, '=');
6172 if (s++) {
6173 long len = strlen(s);
6174 if (RSTRING_LEN(obj) == len && strncmp(s, RSTRING_PTR(obj), len) == 0) {
6175 result = rb_assoc_new(rb_str_new(p, s-p-1), obj);
6176 break;
6177 }
6178 }
6179 env++;
6180 }
6181 FREE_ENVIRON(environ);
6182 }
6183 ENV_UNLOCK();
6184
6185 return result;
6186}
6187
6188/*
6189 * call-seq:
6190 * ENV.key(value) -> name or nil
6191 *
6192 * Returns the name of the first environment variable with +value+, if it exists:
6193 * ENV.replace('foo' => '0', 'bar' => '0')
6194 * ENV.key('0') # => "foo"
6195 * The order in which environment variables are examined is OS-dependent.
6196 * See {About Ordering}[rdoc-ref:ENV@About+Ordering].
6197 *
6198 * Returns +nil+ if there is no such value.
6199 *
6200 * Raises an exception if +value+ is invalid:
6201 * ENV.key(Object.new) # raises TypeError (no implicit conversion of Object into String)
6202 * See {Invalid Names and Values}[rdoc-ref:ENV@Invalid+Names+and+Values].
6203 */
6204static VALUE
6205env_key(VALUE dmy, VALUE value)
6206{
6207 SafeStringValue(value);
6208 VALUE str = Qnil;
6209
6210 ENV_LOCK();
6211 {
6212 char **env = GET_ENVIRON(environ);
6213 while (*env) {
6214 char *s = strchr(*env, '=');
6215 if (s++) {
6216 long len = strlen(s);
6217 if (RSTRING_LEN(value) == len && strncmp(s, RSTRING_PTR(value), len) == 0) {
6218 str = env_str_new(*env, s-*env-1);
6219 break;
6220 }
6221 }
6222 env++;
6223 }
6224 FREE_ENVIRON(environ);
6225 }
6226 ENV_UNLOCK();
6227
6228 return str;
6229}
6230
6231static VALUE
6232env_to_hash(void)
6233{
6234 VALUE hash = rb_hash_new();
6235
6236 ENV_LOCK();
6237 {
6238 char **env = GET_ENVIRON(environ);
6239 while (*env) {
6240 char *s = strchr(*env, '=');
6241 if (s) {
6242 rb_hash_aset(hash, env_str_new(*env, s-*env),
6243 env_str_new2(s+1));
6244 }
6245 env++;
6246 }
6247 FREE_ENVIRON(environ);
6248 }
6249 ENV_UNLOCK();
6250
6251 return hash;
6252}
6253
6254 VALUE
6255rb_envtbl(void)
6256{
6257 return envtbl;
6258}
6259
6260 VALUE
6261rb_env_to_hash(void)
6262{
6263 return env_to_hash();
6264}
6265
6266/*
6267 * call-seq:
6268 * ENV.to_hash -> hash of name/value pairs
6269 *
6270 * Returns a Hash containing all name/value pairs from ENV:
6271 * ENV.replace('foo' => '0', 'bar' => '1')
6272 * ENV.to_hash # => {"bar"=>"1", "foo"=>"0"}
6273 */
6274
6275static VALUE
6276env_f_to_hash(VALUE _)
6277{
6278 return env_to_hash();
6279}
6280
6281/*
6282 * call-seq:
6283 * ENV.to_h -> hash of name/value pairs
6284 * ENV.to_h {|name, value| block } -> hash of name/value pairs
6285 *
6286 * With no block, returns a Hash containing all name/value pairs from ENV:
6287 * ENV.replace('foo' => '0', 'bar' => '1')
6288 * ENV.to_h # => {"bar"=>"1", "foo"=>"0"}
6289 * With a block, returns a Hash whose items are determined by the block.
6290 * Each name/value pair in ENV is yielded to the block.
6291 * The block must return a 2-element Array (name/value pair)
6292 * that is added to the return Hash as a key and value:
6293 * ENV.to_h { |name, value| [name.to_sym, value.to_i] } # => {:bar=>1, :foo=>0}
6294 * Raises an exception if the block does not return an Array:
6295 * ENV.to_h { |name, value| name } # Raises TypeError (wrong element type String (expected array))
6296 * Raises an exception if the block returns an Array of the wrong size:
6297 * ENV.to_h { |name, value| [name] } # Raises ArgumentError (element has wrong array length (expected 2, was 1))
6298 */
6299static VALUE
6300env_to_h(VALUE _)
6301{
6302 VALUE hash = env_to_hash();
6303 if (rb_block_given_p()) {
6304 hash = rb_hash_to_h_block(hash);
6305 }
6306 return hash;
6307}
6308
6309/*
6310 * call-seq:
6311 * ENV.except(*keys) -> a_hash
6312 *
6313 * Returns a hash except the given keys from ENV and their values.
6314 *
6315 * ENV #=> {"LANG"=>"en_US.UTF-8", "TERM"=>"xterm-256color", "HOME"=>"/Users/rhc"}
6316 * ENV.except("TERM","HOME") #=> {"LANG"=>"en_US.UTF-8"}
6317 */
6318static VALUE
6319env_except(int argc, VALUE *argv, VALUE _)
6320{
6321 int i;
6322 VALUE key, hash = env_to_hash();
6323
6324 for (i = 0; i < argc; i++) {
6325 key = argv[i];
6326 rb_hash_delete(hash, key);
6327 }
6328
6329 return hash;
6330}
6331
6332/*
6333 * call-seq:
6334 * ENV.reject { |name, value| block } -> hash of name/value pairs
6335 * ENV.reject -> an_enumerator
6336 *
6337 * Yields each environment variable name and its value as a 2-element Array.
6338 * Returns a Hash whose items are determined by the block.
6339 * When the block returns a truthy value, the name/value pair is added to the return Hash;
6340 * otherwise the pair is ignored:
6341 * ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
6342 * ENV.reject { |name, value| name.start_with?('b') } # => {"foo"=>"0"}
6343 * Returns an Enumerator if no block given:
6344 * e = ENV.reject
6345 * e.each { |name, value| name.start_with?('b') } # => {"foo"=>"0"}
6346 */
6347static VALUE
6348env_reject(VALUE _)
6349{
6350 return rb_hash_delete_if(env_to_hash());
6351}
6352
6353NORETURN(static VALUE env_freeze(VALUE self));
6354/*
6355 * call-seq:
6356 * ENV.freeze
6357 *
6358 * Raises an exception:
6359 * ENV.freeze # Raises TypeError (cannot freeze ENV)
6360 */
6361static VALUE
6362env_freeze(VALUE self)
6363{
6364 rb_raise(rb_eTypeError, "cannot freeze ENV");
6365 UNREACHABLE_RETURN(self);
6366}
6367
6368/*
6369 * call-seq:
6370 * ENV.shift -> [name, value] or nil
6371 *
6372 * Removes the first environment variable from ENV and returns
6373 * a 2-element Array containing its name and value:
6374 * ENV.replace('foo' => '0', 'bar' => '1')
6375 * ENV.to_hash # => {'bar' => '1', 'foo' => '0'}
6376 * ENV.shift # => ['bar', '1']
6377 * ENV.to_hash # => {'foo' => '0'}
6378 * Exactly which environment variable is "first" is OS-dependent.
6379 * See {About Ordering}[rdoc-ref:ENV@About+Ordering].
6380 *
6381 * Returns +nil+ if the environment is empty.
6382 */
6383static VALUE
6384env_shift(VALUE _)
6385{
6386 VALUE result = Qnil;
6387 VALUE key = Qnil;
6388
6389 ENV_LOCK();
6390 {
6391 char **env = GET_ENVIRON(environ);
6392 if (*env) {
6393 const char *p = *env;
6394 char *s = strchr(p, '=');
6395 if (s) {
6396 key = env_str_new(p, s-p);
6397 VALUE val = env_str_new2(getenv(RSTRING_PTR(key)));
6398 result = rb_assoc_new(key, val);
6399 }
6400 }
6401 FREE_ENVIRON(environ);
6402 }
6403 ENV_UNLOCK();
6404
6405 if (!NIL_P(key)) {
6406 env_delete(key);
6407 }
6408
6409 return result;
6410}
6411
6412/*
6413 * call-seq:
6414 * ENV.invert -> hash of value/name pairs
6415 *
6416 * Returns a Hash whose keys are the ENV values,
6417 * and whose values are the corresponding ENV names:
6418 * ENV.replace('foo' => '0', 'bar' => '1')
6419 * ENV.invert # => {"1"=>"bar", "0"=>"foo"}
6420 * For a duplicate ENV value, overwrites the hash entry:
6421 * ENV.replace('foo' => '0', 'bar' => '0')
6422 * ENV.invert # => {"0"=>"foo"}
6423 * Note that the order of the ENV processing is OS-dependent,
6424 * which means that the order of overwriting is also OS-dependent.
6425 * See {About Ordering}[rdoc-ref:ENV@About+Ordering].
6426 */
6427static VALUE
6428env_invert(VALUE _)
6429{
6430 return rb_hash_invert(env_to_hash());
6431}
6432
6433static void
6434keylist_delete(VALUE keys, VALUE key)
6435{
6436 long keylen, elen;
6437 const char *keyptr, *eptr;
6438 RSTRING_GETMEM(key, keyptr, keylen);
6439 /* Don't stop at first key, as it is possible to have
6440 multiple environment values with the same key.
6441 */
6442 for (long i=0; i<RARRAY_LEN(keys); i++) {
6443 VALUE e = RARRAY_AREF(keys, i);
6444 RSTRING_GETMEM(e, eptr, elen);
6445 if (elen != keylen) continue;
6446 if (!ENVNMATCH(keyptr, eptr, elen)) continue;
6447 rb_ary_delete_at(keys, i);
6448 i--;
6449 }
6450}
6451
6452static int
6453env_replace_i(VALUE key, VALUE val, VALUE keys)
6454{
6455 env_name(key);
6456 env_aset(key, val);
6457
6458 keylist_delete(keys, key);
6459 return ST_CONTINUE;
6460}
6461
6462/*
6463 * call-seq:
6464 * ENV.replace(hash) -> ENV
6465 *
6466 * Replaces the entire content of the environment variables
6467 * with the name/value pairs in the given +hash+;
6468 * returns ENV.
6469 *
6470 * Replaces the content of ENV with the given pairs:
6471 * ENV.replace('foo' => '0', 'bar' => '1') # => ENV
6472 * ENV.to_hash # => {"bar"=>"1", "foo"=>"0"}
6473 *
6474 * Raises an exception if a name or value is invalid
6475 * (see {Invalid Names and Values}[rdoc-ref:ENV@Invalid+Names+and+Values]):
6476 * ENV.replace('foo' => '0', :bar => '1') # Raises TypeError (no implicit conversion of Symbol into String)
6477 * ENV.replace('foo' => '0', 'bar' => 1) # Raises TypeError (no implicit conversion of Integer into String)
6478 * ENV.to_hash # => {"bar"=>"1", "foo"=>"0"}
6479 */
6480static VALUE
6481env_replace(VALUE env, VALUE hash)
6482{
6483 VALUE keys;
6484 long i;
6485
6486 keys = env_keys(TRUE);
6487 if (env == hash) return env;
6488 hash = to_hash(hash);
6489 rb_hash_foreach(hash, env_replace_i, keys);
6490
6491 for (i=0; i<RARRAY_LEN(keys); i++) {
6492 env_delete(RARRAY_AREF(keys, i));
6493 }
6494 RB_GC_GUARD(keys);
6495 return env;
6496}
6497
6498static int
6499env_update_i(VALUE key, VALUE val, VALUE _)
6500{
6501 env_aset(key, val);
6502 return ST_CONTINUE;
6503}
6504
6505static int
6506env_update_block_i(VALUE key, VALUE val, VALUE _)
6507{
6508 VALUE oldval = rb_f_getenv(Qnil, key);
6509 if (!NIL_P(oldval)) {
6510 val = rb_yield_values(3, key, oldval, val);
6511 }
6512 env_aset(key, val);
6513 return ST_CONTINUE;
6514}
6515
6516/*
6517 * call-seq:
6518 * ENV.update -> ENV
6519 * ENV.update(*hashes) -> ENV
6520 * ENV.update(*hashes) { |name, env_val, hash_val| block } -> ENV
6521 * ENV.merge! -> ENV
6522 * ENV.merge!(*hashes) -> ENV
6523 * ENV.merge!(*hashes) { |name, env_val, hash_val| block } -> ENV
6524 *
6525 * Adds to ENV each key/value pair in the given +hash+; returns ENV:
6526 * ENV.replace('foo' => '0', 'bar' => '1')
6527 * ENV.merge!('baz' => '2', 'bat' => '3') # => {"bar"=>"1", "bat"=>"3", "baz"=>"2", "foo"=>"0"}
6528 * Deletes the ENV entry for a hash value that is +nil+:
6529 * ENV.merge!('baz' => nil, 'bat' => nil) # => {"bar"=>"1", "foo"=>"0"}
6530 * For an already-existing name, if no block given, overwrites the ENV value:
6531 * ENV.merge!('foo' => '4') # => {"bar"=>"1", "foo"=>"4"}
6532 * For an already-existing name, if block given,
6533 * yields the name, its ENV value, and its hash value;
6534 * the block's return value becomes the new name:
6535 * ENV.merge!('foo' => '5') { |name, env_val, hash_val | env_val + hash_val } # => {"bar"=>"1", "foo"=>"45"}
6536 * Raises an exception if a name or value is invalid
6537 * (see {Invalid Names and Values}[rdoc-ref:ENV@Invalid+Names+and+Values]);
6538 * ENV.replace('foo' => '0', 'bar' => '1')
6539 * ENV.merge!('foo' => '6', :bar => '7', 'baz' => '9') # Raises TypeError (no implicit conversion of Symbol into String)
6540 * ENV # => {"bar"=>"1", "foo"=>"6"}
6541 * ENV.merge!('foo' => '7', 'bar' => 8, 'baz' => '9') # Raises TypeError (no implicit conversion of Integer into String)
6542 * ENV # => {"bar"=>"1", "foo"=>"7"}
6543 * Raises an exception if the block returns an invalid name:
6544 * (see {Invalid Names and Values}[rdoc-ref:ENV@Invalid+Names+and+Values]):
6545 * ENV.merge!('bat' => '8', 'foo' => '9') { |name, env_val, hash_val | 10 } # Raises TypeError (no implicit conversion of Integer into String)
6546 * ENV # => {"bar"=>"1", "bat"=>"8", "foo"=>"7"}
6547 *
6548 * Note that for the exceptions above,
6549 * hash pairs preceding an invalid name or value are processed normally;
6550 * those following are ignored.
6551 */
6552static VALUE
6553env_update(int argc, VALUE *argv, VALUE env)
6554{
6555 rb_foreach_func *func = rb_block_given_p() ?
6556 env_update_block_i : env_update_i;
6557 for (int i = 0; i < argc; ++i) {
6558 VALUE hash = argv[i];
6559 if (env == hash) continue;
6560 hash = to_hash(hash);
6561 rb_hash_foreach(hash, func, 0);
6562 }
6563 return env;
6564}
6565
6566NORETURN(static VALUE env_clone(int, VALUE *, VALUE));
6567/*
6568 * call-seq:
6569 * ENV.clone(freeze: nil) # raises TypeError
6570 *
6571 * Raises TypeError, because ENV is a wrapper for the process-wide
6572 * environment variables and a clone is useless.
6573 * Use #to_h to get a copy of ENV data as a hash.
6574 */
6575static VALUE
6576env_clone(int argc, VALUE *argv, VALUE obj)
6577{
6578 if (argc) {
6579 VALUE opt;
6580 if (rb_scan_args(argc, argv, "0:", &opt) < argc) {
6581 rb_get_freeze_opt(1, &opt);
6582 }
6583 }
6584
6585 rb_raise(rb_eTypeError, "Cannot clone ENV, use ENV.to_h to get a copy of ENV as a hash");
6586}
6587
6588NORETURN(static VALUE env_dup(VALUE));
6589/*
6590 * call-seq:
6591 * ENV.dup # raises TypeError
6592 *
6593 * Raises TypeError, because ENV is a singleton object.
6594 * Use #to_h to get a copy of ENV data as a hash.
6595 */
6596static VALUE
6597env_dup(VALUE obj)
6598{
6599 rb_raise(rb_eTypeError, "Cannot dup ENV, use ENV.to_h to get a copy of ENV as a hash");
6600}
6601
6602static const rb_data_type_t env_data_type = {
6603 "ENV",
6604 {
6605 NULL,
6606 NULL,
6607 NULL,
6608 NULL,
6609 },
6610 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
6611};
6612
6613/*
6614 * A \Hash maps each of its unique keys to a specific value.
6615 *
6616 * A \Hash has certain similarities to an Array, but:
6617 * - An Array index is always an Integer.
6618 * - A \Hash key can be (almost) any object.
6619 *
6620 * === \Hash \Data Syntax
6621 *
6622 * The older syntax for \Hash data uses the "hash rocket," <tt>=></tt>:
6623 *
6624 * h = {:foo => 0, :bar => 1, :baz => 2}
6625 * h # => {:foo=>0, :bar=>1, :baz=>2}
6626 *
6627 * Alternatively, but only for a \Hash key that's a Symbol,
6628 * you can use a newer JSON-style syntax,
6629 * where each bareword becomes a Symbol:
6630 *
6631 * h = {foo: 0, bar: 1, baz: 2}
6632 * h # => {:foo=>0, :bar=>1, :baz=>2}
6633 *
6634 * You can also use a String in place of a bareword:
6635 *
6636 * h = {'foo': 0, 'bar': 1, 'baz': 2}
6637 * h # => {:foo=>0, :bar=>1, :baz=>2}
6638 *
6639 * And you can mix the styles:
6640 *
6641 * h = {foo: 0, :bar => 1, 'baz': 2}
6642 * h # => {:foo=>0, :bar=>1, :baz=>2}
6643 *
6644 * But it's an error to try the JSON-style syntax
6645 * for a key that's not a bareword or a String:
6646 *
6647 * # Raises SyntaxError (syntax error, unexpected ':', expecting =>):
6648 * h = {0: 'zero'}
6649 *
6650 * Hash value can be omitted, meaning that value will be fetched from the context
6651 * by the name of the key:
6652 *
6653 * x = 0
6654 * y = 100
6655 * h = {x:, y:}
6656 * h # => {:x=>0, :y=>100}
6657 *
6658 * === Common Uses
6659 *
6660 * You can use a \Hash to give names to objects:
6661 *
6662 * person = {name: 'Matz', language: 'Ruby'}
6663 * person # => {:name=>"Matz", :language=>"Ruby"}
6664 *
6665 * You can use a \Hash to give names to method arguments:
6666 *
6667 * def some_method(hash)
6668 * p hash
6669 * end
6670 * some_method({foo: 0, bar: 1, baz: 2}) # => {:foo=>0, :bar=>1, :baz=>2}
6671 *
6672 * Note: when the last argument in a method call is a \Hash,
6673 * the curly braces may be omitted:
6674 *
6675 * some_method(foo: 0, bar: 1, baz: 2) # => {:foo=>0, :bar=>1, :baz=>2}
6676 *
6677 * You can use a \Hash to initialize an object:
6678 *
6679 * class Dev
6680 * attr_accessor :name, :language
6681 * def initialize(hash)
6682 * self.name = hash[:name]
6683 * self.language = hash[:language]
6684 * end
6685 * end
6686 * matz = Dev.new(name: 'Matz', language: 'Ruby')
6687 * matz # => #<Dev: @name="Matz", @language="Ruby">
6688 *
6689 * === Creating a \Hash
6690 *
6691 * You can create a \Hash object explicitly with:
6692 *
6693 * - A {hash literal}[rdoc-ref:syntax/literals.rdoc@Hash+Literals].
6694 *
6695 * You can convert certain objects to Hashes with:
6696 *
6697 * - \Method #Hash.
6698 *
6699 * You can create a \Hash by calling method Hash.new.
6700 *
6701 * Create an empty Hash:
6702 *
6703 * h = Hash.new
6704 * h # => {}
6705 * h.class # => Hash
6706 *
6707 * You can create a \Hash by calling method Hash.[].
6708 *
6709 * Create an empty Hash:
6710 *
6711 * h = Hash[]
6712 * h # => {}
6713 *
6714 * Create a \Hash with initial entries:
6715 *
6716 * h = Hash[foo: 0, bar: 1, baz: 2]
6717 * h # => {:foo=>0, :bar=>1, :baz=>2}
6718 *
6719 * You can create a \Hash by using its literal form (curly braces).
6720 *
6721 * Create an empty \Hash:
6722 *
6723 * h = {}
6724 * h # => {}
6725 *
6726 * Create a \Hash with initial entries:
6727 *
6728 * h = {foo: 0, bar: 1, baz: 2}
6729 * h # => {:foo=>0, :bar=>1, :baz=>2}
6730 *
6731 *
6732 * === \Hash Value Basics
6733 *
6734 * The simplest way to retrieve a \Hash value (instance method #[]):
6735 *
6736 * h = {foo: 0, bar: 1, baz: 2}
6737 * h[:foo] # => 0
6738 *
6739 * The simplest way to create or update a \Hash value (instance method #[]=):
6740 *
6741 * h = {foo: 0, bar: 1, baz: 2}
6742 * h[:bat] = 3 # => 3
6743 * h # => {:foo=>0, :bar=>1, :baz=>2, :bat=>3}
6744 * h[:foo] = 4 # => 4
6745 * h # => {:foo=>4, :bar=>1, :baz=>2, :bat=>3}
6746 *
6747 * The simplest way to delete a \Hash entry (instance method #delete):
6748 *
6749 * h = {foo: 0, bar: 1, baz: 2}
6750 * h.delete(:bar) # => 1
6751 * h # => {:foo=>0, :baz=>2}
6752 *
6753 * === Entry Order
6754 *
6755 * A \Hash object presents its entries in the order of their creation. This is seen in:
6756 *
6757 * - Iterative methods such as <tt>each</tt>, <tt>each_key</tt>, <tt>each_pair</tt>, <tt>each_value</tt>.
6758 * - Other order-sensitive methods such as <tt>shift</tt>, <tt>keys</tt>, <tt>values</tt>.
6759 * - The String returned by method <tt>inspect</tt>.
6760 *
6761 * A new \Hash has its initial ordering per the given entries:
6762 *
6763 * h = Hash[foo: 0, bar: 1]
6764 * h # => {:foo=>0, :bar=>1}
6765 *
6766 * New entries are added at the end:
6767 *
6768 * h[:baz] = 2
6769 * h # => {:foo=>0, :bar=>1, :baz=>2}
6770 *
6771 * Updating a value does not affect the order:
6772 *
6773 * h[:baz] = 3
6774 * h # => {:foo=>0, :bar=>1, :baz=>3}
6775 *
6776 * But re-creating a deleted entry can affect the order:
6777 *
6778 * h.delete(:foo)
6779 * h[:foo] = 5
6780 * h # => {:bar=>1, :baz=>3, :foo=>5}
6781 *
6782 * === \Hash Keys
6783 *
6784 * ==== \Hash Key Equivalence
6785 *
6786 * Two objects are treated as the same \hash key when their <code>hash</code> value
6787 * is identical and the two objects are <code>eql?</code> to each other.
6788 *
6789 * ==== Modifying an Active \Hash Key
6790 *
6791 * Modifying a \Hash key while it is in use damages the hash's index.
6792 *
6793 * This \Hash has keys that are Arrays:
6794 *
6795 * a0 = [ :foo, :bar ]
6796 * a1 = [ :baz, :bat ]
6797 * h = {a0 => 0, a1 => 1}
6798 * h.include?(a0) # => true
6799 * h[a0] # => 0
6800 * a0.hash # => 110002110
6801 *
6802 * Modifying array element <tt>a0[0]</tt> changes its hash value:
6803 *
6804 * a0[0] = :bam
6805 * a0.hash # => 1069447059
6806 *
6807 * And damages the \Hash index:
6808 *
6809 * h.include?(a0) # => false
6810 * h[a0] # => nil
6811 *
6812 * You can repair the hash index using method +rehash+:
6813 *
6814 * h.rehash # => {[:bam, :bar]=>0, [:baz, :bat]=>1}
6815 * h.include?(a0) # => true
6816 * h[a0] # => 0
6817 *
6818 * A String key is always safe.
6819 * That's because an unfrozen String
6820 * passed as a key will be replaced by a duplicated and frozen String:
6821 *
6822 * s = 'foo'
6823 * s.frozen? # => false
6824 * h = {s => 0}
6825 * first_key = h.keys.first
6826 * first_key.frozen? # => true
6827 *
6828 * ==== User-Defined \Hash Keys
6829 *
6830 * To be useable as a \Hash key, objects must implement the methods <code>hash</code> and <code>eql?</code>.
6831 * Note: this requirement does not apply if the \Hash uses #compare_by_identity since comparison will then
6832 * rely on the keys' object id instead of <code>hash</code> and <code>eql?</code>.
6833 *
6834 * Object defines basic implementation for <code>hash</code> and <code>eq?</code> that makes each object
6835 * a distinct key. Typically, user-defined classes will want to override these methods to provide meaningful
6836 * behavior, or for example inherit Struct that has useful definitions for these.
6837 *
6838 * A typical implementation of <code>hash</code> is based on the
6839 * object's data while <code>eql?</code> is usually aliased to the overridden
6840 * <code>==</code> method:
6841 *
6842 * class Book
6843 * attr_reader :author, :title
6844 *
6845 * def initialize(author, title)
6846 * @author = author
6847 * @title = title
6848 * end
6849 *
6850 * def ==(other)
6851 * self.class === other &&
6852 * other.author == @author &&
6853 * other.title == @title
6854 * end
6855 *
6856 * alias eql? ==
6857 *
6858 * def hash
6859 * [self.class, @author, @title].hash
6860 * end
6861 * end
6862 *
6863 * book1 = Book.new 'matz', 'Ruby in a Nutshell'
6864 * book2 = Book.new 'matz', 'Ruby in a Nutshell'
6865 *
6866 * reviews = {}
6867 *
6868 * reviews[book1] = 'Great reference!'
6869 * reviews[book2] = 'Nice and compact!'
6870 *
6871 * reviews.length #=> 1
6872 *
6873 * === Default Values
6874 *
6875 * The methods #[], #values_at and #dig need to return the value associated to a certain key.
6876 * When that key is not found, that value will be determined by its default proc (if any)
6877 * or else its default (initially `nil`).
6878 *
6879 * You can retrieve the default value with method #default:
6880 *
6881 * h = Hash.new
6882 * h.default # => nil
6883 *
6884 * You can set the default value by passing an argument to method Hash.new or
6885 * with method #default=
6886 *
6887 * h = Hash.new(-1)
6888 * h.default # => -1
6889 * h.default = 0
6890 * h.default # => 0
6891 *
6892 * This default value is returned for #[], #values_at and #dig when a key is
6893 * not found:
6894 *
6895 * counts = {foo: 42}
6896 * counts.default # => nil (default)
6897 * counts[:foo] = 42
6898 * counts[:bar] # => nil
6899 * counts.default = 0
6900 * counts[:bar] # => 0
6901 * counts.values_at(:foo, :bar, :baz) # => [42, 0, 0]
6902 * counts.dig(:bar) # => 0
6903 *
6904 * Note that the default value is used without being duplicated. It is not advised to set
6905 * the default value to a mutable object:
6906 *
6907 * synonyms = Hash.new([])
6908 * synonyms[:hello] # => []
6909 * synonyms[:hello] << :hi # => [:hi], but this mutates the default!
6910 * synonyms.default # => [:hi]
6911 * synonyms[:world] << :universe
6912 * synonyms[:world] # => [:hi, :universe], oops
6913 * synonyms.keys # => [], oops
6914 *
6915 * To use a mutable object as default, it is recommended to use a default proc
6916 *
6917 * ==== Default Proc
6918 *
6919 * When the default proc for a \Hash is set (i.e., not +nil+),
6920 * the default value returned by method #[] is determined by the default proc alone.
6921 *
6922 * You can retrieve the default proc with method #default_proc:
6923 *
6924 * h = Hash.new
6925 * h.default_proc # => nil
6926 *
6927 * You can set the default proc by calling Hash.new with a block or
6928 * calling the method #default_proc=
6929 *
6930 * h = Hash.new { |hash, key| "Default value for #{key}" }
6931 * h.default_proc.class # => Proc
6932 * h.default_proc = proc { |hash, key| "Default value for #{key.inspect}" }
6933 * h.default_proc.class # => Proc
6934 *
6935 * When the default proc is set (i.e., not +nil+)
6936 * and method #[] is called with with a non-existent key,
6937 * #[] calls the default proc with both the \Hash object itself and the missing key,
6938 * then returns the proc's return value:
6939 *
6940 * h = Hash.new { |hash, key| "Default value for #{key}" }
6941 * h[:nosuch] # => "Default value for nosuch"
6942 *
6943 * Note that in the example above no entry for key +:nosuch+ is created:
6944 *
6945 * h.include?(:nosuch) # => false
6946 *
6947 * However, the proc itself can add a new entry:
6948 *
6949 * synonyms = Hash.new { |hash, key| hash[key] = [] }
6950 * synonyms.include?(:hello) # => false
6951 * synonyms[:hello] << :hi # => [:hi]
6952 * synonyms[:world] << :universe # => [:universe]
6953 * synonyms.keys # => [:hello, :world]
6954 *
6955 * Note that setting the default proc will clear the default value and vice versa.
6956 *
6957 * Be aware that a default proc that modifies the hash is not thread-safe in the
6958 * sense that multiple threads can call into the default proc concurrently for the
6959 * same key.
6960 *
6961 * === What's Here
6962 *
6963 * First, what's elsewhere. \Class \Hash:
6964 *
6965 * - Inherits from {class Object}[rdoc-ref:Object@What-27s+Here].
6966 * - Includes {module Enumerable}[rdoc-ref:Enumerable@What-27s+Here],
6967 * which provides dozens of additional methods.
6968 *
6969 * Here, class \Hash provides methods that are useful for:
6970 *
6971 * - {Creating a Hash}[rdoc-ref:Hash@Methods+for+Creating+a+Hash]
6972 * - {Setting Hash State}[rdoc-ref:Hash@Methods+for+Setting+Hash+State]
6973 * - {Querying}[rdoc-ref:Hash@Methods+for+Querying]
6974 * - {Comparing}[rdoc-ref:Hash@Methods+for+Comparing]
6975 * - {Fetching}[rdoc-ref:Hash@Methods+for+Fetching]
6976 * - {Assigning}[rdoc-ref:Hash@Methods+for+Assigning]
6977 * - {Deleting}[rdoc-ref:Hash@Methods+for+Deleting]
6978 * - {Iterating}[rdoc-ref:Hash@Methods+for+Iterating]
6979 * - {Converting}[rdoc-ref:Hash@Methods+for+Converting]
6980 * - {Transforming Keys and Values}[rdoc-ref:Hash@Methods+for+Transforming+Keys+and+Values]
6981 * - {And more....}[rdoc-ref:Hash@Other+Methods]
6982 *
6983 * \Class \Hash also includes methods from module Enumerable.
6984 *
6985 * ==== Methods for Creating a \Hash
6986 *
6987 * - ::[]: Returns a new hash populated with given objects.
6988 * - ::new: Returns a new empty hash.
6989 * - ::try_convert: Returns a new hash created from a given object.
6990 *
6991 * ==== Methods for Setting \Hash State
6992 *
6993 * - #compare_by_identity: Sets +self+ to consider only identity in comparing keys.
6994 * - #default=: Sets the default to a given value.
6995 * - #default_proc=: Sets the default proc to a given proc.
6996 * - #rehash: Rebuilds the hash table by recomputing the hash index for each key.
6997 *
6998 * ==== Methods for Querying
6999 *
7000 * - #any?: Returns whether any element satisfies a given criterion.
7001 * - #compare_by_identity?: Returns whether the hash considers only identity when comparing keys.
7002 * - #default: Returns the default value, or the default value for a given key.
7003 * - #default_proc: Returns the default proc.
7004 * - #empty?: Returns whether there are no entries.
7005 * - #eql?: Returns whether a given object is equal to +self+.
7006 * - #hash: Returns the integer hash code.
7007 * - #has_value?: Returns whether a given object is a value in +self+.
7008 * - #include?, #has_key?, #member?, #key?: Returns whether a given object is a key in +self+.
7009 * - #length, #size: Returns the count of entries.
7010 * - #value?: Returns whether a given object is a value in +self+.
7011 *
7012 * ==== Methods for Comparing
7013 *
7014 * - #<: Returns whether +self+ is a proper subset of a given object.
7015 * - #<=: Returns whether +self+ is a subset of a given object.
7016 * - #==: Returns whether a given object is equal to +self+.
7017 * - #>: Returns whether +self+ is a proper superset of a given object
7018 * - #>=: Returns whether +self+ is a superset of a given object.
7019 *
7020 * ==== Methods for Fetching
7021 *
7022 * - #[]: Returns the value associated with a given key.
7023 * - #assoc: Returns a 2-element array containing a given key and its value.
7024 * - #dig: Returns the object in nested objects that is specified
7025 * by a given key and additional arguments.
7026 * - #fetch: Returns the value for a given key.
7027 * - #fetch_values: Returns array containing the values associated with given keys.
7028 * - #key: Returns the key for the first-found entry with a given value.
7029 * - #keys: Returns an array containing all keys in +self+.
7030 * - #rassoc: Returns a 2-element array consisting of the key and value
7031 * of the first-found entry having a given value.
7032 * - #values: Returns an array containing all values in +self+/
7033 * - #values_at: Returns an array containing values for given keys.
7034 *
7035 * ==== Methods for Assigning
7036 *
7037 * - #[]=, #store: Associates a given key with a given value.
7038 * - #merge: Returns the hash formed by merging each given hash into a copy of +self+.
7039 * - #merge!, #update: Merges each given hash into +self+.
7040 * - #replace: Replaces the entire contents of +self+ with the contents of a given hash.
7041 *
7042 * ==== Methods for Deleting
7043 *
7044 * These methods remove entries from +self+:
7045 *
7046 * - #clear: Removes all entries from +self+.
7047 * - #compact!: Removes all +nil+-valued entries from +self+.
7048 * - #delete: Removes the entry for a given key.
7049 * - #delete_if: Removes entries selected by a given block.
7050 * - #filter!, #select!: Keep only those entries selected by a given block.
7051 * - #keep_if: Keep only those entries selected by a given block.
7052 * - #reject!: Removes entries selected by a given block.
7053 * - #shift: Removes and returns the first entry.
7054 *
7055 * These methods return a copy of +self+ with some entries removed:
7056 *
7057 * - #compact: Returns a copy of +self+ with all +nil+-valued entries removed.
7058 * - #except: Returns a copy of +self+ with entries removed for specified keys.
7059 * - #filter, #select: Returns a copy of +self+ with only those entries selected by a given block.
7060 * - #reject: Returns a copy of +self+ with entries removed as specified by a given block.
7061 * - #slice: Returns a hash containing the entries for given keys.
7062 *
7063 * ==== Methods for Iterating
7064 * - #each, #each_pair: Calls a given block with each key-value pair.
7065 * - #each_key: Calls a given block with each key.
7066 * - #each_value: Calls a given block with each value.
7067 *
7068 * ==== Methods for Converting
7069 *
7070 * - #inspect, #to_s: Returns a new String containing the hash entries.
7071 * - #to_a: Returns a new array of 2-element arrays;
7072 * each nested array contains a key-value pair from +self+.
7073 * - #to_h: Returns +self+ if a \Hash;
7074 * if a subclass of \Hash, returns a \Hash containing the entries from +self+.
7075 * - #to_hash: Returns +self+.
7076 * - #to_proc: Returns a proc that maps a given key to its value.
7077 *
7078 * ==== Methods for Transforming Keys and Values
7079 *
7080 * - #transform_keys: Returns a copy of +self+ with modified keys.
7081 * - #transform_keys!: Modifies keys in +self+
7082 * - #transform_values: Returns a copy of +self+ with modified values.
7083 * - #transform_values!: Modifies values in +self+.
7084 *
7085 * ==== Other Methods
7086 * - #flatten: Returns an array that is a 1-dimensional flattening of +self+.
7087 * - #invert: Returns a hash with the each key-value pair inverted.
7088 *
7089 */
7090
7091void
7092Init_Hash(void)
7093{
7094 id_hash = rb_intern_const("hash");
7095 id_flatten_bang = rb_intern_const("flatten!");
7096 id_hash_iter_lev = rb_make_internal_id();
7097
7098 rb_cHash = rb_define_class("Hash", rb_cObject);
7099
7100 rb_include_module(rb_cHash, rb_mEnumerable);
7101
7102 rb_define_alloc_func(rb_cHash, empty_hash_alloc);
7103 rb_define_singleton_method(rb_cHash, "[]", rb_hash_s_create, -1);
7104 rb_define_singleton_method(rb_cHash, "try_convert", rb_hash_s_try_convert, 1);
7105 rb_define_method(rb_cHash, "initialize", rb_hash_initialize, -1);
7106 rb_define_method(rb_cHash, "initialize_copy", rb_hash_replace, 1);
7107 rb_define_method(rb_cHash, "rehash", rb_hash_rehash, 0);
7108
7109 rb_define_method(rb_cHash, "to_hash", rb_hash_to_hash, 0);
7110 rb_define_method(rb_cHash, "to_h", rb_hash_to_h, 0);
7111 rb_define_method(rb_cHash, "to_a", rb_hash_to_a, 0);
7112 rb_define_method(rb_cHash, "inspect", rb_hash_inspect, 0);
7113 rb_define_alias(rb_cHash, "to_s", "inspect");
7114 rb_define_method(rb_cHash, "to_proc", rb_hash_to_proc, 0);
7115
7116 rb_define_method(rb_cHash, "==", rb_hash_equal, 1);
7117 rb_define_method(rb_cHash, "[]", rb_hash_aref, 1);
7118 rb_define_method(rb_cHash, "hash", rb_hash_hash, 0);
7119 rb_define_method(rb_cHash, "eql?", rb_hash_eql, 1);
7120 rb_define_method(rb_cHash, "fetch", rb_hash_fetch_m, -1);
7121 rb_define_method(rb_cHash, "[]=", rb_hash_aset, 2);
7122 rb_define_method(rb_cHash, "store", rb_hash_aset, 2);
7123 rb_define_method(rb_cHash, "default", rb_hash_default, -1);
7124 rb_define_method(rb_cHash, "default=", rb_hash_set_default, 1);
7125 rb_define_method(rb_cHash, "default_proc", rb_hash_default_proc, 0);
7126 rb_define_method(rb_cHash, "default_proc=", rb_hash_set_default_proc, 1);
7127 rb_define_method(rb_cHash, "key", rb_hash_key, 1);
7128 rb_define_method(rb_cHash, "size", rb_hash_size, 0);
7129 rb_define_method(rb_cHash, "length", rb_hash_size, 0);
7130 rb_define_method(rb_cHash, "empty?", rb_hash_empty_p, 0);
7131
7132 rb_define_method(rb_cHash, "each_value", rb_hash_each_value, 0);
7133 rb_define_method(rb_cHash, "each_key", rb_hash_each_key, 0);
7134 rb_define_method(rb_cHash, "each_pair", rb_hash_each_pair, 0);
7135 rb_define_method(rb_cHash, "each", rb_hash_each_pair, 0);
7136
7137 rb_define_method(rb_cHash, "transform_keys", rb_hash_transform_keys, -1);
7138 rb_define_method(rb_cHash, "transform_keys!", rb_hash_transform_keys_bang, -1);
7139 rb_define_method(rb_cHash, "transform_values", rb_hash_transform_values, 0);
7140 rb_define_method(rb_cHash, "transform_values!", rb_hash_transform_values_bang, 0);
7141
7142 rb_define_method(rb_cHash, "keys", rb_hash_keys, 0);
7143 rb_define_method(rb_cHash, "values", rb_hash_values, 0);
7144 rb_define_method(rb_cHash, "values_at", rb_hash_values_at, -1);
7145 rb_define_method(rb_cHash, "fetch_values", rb_hash_fetch_values, -1);
7146
7147 rb_define_method(rb_cHash, "shift", rb_hash_shift, 0);
7148 rb_define_method(rb_cHash, "delete", rb_hash_delete_m, 1);
7149 rb_define_method(rb_cHash, "delete_if", rb_hash_delete_if, 0);
7150 rb_define_method(rb_cHash, "keep_if", rb_hash_keep_if, 0);
7151 rb_define_method(rb_cHash, "select", rb_hash_select, 0);
7152 rb_define_method(rb_cHash, "select!", rb_hash_select_bang, 0);
7153 rb_define_method(rb_cHash, "filter", rb_hash_select, 0);
7154 rb_define_method(rb_cHash, "filter!", rb_hash_select_bang, 0);
7155 rb_define_method(rb_cHash, "reject", rb_hash_reject, 0);
7156 rb_define_method(rb_cHash, "reject!", rb_hash_reject_bang, 0);
7157 rb_define_method(rb_cHash, "slice", rb_hash_slice, -1);
7158 rb_define_method(rb_cHash, "except", rb_hash_except, -1);
7159 rb_define_method(rb_cHash, "clear", rb_hash_clear, 0);
7160 rb_define_method(rb_cHash, "invert", rb_hash_invert, 0);
7161 rb_define_method(rb_cHash, "update", rb_hash_update, -1);
7162 rb_define_method(rb_cHash, "replace", rb_hash_replace, 1);
7163 rb_define_method(rb_cHash, "merge!", rb_hash_update, -1);
7164 rb_define_method(rb_cHash, "merge", rb_hash_merge, -1);
7165 rb_define_method(rb_cHash, "assoc", rb_hash_assoc, 1);
7166 rb_define_method(rb_cHash, "rassoc", rb_hash_rassoc, 1);
7167 rb_define_method(rb_cHash, "flatten", rb_hash_flatten, -1);
7168 rb_define_method(rb_cHash, "compact", rb_hash_compact, 0);
7169 rb_define_method(rb_cHash, "compact!", rb_hash_compact_bang, 0);
7170
7171 rb_define_method(rb_cHash, "include?", rb_hash_has_key, 1);
7172 rb_define_method(rb_cHash, "member?", rb_hash_has_key, 1);
7173 rb_define_method(rb_cHash, "has_key?", rb_hash_has_key, 1);
7174 rb_define_method(rb_cHash, "has_value?", rb_hash_has_value, 1);
7175 rb_define_method(rb_cHash, "key?", rb_hash_has_key, 1);
7176 rb_define_method(rb_cHash, "value?", rb_hash_has_value, 1);
7177
7178 rb_define_method(rb_cHash, "compare_by_identity", rb_hash_compare_by_id, 0);
7179 rb_define_method(rb_cHash, "compare_by_identity?", rb_hash_compare_by_id_p, 0);
7180
7181 rb_define_method(rb_cHash, "any?", rb_hash_any_p, -1);
7182 rb_define_method(rb_cHash, "dig", rb_hash_dig, -1);
7183
7184 rb_define_method(rb_cHash, "<=", rb_hash_le, 1);
7185 rb_define_method(rb_cHash, "<", rb_hash_lt, 1);
7186 rb_define_method(rb_cHash, ">=", rb_hash_ge, 1);
7187 rb_define_method(rb_cHash, ">", rb_hash_gt, 1);
7188
7189 rb_define_method(rb_cHash, "deconstruct_keys", rb_hash_deconstruct_keys, 1);
7190
7191 rb_define_singleton_method(rb_cHash, "ruby2_keywords_hash?", rb_hash_s_ruby2_keywords_hash_p, 1);
7192 rb_define_singleton_method(rb_cHash, "ruby2_keywords_hash", rb_hash_s_ruby2_keywords_hash, 1);
7193
7194 /* Document-class: ENV
7195 *
7196 * \ENV is a hash-like accessor for environment variables.
7197 *
7198 * === Interaction with the Operating System
7199 *
7200 * The \ENV object interacts with the operating system's environment variables:
7201 *
7202 * - When you get the value for a name in \ENV, the value is retrieved from among the current environment variables.
7203 * - When you create or set a name-value pair in \ENV, the name and value are immediately set in the environment variables.
7204 * - When you delete a name-value pair in \ENV, it is immediately deleted from the environment variables.
7205 *
7206 * === Names and Values
7207 *
7208 * Generally, a name or value is a String.
7209 *
7210 * ==== Valid Names and Values
7211 *
7212 * Each name or value must be one of the following:
7213 *
7214 * - A String.
7215 * - An object that responds to \#to_str by returning a String, in which case that String will be used as the name or value.
7216 *
7217 * ==== Invalid Names and Values
7218 *
7219 * A new name:
7220 *
7221 * - May not be the empty string:
7222 * ENV[''] = '0'
7223 * # Raises Errno::EINVAL (Invalid argument - ruby_setenv())
7224 *
7225 * - May not contain character <code>"="</code>:
7226 * ENV['='] = '0'
7227 * # Raises Errno::EINVAL (Invalid argument - ruby_setenv(=))
7228 *
7229 * A new name or value:
7230 *
7231 * - May not be a non-String that does not respond to \#to_str:
7232 *
7233 * ENV['foo'] = Object.new
7234 * # Raises TypeError (no implicit conversion of Object into String)
7235 * ENV[Object.new] = '0'
7236 * # Raises TypeError (no implicit conversion of Object into String)
7237 *
7238 * - May not contain the NUL character <code>"\0"</code>:
7239 *
7240 * ENV['foo'] = "\0"
7241 * # Raises ArgumentError (bad environment variable value: contains null byte)
7242 * ENV["\0"] == '0'
7243 * # Raises ArgumentError (bad environment variable name: contains null byte)
7244 *
7245 * - May not have an ASCII-incompatible encoding such as UTF-16LE or ISO-2022-JP:
7246 *
7247 * ENV['foo'] = '0'.force_encoding(Encoding::ISO_2022_JP)
7248 * # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: ISO-2022-JP)
7249 * ENV["foo".force_encoding(Encoding::ISO_2022_JP)] = '0'
7250 * # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: ISO-2022-JP)
7251 *
7252 * === About Ordering
7253 *
7254 * \ENV enumerates its name/value pairs in the order found
7255 * in the operating system's environment variables.
7256 * Therefore the ordering of \ENV content is OS-dependent, and may be indeterminate.
7257 *
7258 * This will be seen in:
7259 * - A Hash returned by an \ENV method.
7260 * - An Enumerator returned by an \ENV method.
7261 * - An Array returned by ENV.keys, ENV.values, or ENV.to_a.
7262 * - The String returned by ENV.inspect.
7263 * - The Array returned by ENV.shift.
7264 * - The name returned by ENV.key.
7265 *
7266 * === About the Examples
7267 * Some methods in \ENV return \ENV itself. Typically, there are many environment variables.
7268 * It's not useful to display a large \ENV in the examples here,
7269 * so most example snippets begin by resetting the contents of \ENV:
7270 * - ENV.replace replaces \ENV with a new collection of entries.
7271 * - ENV.clear empties \ENV.
7272 *
7273 * == What's Here
7274 *
7275 * First, what's elsewhere. \Class \ENV:
7276 *
7277 * - Inherits from {class Object}[rdoc-ref:Object@What-27s+Here].
7278 * - Extends {module Enumerable}[rdoc-ref:Enumerable@What-27s+Here],
7279 *
7280 * Here, class \ENV provides methods that are useful for:
7281 *
7282 * - {Querying}[rdoc-ref:ENV@Methods+for+Querying]
7283 * - {Assigning}[rdoc-ref:ENV@Methods+for+Assigning]
7284 * - {Deleting}[rdoc-ref:ENV@Methods+for+Deleting]
7285 * - {Iterating}[rdoc-ref:ENV@Methods+for+Iterating]
7286 * - {Converting}[rdoc-ref:ENV@Methods+for+Converting]
7287 * - {And more ....}[rdoc-ref:ENV@More+Methods]
7288 *
7289 * === Methods for Querying
7290 *
7291 * - ::[]: Returns the value for the given environment variable name if it exists:
7292 * - ::empty?: Returns whether \ENV is empty.
7293 * - ::has_value?, ::value?: Returns whether the given value is in \ENV.
7294 * - ::include?, ::has_key?, ::key?, ::member?: Returns whether the given name
7295 is in \ENV.
7296 * - ::key: Returns the name of the first entry with the given value.
7297 * - ::size, ::length: Returns the number of entries.
7298 * - ::value?: Returns whether any entry has the given value.
7299 *
7300 * === Methods for Assigning
7301 *
7302 * - ::[]=, ::store: Creates, updates, or deletes the named environment variable.
7303 * - ::clear: Removes every environment variable; returns \ENV:
7304 * - ::update, ::merge!: Adds to \ENV each key/value pair in the given hash.
7305 * - ::replace: Replaces the entire content of the \ENV
7306 * with the name/value pairs in the given hash.
7307 *
7308 * === Methods for Deleting
7309 *
7310 * - ::delete: Deletes the named environment variable name if it exists.
7311 * - ::delete_if: Deletes entries selected by the block.
7312 * - ::keep_if: Deletes entries not selected by the block.
7313 * - ::reject!: Similar to #delete_if, but returns +nil+ if no change was made.
7314 * - ::select!, ::filter!: Deletes entries selected by the block.
7315 * - ::shift: Removes and returns the first entry.
7316 *
7317 * === Methods for Iterating
7318 *
7319 * - ::each, ::each_pair: Calls the block with each name/value pair.
7320 * - ::each_key: Calls the block with each name.
7321 * - ::each_value: Calls the block with each value.
7322 *
7323 * === Methods for Converting
7324 *
7325 * - ::assoc: Returns a 2-element array containing the name and value
7326 * of the named environment variable if it exists:
7327 * - ::clone: Returns \ENV (and issues a warning).
7328 * - ::except: Returns a hash of all name/value pairs except those given.
7329 * - ::fetch: Returns the value for the given name.
7330 * - ::inspect: Returns the contents of \ENV as a string.
7331 * - ::invert: Returns a hash whose keys are the \ENV values,
7332 and whose values are the corresponding \ENV names.
7333 * - ::keys: Returns an array of all names.
7334 * - ::rassoc: Returns the name and value of the first found entry
7335 * that has the given value.
7336 * - ::reject: Returns a hash of those entries not rejected by the block.
7337 * - ::select, ::filter: Returns a hash of name/value pairs selected by the block.
7338 * - ::slice: Returns a hash of the given names and their corresponding values.
7339 * - ::to_a: Returns the entries as an array of 2-element Arrays.
7340 * - ::to_h: Returns a hash of entries selected by the block.
7341 * - ::to_hash: Returns a hash of all entries.
7342 * - ::to_s: Returns the string <tt>'ENV'</tt>.
7343 * - ::values: Returns all values as an array.
7344 * - ::values_at: Returns an array of the values for the given name.
7345 *
7346 * === More Methods
7347 *
7348 * - ::dup: Raises an exception.
7349 * - ::freeze: Raises an exception.
7350 * - ::rehash: Returns +nil+, without modifying \ENV.
7351 *
7352 */
7353
7354 /*
7355 * Hack to get RDoc to regard ENV as a class:
7356 * envtbl = rb_define_class("ENV", rb_cObject);
7357 */
7358 origenviron = environ;
7359 envtbl = TypedData_Wrap_Struct(rb_cObject, &env_data_type, NULL);
7360 rb_extend_object(envtbl, rb_mEnumerable);
7361 FL_SET_RAW(envtbl, RUBY_FL_SHAREABLE);
7362
7363
7364 rb_define_singleton_method(envtbl, "[]", rb_f_getenv, 1);
7365 rb_define_singleton_method(envtbl, "fetch", env_fetch, -1);
7366 rb_define_singleton_method(envtbl, "[]=", env_aset_m, 2);
7367 rb_define_singleton_method(envtbl, "store", env_aset_m, 2);
7368 rb_define_singleton_method(envtbl, "each", env_each_pair, 0);
7369 rb_define_singleton_method(envtbl, "each_pair", env_each_pair, 0);
7370 rb_define_singleton_method(envtbl, "each_key", env_each_key, 0);
7371 rb_define_singleton_method(envtbl, "each_value", env_each_value, 0);
7372 rb_define_singleton_method(envtbl, "delete", env_delete_m, 1);
7373 rb_define_singleton_method(envtbl, "delete_if", env_delete_if, 0);
7374 rb_define_singleton_method(envtbl, "keep_if", env_keep_if, 0);
7375 rb_define_singleton_method(envtbl, "slice", env_slice, -1);
7376 rb_define_singleton_method(envtbl, "except", env_except, -1);
7377 rb_define_singleton_method(envtbl, "clear", env_clear, 0);
7378 rb_define_singleton_method(envtbl, "reject", env_reject, 0);
7379 rb_define_singleton_method(envtbl, "reject!", env_reject_bang, 0);
7380 rb_define_singleton_method(envtbl, "select", env_select, 0);
7381 rb_define_singleton_method(envtbl, "select!", env_select_bang, 0);
7382 rb_define_singleton_method(envtbl, "filter", env_select, 0);
7383 rb_define_singleton_method(envtbl, "filter!", env_select_bang, 0);
7384 rb_define_singleton_method(envtbl, "shift", env_shift, 0);
7385 rb_define_singleton_method(envtbl, "freeze", env_freeze, 0);
7386 rb_define_singleton_method(envtbl, "invert", env_invert, 0);
7387 rb_define_singleton_method(envtbl, "replace", env_replace, 1);
7388 rb_define_singleton_method(envtbl, "update", env_update, -1);
7389 rb_define_singleton_method(envtbl, "merge!", env_update, -1);
7390 rb_define_singleton_method(envtbl, "inspect", env_inspect, 0);
7391 rb_define_singleton_method(envtbl, "rehash", env_none, 0);
7392 rb_define_singleton_method(envtbl, "to_a", env_to_a, 0);
7393 rb_define_singleton_method(envtbl, "to_s", env_to_s, 0);
7394 rb_define_singleton_method(envtbl, "key", env_key, 1);
7395 rb_define_singleton_method(envtbl, "size", env_size, 0);
7396 rb_define_singleton_method(envtbl, "length", env_size, 0);
7397 rb_define_singleton_method(envtbl, "empty?", env_empty_p, 0);
7398 rb_define_singleton_method(envtbl, "keys", env_f_keys, 0);
7399 rb_define_singleton_method(envtbl, "values", env_f_values, 0);
7400 rb_define_singleton_method(envtbl, "values_at", env_values_at, -1);
7401 rb_define_singleton_method(envtbl, "include?", env_has_key, 1);
7402 rb_define_singleton_method(envtbl, "member?", env_has_key, 1);
7403 rb_define_singleton_method(envtbl, "has_key?", env_has_key, 1);
7404 rb_define_singleton_method(envtbl, "has_value?", env_has_value, 1);
7405 rb_define_singleton_method(envtbl, "key?", env_has_key, 1);
7406 rb_define_singleton_method(envtbl, "value?", env_has_value, 1);
7407 rb_define_singleton_method(envtbl, "to_hash", env_f_to_hash, 0);
7408 rb_define_singleton_method(envtbl, "to_h", env_to_h, 0);
7409 rb_define_singleton_method(envtbl, "assoc", env_assoc, 1);
7410 rb_define_singleton_method(envtbl, "rassoc", env_rassoc, 1);
7411 rb_define_singleton_method(envtbl, "clone", env_clone, -1);
7412 rb_define_singleton_method(envtbl, "dup", env_dup, 0);
7413
7414 VALUE envtbl_class = rb_singleton_class(envtbl);
7415 rb_undef_method(envtbl_class, "initialize");
7416 rb_undef_method(envtbl_class, "initialize_clone");
7417 rb_undef_method(envtbl_class, "initialize_copy");
7418 rb_undef_method(envtbl_class, "initialize_dup");
7419
7420 /*
7421 * \ENV is a Hash-like accessor for environment variables.
7422 *
7423 * See ENV (the class) for more details.
7424 */
7425 rb_define_global_const("ENV", envtbl);
7426
7427 /* for callcc */
7428 ruby_register_rollback_func_for_ensure(hash_foreach_ensure, hash_foreach_ensure_rollback);
7429
7430 HASH_ASSERT(sizeof(ar_hint_t) * RHASH_AR_TABLE_MAX_SIZE == sizeof(VALUE));
7431}
RUBY_ASSERT
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:177
rb_define_method
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition cxxanyargs.hpp:670
rb_define_singleton_method
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
Definition cxxanyargs.hpp:685
RB_FL_ANY_RAW
static bool RB_FL_ANY_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_ANY().
Definition fl_type.h:518
RUBY_FL_SHAREABLE
@ RUBY_FL_SHAREABLE
This flag has something to do with Ractor.
Definition fl_type.h:266
rb_include_module
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition class.c:1177
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:970
rb_extend_object
void rb_extend_object(VALUE obj, VALUE module)
Extend the object with the module.
Definition eval.c:1713
rb_singleton_class
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:2284
rb_define_alias
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition class.c:2332
rb_undef_method
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2156
rb_scan_args
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.
Definition class.c:2622
rb_block_given_p
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:866
rb_str_new2
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1675
TYPE
#define TYPE(_)
Old name of rb_type.
Definition value_type.h:107
NEWOBJ_OF
#define NEWOBJ_OF
Old name of RB_NEWOBJ_OF.
Definition newobj.h:61
rb_str_buf_cat2
#define rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
Definition string.h:1682
FL_EXIVAR
#define FL_EXIVAR
Old name of RUBY_FL_EXIVAR.
Definition fl_type.h:66
NUM2LL
#define NUM2LL
Old name of RB_NUM2LL.
Definition long_long.h:34
REALLOC_N
#define REALLOC_N
Old name of RB_REALLOC_N.
Definition memory.h:397
T_STRING
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
xfree
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
Qundef
#define Qundef
Old name of RUBY_Qundef.
Definition special_consts.h:62
INT2FIX
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
T_NIL
#define T_NIL
Old name of RUBY_T_NIL.
Definition value_type.h:72
T_FLOAT
#define T_FLOAT
Old name of RUBY_T_FLOAT.
Definition value_type.h:64
T_BIGNUM
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition value_type.h:57
rb_str_buf_new2
#define rb_str_buf_new2
Old name of rb_str_buf_new_cstr.
Definition string.h:1679
T_FIXNUM
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
Definition value_type.h:63
UNREACHABLE_RETURN
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
T_DATA
#define T_DATA
Old name of RUBY_T_DATA.
Definition value_type.h:60
LONG2FIX
#define LONG2FIX
Old name of RB_INT2FIX.
Definition long.h:49
STATIC_SYM_P
#define STATIC_SYM_P
Old name of RB_STATIC_SYM_P.
Definition special_consts.h:57
T_TRUE
#define T_TRUE
Old name of RUBY_T_TRUE.
Definition value_type.h:81
T_HASH
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
ALLOC_N
#define ALLOC_N
Old name of RB_ALLOC_N.
Definition memory.h:393
FL_TEST_RAW
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:132
rb_usascii_str_new2
#define rb_usascii_str_new2
Old name of rb_usascii_str_new_cstr.
Definition string.h:1680
T_FALSE
#define T_FALSE
Old name of RUBY_T_FALSE.
Definition value_type.h:61
FIXNUM_MIN
#define FIXNUM_MIN
Old name of RUBY_FIXNUM_MIN.
Definition fixnum.h:27
FLONUM_P
#define FLONUM_P
Old name of RB_FLONUM_P.
Definition special_consts.h:67
Qtrue
#define Qtrue
Old name of RUBY_Qtrue.
Definition special_consts.h:61
ST2FIX
#define ST2FIX
Old name of RB_ST2FIX.
Definition st_data_t.h:33
FIXNUM_MAX
#define FIXNUM_MAX
Old name of RUBY_FIXNUM_MAX.
Definition fixnum.h:26
NUM2INT
#define NUM2INT
Old name of RB_NUM2INT.
Definition int.h:44
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
Qfalse
#define Qfalse
Old name of RUBY_Qfalse.
Definition special_consts.h:59
FIX2LONG
#define FIX2LONG
Old name of RB_FIX2LONG.
Definition long.h:46
NIL_P
#define NIL_P
Old name of RB_NIL_P.
Definition special_consts.h:55
ALLOCV_N
#define ALLOCV_N
Old name of RB_ALLOCV_N.
Definition memory.h:399
FL_WB_PROTECTED
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
Definition fl_type.h:59
POSFIXABLE
#define POSFIXABLE
Old name of RB_POSFIXABLE.
Definition fixnum.h:29
T_SYMBOL
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
Definition value_type.h:80
FL_TEST
#define FL_TEST
Old name of RB_FL_TEST.
Definition fl_type.h:131
NUM2LONG
#define NUM2LONG
Old name of RB_NUM2LONG.
Definition long.h:51
FIXNUM_P
#define FIXNUM_P
Old name of RB_FIXNUM_P.
Definition special_consts.h:53
OBJ_WB_UNPROTECT
#define OBJ_WB_UNPROTECT
Old name of RB_OBJ_WB_UNPROTECT.
Definition gc.h:637
rb_ary_new2
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:651
FL_SET_RAW
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:130
ALLOCV_END
#define ALLOCV_END
Old name of RB_ALLOCV_END.
Definition memory.h:400
SYMBOL_P
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
rb_syserr_fail_str
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.
Definition error.c:3573
rb_eTypeError
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1344
rb_eRuntimeError
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1342
rb_warn
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:423
rb_mKernel
VALUE rb_mKernel
Kernel module.
Definition object.c:63
rb_any_to_s
VALUE rb_any_to_s(VALUE obj)
Generates a textual representation of the given object.
Definition object.c:634
rb_mEnumerable
VALUE rb_mEnumerable
Enumerable module.
Definition enum.c:27
rb_eql
int rb_eql(VALUE lhs, VALUE rhs)
Checks for equality of the passed objects, in terms of Object#eql?.
Definition object.c:160
rb_cHash
VALUE rb_cHash
Hash class.
Definition hash.c:110
rb_obj_class
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:215
rb_inspect
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
Definition object.c:645
rb_equal
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
Definition object.c:147
rb_cString
VALUE rb_cString
String class.
Definition string.c:78
rb_to_int
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
Definition object.c:3145
RB_OBJ_WRITTEN
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition gc.h:631
RB_OBJ_WRITE
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:619
rb_external_str_new_with_enc
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.
Definition string.c:1155
rb_funcall
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1121
RGENGC_WB_PROTECTED_HASH
#define RGENGC_WB_PROTECTED_HASH
This is a compile-time flag to enable/disable write barrier for struct RHash.
Definition gc.h:473
INTEGER_PACK_NATIVE_BYTE_ORDER
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Means either INTEGER_PACK_MSBYTE_FIRST or INTEGER_PACK_LSBYTE_FIRST, depending on the host processor'...
Definition bignum.h:546
RETURN_SIZED_ENUMERATOR
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
This roughly resembles return enum_for(__callee__) unless block_given?.
Definition enumerator.h:206
UNLIMITED_ARGUMENTS
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
Definition error.h:35
rb_check_frozen
#define rb_check_frozen
Just another name of rb_check_frozen.
Definition error.h:264
rb_check_arity
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:280
rb_hash_update_func
VALUE rb_hash_update_func(VALUE newkey, VALUE oldkey, VALUE value)
Type of callback functions to pass to rb_hash_update_by().
Definition hash.h:269
st_foreach_safe
#define st_foreach_safe
Just another name of rb_st_foreach_safe.
Definition hash.h:51
rb_proc_lambda_p
VALUE rb_proc_lambda_p(VALUE recv)
Queries if the given object is a lambda.
Definition proc.c:244
rb_block_proc
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition proc.c:808
rb_proc_call_with_block
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,...
Definition proc.c:990
rb_proc_arity
int rb_proc_arity(VALUE recv)
Queries the number of mandatory arguments of the given Proc.
Definition proc.c:1097
rb_obj_is_proc
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition proc.c:119
rb_hash_uint
#define rb_hash_uint(h, i)
Just another name of st_hash_uint.
Definition string.h:942
rb_hash_end
#define rb_hash_end(h)
Just another name of st_hash_end.
Definition string.h:945
rb_str_hash_cmp
int rb_str_hash_cmp(VALUE str1, VALUE str2)
Compares two strings.
Definition string.c:3629
rb_str_ellipsize
VALUE rb_str_ellipsize(VALUE str, long len)
Shortens str and adds three dots, an ellipsis, if it is longer than len characters.
Definition string.c:11013
rb_memhash
st_index_t rb_memhash(const void *ptr, long len)
This is a universal hash function.
Definition random.c:1747
rb_str_new
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1498
rb_str_buf_cat
#define rb_str_buf_cat
Just another name of rb_str_cat.
Definition string.h:1681
rb_str_hash
st_index_t rb_str_hash(VALUE str)
Calculates a hash value of a string.
Definition string.c:3618
rb_hash_start
st_index_t rb_hash_start(st_index_t i)
Starts a series of hashing.
Definition random.c:1741
rb_str_buf_cat_ascii
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 ...
Definition string.c:3351
rb_check_string_type
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
Definition string.c:2681
rb_utf8_str_new
#define rb_utf8_str_new(str, len)
Identical to rb_str_new, except it generates a string of "UTF-8" encoding.
Definition string.h:1549
rb_exec_recursive
VALUE rb_exec_recursive(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
"Recursion" API entry point.
Definition thread.c:5266
rb_exec_recursive_paired
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,...
Definition thread.c:5277
rb_ivar_get
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.
Definition variable.c:1340
rb_respond_to
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
Definition vm_method.c:2937
rb_define_alloc_func
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
rb_intern_const
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:276
rb_define_global_const
void rb_define_global_const(const char *name, VALUE val)
Identical to rb_define_const(), except it defines that of "global", i.e.
Definition variable.c:3702
capa
int capa
Designed capacity of the buffer.
Definition io.h:11
len
int len
Length of the buffer.
Definition io.h:8
ractor.h
util.h
ruby_strdup
char * ruby_strdup(const char *str)
This is our own version of strdup(3) that uses ruby_xmalloc() instead of system malloc (benefits our ...
Definition util.c:535
RB_BLOCK_CALL_FUNC_ARGLIST
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
Definition iterator.h:58
rb_yield_values
VALUE rb_yield_values(int n,...)
Identical to rb_yield(), except it takes variadic number of parameters and pass them to the block.
Definition vm_eval.c:1388
rb_yield_values2
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...
Definition vm_eval.c:1410
rb_yield
VALUE rb_yield(VALUE val)
Yields the block.
Definition vm_eval.c:1376
RB_GC_GUARD
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:161
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition cxxanyargs.hpp:56
ruby::backward::cxxanyargs::rb_ensure
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
Definition cxxanyargs.hpp:310
rb_copy_generic_ivar
void rb_copy_generic_ivar(VALUE clone, VALUE obj)
Copies the list of instance variables.
Definition variable.c:2031
RARRAY_LEN
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
RARRAY_PTR_USE
#define RARRAY_PTR_USE(ary, ptr_name, expr)
Declares a section of code where raw pointers are used.
Definition rarray.h:348
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition rarray.h:403
RBASIC_CLASS
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition rbasic.h:152
RBASIC
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
RHASH_SET_IFNONE
#define RHASH_SET_IFNONE(h, ifnone)
Destructively updates the default value of the hash.
Definition rhash.h:92
RHASH_IFNONE
#define RHASH_IFNONE(h)
Definition rhash.h:59
RHASH_SIZE
#define RHASH_SIZE(h)
Queries the size of the hash.
Definition rhash.h:69
RHASH_EMPTY_P
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
Definition rhash.h:79
SafeStringValue
#define SafeStringValue(v)
Definition rstring.h:98
RSTRING_GETMEM
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
Definition rstring.h:488
TypedData_Wrap_Struct
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
Definition rtypeddata.h:449
rb_obj_classname
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
Definition variable.c:417
RUBY_SPECIAL_SHIFT
@ RUBY_SPECIAL_SHIFT
Least significant 8 bits are reserved.
Definition special_consts.h:119
RTEST
#define RTEST
This is an old name of RB_TEST.
Definition special_consts.h:51
_
#define _(args)
This was a transition path from K&R to ANSI.
Definition stdarg.h:35
__sFILE
Definition vsnprintf.c:160
OnigEncodingTypeST
Definition onigmo.h:160
RBasic::flags
VALUE flags
Per-object flags.
Definition rbasic.h:77
RHash
Definition hash.h:53
ar_table_pair_struct
Definition hash.h:39
ar_table_struct
Definition hash.h:44
assoc_arg
Definition hash.c:4070
equal_data
Definition hash.c:3659
foreach_safe_arg
Definition hash.c:1187
functor
Definition hash.c:855
hash_foreach_arg
Definition hash.c:1222
rb_data_type_struct
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:200
rehash_arg
Definition hash.c:1951
shift_var
Definition hash.c:2428
st_hash_type
Definition st.h:61
st_table
Definition st.h:79
transform_keys_args
Definition hash.c:3125
update_arg
Definition hash.c:1618
update_callback_arg
Definition hash.c:1599
update_func_arg
Definition hash.c:3958
thread_native.h
SIGNED_VALUE
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
Definition value.h:63
ID
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
Check_Type
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
Definition value_type.h:432