mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* hash.c (rb_hash_default): Fix rdoc.
(rb_hash_each, env_each_value, env_each_pair): Return an enumerator if no block is given. (rb_hash_update): Update rdoc. (envix): Conditionalize the definition itself. (rb_f_getenv, env_fetch, env_keys, env_values, env_values_at) (env_select, env_inspect, env_to_a, env_empty_p, env_has_key) (env_has_value, env_index, env_indexes, env_to_hash, env_shift) (env_update): Require secure level 4. (env_each_value, env_each_i): Delay variable initialization. (env_each_key, env_each_value, env_reject_bang) (env_clear, env_replace): Omit duplicated secure level check. (env_has_value): Do to_str conversion. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@16593 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
e292984622
commit
02df8f6a5b
2 changed files with 75 additions and 24 deletions
16
ChangeLog
16
ChangeLog
|
@ -1,3 +1,19 @@
|
||||||
|
Mon May 26 03:16:20 2008 Akinori MUSHA <knu@iDaemons.org>
|
||||||
|
|
||||||
|
* hash.c (rb_hash_default): Fix rdoc.
|
||||||
|
(rb_hash_each, env_each_value, env_each_pair): Return an
|
||||||
|
enumerator if no block is given.
|
||||||
|
(rb_hash_update): Update rdoc.
|
||||||
|
(envix): Conditionalize the definition itself.
|
||||||
|
(rb_f_getenv, env_fetch, env_keys, env_values, env_values_at)
|
||||||
|
(env_select, env_inspect, env_to_a, env_empty_p, env_has_key)
|
||||||
|
(env_has_value, env_index, env_indexes, env_to_hash, env_shift)
|
||||||
|
(env_update): Require secure level 4.
|
||||||
|
(env_each_value, env_each_i): Delay variable initialization.
|
||||||
|
(env_each_key, env_each_value, env_reject_bang)
|
||||||
|
(env_clear, env_replace): Omit duplicated secure level check.
|
||||||
|
(env_has_value): Do to_str conversion.
|
||||||
|
|
||||||
Sun May 25 19:48:12 2008 Akinori MUSHA <knu@iDaemons.org>
|
Sun May 25 19:48:12 2008 Akinori MUSHA <knu@iDaemons.org>
|
||||||
|
|
||||||
* hash.c (env_delete_if): Return an enumerator if no block is
|
* hash.c (env_delete_if): Return an enumerator if no block is
|
||||||
|
|
83
hash.c
83
hash.c
|
@ -538,7 +538,7 @@ rb_hash_fetch(argc, argv, hash)
|
||||||
* h.default(2) #=> "cat"
|
* h.default(2) #=> "cat"
|
||||||
*
|
*
|
||||||
* h = Hash.new {|h,k| h[k] = k.to_i*10} #=> {}
|
* h = Hash.new {|h,k| h[k] = k.to_i*10} #=> {}
|
||||||
* h.default #=> 0
|
* h.default #=> nil
|
||||||
* h.default(2) #=> 20
|
* h.default(2) #=> 20
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -891,7 +891,7 @@ select_i(key, value, result)
|
||||||
*
|
*
|
||||||
* h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }
|
* h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }
|
||||||
* h.values_at("cow", "cat") #=> ["bovine", "feline"]
|
* h.values_at("cow", "cat") #=> ["bovine", "feline"]
|
||||||
*/
|
*/
|
||||||
|
|
||||||
VALUE
|
VALUE
|
||||||
rb_hash_values_at(argc, argv, hash)
|
rb_hash_values_at(argc, argv, hash)
|
||||||
|
@ -1212,6 +1212,7 @@ static VALUE
|
||||||
rb_hash_each(hash)
|
rb_hash_each(hash)
|
||||||
VALUE hash;
|
VALUE hash;
|
||||||
{
|
{
|
||||||
|
RETURN_ENUMERATOR(hash, 0, 0);
|
||||||
rb_hash_foreach(hash, each_i, 0);
|
rb_hash_foreach(hash, each_i, 0);
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
@ -1716,12 +1717,20 @@ rb_hash_update_block_i(key, value, hash)
|
||||||
* hsh.merge!(other_hash){|key, oldval, newval| block} => hsh
|
* hsh.merge!(other_hash){|key, oldval, newval| block} => hsh
|
||||||
* hsh.update(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
|
* Adds the contents of <i>other_hash</i> to <i>hsh</i>. If no
|
||||||
* entries with duplicate keys with those from <i>other_hash</i>.
|
* 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 }
|
* h1 = { "a" => 100, "b" => 200 }
|
||||||
* h2 = { "b" => 254, "c" => 300 }
|
* h2 = { "b" => 254, "c" => 300 }
|
||||||
* h1.merge!(h2) #=> {"a"=>100, "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
|
static VALUE
|
||||||
|
@ -1847,7 +1856,8 @@ rb_f_getenv(obj, name)
|
||||||
{
|
{
|
||||||
char *nam, *env;
|
char *nam, *env;
|
||||||
|
|
||||||
StringValue(name);
|
rb_secure(4);
|
||||||
|
SafeStringValue(name);
|
||||||
nam = RSTRING(name)->ptr;
|
nam = RSTRING(name)->ptr;
|
||||||
if (strlen(nam) != RSTRING(name)->len) {
|
if (strlen(nam) != RSTRING(name)->len) {
|
||||||
rb_raise(rb_eArgError, "bad environment variable name");
|
rb_raise(rb_eArgError, "bad environment variable name");
|
||||||
|
@ -1879,12 +1889,13 @@ env_fetch(argc, argv)
|
||||||
long block_given;
|
long block_given;
|
||||||
char *nam, *env;
|
char *nam, *env;
|
||||||
|
|
||||||
|
rb_secure(4);
|
||||||
rb_scan_args(argc, argv, "11", &key, &if_none);
|
rb_scan_args(argc, argv, "11", &key, &if_none);
|
||||||
block_given = rb_block_given_p();
|
block_given = rb_block_given_p();
|
||||||
if (block_given && argc == 2) {
|
if (block_given && argc == 2) {
|
||||||
rb_warn("block supersedes default value argument");
|
rb_warn("block supersedes default value argument");
|
||||||
}
|
}
|
||||||
StringValue(key);
|
SafeStringValue(key);
|
||||||
nam = RSTRING(key)->ptr;
|
nam = RSTRING(key)->ptr;
|
||||||
if (strlen(nam) != RSTRING(key)->len) {
|
if (strlen(nam) != RSTRING(key)->len) {
|
||||||
rb_raise(rb_eArgError, "bad environment variable name");
|
rb_raise(rb_eArgError, "bad environment variable name");
|
||||||
|
@ -1922,6 +1933,7 @@ rb_env_path_tainted()
|
||||||
return path_tainted;
|
return path_tainted;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !defined(_WIN32) && !(defined(HAVE_SETENV) && defined(HAVE_UNSETENV))
|
||||||
static int
|
static int
|
||||||
envix(nam)
|
envix(nam)
|
||||||
const char *nam;
|
const char *nam;
|
||||||
|
@ -1943,6 +1955,7 @@ envix(nam)
|
||||||
FREE_ENVIRON(environ);
|
FREE_ENVIRON(environ);
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void
|
void
|
||||||
ruby_setenv(name, value)
|
ruby_setenv(name, value)
|
||||||
|
@ -2079,8 +2092,10 @@ static VALUE
|
||||||
env_keys()
|
env_keys()
|
||||||
{
|
{
|
||||||
char **env;
|
char **env;
|
||||||
VALUE ary = rb_ary_new();
|
VALUE ary;
|
||||||
|
|
||||||
|
rb_secure(4);
|
||||||
|
ary = rb_ary_new();
|
||||||
env = GET_ENVIRON(environ);
|
env = GET_ENVIRON(environ);
|
||||||
while (*env) {
|
while (*env) {
|
||||||
char *s = strchr(*env, '=');
|
char *s = strchr(*env, '=');
|
||||||
|
@ -2101,7 +2116,7 @@ env_each_key(ehash)
|
||||||
long i;
|
long i;
|
||||||
|
|
||||||
RETURN_ENUMERATOR(ehash, 0, 0);
|
RETURN_ENUMERATOR(ehash, 0, 0);
|
||||||
keys = env_keys();
|
keys = env_keys(); /* rb_secure(4); */
|
||||||
for (i=0; i<RARRAY(keys)->len; i++) {
|
for (i=0; i<RARRAY(keys)->len; i++) {
|
||||||
rb_yield(RARRAY(keys)->ptr[i]);
|
rb_yield(RARRAY(keys)->ptr[i]);
|
||||||
}
|
}
|
||||||
|
@ -2111,9 +2126,11 @@ env_each_key(ehash)
|
||||||
static VALUE
|
static VALUE
|
||||||
env_values()
|
env_values()
|
||||||
{
|
{
|
||||||
|
VALUE ary;
|
||||||
char **env;
|
char **env;
|
||||||
VALUE ary = rb_ary_new();
|
|
||||||
|
|
||||||
|
rb_secure(4);
|
||||||
|
ary = rb_ary_new();
|
||||||
env = GET_ENVIRON(environ);
|
env = GET_ENVIRON(environ);
|
||||||
while (*env) {
|
while (*env) {
|
||||||
char *s = strchr(*env, '=');
|
char *s = strchr(*env, '=');
|
||||||
|
@ -2130,10 +2147,11 @@ static VALUE
|
||||||
env_each_value(ehash)
|
env_each_value(ehash)
|
||||||
VALUE ehash;
|
VALUE ehash;
|
||||||
{
|
{
|
||||||
VALUE values = env_values();
|
VALUE values;
|
||||||
long i;
|
long i;
|
||||||
|
|
||||||
RETURN_ENUMERATOR(ehash, 0, 0);
|
RETURN_ENUMERATOR(ehash, 0, 0);
|
||||||
|
values = env_values(); /* rb_secure(4); */
|
||||||
for (i=0; i<RARRAY(values)->len; i++) {
|
for (i=0; i<RARRAY(values)->len; i++) {
|
||||||
rb_yield(RARRAY(values)->ptr[i]);
|
rb_yield(RARRAY(values)->ptr[i]);
|
||||||
}
|
}
|
||||||
|
@ -2146,9 +2164,11 @@ env_each_i(ehash, values)
|
||||||
int values;
|
int values;
|
||||||
{
|
{
|
||||||
char **env;
|
char **env;
|
||||||
VALUE ary = rb_ary_new();
|
VALUE ary;
|
||||||
long i;
|
long i;
|
||||||
|
|
||||||
|
rb_secure(4);
|
||||||
|
ary = rb_ary_new();
|
||||||
env = GET_ENVIRON(environ);
|
env = GET_ENVIRON(environ);
|
||||||
while (*env) {
|
while (*env) {
|
||||||
char *s = strchr(*env, '=');
|
char *s = strchr(*env, '=');
|
||||||
|
@ -2183,6 +2203,7 @@ static VALUE
|
||||||
env_each_pair(ehash)
|
env_each_pair(ehash)
|
||||||
VALUE ehash;
|
VALUE ehash;
|
||||||
{
|
{
|
||||||
|
RETURN_ENUMERATOR(ehash, 0, 0);
|
||||||
return env_each_i(ehash, Qtrue);
|
return env_each_i(ehash, Qtrue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2195,9 +2216,7 @@ env_reject_bang(ehash)
|
||||||
int del = 0;
|
int del = 0;
|
||||||
|
|
||||||
RETURN_ENUMERATOR(ehash, 0, 0);
|
RETURN_ENUMERATOR(ehash, 0, 0);
|
||||||
rb_secure(4);
|
keys = env_keys(); /* rb_secure(4); */
|
||||||
keys = env_keys();
|
|
||||||
|
|
||||||
for (i=0; i<RARRAY(keys)->len; i++) {
|
for (i=0; i<RARRAY(keys)->len; i++) {
|
||||||
VALUE val = rb_f_getenv(Qnil, RARRAY(keys)->ptr[i]);
|
VALUE val = rb_f_getenv(Qnil, RARRAY(keys)->ptr[i]);
|
||||||
if (!NIL_P(val)) {
|
if (!NIL_P(val)) {
|
||||||
|
@ -2226,9 +2245,11 @@ env_values_at(argc, argv)
|
||||||
int argc;
|
int argc;
|
||||||
VALUE *argv;
|
VALUE *argv;
|
||||||
{
|
{
|
||||||
VALUE result = rb_ary_new();
|
VALUE result;
|
||||||
long i;
|
long i;
|
||||||
|
|
||||||
|
rb_secure(4);
|
||||||
|
result = rb_ary_new();
|
||||||
for (i=0; i<argc; i++) {
|
for (i=0; i<argc; i++) {
|
||||||
rb_ary_push(result, rb_f_getenv(Qnil, argv[i]));
|
rb_ary_push(result, rb_f_getenv(Qnil, argv[i]));
|
||||||
}
|
}
|
||||||
|
@ -2243,6 +2264,7 @@ env_select(ehash)
|
||||||
char **env;
|
char **env;
|
||||||
|
|
||||||
RETURN_ENUMERATOR(ehash, 0, 0);
|
RETURN_ENUMERATOR(ehash, 0, 0);
|
||||||
|
rb_secure(4);
|
||||||
result = rb_ary_new();
|
result = rb_ary_new();
|
||||||
env = GET_ENVIRON(environ);
|
env = GET_ENVIRON(environ);
|
||||||
while (*env) {
|
while (*env) {
|
||||||
|
@ -2267,9 +2289,7 @@ env_clear()
|
||||||
volatile VALUE keys;
|
volatile VALUE keys;
|
||||||
long i;
|
long i;
|
||||||
|
|
||||||
rb_secure(4);
|
keys = env_keys(); /* rb_secure(4); */
|
||||||
keys = env_keys();
|
|
||||||
|
|
||||||
for (i=0; i<RARRAY(keys)->len; i++) {
|
for (i=0; i<RARRAY(keys)->len; i++) {
|
||||||
VALUE val = rb_f_getenv(Qnil, RARRAY(keys)->ptr[i]);
|
VALUE val = rb_f_getenv(Qnil, RARRAY(keys)->ptr[i]);
|
||||||
if (!NIL_P(val)) {
|
if (!NIL_P(val)) {
|
||||||
|
@ -2289,9 +2309,10 @@ static VALUE
|
||||||
env_inspect()
|
env_inspect()
|
||||||
{
|
{
|
||||||
char **env;
|
char **env;
|
||||||
VALUE str = rb_str_buf_new2("{");
|
VALUE str, i;
|
||||||
VALUE i;
|
|
||||||
|
|
||||||
|
rb_secure(4);
|
||||||
|
str = rb_str_buf_new2("{");
|
||||||
env = GET_ENVIRON(environ);
|
env = GET_ENVIRON(environ);
|
||||||
while (*env) {
|
while (*env) {
|
||||||
char *s = strchr(*env, '=');
|
char *s = strchr(*env, '=');
|
||||||
|
@ -2319,8 +2340,10 @@ static VALUE
|
||||||
env_to_a()
|
env_to_a()
|
||||||
{
|
{
|
||||||
char **env;
|
char **env;
|
||||||
VALUE ary = rb_ary_new();
|
VALUE ary;
|
||||||
|
|
||||||
|
rb_secure(4);
|
||||||
|
ary = rb_ary_new();
|
||||||
env = GET_ENVIRON(environ);
|
env = GET_ENVIRON(environ);
|
||||||
while (*env) {
|
while (*env) {
|
||||||
char *s = strchr(*env, '=');
|
char *s = strchr(*env, '=');
|
||||||
|
@ -2346,6 +2369,7 @@ env_size()
|
||||||
int i;
|
int i;
|
||||||
char **env;
|
char **env;
|
||||||
|
|
||||||
|
rb_secure(4);
|
||||||
env = GET_ENVIRON(environ);
|
env = GET_ENVIRON(environ);
|
||||||
for(i=0; env[i]; i++)
|
for(i=0; env[i]; i++)
|
||||||
;
|
;
|
||||||
|
@ -2358,6 +2382,7 @@ env_empty_p()
|
||||||
{
|
{
|
||||||
char **env;
|
char **env;
|
||||||
|
|
||||||
|
rb_secure(4);
|
||||||
env = GET_ENVIRON(environ);
|
env = GET_ENVIRON(environ);
|
||||||
if (env[0] == 0) {
|
if (env[0] == 0) {
|
||||||
FREE_ENVIRON(environ);
|
FREE_ENVIRON(environ);
|
||||||
|
@ -2373,6 +2398,7 @@ env_has_key(env, key)
|
||||||
{
|
{
|
||||||
char *s;
|
char *s;
|
||||||
|
|
||||||
|
rb_secure(4);
|
||||||
s = StringValuePtr(key);
|
s = StringValuePtr(key);
|
||||||
if (strlen(s) != RSTRING(key)->len)
|
if (strlen(s) != RSTRING(key)->len)
|
||||||
rb_raise(rb_eArgError, "bad environment variable name");
|
rb_raise(rb_eArgError, "bad environment variable name");
|
||||||
|
@ -2386,7 +2412,9 @@ env_has_value(dmy, value)
|
||||||
{
|
{
|
||||||
char **env;
|
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);
|
env = GET_ENVIRON(environ);
|
||||||
while (*env) {
|
while (*env) {
|
||||||
char *s = strchr(*env, '=');
|
char *s = strchr(*env, '=');
|
||||||
|
@ -2410,6 +2438,7 @@ env_index(dmy, value)
|
||||||
char **env;
|
char **env;
|
||||||
VALUE str;
|
VALUE str;
|
||||||
|
|
||||||
|
rb_secure(4);
|
||||||
StringValue(value);
|
StringValue(value);
|
||||||
env = GET_ENVIRON(environ);
|
env = GET_ENVIRON(environ);
|
||||||
while (*env) {
|
while (*env) {
|
||||||
|
@ -2438,6 +2467,7 @@ env_indexes(argc, argv)
|
||||||
|
|
||||||
rb_warn("ENV.%s is deprecated; use ENV.values_at",
|
rb_warn("ENV.%s is deprecated; use ENV.values_at",
|
||||||
rb_id2name(rb_frame_last_func()));
|
rb_id2name(rb_frame_last_func()));
|
||||||
|
rb_secure(4);
|
||||||
for (i=0;i<argc;i++) {
|
for (i=0;i<argc;i++) {
|
||||||
VALUE tmp = rb_check_string_type(argv[i]);
|
VALUE tmp = rb_check_string_type(argv[i]);
|
||||||
if (NIL_P(tmp)) {
|
if (NIL_P(tmp)) {
|
||||||
|
@ -2456,8 +2486,10 @@ static VALUE
|
||||||
env_to_hash()
|
env_to_hash()
|
||||||
{
|
{
|
||||||
char **env;
|
char **env;
|
||||||
VALUE hash = rb_hash_new();
|
VALUE hash;
|
||||||
|
|
||||||
|
rb_secure(4);
|
||||||
|
hash = rb_hash_new();
|
||||||
env = GET_ENVIRON(environ);
|
env = GET_ENVIRON(environ);
|
||||||
while (*env) {
|
while (*env) {
|
||||||
char *s = strchr(*env, '=');
|
char *s = strchr(*env, '=');
|
||||||
|
@ -2482,6 +2514,7 @@ env_shift()
|
||||||
{
|
{
|
||||||
char **env;
|
char **env;
|
||||||
|
|
||||||
|
rb_secure(4);
|
||||||
env = GET_ENVIRON(environ);
|
env = GET_ENVIRON(environ);
|
||||||
if (*env) {
|
if (*env) {
|
||||||
char *s = strchr(*env, '=');
|
char *s = strchr(*env, '=');
|
||||||
|
@ -2519,9 +2552,10 @@ static VALUE
|
||||||
env_replace(env, hash)
|
env_replace(env, hash)
|
||||||
VALUE env, hash;
|
VALUE env, hash;
|
||||||
{
|
{
|
||||||
volatile VALUE keys = env_keys();
|
volatile VALUE keys;
|
||||||
long i;
|
long i;
|
||||||
|
|
||||||
|
keys = env_keys(); /* rb_secure(4); */
|
||||||
if (env == hash) return env;
|
if (env == hash) return env;
|
||||||
hash = to_hash(hash);
|
hash = to_hash(hash);
|
||||||
rb_hash_foreach(hash, env_replace_i, keys);
|
rb_hash_foreach(hash, env_replace_i, keys);
|
||||||
|
@ -2549,6 +2583,7 @@ static VALUE
|
||||||
env_update(env, hash)
|
env_update(env, hash)
|
||||||
VALUE env, hash;
|
VALUE env, hash;
|
||||||
{
|
{
|
||||||
|
rb_secure(4);
|
||||||
if (env == hash) return env;
|
if (env == hash) return env;
|
||||||
hash = to_hash(hash);
|
hash = to_hash(hash);
|
||||||
rb_hash_foreach(hash, env_update_i, 0);
|
rb_hash_foreach(hash, env_update_i, 0);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue