Project

General

Profile

Actions

Feature #12010

closed

Exclude dot and dotdot from Dir#each

Added by naruse (Yui NARUSE) over 8 years ago. Updated over 2 years ago.

Status:
Closed
Target version:
-
[ruby-core:72954]

Description

Dir#each and Dir#read (including Dir.entries, Dir.foreach and other methods) return "." and ".." at first.
But through the all real use case "." and ".." are useless.
How about excluding them?

diff --git a/dir.c b/dir.c
index 193b5be..4c23a2d 100644
--- a/dir.c
+++ b/dir.c
@@ -699,6 +699,8 @@ fundamental_encoding_p(rb_encoding *enc)
 #else
 # define READDIR(dir, enc) readdir((dir))
 #endif
+#define DIR_IS_DOT_OR_DOTDOT(dp) ((dp)->d_name[0] == '.' && \
+	((dp)->d_name[1] == '\0' || ((dp)->d_name[1] == '.' && (dp)->d_name[2] == '\0')))
 
 /*
  *  call-seq:
@@ -720,13 +722,12 @@ dir_read(VALUE dir)
 
     GetDIR(dir, dirp);
     errno = 0;
-    if ((dp = READDIR(dirp->dir, dirp->enc)) != NULL) {
-	return rb_external_str_new_with_enc(dp->d_name, NAMLEN(dp), dirp->enc);
-    }
-    else {
-	if (errno != 0) rb_sys_fail(0);
-	return Qnil;		/* end of stream */
+    while ((dp = READDIR(dirp->dir, dirp->enc)) != NULL) {
+	if (!DIR_IS_DOT_OR_DOTDOT(dp))
+	    return rb_external_str_new_with_enc(dp->d_name, NAMLEN(dp), dirp->enc);
     }
+    if (errno != 0) rb_sys_fail(0);
+    return Qnil;		/* end of stream */
 }
 
 /*
@@ -764,6 +765,7 @@ dir_each(VALUE dir)
 	const char *name = dp->d_name;
 	size_t namlen = NAMLEN(dp);
 	VALUE path;
+	if (DIR_IS_DOT_OR_DOTDOT(dp)) continue;
 #if NORMALIZE_UTF8PATH
 	if (norm_p && has_nonascii(name, namlen) &&
 	    !NIL_P(path = rb_str_normalize_ospath(name, namlen))) {
diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb
index 2690a3f..33f0d44 100644
--- a/test/pathname/test_pathname.rb
+++ b/test/pathname/test_pathname.rb
@@ -1238,7 +1238,7 @@ def test_entries
     with_tmpchdir('rubytest-pathname') {|dir|
       open("a", "w") {}
       open("b", "w") {}
-      assert_equal([Pathname("."), Pathname(".."), Pathname("a"), Pathname("b")], Pathname(".").entries.sort)
+      assert_equal([Pathname("a"), Pathname("b")], Pathname(".").entries.sort)
     }
   end
 
@@ -1248,7 +1248,7 @@ def test_each_entry
       open("b", "w") {}
       a = []
       Pathname(".").each_entry {|v| a << v }
-      assert_equal([Pathname("."), Pathname(".."), Pathname("a"), Pathname("b")], a.sort)
+      assert_equal([Pathname("a"), Pathname("b")], a.sort)
     }
   end
 
@@ -1278,7 +1278,7 @@ def test_opendir
       Pathname(".").opendir {|d|
         d.each {|e| a << e }
       }
-      assert_equal([".", "..", "a", "b"], a.sort)
+      assert_equal(["a", "b"], a.sort)
     }
   end
 
diff --git a/test/ruby/test_dir.rb b/test/ruby/test_dir.rb
index 0cc5a6a..d3f6602 100644
--- a/test/ruby/test_dir.rb
+++ b/test/ruby/test_dir.rb
@@ -186,7 +186,7 @@ def test_glob_recursive
 
   def assert_entries(entries)
     entries.sort!
-    assert_equal(%w(. ..) + ("a".."z").to_a, entries)
+    assert_equal(("a".."z").to_a, entries)
   end
 
   def test_entries

Files

dir-entries-usages.txt (304 KB) dir-entries-usages.txt akr (Akira Tanaka), 01/21/2016 04:23 AM

Related issues 2 (0 open2 closed)

Related to Ruby master - Feature #10121: Dir.empty?Closedmatz (Yukihiro Matsumoto)Actions
Related to Ruby master - Feature #13969: Dir#each_childClosedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0