1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

dir.c: dir_each_entries

* dir.c (dir_each_entries): expand entries without method calls.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58877 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2017-05-25 01:46:36 +00:00
parent 2079b71000
commit bf463442ed

28
dir.c
View file

@ -774,6 +774,14 @@ dir_read(VALUE dir)
} }
} }
static VALUE dir_each_entry(VALUE, VALUE (*)(VALUE, VALUE), VALUE);
static VALUE
dir_yield(VALUE arg, VALUE path)
{
return rb_yield(path);
}
/* /*
* call-seq: * call-seq:
* dir.each { |filename| block } -> dir * dir.each { |filename| block } -> dir
@ -796,12 +804,18 @@ dir_read(VALUE dir)
*/ */
static VALUE static VALUE
dir_each(VALUE dir) dir_each(VALUE dir)
{
RETURN_ENUMERATOR(dir, 0, 0);
return dir_each_entry(dir, dir_yield, Qnil);
}
static VALUE
dir_each_entry(VALUE dir, VALUE (*each)(VALUE, VALUE), VALUE arg)
{ {
struct dir_data *dirp; struct dir_data *dirp;
struct dirent *dp; struct dirent *dp;
IF_NORMALIZE_UTF8PATH(int norm_p); IF_NORMALIZE_UTF8PATH(int norm_p);
RETURN_ENUMERATOR(dir, 0, 0);
GetDIR(dir, dirp); GetDIR(dir, dirp);
rewinddir(dirp->dir); rewinddir(dirp->dir);
IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp->dir, RSTRING_PTR(dirp->path))); IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp->dir, RSTRING_PTR(dirp->path)));
@ -817,7 +831,7 @@ dir_each(VALUE dir)
else else
#endif #endif
path = rb_external_str_new_with_enc(name, namlen, dirp->enc); path = rb_external_str_new_with_enc(name, namlen, dirp->enc);
rb_yield(path); (*each)(arg, path);
if (dirp->dir == NULL) dir_closed(); if (dirp->dir == NULL) dir_closed();
} }
return dir; return dir;
@ -2610,6 +2624,14 @@ dir_foreach(int argc, VALUE *argv, VALUE io)
return Qnil; return Qnil;
} }
static VALUE
dir_collect(VALUE dir)
{
VALUE ary = rb_ary_new();
dir_each_entry(dir, rb_ary_push, ary);
return ary;
}
/* /*
* call-seq: * call-seq:
* Dir.entries( dirname ) -> array * Dir.entries( dirname ) -> array
@ -2631,7 +2653,7 @@ dir_entries(int argc, VALUE *argv, VALUE io)
VALUE dir; VALUE dir;
dir = dir_open_dir(argc, argv); dir = dir_open_dir(argc, argv);
return rb_ensure(rb_Array, dir, dir_close, dir); return rb_ensure(dir_collect, dir, dir_close, dir);
} }
static int static int