Skip to content

Commit

Permalink
Generalize dirEntries to take a pred as parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
nordlow committed Oct 5, 2022
1 parent 14f1104 commit ca3caf4
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions std/file.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -4720,6 +4721,8 @@ private struct DirIteratorImpl

bool mayStepIn()
{
if (pred(_cur))
return false;
return _followSymlink ? _cur.isDir : _cur.isDir && !_cur.isSymlink;
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()`:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ca3caf4

Please sign in to comment.