From: kevin.deisz@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:73102] [Ruby trunk - Feature #12010] Exclude dot and dotdot from Dir#each
Date: Fri, 22 Jan 2016 03:38:53 +0000 [thread overview]
Message-ID: <redmine.journal-56327.20160122033853.2dd0c09e72ad5706@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-12010.20160120021716@ruby-lang.org
Issue #12010 has been updated by Kevin Deisz.
Seems like a good place for an option in these methods, but default it to include to make migration easier.
----------------------------------------
Feature #12010: Exclude dot and dotdot from Dir#each
https://bugs.ruby-lang.org/issues/12010#change-56327
* Author: Yui NARUSE
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
`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
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)
--
https://bugs.ruby-lang.org/
next prev parent reply other threads:[~2016-01-22 3:05 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-12010.20160120021716@ruby-lang.org>
2016-01-20 2:17 ` [ruby-core:72954] [Ruby trunk - Feature #12010] [Assigned] Exclude dot and dotdot from Dir#each naruse
2016-01-20 2:43 ` [ruby-core:72959] " Eric Wong
2016-01-21 3:26 ` [ruby-core:73008] " Martin J. Dürst
2016-01-21 4:57 ` [ruby-core:73012] " Eric Wong
2016-01-20 2:25 ` [ruby-core:72957] [Ruby trunk - Feature #12010] " naruse
2016-01-20 3:11 ` [ruby-core:72960] " nobu
2016-01-20 10:26 ` [ruby-core:72975] " naruse
2016-01-20 12:34 ` [ruby-core:72978] " eregontp
2016-01-21 2:10 ` [ruby-core:73003] " nobu
2016-01-21 2:28 ` [ruby-core:73005] " naruse
2016-01-21 4:25 ` [ruby-core:73011] " akr
2016-01-21 7:47 ` [ruby-core:73014] " naruse
2016-01-21 7:58 ` [ruby-core:73015] " naruse
2016-01-22 3:38 ` kevin.deisz [this message]
2016-01-23 8:42 ` [ruby-core:73308] " shevegen
2018-03-01 1:13 ` [ruby-core:85871] [Ruby trunk Feature#12010] " nobu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-56327.20160122033853.2dd0c09e72ad5706@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).