Ruby  2.0.0p648(2015-12-16revision53162)
hash.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  hash.c -
4 
5  $Author: usa $
6  created at: Mon Nov 22 18:51:18 JST 1993
7 
8  Copyright (C) 1993-2007 Yukihiro Matsumoto
9  Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10  Copyright (C) 2000 Information-technology Promotion Agency, Japan
11 
12 **********************************************************************/
13 
14 #include "ruby/ruby.h"
15 #include "ruby/st.h"
16 #include "ruby/util.h"
17 #include "ruby/encoding.h"
18 #include "internal.h"
19 #include <errno.h>
20 #include "probes.h"
21 
22 #ifdef __APPLE__
23 # ifdef HAVE_CRT_EXTERNS_H
24 # include <crt_externs.h>
25 # else
26 # include "missing/crt_externs.h"
27 # endif
28 #endif
29 
31 
32 #define HASH_DELETED FL_USER1
33 #define HASH_PROC_DEFAULT FL_USER2
34 
35 VALUE
37 {
38  return rb_obj_freeze(hash);
39 }
40 
42 
43 static VALUE envtbl;
45 
46 static int
48 {
49  if (a == b) return 0;
50  if (FIXNUM_P(a) && FIXNUM_P(b)) {
51  return a != b;
52  }
53  if (RB_TYPE_P(a, T_STRING) && RBASIC(a)->klass == rb_cString &&
54  RB_TYPE_P(b, T_STRING) && RBASIC(b)->klass == rb_cString) {
55  return rb_str_hash_cmp(a, b);
56  }
57  if (a == Qundef || b == Qundef) return -1;
58  if (SYMBOL_P(a) && SYMBOL_P(b)) {
59  return a != b;
60  }
61 
62  return !rb_eql(a, b);
63 }
64 
65 VALUE
67 {
68  VALUE hval = rb_funcall(obj, id_hash, 0);
69  retry:
70  switch (TYPE(hval)) {
71  case T_FIXNUM:
72  return hval;
73 
74  case T_BIGNUM:
75  return LONG2FIX(((long*)(RBIGNUM_DIGITS(hval)))[0]);
76 
77  default:
78  hval = rb_to_int(hval);
79  goto retry;
80  }
81 }
82 
83 static st_index_t
85 {
86  VALUE hval;
87  st_index_t hnum;
88 
89  if (SPECIAL_CONST_P(a)) {
90  if (a == Qundef) return 0;
92  }
93  else if (BUILTIN_TYPE(a) == T_STRING) {
94  hnum = rb_str_hash(a);
95  }
96  else {
97  hval = rb_hash(a);
98  hnum = FIX2LONG(hval);
99  }
100  hnum <<= 1;
101  return (st_index_t)RSHIFT(hnum, 1);
102 }
103 
104 static const struct st_hash_type objhash = {
105  rb_any_cmp,
106  rb_any_hash,
107 };
108 
109 extern const struct st_hash_type st_hashtype_num;
110 #define identhash st_hashtype_num
111 
113 
118 };
119 
120 static int
122 {
123  int status;
124 
125  status = (*arg->func)(key, value, arg->arg);
126  if (status == ST_CONTINUE) {
127  return ST_CHECK;
128  }
129  return status;
130 }
131 
132 void
134 {
135  struct foreach_safe_arg arg;
136 
137  arg.tbl = table;
138  arg.func = (st_foreach_func *)func;
139  arg.arg = a;
140  if (st_foreach_check(table, foreach_safe_i, (st_data_t)&arg, 0)) {
141  rb_raise(rb_eRuntimeError, "hash modified during iteration");
142  }
143 }
144 
146 
151 };
152 
153 static int
155 {
156  struct hash_foreach_arg *arg = (struct hash_foreach_arg *)argp;
157  int status;
158  st_table *tbl;
159 
160  tbl = RHASH(arg->hash)->ntbl;
161  status = (*arg->func)((VALUE)key, (VALUE)value, arg->arg);
162  if (RHASH(arg->hash)->ntbl != tbl) {
163  rb_raise(rb_eRuntimeError, "rehash occurred during iteration");
164  }
165  switch (status) {
166  case ST_DELETE:
167  FL_SET(arg->hash, HASH_DELETED);
168  return ST_DELETE;
169  case ST_CONTINUE:
170  break;
171  case ST_STOP:
172  return ST_STOP;
173  }
174  return ST_CHECK;
175 }
176 
177 static VALUE
179 {
180  if (--RHASH_ITER_LEV(hash) == 0) {
181  if (FL_TEST(hash, HASH_DELETED)) {
182  st_cleanup_safe(RHASH(hash)->ntbl, (st_data_t)Qundef);
183  FL_UNSET(hash, HASH_DELETED);
184  }
185  }
186  return 0;
187 }
188 
189 static VALUE
191 {
192  VALUE hash = ((struct hash_foreach_arg *)arg)->hash;
194  rb_raise(rb_eRuntimeError, "hash modified during iteration");
195  }
196  return Qnil;
197 }
198 
199 void
201 {
202  struct hash_foreach_arg arg;
203 
204  if (!RHASH(hash)->ntbl)
205  return;
206  RHASH_ITER_LEV(hash)++;
207  arg.hash = hash;
208  arg.func = (rb_foreach_func *)func;
209  arg.arg = farg;
211 }
212 
213 static VALUE
215 {
216  NEWOBJ_OF(hash, struct RHash, klass, T_HASH);
217 
218  RHASH_IFNONE(hash) = Qnil;
219 
220  return (VALUE)hash;
221 }
222 
223 static VALUE
225 {
228  }
229 
230  return hash_alloc(klass);
231 }
232 
233 VALUE
235 {
236  return hash_alloc(rb_cHash);
237 }
238 
239 VALUE
241 {
242  NEWOBJ_OF(ret, struct RHash,
243  rb_obj_class(hash),
244  (RBASIC(hash)->flags)&(T_MASK|FL_EXIVAR|FL_TAINT|FL_UNTRUSTED));
245  if (FL_TEST((hash), FL_EXIVAR))
246  rb_copy_generic_ivar((VALUE)(ret),(VALUE)(hash));
247 
248  if (!RHASH_EMPTY_P(hash))
249  ret->ntbl = st_copy(RHASH(hash)->ntbl);
250  if (FL_TEST(hash, HASH_PROC_DEFAULT)) {
252  }
253  RHASH_IFNONE(ret) = RHASH_IFNONE(hash);
254  return (VALUE)ret;
255 }
256 
257 static void
259 {
260  rb_check_frozen(hash);
261  if (!OBJ_UNTRUSTED(hash) && rb_safe_level() >= 4)
262  rb_raise(rb_eSecurityError, "Insecure: can't modify hash");
263 }
264 
265 struct st_table *
267 {
268  if (!RHASH(hash)->ntbl) {
269  RHASH(hash)->ntbl = st_init_table(&objhash);
270  }
271  return RHASH(hash)->ntbl;
272 }
273 
274 static void
276 {
277  rb_hash_modify_check(hash);
278  rb_hash_tbl(hash);
279 }
280 
281 NORETURN(static void no_new_key(void));
282 static void
284 {
285  rb_raise(rb_eRuntimeError, "can't add a new key into hash during iteration");
286 }
287 
288 #define NOINSERT_UPDATE_CALLBACK(func) \
289 int \
290 func##_noinsert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
291 { \
292  if (!existing) no_new_key(); \
293  return func(key, val, arg, existing); \
294 }
295 
296 #define UPDATE_CALLBACK(iter_lev, func) ((iter_lev) > 0 ? func##_noinsert : func)
297 
298 #define RHASH_UPDATE_ITER(hash, iter_lev, key, func, arg) \
299  st_update(RHASH(hash)->ntbl, (st_data_t)(key), \
300  UPDATE_CALLBACK((iter_lev), func), \
301  (st_data_t)(arg))
302 #define RHASH_UPDATE(hash, key, func, arg) \
303  RHASH_UPDATE_ITER(hash, RHASH_ITER_LEV(hash), key, func, arg)
304 
305 static void
307 {
308  int n = rb_proc_arity(proc);
309 
310  if (rb_proc_lambda_p(proc) && n != 2 && (n >= 0 || n < -3)) {
311  if (n < 0) n = -n-1;
312  rb_raise(rb_eTypeError, "default_proc takes two arguments (2 for %d)", n);
313  }
314 }
315 
316 /*
317  * call-seq:
318  * Hash.new -> new_hash
319  * Hash.new(obj) -> new_hash
320  * Hash.new {|hash, key| block } -> new_hash
321  *
322  * Returns a new, empty hash. If this hash is subsequently accessed by
323  * a key that doesn't correspond to a hash entry, the value returned
324  * depends on the style of <code>new</code> used to create the hash. In
325  * the first form, the access returns <code>nil</code>. If
326  * <i>obj</i> is specified, this single object will be used for
327  * all <em>default values</em>. If a block is specified, it will be
328  * called with the hash object and the key, and should return the
329  * default value. It is the block's responsibility to store the value
330  * in the hash if required.
331  *
332  * h = Hash.new("Go Fish")
333  * h["a"] = 100
334  * h["b"] = 200
335  * h["a"] #=> 100
336  * h["c"] #=> "Go Fish"
337  * # The following alters the single default object
338  * h["c"].upcase! #=> "GO FISH"
339  * h["d"] #=> "GO FISH"
340  * h.keys #=> ["a", "b"]
341  *
342  * # While this creates a new default object each time
343  * h = Hash.new { |hash, key| hash[key] = "Go Fish: #{key}" }
344  * h["c"] #=> "Go Fish: c"
345  * h["c"].upcase! #=> "GO FISH: C"
346  * h["d"] #=> "Go Fish: d"
347  * h.keys #=> ["c", "d"]
348  *
349  */
350 
351 static VALUE
353 {
354  VALUE ifnone;
355 
356  rb_hash_modify(hash);
357  if (rb_block_given_p()) {
358  rb_check_arity(argc, 0, 0);
359  ifnone = rb_block_proc();
360  default_proc_arity_check(ifnone);
361  RHASH_IFNONE(hash) = ifnone;
362  FL_SET(hash, HASH_PROC_DEFAULT);
363  }
364  else {
365  rb_scan_args(argc, argv, "01", &ifnone);
366  RHASH_IFNONE(hash) = ifnone;
367  }
368 
369  return hash;
370 }
371 
372 /*
373  * call-seq:
374  * Hash[ key, value, ... ] -> new_hash
375  * Hash[ [ [key, value], ... ] ] -> new_hash
376  * Hash[ object ] -> new_hash
377  *
378  * Creates a new hash populated with the given objects. Equivalent to
379  * the literal <code>{ <i>key</i> => <i>value</i>, ... }</code>. In the first
380  * form, keys and values occur in pairs, so there must be an even number of arguments.
381  * The second and third form take a single argument which is either
382  * an array of key-value pairs or an object convertible to a hash.
383  *
384  * Hash["a", 100, "b", 200] #=> {"a"=>100, "b"=>200}
385  * Hash[ [ ["a", 100], ["b", 200] ] ] #=> {"a"=>100, "b"=>200}
386  * Hash["a" => 100, "b" => 200] #=> {"a"=>100, "b"=>200}
387  */
388 
389 static VALUE
391 {
392  VALUE hash, tmp;
393  int i;
394 
395  if (argc == 1) {
396  tmp = rb_hash_s_try_convert(Qnil, argv[0]);
397  if (!NIL_P(tmp)) {
398  hash = hash_alloc(klass);
399  if (RHASH(tmp)->ntbl) {
400  RHASH(hash)->ntbl = st_copy(RHASH(tmp)->ntbl);
401  }
402  return hash;
403  }
404 
405  tmp = rb_check_array_type(argv[0]);
406  if (!NIL_P(tmp)) {
407  long i;
408 
409  hash = hash_alloc(klass);
410  for (i = 0; i < RARRAY_LEN(tmp); ++i) {
411  VALUE e = RARRAY_PTR(tmp)[i];
413  VALUE key, val = Qnil;
414 
415  if (NIL_P(v)) {
416 #if 0 /* refix in the next release */
417  rb_raise(rb_eArgError, "wrong element type %s at %ld (expected array)",
418  rb_builtin_class_name(e), i);
419 
420 #else
421  rb_warn("wrong element type %s at %ld (expected array)",
422  rb_builtin_class_name(e), i);
423  rb_warn("ignoring wrong elements is deprecated, remove them explicitly");
424  rb_warn("this causes ArgumentError in the next release");
425  continue;
426 #endif
427  }
428  switch (RARRAY_LEN(v)) {
429  default:
430  rb_raise(rb_eArgError, "invalid number of elements (%ld for 1..2)",
431  RARRAY_LEN(v));
432  case 2:
433  val = RARRAY_PTR(v)[1];
434  case 1:
435  key = RARRAY_PTR(v)[0];
436  rb_hash_aset(hash, key, val);
437  }
438  }
439  return hash;
440  }
441  }
442  if (argc % 2 != 0) {
443  rb_raise(rb_eArgError, "odd number of arguments for Hash");
444  }
445 
446  hash = hash_alloc(klass);
447  for (i=0; i<argc; i+=2) {
448  rb_hash_aset(hash, argv[i], argv[i + 1]);
449  }
450 
451  return hash;
452 }
453 
454 static VALUE
456 {
457  return rb_convert_type(hash, T_HASH, "Hash", "to_hash");
458 }
459 
460 VALUE
462 {
463  return rb_check_convert_type(hash, T_HASH, "Hash", "to_hash");
464 }
465 
466 /*
467  * call-seq:
468  * Hash.try_convert(obj) -> hash or nil
469  *
470  * Try to convert <i>obj</i> into a hash, using to_hash method.
471  * Returns converted hash or nil if <i>obj</i> cannot be converted
472  * for any reason.
473  *
474  * Hash.try_convert({1=>2}) # => {1=>2}
475  * Hash.try_convert("1=>2") # => nil
476  */
477 static VALUE
479 {
480  return rb_check_hash_type(hash);
481 }
482 
483 struct rehash_arg {
486 };
487 
488 static int
490 {
491  st_table *tbl = (st_table *)arg;
492 
493  st_insert(tbl, (st_data_t)key, (st_data_t)value);
494  return ST_CONTINUE;
495 }
496 
497 /*
498  * call-seq:
499  * hsh.rehash -> hsh
500  *
501  * Rebuilds the hash based on the current hash values for each key. If
502  * values of key objects have changed since they were inserted, this
503  * method will reindex <i>hsh</i>. If <code>Hash#rehash</code> is
504  * called while an iterator is traversing the hash, an
505  * <code>RuntimeError</code> will be raised in the iterator.
506  *
507  * a = [ "a", "b" ]
508  * c = [ "c", "d" ]
509  * h = { a => 100, c => 300 }
510  * h[a] #=> 100
511  * a[0] = "z"
512  * h[a] #=> nil
513  * h.rehash #=> {["z", "b"]=>100, ["c", "d"]=>300}
514  * h[a] #=> 100
515  */
516 
517 static VALUE
519 {
520  VALUE tmp;
521  st_table *tbl;
522 
523  if (RHASH_ITER_LEV(hash) > 0) {
524  rb_raise(rb_eRuntimeError, "rehash during iteration");
525  }
526  rb_hash_modify_check(hash);
527  if (!RHASH(hash)->ntbl)
528  return hash;
529  tmp = hash_alloc(0);
530  tbl = st_init_table_with_size(RHASH(hash)->ntbl->type, RHASH(hash)->ntbl->num_entries);
531  RHASH(tmp)->ntbl = tbl;
532 
534  st_free_table(RHASH(hash)->ntbl);
535  RHASH(hash)->ntbl = tbl;
536  RHASH(tmp)->ntbl = 0;
537 
538  return hash;
539 }
540 
541 static VALUE
543 {
545  VALUE ifnone = RHASH_IFNONE(hash);
546  if (!FL_TEST(hash, HASH_PROC_DEFAULT)) return ifnone;
547  if (key == Qundef) return Qnil;
548  return rb_funcall(ifnone, id_yield, 2, hash, key);
549  }
550  else {
551  return rb_funcall(hash, id_default, 1, key);
552  }
553 }
554 
555 /*
556  * call-seq:
557  * hsh[key] -> value
558  *
559  * Element Reference---Retrieves the <i>value</i> object corresponding
560  * to the <i>key</i> object. If not found, returns the default value (see
561  * <code>Hash::new</code> for details).
562  *
563  * h = { "a" => 100, "b" => 200 }
564  * h["a"] #=> 100
565  * h["c"] #=> nil
566  *
567  */
568 
569 VALUE
571 {
572  st_data_t val;
573 
574  if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key, &val)) {
575  return hash_default_value(hash, key);
576  }
577  return (VALUE)val;
578 }
579 
580 VALUE
582 {
583  st_data_t val;
584 
585  if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key, &val)) {
586  return def; /* without Hash#default */
587  }
588  return (VALUE)val;
589 }
590 
591 VALUE
593 {
594  return rb_hash_lookup2(hash, key, Qnil);
595 }
596 
597 /*
598  * call-seq:
599  * hsh.fetch(key [, default] ) -> obj
600  * hsh.fetch(key) {| key | block } -> obj
601  *
602  * Returns a value from the hash for the given key. If the key can't be
603  * found, there are several options: With no other arguments, it will
604  * raise an <code>KeyError</code> exception; if <i>default</i> is
605  * given, then that will be returned; if the optional code block is
606  * specified, then that will be run and its result returned.
607  *
608  * h = { "a" => 100, "b" => 200 }
609  * h.fetch("a") #=> 100
610  * h.fetch("z", "go fish") #=> "go fish"
611  * h.fetch("z") { |el| "go fish, #{el}"} #=> "go fish, z"
612  *
613  * The following example shows that an exception is raised if the key
614  * is not found and a default value is not supplied.
615  *
616  * h = { "a" => 100, "b" => 200 }
617  * h.fetch("z")
618  *
619  * <em>produces:</em>
620  *
621  * prog.rb:2:in `fetch': key not found (KeyError)
622  * from prog.rb:2
623  *
624  */
625 
626 static VALUE
628 {
629  VALUE key, if_none;
630  st_data_t val;
631  long block_given;
632 
633  rb_scan_args(argc, argv, "11", &key, &if_none);
634 
635  block_given = rb_block_given_p();
636  if (block_given && argc == 2) {
637  rb_warn("block supersedes default value argument");
638  }
639  if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key, &val)) {
640  if (block_given) return rb_yield(key);
641  if (argc == 1) {
642  volatile VALUE desc = rb_protect(rb_inspect, key, 0);
643  if (NIL_P(desc)) {
644  desc = rb_any_to_s(key);
645  }
646  desc = rb_str_ellipsize(desc, 65);
647  rb_raise(rb_eKeyError, "key not found: %s", RSTRING_PTR(desc));
648  }
649  return if_none;
650  }
651  return (VALUE)val;
652 }
653 
654 VALUE
656 {
657  return rb_hash_fetch_m(1, &key, hash);
658 }
659 
660 /*
661  * call-seq:
662  * hsh.default(key=nil) -> obj
663  *
664  * Returns the default value, the value that would be returned by
665  * <i>hsh</i>[<i>key</i>] if <i>key</i> did not exist in <i>hsh</i>.
666  * See also <code>Hash::new</code> and <code>Hash#default=</code>.
667  *
668  * h = Hash.new #=> {}
669  * h.default #=> nil
670  * h.default(2) #=> nil
671  *
672  * h = Hash.new("cat") #=> {}
673  * h.default #=> "cat"
674  * h.default(2) #=> "cat"
675  *
676  * h = Hash.new {|h,k| h[k] = k.to_i*10} #=> {}
677  * h.default #=> nil
678  * h.default(2) #=> 20
679  */
680 
681 static VALUE
683 {
684  VALUE key, ifnone;
685 
686  rb_scan_args(argc, argv, "01", &key);
687  ifnone = RHASH_IFNONE(hash);
688  if (FL_TEST(hash, HASH_PROC_DEFAULT)) {
689  if (argc == 0) return Qnil;
690  return rb_funcall(ifnone, id_yield, 2, hash, key);
691  }
692  return ifnone;
693 }
694 
695 /*
696  * call-seq:
697  * hsh.default = obj -> obj
698  *
699  * Sets the default value, the value returned for a key that does not
700  * exist in the hash. It is not possible to set the default to a
701  * <code>Proc</code> that will be executed on each key lookup.
702  *
703  * h = { "a" => 100, "b" => 200 }
704  * h.default = "Go fish"
705  * h["a"] #=> 100
706  * h["z"] #=> "Go fish"
707  * # This doesn't do what you might hope...
708  * h.default = proc do |hash, key|
709  * hash[key] = key + key
710  * end
711  * h[2] #=> #<Proc:0x401b3948@-:6>
712  * h["cat"] #=> #<Proc:0x401b3948@-:6>
713  */
714 
715 static VALUE
717 {
718  rb_hash_modify_check(hash);
719  RHASH_IFNONE(hash) = ifnone;
721  return ifnone;
722 }
723 
724 /*
725  * call-seq:
726  * hsh.default_proc -> anObject
727  *
728  * If <code>Hash::new</code> was invoked with a block, return that
729  * block, otherwise return <code>nil</code>.
730  *
731  * h = Hash.new {|h,k| h[k] = k*k } #=> {}
732  * p = h.default_proc #=> #<Proc:0x401b3d08@-:1>
733  * a = [] #=> []
734  * p.call(a, 2)
735  * a #=> [nil, nil, 4]
736  */
737 
738 
739 static VALUE
741 {
742  if (FL_TEST(hash, HASH_PROC_DEFAULT)) {
743  return RHASH_IFNONE(hash);
744  }
745  return Qnil;
746 }
747 
748 /*
749  * call-seq:
750  * hsh.default_proc = proc_obj or nil
751  *
752  * Sets the default proc to be executed on each failed key lookup.
753  *
754  * h.default_proc = proc do |hash, key|
755  * hash[key] = key + key
756  * end
757  * h[2] #=> 4
758  * h["cat"] #=> "catcat"
759  */
760 
761 static VALUE
763 {
764  VALUE b;
765 
766  rb_hash_modify_check(hash);
767  if (NIL_P(proc)) {
769  RHASH_IFNONE(hash) = proc;
770  return proc;
771  }
772  b = rb_check_convert_type(proc, T_DATA, "Proc", "to_proc");
773  if (NIL_P(b) || !rb_obj_is_proc(b)) {
775  "wrong default_proc type %s (expected Proc)",
776  rb_obj_classname(proc));
777  }
778  proc = b;
780  RHASH_IFNONE(hash) = proc;
781  FL_SET(hash, HASH_PROC_DEFAULT);
782  return proc;
783 }
784 
785 static int
786 key_i(VALUE key, VALUE value, VALUE arg)
787 {
788  VALUE *args = (VALUE *)arg;
789 
790  if (rb_equal(value, args[0])) {
791  args[1] = key;
792  return ST_STOP;
793  }
794  return ST_CONTINUE;
795 }
796 
797 /*
798  * call-seq:
799  * hsh.key(value) -> key
800  *
801  * Returns the key of an occurrence of a given value. If the value is
802  * not found, returns <code>nil</code>.
803  *
804  * h = { "a" => 100, "b" => 200, "c" => 300, "d" => 300 }
805  * h.key(200) #=> "b"
806  * h.key(300) #=> "c"
807  * h.key(999) #=> nil
808  *
809  */
810 
811 static VALUE
812 rb_hash_key(VALUE hash, VALUE value)
813 {
814  VALUE args[2];
815 
816  args[0] = value;
817  args[1] = Qnil;
818 
819  rb_hash_foreach(hash, key_i, (VALUE)args);
820 
821  return args[1];
822 }
823 
824 /* :nodoc: */
825 static VALUE
827 {
828  rb_warn("Hash#index is deprecated; use Hash#key");
829  return rb_hash_key(hash, value);
830 }
831 
832 static VALUE
834 {
835  st_data_t ktmp = (st_data_t)key, val;
836 
837  if (!RHASH(hash)->ntbl)
838  return Qundef;
839  if (RHASH_ITER_LEV(hash) > 0) {
840  if (st_delete_safe(RHASH(hash)->ntbl, &ktmp, &val, (st_data_t)Qundef)) {
841  FL_SET(hash, HASH_DELETED);
842  return (VALUE)val;
843  }
844  }
845  else if (st_delete(RHASH(hash)->ntbl, &ktmp, &val))
846  return (VALUE)val;
847  return Qundef;
848 }
849 
850 /*
851  * call-seq:
852  * hsh.delete(key) -> value
853  * hsh.delete(key) {| key | block } -> value
854  *
855  * Deletes the key-value pair and returns the value from <i>hsh</i> whose
856  * key is equal to <i>key</i>. If the key is not found, returns the
857  * <em>default value</em>. If the optional code block is given and the
858  * key is not found, pass in the key and return the result of
859  * <i>block</i>.
860  *
861  * h = { "a" => 100, "b" => 200 }
862  * h.delete("a") #=> 100
863  * h.delete("z") #=> nil
864  * h.delete("z") { |el| "#{el} not found" } #=> "z not found"
865  *
866  */
867 
868 VALUE
870 {
871  VALUE val;
872 
873  rb_hash_modify_check(hash);
874  val = rb_hash_delete_key(hash, key);
875  if (val != Qundef) return val;
876  if (rb_block_given_p()) {
877  return rb_yield(key);
878  }
879  return Qnil;
880 }
881 
882 struct shift_var {
885 };
886 
887 static int
889 {
890  struct shift_var *var = (struct shift_var *)arg;
891 
892  if (var->key != Qundef) return ST_STOP;
893  var->key = key;
894  var->val = value;
895  return ST_DELETE;
896 }
897 
898 static int
900 {
901  struct shift_var *var = (struct shift_var *)arg;
902 
903  var->key = key;
904  var->val = value;
905  return ST_STOP;
906 }
907 
908 /*
909  * call-seq:
910  * hsh.shift -> anArray or obj
911  *
912  * Removes a key-value pair from <i>hsh</i> and returns it as the
913  * two-item array <code>[</code> <i>key, value</i> <code>]</code>, or
914  * the hash's default value if the hash is empty.
915  *
916  * h = { 1 => "a", 2 => "b", 3 => "c" }
917  * h.shift #=> [1, "a"]
918  * h #=> {2=>"b", 3=>"c"}
919  */
920 
921 static VALUE
923 {
924  struct shift_var var;
925 
926  rb_hash_modify_check(hash);
927  if (RHASH(hash)->ntbl) {
928  var.key = Qundef;
930  (VALUE)&var);
931 
932  if (var.key != Qundef) {
933  if (RHASH_ITER_LEV(hash) > 0) {
934  rb_hash_delete_key(hash, var.key);
935  }
936  return rb_assoc_new(var.key, var.val);
937  }
938  }
939  return hash_default_value(hash, Qnil);
940 }
941 
942 static int
944 {
945  if (RTEST(rb_yield_values(2, key, value))) {
946  rb_hash_delete_key(hash, key);
947  }
948  return ST_CONTINUE;
949 }
950 
951 static VALUE rb_hash_size(VALUE hash);
952 
953 /*
954  * call-seq:
955  * hsh.delete_if {| key, value | block } -> hsh
956  * hsh.delete_if -> an_enumerator
957  *
958  * Deletes every key-value pair from <i>hsh</i> for which <i>block</i>
959  * evaluates to <code>true</code>.
960  *
961  * If no block is given, an enumerator is returned instead.
962  *
963  * h = { "a" => 100, "b" => 200, "c" => 300 }
964  * h.delete_if {|key, value| key >= "b" } #=> {"a"=>100}
965  *
966  */
967 
968 VALUE
970 {
972  rb_hash_modify_check(hash);
973  if (RHASH(hash)->ntbl)
974  rb_hash_foreach(hash, delete_if_i, hash);
975  return hash;
976 }
977 
978 /*
979  * call-seq:
980  * hsh.reject! {| key, value | block } -> hsh or nil
981  * hsh.reject! -> an_enumerator
982  *
983  * Equivalent to <code>Hash#delete_if</code>, but returns
984  * <code>nil</code> if no changes were made.
985  */
986 
987 VALUE
989 {
990  st_index_t n;
991 
993  rb_hash_modify(hash);
994  if (!RHASH(hash)->ntbl)
995  return Qnil;
996  n = RHASH(hash)->ntbl->num_entries;
997  rb_hash_foreach(hash, delete_if_i, hash);
998  if (n == RHASH(hash)->ntbl->num_entries) return Qnil;
999  return hash;
1000 }
1001 
1002 /*
1003  * call-seq:
1004  * hsh.reject {| key, value | block } -> a_hash
1005  * hsh.reject -> an_enumerator
1006  *
1007  * Same as <code>Hash#delete_if</code>, but works on (and returns) a
1008  * copy of the <i>hsh</i>. Equivalent to
1009  * <code><i>hsh</i>.dup.delete_if</code>.
1010  *
1011  */
1012 
1013 static VALUE
1015 {
1016  return rb_hash_delete_if(rb_obj_dup(hash));
1017 }
1018 
1019 /*
1020  * call-seq:
1021  * hsh.values_at(key, ...) -> array
1022  *
1023  * Return an array containing the values associated with the given keys.
1024  * Also see <code>Hash.select</code>.
1025  *
1026  * h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }
1027  * h.values_at("cow", "cat") #=> ["bovine", "feline"]
1028  */
1029 
1030 VALUE
1032 {
1033  VALUE result = rb_ary_new2(argc);
1034  long i;
1035 
1036  for (i=0; i<argc; i++) {
1037  rb_ary_push(result, rb_hash_aref(hash, argv[i]));
1038  }
1039  return result;
1040 }
1041 
1042 static int
1044 {
1045  if (RTEST(rb_yield_values(2, key, value)))
1046  rb_hash_aset(result, key, value);
1047  return ST_CONTINUE;
1048 }
1049 
1050 /*
1051  * call-seq:
1052  * hsh.select {|key, value| block} -> a_hash
1053  * hsh.select -> an_enumerator
1054  *
1055  * Returns a new hash consisting of entries for which the block returns true.
1056  *
1057  * If no block is given, an enumerator is returned instead.
1058  *
1059  * h = { "a" => 100, "b" => 200, "c" => 300 }
1060  * h.select {|k,v| k > "a"} #=> {"b" => 200, "c" => 300}
1061  * h.select {|k,v| v < 200} #=> {"a" => 100}
1062  */
1063 
1064 VALUE
1066 {
1067  VALUE result;
1068 
1070  result = rb_hash_new();
1071  rb_hash_foreach(hash, select_i, result);
1072  return result;
1073 }
1074 
1075 static int
1077 {
1078  if (!RTEST(rb_yield_values(2, key, value))) {
1079  return ST_DELETE;
1080  }
1081  return ST_CONTINUE;
1082 }
1083 
1084 /*
1085  * call-seq:
1086  * hsh.select! {| key, value | block } -> hsh or nil
1087  * hsh.select! -> an_enumerator
1088  *
1089  * Equivalent to <code>Hash#keep_if</code>, but returns
1090  * <code>nil</code> if no changes were made.
1091  */
1092 
1093 VALUE
1095 {
1096  st_index_t n;
1097 
1099  rb_hash_modify_check(hash);
1100  if (!RHASH(hash)->ntbl)
1101  return Qnil;
1102  n = RHASH(hash)->ntbl->num_entries;
1103  rb_hash_foreach(hash, keep_if_i, hash);
1104  if (n == RHASH(hash)->ntbl->num_entries) return Qnil;
1105  return hash;
1106 }
1107 
1108 /*
1109  * call-seq:
1110  * hsh.keep_if {| key, value | block } -> hsh
1111  * hsh.keep_if -> an_enumerator
1112  *
1113  * Deletes every key-value pair from <i>hsh</i> for which <i>block</i>
1114  * evaluates to false.
1115  *
1116  * If no block is given, an enumerator is returned instead.
1117  *
1118  */
1119 
1120 VALUE
1122 {
1124  rb_hash_modify_check(hash);
1125  if (RHASH(hash)->ntbl)
1126  rb_hash_foreach(hash, keep_if_i, hash);
1127  return hash;
1128 }
1129 
1130 static int
1131 clear_i(VALUE key, VALUE value, VALUE dummy)
1132 {
1133  return ST_DELETE;
1134 }
1135 
1136 /*
1137  * call-seq:
1138  * hsh.clear -> hsh
1139  *
1140  * Removes all key-value pairs from <i>hsh</i>.
1141  *
1142  * h = { "a" => 100, "b" => 200 } #=> {"a"=>100, "b"=>200}
1143  * h.clear #=> {}
1144  *
1145  */
1146 
1147 VALUE
1149 {
1150  rb_hash_modify_check(hash);
1151  if (!RHASH(hash)->ntbl)
1152  return hash;
1153  if (RHASH(hash)->ntbl->num_entries > 0) {
1154  if (RHASH_ITER_LEV(hash) > 0)
1155  rb_hash_foreach(hash, clear_i, 0);
1156  else
1157  st_clear(RHASH(hash)->ntbl);
1158  }
1159 
1160  return hash;
1161 }
1162 
1163 static int
1165 {
1166  *val = arg;
1167  return ST_CONTINUE;
1168 }
1169 
1170 static int
1172 {
1173  *key = (st_data_t)rb_str_new_frozen((VALUE)*key);
1174  return hash_aset(key, val, arg, existing);
1175 }
1176 
1179 
1180 /*
1181  * call-seq:
1182  * hsh[key] = value -> value
1183  * hsh.store(key, value) -> value
1184  *
1185  * == Element Assignment
1186  *
1187  * Associates the value given by +value+ with the key given by +key+.
1188  *
1189  * h = { "a" => 100, "b" => 200 }
1190  * h["a"] = 9
1191  * h["c"] = 4
1192  * h #=> {"a"=>9, "b"=>200, "c"=>4}
1193  *
1194  * +key+ should not have its value changed while it is in use as a key (an
1195  * <tt>unfrozen String</tt> passed as a key will be duplicated and frozen).
1196  *
1197  * a = "a"
1198  * b = "b".freeze
1199  * h = { a => 100, b => 200 }
1200  * h.key(100).equal? a #=> false
1201  * h.key(200).equal? b #=> true
1202  *
1203  */
1204 
1205 VALUE
1207 {
1208  int iter_lev = RHASH_ITER_LEV(hash);
1209  st_table *tbl = RHASH(hash)->ntbl;
1210 
1211  rb_hash_modify(hash);
1212  if (!tbl) {
1213  if (iter_lev > 0) no_new_key();
1214  tbl = RHASH_TBL(hash);
1215  }
1216  if (tbl->type == &identhash || rb_obj_class(key) != rb_cString) {
1217  RHASH_UPDATE_ITER(hash, iter_lev, key, hash_aset, val);
1218  }
1219  else {
1220  RHASH_UPDATE_ITER(hash, iter_lev, key, hash_aset_str, val);
1221  }
1222  return val;
1223 }
1224 
1225 static int
1227 {
1228  rb_hash_aset(hash, key, val);
1229 
1230  return ST_CONTINUE;
1231 }
1232 
1233 static VALUE
1235 {
1236  st_table *ntbl;
1237 
1238  rb_hash_modify_check(hash);
1239  hash2 = to_hash(hash2);
1240 
1241  Check_Type(hash2, T_HASH);
1242 
1243  if (hash == hash2) return hash;
1244 
1245  ntbl = RHASH(hash)->ntbl;
1246  if (RHASH(hash2)->ntbl) {
1247  if (ntbl) st_free_table(ntbl);
1248  RHASH(hash)->ntbl = st_copy(RHASH(hash2)->ntbl);
1249  if (RHASH(hash)->ntbl->num_entries)
1250  rb_hash_rehash(hash);
1251  }
1252  else if (ntbl) {
1253  st_clear(ntbl);
1254  }
1255 
1256  if (FL_TEST(hash2, HASH_PROC_DEFAULT)) {
1257  FL_SET(hash, HASH_PROC_DEFAULT);
1258  }
1259  else {
1260  FL_UNSET(hash, HASH_PROC_DEFAULT);
1261  }
1262  RHASH_IFNONE(hash) = RHASH_IFNONE(hash2);
1263 
1264  return hash;
1265 }
1266 
1267 /*
1268  * call-seq:
1269  * hsh.replace(other_hash) -> hsh
1270  *
1271  * Replaces the contents of <i>hsh</i> with the contents of
1272  * <i>other_hash</i>.
1273  *
1274  * h = { "a" => 100, "b" => 200 }
1275  * h.replace({ "c" => 300, "d" => 400 }) #=> {"c"=>300, "d"=>400}
1276  *
1277  */
1278 
1279 static VALUE
1281 {
1282  rb_hash_modify_check(hash);
1283  hash2 = to_hash(hash2);
1284  if (hash == hash2) return hash;
1285  rb_hash_clear(hash);
1286  if (RHASH(hash2)->ntbl) {
1287  rb_hash_tbl(hash);
1288  RHASH(hash)->ntbl->type = RHASH(hash2)->ntbl->type;
1289  }
1290  rb_hash_foreach(hash2, replace_i, hash);
1291  RHASH_IFNONE(hash) = RHASH_IFNONE(hash2);
1292  if (FL_TEST(hash2, HASH_PROC_DEFAULT)) {
1293  FL_SET(hash, HASH_PROC_DEFAULT);
1294  }
1295  else {
1296  FL_UNSET(hash, HASH_PROC_DEFAULT);
1297  }
1298 
1299  return hash;
1300 }
1301 
1302 /*
1303  * call-seq:
1304  * hsh.length -> fixnum
1305  * hsh.size -> fixnum
1306  *
1307  * Returns the number of key-value pairs in the hash.
1308  *
1309  * h = { "d" => 100, "a" => 200, "v" => 300, "e" => 400 }
1310  * h.length #=> 4
1311  * h.delete("a") #=> 200
1312  * h.length #=> 3
1313  */
1314 
1315 static VALUE
1317 {
1318  if (!RHASH(hash)->ntbl)
1319  return INT2FIX(0);
1320  return INT2FIX(RHASH(hash)->ntbl->num_entries);
1321 }
1322 
1323 
1324 /*
1325  * call-seq:
1326  * hsh.empty? -> true or false
1327  *
1328  * Returns <code>true</code> if <i>hsh</i> contains no key-value pairs.
1329  *
1330  * {}.empty? #=> true
1331  *
1332  */
1333 
1334 static VALUE
1336 {
1337  return RHASH_EMPTY_P(hash) ? Qtrue : Qfalse;
1338 }
1339 
1340 static int
1342 {
1343  rb_yield(value);
1344  return ST_CONTINUE;
1345 }
1346 
1347 /*
1348  * call-seq:
1349  * hsh.each_value {| value | block } -> hsh
1350  * hsh.each_value -> an_enumerator
1351  *
1352  * Calls <i>block</i> once for each key in <i>hsh</i>, passing the
1353  * value as a parameter.
1354  *
1355  * If no block is given, an enumerator is returned instead.
1356  *
1357  * h = { "a" => 100, "b" => 200 }
1358  * h.each_value {|value| puts value }
1359  *
1360  * <em>produces:</em>
1361  *
1362  * 100
1363  * 200
1364  */
1365 
1366 static VALUE
1368 {
1370  rb_hash_foreach(hash, each_value_i, 0);
1371  return hash;
1372 }
1373 
1374 static int
1376 {
1377  rb_yield(key);
1378  return ST_CONTINUE;
1379 }
1380 
1381 /*
1382  * call-seq:
1383  * hsh.each_key {| key | block } -> hsh
1384  * hsh.each_key -> an_enumerator
1385  *
1386  * Calls <i>block</i> once for each key in <i>hsh</i>, passing the key
1387  * as a parameter.
1388  *
1389  * If no block is given, an enumerator is returned instead.
1390  *
1391  * h = { "a" => 100, "b" => 200 }
1392  * h.each_key {|key| puts key }
1393  *
1394  * <em>produces:</em>
1395  *
1396  * a
1397  * b
1398  */
1399 static VALUE
1401 {
1403  rb_hash_foreach(hash, each_key_i, 0);
1404  return hash;
1405 }
1406 
1407 static int
1409 {
1410  rb_yield(rb_assoc_new(key, value));
1411  return ST_CONTINUE;
1412 }
1413 
1414 /*
1415  * call-seq:
1416  * hsh.each {| key, value | block } -> hsh
1417  * hsh.each_pair {| key, value | block } -> hsh
1418  * hsh.each -> an_enumerator
1419  * hsh.each_pair -> an_enumerator
1420  *
1421  * Calls <i>block</i> once for each key in <i>hsh</i>, passing the key-value
1422  * pair as parameters.
1423  *
1424  * If no block is given, an enumerator is returned instead.
1425  *
1426  * h = { "a" => 100, "b" => 200 }
1427  * h.each {|key, value| puts "#{key} is #{value}" }
1428  *
1429  * <em>produces:</em>
1430  *
1431  * a is 100
1432  * b is 200
1433  *
1434  */
1435 
1436 static VALUE
1438 {
1440  rb_hash_foreach(hash, each_pair_i, 0);
1441  return hash;
1442 }
1443 
1444 static int
1446 {
1447  rb_ary_push(ary, rb_assoc_new(key, value));
1448  return ST_CONTINUE;
1449 }
1450 
1451 /*
1452  * call-seq:
1453  * hsh.to_a -> array
1454  *
1455  * Converts <i>hsh</i> to a nested array of <code>[</code> <i>key,
1456  * value</i> <code>]</code> arrays.
1457  *
1458  * h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }
1459  * h.to_a #=> [["c", 300], ["a", 100], ["d", 400]]
1460  */
1461 
1462 static VALUE
1464 {
1465  VALUE ary;
1466 
1467  ary = rb_ary_new();
1468  rb_hash_foreach(hash, to_a_i, ary);
1469  OBJ_INFECT(ary, hash);
1470 
1471  return ary;
1472 }
1473 
1474 static int
1476 {
1477  VALUE str2;
1478 
1479  str2 = rb_inspect(key);
1480  if (RSTRING_LEN(str) > 1) {
1481  rb_str_buf_cat_ascii(str, ", ");
1482  }
1483  else {
1484  rb_enc_copy(str, str2);
1485  }
1486  rb_str_buf_append(str, str2);
1487  OBJ_INFECT(str, str2);
1488  rb_str_buf_cat_ascii(str, "=>");
1489  str2 = rb_inspect(value);
1490  rb_str_buf_append(str, str2);
1491  OBJ_INFECT(str, str2);
1492 
1493  return ST_CONTINUE;
1494 }
1495 
1496 static VALUE
1497 inspect_hash(VALUE hash, VALUE dummy, int recur)
1498 {
1499  VALUE str;
1500 
1501  if (recur) return rb_usascii_str_new2("{...}");
1502  str = rb_str_buf_new2("{");
1503  rb_hash_foreach(hash, inspect_i, str);
1504  rb_str_buf_cat2(str, "}");
1505  OBJ_INFECT(str, hash);
1506 
1507  return str;
1508 }
1509 
1510 /*
1511  * call-seq:
1512  * hsh.to_s -> string
1513  * hsh.inspect -> string
1514  *
1515  * Return the contents of this hash as a string.
1516  *
1517  * h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }
1518  * h.to_s #=> "{\"c\"=>300, \"a\"=>100, \"d\"=>400}"
1519  */
1520 
1521 static VALUE
1523 {
1524  if (RHASH_EMPTY_P(hash))
1525  return rb_usascii_str_new2("{}");
1526  return rb_exec_recursive(inspect_hash, hash, 0);
1527 }
1528 
1529 /*
1530  * call-seq:
1531  * hsh.to_hash => hsh
1532  *
1533  * Returns +self+.
1534  */
1535 
1536 static VALUE
1538 {
1539  return hash;
1540 }
1541 
1542 /*
1543  * call-seq:
1544  * hsh.to_h -> hsh or new_hash
1545  *
1546  * Returns +self+. If called on a subclass of Hash, converts
1547  * the receiver to a Hash object.
1548  */
1549 
1550 static VALUE
1552 {
1553  if (rb_obj_class(hash) != rb_cHash) {
1554  VALUE ret = rb_hash_new();
1555  if (!RHASH_EMPTY_P(hash))
1556  RHASH(ret)->ntbl = st_copy(RHASH(hash)->ntbl);
1557  if (FL_TEST(hash, HASH_PROC_DEFAULT)) {
1558  FL_SET(ret, HASH_PROC_DEFAULT);
1559  }
1560  RHASH_IFNONE(ret) = RHASH_IFNONE(hash);
1561  return ret;
1562  }
1563  return hash;
1564 }
1565 
1566 static int
1568 {
1569  rb_ary_push(ary, key);
1570  return ST_CONTINUE;
1571 }
1572 
1573 /*
1574  * call-seq:
1575  * hsh.keys -> array
1576  *
1577  * Returns a new array populated with the keys from this hash. See also
1578  * <code>Hash#values</code>.
1579  *
1580  * h = { "a" => 100, "b" => 200, "c" => 300, "d" => 400 }
1581  * h.keys #=> ["a", "b", "c", "d"]
1582  *
1583  */
1584 
1585 static VALUE
1587 {
1588  VALUE ary;
1589 
1590  ary = rb_ary_new();
1591  rb_hash_foreach(hash, keys_i, ary);
1592 
1593  return ary;
1594 }
1595 
1596 static int
1598 {
1599  rb_ary_push(ary, value);
1600  return ST_CONTINUE;
1601 }
1602 
1603 /*
1604  * call-seq:
1605  * hsh.values -> array
1606  *
1607  * Returns a new array populated with the values from <i>hsh</i>. See
1608  * also <code>Hash#keys</code>.
1609  *
1610  * h = { "a" => 100, "b" => 200, "c" => 300 }
1611  * h.values #=> [100, 200, 300]
1612  *
1613  */
1614 
1615 static VALUE
1617 {
1618  VALUE ary;
1619 
1620  ary = rb_ary_new();
1621  rb_hash_foreach(hash, values_i, ary);
1622 
1623  return ary;
1624 }
1625 
1626 /*
1627  * call-seq:
1628  * hsh.has_key?(key) -> true or false
1629  * hsh.include?(key) -> true or false
1630  * hsh.key?(key) -> true or false
1631  * hsh.member?(key) -> true or false
1632  *
1633  * Returns <code>true</code> if the given key is present in <i>hsh</i>.
1634  *
1635  * h = { "a" => 100, "b" => 200 }
1636  * h.has_key?("a") #=> true
1637  * h.has_key?("z") #=> false
1638  *
1639  */
1640 
1641 static VALUE
1643 {
1644  if (!RHASH(hash)->ntbl)
1645  return Qfalse;
1646  if (st_lookup(RHASH(hash)->ntbl, key, 0)) {
1647  return Qtrue;
1648  }
1649  return Qfalse;
1650 }
1651 
1652 static int
1654 {
1655  VALUE *data = (VALUE *)arg;
1656 
1657  if (rb_equal(value, data[1])) {
1658  data[0] = Qtrue;
1659  return ST_STOP;
1660  }
1661  return ST_CONTINUE;
1662 }
1663 
1664 /*
1665  * call-seq:
1666  * hsh.has_value?(value) -> true or false
1667  * hsh.value?(value) -> true or false
1668  *
1669  * Returns <code>true</code> if the given value is present for some key
1670  * in <i>hsh</i>.
1671  *
1672  * h = { "a" => 100, "b" => 200 }
1673  * h.has_value?(100) #=> true
1674  * h.has_value?(999) #=> false
1675  */
1676 
1677 static VALUE
1679 {
1680  VALUE data[2];
1681 
1682  data[0] = Qfalse;
1683  data[1] = val;
1685  return data[0];
1686 }
1687 
1688 struct equal_data {
1691  int eql;
1692 };
1693 
1694 static int
1696 {
1697  struct equal_data *data = (struct equal_data *)arg;
1698  st_data_t val2;
1699 
1700  if (!st_lookup(data->tbl, key, &val2)) {
1701  data->result = Qfalse;
1702  return ST_STOP;
1703  }
1704  if (!(data->eql ? rb_eql(val1, (VALUE)val2) : (int)rb_equal(val1, (VALUE)val2))) {
1705  data->result = Qfalse;
1706  return ST_STOP;
1707  }
1708  return ST_CONTINUE;
1709 }
1710 
1711 static VALUE
1713 {
1714  struct equal_data *data;
1715 
1716  if (recur) return Qtrue; /* Subtle! */
1717  data = (struct equal_data*)dt;
1718  data->result = Qtrue;
1719  rb_hash_foreach(hash, eql_i, dt);
1720 
1721  return data->result;
1722 }
1723 
1724 static VALUE
1725 hash_equal(VALUE hash1, VALUE hash2, int eql)
1726 {
1727  struct equal_data data;
1728 
1729  if (hash1 == hash2) return Qtrue;
1730  if (!RB_TYPE_P(hash2, T_HASH)) {
1731  if (!rb_respond_to(hash2, rb_intern("to_hash"))) {
1732  return Qfalse;
1733  }
1734  if (eql)
1735  return rb_eql(hash2, hash1);
1736  else
1737  return rb_equal(hash2, hash1);
1738  }
1739  if (RHASH_SIZE(hash1) != RHASH_SIZE(hash2))
1740  return Qfalse;
1741  if (!RHASH(hash1)->ntbl || !RHASH(hash2)->ntbl)
1742  return Qtrue;
1743  if (RHASH(hash1)->ntbl->type != RHASH(hash2)->ntbl->type)
1744  return Qfalse;
1745 #if 0
1746  if (!(rb_equal(RHASH_IFNONE(hash1), RHASH_IFNONE(hash2)) &&
1747  FL_TEST(hash1, HASH_PROC_DEFAULT) == FL_TEST(hash2, HASH_PROC_DEFAULT)))
1748  return Qfalse;
1749 #endif
1750 
1751  data.tbl = RHASH(hash2)->ntbl;
1752  data.eql = eql;
1753  return rb_exec_recursive_paired(recursive_eql, hash1, hash2, (VALUE)&data);
1754 }
1755 
1756 /*
1757  * call-seq:
1758  * hsh == other_hash -> true or false
1759  *
1760  * Equality---Two hashes are equal if they each contain the same number
1761  * of keys and if each key-value pair is equal to (according to
1762  * <code>Object#==</code>) the corresponding elements in the other
1763  * hash.
1764  *
1765  * h1 = { "a" => 1, "c" => 2 }
1766  * h2 = { 7 => 35, "c" => 2, "a" => 1 }
1767  * h3 = { "a" => 1, "c" => 2, 7 => 35 }
1768  * h4 = { "a" => 1, "d" => 2, "f" => 35 }
1769  * h1 == h2 #=> false
1770  * h2 == h3 #=> true
1771  * h3 == h4 #=> false
1772  *
1773  */
1774 
1775 static VALUE
1777 {
1778  return hash_equal(hash1, hash2, FALSE);
1779 }
1780 
1781 /*
1782  * call-seq:
1783  * hash.eql?(other) -> true or false
1784  *
1785  * Returns <code>true</code> if <i>hash</i> and <i>other</i> are
1786  * both hashes with the same content.
1787  */
1788 
1789 static VALUE
1790 rb_hash_eql(VALUE hash1, VALUE hash2)
1791 {
1792  return hash_equal(hash1, hash2, TRUE);
1793 }
1794 
1795 static int
1797 {
1798  st_index_t *hval = (st_index_t *)arg;
1799  st_index_t hdata[2];
1800 
1801  hdata[0] = rb_hash(key);
1802  hdata[1] = rb_hash(val);
1803  *hval ^= st_hash(hdata, sizeof(hdata), 0);
1804  return ST_CONTINUE;
1805 }
1806 
1807 static VALUE
1808 recursive_hash(VALUE hash, VALUE dummy, int recur)
1809 {
1810  st_index_t hval;
1811 
1812  if (!RHASH(hash)->ntbl)
1813  return LONG2FIX(0);
1814  hval = RHASH(hash)->ntbl->num_entries;
1815  if (!hval) return LONG2FIX(0);
1816  if (recur)
1817  hval = rb_hash_uint(rb_hash_start(rb_hash(rb_cHash)), hval);
1818  else
1819  rb_hash_foreach(hash, hash_i, (VALUE)&hval);
1820  hval = rb_hash_end(hval);
1821  return INT2FIX(hval);
1822 }
1823 
1824 /*
1825  * call-seq:
1826  * hsh.hash -> fixnum
1827  *
1828  * Compute a hash-code for this hash. Two hashes with the same content
1829  * will have the same hash code (and will compare using <code>eql?</code>).
1830  */
1831 
1832 static VALUE
1834 {
1835  return rb_exec_recursive_outer(recursive_hash, hash, 0);
1836 }
1837 
1838 static int
1840 {
1841  rb_hash_aset(hash, value, key);
1842  return ST_CONTINUE;
1843 }
1844 
1845 /*
1846  * call-seq:
1847  * hsh.invert -> new_hash
1848  *
1849  * Returns a new hash created by using <i>hsh</i>'s values as keys, and
1850  * the keys as values.
1851  *
1852  * h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 }
1853  * h.invert #=> {0=>"a", 100=>"m", 200=>"d", 300=>"y"}
1854  *
1855  */
1856 
1857 static VALUE
1859 {
1860  VALUE h = rb_hash_new();
1861 
1863  return h;
1864 }
1865 
1866 static int
1868 {
1869  *value = arg;
1870  return ST_CONTINUE;
1871 }
1872 
1874 
1875 static int
1876 rb_hash_update_i(VALUE key, VALUE value, VALUE hash)
1877 {
1878  RHASH_UPDATE(hash, key, rb_hash_update_callback, value);
1879  return ST_CONTINUE;
1880 }
1881 
1882 static int
1884 {
1885  VALUE newvalue = (VALUE)arg;
1886  if (existing) {
1887  newvalue = rb_yield_values(3, (VALUE)*key, (VALUE)*value, newvalue);
1888  }
1889  *value = (st_data_t)newvalue;
1890  return ST_CONTINUE;
1891 }
1892 
1894 
1895 static int
1896 rb_hash_update_block_i(VALUE key, VALUE value, VALUE hash)
1897 {
1898  RHASH_UPDATE(hash, key, rb_hash_update_block_callback, value);
1899  return ST_CONTINUE;
1900 }
1901 
1902 /*
1903  * call-seq:
1904  * hsh.merge!(other_hash) -> hsh
1905  * hsh.update(other_hash) -> hsh
1906  * hsh.merge!(other_hash){|key, oldval, newval| block} -> hsh
1907  * hsh.update(other_hash){|key, oldval, newval| block} -> hsh
1908  *
1909  * Adds the contents of _other_hash_ to _hsh_. If no block is specified,
1910  * entries with duplicate keys are overwritten with the values from
1911  * _other_hash_, otherwise the value of each duplicate key is determined by
1912  * calling the block with the key, its value in _hsh_ and its value in
1913  * _other_hash_.
1914  *
1915  * h1 = { "a" => 100, "b" => 200 }
1916  * h2 = { "b" => 254, "c" => 300 }
1917  * h1.merge!(h2) #=> {"a"=>100, "b"=>254, "c"=>300}
1918  *
1919  * h1 = { "a" => 100, "b" => 200 }
1920  * h2 = { "b" => 254, "c" => 300 }
1921  * h1.merge!(h2) { |key, v1, v2| v1 }
1922  * #=> {"a"=>100, "b"=>200, "c"=>300}
1923  */
1924 
1925 static VALUE
1927 {
1928  rb_hash_modify(hash1);
1929  hash2 = to_hash(hash2);
1930  if (rb_block_given_p()) {
1931  rb_hash_foreach(hash2, rb_hash_update_block_i, hash1);
1932  }
1933  else {
1934  rb_hash_foreach(hash2, rb_hash_update_i, hash1);
1935  }
1936  return hash1;
1937 }
1938 
1939 struct update_arg {
1943 };
1944 
1945 static int
1947 {
1948  struct update_arg *arg = (struct update_arg *)arg0;
1949  VALUE newvalue = arg->value;
1950  if (existing) {
1951  newvalue = (*arg->func)((VALUE)*key, (VALUE)*value, newvalue);
1952  }
1953  *value = (st_data_t)newvalue;
1954  return ST_CONTINUE;
1955 }
1956 
1958 
1959 static int
1960 rb_hash_update_func_i(VALUE key, VALUE value, VALUE arg0)
1961 {
1962  struct update_arg *arg = (struct update_arg *)arg0;
1963  VALUE hash = arg->hash;
1964 
1965  arg->value = value;
1966  RHASH_UPDATE(hash, key, rb_hash_update_func_callback, arg);
1967  return ST_CONTINUE;
1968 }
1969 
1970 VALUE
1972 {
1973  rb_hash_modify(hash1);
1974  hash2 = to_hash(hash2);
1975  if (func) {
1976  struct update_arg arg;
1977  arg.hash = hash1;
1978  arg.func = func;
1979  rb_hash_foreach(hash2, rb_hash_update_func_i, (VALUE)&arg);
1980  }
1981  else {
1982  rb_hash_foreach(hash2, rb_hash_update_i, hash1);
1983  }
1984  return hash1;
1985 }
1986 
1987 /*
1988  * call-seq:
1989  * hsh.merge(other_hash) -> new_hash
1990  * hsh.merge(other_hash){|key, oldval, newval| block} -> new_hash
1991  *
1992  * Returns a new hash containing the contents of <i>other_hash</i> and
1993  * the contents of <i>hsh</i>. If no block is specified, the value for
1994  * entries with duplicate keys will be that of <i>other_hash</i>. Otherwise
1995  * the value for each duplicate key is determined by calling the block
1996  * with the key, its value in <i>hsh</i> and its value in <i>other_hash</i>.
1997  *
1998  * h1 = { "a" => 100, "b" => 200 }
1999  * h2 = { "b" => 254, "c" => 300 }
2000  * h1.merge(h2) #=> {"a"=>100, "b"=>254, "c"=>300}
2001  * h1.merge(h2){|key, oldval, newval| newval - oldval}
2002  * #=> {"a"=>100, "b"=>54, "c"=>300}
2003  * h1 #=> {"a"=>100, "b"=>200}
2004  *
2005  */
2006 
2007 static VALUE
2009 {
2010  return rb_hash_update(rb_obj_dup(hash1), hash2);
2011 }
2012 
2013 static int
2015 {
2016  VALUE *args = (VALUE *)arg;
2017 
2018  if (RTEST(rb_equal(args[0], key))) {
2019  args[1] = rb_assoc_new(key, val);
2020  return ST_STOP;
2021  }
2022  return ST_CONTINUE;
2023 }
2024 
2025 /*
2026  * call-seq:
2027  * hash.assoc(obj) -> an_array or nil
2028  *
2029  * Searches through the hash comparing _obj_ with the key using <code>==</code>.
2030  * Returns the key-value pair (two elements array) or +nil+
2031  * if no match is found. See <code>Array#assoc</code>.
2032  *
2033  * h = {"colors" => ["red", "blue", "green"],
2034  * "letters" => ["a", "b", "c" ]}
2035  * h.assoc("letters") #=> ["letters", ["a", "b", "c"]]
2036  * h.assoc("foo") #=> nil
2037  */
2038 
2039 VALUE
2041 {
2042  VALUE args[2];
2043 
2044  args[0] = obj;
2045  args[1] = Qnil;
2046  rb_hash_foreach(hash, assoc_i, (VALUE)args);
2047  return args[1];
2048 }
2049 
2050 static int
2052 {
2053  VALUE *args = (VALUE *)arg;
2054 
2055  if (RTEST(rb_equal(args[0], val))) {
2056  args[1] = rb_assoc_new(key, val);
2057  return ST_STOP;
2058  }
2059  return ST_CONTINUE;
2060 }
2061 
2062 /*
2063  * call-seq:
2064  * hash.rassoc(obj) -> an_array or nil
2065  *
2066  * Searches through the hash comparing _obj_ with the value using <code>==</code>.
2067  * Returns the first key-value pair (two-element array) that matches. See
2068  * also <code>Array#rassoc</code>.
2069  *
2070  * a = {1=> "one", 2 => "two", 3 => "three", "ii" => "two"}
2071  * a.rassoc("two") #=> [2, "two"]
2072  * a.rassoc("four") #=> nil
2073  */
2074 
2075 VALUE
2077 {
2078  VALUE args[2];
2079 
2080  args[0] = obj;
2081  args[1] = Qnil;
2082  rb_hash_foreach(hash, rassoc_i, (VALUE)args);
2083  return args[1];
2084 }
2085 
2086 /*
2087  * call-seq:
2088  * hash.flatten -> an_array
2089  * hash.flatten(level) -> an_array
2090  *
2091  * Returns a new array that is a one-dimensional flattening of this
2092  * hash. That is, for every key or value that is an array, extract
2093  * its elements into the new array. Unlike Array#flatten, this
2094  * method does not flatten recursively by default. The optional
2095  * <i>level</i> argument determines the level of recursion to flatten.
2096  *
2097  * a = {1=> "one", 2 => [2,"two"], 3 => "three"}
2098  * a.flatten # => [1, "one", 2, [2, "two"], 3, "three"]
2099  * a.flatten(2) # => [1, "one", 2, 2, "two", 3, "three"]
2100  */
2101 
2102 static VALUE
2104 {
2105  VALUE ary, tmp;
2106 
2107  ary = rb_hash_to_a(hash);
2108  if (argc == 0) {
2109  argc = 1;
2110  tmp = INT2FIX(1);
2111  argv = &tmp;
2112  }
2113  rb_funcall2(ary, rb_intern("flatten!"), argc, argv);
2114  return ary;
2115 }
2116 
2117 /*
2118  * call-seq:
2119  * hsh.compare_by_identity -> hsh
2120  *
2121  * Makes <i>hsh</i> compare its keys by their identity, i.e. it
2122  * will consider exact same objects as same keys.
2123  *
2124  * h1 = { "a" => 100, "b" => 200, :c => "c" }
2125  * h1["a"] #=> 100
2126  * h1.compare_by_identity
2127  * h1.compare_by_identity? #=> true
2128  * h1["a"] #=> nil # different objects.
2129  * h1[:c] #=> "c" # same symbols are all same.
2130  *
2131  */
2132 
2133 static VALUE
2135 {
2136  rb_hash_modify(hash);
2137  RHASH(hash)->ntbl->type = &identhash;
2138  rb_hash_rehash(hash);
2139  return hash;
2140 }
2141 
2142 /*
2143  * call-seq:
2144  * hsh.compare_by_identity? -> true or false
2145  *
2146  * Returns <code>true</code> if <i>hsh</i> will compare its keys by
2147  * their identity. Also see <code>Hash#compare_by_identity</code>.
2148  *
2149  */
2150 
2151 static VALUE
2153 {
2154  if (!RHASH(hash)->ntbl)
2155  return Qfalse;
2156  if (RHASH(hash)->ntbl->type == &identhash) {
2157  return Qtrue;
2158  }
2159  return Qfalse;
2160 }
2161 
2162 static int path_tainted = -1;
2163 
2164 static char **origenviron;
2165 #ifdef _WIN32
2166 #define GET_ENVIRON(e) ((e) = rb_w32_get_environ())
2167 #define FREE_ENVIRON(e) rb_w32_free_environ(e)
2168 static char **my_environ;
2169 #undef environ
2170 #define environ my_environ
2171 #undef getenv
2172 #define getenv(n) rb_w32_ugetenv(n)
2173 #elif defined(__APPLE__)
2174 #undef environ
2175 #define environ (*_NSGetEnviron())
2176 #define GET_ENVIRON(e) (e)
2177 #define FREE_ENVIRON(e)
2178 #else
2179 extern char **environ;
2180 #define GET_ENVIRON(e) (e)
2181 #define FREE_ENVIRON(e)
2182 #endif
2183 #ifdef ENV_IGNORECASE
2184 #define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
2185 #define ENVNMATCH(s1, s2, n) (STRNCASECMP((s1), (s2), (n)) == 0)
2186 #else
2187 #define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
2188 #define ENVNMATCH(s1, s2, n) (memcmp((s1), (s2), (n)) == 0)
2189 #endif
2190 
2191 static VALUE
2192 env_str_new(const char *ptr, long len)
2193 {
2194 #ifdef _WIN32
2196 #else
2197  VALUE str = rb_locale_str_new(ptr, len);
2198 #endif
2199 
2200  rb_obj_freeze(str);
2201  return str;
2202 }
2203 
2204 static VALUE
2205 env_str_new2(const char *ptr)
2206 {
2207  if (!ptr) return Qnil;
2208  return env_str_new(ptr, strlen(ptr));
2209 }
2210 
2211 static VALUE
2213 {
2214  char *nam, *val;
2215 
2216  rb_secure(4);
2217  SafeStringValue(name);
2218  nam = RSTRING_PTR(name);
2219  if (memchr(nam, '\0', RSTRING_LEN(name))) {
2220  rb_raise(rb_eArgError, "bad environment variable name");
2221  }
2222  val = getenv(nam);
2223  if (val) {
2224  VALUE value = env_str_new2(val);
2225 
2226  ruby_setenv(nam, 0);
2227  if (ENVMATCH(nam, PATH_ENV)) {
2228  path_tainted = 0;
2229  }
2230  return value;
2231  }
2232  return Qnil;
2233 }
2234 
2235 /*
2236  * call-seq:
2237  * ENV.delete(name) -> value
2238  * ENV.delete(name) { |name| } -> value
2239  *
2240  * Deletes the environment variable with +name+ and returns the value of the
2241  * variable. If a block is given it will be called when the named environment
2242  * does not exist.
2243  */
2244 static VALUE
2246 {
2247  VALUE val;
2248 
2249  val = env_delete(obj, name);
2250  if (NIL_P(val) && rb_block_given_p()) rb_yield(name);
2251  return val;
2252 }
2253 
2254 static int env_path_tainted(const char *);
2255 
2256 /*
2257  * call-seq:
2258  * ENV[name] -> value
2259  *
2260  * Retrieves the +value+ for environment variable +name+ as a String. Returns
2261  * +nil+ if the named variable does not exist.
2262  */
2263 static VALUE
2265 {
2266  char *nam, *env;
2267 
2268  rb_secure(4);
2269  SafeStringValue(name);
2270  nam = RSTRING_PTR(name);
2271  if (memchr(nam, '\0', RSTRING_LEN(name))) {
2272  rb_raise(rb_eArgError, "bad environment variable name");
2273  }
2274  env = getenv(nam);
2275  if (env) {
2276  if (ENVMATCH(nam, PATH_ENV) && !env_path_tainted(env)) {
2277 #ifdef _WIN32
2279 #else
2280  VALUE str = rb_filesystem_str_new_cstr(env);
2281 #endif
2282 
2283  rb_obj_freeze(str);
2284  return str;
2285  }
2286  return env_str_new2(env);
2287  }
2288  return Qnil;
2289 }
2290 
2291 /*
2292  * :yield: missing_name
2293  * call-seq:
2294  * ENV.fetch(name) -> value
2295  * ENV.fetch(name, default) -> value
2296  * ENV.fetch(name) { |missing_name| ... } -> value
2297  *
2298  * Retrieves the environment variable +name+.
2299  *
2300  * If the given name does not exist and neither +default+ nor a block a
2301  * provided an IndexError is raised. If a block is given it is called with
2302  * the missing name to provide a value. If a default value is given it will
2303  * be returned when no block is given.
2304  */
2305 static VALUE
2307 {
2308  VALUE key, if_none;
2309  long block_given;
2310  char *nam, *env;
2311 
2312  rb_secure(4);
2313  rb_scan_args(argc, argv, "11", &key, &if_none);
2314  block_given = rb_block_given_p();
2315  if (block_given && argc == 2) {
2316  rb_warn("block supersedes default value argument");
2317  }
2318  SafeStringValue(key);
2319  nam = RSTRING_PTR(key);
2320  if (memchr(nam, '\0', RSTRING_LEN(key))) {
2321  rb_raise(rb_eArgError, "bad environment variable name");
2322  }
2323  env = getenv(nam);
2324  if (!env) {
2325  if (block_given) return rb_yield(key);
2326  if (argc == 1) {
2327  rb_raise(rb_eKeyError, "key not found");
2328  }
2329  return if_none;
2330  }
2331  if (ENVMATCH(nam, PATH_ENV) && !env_path_tainted(env))
2332 #ifdef _WIN32
2334 #else
2335  return rb_filesystem_str_new_cstr(env);
2336 #endif
2337  return env_str_new2(env);
2338 }
2339 
2340 static void
2341 path_tainted_p(const char *path)
2342 {
2343  path_tainted = rb_path_check(path)?0:1;
2344 }
2345 
2346 static int
2347 env_path_tainted(const char *path)
2348 {
2349  if (path_tainted < 0) {
2350  path_tainted_p(path);
2351  }
2352  return path_tainted;
2353 }
2354 
2355 int
2357 {
2358  if (path_tainted < 0) {
2360  }
2361  return path_tainted;
2362 }
2363 
2364 #if defined(_WIN32) || (defined(HAVE_SETENV) && defined(HAVE_UNSETENV))
2365 #elif defined __sun
2366 static int
2367 in_origenv(const char *str)
2368 {
2369  char **env;
2370  for (env = origenviron; *env; ++env) {
2371  if (*env == str) return 1;
2372  }
2373  return 0;
2374 }
2375 #else
2376 static int
2377 envix(const char *nam)
2378 {
2379  register int i, len = strlen(nam);
2380  char **env;
2381 
2382  env = GET_ENVIRON(environ);
2383  for (i = 0; env[i]; i++) {
2384  if (ENVNMATCH(env[i],nam,len) && env[i][len] == '=')
2385  break; /* memcmp must come first to avoid */
2386  } /* potential SEGV's */
2387  FREE_ENVIRON(environ);
2388  return i;
2389 }
2390 #endif
2391 
2392 #if defined(_WIN32)
2393 static size_t
2394 getenvsize(const char* p)
2395 {
2396  const char* porg = p;
2397  while (*p++) p += strlen(p) + 1;
2398  return p - porg + 1;
2399 }
2400 static size_t
2401 getenvblocksize()
2402 {
2403  return (rb_w32_osver() >= 5) ? 32767 : 5120;
2404 }
2405 #endif
2406 
2407 void
2408 ruby_setenv(const char *name, const char *value)
2409 {
2410 #if defined(_WIN32)
2411  VALUE buf;
2412  int failed = 0;
2413  if (strchr(name, '=')) {
2414  fail:
2415  errno = EINVAL;
2416  rb_sys_fail("ruby_setenv");
2417  }
2418  if (value) {
2419  char* p = GetEnvironmentStringsA();
2420  size_t n;
2421  if (!p) goto fail; /* never happen */
2422  n = strlen(name) + 2 + strlen(value) + getenvsize(p);
2423  FreeEnvironmentStringsA(p);
2424  if (n >= getenvblocksize()) {
2425  goto fail; /* 2 for '=' & '\0' */
2426  }
2427  buf = rb_sprintf("%s=%s", name, value);
2428  }
2429  else {
2430  buf = rb_sprintf("%s=", name);
2431  }
2432  failed = putenv(RSTRING_PTR(buf));
2433  /* even if putenv() failed, clean up and try to delete the
2434  * variable from the system area. */
2435  rb_str_resize(buf, 0);
2436  if (!value || !*value) {
2437  /* putenv() doesn't handle empty value */
2438  if (!SetEnvironmentVariable(name, value) &&
2439  GetLastError() != ERROR_ENVVAR_NOT_FOUND) goto fail;
2440  }
2441  if (failed) goto fail;
2442 #elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV)
2443 #undef setenv
2444 #undef unsetenv
2445  if (value) {
2446  if (setenv(name, value, 1))
2447  rb_sys_fail("setenv");
2448  } else {
2449 #ifdef VOID_UNSETENV
2450  unsetenv(name);
2451 #else
2452  if (unsetenv(name))
2453  rb_sys_fail("unsetenv");
2454 #endif
2455  }
2456 #elif defined __sun
2457  size_t len;
2458  char **env_ptr, *str;
2459  if (strchr(name, '=')) {
2460  errno = EINVAL;
2461  rb_sys_fail("ruby_setenv");
2462  }
2463  len = strlen(name);
2464  for (env_ptr = GET_ENVIRON(environ); (str = *env_ptr) != 0; ++env_ptr) {
2465  if (!strncmp(str, name, len) && str[len] == '=') {
2466  if (!in_origenv(str)) free(str);
2467  while ((env_ptr[0] = env_ptr[1]) != 0) env_ptr++;
2468  break;
2469  }
2470  }
2471  if (value) {
2472  str = malloc(len += strlen(value) + 2);
2473  snprintf(str, len, "%s=%s", name, value);
2474  if (putenv(str))
2475  rb_sys_fail("putenv");
2476  }
2477 #else /* WIN32 */
2478  size_t len;
2479  int i;
2480  if (strchr(name, '=')) {
2481  errno = EINVAL;
2482  rb_sys_fail("ruby_setenv");
2483  }
2484  i=envix(name); /* where does it go? */
2485 
2486  if (environ == origenviron) { /* need we copy environment? */
2487  int j;
2488  int max;
2489  char **tmpenv;
2490 
2491  for (max = i; environ[max]; max++) ;
2492  tmpenv = ALLOC_N(char*, max+2);
2493  for (j=0; j<max; j++) /* copy environment */
2494  tmpenv[j] = ruby_strdup(environ[j]);
2495  tmpenv[max] = 0;
2496  environ = tmpenv; /* tell exec where it is now */
2497  }
2498  if (environ[i]) {
2499  char **envp = origenviron;
2500  while (*envp && *envp != environ[i]) envp++;
2501  if (!*envp)
2502  xfree(environ[i]);
2503  if (!value) {
2504  while (environ[i]) {
2505  environ[i] = environ[i+1];
2506  i++;
2507  }
2508  return;
2509  }
2510  }
2511  else { /* does not exist yet */
2512  if (!value) return;
2513  REALLOC_N(environ, char*, i+2); /* just expand it a bit */
2514  environ[i+1] = 0; /* make sure it's null terminated */
2515  }
2516  len = strlen(name) + strlen(value) + 2;
2517  environ[i] = ALLOC_N(char, len);
2518  snprintf(environ[i],len,"%s=%s",name,value); /* all that work just for this */
2519 #endif /* WIN32 */
2520 }
2521 
2522 void
2523 ruby_unsetenv(const char *name)
2524 {
2525  ruby_setenv(name, 0);
2526 }
2527 
2528 /*
2529  * call-seq:
2530  * ENV[name] = value
2531  * ENV.store(name, value) -> value
2532  *
2533  * Sets the environment variable +name+ to +value+. If the value given is
2534  * +nil+ the environment variable is deleted.
2535  *
2536  */
2537 static VALUE
2539 {
2540  char *name, *value;
2541 
2542  if (rb_safe_level() >= 4) {
2543  rb_raise(rb_eSecurityError, "can't change environment variable");
2544  }
2545 
2546  if (NIL_P(val)) {
2547  env_delete(obj, nm);
2548  return Qnil;
2549  }
2550  SafeStringValue(nm);
2551  SafeStringValue(val);
2552  name = RSTRING_PTR(nm);
2553  value = RSTRING_PTR(val);
2554  if (memchr(name, '\0', RSTRING_LEN(nm)))
2555  rb_raise(rb_eArgError, "bad environment variable name");
2556  if (memchr(value, '\0', RSTRING_LEN(val)))
2557  rb_raise(rb_eArgError, "bad environment variable value");
2558 
2559  ruby_setenv(name, value);
2560  if (ENVMATCH(name, PATH_ENV)) {
2561  if (OBJ_TAINTED(val)) {
2562  /* already tainted, no check */
2563  path_tainted = 1;
2564  return val;
2565  }
2566  else {
2567  path_tainted_p(value);
2568  }
2569  }
2570  return val;
2571 }
2572 
2573 /*
2574  * call-seq:
2575  * ENV.keys -> Array
2576  *
2577  * Returns every environment variable name in an Array
2578  */
2579 static VALUE
2581 {
2582  char **env;
2583  VALUE ary;
2584 
2585  rb_secure(4);
2586  ary = rb_ary_new();
2587  env = GET_ENVIRON(environ);
2588  while (*env) {
2589  char *s = strchr(*env, '=');
2590  if (s) {
2591  rb_ary_push(ary, env_str_new(*env, s-*env));
2592  }
2593  env++;
2594  }
2595  FREE_ENVIRON(environ);
2596  return ary;
2597 }
2598 
2599 static VALUE
2601 {
2602  char **env;
2603  long cnt = 0;
2604 
2605  rb_secure(4);
2606 
2607  env = GET_ENVIRON(environ);
2608  for (; *env ; ++env) {
2609  if (strchr(*env, '=')) {
2610  cnt++;
2611  }
2612  }
2613  FREE_ENVIRON(environ);
2614  return LONG2FIX(cnt);
2615 }
2616 
2617 /*
2618  * call-seq:
2619  * ENV.each_key { |name| } -> Hash
2620  * ENV.each_key -> Enumerator
2621  *
2622  * Yields each environment variable name.
2623  *
2624  * An Enumerator is returned if no block is given.
2625  */
2626 static VALUE
2628 {
2629  VALUE keys;
2630  long i;
2631 
2632  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
2633  keys = env_keys(); /* rb_secure(4); */
2634  for (i=0; i<RARRAY_LEN(keys); i++) {
2635  rb_yield(RARRAY_PTR(keys)[i]);
2636  }
2637  return ehash;
2638 }
2639 
2640 /*
2641  * call-seq:
2642  * ENV.values -> Array
2643  *
2644  * Returns every environment variable value as an Array
2645  */
2646 static VALUE
2648 {
2649  VALUE ary;
2650  char **env;
2651 
2652  rb_secure(4);
2653  ary = rb_ary_new();
2654  env = GET_ENVIRON(environ);
2655  while (*env) {
2656  char *s = strchr(*env, '=');
2657  if (s) {
2658  rb_ary_push(ary, env_str_new2(s+1));
2659  }
2660  env++;
2661  }
2662  FREE_ENVIRON(environ);
2663  return ary;
2664 }
2665 
2666 /*
2667  * call-seq:
2668  * ENV.each_value { |value| } -> Hash
2669  * ENV.each_value -> Enumerator
2670  *
2671  * Yields each environment variable +value+.
2672  *
2673  * An Enumerator is returned if no block was given.
2674  */
2675 static VALUE
2677 {
2678  VALUE values;
2679  long i;
2680 
2681  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
2682  values = env_values(); /* rb_secure(4); */
2683  for (i=0; i<RARRAY_LEN(values); i++) {
2684  rb_yield(RARRAY_PTR(values)[i]);
2685  }
2686  return ehash;
2687 }
2688 
2689 /*
2690  * call-seq:
2691  * ENV.each { |name, value| } -> Hash
2692  * ENV.each -> Enumerator
2693  * ENV.each_pair { |name, value| } -> Hash
2694  * ENV.each_pair -> Enumerator
2695  *
2696  * Yields each environment variable +name+ and +value+.
2697  *
2698  * If no block is given an Enumerator is returned.
2699  */
2700 static VALUE
2702 {
2703  char **env;
2704  VALUE ary;
2705  long i;
2706 
2707  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
2708 
2709  rb_secure(4);
2710  ary = rb_ary_new();
2711  env = GET_ENVIRON(environ);
2712  while (*env) {
2713  char *s = strchr(*env, '=');
2714  if (s) {
2715  rb_ary_push(ary, env_str_new(*env, s-*env));
2716  rb_ary_push(ary, env_str_new2(s+1));
2717  }
2718  env++;
2719  }
2720  FREE_ENVIRON(environ);
2721 
2722  for (i=0; i<RARRAY_LEN(ary); i+=2) {
2723  rb_yield(rb_assoc_new(RARRAY_PTR(ary)[i], RARRAY_PTR(ary)[i+1]));
2724  }
2725  return ehash;
2726 }
2727 
2728 /*
2729  * call-seq:
2730  * ENV.reject! { |name, value| } -> ENV or nil
2731  * ENV.reject! -> Enumerator
2732  *
2733  * Equivalent to ENV#delete_if but returns +nil+ if no changes were made.
2734  *
2735  * Returns an Enumerator if no block was given.
2736  */
2737 static VALUE
2739 {
2740  volatile VALUE keys;
2741  long i;
2742  int del = 0;
2743 
2744  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
2745  keys = env_keys(); /* rb_secure(4); */
2746  RBASIC(keys)->klass = 0;
2747  for (i=0; i<RARRAY_LEN(keys); i++) {
2748  VALUE val = rb_f_getenv(Qnil, RARRAY_PTR(keys)[i]);
2749  if (!NIL_P(val)) {
2750  if (RTEST(rb_yield_values(2, RARRAY_PTR(keys)[i], val))) {
2751  FL_UNSET(RARRAY_PTR(keys)[i], FL_TAINT);
2752  env_delete(Qnil, RARRAY_PTR(keys)[i]);
2753  del++;
2754  }
2755  }
2756  }
2757  if (del == 0) return Qnil;
2758  return envtbl;
2759 }
2760 
2761 /*
2762  * call-seq:
2763  * ENV.delete_if { |name, value| } -> Hash
2764  * ENV.delete_if -> Enumerator
2765  *
2766  * Deletes every environment variable for which the block evaluates to +true+.
2767  *
2768  * If no block is given an enumerator is returned instead.
2769  */
2770 static VALUE
2772 {
2773  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
2774  env_reject_bang(ehash);
2775  return envtbl;
2776 }
2777 
2778 /*
2779  * call-seq:
2780  * ENV.values_at(name, ...) -> Array
2781  *
2782  * Returns an array containing the environment variable values associated with
2783  * the given names. See also ENV.select.
2784  */
2785 static VALUE
2787 {
2788  VALUE result;
2789  long i;
2790 
2791  rb_secure(4);
2792  result = rb_ary_new();
2793  for (i=0; i<argc; i++) {
2794  rb_ary_push(result, rb_f_getenv(Qnil, argv[i]));
2795  }
2796  return result;
2797 }
2798 
2799 /*
2800  * call-seq:
2801  * ENV.select { |name, value| } -> Hash
2802  * ENV.select -> Enumerator
2803  *
2804  * Returns a copy of the environment for entries where the block returns true.
2805  *
2806  * Returns an Enumerator if no block was given.
2807  */
2808 static VALUE
2810 {
2811  VALUE result;
2812  VALUE keys;
2813  long i;
2814 
2815  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
2816  rb_secure(4);
2817  result = rb_hash_new();
2818  keys = env_keys();
2819  for (i = 0; i < RARRAY_LEN(keys); ++i) {
2820  VALUE key = RARRAY_PTR(keys)[i];
2821  VALUE val = rb_f_getenv(Qnil, key);
2822  if (!NIL_P(val)) {
2823  if (RTEST(rb_yield_values(2, key, val))) {
2824  rb_hash_aset(result, key, val);
2825  }
2826  }
2827  }
2828 
2829  return result;
2830 }
2831 
2832 /*
2833  * call-seq:
2834  * ENV.select! { |name, value| } -> ENV or nil
2835  * ENV.select! -> Enumerator
2836  *
2837  * Equivalent to ENV#keep_if but returns +nil+ if no changes were made.
2838  */
2839 static VALUE
2841 {
2842  volatile VALUE keys;
2843  long i;
2844  int del = 0;
2845 
2846  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
2847  keys = env_keys(); /* rb_secure(4); */
2848  RBASIC(keys)->klass = 0;
2849  for (i=0; i<RARRAY_LEN(keys); i++) {
2850  VALUE val = rb_f_getenv(Qnil, RARRAY_PTR(keys)[i]);
2851  if (!NIL_P(val)) {
2852  if (!RTEST(rb_yield_values(2, RARRAY_PTR(keys)[i], val))) {
2853  FL_UNSET(RARRAY_PTR(keys)[i], FL_TAINT);
2854  env_delete(Qnil, RARRAY_PTR(keys)[i]);
2855  del++;
2856  }
2857  }
2858  }
2859  if (del == 0) return Qnil;
2860  return envtbl;
2861 }
2862 
2863 /*
2864  * call-seq:
2865  * ENV.keep_if { |name, value| } -> Hash
2866  * ENV.keep_if -> Enumerator
2867  *
2868  * Deletes every environment variable where the block evaluates to +false+.
2869  *
2870  * Returns an enumerator if no block was given.
2871  */
2872 static VALUE
2874 {
2875  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
2876  env_select_bang(ehash);
2877  return envtbl;
2878 }
2879 
2880 /*
2881  * call-seq:
2882  * ENV.clear
2883  *
2884  * Removes every environment variable.
2885  */
2886 VALUE
2888 {
2889  volatile VALUE keys;
2890  long i;
2891 
2892  keys = env_keys(); /* rb_secure(4); */
2893  for (i=0; i<RARRAY_LEN(keys); i++) {
2894  VALUE val = rb_f_getenv(Qnil, RARRAY_PTR(keys)[i]);
2895  if (!NIL_P(val)) {
2896  env_delete(Qnil, RARRAY_PTR(keys)[i]);
2897  }
2898  }
2899  return envtbl;
2900 }
2901 
2902 /*
2903  * call-seq:
2904  * ENV.to_s -> "ENV"
2905  *
2906  * Returns "ENV"
2907  */
2908 static VALUE
2910 {
2911  return rb_usascii_str_new2("ENV");
2912 }
2913 
2914 /*
2915  * call-seq:
2916  * ENV.inspect -> string
2917  *
2918  * Returns the contents of the environment as a String.
2919  */
2920 static VALUE
2922 {
2923  char **env;
2924  VALUE str, i;
2925 
2926  rb_secure(4);
2927  str = rb_str_buf_new2("{");
2928  env = GET_ENVIRON(environ);
2929  while (*env) {
2930  char *s = strchr(*env, '=');
2931 
2932  if (env != environ) {
2933  rb_str_buf_cat2(str, ", ");
2934  }
2935  if (s) {
2936  rb_str_buf_cat2(str, "\"");
2937  rb_str_buf_cat(str, *env, s-*env);
2938  rb_str_buf_cat2(str, "\"=>");
2939  i = rb_inspect(rb_str_new2(s+1));
2940  rb_str_buf_append(str, i);
2941  }
2942  env++;
2943  }
2944  FREE_ENVIRON(environ);
2945  rb_str_buf_cat2(str, "}");
2946  OBJ_TAINT(str);
2947 
2948  return str;
2949 }
2950 
2951 /*
2952  * call-seq:
2953  * ENV.to_a -> Array
2954  *
2955  * Converts the environment variables into an array of names and value arrays.
2956  *
2957  * ENV.to_a # => [["TERM", "xterm-color"], ["SHELL", "/bin/bash"], ...]
2958  *
2959  */
2960 static VALUE
2962 {
2963  char **env;
2964  VALUE ary;
2965 
2966  rb_secure(4);
2967  ary = rb_ary_new();
2968  env = GET_ENVIRON(environ);
2969  while (*env) {
2970  char *s = strchr(*env, '=');
2971  if (s) {
2972  rb_ary_push(ary, rb_assoc_new(env_str_new(*env, s-*env),
2973  env_str_new2(s+1)));
2974  }
2975  env++;
2976  }
2977  FREE_ENVIRON(environ);
2978  return ary;
2979 }
2980 
2981 /*
2982  * call-seq:
2983  * ENV.rehash
2984  *
2985  * Re-hashing the environment variables does nothing. It is provided for
2986  * compatibility with Hash.
2987  */
2988 static VALUE
2990 {
2991  return Qnil;
2992 }
2993 
2994 /*
2995  * call-seq:
2996  * ENV.length
2997  * ENV.size
2998  *
2999  * Returns the number of environment variables.
3000  */
3001 static VALUE
3003 {
3004  int i;
3005  char **env;
3006 
3007  rb_secure(4);
3008  env = GET_ENVIRON(environ);
3009  for (i=0; env[i]; i++)
3010  ;
3011  FREE_ENVIRON(environ);
3012  return INT2FIX(i);
3013 }
3014 
3015 /*
3016  * call-seq:
3017  * ENV.empty? -> true or false
3018  *
3019  * Returns true when there are no environment variables
3020  */
3021 static VALUE
3023 {
3024  char **env;
3025 
3026  rb_secure(4);
3027  env = GET_ENVIRON(environ);
3028  if (env[0] == 0) {
3029  FREE_ENVIRON(environ);
3030  return Qtrue;
3031  }
3032  FREE_ENVIRON(environ);
3033  return Qfalse;
3034 }
3035 
3036 /*
3037  * call-seq:
3038  * ENV.key?(name) -> true or false
3039  * ENV.include?(name) -> true or false
3040  * ENV.has_key?(name) -> true or false
3041  * ENV.member?(name) -> true or false
3042  *
3043  * Returns +true+ if there is an environment variable with the given +name+.
3044  */
3045 static VALUE
3047 {
3048  char *s;
3049 
3050  rb_secure(4);
3051  SafeStringValue(key);
3052  s = RSTRING_PTR(key);
3053  if (memchr(s, '\0', RSTRING_LEN(key)))
3054  rb_raise(rb_eArgError, "bad environment variable name");
3055  if (getenv(s)) return Qtrue;
3056  return Qfalse;
3057 }
3058 
3059 /*
3060  * call-seq:
3061  * ENV.assoc(name) -> Array or nil
3062  *
3063  * Returns an Array of the name and value of the environment variable with
3064  * +name+ or +nil+ if the name cannot be found.
3065  */
3066 static VALUE
3068 {
3069  char *s, *e;
3070 
3071  rb_secure(4);
3072  SafeStringValue(key);
3073  s = RSTRING_PTR(key);
3074  if (memchr(s, '\0', RSTRING_LEN(key)))
3075  rb_raise(rb_eArgError, "bad environment variable name");
3076  e = getenv(s);
3077  if (e) return rb_assoc_new(key, rb_tainted_str_new2(e));
3078  return Qnil;
3079 }
3080 
3081 /*
3082  * call-seq:
3083  * ENV.value?(value) -> true or false
3084  * ENV.has_value?(value) -> true or false
3085  *
3086  * Returns +true+ if there is an environment variable with the given +value+.
3087  */
3088 static VALUE
3090 {
3091  char **env;
3092 
3093  rb_secure(4);
3094  obj = rb_check_string_type(obj);
3095  if (NIL_P(obj)) return Qnil;
3096  rb_check_safe_obj(obj);
3097  env = GET_ENVIRON(environ);
3098  while (*env) {
3099  char *s = strchr(*env, '=');
3100  if (s++) {
3101  long len = strlen(s);
3102  if (RSTRING_LEN(obj) == len && strncmp(s, RSTRING_PTR(obj), len) == 0) {
3103  FREE_ENVIRON(environ);
3104  return Qtrue;
3105  }
3106  }
3107  env++;
3108  }
3109  FREE_ENVIRON(environ);
3110  return Qfalse;
3111 }
3112 
3113 /*
3114  * call-seq:
3115  * ENV.rassoc(value)
3116  *
3117  * Returns an Array of the name and value of the environment variable with
3118  * +value+ or +nil+ if the value cannot be found.
3119  */
3120 static VALUE
3122 {
3123  char **env;
3124 
3125  rb_secure(4);
3126  obj = rb_check_string_type(obj);
3127  if (NIL_P(obj)) return Qnil;
3128  rb_check_safe_obj(obj);
3129  env = GET_ENVIRON(environ);
3130  while (*env) {
3131  char *s = strchr(*env, '=');
3132  if (s++) {
3133  long len = strlen(s);
3134  if (RSTRING_LEN(obj) == len && strncmp(s, RSTRING_PTR(obj), len) == 0) {
3135  VALUE result = rb_assoc_new(rb_tainted_str_new(*env, s-*env-1), obj);
3136  FREE_ENVIRON(environ);
3137  return result;
3138  }
3139  }
3140  env++;
3141  }
3142  FREE_ENVIRON(environ);
3143  return Qnil;
3144 }
3145 
3146 /*
3147  * call-seq:
3148  * ENV.key(value) -> name
3149  *
3150  * Returns the name of the environment variable with +value+. If the value is
3151  * not found +nil+ is returned.
3152  */
3153 static VALUE
3155 {
3156  char **env;
3157  VALUE str;
3158 
3159  rb_secure(4);
3160  SafeStringValue(value);
3161  env = GET_ENVIRON(environ);
3162  while (*env) {
3163  char *s = strchr(*env, '=');
3164  if (s++) {
3165  long len = strlen(s);
3166  if (RSTRING_LEN(value) == len && strncmp(s, RSTRING_PTR(value), len) == 0) {
3167  str = env_str_new(*env, s-*env-1);
3168  FREE_ENVIRON(environ);
3169  return str;
3170  }
3171  }
3172  env++;
3173  }
3174  FREE_ENVIRON(environ);
3175  return Qnil;
3176 }
3177 
3178 /*
3179  * call-seq:
3180  * ENV.index(value) -> key
3181  *
3182  * Deprecated method that is equivalent to ENV.key
3183  */
3184 static VALUE
3186 {
3187  rb_warn("ENV.index is deprecated; use ENV.key");
3188  return env_key(dmy, value);
3189 }
3190 
3191 /*
3192  * call-seq:
3193  * ENV.to_hash -> hash
3194  * ENV.to_h -> hash
3195  *
3196  * Creates a hash with a copy of the environment variables.
3197  *
3198  */
3199 static VALUE
3201 {
3202  char **env;
3203  VALUE hash;
3204 
3205  rb_secure(4);
3206  hash = rb_hash_new();
3207  env = GET_ENVIRON(environ);
3208  while (*env) {
3209  char *s = strchr(*env, '=');
3210  if (s) {
3211  rb_hash_aset(hash, env_str_new(*env, s-*env),
3212  env_str_new2(s+1));
3213  }
3214  env++;
3215  }
3216  FREE_ENVIRON(environ);
3217  return hash;
3218 }
3219 
3220 /*
3221  * call-seq:
3222  * ENV.reject { |name, value| } -> Hash
3223  * ENV.reject -> Enumerator
3224  *
3225  * Same as ENV#delete_if, but works on (and returns) a copy of the
3226  * environment.
3227  */
3228 static VALUE
3230 {
3231  return rb_hash_delete_if(env_to_hash());
3232 }
3233 
3234 /*
3235  * call-seq:
3236  * ENV.shift -> Array or nil
3237  *
3238  * Removes an environment variable name-value pair from ENV and returns it as
3239  * an Array. Returns +nil+ if when the environment is empty.
3240  */
3241 static VALUE
3243 {
3244  char **env;
3245  VALUE result = Qnil;
3246 
3247  rb_secure(4);
3248  env = GET_ENVIRON(environ);
3249  if (*env) {
3250  char *s = strchr(*env, '=');
3251  if (s) {
3252  VALUE key = env_str_new(*env, s-*env);
3254  env_delete(Qnil, key);
3255  result = rb_assoc_new(key, val);
3256  }
3257  }
3258  FREE_ENVIRON(environ);
3259  return result;
3260 }
3261 
3262 /*
3263  * call-seq:
3264  * ENV.invert -> Hash
3265  *
3266  * Returns a new hash created by using environment variable names as values
3267  * and values as names.
3268  */
3269 static VALUE
3271 {
3272  return rb_hash_invert(env_to_hash());
3273 }
3274 
3275 static int
3277 {
3278  env_aset(Qnil, key, val);
3279  if (rb_ary_includes(keys, key)) {
3280  rb_ary_delete(keys, key);
3281  }
3282  return ST_CONTINUE;
3283 }
3284 
3285 /*
3286  * call-seq:
3287  * ENV.replace(hash) -> env
3288  *
3289  * Replaces the contents of the environment variables with the contents of
3290  * +hash+.
3291  */
3292 static VALUE
3294 {
3295  volatile VALUE keys;
3296  long i;
3297 
3298  keys = env_keys(); /* rb_secure(4); */
3299  if (env == hash) return env;
3300  hash = to_hash(hash);
3301  rb_hash_foreach(hash, env_replace_i, keys);
3302 
3303  for (i=0; i<RARRAY_LEN(keys); i++) {
3304  env_delete(env, RARRAY_PTR(keys)[i]);
3305  }
3306  return env;
3307 }
3308 
3309 static int
3311 {
3312  if (rb_block_given_p()) {
3313  val = rb_yield_values(3, key, rb_f_getenv(Qnil, key), val);
3314  }
3315  env_aset(Qnil, key, val);
3316  return ST_CONTINUE;
3317 }
3318 
3319 /*
3320  * call-seq:
3321  * ENV.update(hash) -> Hash
3322  * ENV.update(hash) { |name, old_value, new_value| } -> Hash
3323  *
3324  * Adds the contents of +hash+ to the environment variables. If no block is
3325  * specified entries with duplicate keys are overwritten, otherwise the value
3326  * of each duplicate name is determined by calling the block with the key, its
3327  * value from the environment and its value from the hash.
3328  */
3329 static VALUE
3331 {
3332  rb_secure(4);
3333  if (env == hash) return env;
3334  hash = to_hash(hash);
3335  rb_hash_foreach(hash, env_update_i, 0);
3336  return env;
3337 }
3338 
3339 /*
3340  * A Hash is a dictionary-like collection of unique keys and their values.
3341  * Also called associative arrays, they are similar to Arrays, but where an
3342  * Array uses integers as its index, a Hash allows you to use any object
3343  * type.
3344  *
3345  * Hashes enumerate their values in the order that the corresponding keys
3346  * were inserted.
3347  *
3348  * A Hash can be easily created by using its implicit form:
3349  *
3350  * grades = { "Jane Doe" => 10, "Jim Doe" => 6 }
3351  *
3352  * Hashes allow an alternate syntax form when your keys are always symbols.
3353  * Instead of
3354  *
3355  * options = { :font_size => 10, :font_family => "Arial" }
3356  *
3357  * You could write it as:
3358  *
3359  * options = { font_size: 10, font_family: "Arial" }
3360  *
3361  * Each named key is a symbol you can access in hash:
3362  *
3363  * options[:font_size] # => 10
3364  *
3365  * A Hash can also be created through its ::new method:
3366  *
3367  * grades = Hash.new
3368  * grades["Dorothy Doe"] = 9
3369  *
3370  * Hashes have a <em>default value</em> that is returned when accessing
3371  * keys that do not exist in the hash. If no default is set +nil+ is used.
3372  * You can set the default value by sending it as an argument to Hash.new:
3373  *
3374  * grades = Hash.new(0)
3375  *
3376  * Or by using the #default= method:
3377  *
3378  * grades = {"Timmy Doe" => 8}
3379  * grades.default = 0
3380  *
3381  * Accessing a value in a Hash requires using its key:
3382  *
3383  * puts grades["Jane Doe"] # => 10
3384  *
3385  * === Common Uses
3386  *
3387  * Hashes are an easy way to represent data structures, such as
3388  *
3389  * books = {}
3390  * books[:matz] = "The Ruby Language"
3391  * books[:black] = "The Well-Grounded Rubyist"
3392  *
3393  * Hashes are also commonly used as a way to have named parameters in
3394  * functions. Note that no brackets are used below. If a hash is the last
3395  * argument on a method call, no braces are needed, thus creating a really
3396  * clean interface:
3397  *
3398  * Person.create(name: "John Doe", age: 27)
3399  *
3400  * def self.create(params)
3401  * @name = params[:name]
3402  * @age = params[:age]
3403  * end
3404  *
3405  * === Hash Keys
3406  *
3407  * Two objects refer to the same hash key when their <code>hash</code> value
3408  * is identical and the two objects are <code>eql?</code> to each other.
3409  *
3410  * A user-defined class may be used as a hash key if the <code>hash</code>
3411  * and <code>eql?</code> methods are overridden to provide meaningful
3412  * behavior. By default, separate instances refer to separate hash keys.
3413  *
3414  * A typical implementation of <code>hash</code> is based on the
3415  * object's data while <code>eql?</code> is usually aliased to the overridden
3416  * <code>==</code> method:
3417  *
3418  * class Book
3419  * attr_reader :author, :title
3420  *
3421  * def initialize(author, title)
3422  * @author = author
3423  * @title = title
3424  * end
3425  *
3426  * def ==(other)
3427  * self.class === other and
3428  * other.author == @author and
3429  * other.title == @title
3430  * end
3431  *
3432  * alias eql? ==
3433  *
3434  * def hash
3435  * @author.hash ^ @title.hash # XOR
3436  * end
3437  * end
3438  *
3439  * book1 = Book.new 'matz', 'Ruby in a Nutshell'
3440  * book2 = Book.new 'matz', 'Ruby in a Nutshell'
3441  *
3442  * reviews = {}
3443  *
3444  * reviews[book1] = 'Great reference!'
3445  * reviews[book2] = 'Nice and compact!'
3446  *
3447  * reviews.length #=> 1
3448  *
3449  * See also Object#hash and Object#eql?
3450  */
3451 
3452 void
3454 {
3455 #undef rb_intern
3456 #define rb_intern(str) rb_intern_const(str)
3457 
3458  id_hash = rb_intern("hash");
3459  id_yield = rb_intern("yield");
3460  id_default = rb_intern("default");
3461 
3462  rb_cHash = rb_define_class("Hash", rb_cObject);
3463 
3465 
3469  rb_define_method(rb_cHash,"initialize", rb_hash_initialize, -1);
3470  rb_define_method(rb_cHash,"initialize_copy", rb_hash_initialize_copy, 1);
3472 
3473  rb_define_method(rb_cHash,"to_hash", rb_hash_to_hash, 0);
3476  rb_define_method(rb_cHash,"inspect", rb_hash_inspect, 0);
3477  rb_define_alias(rb_cHash, "to_s", "inspect");
3478 
3485  rb_define_method(rb_cHash,"store", rb_hash_aset, 2);
3486  rb_define_method(rb_cHash,"default", rb_hash_default, -1);
3488  rb_define_method(rb_cHash,"default_proc", rb_hash_default_proc, 0);
3489  rb_define_method(rb_cHash,"default_proc=", rb_hash_set_default_proc, 1);
3493  rb_define_method(rb_cHash,"length", rb_hash_size, 0);
3495 
3496  rb_define_method(rb_cHash,"each_value", rb_hash_each_value, 0);
3497  rb_define_method(rb_cHash,"each_key", rb_hash_each_key, 0);
3498  rb_define_method(rb_cHash,"each_pair", rb_hash_each_pair, 0);
3500 
3503  rb_define_method(rb_cHash,"values_at", rb_hash_values_at, -1);
3504 
3507  rb_define_method(rb_cHash,"delete_if", rb_hash_delete_if, 0);
3508  rb_define_method(rb_cHash,"keep_if", rb_hash_keep_if, 0);
3516  rb_define_method(rb_cHash,"replace", rb_hash_replace, 1);
3519  rb_define_method(rb_cHash, "assoc", rb_hash_assoc, 1);
3520  rb_define_method(rb_cHash, "rassoc", rb_hash_rassoc, 1);
3521  rb_define_method(rb_cHash, "flatten", rb_hash_flatten, -1);
3522 
3523  rb_define_method(rb_cHash,"include?", rb_hash_has_key, 1);
3524  rb_define_method(rb_cHash,"member?", rb_hash_has_key, 1);
3525  rb_define_method(rb_cHash,"has_key?", rb_hash_has_key, 1);
3526  rb_define_method(rb_cHash,"has_value?", rb_hash_has_value, 1);
3529 
3530  rb_define_method(rb_cHash,"compare_by_identity", rb_hash_compare_by_id, 0);
3531  rb_define_method(rb_cHash,"compare_by_identity?", rb_hash_compare_by_id_p, 0);
3532 
3533  /* Document-class: ENV
3534  *
3535  * ENV is a hash-like accessor for environment variables.
3536  */
3537 
3538  /*
3539  * Hack to get RDoc to regard ENV as a class:
3540  * envtbl = rb_define_class("ENV", rb_cObject);
3541  */
3542  origenviron = environ;
3545 
3588 
3589  /*
3590  * ENV is a Hash-like accessor for environment variables.
3591  *
3592  * See ENV (the class) for more details.
3593  */
3595 }
RUBY_EXTERN VALUE rb_cString
Definition: ruby.h:1456
VALUE value
Definition: hash.c:1941
void rb_define_global_const(const char *, VALUE)
Definition: variable.c:2218
#define RHASH_UPDATE(hash, key, func, arg)
Definition: hash.c:302
static VALUE empty_hash_alloc(VALUE klass)
Definition: hash.c:224
const char * rb_builtin_class_name(VALUE x)
Definition: error.c:419
VALUE rb_hash(VALUE obj)
Definition: hash.c:66
static VALUE hash_foreach_call(VALUE arg)
Definition: hash.c:190
static int rb_hash_invert_i(VALUE key, VALUE value, VALUE hash)
Definition: hash.c:1839
Definition: st.h:108
static int each_pair_i(VALUE key, VALUE value)
Definition: hash.c:1408
static int rb_hash_update_callback(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
Definition: hash.c:1867
#define FL_EXIVAR
Definition: ruby.h:1117
st_foreach_func * func
Definition: hash.c:116
#define RARRAY_LEN(a)
Definition: ruby.h:899
static VALUE env_each_value(VALUE ehash)
Definition: hash.c:2676
#define FALSE
Definition: nkf.h:174
void rb_enc_copy(VALUE obj1, VALUE obj2)
Definition: encoding.c:856
static VALUE rb_hash_each_value(VALUE hash)
Definition: hash.c:1367
static VALUE env_delete_m(VALUE obj, VALUE name)
Definition: hash.c:2245
size_t strlen(const char *)
rb_hash_update_func * func
Definition: hash.c:1942
static VALUE rb_hash_has_value(VALUE hash, VALUE val)
Definition: hash.c:1678
int i
Definition: win32ole.c:784
static int path_tainted
Definition: hash.c:2162
#define T_FIXNUM
Definition: ruby.h:497
Definition: st.h:77
#define FREE_ENVIRON(e)
Definition: hash.c:2181
static int hash_aset_str(st_data_t *key, st_data_t *val, st_data_t arg, int existing)
Definition: hash.c:1171
Definition: st.h:108
static void path_tainted_p(const char *path)
Definition: hash.c:2341
static int replace_i(VALUE key, VALUE val, VALUE hash)
Definition: hash.c:1226
static ID id_yield
Definition: hash.c:44
VALUE rb_hash_dup(VALUE hash)
Definition: hash.c:240
static VALUE rb_hash_s_try_convert(VALUE, VALUE)
Definition: hash.c:478
static int envix(const char *nam)
Definition: hash.c:2377
VALUE rb_yield_values(int n,...)
Definition: vm_eval.c:944
static int max(int a, int b)
Definition: strftime.c:141
static ID id_hash
Definition: hash.c:44
static int keep_if_i(VALUE key, VALUE value, VALUE hash)
Definition: hash.c:1076
static void no_new_key(void)
Definition: hash.c:283
static int rb_hash_update_block_callback(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
Definition: hash.c:1883
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1497
VALUE rb_exec_recursive_outer(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE)
Definition: thread.c:4904
static int env_update_i(VALUE key, VALUE val)
Definition: hash.c:3310
VALUE rb_eKeyError
Definition: error.c:519
static VALUE rb_hash_size(VALUE hash)
Definition: hash.c:1316
static VALUE rb_hash_empty_p(VALUE hash)
Definition: hash.c:1335
#define RHASH_ITER_LEV(h)
Definition: ruby.h:929
static VALUE env_invert(void)
Definition: hash.c:3270
#define FL_TAINT
Definition: ruby.h:1115
#define CLASS_OF(v)
Definition: ruby.h:448
static VALUE rb_hash_default(int argc, VALUE *argv, VALUE hash)
Definition: hash.c:682
#define ENVMATCH(n1, n2)
Definition: hash.c:2187
#define Qtrue
Definition: ruby.h:434
int st_insert(st_table *, st_data_t, st_data_t)
static void rb_hash_modify(VALUE hash)
Definition: hash.c:275
VALUE rb_cHash
Definition: hash.c:41
static VALUE env_str_new2(const char *ptr)
Definition: hash.c:2205
st_table * tbl
Definition: hash.c:485
st_index_t rb_hash_end(st_index_t)
VALUE rb_hash_select_bang(VALUE hash)
Definition: hash.c:1094
static int keys_i(VALUE key, VALUE value, VALUE ary)
Definition: hash.c:1567
static VALUE env_to_s(void)
Definition: hash.c:2909
int rb_env_path_tainted(void)
Definition: hash.c:2356
static VALUE env_keys(void)
Definition: hash.c:2580
VALUE val
Definition: hash.c:884
VALUE rb_eTypeError
Definition: error.c:516
static VALUE env_delete_if(VALUE ehash)
Definition: hash.c:2771
st_table * tbl
Definition: hash.c:1690
#define HASH_PROC_DEFAULT
Definition: hash.c:33
static VALUE env_shift(void)
Definition: hash.c:3242
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:822
static int eql_i(VALUE key, VALUE val1, VALUE arg)
Definition: hash.c:1695
VALUE rb_str_buf_new2(const char *)
void st_free_table(st_table *)
Definition: st.c:334
#define HASH_DELETED
Definition: hash.c:32
static VALUE env_each_pair(VALUE ehash)
Definition: hash.c:2701
static ID id_default
Definition: hash.c:44
VALUE rb_hash_update_by(VALUE hash1, VALUE hash2, rb_hash_update_func *func)
Definition: hash.c:1971
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:773
struct st_table * rb_hash_tbl(VALUE hash)
Definition: hash.c:266
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *state)
Definition: eval.c:771
static int assoc_i(VALUE key, VALUE val, VALUE arg)
Definition: hash.c:2014
VALUE rb_to_int(VALUE)
Definition: object.c:2482
#define Check_Type(v, t)
Definition: ruby.h:539
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1788
static VALUE rb_hash_shift(VALUE hash)
Definition: hash.c:922
static VALUE inspect_hash(VALUE hash, VALUE dummy, int recur)
Definition: hash.c:1497
VALUE rb_convert_type(VALUE, int, const char *, const char *)
Definition: object.c:2425
VALUE rb_exec_recursive(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE)
Definition: thread.c:4881
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
static VALUE rb_hash_equal(VALUE hash1, VALUE hash2)
Definition: hash.c:1776
#define T_HASH
Definition: ruby.h:493
VALUE rb_tainted_str_new2(const char *)
st_index_t rb_str_hash(VALUE)
Definition: string.c:2248
#define ENVNMATCH(s1, s2, n)
Definition: hash.c:2188
VALUE rb_eSecurityError
Definition: error.c:525
static int hash_foreach_iter(st_data_t key, st_data_t value, st_data_t argp)
Definition: hash.c:154
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:684
static int env_path_tainted(const char *)
Definition: hash.c:2347
VALUE rb_hash_lookup(VALUE hash, VALUE key)
Definition: hash.c:592
static int values_i(VALUE key, VALUE value, VALUE ary)
Definition: hash.c:1597
st_data_t st_index_t
Definition: st.h:63
#define RHASH_UPDATE_ITER(hash, iter_lev, key, func, arg)
Definition: hash.c:298
static VALUE env_keep_if(VALUE ehash)
Definition: hash.c:2873
VALUE rb_hash_fetch(VALUE hash, VALUE key)
Definition: hash.c:655
static VALUE rb_hash_each_key(VALUE hash)
Definition: hash.c:1400
#define FIXNUM_P(f)
Definition: ruby.h:355
rb_encoding * rb_utf8_encoding(void)
Definition: encoding.c:1166
static int rb_hash_search_value(VALUE key, VALUE value, VALUE arg)
Definition: hash.c:1653
char ** environ
Definition: missing-pips.c:6
VALUE rb_str_buf_append(VALUE, VALUE)
Definition: string.c:2109
static VALUE rb_hash_replace(VALUE hash, VALUE hash2)
Definition: hash.c:1280
VALUE result
Definition: hash.c:1689
static int shift_i(VALUE key, VALUE value, VALUE arg)
Definition: hash.c:888
#define rb_intern(str)
#define NOINSERT_UPDATE_CALLBACK(func)
Definition: hash.c:288
#define OBJ_TAINTED(x)
Definition: ruby.h:1153
#define RHASH_IFNONE(h)
Definition: ruby.h:930
const char * rb_obj_classname(VALUE)
Definition: variable.c:396
static VALUE rb_hash_fetch_m(int argc, VALUE *argv, VALUE hash)
Definition: hash.c:627
#define RHASH_TBL(h)
Definition: ruby.h:928
static VALUE envtbl
Definition: hash.c:43
static VALUE env_rassoc(VALUE dmy, VALUE obj)
Definition: hash.c:3121
static VALUE env_each_key(VALUE ehash)
Definition: hash.c:2627
VALUE rb_str_buf_cat(VALUE, const char *, long)
Definition: string.c:1951
#define NEWOBJ_OF(obj, type, klass, flags)
Definition: ruby.h:683
Win32OLEIDispatch * p
Definition: win32ole.c:786
void rb_hash_foreach(VALUE hash, int(*func)(ANYARGS), VALUE farg)
Definition: hash.c:200
int args
Definition: win32ole.c:785
VALUE rb_obj_dup(VALUE)
Definition: object.c:347
#define RB_TYPE_P(obj, type)
Definition: ruby.h:1537
const struct st_hash_type st_hashtype_num
#define RHASH(obj)
Definition: ruby.h:1102
#define fail()
#define FL_UNTRUSTED
Definition: ruby.h:1116
int st_lookup(st_table *, st_data_t, st_data_t *)
static int rb_hash_rehash_i(VALUE key, VALUE value, VALUE arg)
Definition: hash.c:489
static VALUE env_key(VALUE dmy, VALUE value)
Definition: hash.c:3154
#define FL_TEST(x, f)
Definition: ruby.h:1146
void Init_Hash(void)
Definition: hash.c:3453
#define ALLOC_N(type, n)
Definition: ruby.h:1223
int rb_block_given_p(void)
Definition: eval.c:672
static VALUE rb_hash_reject(VALUE hash)
Definition: hash.c:1014
static VALUE env_assoc(VALUE env, VALUE key)
Definition: hash.c:3067
static VALUE rb_hash_set_default_proc(VALUE hash, VALUE proc)
Definition: hash.c:762
VALUE rb_hash_reject_bang(VALUE hash)
Definition: hash.c:988
#define val
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1426
VALUE rb_eRuntimeError
Definition: error.c:515
VALUE rb_exec_recursive_paired(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE, VALUE)
Definition: thread.c:4892
static VALUE env_select_bang(VALUE ehash)
Definition: hash.c:2840
static VALUE rb_hash_initialize_copy(VALUE hash, VALUE hash2)
Definition: hash.c:1234
char * ruby_strdup(const char *)
Definition: util.c:456
static int hash_i(VALUE key, VALUE val, VALUE arg)
Definition: hash.c:1796
static VALUE rb_env_size(VALUE ehash)
Definition: hash.c:2600
VALUE rb_ary_new(void)
Definition: array.c:424
VALUE rb_str_buf_cat2(VALUE, const char *)
Definition: string.c:1961
static int each_value_i(VALUE key, VALUE value)
Definition: hash.c:1341
#define snprintf
Definition: subst.h:6
VALUE rb_locale_str_new(const char *, long)
Definition: string.c:596
#define NIL_P(v)
Definition: ruby.h:446
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:488
static VALUE rb_hash_eql(VALUE hash1, VALUE hash2)
Definition: hash.c:1790
static VALUE env_delete(VALUE obj, VALUE name)
Definition: hash.c:2212
int st_delete(st_table *, st_data_t *, st_data_t *)
rb_atomic_t cnt[RUBY_NSIG]
Definition: signal.c:432
VALUE rb_hash_update_func(VALUE newkey, VALUE oldkey, VALUE value)
Definition: intern.h:479
int rb_foreach_func(VALUE, VALUE, VALUE)
Definition: hash.c:145
#define TYPE(x)
Definition: ruby.h:513
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
Definition: string.c:563
int argc
Definition: ruby.c:130
static VALUE rb_hash_to_h(VALUE hash)
Definition: hash.c:1551
static VALUE env_reject(void)
Definition: hash.c:3229
#define Qfalse
Definition: ruby.h:433
static VALUE rb_hash_each_pair(VALUE hash)
Definition: hash.c:1437
static VALUE rb_hash_values(VALUE hash)
Definition: hash.c:1616
#define rb_sourcefile()
Definition: tcltklib.c:97
int eql
Definition: hash.c:1691
static const struct st_hash_type objhash
Definition: hash.c:104
#define T_BIGNUM
Definition: ruby.h:495
static int rb_any_cmp(VALUE a, VALUE b)
Definition: hash.c:47
VALUE rb_obj_alloc(VALUE)
Definition: object.c:1740
#define identhash
Definition: hash.c:110
#define RBIGNUM_DIGITS(b)
Definition: ruby.h:1087
static VALUE rb_hash_to_hash(VALUE hash)
Definition: hash.c:1537
VALUE hash
Definition: hash.c:148
#define GET_ENVIRON(e)
Definition: hash.c:2180
static VALUE env_replace(VALUE env, VALUE hash)
Definition: hash.c:3293
VALUE rb_str_resize(VALUE, long)
Definition: string.c:1854
static VALUE rb_hash_set_default(VALUE hash, VALUE ifnone)
Definition: hash.c:716
int rb_str_hash_cmp(VALUE, VALUE)
Definition: string.c:2258
void st_foreach_safe(st_table *table, int(*func)(ANYARGS), st_data_t a)
Definition: hash.c:133
static void rb_hash_modify_check(VALUE hash)
Definition: hash.c:258
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1539
static VALUE rb_hash_s_create(int argc, VALUE *argv, VALUE klass)
Definition: hash.c:390
static char ** origenviron
Definition: hash.c:2164
static int select_i(VALUE key, VALUE value, VALUE result)
Definition: hash.c:1043
#define RSTRING_LEN(str)
Definition: ruby.h:862
static VALUE rb_hash_index(VALUE hash, VALUE value)
Definition: hash.c:826
VALUE rb_yield(VALUE)
Definition: vm_eval.c:933
static VALUE env_index(VALUE dmy, VALUE value)
Definition: hash.c:3185
static VALUE rb_f_getenv(VALUE obj, VALUE name)
Definition: hash.c:2264
#define REALLOC_N(var, type, n)
Definition: ruby.h:1225
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:108
int errno
#define TRUE
Definition: nkf.h:175
#define T_DATA
Definition: ruby.h:500
static VALUE hash_foreach_ensure(VALUE hash)
Definition: hash.c:178
VALUE rb_obj_is_proc(VALUE)
Definition: proc.c:91
static VALUE env_inspect(void)
Definition: hash.c:2921
VALUE rb_funcall2(VALUE, ID, int, const VALUE *)
Calls a method.
Definition: vm_eval.c:804
#define OBJ_UNTRUSTED(x)
Definition: ruby.h:1155
VALUE rb_mEnumerable
Definition: enum.c:20
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1272
static VALUE rb_hash_update(VALUE hash1, VALUE hash2)
Definition: hash.c:1926
VALUE rb_hash_delete(VALUE hash, VALUE key)
Definition: hash.c:869
static VALUE rb_hash_key(VALUE hash, VALUE value)
Definition: hash.c:812
int rb_eql(VALUE, VALUE)
Definition: object.c:67
VALUE rb_ary_delete(VALUE ary, VALUE item)
Definition: array.c:2748
VALUE rb_hash_assoc(VALUE hash, VALUE obj)
Definition: hash.c:2040
#define malloc
Definition: ripper.c:98
static VALUE env_has_key(VALUE env, VALUE key)
Definition: hash.c:3046
VALUE rb_hash_new(void)
Definition: hash.c:234
#define unsetenv(name, val)
Definition: util.h:65
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1570
VALUE rb_check_hash_type(VALUE hash)
Definition: hash.c:461
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4308
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Definition: array.c:545
unsigned long ID
Definition: ruby.h:105
#define PATH_ENV
Definition: defines.h:218
#define RHASH_SIZE(h)
Definition: ruby.h:931
#define Qnil
Definition: ruby.h:435
static VALUE rb_hash_compare_by_id_p(VALUE hash)
Definition: hash.c:2152
#define BUILTIN_TYPE(x)
Definition: ruby.h:510
#define OBJ_TAINT(x)
Definition: ruby.h:1154
unsigned long VALUE
Definition: ruby.h:104
rb_encoding * rb_locale_encoding(void)
Definition: encoding.c:1212
static VALUE result
Definition: nkf.c:40
VALUE rb_hash_clear(VALUE hash)
Definition: hash.c:1148
static VALUE env_values(void)
Definition: hash.c:2647
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
Definition: intern.h:215
static int clear_i(VALUE key, VALUE value, VALUE dummy)
Definition: hash.c:1131
#define RBASIC(obj)
Definition: ruby.h:1094
VALUE rb_hash_keep_if(VALUE hash)
Definition: hash.c:1121
static VALUE hash_alloc(VALUE klass)
Definition: hash.c:214
char * strchr(char *, char)
void rb_extend_object(VALUE obj, VALUE module)
Definition: eval.c:1240
#define setenv(name, val)
Definition: util.h:64
static VALUE env_aset(VALUE obj, VALUE nm, VALUE val)
Definition: hash.c:2538
static VALUE env_to_a(void)
Definition: hash.c:2961
char * getenv()
static VALUE hash_equal(VALUE hash1, VALUE hash2, int eql)
Definition: hash.c:1725
static VALUE rb_hash_flatten(int argc, VALUE *argv, VALUE hash)
Definition: hash.c:2103
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
Definition: eval.c:804
VALUE rb_str_ellipsize(VALUE, long)
Shortens str and adds three dots, an ellipsis, if it is longer than len characters.
Definition: string.c:7727
VALUE rb_proc_lambda_p(VALUE)
Definition: proc.c:231
static int each_key_i(VALUE key, VALUE value)
Definition: hash.c:1375
rb_foreach_func * func
Definition: hash.c:149
VALUE key
Definition: hash.c:883
void ruby_unsetenv(const char *name)
Definition: hash.c:2523
void rb_sys_fail(const char *mesg)
Definition: error.c:1907
static VALUE env_select(VALUE ehash)
Definition: hash.c:2809
VALUE rb_hash_freeze(VALUE hash)
Definition: hash.c:36
void xfree(void *)
#define FL_UNSET(x, f)
Definition: ruby.h:1150
#define RUBY_DTRACE_HASH_CREATE_ENABLED()
Definition: probes.h:59
NORETURN(static void no_new_key(void))
int rb_respond_to(VALUE, ID)
Definition: vm_method.c:1598
st_index_t st_hash(const void *ptr, size_t len, st_index_t h)
Definition: st.c:1319
static VALUE env_none(void)
Definition: hash.c:2989
static VALUE env_size(void)
Definition: hash.c:3002
static int delete_if_i(VALUE key, VALUE value, VALUE hash)
Definition: hash.c:943
static VALUE rb_hash_keys(VALUE hash)
Definition: hash.c:1586
#define recur(fmt)
#define RSTRING_PTR(str)
Definition: ruby.h:866
VALUE rb_usascii_str_new2(const char *)
st_table * st_init_table_with_size(const struct st_hash_type *, st_index_t)
Definition: st.c:229
VALUE rb_equal(VALUE, VALUE)
Definition: object.c:56
static int inspect_i(VALUE key, VALUE value, VALUE str)
Definition: hash.c:1475
static VALUE rb_hash_compare_by_id(VALUE hash)
Definition: hash.c:2134
VALUE rb_hash_lookup2(VALUE hash, VALUE key, VALUE def)
Definition: hash.c:581
static VALUE to_hash(VALUE hash)
Definition: hash.c:455
#define rb_check_arity(argc, min, max)
Definition: intern.h:277
#define INT2FIX(i)
Definition: ruby.h:241
static int hash_aset(st_data_t *key, st_data_t *val, st_data_t arg, int existing)
Definition: hash.c:1164
int rb_sourceline(void)
Definition: vm.c:884
int rb_path_check(const char *path)
Definition: file.c:5239
VALUE rb_check_convert_type(VALUE, int, const char *, const char *)
Definition: object.c:2438
VALUE rb_block_proc(void)
Definition: proc.c:458
st_data_t arg
Definition: hash.c:117
VALUE rb_str_buf_cat_ascii(VALUE, const char *)
Definition: string.c:2085
#define ANYARGS
Definition: defines.h:57
static VALUE env_to_hash(void)
Definition: hash.c:3200
static VALUE rb_hash_has_key(VALUE hash, VALUE key)
Definition: hash.c:1642
VALUE rb_check_array_type(VALUE ary)
Definition: array.c:557
VALUE rb_hash_aref(VALUE hash, VALUE key)
Definition: hash.c:570
#define RARRAY_PTR(a)
Definition: ruby.h:904
static VALUE hash_default_value(VALUE hash, VALUE key)
Definition: hash.c:542
static VALUE env_values_at(int argc, VALUE *argv)
Definition: hash.c:2786
VALUE rb_check_string_type(VALUE)
Definition: string.c:1509
uint8_t key[16]
Definition: random.c:1370
VALUE rb_any_to_s(VALUE)
Definition: object.c:393
VALUE rb_ary_includes(VALUE ary, VALUE item)
Definition: array.c:3654
static int shift_i_safe(VALUE key, VALUE value, VALUE arg)
Definition: hash.c:899
static st_index_t rb_any_hash(VALUE a)
Definition: hash.c:84
#define LONG2FIX(i)
Definition: ruby.h:242
#define RTEST(v)
Definition: ruby.h:445
#define T_STRING
Definition: ruby.h:490
#define OBJ_INFECT(x, s)
Definition: ruby.h:1157
void rb_check_safe_obj(VALUE)
Definition: safe.c:113
int st_foreach_check(st_table *, int(*)(ANYARGS), st_data_t, st_data_t)
Definition: st.c:914
st_index_t rb_hash_uint(st_index_t, st_index_t)
static VALUE rb_hash_invert(VALUE hash)
Definition: hash.c:1858
rb_encoding * rb_filesystem_encoding(void)
Definition: encoding.c:1246
int rb_method_basic_definition_p(VALUE, ID)
Definition: vm_method.c:1532
v
Definition: win32ole.c:798
DWORD rb_w32_osver(void)
Definition: win32.c:269
static int foreach_safe_i(st_data_t key, st_data_t value, struct foreach_safe_arg *arg)
Definition: hash.c:121
static VALUE rb_hash_hash(VALUE hash)
Definition: hash.c:1833
static VALUE recursive_eql(VALUE hash, VALUE dt, int recur)
Definition: hash.c:1712
const struct st_hash_type * type
Definition: st.h:78
static unsigned int hash(const char *str, unsigned int len)
Definition: lex.c:56
Definition: st.h:108
static int rassoc_i(VALUE key, VALUE val, VALUE arg)
Definition: hash.c:2051
#define SafeStringValue(v)
Definition: ruby.h:552
VALUE rb_ary_new2(long capa)
Definition: array.c:417
VALUE rb_filesystem_str_new_cstr(const char *)
Definition: string.c:614
st_table * tbl
Definition: hash.c:115
#define rb_safe_level()
Definition: tcltklib.c:94
VALUE hash
Definition: hash.c:484
static VALUE rb_hash_merge(VALUE hash1, VALUE hash2)
Definition: hash.c:2008
const char * name
Definition: nkf.c:208
#define FL_SET(x, f)
Definition: ruby.h:1149
void st_cleanup_safe(st_table *, st_data_t)
Definition: st.c:802
static int rb_hash_update_func_callback(st_data_t *key, st_data_t *value, st_data_t arg0, int existing)
Definition: hash.c:1946
static VALUE recursive_hash(VALUE hash, VALUE dummy, int recur)
Definition: hash.c:1808
unsigned long st_data_t
Definition: st.h:35
VALUE rb_hash_values_at(int argc, VALUE *argv, VALUE hash)
Definition: hash.c:1031
VALUE rb_str_new_frozen(VALUE)
Definition: string.c:713
VALUE rb_inspect(VALUE)
Definition: object.c:411
st_table * st_copy(st_table *)
Definition: st.c:663
VALUE arg
Definition: hash.c:150
static int key_i(VALUE key, VALUE value, VALUE arg)
Definition: hash.c:786
static VALUE rb_hash_rehash(VALUE hash)
Definition: hash.c:518
int st_foreach_func(st_data_t, st_data_t, st_data_t)
Definition: hash.c:112
VALUE rb_env_clear(void)
Definition: hash.c:2887
static int env_replace_i(VALUE key, VALUE val, VALUE keys)
Definition: hash.c:3276
void rb_secure(int)
Definition: safe.c:79
void st_clear(st_table *)
Definition: st.c:308
VALUE rb_hash_delete_if(VALUE hash)
Definition: hash.c:969
Definition: ruby.h:921
#define rb_check_frozen(obj)
Definition: intern.h:258
int rb_proc_arity(VALUE)
Definition: proc.c:681
static VALUE env_empty_p(void)
Definition: hash.c:3022
st_table * st_init_table(const struct st_hash_type *)
Definition: st.c:266
static int to_a_i(VALUE key, VALUE value, VALUE ary)
Definition: hash.c:1445
VALUE rb_obj_freeze(VALUE)
Definition: object.c:1012
void rb_copy_generic_ivar(VALUE, VALUE)
Definition: variable.c:1047
#define SPECIAL_CONST_P(x)
Definition: ruby.h:1143
VALUE rb_tainted_str_new(const char *, long)
#define RHASH_EMPTY_P(h)
Definition: ruby.h:932
#define SYMBOL_P(x)
Definition: ruby.h:362
#define env
static VALUE env_reject_bang(VALUE ehash)
Definition: hash.c:2738
static VALUE env_fetch(int argc, VALUE *argv)
Definition: hash.c:2306
#define FIX2LONG(x)
Definition: ruby.h:353
#define Qundef
Definition: ruby.h:436
VALUE rb_hash_aset(VALUE, VALUE, VALUE)
VALUE rb_hash_select(VALUE hash)
Definition: hash.c:1065
static VALUE env_has_value(VALUE dmy, VALUE obj)
Definition: hash.c:3089
static VALUE env_update(VALUE env, VALUE hash)
Definition: hash.c:3330
int st_delete_safe(st_table *, st_data_t *, st_data_t *, st_data_t)
st_index_t num_entries
Definition: st.h:93
static VALUE rb_hash_delete_key(VALUE hash, VALUE key)
Definition: hash.c:833
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1344
void ruby_setenv(const char *name, const char *value)
Definition: hash.c:2408
VALUE rb_str_new2(const char *)
void rb_warn(const char *fmt,...)
Definition: error.c:221
free(psz)
VALUE rb_eArgError
Definition: error.c:517
static void default_proc_arity_check(VALUE proc)
Definition: hash.c:306
VALUE hash
Definition: hash.c:1940
#define T_MASK
Definition: md5.c:131
st_index_t rb_hash_start(st_index_t)
Definition: random.c:1416
VALUE rb_hash_rassoc(VALUE hash, VALUE obj)
Definition: hash.c:2076
static VALUE rb_hash_to_a(VALUE hash)
Definition: hash.c:1463
char ** argv
Definition: ruby.c:131
static VALUE rb_hash_initialize(int argc, VALUE *argv, VALUE hash)
Definition: hash.c:352
static VALUE env_str_new(const char *ptr, long len)
Definition: hash.c:2192
static VALUE rb_hash_default_proc(VALUE hash)
Definition: hash.c:740
#define RUBY_DTRACE_HASH_CREATE(arg0, arg1, arg2)
Definition: probes.h:60
static VALUE rb_hash_inspect(VALUE hash)
Definition: hash.c:1522
VALUE rb_str_new(const char *, long)
Definition: string.c:425
VALUE rb_obj_class(VALUE)
Definition: object.c:194