From ca3caf4531c96cda25e787a458cd19b6c571e272 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20Nordl=C3=B6w?= Date: Wed, 5 Oct 2022 13:47:07 +0200 Subject: [PATCH] Generalize dirEntries to take a pred as parameter --- std/file.d | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/std/file.d b/std/file.d index d6cac41ef04..b3dc4e8d1a5 100644 --- a/std/file.d +++ b/std/file.d @@ -4616,7 +4616,8 @@ enum SpanMode ["animals", "plants"])); } -private struct DirIteratorImpl +private struct DirIteratorImpl(alias pred = (const scope ref DirEntry entry) => true) +// TODO: if (is(typeof(pred(DirEntry.init)) : bool)) { @safe: SpanMode _mode; @@ -4720,6 +4721,8 @@ private struct DirIteratorImpl bool mayStepIn() { + if (pred(_cur)) + return false; return _followSymlink ? _cur.isDir : _cur.isDir && !_cur.isSymlink; } } @@ -4863,11 +4866,12 @@ private struct DirIteratorImpl } } -struct DirIterator +struct DirIterator(alias pred = (const scope ref DirEntry entry) => true) +// TODO: if (is(typeof(pred(DirEntry.init)) : bool)) { @safe: private: - RefCounted!(DirIteratorImpl, RefCountedAutoInitialize.no) impl; + RefCounted!(DirIteratorImpl!(pred), RefCountedAutoInitialize.no) impl; this(string pathname, SpanMode mode, bool followSymlink) @trusted { impl = typeof(impl)(pathname, mode, followSymlink); @@ -4957,7 +4961,12 @@ foreach (d; dFiles) +/ auto dirEntries(string path, SpanMode mode, bool followSymlink = true) { - return DirIterator(path, mode, followSymlink); + return DirIterator!()(path, mode, followSymlink); +} +auto dirEntries(alias pred)(string path, SpanMode mode, bool followSymlink = true) +// TODO: if (is(typeof(pred(DirEntry.init)) : bool)) +{ + return DirIterator!(pred)(path, mode, followSymlink); } /// Duplicate functionality of D1's `std.file.listdir()`: @@ -5064,7 +5073,7 @@ auto dirEntries(string path, string pattern, SpanMode mode, import std.path : globMatch, baseName; bool f(DirEntry de) { return globMatch(baseName(de.name), pattern); } - return filter!f(DirIterator(path, mode, followSymlink)); + return filter!f(DirIterator!()(path, mode, followSymlink)); } @safe unittest