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 (kankan.nagaokaut.ac.jp [133.44.2.24]) by blade.nagaokaut.ac.jp (Postfix) with ESMTP id B403C19E004C for ; Fri, 22 Jan 2016 12:05:20 +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 BBB83B5D90F for ; Fri, 22 Jan 2016 12:39:04 +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 140D618CC7B1 for ; Fri, 22 Jan 2016 12:39:04 +0900 (JST) Received: from [221.186.184.76] (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 9440712059A; Fri, 22 Jan 2016 12:39:03 +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 7AAAA120524 for ; Fri, 22 Jan 2016 12:38:59 +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=IRN8NQ42PlPxJ2dSNdadKazUdz8=; b=bJ4YgMLQs0mOoykch1 /t8oVulM/1pfQANRthM/Nx1OWL7r4JVHD8Y5v1Ow3LQM1Jrnif2xSfKTYNXCwF6/ jE8fq92n3+tejzg6ZPVDpgoRjp7scrMKkDfLjHlrD+ytURl6414QWBjK/wW3p/v0 NBS6m99tUkpjiJ655UFwMdIPQ= Received: by filter0420p1mdw1.sendgrid.net with SMTP id filter0420p1mdw1.8837.56A1A44D54 2016-01-22 03:38:53.887256805 +0000 UTC Received: from herokuapp.com (ec2-54-145-164-0.compute-1.amazonaws.com [54.145.164.0]) by ismtpd0002p1iad1.sendgrid.net (SG) with ESMTP id rV9noc8jQNm6aDw1A1siKA for ; Fri, 22 Jan 2016 03:38:53.877 +0000 (UTC) Date: Fri, 22 Jan 2016 03:38:53 +0000 From: kevin.deisz@gmail.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 47711 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 12010 X-Redmine-Issue-Author: naruse X-Redmine-Issue-Assignee: matz X-Redmine-Sender: kddeisz 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/Ymy4QrNMhiuLXJG8OTL2vJD1yS45OsH7FmBu6ha00IwCum/rRDs4gcXOgifOLZ zArhE5w+Z1xA2AKeP0LDjQe9EDMtfmGAPq5+FVZNviwCshp1R6+uwrNl2TAtEKNQEJpx/71pL1ewWp rFQTPxMa7a/5DKdVcCEkUN7FLuBwqfb14PcNKouef2Uzm9Jn9v0j4lxizw== X-ML-Name: ruby-core X-Mail-Count: 73102 Subject: [ruby-core:73102] [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 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/