[commit: packages/directory] master: Add getDirectoryContentsA which ignores "." and ".." (07ad46b)

git at git.haskell.org git at git.haskell.org
Fri Dec 18 09:52:48 UTC 2015


Repository : ssh://git@git.haskell.org/directory

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/07ad46b7da730589eee6edbaf06c3f4f074fda70/directory

>---------------------------------------------------------------

commit 07ad46b7da730589eee6edbaf06c3f4f074fda70
Author: Guillaume Bouchard <guillaum.bouchard at gmail.com>
Date:   Fri Sep 25 22:20:04 2015 +0200

    Add getDirectoryContentsA which ignores "." and ".."


>---------------------------------------------------------------

07ad46b7da730589eee6edbaf06c3f4f074fda70
 System/Directory.hs        | 10 ++++++++++
 tests/GetDirContents001.hs |  2 ++
 2 files changed, 12 insertions(+)

diff --git a/System/Directory.hs b/System/Directory.hs
index a1aa8fb..78a9a1d 100644
--- a/System/Directory.hs
+++ b/System/Directory.hs
@@ -30,6 +30,7 @@ module System.Directory
     , removeDirectoryRecursive
     , renameDirectory
     , getDirectoryContents
+    , getDirectoryContentsA
     -- ** Current working directory
     , getCurrentDirectory
     , setCurrentDirectory
@@ -1030,6 +1031,15 @@ getDirectoryContents path =
                  -- no need to reverse, ordering is undefined
 #endif /* mingw32 */
 
+{- | A version of 'getDirectoryContents' which returns /almost all/
+entries, ignoring the current and parent directories, @.@ and @.. at .
+
+-}
+getDirectoryContentsA :: FilePath -> IO [FilePath]
+getDirectoryContentsA path =
+  (filter f) <$> (getDirectoryContents path)
+  where f filename = filename /= "." && filename /= ".."
+
 #endif /* __GLASGOW_HASKELL__ */
 
 
diff --git a/tests/GetDirContents001.hs b/tests/GetDirContents001.hs
index aa7d495..f460bb4 100644
--- a/tests/GetDirContents001.hs
+++ b/tests/GetDirContents001.hs
@@ -11,10 +11,12 @@ main :: TestEnv -> IO ()
 main _t = do
   createDirectory dir
   T(expectEq) () specials . sort =<< getDirectoryContents dir
+  T(expectEq) () [] . sort =<< getDirectoryContentsA dir
   names <- for [1 .. 100 :: Int] $ \ i -> do
     let name = 'f' : show i
     writeFile (dir </> name) ""
     return name
   T(expectEq) () (sort (specials <> names)) . sort =<< getDirectoryContents dir
+  T(expectEq) () (sort (names)) . sort =<< getDirectoryContentsA dir
   where dir      = "dir"
         specials = [".", ".."]



More information about the ghc-commits mailing list