- Fix to Path handling c:\foo and \home parent. #2569

This commit is contained in:
Christoffer Lerno
2025-11-08 23:42:47 +01:00
parent ffc65bcbf4
commit 52ececba37
3 changed files with 21 additions and 0 deletions

View File

@@ -399,6 +399,19 @@ fn Path? Path.parent(self)
{ {
if (is_separator(c, self.env)) if (is_separator(c, self.env))
{ {
if (i == 0) return { self.path_string[..0], self.env, null };
if (self.env == WIN32 && i > 1)
{
if (try volume_len = volume_name_len(self.path_string, WIN32))
{
// Handle C:\foo
if (volume_len == i)
{
if (i + 1 == self.path_string.len) return NO_PARENT?;
return { self.path_string[:i + 1], WIN32, null };
}
}
}
return { self.path_string[:i], self.env, null }; return { self.path_string[:i], self.env, null };
} }
} }

View File

@@ -18,6 +18,7 @@
- Error message with hashmap shows "mangled" name instead of original #2562. - Error message with hashmap shows "mangled" name instead of original #2562.
- Passing a compile time type implicitly converted to a typeid would crash instead of producing an error. #2568 - Passing a compile time type implicitly converted to a typeid would crash instead of producing an error. #2568
- Compiler assert with const enum based on vector #2566 - Compiler assert with const enum based on vector #2566
- Fix to `Path` handling `c:\foo` and `\home` parent. #2569
### Stdlib changes ### Stdlib changes

View File

@@ -34,6 +34,13 @@ fn void test_parent()
p = path::new(mem, "/a/b/c", path_env: PathEnv.WIN32)!!; p = path::new(mem, "/a/b/c", path_env: PathEnv.WIN32)!!;
assert(p.parent().str_view()!! == `\a\b`); assert(p.parent().str_view()!! == `\a\b`);
p.free(); p.free();
p = path::new(mem, "/a/", path_env: PathEnv.POSIX)!!;
assert(p.parent().str_view()!! == "/");
p.free();
p = path::new(mem, `C:\foo`, path_env: WIN32)!!;
assert(p.parent().str_view()!! == `C:\`);
assert(@catch(p.parent().parent()));
p.free();
} }
fn void test_path_normalized() => mem::@scoped(tmem) fn void test_path_normalized() => mem::@scoped(tmem)