Skip to content

Commit e55367f

Browse files
committed
Chomp prefix in DirEntry instead of DirIteratorImpl
1 parent 147bc37 commit e55367f

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

std/file.d

+23-10
Original file line numberDiff line numberDiff line change
@@ -3687,6 +3687,12 @@ assert(de2.name == "/usr/share/include");
36873687
+/
36883688
@property string name() const return scope;
36893689

3690+
/++
3691+
Returns the path to the file represented by this `DirEntry`.
3692+
3693+
Unlike `name`, this property returns the internal name as-is,
3694+
potentially starting with an unexpected prefix.
3695+
+/
36903696
@property string nameWithPrefix() const return scope;
36913697

36923698
/++
@@ -3881,6 +3887,11 @@ else version (Windows)
38813887
return _name;
38823888
}
38833889

3890+
private @property string namePrefix() const pure nothrow return scope
3891+
{
3892+
return _namePrefix;
3893+
}
3894+
38843895
@property bool isDir() const pure nothrow scope
38853896
{
38863897
return (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
@@ -4013,6 +4024,11 @@ else version (Posix)
40134024
return _name;
40144025
}
40154026

4027+
private @property string namePrefix() const pure nothrow return scope
4028+
{
4029+
return _namePrefix;
4030+
}
4031+
40164032
@property bool isDir() scope
40174033
{
40184034
_ensureStatOrLStatDone();
@@ -4675,7 +4691,7 @@ private struct DirIteratorImpl
46754691
DirEntry _cur;
46764692
DirHandle[] _stack;
46774693
DirEntry[] _stashed; //used in depth first mode
4678-
string _pathPrefix = null;
4694+
string _namePrefix = null;
46794695

46804696
//stack helpers
46814697
void pushExtra(DirEntry de)
@@ -4733,7 +4749,6 @@ private struct DirIteratorImpl
47334749
bool toNext(bool fetch, scope WIN32_FIND_DATAW* findinfo) @trusted
47344750
{
47354751
import core.stdc.wchar_ : wcscmp;
4736-
import std.string : chompPrefix;
47374752

47384753
if (fetch)
47394754
{
@@ -4750,7 +4765,7 @@ private struct DirIteratorImpl
47504765
popDirStack();
47514766
return false;
47524767
}
4753-
_cur = DirEntry(_stack[$-1].dirpath.chompPrefix(_pathPrefix), findinfo);
4768+
_cur = DirEntry(_stack[$-1].dirpath, findinfo, _namePrefix);
47544769
return true;
47554770
}
47564771

@@ -4795,8 +4810,6 @@ private struct DirIteratorImpl
47954810

47964811
bool next() @trusted
47974812
{
4798-
import std.string : chompPrefix;
4799-
48004813
if (_stack.length == 0)
48014814
return false;
48024815

@@ -4806,7 +4819,7 @@ private struct DirIteratorImpl
48064819
if (core.stdc.string.strcmp(&fdata.d_name[0], ".") &&
48074820
core.stdc.string.strcmp(&fdata.d_name[0], ".."))
48084821
{
4809-
_cur = DirEntry(_stack[$-1].dirpath.chompPrefix(_pathPrefix), fdata);
4822+
_cur = DirEntry(_stack[$-1].dirpath, fdata, _namePrefix);
48104823
return true;
48114824
}
48124825
}
@@ -4848,7 +4861,7 @@ private struct DirIteratorImpl
48484861
pathname = pathname.absolutePath;
48494862

48504863
const offset = pathnameAbs.length - pathnameRel.length;
4851-
_pathPrefix = pathnameAbs[0 .. offset];
4864+
_namePrefix = pathnameAbs[0 .. offset];
48524865
}
48534866

48544867
if (stepIn(pathname))
@@ -4857,7 +4870,7 @@ private struct DirIteratorImpl
48574870
while (mayStepIn())
48584871
{
48594872
auto thisDir = _cur;
4860-
if (stepIn(_cur.name))
4873+
if (stepIn(_cur.nameWithPrefix))
48614874
{
48624875
pushExtra(thisDir);
48634876
}
@@ -4887,7 +4900,7 @@ private struct DirIteratorImpl
48874900
while (mayStepIn())
48884901
{
48894902
auto thisDir = _cur;
4890-
if (stepIn(_cur.name))
4903+
if (stepIn(_cur.nameWithPrefix))
48914904
{
48924905
pushExtra(thisDir);
48934906
}
@@ -4901,7 +4914,7 @@ private struct DirIteratorImpl
49014914
case SpanMode.breadth:
49024915
if (mayStepIn())
49034916
{
4904-
if (!stepIn(_cur.name))
4917+
if (!stepIn(_cur.nameWithPrefix))
49054918
while (!empty && !next()){}
49064919
}
49074920
else

0 commit comments

Comments
 (0)