From 1af1beb248fe8796023f77c1a4b4a326e772ea5c Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Sat, 14 Feb 2026 12:52:51 +0100 Subject: Fix filepath.dir --- core/path/filepath/path.odin | 20 ++++---------------- tests/core/path/filepath/test_core_filepath.odin | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/core/path/filepath/path.odin b/core/path/filepath/path.odin index a325ce4cd..e383e756c 100644 --- a/core/path/filepath/path.odin +++ b/core/path/filepath/path.odin @@ -250,26 +250,14 @@ rel :: proc(base_path, target_path: string, allocator := context.allocator) -> ( then `"."` is returned. */ dir :: proc(path: string, allocator := context.allocator) -> string { - context.allocator = allocator - vol_len := len(volume_name(path)) - when ODIN_OS == .Windows { - if len(path) >= 3 && is_separator(path[2]) { - vol_len += 1 - } - } - vol := path[:vol_len] - i := len(path) - 1 - for i >= vol_len && !is_separator(path[i]) { + for i > 0 && !is_separator(path[i]) { i -= 1 } - dir, dir_err := clean(path[vol_len : i+1], allocator) + res, dir_err := clean(path[:i], allocator) + if dir_err != nil { return "" } - defer delete(dir) - if dir == "." && vol_len > 2 { - return strings.clone(vol) - } - return strings.concatenate({vol, dir}) + return res } diff --git a/tests/core/path/filepath/test_core_filepath.odin b/tests/core/path/filepath/test_core_filepath.odin index 9ca1b676f..61f87ad02 100644 --- a/tests/core/path/filepath/test_core_filepath.odin +++ b/tests/core/path/filepath/test_core_filepath.odin @@ -132,4 +132,23 @@ test_stem :: proc(t: ^testing.T) { for d in short_stem { testing.expect_value(t, filepath.short_stem(d[0]), d[1]) } +} + +@(test) +test_dir :: proc(t: ^testing.T) { + when ODIN_OS == .Windows { + @static dirs := [][2]string{ + {"../bin/css", "..\\bin"}, + {"W:/Odin/odin", "W:\\Odin"}, + } + } else { + @static dirs := [][2]string{ + {"../bin/css", "../bin"}, + {"/bin/usr/", "/bin/usr"}, + } + } + + for d in dirs { + testing.expect_value(t, filepath.dir(d[0], context.temp_allocator), d[1]) + } } \ No newline at end of file -- cgit v1.2.3