summaryrefslogtreecommitdiff
path: root/dir.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-05-25 01:46:36 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-05-25 01:46:36 +0000
commitbf463442ed29bed024ed0b9fe2c911ce020e5ad9 (patch)
tree8ec5281d599ef9765a202d61f5fd32186fe29124 /dir.c
parent2079b71000c709d239b58d21c2cfbff835b8d189 (diff)
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
Diffstat (limited to 'dir.c')
-rw-r--r--dir.c28
1 files changed, 25 insertions, 3 deletions
diff --git a/dir.c b/dir.c
index 9a53c71..6d7a85b 100644
--- a/dir.c
+++ b/dir.c
@@ -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:
* dir.each { |filename| block } -> dir
@@ -797,11 +805,17 @@ dir_read(VALUE dir)
static VALUE
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 dirent *dp;
IF_NORMALIZE_UTF8PATH(int norm_p);
- RETURN_ENUMERATOR(dir, 0, 0);
GetDIR(dir, dirp);
rewinddir(dirp->dir);
IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp->dir, RSTRING_PTR(dirp->path)));
@@ -817,7 +831,7 @@ dir_each(VALUE dir)
else
#endif
path = rb_external_str_new_with_enc(name, namlen, dirp->enc);
- rb_yield(path);
+ (*each)(arg, path);
if (dirp->dir == NULL) dir_closed();
}
return dir;
@@ -2610,6 +2624,14 @@ dir_foreach(int argc, VALUE *argv, VALUE io)
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:
* Dir.entries( dirname ) -> array
@@ -2631,7 +2653,7 @@ dir_entries(int argc, VALUE *argv, VALUE io)
VALUE dir;
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