From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: poffice@blade.nagaokaut.ac.jp Delivered-To: poffice@blade.nagaokaut.ac.jp Received: from kankan.nagaokaut.ac.jp (smtp.nagaokaut.ac.jp [133.44.2.24]) by blade.nagaokaut.ac.jp (Postfix) with ESMTP id C2D7D19E003B for ; Thu, 21 Jan 2016 16:14:16 +0900 (JST) Received: from voscc.nagaokaut.ac.jp (voscc.nagaokaut.ac.jp [133.44.1.100]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id 3AA9DB5D8AD for ; Thu, 21 Jan 2016 16:47:58 +0900 (JST) Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by voscc.nagaokaut.ac.jp (Postfix) with ESMTP id B6DA818CC7B1 for ; Thu, 21 Jan 2016 16:47:58 +0900 (JST) Received: from [221.186.184.76] (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 8B949120421; Thu, 21 Jan 2016 16:47:56 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from o2.heroku.sendgrid.net (o2.heroku.sendgrid.net [67.228.50.55]) by neon.ruby-lang.org (Postfix) with ESMTPS id A27ED120400 for ; Thu, 21 Jan 2016 16:47:52 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sendgrid.me; h=from:to:references:subject:mime-version:content-type:content-transfer-encoding:list-id; s=smtpapi; bh=2CnQin7k7I6TJ/In2ShEYsxvOkY=; b=sJm6KvHB8vkbGSBrxZ UGTQhyie+Udrd3+rRubZvG4eKr9BLOXuvuaQX6puxIazQNS8DPllJQkiYYYieEDh UT4cU4L0BXWZuybAjqAeJBUPxh2hxQe1sKV2oBHqGL6rv/lQS6WAua93brWIls+m 0++fkfFgK/WLaZL/VaT87OqJ0= Received: by filter0603p1mdw1.sendgrid.net with SMTP id filter0603p1mdw1.17464.56A08D2431 2016-01-21 07:47:48.62988349 +0000 UTC Received: from herokuapp.com (ec2-54-204-122-156.compute-1.amazonaws.com [54.204.122.156]) by ismtpd0004p1iad1.sendgrid.net (SG) with ESMTP id dDsQADWjTaus_lpuLMLZ5w for ; Thu, 21 Jan 2016 07:47:48.759 +0000 (UTC) Date: Thu, 21 Jan 2016 07:47:48 +0000 From: naruse@airemix.jp To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 47624 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 12010 X-Redmine-Issue-Author: naruse X-Redmine-Issue-Assignee: matz X-Redmine-Sender: naruse X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS7qd2F6lMgdtPecwZLDy6Ntp77vWdjOwNEHpZ 5U5wgwMFlV/BSyKQ1s5iDz96h3BVaZJRPKYSa+AlKaMlBWmCdhk/je6MlUaO2jbMTxP2Kq/hO6o0yK QFdEakUOqVtvKkNWCQ5LR2Eu9E2wNmJU9aM6AiAJYN3ldFs4wNfyQaTO2w== X-ML-Name: ruby-core X-Mail-Count: 73014 Subject: [ruby-core:73014] [Ruby trunk - Feature #12010] Exclude dot and dotdot from Dir#each X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #12010 has been updated by Yui NARUSE. Akira Tanaka wrote: > I searched Dir.entries on gems. > > It seems there are size/length invocations on the result of Dir.entries. > > Note that I used https://github.com/akr/gem-codesearch Thanks, I hadn't thought up such use case. It sounds need a treatment at least. Could you check about Dir.foreach? ---------------------------------------- Feature #12010: Exclude dot and dotdot from Dir#each https://bugs.ruby-lang.org/issues/12010#change-56237 * 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/