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