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