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