From 62e3add3ab90a3a6f643bf9c2504bd8f986c0642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Mon, 12 Jun 2017 17:33:25 +0200 Subject: [PATCH] Fix a number of issues related to mixing different path types. --- source/vibe/core/path.d | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/source/vibe/core/path.d b/source/vibe/core/path.d index f4ebc7a..4a7c830 100644 --- a/source/vibe/core/path.d +++ b/source/vibe/core/path.d @@ -36,7 +36,7 @@ Path relativeTo(Path path, Path base_path) size_t base = commonPrefix(path[], base_path[]).length; auto ret = Path("../".replicate(base_path.length - base), path.type) ~ path[base .. $]; - if (path.endsWithSlash && !ret.endsWithSlash) ret ~= Path("./"); + if (path.endsWithSlash && !ret.endsWithSlash) ret ~= Path("./", path.type); return ret; } @@ -233,7 +233,7 @@ struct Path { if (m_type == type) return m_path; if (type == PathType.windows) { - auto ret = this[].map!(p => p.toString()).join('\\'); + auto ret = (absolute ? this[1 .. $] : this[]).map!(p => p.toString()).join('\\'); if (endsWithSlash) ret ~= '\\'; return ret; } else { @@ -277,14 +277,14 @@ struct Path { assert(!subpath.absolute || m_path.length == 0, "Cannot append absolute path."); if (this.endsWithSlash) - return Path(m_path ~ subpath.m_path, m_type); - if (!m_path.length) return subpath; + return Path(m_path ~ subpath.toString(m_type), m_type); + if (!m_path.length) return subpath.m_type == m_type ? subpath : Path(subpath.toString(m_type), m_type); final switch (m_type) { case PathType.inet, PathType.posix: - return Path(m_path ~ '/' ~ subpath.m_path, m_type); + return Path(m_path ~ '/' ~ subpath.toString(m_type), m_type); case PathType.windows: - return Path(m_path ~ '\\' ~ subpath.m_path, m_type); + return Path(m_path ~ '\\' ~ subpath.toString(m_type), m_type); } } @@ -451,6 +451,14 @@ unittest assert(p2.relativeTo(p2).toString() == "./"); } + assert(Path("C:\\Windows", PathType.windows).absolute); + assert(Path("C:\\Windows", PathType.windows).toString(PathType.inet) == "/C:/Windows"); + assert((Path("C:\\Windows", PathType.windows) ~ Path("test/this", PathType.inet)).toString() == "C:\\Windows\\test\\this"); + assert(Path("/C:/Windows", PathType.inet).absolute); + assert(Path("/C:/Windows", PathType.inet).toString(PathType.windows) == "C:\\Windows"); + assert((Path("/C:/Windows", PathType.inet) ~ Path("test\\this", PathType.windows)).toString() == "/C:/Windows/test/this"); + assert((Path("", PathType.inet) ~ Path("foo\\bar", PathType.windows)).toString() == "foo/bar"); + assert(Path("").empty); assert(Path("a/b/c")[1 .. 3].map!(p => p.toString()).equal(["b", "c"]));