Skip to content

Commit 62b9c0c

Browse files
committed
Generalize dirEntries to take a DirEntry predicate as parameter
1 parent 29a71ef commit 62b9c0c

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

std/file.d

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4617,7 +4617,8 @@ enum SpanMode
46174617
["animals", "plants"]));
46184618
}
46194619

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

47224723
bool mayStepIn()
47234724
{
4725+
if (pred(_cur))
4726+
return false;
47244727
return _followSymlink ? _cur.isDir : _cur.isDir && !_cur.isSymlink;
47254728
}
47264729
}
@@ -4864,16 +4867,17 @@ private struct DirIteratorImpl
48644867
}
48654868
}
48664869

4870+
struct _DirIterator(bool useDIP1000, alias pred = (const scope ref DirEntry entry) => true)
4871+
// TODO: if (is(typeof(pred(DirEntry.init)) : bool))
48674872
// Must be a template, because the destructor is unsafe or safe depending on
48684873
// whether `-preview=dip1000` is in use. Otherwise, linking errors would
48694874
// result.
4870-
struct _DirIterator(bool useDIP1000)
48714875
{
48724876
static assert(useDIP1000 == dip1000Enabled,
48734877
"Please don't override useDIP1000 to disagree with compiler switch.");
48744878

48754879
private:
4876-
SafeRefCounted!(DirIteratorImpl, RefCountedAutoInitialize.no) impl;
4880+
SafeRefCounted!(DirIteratorImpl!(pred), RefCountedAutoInitialize.no) impl;
48774881

48784882
this(string pathname, SpanMode mode, bool followSymlink) @trusted
48794883
{
@@ -4975,10 +4979,11 @@ foreach (d; dFiles)
49754979

49764980
// For some reason, doing the same alias-to-a-template trick as with DirIterator
49774981
// does not work here.
4978-
auto dirEntries(bool useDIP1000 = dip1000Enabled)
4982+
auto dirEntries(bool useDIP1000 = dip1000Enabled, alias pred = (const scope ref DirEntry entry) => true)
49794983
(string path, SpanMode mode, bool followSymlink = true)
4984+
// TODO: if (is(typeof(pred(DirEntry.init)) : bool))
49804985
{
4981-
return _DirIterator!useDIP1000(path, mode, followSymlink);
4986+
return _DirIterator!(useDIP1000, pred)(path, mode, followSymlink);
49824987
}
49834988

49844989
/// Duplicate functionality of D1's `std.file.listdir()`:
@@ -5087,7 +5092,7 @@ auto dirEntries(bool useDIP1000 = dip1000Enabled)
50875092
import std.path : globMatch, baseName;
50885093

50895094
bool f(DirEntry de) { return globMatch(baseName(de.name), pattern); }
5090-
return filter!f(_DirIterator!useDIP1000(path, mode, followSymlink));
5095+
return filter!f(_DirIterator!(useDIP1000)(path, mode, followSymlink));
50915096
}
50925097

50935098
@safe unittest

0 commit comments

Comments
 (0)