diff options
Diffstat (limited to 'hash.c')
-rw-r--r-- | hash.c | 85 |
1 files changed, 61 insertions, 24 deletions
@@ -538,7 +538,7 @@ rb_hash_fetch(argc, argv, hash) * h.default(2) #=> "cat" * * h = Hash.new {|h,k| h[k] = k.to_i*10} #=> {} - * h.default #=> 0 + * h.default #=> nil * h.default(2) #=> 20 */ @@ -891,7 +891,7 @@ select_i(key, value, result) * * h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" } * h.values_at("cow", "cat") #=> ["bovine", "feline"] -*/ + */ VALUE rb_hash_values_at(argc, argv, hash) @@ -1212,6 +1212,7 @@ static VALUE rb_hash_each(hash) VALUE hash; { + RETURN_ENUMERATOR(hash, 0, 0); rb_hash_foreach(hash, each_i, 0); return hash; } @@ -1716,12 +1717,20 @@ rb_hash_update_block_i(key, value, hash) * hsh.merge!(other_hash){|key, oldval, newval| block} => hsh * hsh.update(other_hash){|key, oldval, newval| block} => hsh * - * Adds the contents of <i>other_hash</i> to <i>hsh</i>, overwriting - * entries with duplicate keys with those from <i>other_hash</i>. + * Adds the contents of <i>other_hash</i> to <i>hsh</i>. If no + * block is specified entries with duplicate keys are overwritten + * with the values from <i>other_hash</i>, otherwise the value + * of each duplicate key is determined by calling the block with + * the key, its value in <i>hsh</i> and its value in <i>other_hash</i>. * * h1 = { "a" => 100, "b" => 200 } * h2 = { "b" => 254, "c" => 300 } * h1.merge!(h2) #=> {"a"=>100, "b"=>254, "c"=>300} + * + * h1 = { "a" => 100, "b" => 200 } + * h2 = { "b" => 254, "c" => 300 } + * h1.merge!(h2) { |key, v1, v2| v1 } + * #=> {"a"=>100, "b"=>200, "c"=>300} */ static VALUE @@ -1847,7 +1856,8 @@ rb_f_getenv(obj, name) { char *nam, *env; - StringValue(name); + rb_secure(4); + SafeStringValue(name); nam = RSTRING(name)->ptr; if (strlen(nam) != RSTRING(name)->len) { rb_raise(rb_eArgError, "bad environment variable name"); @@ -1879,12 +1889,13 @@ env_fetch(argc, argv) long block_given; char *nam, *env; + rb_secure(4); rb_scan_args(argc, argv, "11", &key, &if_none); block_given = rb_block_given_p(); if (block_given && argc == 2) { rb_warn("block supersedes default value argument"); } - StringValue(key); + SafeStringValue(key); nam = RSTRING(key)->ptr; if (strlen(nam) != RSTRING(key)->len) { rb_raise(rb_eArgError, "bad environment variable name"); @@ -1922,6 +1933,7 @@ rb_env_path_tainted() return path_tainted; } +#if !defined(_WIN32) && !(defined(HAVE_SETENV) && defined(HAVE_UNSETENV)) static int envix(nam) const char *nam; @@ -1943,6 +1955,7 @@ envix(nam) FREE_ENVIRON(environ); return i; } +#endif void ruby_setenv(name, value) @@ -2079,8 +2092,10 @@ static VALUE env_keys() { char **env; - VALUE ary = rb_ary_new(); + VALUE ary; + rb_secure(4); + ary = rb_ary_new(); env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); @@ -2097,10 +2112,11 @@ static VALUE env_each_key(ehash) VALUE ehash; { - VALUE keys = env_keys(); + VALUE keys; long i; RETURN_ENUMERATOR(ehash, 0, 0); + keys = env_keys(); /* rb_secure(4); */ for (i=0; i<RARRAY(keys)->len; i++) { rb_yield(RARRAY(keys)->ptr[i]); } @@ -2110,9 +2126,11 @@ env_each_key(ehash) static VALUE env_values() { + VALUE ary; char **env; - VALUE ary = rb_ary_new(); + rb_secure(4); + ary = rb_ary_new(); env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); @@ -2129,10 +2147,11 @@ static VALUE env_each_value(ehash) VALUE ehash; { - VALUE values = env_values(); + VALUE values; long i; RETURN_ENUMERATOR(ehash, 0, 0); + values = env_values(); /* rb_secure(4); */ for (i=0; i<RARRAY(values)->len; i++) { rb_yield(RARRAY(values)->ptr[i]); } @@ -2145,9 +2164,11 @@ env_each_i(ehash, values) int values; { char **env; - VALUE ary = rb_ary_new(); + VALUE ary; long i; + rb_secure(4); + ary = rb_ary_new(); env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); @@ -2182,6 +2203,7 @@ static VALUE env_each_pair(ehash) VALUE ehash; { + RETURN_ENUMERATOR(ehash, 0, 0); return env_each_i(ehash, Qtrue); } @@ -2194,9 +2216,7 @@ env_reject_bang(ehash) int del = 0; RETURN_ENUMERATOR(ehash, 0, 0); - rb_secure(4); - keys = env_keys(); - + keys = env_keys(); /* rb_secure(4); */ for (i=0; i<RARRAY(keys)->len; i++) { VALUE val = rb_f_getenv(Qnil, RARRAY(keys)->ptr[i]); if (!NIL_P(val)) { @@ -2215,6 +2235,7 @@ static VALUE env_delete_if(ehash) VALUE ehash; { + RETURN_ENUMERATOR(ehash, 0, 0); env_reject_bang(ehash); return envtbl; } @@ -2224,9 +2245,11 @@ env_values_at(argc, argv) int argc; VALUE *argv; { - VALUE result = rb_ary_new(); + VALUE result; long i; + rb_secure(4); + result = rb_ary_new(); for (i=0; i<argc; i++) { rb_ary_push(result, rb_f_getenv(Qnil, argv[i])); } @@ -2241,6 +2264,7 @@ env_select(ehash) char **env; RETURN_ENUMERATOR(ehash, 0, 0); + rb_secure(4); result = rb_ary_new(); env = GET_ENVIRON(environ); while (*env) { @@ -2265,9 +2289,7 @@ env_clear() volatile VALUE keys; long i; - rb_secure(4); - keys = env_keys(); - + keys = env_keys(); /* rb_secure(4); */ for (i=0; i<RARRAY(keys)->len; i++) { VALUE val = rb_f_getenv(Qnil, RARRAY(keys)->ptr[i]); if (!NIL_P(val)) { @@ -2287,9 +2309,10 @@ static VALUE env_inspect() { char **env; - VALUE str = rb_str_buf_new2("{"); - VALUE i; + VALUE str, i; + rb_secure(4); + str = rb_str_buf_new2("{"); env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); @@ -2317,8 +2340,10 @@ static VALUE env_to_a() { char **env; - VALUE ary = rb_ary_new(); + VALUE ary; + rb_secure(4); + ary = rb_ary_new(); env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); @@ -2344,6 +2369,7 @@ env_size() int i; char **env; + rb_secure(4); env = GET_ENVIRON(environ); for(i=0; env[i]; i++) ; @@ -2356,6 +2382,7 @@ env_empty_p() { char **env; + rb_secure(4); env = GET_ENVIRON(environ); if (env[0] == 0) { FREE_ENVIRON(environ); @@ -2371,6 +2398,7 @@ env_has_key(env, key) { char *s; + rb_secure(4); s = StringValuePtr(key); if (strlen(s) != RSTRING(key)->len) rb_raise(rb_eArgError, "bad environment variable name"); @@ -2384,7 +2412,9 @@ env_has_value(dmy, value) { char **env; - if (TYPE(value) != T_STRING) return Qfalse; + rb_secure(4); + value = rb_check_string_type(value); + if (NIL_P(value)) return Qfalse; env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); @@ -2408,6 +2438,7 @@ env_index(dmy, value) char **env; VALUE str; + rb_secure(4); StringValue(value); env = GET_ENVIRON(environ); while (*env) { @@ -2436,6 +2467,7 @@ env_indexes(argc, argv) rb_warn("ENV.%s is deprecated; use ENV.values_at", rb_id2name(rb_frame_last_func())); + rb_secure(4); for (i=0;i<argc;i++) { VALUE tmp = rb_check_string_type(argv[i]); if (NIL_P(tmp)) { @@ -2454,8 +2486,10 @@ static VALUE env_to_hash() { char **env; - VALUE hash = rb_hash_new(); + VALUE hash; + rb_secure(4); + hash = rb_hash_new(); env = GET_ENVIRON(environ); while (*env) { char *s = strchr(*env, '='); @@ -2480,6 +2514,7 @@ env_shift() { char **env; + rb_secure(4); env = GET_ENVIRON(environ); if (*env) { char *s = strchr(*env, '='); @@ -2517,9 +2552,10 @@ static VALUE env_replace(env, hash) VALUE env, hash; { - volatile VALUE keys = env_keys(); + volatile VALUE keys; long i; + keys = env_keys(); /* rb_secure(4); */ if (env == hash) return env; hash = to_hash(hash); rb_hash_foreach(hash, env_replace_i, keys); @@ -2547,6 +2583,7 @@ static VALUE env_update(env, hash) VALUE env, hash; { + rb_secure(4); if (env == hash) return env; hash = to_hash(hash); rb_hash_foreach(hash, env_update_i, 0); |