From 14bbc4bdece5bd5fff80c4bc0a0a55cd67a35f30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Fri, 15 Jan 2021 10:31:49 +0100 Subject: [PATCH] Add GenericPath.normalized property. --- source/vibe/core/path.d | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/source/vibe/core/path.d b/source/vibe/core/path.d index b3aa794..5303875 100644 --- a/source/vibe/core/path.d +++ b/source/vibe/core/path.d @@ -788,7 +788,7 @@ struct GenericPath(F) { /** Determines if the `parentPath` property is valid. */ - bool hasParentPath() + @property bool hasParentPath() const @nogc { auto b = Format.getBackNode(m_path); return b.length < m_path.length; @@ -800,7 +800,7 @@ struct GenericPath(F) { An `Exception` is thrown if this path has no parent path. Use `hasParentPath` to test this upfront. */ - GenericPath parentPath() + @property GenericPath parentPath() const @nogc { auto b = Format.getBackNode(m_path); static immutable Exception e = new Exception("Path has no parent path"); @@ -808,6 +808,24 @@ struct GenericPath(F) { return GenericPath.fromTrustedString(m_path[0 .. $ - b.length]); } + + /** Returns the normalized form of the path. + + See `normalize` for a full description. + */ + @property GenericPath normalized() + const { + GenericPath ret = this; + ret.normalize(); + return ret; + } + + unittest { + assert(PosixPath("foo/../bar").normalized == PosixPath("bar")); + assert(PosixPath("foo//./bar/../baz").normalized == PosixPath("foo/baz")); + } + + /** Removes any redundant path segments and replaces all separators by the default one.