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