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