Ruby  1.9.3p484(2013-11-22revision43786)
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 <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  var->key = key;
833  var->val = value;
834  return ST_STOP;
835 }
836 
837 /*
838  * call-seq:
839  * hsh.shift -> anArray or obj
840  *
841  * Removes a key-value pair from <i>hsh</i> and returns it as the
842  * two-item array <code>[</code> <i>key, value</i> <code>]</code>, or
843  * the hash's default value if the hash is empty.
844  *
845  * h = { 1 => "a", 2 => "b", 3 => "c" }
846  * h.shift #=> [1, "a"]
847  * h #=> {2=>"b", 3=>"c"}
848  */
849 
850 static VALUE
852 {
853  struct shift_var var;
854 
855  rb_hash_modify(hash);
856  var.key = Qundef;
857  if (RHASH(hash)->iter_lev == 0) {
858  if (st_shift(RHASH(hash)->ntbl, &var.key, &var.val)) {
859  return rb_assoc_new(var.key, var.val);
860  }
861  }
862  else {
863  rb_hash_foreach(hash, shift_i_safe, (VALUE)&var);
864 
865  if (var.key != Qundef) {
866  rb_hash_delete_key(hash, var.key);
867  return rb_assoc_new(var.key, var.val);
868  }
869  }
870  if (FL_TEST(hash, HASH_PROC_DEFAULT)) {
871  return rb_funcall(RHASH_IFNONE(hash), id_yield, 2, hash, Qnil);
872  }
873  else {
874  return RHASH_IFNONE(hash);
875  }
876 }
877 
878 static int
880 {
881  if (key == Qundef) return ST_CONTINUE;
882  if (RTEST(rb_yield_values(2, key, value))) {
883  rb_hash_delete_key(hash, key);
884  }
885  return ST_CONTINUE;
886 }
887 
888 /*
889  * call-seq:
890  * hsh.delete_if {| key, value | block } -> hsh
891  * hsh.delete_if -> an_enumerator
892  *
893  * Deletes every key-value pair from <i>hsh</i> for which <i>block</i>
894  * evaluates to <code>true</code>.
895  *
896  * If no block is given, an enumerator is returned instead.
897  *
898  * h = { "a" => 100, "b" => 200, "c" => 300 }
899  * h.delete_if {|key, value| key >= "b" } #=> {"a"=>100}
900  *
901  */
902 
903 VALUE
905 {
906  RETURN_ENUMERATOR(hash, 0, 0);
907  rb_hash_modify(hash);
908  rb_hash_foreach(hash, delete_if_i, hash);
909  return hash;
910 }
911 
912 /*
913  * call-seq:
914  * hsh.reject! {| key, value | block } -> hsh or nil
915  * hsh.reject! -> an_enumerator
916  *
917  * Equivalent to <code>Hash#delete_if</code>, but returns
918  * <code>nil</code> if no changes were made.
919  */
920 
921 VALUE
923 {
924  st_index_t n;
925 
926  RETURN_ENUMERATOR(hash, 0, 0);
927  rb_hash_modify(hash);
928  if (!RHASH(hash)->ntbl)
929  return Qnil;
930  n = RHASH(hash)->ntbl->num_entries;
931  rb_hash_foreach(hash, delete_if_i, hash);
932  if (n == RHASH(hash)->ntbl->num_entries) return Qnil;
933  return hash;
934 }
935 
936 /*
937  * call-seq:
938  * hsh.reject {| key, value | block } -> a_hash
939  * hsh.reject -> an_enumerator
940  *
941  * Same as <code>Hash#delete_if</code>, but works on (and returns) a
942  * copy of the <i>hsh</i>. Equivalent to
943  * <code><i>hsh</i>.dup.delete_if</code>.
944  *
945  */
946 
947 static VALUE
949 {
950  return rb_hash_delete_if(rb_obj_dup(hash));
951 }
952 
953 /*
954  * call-seq:
955  * hsh.values_at(key, ...) -> array
956  *
957  * Return an array containing the values associated with the given keys.
958  * Also see <code>Hash.select</code>.
959  *
960  * h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }
961  * h.values_at("cow", "cat") #=> ["bovine", "feline"]
962  */
963 
964 VALUE
966 {
967  VALUE result = rb_ary_new2(argc);
968  long i;
969 
970  for (i=0; i<argc; i++) {
971  rb_ary_push(result, rb_hash_aref(hash, argv[i]));
972  }
973  return result;
974 }
975 
976 static int
978 {
979  if (key == Qundef) return ST_CONTINUE;
980  if (RTEST(rb_yield_values(2, key, value)))
981  rb_hash_aset(result, key, value);
982  return ST_CONTINUE;
983 }
984 
985 /*
986  * call-seq:
987  * hsh.select {|key, value| block} -> a_hash
988  * hsh.select -> an_enumerator
989  *
990  * Returns a new hash consisting of entries for which the block returns true.
991  *
992  * If no block is given, an enumerator is returned instead.
993  *
994  * h = { "a" => 100, "b" => 200, "c" => 300 }
995  * h.select {|k,v| k > "a"} #=> {"b" => 200, "c" => 300}
996  * h.select {|k,v| v < 200} #=> {"a" => 100}
997  */
998 
999 VALUE
1001 {
1002  VALUE result;
1003 
1004  RETURN_ENUMERATOR(hash, 0, 0);
1005  result = rb_hash_new();
1006  rb_hash_foreach(hash, select_i, result);
1007  return result;
1008 }
1009 
1010 static int
1012 {
1013  if (key == Qundef) return ST_CONTINUE;
1014  if (!RTEST(rb_yield_values(2, key, value))) {
1015  return ST_DELETE;
1016  }
1017  return ST_CONTINUE;
1018 }
1019 
1020 /*
1021  * call-seq:
1022  * hsh.select! {| key, value | block } -> hsh or nil
1023  * hsh.select! -> an_enumerator
1024  *
1025  * Equivalent to <code>Hash#keep_if</code>, but returns
1026  * <code>nil</code> if no changes were made.
1027  */
1028 
1029 VALUE
1031 {
1032  st_index_t n;
1033 
1034  RETURN_ENUMERATOR(hash, 0, 0);
1035  rb_hash_modify(hash);
1036  if (!RHASH(hash)->ntbl)
1037  return Qnil;
1038  n = RHASH(hash)->ntbl->num_entries;
1039  rb_hash_foreach(hash, keep_if_i, hash);
1040  if (n == RHASH(hash)->ntbl->num_entries) return Qnil;
1041  return hash;
1042 }
1043 
1044 /*
1045  * call-seq:
1046  * hsh.keep_if {| key, value | block } -> hsh
1047  * hsh.keep_if -> an_enumerator
1048  *
1049  * Deletes every key-value pair from <i>hsh</i> for which <i>block</i>
1050  * evaluates to false.
1051  *
1052  * If no block is given, an enumerator is returned instead.
1053  *
1054  */
1055 
1056 VALUE
1058 {
1059  RETURN_ENUMERATOR(hash, 0, 0);
1060  rb_hash_modify(hash);
1061  rb_hash_foreach(hash, keep_if_i, hash);
1062  return hash;
1063 }
1064 
1065 static int
1066 clear_i(VALUE key, VALUE value, VALUE dummy)
1067 {
1068  return ST_DELETE;
1069 }
1070 
1071 /*
1072  * call-seq:
1073  * hsh.clear -> hsh
1074  *
1075  * Removes all key-value pairs from <i>hsh</i>.
1076  *
1077  * h = { "a" => 100, "b" => 200 } #=> {"a"=>100, "b"=>200}
1078  * h.clear #=> {}
1079  *
1080  */
1081 
1082 static VALUE
1084 {
1085  rb_hash_modify_check(hash);
1086  if (!RHASH(hash)->ntbl)
1087  return hash;
1088  if (RHASH(hash)->ntbl->num_entries > 0) {
1089  if (RHASH(hash)->iter_lev > 0)
1090  rb_hash_foreach(hash, clear_i, 0);
1091  else
1092  st_clear(RHASH(hash)->ntbl);
1093  }
1094 
1095  return hash;
1096 }
1097 
1098 static st_data_t
1100 {
1101  return (st_data_t)rb_str_new4((VALUE)str);
1102 }
1103 
1104 /*
1105  * call-seq:
1106  * hsh[key] = value -> value
1107  * hsh.store(key, value) -> value
1108  *
1109  * Element Assignment---Associates the value given by
1110  * <i>value</i> with the key given by <i>key</i>.
1111  * <i>key</i> should not have its value changed while it is in
1112  * use as a key (a <code>String</code> passed as a key will be
1113  * duplicated and frozen).
1114  *
1115  * h = { "a" => 100, "b" => 200 }
1116  * h["a"] = 9
1117  * h["c"] = 4
1118  * h #=> {"a"=>9, "b"=>200, "c"=>4}
1119  *
1120  */
1121 
1122 VALUE
1124 {
1125  rb_hash_modify(hash);
1126  hash_update(hash, key);
1127  if (RHASH(hash)->ntbl->type == &identhash || rb_obj_class(key) != rb_cString) {
1128  st_insert(RHASH(hash)->ntbl, key, val);
1129  }
1130  else {
1131  st_insert2(RHASH(hash)->ntbl, key, val, copy_str_key);
1132  }
1133  return val;
1134 }
1135 
1136 static int
1138 {
1139  if (key != Qundef) {
1140  rb_hash_aset(hash, key, val);
1141  }
1142 
1143  return ST_CONTINUE;
1144 }
1145 
1146 /*
1147  * call-seq:
1148  * hsh.replace(other_hash) -> hsh
1149  *
1150  * Replaces the contents of <i>hsh</i> with the contents of
1151  * <i>other_hash</i>.
1152  *
1153  * h = { "a" => 100, "b" => 200 }
1154  * h.replace({ "c" => 300, "d" => 400 }) #=> {"c"=>300, "d"=>400}
1155  *
1156  */
1157 
1158 static VALUE
1160 {
1161  rb_hash_modify_check(hash);
1162  hash2 = to_hash(hash2);
1163  if (hash == hash2) return hash;
1164  rb_hash_clear(hash);
1165  if (RHASH(hash2)->ntbl) {
1166  rb_hash_tbl(hash);
1167  RHASH(hash)->ntbl->type = RHASH(hash2)->ntbl->type;
1168  }
1169  rb_hash_foreach(hash2, replace_i, hash);
1170  RHASH_IFNONE(hash) = RHASH_IFNONE(hash2);
1171  if (FL_TEST(hash2, HASH_PROC_DEFAULT)) {
1172  FL_SET(hash, HASH_PROC_DEFAULT);
1173  }
1174  else {
1175  FL_UNSET(hash, HASH_PROC_DEFAULT);
1176  }
1177 
1178  return hash;
1179 }
1180 
1181 /*
1182  * call-seq:
1183  * hsh.length -> fixnum
1184  * hsh.size -> fixnum
1185  *
1186  * Returns the number of key-value pairs in the hash.
1187  *
1188  * h = { "d" => 100, "a" => 200, "v" => 300, "e" => 400 }
1189  * h.length #=> 4
1190  * h.delete("a") #=> 200
1191  * h.length #=> 3
1192  */
1193 
1194 static VALUE
1196 {
1197  if (!RHASH(hash)->ntbl)
1198  return INT2FIX(0);
1199  return INT2FIX(RHASH(hash)->ntbl->num_entries);
1200 }
1201 
1202 
1203 /*
1204  * call-seq:
1205  * hsh.empty? -> true or false
1206  *
1207  * Returns <code>true</code> if <i>hsh</i> contains no key-value pairs.
1208  *
1209  * {}.empty? #=> true
1210  *
1211  */
1212 
1213 static VALUE
1215 {
1216  return RHASH_EMPTY_P(hash) ? Qtrue : Qfalse;
1217 }
1218 
1219 static int
1221 {
1222  if (key == Qundef) return ST_CONTINUE;
1223  rb_yield(value);
1224  return ST_CONTINUE;
1225 }
1226 
1227 /*
1228  * call-seq:
1229  * hsh.each_value {| value | block } -> hsh
1230  * hsh.each_value -> an_enumerator
1231  *
1232  * Calls <i>block</i> once for each key in <i>hsh</i>, passing the
1233  * value as a parameter.
1234  *
1235  * If no block is given, an enumerator is returned instead.
1236  *
1237  * h = { "a" => 100, "b" => 200 }
1238  * h.each_value {|value| puts value }
1239  *
1240  * <em>produces:</em>
1241  *
1242  * 100
1243  * 200
1244  */
1245 
1246 static VALUE
1248 {
1249  RETURN_ENUMERATOR(hash, 0, 0);
1250  rb_hash_foreach(hash, each_value_i, 0);
1251  return hash;
1252 }
1253 
1254 static int
1256 {
1257  if (key == Qundef) return ST_CONTINUE;
1258  rb_yield(key);
1259  return ST_CONTINUE;
1260 }
1261 
1262 /*
1263  * call-seq:
1264  * hsh.each_key {| key | block } -> hsh
1265  * hsh.each_key -> an_enumerator
1266  *
1267  * Calls <i>block</i> once for each key in <i>hsh</i>, passing the key
1268  * as a parameter.
1269  *
1270  * If no block is given, an enumerator is returned instead.
1271  *
1272  * h = { "a" => 100, "b" => 200 }
1273  * h.each_key {|key| puts key }
1274  *
1275  * <em>produces:</em>
1276  *
1277  * a
1278  * b
1279  */
1280 static VALUE
1282 {
1283  RETURN_ENUMERATOR(hash, 0, 0);
1284  rb_hash_foreach(hash, each_key_i, 0);
1285  return hash;
1286 }
1287 
1288 static int
1290 {
1291  if (key == Qundef) return ST_CONTINUE;
1292  rb_yield(rb_assoc_new(key, value));
1293  return ST_CONTINUE;
1294 }
1295 
1296 /*
1297  * call-seq:
1298  * hsh.each {| key, value | block } -> hsh
1299  * hsh.each_pair {| key, value | block } -> hsh
1300  * hsh.each -> an_enumerator
1301  * hsh.each_pair -> an_enumerator
1302  *
1303  * Calls <i>block</i> once for each key in <i>hsh</i>, passing the key-value
1304  * pair as parameters.
1305  *
1306  * If no block is given, an enumerator is returned instead.
1307  *
1308  * h = { "a" => 100, "b" => 200 }
1309  * h.each {|key, value| puts "#{key} is #{value}" }
1310  *
1311  * <em>produces:</em>
1312  *
1313  * a is 100
1314  * b is 200
1315  *
1316  */
1317 
1318 static VALUE
1320 {
1321  RETURN_ENUMERATOR(hash, 0, 0);
1322  rb_hash_foreach(hash, each_pair_i, 0);
1323  return hash;
1324 }
1325 
1326 static int
1328 {
1329  if (key == Qundef) return ST_CONTINUE;
1330  rb_ary_push(ary, rb_assoc_new(key, value));
1331  return ST_CONTINUE;
1332 }
1333 
1334 /*
1335  * call-seq:
1336  * hsh.to_a -> array
1337  *
1338  * Converts <i>hsh</i> to a nested array of <code>[</code> <i>key,
1339  * value</i> <code>]</code> arrays.
1340  *
1341  * h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }
1342  * h.to_a #=> [["c", 300], ["a", 100], ["d", 400]]
1343  */
1344 
1345 static VALUE
1347 {
1348  VALUE ary;
1349 
1350  ary = rb_ary_new();
1351  rb_hash_foreach(hash, to_a_i, ary);
1352  OBJ_INFECT(ary, hash);
1353 
1354  return ary;
1355 }
1356 
1357 static int
1359 {
1360  VALUE str2;
1361 
1362  if (key == Qundef) return ST_CONTINUE;
1363  str2 = rb_inspect(key);
1364  if (RSTRING_LEN(str) > 1) {
1365  rb_str_cat2(str, ", ");
1366  }
1367  else {
1368  rb_enc_copy(str, str2);
1369  }
1370  rb_str_buf_append(str, str2);
1371  OBJ_INFECT(str, str2);
1372  rb_str_buf_cat2(str, "=>");
1373  str2 = rb_inspect(value);
1374  rb_str_buf_append(str, str2);
1375  OBJ_INFECT(str, str2);
1376 
1377  return ST_CONTINUE;
1378 }
1379 
1380 static VALUE
1381 inspect_hash(VALUE hash, VALUE dummy, int recur)
1382 {
1383  VALUE str;
1384 
1385  if (recur) return rb_usascii_str_new2("{...}");
1386  str = rb_str_buf_new2("{");
1387  rb_hash_foreach(hash, inspect_i, str);
1388  rb_str_buf_cat2(str, "}");
1389  OBJ_INFECT(str, hash);
1390 
1391  return str;
1392 }
1393 
1394 /*
1395  * call-seq:
1396  * hsh.to_s -> string
1397  * hsh.inspect -> string
1398  *
1399  * Return the contents of this hash as a string.
1400  *
1401  * h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }
1402  * h.to_s #=> "{\"c\"=>300, \"a\"=>100, \"d\"=>400}"
1403  */
1404 
1405 static VALUE
1407 {
1408  if (RHASH_EMPTY_P(hash))
1409  return rb_usascii_str_new2("{}");
1410  return rb_exec_recursive(inspect_hash, hash, 0);
1411 }
1412 
1413 /*
1414  * call-seq:
1415  * hsh.to_hash => hsh
1416  *
1417  * Returns +self+.
1418  */
1419 
1420 static VALUE
1422 {
1423  return hash;
1424 }
1425 
1426 static int
1428 {
1429  if (key == Qundef) return ST_CONTINUE;
1430  rb_ary_push(ary, key);
1431  return ST_CONTINUE;
1432 }
1433 
1434 /*
1435  * call-seq:
1436  * hsh.keys -> array
1437  *
1438  * Returns a new array populated with the keys from this hash. See also
1439  * <code>Hash#values</code>.
1440  *
1441  * h = { "a" => 100, "b" => 200, "c" => 300, "d" => 400 }
1442  * h.keys #=> ["a", "b", "c", "d"]
1443  *
1444  */
1445 
1446 static VALUE
1448 {
1449  VALUE ary;
1450 
1451  ary = rb_ary_new();
1452  rb_hash_foreach(hash, keys_i, ary);
1453 
1454  return ary;
1455 }
1456 
1457 static int
1459 {
1460  if (key == Qundef) return ST_CONTINUE;
1461  rb_ary_push(ary, value);
1462  return ST_CONTINUE;
1463 }
1464 
1465 /*
1466  * call-seq:
1467  * hsh.values -> array
1468  *
1469  * Returns a new array populated with the values from <i>hsh</i>. See
1470  * also <code>Hash#keys</code>.
1471  *
1472  * h = { "a" => 100, "b" => 200, "c" => 300 }
1473  * h.values #=> [100, 200, 300]
1474  *
1475  */
1476 
1477 static VALUE
1479 {
1480  VALUE ary;
1481 
1482  ary = rb_ary_new();
1483  rb_hash_foreach(hash, values_i, ary);
1484 
1485  return ary;
1486 }
1487 
1488 /*
1489  * call-seq:
1490  * hsh.has_key?(key) -> true or false
1491  * hsh.include?(key) -> true or false
1492  * hsh.key?(key) -> true or false
1493  * hsh.member?(key) -> true or false
1494  *
1495  * Returns <code>true</code> if the given key is present in <i>hsh</i>.
1496  *
1497  * h = { "a" => 100, "b" => 200 }
1498  * h.has_key?("a") #=> true
1499  * h.has_key?("z") #=> false
1500  *
1501  */
1502 
1503 static VALUE
1505 {
1506  if (!RHASH(hash)->ntbl)
1507  return Qfalse;
1508  if (st_lookup(RHASH(hash)->ntbl, key, 0)) {
1509  return Qtrue;
1510  }
1511  return Qfalse;
1512 }
1513 
1514 static int
1516 {
1517  VALUE *data = (VALUE *)arg;
1518 
1519  if (key == Qundef) return ST_CONTINUE;
1520  if (rb_equal(value, data[1])) {
1521  data[0] = Qtrue;
1522  return ST_STOP;
1523  }
1524  return ST_CONTINUE;
1525 }
1526 
1527 /*
1528  * call-seq:
1529  * hsh.has_value?(value) -> true or false
1530  * hsh.value?(value) -> true or false
1531  *
1532  * Returns <code>true</code> if the given value is present for some key
1533  * in <i>hsh</i>.
1534  *
1535  * h = { "a" => 100, "b" => 200 }
1536  * h.has_value?(100) #=> true
1537  * h.has_value?(999) #=> false
1538  */
1539 
1540 static VALUE
1542 {
1543  VALUE data[2];
1544 
1545  data[0] = Qfalse;
1546  data[1] = val;
1548  return data[0];
1549 }
1550 
1551 struct equal_data {
1554  int eql;
1555 };
1556 
1557 static int
1559 {
1560  struct equal_data *data = (struct equal_data *)arg;
1561  st_data_t val2;
1562 
1563  if (key == Qundef) return ST_CONTINUE;
1564  if (!st_lookup(data->tbl, key, &val2)) {
1565  data->result = Qfalse;
1566  return ST_STOP;
1567  }
1568  if (!(data->eql ? rb_eql(val1, (VALUE)val2) : (int)rb_equal(val1, (VALUE)val2))) {
1569  data->result = Qfalse;
1570  return ST_STOP;
1571  }
1572  return ST_CONTINUE;
1573 }
1574 
1575 static VALUE
1577 {
1578  struct equal_data *data;
1579 
1580  if (recur) return Qtrue; /* Subtle! */
1581  data = (struct equal_data*)dt;
1582  data->result = Qtrue;
1583  rb_hash_foreach(hash, eql_i, dt);
1584 
1585  return data->result;
1586 }
1587 
1588 static VALUE
1589 hash_equal(VALUE hash1, VALUE hash2, int eql)
1590 {
1591  struct equal_data data;
1592 
1593  if (hash1 == hash2) return Qtrue;
1594  if (TYPE(hash2) != T_HASH) {
1595  if (!rb_respond_to(hash2, rb_intern("to_hash"))) {
1596  return Qfalse;
1597  }
1598  if (eql)
1599  return rb_eql(hash2, hash1);
1600  else
1601  return rb_equal(hash2, hash1);
1602  }
1603  if (RHASH_SIZE(hash1) != RHASH_SIZE(hash2))
1604  return Qfalse;
1605  if (!RHASH(hash1)->ntbl || !RHASH(hash2)->ntbl)
1606  return Qtrue;
1607  if (RHASH(hash1)->ntbl->type != RHASH(hash2)->ntbl->type)
1608  return Qfalse;
1609 #if 0
1610  if (!(rb_equal(RHASH_IFNONE(hash1), RHASH_IFNONE(hash2)) &&
1611  FL_TEST(hash1, HASH_PROC_DEFAULT) == FL_TEST(hash2, HASH_PROC_DEFAULT)))
1612  return Qfalse;
1613 #endif
1614 
1615  data.tbl = RHASH(hash2)->ntbl;
1616  data.eql = eql;
1617  return rb_exec_recursive_paired(recursive_eql, hash1, hash2, (VALUE)&data);
1618 }
1619 
1620 /*
1621  * call-seq:
1622  * hsh == other_hash -> true or false
1623  *
1624  * Equality---Two hashes are equal if they each contain the same number
1625  * of keys and if each key-value pair is equal to (according to
1626  * <code>Object#==</code>) the corresponding elements in the other
1627  * hash.
1628  *
1629  * h1 = { "a" => 1, "c" => 2 }
1630  * h2 = { 7 => 35, "c" => 2, "a" => 1 }
1631  * h3 = { "a" => 1, "c" => 2, 7 => 35 }
1632  * h4 = { "a" => 1, "d" => 2, "f" => 35 }
1633  * h1 == h2 #=> false
1634  * h2 == h3 #=> true
1635  * h3 == h4 #=> false
1636  *
1637  */
1638 
1639 static VALUE
1641 {
1642  return hash_equal(hash1, hash2, FALSE);
1643 }
1644 
1645 /*
1646  * call-seq:
1647  * hash.eql?(other) -> true or false
1648  *
1649  * Returns <code>true</code> if <i>hash</i> and <i>other</i> are
1650  * both hashes with the same content.
1651  */
1652 
1653 static VALUE
1654 rb_hash_eql(VALUE hash1, VALUE hash2)
1655 {
1656  return hash_equal(hash1, hash2, TRUE);
1657 }
1658 
1659 static int
1661 {
1662  st_index_t *hval = (st_index_t *)arg;
1663  st_index_t hdata[2];
1664 
1665  if (key == Qundef) return ST_CONTINUE;
1666  hdata[0] = rb_hash(key);
1667  hdata[1] = rb_hash(val);
1668  *hval ^= st_hash(hdata, sizeof(hdata), 0);
1669  return ST_CONTINUE;
1670 }
1671 
1672 static VALUE
1673 recursive_hash(VALUE hash, VALUE dummy, int recur)
1674 {
1675  st_index_t hval;
1676 
1677  if (!RHASH(hash)->ntbl)
1678  return LONG2FIX(0);
1679  hval = RHASH(hash)->ntbl->num_entries;
1680  if (!hval) return LONG2FIX(0);
1681  if (recur)
1682  hval = rb_hash_uint(rb_hash_start(rb_hash(rb_cHash)), hval);
1683  else
1684  rb_hash_foreach(hash, hash_i, (VALUE)&hval);
1685  hval = rb_hash_end(hval);
1686  return INT2FIX(hval);
1687 }
1688 
1689 /*
1690  * call-seq:
1691  * hsh.hash -> fixnum
1692  *
1693  * Compute a hash-code for this hash. Two hashes with the same content
1694  * will have the same hash code (and will compare using <code>eql?</code>).
1695  */
1696 
1697 static VALUE
1699 {
1700  return rb_exec_recursive_outer(recursive_hash, hash, 0);
1701 }
1702 
1703 static int
1705 {
1706  if (key == Qundef) return ST_CONTINUE;
1707  rb_hash_aset(hash, value, key);
1708  return ST_CONTINUE;
1709 }
1710 
1711 /*
1712  * call-seq:
1713  * hsh.invert -> new_hash
1714  *
1715  * Returns a new hash created by using <i>hsh</i>'s values as keys, and
1716  * the keys as values.
1717  *
1718  * h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 }
1719  * h.invert #=> {0=>"a", 100=>"m", 200=>"d", 300=>"y"}
1720  *
1721  */
1722 
1723 static VALUE
1725 {
1726  VALUE h = rb_hash_new();
1727 
1729  return h;
1730 }
1731 
1732 static int
1734 {
1735  if (key == Qundef) return ST_CONTINUE;
1736  hash_update(hash, key);
1737  st_insert(RHASH(hash)->ntbl, key, value);
1738  return ST_CONTINUE;
1739 }
1740 
1741 static int
1743 {
1744  if (key == Qundef) return ST_CONTINUE;
1745  if (rb_hash_has_key(hash, key)) {
1746  value = rb_yield_values(3, key, rb_hash_aref(hash, key), value);
1747  }
1748  hash_update(hash, key);
1749  st_insert(RHASH(hash)->ntbl, key, value);
1750  return ST_CONTINUE;
1751 }
1752 
1753 /*
1754  * call-seq:
1755  * hsh.merge!(other_hash) -> hsh
1756  * hsh.update(other_hash) -> hsh
1757  * hsh.merge!(other_hash){|key, oldval, newval| block} -> hsh
1758  * hsh.update(other_hash){|key, oldval, newval| block} -> hsh
1759  *
1760  * Adds the contents of <i>other_hash</i> to <i>hsh</i>. If no
1761  * block is specified, entries with duplicate keys are overwritten
1762  * with the values from <i>other_hash</i>, otherwise the value
1763  * of each duplicate key is determined by calling the block with
1764  * the key, its value in <i>hsh</i> and its value in <i>other_hash</i>.
1765  *
1766  * h1 = { "a" => 100, "b" => 200 }
1767  * h2 = { "b" => 254, "c" => 300 }
1768  * h1.merge!(h2) #=> {"a"=>100, "b"=>254, "c"=>300}
1769  *
1770  * h1 = { "a" => 100, "b" => 200 }
1771  * h2 = { "b" => 254, "c" => 300 }
1772  * h1.merge!(h2) { |key, v1, v2| v1 }
1773  * #=> {"a"=>100, "b"=>200, "c"=>300}
1774  */
1775 
1776 static VALUE
1778 {
1779  rb_hash_modify(hash1);
1780  hash2 = to_hash(hash2);
1781  if (rb_block_given_p()) {
1782  rb_hash_foreach(hash2, rb_hash_update_block_i, hash1);
1783  }
1784  else {
1785  rb_hash_foreach(hash2, rb_hash_update_i, hash1);
1786  }
1787  return hash1;
1788 }
1789 
1790 struct update_arg {
1793 };
1794 
1795 static int
1797 {
1798  struct update_arg *arg = (struct update_arg *)arg0;
1799  VALUE hash = arg->hash;
1800 
1801  if (key == Qundef) return ST_CONTINUE;
1802  if (rb_hash_has_key(hash, key)) {
1803  value = (*arg->func)(key, rb_hash_aref(hash, key), value);
1804  }
1805  hash_update(hash, key);
1806  st_insert(RHASH(hash)->ntbl, key, value);
1807  return ST_CONTINUE;
1808 }
1809 
1810 VALUE
1812 {
1813  rb_hash_modify(hash1);
1814  hash2 = to_hash(hash2);
1815  if (func) {
1816  struct update_arg arg;
1817  arg.hash = hash1;
1818  arg.func = func;
1820  }
1821  else {
1822  rb_hash_foreach(hash2, rb_hash_update_i, hash1);
1823  }
1824  return hash1;
1825 }
1826 
1827 /*
1828  * call-seq:
1829  * hsh.merge(other_hash) -> new_hash
1830  * hsh.merge(other_hash){|key, oldval, newval| block} -> new_hash
1831  *
1832  * Returns a new hash containing the contents of <i>other_hash</i> and
1833  * the contents of <i>hsh</i>. If no block is specified, the value for
1834  * entries with duplicate keys will be that of <i>other_hash</i>. Otherwise
1835  * the value for each duplicate key is determined by calling the block
1836  * with the key, its value in <i>hsh</i> and its value in <i>other_hash</i>.
1837  *
1838  * h1 = { "a" => 100, "b" => 200 }
1839  * h2 = { "b" => 254, "c" => 300 }
1840  * h1.merge(h2) #=> {"a"=>100, "b"=>254, "c"=>300}
1841  * h1.merge(h2){|key, oldval, newval| newval - oldval}
1842  * #=> {"a"=>100, "b"=>54, "c"=>300}
1843  * h1 #=> {"a"=>100, "b"=>200}
1844  *
1845  */
1846 
1847 static VALUE
1849 {
1850  return rb_hash_update(rb_obj_dup(hash1), hash2);
1851 }
1852 
1853 static int
1855 {
1856  VALUE *args = (VALUE *)arg;
1857 
1858  if (key == Qundef) return ST_CONTINUE;
1859  if (RTEST(rb_equal(args[0], key))) {
1860  args[1] = rb_assoc_new(key, val);
1861  return ST_STOP;
1862  }
1863  return ST_CONTINUE;
1864 }
1865 
1866 /*
1867  * call-seq:
1868  * hash.assoc(obj) -> an_array or nil
1869  *
1870  * Searches through the hash comparing _obj_ with the key using <code>==</code>.
1871  * Returns the key-value pair (two elements array) or +nil+
1872  * if no match is found. See <code>Array#assoc</code>.
1873  *
1874  * h = {"colors" => ["red", "blue", "green"],
1875  * "letters" => ["a", "b", "c" ]}
1876  * h.assoc("letters") #=> ["letters", ["a", "b", "c"]]
1877  * h.assoc("foo") #=> nil
1878  */
1879 
1880 VALUE
1882 {
1883  VALUE args[2];
1884 
1885  args[0] = obj;
1886  args[1] = Qnil;
1887  rb_hash_foreach(hash, assoc_i, (VALUE)args);
1888  return args[1];
1889 }
1890 
1891 static int
1893 {
1894  VALUE *args = (VALUE *)arg;
1895 
1896  if (key == Qundef) return ST_CONTINUE;
1897  if (RTEST(rb_equal(args[0], val))) {
1898  args[1] = rb_assoc_new(key, val);
1899  return ST_STOP;
1900  }
1901  return ST_CONTINUE;
1902 }
1903 
1904 /*
1905  * call-seq:
1906  * hash.rassoc(obj) -> an_array or nil
1907  *
1908  * Searches through the hash comparing _obj_ with the value using <code>==</code>.
1909  * Returns the first key-value pair (two-element array) that matches. See
1910  * also <code>Array#rassoc</code>.
1911  *
1912  * a = {1=> "one", 2 => "two", 3 => "three", "ii" => "two"}
1913  * a.rassoc("two") #=> [2, "two"]
1914  * a.rassoc("four") #=> nil
1915  */
1916 
1917 VALUE
1919 {
1920  VALUE args[2];
1921 
1922  args[0] = obj;
1923  args[1] = Qnil;
1924  rb_hash_foreach(hash, rassoc_i, (VALUE)args);
1925  return args[1];
1926 }
1927 
1928 /*
1929  * call-seq:
1930  * hash.flatten -> an_array
1931  * hash.flatten(level) -> an_array
1932  *
1933  * Returns a new array that is a one-dimensional flattening of this
1934  * hash. That is, for every key or value that is an array, extract
1935  * its elements into the new array. Unlike Array#flatten, this
1936  * method does not flatten recursively by default. The optional
1937  * <i>level</i> argument determines the level of recursion to flatten.
1938  *
1939  * a = {1=> "one", 2 => [2,"two"], 3 => "three"}
1940  * a.flatten # => [1, "one", 2, [2, "two"], 3, "three"]
1941  * a.flatten(2) # => [1, "one", 2, 2, "two", 3, "three"]
1942  */
1943 
1944 static VALUE
1946 {
1947  VALUE ary, tmp;
1948 
1949  ary = rb_hash_to_a(hash);
1950  if (argc == 0) {
1951  argc = 1;
1952  tmp = INT2FIX(1);
1953  argv = &tmp;
1954  }
1955  rb_funcall2(ary, rb_intern("flatten!"), argc, argv);
1956  return ary;
1957 }
1958 
1959 /*
1960  * call-seq:
1961  * hsh.compare_by_identity -> hsh
1962  *
1963  * Makes <i>hsh</i> compare its keys by their identity, i.e. it
1964  * will consider exact same objects as same keys.
1965  *
1966  * h1 = { "a" => 100, "b" => 200, :c => "c" }
1967  * h1["a"] #=> 100
1968  * h1.compare_by_identity
1969  * h1.compare_by_identity? #=> true
1970  * h1["a"] #=> nil # different objects.
1971  * h1[:c] #=> "c" # same symbols are all same.
1972  *
1973  */
1974 
1975 static VALUE
1977 {
1978  rb_hash_modify(hash);
1979  RHASH(hash)->ntbl->type = &identhash;
1980  rb_hash_rehash(hash);
1981  return hash;
1982 }
1983 
1984 /*
1985  * call-seq:
1986  * hsh.compare_by_identity? -> true or false
1987  *
1988  * Returns <code>true</code> if <i>hsh</i> will compare its keys by
1989  * their identity. Also see <code>Hash#compare_by_identity</code>.
1990  *
1991  */
1992 
1993 static VALUE
1995 {
1996  if (!RHASH(hash)->ntbl)
1997  return Qfalse;
1998  if (RHASH(hash)->ntbl->type == &identhash) {
1999  return Qtrue;
2000  }
2001  return Qfalse;
2002 }
2003 
2004 static int path_tainted = -1;
2005 
2006 static char **origenviron;
2007 #ifdef _WIN32
2008 #define GET_ENVIRON(e) ((e) = rb_w32_get_environ())
2009 #define FREE_ENVIRON(e) rb_w32_free_environ(e)
2010 static char **my_environ;
2011 #undef environ
2012 #define environ my_environ
2013 #elif defined(__APPLE__)
2014 #undef environ
2015 #define environ (*_NSGetEnviron())
2016 #define GET_ENVIRON(e) (e)
2017 #define FREE_ENVIRON(e)
2018 #else
2019 extern char **environ;
2020 #define GET_ENVIRON(e) (e)
2021 #define FREE_ENVIRON(e)
2022 #endif
2023 #ifdef ENV_IGNORECASE
2024 #define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
2025 #define ENVNMATCH(s1, s2, n) (STRNCASECMP((s1), (s2), (n)) == 0)
2026 #else
2027 #define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
2028 #define ENVNMATCH(s1, s2, n) (memcmp((s1), (s2), (n)) == 0)
2029 #endif
2030 
2031 static VALUE
2032 env_str_new(const char *ptr, long len)
2033 {
2034  VALUE str = rb_locale_str_new(ptr, len);
2035 
2036  rb_obj_freeze(str);
2037  return str;
2038 }
2039 
2040 static VALUE
2041 env_str_new2(const char *ptr)
2042 {
2043  if (!ptr) return Qnil;
2044  return env_str_new(ptr, strlen(ptr));
2045 }
2046 
2047 static VALUE
2049 {
2050  char *nam, *val;
2051 
2052  rb_secure(4);
2053  SafeStringValue(name);
2054  nam = RSTRING_PTR(name);
2055  if (memchr(nam, '\0', RSTRING_LEN(name))) {
2056  rb_raise(rb_eArgError, "bad environment variable name");
2057  }
2058  val = getenv(nam);
2059  if (val) {
2060  VALUE value = env_str_new2(val);
2061 
2062  ruby_setenv(nam, 0);
2063  if (ENVMATCH(nam, PATH_ENV)) {
2064  path_tainted = 0;
2065  }
2066  return value;
2067  }
2068  return Qnil;
2069 }
2070 
2071 /*
2072  * call-seq:
2073  * ENV.delete(name) -> value
2074  * ENV.delete(name) { |name| } -> value
2075  *
2076  * Deletes the environment variable with +name+ and returns the value of the
2077  * variable. If a block is given it will be called when the named environment
2078  * does not exist.
2079  */
2080 static VALUE
2082 {
2083  VALUE val;
2084 
2085  val = env_delete(obj, name);
2086  if (NIL_P(val) && rb_block_given_p()) rb_yield(name);
2087  return val;
2088 }
2089 
2090 static int env_path_tainted(const char *);
2091 
2092 /*
2093  * call-seq:
2094  * ENV[name] -> value
2095  *
2096  * Retrieves the +value+ for environment variable +name+ as a String. Returns
2097  * +nil+ if the named variable does not exist.
2098  */
2099 static VALUE
2101 {
2102  char *nam, *env;
2103 
2104  rb_secure(4);
2105  SafeStringValue(name);
2106  nam = RSTRING_PTR(name);
2107  if (memchr(nam, '\0', RSTRING_LEN(name))) {
2108  rb_raise(rb_eArgError, "bad environment variable name");
2109  }
2110  env = getenv(nam);
2111  if (env) {
2112  if (ENVMATCH(nam, PATH_ENV) && !env_path_tainted(env)) {
2113  VALUE str = rb_filesystem_str_new_cstr(env);
2114 
2115  rb_obj_freeze(str);
2116  return str;
2117  }
2118  return env_str_new2(env);
2119  }
2120  return Qnil;
2121 }
2122 
2123 /*
2124  * :yield: missing_name
2125  * call-seq:
2126  * ENV.fetch(name) -> value
2127  * ENV.fetch(name, default) -> value
2128  * ENV.fetch(name) { |missing_name| ... } -> value
2129  *
2130  * Retrieves the environment variable +name+.
2131  *
2132  * If the given name does not exist and neither +default+ nor a block a
2133  * provided an IndexError is raised. If a block is given it is called with
2134  * the missing name to provide a value. If a default value is given it will
2135  * be returned when no block is given.
2136  */
2137 static VALUE
2139 {
2140  VALUE key, if_none;
2141  long block_given;
2142  char *nam, *env;
2143 
2144  rb_secure(4);
2145  rb_scan_args(argc, argv, "11", &key, &if_none);
2146  block_given = rb_block_given_p();
2147  if (block_given && argc == 2) {
2148  rb_warn("block supersedes default value argument");
2149  }
2150  SafeStringValue(key);
2151  nam = RSTRING_PTR(key);
2152  if (memchr(nam, '\0', RSTRING_LEN(key))) {
2153  rb_raise(rb_eArgError, "bad environment variable name");
2154  }
2155  env = getenv(nam);
2156  if (!env) {
2157  if (block_given) return rb_yield(key);
2158  if (argc == 1) {
2159  rb_raise(rb_eKeyError, "key not found");
2160  }
2161  return if_none;
2162  }
2163  if (ENVMATCH(nam, PATH_ENV) && !env_path_tainted(env))
2164  return rb_filesystem_str_new_cstr(env);
2165  return env_str_new2(env);
2166 }
2167 
2168 static void
2169 path_tainted_p(const char *path)
2170 {
2171  path_tainted = rb_path_check(path)?0:1;
2172 }
2173 
2174 static int
2175 env_path_tainted(const char *path)
2176 {
2177  if (path_tainted < 0) {
2178  path_tainted_p(path);
2179  }
2180  return path_tainted;
2181 }
2182 
2183 int
2185 {
2186  if (path_tainted < 0) {
2188  }
2189  return path_tainted;
2190 }
2191 
2192 #if defined(_WIN32) || (defined(HAVE_SETENV) && defined(HAVE_UNSETENV))
2193 #elif defined __sun__
2194 static int
2195 in_origenv(const char *str)
2196 {
2197  char **env;
2198  for (env = origenviron; *env; ++env) {
2199  if (*env == str) return 1;
2200  }
2201  return 0;
2202 }
2203 #else
2204 static int
2205 envix(const char *nam)
2206 {
2207  register int i, len = strlen(nam);
2208  char **env;
2209 
2210  env = GET_ENVIRON(environ);
2211  for (i = 0; env[i]; i++) {
2212  if (ENVNMATCH(env[i],nam,len) && env[i][len] == '=')
2213  break; /* memcmp must come first to avoid */
2214  } /* potential SEGV's */
2215  FREE_ENVIRON(environ);
2216  return i;
2217 }
2218 #endif
2219 
2220 #if defined(_WIN32)
2221 static size_t
2222 getenvsize(const char* p)
2223 {
2224  const char* porg = p;
2225  while (*p++) p += strlen(p) + 1;
2226  return p - porg + 1;
2227 }
2228 static size_t
2229 getenvblocksize()
2230 {
2231  return (rb_w32_osver() >= 5) ? 32767 : 5120;
2232 }
2233 #endif
2234 
2235 void
2236 ruby_setenv(const char *name, const char *value)
2237 {
2238 #if defined(_WIN32)
2239  VALUE buf;
2240  int failed = 0;
2241  if (strchr(name, '=')) {
2242  fail:
2243  errno = EINVAL;
2244  rb_sys_fail("ruby_setenv");
2245  }
2246  if (value) {
2247  const char* p = GetEnvironmentStringsA();
2248  if (!p) goto fail; /* never happen */
2249  if (strlen(name) + 2 + strlen(value) + getenvsize(p) >= getenvblocksize()) {
2250  goto fail; /* 2 for '=' & '\0' */
2251  }
2252  buf = rb_sprintf("%s=%s", name, value);
2253  }
2254  else {
2255  buf = rb_sprintf("%s=", name);
2256  }
2257  failed = putenv(RSTRING_PTR(buf));
2258  /* even if putenv() failed, clean up and try to delete the
2259  * variable from the system area. */
2260  rb_str_resize(buf, 0);
2261  if (!value || !*value) {
2262  /* putenv() doesn't handle empty value */
2263  if (!SetEnvironmentVariable(name, value) &&
2264  GetLastError() != ERROR_ENVVAR_NOT_FOUND) goto fail;
2265  }
2266  if (failed) goto fail;
2267 #elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV)
2268 #undef setenv
2269 #undef unsetenv
2270  if (value) {
2271  if (setenv(name, value, 1))
2272  rb_sys_fail("setenv");
2273  } else {
2274 #ifdef VOID_UNSETENV
2275  unsetenv(name);
2276 #else
2277  if (unsetenv(name))
2278  rb_sys_fail("unsetenv");
2279 #endif
2280  }
2281 #elif defined __sun__
2282  size_t len;
2283  char **env_ptr, *str;
2284  if (strchr(name, '=')) {
2285  errno = EINVAL;
2286  rb_sys_fail("ruby_setenv");
2287  }
2288  len = strlen(name);
2289  for (env_ptr = GET_ENVIRON(environ); (str = *env_ptr) != 0; ++env_ptr) {
2290  if (!strncmp(str, name, len) && str[len] == '=') {
2291  if (!in_origenv(str)) free(str);
2292  while ((env_ptr[0] = env_ptr[1]) != 0) env_ptr++;
2293  break;
2294  }
2295  }
2296  if (value) {
2297  str = malloc(len += strlen(value) + 2);
2298  snprintf(str, len, "%s=%s", name, value);
2299  if (putenv(str))
2300  rb_sys_fail("putenv");
2301  }
2302 #else /* WIN32 */
2303  size_t len;
2304  int i;
2305  if (strchr(name, '=')) {
2306  errno = EINVAL;
2307  rb_sys_fail("ruby_setenv");
2308  }
2309  i=envix(name); /* where does it go? */
2310 
2311  if (environ == origenviron) { /* need we copy environment? */
2312  int j;
2313  int max;
2314  char **tmpenv;
2315 
2316  for (max = i; environ[max]; max++) ;
2317  tmpenv = ALLOC_N(char*, max+2);
2318  for (j=0; j<max; j++) /* copy environment */
2319  tmpenv[j] = ruby_strdup(environ[j]);
2320  tmpenv[max] = 0;
2321  environ = tmpenv; /* tell exec where it is now */
2322  }
2323  if (environ[i]) {
2324  char **envp = origenviron;
2325  while (*envp && *envp != environ[i]) envp++;
2326  if (!*envp)
2327  xfree(environ[i]);
2328  if (!value) {
2329  while (environ[i]) {
2330  environ[i] = environ[i+1];
2331  i++;
2332  }
2333  return;
2334  }
2335  }
2336  else { /* does not exist yet */
2337  if (!value) return;
2338  REALLOC_N(environ, char*, i+2); /* just expand it a bit */
2339  environ[i+1] = 0; /* make sure it's null terminated */
2340  }
2341  len = strlen(name) + strlen(value) + 2;
2342  environ[i] = ALLOC_N(char, len);
2343  snprintf(environ[i],len,"%s=%s",name,value); /* all that work just for this */
2344 #endif /* WIN32 */
2345 }
2346 
2347 void
2348 ruby_unsetenv(const char *name)
2349 {
2350  ruby_setenv(name, 0);
2351 }
2352 
2353 /*
2354  * call-seq:
2355  * ENV[name] = value
2356  * ENV.store(name, value) -> value
2357  *
2358  * Sets the environment variable +name+ to +value+. If the value given is
2359  * +nil+ the environment variable is deleted.
2360  *
2361  */
2362 static VALUE
2363 env_aset(VALUE obj, VALUE nm, VALUE val)
2364 {
2365  char *name, *value;
2366 
2367  if (rb_safe_level() >= 4) {
2368  rb_raise(rb_eSecurityError, "can't change environment variable");
2369  }
2370 
2371  if (NIL_P(val)) {
2372  env_delete(obj, nm);
2373  return Qnil;
2374  }
2375  StringValue(nm);
2376  StringValue(val);
2377  name = RSTRING_PTR(nm);
2378  value = RSTRING_PTR(val);
2379  if (memchr(name, '\0', RSTRING_LEN(nm)))
2380  rb_raise(rb_eArgError, "bad environment variable name");
2381  if (memchr(value, '\0', RSTRING_LEN(val)))
2382  rb_raise(rb_eArgError, "bad environment variable value");
2383 
2384  ruby_setenv(name, value);
2385  if (ENVMATCH(name, PATH_ENV)) {
2386  if (OBJ_TAINTED(val)) {
2387  /* already tainted, no check */
2388  path_tainted = 1;
2389  return val;
2390  }
2391  else {
2392  path_tainted_p(value);
2393  }
2394  }
2395  return val;
2396 }
2397 
2398 /*
2399  * call-seq:
2400  * ENV.keys -> Array
2401  *
2402  * Returns every environment variable name in an Array
2403  */
2404 static VALUE
2406 {
2407  char **env;
2408  VALUE ary;
2409 
2410  rb_secure(4);
2411  ary = rb_ary_new();
2412  env = GET_ENVIRON(environ);
2413  while (*env) {
2414  char *s = strchr(*env, '=');
2415  if (s) {
2416  rb_ary_push(ary, env_str_new(*env, s-*env));
2417  }
2418  env++;
2419  }
2420  FREE_ENVIRON(environ);
2421  return ary;
2422 }
2423 
2424 /*
2425  * call-seq:
2426  * ENV.each_key { |name| } -> Hash
2427  * ENV.each_key -> Enumerator
2428  *
2429  * Yields each environment variable name.
2430  *
2431  * An Enumerator is returned if no block is given.
2432  */
2433 static VALUE
2435 {
2436  VALUE keys;
2437  long i;
2438 
2439  RETURN_ENUMERATOR(ehash, 0, 0);
2440  keys = env_keys(); /* rb_secure(4); */
2441  for (i=0; i<RARRAY_LEN(keys); i++) {
2442  rb_yield(RARRAY_PTR(keys)[i]);
2443  }
2444  return ehash;
2445 }
2446 
2447 /*
2448  * call-seq:
2449  * ENV.values -> Array
2450  *
2451  * Returns every environment variable value as an Array
2452  */
2453 static VALUE
2455 {
2456  VALUE ary;
2457  char **env;
2458 
2459  rb_secure(4);
2460  ary = rb_ary_new();
2461  env = GET_ENVIRON(environ);
2462  while (*env) {
2463  char *s = strchr(*env, '=');
2464  if (s) {
2465  rb_ary_push(ary, env_str_new2(s+1));
2466  }
2467  env++;
2468  }
2469  FREE_ENVIRON(environ);
2470  return ary;
2471 }
2472 
2473 /*
2474  * call-seq:
2475  * ENV.each_value { |value| } -> Hash
2476  * ENV.each_value -> Enumerator
2477  *
2478  * Yields each environment variable +value+.
2479  *
2480  * An Enumerator is returned if no block was given.
2481  */
2482 static VALUE
2484 {
2485  VALUE values;
2486  long i;
2487 
2488  RETURN_ENUMERATOR(ehash, 0, 0);
2489  values = env_values(); /* rb_secure(4); */
2490  for (i=0; i<RARRAY_LEN(values); i++) {
2491  rb_yield(RARRAY_PTR(values)[i]);
2492  }
2493  return ehash;
2494 }
2495 
2496 /*
2497  * call-seq:
2498  * ENV.each { |name, value| } -> Hash
2499  * ENV.each -> Enumerator
2500  * ENV.each_pair { |name, value| } -> Hash
2501  * ENV.each_pair -> Enumerator
2502  *
2503  * Yields each environment variable +name+ and +value+.
2504  *
2505  * If no block is given an Enumerator is returned.
2506  */
2507 static VALUE
2509 {
2510  char **env;
2511  VALUE ary;
2512  long i;
2513 
2514  RETURN_ENUMERATOR(ehash, 0, 0);
2515 
2516  rb_secure(4);
2517  ary = rb_ary_new();
2518  env = GET_ENVIRON(environ);
2519  while (*env) {
2520  char *s = strchr(*env, '=');
2521  if (s) {
2522  rb_ary_push(ary, env_str_new(*env, s-*env));
2523  rb_ary_push(ary, env_str_new2(s+1));
2524  }
2525  env++;
2526  }
2527  FREE_ENVIRON(environ);
2528 
2529  for (i=0; i<RARRAY_LEN(ary); i+=2) {
2530  rb_yield(rb_assoc_new(RARRAY_PTR(ary)[i], RARRAY_PTR(ary)[i+1]));
2531  }
2532  return ehash;
2533 }
2534 
2535 /*
2536  * call-seq:
2537  * ENV.reject! { |name, value| } -> Hash or nil
2538  * ENV.reject! -> Enumerator
2539  *
2540  * Equivalent to ENV#delete_if but returns +nil+ if no changes were made.
2541  *
2542  * Returns an Enumerator if no block was given.
2543  */
2544 static VALUE
2546 {
2547  volatile VALUE keys;
2548  long i;
2549  int del = 0;
2550 
2551  RETURN_ENUMERATOR(ehash, 0, 0);
2552  keys = env_keys(); /* rb_secure(4); */
2553  for (i=0; i<RARRAY_LEN(keys); i++) {
2554  VALUE val = rb_f_getenv(Qnil, RARRAY_PTR(keys)[i]);
2555  if (!NIL_P(val)) {
2556  if (RTEST(rb_yield_values(2, RARRAY_PTR(keys)[i], val))) {
2557  FL_UNSET(RARRAY_PTR(keys)[i], FL_TAINT);
2558  env_delete(Qnil, RARRAY_PTR(keys)[i]);
2559  del++;
2560  }
2561  }
2562  }
2563  if (del == 0) return Qnil;
2564  return envtbl;
2565 }
2566 
2567 /*
2568  * call-seq:
2569  * ENV.delete_if { |name, value| } -> Hash
2570  * ENV.delete_if -> Enumerator
2571  *
2572  * Deletes every environment variable for which the block evaluates to +true+.
2573  *
2574  * If no block is given an enumerator is returned instead.
2575  */
2576 static VALUE
2578 {
2579  RETURN_ENUMERATOR(ehash, 0, 0);
2580  env_reject_bang(ehash);
2581  return envtbl;
2582 }
2583 
2584 /*
2585  * call-seq:
2586  * ENV.values_at(name, ...) -> Array
2587  *
2588  * Returns an array containing the environment variable values associated with
2589  * the given names. See also ENV.select.
2590  */
2591 static VALUE
2593 {
2594  VALUE result;
2595  long i;
2596 
2597  rb_secure(4);
2598  result = rb_ary_new();
2599  for (i=0; i<argc; i++) {
2600  rb_ary_push(result, rb_f_getenv(Qnil, argv[i]));
2601  }
2602  return result;
2603 }
2604 
2605 /*
2606  * call-seq:
2607  * ENV.select { |name, value| } -> Hash
2608  * ENV.select -> Enumerator
2609  *
2610  * Returns a copy of the environment for entries where the block returns true.
2611  *
2612  * Returns an Enumerator if no block was given.
2613  */
2614 static VALUE
2616 {
2617  VALUE result;
2618  char **env;
2619 
2620  RETURN_ENUMERATOR(ehash, 0, 0);
2621  rb_secure(4);
2622  result = rb_hash_new();
2623  env = GET_ENVIRON(environ);
2624  while (*env) {
2625  char *s = strchr(*env, '=');
2626  if (s) {
2627  VALUE k = env_str_new(*env, s-*env);
2628  VALUE v = env_str_new2(s+1);
2629  if (RTEST(rb_yield_values(2, k, v))) {
2630  rb_hash_aset(result, k, v);
2631  }
2632  }
2633  env++;
2634  }
2635  FREE_ENVIRON(environ);
2636 
2637  return result;
2638 }
2639 
2640 /*
2641  * call-seq:
2642  * ENV.select! { |name, value| } -> ENV or nil
2643  * ENV.select! -> Enumerator
2644  *
2645  * Equivalent to ENV#keep_if but returns +nil+ if no changes were made.
2646  */
2647 static VALUE
2649 {
2650  volatile VALUE keys;
2651  long i;
2652  int del = 0;
2653 
2654  RETURN_ENUMERATOR(ehash, 0, 0);
2655  keys = env_keys(); /* rb_secure(4); */
2656  for (i=0; i<RARRAY_LEN(keys); i++) {
2657  VALUE val = rb_f_getenv(Qnil, RARRAY_PTR(keys)[i]);
2658  if (!NIL_P(val)) {
2659  if (!RTEST(rb_yield_values(2, RARRAY_PTR(keys)[i], val))) {
2660  FL_UNSET(RARRAY_PTR(keys)[i], FL_TAINT);
2661  env_delete(Qnil, RARRAY_PTR(keys)[i]);
2662  del++;
2663  }
2664  }
2665  }
2666  if (del == 0) return Qnil;
2667  return envtbl;
2668 }
2669 
2670 /*
2671  * call-seq:
2672  * ENV.keep_if { |name, value| } -> Hash
2673  * ENV.keep_if -> Enumerator
2674  *
2675  * Deletes every environment variable where the block evaluates to +false+.
2676  *
2677  * Returns an enumerator if no block was given.
2678  */
2679 static VALUE
2681 {
2682  RETURN_ENUMERATOR(ehash, 0, 0);
2683  env_select_bang(ehash);
2684  return envtbl;
2685 }
2686 
2687 /*
2688  * call-seq:
2689  * ENV.clear
2690  *
2691  * Removes every environment variable.
2692  */
2693 VALUE
2695 {
2696  volatile VALUE keys;
2697  long i;
2698 
2699  keys = env_keys(); /* rb_secure(4); */
2700  for (i=0; i<RARRAY_LEN(keys); i++) {
2701  VALUE val = rb_f_getenv(Qnil, RARRAY_PTR(keys)[i]);
2702  if (!NIL_P(val)) {
2703  env_delete(Qnil, RARRAY_PTR(keys)[i]);
2704  }
2705  }
2706  return envtbl;
2707 }
2708 
2709 /*
2710  * call-seq:
2711  * ENV.to_s -> "ENV"
2712  *
2713  * Returns "ENV"
2714  */
2715 static VALUE
2717 {
2718  return rb_usascii_str_new2("ENV");
2719 }
2720 
2721 /*
2722  * call-seq:
2723  * ENV.inspect -> string
2724  *
2725  * Returns the contents of the environment as a String.
2726  */
2727 static VALUE
2729 {
2730  char **env;
2731  VALUE str, i;
2732 
2733  rb_secure(4);
2734  str = rb_str_buf_new2("{");
2735  env = GET_ENVIRON(environ);
2736  while (*env) {
2737  char *s = strchr(*env, '=');
2738 
2739  if (env != environ) {
2740  rb_str_buf_cat2(str, ", ");
2741  }
2742  if (s) {
2743  rb_str_buf_cat2(str, "\"");
2744  rb_str_buf_cat(str, *env, s-*env);
2745  rb_str_buf_cat2(str, "\"=>");
2746  i = rb_inspect(rb_str_new2(s+1));
2747  rb_str_buf_append(str, i);
2748  }
2749  env++;
2750  }
2751  FREE_ENVIRON(environ);
2752  rb_str_buf_cat2(str, "}");
2753  OBJ_TAINT(str);
2754 
2755  return str;
2756 }
2757 
2758 /*
2759  * call-seq:
2760  * ENV.to_a -> Array
2761  *
2762  * Converts the environment variables into an array of names and value arrays.
2763  *
2764  * ENV.to_a # => [["TERM" => "xterm-color"], ["SHELL" => "/bin/bash"], ...]
2765  *
2766  */
2767 static VALUE
2769 {
2770  char **env;
2771  VALUE ary;
2772 
2773  rb_secure(4);
2774  ary = rb_ary_new();
2775  env = GET_ENVIRON(environ);
2776  while (*env) {
2777  char *s = strchr(*env, '=');
2778  if (s) {
2779  rb_ary_push(ary, rb_assoc_new(env_str_new(*env, s-*env),
2780  env_str_new2(s+1)));
2781  }
2782  env++;
2783  }
2784  FREE_ENVIRON(environ);
2785  return ary;
2786 }
2787 
2788 /*
2789  * call-seq:
2790  * ENV.rehash
2791  *
2792  * Re-hashing the environment variables does nothing. It is provided for
2793  * compatibility with Hash.
2794  */
2795 static VALUE
2797 {
2798  return Qnil;
2799 }
2800 
2801 /*
2802  * call-seq:
2803  * ENV.length
2804  * ENV.size
2805  *
2806  * Returns the number of environment variables.
2807  */
2808 static VALUE
2810 {
2811  int i;
2812  char **env;
2813 
2814  rb_secure(4);
2815  env = GET_ENVIRON(environ);
2816  for(i=0; env[i]; i++)
2817  ;
2818  FREE_ENVIRON(environ);
2819  return INT2FIX(i);
2820 }
2821 
2822 /*
2823  * call-seq:
2824  * ENV.empty? -> true or false
2825  *
2826  * Returns true when there are no environment variables
2827  */
2828 static VALUE
2830 {
2831  char **env;
2832 
2833  rb_secure(4);
2834  env = GET_ENVIRON(environ);
2835  if (env[0] == 0) {
2836  FREE_ENVIRON(environ);
2837  return Qtrue;
2838  }
2839  FREE_ENVIRON(environ);
2840  return Qfalse;
2841 }
2842 
2843 /*
2844  * call-seq:
2845  * ENV.key?(name) -> true or false
2846  * ENV.include?(name) -> true or false
2847  * ENV.has_key?(name) -> true or false
2848  * ENV.member?(name) -> true or false
2849  *
2850  * Returns +true+ if there is an environment variable with the given +name+.
2851  */
2852 static VALUE
2854 {
2855  char *s;
2856 
2857  rb_secure(4);
2858  s = StringValuePtr(key);
2859  if (memchr(s, '\0', RSTRING_LEN(key)))
2860  rb_raise(rb_eArgError, "bad environment variable name");
2861  if (getenv(s)) return Qtrue;
2862  return Qfalse;
2863 }
2864 
2865 /*
2866  * call-seq:
2867  * ENV.assoc(name) -> Array or nil
2868  *
2869  * Returns an Array of the name and value of the environment variable with
2870  * +name+ or +nil+ if the name cannot be found.
2871  */
2872 static VALUE
2874 {
2875  char *s, *e;
2876 
2877  rb_secure(4);
2878  s = StringValuePtr(key);
2879  if (memchr(s, '\0', RSTRING_LEN(key)))
2880  rb_raise(rb_eArgError, "bad environment variable name");
2881  e = getenv(s);
2882  if (e) return rb_assoc_new(key, rb_tainted_str_new2(e));
2883  return Qnil;
2884 }
2885 
2886 /*
2887  * call-seq:
2888  * ENV.value?(value) -> true or false
2889  * ENV.has_value?(value) -> true or false
2890  *
2891  * Returns +true+ if there is an environment variable with the given +value+.
2892  */
2893 static VALUE
2895 {
2896  char **env;
2897 
2898  rb_secure(4);
2899  obj = rb_check_string_type(obj);
2900  if (NIL_P(obj)) return Qnil;
2901  env = GET_ENVIRON(environ);
2902  while (*env) {
2903  char *s = strchr(*env, '=');
2904  if (s++) {
2905  long len = strlen(s);
2906  if (RSTRING_LEN(obj) == len && strncmp(s, RSTRING_PTR(obj), len) == 0) {
2907  FREE_ENVIRON(environ);
2908  return Qtrue;
2909  }
2910  }
2911  env++;
2912  }
2913  FREE_ENVIRON(environ);
2914  return Qfalse;
2915 }
2916 
2917 /*
2918  * call-seq:
2919  * ENV.rassoc(value)
2920  *
2921  * Returns an Array of the name and value of the environment variable with
2922  * +value+ or +nil+ if the value cannot be found.
2923  */
2924 static VALUE
2926 {
2927  char **env;
2928 
2929  rb_secure(4);
2930  obj = rb_check_string_type(obj);
2931  if (NIL_P(obj)) return Qnil;
2932  env = GET_ENVIRON(environ);
2933  while (*env) {
2934  char *s = strchr(*env, '=');
2935  if (s++) {
2936  long len = strlen(s);
2937  if (RSTRING_LEN(obj) == len && strncmp(s, RSTRING_PTR(obj), len) == 0) {
2938  VALUE result = rb_assoc_new(rb_tainted_str_new(*env, s-*env-1), obj);
2939  FREE_ENVIRON(environ);
2940  return result;
2941  }
2942  }
2943  env++;
2944  }
2945  FREE_ENVIRON(environ);
2946  return Qnil;
2947 }
2948 
2949 /*
2950  * call-seq:
2951  * ENV.key(value) -> name
2952  *
2953  * Returns the name of the environment variable with +value+. If the value is
2954  * not found +nil+ is returned.
2955  */
2956 static VALUE
2957 env_key(VALUE dmy, VALUE value)
2958 {
2959  char **env;
2960  VALUE str;
2961 
2962  rb_secure(4);
2963  StringValue(value);
2964  env = GET_ENVIRON(environ);
2965  while (*env) {
2966  char *s = strchr(*env, '=');
2967  if (s++) {
2968  long len = strlen(s);
2969  if (RSTRING_LEN(value) == len && strncmp(s, RSTRING_PTR(value), len) == 0) {
2970  str = env_str_new(*env, s-*env-1);
2971  FREE_ENVIRON(environ);
2972  return str;
2973  }
2974  }
2975  env++;
2976  }
2977  FREE_ENVIRON(environ);
2978  return Qnil;
2979 }
2980 
2981 /*
2982  * call-seq:
2983  * ENV.index(value) -> key
2984  *
2985  * Deprecated method that is equivalent to ENV.key
2986  */
2987 static VALUE
2988 env_index(VALUE dmy, VALUE value)
2989 {
2990  rb_warn("ENV.index is deprecated; use ENV.key");
2991  return env_key(dmy, value);
2992 }
2993 
2994 /*
2995  * call-seq:
2996  * ENV.to_hash -> Hash
2997  *
2998  * Creates a hash with a copy of the environment variables.
2999  *
3000  */
3001 static VALUE
3003 {
3004  char **env;
3005  VALUE hash;
3006 
3007  rb_secure(4);
3008  hash = rb_hash_new();
3009  env = GET_ENVIRON(environ);
3010  while (*env) {
3011  char *s = strchr(*env, '=');
3012  if (s) {
3013  rb_hash_aset(hash, env_str_new(*env, s-*env),
3014  env_str_new2(s+1));
3015  }
3016  env++;
3017  }
3018  FREE_ENVIRON(environ);
3019  return hash;
3020 }
3021 
3022 /*
3023  * call-seq:
3024  * ENV.reject { |name, value| } -> Hash
3025  * ENV.reject -> Enumerator
3026  *
3027  * Same as ENV#delete_if, but works on (and returns) a copy of the
3028  * environment.
3029  */
3030 static VALUE
3032 {
3033  return rb_hash_delete_if(env_to_hash());
3034 }
3035 
3036 /*
3037  * call-seq:
3038  * ENV.shift -> Array or nil
3039  *
3040  * Removes an environment variable name-value pair from ENV and returns it as
3041  * an Array. Returns +nil+ if when the environment is empty.
3042  */
3043 static VALUE
3045 {
3046  char **env;
3047 
3048  rb_secure(4);
3049  env = GET_ENVIRON(environ);
3050  if (*env) {
3051  char *s = strchr(*env, '=');
3052  if (s) {
3053  VALUE key = env_str_new(*env, s-*env);
3054  VALUE val = env_str_new2(getenv(RSTRING_PTR(key)));
3055  env_delete(Qnil, key);
3056  return rb_assoc_new(key, val);
3057  }
3058  }
3059  FREE_ENVIRON(environ);
3060  return Qnil;
3061 }
3062 
3063 /*
3064  * call-seq:
3065  * ENV.invert -> Hash
3066  *
3067  * Returns a new hash created by using environment variable names as values
3068  * and values as names.
3069  */
3070 static VALUE
3072 {
3073  return rb_hash_invert(env_to_hash());
3074 }
3075 
3076 static int
3078 {
3079  if (key != Qundef) {
3080  env_aset(Qnil, key, val);
3081  if (rb_ary_includes(keys, key)) {
3082  rb_ary_delete(keys, key);
3083  }
3084  }
3085  return ST_CONTINUE;
3086 }
3087 
3088 /*
3089  * call-seq:
3090  * ENV.replace(hash) -> env
3091  *
3092  * Replaces the contents of the environment variables with the contents of
3093  * +hash+.
3094  */
3095 static VALUE
3097 {
3098  volatile VALUE keys;
3099  long i;
3100 
3101  keys = env_keys(); /* rb_secure(4); */
3102  if (env == hash) return env;
3103  hash = to_hash(hash);
3104  rb_hash_foreach(hash, env_replace_i, keys);
3105 
3106  for (i=0; i<RARRAY_LEN(keys); i++) {
3107  env_delete(env, RARRAY_PTR(keys)[i]);
3108  }
3109  return env;
3110 }
3111 
3112 static int
3114 {
3115  if (key != Qundef) {
3116  if (rb_block_given_p()) {
3117  val = rb_yield_values(3, key, rb_f_getenv(Qnil, key), val);
3118  }
3119  env_aset(Qnil, key, val);
3120  }
3121  return ST_CONTINUE;
3122 }
3123 
3124 /*
3125  * call-seq:
3126  * ENV.update(hash) -> Hash
3127  * ENV.update(hash) { |name, old_value, new_value| } -> Hash
3128  *
3129  * Adds the contents of +hash+ to the environment variables. If no block is
3130  * specified entries with duplicate keys are overwritten, otherwise the value
3131  * of each duplicate name is determined by calling the block with the key, its
3132  * value from the environment and its value from the hash.
3133  */
3134 static VALUE
3136 {
3137  rb_secure(4);
3138  if (env == hash) return env;
3139  hash = to_hash(hash);
3140  rb_hash_foreach(hash, env_update_i, 0);
3141  return env;
3142 }
3143 
3144 /*
3145  * A <code>Hash</code> is a collection of key-value pairs. It is
3146  * similar to an <code>Array</code>, except that indexing is done via
3147  * arbitrary keys of any object type, not an integer index. Hashes enumerate
3148  * their values in the order that the corresponding keys were inserted.
3149  *
3150  * Hashes have a <em>default value</em> that is returned when accessing
3151  * keys that do not exist in the hash. By default, that value is
3152  * <code>nil</code>.
3153  *
3154  */
3155 
3156 void
3158 {
3159 #undef rb_intern
3160 #define rb_intern(str) rb_intern_const(str)
3161 
3162  id_hash = rb_intern("hash");
3163  id_yield = rb_intern("yield");
3164  id_default = rb_intern("default");
3165 
3166  rb_cHash = rb_define_class("Hash", rb_cObject);
3167 
3169 
3173  rb_define_method(rb_cHash,"initialize", rb_hash_initialize, -1);
3174  rb_define_method(rb_cHash,"initialize_copy", rb_hash_replace, 1);
3176 
3177  rb_define_method(rb_cHash,"to_hash", rb_hash_to_hash, 0);
3179  rb_define_method(rb_cHash,"inspect", rb_hash_inspect, 0);
3180  rb_define_alias(rb_cHash, "to_s", "inspect");
3181 
3188  rb_define_method(rb_cHash,"store", rb_hash_aset, 2);
3189  rb_define_method(rb_cHash,"default", rb_hash_default, -1);
3191  rb_define_method(rb_cHash,"default_proc", rb_hash_default_proc, 0);
3192  rb_define_method(rb_cHash,"default_proc=", rb_hash_set_default_proc, 1);
3196  rb_define_method(rb_cHash,"length", rb_hash_size, 0);
3198 
3199  rb_define_method(rb_cHash,"each_value", rb_hash_each_value, 0);
3200  rb_define_method(rb_cHash,"each_key", rb_hash_each_key, 0);
3201  rb_define_method(rb_cHash,"each_pair", rb_hash_each_pair, 0);
3203 
3206  rb_define_method(rb_cHash,"values_at", rb_hash_values_at, -1);
3207 
3210  rb_define_method(rb_cHash,"delete_if", rb_hash_delete_if, 0);
3211  rb_define_method(rb_cHash,"keep_if", rb_hash_keep_if, 0);
3219  rb_define_method(rb_cHash,"replace", rb_hash_replace, 1);
3222  rb_define_method(rb_cHash, "assoc", rb_hash_assoc, 1);
3223  rb_define_method(rb_cHash, "rassoc", rb_hash_rassoc, 1);
3224  rb_define_method(rb_cHash, "flatten", rb_hash_flatten, -1);
3225 
3226  rb_define_method(rb_cHash,"include?", rb_hash_has_key, 1);
3227  rb_define_method(rb_cHash,"member?", rb_hash_has_key, 1);
3228  rb_define_method(rb_cHash,"has_key?", rb_hash_has_key, 1);
3229  rb_define_method(rb_cHash,"has_value?", rb_hash_has_value, 1);
3232 
3233  rb_define_method(rb_cHash,"compare_by_identity", rb_hash_compare_by_id, 0);
3234  rb_define_method(rb_cHash,"compare_by_identity?", rb_hash_compare_by_id_p, 0);
3235 
3236  /* Document-class: ENV
3237  *
3238  * ENV is a hash-like accessor for environment variables.
3239  */
3240 
3241  /*
3242  * Hack to get RDoc to regard ENV as a class:
3243  * envtbl = rb_define_class("ENV", rb_cObject);
3244  */
3245  origenviron = environ;
3248 
3290 
3291  /*
3292  * ENV is a Hash-like accessor for environment variables.
3293  *
3294  * See ENV (the class) for more details.
3295  */
3297 }
3298