Ruby  2.4.2p198(2017-09-14revision59899)
hash.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  hash.c -
4 
5  $Author: naruse $
6  created at: Mon Nov 22 18:51:18 JST 1993
7 
8  Copyright (C) 1993-2007 Yukihiro Matsumoto
9  Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10  Copyright (C) 2000 Information-technology Promotion Agency, Japan
11 
12 **********************************************************************/
13 
14 #include "internal.h"
15 #include "ruby/st.h"
16 #include "ruby/util.h"
17 #include <errno.h>
18 #include "probes.h"
19 #include "id.h"
20 #include "symbol.h"
21 
22 #ifdef __APPLE__
23 # ifdef HAVE_CRT_EXTERNS_H
24 # include <crt_externs.h>
25 # else
26 # include "missing/crt_externs.h"
27 # endif
28 #endif
29 
30 #define HAS_EXTRA_STATES(hash, klass) ( \
31  ((klass = has_extra_methods(rb_obj_class(hash))) != 0) || \
32  FL_TEST((hash), FL_EXIVAR|FL_TAINT|HASH_PROC_DEFAULT) || \
33  !NIL_P(RHASH_IFNONE(hash)))
34 
35 #define SET_DEFAULT(hash, ifnone) ( \
36  FL_UNSET_RAW(hash, HASH_PROC_DEFAULT), \
37  RHASH_SET_IFNONE(hash, ifnone))
38 
39 #define SET_PROC_DEFAULT(hash, proc) set_proc_default(hash, proc)
40 
41 #define COPY_DEFAULT(hash, hash2) copy_default(RHASH(hash), RHASH(hash2))
42 
43 static inline void
44 copy_default(struct RHash *hash, const struct RHash *hash2)
45 {
46  hash->basic.flags &= ~HASH_PROC_DEFAULT;
47  hash->basic.flags |= hash2->basic.flags & HASH_PROC_DEFAULT;
48  RHASH_SET_IFNONE(hash, RHASH_IFNONE(hash2));
49 }
50 
51 static VALUE
53 {
54  const VALUE base = rb_cHash;
55  VALUE c = klass;
56  while (c != base) {
57  if (rb_class_has_methods(c)) return klass;
58  c = RCLASS_SUPER(c);
59  }
60  return 0;
61 }
62 
64 
65 /*
66  * Hash WB strategy:
67  * 1. Check mutate st_* functions
68  * * st_insert()
69  * * st_insert2()
70  * * st_update()
71  * * st_add_direct()
72  * 2. Insert WBs
73  */
74 
75 VALUE
77 {
78  return rb_obj_freeze(hash);
79 }
80 
82 
83 static VALUE envtbl;
85 
86 VALUE
88 {
89  return RHASH_IFNONE(h);
90 }
91 
92 VALUE
94 {
95  RB_OBJ_WRITE(hash, (&RHASH(hash)->ifnone), ifnone);
96  return hash;
97 }
98 
99 static int
101 {
102  if (a == b) return 0;
103  if (FIXNUM_P(a) && FIXNUM_P(b)) {
104  return a != b;
105  }
106  if (RB_TYPE_P(a, T_STRING) && RBASIC(a)->klass == rb_cString &&
107  RB_TYPE_P(b, T_STRING) && RBASIC(b)->klass == rb_cString) {
108  return rb_str_hash_cmp(a, b);
109  }
110  if (a == Qundef || b == Qundef) return -1;
111  if (SYMBOL_P(a) && SYMBOL_P(b)) {
112  return a != b;
113  }
114 
115  return !rb_eql(a, b);
116 }
117 
118 static VALUE
119 hash_recursive(VALUE obj, VALUE arg, int recurse)
120 {
121  if (recurse) return INT2FIX(0);
122  return rb_funcallv(obj, id_hash, 0, 0);
123 }
124 
125 VALUE
127 {
129 
130  while (!FIXNUM_P(hval)) {
131  if (RB_TYPE_P(hval, T_BIGNUM)) {
132  int sign;
133  unsigned long ul;
134  sign = rb_integer_pack(hval, &ul, 1, sizeof(ul), 0,
136  ul &= (1UL << (sizeof(long)*CHAR_BIT-1)) - 1;
137  if (sign < 0)
138  return LONG2FIX(-(long)ul);
139  return LONG2FIX((long)ul);
140  }
141  hval = rb_to_int(hval);
142  }
143  return hval;
144 }
145 
146 long rb_objid_hash(st_index_t index);
147 
148 long
150 {
151  /* normalize -0.0 to 0.0 */
152  if (d == 0.0) d = 0.0;
153 #if SIZEOF_INT == SIZEOF_VOIDP
154  return rb_memhash(&d, sizeof(d));
155 #else
156  {
157  union {double d; uint64_t i;} u;
158 
159  u.d = d;
160  return rb_objid_hash(rb_hash_start(u.i));
161  }
162 #endif
163 }
164 
165 static inline long
166 any_hash(VALUE a, st_index_t (*other_func)(VALUE))
167 {
168  VALUE hval;
169  st_index_t hnum;
170 
171  if (SPECIAL_CONST_P(a)) {
172  if (STATIC_SYM_P(a)) {
173  hnum = a >> (RUBY_SPECIAL_SHIFT + ID_SCOPE_SHIFT);
174  hnum = rb_hash_start(hnum);
175  goto out;
176  }
177  else if (FLONUM_P(a)) {
178  /* prevent pathological behavior: [Bug #10761] */
179  goto flt;
180  }
181  hnum = rb_objid_hash((st_index_t)a);
182  }
183  else if (BUILTIN_TYPE(a) == T_STRING) {
184  hnum = rb_str_hash(a);
185  }
186  else if (BUILTIN_TYPE(a) == T_SYMBOL) {
187  hnum = RSYMBOL(a)->hashval;
188  }
189  else if (BUILTIN_TYPE(a) == T_BIGNUM) {
190  hval = rb_big_hash(a);
191  hnum = FIX2LONG(hval);
192  }
193  else if (BUILTIN_TYPE(a) == T_FLOAT) {
194  flt:
195  hnum = rb_dbl_long_hash(rb_float_value(a));
196  }
197  else {
198  hnum = other_func(a);
199  }
200  out:
201  hnum <<= 1;
202  return (long)RSHIFT(hnum, 1);
203 }
204 
205 static st_index_t
207 {
208  obj = rb_hash(obj);
209  return FIX2LONG(obj);
210 }
211 
212 static st_index_t
214 {
215  return any_hash(a, obj_any_hash);
216 }
217 
218 /* Here is a hash function for 64-bit key. It is about 5 times faster
219  (2 times faster when uint128 type is absent) on Haswell than
220  tailored Spooky or City hash function can be. */
221 
222 /* Here we two primes with random bit generation. */
223 static const uint64_t prime1 = ((uint64_t)0x2e0bb864 << 32) | 0xe9ea7df5;
224 static const uint64_t prime2 = ((uint64_t)0xcdb32970 << 32) | 0x830fcaa1;
225 
226 
227 static inline uint64_t
229 {
230 #if defined(__GNUC__) && UINT_MAX != ULONG_MAX
231  __uint128_t r = (__uint128_t) m1 * (__uint128_t) m2;
232  return (uint64_t) (r >> 64) ^ (uint64_t) r;
233 #else
234  uint64_t hm1 = m1 >> 32, hm2 = m2 >> 32;
235  uint64_t lm1 = m1, lm2 = m2;
236  uint64_t v64_128 = hm1 * hm2;
237  uint64_t v32_96 = hm1 * lm2 + lm1 * hm2;
238  uint64_t v1_32 = lm1 * lm2;
239 
240  return (v64_128 + (v32_96 >> 32)) ^ ((v32_96 << 32) + v1_32);
241 #endif
242 }
243 
244 static inline uint64_t
246 {
247  return mult_and_mix(key + seed, prime1);
248 }
249 
250 long
252 {
253  return (long)key64_hash(rb_hash_start(index), (uint32_t)prime2);
254 }
255 
256 static st_index_t
258 {
259  return rb_objid_hash((st_index_t)obj);
260 }
261 
262 VALUE
264 {
265  long hnum = any_hash(obj, objid_hash);
266  return ST2FIX(hnum);
267 }
268 
269 int
271 {
272  return RHASH_ITER_LEV(h);
273 }
274 
275 static const struct st_hash_type objhash = {
276  rb_any_cmp,
277  rb_any_hash,
278 };
279 
280 #define rb_ident_cmp st_numcmp
281 
282 static st_index_t
284 {
285 #ifdef USE_FLONUM /* RUBY */
286  /*
287  * - flonum (on 64-bit) is pathologically bad, mix the actual
288  * float value in, but do not use the float value as-is since
289  * many integers get interpreted as 2.0 or -2.0 [Bug #10761]
290  */
291  if (FLONUM_P(n)) {
292  n ^= (st_data_t)rb_float_value(n);
293  }
294 #endif
295 
296  return (st_index_t)key64_hash(rb_hash_start((st_index_t)n), (uint32_t)prime2);
297 }
298 
299 static const struct st_hash_type identhash = {
300  rb_ident_cmp,
302 };
303 
305 
310 };
311 
312 static int
314 {
315  int status;
316  struct foreach_safe_arg *arg = (void *)args;
317 
318  if (error) return ST_STOP;
319  status = (*arg->func)(key, value, arg->arg);
320  if (status == ST_CONTINUE) {
321  return ST_CHECK;
322  }
323  return status;
324 }
325 
326 void
328 {
329  struct foreach_safe_arg arg;
330 
331  arg.tbl = table;
332  arg.func = (st_foreach_func *)func;
333  arg.arg = a;
334  if (st_foreach_check(table, foreach_safe_i, (st_data_t)&arg, 0)) {
335  rb_raise(rb_eRuntimeError, "hash modified during iteration");
336  }
337 }
338 
340 
345 };
346 
347 static int
349 {
350  struct hash_foreach_arg *arg = (struct hash_foreach_arg *)argp;
351  int status;
352  st_table *tbl;
353 
354  if (error) return ST_STOP;
355  tbl = RHASH(arg->hash)->ntbl;
356  status = (*arg->func)((VALUE)key, (VALUE)value, arg->arg);
357  if (RHASH(arg->hash)->ntbl != tbl) {
358  rb_raise(rb_eRuntimeError, "rehash occurred during iteration");
359  }
360  switch (status) {
361  case ST_DELETE:
362  FL_SET(arg->hash, HASH_DELETED);
363  return ST_DELETE;
364  case ST_CONTINUE:
365  break;
366  case ST_STOP:
367  return ST_STOP;
368  }
369  return ST_CHECK;
370 }
371 
372 static VALUE
374 {
375  RHASH_ITER_LEV(hash)++;
376  return 0;
377 }
378 
379 static VALUE
381 {
382  if (--RHASH_ITER_LEV(hash) == 0) {
383  if (FL_TEST(hash, HASH_DELETED)) {
384  st_cleanup_safe(RHASH(hash)->ntbl, (st_data_t)Qundef);
385  FL_UNSET(hash, HASH_DELETED);
386  }
387  }
388  return 0;
389 }
390 
391 static VALUE
393 {
394  VALUE hash = ((struct hash_foreach_arg *)arg)->hash;
396  rb_raise(rb_eRuntimeError, "hash modified during iteration");
397  }
398  return Qnil;
399 }
400 
401 void
403 {
404  struct hash_foreach_arg arg;
405 
406  if (!RHASH(hash)->ntbl)
407  return;
408  RHASH_ITER_LEV(hash)++;
409  arg.hash = hash;
410  arg.func = (rb_foreach_func *)func;
411  arg.arg = farg;
413 }
414 
415 static VALUE
416 hash_alloc_flags(VALUE klass, VALUE flags, VALUE ifnone)
417 {
419  NEWOBJ_OF(hash, struct RHash, klass, T_HASH | wb | flags);
420 
421  RHASH_SET_IFNONE((VALUE)hash, ifnone);
422 
423  return (VALUE)hash;
424 }
425 
426 static VALUE
428 {
429  return hash_alloc_flags(klass, 0, Qnil);
430 }
431 
432 static VALUE
434 {
435  RUBY_DTRACE_CREATE_HOOK(HASH, 0);
436 
437  return hash_alloc(klass);
438 }
439 
440 VALUE
442 {
443  return hash_alloc(rb_cHash);
444 }
445 
446 static VALUE
447 hash_dup(VALUE hash, VALUE klass, VALUE flags)
448 {
449  VALUE ret = hash_alloc_flags(klass, flags,
450  RHASH_IFNONE(hash));
451  if (!RHASH_EMPTY_P(hash))
452  RHASH(ret)->ntbl = st_copy(RHASH(hash)->ntbl);
453  return ret;
454 }
455 
456 VALUE
458 {
459  const VALUE flags = RBASIC(hash)->flags;
460  VALUE ret = hash_dup(hash, rb_obj_class(hash),
462  if (flags & FL_EXIVAR)
463  rb_copy_generic_ivar(ret, hash);
464  return ret;
465 }
466 
467 static void
469 {
470  rb_check_frozen(hash);
471 }
472 
473 static struct st_table *
475 {
476  if (!RHASH(hash)->ntbl) {
477  RHASH(hash)->ntbl = st_init_table(&objhash);
478  }
479  return RHASH(hash)->ntbl;
480 }
481 
482 struct st_table *
484 {
485  OBJ_WB_UNPROTECT(hash);
486  return hash_tbl(hash);
487 }
488 
489 struct st_table *
491 {
492  return hash_tbl(hash);
493 }
494 
495 static void
497 {
498  rb_hash_modify_check(hash);
499  hash_tbl(hash);
500 }
501 
502 NORETURN(static void no_new_key(void));
503 static void
505 {
506  rb_raise(rb_eRuntimeError, "can't add a new key into hash during iteration");
507 }
508 
512 };
513 
514 #define NOINSERT_UPDATE_CALLBACK(func) \
515 static int \
516 func##_noinsert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
517 { \
518  if (!existing) no_new_key(); \
519  return func(key, val, (struct update_arg *)arg, existing); \
520 } \
521  \
522 static int \
523 func##_insert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
524 { \
525  return func(key, val, (struct update_arg *)arg, existing); \
526 }
527 
528 struct update_arg {
535 };
536 
537 typedef int (*tbl_update_func)(st_data_t *, st_data_t *, st_data_t, int);
538 
539 static int
540 tbl_update(VALUE hash, VALUE key, tbl_update_func func, st_data_t optional_arg)
541 {
542  struct update_arg arg;
543  int result;
544 
545  arg.arg = optional_arg;
546  arg.hash = hash;
547  arg.new_key = 0;
548  arg.old_key = Qundef;
549  arg.new_value = 0;
550  arg.old_value = Qundef;
551 
552  result = st_update(RHASH(hash)->ntbl, (st_data_t)key, func, (st_data_t)&arg);
553 
554  /* write barrier */
555  if (arg.new_key) RB_OBJ_WRITTEN(hash, arg.old_key, arg.new_key);
556  if (arg.new_value) RB_OBJ_WRITTEN(hash, arg.old_value, arg.new_value);
557 
558  return result;
559 }
560 
561 #define UPDATE_CALLBACK(iter_lev, func) ((iter_lev) > 0 ? func##_noinsert : func##_insert)
562 
563 #define RHASH_UPDATE_ITER(h, iter_lev, key, func, a) do { \
564  tbl_update((h), (key), UPDATE_CALLBACK((iter_lev), func), (st_data_t)(a)); \
565 } while (0)
566 
567 #define RHASH_UPDATE(hash, key, func, arg) \
568  RHASH_UPDATE_ITER(hash, RHASH_ITER_LEV(hash), key, func, arg)
569 
570 static void
572 {
573  if (rb_proc_lambda_p(proc)) {
574  int n = rb_proc_arity(proc);
575 
576  if (n != 2 && (n >= 0 || n < -3)) {
577  if (n < 0) n = -n-1;
578  rb_raise(rb_eTypeError, "default_proc takes two arguments (2 for %d)", n);
579  }
580  }
581 
583  RHASH_SET_IFNONE(hash, proc);
584 }
585 
586 /*
587  * call-seq:
588  * Hash.new -> new_hash
589  * Hash.new(obj) -> new_hash
590  * Hash.new {|hash, key| block } -> new_hash
591  *
592  * Returns a new, empty hash. If this hash is subsequently accessed by
593  * a key that doesn't correspond to a hash entry, the value returned
594  * depends on the style of <code>new</code> used to create the hash. In
595  * the first form, the access returns <code>nil</code>. If
596  * <i>obj</i> is specified, this single object will be used for
597  * all <em>default values</em>. If a block is specified, it will be
598  * called with the hash object and the key, and should return the
599  * default value. It is the block's responsibility to store the value
600  * in the hash if required.
601  *
602  * h = Hash.new("Go Fish")
603  * h["a"] = 100
604  * h["b"] = 200
605  * h["a"] #=> 100
606  * h["c"] #=> "Go Fish"
607  * # The following alters the single default object
608  * h["c"].upcase! #=> "GO FISH"
609  * h["d"] #=> "GO FISH"
610  * h.keys #=> ["a", "b"]
611  *
612  * # While this creates a new default object each time
613  * h = Hash.new { |hash, key| hash[key] = "Go Fish: #{key}" }
614  * h["c"] #=> "Go Fish: c"
615  * h["c"].upcase! #=> "GO FISH: C"
616  * h["d"] #=> "Go Fish: d"
617  * h.keys #=> ["c", "d"]
618  *
619  */
620 
621 static VALUE
623 {
624  VALUE ifnone;
625 
626  rb_hash_modify(hash);
627  if (rb_block_given_p()) {
628  rb_check_arity(argc, 0, 0);
629  ifnone = rb_block_proc();
630  SET_PROC_DEFAULT(hash, ifnone);
631  }
632  else {
633  rb_check_arity(argc, 0, 1);
634  ifnone = argc == 0 ? Qnil : argv[0];
635  RHASH_SET_IFNONE(hash, ifnone);
636  }
637 
638  return hash;
639 }
640 
641 /*
642  * call-seq:
643  * Hash[ key, value, ... ] -> new_hash
644  * Hash[ [ [key, value], ... ] ] -> new_hash
645  * Hash[ object ] -> new_hash
646  *
647  * Creates a new hash populated with the given objects.
648  *
649  * Similar to the literal <code>{ _key_ => _value_, ... }</code>. In the first
650  * form, keys and values occur in pairs, so there must be an even number of
651  * arguments.
652  *
653  * The second and third form take a single argument which is either an array
654  * of key-value pairs or an object convertible to a hash.
655  *
656  * Hash["a", 100, "b", 200] #=> {"a"=>100, "b"=>200}
657  * Hash[ [ ["a", 100], ["b", 200] ] ] #=> {"a"=>100, "b"=>200}
658  * Hash["a" => 100, "b" => 200] #=> {"a"=>100, "b"=>200}
659  */
660 
661 static VALUE
663 {
664  VALUE hash, tmp;
665  int i;
666 
667  if (argc == 1) {
668  tmp = rb_hash_s_try_convert(Qnil, argv[0]);
669  if (!NIL_P(tmp)) {
670  hash = hash_alloc(klass);
671  if (RHASH(tmp)->ntbl) {
672  RHASH(hash)->ntbl = st_copy(RHASH(tmp)->ntbl);
673  }
674  return hash;
675  }
676 
677  tmp = rb_check_array_type(argv[0]);
678  if (!NIL_P(tmp)) {
679  long i;
680 
681  hash = hash_alloc(klass);
682  for (i = 0; i < RARRAY_LEN(tmp); ++i) {
683  VALUE e = RARRAY_AREF(tmp, i);
684  VALUE v = rb_check_array_type(e);
685  VALUE key, val = Qnil;
686 
687  if (NIL_P(v)) {
688 #if 0 /* refix in the next release */
689  rb_raise(rb_eArgError, "wrong element type %s at %ld (expected array)",
690  rb_builtin_class_name(e), i);
691 
692 #else
693  rb_warn("wrong element type %s at %ld (expected array)",
694  rb_builtin_class_name(e), i);
695  rb_warn("ignoring wrong elements is deprecated, remove them explicitly");
696  rb_warn("this causes ArgumentError in the next release");
697  continue;
698 #endif
699  }
700  switch (RARRAY_LEN(v)) {
701  default:
702  rb_raise(rb_eArgError, "invalid number of elements (%ld for 1..2)",
703  RARRAY_LEN(v));
704  case 2:
705  val = RARRAY_AREF(v, 1);
706  case 1:
707  key = RARRAY_AREF(v, 0);
708  rb_hash_aset(hash, key, val);
709  }
710  }
711  return hash;
712  }
713  }
714  if (argc % 2 != 0) {
715  rb_raise(rb_eArgError, "odd number of arguments for Hash");
716  }
717 
718  hash = hash_alloc(klass);
719  if (argc > 0) {
720  RHASH(hash)->ntbl = st_init_table_with_size(&objhash, argc / 2);
721  }
722  for (i=0; i<argc; i+=2) {
723  rb_hash_aset(hash, argv[i], argv[i + 1]);
724  }
725 
726  return hash;
727 }
728 
729 static VALUE
731 {
732  return rb_convert_type(hash, T_HASH, "Hash", "to_hash");
733 }
734 
735 VALUE
737 {
738  return rb_check_convert_type(hash, T_HASH, "Hash", "to_hash");
739 }
740 
741 /*
742  * call-seq:
743  * Hash.try_convert(obj) -> hash or nil
744  *
745  * Try to convert <i>obj</i> into a hash, using to_hash method.
746  * Returns converted hash or nil if <i>obj</i> cannot be converted
747  * for any reason.
748  *
749  * Hash.try_convert({1=>2}) # => {1=>2}
750  * Hash.try_convert("1=>2") # => nil
751  */
752 static VALUE
754 {
755  return rb_check_hash_type(hash);
756 }
757 
758 struct rehash_arg {
761 };
762 
763 static int
765 {
766  st_table *tbl = (st_table *)arg;
767 
768  st_insert(tbl, (st_data_t)key, (st_data_t)value);
769  return ST_CONTINUE;
770 }
771 
772 /*
773  * call-seq:
774  * hsh.rehash -> hsh
775  *
776  * Rebuilds the hash based on the current hash values for each key. If
777  * values of key objects have changed since they were inserted, this
778  * method will reindex <i>hsh</i>. If <code>Hash#rehash</code> is
779  * called while an iterator is traversing the hash, a
780  * <code>RuntimeError</code> will be raised in the iterator.
781  *
782  * a = [ "a", "b" ]
783  * c = [ "c", "d" ]
784  * h = { a => 100, c => 300 }
785  * h[a] #=> 100
786  * a[0] = "z"
787  * h[a] #=> nil
788  * h.rehash #=> {["z", "b"]=>100, ["c", "d"]=>300}
789  * h[a] #=> 100
790  */
791 
792 VALUE
794 {
795  VALUE tmp;
796  st_table *tbl;
797 
798  if (RHASH_ITER_LEV(hash) > 0) {
799  rb_raise(rb_eRuntimeError, "rehash during iteration");
800  }
801  rb_hash_modify_check(hash);
802  if (!RHASH(hash)->ntbl)
803  return hash;
804  tmp = hash_alloc(0);
805  tbl = st_init_table_with_size(RHASH(hash)->ntbl->type, RHASH(hash)->ntbl->num_entries);
806  RHASH(tmp)->ntbl = tbl;
807 
809  st_free_table(RHASH(hash)->ntbl);
810  RHASH(hash)->ntbl = tbl;
811  RHASH(tmp)->ntbl = 0;
812 
813  return hash;
814 }
815 
816 VALUE
818 {
820  VALUE ifnone = RHASH_IFNONE(hash);
821  if (!FL_TEST(hash, HASH_PROC_DEFAULT)) return ifnone;
822  if (key == Qundef) return Qnil;
823  return rb_funcall(ifnone, id_yield, 2, hash, key);
824  }
825  else {
826  return rb_funcall(hash, id_default, 1, key);
827  }
828 }
829 
830 /*
831  * call-seq:
832  * hsh[key] -> value
833  *
834  * Element Reference---Retrieves the <i>value</i> object corresponding
835  * to the <i>key</i> object. If not found, returns the default value (see
836  * <code>Hash::new</code> for details).
837  *
838  * h = { "a" => 100, "b" => 200 }
839  * h["a"] #=> 100
840  * h["c"] #=> nil
841  *
842  */
843 
844 VALUE
846 {
847  st_data_t val;
848 
849  if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key, &val)) {
850  return rb_hash_default_value(hash, key);
851  }
852  return (VALUE)val;
853 }
854 
855 VALUE
857 {
858  st_data_t val;
859 
860  if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key, &val)) {
861  return def; /* without Hash#default */
862  }
863  return (VALUE)val;
864 }
865 
866 VALUE
868 {
869  return rb_hash_lookup2(hash, key, Qnil);
870 }
871 
872 /*
873  * call-seq:
874  * hsh.fetch(key [, default] ) -> obj
875  * hsh.fetch(key) {| key | block } -> obj
876  *
877  * Returns a value from the hash for the given key. If the key can't be
878  * found, there are several options: With no other arguments, it will
879  * raise a <code>KeyError</code> exception; if <i>default</i> is given,
880  * then that will be returned; if the optional code block is specified,
881  * then that will be run and its result returned.
882  *
883  * h = { "a" => 100, "b" => 200 }
884  * h.fetch("a") #=> 100
885  * h.fetch("z", "go fish") #=> "go fish"
886  * h.fetch("z") { |el| "go fish, #{el}"} #=> "go fish, z"
887  *
888  * The following example shows that an exception is raised if the key
889  * is not found and a default value is not supplied.
890  *
891  * h = { "a" => 100, "b" => 200 }
892  * h.fetch("z")
893  *
894  * <em>produces:</em>
895  *
896  * prog.rb:2:in `fetch': key not found (KeyError)
897  * from prog.rb:2
898  *
899  */
900 
901 static VALUE
903 {
904  VALUE key;
905  st_data_t val;
906  long block_given;
907 
908  rb_check_arity(argc, 1, 2);
909  key = argv[0];
910 
911  block_given = rb_block_given_p();
912  if (block_given && argc == 2) {
913  rb_warn("block supersedes default value argument");
914  }
915  if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key, &val)) {
916  if (block_given) return rb_yield(key);
917  if (argc == 1) {
918  VALUE desc = rb_protect(rb_inspect, key, 0);
919  if (NIL_P(desc)) {
920  desc = rb_any_to_s(key);
921  }
922  desc = rb_str_ellipsize(desc, 65);
923  rb_raise(rb_eKeyError, "key not found: %"PRIsVALUE, desc);
924  }
925  return argv[1];
926  }
927  return (VALUE)val;
928 }
929 
930 VALUE
932 {
933  return rb_hash_fetch_m(1, &key, hash);
934 }
935 
936 /*
937  * call-seq:
938  * hsh.default(key=nil) -> obj
939  *
940  * Returns the default value, the value that would be returned by
941  * <i>hsh</i>[<i>key</i>] if <i>key</i> did not exist in <i>hsh</i>.
942  * See also <code>Hash::new</code> and <code>Hash#default=</code>.
943  *
944  * h = Hash.new #=> {}
945  * h.default #=> nil
946  * h.default(2) #=> nil
947  *
948  * h = Hash.new("cat") #=> {}
949  * h.default #=> "cat"
950  * h.default(2) #=> "cat"
951  *
952  * h = Hash.new {|h,k| h[k] = k.to_i*10} #=> {}
953  * h.default #=> nil
954  * h.default(2) #=> 20
955  */
956 
957 static VALUE
959 {
960  VALUE args[2], ifnone;
961 
962  rb_check_arity(argc, 0, 1);
963  ifnone = RHASH_IFNONE(hash);
964  if (FL_TEST(hash, HASH_PROC_DEFAULT)) {
965  if (argc == 0) return Qnil;
966  args[0] = hash;
967  args[1] = argv[0];
968  return rb_funcallv(ifnone, id_yield, 2, args);
969  }
970  return ifnone;
971 }
972 
973 /*
974  * call-seq:
975  * hsh.default = obj -> obj
976  *
977  * Sets the default value, the value returned for a key that does not
978  * exist in the hash. It is not possible to set the default to a
979  * <code>Proc</code> that will be executed on each key lookup.
980  *
981  * h = { "a" => 100, "b" => 200 }
982  * h.default = "Go fish"
983  * h["a"] #=> 100
984  * h["z"] #=> "Go fish"
985  * # This doesn't do what you might hope...
986  * h.default = proc do |hash, key|
987  * hash[key] = key + key
988  * end
989  * h[2] #=> #<Proc:0x401b3948@-:6>
990  * h["cat"] #=> #<Proc:0x401b3948@-:6>
991  */
992 
993 static VALUE
995 {
996  rb_hash_modify_check(hash);
997  SET_DEFAULT(hash, ifnone);
998  return ifnone;
999 }
1000 
1001 /*
1002  * call-seq:
1003  * hsh.default_proc -> anObject
1004  *
1005  * If <code>Hash::new</code> was invoked with a block, return that
1006  * block, otherwise return <code>nil</code>.
1007  *
1008  * h = Hash.new {|h,k| h[k] = k*k } #=> {}
1009  * p = h.default_proc #=> #<Proc:0x401b3d08@-:1>
1010  * a = [] #=> []
1011  * p.call(a, 2)
1012  * a #=> [nil, nil, 4]
1013  */
1014 
1015 
1016 static VALUE
1018 {
1019  if (FL_TEST(hash, HASH_PROC_DEFAULT)) {
1020  return RHASH_IFNONE(hash);
1021  }
1022  return Qnil;
1023 }
1024 
1025 /*
1026  * call-seq:
1027  * hsh.default_proc = proc_obj or nil
1028  *
1029  * Sets the default proc to be executed on each failed key lookup.
1030  *
1031  * h.default_proc = proc do |hash, key|
1032  * hash[key] = key + key
1033  * end
1034  * h[2] #=> 4
1035  * h["cat"] #=> "catcat"
1036  */
1037 
1038 VALUE
1040 {
1041  VALUE b;
1042 
1043  rb_hash_modify_check(hash);
1044  if (NIL_P(proc)) {
1045  SET_DEFAULT(hash, proc);
1046  return proc;
1047  }
1048  b = rb_check_convert_type(proc, T_DATA, "Proc", "to_proc");
1049  if (NIL_P(b) || !rb_obj_is_proc(b)) {
1051  "wrong default_proc type %s (expected Proc)",
1052  rb_obj_classname(proc));
1053  }
1054  proc = b;
1055  SET_PROC_DEFAULT(hash, proc);
1056  return proc;
1057 }
1058 
1059 static int
1060 key_i(VALUE key, VALUE value, VALUE arg)
1061 {
1062  VALUE *args = (VALUE *)arg;
1063 
1064  if (rb_equal(value, args[0])) {
1065  args[1] = key;
1066  return ST_STOP;
1067  }
1068  return ST_CONTINUE;
1069 }
1070 
1071 /*
1072  * call-seq:
1073  * hsh.key(value) -> key
1074  *
1075  * Returns the key of an occurrence of a given value. If the value is
1076  * not found, returns <code>nil</code>.
1077  *
1078  * h = { "a" => 100, "b" => 200, "c" => 300, "d" => 300 }
1079  * h.key(200) #=> "b"
1080  * h.key(300) #=> "c"
1081  * h.key(999) #=> nil
1082  *
1083  */
1084 
1085 static VALUE
1087 {
1088  VALUE args[2];
1089 
1090  args[0] = value;
1091  args[1] = Qnil;
1092 
1093  rb_hash_foreach(hash, key_i, (VALUE)args);
1094 
1095  return args[1];
1096 }
1097 
1098 /* :nodoc: */
1099 static VALUE
1101 {
1102  rb_warn("Hash#index is deprecated; use Hash#key");
1103  return rb_hash_key(hash, value);
1104 }
1105 
1106 /*
1107  * delete a specified entry a given key.
1108  * if there is the corresponding entry, return a value of the entry.
1109  * if there is no corresponding entry, return Qundef.
1110  */
1111 VALUE
1113 {
1114  st_data_t ktmp = (st_data_t)key, val;
1115 
1116  if (!RHASH(hash)->ntbl) {
1117  return Qundef;
1118  }
1119  else if (RHASH_ITER_LEV(hash) > 0 &&
1120  (st_delete_safe(RHASH(hash)->ntbl, &ktmp, &val, (st_data_t)Qundef))) {
1121  FL_SET(hash, HASH_DELETED);
1122  return (VALUE)val;
1123  }
1124  else if (st_delete(RHASH(hash)->ntbl, &ktmp, &val)) {
1125  return (VALUE)val;
1126  }
1127  else {
1128  return Qundef;
1129  }
1130 }
1131 
1132 /*
1133  * delete a specified entry by a given key.
1134  * if there is the corresponding entry, return a value of the entry.
1135  * if there is no corresponding entry, return Qnil.
1136  */
1137 VALUE
1139 {
1140  VALUE deleted_value = rb_hash_delete_entry(hash, key);
1141 
1142  if (deleted_value != Qundef) { /* likely pass */
1143  return deleted_value;
1144  }
1145  else {
1146  return Qnil;
1147  }
1148 }
1149 
1150 /*
1151  * call-seq:
1152  * hsh.delete(key) -> value
1153  * hsh.delete(key) {| key | block } -> value
1154  *
1155  * Deletes the key-value pair and returns the value from <i>hsh</i> whose
1156  * key is equal to <i>key</i>. If the key is not found, it returns
1157  * <em>nil</em>. If the optional code block is given and the
1158  * key is not found, pass in the key and return the result of
1159  * <i>block</i>.
1160  *
1161  * h = { "a" => 100, "b" => 200 }
1162  * h.delete("a") #=> 100
1163  * h.delete("z") #=> nil
1164  * h.delete("z") { |el| "#{el} not found" } #=> "z not found"
1165  *
1166  */
1167 
1168 static VALUE
1170 {
1171  VALUE val;
1172 
1173  rb_hash_modify_check(hash);
1174  val = rb_hash_delete_entry(hash, key);
1175 
1176  if (val != Qundef) {
1177  return val;
1178  }
1179  else {
1180  if (rb_block_given_p()) {
1181  return rb_yield(key);
1182  }
1183  else {
1184  return Qnil;
1185  }
1186  }
1187 }
1188 
1189 struct shift_var {
1192 };
1193 
1194 static int
1196 {
1197  struct shift_var *var = (struct shift_var *)arg;
1198 
1199  var->key = key;
1200  var->val = value;
1201  return ST_STOP;
1202 }
1203 
1204 /*
1205  * call-seq:
1206  * hsh.shift -> anArray or obj
1207  *
1208  * Removes a key-value pair from <i>hsh</i> and returns it as the
1209  * two-item array <code>[</code> <i>key, value</i> <code>]</code>, or
1210  * the hash's default value if the hash is empty.
1211  *
1212  * h = { 1 => "a", 2 => "b", 3 => "c" }
1213  * h.shift #=> [1, "a"]
1214  * h #=> {2=>"b", 3=>"c"}
1215  */
1216 
1217 static VALUE
1219 {
1220  struct shift_var var;
1221 
1222  rb_hash_modify_check(hash);
1223  if (RHASH(hash)->ntbl) {
1224  var.key = Qundef;
1225  if (RHASH_ITER_LEV(hash) == 0) {
1226  if (st_shift(RHASH(hash)->ntbl, &var.key, &var.val)) {
1227  return rb_assoc_new(var.key, var.val);
1228  }
1229  }
1230  else {
1231  rb_hash_foreach(hash, shift_i_safe, (VALUE)&var);
1232  if (var.key != Qundef) {
1233  rb_hash_delete_entry(hash, var.key);
1234  return rb_assoc_new(var.key, var.val);
1235  }
1236  }
1237  }
1238  return rb_hash_default_value(hash, Qnil);
1239 }
1240 
1241 static int
1243 {
1244  if (RTEST(rb_yield_values(2, key, value))) {
1245  return ST_DELETE;
1246  }
1247  return ST_CONTINUE;
1248 }
1249 
1250 static VALUE
1251 hash_enum_size(VALUE hash, VALUE args, VALUE eobj)
1252 {
1253  return rb_hash_size(hash);
1254 }
1255 
1256 /*
1257  * call-seq:
1258  * hsh.delete_if {| key, value | block } -> hsh
1259  * hsh.delete_if -> an_enumerator
1260  *
1261  * Deletes every key-value pair from <i>hsh</i> for which <i>block</i>
1262  * evaluates to <code>true</code>.
1263  *
1264  * If no block is given, an enumerator is returned instead.
1265  *
1266  * h = { "a" => 100, "b" => 200, "c" => 300 }
1267  * h.delete_if {|key, value| key >= "b" } #=> {"a"=>100}
1268  *
1269  */
1270 
1271 VALUE
1273 {
1275  rb_hash_modify_check(hash);
1276  if (RHASH(hash)->ntbl)
1277  rb_hash_foreach(hash, delete_if_i, hash);
1278  return hash;
1279 }
1280 
1281 /*
1282  * call-seq:
1283  * hsh.reject! {| key, value | block } -> hsh or nil
1284  * hsh.reject! -> an_enumerator
1285  *
1286  * Equivalent to <code>Hash#delete_if</code>, but returns
1287  * <code>nil</code> if no changes were made.
1288  */
1289 
1290 VALUE
1292 {
1293  st_index_t n;
1294 
1296  rb_hash_modify(hash);
1297  n = RHASH_SIZE(hash);
1298  if (!n) return Qnil;
1299  rb_hash_foreach(hash, delete_if_i, hash);
1300  if (n == RHASH(hash)->ntbl->num_entries) return Qnil;
1301  return hash;
1302 }
1303 
1304 static int
1306 {
1307  if (!RTEST(rb_yield_values(2, key, value))) {
1308  rb_hash_aset(result, key, value);
1309  }
1310  return ST_CONTINUE;
1311 }
1312 
1313 /*
1314  * call-seq:
1315  * hsh.reject {|key, value| block} -> a_hash
1316  * hsh.reject -> an_enumerator
1317  *
1318  * Returns a new hash consisting of entries for which the block returns false.
1319  *
1320  * If no block is given, an enumerator is returned instead.
1321  *
1322  * h = { "a" => 100, "b" => 200, "c" => 300 }
1323  * h.reject {|k,v| k < "b"} #=> {"b" => 200, "c" => 300}
1324  * h.reject {|k,v| v > 100} #=> {"a" => 100}
1325  */
1326 
1327 VALUE
1329 {
1330  VALUE result;
1331 
1333  if (RTEST(ruby_verbose)) {
1334  VALUE klass;
1335  if (HAS_EXTRA_STATES(hash, klass)) {
1336  rb_warn("extra states are no longer copied: %+"PRIsVALUE, hash);
1337  }
1338  }
1339  result = rb_hash_new();
1340  if (!RHASH_EMPTY_P(hash)) {
1341  rb_hash_foreach(hash, reject_i, result);
1342  }
1343  return result;
1344 }
1345 
1346 /*
1347  * call-seq:
1348  * hsh.values_at(key, ...) -> array
1349  *
1350  * Return an array containing the values associated with the given keys.
1351  * Also see <code>Hash.select</code>.
1352  *
1353  * h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }
1354  * h.values_at("cow", "cat") #=> ["bovine", "feline"]
1355  */
1356 
1357 VALUE
1359 {
1360  VALUE result = rb_ary_new2(argc);
1361  long i;
1362 
1363  for (i=0; i<argc; i++) {
1364  rb_ary_push(result, rb_hash_aref(hash, argv[i]));
1365  }
1366  return result;
1367 }
1368 
1369 /*
1370  * call-seq:
1371  * hsh.fetch_values(key, ...) -> array
1372  * hsh.fetch_values(key, ...) { |key| block } -> array
1373  *
1374  * Returns an array containing the values associated with the given keys
1375  * but also raises <code>KeyError</code> when one of keys can't be found.
1376  * Also see <code>Hash#values_at</code> and <code>Hash#fetch</code>.
1377  *
1378  * h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }
1379  *
1380  * h.fetch_values("cow", "cat") #=> ["bovine", "feline"]
1381  * h.fetch_values("cow", "bird") # raises KeyError
1382  * h.fetch_values("cow", "bird") { |k| k.upcase } #=> ["bovine", "BIRD"]
1383  */
1384 
1385 VALUE
1387 {
1388  VALUE result = rb_ary_new2(argc);
1389  long i;
1390 
1391  for (i=0; i<argc; i++) {
1392  rb_ary_push(result, rb_hash_fetch(hash, argv[i]));
1393  }
1394  return result;
1395 }
1396 
1397 static int
1399 {
1400  if (RTEST(rb_yield_values(2, key, value))) {
1401  rb_hash_aset(result, key, value);
1402  }
1403  return ST_CONTINUE;
1404 }
1405 
1406 /*
1407  * call-seq:
1408  * hsh.select {|key, value| block} -> a_hash
1409  * hsh.select -> an_enumerator
1410  *
1411  * Returns a new hash consisting of entries for which the block returns true.
1412  *
1413  * If no block is given, an enumerator is returned instead.
1414  *
1415  * h = { "a" => 100, "b" => 200, "c" => 300 }
1416  * h.select {|k,v| k > "a"} #=> {"b" => 200, "c" => 300}
1417  * h.select {|k,v| v < 200} #=> {"a" => 100}
1418  */
1419 
1420 VALUE
1422 {
1423  VALUE result;
1424 
1426  result = rb_hash_new();
1427  if (!RHASH_EMPTY_P(hash)) {
1428  rb_hash_foreach(hash, select_i, result);
1429  }
1430  return result;
1431 }
1432 
1433 static int
1435 {
1436  if (!RTEST(rb_yield_values(2, key, value))) {
1437  return ST_DELETE;
1438  }
1439  return ST_CONTINUE;
1440 }
1441 
1442 /*
1443  * call-seq:
1444  * hsh.select! {| key, value | block } -> hsh or nil
1445  * hsh.select! -> an_enumerator
1446  *
1447  * Equivalent to <code>Hash#keep_if</code>, but returns
1448  * <code>nil</code> if no changes were made.
1449  */
1450 
1451 VALUE
1453 {
1454  st_index_t n;
1455 
1457  rb_hash_modify_check(hash);
1458  if (!RHASH(hash)->ntbl)
1459  return Qnil;
1460  n = RHASH(hash)->ntbl->num_entries;
1461  rb_hash_foreach(hash, keep_if_i, hash);
1462  if (n == RHASH(hash)->ntbl->num_entries) return Qnil;
1463  return hash;
1464 }
1465 
1466 /*
1467  * call-seq:
1468  * hsh.keep_if {| key, value | block } -> hsh
1469  * hsh.keep_if -> an_enumerator
1470  *
1471  * Deletes every key-value pair from <i>hsh</i> for which <i>block</i>
1472  * evaluates to false.
1473  *
1474  * If no block is given, an enumerator is returned instead.
1475  *
1476  */
1477 
1478 VALUE
1480 {
1482  rb_hash_modify_check(hash);
1483  if (RHASH(hash)->ntbl)
1484  rb_hash_foreach(hash, keep_if_i, hash);
1485  return hash;
1486 }
1487 
1488 static int
1489 clear_i(VALUE key, VALUE value, VALUE dummy)
1490 {
1491  return ST_DELETE;
1492 }
1493 
1494 /*
1495  * call-seq:
1496  * hsh.clear -> hsh
1497  *
1498  * Removes all key-value pairs from <i>hsh</i>.
1499  *
1500  * h = { "a" => 100, "b" => 200 } #=> {"a"=>100, "b"=>200}
1501  * h.clear #=> {}
1502  *
1503  */
1504 
1505 VALUE
1507 {
1508  rb_hash_modify_check(hash);
1509  if (!RHASH(hash)->ntbl)
1510  return hash;
1511  if (RHASH(hash)->ntbl->num_entries > 0) {
1512  if (RHASH_ITER_LEV(hash) > 0)
1513  rb_hash_foreach(hash, clear_i, 0);
1514  else
1515  st_clear(RHASH(hash)->ntbl);
1516  }
1517 
1518  return hash;
1519 }
1520 
1521 static int
1522 hash_aset(st_data_t *key, st_data_t *val, struct update_arg *arg, int existing)
1523 {
1524  if (existing) {
1525  arg->new_value = arg->arg;
1526  arg->old_value = *val;
1527  }
1528  else {
1529  arg->new_key = *key;
1530  arg->new_value = arg->arg;
1531  }
1532  *val = arg->arg;
1533  return ST_CONTINUE;
1534 }
1535 
1536 static int
1537 hash_aset_str(st_data_t *key, st_data_t *val, struct update_arg *arg, int existing)
1538 {
1539  if (!existing) {
1540  *key = rb_str_new_frozen(*key);
1541  }
1542  return hash_aset(key, val, arg, existing);
1543 }
1544 
1547 
1548 /*
1549  * call-seq:
1550  * hsh[key] = value -> value
1551  * hsh.store(key, value) -> value
1552  *
1553  * == Element Assignment
1554  *
1555  * Associates the value given by +value+ with the key given by +key+.
1556  *
1557  * h = { "a" => 100, "b" => 200 }
1558  * h["a"] = 9
1559  * h["c"] = 4
1560  * h #=> {"a"=>9, "b"=>200, "c"=>4}
1561  * h.store("d", 42) #=> 42
1562  * h #=> {"a"=>9, "b"=>200, "c"=>4, "d"=>42}
1563  *
1564  * +key+ should not have its value changed while it is in use as a key (an
1565  * <tt>unfrozen String</tt> passed as a key will be duplicated and frozen).
1566  *
1567  * a = "a"
1568  * b = "b".freeze
1569  * h = { a => 100, b => 200 }
1570  * h.key(100).equal? a #=> false
1571  * h.key(200).equal? b #=> true
1572  *
1573  */
1574 
1575 VALUE
1577 {
1578  int iter_lev = RHASH_ITER_LEV(hash);
1579  st_table *tbl = RHASH(hash)->ntbl;
1580 
1581  rb_hash_modify(hash);
1582  if (!tbl) {
1583  if (iter_lev > 0) no_new_key();
1584  tbl = hash_tbl(hash);
1585  }
1586  if (tbl->type == &identhash || rb_obj_class(key) != rb_cString) {
1587  RHASH_UPDATE_ITER(hash, iter_lev, key, hash_aset, val);
1588  }
1589  else {
1590  RHASH_UPDATE_ITER(hash, iter_lev, key, hash_aset_str, val);
1591  }
1592  return val;
1593 }
1594 
1595 static int
1597 {
1598  rb_hash_aset(hash, key, val);
1599 
1600  return ST_CONTINUE;
1601 }
1602 
1603 /* :nodoc: */
1604 static VALUE
1606 {
1607  st_table *ntbl;
1608 
1609  rb_hash_modify_check(hash);
1610  hash2 = to_hash(hash2);
1611 
1612  Check_Type(hash2, T_HASH);
1613 
1614  if (hash == hash2) return hash;
1615 
1616  ntbl = RHASH(hash)->ntbl;
1617  if (RHASH(hash2)->ntbl) {
1618  if (ntbl) st_free_table(ntbl);
1619  RHASH(hash)->ntbl = st_copy(RHASH(hash2)->ntbl);
1620  if (RHASH(hash)->ntbl->num_entries)
1621  rb_hash_rehash(hash);
1622  }
1623  else if (ntbl) {
1624  st_clear(ntbl);
1625  }
1626 
1627  COPY_DEFAULT(hash, hash2);
1628 
1629  return hash;
1630 }
1631 
1632 /*
1633  * call-seq:
1634  * hsh.replace(other_hash) -> hsh
1635  *
1636  * Replaces the contents of <i>hsh</i> with the contents of
1637  * <i>other_hash</i>.
1638  *
1639  * h = { "a" => 100, "b" => 200 }
1640  * h.replace({ "c" => 300, "d" => 400 }) #=> {"c"=>300, "d"=>400}
1641  *
1642  */
1643 
1644 static VALUE
1646 {
1647  st_table *table2;
1648 
1649  rb_hash_modify_check(hash);
1650  if (hash == hash2) return hash;
1651  hash2 = to_hash(hash2);
1652 
1653  COPY_DEFAULT(hash, hash2);
1654 
1655  table2 = RHASH(hash2)->ntbl;
1656 
1657  rb_hash_clear(hash);
1658  if (table2) hash_tbl(hash)->type = table2->type;
1659  rb_hash_foreach(hash2, replace_i, hash);
1660 
1661  return hash;
1662 }
1663 
1664 /*
1665  * call-seq:
1666  * hsh.length -> integer
1667  * hsh.size -> integer
1668  *
1669  * Returns the number of key-value pairs in the hash.
1670  *
1671  * h = { "d" => 100, "a" => 200, "v" => 300, "e" => 400 }
1672  * h.length #=> 4
1673  * h.delete("a") #=> 200
1674  * h.length #=> 3
1675  */
1676 
1677 VALUE
1679 {
1680  return INT2FIX(RHASH_SIZE(hash));
1681 }
1682 
1683 
1684 /*
1685  * call-seq:
1686  * hsh.empty? -> true or false
1687  *
1688  * Returns <code>true</code> if <i>hsh</i> contains no key-value pairs.
1689  *
1690  * {}.empty? #=> true
1691  *
1692  */
1693 
1694 static VALUE
1696 {
1697  return RHASH_EMPTY_P(hash) ? Qtrue : Qfalse;
1698 }
1699 
1700 static int
1702 {
1703  rb_yield(value);
1704  return ST_CONTINUE;
1705 }
1706 
1707 /*
1708  * call-seq:
1709  * hsh.each_value {| value | block } -> hsh
1710  * hsh.each_value -> an_enumerator
1711  *
1712  * Calls <i>block</i> once for each key in <i>hsh</i>, passing the
1713  * value as a parameter.
1714  *
1715  * If no block is given, an enumerator is returned instead.
1716  *
1717  * h = { "a" => 100, "b" => 200 }
1718  * h.each_value {|value| puts value }
1719  *
1720  * <em>produces:</em>
1721  *
1722  * 100
1723  * 200
1724  */
1725 
1726 static VALUE
1728 {
1730  rb_hash_foreach(hash, each_value_i, 0);
1731  return hash;
1732 }
1733 
1734 static int
1736 {
1737  rb_yield(key);
1738  return ST_CONTINUE;
1739 }
1740 
1741 /*
1742  * call-seq:
1743  * hsh.each_key {| key | block } -> hsh
1744  * hsh.each_key -> an_enumerator
1745  *
1746  * Calls <i>block</i> once for each key in <i>hsh</i>, passing the key
1747  * as a parameter.
1748  *
1749  * If no block is given, an enumerator is returned instead.
1750  *
1751  * h = { "a" => 100, "b" => 200 }
1752  * h.each_key {|key| puts key }
1753  *
1754  * <em>produces:</em>
1755  *
1756  * a
1757  * b
1758  */
1759 static VALUE
1761 {
1763  rb_hash_foreach(hash, each_key_i, 0);
1764  return hash;
1765 }
1766 
1767 static int
1769 {
1770  rb_yield(rb_assoc_new(key, value));
1771  return ST_CONTINUE;
1772 }
1773 
1774 static int
1776 {
1777  VALUE argv[2];
1778  argv[0] = key;
1779  argv[1] = value;
1780  rb_yield_values2(2, argv);
1781  return ST_CONTINUE;
1782 }
1783 
1784 /*
1785  * call-seq:
1786  * hsh.each {| key, value | block } -> hsh
1787  * hsh.each_pair {| key, value | block } -> hsh
1788  * hsh.each -> an_enumerator
1789  * hsh.each_pair -> an_enumerator
1790  *
1791  * Calls <i>block</i> once for each key in <i>hsh</i>, passing the key-value
1792  * pair as parameters.
1793  *
1794  * If no block is given, an enumerator is returned instead.
1795  *
1796  * h = { "a" => 100, "b" => 200 }
1797  * h.each {|key, value| puts "#{key} is #{value}" }
1798  *
1799  * <em>produces:</em>
1800  *
1801  * a is 100
1802  * b is 200
1803  *
1804  */
1805 
1806 static VALUE
1808 {
1810  if (rb_block_arity() > 1)
1812  else
1813  rb_hash_foreach(hash, each_pair_i, 0);
1814  return hash;
1815 }
1816 
1817 static int
1819 {
1820  VALUE new_value = rb_yield(value);
1821  rb_hash_aset(result, key, new_value);
1822  return ST_CONTINUE;
1823 }
1824 
1825 /*
1826  * call-seq:
1827  * hsh.transform_values {|value| block } -> hsh
1828  * hsh.transform_values -> an_enumerator
1829  *
1830  * Return a new with the results of running block once for every value.
1831  * This method does not change the keys.
1832  *
1833  * h = { a: 1, b: 2, c: 3 }
1834  * h.transform_values {|v| v * v + 1 } #=> { a: 2, b: 5, c: 10 }
1835  * h.transform_values(&:to_s) #=> { a: "1", b: "2", c: "3" }
1836  * h.transform_values.with_index {|v, i| "#{v}.#{i}" }
1837  * #=> { a: "1.0", b: "2.1", c: "3.2" }
1838  *
1839  * If no block is given, an enumerator is returned instead.
1840  */
1841 static VALUE
1843 {
1844  VALUE result;
1845 
1847  result = rb_hash_new();
1848  if (!RHASH_EMPTY_P(hash)) {
1849  rb_hash_foreach(hash, transform_values_i, result);
1850  }
1851 
1852  return result;
1853 }
1854 
1855 /*
1856  * call-seq:
1857  * hsh.transform_values! {|value| block } -> hsh
1858  * hsh.transform_values! -> an_enumerator
1859  *
1860  * Return a new with the results of running block once for every value.
1861  * This method does not change the keys.
1862  *
1863  * h = { a: 1, b: 2, c: 3 }
1864  * h.transform_values! {|v| v * v + 1 } #=> { a: 2, b: 5, c: 10 }
1865  * h.transform_values!(&:to_s) #=> { a: "1", b: "2", c: "3" }
1866  * h.transform_values!.with_index {|v, i| "#{v}.#{i}" }
1867  * #=> { a: "1.0", b: "2.1", c: "3.2" }
1868  *
1869  * If no block is given, an enumerator is returned instead.
1870  */
1871 static VALUE
1873 {
1875  rb_hash_modify_check(hash);
1876  if (RHASH(hash)->ntbl)
1877  rb_hash_foreach(hash, transform_values_i, hash);
1878  return hash;
1879 }
1880 
1881 static int
1883 {
1884  rb_ary_push(ary, rb_assoc_new(key, value));
1885  return ST_CONTINUE;
1886 }
1887 
1888 /*
1889  * call-seq:
1890  * hsh.to_a -> array
1891  *
1892  * Converts <i>hsh</i> to a nested array of <code>[</code> <i>key,
1893  * value</i> <code>]</code> arrays.
1894  *
1895  * h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }
1896  * h.to_a #=> [["c", 300], ["a", 100], ["d", 400]]
1897  */
1898 
1899 static VALUE
1901 {
1902  VALUE ary;
1903 
1904  ary = rb_ary_new_capa(RHASH_SIZE(hash));
1905  rb_hash_foreach(hash, to_a_i, ary);
1906  OBJ_INFECT(ary, hash);
1907 
1908  return ary;
1909 }
1910 
1911 static int
1913 {
1914  VALUE str2;
1915 
1916  str2 = rb_inspect(key);
1917  if (RSTRING_LEN(str) > 1) {
1918  rb_str_buf_cat_ascii(str, ", ");
1919  }
1920  else {
1921  rb_enc_copy(str, str2);
1922  }
1923  rb_str_buf_append(str, str2);
1924  OBJ_INFECT(str, str2);
1925  rb_str_buf_cat_ascii(str, "=>");
1926  str2 = rb_inspect(value);
1927  rb_str_buf_append(str, str2);
1928  OBJ_INFECT(str, str2);
1929 
1930  return ST_CONTINUE;
1931 }
1932 
1933 static VALUE
1934 inspect_hash(VALUE hash, VALUE dummy, int recur)
1935 {
1936  VALUE str;
1937 
1938  if (recur) return rb_usascii_str_new2("{...}");
1939  str = rb_str_buf_new2("{");
1940  rb_hash_foreach(hash, inspect_i, str);
1941  rb_str_buf_cat2(str, "}");
1942  OBJ_INFECT(str, hash);
1943 
1944  return str;
1945 }
1946 
1947 /*
1948  * call-seq:
1949  * hsh.to_s -> string
1950  * hsh.inspect -> string
1951  *
1952  * Return the contents of this hash as a string.
1953  *
1954  * h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }
1955  * h.to_s #=> "{\"c\"=>300, \"a\"=>100, \"d\"=>400}"
1956  */
1957 
1958 static VALUE
1960 {
1961  if (RHASH_EMPTY_P(hash))
1962  return rb_usascii_str_new2("{}");
1963  return rb_exec_recursive(inspect_hash, hash, 0);
1964 }
1965 
1966 /*
1967  * call-seq:
1968  * hsh.to_hash => hsh
1969  *
1970  * Returns +self+.
1971  */
1972 
1973 static VALUE
1975 {
1976  return hash;
1977 }
1978 
1979 /*
1980  * call-seq:
1981  * hsh.to_h -> hsh or new_hash
1982  *
1983  * Returns +self+. If called on a subclass of Hash, converts
1984  * the receiver to a Hash object.
1985  */
1986 
1987 static VALUE
1989 {
1990  if (rb_obj_class(hash) != rb_cHash) {
1991  const VALUE flags = RBASIC(hash)->flags;
1992  hash = hash_dup(hash, rb_cHash, flags & HASH_PROC_DEFAULT);
1993  }
1994  return hash;
1995 }
1996 
1997 static int
1999 {
2000  rb_ary_push(ary, key);
2001  return ST_CONTINUE;
2002 }
2003 
2004 /*
2005  * call-seq:
2006  * hsh.keys -> array
2007  *
2008  * Returns a new array populated with the keys from this hash. See also
2009  * <code>Hash#values</code>.
2010  *
2011  * h = { "a" => 100, "b" => 200, "c" => 300, "d" => 400 }
2012  * h.keys #=> ["a", "b", "c", "d"]
2013  *
2014  */
2015 
2016 VALUE
2018 {
2019  VALUE keys;
2020  st_index_t size = RHASH_SIZE(hash);
2021 
2022  keys = rb_ary_new_capa(size);
2023  if (size == 0) return keys;
2024 
2025  if (ST_DATA_COMPATIBLE_P(VALUE)) {
2026  st_table *table = RHASH(hash)->ntbl;
2027 
2029  RARRAY_PTR_USE(keys, ptr, {
2030  size = st_keys_check(table, ptr, size, Qundef);
2031  });
2032  rb_ary_set_len(keys, size);
2033  }
2034  else {
2035  rb_hash_foreach(hash, keys_i, keys);
2036  }
2037 
2038  return keys;
2039 }
2040 
2041 static int
2043 {
2044  rb_ary_push(ary, value);
2045  return ST_CONTINUE;
2046 }
2047 
2048 /*
2049  * call-seq:
2050  * hsh.values -> array
2051  *
2052  * Returns a new array populated with the values from <i>hsh</i>. See
2053  * also <code>Hash#keys</code>.
2054  *
2055  * h = { "a" => 100, "b" => 200, "c" => 300 }
2056  * h.values #=> [100, 200, 300]
2057  *
2058  */
2059 
2060 VALUE
2062 {
2063  VALUE values;
2064  st_index_t size = RHASH_SIZE(hash);
2065 
2066  values = rb_ary_new_capa(size);
2067  if (size == 0) return values;
2068 
2069  if (ST_DATA_COMPATIBLE_P(VALUE)) {
2070  st_table *table = RHASH(hash)->ntbl;
2071 
2073  RARRAY_PTR_USE(values, ptr, {
2074  size = st_values_check(table, ptr, size, Qundef);
2075  });
2076  rb_ary_set_len(values, size);
2077  }
2078  else {
2079  rb_hash_foreach(hash, values_i, values);
2080  }
2081 
2082  return values;
2083 }
2084 
2085 /*
2086  * call-seq:
2087  * hsh.has_key?(key) -> true or false
2088  * hsh.include?(key) -> true or false
2089  * hsh.key?(key) -> true or false
2090  * hsh.member?(key) -> true or false
2091  *
2092  * Returns <code>true</code> if the given key is present in <i>hsh</i>.
2093  *
2094  * h = { "a" => 100, "b" => 200 }
2095  * h.has_key?("a") #=> true
2096  * h.has_key?("z") #=> false
2097  *
2098  * Note that <code>include?</code> and <code>member?</code> do not test member
2099  * equality using <code>==</code> as do other Enumerables.
2100  *
2101  * See also Enumerable#include?
2102  */
2103 
2104 VALUE
2106 {
2107  if (!RHASH(hash)->ntbl)
2108  return Qfalse;
2109  if (st_lookup(RHASH(hash)->ntbl, key, 0)) {
2110  return Qtrue;
2111  }
2112  return Qfalse;
2113 }
2114 
2115 static int
2117 {
2118  VALUE *data = (VALUE *)arg;
2119 
2120  if (rb_equal(value, data[1])) {
2121  data[0] = Qtrue;
2122  return ST_STOP;
2123  }
2124  return ST_CONTINUE;
2125 }
2126 
2127 /*
2128  * call-seq:
2129  * hsh.has_value?(value) -> true or false
2130  * hsh.value?(value) -> true or false
2131  *
2132  * Returns <code>true</code> if the given value is present for some key
2133  * in <i>hsh</i>.
2134  *
2135  * h = { "a" => 100, "b" => 200 }
2136  * h.value?(100) #=> true
2137  * h.value?(999) #=> false
2138  */
2139 
2140 static VALUE
2142 {
2143  VALUE data[2];
2144 
2145  data[0] = Qfalse;
2146  data[1] = val;
2148  return data[0];
2149 }
2150 
2151 struct equal_data {
2154  int eql;
2155 };
2156 
2157 static int
2159 {
2160  struct equal_data *data = (struct equal_data *)arg;
2161  st_data_t val2;
2162 
2163  if (!st_lookup(data->tbl, key, &val2)) {
2164  data->result = Qfalse;
2165  return ST_STOP;
2166  }
2167  if (!(data->eql ? rb_eql(val1, (VALUE)val2) : (int)rb_equal(val1, (VALUE)val2))) {
2168  data->result = Qfalse;
2169  return ST_STOP;
2170  }
2171  return ST_CONTINUE;
2172 }
2173 
2174 static VALUE
2176 {
2177  struct equal_data *data;
2178 
2179  if (recur) return Qtrue; /* Subtle! */
2180  data = (struct equal_data*)dt;
2181  data->result = Qtrue;
2182  rb_hash_foreach(hash, eql_i, dt);
2183 
2184  return data->result;
2185 }
2186 
2187 static VALUE
2188 hash_equal(VALUE hash1, VALUE hash2, int eql)
2189 {
2190  struct equal_data data;
2191 
2192  if (hash1 == hash2) return Qtrue;
2193  if (!RB_TYPE_P(hash2, T_HASH)) {
2194  if (!rb_respond_to(hash2, idTo_hash)) {
2195  return Qfalse;
2196  }
2197  if (eql) {
2198  if (rb_eql(hash2, hash1)) {
2199  return Qtrue;
2200  }
2201  else {
2202  return Qfalse;
2203  }
2204  }
2205  else {
2206  return rb_equal(hash2, hash1);
2207  }
2208  }
2209  if (RHASH_SIZE(hash1) != RHASH_SIZE(hash2))
2210  return Qfalse;
2211  if (!RHASH(hash1)->ntbl || !RHASH(hash2)->ntbl)
2212  return Qtrue;
2213  if (RHASH(hash1)->ntbl->type != RHASH(hash2)->ntbl->type)
2214  return Qfalse;
2215 #if 0
2216  if (!(rb_equal(RHASH_IFNONE(hash1), RHASH_IFNONE(hash2)) &&
2217  FL_TEST(hash1, HASH_PROC_DEFAULT) == FL_TEST(hash2, HASH_PROC_DEFAULT)))
2218  return Qfalse;
2219 #endif
2220 
2221  data.tbl = RHASH(hash2)->ntbl;
2222  data.eql = eql;
2223  return rb_exec_recursive_paired(recursive_eql, hash1, hash2, (VALUE)&data);
2224 }
2225 
2226 /*
2227  * call-seq:
2228  * hsh == other_hash -> true or false
2229  *
2230  * Equality---Two hashes are equal if they each contain the same number
2231  * of keys and if each key-value pair is equal to (according to
2232  * <code>Object#==</code>) the corresponding elements in the other
2233  * hash.
2234  *
2235  * h1 = { "a" => 1, "c" => 2 }
2236  * h2 = { 7 => 35, "c" => 2, "a" => 1 }
2237  * h3 = { "a" => 1, "c" => 2, 7 => 35 }
2238  * h4 = { "a" => 1, "d" => 2, "f" => 35 }
2239  * h1 == h2 #=> false
2240  * h2 == h3 #=> true
2241  * h3 == h4 #=> false
2242  *
2243  * The orders of each hashes are not compared.
2244  *
2245  * h1 = { "a" => 1, "c" => 2 }
2246  * h2 = { "c" => 2, "a" => 1 }
2247  * h1 == h2 #=> true
2248  *
2249  */
2250 
2251 static VALUE
2253 {
2254  return hash_equal(hash1, hash2, FALSE);
2255 }
2256 
2257 /*
2258  * call-seq:
2259  * hash.eql?(other) -> true or false
2260  *
2261  * Returns <code>true</code> if <i>hash</i> and <i>other</i> are
2262  * both hashes with the same content.
2263  * The orders of each hashes are not compared.
2264  */
2265 
2266 static VALUE
2267 rb_hash_eql(VALUE hash1, VALUE hash2)
2268 {
2269  return hash_equal(hash1, hash2, TRUE);
2270 }
2271 
2272 static int
2274 {
2275  st_index_t *hval = (st_index_t *)arg;
2276  st_index_t hdata[2];
2277 
2278  hdata[0] = rb_hash(key);
2279  hdata[1] = rb_hash(val);
2280  *hval ^= st_hash(hdata, sizeof(hdata), 0);
2281  return ST_CONTINUE;
2282 }
2283 
2284 /*
2285  * call-seq:
2286  * hsh.hash -> integer
2287  *
2288  * Compute a hash-code for this hash. Two hashes with the same content
2289  * will have the same hash code (and will compare using <code>eql?</code>).
2290  *
2291  * See also Object#hash.
2292  */
2293 
2294 static VALUE
2296 {
2297  st_index_t size = RHASH_SIZE(hash);
2298  st_index_t hval = rb_hash_start(size);
2299  hval = rb_hash_uint(hval, (st_index_t)rb_hash_hash);
2300  if (size) {
2301  rb_hash_foreach(hash, hash_i, (VALUE)&hval);
2302  }
2303  hval = rb_hash_end(hval);
2304  return ST2FIX(hval);
2305 }
2306 
2307 static int
2309 {
2310  rb_hash_aset(hash, value, key);
2311  return ST_CONTINUE;
2312 }
2313 
2314 /*
2315  * call-seq:
2316  * hsh.invert -> new_hash
2317  *
2318  * Returns a new hash created by using <i>hsh</i>'s values as keys, and
2319  * the keys as values.
2320  * If a key with the same value already exists in the <i>hsh</i>, then
2321  * the last one defined will be used, the earlier value(s) will be discarded.
2322  *
2323  * h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 }
2324  * h.invert #=> {0=>"a", 100=>"m", 200=>"d", 300=>"y"}
2325  *
2326  * If there is no key with the same value, Hash#invert is involutive.
2327  *
2328  * h = { a: 1, b: 3, c: 4 }
2329  * h.invert.invert == h #=> true
2330  *
2331  * The condition, no key with the same value, can be tested by comparing
2332  * the size of inverted hash.
2333  *
2334  * # no key with the same value
2335  * h = { a: 1, b: 3, c: 4 }
2336  * h.size == h.invert.size #=> true
2337  *
2338  * # two (or more) keys has the same value
2339  * h = { a: 1, b: 3, c: 1 }
2340  * h.size == h.invert.size #=> false
2341  *
2342  */
2343 
2344 static VALUE
2346 {
2347  VALUE h = rb_hash_new();
2348 
2350  return h;
2351 }
2352 
2353 static int
2354 rb_hash_update_callback(st_data_t *key, st_data_t *value, struct update_arg *arg, int existing)
2355 {
2356  if (existing) {
2357  arg->old_value = *value;
2358  arg->new_value = arg->arg;
2359  }
2360  else {
2361  arg->new_key = *key;
2362  arg->new_value = arg->arg;
2363  }
2364  *value = arg->arg;
2365  return ST_CONTINUE;
2366 }
2367 
2369 
2370 static int
2372 {
2373  RHASH_UPDATE(hash, key, rb_hash_update_callback, value);
2374  return ST_CONTINUE;
2375 }
2376 
2377 static int
2378 rb_hash_update_block_callback(st_data_t *key, st_data_t *value, struct update_arg *arg, int existing)
2379 {
2380  VALUE newvalue = (VALUE)arg->arg;
2381 
2382  if (existing) {
2383  newvalue = rb_yield_values(3, (VALUE)*key, (VALUE)*value, newvalue);
2384  arg->old_value = *value;
2385  }
2386  else {
2387  arg->new_key = *key;
2388  }
2389  arg->new_value = newvalue;
2390  *value = newvalue;
2391  return ST_CONTINUE;
2392 }
2393 
2395 
2396 static int
2398 {
2399  RHASH_UPDATE(hash, key, rb_hash_update_block_callback, value);
2400  return ST_CONTINUE;
2401 }
2402 
2403 /*
2404  * call-seq:
2405  * hsh.merge!(other_hash) -> hsh
2406  * hsh.update(other_hash) -> hsh
2407  * hsh.merge!(other_hash){|key, oldval, newval| block} -> hsh
2408  * hsh.update(other_hash){|key, oldval, newval| block} -> hsh
2409  *
2410  * Adds the contents of _other_hash_ to _hsh_. If no block is specified,
2411  * entries with duplicate keys are overwritten with the values from
2412  * _other_hash_, otherwise the value of each duplicate key is determined by
2413  * calling the block with the key, its value in _hsh_ and its value in
2414  * _other_hash_.
2415  *
2416  * h1 = { "a" => 100, "b" => 200 }
2417  * h2 = { "b" => 254, "c" => 300 }
2418  * h1.merge!(h2) #=> {"a"=>100, "b"=>254, "c"=>300}
2419  *
2420  * h1 = { "a" => 100, "b" => 200 }
2421  * h2 = { "b" => 254, "c" => 300 }
2422  * h1.merge!(h2) { |key, v1, v2| v1 }
2423  * #=> {"a"=>100, "b"=>200, "c"=>300}
2424  */
2425 
2426 static VALUE
2428 {
2429  rb_hash_modify(hash1);
2430  hash2 = to_hash(hash2);
2431  if (rb_block_given_p()) {
2432  rb_hash_foreach(hash2, rb_hash_update_block_i, hash1);
2433  }
2434  else {
2435  rb_hash_foreach(hash2, rb_hash_update_i, hash1);
2436  }
2437  return hash1;
2438 }
2439 
2444 };
2445 
2446 static int
2447 rb_hash_update_func_callback(st_data_t *key, st_data_t *value, struct update_arg *arg, int existing)
2448 {
2449  struct update_func_arg *uf_arg = (struct update_func_arg *)arg->arg;
2450  VALUE newvalue = uf_arg->value;
2451 
2452  if (existing) {
2453  newvalue = (*uf_arg->func)((VALUE)*key, (VALUE)*value, newvalue);
2454  arg->old_value = *value;
2455  }
2456  else {
2457  arg->new_key = *key;
2458  }
2459  arg->new_value = newvalue;
2460  *value = newvalue;
2461  return ST_CONTINUE;
2462 }
2463 
2465 
2466 static int
2468 {
2469  struct update_func_arg *arg = (struct update_func_arg *)arg0;
2470  VALUE hash = arg->hash;
2471 
2472  arg->value = value;
2474  return ST_CONTINUE;
2475 }
2476 
2477 VALUE
2479 {
2480  rb_hash_modify(hash1);
2481  hash2 = to_hash(hash2);
2482  if (func) {
2483  struct update_func_arg arg;
2484  arg.hash = hash1;
2485  arg.func = func;
2487  }
2488  else {
2489  rb_hash_foreach(hash2, rb_hash_update_i, hash1);
2490  }
2491  return hash1;
2492 }
2493 
2494 /*
2495  * call-seq:
2496  * hsh.merge(other_hash) -> new_hash
2497  * hsh.merge(other_hash){|key, oldval, newval| block} -> new_hash
2498  *
2499  * Returns a new hash containing the contents of <i>other_hash</i> and
2500  * the contents of <i>hsh</i>. If no block is specified, the value for
2501  * entries with duplicate keys will be that of <i>other_hash</i>. Otherwise
2502  * the value for each duplicate key is determined by calling the block
2503  * with the key, its value in <i>hsh</i> and its value in <i>other_hash</i>.
2504  *
2505  * h1 = { "a" => 100, "b" => 200 }
2506  * h2 = { "b" => 254, "c" => 300 }
2507  * h1.merge(h2) #=> {"a"=>100, "b"=>254, "c"=>300}
2508  * h1.merge(h2){|key, oldval, newval| newval - oldval}
2509  * #=> {"a"=>100, "b"=>54, "c"=>300}
2510  * h1 #=> {"a"=>100, "b"=>200}
2511  *
2512  */
2513 
2514 static VALUE
2516 {
2517  return rb_hash_update(rb_obj_dup(hash1), hash2);
2518 }
2519 
2520 static int
2522 {
2523  return !RTEST(rb_equal(a, b));
2524 }
2525 
2526 static VALUE
2528 {
2529  VALUE *args = (VALUE *)arg;
2530  return rb_hash_lookup2(args[0], args[1], Qundef);
2531 }
2532 
2535  const struct st_hash_type *orighash;
2536 };
2537 
2538 static VALUE
2540 {
2541  struct reset_hash_type_arg *p = (struct reset_hash_type_arg *)arg;
2542  RHASH(p->hash)->ntbl->type = p->orighash;
2543  return Qundef;
2544 }
2545 
2546 static int
2548 {
2549  VALUE *args = (VALUE *)arg;
2550 
2551  if (RTEST(rb_equal(args[0], key))) {
2552  args[1] = rb_assoc_new(key, val);
2553  return ST_STOP;
2554  }
2555  return ST_CONTINUE;
2556 }
2557 
2558 /*
2559  * call-seq:
2560  * hash.assoc(obj) -> an_array or nil
2561  *
2562  * Searches through the hash comparing _obj_ with the key using <code>==</code>.
2563  * Returns the key-value pair (two elements array) or +nil+
2564  * if no match is found. See <code>Array#assoc</code>.
2565  *
2566  * h = {"colors" => ["red", "blue", "green"],
2567  * "letters" => ["a", "b", "c" ]}
2568  * h.assoc("letters") #=> ["letters", ["a", "b", "c"]]
2569  * h.assoc("foo") #=> nil
2570  */
2571 
2572 VALUE
2574 {
2575  st_table *table;
2576  const struct st_hash_type *orighash;
2577  VALUE args[2];
2578 
2579  if (RHASH_EMPTY_P(hash)) return Qnil;
2580  table = RHASH(hash)->ntbl;
2581  orighash = table->type;
2582 
2583  if (orighash != &identhash) {
2584  VALUE value;
2585  struct reset_hash_type_arg ensure_arg;
2586  struct st_hash_type assochash;
2587 
2588  assochash.compare = assoc_cmp;
2589  assochash.hash = orighash->hash;
2590  table->type = &assochash;
2591  args[0] = hash;
2592  args[1] = key;
2593  ensure_arg.hash = hash;
2594  ensure_arg.orighash = orighash;
2595  value = rb_ensure(lookup2_call, (VALUE)&args, reset_hash_type, (VALUE)&ensure_arg);
2596  if (value != Qundef) return rb_assoc_new(key, value);
2597  }
2598 
2599  args[0] = key;
2600  args[1] = Qnil;
2601  rb_hash_foreach(hash, assoc_i, (VALUE)args);
2602  return args[1];
2603 }
2604 
2605 static int
2607 {
2608  VALUE *args = (VALUE *)arg;
2609 
2610  if (RTEST(rb_equal(args[0], val))) {
2611  args[1] = rb_assoc_new(key, val);
2612  return ST_STOP;
2613  }
2614  return ST_CONTINUE;
2615 }
2616 
2617 /*
2618  * call-seq:
2619  * hash.rassoc(obj) -> an_array or nil
2620  *
2621  * Searches through the hash comparing _obj_ with the value using <code>==</code>.
2622  * Returns the first key-value pair (two-element array) that matches. See
2623  * also <code>Array#rassoc</code>.
2624  *
2625  * a = {1=> "one", 2 => "two", 3 => "three", "ii" => "two"}
2626  * a.rassoc("two") #=> [2, "two"]
2627  * a.rassoc("four") #=> nil
2628  */
2629 
2630 VALUE
2632 {
2633  VALUE args[2];
2634 
2635  args[0] = obj;
2636  args[1] = Qnil;
2637  rb_hash_foreach(hash, rassoc_i, (VALUE)args);
2638  return args[1];
2639 }
2640 
2641 static int
2643 {
2644  VALUE pair[2];
2645 
2646  pair[0] = key;
2647  pair[1] = val;
2648  rb_ary_cat(ary, pair, 2);
2649 
2650  return ST_CONTINUE;
2651 }
2652 
2653 /*
2654  * call-seq:
2655  * hash.flatten -> an_array
2656  * hash.flatten(level) -> an_array
2657  *
2658  * Returns a new array that is a one-dimensional flattening of this
2659  * hash. That is, for every key or value that is an array, extract
2660  * its elements into the new array. Unlike Array#flatten, this
2661  * method does not flatten recursively by default. The optional
2662  * <i>level</i> argument determines the level of recursion to flatten.
2663  *
2664  * a = {1=> "one", 2 => [2,"two"], 3 => "three"}
2665  * a.flatten # => [1, "one", 2, [2, "two"], 3, "three"]
2666  * a.flatten(2) # => [1, "one", 2, 2, "two", 3, "three"]
2667  */
2668 
2669 static VALUE
2671 {
2672  VALUE ary;
2673 
2674  if (argc) {
2675  int level = NUM2INT(*argv);
2676  if (level == 0) return rb_hash_to_a(hash);
2677 
2678  ary = rb_ary_new_capa(RHASH_SIZE(hash) * 2);
2679  rb_hash_foreach(hash, flatten_i, ary);
2680  if (level - 1 > 0) {
2681  *argv = INT2FIX(level - 1);
2682  rb_funcallv(ary, id_flatten_bang, argc, argv);
2683  }
2684  else if (level < 0) {
2685  rb_funcallv(ary, id_flatten_bang, 0, 0);
2686  }
2687  }
2688  else {
2689  ary = rb_ary_new_capa(RHASH_SIZE(hash) * 2);
2690  rb_hash_foreach(hash, flatten_i, ary);
2691  }
2692 
2693  return ary;
2694 }
2695 
2696 static int
2698 {
2699  if (NIL_P(value)) {
2700  return ST_DELETE;
2701  }
2702  return ST_CONTINUE;
2703 }
2704 
2705 static int
2707 {
2708  if (!NIL_P(value)) {
2709  rb_hash_aset(hash, key, value);
2710  }
2711  return ST_CONTINUE;
2712 }
2713 
2714 /*
2715  * call-seq:
2716  * hsh.compact -> new_hash
2717  *
2718  * Returns a new hash with the nil values/key pairs removed
2719  *
2720  * h = { a: 1, b: false, c: nil }
2721  * h.compact #=> { a: 1, b: false }
2722  * h #=> { a: 1, b: false, c: nil }
2723  *
2724  */
2725 
2726 static VALUE
2728 {
2729  VALUE result = rb_hash_new();
2730  if (!RHASH_EMPTY_P(hash)) {
2731  rb_hash_foreach(hash, set_if_not_nil, result);
2732  }
2733  return result;
2734 }
2735 
2736 /*
2737  * call-seq:
2738  * hsh.compact! -> hsh
2739  *
2740  * Removes all nil values from the hash.
2741  * Returns the hash.
2742  *
2743  * h = { a: 1, b: false, c: nil }
2744  * h.compact! #=> { a: 1, b: false }
2745  *
2746  */
2747 
2748 static VALUE
2750 {
2751  rb_hash_modify_check(hash);
2752  if (RHASH(hash)->ntbl) {
2753  st_index_t n = RHASH(hash)->ntbl->num_entries;
2754  rb_hash_foreach(hash, delete_if_nil, hash);
2755  if (n != RHASH(hash)->ntbl->num_entries)
2756  return hash;
2757  }
2758  return Qnil;
2759 }
2760 
2761 /*
2762  * call-seq:
2763  * hsh.compare_by_identity -> hsh
2764  *
2765  * Makes <i>hsh</i> compare its keys by their identity, i.e. it
2766  * will consider exact same objects as same keys.
2767  *
2768  * h1 = { "a" => 100, "b" => 200, :c => "c" }
2769  * h1["a"] #=> 100
2770  * h1.compare_by_identity
2771  * h1.compare_by_identity? #=> true
2772  * h1["a".dup] #=> nil # different objects.
2773  * h1[:c] #=> "c" # same symbols are all same.
2774  *
2775  */
2776 
2777 static VALUE
2779 {
2780  if (rb_hash_compare_by_id_p(hash)) return hash;
2781  rb_hash_modify(hash);
2782  RHASH(hash)->ntbl->type = &identhash;
2783  rb_hash_rehash(hash);
2784  return hash;
2785 }
2786 
2787 /*
2788  * call-seq:
2789  * hsh.compare_by_identity? -> true or false
2790  *
2791  * Returns <code>true</code> if <i>hsh</i> will compare its keys by
2792  * their identity. Also see <code>Hash#compare_by_identity</code>.
2793  *
2794  */
2795 
2796 VALUE
2798 {
2799  if (!RHASH(hash)->ntbl)
2800  return Qfalse;
2801  if (RHASH(hash)->ntbl->type == &identhash) {
2802  return Qtrue;
2803  }
2804  return Qfalse;
2805 }
2806 
2807 VALUE
2809 {
2810  VALUE hash = rb_hash_new();
2811  RHASH(hash)->ntbl = st_init_table(&identhash);
2812  return hash;
2813 }
2814 
2815 st_table *
2817 {
2818  return st_init_table(&identhash);
2819 }
2820 
2821 st_table *
2823 {
2824  return st_init_table_with_size(&identhash, size);
2825 }
2826 
2827 static int
2829 {
2830  VALUE ret = rb_yield(rb_assoc_new(key, value));
2831  if (RTEST(ret)) {
2832  *(VALUE *)arg = Qtrue;
2833  return ST_STOP;
2834  }
2835  return ST_CONTINUE;
2836 }
2837 
2838 static int
2840 {
2841  VALUE ret = rb_yield_values(2, key, value);
2842  if (RTEST(ret)) {
2843  *(VALUE *)arg = Qtrue;
2844  return ST_STOP;
2845  }
2846  return ST_CONTINUE;
2847 }
2848 
2849 /*
2850  * call-seq:
2851  * hsh.any? [{ |(key, value)| block }] -> true or false
2852  *
2853  * See also Enumerable#any?
2854  */
2855 
2856 static VALUE
2858 {
2859  VALUE ret = Qfalse;
2860 
2861  if (RHASH_EMPTY_P(hash)) return Qfalse;
2862  if (!rb_block_given_p()) {
2863  /* yields pairs, never false */
2864  return Qtrue;
2865  }
2866  if (rb_block_arity() > 1)
2867  rb_hash_foreach(hash, any_p_i_fast, (VALUE)&ret);
2868  else
2869  rb_hash_foreach(hash, any_p_i, (VALUE)&ret);
2870  return ret;
2871 }
2872 
2873 /*
2874  * call-seq:
2875  * hsh.dig(key, ...) -> object
2876  *
2877  * Extracts the nested value specified by the sequence of <i>key</i>
2878  * objects by calling +dig+ at each step, returning +nil+ if any
2879  * intermediate step is +nil+.
2880  *
2881  * h = { foo: {bar: {baz: 1}}}
2882  *
2883  * h.dig(:foo, :bar, :baz) #=> 1
2884  * h.dig(:foo, :zot, :xyz) #=> nil
2885  *
2886  * g = { foo: [10, 11, 12] }
2887  * g.dig(:foo, 1) #=> 11
2888  * g.dig(:foo, 1, 0) #=> TypeError: Integer does not have #dig method
2889  * g.dig(:foo, :bar) #=> TypeError: no implicit conversion of Symbol into Integer
2890  */
2891 
2892 VALUE
2894 {
2896  self = rb_hash_aref(self, *argv);
2897  if (!--argc) return self;
2898  ++argv;
2899  return rb_obj_dig(argc, argv, self, Qnil);
2900 }
2901 
2902 static int
2904 {
2905  VALUE *args = (VALUE *)arg;
2906  VALUE v = rb_hash_lookup2(args[0], key, Qundef);
2907  if (v != Qundef && rb_equal(value, v)) return ST_CONTINUE;
2908  args[1] = Qfalse;
2909  return ST_STOP;
2910 }
2911 
2912 static VALUE
2913 hash_le(VALUE hash1, VALUE hash2)
2914 {
2915  VALUE args[2];
2916  args[0] = hash2;
2917  args[1] = Qtrue;
2918  rb_hash_foreach(hash1, hash_le_i, (VALUE)args);
2919  return args[1];
2920 }
2921 
2922 /*
2923  * call-seq:
2924  * hash <= other -> true or false
2925  *
2926  * Returns <code>true</code> if <i>hash</i> is subset of
2927  * <i>other</i> or equals to <i>other</i>.
2928  *
2929  * h1 = {a:1, b:2}
2930  * h2 = {a:1, b:2, c:3}
2931  * h1 <= h2 #=> true
2932  * h2 <= h1 #=> false
2933  * h1 <= h1 #=> true
2934  */
2935 static VALUE
2936 rb_hash_le(VALUE hash, VALUE other)
2937 {
2938  other = to_hash(other);
2939  if (RHASH_SIZE(hash) > RHASH_SIZE(other)) return Qfalse;
2940  return hash_le(hash, other);
2941 }
2942 
2943 /*
2944  * call-seq:
2945  * hash < other -> true or false
2946  *
2947  * Returns <code>true</code> if <i>hash</i> is subset of
2948  * <i>other</i>.
2949  *
2950  * h1 = {a:1, b:2}
2951  * h2 = {a:1, b:2, c:3}
2952  * h1 < h2 #=> true
2953  * h2 < h1 #=> false
2954  * h1 < h1 #=> false
2955  */
2956 static VALUE
2957 rb_hash_lt(VALUE hash, VALUE other)
2958 {
2959  other = to_hash(other);
2960  if (RHASH_SIZE(hash) >= RHASH_SIZE(other)) return Qfalse;
2961  return hash_le(hash, other);
2962 }
2963 
2964 /*
2965  * call-seq:
2966  * hash >= other -> true or false
2967  *
2968  * Returns <code>true</code> if <i>other</i> is subset of
2969  * <i>hash</i> or equals to <i>hash</i>.
2970  *
2971  * h1 = {a:1, b:2}
2972  * h2 = {a:1, b:2, c:3}
2973  * h1 >= h2 #=> false
2974  * h2 >= h1 #=> true
2975  * h1 >= h1 #=> true
2976  */
2977 static VALUE
2978 rb_hash_ge(VALUE hash, VALUE other)
2979 {
2980  other = to_hash(other);
2981  if (RHASH_SIZE(hash) < RHASH_SIZE(other)) return Qfalse;
2982  return hash_le(other, hash);
2983 }
2984 
2985 /*
2986  * call-seq:
2987  * hash > other -> true or false
2988  *
2989  * Returns <code>true</code> if <i>other</i> is subset of
2990  * <i>hash</i>.
2991  *
2992  * h1 = {a:1, b:2}
2993  * h2 = {a:1, b:2, c:3}
2994  * h1 > h2 #=> false
2995  * h2 > h1 #=> true
2996  * h1 > h1 #=> false
2997  */
2998 static VALUE
2999 rb_hash_gt(VALUE hash, VALUE other)
3000 {
3001  other = to_hash(other);
3002  if (RHASH_SIZE(hash) <= RHASH_SIZE(other)) return Qfalse;
3003  return hash_le(other, hash);
3004 }
3005 
3006 static VALUE
3007 hash_proc_call(VALUE key, VALUE hash, int argc, const VALUE *argv, VALUE passed_proc)
3008 {
3009  rb_check_arity(argc, 1, 1);
3010  return rb_hash_aref(hash, *argv);
3011 }
3012 
3013 static VALUE
3015 {
3016  return rb_func_proc_new(hash_proc_call, hash);
3017 }
3018 
3019 static int
3020 add_new_i(st_data_t *key, st_data_t *val, st_data_t arg, int existing)
3021 {
3022  VALUE *args = (VALUE *)arg;
3023  if (existing) return ST_STOP;
3024  RB_OBJ_WRITTEN(args[0], Qundef, (VALUE)*key);
3025  RB_OBJ_WRITE(args[0], (VALUE *)val, args[1]);
3026  return ST_CONTINUE;
3027 }
3028 
3029 /*
3030  * add +key+ to +val+ pair if +hash+ does not contain +key+.
3031  * returns non-zero if +key+ was contained.
3032  */
3033 int
3035 {
3036  st_table *tbl = rb_hash_tbl_raw(hash);
3037  VALUE args[2];
3038  args[0] = hash;
3039  args[1] = val;
3040  return st_update(tbl, (st_data_t)key, add_new_i, (st_data_t)args);
3041 }
3042 
3043 static int path_tainted = -1;
3044 
3045 static char **origenviron;
3046 #ifdef _WIN32
3047 #define GET_ENVIRON(e) ((e) = rb_w32_get_environ())
3048 #define FREE_ENVIRON(e) rb_w32_free_environ(e)
3049 static char **my_environ;
3050 #undef environ
3051 #define environ my_environ
3052 #undef getenv
3053 static char *(*w32_getenv)(const char*);
3054 static char *
3055 w32_getenv_unknown(const char *name)
3056 {
3057  char *(*func)(const char*);
3059  func = rb_w32_getenv;
3060  }
3061  else {
3062  func = rb_w32_ugetenv;
3063  }
3064  /* atomic assignment in flat memory model */
3065  return (w32_getenv = func)(name);
3066 }
3067 static char *(*w32_getenv)(const char*) = w32_getenv_unknown;
3068 #define getenv(n) w32_getenv(n)
3069 #elif defined(__APPLE__)
3070 #undef environ
3071 #define environ (*_NSGetEnviron())
3072 #define GET_ENVIRON(e) (e)
3073 #define FREE_ENVIRON(e)
3074 #else
3075 extern char **environ;
3076 #define GET_ENVIRON(e) (e)
3077 #define FREE_ENVIRON(e)
3078 #endif
3079 #ifdef ENV_IGNORECASE
3080 #define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
3081 #define ENVNMATCH(s1, s2, n) (STRNCASECMP((s1), (s2), (n)) == 0)
3082 #else
3083 #define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
3084 #define ENVNMATCH(s1, s2, n) (memcmp((s1), (s2), (n)) == 0)
3085 #endif
3086 
3087 #ifdef _WIN32
3088 static VALUE
3089 env_str_transcode(VALUE str, rb_encoding *enc)
3090 {
3091  return rb_str_conv_enc_opts(str, NULL, enc,
3093 }
3094 #endif
3095 
3096 static VALUE
3097 env_enc_str_new(const char *ptr, long len, rb_encoding *enc)
3098 {
3099 #ifdef _WIN32
3100  VALUE str = env_str_transcode(rb_utf8_str_new(ptr, len), enc);
3101 #else
3102  VALUE str = rb_external_str_new_with_enc(ptr, len, enc);
3103 #endif
3104 
3105  OBJ_TAINT(str);
3106  rb_obj_freeze(str);
3107  return str;
3108 }
3109 
3110 static VALUE
3111 env_enc_str_new_cstr(const char *ptr, rb_encoding *enc)
3112 {
3113  return env_enc_str_new(ptr, strlen(ptr), enc);
3114 }
3115 
3116 static VALUE
3117 env_str_new(const char *ptr, long len)
3118 {
3119  return env_enc_str_new(ptr, len, rb_locale_encoding());
3120 }
3121 
3122 static VALUE
3123 env_str_new2(const char *ptr)
3124 {
3125  if (!ptr) return Qnil;
3126  return env_str_new(ptr, strlen(ptr));
3127 }
3128 
3129 static int env_path_tainted(const char *);
3130 
3131 static rb_encoding *
3132 env_encoding_for(const char *name, const char *ptr)
3133 {
3134  if (ENVMATCH(name, PATH_ENV) && !env_path_tainted(ptr)) {
3135  return rb_filesystem_encoding();
3136  }
3137  else {
3138  return rb_locale_encoding();
3139  }
3140 }
3141 
3142 static VALUE
3143 env_name_new(const char *name, const char *ptr)
3144 {
3145  return env_enc_str_new_cstr(ptr, env_encoding_for(name, ptr));
3146 }
3147 
3148 static void *
3150 #ifdef _WIN32
3151  volatile VALUE *pstr,
3152 #else
3153  VALUE str,
3154 #endif
3155  const char *name)
3156 {
3157 #ifdef _WIN32
3158  VALUE str = *pstr;
3159 #endif
3160  char *var;
3161  rb_encoding *enc = rb_enc_get(str);
3162  if (!rb_enc_asciicompat(enc)) {
3163  rb_raise(rb_eArgError, "bad environment variable %s: ASCII incompatible encoding: %s",
3164  name, rb_enc_name(enc));
3165  }
3166 #ifdef _WIN32
3167  if (!rb_enc_str_asciionly_p(str)) {
3168  *pstr = str = rb_str_conv_enc(str, NULL, rb_utf8_encoding());
3169  }
3170 #endif
3171  var = RSTRING_PTR(str);
3172  if (memchr(var, '\0', RSTRING_LEN(str))) {
3173  rb_raise(rb_eArgError, "bad environment variable %s: contains null byte", name);
3174  }
3175  return rb_str_fill_terminator(str, 1); /* ASCII compatible */
3176 }
3177 
3178 #ifdef _WIN32
3179 #define get_env_ptr(var, val) \
3180  (var = get_env_cstr(&(val), #var))
3181 #else
3182 #define get_env_ptr(var, val) \
3183  (var = get_env_cstr(val, #var))
3184 #endif
3185 
3186 static inline const char *
3187 env_name(volatile VALUE *s)
3188 {
3189  const char *name;
3190  SafeStringValue(*s);
3191  get_env_ptr(name, *s);
3192  return name;
3193 }
3194 
3195 #define env_name(s) env_name(&(s))
3196 
3197 static VALUE
3199 {
3200  const char *nam, *val;
3201 
3202  nam = env_name(name);
3203  val = getenv(nam);
3204  if (val) {
3205  VALUE value = env_str_new2(val);
3206 
3207  ruby_setenv(nam, 0);
3208  if (ENVMATCH(nam, PATH_ENV)) {
3209  RB_GC_GUARD(name);
3210  path_tainted = 0;
3211  }
3212  return value;
3213  }
3214  return Qnil;
3215 }
3216 
3217 /*
3218  * call-seq:
3219  * ENV.delete(name) -> value
3220  * ENV.delete(name) { |name| } -> value
3221  *
3222  * Deletes the environment variable with +name+ and returns the value of the
3223  * variable. If a block is given it will be called when the named environment
3224  * does not exist.
3225  */
3226 static VALUE
3228 {
3229  VALUE val;
3230 
3231  val = env_delete(obj, name);
3232  if (NIL_P(val) && rb_block_given_p()) rb_yield(name);
3233  return val;
3234 }
3235 
3236 /*
3237  * call-seq:
3238  * ENV[name] -> value
3239  *
3240  * Retrieves the +value+ for environment variable +name+ as a String. Returns
3241  * +nil+ if the named variable does not exist.
3242  */
3243 static VALUE
3245 {
3246  const char *nam, *env;
3247 
3248  nam = env_name(name);
3249  env = getenv(nam);
3250  if (env) {
3251  return env_name_new(nam, env);
3252  }
3253  return Qnil;
3254 }
3255 
3256 /*
3257  * :yield: missing_name
3258  * call-seq:
3259  * ENV.fetch(name) -> value
3260  * ENV.fetch(name, default) -> value
3261  * ENV.fetch(name) { |missing_name| ... } -> value
3262  *
3263  * Retrieves the environment variable +name+.
3264  *
3265  * If the given name does not exist and neither +default+ nor a block a
3266  * provided an IndexError is raised. If a block is given it is called with
3267  * the missing name to provide a value. If a default value is given it will
3268  * be returned when no block is given.
3269  */
3270 static VALUE
3272 {
3273  VALUE key;
3274  long block_given;
3275  const char *nam, *env;
3276 
3277  rb_check_arity(argc, 1, 2);
3278  key = argv[0];
3279  block_given = rb_block_given_p();
3280  if (block_given && argc == 2) {
3281  rb_warn("block supersedes default value argument");
3282  }
3283  nam = env_name(key);
3284  env = getenv(nam);
3285  if (!env) {
3286  if (block_given) return rb_yield(key);
3287  if (argc == 1) {
3288  rb_raise(rb_eKeyError, "key not found: \"%"PRIsVALUE"\"", key);
3289  }
3290  return argv[1];
3291  }
3292  return env_name_new(nam, env);
3293 }
3294 
3295 static void
3296 path_tainted_p(const char *path)
3297 {
3298  path_tainted = rb_path_check(path)?0:1;
3299 }
3300 
3301 static int
3302 env_path_tainted(const char *path)
3303 {
3304  if (path_tainted < 0) {
3305  path_tainted_p(path);
3306  }
3307  return path_tainted;
3308 }
3309 
3310 int
3312 {
3313  if (path_tainted < 0) {
3315  }
3316  return path_tainted;
3317 }
3318 
3319 #if defined(_WIN32) || (defined(HAVE_SETENV) && defined(HAVE_UNSETENV))
3320 #elif defined __sun
3321 static int
3322 in_origenv(const char *str)
3323 {
3324  char **env;
3325  for (env = origenviron; *env; ++env) {
3326  if (*env == str) return 1;
3327  }
3328  return 0;
3329 }
3330 #else
3331 static int
3332 envix(const char *nam)
3333 {
3334  register int i, len = strlen(nam);
3335  char **env;
3336 
3337  env = GET_ENVIRON(environ);
3338  for (i = 0; env[i]; i++) {
3339  if (ENVNMATCH(env[i],nam,len) && env[i][len] == '=')
3340  break; /* memcmp must come first to avoid */
3341  } /* potential SEGV's */
3342  FREE_ENVIRON(environ);
3343  return i;
3344 }
3345 #endif
3346 
3347 #if defined(_WIN32)
3348 static size_t
3349 getenvsize(const WCHAR* p)
3350 {
3351  const WCHAR* porg = p;
3352  while (*p++) p += lstrlenW(p) + 1;
3353  return p - porg + 1;
3354 }
3355 static size_t
3356 getenvblocksize(void)
3357 {
3358  return 32767;
3359 }
3360 #endif
3361 
3362 #if defined(_WIN32) || \
3363  (defined(__sun) && !(defined(HAVE_SETENV) && defined(HAVE_UNSETENV)))
3364 
3365 NORETURN(static void invalid_envname(const char *name));
3366 
3367 static void
3368 invalid_envname(const char *name)
3369 {
3370  rb_syserr_fail_str(EINVAL, rb_sprintf("ruby_setenv(%s)", name));
3371 }
3372 
3373 static const char *
3374 check_envname(const char *name)
3375 {
3376  if (strchr(name, '=')) {
3377  invalid_envname(name);
3378  }
3379  return name;
3380 }
3381 #endif
3382 
3383 void
3384 ruby_setenv(const char *name, const char *value)
3385 {
3386 #if defined(_WIN32)
3387 # if defined(MINGW_HAS_SECURE_API) || RUBY_MSVCRT_VERSION >= 80
3388 # define HAVE__WPUTENV_S 1
3389 # endif
3390  VALUE buf;
3391  WCHAR *wname;
3392  WCHAR *wvalue = 0;
3393  int failed = 0;
3394  int len;
3395  check_envname(name);
3396  len = MultiByteToWideChar(CP_UTF8, 0, name, -1, NULL, 0);
3397  if (value) {
3398  WCHAR* p = GetEnvironmentStringsW();
3399  size_t n;
3400  int len2;
3401  if (!p) goto fail; /* never happen */
3402  n = lstrlen(name) + 2 + strlen(value) + getenvsize(p);
3403  FreeEnvironmentStringsW(p);
3404  if (n >= getenvblocksize()) {
3405  goto fail; /* 2 for '=' & '\0' */
3406  }
3407  len2 = MultiByteToWideChar(CP_UTF8, 0, value, -1, NULL, 0);
3408  wname = ALLOCV_N(WCHAR, buf, len + len2);
3409  wvalue = wname + len;
3410  MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, len);
3411  MultiByteToWideChar(CP_UTF8, 0, value, -1, wvalue, len2);
3412 #ifndef HAVE__WPUTENV_S
3413  wname[len-1] = L'=';
3414 #endif
3415  }
3416  else {
3417  wname = ALLOCV_N(WCHAR, buf, len + 1);
3418  MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, len);
3419  wvalue = wname + len;
3420  *wvalue = L'\0';
3421 #ifndef HAVE__WPUTENV_S
3422  wname[len-1] = L'=';
3423 #endif
3424  }
3425 #ifndef HAVE__WPUTENV_S
3426  failed = _wputenv(wname);
3427 #else
3428  failed = _wputenv_s(wname, wvalue);
3429 #endif
3430  ALLOCV_END(buf);
3431  /* even if putenv() failed, clean up and try to delete the
3432  * variable from the system area. */
3433  if (!value || !*value) {
3434  /* putenv() doesn't handle empty value */
3435  if (!SetEnvironmentVariable(name, value) &&
3436  GetLastError() != ERROR_ENVVAR_NOT_FOUND) goto fail;
3437  }
3438  if (failed) {
3439  fail:
3440  invalid_envname(name);
3441  }
3442 #elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV)
3443  if (value) {
3444  if (setenv(name, value, 1))
3445  rb_sys_fail_str(rb_sprintf("setenv(%s)", name));
3446  }
3447  else {
3448 #ifdef VOID_UNSETENV
3449  unsetenv(name);
3450 #else
3451  if (unsetenv(name))
3452  rb_sys_fail_str(rb_sprintf("unsetenv(%s)", name));
3453 #endif
3454  }
3455 #elif defined __sun
3456  /* Solaris 9 (or earlier) does not have setenv(3C) and unsetenv(3C). */
3457  /* The below code was tested on Solaris 10 by:
3458  % ./configure ac_cv_func_setenv=no ac_cv_func_unsetenv=no
3459  */
3460  size_t len, mem_size;
3461  char **env_ptr, *str, *mem_ptr;
3462 
3463  check_envname(name);
3464  len = strlen(name);
3465  if (value) {
3466  mem_size = len + strlen(value) + 2;
3467  mem_ptr = malloc(mem_size);
3468  if (mem_ptr == NULL)
3469  rb_sys_fail_str(rb_sprintf("malloc("PRIuSIZE")", mem_size));
3470  snprintf(mem_ptr, mem_size, "%s=%s", name, value);
3471  }
3472  for (env_ptr = GET_ENVIRON(environ); (str = *env_ptr) != 0; ++env_ptr) {
3473  if (!strncmp(str, name, len) && str[len] == '=') {
3474  if (!in_origenv(str)) free(str);
3475  while ((env_ptr[0] = env_ptr[1]) != 0) env_ptr++;
3476  break;
3477  }
3478  }
3479  if (value) {
3480  if (putenv(mem_ptr)) {
3481  free(mem_ptr);
3482  rb_sys_fail_str(rb_sprintf("putenv(%s)", name));
3483  }
3484  }
3485 #else /* WIN32 */
3486  size_t len;
3487  int i;
3488 
3489  i=envix(name); /* where does it go? */
3490 
3491  if (environ == origenviron) { /* need we copy environment? */
3492  int j;
3493  int max;
3494  char **tmpenv;
3495 
3496  for (max = i; environ[max]; max++) ;
3497  tmpenv = ALLOC_N(char*, max+2);
3498  for (j=0; j<max; j++) /* copy environment */
3499  tmpenv[j] = ruby_strdup(environ[j]);
3500  tmpenv[max] = 0;
3501  environ = tmpenv; /* tell exec where it is now */
3502  }
3503  if (environ[i]) {
3504  char **envp = origenviron;
3505  while (*envp && *envp != environ[i]) envp++;
3506  if (!*envp)
3507  xfree(environ[i]);
3508  if (!value) {
3509  while (environ[i]) {
3510  environ[i] = environ[i+1];
3511  i++;
3512  }
3513  return;
3514  }
3515  }
3516  else { /* does not exist yet */
3517  if (!value) return;
3518  REALLOC_N(environ, char*, i+2); /* just expand it a bit */
3519  environ[i+1] = 0; /* make sure it's null terminated */
3520  }
3521  len = strlen(name) + strlen(value) + 2;
3522  environ[i] = ALLOC_N(char, len);
3523  snprintf(environ[i],len,"%s=%s",name,value); /* all that work just for this */
3524 #endif /* WIN32 */
3525 }
3526 
3527 void
3528 ruby_unsetenv(const char *name)
3529 {
3530  ruby_setenv(name, 0);
3531 }
3532 
3533 /*
3534  * call-seq:
3535  * ENV[name] = value
3536  * ENV.store(name, value) -> value
3537  *
3538  * Sets the environment variable +name+ to +value+. If the value given is
3539  * +nil+ the environment variable is deleted.
3540  * +name+ must be a string.
3541  *
3542  */
3543 static VALUE
3545 {
3546  char *name, *value;
3547 
3548  if (NIL_P(val)) {
3549  env_delete(obj, nm);
3550  return Qnil;
3551  }
3552  SafeStringValue(nm);
3553  SafeStringValue(val);
3554  /* nm can be modified in `val.to_str`, don't get `name` before
3555  * check for `val` */
3556  get_env_ptr(name, nm);
3557  get_env_ptr(value, val);
3558 
3559  ruby_setenv(name, value);
3560  if (ENVMATCH(name, PATH_ENV)) {
3561  RB_GC_GUARD(nm);
3562  if (OBJ_TAINTED(val)) {
3563  /* already tainted, no check */
3564  path_tainted = 1;
3565  return val;
3566  }
3567  else {
3568  path_tainted_p(value);
3569  }
3570  }
3571  return val;
3572 }
3573 
3574 /*
3575  * call-seq:
3576  * ENV.keys -> Array
3577  *
3578  * Returns every environment variable name in an Array
3579  */
3580 static VALUE
3582 {
3583  char **env;
3584  VALUE ary;
3585 
3586  ary = rb_ary_new();
3587  env = GET_ENVIRON(environ);
3588  while (*env) {
3589  char *s = strchr(*env, '=');
3590  if (s) {
3591  rb_ary_push(ary, env_str_new(*env, s-*env));
3592  }
3593  env++;
3594  }
3595  FREE_ENVIRON(environ);
3596  return ary;
3597 }
3598 
3599 static VALUE
3600 rb_env_size(VALUE ehash, VALUE args, VALUE eobj)
3601 {
3602  char **env;
3603  long cnt = 0;
3604 
3605  env = GET_ENVIRON(environ);
3606  for (; *env ; ++env) {
3607  if (strchr(*env, '=')) {
3608  cnt++;
3609  }
3610  }
3611  FREE_ENVIRON(environ);
3612  return LONG2FIX(cnt);
3613 }
3614 
3615 /*
3616  * call-seq:
3617  * ENV.each_key { |name| } -> Hash
3618  * ENV.each_key -> Enumerator
3619  *
3620  * Yields each environment variable name.
3621  *
3622  * An Enumerator is returned if no block is given.
3623  */
3624 static VALUE
3626 {
3627  VALUE keys;
3628  long i;
3629 
3630  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
3631  keys = env_keys();
3632  for (i=0; i<RARRAY_LEN(keys); i++) {
3633  rb_yield(RARRAY_AREF(keys, i));
3634  }
3635  return ehash;
3636 }
3637 
3638 /*
3639  * call-seq:
3640  * ENV.values -> Array
3641  *
3642  * Returns every environment variable value as an Array
3643  */
3644 static VALUE
3646 {
3647  VALUE ary;
3648  char **env;
3649 
3650  ary = rb_ary_new();
3651  env = GET_ENVIRON(environ);
3652  while (*env) {
3653  char *s = strchr(*env, '=');
3654  if (s) {
3655  rb_ary_push(ary, env_str_new2(s+1));
3656  }
3657  env++;
3658  }
3659  FREE_ENVIRON(environ);
3660  return ary;
3661 }
3662 
3663 /*
3664  * call-seq:
3665  * ENV.each_value { |value| } -> Hash
3666  * ENV.each_value -> Enumerator
3667  *
3668  * Yields each environment variable +value+.
3669  *
3670  * An Enumerator is returned if no block was given.
3671  */
3672 static VALUE
3674 {
3675  VALUE values;
3676  long i;
3677 
3678  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
3679  values = env_values();
3680  for (i=0; i<RARRAY_LEN(values); i++) {
3681  rb_yield(RARRAY_AREF(values, i));
3682  }
3683  return ehash;
3684 }
3685 
3686 /*
3687  * call-seq:
3688  * ENV.each { |name, value| } -> Hash
3689  * ENV.each -> Enumerator
3690  * ENV.each_pair { |name, value| } -> Hash
3691  * ENV.each_pair -> Enumerator
3692  *
3693  * Yields each environment variable +name+ and +value+.
3694  *
3695  * If no block is given an Enumerator is returned.
3696  */
3697 static VALUE
3699 {
3700  char **env;
3701  VALUE ary;
3702  long i;
3703 
3704  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
3705 
3706  ary = rb_ary_new();
3707  env = GET_ENVIRON(environ);
3708  while (*env) {
3709  char *s = strchr(*env, '=');
3710  if (s) {
3711  rb_ary_push(ary, env_str_new(*env, s-*env));
3712  rb_ary_push(ary, env_str_new2(s+1));
3713  }
3714  env++;
3715  }
3716  FREE_ENVIRON(environ);
3717 
3718  if (rb_block_arity() > 1) {
3719  for (i=0; i<RARRAY_LEN(ary); i+=2) {
3720  rb_yield_values(2, RARRAY_AREF(ary, i), RARRAY_AREF(ary, i+1));
3721  }
3722  }
3723  else {
3724  for (i=0; i<RARRAY_LEN(ary); i+=2) {
3725  rb_yield(rb_assoc_new(RARRAY_AREF(ary, i), RARRAY_AREF(ary, i+1)));
3726  }
3727  }
3728  return ehash;
3729 }
3730 
3731 /*
3732  * call-seq:
3733  * ENV.reject! { |name, value| } -> ENV or nil
3734  * ENV.reject! -> Enumerator
3735  *
3736  * Equivalent to ENV#delete_if but returns +nil+ if no changes were made.
3737  *
3738  * Returns an Enumerator if no block was given.
3739  */
3740 static VALUE
3742 {
3743  VALUE keys;
3744  long i;
3745  int del = 0;
3746 
3747  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
3748  keys = env_keys();
3749  RBASIC_CLEAR_CLASS(keys);
3750  for (i=0; i<RARRAY_LEN(keys); i++) {
3751  VALUE val = rb_f_getenv(Qnil, RARRAY_AREF(keys, i));
3752  if (!NIL_P(val)) {
3753  if (RTEST(rb_yield_values(2, RARRAY_AREF(keys, i), val))) {
3754  FL_UNSET(RARRAY_AREF(keys, i), FL_TAINT);
3755  env_delete(Qnil, RARRAY_AREF(keys, i));
3756  del++;
3757  }
3758  }
3759  }
3760  RB_GC_GUARD(keys);
3761  if (del == 0) return Qnil;
3762  return envtbl;
3763 }
3764 
3765 /*
3766  * call-seq:
3767  * ENV.delete_if { |name, value| } -> Hash
3768  * ENV.delete_if -> Enumerator
3769  *
3770  * Deletes every environment variable for which the block evaluates to +true+.
3771  *
3772  * If no block is given an enumerator is returned instead.
3773  */
3774 static VALUE
3776 {
3777  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
3778  env_reject_bang(ehash);
3779  return envtbl;
3780 }
3781 
3782 /*
3783  * call-seq:
3784  * ENV.values_at(name, ...) -> Array
3785  *
3786  * Returns an array containing the environment variable values associated with
3787  * the given names. See also ENV.select.
3788  */
3789 static VALUE
3791 {
3792  VALUE result;
3793  long i;
3794 
3795  result = rb_ary_new();
3796  for (i=0; i<argc; i++) {
3797  rb_ary_push(result, rb_f_getenv(Qnil, argv[i]));
3798  }
3799  return result;
3800 }
3801 
3802 /*
3803  * call-seq:
3804  * ENV.select { |name, value| } -> Hash
3805  * ENV.select -> Enumerator
3806  *
3807  * Returns a copy of the environment for entries where the block returns true.
3808  *
3809  * Returns an Enumerator if no block was given.
3810  */
3811 static VALUE
3813 {
3814  VALUE result;
3815  VALUE keys;
3816  long i;
3817 
3818  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
3819  result = rb_hash_new();
3820  keys = env_keys();
3821  for (i = 0; i < RARRAY_LEN(keys); ++i) {
3822  VALUE key = RARRAY_AREF(keys, i);
3823  VALUE val = rb_f_getenv(Qnil, key);
3824  if (!NIL_P(val)) {
3825  if (RTEST(rb_yield_values(2, key, val))) {
3826  rb_hash_aset(result, key, val);
3827  }
3828  }
3829  }
3830  RB_GC_GUARD(keys);
3831 
3832  return result;
3833 }
3834 
3835 /*
3836  * call-seq:
3837  * ENV.select! { |name, value| } -> ENV or nil
3838  * ENV.select! -> Enumerator
3839  *
3840  * Equivalent to ENV#keep_if but returns +nil+ if no changes were made.
3841  */
3842 static VALUE
3844 {
3845  VALUE keys;
3846  long i;
3847  int del = 0;
3848 
3849  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
3850  keys = env_keys();
3851  RBASIC_CLEAR_CLASS(keys);
3852  for (i=0; i<RARRAY_LEN(keys); i++) {
3853  VALUE val = rb_f_getenv(Qnil, RARRAY_AREF(keys, i));
3854  if (!NIL_P(val)) {
3855  if (!RTEST(rb_yield_values(2, RARRAY_AREF(keys, i), val))) {
3856  FL_UNSET(RARRAY_AREF(keys, i), FL_TAINT);
3857  env_delete(Qnil, RARRAY_AREF(keys, i));
3858  del++;
3859  }
3860  }
3861  }
3862  RB_GC_GUARD(keys);
3863  if (del == 0) return Qnil;
3864  return envtbl;
3865 }
3866 
3867 /*
3868  * call-seq:
3869  * ENV.keep_if { |name, value| } -> Hash
3870  * ENV.keep_if -> Enumerator
3871  *
3872  * Deletes every environment variable where the block evaluates to +false+.
3873  *
3874  * Returns an enumerator if no block was given.
3875  */
3876 static VALUE
3878 {
3879  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
3880  env_select_bang(ehash);
3881  return envtbl;
3882 }
3883 
3884 /*
3885  * call-seq:
3886  * ENV.clear
3887  *
3888  * Removes every environment variable.
3889  */
3890 VALUE
3892 {
3893  VALUE keys;
3894  long i;
3895 
3896  keys = env_keys();
3897  for (i=0; i<RARRAY_LEN(keys); i++) {
3898  VALUE val = rb_f_getenv(Qnil, RARRAY_AREF(keys, i));
3899  if (!NIL_P(val)) {
3900  env_delete(Qnil, RARRAY_AREF(keys, i));
3901  }
3902  }
3903  RB_GC_GUARD(keys);
3904  return envtbl;
3905 }
3906 
3907 /*
3908  * call-seq:
3909  * ENV.to_s -> "ENV"
3910  *
3911  * Returns "ENV"
3912  */
3913 static VALUE
3915 {
3916  return rb_usascii_str_new2("ENV");
3917 }
3918 
3919 /*
3920  * call-seq:
3921  * ENV.inspect -> string
3922  *
3923  * Returns the contents of the environment as a String.
3924  */
3925 static VALUE
3927 {
3928  char **env;
3929  VALUE str, i;
3930 
3931  str = rb_str_buf_new2("{");
3932  env = GET_ENVIRON(environ);
3933  while (*env) {
3934  char *s = strchr(*env, '=');
3935 
3936  if (env != environ) {
3937  rb_str_buf_cat2(str, ", ");
3938  }
3939  if (s) {
3940  rb_str_buf_cat2(str, "\"");
3941  rb_str_buf_cat(str, *env, s-*env);
3942  rb_str_buf_cat2(str, "\"=>");
3943  i = rb_inspect(rb_str_new2(s+1));
3944  rb_str_buf_append(str, i);
3945  }
3946  env++;
3947  }
3948  FREE_ENVIRON(environ);
3949  rb_str_buf_cat2(str, "}");
3950  OBJ_TAINT(str);
3951 
3952  return str;
3953 }
3954 
3955 /*
3956  * call-seq:
3957  * ENV.to_a -> Array
3958  *
3959  * Converts the environment variables into an array of names and value arrays.
3960  *
3961  * ENV.to_a # => [["TERM", "xterm-color"], ["SHELL", "/bin/bash"], ...]
3962  *
3963  */
3964 static VALUE
3966 {
3967  char **env;
3968  VALUE ary;
3969 
3970  ary = rb_ary_new();
3971  env = GET_ENVIRON(environ);
3972  while (*env) {
3973  char *s = strchr(*env, '=');
3974  if (s) {
3975  rb_ary_push(ary, rb_assoc_new(env_str_new(*env, s-*env),
3976  env_str_new2(s+1)));
3977  }
3978  env++;
3979  }
3980  FREE_ENVIRON(environ);
3981  return ary;
3982 }
3983 
3984 /*
3985  * call-seq:
3986  * ENV.rehash
3987  *
3988  * Re-hashing the environment variables does nothing. It is provided for
3989  * compatibility with Hash.
3990  */
3991 static VALUE
3993 {
3994  return Qnil;
3995 }
3996 
3997 /*
3998  * call-seq:
3999  * ENV.length
4000  * ENV.size
4001  *
4002  * Returns the number of environment variables.
4003  */
4004 static VALUE
4006 {
4007  int i;
4008  char **env;
4009 
4010  env = GET_ENVIRON(environ);
4011  for (i=0; env[i]; i++)
4012  ;
4013  FREE_ENVIRON(environ);
4014  return INT2FIX(i);
4015 }
4016 
4017 /*
4018  * call-seq:
4019  * ENV.empty? -> true or false
4020  *
4021  * Returns true when there are no environment variables
4022  */
4023 static VALUE
4025 {
4026  char **env;
4027 
4028  env = GET_ENVIRON(environ);
4029  if (env[0] == 0) {
4030  FREE_ENVIRON(environ);
4031  return Qtrue;
4032  }
4033  FREE_ENVIRON(environ);
4034  return Qfalse;
4035 }
4036 
4037 /*
4038  * call-seq:
4039  * ENV.key?(name) -> true or false
4040  * ENV.include?(name) -> true or false
4041  * ENV.has_key?(name) -> true or false
4042  * ENV.member?(name) -> true or false
4043  *
4044  * Returns +true+ if there is an environment variable with the given +name+.
4045  */
4046 static VALUE
4048 {
4049  const char *s;
4050 
4051  s = env_name(key);
4052  if (getenv(s)) return Qtrue;
4053  return Qfalse;
4054 }
4055 
4056 /*
4057  * call-seq:
4058  * ENV.assoc(name) -> Array or nil
4059  *
4060  * Returns an Array of the name and value of the environment variable with
4061  * +name+ or +nil+ if the name cannot be found.
4062  */
4063 static VALUE
4065 {
4066  const char *s, *e;
4067 
4068  s = env_name(key);
4069  e = getenv(s);
4070  if (e) return rb_assoc_new(key, env_str_new2(e));
4071  return Qnil;
4072 }
4073 
4074 /*
4075  * call-seq:
4076  * ENV.value?(value) -> true or false
4077  * ENV.has_value?(value) -> true or false
4078  *
4079  * Returns +true+ if there is an environment variable with the given +value+.
4080  */
4081 static VALUE
4083 {
4084  char **env;
4085 
4086  obj = rb_check_string_type(obj);
4087  if (NIL_P(obj)) return Qnil;
4088  rb_check_safe_obj(obj);
4089  env = GET_ENVIRON(environ);
4090  while (*env) {
4091  char *s = strchr(*env, '=');
4092  if (s++) {
4093  long len = strlen(s);
4094  if (RSTRING_LEN(obj) == len && strncmp(s, RSTRING_PTR(obj), len) == 0) {
4095  FREE_ENVIRON(environ);
4096  return Qtrue;
4097  }
4098  }
4099  env++;
4100  }
4101  FREE_ENVIRON(environ);
4102  return Qfalse;
4103 }
4104 
4105 /*
4106  * call-seq:
4107  * ENV.rassoc(value)
4108  *
4109  * Returns an Array of the name and value of the environment variable with
4110  * +value+ or +nil+ if the value cannot be found.
4111  */
4112 static VALUE
4114 {
4115  char **env;
4116 
4117  obj = rb_check_string_type(obj);
4118  if (NIL_P(obj)) return Qnil;
4119  rb_check_safe_obj(obj);
4120  env = GET_ENVIRON(environ);
4121  while (*env) {
4122  char *s = strchr(*env, '=');
4123  if (s++) {
4124  long len = strlen(s);
4125  if (RSTRING_LEN(obj) == len && strncmp(s, RSTRING_PTR(obj), len) == 0) {
4126  VALUE result = rb_assoc_new(rb_tainted_str_new(*env, s-*env-1), obj);
4127  FREE_ENVIRON(environ);
4128  return result;
4129  }
4130  }
4131  env++;
4132  }
4133  FREE_ENVIRON(environ);
4134  return Qnil;
4135 }
4136 
4137 /*
4138  * call-seq:
4139  * ENV.key(value) -> name
4140  *
4141  * Returns the name of the environment variable with +value+. If the value is
4142  * not found +nil+ is returned.
4143  */
4144 static VALUE
4145 env_key(VALUE dmy, VALUE value)
4146 {
4147  char **env;
4148  VALUE str;
4149 
4150  SafeStringValue(value);
4151  env = GET_ENVIRON(environ);
4152  while (*env) {
4153  char *s = strchr(*env, '=');
4154  if (s++) {
4155  long len = strlen(s);
4156  if (RSTRING_LEN(value) == len && strncmp(s, RSTRING_PTR(value), len) == 0) {
4157  str = env_str_new(*env, s-*env-1);
4158  FREE_ENVIRON(environ);
4159  return str;
4160  }
4161  }
4162  env++;
4163  }
4164  FREE_ENVIRON(environ);
4165  return Qnil;
4166 }
4167 
4168 /*
4169  * call-seq:
4170  * ENV.index(value) -> key
4171  *
4172  * Deprecated method that is equivalent to ENV.key
4173  */
4174 static VALUE
4175 env_index(VALUE dmy, VALUE value)
4176 {
4177  rb_warn("ENV.index is deprecated; use ENV.key");
4178  return env_key(dmy, value);
4179 }
4180 
4181 /*
4182  * call-seq:
4183  * ENV.to_hash -> hash
4184  * ENV.to_h -> hash
4185  *
4186  * Creates a hash with a copy of the environment variables.
4187  *
4188  */
4189 static VALUE
4191 {
4192  char **env;
4193  VALUE hash;
4194 
4195  hash = rb_hash_new();
4196  env = GET_ENVIRON(environ);
4197  while (*env) {
4198  char *s = strchr(*env, '=');
4199  if (s) {
4200  rb_hash_aset(hash, env_str_new(*env, s-*env),
4201  env_str_new2(s+1));
4202  }
4203  env++;
4204  }
4205  FREE_ENVIRON(environ);
4206  return hash;
4207 }
4208 
4209 /*
4210  * call-seq:
4211  * ENV.reject { |name, value| } -> Hash
4212  * ENV.reject -> Enumerator
4213  *
4214  * Same as ENV#delete_if, but works on (and returns) a copy of the
4215  * environment.
4216  */
4217 static VALUE
4219 {
4220  return rb_hash_delete_if(env_to_hash());
4221 }
4222 
4223 /*
4224  * call-seq:
4225  * ENV.shift -> Array or nil
4226  *
4227  * Removes an environment variable name-value pair from ENV and returns it as
4228  * an Array. Returns +nil+ if when the environment is empty.
4229  */
4230 static VALUE
4232 {
4233  char **env;
4234  VALUE result = Qnil;
4235 
4236  env = GET_ENVIRON(environ);
4237  if (*env) {
4238  char *s = strchr(*env, '=');
4239  if (s) {
4240  VALUE key = env_str_new(*env, s-*env);
4242  env_delete(Qnil, key);
4243  result = rb_assoc_new(key, val);
4244  }
4245  }
4246  FREE_ENVIRON(environ);
4247  return result;
4248 }
4249 
4250 /*
4251  * call-seq:
4252  * ENV.invert -> Hash
4253  *
4254  * Returns a new hash created by using environment variable names as values
4255  * and values as names.
4256  */
4257 static VALUE
4259 {
4260  return rb_hash_invert(env_to_hash());
4261 }
4262 
4263 static int
4265 {
4266  env_aset(Qnil, key, val);
4267  if (rb_ary_includes(keys, key)) {
4268  rb_ary_delete(keys, key);
4269  }
4270  return ST_CONTINUE;
4271 }
4272 
4273 /*
4274  * call-seq:
4275  * ENV.replace(hash) -> env
4276  *
4277  * Replaces the contents of the environment variables with the contents of
4278  * +hash+.
4279  */
4280 static VALUE
4282 {
4283  VALUE keys;
4284  long i;
4285 
4286  keys = env_keys();
4287  if (env == hash) return env;
4288  hash = to_hash(hash);
4289  rb_hash_foreach(hash, env_replace_i, keys);
4290 
4291  for (i=0; i<RARRAY_LEN(keys); i++) {
4292  env_delete(env, RARRAY_AREF(keys, i));
4293  }
4294  RB_GC_GUARD(keys);
4295  return env;
4296 }
4297 
4298 static int
4300 {
4301  if (rb_block_given_p()) {
4302  val = rb_yield_values(3, key, rb_f_getenv(Qnil, key), val);
4303  }
4304  env_aset(Qnil, key, val);
4305  return ST_CONTINUE;
4306 }
4307 
4308 /*
4309  * call-seq:
4310  * ENV.update(hash) -> Hash
4311  * ENV.update(hash) { |name, old_value, new_value| } -> Hash
4312  *
4313  * Adds the contents of +hash+ to the environment variables. If no block is
4314  * specified entries with duplicate keys are overwritten, otherwise the value
4315  * of each duplicate name is determined by calling the block with the key, its
4316  * value from the environment and its value from the hash.
4317  */
4318 static VALUE
4320 {
4321  if (env == hash) return env;
4322  hash = to_hash(hash);
4323  rb_hash_foreach(hash, env_update_i, 0);
4324  return env;
4325 }
4326 
4327 /*
4328  * A Hash is a dictionary-like collection of unique keys and their values.
4329  * Also called associative arrays, they are similar to Arrays, but where an
4330  * Array uses integers as its index, a Hash allows you to use any object
4331  * type.
4332  *
4333  * Hashes enumerate their values in the order that the corresponding keys
4334  * were inserted.
4335  *
4336  * A Hash can be easily created by using its implicit form:
4337  *
4338  * grades = { "Jane Doe" => 10, "Jim Doe" => 6 }
4339  *
4340  * Hashes allow an alternate syntax for keys that are symbols.
4341  * Instead of
4342  *
4343  * options = { :font_size => 10, :font_family => "Arial" }
4344  *
4345  * You could write it as:
4346  *
4347  * options = { font_size: 10, font_family: "Arial" }
4348  *
4349  * Each named key is a symbol you can access in hash:
4350  *
4351  * options[:font_size] # => 10
4352  *
4353  * A Hash can also be created through its ::new method:
4354  *
4355  * grades = Hash.new
4356  * grades["Dorothy Doe"] = 9
4357  *
4358  * Hashes have a <em>default value</em> that is returned when accessing
4359  * keys that do not exist in the hash. If no default is set +nil+ is used.
4360  * You can set the default value by sending it as an argument to Hash.new:
4361  *
4362  * grades = Hash.new(0)
4363  *
4364  * Or by using the #default= method:
4365  *
4366  * grades = {"Timmy Doe" => 8}
4367  * grades.default = 0
4368  *
4369  * Accessing a value in a Hash requires using its key:
4370  *
4371  * puts grades["Jane Doe"] # => 0
4372  *
4373  * === Common Uses
4374  *
4375  * Hashes are an easy way to represent data structures, such as
4376  *
4377  * books = {}
4378  * books[:matz] = "The Ruby Programming Language"
4379  * books[:black] = "The Well-Grounded Rubyist"
4380  *
4381  * Hashes are also commonly used as a way to have named parameters in
4382  * functions. Note that no brackets are used below. If a hash is the last
4383  * argument on a method call, no braces are needed, thus creating a really
4384  * clean interface:
4385  *
4386  * Person.create(name: "John Doe", age: 27)
4387  *
4388  * def self.create(params)
4389  * @name = params[:name]
4390  * @age = params[:age]
4391  * end
4392  *
4393  * === Hash Keys
4394  *
4395  * Two objects refer to the same hash key when their <code>hash</code> value
4396  * is identical and the two objects are <code>eql?</code> to each other.
4397  *
4398  * A user-defined class may be used as a hash key if the <code>hash</code>
4399  * and <code>eql?</code> methods are overridden to provide meaningful
4400  * behavior. By default, separate instances refer to separate hash keys.
4401  *
4402  * A typical implementation of <code>hash</code> is based on the
4403  * object's data while <code>eql?</code> is usually aliased to the overridden
4404  * <code>==</code> method:
4405  *
4406  * class Book
4407  * attr_reader :author, :title
4408  *
4409  * def initialize(author, title)
4410  * @author = author
4411  * @title = title
4412  * end
4413  *
4414  * def ==(other)
4415  * self.class === other and
4416  * other.author == @author and
4417  * other.title == @title
4418  * end
4419  *
4420  * alias eql? ==
4421  *
4422  * def hash
4423  * @author.hash ^ @title.hash # XOR
4424  * end
4425  * end
4426  *
4427  * book1 = Book.new 'matz', 'Ruby in a Nutshell'
4428  * book2 = Book.new 'matz', 'Ruby in a Nutshell'
4429  *
4430  * reviews = {}
4431  *
4432  * reviews[book1] = 'Great reference!'
4433  * reviews[book2] = 'Nice and compact!'
4434  *
4435  * reviews.length #=> 1
4436  *
4437  * See also Object#hash and Object#eql?
4438  */
4439 
4440 void
4442 {
4443 #undef rb_intern
4444 #define rb_intern(str) rb_intern_const(str)
4445 
4446  id_hash = rb_intern("hash");
4447  id_yield = rb_intern("yield");
4448  id_default = rb_intern("default");
4449  id_flatten_bang = rb_intern("flatten!");
4450 
4451  rb_cHash = rb_define_class("Hash", rb_cObject);
4452 
4454 
4458  rb_define_method(rb_cHash, "initialize", rb_hash_initialize, -1);
4459  rb_define_method(rb_cHash, "initialize_copy", rb_hash_initialize_copy, 1);
4460  rb_define_method(rb_cHash, "rehash", rb_hash_rehash, 0);
4461 
4462  rb_define_method(rb_cHash, "to_hash", rb_hash_to_hash, 0);
4463  rb_define_method(rb_cHash, "to_h", rb_hash_to_h, 0);
4464  rb_define_method(rb_cHash, "to_a", rb_hash_to_a, 0);
4465  rb_define_method(rb_cHash, "inspect", rb_hash_inspect, 0);
4466  rb_define_alias(rb_cHash, "to_s", "inspect");
4467  rb_define_method(rb_cHash, "to_proc", rb_hash_to_proc, 0);
4468 
4471  rb_define_method(rb_cHash, "hash", rb_hash_hash, 0);
4472  rb_define_method(rb_cHash, "eql?", rb_hash_eql, 1);
4473  rb_define_method(rb_cHash, "fetch", rb_hash_fetch_m, -1);
4475  rb_define_method(rb_cHash, "store", rb_hash_aset, 2);
4476  rb_define_method(rb_cHash, "default", rb_hash_default, -1);
4478  rb_define_method(rb_cHash, "default_proc", rb_hash_default_proc, 0);
4479  rb_define_method(rb_cHash, "default_proc=", rb_hash_set_default_proc, 1);
4481  rb_define_method(rb_cHash, "index", rb_hash_index, 1);
4482  rb_define_method(rb_cHash, "size", rb_hash_size, 0);
4483  rb_define_method(rb_cHash, "length", rb_hash_size, 0);
4484  rb_define_method(rb_cHash, "empty?", rb_hash_empty_p, 0);
4485 
4486  rb_define_method(rb_cHash, "each_value", rb_hash_each_value, 0);
4487  rb_define_method(rb_cHash, "each_key", rb_hash_each_key, 0);
4488  rb_define_method(rb_cHash, "each_pair", rb_hash_each_pair, 0);
4490 
4491  rb_define_method(rb_cHash, "transform_values", rb_hash_transform_values, 0);
4492  rb_define_method(rb_cHash, "transform_values!", rb_hash_transform_values_bang, 0);
4493 
4494  rb_define_method(rb_cHash, "keys", rb_hash_keys, 0);
4495  rb_define_method(rb_cHash, "values", rb_hash_values, 0);
4496  rb_define_method(rb_cHash, "values_at", rb_hash_values_at, -1);
4497  rb_define_method(rb_cHash, "fetch_values", rb_hash_fetch_values, -1);
4498 
4499  rb_define_method(rb_cHash, "shift", rb_hash_shift, 0);
4501  rb_define_method(rb_cHash, "delete_if", rb_hash_delete_if, 0);
4502  rb_define_method(rb_cHash, "keep_if", rb_hash_keep_if, 0);
4503  rb_define_method(rb_cHash, "select", rb_hash_select, 0);
4505  rb_define_method(rb_cHash, "reject", rb_hash_reject, 0);
4507  rb_define_method(rb_cHash, "clear", rb_hash_clear, 0);
4508  rb_define_method(rb_cHash, "invert", rb_hash_invert, 0);
4509  rb_define_method(rb_cHash, "update", rb_hash_update, 1);
4510  rb_define_method(rb_cHash, "replace", rb_hash_replace, 1);
4511  rb_define_method(rb_cHash, "merge!", rb_hash_update, 1);
4512  rb_define_method(rb_cHash, "merge", rb_hash_merge, 1);
4513  rb_define_method(rb_cHash, "assoc", rb_hash_assoc, 1);
4514  rb_define_method(rb_cHash, "rassoc", rb_hash_rassoc, 1);
4515  rb_define_method(rb_cHash, "flatten", rb_hash_flatten, -1);
4516  rb_define_method(rb_cHash, "compact", rb_hash_compact, 0);
4518 
4519  rb_define_method(rb_cHash, "include?", rb_hash_has_key, 1);
4520  rb_define_method(rb_cHash, "member?", rb_hash_has_key, 1);
4521  rb_define_method(rb_cHash, "has_key?", rb_hash_has_key, 1);
4522  rb_define_method(rb_cHash, "has_value?", rb_hash_has_value, 1);
4525 
4526  rb_define_method(rb_cHash, "compare_by_identity", rb_hash_compare_by_id, 0);
4527  rb_define_method(rb_cHash, "compare_by_identity?", rb_hash_compare_by_id_p, 0);
4528 
4530  rb_define_method(rb_cHash, "dig", rb_hash_dig, -1);
4531 
4536 
4537  /* Document-class: ENV
4538  *
4539  * ENV is a hash-like accessor for environment variables.
4540  */
4541 
4542  /*
4543  * Hack to get RDoc to regard ENV as a class:
4544  * envtbl = rb_define_class("ENV", rb_cObject);
4545  */
4546  origenviron = environ;
4549 
4581  rb_define_singleton_method(envtbl, "values_at", env_values_at, -1);
4585  rb_define_singleton_method(envtbl, "has_value?", env_has_value, 1);
4592 
4593  /*
4594  * ENV is a Hash-like accessor for environment variables.
4595  *
4596  * See ENV (the class) for more details.
4597  */
4599 
4600  /* for callcc */
4602 }
RUBY_EXTERN VALUE rb_cString
Definition: ruby.h:1906
void rb_define_global_const(const char *, VALUE)
Definition: variable.c:2745
#define RBASIC_CLEAR_CLASS(obj)
Definition: internal.h:1312
#define RHASH_UPDATE(hash, key, func, arg)
Definition: hash.c:567
static VALUE empty_hash_alloc(VALUE klass)
Definition: hash.c:433
#define SET_PROC_DEFAULT(hash, proc)
Definition: hash.c:39
const char * rb_builtin_class_name(VALUE x)
Definition: error.c:645
#define T_SYMBOL
Definition: ruby.h:508
VALUE rb_hash(VALUE obj)
Definition: hash.c:126
static VALUE hash_foreach_call(VALUE arg)
Definition: hash.c:392
static int rb_hash_invert_i(VALUE key, VALUE value, VALUE hash)
Definition: hash.c:2308
Definition: st.h:99
static st_index_t objid_hash(VALUE obj)
Definition: hash.c:257
static int each_pair_i(VALUE key, VALUE value)
Definition: hash.c:1768
int rb_integer_pack(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
Definition: bignum.c:3531
#define FL_EXIVAR
Definition: ruby.h:1222
const struct st_hash_type * orighash
Definition: hash.c:2535
#define get_env_ptr(var, val)
Definition: hash.c:3182
st_foreach_func * func
Definition: hash.c:308
#define RARRAY_LEN(a)
Definition: ruby.h:1026
static VALUE env_each_value(VALUE ehash)
Definition: hash.c:3673
VALUE rb_ary_new_capa(long capa)
Definition: array.c:487
char * rb_w32_ugetenv(const char *)
Definition: win32.c:5142
#define FALSE
Definition: nkf.h:174
void rb_enc_copy(VALUE obj1, VALUE obj2)
Definition: encoding.c:978
static VALUE rb_hash_each_value(VALUE hash)
Definition: hash.c:1727
st_data_t arg
Definition: hash.c:511
static VALUE env_delete_m(VALUE obj, VALUE name)
Definition: hash.c:3227
static VALUE rb_hash_gt(VALUE hash, VALUE other)
Definition: hash.c:2999
size_t strlen(const char *)
static VALUE rb_hash_has_value(VALUE hash, VALUE val)
Definition: hash.c:2141
static int path_tainted
Definition: hash.c:3043
Definition: st.h:79
#define FREE_ENVIRON(e)
Definition: hash.c:3077
static uint64_t mult_and_mix(uint64_t m1, uint64_t m2)
Definition: hash.c:228
Definition: st.h:99
static VALUE hash_alloc_flags(VALUE klass, VALUE flags, VALUE ifnone)
Definition: hash.c:416
static void path_tainted_p(const char *path)
Definition: hash.c:3296
static int replace_i(VALUE key, VALUE val, VALUE hash)
Definition: hash.c:1596
static ID id_yield
Definition: hash.c:84
VALUE rb_hash_dup(VALUE hash)
Definition: hash.c:457
static VALUE rb_hash_s_try_convert(VALUE, VALUE)
Definition: hash.c:753
static int envix(const char *nam)
Definition: hash.c:3332
VALUE rb_yield_values(int n,...)
Definition: vm_eval.c:1031
#define NUM2INT(x)
Definition: ruby.h:684
#define RB_OBJ_WRITTEN(a, oldv, b)
Definition: ruby.h:1438
static int max(int a, int b)
Definition: strftime.c:142
static ID id_hash
Definition: hash.c:84
static unsigned int hash(str, len) register const char *str
static int keep_if_i(VALUE key, VALUE value, VALUE hash)
Definition: hash.c:1434
VALUE rb_obj_hash(VALUE obj)
Definition: hash.c:263
static void no_new_key(void)
Definition: hash.c:504
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1716
VALUE rb_exec_recursive_outer(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE)
Definition: thread.c:4720
static int env_update_i(VALUE key, VALUE val)
Definition: hash.c:4299
VALUE rb_eKeyError
Definition: error.c:765
static VALUE rb_hash_empty_p(VALUE hash)
Definition: hash.c:1695
#define RHASH_ITER_LEV(h)
Definition: ruby.h:1064
static VALUE env_invert(void)
Definition: hash.c:4258
static int hash_aset_str(st_data_t *key, st_data_t *val, struct update_arg *arg, int existing)
Definition: hash.c:1537
#define rb_usascii_str_new2
Definition: intern.h:863
#define FL_TAINT
Definition: ruby.h:1220
#define CLASS_OF(v)
Definition: ruby.h:453
static VALUE rb_hash_default(int argc, VALUE *argv, VALUE hash)
Definition: hash.c:958
#define ENVMATCH(n1, n2)
Definition: hash.c:3083
#define Qtrue
Definition: ruby.h:437
static int rb_hash_update_block_i(VALUE key, VALUE value, VALUE hash)
Definition: hash.c:2397
static void rb_hash_modify(VALUE hash)
Definition: hash.c:496
VALUE rb_cHash
Definition: hash.c:81
static VALUE env_str_new2(const char *ptr)
Definition: hash.c:3123
st_table * rb_init_identtable_with_size(st_index_t size)
Definition: hash.c:2822
st_table * tbl
Definition: hash.c:760
st_index_t rb_hash_end(st_index_t)
static VALUE hash_proc_call(VALUE key, VALUE hash, int argc, const VALUE *argv, VALUE passed_proc)
Definition: hash.c:3007
static int foreach_safe_i(st_data_t key, st_data_t value, st_data_t args, int error)
Definition: hash.c:313
VALUE rb_hash_select_bang(VALUE hash)
Definition: hash.c:1452
static int keys_i(VALUE key, VALUE value, VALUE ary)
Definition: hash.c:1998
double rb_float_value(VALUE v)
Definition: numeric.c:5510
static ID id_flatten_bang
Definition: hash.c:84
static VALUE env_to_s(void)
Definition: hash.c:3914
int rb_hash_add_new_element(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:3034
int rb_env_path_tainted(void)
Definition: hash.c:3311
Definition: st.h:99
static VALUE env_keys(void)
Definition: hash.c:3581
VALUE rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *)
Definition: string.c:998
int rb_hash_iter_lev(VALUE h)
Definition: hash.c:270
VALUE val
Definition: hash.c:1191
VALUE rb_eTypeError
Definition: error.c:762
static VALUE env_delete_if(VALUE ehash)
Definition: hash.c:3775
st_table * tbl
Definition: hash.c:2153
#define rb_check_arity
Definition: intern.h:303
static VALUE env_shift(void)
Definition: hash.c:4231
static VALUE rb_hash_lt(VALUE hash, VALUE other)
Definition: hash.c:2957
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:905
VALUE new_value
Definition: hash.c:533
static int eql_i(VALUE key, VALUE val1, VALUE arg)
Definition: hash.c:2158
VALUE rb_str_buf_new2(const char *)
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:54
if(len<=MAX_WORD_LENGTH &&len >=MIN_WORD_LENGTH)
Definition: zonetab.h:883
static VALUE env_each_pair(VALUE ehash)
Definition: hash.c:3698
static VALUE rb_env_size(VALUE ehash, VALUE args, VALUE eobj)
Definition: hash.c:3600
#define SET_DEFAULT(hash, ifnone)
Definition: hash.c:35
st_index_t rb_memhash(const void *ptr, long len)
Definition: random.c:1502
struct st_table * rb_hash_tbl_raw(VALUE hash)
Definition: hash.c:490
static ID id_default
Definition: hash.c:84
VALUE rb_hash_update_by(VALUE hash1, VALUE hash2, rb_hash_update_func *func)
Definition: hash.c:2478
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:821
struct st_table * rb_hash_tbl(VALUE hash)
Definition: hash.c:483
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Definition: intern.h:142
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *state)
Definition: eval.c:891
static int assoc_i(VALUE key, VALUE val, VALUE arg)
Definition: hash.c:2547
VALUE rb_to_int(VALUE)
Definition: object.c:2687
#define Check_Type(v, t)
Definition: ruby.h:562
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2207
static VALUE rb_hash_shift(VALUE hash)
Definition: hash.c:1218
char * rb_str_fill_terminator(VALUE str, const int termlen)
Definition: string.c:2156
static const uint64_t prime1
Definition: hash.c:223
static VALUE inspect_hash(VALUE hash, VALUE dummy, int recur)
Definition: hash.c:1934
long rb_dbl_long_hash(double d)
Definition: hash.c:149
VALUE rb_convert_type(VALUE, int, const char *, const char *)
Definition: object.c:2630
VALUE rb_exec_recursive(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE)
Definition: thread.c:4697
#define RB_GC_GUARD(v)
Definition: ruby.h:552
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
static VALUE rb_hash_equal(VALUE hash1, VALUE hash2)
Definition: hash.c:2252
#define T_HASH
Definition: ruby.h:499
st_index_t rb_str_hash(VALUE)
Definition: string.c:2985
#define ENVNMATCH(s1, s2, n)
Definition: hash.c:3084
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:864
static int env_path_tainted(const char *)
Definition: hash.c:3302
VALUE rb_hash_lookup(VALUE hash, VALUE key)
Definition: hash.c:867
static int values_i(VALUE key, VALUE value, VALUE ary)
Definition: hash.c:2042
st_data_t st_index_t
Definition: st.h:50
#define st_delete
Definition: regint.h:182
#define st_lookup
Definition: regint.h:185
int st_update(st_table *table, st_data_t key, st_update_callback_func *func, st_data_t arg)
Definition: st.c:1371
static VALUE env_keep_if(VALUE ehash)
Definition: hash.c:3877
static int rb_hash_update_i(VALUE key, VALUE value, VALUE hash)
Definition: hash.c:2371
VALUE rb_hash_fetch(VALUE hash, VALUE key)
Definition: hash.c:931
static VALUE rb_hash_each_key(VALUE hash)
Definition: hash.c:1760
#define FIXNUM_P(f)
Definition: ruby.h:365
rb_encoding * rb_utf8_encoding(void)
Definition: encoding.c:1320
static int rb_hash_search_value(VALUE key, VALUE value, VALUE arg)
Definition: hash.c:2116
char ** environ
VALUE rb_ary_cat(VALUE ary, const VALUE *argv, long len)
Definition: array.c:917
VALUE rb_str_buf_append(VALUE, VALUE)
Definition: string.c:2802
static VALUE rb_hash_replace(VALUE hash, VALUE hash2)
Definition: hash.c:1645
VALUE old_key
Definition: hash.c:532
static int any_p_i_fast(VALUE key, VALUE value, VALUE arg)
Definition: hash.c:2839
VALUE result
Definition: hash.c:2152
VALUE rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts)
Definition: string.c:884
#define rb_intern(str)
#define NOINSERT_UPDATE_CALLBACK(func)
Definition: hash.c:514
#define OBJ_TAINTED(x)
Definition: ruby.h:1298
#define RHASH_IFNONE(h)
Definition: ruby.h:1065
const char * rb_obj_classname(VALUE)
Definition: variable.c:458
static VALUE rb_hash_fetch_m(int argc, VALUE *argv, VALUE hash)
Definition: hash.c:902
VALUE rb_big_hash(VALUE x)
Definition: bignum.c:6649
#define rb_ary_new2
Definition: intern.h:90
static VALUE envtbl
Definition: hash.c:83
static VALUE env_rassoc(VALUE dmy, VALUE obj)
Definition: hash.c:4113
static int rb_hash_update_func_i(VALUE key, VALUE value, VALUE arg0)
Definition: hash.c:2467
static VALUE env_each_key(VALUE ehash)
Definition: hash.c:3625
#define RHASH_SET_IFNONE(h, ifnone)
Definition: ruby.h:1068
static st_index_t obj_any_hash(VALUE obj)
Definition: hash.c:206
VALUE rb_hash_keys(VALUE hash)
Definition: hash.c:2017
VALUE rb_str_buf_cat(VALUE, const char *, long)
RUBY_SYMBOL_EXPORT_BEGIN typedef unsigned long st_data_t
Definition: st.h:22
VALUE rb_hash_default_value(VALUE hash, VALUE key)
Definition: hash.c:817
#define rb_ident_cmp
Definition: hash.c:280
#define NEWOBJ_OF(obj, type, klass, flags)
Definition: ruby.h:754
VALUE rb_utf8_str_new(const char *, long)
Definition: string.c:750
void rb_hash_foreach(VALUE hash, int(*func)(ANYARGS), VALUE farg)
Definition: hash.c:402
#define RHASH(obj)
Definition: internal.h:562
VALUE rb_obj_dup(VALUE)
Definition: object.c:437
#define HAS_EXTRA_STATES(hash, klass)
Definition: hash.c:30
#define RB_TYPE_P(obj, type)
Definition: ruby.h:527
static VALUE rb_hash_to_proc(VALUE hash)
Definition: hash.c:3014
static VALUE rb_hash_transform_values_bang(VALUE hash)
Definition: hash.c:1872
#define fail()
static VALUE hash_le(VALUE hash1, VALUE hash2)
Definition: hash.c:2913
VALUE rb_hash_rehash(VALUE hash)
Definition: hash.c:793
static int rb_hash_rehash_i(VALUE key, VALUE value, VALUE arg)
Definition: hash.c:764
static VALUE env_key(VALUE dmy, VALUE value)
Definition: hash.c:4145
#define FL_TEST(x, f)
Definition: ruby.h:1284
unsigned long long uint64_t
Definition: sha2.h:102
static int transform_values_i(VALUE key, VALUE value, VALUE result)
Definition: hash.c:1818
void Init_Hash(void)
Definition: hash.c:4441
#define COPY_DEFAULT(hash, hash2)
Definition: hash.c:41
#define ALLOC_N(type, n)
Definition: ruby.h:1587
int rb_block_given_p(void)
Definition: eval.c:797
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:1576
static VALUE env_assoc(VALUE env, VALUE key)
Definition: hash.c:4064
VALUE rb_hash_reject_bang(VALUE hash)
Definition: hash.c:1291
#define val
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1872
VALUE rb_eRuntimeError
Definition: error.c:761
VALUE rb_exec_recursive_paired(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE, VALUE)
Definition: thread.c:4708
static VALUE env_select_bang(VALUE ehash)
Definition: hash.c:3843
static int assoc_cmp(VALUE a, VALUE b)
Definition: hash.c:2521
#define st_init_table_with_size
Definition: regint.h:177
static VALUE rb_hash_initialize_copy(VALUE hash, VALUE hash2)
Definition: hash.c:1605
static int hash_le_i(VALUE key, VALUE value, VALUE arg)
Definition: hash.c:2903
char * ruby_strdup(const char *)
Definition: util.c:496
static int hash_i(VALUE key, VALUE val, VALUE arg)
Definition: hash.c:2273
VALUE rb_hash_delete_entry(VALUE hash, VALUE key)
Definition: hash.c:1112
#define ECONV_INVALID_REPLACE
Definition: encoding.h:388
static int add_new_i(st_data_t *key, st_data_t *val, st_data_t arg, int existing)
Definition: hash.c:3020
VALUE rb_ary_new(void)
Definition: array.c:493
VALUE rb_str_buf_cat2(VALUE, const char *)
#define OBJ_WB_UNPROTECT(x)
Definition: ruby.h:1424
int rb_ascii8bit_encindex(void)
Definition: encoding.c:1314
long rb_objid_hash(st_index_t index)
Definition: hash.c:251
int rb_class_has_methods(VALUE c)
Definition: class.c:2051
static int each_value_i(VALUE key, VALUE value)
Definition: hash.c:1701
VALUE rb_hash_set_ifnone(VALUE hash, VALUE ifnone)
Definition: hash.c:93
#define RSYMBOL(obj)
Definition: symbol.h:33
#define snprintf
Definition: subst.h:6
static char * w32_getenv(const char *name, UINT cp)
Definition: win32.c:5108
static void copy_default(struct RHash *hash, const struct RHash *hash2)
Definition: hash.c:44
#define NIL_P(v)
Definition: ruby.h:451
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:646
static VALUE rb_hash_eql(VALUE hash1, VALUE hash2)
Definition: hash.c:2267
static VALUE env_delete(VALUE obj, VALUE name)
Definition: hash.c:3198
#define ID_SCOPE_SHIFT
Definition: id.h:31
register int hval
Definition: zonetab.h:82
void rb_sys_fail_str(VALUE mesg)
Definition: error.c:2332
rb_atomic_t cnt[RUBY_NSIG]
Definition: signal.c:525
VALUE rb_hash_update_func(VALUE newkey, VALUE oldkey, VALUE value)
Definition: intern.h:523
static VALUE rb_hash_any_p(VALUE hash)
Definition: hash.c:2857
#define FLONUM_P(x)
Definition: ruby.h:399
VALUE value
Definition: hash.c:2442
#define T_FLOAT
Definition: ruby.h:495
int rb_foreach_func(VALUE, VALUE, VALUE)
Definition: hash.c:339
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
Definition: string.c:992
int argc
Definition: ruby.c:183
static VALUE rb_hash_to_h(VALUE hash)
Definition: hash.c:1988
static VALUE env_reject(void)
Definition: hash.c:4218
#define Qfalse
Definition: ruby.h:436
static VALUE rb_hash_each_pair(VALUE hash)
Definition: hash.c:1807
VALUE rb_hash_has_key(VALUE hash, VALUE key)
Definition: hash.c:2105
#define ALLOCV_N(type, v, n)
Definition: ruby.h:1657
int rb_locale_encindex(void)
Definition: encoding.c:1352
int eql
Definition: hash.c:2154
static const struct st_hash_type objhash
Definition: hash.c:275
#define T_BIGNUM
Definition: ruby.h:501
VALUE rb_hash_ifnone(VALUE h)
Definition: hash.c:87
static int rb_any_cmp(VALUE a, VALUE b)
Definition: hash.c:100
static VALUE env_name_new(const char *name, const char *ptr)
Definition: hash.c:3143
VALUE rb_hash_reject(VALUE hash)
Definition: hash.c:1328
#define rb_str_new2
Definition: intern.h:857
VALUE rb_obj_alloc(VALUE)
Definition: object.c:1845
st_index_t(* hash)(ANYARGS)
Definition: st.h:63
VALUE new_key
Definition: hash.c:531
static VALUE reset_hash_type(VALUE arg)
Definition: hash.c:2539
#define ALLOCV_END(v)
Definition: ruby.h:1658
VALUE rb_hash_size(VALUE hash)
Definition: hash.c:1678
#define HASH_PROC_DEFAULT
Definition: internal.h:1124
static int reject_i(VALUE key, VALUE value, VALUE result)
Definition: hash.c:1305
static VALUE rb_hash_to_hash(VALUE hash)
Definition: hash.c:1974
VALUE hash
Definition: hash.c:342
#define RUBY_DTRACE_CREATE_HOOK(name, arg)
Definition: internal.h:1753
#define GET_ENVIRON(e)
Definition: hash.c:3076
static VALUE env_replace(VALUE env, VALUE hash)
Definition: hash.c:4281
static VALUE rb_hash_set_default(VALUE hash, VALUE ifnone)
Definition: hash.c:994
static long any_hash(VALUE a, st_index_t(*other_func)(VALUE))
Definition: hash.c:166
int rb_str_hash_cmp(VALUE, VALUE)
Definition: string.c:2995
void st_foreach_safe(st_table *table, int(*func)(ANYARGS), st_data_t a)
Definition: hash.c:327
static void rb_hash_modify_check(VALUE hash)
Definition: hash.c:468
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1758
static VALUE rb_hash_s_create(int argc, VALUE *argv, VALUE klass)
Definition: hash.c:662
static char ** origenviron
Definition: hash.c:3045
static int select_i(VALUE key, VALUE value, VALUE result)
Definition: hash.c:1398
#define RSTRING_LEN(str)
Definition: ruby.h:978
static VALUE rb_hash_index(VALUE hash, VALUE value)
Definition: hash.c:1100
VALUE rb_yield(VALUE)
Definition: vm_eval.c:1020
static VALUE env_index(VALUE dmy, VALUE value)
Definition: hash.c:4175
static VALUE rb_f_getenv(VALUE obj, VALUE name)
Definition: hash.c:3244
#define REALLOC_N(var, type, n)
Definition: ruby.h:1591
st_index_t st_hash(const void *ptr, size_t len, st_index_t h)
Definition: st.c:1698
#define TRUE
Definition: nkf.h:175
#define T_DATA
Definition: ruby.h:506
static int tbl_update(VALUE hash, VALUE key, tbl_update_func func, st_data_t optional_arg)
Definition: hash.c:540
static VALUE hash_foreach_ensure(VALUE hash)
Definition: hash.c:380
VALUE rb_obj_is_proc(VALUE)
Definition: proc.c:117
static VALUE env_inspect(void)
Definition: hash.c:3926
VALUE rb_mEnumerable
Definition: enum.c:18
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1440
st_table * rb_init_identtable(void)
Definition: hash.c:2816
static VALUE rb_hash_update(VALUE hash1, VALUE hash2)
Definition: hash.c:2427
VALUE rb_hash_delete(VALUE hash, VALUE key)
Definition: hash.c:1138
static VALUE rb_hash_key(VALUE hash, VALUE value)
Definition: hash.c:1086
int rb_eql(VALUE, VALUE)
Definition: object.c:97
#define RARRAY_PTR_USE(ary, ptr_name, expr)
Definition: ruby.h:1033
#define rb_enc_name(enc)
Definition: encoding.h:171
#define st_init_table
Definition: regint.h:176
VALUE rb_ary_delete(VALUE ary, VALUE item)
Definition: array.c:2992
#define env_name(s)
Definition: hash.c:3195
#define malloc
Definition: ripper.c:116
static VALUE env_has_key(VALUE env, VALUE key)
Definition: hash.c:4047
VALUE old_value
Definition: hash.c:534
VALUE rb_hash_values(VALUE hash)
Definition: hash.c:2061
#define RHASH_SIZE(hsh)
Definition: fbuffer.h:8
VALUE rb_hash_new(void)
Definition: hash.c:441
static const struct st_hash_type identhash
Definition: hash.c:299
int(* tbl_update_func)(st_data_t *, st_data_t *, st_data_t, int)
Definition: hash.c:537
static int each_pair_i_fast(VALUE key, VALUE value)
Definition: hash.c:1775
VALUE rb_check_hash_type(VALUE hash)
Definition: hash.c:736
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4309
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Definition: array.c:623
#define PRIsVALUE
Definition: ruby.h:135
unsigned long ID
Definition: ruby.h:86
#define PATH_ENV
Definition: defines.h:297
int rb_block_arity(void)
Definition: proc.c:1041
#define Qnil
Definition: ruby.h:438
#define BUILTIN_TYPE(x)
Definition: ruby.h:518
static int rb_hash_update_block_callback(st_data_t *key, st_data_t *value, struct update_arg *arg, int existing)
Definition: hash.c:2378
#define OBJ_TAINT(x)
Definition: ruby.h:1300
unsigned long VALUE
Definition: ruby.h:85
rb_encoding * rb_locale_encoding(void)
Definition: encoding.c:1370
static VALUE lookup2_call(VALUE arg)
Definition: hash.c:2527
static VALUE result
Definition: nkf.c:40
char * rb_w32_getenv(const char *)
Definition: win32.c:5149
VALUE rb_hash_clear(VALUE hash)
Definition: hash.c:1506
static VALUE env_values(void)
Definition: hash.c:3645
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
Definition: intern.h:236
static int clear_i(VALUE key, VALUE value, VALUE dummy)
Definition: hash.c:1489
#define RBASIC(obj)
Definition: ruby.h:1204
VALUE rb_hash_keep_if(VALUE hash)
Definition: hash.c:1479
static VALUE hash_alloc(VALUE klass)
Definition: hash.c:427
char * strchr(char *, char)
void rb_extend_object(VALUE obj, VALUE module)
Definition: eval.c:1429
#define STATIC_SYM_P(x)
Definition: ruby.h:380
static VALUE env_aset(VALUE obj, VALUE nm, VALUE val)
Definition: hash.c:3544
static VALUE env_to_a(void)
Definition: hash.c:3965
st_index_t st_values_check(st_table *table, st_data_t *values, st_index_t size, st_data_t never)
Definition: st.c:1608
static VALUE hash_equal(VALUE hash1, VALUE hash2, int eql)
Definition: hash.c:2188
static VALUE rb_hash_flatten(int argc, VALUE *argv, VALUE hash)
Definition: hash.c:2670
#define rb_enc_asciicompat(enc)
Definition: encoding.h:239
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
Definition: eval.c:923
VALUE flags
Definition: ruby.h:855
VALUE rb_str_ellipsize(VALUE, long)
Shortens str and adds three dots, an ellipsis, if it is longer than len characters.
Definition: string.c:9169
VALUE rb_proc_lambda_p(VALUE)
Definition: proc.c:255
static int each_key_i(VALUE key, VALUE value)
Definition: hash.c:1735
#define ST2FIX(h)
Definition: ruby.h:1579
rb_foreach_func * func
Definition: hash.c:343
VALUE key
Definition: hash.c:1190
void ruby_unsetenv(const char *name)
Definition: hash.c:3528
static VALUE env_enc_str_new(const char *ptr, long len, rb_encoding *enc)
Definition: hash.c:3097
static VALUE env_select(VALUE ehash)
Definition: hash.c:3812
#define CHAR_BIT
Definition: ruby.h:196
VALUE rb_hash_freeze(VALUE hash)
Definition: hash.c:76
#define FL_UNSET(x, f)
Definition: ruby.h:1292
NORETURN(static void no_new_key(void))
#define rb_funcallv
Definition: console.c:21
int rb_respond_to(VALUE, ID)
Definition: vm_method.c:1995
unsigned int uint32_t
Definition: sha2.h:101
register unsigned int len
Definition: zonetab.h:51
static VALUE env_none(void)
Definition: hash.c:3992
static VALUE env_size(void)
Definition: hash.c:4005
static int delete_if_i(VALUE key, VALUE value, VALUE hash)
Definition: hash.c:1242
static VALUE has_extra_methods(VALUE klass)
Definition: hash.c:52
#define getenv(name)
Definition: win32.c:71
static void * get_env_cstr(VALUE str, const char *name)
Definition: hash.c:3149
#define recur(fmt)
#define RSTRING_PTR(str)
Definition: ruby.h:982
static st_index_t rb_ident_hash(st_data_t n)
Definition: hash.c:283
#define ECONV_UNDEF_REPLACE
Definition: encoding.h:390
st_data_t arg
Definition: hash.c:529
VALUE rb_equal(VALUE, VALUE)
Definition: object.c:86
rb_encoding * rb_enc_get(VALUE obj)
Definition: encoding.c:860
static int inspect_i(VALUE key, VALUE value, VALUE str)
Definition: hash.c:1912
static VALUE rb_hash_compare_by_id(VALUE hash)
Definition: hash.c:2778
int size
Definition: encoding.c:57
VALUE rb_ident_hash_new(void)
Definition: hash.c:2808
VALUE rb_yield_values2(int n, const VALUE *argv)
Definition: vm_eval.c:1053
VALUE rb_hash_lookup2(VALUE hash, VALUE key, VALUE def)
Definition: hash.c:856
static VALUE to_hash(VALUE hash)
Definition: hash.c:730
#define INT2FIX(i)
Definition: ruby.h:232
#define UNLIMITED_ARGUMENTS
Definition: intern.h:44
VALUE rb_hash_compare_by_id_p(VALUE hash)
Definition: hash.c:2797
int st_shift(st_table *, st_data_t *, st_data_t *)
Definition: st.c:1315
#define RCLASS_SUPER(c)
Definition: classext.h:16
static VALUE hash_enum_size(VALUE hash, VALUE args, VALUE eobj)
Definition: hash.c:1251
#define RARRAY_AREF(a, i)
Definition: ruby.h:1040
int rb_path_check(const char *path)
Definition: file.c:5670
VALUE rb_check_convert_type(VALUE, int, const char *, const char *)
Definition: object.c:2643
VALUE rb_block_proc(void)
Definition: proc.c:787
static int flatten_i(VALUE key, VALUE val, VALUE ary)
Definition: hash.c:2642
st_data_t arg
Definition: hash.c:309
VALUE rb_str_buf_cat_ascii(VALUE, const char *)
Definition: string.c:2778
#define ANYARGS
Definition: defines.h:173
static union @153 seed
static VALUE env_to_hash(void)
Definition: hash.c:4190
VALUE rb_check_array_type(VALUE ary)
Definition: array.c:635
#define FL_SET_RAW(x, f)
Definition: ruby.h:1289
VALUE rb_hash_aref(VALUE hash, VALUE key)
Definition: hash.c:845
VALUE rb_obj_dig(int argc, VALUE *argv, VALUE self, VALUE notfound)
Definition: object.c:3219
static VALUE hash_dup(VALUE hash, VALUE klass, VALUE flags)
Definition: hash.c:447
#define st_cleanup_safe
Definition: regint.h:189
#define RHASH_UPDATE_ITER(h, iter_lev, key, func, a)
Definition: hash.c:563
static int hash_foreach_iter(st_data_t key, st_data_t value, st_data_t argp, int error)
Definition: hash.c:348
void rb_syserr_fail_str(int e, VALUE mesg)
Definition: error.c:2320
static VALUE env_enc_str_new_cstr(const char *ptr, rb_encoding *enc)
Definition: hash.c:3111
#define FL_WB_PROTECTED
Definition: ruby.h:1216
static VALUE env_values_at(int argc, VALUE *argv)
Definition: hash.c:3790
VALUE rb_check_string_type(VALUE)
Definition: string.c:2164
VALUE rb_any_to_s(VALUE)
Definition: object.c:500
VALUE rb_ary_includes(VALUE ary, VALUE item)
Definition: array.c:3958
static int shift_i_safe(VALUE key, VALUE value, VALUE arg)
Definition: hash.c:1195
static st_index_t rb_any_hash(VALUE a)
Definition: hash.c:213
#define LONG2FIX(i)
Definition: ruby.h:234
#define RTEST(v)
Definition: ruby.h:450
#define T_STRING
Definition: ruby.h:496
#define PRIuSIZE
Definition: ruby.h:177
#define OBJ_INFECT(x, s)
Definition: ruby.h:1304
void rb_check_safe_obj(VALUE)
Definition: safe.c:117
int st_foreach_check(st_table *, int(*)(ANYARGS), st_data_t, st_data_t)
st_index_t rb_hash_uint(st_index_t, st_index_t)
static VALUE rb_hash_invert(VALUE hash)
Definition: hash.c:2345
rb_encoding * rb_filesystem_encoding(void)
Definition: encoding.c:1385
int rb_method_basic_definition_p(VALUE, ID)
Definition: vm_method.c:1880
static rb_encoding * env_encoding_for(const char *name, const char *ptr)
Definition: hash.c:3132
static uint64_t key64_hash(uint64_t key, uint32_t seed)
Definition: hash.c:245
static VALUE rb_hash_hash(VALUE hash)
Definition: hash.c:2295
static VALUE rb_hash_ge(VALUE hash, VALUE other)
Definition: hash.c:2978
static VALUE recursive_eql(VALUE hash, VALUE dt, int recur)
Definition: hash.c:2175
VALUE rb_func_proc_new(rb_block_call_func_t func, VALUE val)
Definition: proc.c:676
const struct st_hash_type * type
Definition: st.h:84
int(* compare)(ANYARGS)
Definition: st.h:62
static int any_p_i(VALUE key, VALUE value, VALUE arg)
Definition: hash.c:2828
void rb_gc_writebarrier_remember(VALUE obj)
Definition: gc.c:5967
Definition: st.h:99
static int rassoc_i(VALUE key, VALUE val, VALUE arg)
Definition: hash.c:2606
#define SafeStringValue(v)
Definition: ruby.h:574
#define st_insert
Definition: regint.h:184
st_table * tbl
Definition: hash.c:307
st_index_t st_keys_check(st_table *table, st_data_t *keys, st_index_t size, st_data_t never)
Definition: st.c:1571
VALUE rb_hash_set_default_proc(VALUE hash, VALUE proc)
Definition: hash.c:1039
VALUE hash
Definition: hash.c:759
static VALUE rb_hash_merge(VALUE hash1, VALUE hash2)
Definition: hash.c:2515
static VALUE rb_hash_delete_m(VALUE hash, VALUE key)
Definition: hash.c:1169
const char * name
Definition: nkf.c:208
#define FL_SET(x, f)
Definition: ruby.h:1290
struct RBasic basic
Definition: internal.h:556
VALUE rb_hash_values_at(int argc, VALUE *argv, VALUE hash)
Definition: hash.c:1358
static int hash_aset(st_data_t *key, st_data_t *val, struct update_arg *arg, int existing)
Definition: hash.c:1522
VALUE rb_str_new_frozen(VALUE)
Definition: string.c:1123
#define st_free_table
Definition: regint.h:188
#define HASH_DELETED
Definition: internal.h:1123
VALUE rb_inspect(VALUE)
Definition: object.c:519
VALUE arg
Definition: hash.c:344
static int key_i(VALUE key, VALUE value, VALUE arg)
Definition: hash.c:1060
#define ST_DATA_COMPATIBLE_P(type)
Definition: st.h:72
int st_foreach_func(st_data_t, st_data_t, st_data_t)
Definition: hash.c:304
VALUE rb_env_clear(void)
Definition: hash.c:3891
static int env_replace_i(VALUE key, VALUE val, VALUE keys)
Definition: hash.c:4264
void st_clear(st_table *)
Definition: st.c:653
rb_hash_update_func * func
Definition: hash.c:2443
VALUE rb_hash_delete_if(VALUE hash)
Definition: hash.c:1272
#define rb_check_frozen(obj)
Definition: intern.h:276
void ruby_register_rollback_func_for_ensure(VALUE(*ensure_func)(ANYARGS), VALUE(*rollback_func)(ANYARGS))
Definition: cont.c:977
int rb_proc_arity(VALUE)
Definition: proc.c:1009
static VALUE env_empty_p(void)
Definition: hash.c:4024
VALUE rb_hash_assoc(VALUE hash, VALUE key)
Definition: hash.c:2573
static int to_a_i(VALUE key, VALUE value, VALUE ary)
Definition: hash.c:1882
static VALUE rb_hash_compact(VALUE hash)
Definition: hash.c:2727
VALUE rb_obj_freeze(VALUE)
Definition: object.c:1111
void rb_copy_generic_ivar(VALUE, VALUE)
Definition: variable.c:1549
#define SPECIAL_CONST_P(x)
Definition: ruby.h:1249
void void xfree(void *)
VALUE rb_tainted_str_new(const char *, long)
Definition: string.c:853
#define RHASH_EMPTY_P(h)
Definition: ruby.h:1067
static VALUE hash_recursive(VALUE obj, VALUE arg, int recurse)
Definition: hash.c:119
int rb_enc_str_asciionly_p(VALUE)
Definition: string.c:640
static VALUE rb_hash_le(VALUE hash, VALUE other)
Definition: hash.c:2936
#define SYMBOL_P(x)
Definition: ruby.h:382
static VALUE hash_foreach_ensure_rollback(VALUE hash)
Definition: hash.c:373
static VALUE rb_hash_compact_bang(VALUE hash)
Definition: hash.c:2749
#define env
static VALUE env_reject_bang(VALUE ehash)
Definition: hash.c:3741
#define st_copy
Definition: regint.h:190
#define NULL
Definition: _sdbm.c:102
static const uint64_t prime2
Definition: hash.c:224
static VALUE env_fetch(int argc, VALUE *argv)
Definition: hash.c:3271
#define FIX2LONG(x)
Definition: ruby.h:363
#define Qundef
Definition: ruby.h:439
VALUE rb_hash_select(VALUE hash)
Definition: hash.c:1421
static VALUE env_has_value(VALUE dmy, VALUE obj)
Definition: hash.c:4082
static VALUE env_update(VALUE env, VALUE hash)
Definition: hash.c:4319
static int set_if_not_nil(VALUE key, VALUE value, VALUE hash)
Definition: hash.c:2706
#define RGENGC_WB_PROTECTED_HASH
Definition: ruby.h:774
st_index_t num_entries
Definition: st.h:86
#define st_delete_safe
Definition: regint.h:183
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1515
#define ruby_verbose
Definition: ruby.h:1792
void ruby_setenv(const char *name, const char *value)
Definition: hash.c:3384
static int delete_if_nil(VALUE key, VALUE value, VALUE hash)
Definition: hash.c:2697
void rb_warn(const char *fmt,...)
Definition: error.c:221
free(psz)
VALUE rb_eArgError
Definition: error.c:763
static VALUE rb_hash_transform_values(VALUE hash)
Definition: hash.c:1842
VALUE hash
Definition: hash.c:530
static struct st_table * hash_tbl(VALUE hash)
Definition: hash.c:474
st_index_t rb_hash_start(st_index_t)
Definition: random.c:1496
static int rb_hash_update_callback(st_data_t *key, st_data_t *value, struct update_arg *arg, int existing)
Definition: hash.c:2354
#define RB_OBJ_WRITE(a, slot, b)
Definition: ruby.h:1437
VALUE rb_hash_rassoc(VALUE hash, VALUE obj)
Definition: hash.c:2631
static VALUE rb_hash_to_a(VALUE hash)
Definition: hash.c:1900
char ** argv
Definition: ruby.c:184
static VALUE rb_hash_initialize(int argc, VALUE *argv, VALUE hash)
Definition: hash.c:622
static int rb_hash_update_func_callback(st_data_t *key, st_data_t *value, struct update_arg *arg, int existing)
Definition: hash.c:2447
VALUE rb_hash_fetch_values(int argc, VALUE *argv, VALUE hash)
Definition: hash.c:1386
static VALUE env_str_new(const char *ptr, long len)
Definition: hash.c:3117
static VALUE rb_hash_default_proc(VALUE hash)
Definition: hash.c:1017
#define L(x)
Definition: asm.h:125
static VALUE rb_hash_inspect(VALUE hash)
Definition: hash.c:1959
VALUE hash
Definition: hash.c:2441
void rb_ary_set_len(VALUE ary, long len)
Definition: array.c:1627
static void set_proc_default(VALUE hash, VALUE proc)
Definition: hash.c:571
VALUE rb_obj_class(VALUE)
Definition: object.c:229
VALUE rb_hash_dig(int argc, VALUE *argv, VALUE self)
Definition: hash.c:2893