diff --git a/core/os/path.odin b/core/os/path.odin index 3c22852bf..d3525b504 100644 --- a/core/os/path.odin +++ b/core/os/path.odin @@ -382,25 +382,21 @@ Returns an empty string if there is no stem. e.g: '.gitignore'. Returns an empty string if there's a trailing path separator. */ stem :: proc(path: string) -> string { - if len(path) > 0 { - if is_path_separator(path[len(path) - 1]) { - // NOTE(tetra): Trailing separator - return "" - } else if path[0] == '.' { - return "" - } + // If the last character is a path separator, there is no file. + if is_path_separator(path[len(path) - 1]) { + return "" } - // NOTE(tetra): Get the basename - path := path - if i := strings.last_index_any(path, Path_Separator_Chars); i != -1 { - path = path[i+1:] + // Get the base path. + p := base(path) + if i := strings.last_index_any(p, Path_Separator_Chars); i != -1 { + p = p[i+1:] } - if i := strings.last_index_byte(path, '.'); i != -1 { - return path[:i] + if i := strings.last_index_byte(p, '.'); i != -1 { + return p[:i] } - return path + return p } /* diff --git a/tests/core/path/filepath/test_core_filepath.odin b/tests/core/path/filepath/test_core_filepath.odin index a0de7e831..688259e9c 100644 --- a/tests/core/path/filepath/test_core_filepath.odin +++ b/tests/core/path/filepath/test_core_filepath.odin @@ -109,4 +109,27 @@ delete_split :: proc(s: []string) { delete(part) } delete(s) +} + +@(test) +test_stem :: proc(t: ^testing.T) { + @static stem := [][2]string{ + {"builder.tar.gz", "builder.tar"}, + {"./builder.tar.gz", "builder.tar"}, + {"./builder/", ""}, + } + + @static short_stem := [][2]string{ + {"builder.tar", "builder"}, + {"./builder.tar", "builder"}, + {"./builder/", ""}, + } + + for d in stem { + testing.expect_value(t, filepath.stem(d[0]), d[1]) + } + + for d in short_stem { + testing.expect_value(t, filepath.stem(d[0]), d[1]) + } } \ No newline at end of file