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