Skip to content

Commit ca3caf4

Browse files
committed
Generalize dirEntries to take a pred as parameter
1 parent 14f1104 commit ca3caf4

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

std/file.d

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4616,7 +4616,8 @@ enum SpanMode
46164616
["animals", "plants"]));
46174617
}
46184618

4619-
private struct DirIteratorImpl
4619+
private struct DirIteratorImpl(alias pred = (const scope ref DirEntry entry) => true)
4620+
// TODO: if (is(typeof(pred(DirEntry.init)) : bool))
46204621
{
46214622
@safe:
46224623
SpanMode _mode;
@@ -4720,6 +4721,8 @@ private struct DirIteratorImpl
47204721

47214722
bool mayStepIn()
47224723
{
4724+
if (pred(_cur))
4725+
return false;
47234726
return _followSymlink ? _cur.isDir : _cur.isDir && !_cur.isSymlink;
47244727
}
47254728
}
@@ -4863,11 +4866,12 @@ private struct DirIteratorImpl
48634866
}
48644867
}
48654868

4866-
struct DirIterator
4869+
struct DirIterator(alias pred = (const scope ref DirEntry entry) => true)
4870+
// TODO: if (is(typeof(pred(DirEntry.init)) : bool))
48674871
{
48684872
@safe:
48694873
private:
4870-
RefCounted!(DirIteratorImpl, RefCountedAutoInitialize.no) impl;
4874+
RefCounted!(DirIteratorImpl!(pred), RefCountedAutoInitialize.no) impl;
48714875
this(string pathname, SpanMode mode, bool followSymlink) @trusted
48724876
{
48734877
impl = typeof(impl)(pathname, mode, followSymlink);
@@ -4957,7 +4961,12 @@ foreach (d; dFiles)
49574961
+/
49584962
auto dirEntries(string path, SpanMode mode, bool followSymlink = true)
49594963
{
4960-
return DirIterator(path, mode, followSymlink);
4964+
return DirIterator!()(path, mode, followSymlink);
4965+
}
4966+
auto dirEntries(alias pred)(string path, SpanMode mode, bool followSymlink = true)
4967+
// TODO: if (is(typeof(pred(DirEntry.init)) : bool))
4968+
{
4969+
return DirIterator!(pred)(path, mode, followSymlink);
49614970
}
49624971

49634972
/// Duplicate functionality of D1's `std.file.listdir()`:
@@ -5064,7 +5073,7 @@ auto dirEntries(string path, string pattern, SpanMode mode,
50645073
import std.path : globMatch, baseName;
50655074

50665075
bool f(DirEntry de) { return globMatch(baseName(de.name), pattern); }
5067-
return filter!f(DirIterator(path, mode, followSymlink));
5076+
return filter!f(DirIterator!()(path, mode, followSymlink));
50685077
}
50695078

50705079
@safe unittest

0 commit comments

Comments
 (0)