Ruby: hash.c Source File

Ruby 4.0.5p0 (2026-05-20 revision 64336ffd0ee9e1f4c05891695a3d7b49cb709721)

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