Ruby 2.0.0p353(2013-11-22revision43784)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

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