From 0516e5a16f2c5e6e6475370e895962b484c745e4 Mon Sep 17 00:00:00 2001 From: scivision Date: Mon, 7 Oct 2024 18:47:41 -0400 Subject: [PATCH] parent: handle Windows root paths --- +stdlib/parent.m | 6 ++++++ test/TestFilePure.m | 14 +++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/+stdlib/parent.m b/+stdlib/parent.m index e59917e..6b5367a 100644 --- a/+stdlib/parent.m +++ b/+stdlib/parent.m @@ -12,6 +12,12 @@ else % drop duplicated slashes in the parent path p = regexprep(p, "//+", "/"); + + if ispc && any(strlength(p) == [2,3]) && isletter(extractBetween(p, 1, 1) && extractBetween(p, 2, 2) == ":") + % 2 or 3 char drive letter + p = stdlib.root(p); + return + end % have to drop_slash on input to get expected parent path p = strip(stdlib.posix(p), "right", "/"); j = strfind(p, "/"); diff --git a/test/TestFilePure.m b/test/TestFilePure.m index 2fdcb7e..997d051 100644 --- a/test/TestFilePure.m +++ b/test/TestFilePure.m @@ -17,8 +17,8 @@ sub_is_subdir ref_is_subdir -in_parent = {"", ".", "..", "../..", "a/", "a/b", "a/b/", "ab/.parent", "ab/.parent.txt", "a/b/../.parent.txt", "a/////b////c"} -ref_parent = {".", ".", ".", "..", ".", "a", "a", "ab", "ab", "a/b/..", "a/b"} +in_parent +ref_parent in_suffix = {"", "/foo/bar/baz", "/foo/bar/baz/", "foo/bar/baz.txt", "foo/bar/baz.txt.gz", ".stat", ".stat.txt"} ref_suffix = {"", "", "", ".txt", ".gz", ".stat", ".txt"} @@ -37,11 +37,14 @@ end methods (TestParameterDefinition, Static) -function [base_relative_to, other_relative_to, ref_relative_to, ref_proximate_to, in_root, ref_root] = init_relative_to(classToTest) %#ok +function [base_relative_to, other_relative_to, ref_relative_to, ref_proximate_to, in_root, ref_root, in_parent, ref_parent] = init_relative_to(classToTest) %#ok in_root = {"", "a/b", "./a/b", "../a/b", "/etc", "c:/etc"}; ref_root = {"", "", "", "", "/", ""}; +in_parent = {"", ".", "..", "../..", "a/", "a/b", "a/b/", "ab/.parent", "ab/.parent.txt", "a/b/../.parent.txt", "a/////b////c", "c:/", "c:\", "c:/a/b", "c:\a/b"}; +ref_parent = {".", ".", ".", "..", ".", "a", "a", "ab", "ab", "a/b/..", "a/b", ".", ".", "c:/a", "c:\a"}; + if ispc base_relative_to = {'', 'Hello', 'Hello', ... @@ -55,6 +58,11 @@ ref_proximate_to = ref_relative_to; ref_proximate_to{end} = other_relative_to{end}; +ref_root{12} = "c:/"; +ref_root{13} = "c:/"; +ref_root{14} = "c:/a"; +ref_root{15} = "c:/a"; + ref_root{5} = ""; ref_root{6} = "c:/";