Skip to content

Commit eb70e07

Browse files
authored
Merge pull request #422 from vibe-d/swap_path_normalzed
GenericPath cleanup
2 parents 6dce0cb + 754d419 commit eb70e07

File tree

2 files changed

+44
-27
lines changed

2 files changed

+44
-27
lines changed

source/vibe/core/file.d

+1-1
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ private void performListDirectory(ListDirectoryRequest req)
11341134
if (fi.isSymlink && !req.followSymlinks)
11351135
continue;
11361136
try {
1137-
if (!scanRec(path ~ NativePath.Segment2(fi.name)))
1137+
if (!scanRec(path ~ NativePath.Segment(fi.name)))
11381138
return false;
11391139
} catch (Exception e) {}
11401140
}

source/vibe/core/path.d

+43-26
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
*/
88
module vibe.core.path;
99

10-
import std.algorithm.searching : commonPrefix, endsWith, startsWith;
11-
import std.algorithm.comparison : equal, min;
10+
import std.algorithm.searching : any, commonPrefix, endsWith, startsWith;
11+
import std.algorithm.comparison : among, equal, min;
1212
import std.algorithm.iteration : map;
1313
import std.exception : enforce;
1414
import std.range : empty, front, popFront, popFrontExactly, takeExactly;
@@ -65,7 +65,7 @@ Path relativeTo(Path)(in Path path, in Path base_path) @safe
6565
base++;
6666
}
6767

68-
enum up = Path.Segment2("..", Path.defaultSeparator);
68+
enum up = Path.Segment("..", Path.defaultSeparator);
6969
auto ret = Path(base_nodes.map!(p => up).chain(nodes));
7070
if (path.endsWithSlash) {
7171
if (ret.empty) return Path.fromTrustedString("." ~ path.toString()[$-1]);
@@ -216,6 +216,7 @@ struct GenericPath(F) {
216216
alias Format = F;
217217

218218
/// vibe-core 1.x compatibility alias
219+
deprecated("Use `Segment` instead.")
219220
alias Segment2 = Segment;
220221

221222
/** A single path segment.
@@ -524,6 +525,7 @@ struct GenericPath(F) {
524525
}
525526

526527
/// vibe-core 1.x compatibility alias
528+
deprecated("Use `.bySegment` instead.")
527529
alias bySegment2 = bySegment;
528530

529531
/** Iterates over the individual segments of the path.
@@ -665,6 +667,7 @@ struct GenericPath(F) {
665667
}
666668

667669
// vibe-core 1.x compatibility alias
670+
deprecated("Use `.head` instead.")
668671
alias head2 = head;
669672

670673
/// Returns the trailing segment of the path.
@@ -723,26 +726,9 @@ struct GenericPath(F) {
723726

724727
/** Returns the normalized form of the path.
725728
726-
See `normalize` for a full description.
727-
*/
728-
@property GenericPath normalized()
729-
const {
730-
GenericPath ret = this;
731-
ret.normalize();
732-
return ret;
733-
}
734-
735-
unittest {
736-
assert(PosixPath("foo/../bar").normalized == PosixPath("bar"));
737-
assert(PosixPath("foo//./bar/../baz").normalized == PosixPath("foo/baz"));
738-
}
739-
740-
741-
/** Removes any redundant path segments and replaces all separators by the
742-
default one.
743-
744-
The resulting path representation is suitable for basic semantic
745-
comparison to other normalized paths.
729+
This removes any redundant path segments and replaces all separators
730+
by the default one. The resulting path representation is suitable for
731+
basic semantic comparison to other normalized paths.
746732
747733
Note that there are still ways for different normalized paths to
748734
represent the same file. Examples of this are the tilde shortcut to the
@@ -754,10 +740,17 @@ struct GenericPath(F) {
754740
segments ("..") that lead to a path that is a parent path of the
755741
root path.
756742
*/
757-
void normalize()
758-
{
743+
@property GenericPath normalized()
744+
const {
759745
import std.array : appender, join;
760746

747+
// avoid constucting a new path if already normalized
748+
if (!this.bySegment.any!(s => s.encodedName.among("", ".", "..")
749+
|| (s.hasSeparator && s.separator != Format.defaultSeparator)))
750+
{
751+
return this;
752+
}
753+
761754
Segment[] newnodes;
762755
bool got_non_sep = false;
763756
foreach (n; this.bySegment) {
@@ -779,7 +772,31 @@ struct GenericPath(F) {
779772

780773
auto dst = appender!string;
781774
Format.toString(newnodes, dst);
782-
m_path = dst.data;
775+
776+
GenericPath ret;
777+
ret.m_path = dst.data;
778+
return ret;
779+
}
780+
781+
///
782+
unittest {
783+
assert(PosixPath("foo/../bar").normalized == PosixPath("bar"));
784+
assert(PosixPath("foo//./bar/../baz").normalized == PosixPath("foo/baz"));
785+
assert(PosixPath("/foo/../bar").normalized == PosixPath("/bar"));
786+
assert(WindowsPath(`\\PC/c$\foo/../bar/`).normalized == WindowsPath(`\\PC\c$\bar\`));
787+
assert(PosixPath("/foo/bar").normalized == PosixPath("/foo/bar"));
788+
assert(PosixPath("foo/bar/").normalized == PosixPath("foo/bar/"));
789+
}
790+
791+
792+
/** Replaces the path representation with its normalized form.
793+
794+
This is a simple convenience wrapper around the functional style
795+
`.normalized`, which is usually preferable.
796+
*/
797+
void normalize()
798+
{
799+
this.m_path = this.normalized.m_path;
783800
}
784801

785802
///

0 commit comments

Comments
 (0)