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