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