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